source: contrib/davical/inc/RRule.php @ 3733

Revision 3733, 28.9 KB checked in by gabriel.malheiros, 13 years ago (diff)

Ticket #1541 - <Davical customizado para o Expresso.Utiliza Caldav e CardDav?>

Line 
1<?php
2/**
3* Class for parsing RRule and getting us the dates
4*
5* @package   awl
6* @subpackage   caldav
7* @author    Andrew McMillan <andrew@catalyst.net.nz>
8* @copyright Catalyst .Net Ltd
9* @license   http://gnu.org/copyleft/gpl.html GNU GPL v2
10*/
11
12$ical_weekdays = array( 'SU' => 0, 'MO' => 1, 'TU' => 2, 'WE' => 3, 'TH' => 4, 'FR' => 5, 'SA' => 6 );
13
14/**
15* A Class for handling dates in iCalendar format.  We do make the simplifying assumption
16* that all date handling in here is normalised to GMT.  One day we might provide some
17* functions to do that, but for now it is done externally.
18*
19* @package awl
20*/
21class iCalDate {
22  /**#@+
23  * @access private
24  */
25
26  /** Text version */
27  var $_text;
28
29  /** Epoch version */
30  var $_epoch;
31
32  /** Fragmented parts */
33  var $_yy;
34  var $_mo;
35  var $_dd;
36  var $_hh;
37  var $_mi;
38  var $_ss;
39  var $_tz;
40
41  /** Which day of the week does the week start on */
42  var $_wkst;
43
44  /**#@-*/
45
46  /**
47  * The constructor takes either an iCalendar date, a text string formatted as
48  * an iCalendar date, or epoch seconds.
49  */
50  function iCalDate( $input ) {
51    if ( gettype($input) == 'object' ) {
52      $this->_text = $input->_text;
53      $this->_epoch = $input->_epoch;
54      $this->_yy = $input->_yy;
55      $this->_mo = $input->_mo;
56      $this->_dd = $input->_dd;
57      $this->_hh = $input->_hh;
58      $this->_mi = $input->_mi;
59      $this->_ss = $input->_ss;
60      $this->_tz = $input->_tz;
61      return;
62    }
63
64    $this->_wkst = 1; // Monday
65    if ( preg_match( '/^\d{8}[T ]\d{6}$/', $input ) ) {
66      $this->SetLocalDate($input);
67    }
68    else if ( preg_match( '/^\d{8}[T ]\d{6}Z$/', $input ) ) {
69      $this->SetGMTDate($input);
70    }
71    else if ( intval($input) == 0 ) {
72      $this->SetLocalDate(strtotime($input));
73      return;
74    }
75    else {
76      $this->SetEpochDate($input);
77    }
78  }
79
80
81  /**
82  * Set the date from a text string
83  */
84  function SetGMTDate( $input ) {
85    $this->_text = $input;
86    $this->_PartsFromText();
87    $this->_GMTEpochFromParts();
88  }
89
90
91  /**
92  * Set the date from a text string
93  */
94  function SetLocalDate( $input ) {
95    $this->_text = $input;
96    $this->_PartsFromText();
97    $this->_EpochFromParts();
98  }
99
100
101  /**
102  * Set the date from an epoch
103  */
104  function SetEpochDate( $input ) {
105    $this->_epoch = intval($input);
106    $this->_TextFromEpoch();
107    $this->_PartsFromText();
108  }
109
110
111  /**
112  * Given an epoch date, convert it to text
113  */
114  function _TextFromEpoch() {
115    $this->_text = date('Ymd\THis', $this->_epoch );
116//    dbg_error_log( "RRule", " Text %s from epoch %d", $this->_text, $this->_epoch );
117  }
118
119  /**
120  * Given a GMT epoch date, convert it to text
121  */
122  function _GMTTextFromEpoch() {
123    $this->_text = gmdate('Ymd\THis', $this->_epoch );
124//    dbg_error_log( "RRule", " Text %s from epoch %d", $this->_text, $this->_epoch );
125  }
126
127  /**
128  * Given a text date, convert it to parts
129  */
130  function _PartsFromText() {
131    $this->_yy = intval(substr($this->_text,0,4));
132    $this->_mo = intval(substr($this->_text,4,2));
133    $this->_dd = intval(substr($this->_text,6,2));
134    $this->_hh = intval(substr($this->_text,9,2));
135    $this->_mi = intval(substr($this->_text,11,2));
136    $this->_ss = intval(substr($this->_text,13,2));
137  }
138
139
140  /**
141  * Given a GMT text date, convert it to an epoch
142  */
143  function _GMTEpochFromParts() {
144    $this->_epoch = gmmktime ( $this->_hh, $this->_mi, $this->_ss, $this->_mo, $this->_dd, $this->_yy );
145//    dbg_error_log( "RRule", " Epoch %d from %04d-%02d-%02d %02d:%02d:%02d", $this->_epoch, $this->_yy, $this->_mo, $this->_dd, $this->_hh, $this->_mi, $this->_ss );
146  }
147
148
149  /**
150  * Given a local text date, convert it to an epoch
151  */
152  function _EpochFromParts() {
153    $this->_epoch = mktime ( $this->_hh, $this->_mi, $this->_ss, $this->_mo, $this->_dd, $this->_yy );
154//    dbg_error_log( "RRule", " Epoch %d from %04d-%02d-%02d %02d:%02d:%02d", $this->_epoch, $this->_yy, $this->_mo, $this->_dd, $this->_hh, $this->_mi, $this->_ss );
155  }
156
157
158  /**
159  * Set the day of week used for calculation of week starts
160  *
161  * @param string $weekstart The day of the week which is the first business day.
162  */
163  function SetWeekStart($weekstart) {
164    global $ical_weekdays;
165    $this->_wkst = $ical_weekdays[$weekstart];
166  }
167
168
169  /**
170  * Set the day of week used for calculation of week starts
171  */
172  function Render( $fmt = 'Y-m-d H:i:s' ) {
173    return date( $fmt, $this->_epoch );
174  }
175
176
177  /**
178  * Render the date as GMT
179  */
180  function RenderGMT( $fmt = 'Ymd\THis\Z' ) {
181    return gmdate( $fmt, $this->_epoch );
182  }
183
184
185  /**
186  * No of days in a month 1(Jan) - 12(Dec)
187  */
188  function DaysInMonth( $mo=false, $yy=false ) {
189    if ( $mo === false ) $mo = $this->_mo;
190    switch( $mo ) {
191      case  1: // January
192      case  3: // March
193      case  5: // May
194      case  7: // July
195      case  8: // August
196      case 10: // October
197      case 12: // December
198        return 31;
199        break;
200
201      case  4: // April
202      case  6: // June
203      case  9: // September
204      case 11: // November
205        return 30;
206        break;
207
208      case  2: // February
209        if ( $yy === false ) $yy = $this->_yy;
210        if ( (($yy % 4) == 0) && ((($yy % 100) != 0) || (($yy % 400) == 0) ) ) return 29;
211        return 28;
212        break;
213
214      default:
215        dbg_error_log( "ERROR"," Invalid month of '%s' passed to DaysInMonth", $mo );
216        break;
217
218    }
219  }
220
221
222  /**
223  * Set the day in the month to what we have been given
224  */
225  function SetMonthDay( $dd ) {
226    if ( $dd == $this->_dd ) return; // Shortcut
227    $dd = min($dd,$this->DaysInMonth());
228    $this->_dd = $dd;
229    $this->_EpochFromParts();
230    $this->_TextFromEpoch();
231  }
232
233
234  /**
235  * Add some number of months to a date
236  */
237  function AddMonths( $mo ) {
238//    dbg_error_log( "RRule", " Adding %d months to %s", $mo, $this->_text );
239    $this->_mo += $mo;
240    while ( $this->_mo < 1 ) {
241      $this->_mo += 12;
242      $this->_yy--;
243    }
244    while ( $this->_mo > 12 ) {
245      $this->_mo -= 12;
246      $this->_yy++;
247    }
248
249    if ( ($this->_dd > 28 && $this->_mo == 2) || $this->_dd > 30 ) {
250      // Ensure the day of month is still reasonable and coerce to last day of month if needed
251      $dim = $this->DaysInMonth();
252      if ( $this->_dd > $dim ) {
253        $this->_dd = $dim;
254      }
255    }
256    $this->_EpochFromParts();
257    $this->_TextFromEpoch();
258//    dbg_error_log( "RRule", " Added %d months and got %s", $mo, $this->_text );
259  }
260
261
262  /**
263  * Add some integer number of days to a date
264  */
265  function AddDays( $dd ) {
266    $at_start = $this->_text;
267    $this->_dd += $dd;
268    while ( 1 > $this->_dd ) {
269      $this->_mo--;
270      if ( $this->_mo < 1 ) {
271        $this->_mo += 12;
272        $this->_yy--;
273      }
274      $this->_dd += $this->DaysInMonth();
275    }
276    while ( ($dim = $this->DaysInMonth($this->_mo)) < $this->_dd ) {
277      $this->_dd -= $dim;
278      $this->_mo++;
279      if ( $this->_mo > 12 ) {
280        $this->_mo -= 12;
281        $this->_yy++;
282      }
283    }
284    $this->_EpochFromParts();
285    $this->_TextFromEpoch();
286//    dbg_error_log( "RRule", " Added %d days to %s and got %s", $dd, $at_start, $this->_text );
287  }
288
289
290  /**
291  * Add duration
292  */
293  function AddDuration( $duration ) {
294    if ( strstr($duration,'T') === false ) $duration .= 'T';
295    list( $sign, $days, $time ) = preg_split( '/[PT]/', $duration );
296    $sign = ( $sign == "-" ? -1 : 1);
297//    dbg_error_log( "RRule", " Adding duration to '%s' of sign: %d,  days: %s,  time: %s", $this->_text, $sign, $days, $time );
298    if ( preg_match( '/(\d+)(D|W)/', $days, $matches ) ) {
299      $days = intval($matches[1]);
300      if ( $matches[2] == 'W' ) $days *= 7;
301      $this->AddDays( $days * $sign );
302    }
303    $hh = 0;    $mi = 0;    $ss = 0;
304    if ( preg_match( '/(\d+)(H)/', $time, $matches ) )  $hh = $matches[1];
305    if ( preg_match( '/(\d+)(M)/', $time, $matches ) )  $mi = $matches[1];
306    if ( preg_match( '/(\d+)(S)/', $time, $matches ) )  $ss = $matches[1];
307
308//    dbg_error_log( "RRule", " Adding %02d:%02d:%02d * %d to %02d:%02d:%02d", $hh, $mi, $ss, $sign, $this->_hh, $this->_mi, $this->_ss );
309    $this->_hh += ($hh * $sign);
310    $this->_mi += ($mi * $sign);
311    $this->_ss += ($ss * $sign);
312
313    if ( $this->_ss < 0 ) {  $this->_mi -= (intval(abs($this->_ss/60))+1); $this->_ss += ((intval(abs($this->_mi/60))+1) * 60); }
314    if ( $this->_ss > 59) {  $this->_mi += (intval(abs($this->_ss/60))+1); $this->_ss -= ((intval(abs($this->_mi/60))+1) * 60); }
315    if ( $this->_mi < 0 ) {  $this->_hh -= (intval(abs($this->_mi/60))+1); $this->_mi += ((intval(abs($this->_mi/60))+1) * 60); }
316    if ( $this->_mi > 59) {  $this->_hh += (intval(abs($this->_mi/60))+1); $this->_mi -= ((intval(abs($this->_mi/60))+1) * 60); }
317    if ( $this->_hh < 0 ) {  $this->AddDays( -1 * (intval(abs($this->_hh/24))+1) );  $this->_hh += ((intval(abs($this->_hh/24))+1)*24);  }
318    if ( $this->_hh > 23) {  $this->AddDays( (intval(abs($this->_hh/24))+1) );       $this->_hh -= ((intval(abs($this->_hh/24))+1)*24);  }
319
320    $this->_EpochFromParts();
321    $this->_TextFromEpoch();
322  }
323
324
325  /**
326  * Produce an iCalendar format DURATION for the difference between this an another iCalDate
327  *
328  * @param date $from The start of the period
329  * @return string The date difference, as an iCalendar duration format
330  */
331  function DateDifference( $from ) {
332    if ( !is_object($from) ) {
333      $from = new iCalDate($from);
334    }
335    if ( $from->_epoch < $this->_epoch ) {
336      /** One way to simplify is to always go for positive differences */
337      return( "-". $from->DateDifference( $self ) );
338    }
339//    if ( $from->_yy == $this->_yy && $from->_mo == $this->_mo ) {
340      /** Also somewhat simpler if we can use seconds */
341      $diff = $from->_epoch - $this->_epoch;
342      $result = "";
343      if ( $diff >= 86400) {
344        $result = intval($diff / 86400);
345        $diff = $diff % 86400;
346        if ( $diff == 0 && (($result % 7) == 0) ) {
347          // Duration is an integer number of weeks.
348          $result .= intval($result / 7) . "W";
349          return $result;
350        }
351        $result .= "D";
352      }
353      $result = "P".$result."T";
354      if ( $diff >= 3600) {
355        $result .= intval($diff / 3600) . "H";
356        $diff = $diff % 3600;
357      }
358      if ( $diff >= 60) {
359        $result .= intval($diff / 60) . "M";
360        $diff = $diff % 60;
361      }
362      if ( $diff > 0) {
363        $result .= intval($diff) . "S";
364      }
365      return $result;
366//    }
367
368/**
369* From an intense reading of RFC2445 it appears that durations which are not expressible
370* in Weeks/Days/Hours/Minutes/Seconds are invalid.
371*  ==> This code is not needed then :-)
372    $yy = $from->_yy - $this->_yy;
373    $mo = $from->_mo - $this->_mo;
374    $dd = $from->_dd - $this->_dd;
375    $hh = $from->_hh - $this->_hh;
376    $mi = $from->_mi - $this->_mi;
377    $ss = $from->_ss - $this->_ss;
378
379    if ( $ss < 0 ) {  $mi -= 1;   $ss += 60;  }
380    if ( $mi < 0 ) {  $hh -= 1;   $mi += 60;  }
381    if ( $hh < 0 ) {  $dd -= 1;   $hh += 24;  }
382    if ( $dd < 0 ) {  $mo -= 1;   $dd += $this->DaysInMonth();  } // Which will use $this->_(mo|yy) - seemingly sensible
383    if ( $mo < 0 ) {  $yy -= 1;   $mo += 12;  }
384
385    $result = "";
386    if ( $yy > 0) {    $result .= $yy."Y";   }
387    if ( $mo > 0) {    $result .= $mo."M";   }
388    if ( $dd > 0) {    $result .= $dd."D";   }
389    $result .= "T";
390    if ( $hh > 0) {    $result .= $hh."H";   }
391    if ( $mi > 0) {    $result .= $mi."M";   }
392    if ( $ss > 0) {    $result .= $ss."S";   }
393    return $result;
394*/
395  }
396
397  /**
398  * Test to see if our _mo matches something in the list of months we have received.
399  * @param string $monthlist A comma-separated list of months.
400  * @return boolean Whether this date falls within one of those months.
401  */
402  function TestByMonth( $monthlist ) {
403//    dbg_error_log( "RRule", " Testing BYMONTH %s against month %d", (isset($monthlist) ? $monthlist : "no month list"), $this->_mo );
404    if ( !isset($monthlist) ) return true;  // If BYMONTH is not specified any month is OK
405    $months = array_flip(explode( ',',$monthlist ));
406    return isset($months[$this->_mo]);
407  }
408
409  /**
410  * Applies any BYDAY to the month to return a set of days
411  * @param string $byday The BYDAY rule
412  * @return array An array of the day numbers for the month which meet the rule.
413  */
414  function GetMonthByDay($byday) {
415//    dbg_error_log( "RRule", " Applying BYDAY %s to month", $byday );
416    $days_in_month = $this->DaysInMonth();
417    $dayrules = explode(',',$byday);
418    $set = array();
419    $first_dow = (date('w',$this->_epoch) - $this->_dd + 36) % 7;
420    foreach( $dayrules AS $k => $v ) {
421      $days = $this->MonthDays($first_dow,$days_in_month,$v);
422      foreach( $days AS $k2 => $v2 ) {
423        $set[$v2] = $v2;
424      }
425    }
426    asort( $set, SORT_NUMERIC );
427    return $set;
428  }
429
430  /**
431  * Applies any BYMONTHDAY to the month to return a set of days
432  * @param string $bymonthday The BYMONTHDAY rule
433  * @return array An array of the day numbers for the month which meet the rule.
434  */
435  function GetMonthByMonthDay($bymonthday) {
436//    dbg_error_log( "RRule", " Applying BYMONTHDAY %s to month", $bymonthday );
437    $days_in_month = $this->DaysInMonth();
438    $dayrules = explode(',',$bymonthday);
439    $set = array();
440    foreach( $dayrules AS $k => $v ) {
441      $v = intval($v);
442      if ( $v > 0 && $v <= $days_in_month ) $set[$v] = $v;
443    }
444    asort( $set, SORT_NUMERIC );
445    return $set;
446  }
447
448
449  /**
450  * Applies any BYDAY to the week to return a set of days
451  * @param string $byday The BYDAY rule
452  * @param string $increasing When we are moving by months, we want any day of the week, but when by day we only want to increase. Default false.
453  * @return array An array of the day numbers for the week which meet the rule.
454  */
455  function GetWeekByDay($byday, $increasing = false) {
456    global $ical_weekdays;
457//    dbg_error_log( "RRule", " Applying BYDAY %s to week", $byday );
458    $days = explode(',',$byday);
459    $dow = date('w',$this->_epoch);
460    $set = array();
461    foreach( $days AS $k => $v ) {
462      $daynum = $ical_weekdays[$v];
463      $dd = $this->_dd - $dow + $daynum;
464      if ( $daynum < $this->_wkst ) $dd += 7;
465      if ( $dd > $this->_dd || !$increasing ) $set[$dd] = $dd;
466    }
467    asort( $set, SORT_NUMERIC );
468
469    return $set;
470  }
471
472
473  /**
474  * Test if $this is greater than the date parameter
475  * @param string $lesser The other date, as a local time string
476  * @return boolean True if $this > $lesser
477  */
478  function GreaterThan($lesser) {
479    if ( is_object($lesser) ) {
480//      dbg_error_log( "RRule", " Comparing %s with %s", $this->_text, $lesser->_text );
481      return ( $this->_text > $lesser->_text );
482    }
483//    dbg_error_log( "RRule", " Comparing %s with %s", $this->_text, $lesser );
484    return ( $this->_text > $lesser );  // These sorts of dates are designed that way...
485  }
486
487
488  /**
489  * Test if $this is less than the date parameter
490  * @param string $greater The other date, as a local time string
491  * @return boolean True if $this < $greater
492  */
493  function LessThan($greater) {
494    if ( is_object($greater) ) {
495//      dbg_error_log( "RRule", " Comparing %s with %s", $this->_text, $greater->_text );
496      return ( $this->_text < $greater->_text );
497    }
498//    dbg_error_log( "RRule", " Comparing %s with %s", $this->_text, $greater );
499    return ( $this->_text < $greater );  // These sorts of dates are designed that way...
500  }
501
502
503  /**
504  * Given a MonthDays string like "1MO", "-2WE" return an integer day of the month.
505  *
506  * @param string $dow_first The day of week of the first of the month.
507  * @param string $days_in_month The number of days in the month.
508  * @param string $dayspec The specification for a month day (or days) which we parse.
509  *
510  * @return array An array of the day numbers for the month which meet the rule.
511  */
512  function &MonthDays($dow_first, $days_in_month, $dayspec) {
513    global $ical_weekdays;
514//    dbg_error_log( "RRule", "MonthDays: Getting days for '%s'. %d days starting on a %d", $dayspec, $days_in_month, $dow_first );
515    $set = array();
516    preg_match( '/([0-9-]*)(MO|TU|WE|TH|FR|SA|SU)/', $dayspec, $matches);
517    $numeric = intval($matches[1]);
518    $dow = $ical_weekdays[$matches[2]];
519
520    $first_matching_day = 1 + ($dow - $dow_first);
521    while ( $first_matching_day < 1 ) $first_matching_day += 7;
522
523//    dbg_error_log( "RRule", " MonthDays: Looking at %d for first match on (%s/%s), %d for numeric", $first_matching_day, $matches[1], $matches[2], $numeric );
524
525    while( $first_matching_day <= $days_in_month ) {
526      $set[] = $first_matching_day;
527      $first_matching_day += 7;
528    }
529
530    if ( $numeric != 0 ) {
531      if ( $numeric < 0 ) {
532        $numeric += count($set);
533      }
534      else {
535        $numeric--;
536      }
537      $answer = $set[$numeric];
538      $set = array( $answer => $answer );
539    }
540    else {
541      $answers = $set;
542      $set = array();
543      foreach( $answers AS $k => $v ) {
544        $set[$v] = $v;
545      }
546    }
547
548//    dbg_log_array( "RRule", 'MonthDays', $set, false );
549
550    return $set;
551  }
552
553
554  /**
555  * Given set position descriptions like '1', '3', '11', '-3' or '-1' and a set,
556  * return the subset matching the list of set positions.
557  *
558  * @param string $bysplist  The list of set positions.
559  * @param string $set The set of days that we will apply the positions to.
560  *
561  * @return array The subset which matches.
562  */
563  function &ApplyBySetPos($bysplist, $set) {
564//    dbg_error_log( "RRule", " ApplyBySetPos: Applying set position '%s' to set of %d days", $bysplist, count($set) );
565    $subset = array();
566    sort( $set, SORT_NUMERIC );
567    $max = count($set);
568    $positions = explode( '[^0-9-]', $bysplist );
569    foreach( $positions AS $k => $v ) {
570      if ( $v < 0 ) {
571        $v += $max;
572      }
573      else {
574        $v--;
575      }
576      $subset[$set[$v]] = $set[$v];
577    }
578    return $subset;
579  }
580}
581
582
583
584/**
585* A Class for handling Events on a calendar which repeat
586*
587* Here's the spec, from RFC2445:
588*
589     recur      = "FREQ"=freq *(
590
591                ; either UNTIL or COUNT may appear in a 'recur',
592                ; but UNTIL and COUNT MUST NOT occur in the same 'recur'
593
594                ( ";" "UNTIL" "=" enddate ) /
595                ( ";" "COUNT" "=" 1*DIGIT ) /
596
597                ; the rest of these keywords are optional,
598                ; but MUST NOT occur more than once
599
600                ( ";" "INTERVAL" "=" 1*DIGIT )          /
601                ( ";" "BYSECOND" "=" byseclist )        /
602                ( ";" "BYMINUTE" "=" byminlist )        /
603                ( ";" "BYHOUR" "=" byhrlist )           /
604                ( ";" "BYDAY" "=" bywdaylist )          /
605                ( ";" "BYMONTHDAY" "=" bymodaylist )    /
606                ( ";" "BYYEARDAY" "=" byyrdaylist )     /
607                ( ";" "BYWEEKNO" "=" bywknolist )       /
608                ( ";" "BYMONTH" "=" bymolist )          /
609                ( ";" "BYSETPOS" "=" bysplist )         /
610                ( ";" "WKST" "=" weekday )              /
611                ( ";" x-name "=" text )
612                )
613
614     freq       = "SECONDLY" / "MINUTELY" / "HOURLY" / "DAILY"
615                / "WEEKLY" / "MONTHLY" / "YEARLY"
616
617     enddate    = date
618     enddate    =/ date-time            ;An UTC value
619
620     byseclist  = seconds / ( seconds *("," seconds) )
621
622     seconds    = 1DIGIT / 2DIGIT       ;0 to 59
623
624     byminlist  = minutes / ( minutes *("," minutes) )
625
626     minutes    = 1DIGIT / 2DIGIT       ;0 to 59
627
628     byhrlist   = hour / ( hour *("," hour) )
629
630     hour       = 1DIGIT / 2DIGIT       ;0 to 23
631
632     bywdaylist = weekdaynum / ( weekdaynum *("," weekdaynum) )
633
634     weekdaynum = [([plus] ordwk / minus ordwk)] weekday
635
636     plus       = "+"
637
638     minus      = "-"
639
640     ordwk      = 1DIGIT / 2DIGIT       ;1 to 53
641
642     weekday    = "SU" / "MO" / "TU" / "WE" / "TH" / "FR" / "SA"
643     ;Corresponding to SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY,
644     ;FRIDAY, SATURDAY and SUNDAY days of the week.
645
646     bymodaylist = monthdaynum / ( monthdaynum *("," monthdaynum) )
647
648     monthdaynum = ([plus] ordmoday) / (minus ordmoday)
649
650     ordmoday   = 1DIGIT / 2DIGIT       ;1 to 31
651
652     byyrdaylist = yeardaynum / ( yeardaynum *("," yeardaynum) )
653
654     yeardaynum = ([plus] ordyrday) / (minus ordyrday)
655
656     ordyrday   = 1DIGIT / 2DIGIT / 3DIGIT      ;1 to 366
657
658     bywknolist = weeknum / ( weeknum *("," weeknum) )
659
660     weeknum    = ([plus] ordwk) / (minus ordwk)
661
662     bymolist   = monthnum / ( monthnum *("," monthnum) )
663
664     monthnum   = 1DIGIT / 2DIGIT       ;1 to 12
665
666     bysplist   = setposday / ( setposday *("," setposday) )
667
668     setposday  = yeardaynum
669*
670* At this point we are going to restrict ourselves to parts of the RRULE specification
671* seen in the wild.  And by "in the wild" I don't include within people's timezone
672* definitions.  We always convert time zones to canonical names and assume the lower
673* level libraries can do a better job with them than we can.
674*
675* We will concentrate on:
676*  FREQ=(YEARLY|MONTHLY|WEEKLY|DAILY)
677*  UNTIL=
678*  COUNT=
679*  INTERVAL=
680*  BYDAY=
681*  BYMONTHDAY=
682*  BYSETPOS=
683*  WKST=
684*  BYYEARDAY=
685*  BYWEEKNO=
686*  BYMONTH=
687*
688*
689* @package awl
690*/
691class RRule {
692  /**#@+
693  * @access private
694  */
695
696  /** The first instance */
697  var $_first;
698
699  /** The current instance pointer */
700  var $_current;
701
702  /** An array of all the dates so far */
703  var $_dates;
704
705  /** Whether we have calculated any of the dates */
706  var $_started;
707
708  /** Whether we have calculated all of the dates */
709  var $_finished;
710
711  /** The rule, in all it's glory */
712  var $_rule;
713
714  /** The rule, in all it's parts */
715  var $_part;
716
717  /**#@-*/
718
719  /**
720  * The constructor takes a start date and an RRULE definition.  Both of these
721  * follow the iCalendar standard.
722  */
723  function RRule( $start, $rrule ) {
724    $this->_first = new iCalDate($start);
725    $this->_finished = false;
726    $this->_started = false;
727    $this->_dates = array();
728    $this->_current = -1;
729
730    $this->_rule = preg_replace( '/\s/m', '', $rrule);
731    if ( substr($this->_rule, 0, 6) == 'RRULE:' ) {
732      $this->_rule = substr($this->_rule, 6);
733    }
734
735    dbg_error_log( "RRule", " new RRule: Start: %s, RRULE: %s", $start->Render(), $this->_rule );
736
737    $parts = explode(';',$this->_rule);
738    $this->_part = array( 'INTERVAL' => 1 );
739    foreach( $parts AS $k => $v ) {
740      list( $type, $value ) = explode( '=', $v, 2);
741//      dbg_error_log( "RRule", " Parts of %s explode into %s and %s", $v, $type, $value );
742      $this->_part[$type] = $value;
743    }
744
745    // A little bit of validation
746    if ( !isset($this->_part['FREQ']) ) {
747      dbg_error_log( "ERROR", " RRULE MUST have FREQ=value (%s)", $rrule );
748    }
749    if ( isset($this->_part['COUNT']) && isset($this->_part['UNTIL'])  ) {
750      dbg_error_log( "ERROR", " RRULE MUST NOT have both COUNT=value and UNTIL=value (%s)", $rrule );
751    }
752    if ( isset($this->_part['COUNT']) && intval($this->_part['COUNT']) < 1 ) {
753      dbg_error_log( "ERROR", " RRULE MUST NOT have both COUNT=value and UNTIL=value (%s)", $rrule );
754    }
755    if ( !preg_match( '/(YEAR|MONTH|WEEK|DAI)LY/', $this->_part['FREQ']) ) {
756      dbg_error_log( "ERROR", " RRULE Only FREQ=(YEARLY|MONTHLY|WEEKLY|DAILY) are supported at present (%s)", $rrule );
757    }
758    if ( $this->_part['FREQ'] == "YEARLY" ) {
759      $this->_part['INTERVAL'] *= 12;
760      $this->_part['FREQ'] = "MONTHLY";
761    }
762  }
763
764
765  /**
766  * Processes the array of $relative_days to $base and removes any
767  * which are not within the scope of our rule.
768  */
769  function WithinScope( $base, $relative_days ) {
770
771    $ok_days = array();
772
773    $ptr = $this->_current;
774
775//    dbg_error_log( "RRule", " WithinScope: Processing list of %d days relative to %s", count($relative_days), $base->Render() );
776    foreach( $relative_days AS $day => $v ) {
777
778      $test = new iCalDate($base);
779      $days_in_month = $test->DaysInMonth();
780
781//      dbg_error_log( "RRule", " WithinScope: Testing for day %d based on %s, with %d days in month", $day, $test->Render(), $days_in_month );
782      if ( $day > $days_in_month ) {
783        $test->SetMonthDay($days_in_month);
784        $test->AddDays(1);
785        $day -= $days_in_month;
786        $test->SetMonthDay($day);
787      }
788      else if ( $day < 1 ) {
789        $test->SetMonthDay(1);
790        $test->AddDays(-1);
791        $days_in_month = $test->DaysInMonth();
792        $day += $days_in_month;
793        $test->SetMonthDay($day);
794      }
795      else {
796        $test->SetMonthDay($day);
797      }
798
799//      dbg_error_log( "RRule", " WithinScope: Testing if %s is within scope", count($relative_days), $test->Render() );
800
801      if ( isset($this->_part['UNTIL']) && $test->GreaterThan($this->_part['UNTIL']) ) {
802        $this->_finished = true;
803        return $ok_days;
804      }
805
806      // if ( $this->_current >= 0 && $test->LessThan($this->_dates[$this->_current]) ) continue;
807
808      if ( !$test->LessThan($this->_first) ) {
809//        dbg_error_log( "RRule", " WithinScope: Looks like %s is within scope", $test->Render() );
810        $ok_days[$day] = $test;
811        $ptr++;
812      }
813
814      if ( isset($this->_part['COUNT']) && $ptr >= $this->_part['COUNT'] ) {
815        $this->_finished = true;
816        return $ok_days;
817      }
818
819    }
820
821    return $ok_days;
822  }
823
824
825  /**
826  * This is most of the meat of the RRULE processing, where we find the next date.
827  * We maintain an
828  */
829  function &GetNext( ) {
830
831    if ( $this->_current < 0 ) {
832      $next = new iCalDate($this->_first);
833      $this->_current++;
834    }
835    else {
836      $next = new iCalDate($this->_dates[$this->_current]);
837      $this->_current++;
838
839      /**
840      * If we have already found some dates we may just be able to return one of those.
841      */
842      if ( isset($this->_dates[$this->_current]) ) {
843//        dbg_error_log( "RRule", " GetNext: Returning %s, (%d'th)", $this->_dates[$this->_current]->Render(), $this->_current );
844        return $this->_dates[$this->_current];
845      }
846      else {
847        if ( isset($this->_part['COUNT']) && $this->_current >= $this->_part['COUNT'] ) // >= since _current is 0-based and COUNT is 1-based
848          $this->_finished = true;
849      }
850    }
851
852    if ( $this->_finished ) {
853      $next = null;
854      return $next;
855    }
856
857    $days = array();
858    if ( isset($this->_part['WKST']) ) $next->SetWeekStart($this->_part['WKST']);
859    if ( $this->_part['FREQ'] == "MONTHLY" ) {
860//      dbg_error_log( "RRule", " GetNext: Calculating more dates for MONTHLY rule" );
861      $limit = 200;
862      do {
863        $limit--;
864        do {
865          $limit--;
866          if ( $this->_started ) {
867            $next->AddMonths($this->_part['INTERVAL']);
868          }
869          else {
870            $this->_started = true;
871          }
872        }
873        while ( isset($this->_part['BYMONTH']) && $limit > 0 && ! $next->TestByMonth($this->_part['BYMONTH']) );
874
875        if ( isset($this->_part['BYDAY']) ) {
876          $days = $next->GetMonthByDay($this->_part['BYDAY']);
877        }
878        else if ( isset($this->_part['BYMONTHDAY']) ) {
879          $days = $next->GetMonthByMonthDay($this->_part['BYMONTHDAY']);
880        }
881        else
882          $days[$next->_dd] = $next->_dd;
883
884        if ( isset($this->_part['BYSETPOS']) ) {
885          $days = $next->ApplyBySetpos($this->_part['BYSETPOS'], $days);
886        }
887
888        $days = $this->WithinScope( $next, $days);
889      }
890      while( $limit && count($days) < 1 && ! $this->_finished );
891//      dbg_error_log( "RRule", " GetNext: Found %d days for MONTHLY rule", count($days) );
892
893    }
894    else if ( $this->_part['FREQ'] == "WEEKLY" ) {
895//      dbg_error_log( "RRule", " GetNext: Calculating more dates for WEEKLY rule" );
896      $limit = 200;
897      do {
898        $limit--;
899        if ( $this->_started ) {
900          $next->AddDays($this->_part['INTERVAL'] * 7);
901        }
902        else {
903          $this->_started = true;
904        }
905
906        if ( isset($this->_part['BYDAY']) ) {
907          $days = $next->GetWeekByDay($this->_part['BYDAY'], false );
908        }
909        else
910          $days[$next->_dd] = $next->_dd;
911
912        if ( isset($this->_part['BYSETPOS']) ) {
913          $days = $next->ApplyBySetpos($this->_part['BYSETPOS'], $days);
914        }
915
916        $days = $this->WithinScope( $next, $days);
917      }
918      while( $limit && count($days) < 1 && ! $this->_finished );
919
920//      dbg_error_log( "RRule", " GetNext: Found %d days for WEEKLY rule", count($days) );
921    }
922    else if ( $this->_part['FREQ'] == "DAILY" ) {
923//      dbg_error_log( "RRule", " GetNext: Calculating more dates for DAILY rule" );
924      $limit = 100;
925      do {
926        $limit--;
927        if ( $this->_started ) {
928          $next->AddDays($this->_part['INTERVAL']);
929        }
930
931        if ( isset($this->_part['BYDAY']) ) {
932          $days = $next->GetWeekByDay($this->_part['BYDAY'], $this->_started );
933        }
934        else
935          $days[$next->_dd] = $next->_dd;
936
937        if ( isset($this->_part['BYSETPOS']) ) {
938          $days = $next->ApplyBySetpos($this->_part['BYSETPOS'], $days);
939        }
940
941        $days = $this->WithinScope( $next, $days);
942        $this->_started = true;
943      }
944      while( $limit && count($days) < 1 && ! $this->_finished );
945
946//      dbg_error_log( "RRule", " GetNext: Found %d days for DAILY rule", count($days) );
947    }
948
949    $ptr = $this->_current;
950    foreach( $days AS $k => $v ) {
951      $this->_dates[$ptr++] = $v;
952    }
953
954    if ( isset($this->_dates[$this->_current]) ) {
955//      dbg_error_log( "RRule", " GetNext: Returning %s, (%d'th)", $this->_dates[$this->_current]->Render(), $this->_current );
956      return $this->_dates[$this->_current];
957    }
958    else {
959//      dbg_error_log( "RRule", " GetNext: Returning null date" );
960      $next = null;
961      return $next;
962    }
963  }
964
965}
966
Note: See TracBrowser for help on using the repository browser.