DAViCal
RRule.php
1 <?php
12 if ( !class_exists('DateTime') ) return;
13 
19 function 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);
34 define( 'DEBUG_RRULE', false );
35 
39 class 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 >= 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 
210 class 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 ) printf( "Date%s property%s: %s%s\n", ($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 ) printf( "Date%s property%s: %s%s\n", ($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 ) printf( "Floating Date value: %s\n", $date );
280  }
281  elseif ( $dtz === null || $dtz == '' ) {
282  $dtz = self::$UTCzone;
283  if ( preg_match('/(\d{8}(T\d{6})?)(Z?)/', $date, $matches) ) {
284  $date = $matches[1];
285  $this->tzid = ( $matches[3] == 'Z' ? 'UTC' : null );
286  }
287  $this->is_date = (strlen($date) == 8 );
288  if ( DEBUG_RRULE ) printf( "Date%s value with timezone: %s in %s\n", ($this->is_date?"":"Time"), $date, $this->tzid );
289  }
290  elseif ( is_string($dtz) ) {
291  $dtz = new RepeatRuleTimeZone($dtz);
292  $this->tzid = $dtz->tzid();
293  $type = gettype($date);
294  if ( DEBUG_RRULE ) printf( "Date%s $type with timezone: %s in %s\n", ($this->is_date?"":"Time"), $date, $this->tzid );
295  }
296  else {
297  $this->tzid = $dtz->getName();
298  $type = gettype($date);
299  if ( DEBUG_RRULE ) printf( "Date%s $type with timezone: %s in %s\n", ($this->is_date?"":"Time"), $date, $this->tzid );
300  }
301 
302  parent::__construct($date, $dtz);
303  if ( isset($is_date) ) $this->is_date = $is_date;
304 
305  return $this;
306  }
307 
308  public static function withFallbackTzid( $date, $fallback_tzid ) {
309  // Floating times or dates (either VALUE=DATE or with no TZID) can default to the collection's tzid, if one is set
310 
311  if ($date->GetParameterValue('VALUE') == 'DATE' && isset($fallback_tzid)) {
312  return new RepeatRuleDateTime($date->Value()."T000000", new RepeatRuleTimeZone($fallback_tzid));
313  } else if ($date->GetParameterValue('TZID') === null && isset($fallback_tzid)) {
314  return new RepeatRuleDateTime($date->Value(), new RepeatRuleTimeZone($fallback_tzid));
315  } else {
316  return new RepeatRuleDateTime($date);
317  }
318  }
319 
320 
321  public function __toString() {
322  return (string)parent::format(self::$Format) . ' ' . parent::getTimeZone()->getName();
323  }
324 
325 
326  public function AsDate() {
327  return $this->format('Ymd');
328  }
329 
330 
331  public function setAsFloat() {
332  unset($this->tzid);
333  }
334 
335 
336  public function isFloating() {
337  return !isset($this->tzid);
338  }
339 
340  public function isDate() {
341  return $this->is_date;
342  }
343 
344 
345  public function setAsDate() {
346  $this->is_date = true;
347  }
348 
349 
350  public function modify( $interval ) {
351 // print ">>$interval<<\n";
352  if ( preg_match('{^(-)?P(([0-9-]+)W)?(([0-9-]+)D)?T?(([0-9-]+)H)?(([0-9-]+)M)?(([0-9-]+)S)?$}', $interval, $matches) ) {
353  $minus = (isset($matches[1])?$matches[1]:'');
354  $interval = '';
355  if ( isset($matches[2]) && $matches[2] != '' ) $interval .= $minus . $matches[3] . ' weeks ';
356  if ( isset($matches[4]) && $matches[4] != '' ) $interval .= $minus . $matches[5] . ' days ';
357  if ( isset($matches[6]) && $matches[6] != '' ) $interval .= $minus . $matches[7] . ' hours ';
358  if ( isset($matches[8]) && $matches[8] != '' ) $interval .= $minus . $matches[9] . ' minutes ';
359  if (isset($matches[10]) &&$matches[10] != '' ) $interval .= $minus . $matches[11] . ' seconds ';
360  }
361 // printf( "Modify '%s' by: >>%s<<\n", $this->__toString(), $interval );
362 // print_r($this);
363  if ( !isset($interval) || $interval == '' ) $interval = '1 day';
364  if ( parent::format('d') > 28 && strstr($interval,'month') !== false ) {
365  $this->setDate(null,null,28);
366  }
367  parent::modify($interval);
368  return $this->__toString();
369  }
370 
371 
379  public function UTC($fmt = 'Ymd\THis\Z' ) {
380  $gmt = clone($this);
381  if ( $this->tzid != 'UTC' ) {
382  if ( isset($this->tzid)) {
383  $dtz = parent::getTimezone();
384  }
385  else {
386  $dtz = new DateTimeZone(date_default_timezone_get());
387  }
388  $offset = 0 - $dtz->getOffset($gmt);
389  $gmt->modify( $offset . ' seconds' );
390  }
391  return $gmt->format($fmt);
392  }
393 
394 
406  public function FloatOrUTC($return_floating_times = false) {
407  $gmt = clone($this);
408  if ( !$return_floating_times && isset($this->tzid) && $this->tzid != 'UTC' ) {
409  $dtz = parent::getTimezone();
410  $offset = 0 - $dtz->getOffset($gmt);
411  $gmt->modify( $offset . ' seconds' );
412  }
413  if ( $this->is_date ) return $gmt->format('Ymd');
414  if ( $return_floating_times ) return $gmt->format('Ymd\THis');
415  return $gmt->format('Ymd\THis') . (!$return_floating_times && isset($this->tzid) ? 'Z' : '');
416  }
417 
418 
422  public function RFC5545($return_floating_times = false) {
423  $result = '';
424  if ( isset($this->tzid) && $this->tzid != 'UTC' ) {
425  $result = ';TZID='.$this->tzid;
426  }
427  if ( $this->is_date ) {
428  $result .= ';VALUE=DATE:' . $this->format('Ymd');
429  }
430  else {
431  $result .= ':' . $this->format('Ymd\THis');
432  if ( !$return_floating_times && isset($this->tzid) && $this->tzid == 'UTC' ) {
433  $result .= 'Z';
434  }
435  }
436  return $result;
437  }
438 
439 
440  public function setTimeZone( $tz ) {
441  if ( is_string($tz) ) {
442  $tz = new RepeatRuleTimeZone($tz);
443  $this->tzid = $tz->tzid();
444  }
445  parent::setTimeZone( $tz );
446  return $this;
447  }
448 
449 
450  public function getTimeZone() {
451  return $this->tzid;
452  }
453 
454 
460  public static function hasLeapDay($year) {
461  if ( ($year % 4) == 0 && (($year % 100) != 0 || ($year % 400) == 0) ) return 1;
462  return 0;
463  }
464 
471  public static function daysInMonth( $year, $month ) {
472  if ($month == 4 || $month == 6 || $month == 9 || $month == 11) return 30;
473  else if ($month != 2) return 31;
474  return 28 + RepeatRuleDateTime::hasLeapDay($year);
475  }
476 
477 
478  function setDate( $year=null, $month=null, $day=null ) {
479  if ( !isset($year) ) $year = parent::format('Y');
480  if ( !isset($month) ) $month = parent::format('m');
481  if ( !isset($day) ) $day = parent::format('d');
482  if ( $day < 0 ) {
483  $day += RepeatRuleDateTime::daysInMonth($year, $month) + 1;
484  }
485  parent::setDate( $year , $month , $day );
486  return $this;
487  }
488 
489  function setYearDay( $yearday ) {
490  if ( $yearday > 0 ) {
491  $current_yearday = parent::format('z') + 1;
492  }
493  else {
494  $current_yearday = (parent::format('z') - (365 + parent::format('L')));
495  }
496  $diff = $yearday - $current_yearday;
497  if ( $diff < 0 ) $this->modify('-P'.-$diff.'D');
498  else if ( $diff > 0 ) $this->modify('P'.$diff.'D');
499 // printf( "Current: %d, Looking for: %d, Diff: %d, What we got: %s (%d,%d)\n", $current_yearday, $yearday, $diff,
500 // parent::format('Y-m-d'), (parent::format('z')+1), ((parent::format('z') - (365 + parent::format('L')))) );
501  return $this;
502  }
503 
504  function year() {
505  return parent::format('Y');
506  }
507 
508  function month() {
509  return parent::format('m');
510  }
511 
512  function day() {
513  return parent::format('d');
514  }
515 
516  function hour() {
517  return parent::format('H');
518  }
519 
520  function minute() {
521  return parent::format('i');
522  }
523 
524  function second() {
525  return parent::format('s');
526  }
527 
528  function epoch() {
529  return parent::format('U');
530  }
531 }
532 
533 
541  public $from;
542  public $until;
543 
553  function __construct( $date1, $date2 ) {
554  if ( $date1 != null && $date2 != null && $date1 > $date2 ) {
555  $this->from = $date2;
556  $this->until = $date1;
557  }
558  else {
559  $this->from = $date1;
560  $this->until = $date2;
561  }
562  }
563 
569  function overlaps( RepeatRuleDateRange $other ) {
570  if ( ($this->until == null && $this->from == null) || ($other->until == null && $other->from == null ) ) return true;
571  if ( $this->until == null && $other->until == null ) return true;
572  if ( $this->from == null && $other->from == null ) return true;
573 
574  if ( $this->until == null ) return ($other->until > $this->from);
575  if ( $this->from == null ) return ($other->from < $this->until);
576  if ( $other->until == null ) return ($this->until > $other->from);
577  if ( $other->from == null ) return ($this->from < $other->until);
578 
579  return !( $this->until < $other->from || $this->from > $other->until );
580  }
581 
588  function getDuration() {
589  if ( !isset($this->from) ) return null;
590  if ( $this->from->isDate() && !isset($this->until) )
591  $duration = 'P1D';
592  else if ( !isset($this->until) )
593  $duration = 'P0D';
594  else
595  $duration = ( $this->until->epoch() - $this->from->epoch() );
596  return new Rfc5545Duration( $duration );
597  }
598 }
599 
600 
608 class RepeatRule {
609 
610  private $base;
611  private $until;
612  private $freq;
613  private $count;
614  private $interval;
615  private $bysecond;
616  private $byminute;
617  private $byhour;
618  private $bymonthday;
619  private $byyearday;
620  private $byweekno;
621  private $byday;
622  private $bymonth;
623  private $bysetpos;
624  private $wkst;
625 
626  private $instances;
627  private $position;
628  private $finished;
629  private $current_base;
630  private $original_rule;
631 
632 
633  public function __construct( $basedate, $rrule, $is_date=null, $return_floating_times=false ) {
634  if ( $return_floating_times ) $basedate->setAsFloat();
635  $this->base = (is_object($basedate) ? $basedate : new RepeatRuleDateTime($basedate) );
636  $this->original_rule = $rrule;
637 
638  if ( DEBUG_RRULE ) {
639  printf( "Constructing RRULE based on: '%s', rrule: '%s' (we float: %s)\n", $basedate, $rrule, ($return_floating_times?"yes":"no") );
640  }
641 
642  if ( preg_match('{FREQ=([A-Z]+)(;|$)}', $rrule, $m) ) $this->freq = $m[1];
643 
644  if ( preg_match('{UNTIL=([0-9TZ]+)(;|$)}', $rrule, $m) )
645  $this->until = new RepeatRuleDateTime($m[1],$this->base->getTimeZone(),$is_date);
646  if ( preg_match('{COUNT=([0-9]+)(;|$)}', $rrule, $m) ) $this->count = $m[1];
647  if ( preg_match('{INTERVAL=([0-9]+)(;|$)}', $rrule, $m) ) $this->interval = $m[1];
648 
649  if ( preg_match('{WKST=(MO|TU|WE|TH|FR|SA|SU)(;|$)}', $rrule, $m) ) $this->wkst = $m[1];
650 
651  if ( preg_match('{BYDAY=(([+-]?[0-9]{0,2}(MO|TU|WE|TH|FR|SA|SU),?)+)(;|$)}', $rrule, $m) )
652  $this->byday = explode(',',$m[1]);
653 
654  if ( preg_match('{BYYEARDAY=([0-9,+-]+)(;|$)}', $rrule, $m) ) $this->byyearday = explode(',',$m[1]);
655  if ( preg_match('{BYWEEKNO=([0-9,+-]+)(;|$)}', $rrule, $m) ) $this->byweekno = explode(',',$m[1]);
656  if ( preg_match('{BYMONTHDAY=([0-9,+-]+)(;|$)}', $rrule, $m) ) $this->bymonthday = explode(',',$m[1]);
657  if ( preg_match('{BYMONTH=(([+-]?[0-1]?[0-9],?)+)(;|$)}', $rrule, $m) ) $this->bymonth = explode(',',$m[1]);
658  if ( preg_match('{BYSETPOS=(([+-]?[0-9]{1,3},?)+)(;|$)}', $rrule, $m) ) $this->bysetpos = explode(',',$m[1]);
659 
660  if ( preg_match('{BYSECOND=([0-9,]+)(;|$)}', $rrule, $m) ) $this->bysecond = explode(',',$m[1]);
661  if ( preg_match('{BYMINUTE=([0-9,]+)(;|$)}', $rrule, $m) ) $this->byminute = explode(',',$m[1]);
662  if ( preg_match('{BYHOUR=([0-9,]+)(;|$)}', $rrule, $m) ) $this->byhour = explode(',',$m[1]);
663 
664  if ( !isset($this->interval) ) $this->interval = 1;
665  switch( $this->freq ) {
666  case 'SECONDLY': $this->freq_name = 'second'; break;
667  case 'MINUTELY': $this->freq_name = 'minute'; break;
668  case 'HOURLY': $this->freq_name = 'hour'; break;
669  case 'DAILY': $this->freq_name = 'day'; break;
670  case 'WEEKLY': $this->freq_name = 'week'; break;
671  case 'MONTHLY': $this->freq_name = 'month'; break;
672  case 'YEARLY': $this->freq_name = 'year'; break;
673  default:
675  }
676  $this->frequency_string = sprintf('+%d %s', $this->interval, $this->freq_name );
677  if ( DEBUG_RRULE ) printf( "Frequency modify string is: '%s', base is: '%s'\n", $this->frequency_string, $this->base->format('c') );
678  $this->Start($return_floating_times);
679  }
680 
681 
686  public function hasLimitedOccurrences() {
687  return ( isset($this->count) || isset($this->until) );
688  }
689 
690 
691  public function set_timezone( $tzstring ) {
692  $this->base->setTimezone(new DateTimeZone($tzstring));
693  }
694 
695 
696  public function Start($return_floating_times=false) {
697  $this->instances = array();
698  $this->GetMoreInstances($return_floating_times);
699  $this->rewind();
700  $this->finished = false;
701  }
702 
703 
704  public function rewind() {
705  $this->position = -1;
706  }
707 
708 
714  public function next($return_floating_times=false) {
715  $this->position++;
716  return $this->current($return_floating_times);
717  }
718 
719 
720  public function current($return_floating_times=false) {
721  if ( !$this->valid() ) return null;
722  if ( !isset($this->instances[$this->position]) ) $this->GetMoreInstances($return_floating_times);
723  if ( !$this->valid() ) return null;
724  if ( DEBUG_RRULE ) printf( "Returning date from position %d: %s (%s)\n", $this->position,
725  $this->instances[$this->position]->format('c'), $this->instances[$this->position]->FloatOrUTC($return_floating_times) );
726  return $this->instances[$this->position];
727  }
728 
729 
730  public function key($return_floating_times=false) {
731  if ( !$this->valid() ) return null;
732  if ( !isset($this->instances[$this->position]) ) $this->GetMoreInstances($return_floating_times);
733  if ( !isset($this->keys[$this->position]) ) {
734  $this->keys[$this->position] = $this->instances[$this->position];
735  }
736  return $this->keys[$this->position];
737  }
738 
739 
740  public function valid() {
741  if ( isset($this->instances[$this->position]) || !$this->finished ) return true;
742  return false;
743  }
744 
753  private static function rrule_expand_limit( $freq ) {
754  switch( $freq ) {
755  case 'YEARLY':
756  return array( 'bymonth' => 'expand', 'byweekno' => 'expand', 'byyearday' => 'expand', 'bymonthday' => 'expand',
757  'byday' => 'expand', 'byhour' => 'expand', 'byminute' => 'expand', 'bysecond' => 'expand' );
758  case 'MONTHLY':
759  return array( 'bymonth' => 'limit', 'bymonthday' => 'expand',
760  'byday' => 'expand', 'byhour' => 'expand', 'byminute' => 'expand', 'bysecond' => 'expand' );
761  case 'WEEKLY':
762  return array( 'bymonth' => 'limit',
763  'byday' => 'expand', 'byhour' => 'expand', 'byminute' => 'expand', 'bysecond' => 'expand' );
764  case 'DAILY':
765  return array( 'bymonth' => 'limit', 'bymonthday' => 'limit',
766  'byday' => 'limit', 'byhour' => 'expand', 'byminute' => 'expand', 'bysecond' => 'expand' );
767  case 'HOURLY':
768  return array( 'bymonth' => 'limit', 'bymonthday' => 'limit',
769  'byday' => 'limit', 'byhour' => 'limit', 'byminute' => 'expand', 'bysecond' => 'expand' );
770  case 'MINUTELY':
771  return array( 'bymonth' => 'limit', 'bymonthday' => 'limit',
772  'byday' => 'limit', 'byhour' => 'limit', 'byminute' => 'limit', 'bysecond' => 'expand' );
773  case 'SECONDLY':
774  return array( 'bymonth' => 'limit', 'bymonthday' => 'limit',
775  'byday' => 'limit', 'byhour' => 'limit', 'byminute' => 'limit', 'bysecond' => 'limit' );
776  }
777  dbg_error_log('ERROR','Invalid frequency code "%s" - pretending it is "DAILY"', $freq);
778  return array( 'bymonth' => 'limit', 'bymonthday' => 'limit',
779  'byday' => 'limit', 'byhour' => 'expand', 'byminute' => 'expand', 'bysecond' => 'expand' );
780  }
781 
782  private function GetMoreInstances($return_floating_times=false) {
783  if ( $this->finished ) return;
784  $got_more = false;
785  $loop_limit = 10;
786  $loops = 0;
787  if ( $return_floating_times ) $this->base->setAsFloat();
788  while( !$this->finished && !$got_more && $loops++ < $loop_limit ) {
789  if ( !isset($this->current_base) ) {
790  $this->current_base = clone($this->base);
791  }
792  else {
793  $this->current_base->modify( $this->frequency_string );
794  }
795  if ( $return_floating_times ) $this->current_base->setAsFloat();
796  if ( DEBUG_RRULE ) printf( "Getting more instances from: '%s' - %d\n", $this->current_base->format('c'), count($this->instances) );
797  $this->current_set = array( clone($this->current_base) );
798  foreach( self::rrule_expand_limit($this->freq) AS $bytype => $action ) {
799  if ( isset($this->{$bytype}) ) {
800  $this->{$action.'_'.$bytype}();
801  if ( !isset($this->current_set[0]) ) break;
802  }
803  }
804 
805  sort($this->current_set);
806  if ( isset($this->bysetpos) ) $this->limit_bysetpos();
807 
808  $position = count($this->instances) - 1;
809  if ( DEBUG_RRULE ) printf( "Inserting %d from current_set into position %d\n", count($this->current_set), $position + 1 );
810  foreach( $this->current_set AS $k => $instance ) {
811  if ( $instance < $this->base ) continue;
812  if ( isset($this->until) && $instance > $this->until ) {
813  $this->finished = true;
814  return;
815  }
816  if ( !isset($this->instances[$position]) || $instance != $this->instances[$position] ) {
817  $got_more = true;
818  $position++;
819  $this->instances[$position] = $instance;
820  if ( DEBUG_RRULE ) printf( "Added date %s into position %d in current set\n", $instance->format('c'), $position );
821  if ( isset($this->count) && ($position + 1) >= $this->count ) {
822  $this->finished = true;
823  return;
824  }
825  }
826  }
827  }
828  }
829 
830 
831  public static function rrule_day_number( $day ) {
832  switch( $day ) {
833  case 'SU': return 0;
834  case 'MO': return 1;
835  case 'TU': return 2;
836  case 'WE': return 3;
837  case 'TH': return 4;
838  case 'FR': return 5;
839  case 'SA': return 6;
840  }
841  return false;
842  }
843 
844 
845  static public function date_mask( $date, $y, $mo, $d, $h, $mi, $s ) {
846  $date_parts = explode(',',$date->format('Y,m,d,H,i,s'));
847 
848  if ( isset($y) || isset($mo) || isset($d) ) {
849  if ( isset($y) ) $date_parts[0] = $y;
850  if ( isset($mo) ) $date_parts[1] = $mo;
851  if ( isset($d) ) $date_parts[2] = $d;
852  $date->setDate( $date_parts[0], $date_parts[1], $date_parts[2] );
853  }
854  if ( isset($h) || isset($mi) || isset($s) ) {
855  if ( isset($h) ) $date_parts[3] = $h;
856  if ( isset($mi) ) $date_parts[4] = $mi;
857  if ( isset($s) ) $date_parts[5] = $s;
858  $date->setTime( $date_parts[3], $date_parts[4], $date_parts[5] );
859  }
860  return $date;
861  }
862 
863 
864  private function expand_bymonth() {
865  $instances = $this->current_set;
866  $this->current_set = array();
867  foreach( $instances AS $k => $instance ) {
868  foreach( $this->bymonth AS $k => $month ) {
869  $expanded = $this->date_mask( clone($instance), null, $month, null, null, null, null);
870  if ( DEBUG_RRULE ) printf( "Expanded BYMONTH $month into date %s\n", $expanded->format('c') );
871  $this->current_set[] = $expanded;
872  }
873  }
874  }
875 
876  private function expand_bymonthday() {
877  $instances = $this->current_set;
878  $this->current_set = array();
879  foreach( $instances AS $k => $instance ) {
880  foreach( $this->bymonthday AS $k => $monthday ) {
881  $expanded = $this->date_mask( clone($instance), null, null, $monthday, null, null, null);
882  if ( DEBUG_RRULE ) printf( "Expanded BYMONTHDAY $monthday into date %s from %s\n", $expanded->format('c'), $instance->format('c') );
883  $this->current_set[] = $expanded;
884  }
885  }
886  }
887 
888  private function expand_byyearday() {
889  $instances = $this->current_set;
890  $this->current_set = array();
891  $days_set = array();
892  foreach( $instances AS $k => $instance ) {
893  foreach( $this->byyearday AS $k => $yearday ) {
894  $on_yearday = clone($instance);
895  $on_yearday->setYearDay($yearday);
896  if ( isset($days_set[$on_yearday->UTC()]) ) continue;
897  $this->current_set[] = $on_yearday;
898  $days_set[$on_yearday->UTC()] = true;
899  }
900  }
901  }
902 
903  private function expand_byday_in_week( $day_in_week ) {
904 
910  $dow_of_instance = $day_in_week->format('w'); // 0 == Sunday
911  foreach( $this->byday AS $k => $weekday ) {
912  $dow = self::rrule_day_number($weekday);
913  $offset = $dow - $dow_of_instance;
914  if ( $offset < 0 ) $offset += 7;
915  $expanded = clone($day_in_week);
916  $expanded->modify( sprintf('+%d day', $offset) );
917  $this->current_set[] = $expanded;
918  if ( DEBUG_RRULE ) printf( "Expanded BYDAY(W) $weekday into date %s\n", $expanded->format('c') );
919  }
920  }
921 
922 
923  private function expand_byday_in_month( $day_in_month ) {
924 
925  $first_of_month = $this->date_mask( clone($day_in_month), null, null, 1, null, null, null);
926  $dow_of_first = $first_of_month->format('w'); // 0 == Sunday
927  $days_in_month = cal_days_in_month(CAL_GREGORIAN, $first_of_month->format('m'), $first_of_month->format('Y'));
928  foreach( $this->byday AS $k => $weekday ) {
929  if ( preg_match('{([+-])?(\d)?(MO|TU|WE|TH|FR|SA|SU)}', $weekday, $matches ) ) {
930  $dow = self::rrule_day_number($matches[3]);
931  $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.
932  $whichweek = intval($matches[2]);
933  if ( DEBUG_RRULE ) printf( "Expanding BYDAY(M) $weekday in month of %s\n", $first_of_month->format('c') );
934  if ( $whichweek > 0 ) {
935  $whichweek--;
936  $monthday = $first_dom;
937  if ( $matches[1] == '-' ) {
938  $monthday += 35;
939  while( $monthday > $days_in_month ) $monthday -= 7;
940  $monthday -= (7 * $whichweek);
941  }
942  else {
943  $monthday += (7 * $whichweek);
944  }
945  if ( $monthday > 0 && $monthday <= $days_in_month ) {
946  $expanded = $this->date_mask( clone($day_in_month), null, null, $monthday, null, null, null);
947  if ( DEBUG_RRULE ) printf( "Expanded BYDAY(M) $weekday now $monthday into date %s\n", $expanded->format('c') );
948  $this->current_set[] = $expanded;
949  }
950  }
951  else {
952  for( $monthday = $first_dom; $monthday <= $days_in_month; $monthday += 7 ) {
953  $expanded = $this->date_mask( clone($day_in_month), null, null, $monthday, null, null, null);
954  if ( DEBUG_RRULE ) printf( "Expanded BYDAY(M) $weekday now $monthday into date %s\n", $expanded->format('c') );
955  $this->current_set[] = $expanded;
956  }
957  }
958  }
959  }
960  }
961 
962 
963  private function expand_byday_in_year( $day_in_year ) {
964 
965  $first_of_year = $this->date_mask( clone($day_in_year), null, 1, 1, null, null, null);
966  $dow_of_first = $first_of_year->format('w'); // 0 == Sunday
967  $days_in_year = 337 + cal_days_in_month(CAL_GREGORIAN, 2, $first_of_year->format('Y'));
968  foreach( $this->byday AS $k => $weekday ) {
969  if ( preg_match('{([+-])?(\d)?(MO|TU|WE|TH|FR|SA|SU)}', $weekday, $matches ) ) {
970  $expanded = clone($first_of_year);
971  $dow = self::rrule_day_number($matches[3]);
972  $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.
973  $whichweek = intval($matches[2]);
974  if ( DEBUG_RRULE ) printf( "Expanding BYDAY(Y) $weekday from date %s\n", $instance->format('c') );
975  if ( $whichweek > 0 ) {
976  $whichweek--;
977  $yearday = $first_doy;
978  if ( $matches[1] == '-' ) {
979  $yearday += 371;
980  while( $yearday > $days_in_year ) $yearday -= 7;
981  $yearday -= (7 * $whichweek);
982  }
983  else {
984  $yearday += (7 * $whichweek);
985  }
986  if ( $yearday > 0 && $yearday <= $days_in_year ) {
987  $expanded->modify(sprintf('+%d day', $yearday - 1));
988  if ( DEBUG_RRULE ) printf( "Expanded BYDAY(Y) $weekday now $yearday into date %s\n", $expanded->format('c') );
989  $this->current_set[] = $expanded;
990  }
991  }
992  else {
993  $expanded->modify(sprintf('+%d day', $first_doy - 1));
994  for( $yearday = $first_doy; $yearday <= $days_in_year; $yearday += 7 ) {
995  if ( DEBUG_RRULE ) printf( "Expanded BYDAY(Y) $weekday now $yearday into date %s\n", $expanded->format('c') );
996  $this->current_set[] = clone($expanded);
997  $expanded->modify('+1 week');
998  }
999  }
1000  }
1001  }
1002  }
1003 
1004 
1005  private function expand_byday() {
1006  if ( !isset($this->current_set[0]) ) return;
1007  if ( $this->freq == 'MONTHLY' || $this->freq == 'YEARLY' ) {
1008  if ( isset($this->bymonthday) || isset($this->byyearday) ) {
1009  $this->limit_byday();
1010  return;
1011  }
1012  }
1013  $instances = $this->current_set;
1014  $this->current_set = array();
1015  foreach( $instances AS $k => $instance ) {
1016  if ( $this->freq == 'MONTHLY' ) {
1017  $this->expand_byday_in_month($instance);
1018  }
1019  else if ( $this->freq == 'WEEKLY' ) {
1020  $this->expand_byday_in_week($instance);
1021  }
1022  else { // YEARLY
1023  if ( isset($this->bymonth) ) {
1024  $this->expand_byday_in_month($instance);
1025  }
1026  else if ( isset($this->byweekno) ) {
1027  $this->expand_byday_in_week($instance);
1028  }
1029  else {
1030  $this->expand_byday_in_year($instance);
1031  }
1032  }
1033 
1034  }
1035  }
1036 
1037  private function expand_byhour() {
1038  $instances = $this->current_set;
1039  $this->current_set = array();
1040  foreach( $instances AS $k => $instance ) {
1041  foreach( $this->bymonth AS $k => $month ) {
1042  $this->current_set[] = $this->date_mask( clone($instance), null, null, null, $hour, null, null);
1043  }
1044  }
1045  }
1046 
1047  private function expand_byminute() {
1048  $instances = $this->current_set;
1049  $this->current_set = array();
1050  foreach( $instances AS $k => $instance ) {
1051  foreach( $this->bymonth AS $k => $month ) {
1052  $this->current_set[] = $this->date_mask( clone($instance), null, null, null, null, $minute, null);
1053  }
1054  }
1055  }
1056 
1057  private function expand_bysecond() {
1058  $instances = $this->current_set;
1059  $this->current_set = array();
1060  foreach( $instances AS $k => $instance ) {
1061  foreach( $this->bymonth AS $k => $second ) {
1062  $this->current_set[] = $this->date_mask( clone($instance), null, null, null, null, null, $second);
1063  }
1064  }
1065  }
1066 
1067 
1068  private function limit_generally( $fmt_char, $element_name ) {
1069  $instances = $this->current_set;
1070  $this->current_set = array();
1071  foreach( $instances AS $k => $instance ) {
1072  foreach( $this->{$element_name} AS $k => $element_value ) {
1073  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') );
1074  if ( $instance->format($fmt_char) == $element_value ) $this->current_set[] = $instance;
1075  }
1076  }
1077  }
1078 
1079  private function limit_byday() {
1080  $fmt_char = 'w';
1081  $instances = $this->current_set;
1082  $this->current_set = array();
1083  foreach( $this->byday AS $k => $weekday ) {
1084  $dow = self::rrule_day_number($weekday);
1085  foreach( $instances AS $k => $instance ) {
1086  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') );
1087  if ( $instance->format($fmt_char) == $dow ) $this->current_set[] = $instance;
1088  }
1089  }
1090  }
1091 
1092  private function limit_bymonth() { $this->limit_generally( 'm', 'bymonth' ); }
1093  private function limit_byyearday() { $this->limit_generally( 'z', 'byyearday' ); }
1094  private function limit_bymonthday() { $this->limit_generally( 'd', 'bymonthday' ); }
1095  private function limit_byhour() { $this->limit_generally( 'H', 'byhour' ); }
1096  private function limit_byminute() { $this->limit_generally( 'i', 'byminute' ); }
1097  private function limit_bysecond() { $this->limit_generally( 's', 'bysecond' ); }
1098 
1099 
1100  private function limit_bysetpos( ) {
1101  $instances = $this->current_set;
1102  $count = count($instances);
1103  $this->current_set = array();
1104  foreach( $this->bysetpos AS $k => $element_value ) {
1105  if ( DEBUG_RRULE ) printf( "Limiting bysetpos %s of %d instances\n", $element_value, $count );
1106  if ( $element_value > 0 ) {
1107  $this->current_set[] = $instances[$element_value - 1];
1108  }
1109  else if ( $element_value < 0 ) {
1110  $this->current_set[] = $instances[$count + $element_value];
1111  }
1112  }
1113  }
1114 
1115 
1116 }
1117 
1118 
1119 
1120 require_once("vComponent.php");
1121 
1131 function rdate_expand( $dtstart, $property, $component, $range_end = null, $is_date=null, $return_floating_times=false ) {
1132  $properties = $component->GetProperties($property);
1133  $expansion = array();
1134  foreach( $properties AS $p ) {
1135  $timezone = $p->GetParameterValue('TZID');
1136  $rdate = $p->Value();
1137  $rdates = explode( ',', $rdate );
1138  foreach( $rdates AS $k => $v ) {
1139  $rdate = new RepeatRuleDateTime( $v, $timezone, $is_date);
1140  if ( $return_floating_times ) $rdate->setAsFloat();
1141  $expansion[$rdate->FloatOrUTC($return_floating_times)] = $component;
1142  if ( $rdate > $range_end ) break;
1143  }
1144  }
1145  return $expansion;
1146 }
1147 
1148 
1159 function rrule_expand( $dtstart, $property, $component, $range_end, $is_date=null, $return_floating_times=false, $fallback_tzid=null ) {
1160  global $c;
1161  $expansion = array();
1162 
1163  $recur = $component->GetProperty($property);
1164  if ( !isset($recur) ) return $expansion;
1165  $recur = $recur->Value();
1166 
1167  $this_start = $component->GetProperty('DTSTART');
1168  if ( isset($this_start) ) {
1169  $this_start = RepeatRuleDateTime::withFallbackTzid($this_start, $fallback_tzid);
1170  }
1171  else {
1172  $this_start = clone($dtstart);
1173  }
1174  if ( $return_floating_times ) $this_start->setAsFloat();
1175 
1176 // if ( DEBUG_RRULE ) print_r( $this_start );
1177  if ( DEBUG_RRULE ) printf( "RRULE: %s (floating: %s)\n", $recur, ($return_floating_times?"yes":"no") );
1178  $rule = new RepeatRule( $this_start, $recur, $is_date, $return_floating_times );
1179  $i = 0;
1180 
1181  if ( !isset($c->rrule_expansion_limit) ) $c->rrule_expansion_limit = 5000;
1182  while( $date = $rule->next($return_floating_times) ) {
1183 // if ( DEBUG_RRULE ) printf( "[%3d] %s\n", $i, $date->UTC() );
1184  $expansion[$date->FloatOrUTC($return_floating_times)] = $component;
1185  if ( $date > $range_end ) break;
1186  if ( $i++ >= $c->rrule_expansion_limit ) {
1187  dbg_error_log( 'ERROR', "Hit rrule expansion limit of ".$c->rrule_expansion_limit." - increase rrule_expansion_limit in config to avoid events missing from freebusy" );
1188  }
1189  }
1190 // if ( DEBUG_RRULE ) print_r( $expansion );
1191  return $expansion;
1192 }
1193 
1194 
1206 function expand_event_instances( vComponent $vResource, $range_start = null, $range_end = null, $return_floating_times=false ) {
1207  global $c;
1208  $components = $vResource->GetComponents();
1209 
1210  $clear_instance_props = array(
1211  'DTSTART' => true,
1212  'DUE' => true,
1213  'DTEND' => true
1214  );
1215  if ( empty( $c->expanded_instances_include_rrule ) ) {
1216  $clear_instance_props += array(
1217  'RRULE' => true,
1218  'RDATE' => true,
1219  'EXDATE' => true
1220  );
1221  }
1222 
1223  if ( empty($range_start) ) { $range_start = new RepeatRuleDateTime(); $range_start->modify('-6 weeks'); }
1224  if ( empty($range_end) ) {
1225  $range_end = clone($range_start);
1226  $range_end->modify('+6 months');
1227  }
1228 
1229  $instances = array();
1230  $expand = false;
1231  $dtstart = null;
1232  $is_date = false;
1233  $has_repeats = false;
1234  $dtstart_type = 'DTSTART';
1235 
1236  $components_prefix = [];
1237  $components_base_events = [];
1238  $components_override_events = [];
1239 
1240  foreach ($components AS $k => $comp) {
1241  if ( $comp->GetType() != 'VEVENT' && $comp->GetType() != 'VTODO' && $comp->GetType() != 'VJOURNAL' ) {
1242  // Other types of component (such as VTIMEZONE) go first
1243  $components_prefix[] = $comp;
1244  } else if ($comp->GetProperty('RECURRENCE-ID') === null) {
1245  // This is the base event, we need to handle it first
1246  $components_base_events[] = $comp;
1247  } else {
1248  // This is an override of an event instance, handle it last
1249  $components_override_events[] = $comp;
1250  }
1251  }
1252 
1253  $components = array_merge($components_prefix, $components_base_events, $components_override_events);
1254 
1255  foreach( $components AS $k => $comp ) {
1256  if ( $comp->GetType() != 'VEVENT' && $comp->GetType() != 'VTODO' && $comp->GetType() != 'VJOURNAL' ) {
1257  continue;
1258  }
1259  if ( !isset($dtstart) ) {
1260  $dtstart_prop = $comp->GetProperty($dtstart_type);
1261  if ( !isset($dtstart_prop) && $comp->GetType() != 'VTODO' ) {
1262  $dtstart_type = 'DUE';
1263  $dtstart_prop = $comp->GetProperty($dtstart_type);
1264  }
1265  if ( !isset($dtstart_prop) ) continue;
1266  $dtstart = new RepeatRuleDateTime( $dtstart_prop );
1267  if ( $return_floating_times ) $dtstart->setAsFloat();
1268  if ( DEBUG_RRULE ) printf( "Component is: %s (floating: %s)\n", $comp->GetType(), ($return_floating_times?"yes":"no") );
1269  $is_date = $dtstart->isDate();
1270  $instances[$dtstart->FloatOrUTC($return_floating_times)] = $comp;
1271  $rrule = $comp->GetProperty('RRULE');
1272  $has_repeats = isset($rrule);
1273  }
1274  $p = $comp->GetProperty('RECURRENCE-ID');
1275  if ( isset($p) && $p->Value() != '' ) {
1276  $range = $p->GetParameterValue('RANGE');
1277  $recur_utc = new RepeatRuleDateTime($p);
1278  if ( $is_date ) $recur_utc->setAsDate();
1279  $recur_utc = $recur_utc->FloatOrUTC($return_floating_times);
1280  if ( isset($range) && $range == 'THISANDFUTURE' ) {
1281  foreach( $instances AS $k => $v ) {
1282  if ( DEBUG_RRULE ) printf( "Removing overridden instance at: $k\n" );
1283  if ( $k >= $recur_utc ) unset($instances[$k]);
1284  }
1285  }
1286  else {
1287  unset($instances[$recur_utc]);
1288  // This is a single instance of a recurring event, it can not in itself produce extra instances due to RRULE etc
1289  continue;
1290  }
1291  }
1292  else if ( DEBUG_RRULE ) {
1293  $p = $comp->GetProperty('SUMMARY');
1294  $summary = ( isset($p) ? $p->Value() : 'not set');
1295  $p = $comp->GetProperty('UID');
1296  $uid = ( isset($p) ? $p->Value() : 'not set');
1297  printf( "Processing event '%s' with UID '%s' starting on %s\n",
1298  $summary, $uid, $dtstart->FloatOrUTC($return_floating_times) );
1299  print( "Instances at start");
1300  foreach( $instances AS $k => $v ) {
1301  print ' : '.$k;
1302  }
1303  print "\n";
1304  }
1305  $instances += rrule_expand($dtstart, 'RRULE', $comp, $range_end, null, $return_floating_times);
1306  if ( DEBUG_RRULE ) {
1307  print( "After rrule_expand");
1308  foreach( $instances AS $k => $v ) {
1309  print ' : '.$k;
1310  }
1311  print "\n";
1312  }
1313  $instances += rdate_expand($dtstart, 'RDATE', $comp, $range_end, null, $return_floating_times);
1314  if ( DEBUG_RRULE ) {
1315  print( "After rdate_expand");
1316  foreach( $instances AS $k => $v ) {
1317  print ' : '.$k;
1318  }
1319  print "\n";
1320  }
1321  foreach ( rdate_expand($dtstart, 'EXDATE', $comp, $range_end, null, $return_floating_times) AS $k => $v ) {
1322  unset($instances[$k]);
1323  }
1324  if ( DEBUG_RRULE ) {
1325  print( "After exdate_expand");
1326  foreach( $instances AS $k => $v ) {
1327  print ' : '.$k;
1328  }
1329  print "\n";
1330  }
1331  }
1332 
1333  $last_duration = null;
1334  $early_start = null;
1335  $new_components = array();
1336  $start_utc = $range_start->FloatOrUTC($return_floating_times);
1337  $end_utc = $range_end->FloatOrUTC($return_floating_times);
1338  foreach( $instances AS $utc => $comp ) {
1339  if ( $utc > $end_utc ) {
1340  if ( DEBUG_RRULE ) printf( "We're done: $utc is out of the range.\n");
1341  break;
1342  }
1343 
1344  $end_type = ($comp->GetType() == 'VTODO' ? 'DUE' : 'DTEND');
1345  $duration = $comp->GetProperty('DURATION');
1346  if ( !isset($duration) || $duration->Value() == '' ) {
1347  $instance_start = $comp->GetProperty($dtstart_type);
1348  $dtsrt = new RepeatRuleDateTime( $instance_start );
1349  if ( $return_floating_times ) $dtsrt->setAsFloat();
1350  $instance_end = $comp->GetProperty($end_type);
1351  if ( isset($instance_end) ) {
1352  $dtend = new RepeatRuleDateTime( $instance_end );
1353  $duration = Rfc5545Duration::fromTwoDates($dtsrt, $dtend);
1354  }
1355  else {
1356  if ( $instance_start->GetParameterValue('VALUE') == 'DATE' ) {
1357  $duration = new Rfc5545Duration('P1D');
1358  }
1359  else {
1360  $duration = new Rfc5545Duration(0);
1361  }
1362  }
1363  }
1364  else {
1365  $duration = new Rfc5545Duration($duration->Value());
1366  }
1367 
1368  if ( $utc < $start_utc ) {
1369  if ( isset($early_start) && isset($last_duration) && $duration->equals($last_duration) ) {
1370  if ( $utc < $early_start ) {
1371  if ( DEBUG_RRULE ) printf( "Next please: $utc is before $early_start and before $start_utc.\n");
1372  continue;
1373  }
1374  }
1375  else {
1377  $latest_start = clone($range_start);
1378  $latest_start->modify('-'.$duration);
1379  $early_start = $latest_start->FloatOrUTC($return_floating_times);
1380  $last_duration = $duration;
1381  if ( $utc < $early_start ) {
1382  if ( DEBUG_RRULE ) printf( "Another please: $utc is before $early_start and before $start_utc.\n");
1383  continue;
1384  }
1385  }
1386  }
1387  $component = clone($comp);
1388  $component->ClearProperties( $clear_instance_props );
1389  $component->AddProperty($dtstart_type, $utc, ($is_date ? array('VALUE' => 'DATE') : null) );
1390  $component->AddProperty('DURATION', $duration );
1391  if ( $has_repeats && $dtstart->FloatOrUTC($return_floating_times) != $utc )
1392  $component->AddProperty('RECURRENCE-ID', $utc, ($is_date ? array('VALUE' => 'DATE') : null) );
1393  $new_components[$utc] = $component;
1394  }
1395 
1396  // Add overriden instances
1397  foreach( $components AS $k => $comp ) {
1398  $p = $comp->GetProperty('RECURRENCE-ID');
1399  if ( isset($p) && $p->Value() != '') {
1400  $recurrence_id = $p->Value();
1401 
1402 
1403  $dtstart_prop = $comp->GetProperty('DTSTART');
1404  if ( !isset($dtstart_prop) && $comp->GetType() != 'VTODO' ) {
1405  $dtstart_prop = $comp->GetProperty('DUE');
1406  }
1407 
1408  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
1409  $dtstart_rrdt = new RepeatRuleDateTime( $dtstart_prop );
1410  $is_date = $dtstart_rrdt->isDate();
1411  if ( $return_floating_times ) $dtstart_rrdt->setAsFloat();
1412  $dtstart = $dtstart_rrdt->FloatOrUTC($return_floating_times);
1413  if ( !isset($new_components[$recurrence_id]) && $dtstart > $end_utc ) continue; // Start after end of range, skip it
1414 
1415  $end_type = ($comp->GetType() == 'VTODO' ? 'DUE' : 'DTEND');
1416  $duration = $comp->GetProperty('DURATION');
1417 
1418  if ( !isset($duration) || $duration->Value() == '' ) {
1419  $instance_end = $comp->GetProperty($end_type);
1420  if ( isset($instance_end) ) {
1421  $dtend_rrdt = new RepeatRuleDateTime( $instance_end );
1422  if ( $return_floating_times ) $dtend_rrdt->setAsFloat();
1423  $dtend = $dtend_rrdt->FloatOrUTC($return_floating_times);
1424 
1425  $comp->AddProperty('DURATION', Rfc5545Duration::fromTwoDates($dtstart_rrdt, $dtend_rrdt) );
1426  }
1427  else {
1428  $dtend = $dtstart + ($is_date ? $dtstart + 86400 : 0 );
1429  }
1430  }
1431  else {
1432  $duration = new Rfc5545Duration($duration->Value());
1433  $dtend = $dtstart + $duration->asSeconds();
1434  }
1435 
1436  if ( !isset($new_components[$recurrence_id]) && $dtend < $start_utc ) continue; // End before start of range: skip that too.
1437 
1438  if ( DEBUG_RRULE ) printf( "Replacing overridden instance at %s\n", $recurrence_id);
1439  $new_components[$recurrence_id] = $comp;
1440  }
1441  }
1442 
1443  $vResource->SetComponents($new_components);
1444 
1445  return $vResource;
1446 }
1447 
1448 
1455 function getComponentRange(vComponent $comp, string $fallback_tzid = null) {
1456  $dtstart_prop = $comp->GetProperty('DTSTART');
1457  $duration_prop = $comp->GetProperty('DURATION');
1458  if ( isset($duration_prop) ) {
1459  if ( !isset($dtstart_prop) ) throw new Exception('Invalid '.$comp->GetType().' containing DURATION without DTSTART', 0);
1460  $dtstart = RepeatRuleDateTime::withFallbackTzid($dtstart_prop, $fallback_tzid);
1461  $dtend = clone($dtstart);
1462  $dtend->modify(new Rfc5545Duration($duration_prop->Value()));
1463  }
1464  else {
1465  $completed_prop = null;
1466  switch ( $comp->GetType() ) {
1467  case 'VEVENT':
1468  if ( !isset($dtstart_prop) ) throw new Exception('Invalid VEVENT without DTSTART', 0);
1469  $dtend_prop = $comp->GetProperty('DTEND');
1470  break;
1471  case 'VTODO':
1472  $completed_prop = $comp->GetProperty('COMPLETED');
1473  $dtend_prop = $comp->GetProperty('DUE');
1474  break;
1475  case 'VJOURNAL':
1476  if ( !isset($dtstart_prop) )
1477  $dtstart_prop = $comp->GetProperty('DTSTAMP');
1478  $dtend_prop = $dtstart_prop;
1479  default:
1480  throw new Exception('getComponentRange cannot handle "'.$comp->GetType().'" components', 0);
1481  }
1482 
1483  if ( isset($dtstart_prop) )
1484  $dtstart = RepeatRuleDateTime::withFallbackTzid($dtstart_prop, $fallback_tzid);
1485  else
1486  $dtstart = null;
1487 
1488  if ( isset($dtend_prop) )
1489  $dtend = RepeatRuleDateTime::withFallbackTzid($dtend_prop, $fallback_tzid);
1490  else
1491  $dtend = null;
1492 
1493  if ( isset($completed_prop) ) {
1494  $completed = RepeatRuleDateTime::withFallbackTzid($completed_prop, $fallback_tzid);
1495  if ( !isset($dtstart) || (isset($dtstart) && $completed < $dtstart) ) $dtstart = $completed;
1496  if ( !isset($dtend) || (isset($dtend) && $completed > $dtend) ) $dtend = $completed;
1497  }
1498  }
1499  return new RepeatRuleDateRange($dtstart, $dtend);
1500 }
1501 
1510 function getVCalendarRange( $vResource, string $fallback_tzid = null ) {
1511  $components = $vResource->GetComponents();
1512 
1513  $dtstart = null;
1514  $duration = null;
1515  $earliest_start = null;
1516  $latest_end = null;
1517  $has_repeats = false;
1518  foreach( $components AS $k => $comp ) {
1519  if ( $comp->GetType() == 'VTIMEZONE' ) continue;
1520  $range = getComponentRange($comp, $fallback_tzid);
1521  $dtstart = $range->from;
1522  if ( !isset($dtstart) ) continue;
1523  $duration = $range->getDuration();
1524 
1525  $rrule = $comp->GetProperty('RRULE');
1526  $limited_occurrences = true;
1527  if ( isset($rrule) ) {
1528  $rule = new RepeatRule($dtstart, $rrule);
1529  $limited_occurrences = $rule->hasLimitedOccurrences();
1530  }
1531 
1532  if ( $limited_occurrences ) {
1533  $instances = array();
1534  $instances[$dtstart->FloatOrUTC()] = $dtstart;
1535  if ( !isset($range_end) ) {
1536  $range_end = new RepeatRuleDateTime();
1537  $range_end->modify('+150 years');
1538  }
1539  $instances += rrule_expand($dtstart, 'RRULE', $comp, $range_end, null, false, $fallback_tzid);
1540  $instances += rdate_expand($dtstart, 'RDATE', $comp, $range_end);
1541  foreach ( rdate_expand($dtstart, 'EXDATE', $comp, $range_end) AS $k => $v ) {
1542  unset($instances[$k]);
1543  }
1544  if ( count($instances) < 1 ) {
1545  if ( empty($earliest_start) || $dtstart < $earliest_start ) $earliest_start = $dtstart;
1546  $latest_end = null;
1547  break;
1548  }
1549  $instances = array_keys($instances);
1550  asort($instances);
1551  $first = new RepeatRuleDateTime($instances[0]);
1552  $last = new RepeatRuleDateTime($instances[count($instances)-1]);
1553  $last->modify($duration);
1554  if ( empty($earliest_start) || $first < $earliest_start ) $earliest_start = $first;
1555  if ( empty($latest_end) || $last > $latest_end ) $latest_end = $last;
1556  }
1557  else {
1558  if ( empty($earliest_start) || $dtstart < $earliest_start ) $earliest_start = $dtstart;
1559  $latest_end = null;
1560  break;
1561  }
1562  }
1563 
1564  return new RepeatRuleDateRange($earliest_start, $latest_end );
1565 }
expand_byday_in_week( $day_in_week)
Definition: RRule.php:903
FloatOrUTC($return_floating_times=false)
Definition: RRule.php:406
hasLimitedOccurrences()
Definition: RRule.php:686
static hasLeapDay($year)
Definition: RRule.php:460
RFC5545($return_floating_times=false)
Definition: RRule.php:422
static fromTwoDates( $d1, $d2)
Definition: RRule.php:198
expand_byday()
Definition: RRule.php:1005
equals( $other)
Definition: RRule.php:109
__construct( $basedate, $rrule, $is_date=null, $return_floating_times=false)
Definition: RRule.php:633
__construct( $date1, $date2)
Definition: RRule.php:553
__construct( $in_duration)
Definition: RRule.php:89
UTC($fmt='Ymd\THis\Z')
Definition: RRule.php:379
next($return_floating_times=false)
Definition: RRule.php:714
static daysInMonth( $year, $month)
Definition: RRule.php:471
static rrule_expand_limit( $freq)
Definition: RRule.php:753
overlaps(RepeatRuleDateRange $other)
Definition: RRule.php:569