2 /* $Id: date_api_ical.inc,v 1.35.4.26 2009/02/21 12:53:25 karens Exp $ */
7 * This file must be included when these functions are needed.
10 * Return an array of iCalendar information from an iCalendar file.
12 * No timezone adjustment is performed in the import since the timezone
13 * conversion needed will vary depending on whether the value is being
14 * imported into the database (when it needs to be converted to UTC), is being
15 * viewed on a site that has user-configurable timezones (when it needs to be
16 * converted to the user's timezone), if it needs to be converted to the
17 * site timezone, or if it is a date without a timezone which should not have
18 * any timezone conversion applied.
20 * Properties that have dates and times are converted to sub-arrays like:
21 * 'datetime' => date in YYYY-MM-DD HH:MM format, not timezone adjusted
22 * 'all_day' => whether this is an all-day event
23 * 'tz' => the timezone of the date, could be blank for absolute
24 * times that should get no timezone conversion.
26 * Exception dates can have muliple values and are returned as arrays
27 * like the above for each exception date.
29 * Most other properties are returned as PROPERTY => VALUE.
31 * Each item in the VCALENDAR will return an array like:
35 * [SUMMARY] => An example event
36 * [URL] => http://example.com/node/1
37 * [DTSTART] => Array (
38 * [datetime] => 1997-09-07 09:00:00
43 * [datetime] => 1997-09-07 11:00:00
58 * [datetime] => 1997-09-21 09:00:00
63 * [datetime] => 1997-10-05 09:00:00
71 * Location (local or remote) of a valid iCalendar file
73 * An array with all the elements from the ical
75 * figure out how to handle this if subgroups are nested,
76 * like a VALARM nested inside a VEVENT.
78 function date_ical_import($filename) {
79 // Fetch the iCal data. If file is a URL, use drupal_http_request. fopen
80 // isn't always configured to allow network connections.
81 if (substr($filename, 0, 4) == 'http') {
82 // Fetch the ical data from the specified network location
83 $icaldatafetch = drupal_http_request($filename);
84 // Check the return result
85 if ($icaldatafetch->error) {
86 watchdog('date ical', 'HTTP Request Error importing %filename: @error', array('%filename' => $filename, '@error' => $icaldatafetch->error));
89 // Break the return result into one array entry per lines
90 $icaldatafolded = explode("\n", $icaldatafetch->data);
93 $icaldatafolded = @file($filename, FILE_IGNORE_NEW_LINES);
94 if ($icaldatafolded === FALSE) {
95 watchdog('date ical', 'Failed to open file: %filename', array('%filename' => $filename));
99 // Verify this is iCal data
100 if (trim($icaldatafolded[0]) != 'BEGIN:VCALENDAR') {
101 watchdog('date ical', 'Invalid calendar file: %filename', array('%filename' => $filename));
104 return date_ical_parse($icaldatafolded);
108 * Return an array of iCalendar information from an iCalendar file.
110 * As date_ical_import() but different param.
112 * @param $icaldatafolded
113 * an array of lines from an ical feed.
115 * An array with all the elements from the ical.
117 function date_ical_parse($icaldatafolded = array()) {
120 // Verify this is iCal data
121 if (trim($icaldatafolded[0]) != 'BEGIN:VCALENDAR') {
122 watchdog('date ical', 'Invalid calendar file.');
127 // "unfold" wrapped lines
129 foreach ($icaldatafolded as $line) {
131 // See if this looks like the beginning of a new property or value.
132 // If not, it is a continuation of the previous line.
133 // The regex is to ensure that wrapped QUOTED-PRINTABLE data
135 if (!preg_match('/([A-Z]+)[:;](.*)/', $line, $out)) {
136 $line = array_pop($icaldata) . ($line);
140 unset($icaldatafolded);
142 // Parse the iCal information
144 $subgroups = array();
146 foreach ($icaldata as $line) {
148 $vcal .= $line ."\n";
149 // Deal with begin/end tags separately
150 if (preg_match('/(BEGIN|END):V(\S+)/', $line, $matches)) {
151 $closure = $matches[1];
152 $type = 'V'. $matches[2];
153 if ($closure == 'BEGIN') {
154 array_push($parents, $type);
155 array_push($subgroups, array());
157 else if ($closure == 'END') {
159 $subgroup =& $subgroups[key($subgroups)];
162 if (prev($subgroups) == false) {
163 $items[] = array_pop($subgroups);
166 $parent[array_pop($parents)][] = array_pop($subgroups);
169 // Add the timezones in with their index their TZID
171 $subgroup = end($subgroups);
172 $id = $subgroup['TZID'];
173 unset($subgroup['TZID']);
175 // Append this subgroup onto the one above it
177 $parent =& $subgroups[key($subgroups)];
179 $parent[$type][$id] = $subgroup;
181 array_pop($subgroups);
184 // Do some fun stuff with durations and all_day events
185 // and then append to parent
193 // Can't be sure whether DTSTART is before or after DURATION,
194 // so parse DURATION at the end.
195 if (isset($subgroup['DURATION'])) {
196 date_ical_parse_duration($subgroup, 'DURATION');
198 // Add a top-level indication for the 'All day' condition.
199 // Leave it in the individual date components, too, so it
200 // is always available even when you are working with only
201 // a portion of the VEVENT array, like in Feed API parsers.
202 $subgroup['all_day'] = FALSE;
203 if (!empty($subgroup['DTSTART']) && (!empty($subgroup['DTSTART']['all_day']) ||
204 (empty($subgroup['DTEND']) && empty($subgroup['RRULE']) && empty($subgroup['RRULE']['COUNT'])))) {
205 // Many programs output DTEND for an all day event as the
206 // following day, reset this to the same day for internal use.
207 $subgroup['all_day'] = TRUE;
208 $subgroup['DTEND'] = $subgroup['DTSTART'];
210 // Add this element to the parent as an array under the
214 $parent =& $subgroups[key($subgroups)];
216 $parent[$type][] = $subgroup;
218 array_pop($subgroups);
224 // Handle all other possibilities
226 // Grab current subgroup
228 $subgroup =& $subgroups[key($subgroups)];
230 // Split up the line into nice pieces for PROPERTYNAME,
231 // PROPERTYATTRIBUTES, and PROPERTYVALUE
232 preg_match('/([^;:]+)(?:;([^:]*))?:(.+)/', $line, $matches);
233 $name = !empty($matches[1]) ? strtoupper(trim($matches[1])) : '';
234 $field = !empty($matches[2]) ? $matches[2] : '';
235 $data = !empty($matches[3]) ? $matches[3] : '';
238 // Keep blank lines out of the results.
242 // Lots of properties have date values that must be parsed out.
244 case 'LAST-MODIFIED':
252 $parse_result = date_ical_parse_date($field, $data);
256 $parse_result = date_ical_parse_exceptions($field, $data);
260 // A TRIGGER can either be a date or in the form -PT1H.
261 if (!empty($field)) {
262 $parse_result = date_ical_parse_date($field, $data);
265 $parse_result = array('DATA' => $data);
270 // Can't be sure whether DTSTART is before or after DURATION in
271 // the VEVENT, so store the data and parse it at the end.
272 $parse_result = array('DATA' => $data);
277 $parse_result = date_ical_parse_rrule($field, $data);
282 $parse_result = date_ical_parse_text($field, $data);
286 $parse_result = date_ical_parse_location($field, $data);
289 // For all other properties, just store the property and the value.
290 // This can be expanded on in the future if other properties should
291 // be given special treatment.
293 $parse_result = $data;
297 // Store the result of our parsing
298 $subgroup[$name] = $parse_result;
305 * Parse a ical date element.
307 * Possible formats to parse include:
308 * PROPERTY:YYYYMMDD[T][HH][MM][SS][Z]
309 * PROPERTY;VALUE=DATE:YYYYMMDD[T][HH][MM][SS][Z]
310 * PROPERTY;VALUE=DATE-TIME:YYYYMMDD[T][HH][MM][SS][Z]
311 * PROPERTY;TZID=XXXXXXXX;VALUE=DATE:YYYYMMDD[T][HH][MM][SS]
312 * PROPERTY;TZID=XXXXXXXX:YYYYMMDD[T][HH][MM][SS]
314 * The property and the colon before the date are removed in the import
315 * process above and we are left with $field and $data.
318 * The text before the colon and the date, i.e.
319 * ';VALUE=DATE:', ';VALUE=DATE-TIME:', ';TZID='
321 * The date itself, after the colon, in the format YYYYMMDD[T][HH][MM][SS][Z]
322 * 'Z', if supplied, means the date is in UTC.
325 * $items array, consisting of:
326 * 'datetime' => date in YYYY-MM-DD HH:MM format, not timezone adjusted
327 * 'all_day' => whether this is an all-day event with no time
328 * 'tz' => the timezone of the date, could be blank if the ical
329 * has no timezone; the ical specs say no timezone
330 * conversion should be done if no timezone info is
333 * Another option for dates is the format PROPERTY;VALUE=PERIOD:XXXX. The period
334 * may include a duration, or a date and a duration, or two dates, so would
335 * have to be split into parts and run through date_ical_parse_date() and
336 * date_ical_parse_duration(). This is not commonly used, so ignored for now.
337 * It will take more work to figure how to support that.
339 function date_ical_parse_date($field, $data) {
340 $items = array('datetime' => '', 'all_day' => '', 'tz' => '');
344 // Make this a little more whitespace independent
347 // Turn the properties into a nice indexed array of
348 // array(PROPERTYNAME => PROPERTYVALUE);
349 $field_parts = preg_split('/[;:]/', $field);
350 $properties = array();
351 foreach ($field_parts as $part) {
352 if (strpos($part, '=') !== false) {
353 $tmp = explode('=', $part);
354 $properties[$tmp[0]] = $tmp[1];
358 // Make this a little more whitespace independent
361 // Record if a time has been found
364 // If a format is specified, parse it according to that format
365 if (isset($properties['VALUE'])) {
366 switch ($properties['VALUE']) {
368 preg_match (DATE_REGEX_ICAL_DATE, $data, $regs);
369 $datetime = date_pad($regs[1]) .'-'. date_pad($regs[2]) .'-'. date_pad($regs[3]); // Date
372 preg_match (DATE_REGEX_ICAL_DATETIME, $data, $regs);
373 $datetime = date_pad($regs[1]) .'-'. date_pad($regs[2]) .'-'. date_pad($regs[3]); // Date
374 $datetime .= ' '. date_pad($regs[4]) .':'. date_pad($regs[5]) .':'. date_pad($regs[6]); // Time
379 // If no format is specified, attempt a loose match
381 preg_match (DATE_REGEX_LOOSE, $data, $regs);
382 $datetime = date_pad($regs[1]) .'-'. date_pad($regs[2]) .'-'. date_pad($regs[3]); // Date
383 if (isset($regs[4])) {
385 $datetime .= ' '.date_pad($regs[5]) .':'. date_pad($regs[6]) .':'. date_pad($regs[7]); // Time
389 // Use timezone if explicitly declared
390 if (isset($properties['TZID'])) {
391 $tz = $properties['TZID'];
392 // Fix commonly used alternatives like US-Eastern which should be US/Eastern.
393 $tz = str_replace('-', '/', $tz);
394 // Unset invalid timezone names.
395 if (!date_timezone_is_valid($tz)) {
399 // If declared as UTC with terminating 'Z', use that timezone
400 else if (strpos($data, 'Z') !== false) {
403 // Otherwise this date is floating...
408 $items['datetime'] = $datetime;
409 $items['all_day'] = $has_time ? false : true;
415 * Parse an ical repeat rule.
418 * Array in the form of PROPERTY => array(VALUES)
419 * PROPERTIES include FREQ, INTERVAL, COUNT, BYDAY, BYMONTH, BYYEAR, UNTIL
421 function date_ical_parse_rrule($field, $data) {
422 $data = str_replace('RRULE:', '', $data);
423 $items = array('DATA' => $data);
424 $rrule = explode(';', $data);
425 foreach ($rrule as $key => $value) {
426 $param = explode('=', $value);
427 // Must be some kind of invalid data.
428 if (count($param) != 2) {
431 if ($param[0] == 'UNTIL') {
432 $values = date_ical_parse_date('', $param[1]);
435 $values = explode(',', $param[1]);
437 // Different treatment for items that can have multiple values and those that cannot.
438 if (in_array($param[0], array('FREQ', 'INTERVAL', 'COUNT', 'WKST'))) {
439 $items[$param[0]] = $param[1];
442 $items[$param[0]] = $values;
449 * Parse exception dates (can be multiple values).
452 * an array of date value arrays.
454 function date_ical_parse_exceptions($field, $data) {
455 $data = str_replace('EXDATE:', '', $data);
456 $items = array('DATA' => $data);
457 $ex_dates = explode(',', $data);
458 foreach ($ex_dates as $ex_date) {
459 $items[] = date_ical_parse_date('', $ex_date);
465 * Parse the duration of the event.
471 * array of other values in the vevent so we can check for DTSTART
473 function date_ical_parse_duration(&$subgroup, $field = 'DURATION') {
474 $items = $subgroup[$field];
475 $data = $items['DATA'];
476 preg_match('/^P(\d{1,4}[Y])?(\d{1,2}[M])?(\d{1,2}[W])?(\d{1,2}[D])?([T]{0,1})?(\d{1,2}[H])?(\d{1,2}[M])?(\d{1,2}[S])?/', $data, $duration);
477 $items['year'] = isset($duration[1]) ? str_replace('Y', '', $duration[1]) : '';
478 $items['month'] = isset($duration[2]) ?str_replace('M', '', $duration[2]) : '';
479 $items['week'] = isset($duration[3]) ?str_replace('W', '', $duration[3]) : '';
480 $items['day'] = isset($duration[4]) ?str_replace('D', '', $duration[4]) : '';
481 $items['hour'] = isset($duration[6]) ?str_replace('H', '', $duration[6]) : '';
482 $items['minute'] = isset($duration[7]) ?str_replace('M', '', $duration[7]) : '';
483 $items['second'] = isset($duration[8]) ?str_replace('S', '', $duration[8]) : '';
484 $start_date = array_key_exists('DTSTART', $subgroup) ? $subgroup['DTSTART']['datetime'] : date_format(date_now(), DATE_FORMAT_ISO);
485 $timezone = array_key_exists('DTSTART', $subgroup) ? $subgroup['DTSTART']['tz'] : variable_get('date_default_timezone_name', NULL);
486 if (empty($timezone)) {
489 $date = date_make_date($start_date, $timezone);
490 $date2 = drupal_clone($date);
491 foreach ($items as $item => $count) {
493 date_modify($date2, '+'. $count .' '. $item);
496 $format = isset($subgroup['DTSTART']['type']) && $subgroup['DTSTART']['type'] == 'DATE' ? 'Y-m-d' : 'Y-m-d H:i:s';
497 $subgroup['DTEND'] = array(
498 'datetime' => date_format($date2, DATE_FORMAT_DATETIME),
499 'all_day' => isset($subgroup['DTSTART']['all_day']) ? $subgroup['DTSTART']['all_day'] : 0,
502 $duration = date_format($date2, 'U') - date_format($date, 'U');
503 $subgroup['DURATION'] = array('DATA' => $data, 'DURATION' => $duration);
507 * Parse and clean up ical text elements.
509 function date_ical_parse_text($field, $data) {
510 if (strstr($field, 'QUOTED-PRINTABLE')) {
511 $data = quoted_printable_decode($data);
513 // Strip line breaks within element
514 $data = str_replace(array("\r\n ", "\n ", "\r "), '', $data);
515 // Put in line breaks where encoded
516 $data = str_replace(array("\\n", "\\N"), "\n", $data);
517 // Remove other escaping
518 $data = stripslashes($data);
523 * Parse location elements.
525 * Catch situations like the upcoming.org feed that uses
526 * LOCATION;VENUE-UID="http://upcoming.yahoo.com/venue/104/":111 First Street...
527 * or more normal LOCATION;UID=123:111 First Street...
528 * Upcoming feed would have been improperly broken on the ':' in http://
529 * so we paste the $field and $data back together first.
531 * Use non-greedy check for ':' in case there are more of them in the address.
533 function date_ical_parse_location($field, $data) {
534 if (preg_match('/UID=[?"](.+)[?"][*?:](.+)/', $field .':'. $data, $matches)) {
536 $location['UID'] = $matches[1];
537 $location['DESCRIPTION'] = stripslashes($matches[2]);
541 // Remove other escaping
542 $location = stripslashes($data);
548 * Return a date object for the ical date, adjusted to its local timezone.
551 * an array of ical date information created in the ical import.
553 * the timezone to convert the date's value to.
555 * a timezone-adjusted date object
557 function date_ical_date($ical_date, $to_tz = FALSE) {
558 // If the ical date has no timezone, must assume it is stateless
559 // so treat it as a local date.
560 if (empty($ical_date['tz'])) {
561 $from_tz = date_default_timezone_name();
564 $from_tz = $ical_date['tz'];
566 $date = date_make_date($ical_date['datetime'], $from_tz);
567 if ($to_tz && $ical_date['tz'] != '' && $to_tz != $ical_date['tz']) {
568 date_timezone_set($date, timezone_open($to_tz));
574 * Escape #text elements for safe iCal use
583 function date_ical_escape_text($text) {
584 //$text = strip_tags($text);
585 $text = str_replace('"', '\"', $text);
586 $text = str_replace("\\", "\\\\", $text);
587 $text = str_replace(",", "\,", $text);
588 $text = str_replace(":", "\:", $text);
589 $text = str_replace(";", "\;", $text);
590 $text = str_replace("\n", "\n ", $text);
595 * Build an iCal RULE from $form_values.
597 * @param $form_values
598 * an array constructed like the one created by date_ical_parse_rrule()
609 * [datetime] => 1997-21-31 09:00:00
614 * [EXDATE] => Array (
616 * [datetime] => 1997-09-21 09:00:00
621 * [datetime] => 1997-10-05 09:00:00
628 function date_api_ical_build_rrule($form_values) {
630 if (empty($form_values) || !is_array($form_values)) {
633 //grab the RRULE data and put them into iCal RRULE format
634 $RRULE .= 'RRULE:FREQ='. (!array_key_exists('FREQ', $form_values) ? 'DAILY' : $form_values['FREQ']);
635 $RRULE .= ';INTERVAL='. (!array_key_exists('INTERVAL', $form_values) ? 1 : $form_values['INTERVAL']);
637 // Unset the empty 'All' values.
638 if (array_key_exists('BYDAY', $form_values)) unset($form_values['BYDAY']['']);
639 if (array_key_exists('BYMONTH', $form_values)) unset($form_values['BYMONTH']['']);
640 if (array_key_exists('BYMONTHDAY', $form_values)) unset($form_values['BYMONTHDAY']['']);
642 if (array_key_exists('BYDAY', $form_values) && $BYDAY = implode(",", $form_values['BYDAY'])) {
643 $RRULE .= ';BYDAY='. $BYDAY;
645 if (array_key_exists('BYMONTH', $form_values) && $BYMONTH = implode(",", $form_values['BYMONTH'])) {
646 $RRULE .= ';BYMONTH='. $BYMONTH;
648 if (array_key_exists('BYMONTHDAY', $form_values) && $BYMONTHDAY = implode(",", $form_values['BYMONTHDAY'])) {
649 $RRULE .= ';BYMONTHDAY='. $BYMONTHDAY;
651 // The UNTIL date is supposed to always be expressed in UTC.
652 if (array_key_exists('UNTIL', $form_values) && array_key_exists('datetime', $form_values['UNTIL'])) {
653 $until = date_ical_date($form_values['UNTIL'], 'UTC');
654 $RRULE .= ';UNTIL='. date_format($until, DATE_FORMAT_ICAL) .'Z';
656 // Our form doesn't allow a value for COUNT, but it may be needed by
657 // modules using the API, so add it to the rule.
658 if (array_key_exists('COUNT', $form_values)) {
659 $RRULE .= ';COUNT='. $form_values['COUNT'];
662 // iCal rules presume the week starts on Monday unless otherwise specified,
663 // so we'll specify it.
664 if (array_key_exists('WKST', $form_values)) {
665 $RRULE .= ';WKST='. $form_values['WKST'];
668 $RRULE .= ';WKST='. date_repeat_dow2day(variable_get('date_first_day', 1));
671 // Exceptions dates go last, on their own line.
672 if (isset($form_values['EXDATE']) && is_array($form_values['EXDATE'])) {
674 foreach ($form_values['EXDATE'] as $value) {
675 $ex_date = date_convert($value['datetime'], DATE_DATETIME, DATE_ICAL);
676 if (!empty($ex_date)) {
677 $ex_dates[] = $ex_date;
680 if (!empty($ex_dates)) {
682 $RRULE .= chr(13) . chr(10) .'EXDATE:'. implode(',', $ex_dates);
685 elseif (!empty($form_values['EXDATE'])) {
686 $RRULE .= chr(13) . chr(10) .'EXDATE:'. $form_values['EXDATE'];