From 4e7ad35dfb7eb575384c1cc868f2216222500e67 Mon Sep 17 00:00:00 2001 From: Andrew McMillan Date: Wed, 24 Feb 2010 16:41:31 +1300 Subject: [PATCH] Apparently CentOS 5 doesn't have DateTime class in it's PHP :-( --- inc/RRule-v2.php | 2 + inc/caldav-REPORT.php | 283 +++++++++++++++++++++--------------------- 2 files changed, 144 insertions(+), 141 deletions(-) diff --git a/inc/RRule-v2.php b/inc/RRule-v2.php index 7c9ea7a4..f49706ff 100644 --- a/inc/RRule-v2.php +++ b/inc/RRule-v2.php @@ -9,6 +9,8 @@ * @license http://gnu.org/copyleft/gpl.html GNU GPL v2 or later */ +if ( !class_exists('DateTime') ) return; + $rrule_expand_limit = array( 'YEARLY' => array( 'bymonth' => 'expand', 'byweekno' => 'expand', 'byyearday' => 'expand', 'bymonthday' => 'expand', 'byday' => 'expand', 'byhour' => 'expand', 'byminute' => 'expand', 'bysecond' => 'expand' ), diff --git a/inc/caldav-REPORT.php b/inc/caldav-REPORT.php index a1d35cff..38dac255 100644 --- a/inc/caldav-REPORT.php +++ b/inc/caldav-REPORT.php @@ -67,161 +67,162 @@ switch( $xmltree->GetTag() ) { } -/** -* Expand the event instances for an RDATE or EXDATE property -* -* @param string $property RDATE or EXDATE, depending... -* @param array $component An iCalComponent which is applies for these instances -* @param array $range_end A date after which we care less about expansion -* -* @return array An array keyed on the UTC dates, referring to the component -*/ -function rdate_expand( $property, $component, $range_end ) { - $timezone = $comp->GetPParamValue($property, 'TZID'); - $rdate = $comp->GetPValue($property); - $rdates = explode( ',', $rdate ); - $expansion = array(); - foreach( $rdates AS $k => $v ) { - $rdate = new RepeatRuleDateTime( $v, $timezone); - $expansion[$rdate->UTC()] = $component; - } - return $expansion; -} - - -/** -* Expand the event instances for an RRULE property -* -* @param object $dtstart A RepeatRuleDateTime which is the master dtstart -* @param string $property RDATE or EXDATE, depending... -* @param array $component An iCalComponent which is applies for these instances -* @param array $range_end A date after which we care less about expansion -* -* @return array An array keyed on the UTC dates, referring to the component -*/ -function rrule_expand( $dtstart, $property, $component, $range_end ) { - $expansion = array(); - - $recur = $comp->GetPValue($property); - if ( !isset($recur) ) return $expansion; - - $this_start = $comp->GetPValue('DTSTART'); - if ( isset($this_start) ) { - $timezone = $comp->GetPParamValue('DTSTART', 'TZID'); - $this_start = new RepeatRuleDateTime($this_start,$timezone); - } - else { - $this_start = clone($dtstart); - } - - $rule = new RepeatRule( $this_start, $recur ); - $i = 0; - $result_limit = 1000; - while( $date = $rule->next() ) { - $expansion[$date->UTC()] = $component; - if ( $i >= $result_limit ) break; - } - return $expansion; -} - - -/** -* Expand the event instances for an iCalendar VEVENT (or VTODO) -* -* @param object $ics An iCalComponent which is the master VCALENDAR -* @param object $range_start A RepeatRuleDateTime which is the beginning of the range for events -* @param object $range_end A RepeatRuleDateTime which is the end of the range for events -* -* @return iCalComponent The original iCalComponent with expanded events in the range. -*/ -function expand_event_instances( $ics, $range_start = null, $range_end = null ) { - $components = $ics->GetComponents(); - - if ( !isset($range_start) ) { $range_start = new RepeatRuleDateTime($range_start); $range_start->modify('-6 weeks'); } - if ( !isset($range_end) ) { $range_end = new RepeatRuleDateTime($range_end); $range_end->modify('+4 months'); } - - $new_components = array(); - $result_limit = 1000; - $instances = array(); - $expand = false; - $dtstart = null; - foreach( $components AS $k => $comp ) { - if ( $comp->GetType() != 'VEVENT' && $comp->GetType() != 'VTODO' && $comp->GetType() != 'VJOURNAL' ) { - $new_components[] = $comp; - continue; +if ( class_exists('RepeatRule') ) { + /** + * Expand the event instances for an RDATE or EXDATE property + * + * @param string $property RDATE or EXDATE, depending... + * @param array $component An iCalComponent which is applies for these instances + * @param array $range_end A date after which we care less about expansion + * + * @return array An array keyed on the UTC dates, referring to the component + */ + function rdate_expand( $property, $component, $range_end ) { + $timezone = $comp->GetPParamValue($property, 'TZID'); + $rdate = $comp->GetPValue($property); + $rdates = explode( ',', $rdate ); + $expansion = array(); + foreach( $rdates AS $k => $v ) { + $rdate = new RepeatRuleDateTime( $v, $timezone); + $expansion[$rdate->UTC()] = $component; } - if ( !isset($dtstart) ) { - $dtstart = new RepeatRuleDateTime( $comp->GetPValue('DTSTART'), $comp->GetPParamValue('DTSTART', 'TZID') ); - $instances[$dtstart->UTC()] = $comp; + return $expansion; + } + + + /** + * Expand the event instances for an RRULE property + * + * @param object $dtstart A RepeatRuleDateTime which is the master dtstart + * @param string $property RDATE or EXDATE, depending... + * @param array $component An iCalComponent which is applies for these instances + * @param array $range_end A date after which we care less about expansion + * + * @return array An array keyed on the UTC dates, referring to the component + */ + function rrule_expand( $dtstart, $property, $component, $range_end ) { + $expansion = array(); + + $recur = $comp->GetPValue($property); + if ( !isset($recur) ) return $expansion; + + $this_start = $comp->GetPValue('DTSTART'); + if ( isset($this_start) ) { + $timezone = $comp->GetPParamValue('DTSTART', 'TZID'); + $this_start = new RepeatRuleDateTime($this_start,$timezone); } - $p = $comp->GetPValue('RECURRENCE-ID'); - if ( isset($p) && $p != '' ) { - $range = $comp->GetPParamValue('RECURRENCE-ID', 'RANGE'); - $recur_tzid = $comp->GetPParamValue('RECURRENCE-ID', 'TZID'); - $recur_utc = new RepeatRuleDateTime($p,$recur_tzid); - $recur_utc = $recur_utc->UTC(); - if ( isset($range) && $range == 'THISANDFUTURE' ) { - foreach( $instances AS $k => $v ) { - if ( $k >= $recur_utc ) unset($instances[$k]); - } + else { + $this_start = clone($dtstart); + } + + $rule = new RepeatRule( $this_start, $recur ); + $i = 0; + $result_limit = 1000; + while( $date = $rule->next() ) { + $expansion[$date->UTC()] = $component; + if ( $i >= $result_limit ) break; + } + return $expansion; + } + + + /** + * Expand the event instances for an iCalendar VEVENT (or VTODO) + * + * @param object $ics An iCalComponent which is the master VCALENDAR + * @param object $range_start A RepeatRuleDateTime which is the beginning of the range for events + * @param object $range_end A RepeatRuleDateTime which is the end of the range for events + * + * @return iCalComponent The original iCalComponent with expanded events in the range. + */ + function expand_event_instances( $ics, $range_start = null, $range_end = null ) { + $components = $ics->GetComponents(); + + if ( !isset($range_start) ) { $range_start = new RepeatRuleDateTime($range_start); $range_start->modify('-6 weeks'); } + if ( !isset($range_end) ) { $range_end = new RepeatRuleDateTime($range_end); $range_end->modify('+4 months'); } + + $new_components = array(); + $result_limit = 1000; + $instances = array(); + $expand = false; + $dtstart = null; + foreach( $components AS $k => $comp ) { + if ( $comp->GetType() != 'VEVENT' && $comp->GetType() != 'VTODO' && $comp->GetType() != 'VJOURNAL' ) { + $new_components[] = $comp; + continue; } - else { - unset($instances[$recur_utc]); + if ( !isset($dtstart) ) { + $dtstart = new RepeatRuleDateTime( $comp->GetPValue('DTSTART'), $comp->GetPParamValue('DTSTART', 'TZID') ); + $instances[$dtstart->UTC()] = $comp; } - $instances[] = $comp; - } - $instances = array_merge( $instances, rrule_expand($dtstart, 'RRULE', $comp) ); - $instances = array_merge( $instances, rdate_expand($dtstart, 'RDATE', $comp) ); - foreach ( rdate_expand($dtstart, 'EXDATE', $comp) AS $k => $v ) { - unset($instances[$k]); - } - } - - $last_duration = null; - $in_range = false; - $new_components = array(); - $start_utc = $range_start->UTC(); - $end_utc = $range_end->UTC(); - foreach( $instances AS $utc => $comp ) { - if ( $utc < $start_utc ) { - $duration = $comp->GetPValue('DURATION'); - if ( !isset($duration) ) { - $dtend = new RepeatRuleDateTime( $comp->GetPValue('DTEND'), $comp->GetPValue('DTEND', 'TZID')); - $dtsrt = new RepeatRuleDateTime( $comp->GetPValue('DTSTART'), $comp->GetPValue('DTSTART', 'TZID')); - $duration = sprintf( '%d minutes', (($dtend->epoch() - $dtsrt->epoch()) / 60) ); - } - if ( isset($duration) ) { - if ( isset($last_duration) && $duration == $last_duration) { - if ( $utc < $early_start ) continue; + $p = $comp->GetPValue('RECURRENCE-ID'); + if ( isset($p) && $p != '' ) { + $range = $comp->GetPParamValue('RECURRENCE-ID', 'RANGE'); + $recur_tzid = $comp->GetPParamValue('RECURRENCE-ID', 'TZID'); + $recur_utc = new RepeatRuleDateTime($p,$recur_tzid); + $recur_utc = $recur_utc->UTC(); + if ( isset($range) && $range == 'THISANDFUTURE' ) { + foreach( $instances AS $k => $v ) { + if ( $k >= $recur_utc ) unset($instances[$k]); + } } else { - $latest_start = clone($range_start); - $latest_start->modify('-'.$duration); - $early_start = $latest_start->UTC(); - $last_duration = $duration; - if ( $utc < $early_start ) continue; + unset($instances[$recur_utc]); } + $instances[] = $comp; } - else { + $instances = array_merge( $instances, rrule_expand($dtstart, 'RRULE', $comp) ); + $instances = array_merge( $instances, rdate_expand($dtstart, 'RDATE', $comp) ); + foreach ( rdate_expand($dtstart, 'EXDATE', $comp) AS $k => $v ) { + unset($instances[$k]); } - continue; } - if ( $utc > $end_utc ) break; - $new_components[] = $comp; - $in_range = true; - } - if ( $in_range ) { - $ics->SetComponents($new_components); - } - else { - $ics->SetComponents(array()); - } + $last_duration = null; + $in_range = false; + $new_components = array(); + $start_utc = $range_start->UTC(); + $end_utc = $range_end->UTC(); + foreach( $instances AS $utc => $comp ) { + if ( $utc < $start_utc ) { + $duration = $comp->GetPValue('DURATION'); + if ( !isset($duration) ) { + $dtend = new RepeatRuleDateTime( $comp->GetPValue('DTEND'), $comp->GetPValue('DTEND', 'TZID')); + $dtsrt = new RepeatRuleDateTime( $comp->GetPValue('DTSTART'), $comp->GetPValue('DTSTART', 'TZID')); + $duration = sprintf( '%d minutes', (($dtend->epoch() - $dtsrt->epoch()) / 60) ); + } + if ( isset($duration) ) { + if ( isset($last_duration) && $duration == $last_duration) { + if ( $utc < $early_start ) continue; + } + else { + $latest_start = clone($range_start); + $latest_start->modify('-'.$duration); + $early_start = $latest_start->UTC(); + $last_duration = $duration; + if ( $utc < $early_start ) continue; + } + } + else { + } + continue; + } + if ( $utc > $end_utc ) break; + $new_components[] = $comp; + $in_range = true; + } - return $ics; + if ( $in_range ) { + $ics->SetComponents($new_components); + } + else { + $ics->SetComponents(array()); + } + + return $ics; + } } - /** * Return XML for a single calendar (or todo) entry from the DB *