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