6 * CalendarQuery Validator
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'.
11 * This is used to determine which icalendar objects should be returned for a
12 * calendar-query REPORT request.
16 * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
17 * @author Evert Pot (http://www.rooftopsolutions.nl/)
18 * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
20 class Sabre_CalDAV_CalendarQueryValidator {
23 * Verify if a list of filters applies to the calendar data object
25 * The list of filters must be formatted as parsed by Sabre_CalDAV_CalendarQueryParser
27 * @param VObject\Component $vObject
28 * @param array $filters
31 public function validate(VObject\Component $vObject,array $filters) {
33 // The top level object is always a component filter.
34 // We'll parse it manually, as it's pretty simple.
35 if ($vObject->name !== $filters['name']) {
40 $this->validateCompFilters($vObject, $filters['comp-filters']) &&
41 $this->validatePropFilters($vObject, $filters['prop-filters']);
47 * This method checks the validity of comp-filters.
49 * A list of comp-filters needs to be specified. Also the parent of the
50 * component we're checking should be specified, not the component to check
53 * @param VObject\Component $parent
54 * @param array $filters
57 protected function validateCompFilters(VObject\Component $parent, array $filters) {
59 foreach($filters as $filter) {
61 $isDefined = isset($parent->$filter['name']);
63 if ($filter['is-not-defined']) {
76 if ($filter['time-range']) {
77 foreach($parent->$filter['name'] as $subComponent) {
78 if ($this->validateTimeRange($subComponent, $filter['time-range']['start'], $filter['time-range']['end'])) {
85 if (!$filter['comp-filters'] && !$filter['prop-filters']) {
89 // If there are sub-filters, we need to find at least one component
90 // for which the subfilters hold true.
91 foreach($parent->$filter['name'] as $subComponent) {
94 $this->validateCompFilters($subComponent, $filter['comp-filters']) &&
95 $this->validatePropFilters($subComponent, $filter['prop-filters'])) {
96 // We had a match, so this comp-filter succeeds
102 // If we got here it means there were sub-comp-filters or
103 // sub-prop-filters and there was no match. This means this filter
104 // needs to return false.
109 // If we got here it means we got through all comp-filters alive so the
110 // filters were all true.
116 * This method checks the validity of prop-filters.
118 * A list of prop-filters needs to be specified. Also the parent of the
119 * property we're checking should be specified, not the property to check
122 * @param VObject\Component $parent
123 * @param array $filters
126 protected function validatePropFilters(VObject\Component $parent, array $filters) {
128 foreach($filters as $filter) {
130 $isDefined = isset($parent->$filter['name']);
132 if ($filter['is-not-defined']) {
145 if ($filter['time-range']) {
146 foreach($parent->$filter['name'] as $subComponent) {
147 if ($this->validateTimeRange($subComponent, $filter['time-range']['start'], $filter['time-range']['end'])) {
154 if (!$filter['param-filters'] && !$filter['text-match']) {
158 // If there are sub-filters, we need to find at least one property
159 // for which the subfilters hold true.
160 foreach($parent->$filter['name'] as $subComponent) {
163 $this->validateParamFilters($subComponent, $filter['param-filters']) &&
164 (!$filter['text-match'] || $this->validateTextMatch($subComponent, $filter['text-match']))
166 // We had a match, so this prop-filter succeeds
172 // If we got here it means there were sub-param-filters or
173 // text-match filters and there was no match. This means the
174 // filter needs to return false.
179 // If we got here it means we got through all prop-filters alive so the
180 // filters were all true.
186 * This method checks the validity of param-filters.
188 * A list of param-filters needs to be specified. Also the parent of the
189 * parameter we're checking should be specified, not the parameter to check
192 * @param VObject\Property $parent
193 * @param array $filters
196 protected function validateParamFilters(VObject\Property $parent, array $filters) {
198 foreach($filters as $filter) {
200 $isDefined = isset($parent[$filter['name']]);
202 if ($filter['is-not-defined']) {
215 if (!$filter['text-match']) {
219 // If there are sub-filters, we need to find at least one parameter
220 // for which the subfilters hold true.
221 foreach($parent[$filter['name']] as $subParam) {
223 if($this->validateTextMatch($subParam,$filter['text-match'])) {
224 // We had a match, so this param-filter succeeds
230 // If we got here it means there was a text-match filter and there
231 // were no matches. This means the filter needs to return false.
236 // If we got here it means we got through all param-filters alive so the
237 // filters were all true.
243 * This method checks the validity of a text-match.
245 * A single text-match should be specified as well as the specific property
246 * or parameter we need to validate.
248 * @param VObject\Node $parent
249 * @param array $textMatch
252 protected function validateTextMatch(VObject\Node $parent, array $textMatch) {
254 $value = (string)$parent;
256 $isMatching = Sabre_DAV_StringUtil::textMatch($value, $textMatch['value'], $textMatch['collation']);
258 return ($textMatch['negate-condition'] xor $isMatching);
263 * Validates if a component matches the given time range.
265 * This is all based on the rules specified in rfc4791, which are quite
268 * @param VObject\Node $component
269 * @param DateTime $start
270 * @param DateTime $end
273 protected function validateTimeRange(VObject\Node $component, $start, $end) {
275 if (is_null($start)) {
276 $start = new DateTime('1900-01-01');
279 $end = new DateTime('3000-01-01');
282 switch($component->name) {
288 return $component->isInTimeRange($start, $end);
292 // If the valarm is wrapped in a recurring event, we need to
293 // expand the recursions, and validate each.
295 // Our datamodel doesn't easily allow us to do this straight
296 // in the VALARM component code, so this is a hack, and an
297 // expensive one too.
298 if ($component->parent->name === 'VEVENT' && $component->parent->RRULE) {
300 // Fire up the iterator!
301 $it = new VObject\RecurrenceIterator($component->parent->parent, (string)$component->parent->UID);
302 while($it->valid()) {
303 $expandedEvent = $it->getEventObject();
305 // We need to check from these expanded alarms, which
306 // one is the first to trigger. Based on this, we can
307 // determine if we can 'give up' expanding events.
309 if ($expandedEvent->VALARM !== null) {
310 foreach($expandedEvent->VALARM as $expandedAlarm) {
312 $effectiveTrigger = $expandedAlarm->getEffectiveTriggerTime();
313 if ($expandedAlarm->isInTimeRange($start, $end)) {
317 if ((string)$expandedAlarm->TRIGGER['VALUE'] === 'DATE-TIME') {
318 // This is an alarm with a non-relative trigger
319 // time, likely created by a buggy client. The
320 // implication is that every alarm in this
321 // recurring event trigger at the exact same
322 // time. It doesn't make sense to traverse
325 // We store the first alarm as a means to
326 // figure out when we can stop traversing.
327 if (!$firstAlarm || $effectiveTrigger < $firstAlarm) {
328 $firstAlarm = $effectiveTrigger;
333 if (is_null($firstAlarm)) {
334 // No alarm was found.
336 // Or technically: No alarm that will change for
337 // every instance of the recurrence was found,
338 // which means we can assume there was no match.
341 if ($firstAlarm > $end) {
348 return $component->isInTimeRange($start, $end);
352 throw new Sabre_DAV_Exception_NotImplemented('time-range filters are currently not supported on ' . $component->name . ' components');
360 case 'LAST-MODIFIED' :
361 return ($start <= $component->getDateTime() && $end >= $component->getDateTime());
366 throw new Sabre_DAV_Exception_BadRequest('You cannot create a time-range filter on a ' . $component->name . ' component');