Add the ability to override dns for iSchedule using a global variable $icfg
[davical.git] / inc / RRule-v2.php
blobaca9dfbc6302bd70693bcde8e696c6a0ac0a87c1
1 <?php
2 /**
3 * Class for parsing RRule and getting us the dates
5 * @package awl
6 * @subpackage caldav
7 * @author Andrew McMillan <andrew@mcmillan.net.nz>
8 * @copyright Morphoss Ltd
9 * @license http://gnu.org/copyleft/gpl.html GNU GPL v2 or later
12 if ( !class_exists('DateTime') ) return;
14 /**
15 * Try and extract something like "Pacific/Auckland" or "America/Indiana/Indianapolis" if possible, given
16 * the VTIMEZONE component that is passed in. This is much more complex than olson_from_tzstring since
17 * we start to examine the rules and work out what actual timezone this might be.
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;
27 /**
28 * @todo: We'll do other stuff here, in due course...
30 return null;
33 // define( 'DEBUG_RRULE', true);
34 define( 'DEBUG_RRULE', false );
36 /**
37 * Wrap the DateTimeZone class to allow parsing some iCalendar TZID strangenesses
39 class RepeatRuleTimeZone extends DateTimeZone {
40 private $tz_defined;
42 public function __construct($in_dtz = null) {
43 $this->tz_defined = false;
44 if ( !isset($in_dtz) ) return;
46 $olson = olson_from_tzstring($in_dtz);
47 if ( isset($olson) ) {
48 try {
49 parent::__construct($olson);
50 $this->tz_defined = $olson;
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;
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;
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;
73 /**
74 * Provide a useful way of dealing with RFC5545 duration strings of the form
75 * ^-?P(\dW)|((\dD)?(T(\dH)?(\dM)?(\dS)?)?)$
77 class Rfc5545Duration {
78 private $epoch_seconds = null;
79 private $days = 0;
80 private $secs = 0;
81 private $as_text = '';
83 /**
84 * Construct a new Rfc5545Duration either from incoming seconds or a text string.
85 * @param mixed $in_duration
87 function __construct( $in_duration ) {
88 if ( is_integer($in_duration) ) {
89 $this->epoch_seconds = $in_duration;
90 $this->as_text = '';
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;
97 else {
98 // fatal('Passed duration is neither numeric nor string!');
103 * Return true if $this and $other are equal, false otherwise.
104 * @param Rfc5545Duration $other
105 * @return boolean
107 function equals( $other ) {
108 if ( $this == $other ) return true;
109 if ( $this->asSeconds() == $other->asSeconds() ) return true;
110 return false;
114 * Returns the duration as epoch seconds.
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 if ( isset($matches[2]) ) $this->days = ($matches[2] * 7);
120 else {
121 if ( isset($matches[4]) ) $this->days = $matches[4];
122 $this->secs = 0;
123 if ( isset($matches[6]) ) $this->secs += $matches[6] * 3600;
124 if ( isset($matches[7]) ) $this->days += $matches[7] * 60;
125 if ( isset($matches[8]) ) $this->days += $matches[8];
127 if ( $matches[1] == '-' ) {
128 $this->days *= -1;
129 $this->secs *= -1;
131 $this->epoch_seconds = ($this->days * 86400) + $this->secs;
133 else {
134 throw new Exception('Invalid epoch: "'+$this->as_text+"'");
137 return $this->epoch_seconds;
142 * Returns the duration as a text string of the form ^(-?)P(\d+W)|((\d+D)?(T(\d+H)?(\d+M)?(\d+S)?)?)$
143 * @return string The stringified stuff.
145 function __toString() {
146 if ( empty($this->as_text) ) {
147 $this->as_text = ($this->epoch_seconds < 0 ? '-P' : 'P');
148 $in_duration = abs($this->epoch_seconds);
149 if ( $in_duration >= 86400 ) {
150 $this->days = floor($in_duration / 86400);
151 $in_duration -= $this->days * 86400;
152 if ( $in_duration == 0 && ($this->days / 7) == floor($this->days / 7) ) {
153 $this->as_text .= ($this->days/7).'W';
154 return $this->as_text;
156 $this->as_text .= $this->days.'D';
158 if ( $in_duration > 0 ) {
159 $secs = $in_duration;
160 $this->as_text .= 'T';
161 $hours = floor($in_duration / 3600);
162 if ( $hours > 0 ) $this->as_text .= $hours . 'H';
163 $minutes = floor(($in_duration % 3600) / 60);
164 if ( $minutes > 0 ) $this->as_text .= $minutes . 'M';
165 $seconds = $in_duration % 60;
166 if ( $seconds > 0 ) $this->as_text .= $seconds . 'S';
169 return $this->as_text;
174 * Factory method to return an Rfc5545Duration object from the difference
175 * between two dates.
177 * This is flawed, at present: we should really localise both dates and work
178 * out the difference in days, then localise the times and work out the difference
179 * between the clock times. On the other hand we're replacing a quick and dirty
180 * hack that did it exactly the same way in the past, so we're not making things
181 * any *worse* and at least we're making it clear that it could be improved...
183 * The problem strikes (as they all do) across DST boundaries.
185 * @todo Improve this to calculate the days difference and then the clock time diff
186 * and work from there.
188 * @param RepeatRuleDateTime $d1
189 * @param RepeatRuleDateTime $d2
190 * @return Rfc5545Duration
192 static function fromTwoDates( $d1, $d2 ) {
193 $diff = $d2->epoch() - $d1->epoch();
194 return new Rfc5545Duration($diff);
199 * Wrap the DateTime class to make it friendlier to passing in random strings from iCalendar
200 * objects, and especially the random stuff used to identify timezones. We also add some
201 * utility methods and stuff too, in order to simplify some of the operations we need to do
202 * with dates.
204 class RepeatRuleDateTime extends DateTime {
205 // public static $Format = 'Y-m-d H:i:s';
206 public static $Format = 'c';
207 private static $UTCzone;
208 private $tzid;
209 private $is_date;
211 public function __construct($date = null, $dtz = null, $is_date = null ) {
212 if ( !isset(self::$UTCzone) ) self::$UTCzone = new RepeatRuleTimeZone('UTC');
213 $this->is_date = false;
214 if ( isset($is_date) ) $this->is_date = $is_date;
215 if ( !isset($date) ) {
216 $date = date('Ymd\THis');
217 // Floating
218 $dtz = self::$UTCzone;
220 $this->tzid = null;
222 if ( is_object($date) && method_exists($date,'GetParameterValue') ) {
223 $tzid = $date->GetParameterValue('TZID');
224 $actual_date = $date->Value();
225 if ( isset($tzid) ) {
226 $dtz = new RepeatRuleTimeZone($tzid);
227 $this->tzid = $dtz->tzid();
229 else {
230 $dtz = self::$UTCzone;
231 if ( substr($actual_date,-1) == 'Z' ) {
232 $this->tzid = 'UTC';
233 $actual_date = substr($actual_date, 0, strlen($actual_date) - 1);
236 if ( strlen($actual_date) == 8 ) {
237 // We allow dates without VALUE=DATE parameter, but we don't create them like that
238 $this->is_date = true;
240 // $value_type = $date->GetParameterValue('VALUE');
241 // if ( isset($value_type) && $value_type == 'DATE' ) $this->is_date = true;
242 $date = $actual_date;
243 if ( DEBUG_RRULE ) printf( "Date%s property%s: %s%s\n", ($this->is_date ? "" : "Time"),
244 (isset($this->tzid) ? ' with timezone' : ''), $date,
245 (isset($this->tzid) ? ' in '.$this->tzid : '') );
247 elseif (preg_match('/;TZID= ([^:;]+) (?: ;.* )? : ( \d{8} (?:T\d{6})? ) (Z)?/x', $date, $matches) ) {
248 $date = $matches[2];
249 $this->is_date = (strlen($date) == 8);
250 if ( isset($matches[3]) && $matches[3] == 'Z' ) {
251 $dtz = self::$UTCzone;
252 $this->tzid = 'UTC';
254 else if ( isset($matches[1]) && $matches[1] != '' ) {
255 $dtz = new RepeatRuleTimeZone($matches[1]);
256 $this->tzid = $dtz->tzid();
258 else {
259 $dtz = self::$UTCzone;
260 $this->tzid = null;
262 if ( DEBUG_RRULE ) printf( "Date%s property%s: %s%s\n", ($this->is_date ? "" : "Time"),
263 (isset($this->tzid) ? ' with timezone' : ''), $date,
264 (isset($this->tzid) ? ' in '.$this->tzid : '') );
266 elseif ( ( $dtz === null || $dtz == '' )
267 && preg_match('{;VALUE=DATE (?:;[^:]+) : ((?:[12]\d{3}) (?:0[1-9]|1[012]) (?:0[1-9]|[12]\d|3[01]Z?) )$}x', $date, $matches) ) {
268 $this->is_date = true;
269 $date = $matches[1];
270 // Floating
271 $dtz = self::$UTCzone;
272 $this->tzid = null;
273 if ( DEBUG_RRULE ) printf( "Floating Date value: %s\n", $date );
275 elseif ( $dtz === null || $dtz == '' ) {
276 $dtz = self::$UTCzone;
277 if ( preg_match('/(\d{8}(T\d{6})?)(Z?)/', $date, $matches) ) {
278 $date = $matches[1];
279 $this->tzid = ( $matches[3] == 'Z' ? 'UTC' : null );
281 $this->is_date = (strlen($date) == 8 );
282 if ( DEBUG_RRULE ) printf( "Date%s value with timezone: %s in %s\n", ($this->is_date?"":"Time"), $date, $this->tzid );
284 elseif ( is_string($dtz) ) {
285 $dtz = new RepeatRuleTimeZone($dtz);
286 $this->tzid = $dtz->tzid();
287 $type = gettype($date);
288 if ( DEBUG_RRULE ) printf( "Date%s $type with timezone: %s in %s\n", ($this->is_date?"":"Time"), $date, $this->tzid );
290 else {
291 $this->tzid = $dtz->getName();
292 $type = gettype($date);
293 if ( DEBUG_RRULE ) printf( "Date%s $type with timezone: %s in %s\n", ($this->is_date?"":"Time"), $date, $this->tzid );
296 parent::__construct($date, $dtz);
297 if ( isset($is_date) ) $this->is_date = $is_date;
299 return $this;
303 public function __toString() {
304 return (string)parent::format(self::$Format) . ' ' . parent::getTimeZone()->getName();
308 public function AsDate() {
309 return $this->format('Ymd');
313 public function setAsFloat() {
314 unset($this->tzid);
318 public function isFloating() {
319 return !isset($this->tzid);
322 public function isDate() {
323 return $this->is_date;
327 public function setAsDate() {
328 $this->is_date = true;
332 public function modify( $interval ) {
333 // print ">>$interval<<\n";
334 if ( preg_match('{^(-)?P(([0-9-]+)W)?(([0-9-]+)D)?T?(([0-9-]+)H)?(([0-9-]+)M)?(([0-9-]+)S)?$}', $interval, $matches) ) {
335 $minus = (isset($matches[1])?$matches[1]:'');
336 $interval = '';
337 if ( isset($matches[2]) && $matches[2] != '' ) $interval .= $minus . $matches[3] . ' weeks ';
338 if ( isset($matches[4]) && $matches[4] != '' ) $interval .= $minus . $matches[5] . ' days ';
339 if ( isset($matches[6]) && $matches[6] != '' ) $interval .= $minus . $matches[7] . ' hours ';
340 if ( isset($matches[8]) && $matches[8] != '' ) $interval .= $minus . $matches[9] . ' minutes ';
341 if (isset($matches[10]) &&$matches[10] != '' ) $interval .= $minus . $matches[11] . ' seconds ';
343 // printf( "Modify '%s' by: >>%s<<\n", $this->__toString(), $interval );
344 // print_r($this);
345 if ( !isset($interval) || $interval == '' ) $interval = '1 day';
346 if ( parent::format('d') > 28 && strstr($interval,'month') !== false ) {
347 $this->setDate(null,null,28);
349 parent::modify($interval);
350 return $this->__toString();
355 * Always returns a time localised to UTC. Even floating times are converted to UTC
356 * using the server's currently configured PHP timezone. Even dates will include a
357 * time, which will be non-zero if they were localised dates.
359 * @see RepeatRuleDateTime::FloatOrUTC()
361 public function UTC($fmt = 'Ymd\THis\Z' ) {
362 $gmt = clone($this);
363 if ( $this->tzid != 'UTC' ) {
364 if ( isset($this->tzid)) {
365 $dtz = parent::getTimezone();
367 else {
368 $dtz = new DateTimeZone(date_default_timezone_get());
370 $offset = 0 - $dtz->getOffset($gmt);
371 $gmt->modify( $offset . ' seconds' );
373 return $gmt->format($fmt);
378 * If this is a localised time then this will return the UTC equivalent. If it is a
379 * floating time, then you will just get the floating time. If it is a date then it
380 * will be returned as a date. Note that if it is a *localised* date then the answer
381 * will still be the UTC equivalent but only the date itself will be returned.
383 * If return_floating_times is true then all dates will be returned as floating times
384 * and UTC will not be returned.
386 * @see RepeatRuleDateTime::UTC()
388 public function FloatOrUTC($return_floating_times = false) {
389 $gmt = clone($this);
390 if ( !$return_floating_times && isset($this->tzid) && $this->tzid != 'UTC' ) {
391 $dtz = parent::getTimezone();
392 $offset = 0 - $dtz->getOffset($gmt);
393 $gmt->modify( $offset . ' seconds' );
395 if ( $this->is_date ) return $gmt->format('Ymd');
396 if ( $return_floating_times ) return $gmt->format('Ymd\THis');
397 return $gmt->format('Ymd\THis') . (!$return_floating_times && isset($this->tzid) ? 'Z' : '');
402 * Returns the string following a property name for an RFC5545 DATE-TIME value.
404 public function RFC5545($return_floating_times = false) {
405 $result = '';
406 if ( isset($this->tzid) && $this->tzid != 'UTC' ) {
407 $result = ';TZID='.$this->tzid;
409 if ( $this->is_date ) {
410 $result .= ';VALUE=DATE:' . $this->format('Ymd');
412 else {
413 $result .= ':' . $this->format('Ymd\THis');
414 if ( !$return_floating_times && isset($this->tzid) && $this->tzid == 'UTC' ) {
415 $result .= 'Z';
418 return $result;
422 public function setTimeZone( $tz ) {
423 if ( is_string($tz) ) {
424 $tz = new RepeatRuleTimeZone($tz);
425 $this->tzid = $tz->tzid();
427 parent::setTimeZone( $tz );
428 return $this;
432 public function getTimeZone() {
433 return $this->tzid;
438 * Returns a 1 if this year is a leap year, otherwise a 0
439 * @param int $year The year we are quizzical about.
440 * @return 1 if this is a leap year, 0 otherwise
442 public static function hasLeapDay($year) {
443 if ( ($year % 4) == 0 && (($year % 100) != 0 || ($year % 400) == 0) ) return 1;
444 return 0;
448 * Returns the number of days in a year/month pair
449 * @param int $year
450 * @param int $month
451 * @return int the number of days in the month
453 public static function daysInMonth( $year, $month ) {
454 if ($month == 4 || $month == 6 || $month == 9 || $month == 11) return 30;
455 else if ($month != 2) return 31;
456 return 28 + RepeatRuleDateTime::hasLeapDay($year);
460 function setDate( $year=null, $month=null, $day=null ) {
461 if ( !isset($year) ) $year = parent::format('Y');
462 if ( !isset($month) ) $month = parent::format('m');
463 if ( !isset($day) ) $day = parent::format('d');
464 if ( $day < 0 ) {
465 $day += RepeatRuleDateTime::daysInMonth($year, $month) + 1;
467 parent::setDate( $year , $month , $day );
468 return $this;
471 function setYearDay( $yearday ) {
472 if ( $yearday > 0 ) {
473 $current_yearday = parent::format('z') + 1;
475 else {
476 $current_yearday = (parent::format('z') - (365 + parent::format('L')));
478 $diff = $yearday - $current_yearday;
479 if ( $diff < 0 ) $this->modify('-P'.-$diff.'D');
480 else if ( $diff > 0 ) $this->modify('P'.$diff.'D');
481 // printf( "Current: %d, Looking for: %d, Diff: %d, What we got: %s (%d,%d)\n", $current_yearday, $yearday, $diff,
482 // parent::format('Y-m-d'), (parent::format('z')+1), ((parent::format('z') - (365 + parent::format('L')))) );
483 return $this;
486 function year() {
487 return parent::format('Y');
490 function month() {
491 return parent::format('m');
494 function day() {
495 return parent::format('d');
498 function hour() {
499 return parent::format('H');
502 function minute() {
503 return parent::format('i');
506 function second() {
507 return parent::format('s');
510 function epoch() {
511 return parent::format('U');
517 * This class is used to hold a pair of dates defining a range. The range may be open-ended by including
518 * a null for one end or the other, or both.
520 * @author Andrew McMillan <andrew@mcmillan.net.nz>
522 class RepeatRuleDateRange {
523 public $from;
524 public $until;
527 * Construct a new RepeatRuleDateRange which will be the range between $date1 and $date2. The earliest of the two
528 * dates will be used as the start of the period, the latest as the end. If one of the dates is null then the order
529 * of the parameters is significant, with the null treated as -infinity if it is first, or +infinity if it is second.
530 * If both parameters are null then the range is from -infinity to +infinity.
532 * @param RepeatRuleDateTime $date1
533 * @param RepeatRuleDateTime $date2
535 function __construct( $date1, $date2 ) {
536 if ( $date1 != null && $date2 != null && $date1 > $date2 ) {
537 $this->from = $date2;
538 $this->until = $date1;
540 else {
541 $this->from = $date1;
542 $this->until = $date2;
547 * Assess whether this range overlaps the supplied range. null values are treated as infinity.
548 * @param RepeatRuleDateRange $other
549 * @return boolean
551 function overlaps( RepeatRuleDateRange $other ) {
552 if ( ($this->until == null && $this->from == null) || ($other->until == null && $other->from == null ) ) return true;
553 if ( $this->until == null && $other->until == null ) return true;
554 if ( $this->from == null && $other->from == null ) return true;
556 if ( $this->until == null ) return ($other->until > $this->from);
557 if ( $this->from == null ) return ($other->from < $this->until);
558 if ( $other->until == null ) return ($this->until > $other->from);
559 if ( $other->from == null ) return ($thi->from < $other->until);
561 return !( $this->until < $other->from || $this->from > $other->until );
565 * Get an Rfc5545Duration from this date range. If the from date is null it will be null.
566 * If the until date is null the duration will either be 1 day (if the from is a date) or 0 otherwise.
568 * @return NULL|Rfc5545Duration
570 function getDuration() {
571 if ( !isset($this->from) ) return null;
572 if ( $this->from->isDate() && !isset($this->until) )
573 $duration = 'P1D';
574 else if ( !isset($this->until) )
575 $duration = 'P0D';
576 else
577 $duration = ( $this->until->epoch() - $this->from->epoch() );
578 return new Rfc5545Duration( $duration );
584 * This class is an implementation of RRULE parsing and expansion, as per RFC5545. It should be reasonably
585 * complete, except that it does not handle changing the WKST - there may be a few errors in unusual rules
586 * also, but all of the common cases should be handled correctly.
588 * @author Andrew McMillan <andrew@mcmillan.net.nz>
590 class RepeatRule {
592 private $base;
593 private $until;
594 private $freq;
595 private $count;
596 private $interval;
597 private $bysecond;
598 private $byminute;
599 private $byhour;
600 private $bymonthday;
601 private $byyearday;
602 private $byweekno;
603 private $byday;
604 private $bymonth;
605 private $bysetpos;
606 private $wkst;
608 private $instances;
609 private $position;
610 private $finished;
611 private $current_base;
612 private $original_rule;
615 public function __construct( $basedate, $rrule, $is_date=null, $return_floating_times=false ) {
616 if ( $return_floating_times ) $basedate->setAsFloat();
617 $this->base = (is_object($basedate) ? $basedate : new RepeatRuleDateTime($basedate) );
618 $this->original_rule = $rrule;
620 if ( DEBUG_RRULE ) {
621 printf( "Constructing RRULE based on: '%s', rrule: '%s' (we float: %s)\n", $basedate, $rrule, ($return_floating_times?"yes":"no") );
624 if ( preg_match('{FREQ=([A-Z]+)(;|$)}', $rrule, $m) ) $this->freq = $m[1];
626 if ( preg_match('{UNTIL=([0-9TZ]+)(;|$)}', $rrule, $m) )
627 $this->until = new RepeatRuleDateTime($m[1],$this->base->getTimeZone(),$is_date);
628 if ( preg_match('{COUNT=([0-9]+)(;|$)}', $rrule, $m) ) $this->count = $m[1];
629 if ( preg_match('{INTERVAL=([0-9]+)(;|$)}', $rrule, $m) ) $this->interval = $m[1];
631 if ( preg_match('{WKST=(MO|TU|WE|TH|FR|SA|SU)(;|$)}', $rrule, $m) ) $this->wkst = $m[1];
633 if ( preg_match('{BYDAY=(([+-]?[0-9]{0,2}(MO|TU|WE|TH|FR|SA|SU),?)+)(;|$)}', $rrule, $m) )
634 $this->byday = explode(',',$m[1]);
636 if ( preg_match('{BYYEARDAY=([0-9,+-]+)(;|$)}', $rrule, $m) ) $this->byyearday = explode(',',$m[1]);
637 if ( preg_match('{BYWEEKNO=([0-9,+-]+)(;|$)}', $rrule, $m) ) $this->byweekno = explode(',',$m[1]);
638 if ( preg_match('{BYMONTHDAY=([0-9,+-]+)(;|$)}', $rrule, $m) ) $this->bymonthday = explode(',',$m[1]);
639 if ( preg_match('{BYMONTH=(([+-]?[0-1]?[0-9],?)+)(;|$)}', $rrule, $m) ) $this->bymonth = explode(',',$m[1]);
640 if ( preg_match('{BYSETPOS=(([+-]?[0-9]{1,3},?)+)(;|$)}', $rrule, $m) ) $this->bysetpos = explode(',',$m[1]);
642 if ( preg_match('{BYSECOND=([0-9,]+)(;|$)}', $rrule, $m) ) $this->bysecond = explode(',',$m[1]);
643 if ( preg_match('{BYMINUTE=([0-9,]+)(;|$)}', $rrule, $m) ) $this->byminute = explode(',',$m[1]);
644 if ( preg_match('{BYHOUR=([0-9,]+)(;|$)}', $rrule, $m) ) $this->byhour = explode(',',$m[1]);
646 if ( !isset($this->interval) ) $this->interval = 1;
647 switch( $this->freq ) {
648 case 'SECONDLY': $this->freq_name = 'second'; break;
649 case 'MINUTELY': $this->freq_name = 'minute'; break;
650 case 'HOURLY': $this->freq_name = 'hour'; break;
651 case 'DAILY': $this->freq_name = 'day'; break;
652 case 'WEEKLY': $this->freq_name = 'week'; break;
653 case 'MONTHLY': $this->freq_name = 'month'; break;
654 case 'YEARLY': $this->freq_name = 'year'; break;
655 default:
656 /** need to handle the error, but FREQ is mandatory so unlikely */
658 $this->frequency_string = sprintf('+%d %s', $this->interval, $this->freq_name );
659 if ( DEBUG_RRULE ) printf( "Frequency modify string is: '%s', base is: '%s'\n", $this->frequency_string, $this->base->format('c') );
660 $this->Start($return_floating_times);
665 * If this repeat rule has an UNTIL= or COUNT= then we can know it will end. Eventually.
666 * @return boolean Whether or not one of these properties is present.
668 public function hasLimitedOccurrences() {
669 return ( isset($this->count) || isset($this->until) );
673 public function set_timezone( $tzstring ) {
674 $this->base->setTimezone(new DateTimeZone($tzstring));
678 public function Start($return_floating_times=false) {
679 $this->instances = array();
680 $this->GetMoreInstances($return_floating_times);
681 $this->rewind();
682 $this->finished = false;
686 public function rewind() {
687 $this->position = -1;
692 * Return the next date in the repeating series.
693 * @param boolean $return_floating_times Whether to return dates as floating times.
694 * @return vComponent The next instance.
696 public function next($return_floating_times=false) {
697 $this->position++;
698 return $this->current($return_floating_times);
702 public function current($return_floating_times=false) {
703 if ( !$this->valid() ) return null;
704 if ( !isset($this->instances[$this->position]) ) $this->GetMoreInstances($return_floating_times);
705 if ( !$this->valid() ) return null;
706 if ( DEBUG_RRULE ) printf( "Returning date from position %d: %s (%s)\n", $this->position,
707 $this->instances[$this->position]->format('c'), $this->instances[$this->position]->FloatOrUTC($return_floating_times) );
708 return $this->instances[$this->position];
712 public function key($return_floating_times=false) {
713 if ( !$this->valid() ) return null;
714 if ( !isset($this->instances[$this->position]) ) $this->GetMoreInstances($return_floating_times);
715 if ( !isset($this->keys[$this->position]) ) {
716 $this->keys[$this->position] = $this->instances[$this->position];
718 return $this->keys[$this->position];
722 public function valid() {
723 if ( isset($this->instances[$this->position]) || !$this->finished ) return true;
724 return false;
728 * This function returns an array which lists the order of processing, and whether the processing is
729 * to expand or limit based on this component.
731 * Note that yearly-byday and monthly-byday have special handling which is coded within the
732 * expand_byday() method
733 * @param $freq a string indicating the frequency.
735 private static function rrule_expand_limit( $freq ) {
736 switch( $freq ) {
737 case 'YEARLY':
738 return array( 'bymonth' => 'expand', 'byweekno' => 'expand', 'byyearday' => 'expand', 'bymonthday' => 'expand',
739 'byday' => 'expand', 'byhour' => 'expand', 'byminute' => 'expand', 'bysecond' => 'expand' );
740 case 'MONTHLY':
741 return array( 'bymonth' => 'limit', 'bymonthday' => 'expand',
742 'byday' => 'expand', 'byhour' => 'expand', 'byminute' => 'expand', 'bysecond' => 'expand' );
743 case 'WEEKLY':
744 return array( 'bymonth' => 'limit',
745 'byday' => 'expand', 'byhour' => 'expand', 'byminute' => 'expand', 'bysecond' => 'expand' );
746 case 'DAILY':
747 return array( 'bymonth' => 'limit', 'bymonthday' => 'limit',
748 'byday' => 'limit', 'byhour' => 'expand', 'byminute' => 'expand', 'bysecond' => 'expand' );
749 case 'HOURLY':
750 return array( 'bymonth' => 'limit', 'bymonthday' => 'limit',
751 'byday' => 'limit', 'byhour' => 'limit', 'byminute' => 'expand', 'bysecond' => 'expand' );
752 case 'MINUTELY':
753 return array( 'bymonth' => 'limit', 'bymonthday' => 'limit',
754 'byday' => 'limit', 'byhour' => 'limit', 'byminute' => 'limit', 'bysecond' => 'expand' );
755 case 'SECONDLY':
756 return array( 'bymonth' => 'limit', 'bymonthday' => 'limit',
757 'byday' => 'limit', 'byhour' => 'limit', 'byminute' => 'limit', 'bysecond' => 'limit' );
761 private function GetMoreInstances($return_floating_times=false) {
762 if ( $this->finished ) return;
763 $got_more = false;
764 $loop_limit = 10;
765 $loops = 0;
766 if ( $return_floating_times ) $this->base->setAsFloat();
767 while( !$this->finished && !$got_more && $loops++ < $loop_limit ) {
768 if ( !isset($this->current_base) ) {
769 $this->current_base = clone($this->base);
771 else {
772 $this->current_base->modify( $this->frequency_string );
774 if ( $return_floating_times ) $this->current_base->setAsFloat();
775 if ( DEBUG_RRULE ) printf( "Getting more instances from: '%s' - %d\n", $this->current_base->format('c'), count($this->instances) );
776 $this->current_set = array( clone($this->current_base) );
777 foreach( self::rrule_expand_limit($this->freq) AS $bytype => $action ) {
778 if ( isset($this->{$bytype}) ) {
779 $this->{$action.'_'.$bytype}();
780 if ( !isset($this->current_set[0]) ) break;
784 sort($this->current_set);
785 if ( isset($this->bysetpos) ) $this->limit_bysetpos();
787 $position = count($this->instances) - 1;
788 if ( DEBUG_RRULE ) printf( "Inserting %d from current_set into position %d\n", count($this->current_set), $position + 1 );
789 foreach( $this->current_set AS $k => $instance ) {
790 if ( $instance < $this->base ) continue;
791 if ( isset($this->until) && $instance > $this->until ) {
792 $this->finished = true;
793 return;
795 if ( !isset($this->instances[$position]) || $instance != $this->instances[$position] ) {
796 $got_more = true;
797 $position++;
798 $this->instances[$position] = $instance;
799 if ( DEBUG_RRULE ) printf( "Added date %s into position %d in current set\n", $instance->format('c'), $position );
800 if ( isset($this->count) && ($position + 1) >= $this->count ) {
801 $this->finished = true;
802 return;
810 public static function rrule_day_number( $day ) {
811 switch( $day ) {
812 case 'SU': return 0;
813 case 'MO': return 1;
814 case 'TU': return 2;
815 case 'WE': return 3;
816 case 'TH': return 4;
817 case 'FR': return 5;
818 case 'SA': return 6;
820 return false;
824 static public function date_mask( $date, $y, $mo, $d, $h, $mi, $s ) {
825 $date_parts = explode(',',$date->format('Y,m,d,H,i,s'));
827 if ( isset($y) || isset($mo) || isset($d) ) {
828 if ( isset($y) ) $date_parts[0] = $y;
829 if ( isset($mo) ) $date_parts[1] = $mo;
830 if ( isset($d) ) $date_parts[2] = $d;
831 $date->setDate( $date_parts[0], $date_parts[1], $date_parts[2] );
833 if ( isset($h) || isset($mi) || isset($s) ) {
834 if ( isset($h) ) $date_parts[3] = $h;
835 if ( isset($mi) ) $date_parts[4] = $mi;
836 if ( isset($s) ) $date_parts[5] = $s;
837 $date->setTime( $date_parts[3], $date_parts[4], $date_parts[5] );
839 return $date;
843 private function expand_bymonth() {
844 $instances = $this->current_set;
845 $this->current_set = array();
846 foreach( $instances AS $k => $instance ) {
847 foreach( $this->bymonth AS $k => $month ) {
848 $expanded = $this->date_mask( clone($instance), null, $month, null, null, null, null);
849 if ( DEBUG_RRULE ) printf( "Expanded BYMONTH $month into date %s\n", $expanded->format('c') );
850 $this->current_set[] = $expanded;
855 private function expand_bymonthday() {
856 $instances = $this->current_set;
857 $this->current_set = array();
858 foreach( $instances AS $k => $instance ) {
859 foreach( $this->bymonthday AS $k => $monthday ) {
860 $expanded = $this->date_mask( clone($instance), null, null, $monthday, null, null, null);
861 if ( DEBUG_RRULE ) printf( "Expanded BYMONTHDAY $monthday into date %s from %s\n", $expanded->format('c'), $instance->format('c') );
862 $this->current_set[] = $expanded;
867 private function expand_byyearday() {
868 $instances = $this->current_set;
869 $this->current_set = array();
870 $days_set = array();
871 foreach( $instances AS $k => $instance ) {
872 foreach( $this->byyearday AS $k => $yearday ) {
873 $on_yearday = clone($instance);
874 $on_yearday->setYearDay($yearday);
875 if ( isset($days_set[$on_yearday->UTC()]) ) continue;
876 $this->current_set[] = $on_yearday;
877 $days_set[$on_yearday->UTC()] = true;
882 private function expand_byday_in_week( $day_in_week ) {
885 * @todo This should really allow for WKST, since if we start a series
886 * on (eg.) TH and interval > 1, a MO, TU, FR repeat will not be in the
887 * same week with this code.
889 $dow_of_instance = $day_in_week->format('w'); // 0 == Sunday
890 foreach( $this->byday AS $k => $weekday ) {
891 $dow = self::rrule_day_number($weekday);
892 $offset = $dow - $dow_of_instance;
893 if ( $offset < 0 ) $offset += 7;
894 $expanded = clone($day_in_week);
895 $expanded->modify( sprintf('+%d day', $offset) );
896 $this->current_set[] = $expanded;
897 if ( DEBUG_RRULE ) printf( "Expanded BYDAY(W) $weekday into date %s\n", $expanded->format('c') );
902 private function expand_byday_in_month( $day_in_month ) {
904 $first_of_month = $this->date_mask( clone($day_in_month), null, null, 1, null, null, null);
905 $dow_of_first = $first_of_month->format('w'); // 0 == Sunday
906 $days_in_month = cal_days_in_month(CAL_GREGORIAN, $first_of_month->format('m'), $first_of_month->format('Y'));
907 foreach( $this->byday AS $k => $weekday ) {
908 if ( preg_match('{([+-])?(\d)?(MO|TU|WE|TH|FR|SA|SU)}', $weekday, $matches ) ) {
909 $dow = self::rrule_day_number($matches[3]);
910 $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.
911 $whichweek = intval($matches[2]);
912 if ( DEBUG_RRULE ) printf( "Expanding BYDAY(M) $weekday in month of %s\n", $first_of_month->format('c') );
913 if ( $whichweek > 0 ) {
914 $whichweek--;
915 $monthday = $first_dom;
916 if ( $matches[1] == '-' ) {
917 $monthday += 35;
918 while( $monthday > $days_in_month ) $monthday -= 7;
919 $monthday -= (7 * $whichweek);
921 else {
922 $monthday += (7 * $whichweek);
924 if ( $monthday > 0 && $monthday <= $days_in_month ) {
925 $expanded = $this->date_mask( clone($day_in_month), null, null, $monthday, null, null, null);
926 if ( DEBUG_RRULE ) printf( "Expanded BYDAY(M) $weekday now $monthday into date %s\n", $expanded->format('c') );
927 $this->current_set[] = $expanded;
930 else {
931 for( $monthday = $first_dom; $monthday <= $days_in_month; $monthday += 7 ) {
932 $expanded = $this->date_mask( clone($day_in_month), null, null, $monthday, null, null, null);
933 if ( DEBUG_RRULE ) printf( "Expanded BYDAY(M) $weekday now $monthday into date %s\n", $expanded->format('c') );
934 $this->current_set[] = $expanded;
942 private function expand_byday_in_year( $day_in_year ) {
944 $first_of_year = $this->date_mask( clone($day_in_year), null, 1, 1, null, null, null);
945 $dow_of_first = $first_of_year->format('w'); // 0 == Sunday
946 $days_in_year = 337 + cal_days_in_month(CAL_GREGORIAN, 2, $first_of_year->format('Y'));
947 foreach( $this->byday AS $k => $weekday ) {
948 if ( preg_match('{([+-])?(\d)?(MO|TU|WE|TH|FR|SA|SU)}', $weekday, $matches ) ) {
949 $expanded = clone($first_of_year);
950 $dow = self::rrule_day_number($matches[3]);
951 $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.
952 $whichweek = intval($matches[2]);
953 if ( DEBUG_RRULE ) printf( "Expanding BYDAY(Y) $weekday from date %s\n", $instance->format('c') );
954 if ( $whichweek > 0 ) {
955 $whichweek--;
956 $yearday = $first_doy;
957 if ( $matches[1] == '-' ) {
958 $yearday += 371;
959 while( $yearday > $days_in_year ) $yearday -= 7;
960 $yearday -= (7 * $whichweek);
962 else {
963 $yearday += (7 * $whichweek);
965 if ( $yearday > 0 && $yearday <= $days_in_year ) {
966 $expanded->modify(sprintf('+%d day', $yearday - 1));
967 if ( DEBUG_RRULE ) printf( "Expanded BYDAY(Y) $weekday now $yearday into date %s\n", $expanded->format('c') );
968 $this->current_set[] = $expanded;
971 else {
972 $expanded->modify(sprintf('+%d day', $first_doy - 1));
973 for( $yearday = $first_doy; $yearday <= $days_in_year; $yearday += 7 ) {
974 if ( DEBUG_RRULE ) printf( "Expanded BYDAY(Y) $weekday now $yearday into date %s\n", $expanded->format('c') );
975 $this->current_set[] = clone($expanded);
976 $expanded->modify('+1 week');
984 private function expand_byday() {
985 if ( !isset($this->current_set[0]) ) return;
986 if ( $this->freq == 'MONTHLY' || $this->freq == 'YEARLY' ) {
987 if ( isset($this->bymonthday) || isset($this->byyearday) ) {
988 $this->limit_byday(); /** Per RFC5545 3.3.10 from note 1&2 to table */
989 return;
992 $instances = $this->current_set;
993 $this->current_set = array();
994 foreach( $instances AS $k => $instance ) {
995 if ( $this->freq == 'MONTHLY' ) {
996 $this->expand_byday_in_month($instance);
998 else if ( $this->freq == 'WEEKLY' ) {
999 $this->expand_byday_in_week($instance);
1001 else { // YEARLY
1002 if ( isset($this->bymonth) ) {
1003 $this->expand_byday_in_month($instance);
1005 else if ( isset($this->byweekno) ) {
1006 $this->expand_byday_in_week($instance);
1008 else {
1009 $this->expand_byday_in_year($instance);
1016 private function expand_byhour() {
1017 $instances = $this->current_set;
1018 $this->current_set = array();
1019 foreach( $instances AS $k => $instance ) {
1020 foreach( $this->bymonth AS $k => $month ) {
1021 $this->current_set[] = $this->date_mask( clone($instance), null, null, null, $hour, null, null);
1026 private function expand_byminute() {
1027 $instances = $this->current_set;
1028 $this->current_set = array();
1029 foreach( $instances AS $k => $instance ) {
1030 foreach( $this->bymonth AS $k => $month ) {
1031 $this->current_set[] = $this->date_mask( clone($instance), null, null, null, null, $minute, null);
1036 private function expand_bysecond() {
1037 $instances = $this->current_set;
1038 $this->current_set = array();
1039 foreach( $instances AS $k => $instance ) {
1040 foreach( $this->bymonth AS $k => $second ) {
1041 $this->current_set[] = $this->date_mask( clone($instance), null, null, null, null, null, $second);
1047 private function limit_generally( $fmt_char, $element_name ) {
1048 $instances = $this->current_set;
1049 $this->current_set = array();
1050 foreach( $instances AS $k => $instance ) {
1051 foreach( $this->{$element_name} AS $k => $element_value ) {
1052 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') );
1053 if ( $instance->format($fmt_char) == $element_value ) $this->current_set[] = $instance;
1058 private function limit_byday() {
1059 $fmt_char = 'w';
1060 $instances = $this->current_set;
1061 $this->current_set = array();
1062 foreach( $this->byday AS $k => $weekday ) {
1063 $dow = self::rrule_day_number($weekday);
1064 foreach( $instances AS $k => $instance ) {
1065 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') );
1066 if ( $instance->format($fmt_char) == $dow ) $this->current_set[] = $instance;
1071 private function limit_bymonth() { $this->limit_generally( 'm', 'bymonth' ); }
1072 private function limit_byyearday() { $this->limit_generally( 'z', 'byyearday' ); }
1073 private function limit_bymonthday() { $this->limit_generally( 'd', 'bymonthday' ); }
1074 private function limit_byhour() { $this->limit_generally( 'H', 'byhour' ); }
1075 private function limit_byminute() { $this->limit_generally( 'i', 'byminute' ); }
1076 private function limit_bysecond() { $this->limit_generally( 's', 'bysecond' ); }
1079 private function limit_bysetpos( ) {
1080 $instances = $this->current_set;
1081 $count = count($instances);
1082 $this->current_set = array();
1083 foreach( $this->bysetpos AS $k => $element_value ) {
1084 if ( DEBUG_RRULE ) printf( "Limiting bysetpos %s of %d instances\n", $element_value, $count );
1085 if ( $element_value > 0 ) {
1086 $this->current_set[] = $instances[$element_value - 1];
1088 else if ( $element_value < 0 ) {
1089 $this->current_set[] = $instances[$count + $element_value];
1099 require_once("vComponent.php");
1102 * Expand the event instances for an RDATE or EXDATE property
1104 * @param string $property RDATE or EXDATE, depending...
1105 * @param array $component A vComponent which is a VEVENT, VTODO or VJOURNAL
1106 * @param array $range_end A date after which we care less about expansion
1108 * @return array An array keyed on the UTC dates, referring to the component
1110 function rdate_expand( $dtstart, $property, $component, $range_end = null, $is_date=null, $return_floating_times=false ) {
1111 $properties = $component->GetProperties($property);
1112 $expansion = array();
1113 foreach( $properties AS $p ) {
1114 $timezone = $p->GetParameterValue('TZID');
1115 $rdate = $p->Value();
1116 $rdates = explode( ',', $rdate );
1117 foreach( $rdates AS $k => $v ) {
1118 $rdate = new RepeatRuleDateTime( $v, $timezone, $is_date);
1119 if ( $return_floating_times ) $rdate->setAsFloat();
1120 $expansion[$rdate->FloatOrUTC($return_floating_times)] = $component;
1121 if ( $rdate > $range_end ) break;
1124 return $expansion;
1129 * Expand the event instances for an RRULE property
1131 * @param object $dtstart A RepeatRuleDateTime which is the master dtstart
1132 * @param string $property RDATE or EXDATE, depending...
1133 * @param array $component A vComponent which is a VEVENT, VTODO or VJOURNAL
1134 * @param array $range_end A date after which we care less about expansion
1136 * @return array An array keyed on the UTC dates, referring to the component
1138 function rrule_expand( $dtstart, $property, $component, $range_end, $is_date=null, $return_floating_times=false ) {
1139 $expansion = array();
1141 $recur = $component->GetProperty($property);
1142 if ( !isset($recur) ) return $expansion;
1143 $recur = $recur->Value();
1145 $this_start = $component->GetProperty('DTSTART');
1146 if ( isset($this_start) ) {
1147 $this_start = new RepeatRuleDateTime($this_start);
1149 else {
1150 $this_start = clone($dtstart);
1152 if ( $return_floating_times ) $this_start->setAsFloat();
1154 // if ( DEBUG_RRULE ) print_r( $this_start );
1155 if ( DEBUG_RRULE ) printf( "RRULE: %s (floating: %s)\n", $recur, ($return_floating_times?"yes":"no") );
1156 $rule = new RepeatRule( $this_start, $recur, $is_date, $return_floating_times );
1157 $i = 0;
1158 $result_limit = 1000;
1159 while( $date = $rule->next($return_floating_times) ) {
1160 // if ( DEBUG_RRULE ) printf( "[%3d] %s\n", $i, $date->UTC() );
1161 $expansion[$date->FloatOrUTC($return_floating_times)] = $component;
1162 if ( $i++ >= $result_limit || $date > $range_end ) break;
1164 // if ( DEBUG_RRULE ) print_r( $expansion );
1165 return $expansion;
1170 * Expand the event instances for an iCalendar VEVENT (or VTODO)
1172 * Note: expansion here does not apply modifications to instances other than modifying start/end/due/duration.
1174 * @param object $vResource A vComponent which is a VCALENDAR containing components needing expansion
1175 * @param object $range_start A RepeatRuleDateTime which is the beginning of the range for events, default -6 weeks
1176 * @param object $range_end A RepeatRuleDateTime which is the end of the range for events, default +6 weeks
1178 * @return vComponent The original vComponent, with the instances of the internal components expanded.
1180 function expand_event_instances( $vResource, $range_start = null, $range_end = null, $return_floating_times=false ) {
1181 global $c;
1182 $components = $vResource->GetComponents();
1184 if ( empty($range_start) ) { $range_start = new RepeatRuleDateTime(); $range_start->modify('-6 weeks'); }
1185 if ( empty($range_end) ) {
1186 $range_end = clone($range_start);
1187 $range_end->modify('+6 months');
1190 $new_components = array();
1191 $instances = array();
1192 $expand = false;
1193 $dtstart = null;
1194 $is_date = false;
1195 $has_repeats = false;
1196 $dtstart_type = 'DTSTART';
1197 foreach( $components AS $k => $comp ) {
1198 if ( $comp->GetType() != 'VEVENT' && $comp->GetType() != 'VTODO' && $comp->GetType() != 'VJOURNAL' ) {
1199 if ( $comp->GetType() != 'VTIMEZONE' ) $new_components[] = $comp;
1200 continue;
1202 if ( !isset($dtstart) ) {
1203 $dtstart_prop = $comp->GetProperty($dtstart_type);
1204 if ( !isset($dtstart_prop) && $comp->GetType() != 'VTODO' ) {
1205 $dtstart_type = 'DUE';
1206 $dtstart_prop = $comp->GetProperty($dtstart_type);
1208 if ( !isset($dtstart_prop) ) continue;
1209 $dtstart = new RepeatRuleDateTime( $dtstart_prop );
1210 if ( $return_floating_times ) $dtstart->setAsFloat();
1211 if ( DEBUG_RRULE ) printf( "Component is: %s (floating: %s)\n", $comp->GetType(), ($return_floating_times?"yes":"no") );
1212 $is_date = $dtstart->isDate();
1213 $instances[$dtstart->FloatOrUTC($return_floating_times)] = $comp;
1214 $rrule = $comp->GetProperty('RRULE');
1215 $has_repeats = isset($rrule);
1217 $p = $comp->GetProperty('RECURRENCE-ID');
1218 if ( isset($p) && $p->Value() != '' ) {
1219 $range = $p->GetParameterValue('RANGE');
1220 $recur_utc = new RepeatRuleDateTime($p);
1221 if ( $is_date ) $recur_utc->setAsDate();
1222 $recur_utc = $recur_utc->FloatOrUTC($return_floating_times);
1223 if ( isset($range) && $range == 'THISANDFUTURE' ) {
1224 foreach( $instances AS $k => $v ) {
1225 if ( DEBUG_RRULE ) printf( "Removing overridden instance at: $k\n" );
1226 if ( $k >= $recur_utc ) unset($instances[$k]);
1229 else {
1230 unset($instances[$recur_utc]);
1233 else if ( DEBUG_RRULE ) {
1234 $p = $comp->GetProperty('SUMMARY');
1235 $summary = ( isset($p) ? $p->Value() : 'not set');
1236 $p = $comp->GetProperty('UID');
1237 $uid = ( isset($p) ? $p->Value() : 'not set');
1238 printf( "Processing event '%s' with UID '%s' starting on %s\n",
1239 $summary, $uid, $dtstart->FloatOrUTC($return_floating_times) );
1240 print( "Instances at start");
1241 foreach( $instances AS $k => $v ) {
1242 print ' : '.$k;
1244 print "\n";
1246 $instances += rrule_expand($dtstart, 'RRULE', $comp, $range_end, null, $return_floating_times);
1247 if ( DEBUG_RRULE ) {
1248 print( "After rrule_expand");
1249 foreach( $instances AS $k => $v ) {
1250 print ' : '.$k;
1252 print "\n";
1254 $instances += rdate_expand($dtstart, 'RDATE', $comp, $range_end, null, $return_floating_times);
1255 if ( DEBUG_RRULE ) {
1256 print( "After rdate_expand");
1257 foreach( $instances AS $k => $v ) {
1258 print ' : '.$k;
1260 print "\n";
1262 foreach ( rdate_expand($dtstart, 'EXDATE', $comp, $range_end, null, $return_floating_times) AS $k => $v ) {
1263 unset($instances[$k]);
1265 if ( DEBUG_RRULE ) {
1266 print( "After exdate_expand");
1267 foreach( $instances AS $k => $v ) {
1268 print ' : '.$k;
1270 print "\n";
1274 $last_duration = null;
1275 $early_start = null;
1276 $new_components = array();
1277 $start_utc = $range_start->FloatOrUTC($return_floating_times);
1278 $end_utc = $range_end->FloatOrUTC($return_floating_times);
1279 foreach( $instances AS $utc => $comp ) {
1280 if ( $utc > $end_utc ) {
1281 if ( DEBUG_RRULE ) printf( "We're done: $utc is out of the range.\n");
1282 break;
1285 $end_type = ($comp->GetType() == 'VTODO' ? 'DUE' : 'DTEND');
1286 $duration = $comp->GetProperty('DURATION');
1287 if ( !isset($duration) || $duration->Value() == '' ) {
1288 $instance_start = $comp->GetProperty($dtstart_type);
1289 $dtsrt = new RepeatRuleDateTime( $instance_start );
1290 if ( $return_floating_times ) $dtsrt->setAsFloat();
1291 $instance_end = $comp->GetProperty($end_type);
1292 if ( isset($instance_end) ) {
1293 $dtend = new RepeatRuleDateTime( $instance_end );
1294 $duration = Rfc5545Duration::fromTwoDates($dtsrt, $dtend);
1296 else {
1297 if ( $instance_start->GetParameterValue('VALUE') == 'DATE' ) {
1298 $duration = new Rfc5545Duration('P1D');
1300 else {
1301 $duration = new Rfc5545Duration(0);
1305 else {
1306 $duration = new Rfc5545Duration($duration->Value());
1309 if ( $utc < $start_utc ) {
1310 if ( isset($early_start) && isset($last_duration) && $duration->equals($last_duration) ) {
1311 if ( $utc < $early_start ) {
1312 if ( DEBUG_RRULE ) printf( "Next please: $utc is before $early_start and before $start_utc.\n");
1313 continue;
1316 else {
1317 /** Calculate the latest possible start date when this event would overlap our range start */
1318 $latest_start = clone($range_start);
1319 $latest_start->modify('-'.$duration);
1320 $early_start = $latest_start->FloatOrUTC($return_floating_times);
1321 $last_duration = $duration;
1322 if ( $utc < $early_start ) {
1323 if ( DEBUG_RRULE ) printf( "Another please: $utc is before $early_start and before $start_utc.\n");
1324 continue;
1328 $component = clone($comp);
1329 if ( isset($c->expanded_instances_include_rrule) ) {
1330 $component->ClearProperties( array('DTSTART'=> true, 'DUE' => true, 'DTEND' => true ) );
1332 else {
1333 $component->ClearProperties( array('DTSTART'=> true, 'DUE' => true, 'DTEND' => true,
1334 'RRULE' => true, 'RDATE' => true, 'EXDATE' => true) );
1336 $component->AddProperty('DTSTART', $utc, ($is_date ? array('VALUE' => 'DATE') : null) );
1337 $component->AddProperty('DURATION', $duration );
1338 if ( $has_repeats && $dtstart->FloatOrUTC($return_floating_times) != $utc )
1339 $component->AddProperty('RECURRENCE-ID', $utc, ($is_date ? array('VALUE' => 'DATE') : null) );
1340 $new_components[$utc] = $component;
1343 // Add overriden instances
1344 foreach( $components AS $k => $comp ) {
1345 $p = $comp->GetProperty('RECURRENCE-ID');
1346 if ( isset($p) && $p->Value() != '') {
1347 if ( !isset($new_components[$p->Value()]) ) {
1348 // The component we're replacing is outside the range. Unless the replacement
1349 // is *in* the range we will move along to the next one.
1350 $dtstart_prop = $comp->GetProperty($dtstart_type);
1351 if ( !isset($dtstart_prop) ) continue; // No start: no expansion. Note that we consider 'DUE' to be a start if DTSTART is missing
1352 $dtstart = new RepeatRuleDateTime( $dtstart_prop );
1353 $is_date = $dtstart->isDate();
1354 if ( $return_floating_times ) $dtstart->setAsFloat();
1355 $dtstart = $dtstart->FloatOrUTC($return_floating_times);
1356 if ( $dtstart > $end_utc ) continue; // Start after end of range, skip it
1358 $end_type = ($comp->GetType() == 'VTODO' ? 'DUE' : 'DTEND');
1359 $duration = $comp->GetProperty('DURATION');
1360 if ( !isset($duration) || $duration->Value() == '' ) {
1361 $instance_end = $comp->GetProperty($end_type);
1362 if ( isset($instance_end) ) {
1363 $dtend = new RepeatRuleDateTime( $instance_end );
1364 if ( $return_floating_times ) $dtend->setAsFloat();
1365 $dtend = $dtend->FloatOrUTC($return_floating_times);
1367 else {
1368 $dtend = $dtstart + ($is_date ? $dtstart + 86400 : 0 );
1371 else {
1372 $duration = new Rfc5545Duration($duration->Value());
1373 $dtend = $dtstart + $duration->asSeconds();
1375 if ( $dtend < $start_utc ) continue; // End before start of range: skip that too.
1377 if ( DEBUG_RRULE ) printf( "Replacing overridden instance at %s\n", $p->Value());
1378 $new_components[$p->Value()] = $comp;
1382 $vResource->SetComponents($new_components);
1384 return $vResource;
1389 * Return a date range for this component.
1390 * @param vComponent $comp
1391 * @throws Exception (1) When DTSTART is not present but the RFC says MUST and (2) when we get an unsupported component
1392 * @return RepeatRuleDateRange
1394 function getComponentRange(vComponent $comp) {
1395 $dtstart_prop = $comp->GetProperty('DTSTART');
1396 $duration_prop = $comp->GetProperty('DURATION');
1397 if ( isset($duration_prop) ) {
1398 if ( !isset($dtstart_prop) ) throw new Exception('Invalid '.$comp->GetType().' containing DURATION without DTSTART', 0);
1399 $dtstart = new RepeatRuleDateTime($dtstart_prop);
1400 $dtend = clone($dtstart);
1401 $dtend->modify(new Rfc5545Duration($duration_prop->Value()));
1403 else {
1404 $completed_prop = null;
1405 switch ( $comp->GetType() ) {
1406 case 'VEVENT':
1407 if ( !isset($dtstart_prop) ) throw new Exception('Invalid VEVENT without DTSTART', 0);
1408 $dtend_prop = $comp->GetProperty('DTEND');
1409 break;
1410 case 'VTODO':
1411 $completed_prop = $comp->GetProperty('COMPLETED');
1412 $dtend_prop = $comp->GetProperty('DUE');
1413 break;
1414 case 'VJOURNAL':
1415 if ( !isset($dtstart_prop) )
1416 $dtstart_prop = $comp->GetProperty('DTSTAMP');
1417 $dtend_prop = $dtstart_prop;
1418 default:
1419 throw new Exception('getComponentRange cannot handle "'.$comp->GetType().'" components', 0);
1422 if ( isset($dtstart_prop) )
1423 $dtstart = new RepeatRuleDateTime($dtstart_prop);
1424 else
1425 $dtstart = null;
1427 if ( isset($dtend_prop) )
1428 $dtend = new RepeatRuleDateTime($dtend_prop);
1429 else
1430 $dtend = null;
1432 if ( isset($completed_prop) ) {
1433 $completed = new RepeatRuleDateTime($completed_prop);
1434 if ( !isset($dtstart) || (isset($dtstart) && $completed < $dtstart) ) $dtstart = $completed;
1435 if ( !isset($dtend) || (isset($dtend) && $completed > $dtend) ) $dtend = $completed;
1438 return new RepeatRuleDateRange($dtstart, $dtend);
1442 * Return a RepeatRuleDateRange from the earliest start to the latest end of the event.
1444 * @todo: This should probably be made part of the VCalendar object when we move the RRule.php into AWL.
1446 * @param object $vResource A vComponent which is a VCALENDAR containing components needing expansion
1447 * @return RepeatRuleDateRange Representing the range of time covered by the event.
1449 function getVCalendarRange( $vResource ) {
1450 global $c;
1451 $components = $vResource->GetComponents();
1453 $dtstart = null;
1454 $duration = null;
1455 $earliest_start = null;
1456 $latest_end = null;
1457 $has_repeats = false;
1458 foreach( $components AS $k => $comp ) {
1459 if ( $comp->GetType() == 'VTIMEZONE' ) continue;
1460 $range = getComponentRange($comp);
1461 $dtstart = $range->from;
1462 if ( !isset($dtstart) ) continue;
1463 $duration = $range->getDuration();
1465 $rrule = $comp->GetProperty('RRULE');
1466 $limited_occurrences = true;
1467 if ( isset($rrule) ) {
1468 $rule = new RepeatRule($dtstart, $rrule);
1469 $limited_occurrences = $rule->hasLimitedOccurrences();
1472 if ( $limited_occurrences ) {
1473 $instances = array();
1474 $instances[$dtstart->FloatOrUTC()] = $dtstart;
1475 if ( !isset($range_end) ) {
1476 $range_end = new RepeatRuleDateTime();
1477 $range_end->modify('+150 years');
1479 $instances += rrule_expand($dtstart, 'RRULE', $comp, $range_end);
1480 $instances += rdate_expand($dtstart, 'RDATE', $comp, $range_end);
1481 foreach ( rdate_expand($dtstart, 'EXDATE', $comp, $range_end) AS $k => $v ) {
1482 unset($instances[$k]);
1484 $instances = array_keys($instances);
1485 asort($instances);
1486 $first = new RepeatRuleDateTime($instances[0]);
1487 $last = new RepeatRuleDateTime($instances[count($instances)-1]);
1488 $last->modify($duration);
1489 if ( empty($earliest_start) || $first < $earliest_start ) $earliest_start = $first;
1490 if ( empty($latest_end) || $last > $latest_end ) $latest_end = $last;
1492 else {
1493 if ( empty($earliest_start) || $dtstart < $earliest_start ) $earliest_start = $dtstart;
1494 $latest_end = null;
1495 break;
1499 return new RepeatRuleDateRange($earliest_start, $latest_end );