SearchPlugin.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\DAV\CalDAV\Search;
  7. use OCA\DAV\CalDAV\CalendarHome;
  8. use OCA\DAV\CalDAV\Search\Xml\Request\CalendarSearchReport;
  9. use OCP\AppFramework\Http;
  10. use Sabre\DAV\Server;
  11. use Sabre\DAV\ServerPlugin;
  12. class SearchPlugin extends ServerPlugin {
  13. public const NS_Nextcloud = 'http://nextcloud.com/ns';
  14. /**
  15. * Reference to SabreDAV server object.
  16. *
  17. * @var \Sabre\DAV\Server
  18. */
  19. protected $server;
  20. /**
  21. * This method should return a list of server-features.
  22. *
  23. * This is for example 'versioning' and is added to the DAV: header
  24. * in an OPTIONS response.
  25. *
  26. * @return string[]
  27. */
  28. public function getFeatures() {
  29. // May have to be changed to be detected
  30. return ['nc-calendar-search'];
  31. }
  32. /**
  33. * Returns a plugin name.
  34. *
  35. * Using this name other plugins will be able to access other plugins
  36. * using Sabre\DAV\Server::getPlugin
  37. *
  38. * @return string
  39. */
  40. public function getPluginName() {
  41. return 'nc-calendar-search';
  42. }
  43. /**
  44. * This initializes the plugin.
  45. *
  46. * This function is called by Sabre\DAV\Server, after
  47. * addPlugin is called.
  48. *
  49. * This method should set up the required event subscriptions.
  50. *
  51. * @param Server $server
  52. */
  53. public function initialize(Server $server) {
  54. $this->server = $server;
  55. $server->on('report', [$this, 'report']);
  56. $server->xml->elementMap['{' . self::NS_Nextcloud . '}calendar-search'] =
  57. CalendarSearchReport::class;
  58. }
  59. /**
  60. * This functions handles REPORT requests specific to CalDAV
  61. *
  62. * @param string $reportName
  63. * @param mixed $report
  64. * @param mixed $path
  65. * @return bool
  66. */
  67. public function report($reportName, $report, $path) {
  68. switch ($reportName) {
  69. case '{' . self::NS_Nextcloud . '}calendar-search':
  70. $this->server->transactionType = 'report-nc-calendar-search';
  71. $this->calendarSearch($report);
  72. return false;
  73. }
  74. }
  75. /**
  76. * Returns a list of reports this plugin supports.
  77. *
  78. * This will be used in the {DAV:}supported-report-set property.
  79. * Note that you still need to subscribe to the 'report' event to actually
  80. * implement them
  81. *
  82. * @param string $uri
  83. * @return array
  84. */
  85. public function getSupportedReportSet($uri) {
  86. $node = $this->server->tree->getNodeForPath($uri);
  87. $reports = [];
  88. if ($node instanceof CalendarHome) {
  89. $reports[] = '{' . self::NS_Nextcloud . '}calendar-search';
  90. }
  91. return $reports;
  92. }
  93. /**
  94. * This function handles the calendar-query REPORT
  95. *
  96. * This report is used by clients to request calendar objects based on
  97. * complex conditions.
  98. *
  99. * @param CalendarSearchReport $report
  100. * @return void
  101. */
  102. private function calendarSearch($report) {
  103. $node = $this->server->tree->getNodeForPath($this->server->getRequestUri());
  104. $depth = $this->server->getHTTPDepth(2);
  105. // The default result is an empty array
  106. $result = [];
  107. // If we're dealing with the calendar home, the calendar home itself is
  108. // responsible for the calendar-query
  109. if ($node instanceof CalendarHome && $depth === 2) {
  110. $nodePaths = $node->calendarSearch($report->filters, $report->limit, $report->offset);
  111. foreach ($nodePaths as $path) {
  112. [$properties] = $this->server->getPropertiesForPath(
  113. $this->server->getRequestUri() . '/' . $path,
  114. $report->properties);
  115. $result[] = $properties;
  116. }
  117. }
  118. $prefer = $this->server->getHTTPPrefer();
  119. $this->server->httpResponse->setStatus(Http::STATUS_MULTI_STATUS);
  120. $this->server->httpResponse->setHeader('Content-Type',
  121. 'application/xml; charset=utf-8');
  122. $this->server->httpResponse->setHeader('Vary', 'Brief,Prefer');
  123. $this->server->httpResponse->setBody(
  124. $this->server->generateMultiStatus($result,
  125. $prefer['return'] === 'minimal'));
  126. }
  127. }