forked from u01jmg3/ics-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.iCalReader.php
542 lines (499 loc) · 24.1 KB
/
class.iCalReader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
<?php
/**
* This PHP-Class should only read a iCal-File (*.ics), parse it and give an
* array with its content.
*
* PHP Version 5
*
* @category Parser
* @package Ics-parser
* @author Martin Thoma <[email protected]>
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* @version SVN: <svn_id>
* @link http://code.google.com/p/ics-parser/
* @example $ical = new ical('MyCal.ics');
* print_r( $ical->events() );
*/
// error_reporting(E_ALL);
/**
* This is the iCal-class
*
* @category Parser
* @package Ics-parser
* @author Martin Thoma <[email protected]>
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* @link http://code.google.com/p/ics-parser/
*
* @param {string} filename The name of the file which should be parsed
* @constructor
*/
class ICal
{
/* How many ToDos are in this ical? */
public /** @type {int} */ $todo_count = 0;
/* How many events are in this ical? */
public /** @type {int} */ $event_count = 0;
/* How many freebusy are in this ical? */
public /** @type {int} */ $freebusy_count = 0;
/* The parsed calendar */
public /** @type {Array} */ $cal;
/* Which keyword has been added to cal at last? */
private /** @type {string} */ $_lastKeyWord;
/**
* Creates the iCal-Object
*
* @param {mixed} $filename The path to the iCal-file or an array of lines from an iCal file
*
* @return Object The iCal-Object
*/
public function __construct($filename, $context=null)
{
if (!$filename) {
return false;
}
if (is_array($filename)){
$lines = $filename;
} else {
$lines = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES, $context);
}
return $this->initLines($lines);
}
/**
* Initializes lines from file
*
* @param {array} $lines The lines to initialize
*
* @return Object The iCal-Object
*/
public function initLines($lines)
{
//stitch together any lines broken over two lines: https://www.ietf.org/rfc/rfc2445.txt (4.1)
$newLines = array();
for($i = 0; $i < count($lines); $i++)
{
if($lines[$i][0] == " ")
{
$newLines[$i - 1] = $newLines[$i - 1] . substr($lines[$i], 1);
}
else
{
$newLines[$i] = $lines[$i];
}
}
$lines = $newLines;
if (stristr($lines[0], 'BEGIN:VCALENDAR') === false) {
return false;
} else {
// TODO: Fix multiline-description problem (see http://tools.ietf.org/html/rfc2445#section-4.8.1.5)
foreach ($lines as $line) {
//ajax_debug($line);
$line = trim($line);
$add = $this->keyValueFromString($line);
if ($add === false) {
$this->addCalendarComponentWithKeyAndValue($type, false, $line);
continue;
}
list($keyword, $value) = $add;
switch ($line) {
// http://www.kanzaki.com/docs/ical/vtodo.html
case "BEGIN:VTODO":
$this->todo_count++;
$type = "VTODO";
break;
// http://www.kanzaki.com/docs/ical/vevent.html
case "BEGIN:VEVENT":
$this->event_count++;
$type = "VEVENT";
break;
// http://www.kanzaki.com/docs/ical/vfreebusy.html
case "BEGIN:VFREEBUSY":
$this->freebusy_count++;
$type = "VFREEBUSY";
break;
//all other special strings
case "BEGIN:VCALENDAR":
case "BEGIN:VALARM":
case "BEGIN:DAYLIGHT":
// http://www.kanzaki.com/docs/ical/vtimezone.html
case "BEGIN:VTIMEZONE":
case "BEGIN:STANDARD":
$type = $value;
break;
case "END:VTODO": // end special text - goto VCALENDAR key
case "END:VEVENT":
case "END:VFREEBUSY":
case "END:VCALENDAR":
case "END:VALARM":
case "END:DAYLIGHT":
case "END:VTIMEZONE":
case "END:STANDARD":
$type = "VCALENDAR";
break;
default:
$this->addCalendarComponentWithKeyAndValue($type, $keyword, $value);
break;
}
}
$this->process_recurrences();
return $this->cal;
}
}
/**
* Add to $this->ical array one value and key.
*
* @param {string} $component This could be VTODO, VEVENT, VCALENDAR, ...
* @param {string} $keyword The keyword, for example DTSTART
* @param {string} $value The value, for example 20110105T090000Z
*
* @return {None}
*/
public function addCalendarComponentWithKeyAndValue($component, $keyword, $value)
{
if (strstr($keyword, ';')) {
// Ignore everything in keyword after a ; (things like Language, etc)
$keyword = substr($keyword, 0, strpos($keyword, ";"));
}
if ($keyword == false) {
$keyword = $this->last_keyword;
switch ($component) {
case 'VEVENT':
$value = $this->cal[$component][$this->event_count - 1][$keyword].$value;
break;
case 'VTODO' :
$value = $this->cal[$component][$this->todo_count - 1][$keyword].$value;
break;
case 'VFREEBUSY' :
$value = $this->cal[$component][$this->freebusy_count - 1][$keyword].$value;
break;
}
}
if (stristr($keyword, "DTSTART") or stristr($keyword, "DTEND") or stristr($keyword, "EXDATE")) {
$keyword = explode(";", $keyword);
$keyword = $keyword[0];
}
switch ($component) {
case "VTODO":
$this->cal[$component][$this->todo_count - 1][$keyword] = $value;
break;
case "VEVENT":
$this->cal[$component][$this->event_count - 1][$keyword] = $value;
if (!isset($this->cal[$component][$this->event_count - 1][$keyword . "_array"])) {
$this->cal[$component][$this->event_count - 1][$keyword . "_array"] = array();
}
$this->cal[$component][$this->event_count - 1][$keyword . "_array"][] = $value;
break;
case "VFREEBUSY":
$this->cal[$component][$this->freebusy_count - 1][$keyword] = $value;
break;
default:
$this->cal[$component][$keyword] = $value;
break;
}
$this->last_keyword = $keyword;
}
/**
* Get a key-value pair of a string.
*
* @param {string} $text which is like "VCALENDAR:Begin" or "LOCATION:"
*
* @return {array} array("VCALENDAR", "Begin")
*/
public function keyValueFromString($text)
{
preg_match("/([^:]+)[:]([\w\W]*)/", $text, $matches);
if (count($matches) == 0) {
return false;
}
$matches = array_splice($matches, 1, 2);
return $matches;
}
/**
* Return Unix timestamp from ical date time format
*
* @param {string} $icalDate A Date in the format YYYYMMDD[T]HHMMSS[Z] or
* YYYYMMDD[T]HHMMSS
*
* @return {int}
*/
public function iCalDateToUnixTimestamp($icalDate)
{
$icalDate = str_replace('T', '', $icalDate);
$icalDate = str_replace('Z', '', $icalDate);
$pattern = '/([0-9]{4})'; // 1: YYYY
$pattern .= '([0-9]{2})'; // 2: MM
$pattern .= '([0-9]{2})'; // 3: DD
$pattern .= '([0-9]{0,2})'; // 4: HH
$pattern .= '([0-9]{0,2})'; // 5: MM
$pattern .= '([0-9]{0,2})/'; // 6: SS
preg_match($pattern, $icalDate, $date);
// Unix timestamp can't represent dates before 1970
if ($date[1] <= 1970) {
return false;
}
// Unix timestamps after 03:14:07 UTC 2038-01-19 might cause an overflow
// if 32 bit integers are used.
$timestamp = mktime((int)$date[4], (int)$date[5], (int)$date[6], (int)$date[2], (int)$date[3], (int)$date[1]);
return $timestamp;
}
/**
* Processes recurrences
*
* @author John Grogg <[email protected]>
* @return {array}
*/
public function process_recurrences()
{
$array = $this->cal;
$events = $array['VEVENT'];
foreach ($array['VEVENT'] as $anEvent) {
if (isset($anEvent['RRULE']) && $anEvent['RRULE'] != '') {
// Recurring event, parse RRULE and add appropriate duplicate events
$rrules = array();
$rrule_strings = explode(';',$anEvent['RRULE']);
foreach ($rrule_strings as $s) {
list($k,$v) = explode('=', $s);
$rrules[$k] = $v;
}
// Get Start timestamp
$start_timestamp = $this->iCalDateToUnixTimestamp($anEvent['DTSTART']);
$end_timestamp = $this->iCalDateToUnixTimestamp($anEvent['DTEND']);
$event_timestmap_offset = $end_timestamp - $start_timestamp;
// Get Interval
$interval = (isset($rrules['INTERVAL']) && $rrules['INTERVAL'] != '') ? $rrules['INTERVAL'] : 1;
// Get Until
if (!isset($rrules['UNTIL'])) {
$rrules['UNTIL'] = date("Ymd", strtotime(" + 4 year")) . 'T235900'; // 20150506T233000
}
$until = $this->iCalDateToUnixTimestamp($rrules['UNTIL']);
// Decide how often to add events and do so
switch ($rrules['FREQ']) {
case 'DAILY':
// Simply add a new event each interval of days until UNTIL is reached
$offset = "+$interval day";
$recurring_timestamp = strtotime($offset, $start_timestamp);
while ($recurring_timestamp <= $until) {
// Add event
$anEvent['DTSTART'] = date('Ymd\THis',$recurring_timestamp);
$anEvent['DTEND'] = date('Ymd\THis',$recurring_timestamp+$event_timestmap_offset);
if ((array_key_exists('DTSTART', $anEvent)) && (array_key_exists('EXDATE_array', $anEvent)) && (!in_array($anEvent['DTSTART'], $anEvent['EXDATE_array']))) {
$events[] = $anEvent;
}
// Move forward
$recurring_timestamp = strtotime($offset,$recurring_timestamp);
}
break;
case 'WEEKLY':
// Create offset
$offset = "+$interval week";
// Build list of days of week to add events
$weekdays = array('SU','MO','TU','WE','TH','FR','SA');
if(isset($rrules['BYDAY']) && $rrules['BYDAY'] != '') {
$bydays = explode(',', $rrules['BYDAY']);
} else {
$weekTemp = array('SU','MO','TU','WE','TH','FR','SA');
$findDay = $weekTemp[date('w',$start_timestamp)];
$bydays = array($findDay);
}
// Get timestamp of first day of start week
$week_recurring_timestamp = (date('w', $start_timestamp) == 0) ? $start_timestamp : strtotime('last Sunday '.date('H:i:s',$start_timestamp), $start_timestamp);
// Step through weeks
while ($week_recurring_timestamp <= $until) {
// Add events for bydays
$day_recurring_timestamp = $week_recurring_timestamp;
foreach ($weekdays as $day) {
// Check if day should be added
if (in_array($day, $bydays) && $day_recurring_timestamp > $start_timestamp && $day_recurring_timestamp <= $until) {
// Add event to day
$anEvent['DTSTART'] = date('Ymd\THis',$day_recurring_timestamp);
$anEvent['DTEND'] = date('Ymd\THis',$day_recurring_timestamp+$event_timestmap_offset);
if ((array_key_exists('DTSTART', $anEvent)) && (array_key_exists('EXDATE_array', $anEvent)) && (!in_array($anEvent['DTSTART'], $anEvent['EXDATE_array']))) {
$events[] = $anEvent;
}
}
// Move forward a day
$day_recurring_timestamp = strtotime('+1 day',$day_recurring_timestamp);
}
// Move forward $interaval weeks
$week_recurring_timestamp = strtotime($offset,$week_recurring_timestamp);
}
break;
case 'MONTHLY':
// Create offset
$offset = "+$interval month";
$recurring_timestamp = strtotime($offset, $start_timestamp);
if (isset($rrules['BYMONTHDAY']) && $rrules['BYMONTHDAY'] != '') {
// Deal with BYMONTHDAY
while ($recurring_timestamp <= $until) {
// Add event
$anEvent['DTSTART'] = date('Ym'.sprintf('%02d',$rrules['BYMONTHDAY']).'\THis',$recurring_timestamp);
$anEvent['DTEND'] = date('Ymd\THis',$this->iCalDateToUnixTimestamp($anEvent['DTSTART'])+$event_timestmap_offset);
if ((array_key_exists('DTSTART', $anEvent)) && (array_key_exists('EXDATE_array', $anEvent)) && (!in_array($anEvent['DTSTART'], $anEvent['EXDATE_array']))) {
$events[] = $anEvent;
}
// Move forward
$recurring_timestamp = strtotime($offset,$recurring_timestamp);
}
} elseif (isset($rrules['BYDAY']) && $rrules['BYDAY'] != '') {
$start_time = date('His',$start_timestamp);
// Deal with BYDAY
$day_number = substr($rrules['BYDAY'], 0, 1);
$week_day = substr($rrules['BYDAY'], 1);
$day_cardinals = array(1 => 'first', 2 => 'second', 3 => 'third', 4 => 'fourth', 5 => 'fifth');
$weekdays = array('SU' => 'sunday','MO' => 'monday','TU' => 'tuesday','WE' => 'wednesday','TH' => 'thursday','FR' => 'friday','SA' => 'saturday');
while ($recurring_timestamp <= $until) {
$event_start_desc = "{$day_cardinals[$day_number]} {$weekdays[$week_day]} of ".date('F',$recurring_timestamp)." ".date('Y',$recurring_timestamp)." ".date('H:i:s',$recurring_timestamp);
$event_start_timestamp = strtotime($event_start_desc);
if ($event_start_timestamp > $start_timestamp && $event_start_timestamp < $until) {
$anEvent['DTSTART'] = date('Ymd\T',$event_start_timestamp).$start_time;
$anEvent['DTEND'] = date('Ymd\THis',$this->iCalDateToUnixTimestamp($anEvent['DTSTART'])+$event_timestmap_offset);
if ((array_key_exists('DTSTART', $anEvent)) && (array_key_exists('EXDATE_array', $anEvent)) && (!in_array($anEvent['DTSTART'], $anEvent['EXDATE_array']))) {
$events[] = $anEvent;
}
}
// Move forward
$recurring_timestamp = strtotime($offset,$recurring_timestamp);
}
}
break;
case 'YEARLY':
// Create offset
$offset = "+$interval year";
$recurring_timestamp = strtotime($offset, $start_timestamp);
$month_names = array(1=>"January", 2=>"Februrary", 3=>"March", 4=>"April", 5=>"May", 6=>"June", 7=>"July", 8=>"August", 9=>"September", 10=>"October", 11=>"November", 12=>"December");
// HACK: Exchange doesn't set a correct UNTIL for yearly events, so just go 2 years out
$until = strtotime('+2 year',$start_timestamp);
// Check if BYDAY rule exists
if (isset($rrules['BYDAY']) && $rrules['BYDAY'] != '') {
$start_time = date('His',$start_timestamp);
// Deal with BYDAY
$day_number = substr($rrules['BYDAY'], 0, 1);
$month_day = substr($rrules['BYDAY'], 1);
$day_cardinals = array(1 => 'first', 2 => 'second', 3 => 'third', 4 => 'fourth', 5 => 'fifth');
$weekdays = array('SU' => 'sunday','MO' => 'monday','TU' => 'tuesday','WE' => 'wednesday','TH' => 'thursday','FR' => 'friday','SA' => 'saturday');
while ($recurring_timestamp <= $until) {
$event_start_desc = "{$day_cardinals[$day_number]} {$weekdays[$month_day]} of {$month_names[$rrules['BYMONTH']]} ".date('Y',$recurring_timestamp)." ".date('H:i:s',$recurring_timestamp);
$event_start_timestamp = strtotime($event_start_desc);
if ($event_start_timestamp > $start_timestamp && $event_start_timestamp < $until) {
$anEvent['DTSTART'] = date('Ymd\T',$event_start_timestamp).$start_time;
$anEvent['DTEND'] = date('Ymd\THis',$this->iCalDateToUnixTimestamp($anEvent['DTSTART'])+$event_timestmap_offset);
if ((array_key_exists('DTSTART', $anEvent)) && (array_key_exists('EXDATE_array', $anEvent)) && (!in_array($anEvent['DTSTART'], $anEvent['EXDATE_array']))) {
$events[] = $anEvent;
}
}
// Move forward
$recurring_timestamp = strtotime($offset,$recurring_timestamp);
}
} else {
$day = date('d',$start_timestamp);
// Step through years adding specific month dates
while ($recurring_timestamp <= $until) {
$event_start_desc = "$day {$month_names[$rrules['BYMONTH']]} ".date('Y',$recurring_timestamp)." ".date('H:i:s',$recurring_timestamp);
$event_start_timestamp = strtotime($event_start_desc);
if ($event_start_timestamp > $start_timestamp && $event_start_timestamp < $until) {
$anEvent['DTSTART'] = date('Ymd\T',$event_start_timestamp).$start_time;
$anEvent['DTEND'] = date('Ymd\THis',$this->iCalDateToUnixTimestamp($anEvent['DTSTART'])+$event_timestmap_offset);
if ((array_key_exists('DTSTART', $anEvent)) && (array_key_exists('EXDATE_array', $anEvent)) && (!in_array($anEvent['DTSTART'], $anEvent['EXDATE_array']))) {
$events[] = $anEvent;
}
}
// Move forward
$recurring_timestamp = strtotime($offset,$recurring_timestamp);
}
}
break;
}
}
}
$this->cal['VEVENT'] = $events;
}
/**
* Returns an array of arrays with all events. Every event is an associative
* array and each property is an element it.
*
* @return {array}
*/
public function events()
{
$array = $this->cal;
return $array['VEVENT'];
}
/**
* Returns a boolean value whether thr current calendar has events or not
*
* @return {boolean}
*/
public function hasEvents()
{
return ( count($this->events()) > 0 ? true : false );
}
/**
* Returns false when the current calendar has no events in range, else the
* events.
*
* Note that this function makes use of a UNIX timestamp. This might be a
* problem on January the 29th, 2038.
* See http://en.wikipedia.org/wiki/Unix_time#Representing_the_number
*
* @param {boolean} $rangeStart Either true or false
* @param {boolean} $rangeEnd Either true or false
*
* @return {mixed}
*/
public function eventsFromRange($rangeStart = false, $rangeEnd = false)
{
$events = $this->sortEventsWithOrder($this->events(), SORT_ASC);
if (!$events) {
return false;
}
$extendedEvents = array();
if ($rangeStart === false) {
$rangeStart = new DateTime();
} else {
$rangeStart = new DateTime($rangeStart);
}
if ($rangeEnd === false or $rangeEnd <= 0) {
$rangeEnd = new DateTime('2038/01/18');
} else {
$rangeEnd = new DateTime($rangeEnd);
}
$rangeStart = $rangeStart->format('U');
$rangeEnd = $rangeEnd->format('U');
// loop through all events by adding two new elements
foreach ($events as $anEvent) {
$timestamp = $this->iCalDateToUnixTimestamp($anEvent['DTSTART']);
if ($timestamp >= $rangeStart && $timestamp <= $rangeEnd) {
$extendedEvents[] = $anEvent;
}
}
return $extendedEvents;
}
/**
* Returns a boolean value whether thr current calendar has events or not
*
* @param {array} $events An array with events.
* @param {array} $sortOrder Either SORT_ASC, SORT_DESC, SORT_REGULAR,
* SORT_NUMERIC, SORT_STRING
*
* @return {boolean}
*/
public function sortEventsWithOrder($events, $sortOrder = SORT_ASC)
{
$extendedEvents = array();
// loop through all events by adding two new elements
foreach ($events as $anEvent) {
if (!array_key_exists('UNIX_TIMESTAMP', $anEvent)) {
$anEvent['UNIX_TIMESTAMP'] = $this->iCalDateToUnixTimestamp($anEvent['DTSTART']);
}
if (!array_key_exists('REAL_DATETIME', $anEvent)) {
$anEvent['REAL_DATETIME'] = date("d.m.Y", $anEvent['UNIX_TIMESTAMP']);
}
$extendedEvents[] = $anEvent;
}
foreach ($extendedEvents as $key => $value) {
$timestamp[$key] = $value['UNIX_TIMESTAMP'];
}
array_multisort($timestamp, $sortOrder, $extendedEvents);
return $extendedEvents;
}
}
?>