1
0

SearchPlugin.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Georg Ehrke <oc.list@georgehrke.com>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OCA\DAV\CalDAV\Search;
  28. use OCA\DAV\CalDAV\CalendarHome;
  29. use OCA\DAV\CalDAV\Search\Xml\Request\CalendarSearchReport;
  30. use Sabre\DAV\Server;
  31. use Sabre\DAV\ServerPlugin;
  32. class SearchPlugin extends ServerPlugin {
  33. public const NS_Nextcloud = 'http://nextcloud.com/ns';
  34. /**
  35. * Reference to SabreDAV server object.
  36. *
  37. * @var \Sabre\DAV\Server
  38. */
  39. protected $server;
  40. /**
  41. * This method should return a list of server-features.
  42. *
  43. * This is for example 'versioning' and is added to the DAV: header
  44. * in an OPTIONS response.
  45. *
  46. * @return string[]
  47. */
  48. public function getFeatures() {
  49. // May have to be changed to be detected
  50. return ['nc-calendar-search'];
  51. }
  52. /**
  53. * Returns a plugin name.
  54. *
  55. * Using this name other plugins will be able to access other plugins
  56. * using Sabre\DAV\Server::getPlugin
  57. *
  58. * @return string
  59. */
  60. public function getPluginName() {
  61. return 'nc-calendar-search';
  62. }
  63. /**
  64. * This initializes the plugin.
  65. *
  66. * This function is called by Sabre\DAV\Server, after
  67. * addPlugin is called.
  68. *
  69. * This method should set up the required event subscriptions.
  70. *
  71. * @param Server $server
  72. */
  73. public function initialize(Server $server) {
  74. $this->server = $server;
  75. $server->on('report', [$this, 'report']);
  76. $server->xml->elementMap['{' . self::NS_Nextcloud . '}calendar-search'] =
  77. CalendarSearchReport::class;
  78. }
  79. /**
  80. * This functions handles REPORT requests specific to CalDAV
  81. *
  82. * @param string $reportName
  83. * @param mixed $report
  84. * @param mixed $path
  85. * @return bool
  86. */
  87. public function report($reportName, $report, $path) {
  88. switch ($reportName) {
  89. case '{' . self::NS_Nextcloud . '}calendar-search':
  90. $this->server->transactionType = 'report-nc-calendar-search';
  91. $this->calendarSearch($report);
  92. return false;
  93. }
  94. }
  95. /**
  96. * Returns a list of reports this plugin supports.
  97. *
  98. * This will be used in the {DAV:}supported-report-set property.
  99. * Note that you still need to subscribe to the 'report' event to actually
  100. * implement them
  101. *
  102. * @param string $uri
  103. * @return array
  104. */
  105. public function getSupportedReportSet($uri) {
  106. $node = $this->server->tree->getNodeForPath($uri);
  107. $reports = [];
  108. if ($node instanceof CalendarHome) {
  109. $reports[] = '{' . self::NS_Nextcloud . '}calendar-search';
  110. }
  111. return $reports;
  112. }
  113. /**
  114. * This function handles the calendar-query REPORT
  115. *
  116. * This report is used by clients to request calendar objects based on
  117. * complex conditions.
  118. *
  119. * @param Xml\Request\CalendarSearchReport $report
  120. * @return void
  121. */
  122. private function calendarSearch($report) {
  123. $node = $this->server->tree->getNodeForPath($this->server->getRequestUri());
  124. $depth = $this->server->getHTTPDepth(2);
  125. // The default result is an empty array
  126. $result = [];
  127. // If we're dealing with the calendar home, the calendar home itself is
  128. // responsible for the calendar-query
  129. if ($node instanceof CalendarHome && $depth === 2) {
  130. $nodePaths = $node->calendarSearch($report->filters, $report->limit, $report->offset);
  131. foreach ($nodePaths as $path) {
  132. [$properties] = $this->server->getPropertiesForPath(
  133. $this->server->getRequestUri() . '/' . $path,
  134. $report->properties);
  135. $result[] = $properties;
  136. }
  137. }
  138. $prefer = $this->server->getHTTPPrefer();
  139. $this->server->httpResponse->setStatus(207);
  140. $this->server->httpResponse->setHeader('Content-Type',
  141. 'application/xml; charset=utf-8');
  142. $this->server->httpResponse->setHeader('Vary', 'Brief,Prefer');
  143. $this->server->httpResponse->setBody(
  144. $this->server->generateMultiStatus($result,
  145. $prefer['return'] === 'minimal'));
  146. }
  147. }