1
0

SearchPlugin.php 3.7 KB

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