DAViCal
RRule.php
1<?php
12if ( !class_exists('DateTime') ) return;
13
19function olson_from_vtimezone( vComponent $vtz ) {
20 $tzid = $vtz->GetProperty('TZID');
21 if ( empty($tzid) ) $tzid = $vtz->GetProperty('TZID');
22 if ( !empty($tzid) ) {
23 $result = olson_from_tzstring($tzid);
24 if ( !empty($result) ) return $result;
25 }
26
30 return null;
31}
32
33// define( 'DEBUG_RRULE', true);
34define( 'DEBUG_RRULE', false );
35
39class RepeatRuleTimeZone extends DateTimeZone {
40 private $tz_defined;
41
42 public function __construct($in_dtz = null) {
43 $this->tz_defined = false;
44 if ( !isset($in_dtz) ) return;
45
46 $olson = olson_from_tzstring($in_dtz);
47 if ( isset($olson) ) {
48 try {
49 parent::__construct($olson);
50 $this->tz_defined = $olson;
51 }
52 catch (Exception $e) {
53 dbg_error_log( 'ERROR', 'Could not handle timezone "%s" (%s) - will use floating time', $in_dtz, $olson );
54 parent::__construct('UTC');
55 $this->tz_defined = false;
56 }
57 }
58 else {
59 dbg_error_log( 'ERROR', 'Could not recognize timezone "%s" - will use floating time', $in_dtz );
60 parent::__construct('UTC');
61 $this->tz_defined = false;
62 }
63 }
64
65 function tzid() {
66 if ( $this->tz_defined === false ) return false;
67 $tzid = $this->getName();
68 if ( $tzid != 'UTC' ) return $tzid;
69 return $this->tz_defined;
70 }
71}
72
80 private $epoch_seconds = null;
81 private $days = 0;
82 private $secs = 0;
83 private $as_text = '';
84
89 function __construct( $in_duration ) {
90 if ( is_integer($in_duration) ) {
91 $this->epoch_seconds = $in_duration;
92 $this->as_text = '';
93 }
94 else if ( gettype($in_duration) == 'string' ) {
95// preg_match('{^-?P(\dW)|((\dD)?(T(\dH)?(\dM)?(\dS)?)?)$}i', $in_duration, $matches)
96 $this->as_text = $in_duration;
97 $this->epoch_seconds = null;
98 }
99 else {
100// fatal('Passed duration is neither numeric nor string!');
101 }
102 }
103
109 function equals( $other ) {
110 if ( $this == $other ) return true;
111 if ( $this->asSeconds() == $other->asSeconds() ) return true;
112 return false;
113 }
114
118 function asSeconds() {
119 if ( !isset($this->epoch_seconds) ) {
120 if ( preg_match('{^(-?)P(?:(\d+W)|(?:(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S?)?)?))$}i', $this->as_text, $matches) ) {
121 // @printf("%s - %s - %s - %s - %s - %s\n", $matches[1], $matches[2], $matches[3], $matches[4], $matches[5], $matches[6]);
122 $this->secs = 0;
123 if ( !empty($matches[2]) ) {
124 $this->days = (intval($matches[2]) * 7);
125 }
126 else {
127 if ( !empty($matches[3]) ) $this->days = intval($matches[3]);
128 if ( !empty($matches[4]) ) $this->secs += intval($matches[4]) * 3600;
129 if ( !empty($matches[5]) ) $this->secs += intval($matches[5]) * 60;
130 if ( !empty($matches[6]) ) $this->secs += intval($matches[6]);
131 }
132 if ( $matches[1] == '-' ) {
133 $this->days *= -1;
134 $this->secs *= -1;
135 }
136 $this->epoch_seconds = ($this->days * 86400) + $this->secs;
137 // printf("Duration: %d days & %d seconds (%d epoch seconds)\n", $this->days, $this->secs, $this->epoch_seconds);
138 }
139 else {
140 throw new Exception('Invalid epoch: "'+$this->as_text+"'");
141 }
142 }
143 return $this->epoch_seconds;
144 }
145
146
151 function __toString() {
152 if ( empty($this->as_text) ) {
153 $this->as_text = ($this->epoch_seconds < 0 ? '-P' : 'P');
154 $in_duration = abs($this->epoch_seconds);
155 if ( $in_duration == 0 ) {
156 $this->as_text .= '0D';
157 } elseif ( $in_duration >= 86400 ) {
158 $this->days = floor($in_duration / 86400);
159 $in_duration -= $this->days * 86400;
160 if ( $in_duration == 0 && ($this->days / 7) == floor($this->days / 7) ) {
161 $this->as_text .= ($this->days/7).'W';
162 return $this->as_text;
163 }
164 $this->as_text .= $this->days.'D';
165 }
166 if ( $in_duration > 0 ) {
167 $secs = $in_duration;
168 $this->as_text .= 'T';
169 $hours = floor($in_duration / 3600);
170 if ( $hours > 0 ) $this->as_text .= $hours . 'H';
171 $minutes = floor(($in_duration % 3600) / 60);
172 if ( $minutes > 0 ) $this->as_text .= $minutes . 'M';
173 $seconds = $in_duration % 60;
174 if ( $seconds > 0 ) $this->as_text .= $seconds . 'S';
175 }
176 }
177 return $this->as_text;
178 }
179
180
200 static function fromTwoDates( $d1, $d2 ) {
201 $diff = $d2->epoch() - $d1->epoch();
202 return new Rfc5545Duration($diff);
203 }
204}
205
212class RepeatRuleDateTime extends DateTime {
213 // public static $Format = 'Y-m-d H:i:s';
214 public static $Format = 'c';
215 private static $UTCzone;
216 private $tzid;
217 private $is_date;
218
219 public function __construct($date = null, $dtz = null, $is_date = null ) {
220 if ( !isset(self::$UTCzone) ) self::$UTCzone = new RepeatRuleTimeZone('UTC');
221 $this->is_date = false;
222 if ( isset($is_date) ) $this->is_date = $is_date;
223 if ( !isset($date) ) {
224 $date = date('Ymd\THis');
225 // Floating
226 $dtz = self::$UTCzone;
227 }
228 $this->tzid = null;
229
230 if ( is_object($date) && method_exists($date,'GetParameterValue') ) {
231 $tzid = $date->GetParameterValue('TZID');
232 $actual_date = $date->Value();
233 if ( isset($tzid) ) {
234 $dtz = new RepeatRuleTimeZone($tzid);
235 $this->tzid = $dtz->tzid();
236 }
237 else {
238 $dtz = self::$UTCzone;
239 if ( substr($actual_date,-1) == 'Z' ) {
240 $this->tzid = 'UTC';
241 $actual_date = substr($actual_date, 0, strlen($actual_date) - 1);
242 }
243 }
244 if ( strlen($actual_date) == 8 ) {
245 // We allow dates without VALUE=DATE parameter, but we don't create them like that
246 $this->is_date = true;
247 }
248// $value_type = $date->GetParameterValue('VALUE');
249// if ( isset($value_type) && $value_type == 'DATE' ) $this->is_date = true;
250 $date = $actual_date;
251 if ( DEBUG_RRULE ) printf( "Date%s property%s: %s%s\n", ($this->is_date ? "" : "Time"),
252 (isset($this->tzid) ? ' with timezone' : ''), $date,
253 (isset($this->tzid) ? ' in '.$this->tzid : '') );
254 }
255 elseif (preg_match('/;TZID= ([^:;]+) (?: ;.* )? : ( \d{8} (?:T\d{6})? ) (Z)?/x', $date, $matches) ) {
256 $date = $matches[2];
257 $this->is_date = (strlen($date) == 8);
258 if ( isset($matches[3]) && $matches[3] == 'Z' ) {
259 $dtz = self::$UTCzone;
260 $this->tzid = 'UTC';
261 }
262 else if ( isset($matches[1]) && $matches[1] != '' ) {
263 $dtz = new RepeatRuleTimeZone($matches[1]);
264 $this->tzid = $dtz->tzid();
265 }
266 else {
267 $dtz = self::$UTCzone;
268 $this->tzid = null;
269 }
270 if ( DEBUG_RRULE ) printf( "Date%s property%s: %s%s\n", ($this->is_date ? "" : "Time"),
271 (isset($this->tzid) ? ' with timezone' : ''), $date,
272 (isset($this->tzid) ? ' in '.$this->tzid : '') );
273 }
274 elseif ( ( $dtz === null || $dtz == '' )
275 && preg_match('{;VALUE=DATE (?:;[^:]+) : ((?:[12]\d{3}) (?:0[1-9]|1[012]) (?:0[1-9]|[12]\d|3[01]Z?) )$}x', $date, $matches) ) {
276 $this->is_date = true;
277 $date = $matches[1];
278 // Floating
279 $dtz = self::$UTCzone;
280 $this->tzid = null;
281 if ( DEBUG_RRULE ) printf( "Floating Date value: %s\n", $date );
282 }
283 elseif ( $dtz === null || $dtz == '' ) {
284 $dtz = self::$UTCzone;
285 if ( preg_match('/(\d{8}(T\d{6})?) ?(.*)$/', $date, $matches) ) {
286 $date = $matches[1];
287 if ( $matches[3] == 'Z' ) {
288 $this->tzid = 'UTC';
289 } else {
290 $dtz = new RepeatRuleTimeZone($matches[3]);
291 $this->tzid = $dtz->tzid();
292 }
293 }
294 $this->is_date = (strlen($date) == 8 );
295 if ( DEBUG_RRULE ) printf( "Date%s value with timezone 1: %s in %s\n", ($this->is_date?"":"Time"), $date, $this->tzid );
296 }
297 elseif ( is_string($dtz) ) {
298 $dtz = new RepeatRuleTimeZone($dtz);
299 $this->tzid = $dtz->tzid();
300 $type = gettype($date);
301 if ( DEBUG_RRULE ) printf( "Date%s $type with timezone 2: %s in %s\n", ($this->is_date?"":"Time"), $date, $this->tzid );
302 }
303 else {
304 $this->tzid = $dtz->getName();
305 $type = gettype($date);
306 if ( DEBUG_RRULE ) printf( "Date%s $type with timezone 3: %s in %s\n", ($this->is_date?"":"Time"), $date, $this->tzid );
307 }
308
309 parent::__construct($date, $dtz);
310 if ( isset($is_date) ) $this->is_date = $is_date;
311
312 return $this;
313 }
314
315 public static function withFallbackTzid( $date, $fallback_tzid ) {
316 // Floating times or dates (either VALUE=DATE or with no TZID) can default to the collection's tzid, if one is set
317
318 if ($date->GetParameterValue('VALUE') == 'DATE' && isset($fallback_tzid)) {
319 return new RepeatRuleDateTime($date->Value()."T000000", new RepeatRuleTimeZone($fallback_tzid));
320 } else if ($date->GetParameterValue('TZID') === null && isset($fallback_tzid)) {
321 return new RepeatRuleDateTime($date->Value(), new RepeatRuleTimeZone($fallback_tzid));
322 } else {
323 return new RepeatRuleDateTime($date);
324 }
325 }
326
327
328 public function __toString() {
329 return (string)parent::format(self::$Format) . ' ' . parent::getTimeZone()->getName();
330 }
331
332
333 public function AsDate() {
334 return $this->format('Ymd');
335 }
336
337
338 public function setAsFloat() {
339 unset($this->tzid);
340 }
341
342
343 public function isFloating() {
344 return !isset($this->tzid);
345 }
346
347 public function isDate() {
348 return $this->is_date;
349 }
350
351
352 public function setAsDate() {
353 $this->is_date = true;
354 }
355
356
357 #[\ReturnTypeWillChange]
358 public function modify( $interval ) {
359// print ">>$interval<<\n";
360 if ( preg_match('{^(-)?P(([0-9-]+)W)?(([0-9-]+)D)?T?(([0-9-]+)H)?(([0-9-]+)M)?(([0-9-]+)S)?$}', $interval, $matches) ) {
361 $minus = (isset($matches[1])?$matches[1]:'');
362 $interval = '';
363 if ( isset($matches[2]) && $matches[2] != '' ) $interval .= $minus . $matches[3] . ' weeks ';
364 if ( isset($matches[4]) && $matches[4] != '' ) $interval .= $minus . $matches[5] . ' days ';
365 if ( isset($matches[6]) && $matches[6] != '' ) $interval .= $minus . $matches[7] . ' hours ';
366 if ( isset($matches[8]) && $matches[8] != '' ) $interval .= $minus . $matches[9] . ' minutes ';
367 if (isset($matches[10]) &&$matches[10] != '' ) $interval .= $minus . $matches[11] . ' seconds ';
368 }
369// printf( "Modify '%s' by: >>%s<<\n", $this->__toString(), $interval );
370// print_r($this);
371 if ( !isset($interval) || $interval == '' ) $interval = '1 day';
372 if ( parent::format('d') > 28 && strstr($interval,'month') !== false ) {
373 $this->setDate(null,null,28);
374 }
375 parent::modify($interval);
376 return $this->__toString();
377 }
378
379
387 public function UTC($fmt = 'Ymd\THis\Z' ) {
388 $gmt = clone($this);
389 if ( $this->tzid != 'UTC' ) {
390 if ( isset($this->tzid)) {
391 $dtz = parent::getTimezone();
392 }
393 else {
394 $dtz = new DateTimeZone(date_default_timezone_get());
395 }
396 $offset = 0 - $dtz->getOffset($gmt);
397 $gmt->modify( $offset . ' seconds' );
398 }
399 return $gmt->format($fmt);
400 }
401
402
414 public function FloatOrUTC($return_floating_times = false) {
415 $gmt = clone($this);
416 if ( !$return_floating_times && isset($this->tzid) && $this->tzid != 'UTC' ) {
417 $dtz = parent::getTimezone();
418 $offset = 0 - $dtz->getOffset($gmt);
419 $gmt->modify( $offset . ' seconds' );
420 }
421 if ( $this->is_date ) return $gmt->format('Ymd');
422 if ( $return_floating_times ) return $gmt->format('Ymd\THis');
423 return $gmt->format('Ymd\THis') . (!$return_floating_times && isset($this->tzid) ? 'Z' : '');
424 }
425
426
430 public function RFC5545($return_floating_times = false) {
431 $result = '';
432 if ( isset($this->tzid) && $this->tzid != 'UTC' ) {
433 $result = ';TZID='.$this->tzid;
434 }
435 if ( $this->is_date ) {
436 $result .= ';VALUE=DATE:' . $this->format('Ymd');
437 }
438 else {
439 $result .= ':' . $this->format('Ymd\THis');
440 if ( !$return_floating_times && isset($this->tzid) && $this->tzid == 'UTC' ) {
441 $result .= 'Z';
442 }
443 }
444 return $result;
445 }
446
447
448 #[\ReturnTypeWillChange]
449 public function setTimeZone( $tz ) {
450 if ( is_string($tz) ) {
451 $tz = new RepeatRuleTimeZone($tz);
452 $this->tzid = $tz->tzid();
453 }
454 parent::setTimeZone( $tz );
455 return $this;
456 }
457
458
459 #[\ReturnTypeWillChange]
460 public function getTimeZone() {
461 return $this->tzid;
462 }
463
464
470 public static function hasLeapDay($year) {
471 if ( ($year % 4) == 0 && (($year % 100) != 0 || ($year % 400) == 0) ) return 1;
472 return 0;
473 }
474
481 public static function daysInMonth( $year, $month ) {
482 if ($month == 4 || $month == 6 || $month == 9 || $month == 11) return 30;
483 else if ($month != 2) return 31;
484 return 28 + RepeatRuleDateTime::hasLeapDay($year);
485 }
486
487
488 #[\ReturnTypeWillChange]
489 function setDate( $year=null, $month=null, $day=null ) {
490 if ( !isset($year) ) $year = parent::format('Y');
491 if ( !isset($month) ) $month = parent::format('m');
492 if ( !isset($day) ) $day = parent::format('d');
493 if ( $day < 0 ) {
494 $day += RepeatRuleDateTime::daysInMonth($year, $month) + 1;
495 }
496 parent::setDate( $year , $month , $day );
497 return $this;
498 }
499
500 function setYearDay( $yearday ) {
501 if ( $yearday > 0 ) {
502 $current_yearday = parent::format('z') + 1;
503 }
504 else {
505 $current_yearday = (parent::format('z') - (365 + parent::format('L')));
506 }
507 $diff = $yearday - $current_yearday;
508 if ( $diff < 0 ) $this->modify('-P'.-$diff.'D');
509 else if ( $diff > 0 ) $this->modify('P'.$diff.'D');
510// printf( "Current: %d, Looking for: %d, Diff: %d, What we got: %s (%d,%d)\n", $current_yearday, $yearday, $diff,
511// parent::format('Y-m-d'), (parent::format('z')+1), ((parent::format('z') - (365 + parent::format('L')))) );
512 return $this;
513 }
514
515 function year() {
516 return parent::format('Y');
517 }
518
519 function month() {
520 return parent::format('m');
521 }
522
523 function day() {
524 return parent::format('d');
525 }
526
527 function hour() {
528 return parent::format('H');
529 }
530
531 function minute() {
532 return parent::format('i');
533 }
534
535 function second() {
536 return parent::format('s');
537 }
538
539 function epoch() {
540 return parent::format('U');
541 }
542}
543
544
552 public $from;
553 public $until;
554
564 function __construct( $date1, $date2 ) {
565 if ( $date1 != null && $date2 != null && $date1 > $date2 ) {
566 $this->from = $date2;
567 $this->until = $date1;
568 }
569 else {
570 $this->from = $date1;
571 $this->until = $date2;
572 }
573 }
574
580 function overlaps( RepeatRuleDateRange $other ) {
581 if ( ($this->until == null && $this->from == null) || ($other->until == null && $other->from == null ) ) return true;
582 if ( $this->until == null && $other->until == null ) return true;
583 if ( $this->from == null && $other->from == null ) return true;
584
585 if ( $this->until == null ) return ($other->until > $this->from);
586 if ( $this->from == null ) return ($other->from < $this->until);
587 if ( $other->until == null ) return ($this->until > $other->from);
588 if ( $other->from == null ) return ($this->from < $other->until);
589
590 return !( $this->until < $other->from || $this->from > $other->until );
591 }
592
599 function getDuration() {
600 if ( !isset($this->from) ) return null;
601 if ( $this->from->isDate() && !isset($this->until) )
602 $duration = 'P1D';
603 else if ( !isset($this->until) )
604 $duration = 'P0D';
605 else
606 $duration = ( $this->until->epoch() - $this->from->epoch() );
607 return new Rfc5545Duration( $duration );
608 }
609}
610
611
620
621 private $base;
622 private $until;
623 private $freq;
624 private $count;
625 private $interval;
626 private $bysecond;
627 private $byminute;
628 private $byhour;
629 private $bymonthday;
630 private $byyearday;
631 private $byweekno;
632 private $byday;
633 private $bymonth;
634 private $bysetpos;
635 private $wkst;
636
637 private $instances;
638 private $position;
639 private $finished;
640 private $current_base;
641 private $original_rule;
642
643
644 public function __construct( $basedate, $rrule, $is_date=null, $return_floating_times=false ) {
645 if ( $return_floating_times ) $basedate->setAsFloat();
646 $this->base = (is_object($basedate) ? $basedate : new RepeatRuleDateTime($basedate) );
647 $this->original_rule = $rrule;
648
649 if ( DEBUG_RRULE ) {
650 printf( "Constructing RRULE based on: '%s', rrule: '%s' (float: %s)\n", $basedate, $rrule, ($return_floating_times ? "yes" : "no") );
651 }
652
653 if ( preg_match('{FREQ=([A-Z]+)(;|$)}', $rrule, $m) ) $this->freq = $m[1];
654
655 if ( preg_match('{UNTIL=([0-9TZ]+)(;|$)}', $rrule, $m) )
656 $this->until = new RepeatRuleDateTime($m[1],$this->base->getTimeZone(),$is_date);
657 if ( preg_match('{COUNT=([0-9]+)(;|$)}', $rrule, $m) ) $this->count = $m[1];
658 if ( preg_match('{INTERVAL=([0-9]+)(;|$)}', $rrule, $m) ) $this->interval = $m[1];
659
660 if ( preg_match('{WKST=(MO|TU|WE|TH|FR|SA|SU)(;|$)}', $rrule, $m) ) $this->wkst = $m[1];
661
662 if ( preg_match('{BYDAY=(([+-]?[0-9]{0,2}(MO|TU|WE|TH|FR|SA|SU),?)+)(;|$)}', $rrule, $m) )
663 $this->byday = explode(',',$m[1]);
664
665 if ( preg_match('{BYYEARDAY=([0-9,+-]+)(;|$)}', $rrule, $m) ) $this->byyearday = explode(',',$m[1]);
666 if ( preg_match('{BYWEEKNO=([0-9,+-]+)(;|$)}', $rrule, $m) ) $this->byweekno = explode(',',$m[1]);
667 if ( preg_match('{BYMONTHDAY=([0-9,+-]+)(;|$)}', $rrule, $m) ) $this->bymonthday = explode(',',$m[1]);
668 if ( preg_match('{BYMONTH=(([+-]?[0-1]?[0-9],?)+)(;|$)}', $rrule, $m) ) $this->bymonth = explode(',',$m[1]);
669 if ( preg_match('{BYSETPOS=(([+-]?[0-9]{1,3},?)+)(;|$)}', $rrule, $m) ) $this->bysetpos = explode(',',$m[1]);
670
671 if ( preg_match('{BYSECOND=([0-9,]+)(;|$)}', $rrule, $m) ) $this->bysecond = explode(',',$m[1]);
672 if ( preg_match('{BYMINUTE=([0-9,]+)(;|$)}', $rrule, $m) ) $this->byminute = explode(',',$m[1]);
673 if ( preg_match('{BYHOUR=([0-9,]+)(;|$)}', $rrule, $m) ) $this->byhour = explode(',',$m[1]);
674
675 if ( !isset($this->interval) ) $this->interval = 1;
676 switch( $this->freq ) {
677 case 'SECONDLY': $this->freq_name = 'second'; break;
678 case 'MINUTELY': $this->freq_name = 'minute'; break;
679 case 'HOURLY': $this->freq_name = 'hour'; break;
680 case 'DAILY': $this->freq_name = 'day'; break;
681 case 'WEEKLY': $this->freq_name = 'week'; break;
682 case 'MONTHLY': $this->freq_name = 'month'; break;
683 case 'YEARLY': $this->freq_name = 'year'; break;
684 default:
686 }
687 $this->frequency_string = sprintf('+%d %s', $this->interval, $this->freq_name );
688 if ( DEBUG_RRULE ) printf( "Frequency modify string is: '%s', base is: '%s', TZ: %s\n", $this->frequency_string, $this->base->format('c'), $this->base->getTimeZone() );
689 $this->Start($return_floating_times);
690 }
691
692
697 public function hasLimitedOccurrences() {
698 return ( isset($this->count) || isset($this->until) );
699 }
700
701
702 public function set_timezone( $tzstring ) {
703 $this->base->setTimezone(new DateTimeZone($tzstring));
704 }
705
706
707 public function Start($return_floating_times=false) {
708 $this->instances = array();
709 $this->GetMoreInstances($return_floating_times);
710 $this->rewind();
711 $this->finished = false;
712 }
713
714
715 public function rewind() {
716 $this->position = -1;
717 }
718
719
725 public function next($return_floating_times=false) {
726 $this->position++;
727 return $this->current($return_floating_times);
728 }
729
730
731 public function current($return_floating_times=false) {
732 if ( !$this->valid() ) return null;
733 if ( !isset($this->instances[$this->position]) ) $this->GetMoreInstances($return_floating_times);
734 if ( !$this->valid() ) return null;
735 if ( DEBUG_RRULE ) printf( "Returning date from position %d: %s (%s)\n", $this->position,
736 $this->instances[$this->position]->format('c'), $this->instances[$this->position]->FloatOrUTC($return_floating_times) );
737 return $this->instances[$this->position];
738 }
739
740
741 public function key($return_floating_times=false) {
742 if ( !$this->valid() ) return null;
743 if ( !isset($this->instances[$this->position]) ) $this->GetMoreInstances($return_floating_times);
744 if ( !isset($this->keys[$this->position]) ) {
745 $this->keys[$this->position] = $this->instances[$this->position];
746 }
747 return $this->keys[$this->position];
748 }
749
750
751 public function valid() {
752 if ( DEBUG_RRULE && isset($this->instances[$this->position])) {
753 $current = $this->instances[$this->position];
754 print "TimeZone: " . $current->getTimeZone() . "\n";
755 print "Date: " . $current->format('r') . "\n";
756 print "Errors:\n";
757 print_r($current->getLastErrors());
758 }
759 if ( isset($this->instances[$this->position]) || !$this->finished ) return true;
760 return false;
761 }
762
771 private static function rrule_expand_limit( $freq ) {
772 switch( $freq ) {
773 case 'YEARLY':
774 return array( 'bymonth' => 'expand', 'byweekno' => 'expand', 'byyearday' => 'expand', 'bymonthday' => 'expand',
775 'byday' => 'expand', 'byhour' => 'expand', 'byminute' => 'expand', 'bysecond' => 'expand' );
776 case 'MONTHLY':
777 return array( 'bymonth' => 'limit', 'bymonthday' => 'expand',
778 'byday' => 'expand', 'byhour' => 'expand', 'byminute' => 'expand', 'bysecond' => 'expand' );
779 case 'WEEKLY':
780 return array( 'bymonth' => 'limit',
781 'byday' => 'expand', 'byhour' => 'expand', 'byminute' => 'expand', 'bysecond' => 'expand' );
782 case 'DAILY':
783 return array( 'bymonth' => 'limit', 'bymonthday' => 'limit',
784 'byday' => 'limit', 'byhour' => 'expand', 'byminute' => 'expand', 'bysecond' => 'expand' );
785 case 'HOURLY':
786 return array( 'bymonth' => 'limit', 'bymonthday' => 'limit',
787 'byday' => 'limit', 'byhour' => 'limit', 'byminute' => 'expand', 'bysecond' => 'expand' );
788 case 'MINUTELY':
789 return array( 'bymonth' => 'limit', 'bymonthday' => 'limit',
790 'byday' => 'limit', 'byhour' => 'limit', 'byminute' => 'limit', 'bysecond' => 'expand' );
791 case 'SECONDLY':
792 return array( 'bymonth' => 'limit', 'bymonthday' => 'limit',
793 'byday' => 'limit', 'byhour' => 'limit', 'byminute' => 'limit', 'bysecond' => 'limit' );
794 }
795 dbg_error_log('ERROR','Invalid frequency code "%s" - pretending it is "DAILY"', $freq);
796 return array( 'bymonth' => 'limit', 'bymonthday' => 'limit',
797 'byday' => 'limit', 'byhour' => 'expand', 'byminute' => 'expand', 'bysecond' => 'expand' );
798 }
799
800 private function GetMoreInstances($return_floating_times=false) {
801 if ( $this->finished ) return;
802 $got_more = false;
803 $loop_limit = 10;
804 $loops = 0;
805 if ( $return_floating_times ) $this->base->setAsFloat();
806 while( !$this->finished && !$got_more && $loops++ < $loop_limit ) {
807 if ( !isset($this->current_base) ) {
808 $this->current_base = clone($this->base);
809 }
810 else {
811 $this->current_base->modify( $this->frequency_string );
812 }
813 if ( $return_floating_times ) $this->current_base->setAsFloat();
814 if ( DEBUG_RRULE ) printf( "Getting more instances from: '%s' - %d, TZ: %s\n", $this->current_base->format('c'), count($this->instances), $this->current_base->getTimeZone() );
815 $this->current_set = array( clone($this->current_base) );
816 foreach( self::rrule_expand_limit($this->freq) AS $bytype => $action ) {
817 if ( isset($this->{$bytype}) ) {
818 $this->{$action.'_'.$bytype}();
819 if ( !isset($this->current_set[0]) ) break;
820 }
821 }
822
823 sort($this->current_set);
824 if ( isset($this->bysetpos) ) $this->limit_bysetpos();
825
826 $position = count($this->instances) - 1;
827 if ( DEBUG_RRULE ) printf( "Inserting %d from current_set into position %d\n", count($this->current_set), $position + 1 );
828 foreach( $this->current_set AS $k => $instance ) {
829 if ( $instance < $this->base ) continue;
830 if ( isset($this->until) && $instance > $this->until ) {
831 $this->finished = true;
832 return;
833 }
834 if ( !isset($this->instances[$position]) || $instance != $this->instances[$position] ) {
835 $got_more = true;
836 $position++;
837 if ( isset($this->count) && $position >= $this->count ) {
838 $this->finished = true;
839 return;
840 }
841 $this->instances[$position] = $instance;
842 if ( DEBUG_RRULE ) printf( "Added date %s into position %d in current set\n", $instance->format('c'), $position );
843 }
844 }
845 }
846 }
847
848
849 public static function rrule_day_number( $day ) {
850 switch( $day ) {
851 case 'SU': return 0;
852 case 'MO': return 1;
853 case 'TU': return 2;
854 case 'WE': return 3;
855 case 'TH': return 4;
856 case 'FR': return 5;
857 case 'SA': return 6;
858 }
859 return false;
860 }
861
862
863 static public function date_mask( $date, $y, $mo, $d, $h, $mi, $s ) {
864 $date_parts = explode(',',$date->format('Y,m,d,H,i,s'));
865
866 if ( isset($y) || isset($mo) || isset($d) ) {
867 if ( isset($y) ) $date_parts[0] = $y;
868 if ( isset($mo) ) $date_parts[1] = $mo;
869 if ( isset($d) ) $date_parts[2] = $d;
870 $date->setDate( $date_parts[0], $date_parts[1], $date_parts[2] );
871 }
872 if ( isset($h) || isset($mi) || isset($s) ) {
873 if ( isset($h) ) $date_parts[3] = $h;
874 if ( isset($mi) ) $date_parts[4] = $mi;
875 if ( isset($s) ) $date_parts[5] = $s;
876 $date->setTime( $date_parts[3], $date_parts[4], $date_parts[5] );
877 }
878 return $date;
879 }
880
881
882 private function expand_bymonth() {
883 $instances = $this->current_set;
884 $this->current_set = array();
885 foreach( $instances AS $k => $instance ) {
886 foreach( $this->bymonth AS $k => $month ) {
887 $expanded = $this->date_mask( clone($instance), null, $month, null, null, null, null);
888 if ( DEBUG_RRULE ) printf( "Expanded BYMONTH $month into date %s\n", $expanded->format('c') );
889 $this->current_set[] = $expanded;
890 }
891 }
892 }
893
894 private function expand_bymonthday() {
895 $instances = $this->current_set;
896 $this->current_set = array();
897 foreach( $instances AS $k => $instance ) {
898 foreach( $this->bymonthday AS $k => $monthday ) {
899 $expanded = $this->date_mask( clone($instance), null, null, $monthday, null, null, null);
900 if ($monthday == -1 || $expanded->format('d') == $monthday) {
901 if ( DEBUG_RRULE ) printf( "Expanded BYMONTHDAY $monthday into date %s from %s\n", $expanded->format('c'), $instance->format('c') );
902 $this->current_set[] = $expanded;
903 } else {
904 if ( DEBUG_RRULE ) printf( "Expanded BYMONTHDAY $monthday into date %s from %s, which is not the same day of month, skipping.\n", $expanded->format('c'), $instance->format('c') );
905 }
906 }
907 }
908 }
909
910 private function expand_byyearday() {
911 $instances = $this->current_set;
912 $this->current_set = array();
913 $days_set = array();
914 foreach( $instances AS $k => $instance ) {
915 foreach( $this->byyearday AS $k => $yearday ) {
916 $on_yearday = clone($instance);
917 $on_yearday->setYearDay($yearday);
918 if ( isset($days_set[$on_yearday->UTC()]) ) continue;
919 $this->current_set[] = $on_yearday;
920 $days_set[$on_yearday->UTC()] = true;
921 }
922 }
923 }
924
925 private function expand_byday_in_week( $day_in_week ) {
926
932 $dow_of_instance = $day_in_week->format('w'); // 0 == Sunday
933 foreach( $this->byday AS $k => $weekday ) {
934 $dow = self::rrule_day_number($weekday);
935 $offset = $dow - $dow_of_instance;
936 if ( $offset < 0 ) $offset += 7;
937 $expanded = clone($day_in_week);
938 $expanded->modify( sprintf('+%d day', $offset) );
939 $this->current_set[] = $expanded;
940 if ( DEBUG_RRULE ) printf( "Expanded BYDAY(W) $weekday into date %s\n", $expanded->format('c') );
941 }
942 }
943
944
945 private function expand_byday_in_month( $day_in_month ) {
946
947 $first_of_month = $this->date_mask( clone($day_in_month), null, null, 1, null, null, null);
948 $dow_of_first = $first_of_month->format('w'); // 0 == Sunday
949 $days_in_month = cal_days_in_month(CAL_GREGORIAN, $first_of_month->format('m'), $first_of_month->format('Y'));
950 foreach( $this->byday AS $k => $weekday ) {
951 if ( preg_match('{([+-])?(\d)?(MO|TU|WE|TH|FR|SA|SU)}', $weekday, $matches ) ) {
952 $dow = self::rrule_day_number($matches[3]);
953 $first_dom = 1 + $dow - $dow_of_first; if ( $first_dom < 1 ) $first_dom +=7; // e.g. 1st=WE, dow=MO => 1+1-3=-1 => MO is 6th, etc.
954 $whichweek = intval($matches[2]);
955 if ( DEBUG_RRULE ) printf( "Expanding BYDAY(M) $weekday in month of %s\n", $first_of_month->format('c') );
956 if ( $whichweek > 0 ) {
957 $whichweek--;
958 $monthday = $first_dom;
959 if ( $matches[1] == '-' ) {
960 $monthday += 35;
961 while( $monthday > $days_in_month ) $monthday -= 7;
962 $monthday -= (7 * $whichweek);
963 }
964 else {
965 $monthday += (7 * $whichweek);
966 }
967 if ( $monthday > 0 && $monthday <= $days_in_month ) {
968 $expanded = $this->date_mask( clone($day_in_month), null, null, $monthday, null, null, null);
969 if ( DEBUG_RRULE ) printf( "Expanded BYDAY(M) $weekday now $monthday into date %s\n", $expanded->format('c') );
970 $this->current_set[] = $expanded;
971 }
972 }
973 else {
974 for( $monthday = $first_dom; $monthday <= $days_in_month; $monthday += 7 ) {
975 $expanded = $this->date_mask( clone($day_in_month), null, null, $monthday, null, null, null);
976 if ( DEBUG_RRULE ) printf( "Expanded BYDAY(M) $weekday now $monthday into date %s\n", $expanded->format('c') );
977 $this->current_set[] = $expanded;
978 }
979 }
980 }
981 }
982 }
983
984
985 private function expand_byday_in_year( $day_in_year ) {
986
987 $first_of_year = $this->date_mask( clone($day_in_year), null, 1, 1, null, null, null);
988 $dow_of_first = $first_of_year->format('w'); // 0 == Sunday
989 $days_in_year = 337 + cal_days_in_month(CAL_GREGORIAN, 2, $first_of_year->format('Y'));
990 foreach( $this->byday AS $k => $weekday ) {
991 if ( preg_match('{([+-])?(\d)?(MO|TU|WE|TH|FR|SA|SU)}', $weekday, $matches ) ) {
992 $expanded = clone($first_of_year);
993 $dow = self::rrule_day_number($matches[3]);
994 $first_doy = 1 + $dow - $dow_of_first; if ( $first_doy < 1 ) $first_doy +=7; // e.g. 1st=WE, dow=MO => 1+1-3=-1 => MO is 6th, etc.
995 $whichweek = intval($matches[2]);
996 if ( DEBUG_RRULE ) printf( "Expanding BYDAY(Y) $weekday from date %s\n", $instance->format('c') );
997 if ( $whichweek > 0 ) {
998 $whichweek--;
999 $yearday = $first_doy;
1000 if ( $matches[1] == '-' ) {
1001 $yearday += 371;
1002 while( $yearday > $days_in_year ) $yearday -= 7;
1003 $yearday -= (7 * $whichweek);
1004 }
1005 else {
1006 $yearday += (7 * $whichweek);
1007 }
1008 if ( $yearday > 0 && $yearday <= $days_in_year ) {
1009 $expanded->modify(sprintf('+%d day', $yearday - 1));
1010 if ( DEBUG_RRULE ) printf( "Expanded BYDAY(Y) $weekday now $yearday into date %s\n", $expanded->format('c') );
1011 $this->current_set[] = $expanded;
1012 }
1013 }
1014 else {
1015 $expanded->modify(sprintf('+%d day', $first_doy - 1));
1016 for( $yearday = $first_doy; $yearday <= $days_in_year; $yearday += 7 ) {
1017 if ( DEBUG_RRULE ) printf( "Expanded BYDAY(Y) $weekday now $yearday into date %s\n", $expanded->format('c') );
1018 $this->current_set[] = clone($expanded);
1019 $expanded->modify('+1 week');
1020 }
1021 }
1022 }
1023 }
1024 }
1025
1026
1027 private function expand_byday() {
1028 if ( !isset($this->current_set[0]) ) return;
1029 if ( $this->freq == 'MONTHLY' || $this->freq == 'YEARLY' ) {
1030 if ( isset($this->bymonthday) || isset($this->byyearday) ) {
1031 $this->limit_byday();
1032 return;
1033 }
1034 }
1035 $instances = $this->current_set;
1036 $this->current_set = array();
1037 foreach( $instances AS $k => $instance ) {
1038 if ( $this->freq == 'MONTHLY' ) {
1039 $this->expand_byday_in_month($instance);
1040 }
1041 else if ( $this->freq == 'WEEKLY' ) {
1042 $this->expand_byday_in_week($instance);
1043 }
1044 else { // YEARLY
1045 if ( isset($this->bymonth) ) {
1046 $this->expand_byday_in_month($instance);
1047 }
1048 else if ( isset($this->byweekno) ) {
1049 $this->expand_byday_in_week($instance);
1050 }
1051 else {
1052 $this->expand_byday_in_year($instance);
1053 }
1054 }
1055
1056 }
1057 }
1058
1059 private function expand_byhour() {
1060 $instances = $this->current_set;
1061 $this->current_set = array();
1062 foreach( $instances AS $k => $instance ) {
1063 foreach( $this->bymonth AS $k => $month ) {
1064 $this->current_set[] = $this->date_mask( clone($instance), null, null, null, $hour, null, null);
1065 }
1066 }
1067 }
1068
1069 private function expand_byminute() {
1070 $instances = $this->current_set;
1071 $this->current_set = array();
1072 foreach( $instances AS $k => $instance ) {
1073 foreach( $this->bymonth AS $k => $month ) {
1074 $this->current_set[] = $this->date_mask( clone($instance), null, null, null, null, $minute, null);
1075 }
1076 }
1077 }
1078
1079 private function expand_bysecond() {
1080 $instances = $this->current_set;
1081 $this->current_set = array();
1082 foreach( $instances AS $k => $instance ) {
1083 foreach( $this->bymonth AS $k => $second ) {
1084 $this->current_set[] = $this->date_mask( clone($instance), null, null, null, null, null, $second);
1085 }
1086 }
1087 }
1088
1089
1090 private function limit_generally( $fmt_char, $element_name ) {
1091 $instances = $this->current_set;
1092 $this->current_set = array();
1093 foreach( $instances AS $k => $instance ) {
1094 foreach( $this->{$element_name} AS $k => $element_value ) {
1095 if ( DEBUG_RRULE ) printf( "Limiting '$fmt_char' on '%s' => '%s' ?=? '%s' ? %s\n", $instance->format('c'), $instance->format($fmt_char), $element_value, ($instance->format($fmt_char) == $element_value ? 'Yes' : 'No') );
1096 if ( $instance->format($fmt_char) == $element_value ) $this->current_set[] = $instance;
1097 }
1098 }
1099 }
1100
1101 private function limit_byday() {
1102 $fmt_char = 'w';
1103 $instances = $this->current_set;
1104 $this->current_set = array();
1105 foreach( $this->byday AS $k => $weekday ) {
1106 $dow = self::rrule_day_number($weekday);
1107 foreach( $instances AS $k => $instance ) {
1108 if ( DEBUG_RRULE ) printf( "Limiting '$fmt_char' on '%s' => '%s' ?=? '%s' (%d) ? %s\n", $instance->format('c'), $instance->format($fmt_char), $weekday, $dow, ($instance->format($fmt_char) == $dow ? 'Yes' : 'No') );
1109 if ( $instance->format($fmt_char) == $dow ) $this->current_set[] = $instance;
1110 }
1111 }
1112 }
1113
1114 private function limit_bymonth() { $this->limit_generally( 'm', 'bymonth' ); }
1115 private function limit_byyearday() { $this->limit_generally( 'z', 'byyearday' ); }
1116 private function limit_bymonthday() { $this->limit_generally( 'd', 'bymonthday' ); }
1117 private function limit_byhour() { $this->limit_generally( 'H', 'byhour' ); }
1118 private function limit_byminute() { $this->limit_generally( 'i', 'byminute' ); }
1119 private function limit_bysecond() { $this->limit_generally( 's', 'bysecond' ); }
1120
1121
1122 private function limit_bysetpos( ) {
1123 $instances = $this->current_set;
1124 $count = count($instances);
1125 $this->current_set = array();
1126 foreach( $this->bysetpos AS $k => $element_value ) {
1127 if ( DEBUG_RRULE ) printf( "Limiting bysetpos %s of %d instances\n", $element_value, $count );
1128 if ( $element_value > 0 ) {
1129 $this->current_set[] = $instances[$element_value - 1];
1130 }
1131 else if ( $element_value < 0 ) {
1132 $this->current_set[] = $instances[$count + $element_value];
1133 }
1134 }
1135 }
1136
1137
1138}
1139
1140
1141
1142require_once("vComponent.php");
1143
1153function rdate_expand( $dtstart, $property, $component, $range_end = null, $is_date=null, $return_floating_times=false ) {
1154 $properties = $component->GetProperties($property);
1155 $expansion = array();
1156 foreach( $properties AS $p ) {
1157 $timezone = $p->GetParameterValue('TZID');
1158 $rdate = $p->Value();
1159 $rdates = explode( ',', $rdate );
1160 foreach( $rdates AS $k => $v ) {
1161 $rdate = new RepeatRuleDateTime( $v, $timezone, $is_date);
1162 if ( $return_floating_times ) $rdate->setAsFloat();
1163 $expansion[$rdate->FloatOrUTC($return_floating_times)] = $component;
1164 if ( $rdate > $range_end ) break;
1165 }
1166 }
1167 return $expansion;
1168}
1169
1170
1181function rrule_expand( $dtstart, $property, $component, $range_end, $is_date=null, $return_floating_times=false, $fallback_tzid=null ) {
1182 global $c;
1183 $expansion = array();
1184
1185 $recur = $component->GetProperty($property);
1186 if ( !isset($recur) ) return $expansion;
1187 $recur = $recur->Value();
1188
1189 $this_start = $component->GetProperty('DTSTART');
1190 if ( isset($this_start) ) {
1191 $this_start = RepeatRuleDateTime::withFallbackTzid($this_start, $fallback_tzid);
1192 }
1193 else {
1194 $this_start = clone($dtstart);
1195 }
1196 if ( $return_floating_times ) $this_start->setAsFloat();
1197
1198// if ( DEBUG_RRULE ) print_r( $this_start );
1199 if ( DEBUG_RRULE ) printf( "RRULE: %s (floating: %s)\n", $recur, ($return_floating_times?"yes":"no") );
1200 $rule = new RepeatRule( $this_start, $recur, $is_date, $return_floating_times );
1201 $i = 0;
1202
1203 if ( !isset($c->rrule_expansion_limit) ) $c->rrule_expansion_limit = 5000;
1204 while( $date = $rule->next($return_floating_times) ) {
1205// if ( DEBUG_RRULE ) printf( "[%3d] %s\n", $i, $date->UTC() );
1206 $expansion[$date->FloatOrUTC($return_floating_times)] = $component;
1207 if ( $date > $range_end ) break;
1208 if ( $i++ >= $c->rrule_expansion_limit ) {
1209 dbg_error_log( 'ERROR', "Hit rrule expansion limit of ".$c->rrule_expansion_limit." on %s %s - increase rrule_expansion_limit in config to avoid events missing from freebusy", $component->GetType(), $component->GetProperty('UID'));
1210 }
1211 }
1212// if ( DEBUG_RRULE ) print_r( $expansion );
1213 return $expansion;
1214}
1215
1216
1228function expand_event_instances( vComponent $vResource, $range_start = null, $range_end = null, $return_floating_times=false, $fallback_tzid=null ) {
1229 global $c;
1230 $components = $vResource->GetComponents();
1231
1232 $clear_instance_props = array(
1233 'DTSTART' => true,
1234 'DUE' => true,
1235 'DTEND' => true
1236 );
1237 if ( empty( $c->expanded_instances_include_rrule ) ) {
1238 $clear_instance_props += array(
1239 'RRULE' => true,
1240 'RDATE' => true,
1241 'EXDATE' => true
1242 );
1243 }
1244
1245 if ( empty($range_start) ) { $range_start = new RepeatRuleDateTime(); $range_start->modify('-6 weeks'); }
1246 if ( empty($range_end) ) {
1247 $range_end = clone($range_start);
1248 $range_end->modify('+6 months');
1249 }
1250
1251 $instances = array();
1252 $expand = false;
1253 $dtstart = null;
1254 $is_date = false;
1255 $has_repeats = false;
1256 $dtstart_type = 'DTSTART';
1257
1258 $components_prefix = [];
1259 $components_base_events = [];
1260 $components_override_events = [];
1261
1262 foreach ($components AS $k => $comp) {
1263 if ( $comp->GetType() != 'VEVENT' && $comp->GetType() != 'VTODO' && $comp->GetType() != 'VJOURNAL' ) {
1264 // Other types of component (such as VTIMEZONE) go first
1265 $components_prefix[] = $comp;
1266 } else if ($comp->GetProperty('RECURRENCE-ID') === null) {
1267 // This is the base event, we need to handle it first
1268 $components_base_events[] = $comp;
1269 } else {
1270 // This is an override of an event instance, handle it last
1271 $components_override_events[] = $comp;
1272 }
1273 }
1274
1275 $components = array_merge($components_prefix, $components_base_events, $components_override_events);
1276
1277 foreach( $components AS $k => $comp ) {
1278 if ( $comp->GetType() != 'VEVENT' && $comp->GetType() != 'VTODO' && $comp->GetType() != 'VJOURNAL' ) {
1279 continue;
1280 }
1281 if ( !isset($dtstart) ) {
1282 $dtstart_prop = $comp->GetProperty($dtstart_type);
1283 if ( !isset($dtstart_prop) && $comp->GetType() != 'VTODO' ) {
1284 $dtstart_type = 'DUE';
1285 $dtstart_prop = $comp->GetProperty($dtstart_type);
1286 }
1287 if ( !isset($dtstart_prop) ) continue;
1288 $dtstart = new RepeatRuleDateTime( $dtstart_prop );
1289 if ( $return_floating_times ) $dtstart->setAsFloat();
1290 if ( DEBUG_RRULE ) printf( "Component is: %s (floating: %s)\n", $comp->GetType(), ($return_floating_times?"yes":"no") );
1291 $is_date = $dtstart->isDate();
1292 $instances[$dtstart->FloatOrUTC($return_floating_times)] = $comp;
1293 $rrule = $comp->GetProperty('RRULE');
1294 $has_repeats = isset($rrule);
1295 }
1296 $p = $comp->GetProperty('RECURRENCE-ID');
1297 if ( isset($p) && $p->Value() != '' ) {
1298 $range = $p->GetParameterValue('RANGE');
1299 $recur_utc = new RepeatRuleDateTime($p);
1300 if ( $is_date ) $recur_utc->setAsDate();
1301 $recur_utc = $recur_utc->FloatOrUTC($return_floating_times);
1302 if ( isset($range) && $range == 'THISANDFUTURE' ) {
1303 foreach( $instances AS $k => $v ) {
1304 if ( DEBUG_RRULE ) printf( "Removing overridden instance at: $k\n" );
1305 if ( $k >= $recur_utc ) unset($instances[$k]);
1306 }
1307 }
1308 else {
1309 unset($instances[$recur_utc]);
1310 // This is a single instance of a recurring event, it can not in itself produce extra instances due to RRULE etc
1311 continue;
1312 }
1313 }
1314 else if ( DEBUG_RRULE ) {
1315 $p = $comp->GetProperty('SUMMARY');
1316 $summary = ( isset($p) ? $p->Value() : 'not set');
1317 $p = $comp->GetProperty('UID');
1318 $uid = ( isset($p) ? $p->Value() : 'not set');
1319 printf( "Processing event '%s' with UID '%s' starting on %s\n",
1320 $summary, $uid, $dtstart->FloatOrUTC($return_floating_times) );
1321 print( "Instances at start");
1322 foreach( $instances AS $k => $v ) {
1323 print ' : '.$k;
1324 }
1325 print "\n";
1326 }
1327 $instances += rrule_expand($dtstart, 'RRULE', $comp, $range_end, null, $return_floating_times, $fallback_tzid);
1328 if ( DEBUG_RRULE ) {
1329 print( "After rrule_expand");
1330 foreach( $instances AS $k => $v ) {
1331 print ' : '.$k;
1332 }
1333 print "\n";
1334 }
1335 $instances += rdate_expand($dtstart, 'RDATE', $comp, $range_end, null, $return_floating_times);
1336 if ( DEBUG_RRULE ) {
1337 print( "After rdate_expand");
1338 foreach( $instances AS $k => $v ) {
1339 print ' : '.$k;
1340 }
1341 print "\n";
1342 }
1343 foreach ( rdate_expand($dtstart, 'EXDATE', $comp, $range_end, null, $return_floating_times) AS $k => $v ) {
1344 unset($instances[$k]);
1345 }
1346 if ( DEBUG_RRULE ) {
1347 print( "After exdate_expand");
1348 foreach( $instances AS $k => $v ) {
1349 print ' : '.$k;
1350 }
1351 print "\n";
1352 }
1353 }
1354
1355 $last_duration = null;
1356 $early_start = null;
1357 $new_components = array();
1358 $start_utc = $range_start->FloatOrUTC($return_floating_times);
1359 $end_utc = $range_end->FloatOrUTC($return_floating_times);
1360 foreach( $instances AS $utc => $comp ) {
1361 if ( $utc > $end_utc ) {
1362 if ( DEBUG_RRULE ) printf( "We're done: $utc is out of the range.\n");
1363 break;
1364 }
1365
1366 $end_type = ($comp->GetType() == 'VTODO' ? 'DUE' : 'DTEND');
1367 $duration = $comp->GetProperty('DURATION');
1368 if ( !isset($duration) || $duration->Value() == '' ) {
1369 $instance_start = $comp->GetProperty($dtstart_type);
1370 $dtsrt = new RepeatRuleDateTime( $instance_start );
1371 if ( $return_floating_times ) $dtsrt->setAsFloat();
1372 $instance_end = $comp->GetProperty($end_type);
1373 if ( isset($instance_end) ) {
1374 $dtend = new RepeatRuleDateTime( $instance_end );
1375 $duration = Rfc5545Duration::fromTwoDates($dtsrt, $dtend);
1376 }
1377 else {
1378 if ( $instance_start->GetParameterValue('VALUE') == 'DATE' ) {
1379 $duration = new Rfc5545Duration('P1D');
1380 }
1381 else {
1382 $duration = new Rfc5545Duration(0);
1383 }
1384 }
1385 }
1386 else {
1387 $duration = new Rfc5545Duration($duration->Value());
1388 }
1389
1390 if ( $utc < $start_utc ) {
1391 if ( isset($early_start) && isset($last_duration) && $duration->equals($last_duration) ) {
1392 if ( $utc < $early_start ) {
1393 if ( DEBUG_RRULE ) printf( "Next please: $utc is before $early_start and before $start_utc.\n");
1394 continue;
1395 }
1396 }
1397 else {
1399 $latest_start = clone($range_start);
1400 $latest_start->modify('-'.$duration);
1401 $early_start = $latest_start->FloatOrUTC($return_floating_times);
1402 $last_duration = $duration;
1403 if ( $utc < $early_start ) {
1404 if ( DEBUG_RRULE ) printf( "Another please: $utc is before $early_start and before $start_utc.\n");
1405 continue;
1406 }
1407 }
1408 }
1409 $component = clone($comp);
1410 $component->ClearProperties( $clear_instance_props );
1411 $component->AddProperty($dtstart_type, $utc, ($is_date ? array('VALUE' => 'DATE') : null) );
1412 $component->AddProperty('DURATION', $duration );
1413 if ( $has_repeats && $dtstart->FloatOrUTC($return_floating_times) != $utc )
1414 $component->AddProperty('RECURRENCE-ID', $utc, ($is_date ? array('VALUE' => 'DATE') : null) );
1415 $new_components[$utc] = $component;
1416 }
1417
1418 // Add overriden instances
1419 foreach( $components AS $k => $comp ) {
1420 $p = $comp->GetProperty('RECURRENCE-ID');
1421 if ( isset($p) && $p->Value() != '') {
1422 $recurrence_id = $p->Value();
1423
1424
1425 $dtstart_prop = $comp->GetProperty('DTSTART');
1426 if ( !isset($dtstart_prop) && $comp->GetType() != 'VTODO' ) {
1427 $dtstart_prop = $comp->GetProperty('DUE');
1428 }
1429
1430 if ( !isset($new_components[$recurrence_id]) && !isset($dtstart_prop) ) continue; // No start: no expansion. Note that we consider 'DUE' to be a start if DTSTART is missing
1431 $dtstart_rrdt = new RepeatRuleDateTime( $dtstart_prop );
1432 $is_date = $dtstart_rrdt->isDate();
1433 if ( $return_floating_times ) $dtstart_rrdt->setAsFloat();
1434 $dtstart = $dtstart_rrdt->FloatOrUTC($return_floating_times);
1435 if ( !isset($new_components[$recurrence_id]) && $dtstart > $end_utc ) continue; // Start after end of range, skip it
1436
1437 $end_type = ($comp->GetType() == 'VTODO' ? 'DUE' : 'DTEND');
1438 $duration = $comp->GetProperty('DURATION');
1439
1440 if ( !isset($duration) || $duration->Value() == '' ) {
1441 $instance_end = $comp->GetProperty($end_type);
1442 if ( isset($instance_end) ) {
1443 $dtend_rrdt = new RepeatRuleDateTime( $instance_end );
1444 if ( $return_floating_times ) $dtend_rrdt->setAsFloat();
1445 $dtend = $dtend_rrdt->FloatOrUTC($return_floating_times);
1446
1447 $comp->AddProperty('DURATION', Rfc5545Duration::fromTwoDates($dtstart_rrdt, $dtend_rrdt) );
1448 }
1449 else {
1450 $dtend = $dtstart + ($is_date ? $dtstart + 86400 : 0 );
1451 }
1452 }
1453 else {
1454 $duration = new Rfc5545Duration($duration->Value());
1455 $dtend = $dtstart + $duration->asSeconds();
1456 }
1457
1458 if ( !isset($new_components[$recurrence_id]) && $dtend < $start_utc ) continue; // End before start of range: skip that too.
1459
1460 if ( DEBUG_RRULE ) printf( "Replacing overridden instance at %s\n", $recurrence_id);
1461 $new_components[$recurrence_id] = $comp;
1462 }
1463 }
1464
1465 $vResource->SetComponents($new_components);
1466
1467 return $vResource;
1468}
1469
1470
1478function getComponentRange(vComponent $comp, $fallback_tzid = null) {
1479 $dtstart_prop = $comp->GetProperty('DTSTART');
1480 $duration_prop = $comp->GetProperty('DURATION');
1481 if ( isset($duration_prop) ) {
1482 if ( !isset($dtstart_prop) ) throw new Exception('Invalid '.$comp->GetType().' containing DURATION without DTSTART', 0);
1483 $dtstart = RepeatRuleDateTime::withFallbackTzid($dtstart_prop, $fallback_tzid);
1484 $dtend = clone($dtstart);
1485 $dtend->modify(new Rfc5545Duration($duration_prop->Value()));
1486 }
1487 else {
1488 $completed_prop = null;
1489 switch ( $comp->GetType() ) {
1490 case 'VEVENT':
1491 if ( !isset($dtstart_prop) ) throw new Exception('Invalid VEVENT without DTSTART', 0);
1492 $dtend_prop = $comp->GetProperty('DTEND');
1493 break;
1494 case 'VTODO':
1495 $completed_prop = $comp->GetProperty('COMPLETED');
1496 $dtend_prop = $comp->GetProperty('DUE');
1497 break;
1498 case 'VJOURNAL':
1499 if ( !isset($dtstart_prop) )
1500 $dtstart_prop = $comp->GetProperty('DTSTAMP');
1501 $dtend_prop = $dtstart_prop;
1502 break;
1503 default:
1504 throw new Exception('getComponentRange cannot handle "'.$comp->GetType().'" components', 0);
1505 }
1506
1507 if ( isset($dtstart_prop) )
1508 $dtstart = RepeatRuleDateTime::withFallbackTzid($dtstart_prop, $fallback_tzid);
1509 else
1510 $dtstart = null;
1511
1512 if ( isset($dtend_prop) )
1513 $dtend = RepeatRuleDateTime::withFallbackTzid($dtend_prop, $fallback_tzid);
1514 else
1515 $dtend = null;
1516
1517 if ( isset($completed_prop) ) {
1518 $completed = RepeatRuleDateTime::withFallbackTzid($completed_prop, $fallback_tzid);
1519 if ( !isset($dtstart) || (isset($dtstart) && $completed < $dtstart) ) $dtstart = $completed;
1520 if ( !isset($dtend) || (isset($dtend) && $completed > $dtend) ) $dtend = $completed;
1521 }
1522 }
1523 return new RepeatRuleDateRange($dtstart, $dtend);
1524}
1525
1535function getVCalendarRange( $vResource, $fallback_tzid = null ) {
1536 $components = $vResource->GetComponents();
1537
1538 $dtstart = null;
1539 $duration = null;
1540 $earliest_start = null;
1541 $latest_end = null;
1542 $has_repeats = false;
1543 foreach( $components AS $k => $comp ) {
1544 if ( $comp->GetType() == 'VTIMEZONE' ) continue;
1545 $range = getComponentRange($comp, $fallback_tzid);
1546 $dtstart = $range->from;
1547 if ( !isset($dtstart) ) continue;
1548 $duration = $range->getDuration();
1549
1550 $rrule = $comp->GetProperty('RRULE');
1551 $limited_occurrences = true;
1552 if ( isset($rrule) ) {
1553 $rule = new RepeatRule($dtstart, $rrule);
1554 $limited_occurrences = $rule->hasLimitedOccurrences();
1555 }
1556
1557 if ( $limited_occurrences ) {
1558 $instances = array();
1559 $instances[$dtstart->FloatOrUTC()] = $dtstart;
1560 if ( !isset($range_end) ) {
1561 $range_end = new RepeatRuleDateTime();
1562 $range_end->modify('+150 years');
1563 }
1564 $instances += rrule_expand($dtstart, 'RRULE', $comp, $range_end, null, false, $fallback_tzid);
1565 $instances += rdate_expand($dtstart, 'RDATE', $comp, $range_end);
1566 foreach ( rdate_expand($dtstart, 'EXDATE', $comp, $range_end) AS $k => $v ) {
1567 unset($instances[$k]);
1568 }
1569 if ( count($instances) < 1 ) {
1570 if ( empty($earliest_start) || $dtstart < $earliest_start ) $earliest_start = $dtstart;
1571 $latest_end = null;
1572 break;
1573 }
1574 $instances = array_keys($instances);
1575 asort($instances);
1576 $first = new RepeatRuleDateTime($instances[0]);
1577 $last = new RepeatRuleDateTime($instances[count($instances)-1]);
1578 $last->modify($duration);
1579 if ( empty($earliest_start) || $first < $earliest_start ) $earliest_start = $first;
1580 if ( empty($latest_end) || $last > $latest_end ) $latest_end = $last;
1581 }
1582 else {
1583 if ( empty($earliest_start) || $dtstart < $earliest_start ) $earliest_start = $dtstart;
1584 $latest_end = null;
1585 break;
1586 }
1587 }
1588
1589 return new RepeatRuleDateRange($earliest_start, $latest_end );
1590}
__construct( $date1, $date2)
Definition: RRule.php:564
overlaps(RepeatRuleDateRange $other)
Definition: RRule.php:580
UTC($fmt='Ymd\THis\Z')
Definition: RRule.php:387
RFC5545($return_floating_times=false)
Definition: RRule.php:430
static daysInMonth( $year, $month)
Definition: RRule.php:481
static hasLeapDay($year)
Definition: RRule.php:470
FloatOrUTC($return_floating_times=false)
Definition: RRule.php:414
hasLimitedOccurrences()
Definition: RRule.php:697
expand_byday()
Definition: RRule.php:1027
static rrule_expand_limit( $freq)
Definition: RRule.php:771
next($return_floating_times=false)
Definition: RRule.php:725
expand_byday_in_week( $day_in_week)
Definition: RRule.php:925
__construct( $basedate, $rrule, $is_date=null, $return_floating_times=false)
Definition: RRule.php:644
__construct( $in_duration)
Definition: RRule.php:89
equals( $other)
Definition: RRule.php:109
static fromTwoDates( $d1, $d2)
Definition: RRule.php:200