-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathCalFileParser.php
More file actions
371 lines (302 loc) · 11.8 KB
/
CalFileParser.php
File metadata and controls
371 lines (302 loc) · 11.8 KB
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
<?php
/*
* CalFileParser
*
* Parser for iCal and vCal files. Reads event information and
* outputs data into an Array or JSON
*
* @author Michael Mottola <mikemottola@gmail.com>
* @license MIT
* @version 1.0
*
*/
class CalFileParser {
private $_base_path = './';
private $_file_name = '';
private $_output = 'array';
private $DTfields = array('DTSTART', 'DTEND', 'DTSTAMP', 'CREATED', 'EXDATE', 'LAST-MODIFIED');
private $_user_timezone = null;
private $_file_timezone = null;
function __construct() {
$this->_default_output = $this->_output;
}
public function set_base_path($path) {
if (isset($path)) {
$this->_base_path = $path;
}
}
public function set_file_name($filename) {
if (!empty($filename)) {
$this->_file_name = $filename;
}
}
public function set_output($output) {
if (!empty($output)) {
$this->_output = $output;
}
}
public function set_timezone($timezone) {
if (!empty($timezone)) {
$this->_user_timezone = $timezone;
}
}
public function get_base_path() {
return $this->_base_path;
}
public function get_file_name() {
return $this->_file_name;
}
public function get_output() {
return $this->_output;
}
/**
* Read File
*
* @param string $file
* @return string
*
* @example
* read_file('schedule.vcal')
* read_file('../2011-08/'schedule.vcal');
* read_file('http://michaelencode.com/example.vcal');
*/
public function read_file($file = '') {
if (empty($file)) {
$file = $this->_file_name;
}
// check to see if file path is a url
if (preg_match('/^(http|https):/', $file) === 1) {
return $this->read_remote_file($file);
}
//empty base path if file starts with forward-slash
if (substr($file, 0, 1) === '/') {
$this->set_base_path('');
}
if (!empty($file) && file_exists($this->_base_path . $file)) {
$file_contents = file_get_contents($this->_base_path . $file);
return $file_contents;
} else {
return false;
}
}
/**
* Read Remote File
* @param $file
* @return bool|string
*/
public function read_remote_file($file) {
if (!empty($file)) {
$data = file_get_contents($file);
if ($data !== false) {
return $data;
}
}
return false;
}
/**
* Parse
* Parses iCal or vCal file and returns data of a type that is specified
* @param string $file
* @param string $output
* @return mixed|string
*/
public function parse($file = '', $output = '') {
$file_contents = $this->read_file($file);
if ($file_contents === false) {
return 'Error: File Could not be read';
}
if (empty($output)) {
$output = $this->_output;
}
if (empty($output)) {
$output = $this->_default_output;
}
$events_arr = array();
// fetch timezone to create datetime object
if (preg_match('/X-WR-TIMEZONE:(.+)/i', $file_contents, $timezone) === 1) {
$this->_file_timezone = trim($timezone[1]);
if ($this->_user_timezone == null) {
$this->_user_timezone = $this->_file_timezone;
}
} else {
$this->_file_timezone = $this->_user_timezone;
}
// tell user if setting timezone is necessary
if ($this->_user_timezone == null) {
return 'Error: no timezone set or found';
}
//put contains between start and end of VEVENT into array called $events
preg_match_all('/(BEGIN:VEVENT.*?END:VEVENT)/si', $file_contents, $events);
if (!empty($events)) {
foreach ($events[0] as $event_str) {
//remove begin and end "tags"
$event_str = trim(str_replace(array('BEGIN:VEVENT','END:VEVENT'),'',$event_str));
//convert string of entire event into an array with elements containing string of 'key:value'
$event_key_pairs = $this->convert_event_string_to_array($event_str);
//convert array of 'key:value' strings to an array of key => values
$events_arr[] = $this->convert_key_value_strings($event_key_pairs);
}
}
$this->_output = $this->_default_output;
return $this->output($events_arr, $output);
}
/**
* Output
* outputs data in the format specified
*
* @param $events_arr
* @param string $output
* @return mixed
*/
private function output($events_arr, $output = 'array') {
switch ($output) {
case 'json' :
return json_encode($events_arr);
break;
default :
return $events_arr;
break;
}
}
/**
* Convert event string to array
* accepts a string of calendar event data and produces array of 'key:value' strings
* See convert_key_value_strings() to convert strings to
* @param string $event_str
* @return array
*/
private function convert_event_string_to_array($event_str = '') {
if (!empty($event_str)) {
//replace new lines with a custom delimiter
$event_str = preg_replace("/[\r\n]/", "%%" ,$event_str);
// take care of line wrapping
$event_str = preg_replace("/%%%% /", "" ,$event_str);
if (strpos(substr($event_str, 2), '%%') == '0') { //if this code is executed, then file consisted of one line causing previous tactic to fail
$tmp_piece = explode(':',$event_str);
$num_pieces = count($tmp_piece);
$event_str = '';
foreach ($tmp_piece as $key => $item_str) {
if ($key != ($num_pieces -1) ) {
//split at spaces
$tmp_pieces = preg_split('/\s/',$item_str);
//get the last whole word in the string [item]
$last_word = end($tmp_pieces);
//adds delimiter to front and back of item string, and also between each new key
$item_str = trim(str_replace(array($last_word,' %%' . $last_word),array('%%' . $last_word . ':', '%%' . $last_word), $item_str));
}
//build the event string back together, piece by piece
$event_str .= trim($item_str);
}
}
//perform some house cleaning just in case
$event_str = str_replace('%%%%','%%', $event_str);
if (substr($event_str, 0, 2) == '%%') {
$event_str = substr($event_str, 2);
}
//break string into array elements at custom delimiter
$return = explode('%%',$event_str);
} else {
$return = array();
}
return $return;
}
/**
* Parse Key Value String
* accepts an array of strings in the format of 'key:value' and returns an array of keys and values
* @param array $event_key_pairs
* @return array
*/
private function convert_key_value_strings($event_key_pairs = array()) {
$event = array();
$event_alarm = array();
$event_alarms = array();
$inside_alarm = false;
if (!empty($event_key_pairs)) {
foreach ($event_key_pairs as $line) {
if (empty($line)) continue;
$line_data = explode(':', $line, 2);
$key = trim((isset($line_data[0])) ? $line_data[0] : "");
$value = trim((isset($line_data[1])) ? $line_data[1] : "");
// we are parsing an alarm for this event
if ($key == "BEGIN" && $value == "VALARM") {
$inside_alarm = true;
$event_alarm = array();
continue;
}
// we finished parsing an alarm for this event
if ($key == "END" && $value == "VALARM") {
$inside_alarm = false;
$event_alarms[] = $event_alarm;
continue;
}
// autoconvert datetime fields to DateTime object
$date_key = (strstr($key,";")) ? strstr($key,";", true) : $key;
$date_format = (strstr($key,";")) ? strstr($key,";") : ";VALUE=DATE-TIME";
if (in_array($date_key, $this->DTfields)) {
// set date key without format
$key = $date_key;
$timezone = $this->_file_timezone;
// found time zone in date format info
if (strstr($date_format,"TZID")) {
$strstr = strstr($date_format,"TZID");
$timezone = substr($strstr, 5);
}
// process all dates if there are more then one and comma seperated
$processed_value = array();
foreach(explode(",", $value) AS $date_value) {
// this is simply a date
if ($date_format == ";VALUE=DATE") $date_value .= "T000000";
// date-time in UTC
if (substr($date_value, -1) == "Z") $timezone = "UTC";
// format date
$date = DateTime::createFromFormat('Ymd\THis', str_replace('Z', '', $date_value), new DateTimeZone($timezone));
if ($date !== false) $date->setTimezone(new DateTimeZone($this->_user_timezone));
if ($date !== false) $processed_value[] = $date;
}
// we have more then one date value then return it as an array
if (count($processed_value) > 1) {
$value = $processed_value;
} else {
if ($date !== false) $value = $date;
}
}
// check if current key was already set
// if this is the case then add value data and turn it into an array
$value_current_key = false;
if ($inside_alarm) {
if (isset($event_alarm[$key])) $value_current_key = $event_alarm[$key];
} else {
if (isset($event[$key])) $value_current_key = $event[$key];
}
// this current key already has data add more
if ($value_current_key !== false) {
// check if data is array and merge
if (is_array($value_current_key)) {
if (is_array($value)) {
$value = array_merge($value_current_key, $value);
} else {
$value = array_merge($value_current_key, array($value));
}
} else {
if (is_array($value)) {
$value = array_merge(array($value_current_key), $value);
} else {
$value = array($value_current_key, $value);
}
}
}
if ($inside_alarm) {
$event_alarm[$key] = $value;
} else {
$event[$key] = $value;
}
}
}
// add alarm data
$event["VALARM"] = $event_alarms;
// unescape every element if string.
return array_map(function($value) {
return (is_string($value) ? stripcslashes($value) : $value);
}, $event);
}
}