PageRenderTime 37ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/rainloop/v/0.0.0/app/libraries/SabreForRainLoop/CalDAV/CalendarQueryValidator.php

https://gitlab.com/wuhang2003/rainloop-webmail
PHP | 392 lines | 183 code | 82 blank | 127 comment | 44 complexity | 2fd687acdf1c84fd52d579bf4e390b59 MD5 | raw file
  1. <?php
  2. namespace SabreForRainLoop\CalDAV;
  3. use SabreForRainLoop\VObject;
  4. use DateTime;
  5. /**
  6. * CalendarQuery Validator
  7. *
  8. * This class is responsible for checking if an iCalendar object matches a set
  9. * of filters. The main function to do this is 'validate'.
  10. *
  11. * This is used to determine which icalendar objects should be returned for a
  12. * calendar-query REPORT request.
  13. *
  14. * @copyright Copyright (C) 2007-2013 fruux GmbH (https://fruux.com/).
  15. * @author Evert Pot (http://evertpot.com/)
  16. * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
  17. */
  18. class CalendarQueryValidator {
  19. /**
  20. * Verify if a list of filters applies to the calendar data object
  21. *
  22. * The list of filters must be formatted as parsed by \SabreForRainLoop\CalDAV\CalendarQueryParser
  23. *
  24. * @param VObject\Component $vObject
  25. * @param array $filters
  26. * @return bool
  27. */
  28. public function validate(VObject\Component $vObject,array $filters) {
  29. // The top level object is always a component filter.
  30. // We'll parse it manually, as it's pretty simple.
  31. if ($vObject->name !== $filters['name']) {
  32. return false;
  33. }
  34. return
  35. $this->validateCompFilters($vObject, $filters['comp-filters']) &&
  36. $this->validatePropFilters($vObject, $filters['prop-filters']);
  37. }
  38. /**
  39. * This method checks the validity of comp-filters.
  40. *
  41. * A list of comp-filters needs to be specified. Also the parent of the
  42. * component we're checking should be specified, not the component to check
  43. * itself.
  44. *
  45. * @param VObject\Component $parent
  46. * @param array $filters
  47. * @return bool
  48. */
  49. protected function validateCompFilters(VObject\Component $parent, array $filters) {
  50. foreach($filters as $filter) {
  51. $isDefined = isset($parent->$filter['name']);
  52. if ($filter['is-not-defined']) {
  53. if ($isDefined) {
  54. return false;
  55. } else {
  56. continue;
  57. }
  58. }
  59. if (!$isDefined) {
  60. return false;
  61. }
  62. if ($filter['time-range']) {
  63. foreach($parent->$filter['name'] as $subComponent) {
  64. if ($this->validateTimeRange($subComponent, $filter['time-range']['start'], $filter['time-range']['end'])) {
  65. continue 2;
  66. }
  67. }
  68. return false;
  69. }
  70. if (!$filter['comp-filters'] && !$filter['prop-filters']) {
  71. continue;
  72. }
  73. // If there are sub-filters, we need to find at least one component
  74. // for which the subfilters hold true.
  75. foreach($parent->$filter['name'] as $subComponent) {
  76. if (
  77. $this->validateCompFilters($subComponent, $filter['comp-filters']) &&
  78. $this->validatePropFilters($subComponent, $filter['prop-filters'])) {
  79. // We had a match, so this comp-filter succeeds
  80. continue 2;
  81. }
  82. }
  83. // If we got here it means there were sub-comp-filters or
  84. // sub-prop-filters and there was no match. This means this filter
  85. // needs to return false.
  86. return false;
  87. }
  88. // If we got here it means we got through all comp-filters alive so the
  89. // filters were all true.
  90. return true;
  91. }
  92. /**
  93. * This method checks the validity of prop-filters.
  94. *
  95. * A list of prop-filters needs to be specified. Also the parent of the
  96. * property we're checking should be specified, not the property to check
  97. * itself.
  98. *
  99. * @param VObject\Component $parent
  100. * @param array $filters
  101. * @return bool
  102. */
  103. protected function validatePropFilters(VObject\Component $parent, array $filters) {
  104. foreach($filters as $filter) {
  105. $isDefined = isset($parent->$filter['name']);
  106. if ($filter['is-not-defined']) {
  107. if ($isDefined) {
  108. return false;
  109. } else {
  110. continue;
  111. }
  112. }
  113. if (!$isDefined) {
  114. return false;
  115. }
  116. if ($filter['time-range']) {
  117. foreach($parent->$filter['name'] as $subComponent) {
  118. if ($this->validateTimeRange($subComponent, $filter['time-range']['start'], $filter['time-range']['end'])) {
  119. continue 2;
  120. }
  121. }
  122. return false;
  123. }
  124. if (!$filter['param-filters'] && !$filter['text-match']) {
  125. continue;
  126. }
  127. // If there are sub-filters, we need to find at least one property
  128. // for which the subfilters hold true.
  129. foreach($parent->$filter['name'] as $subComponent) {
  130. if(
  131. $this->validateParamFilters($subComponent, $filter['param-filters']) &&
  132. (!$filter['text-match'] || $this->validateTextMatch($subComponent, $filter['text-match']))
  133. ) {
  134. // We had a match, so this prop-filter succeeds
  135. continue 2;
  136. }
  137. }
  138. // If we got here it means there were sub-param-filters or
  139. // text-match filters and there was no match. This means the
  140. // filter needs to return false.
  141. return false;
  142. }
  143. // If we got here it means we got through all prop-filters alive so the
  144. // filters were all true.
  145. return true;
  146. }
  147. /**
  148. * This method checks the validity of param-filters.
  149. *
  150. * A list of param-filters needs to be specified. Also the parent of the
  151. * parameter we're checking should be specified, not the parameter to check
  152. * itself.
  153. *
  154. * @param VObject\Property $parent
  155. * @param array $filters
  156. * @return bool
  157. */
  158. protected function validateParamFilters(VObject\Property $parent, array $filters) {
  159. foreach($filters as $filter) {
  160. $isDefined = isset($parent[$filter['name']]);
  161. if ($filter['is-not-defined']) {
  162. if ($isDefined) {
  163. return false;
  164. } else {
  165. continue;
  166. }
  167. }
  168. if (!$isDefined) {
  169. return false;
  170. }
  171. if (!$filter['text-match']) {
  172. continue;
  173. }
  174. if (version_compare(VObject\Version::VERSION, '3.0.0beta1', '>=')) {
  175. // If there are sub-filters, we need to find at least one parameter
  176. // for which the subfilters hold true.
  177. foreach($parent[$filter['name']]->getParts() as $subParam) {
  178. if($this->validateTextMatch($subParam,$filter['text-match'])) {
  179. // We had a match, so this param-filter succeeds
  180. continue 2;
  181. }
  182. }
  183. } else {
  184. // If there are sub-filters, we need to find at least one parameter
  185. // for which the subfilters hold true.
  186. foreach($parent[$filter['name']] as $subParam) {
  187. if($this->validateTextMatch($subParam,$filter['text-match'])) {
  188. // We had a match, so this param-filter succeeds
  189. continue 2;
  190. }
  191. }
  192. }
  193. // If we got here it means there was a text-match filter and there
  194. // were no matches. This means the filter needs to return false.
  195. return false;
  196. }
  197. // If we got here it means we got through all param-filters alive so the
  198. // filters were all true.
  199. return true;
  200. }
  201. /**
  202. * This method checks the validity of a text-match.
  203. *
  204. * A single text-match should be specified as well as the specific property
  205. * or parameter we need to validate.
  206. *
  207. * @param VObject\Node|string $check Value to check against.
  208. * @param array $textMatch
  209. * @return bool
  210. */
  211. protected function validateTextMatch($check, array $textMatch) {
  212. if ($check instanceof VObject\Node) {
  213. $check = (string)$check;
  214. }
  215. $isMatching = \SabreForRainLoop\DAV\StringUtil::textMatch($check, $textMatch['value'], $textMatch['collation']);
  216. return ($textMatch['negate-condition'] xor $isMatching);
  217. }
  218. /**
  219. * Validates if a component matches the given time range.
  220. *
  221. * This is all based on the rules specified in rfc4791, which are quite
  222. * complex.
  223. *
  224. * @param VObject\Node $component
  225. * @param DateTime $start
  226. * @param DateTime $end
  227. * @return bool
  228. */
  229. protected function validateTimeRange(VObject\Node $component, $start, $end) {
  230. if (is_null($start)) {
  231. $start = new DateTime('1900-01-01');
  232. }
  233. if (is_null($end)) {
  234. $end = new DateTime('3000-01-01');
  235. }
  236. switch($component->name) {
  237. case 'VEVENT' :
  238. case 'VTODO' :
  239. case 'VJOURNAL' :
  240. return $component->isInTimeRange($start, $end);
  241. case 'VALARM' :
  242. // If the valarm is wrapped in a recurring event, we need to
  243. // expand the recursions, and validate each.
  244. //
  245. // Our datamodel doesn't easily allow us to do this straight
  246. // in the VALARM component code, so this is a hack, and an
  247. // expensive one too.
  248. if ($component->parent->name === 'VEVENT' && $component->parent->RRULE) {
  249. // Fire up the iterator!
  250. $it = new VObject\RecurrenceIterator($component->parent->parent, (string)$component->parent->UID);
  251. while($it->valid()) {
  252. $expandedEvent = $it->getEventObject();
  253. // We need to check from these expanded alarms, which
  254. // one is the first to trigger. Based on this, we can
  255. // determine if we can 'give up' expanding events.
  256. $firstAlarm = null;
  257. if ($expandedEvent->VALARM !== null) {
  258. foreach($expandedEvent->VALARM as $expandedAlarm) {
  259. $effectiveTrigger = $expandedAlarm->getEffectiveTriggerTime();
  260. if ($expandedAlarm->isInTimeRange($start, $end)) {
  261. return true;
  262. }
  263. if ((string)$expandedAlarm->TRIGGER['VALUE'] === 'DATE-TIME') {
  264. // This is an alarm with a non-relative trigger
  265. // time, likely created by a buggy client. The
  266. // implication is that every alarm in this
  267. // recurring event trigger at the exact same
  268. // time. It doesn't make sense to traverse
  269. // further.
  270. } else {
  271. // We store the first alarm as a means to
  272. // figure out when we can stop traversing.
  273. if (!$firstAlarm || $effectiveTrigger < $firstAlarm) {
  274. $firstAlarm = $effectiveTrigger;
  275. }
  276. }
  277. }
  278. }
  279. if (is_null($firstAlarm)) {
  280. // No alarm was found.
  281. //
  282. // Or technically: No alarm that will change for
  283. // every instance of the recurrence was found,
  284. // which means we can assume there was no match.
  285. return false;
  286. }
  287. if ($firstAlarm > $end) {
  288. return false;
  289. }
  290. $it->next();
  291. }
  292. return false;
  293. } else {
  294. return $component->isInTimeRange($start, $end);
  295. }
  296. case 'VFREEBUSY' :
  297. throw new \SabreForRainLoop\DAV\Exception\NotImplemented('time-range filters are currently not supported on ' . $component->name . ' components');
  298. case 'COMPLETED' :
  299. case 'CREATED' :
  300. case 'DTEND' :
  301. case 'DTSTAMP' :
  302. case 'DTSTART' :
  303. case 'DUE' :
  304. case 'LAST-MODIFIED' :
  305. return ($start <= $component->getDateTime() && $end >= $component->getDateTime());
  306. default :
  307. throw new \SabreForRainLoop\DAV\Exception\BadRequest('You cannot create a time-range filter on a ' . $component->name . ' component');
  308. }
  309. }
  310. }