1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Parse iCal data.
|
6 |
|
|
*
|
7 |
|
|
* This file must be included when these functions are needed.
|
8 |
|
|
*/
|
9 |
|
|
|
10 |
|
|
/**
|
11 |
|
|
* Return an array of iCalendar information from an iCalendar file.
|
12 |
|
|
*
|
13 |
|
|
* No timezone adjustment is performed in the import since the timezone
|
14 |
|
|
* conversion needed will vary depending on whether the value is being
|
15 |
|
|
* imported into the database (when it needs to be converted to UTC), is being
|
16 |
|
|
* viewed on a site that has user-configurable timezones (when it needs to be
|
17 |
|
|
* converted to the user's timezone), if it needs to be converted to the
|
18 |
|
|
* site timezone, or if it is a date without a timezone which should not have
|
19 |
|
|
* any timezone conversion applied.
|
20 |
|
|
*
|
21 |
|
|
* Properties that have dates and times are converted to sub-arrays like:
|
22 |
|
|
* 'datetime' => date in YYYY-MM-DD HH:MM format, not timezone adjusted
|
23 |
|
|
* 'all_day' => whether this is an all-day event
|
24 |
|
|
* 'tz' => the timezone of the date, could be blank for absolute
|
25 |
|
|
* times that should get no timezone conversion.
|
26 |
|
|
*
|
27 |
|
|
* Exception dates can have muliple values and are returned as arrays
|
28 |
|
|
* like the above for each exception date.
|
29 |
|
|
*
|
30 |
|
|
* Most other properties are returned as PROPERTY => VALUE.
|
31 |
|
|
*
|
32 |
|
|
* Each item in the VCALENDAR will return an array like:
|
33 |
|
|
* [0] => Array (
|
34 |
|
|
* [TYPE] => VEVENT
|
35 |
|
|
* [UID] => 104
|
36 |
|
|
* [SUMMARY] => An example event
|
37 |
|
|
* [URL] => http://example.com/node/1
|
38 |
|
|
* [DTSTART] => Array (
|
39 |
|
|
* [datetime] => 1997-09-07 09:00:00
|
40 |
|
|
* [all_day] => 0
|
41 |
|
|
* [tz] => US/Eastern
|
42 |
|
|
* )
|
43 |
|
|
* [DTEND] => Array (
|
44 |
|
|
* [datetime] => 1997-09-07 11:00:00
|
45 |
|
|
* [all_day] => 0
|
46 |
|
|
* [tz] => US/Eastern
|
47 |
|
|
* )
|
48 |
|
|
* [RRULE] => Array (
|
49 |
|
|
* [FREQ] => Array (
|
50 |
|
|
* [0] => MONTHLY
|
51 |
|
|
* )
|
52 |
|
|
* [BYDAY] => Array (
|
53 |
|
|
* [0] => 1SU
|
54 |
|
|
* [1] => -1SU
|
55 |
|
|
* )
|
56 |
|
|
* )
|
57 |
|
|
* [EXDATE] => Array (
|
58 |
|
|
* [0] = Array (
|
59 |
|
|
* [datetime] => 1997-09-21 09:00:00
|
60 |
|
|
* [all_day] => 0
|
61 |
|
|
* [tz] => US/Eastern
|
62 |
|
|
* )
|
63 |
|
|
* [1] = Array (
|
64 |
|
|
* [datetime] => 1997-10-05 09:00:00
|
65 |
|
|
* [all_day] => 0
|
66 |
|
|
* [tz] => US/Eastern
|
67 |
|
|
* )
|
68 |
|
|
* )
|
69 |
|
|
* [RDATE] => Array (
|
70 |
|
|
* [0] = Array (
|
71 |
|
|
* [datetime] => 1997-09-21 09:00:00
|
72 |
|
|
* [all_day] => 0
|
73 |
|
|
* [tz] => US/Eastern
|
74 |
|
|
* )
|
75 |
|
|
* [1] = Array (
|
76 |
|
|
* [datetime] => 1997-10-05 09:00:00
|
77 |
|
|
* [all_day] => 0
|
78 |
|
|
* [tz] => US/Eastern
|
79 |
|
|
* )
|
80 |
|
|
* )
|
81 |
|
|
* )
|
82 |
|
|
*
|
83 |
|
|
* @todo
|
84 |
|
|
* figure out how to handle this if subgroups are nested,
|
85 |
|
|
* like a VALARM nested inside a VEVENT.
|
86 |
|
|
*
|
87 |
|
|
* @param string $filename
|
88 |
|
|
* Location (local or remote) of a valid iCalendar file.
|
89 |
|
|
*
|
90 |
|
|
* @return array
|
91 |
|
|
* An array with all the elements from the ical.
|
92 |
|
|
*/
|
93 |
|
|
function date_ical_import($filename) {
|
94 |
|
|
// Fetch the iCal data. If file is a URL, use drupal_http_request. fopen
|
95 |
|
|
// isn't always configured to allow network connections.
|
96 |
|
|
if (substr($filename, 0, 4) == 'http') {
|
97 |
|
|
// Fetch the ical data from the specified network location.
|
98 |
|
|
$icaldatafetch = drupal_http_request($filename);
|
99 |
|
|
// Check the return result.
|
100 |
|
|
if ($icaldatafetch->error) {
|
101 |
|
|
watchdog('date ical', 'HTTP Request Error importing %filename: @error', array('%filename' => $filename, '@error' => $icaldatafetch->error));
|
102 |
|
|
return FALSE;
|
103 |
|
|
}
|
104 |
|
|
// Break the return result into one array entry per lines.
|
105 |
|
|
$icaldatafolded = explode("\n", $icaldatafetch->data);
|
106 |
|
|
}
|
107 |
|
|
else {
|
108 |
|
|
$icaldatafolded = @file($filename, FILE_IGNORE_NEW_LINES);
|
109 |
|
|
if ($icaldatafolded === FALSE) {
|
110 |
|
|
watchdog('date ical', 'Failed to open file: %filename', array('%filename' => $filename));
|
111 |
|
|
return FALSE;
|
112 |
|
|
}
|
113 |
|
|
}
|
114 |
|
|
// Verify this is iCal data.
|
115 |
|
|
if (trim($icaldatafolded[0]) != 'BEGIN:VCALENDAR') {
|
116 |
|
|
watchdog('date ical', 'Invalid calendar file: %filename', array('%filename' => $filename));
|
117 |
|
|
return FALSE;
|
118 |
|
|
}
|
119 |
|
|
return date_ical_parse($icaldatafolded);
|
120 |
|
|
}
|
121 |
|
|
|
122 |
|
|
/**
|
123 |
|
|
* Returns an array of iCalendar information from an iCalendar file.
|
124 |
|
|
*
|
125 |
|
|
* As date_ical_import() but different param.
|
126 |
|
|
*
|
127 |
|
|
* @param array $icaldatafolded
|
128 |
|
|
* An array of lines from an ical feed.
|
129 |
|
|
*
|
130 |
|
|
* @return array
|
131 |
|
|
* An array with all the elements from the ical.
|
132 |
|
|
*/
|
133 |
|
|
function date_ical_parse($icaldatafolded = array()) {
|
134 |
|
|
$items = array();
|
135 |
|
|
|
136 |
|
|
// Verify this is iCal data.
|
137 |
|
|
if (trim($icaldatafolded[0]) != 'BEGIN:VCALENDAR') {
|
138 |
|
|
watchdog('date ical', 'Invalid calendar file.');
|
139 |
|
|
return FALSE;
|
140 |
|
|
}
|
141 |
|
|
|
142 |
|
|
// "Unfold" wrapped lines.
|
143 |
|
|
$icaldata = array();
|
144 |
|
|
foreach ($icaldatafolded as $line) {
|
145 |
|
|
$out = array();
|
146 |
|
|
// See if this looks like the beginning of a new property or value. If not,
|
147 |
|
|
// it is a continuation of the previous line. The regex is to ensure that
|
148 |
|
|
// wrapped QUOTED-PRINTABLE data is kept intact.
|
149 |
|
|
if (!preg_match('/([A-Z]+)[:;](.*)/', $line, $out)) {
|
150 |
|
|
// Trim up to 1 leading space from wrapped line per iCalendar standard.
|
151 |
|
|
$line = array_pop($icaldata) . (ltrim(substr($line, 0, 1)) . substr($line, 1));
|
152 |
|
|
}
|
153 |
|
|
$icaldata[] = $line;
|
154 |
|
|
}
|
155 |
|
|
unset($icaldatafolded);
|
156 |
|
|
|
157 |
|
|
// Parse the iCal information.
|
158 |
|
|
$parents = array();
|
159 |
|
|
$subgroups = array();
|
160 |
|
|
$vcal = '';
|
161 |
|
|
foreach ($icaldata as $line) {
|
162 |
|
|
$line = trim($line);
|
163 |
|
|
$vcal .= $line . "\n";
|
164 |
|
|
// Deal with begin/end tags separately.
|
165 |
|
|
if (preg_match('/(BEGIN|END):V(\S+)/', $line, $matches)) {
|
166 |
|
|
$closure = $matches[1];
|
167 |
|
|
$type = 'V' . $matches[2];
|
168 |
|
|
if ($closure == 'BEGIN') {
|
169 |
|
|
array_push($parents, $type);
|
170 |
|
|
array_push($subgroups, array());
|
171 |
|
|
}
|
172 |
|
|
elseif ($closure == 'END') {
|
173 |
|
|
end($subgroups);
|
174 |
|
|
$subgroup = &$subgroups[key($subgroups)];
|
175 |
|
|
switch ($type) {
|
176 |
|
|
case 'VCALENDAR':
|
177 |
|
|
if (prev($subgroups) == FALSE) {
|
178 |
|
|
$items[] = array_pop($subgroups);
|
179 |
|
|
}
|
180 |
|
|
else {
|
181 |
|
|
$parent[array_pop($parents)][] = array_pop($subgroups);
|
182 |
|
|
}
|
183 |
|
|
break;
|
184 |
b720ea3e
|
Assos Assos
|
|
185 |
85ad3d82
|
Assos Assos
|
// Add the timezones in with their index their TZID.
|
186 |
|
|
case 'VTIMEZONE':
|
187 |
|
|
$subgroup = end($subgroups);
|
188 |
|
|
$id = $subgroup['TZID'];
|
189 |
|
|
unset($subgroup['TZID']);
|
190 |
|
|
|
191 |
|
|
// Append this subgroup onto the one above it.
|
192 |
|
|
prev($subgroups);
|
193 |
|
|
$parent = &$subgroups[key($subgroups)];
|
194 |
|
|
|
195 |
|
|
$parent[$type][$id] = $subgroup;
|
196 |
|
|
|
197 |
|
|
array_pop($subgroups);
|
198 |
|
|
array_pop($parents);
|
199 |
|
|
break;
|
200 |
b720ea3e
|
Assos Assos
|
|
201 |
85ad3d82
|
Assos Assos
|
// Do some fun stuff with durations and all_day events and then append
|
202 |
|
|
// to parent.
|
203 |
|
|
case 'VEVENT':
|
204 |
|
|
case 'VALARM':
|
205 |
|
|
case 'VTODO':
|
206 |
|
|
case 'VJOURNAL':
|
207 |
|
|
case 'VVENUE':
|
208 |
|
|
case 'VFREEBUSY':
|
209 |
|
|
default:
|
210 |
|
|
// Can't be sure whether DTSTART is before or after DURATION, so
|
211 |
|
|
// parse DURATION at the end.
|
212 |
|
|
if (isset($subgroup['DURATION'])) {
|
213 |
|
|
date_ical_parse_duration($subgroup, 'DURATION');
|
214 |
|
|
}
|
215 |
|
|
// Add a top-level indication for the 'All day' condition. Leave it
|
216 |
|
|
// in the individual date components, too, so it is always available
|
217 |
|
|
// even when you are working with only a portion of the VEVENT
|
218 |
|
|
// array, like in Feed API parsers.
|
219 |
|
|
$subgroup['all_day'] = FALSE;
|
220 |
|
|
|
221 |
|
|
// iCal spec states 'The "DTEND" property for a "VEVENT" calendar
|
222 |
|
|
// component specifies the non-inclusive end of the event'. Adjust
|
223 |
|
|
// multi-day events to remove the extra day because the Date code
|
224 |
|
|
// assumes the end date is inclusive.
|
225 |
|
|
if (!empty($subgroup['DTEND']) && (!empty($subgroup['DTEND']['all_day']))) {
|
226 |
|
|
// Make the end date one day earlier.
|
227 |
b720ea3e
|
Assos Assos
|
$date = new DateObject($subgroup['DTEND']['datetime'] . ' 00:00:00', $subgroup['DTEND']['tz']);
|
228 |
85ad3d82
|
Assos Assos
|
date_modify($date, '-1 day');
|
229 |
b720ea3e
|
Assos Assos
|
$subgroup['DTEND']['datetime'] = date_format($date, 'Y-m-d');
|
230 |
85ad3d82
|
Assos Assos
|
}
|
231 |
|
|
// If a start datetime is defined AND there is no definition for
|
232 |
|
|
// the end datetime THEN make the end datetime equal the start
|
233 |
|
|
// datetime and if it is an all day event define the entire event
|
234 |
|
|
// as a single all day event.
|
235 |
|
|
if (!empty($subgroup['DTSTART']) &&
|
236 |
|
|
(empty($subgroup['DTEND']) && empty($subgroup['RRULE']) && empty($subgroup['RRULE']['COUNT']))) {
|
237 |
|
|
$subgroup['DTEND'] = $subgroup['DTSTART'];
|
238 |
|
|
}
|
239 |
|
|
// Add this element to the parent as an array under the component
|
240 |
|
|
// name.
|
241 |
|
|
if (!empty($subgroup['DTSTART']['all_day'])) {
|
242 |
|
|
$subgroup['all_day'] = TRUE;
|
243 |
|
|
}
|
244 |
b720ea3e
|
Assos Assos
|
// Add this element to the parent as an array under the.
|
245 |
85ad3d82
|
Assos Assos
|
prev($subgroups);
|
246 |
|
|
$parent = &$subgroups[key($subgroups)];
|
247 |
|
|
|
248 |
|
|
$parent[$type][] = $subgroup;
|
249 |
|
|
|
250 |
|
|
array_pop($subgroups);
|
251 |
|
|
array_pop($parents);
|
252 |
|
|
break;
|
253 |
|
|
}
|
254 |
|
|
}
|
255 |
|
|
}
|
256 |
|
|
// Handle all other possibilities.
|
257 |
|
|
else {
|
258 |
|
|
// Grab current subgroup.
|
259 |
|
|
end($subgroups);
|
260 |
|
|
$subgroup = &$subgroups[key($subgroups)];
|
261 |
|
|
|
262 |
|
|
// Split up the line into nice pieces for PROPERTYNAME,
|
263 |
|
|
// PROPERTYATTRIBUTES, and PROPERTYVALUE.
|
264 |
|
|
preg_match('/([^;:]+)(?:;([^:]*))?:(.+)/', $line, $matches);
|
265 |
|
|
$name = !empty($matches[1]) ? strtoupper(trim($matches[1])) : '';
|
266 |
|
|
$field = !empty($matches[2]) ? $matches[2] : '';
|
267 |
|
|
$data = !empty($matches[3]) ? $matches[3] : '';
|
268 |
|
|
$parse_result = '';
|
269 |
b720ea3e
|
Assos Assos
|
|
270 |
85ad3d82
|
Assos Assos
|
switch ($name) {
|
271 |
|
|
// Keep blank lines out of the results.
|
272 |
|
|
case '':
|
273 |
|
|
break;
|
274 |
|
|
|
275 |
b720ea3e
|
Assos Assos
|
// Lots of properties have date values that must be parsed out.
|
276 |
85ad3d82
|
Assos Assos
|
case 'CREATED':
|
277 |
|
|
case 'LAST-MODIFIED':
|
278 |
|
|
case 'DTSTART':
|
279 |
|
|
case 'DTEND':
|
280 |
|
|
case 'DTSTAMP':
|
281 |
|
|
case 'FREEBUSY':
|
282 |
|
|
case 'DUE':
|
283 |
|
|
case 'COMPLETED':
|
284 |
|
|
$parse_result = date_ical_parse_date($field, $data);
|
285 |
|
|
break;
|
286 |
|
|
|
287 |
|
|
case 'EXDATE':
|
288 |
|
|
case 'RDATE':
|
289 |
|
|
$parse_result = date_ical_parse_exceptions($field, $data);
|
290 |
|
|
break;
|
291 |
|
|
|
292 |
|
|
case 'TRIGGER':
|
293 |
|
|
// A TRIGGER can either be a date or in the form -PT1H.
|
294 |
|
|
if (!empty($field)) {
|
295 |
|
|
$parse_result = date_ical_parse_date($field, $data);
|
296 |
|
|
}
|
297 |
|
|
else {
|
298 |
|
|
$parse_result = array('DATA' => $data);
|
299 |
|
|
}
|
300 |
|
|
break;
|
301 |
|
|
|
302 |
|
|
case 'DURATION':
|
303 |
|
|
// Can't be sure whether DTSTART is before or after DURATION in
|
304 |
|
|
// the VEVENT, so store the data and parse it at the end.
|
305 |
|
|
$parse_result = array('DATA' => $data);
|
306 |
|
|
break;
|
307 |
|
|
|
308 |
|
|
case 'RRULE':
|
309 |
|
|
case 'EXRULE':
|
310 |
|
|
$parse_result = date_ical_parse_rrule($field, $data);
|
311 |
|
|
break;
|
312 |
|
|
|
313 |
|
|
case 'STATUS':
|
314 |
|
|
case 'SUMMARY':
|
315 |
|
|
case 'DESCRIPTION':
|
316 |
|
|
$parse_result = date_ical_parse_text($field, $data);
|
317 |
|
|
break;
|
318 |
|
|
|
319 |
|
|
case 'LOCATION':
|
320 |
|
|
$parse_result = date_ical_parse_location($field, $data);
|
321 |
|
|
break;
|
322 |
|
|
|
323 |
b720ea3e
|
Assos Assos
|
// For all other properties, just store the property and the value.
|
324 |
|
|
// This can be expanded on in the future if other properties should
|
325 |
|
|
// be given special treatment.
|
326 |
85ad3d82
|
Assos Assos
|
default:
|
327 |
|
|
$parse_result = $data;
|
328 |
|
|
break;
|
329 |
|
|
}
|
330 |
|
|
|
331 |
|
|
// Store the result of our parsing.
|
332 |
|
|
$subgroup[$name] = $parse_result;
|
333 |
|
|
}
|
334 |
|
|
}
|
335 |
|
|
return $items;
|
336 |
|
|
}
|
337 |
|
|
|
338 |
|
|
/**
|
339 |
|
|
* Parses a ical date element.
|
340 |
|
|
*
|
341 |
|
|
* Possible formats to parse include:
|
342 |
|
|
* PROPERTY:YYYYMMDD[T][HH][MM][SS][Z]
|
343 |
|
|
* PROPERTY;VALUE=DATE:YYYYMMDD[T][HH][MM][SS][Z]
|
344 |
|
|
* PROPERTY;VALUE=DATE-TIME:YYYYMMDD[T][HH][MM][SS][Z]
|
345 |
|
|
* PROPERTY;TZID=XXXXXXXX;VALUE=DATE:YYYYMMDD[T][HH][MM][SS]
|
346 |
|
|
* PROPERTY;TZID=XXXXXXXX:YYYYMMDD[T][HH][MM][SS]
|
347 |
|
|
*
|
348 |
|
|
* The property and the colon before the date are removed in the import
|
349 |
|
|
* process above and we are left with $field and $data.
|
350 |
|
|
*
|
351 |
|
|
* @param string $field
|
352 |
|
|
* The text before the colon and the date, i.e.
|
353 |
|
|
* ';VALUE=DATE:', ';VALUE=DATE-TIME:', ';TZID='
|
354 |
|
|
* @param string $data
|
355 |
|
|
* The date itself, after the colon, in the format YYYYMMDD[T][HH][MM][SS][Z]
|
356 |
|
|
* 'Z', if supplied, means the date is in UTC.
|
357 |
|
|
*
|
358 |
|
|
* @return array
|
359 |
|
|
* $items array, consisting of:
|
360 |
|
|
* 'datetime' => date in YYYY-MM-DD HH:MM format, not timezone adjusted
|
361 |
|
|
* 'all_day' => whether this is an all-day event with no time
|
362 |
|
|
* 'tz' => the timezone of the date, could be blank if the ical
|
363 |
|
|
* has no timezone; the ical specs say no timezone
|
364 |
|
|
* conversion should be done if no timezone info is
|
365 |
|
|
* supplied
|
366 |
b720ea3e
|
Assos Assos
|
* @todo
|
367 |
85ad3d82
|
Assos Assos
|
* Another option for dates is the format PROPERTY;VALUE=PERIOD:XXXX. The
|
368 |
|
|
* period may include a duration, or a date and a duration, or two dates, so
|
369 |
|
|
* would have to be split into parts and run through date_ical_parse_date()
|
370 |
|
|
* and date_ical_parse_duration(). This is not commonly used, so ignored for
|
371 |
|
|
* now. It will take more work to figure how to support that.
|
372 |
|
|
*/
|
373 |
|
|
function date_ical_parse_date($field, $data) {
|
374 |
|
|
|
375 |
|
|
$items = array('datetime' => '', 'all_day' => '', 'tz' => '');
|
376 |
|
|
if (empty($data)) {
|
377 |
|
|
return $items;
|
378 |
|
|
}
|
379 |
|
|
// Make this a little more whitespace independent.
|
380 |
|
|
$data = trim($data);
|
381 |
|
|
|
382 |
|
|
// Turn the properties into a nice indexed array of
|
383 |
|
|
// array(PROPERTYNAME => PROPERTYVALUE);
|
384 |
|
|
$field_parts = preg_split('/[;:]/', $field);
|
385 |
|
|
$properties = array();
|
386 |
|
|
foreach ($field_parts as $part) {
|
387 |
|
|
if (strpos($part, '=') !== FALSE) {
|
388 |
|
|
$tmp = explode('=', $part);
|
389 |
|
|
$properties[$tmp[0]] = $tmp[1];
|
390 |
|
|
}
|
391 |
|
|
}
|
392 |
|
|
|
393 |
|
|
// Make this a little more whitespace independent.
|
394 |
|
|
$data = trim($data);
|
395 |
|
|
|
396 |
|
|
// Record if a time has been found.
|
397 |
|
|
$has_time = FALSE;
|
398 |
|
|
|
399 |
|
|
// If a format is specified, parse it according to that format.
|
400 |
|
|
if (isset($properties['VALUE'])) {
|
401 |
|
|
switch ($properties['VALUE']) {
|
402 |
|
|
case 'DATE':
|
403 |
|
|
preg_match(DATE_REGEX_ICAL_DATE, $data, $regs);
|
404 |
|
|
// Date.
|
405 |
|
|
$datetime = date_pad($regs[1]) . '-' . date_pad($regs[2]) . '-' . date_pad($regs[3]);
|
406 |
|
|
break;
|
407 |
b720ea3e
|
Assos Assos
|
|
408 |
85ad3d82
|
Assos Assos
|
case 'DATE-TIME':
|
409 |
|
|
preg_match(DATE_REGEX_ICAL_DATETIME, $data, $regs);
|
410 |
|
|
// Date.
|
411 |
|
|
$datetime = date_pad($regs[1]) . '-' . date_pad($regs[2]) . '-' . date_pad($regs[3]);
|
412 |
|
|
// Time.
|
413 |
|
|
$datetime .= ' ' . date_pad($regs[4]) . ':' . date_pad($regs[5]) . ':' . date_pad($regs[6]);
|
414 |
|
|
$has_time = TRUE;
|
415 |
|
|
break;
|
416 |
|
|
}
|
417 |
|
|
}
|
418 |
|
|
// If no format is specified, attempt a loose match.
|
419 |
|
|
else {
|
420 |
|
|
preg_match(DATE_REGEX_LOOSE, $data, $regs);
|
421 |
|
|
if (!empty($regs) && count($regs) > 2) {
|
422 |
|
|
// Date.
|
423 |
|
|
$datetime = date_pad($regs[1]) . '-' . date_pad($regs[2]) . '-' . date_pad($regs[3]);
|
424 |
|
|
if (isset($regs[4])) {
|
425 |
|
|
$has_time = TRUE;
|
426 |
|
|
// Time.
|
427 |
|
|
$datetime .= ' ' . (!empty($regs[5]) ? date_pad($regs[5]) : '00') .
|
428 |
|
|
':' . (!empty($regs[6]) ? date_pad($regs[6]) : '00') .
|
429 |
|
|
':' . (!empty($regs[7]) ? date_pad($regs[7]) : '00');
|
430 |
|
|
}
|
431 |
|
|
}
|
432 |
|
|
}
|
433 |
|
|
|
434 |
|
|
// Use timezone if explicitly declared.
|
435 |
|
|
if (isset($properties['TZID'])) {
|
436 |
|
|
$tz = $properties['TZID'];
|
437 |
|
|
// Fix alternatives like US-Eastern which should be US/Eastern.
|
438 |
|
|
$tz = str_replace('-', '/', $tz);
|
439 |
|
|
// Unset invalid timezone names.
|
440 |
|
|
module_load_include('inc', 'date_api', 'date_api.admin');
|
441 |
|
|
$tz = _date_timezone_replacement($tz);
|
442 |
|
|
if (!date_timezone_is_valid($tz)) {
|
443 |
|
|
$tz = '';
|
444 |
|
|
}
|
445 |
|
|
}
|
446 |
|
|
// If declared as UTC with terminating 'Z', use that timezone.
|
447 |
|
|
elseif (strpos($data, 'Z') !== FALSE) {
|
448 |
|
|
$tz = 'UTC';
|
449 |
|
|
}
|
450 |
|
|
// Otherwise this date is floating.
|
451 |
|
|
else {
|
452 |
|
|
$tz = '';
|
453 |
|
|
}
|
454 |
|
|
|
455 |
|
|
$items['datetime'] = $datetime;
|
456 |
|
|
$items['all_day'] = $has_time ? FALSE : TRUE;
|
457 |
|
|
$items['tz'] = $tz;
|
458 |
|
|
return $items;
|
459 |
|
|
}
|
460 |
|
|
|
461 |
|
|
/**
|
462 |
|
|
* Parse an ical repeat rule.
|
463 |
|
|
*
|
464 |
|
|
* @return array
|
465 |
|
|
* Array in the form of PROPERTY => array(VALUES)
|
466 |
|
|
* PROPERTIES include FREQ, INTERVAL, COUNT, BYDAY, BYMONTH, BYYEAR, UNTIL
|
467 |
|
|
*/
|
468 |
|
|
function date_ical_parse_rrule($field, $data) {
|
469 |
|
|
$data = preg_replace("/RRULE.*:/", '', $data);
|
470 |
|
|
$items = array('DATA' => $data);
|
471 |
|
|
$rrule = explode(';', $data);
|
472 |
|
|
foreach ($rrule as $key => $value) {
|
473 |
|
|
$param = explode('=', $value);
|
474 |
|
|
// Must be some kind of invalid data.
|
475 |
|
|
if (count($param) != 2) {
|
476 |
|
|
continue;
|
477 |
|
|
}
|
478 |
|
|
if ($param[0] == 'UNTIL') {
|
479 |
|
|
$values = date_ical_parse_date('', $param[1]);
|
480 |
|
|
}
|
481 |
|
|
else {
|
482 |
|
|
$values = explode(',', $param[1]);
|
483 |
|
|
}
|
484 |
|
|
// Treat items differently if they have multiple or single values.
|
485 |
|
|
if (in_array($param[0], array('FREQ', 'INTERVAL', 'COUNT', 'WKST'))) {
|
486 |
|
|
$items[$param[0]] = $param[1];
|
487 |
|
|
}
|
488 |
|
|
else {
|
489 |
|
|
$items[$param[0]] = $values;
|
490 |
|
|
}
|
491 |
|
|
}
|
492 |
|
|
return $items;
|
493 |
|
|
}
|
494 |
|
|
|
495 |
|
|
/**
|
496 |
|
|
* Parse exception dates (can be multiple values).
|
497 |
|
|
*
|
498 |
|
|
* @return array
|
499 |
|
|
* an array of date value arrays.
|
500 |
|
|
*/
|
501 |
|
|
function date_ical_parse_exceptions($field, $data) {
|
502 |
|
|
$data = str_replace($field . ':', '', $data);
|
503 |
|
|
$items = array('DATA' => $data);
|
504 |
|
|
$ex_dates = explode(',', $data);
|
505 |
|
|
foreach ($ex_dates as $ex_date) {
|
506 |
|
|
$items[] = date_ical_parse_date('', $ex_date);
|
507 |
|
|
}
|
508 |
|
|
return $items;
|
509 |
|
|
}
|
510 |
|
|
|
511 |
|
|
/**
|
512 |
|
|
* Parses the duration of the event.
|
513 |
|
|
*
|
514 |
|
|
* Example:
|
515 |
|
|
* DURATION:PT1H30M
|
516 |
|
|
* DURATION:P1Y2M
|
517 |
|
|
*
|
518 |
|
|
* @param array $subgroup
|
519 |
|
|
* Array of other values in the vevent so we can check for DTSTART.
|
520 |
|
|
*/
|
521 |
|
|
function date_ical_parse_duration(&$subgroup, $field = 'DURATION') {
|
522 |
|
|
$items = $subgroup[$field];
|
523 |
|
|
$data = $items['DATA'];
|
524 |
|
|
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);
|
525 |
|
|
$items['year'] = isset($duration[1]) ? str_replace('Y', '', $duration[1]) : '';
|
526 |
b720ea3e
|
Assos Assos
|
$items['month'] = isset($duration[2]) ? str_replace('M', '', $duration[2]) : '';
|
527 |
|
|
$items['week'] = isset($duration[3]) ? str_replace('W', '', $duration[3]) : '';
|
528 |
|
|
$items['day'] = isset($duration[4]) ? str_replace('D', '', $duration[4]) : '';
|
529 |
|
|
$items['hour'] = isset($duration[6]) ? str_replace('H', '', $duration[6]) : '';
|
530 |
|
|
$items['minute'] = isset($duration[7]) ? str_replace('M', '', $duration[7]) : '';
|
531 |
|
|
$items['second'] = isset($duration[8]) ? str_replace('S', '', $duration[8]) : '';
|
532 |
85ad3d82
|
Assos Assos
|
$start_date = array_key_exists('DTSTART', $subgroup) ? $subgroup['DTSTART']['datetime'] : date_format(date_now(), DATE_FORMAT_ISO);
|
533 |
|
|
$timezone = array_key_exists('DTSTART', $subgroup) ? $subgroup['DTSTART']['tz'] : variable_get('date_default_timezone');
|
534 |
|
|
if (empty($timezone)) {
|
535 |
|
|
$timezone = 'UTC';
|
536 |
|
|
}
|
537 |
|
|
$date = new DateObject($start_date, $timezone);
|
538 |
|
|
$date2 = clone($date);
|
539 |
|
|
foreach ($items as $item => $count) {
|
540 |
|
|
if ($count > 0) {
|
541 |
|
|
date_modify($date2, '+' . $count . ' ' . $item);
|
542 |
|
|
}
|
543 |
|
|
}
|
544 |
|
|
$format = isset($subgroup['DTSTART']['type']) && $subgroup['DTSTART']['type'] == 'DATE' ? 'Y-m-d' : 'Y-m-d H:i:s';
|
545 |
|
|
$subgroup['DTEND'] = array(
|
546 |
|
|
'datetime' => date_format($date2, DATE_FORMAT_DATETIME),
|
547 |
|
|
'all_day' => isset($subgroup['DTSTART']['all_day']) ? $subgroup['DTSTART']['all_day'] : 0,
|
548 |
|
|
'tz' => $timezone,
|
549 |
b720ea3e
|
Assos Assos
|
);
|
550 |
85ad3d82
|
Assos Assos
|
$duration = date_format($date2, 'U') - date_format($date, 'U');
|
551 |
|
|
$subgroup['DURATION'] = array('DATA' => $data, 'DURATION' => $duration);
|
552 |
|
|
}
|
553 |
|
|
|
554 |
|
|
/**
|
555 |
|
|
* Parse and clean up ical text elements.
|
556 |
|
|
*/
|
557 |
|
|
function date_ical_parse_text($field, $data) {
|
558 |
|
|
if (strstr($field, 'QUOTED-PRINTABLE')) {
|
559 |
|
|
$data = quoted_printable_decode($data);
|
560 |
|
|
}
|
561 |
|
|
// Strip line breaks within element.
|
562 |
|
|
$data = str_replace(array("\r\n ", "\n ", "\r "), '', $data);
|
563 |
|
|
// Put in line breaks where encoded.
|
564 |
|
|
$data = str_replace(array("\\n", "\\N"), "\n", $data);
|
565 |
|
|
// Remove other escaping.
|
566 |
|
|
$data = stripslashes($data);
|
567 |
|
|
return $data;
|
568 |
|
|
}
|
569 |
|
|
|
570 |
|
|
/**
|
571 |
|
|
* Parse location elements.
|
572 |
|
|
*
|
573 |
|
|
* Catch situations like the upcoming.org feed that uses
|
574 |
|
|
* LOCATION;VENUE-UID="http://upcoming.yahoo.com/venue/104/":111 First Street...
|
575 |
|
|
* or more normal LOCATION;UID=123:111 First Street...
|
576 |
|
|
* Upcoming feed would have been improperly broken on the ':' in http://
|
577 |
|
|
* so we paste the $field and $data back together first.
|
578 |
|
|
*
|
579 |
|
|
* Use non-greedy check for ':' in case there are more of them in the address.
|
580 |
|
|
*/
|
581 |
|
|
function date_ical_parse_location($field, $data) {
|
582 |
|
|
if (preg_match('/UID=[?"](.+)[?"][*?:](.+)/', $field . ':' . $data, $matches)) {
|
583 |
|
|
$location = array();
|
584 |
|
|
$location['UID'] = $matches[1];
|
585 |
|
|
$location['DESCRIPTION'] = stripslashes($matches[2]);
|
586 |
|
|
return $location;
|
587 |
|
|
}
|
588 |
|
|
else {
|
589 |
|
|
// Remove other escaping.
|
590 |
|
|
$location = stripslashes($data);
|
591 |
|
|
return $location;
|
592 |
|
|
}
|
593 |
|
|
}
|
594 |
|
|
|
595 |
|
|
/**
|
596 |
|
|
* Return a date object for the ical date, adjusted to its local timezone.
|
597 |
|
|
*
|
598 |
|
|
* @param array $ical_date
|
599 |
|
|
* An array of ical date information created in the ical import.
|
600 |
|
|
* @param string $to_tz
|
601 |
|
|
* The timezone to convert the date's value to.
|
602 |
|
|
*
|
603 |
|
|
* @return object
|
604 |
|
|
* A timezone-adjusted date object.
|
605 |
|
|
*/
|
606 |
|
|
function date_ical_date($ical_date, $to_tz = FALSE) {
|
607 |
|
|
|
608 |
|
|
// If the ical date has no timezone, must assume it is stateless
|
609 |
|
|
// so treat it as a local date.
|
610 |
|
|
if (empty($ical_date['datetime'])) {
|
611 |
|
|
return NULL;
|
612 |
|
|
}
|
613 |
|
|
elseif (empty($ical_date['tz'])) {
|
614 |
|
|
$from_tz = date_default_timezone();
|
615 |
|
|
}
|
616 |
|
|
else {
|
617 |
|
|
$from_tz = $ical_date['tz'];
|
618 |
|
|
}
|
619 |
|
|
if (strlen($ical_date['datetime']) < 11) {
|
620 |
|
|
$ical_date['datetime'] .= ' 00:00:00';
|
621 |
|
|
}
|
622 |
|
|
$date = new DateObject($ical_date['datetime'], new DateTimeZone($from_tz));
|
623 |
|
|
|
624 |
|
|
if ($to_tz && $ical_date['tz'] != '' && $to_tz != $ical_date['tz']) {
|
625 |
|
|
date_timezone_set($date, timezone_open($to_tz));
|
626 |
|
|
}
|
627 |
|
|
return $date;
|
628 |
|
|
}
|
629 |
|
|
|
630 |
|
|
/**
|
631 |
|
|
* Escape #text elements for safe iCal use.
|
632 |
|
|
*
|
633 |
|
|
* @param string $text
|
634 |
|
|
* Text to escape
|
635 |
|
|
*
|
636 |
|
|
* @return string
|
637 |
|
|
* Escaped text
|
638 |
|
|
*/
|
639 |
|
|
function date_ical_escape_text($text) {
|
640 |
|
|
$text = drupal_html_to_text($text);
|
641 |
|
|
$text = trim($text);
|
642 |
|
|
// TODO Per #38130 the iCal specs don't want : and " escaped
|
643 |
|
|
// but there was some reason for adding this in. Need to watch
|
644 |
|
|
// this and see if anything breaks.
|
645 |
|
|
// $text = str_replace('"', '\"', $text);
|
646 |
|
|
// $text = str_replace(":", "\:", $text);
|
647 |
|
|
$text = preg_replace("/\\\b/", "\\\\", $text);
|
648 |
|
|
$text = str_replace(",", "\,", $text);
|
649 |
|
|
$text = str_replace(";", "\;", $text);
|
650 |
|
|
$text = str_replace("\n", "\\n ", $text);
|
651 |
|
|
return trim($text);
|
652 |
|
|
}
|
653 |
|
|
|
654 |
|
|
/**
|
655 |
|
|
* Build an iCal RULE from $form_values.
|
656 |
|
|
*
|
657 |
|
|
* @param array $form_values
|
658 |
|
|
* An array constructed like the one created by date_ical_parse_rrule().
|
659 |
|
|
* [RRULE] => Array (
|
660 |
|
|
* [FREQ] => Array (
|
661 |
|
|
* [0] => MONTHLY
|
662 |
|
|
* )
|
663 |
|
|
* [BYDAY] => Array (
|
664 |
|
|
* [0] => 1SU
|
665 |
|
|
* [1] => -1SU
|
666 |
|
|
* )
|
667 |
|
|
* [UNTIL] => Array (
|
668 |
|
|
* [datetime] => 1997-21-31 09:00:00
|
669 |
|
|
* [all_day] => 0
|
670 |
|
|
* [tz] => US/Eastern
|
671 |
|
|
* )
|
672 |
|
|
* )
|
673 |
|
|
* [EXDATE] => Array (
|
674 |
|
|
* [0] = Array (
|
675 |
|
|
* [datetime] => 1997-09-21 09:00:00
|
676 |
|
|
* [all_day] => 0
|
677 |
|
|
* [tz] => US/Eastern
|
678 |
|
|
* )
|
679 |
|
|
* [1] = Array (
|
680 |
|
|
* [datetime] => 1997-10-05 09:00:00
|
681 |
|
|
* [all_day] => 0
|
682 |
|
|
* [tz] => US/Eastern
|
683 |
|
|
* )
|
684 |
|
|
* )
|
685 |
|
|
* [RDATE] => Array (
|
686 |
|
|
* [0] = Array (
|
687 |
|
|
* [datetime] => 1997-09-21 09:00:00
|
688 |
|
|
* [all_day] => 0
|
689 |
|
|
* [tz] => US/Eastern
|
690 |
|
|
* )
|
691 |
|
|
* [1] = Array (
|
692 |
|
|
* [datetime] => 1997-10-05 09:00:00
|
693 |
|
|
* [all_day] => 0
|
694 |
|
|
* [tz] => US/Eastern
|
695 |
|
|
* )
|
696 |
|
|
* )
|
697 |
|
|
*/
|
698 |
|
|
function date_api_ical_build_rrule($form_values) {
|
699 |
b720ea3e
|
Assos Assos
|
$rrule = '';
|
700 |
85ad3d82
|
Assos Assos
|
if (empty($form_values) || !is_array($form_values)) {
|
701 |
b720ea3e
|
Assos Assos
|
return $rrule;
|
702 |
85ad3d82
|
Assos Assos
|
}
|
703 |
|
|
|
704 |
|
|
// Grab the RRULE data and put them into iCal RRULE format.
|
705 |
b720ea3e
|
Assos Assos
|
$rrule .= 'RRULE:FREQ=' . (!array_key_exists('FREQ', $form_values) ? 'DAILY' : $form_values['FREQ']);
|
706 |
|
|
$rrule .= ';INTERVAL=' . (!array_key_exists('INTERVAL', $form_values) ? 1 : $form_values['INTERVAL']);
|
707 |
85ad3d82
|
Assos Assos
|
|
708 |
|
|
// Unset the empty 'All' values.
|
709 |
|
|
if (array_key_exists('BYDAY', $form_values) && is_array($form_values['BYDAY'])) {
|
710 |
|
|
unset($form_values['BYDAY']['']);
|
711 |
|
|
}
|
712 |
|
|
if (array_key_exists('BYMONTH', $form_values) && is_array($form_values['BYMONTH'])) {
|
713 |
|
|
unset($form_values['BYMONTH']['']);
|
714 |
|
|
}
|
715 |
|
|
if (array_key_exists('BYMONTHDAY', $form_values) && is_array($form_values['BYMONTHDAY'])) {
|
716 |
|
|
unset($form_values['BYMONTHDAY']['']);
|
717 |
|
|
}
|
718 |
|
|
|
719 |
b720ea3e
|
Assos Assos
|
if (array_key_exists('BYDAY', $form_values) && is_array($form_values['BYDAY']) && $byday = implode(",", $form_values['BYDAY'])) {
|
720 |
|
|
$rrule .= ';BYDAY=' . $byday;
|
721 |
85ad3d82
|
Assos Assos
|
}
|
722 |
b720ea3e
|
Assos Assos
|
if (array_key_exists('BYMONTH', $form_values) && is_array($form_values['BYMONTH']) && $bymonth = implode(",", $form_values['BYMONTH'])) {
|
723 |
|
|
$rrule .= ';BYMONTH=' . $bymonth;
|
724 |
85ad3d82
|
Assos Assos
|
}
|
725 |
b720ea3e
|
Assos Assos
|
if (array_key_exists('BYMONTHDAY', $form_values) && is_array($form_values['BYMONTHDAY']) && $bymonthday = implode(",", $form_values['BYMONTHDAY'])) {
|
726 |
|
|
$rrule .= ';BYMONTHDAY=' . $bymonthday;
|
727 |
85ad3d82
|
Assos Assos
|
}
|
728 |
|
|
// The UNTIL date is supposed to always be expressed in UTC.
|
729 |
|
|
// The input date values may already have been converted to a date object on a
|
730 |
|
|
// previous pass, so check for that.
|
731 |
|
|
if (array_key_exists('UNTIL', $form_values) && array_key_exists('datetime', $form_values['UNTIL']) && !empty($form_values['UNTIL']['datetime'])) {
|
732 |
|
|
// We only collect a date for UNTIL, but we need it to be inclusive, so
|
733 |
|
|
// force it to a full datetime element at the last second of the day.
|
734 |
|
|
if (!is_object($form_values['UNTIL']['datetime'])) {
|
735 |
|
|
// If this is a date without time, give it time.
|
736 |
|
|
if (strlen($form_values['UNTIL']['datetime']) < 11) {
|
737 |
b720ea3e
|
Assos Assos
|
$granularity_options = drupal_map_assoc(array(
|
738 |
|
|
'year',
|
739 |
|
|
'month',
|
740 |
|
|
'day',
|
741 |
|
|
'hour',
|
742 |
|
|
'minute',
|
743 |
|
|
'second',
|
744 |
|
|
));
|
745 |
|
|
|
746 |
85ad3d82
|
Assos Assos
|
$form_values['UNTIL']['datetime'] .= ' 23:59:59';
|
747 |
b720ea3e
|
Assos Assos
|
$form_values['UNTIL']['granularity'] = serialize($granularity_options);
|
748 |
85ad3d82
|
Assos Assos
|
$form_values['UNTIL']['all_day'] = FALSE;
|
749 |
|
|
}
|
750 |
|
|
$until = date_ical_date($form_values['UNTIL'], 'UTC');
|
751 |
|
|
}
|
752 |
|
|
else {
|
753 |
|
|
$until = $form_values['UNTIL']['datetime'];
|
754 |
|
|
}
|
755 |
b720ea3e
|
Assos Assos
|
$rrule .= ';UNTIL=' . date_format($until, DATE_FORMAT_ICAL) . 'Z';
|
756 |
85ad3d82
|
Assos Assos
|
}
|
757 |
|
|
// Our form doesn't allow a value for COUNT, but it may be needed by
|
758 |
|
|
// modules using the API, so add it to the rule.
|
759 |
|
|
if (array_key_exists('COUNT', $form_values)) {
|
760 |
b720ea3e
|
Assos Assos
|
$rrule .= ';COUNT=' . $form_values['COUNT'];
|
761 |
85ad3d82
|
Assos Assos
|
}
|
762 |
|
|
|
763 |
|
|
// iCal rules presume the week starts on Monday unless otherwise specified,
|
764 |
|
|
// so we'll specify it.
|
765 |
|
|
if (array_key_exists('WKST', $form_values)) {
|
766 |
b720ea3e
|
Assos Assos
|
$rrule .= ';WKST=' . $form_values['WKST'];
|
767 |
85ad3d82
|
Assos Assos
|
}
|
768 |
|
|
else {
|
769 |
b720ea3e
|
Assos Assos
|
$rrule .= ';WKST=' . date_repeat_dow2day(variable_get('date_first_day', 0));
|
770 |
85ad3d82
|
Assos Assos
|
}
|
771 |
|
|
|
772 |
|
|
// Exceptions dates go last, on their own line.
|
773 |
|
|
// The input date values may already have been converted to a date
|
774 |
|
|
// object on a previous pass, so check for that.
|
775 |
|
|
if (isset($form_values['EXDATE']) && is_array($form_values['EXDATE'])) {
|
776 |
|
|
$ex_dates = array();
|
777 |
|
|
foreach ($form_values['EXDATE'] as $value) {
|
778 |
|
|
if (!empty($value['datetime'])) {
|
779 |
|
|
$date = !is_object($value['datetime']) ? date_ical_date($value, 'UTC') : $value['datetime'];
|
780 |
b720ea3e
|
Assos Assos
|
$ex_date = !empty($date) ? date_format($date, DATE_FORMAT_ICAL) . 'Z' : '';
|
781 |
85ad3d82
|
Assos Assos
|
if (!empty($ex_date)) {
|
782 |
|
|
$ex_dates[] = $ex_date;
|
783 |
|
|
}
|
784 |
|
|
}
|
785 |
|
|
}
|
786 |
|
|
if (!empty($ex_dates)) {
|
787 |
|
|
sort($ex_dates);
|
788 |
b720ea3e
|
Assos Assos
|
$rrule .= chr(13) . chr(10) . 'EXDATE:' . implode(',', $ex_dates);
|
789 |
85ad3d82
|
Assos Assos
|
}
|
790 |
|
|
}
|
791 |
|
|
elseif (!empty($form_values['EXDATE'])) {
|
792 |
b720ea3e
|
Assos Assos
|
$rrule .= chr(13) . chr(10) . 'EXDATE:' . $form_values['EXDATE'];
|
793 |
85ad3d82
|
Assos Assos
|
}
|
794 |
|
|
|
795 |
|
|
// Exceptions dates go last, on their own line.
|
796 |
|
|
if (isset($form_values['RDATE']) && is_array($form_values['RDATE'])) {
|
797 |
|
|
$ex_dates = array();
|
798 |
|
|
foreach ($form_values['RDATE'] as $value) {
|
799 |
|
|
$date = !is_object($value['datetime']) ? date_ical_date($value, 'UTC') : $value['datetime'];
|
800 |
b720ea3e
|
Assos Assos
|
$ex_date = !empty($date) ? date_format($date, DATE_FORMAT_ICAL) . 'Z' : '';
|
801 |
85ad3d82
|
Assos Assos
|
if (!empty($ex_date)) {
|
802 |
|
|
$ex_dates[] = $ex_date;
|
803 |
|
|
}
|
804 |
|
|
}
|
805 |
|
|
if (!empty($ex_dates)) {
|
806 |
|
|
sort($ex_dates);
|
807 |
b720ea3e
|
Assos Assos
|
$rrule .= chr(13) . chr(10) . 'RDATE:' . implode(',', $ex_dates);
|
808 |
85ad3d82
|
Assos Assos
|
}
|
809 |
|
|
}
|
810 |
|
|
elseif (!empty($form_values['RDATE'])) {
|
811 |
b720ea3e
|
Assos Assos
|
$rrule .= chr(13) . chr(10) . 'RDATE:' . $form_values['RDATE'];
|
812 |
85ad3d82
|
Assos Assos
|
}
|
813 |
|
|
|
814 |
b720ea3e
|
Assos Assos
|
return $rrule;
|
815 |
85ad3d82
|
Assos Assos
|
} |