function date_ical_parse in Date 7
Same name and namespace in other branches
- 5.2 date_api_ical.inc \date_ical_parse()
- 6.2 date_api_ical.inc \date_ical_parse()
- 6 date_api_ical.inc \date_ical_parse()
- 7.3 date_api/date_api_ical.inc \date_ical_parse()
- 7.2 date_api/date_api_ical.inc \date_ical_parse()
Return an array of iCalendar information from an iCalendar file.
As date_ical_import() but different param.
Parameters
$icaldatafolded: an array of lines from an ical feed.
Return value
array An array with all the elements from the ical.
2 calls to date_ical_parse()
- date_ical_import in date_api/
date_api_ical.inc - Return an array of iCalendar information from an iCalendar file.
- date_repeat_build_dates in ./
date_repeat.inc - Helper function to build repeating dates from a $node_field.
File
- date_api/
date_api_ical.inc, line 128 - Parse iCal data.
Code
function date_ical_parse($icaldatafolded = array()) {
$items = array();
// Verify this is iCal data
if (trim($icaldatafolded[0]) != 'BEGIN:VCALENDAR') {
watchdog('date ical', 'Invalid calendar file.');
return FALSE;
}
// "unfold" wrapped lines
$icaldata = array();
foreach ($icaldatafolded as $line) {
$out = array();
// See if this looks like the beginning of a new property or value.
// If not, it is a continuation of the previous line.
// The regex is to ensure that wrapped QUOTED-PRINTABLE data
// is kept intact.
if (!preg_match('/([A-Z]+)[:;](.*)/', $line, $out)) {
$line = array_pop($icaldata) . $line;
}
$icaldata[] = $line;
}
unset($icaldatafolded);
// Parse the iCal information
$parents = array();
$subgroups = array();
$vcal = '';
foreach ($icaldata as $line) {
$line = trim($line);
$vcal .= $line . "\n";
// Deal with begin/end tags separately
if (preg_match('/(BEGIN|END):V(\\S+)/', $line, $matches)) {
$closure = $matches[1];
$type = 'V' . $matches[2];
if ($closure == 'BEGIN') {
array_push($parents, $type);
array_push($subgroups, array());
}
elseif ($closure == 'END') {
end($subgroups);
$subgroup =& $subgroups[key($subgroups)];
switch ($type) {
case 'VCALENDAR':
if (prev($subgroups) == FALSE) {
$items[] = array_pop($subgroups);
}
else {
$parent[array_pop($parents)][] = array_pop($subgroups);
}
break;
// Add the timezones in with their index their TZID
case 'VTIMEZONE':
$subgroup = end($subgroups);
$id = $subgroup['TZID'];
unset($subgroup['TZID']);
// Append this subgroup onto the one above it
prev($subgroups);
$parent =& $subgroups[key($subgroups)];
$parent[$type][$id] = $subgroup;
array_pop($subgroups);
array_pop($parents);
break;
// Do some fun stuff with durations and all_day events
// and then append to parent
case 'VEVENT':
case 'VALARM':
case 'VTODO':
case 'VJOURNAL':
case 'VVENUE':
case 'VFREEBUSY':
default:
// Can't be sure whether DTSTART is before or after DURATION,
// so parse DURATION at the end.
if (isset($subgroup['DURATION'])) {
date_ical_parse_duration($subgroup, 'DURATION');
}
// Add a top-level indication for the 'All day' condition.
// Leave it in the individual date components, too, so it
// is always available even when you are working with only
// a portion of the VEVENT array, like in Feed API parsers.
$subgroup['all_day'] = FALSE;
if (!empty($subgroup['DTSTART']) && (!empty($subgroup['DTSTART']['all_day']) || empty($subgroup['DTEND']) && empty($subgroup['RRULE']) && empty($subgroup['RRULE']['COUNT']))) {
// Many programs output DTEND for an all day event as the
// following day, reset this to the same day for internal use.
$subgroup['all_day'] = TRUE;
$subgroup['DTEND'] = $subgroup['DTSTART'];
}
// Add this element to the parent as an array under the
// component name
default:
prev($subgroups);
$parent =& $subgroups[key($subgroups)];
$parent[$type][] = $subgroup;
array_pop($subgroups);
array_pop($parents);
break;
}
}
}
else {
// Grab current subgroup
end($subgroups);
$subgroup =& $subgroups[key($subgroups)];
// Split up the line into nice pieces for PROPERTYNAME,
// PROPERTYATTRIBUTES, and PROPERTYVALUE
preg_match('/([^;:]+)(?:;([^:]*))?:(.+)/', $line, $matches);
$name = !empty($matches[1]) ? strtoupper(trim($matches[1])) : '';
$field = !empty($matches[2]) ? $matches[2] : '';
$data = !empty($matches[3]) ? $matches[3] : '';
$parse_result = '';
switch ($name) {
// Keep blank lines out of the results.
case '':
break;
// Lots of properties have date values that must be parsed out.
case 'CREATED':
case 'LAST-MODIFIED':
case 'DTSTART':
case 'DTEND':
case 'DTSTAMP':
case 'FREEBUSY':
case 'DUE':
case 'COMPLETED':
$parse_result = date_ical_parse_date($field, $data);
break;
case 'EXDATE':
case 'RDATE':
$parse_result = date_ical_parse_exceptions($field, $data);
break;
case 'TRIGGER':
// A TRIGGER can either be a date or in the form -PT1H.
if (!empty($field)) {
$parse_result = date_ical_parse_date($field, $data);
}
else {
$parse_result = array(
'DATA' => $data,
);
}
break;
case 'DURATION':
// Can't be sure whether DTSTART is before or after DURATION in
// the VEVENT, so store the data and parse it at the end.
$parse_result = array(
'DATA' => $data,
);
break;
case 'RRULE':
case 'EXRULE':
$parse_result = date_ical_parse_rrule($field, $data);
break;
case 'SUMMARY':
case 'DESCRIPTION':
$parse_result = date_ical_parse_text($field, $data);
break;
case 'LOCATION':
$parse_result = date_ical_parse_location($field, $data);
break;
// For all other properties, just store the property and the value.
// This can be expanded on in the future if other properties should
// be given special treatment.
default:
$parse_result = $data;
break;
}
// Store the result of our parsing
$subgroup[$name] = $parse_result;
}
}
return $items;
}