-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathiCalEasyReader.php
273 lines (238 loc) · 6.79 KB
/
iCalEasyReader.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
<?php
/**
* iCalEasyReader is an easy to understood class, loads a "ics" format string and returns an array with the traditional iCal fields
*
* @category Parser
* @author Matias Perrone <matias.perrone at gmail dot com>
* @author Timo Henke <phpstuff@thenke.de> (Some ideas taken partially from iCalParse on http://www.phpclasses.org/)
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* @version 1.4.1
* @param string $data ics file string content
* @param array|false $data $makeItEasy the idea is to convert this "keys" into the "values", converting the DATE and DATE-TIME values to the respective DateTime type of PHP, also all the keys are lowercased
* @return array|false
*/
class iCalEasyReader
{
private $ical = null;
private $_lastitem = null;
public function &load($data)
{
$this->ical = false;
$regex_opt = 'mib';
// Lines in the string
$lines = mb_split( '[\r\n]+', $data );
// Delete empty ones
$last = count( $lines );
for($i = 0; $i < $last; $i ++)
{
if ( trim( $lines[$i] ) == "" )
unset( $lines[$i] );
}
$lines = array_values( $lines );
// First and last items
$first = 0;
$last = count( $lines ) - 1;
if (! ( mb_ereg_match( '^BEGIN:VCALENDAR', $lines[$first], $regex_opt ) and mb_ereg_match( '^END:VCALENDAR', $lines[$last], $regex_opt ) ))
{
$first = null;
$last = null;
foreach ( $lines as $i => &$line )
{
if (mb_ereg_match( '^BEGIN:VCALENDAR', $line, $regex_opt ))
$first = $i;
if (mb_ereg_match( '^END:VCALENDAR', $line, $regex_opt ))
{
$last = $i;
break;
}
}
}
// Procesing
if (! is_null( $first ) and ! is_null( $last ))
{
$lines = array_slice( $lines, $first + 1, ( $last - $first - 1 ), true );
$group = null;
$parentgroup = null;
$this->ical = [];
$addTo = [];
$addToElement = null;
reset($lines);
$line = true;
while ( true )
{
$line = each($lines);
if ($line === false)
break;
$line = current($lines);
if ( substr( $line, 0, 2) === 'X-' Or trim($line) == '')
continue;
$clave = null;
$pattern = '^(BEGIN|END)\:(.+)$'; // (VALARM|VTODO|VJOURNAL|VEVENT|VFREEBUSY|VCALENDAR|DAYLIGHT|VTIMEZONE|STANDARD)
mb_ereg_search_init( $line );
$regs = mb_ereg_search_regs( $pattern, $regex_opt );
if ($regs)
{
// $regs
// 0 => BEGIN:VEVENT
// 1 => BEGIN
// 2 => VEVENT
switch ( $regs[1] )
{
case 'BEGIN' :
if (! is_null( $group ))
$parentgroup = $group;
$group = trim( $regs[2] );
// Adding new values to groups
if (is_null( $parentgroup ))
{
if (! array_key_exists( $group, $this->ical ))
$this->ical[$group] = [null];
else
$this->ical[$group][] = null;
}
else
{
if (! array_key_exists( $parentgroup, $this->ical ))
$this->ical[$parentgroup] = [$group => [null]];
if (! array_key_exists( $group, $this->ical[$parentgroup] ))
$this->ical[$parentgroup][$group] = [null];
else
$this->ical[$parentgroup][$group][] = null;
}
break;
case 'END' :
if (is_null( $group ))
$parentgroup = null;
$group = null;
break;
}
continue;
}
// There are cases like "ATTENDEE" that may take several lines.
if ( !in_array( $line[0], [" ", "\t"] ) And strpos( ':', $line ) === false )
{
$r = current($lines);
$concatenar = next($lines);
while ( $concatenar And in_array( $concatenar[0], [" ", "\t"] ) )
{
$r .= substr($concatenar, 1);
$concatenar = next($lines);
}
prev($lines);
if ($r !== $line )
$line = $r;
}
if (! in_array( $line[0], [" ", "\t"] ))
$this->addItem( $line, $group, $parentgroup );
else
$this->concatItem( $line );
}
}
return $this->ical;
}
public function addType(&$value, $item)
{
$type = explode( '=', $item );
if (count( $type ) > 1 and $type[0] == 'VALUE')
$value['type'] = $type[1];
else
$value[$type[0]] = $type[1];
return $value;
}
public function addItem($line, $group, $parentgroup)
{
$line = $this->transformLine( $line );
$item = explode( ':', $line, 2 );
if (!array_key_exists( 1, $item ))
{
trigger_error ("Unexpected Line error. Possible Corruption. Line ".strlen( $line ).":".PHP_EOL.$line.PHP_EOL, E_USER_NOTICE);
return;
}
// If $group is null is an independent value
if (is_null( $group ))
{
$this->ical[$item[0]] = ( count( $item ) > 1 ? $item[1] : null );
$this->_lastitem = &$this->ical[$item[0]];
}
// If $group is set then is an item of a group
else
{
$subitem = explode( ';', $item[0], 2 );
if (count( $subitem ) == 1)
$value = ( count( $item ) > 1 ? $item[1] : null );
else
{
$value = ['value' => $item[1]];
if (strpos($subitem[1], ";") !== false)
$value += $this->processMultivalue($subitem[1]);
else
$this->addType( $value, $subitem[1] );
}
// Multi value
if (is_string( $value ))
$this->processMultivalue($value);
if (is_null( $parentgroup ))
{
$this->ical[$group][count( $this->ical[$group] ) - 1][$subitem[0]] = $value;
$this->_lastitem = &$this->ical[$group][count( $this->ical[$group] ) - 1][$subitem[0]];
}
else
{
$this->ical[$parentgroup][$group][count( $this->ical[$parentgroup][$group] ) - 1][$subitem[0]] = $value;
$this->_lastitem = &$this->ical[$parentgroup][$group][count( $this->ical[$parentgroup][$group] ) - 1][$subitem[0]];
}
}
}
public function processMultivalue(&$value)
{
$z = explode( ';', $value );
if (count( $z ) > 1)
{
$value = [];
foreach ( $z as &$v )
{
$t = explode( '=', $v );
$value[$t[0]] = $t[count( $t ) - 1];
}
}
unset( $z );
return $value;
}
public function concatItem($line)
{
$line = mb_substr( $line, 1 );
if (is_array( $this->_lastitem ))
{
$line = $this->transformLine( $this->_lastitem['value'] . $line );
$this->_lastitem['value'] = $line;
}
else
{
$line = $this->transformLine( $this->_lastitem . $line );
$this->_lastitem = $line;
}
}
public function transformLine($line)
{
$patterns = ['\\\\[n]', '\\\\[t]', '\\\\,', '\\\\;'];
$replacements = ["\n", "\t", ",", ";"];
return $this->mb_eregi_replace_all( $patterns, $replacements, $line );
}
public function mb_eregi_replace_all($pattern, $replacement, $string)
{
if (is_array( $pattern ) and is_array( $replacement ))
{
foreach ( $pattern as $i => $patron )
{
if (array_key_exists( $i, $replacement ))
$reemplazo = $replacement[$i];
else
$reemplazo = '';
$string = mb_eregi_replace( $patron, $reemplazo, $string );
}
}
elseif (is_string( $pattern ) and is_string( $replacement ))
$string = mb_eregi_replace( $pattern, $replacement, $string );
return $string;
}
}