CalendarSearchReport.php 5.7 KB

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