You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CalendarSearchReport.php 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\DAV\CalDAV\Search\Xml\Request;
  26. use OCA\DAV\CalDAV\Search\SearchPlugin;
  27. use Sabre\DAV\Exception\BadRequest;
  28. use Sabre\Xml\Reader;
  29. use Sabre\Xml\XmlDeserializable;
  30. /**
  31. * CalendarSearchReport request parser.
  32. *
  33. * This class parses the {urn:ietf:params:xml:ns:caldav}calendar-query
  34. * REPORT, as defined in:
  35. *
  36. * https:// link to standard
  37. */
  38. class CalendarSearchReport implements XmlDeserializable {
  39. /**
  40. * An array with requested properties.
  41. *
  42. * @var array
  43. */
  44. public $properties;
  45. /**
  46. * List of property/component filters.
  47. *
  48. * @var array
  49. */
  50. public $filters;
  51. /**
  52. * @var int
  53. */
  54. public $limit;
  55. /**
  56. * @var int
  57. */
  58. public $offset;
  59. /**
  60. * The deserialize method is called during xml parsing.
  61. *
  62. * This method is called statically, this is because in theory this method
  63. * may be used as a type of constructor, or factory method.
  64. *
  65. * Often you want to return an instance of the current class, but you are
  66. * free to return other data as well.
  67. *
  68. * You are responsible for advancing the reader to the next element. Not
  69. * doing anything will result in a never-ending loop.
  70. *
  71. * If you just want to skip parsing for this element altogether, you can
  72. * just call $reader->next();
  73. *
  74. * $reader->parseInnerTree() will parse the entire sub-tree, and advance to
  75. * the next element.
  76. *
  77. * @param Reader $reader
  78. * @return mixed
  79. */
  80. public static function xmlDeserialize(Reader $reader) {
  81. $elems = $reader->parseInnerTree([
  82. '{http://nextcloud.com/ns}comp-filter' => 'OCA\\DAV\\CalDAV\\Search\\Xml\\Filter\\CompFilter',
  83. '{http://nextcloud.com/ns}prop-filter' => 'OCA\\DAV\\CalDAV\\Search\\Xml\\Filter\\PropFilter',
  84. '{http://nextcloud.com/ns}param-filter' => 'OCA\\DAV\\CalDAV\\Search\\Xml\\Filter\\ParamFilter',
  85. '{http://nextcloud.com/ns}search-term' => 'OCA\\DAV\\CalDAV\\Search\\Xml\\Filter\\SearchTermFilter',
  86. '{http://nextcloud.com/ns}limit' => 'OCA\\DAV\\CalDAV\\Search\\Xml\\Filter\\LimitFilter',
  87. '{http://nextcloud.com/ns}offset' => 'OCA\\DAV\\CalDAV\\Search\\Xml\\Filter\\OffsetFilter',
  88. '{DAV:}prop' => 'Sabre\\Xml\\Element\\KeyValue',
  89. ]);
  90. $newProps = [
  91. 'filters' => [],
  92. 'properties' => [],
  93. 'limit' => null,
  94. 'offset' => null
  95. ];
  96. if (!is_array($elems)) {
  97. $elems = [];
  98. }
  99. foreach ($elems as $elem) {
  100. switch ($elem['name']) {
  101. case '{DAV:}prop':
  102. $newProps['properties'] = array_keys($elem['value']);
  103. break;
  104. case '{' . SearchPlugin::NS_Nextcloud . '}filter':
  105. foreach ($elem['value'] as $subElem) {
  106. if ($subElem['name'] === '{' . SearchPlugin::NS_Nextcloud . '}comp-filter') {
  107. if (!isset($newProps['filters']['comps']) || !is_array($newProps['filters']['comps'])) {
  108. $newProps['filters']['comps'] = [];
  109. }
  110. $newProps['filters']['comps'][] = $subElem['value'];
  111. } elseif ($subElem['name'] === '{' . SearchPlugin::NS_Nextcloud . '}prop-filter') {
  112. if (!isset($newProps['filters']['props']) || !is_array($newProps['filters']['props'])) {
  113. $newProps['filters']['props'] = [];
  114. }
  115. $newProps['filters']['props'][] = $subElem['value'];
  116. } elseif ($subElem['name'] === '{' . SearchPlugin::NS_Nextcloud . '}param-filter') {
  117. if (!isset($newProps['filters']['params']) || !is_array($newProps['filters']['params'])) {
  118. $newProps['filters']['params'] = [];
  119. }
  120. $newProps['filters']['params'][] = $subElem['value'];
  121. } elseif ($subElem['name'] === '{' . SearchPlugin::NS_Nextcloud . '}search-term') {
  122. $newProps['filters']['search-term'] = $subElem['value'];
  123. }
  124. }
  125. break;
  126. case '{' . SearchPlugin::NS_Nextcloud . '}limit':
  127. $newProps['limit'] = $elem['value'];
  128. break;
  129. case '{' . SearchPlugin::NS_Nextcloud . '}offset':
  130. $newProps['offset'] = $elem['value'];
  131. break;
  132. }
  133. }
  134. if (empty($newProps['filters'])) {
  135. throw new BadRequest('The {' . SearchPlugin::NS_Nextcloud . '}filter element is required for this request');
  136. }
  137. $propsOrParamsDefined = (!empty($newProps['filters']['props']) || !empty($newProps['filters']['params']));
  138. $noCompsDefined = empty($newProps['filters']['comps']);
  139. if ($propsOrParamsDefined && $noCompsDefined) {
  140. throw new BadRequest('{' . SearchPlugin::NS_Nextcloud . '}prop-filter or {' . SearchPlugin::NS_Nextcloud . '}param-filter given without any {' . SearchPlugin::NS_Nextcloud . '}comp-filter');
  141. }
  142. if (!isset($newProps['filters']['search-term'])) {
  143. throw new BadRequest('{' . SearchPlugin::NS_Nextcloud . '}search-term is required for this request');
  144. }
  145. if (empty($newProps['filters']['props']) && empty($newProps['filters']['params'])) {
  146. throw new BadRequest('At least one{' . SearchPlugin::NS_Nextcloud . '}prop-filter or {' . SearchPlugin::NS_Nextcloud . '}param-filter is required for this request');
  147. }
  148. $obj = new self();
  149. foreach ($newProps as $key => $value) {
  150. $obj->$key = $value;
  151. }
  152. return $obj;
  153. }
  154. }