forked from YetiForceCompany/iCalendar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iCalendar_rfc2445.php
750 lines (646 loc) · 18.4 KB
/
iCalendar_rfc2445.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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
<?php
// $Id: iCalendar_rfc2445.php,v 1.7 2005/07/21 23:23:48 defacer Exp $
/*
All names of properties, property parameters, enumerated property
values and property parameter values are case-insensitive. However,
all other property values are case-sensitive, unless otherwise
stated.
*/
define('RFC2445_CRLF', "\r\n");
define('RFC2445_WSP', "\t ");
define('RFC2445_WEEKDAYS', 'MO,TU,WE,TH,FR,SA,SU');
define('RFC2445_FOLDED_LINE_LENGTH', 75);
define('RFC2445_REQUIRED', 0x01);
define('RFC2445_OPTIONAL', 0x02);
define('RFC2445_ONCE', 0x04);
define('RFC2445_PROP_FLAGS', 0);
define('RFC2445_PROP_TYPE', 1);
define('RFC2445_PROP_DEFAULT', 2);
define('RFC2445_XNAME', 'X-');
define('RFC2445_TYPE_BINARY', 0);
define('RFC2445_TYPE_BOOLEAN', 1);
define('RFC2445_TYPE_CAL_ADDRESS', 2);
define('RFC2445_TYPE_DATE', 3);
define('RFC2445_TYPE_DATE_TIME', 4);
define('RFC2445_TYPE_DURATION', 5);
define('RFC2445_TYPE_FLOAT', 6);
define('RFC2445_TYPE_INTEGER', 7);
define('RFC2445_TYPE_PERIOD', 8);
define('RFC2445_TYPE_RECUR', 9);
define('RFC2445_TYPE_TEXT', 10);
define('RFC2445_TYPE_TIME', 11);
define('RFC2445_TYPE_URI', 12); // CAL_ADDRESS === URI
define('RFC2445_TYPE_UTC_OFFSET', 13);
class ICalendarRfc
{
public static function rfc2445Fold($string)
{
if (strlen($string) <= RFC2445_FOLDED_LINE_LENGTH) {
return $string;
}
$retval = '';
while (strlen($string) > RFC2445_FOLDED_LINE_LENGTH) {
$retval .= substr($string, 0, RFC2445_FOLDED_LINE_LENGTH - 1) . RFC2445_CRLF . ' ';
$string = substr($string, RFC2445_FOLDED_LINE_LENGTH - 1);
}
$retval .= $string;
return $retval;
}
public static function rfc2445IsXname($name)
{
// If it's less than 3 chars, it cannot be legal
if (strlen($name) < 3) {
return false;
}
// If it contains an illegal char anywhere, reject it
if (strspn($name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-') != strlen($name)) {
return false;
}
// To be legal, it must still start with "X-"
return substr($name, 0, 2) === 'X-';
}
public static function rfc2445IsValidValue($value, $type)
{
// This branch should only be taken with xname values
if ($type === null) {
return true;
}
switch ($type) {
case RFC2445_TYPE_CAL_ADDRESS:
case RFC2445_TYPE_URI:
if (!is_string($value)) {
return false;
}
$valid_schemes = ['ftp', 'http', 'ldap', 'gopher', 'mailto', 'news', 'nntp', 'telnet', 'wais', 'file', 'prospero'];
$pos = strpos($value, ':');
if (!$pos) {
return false;
}
$scheme = strtolower(substr($value, 0, $pos));
$remain = substr($value, $pos + 1);
if (!in_array($scheme, $valid_schemes)) {
return false;
}
if ($scheme === 'mailto') {
$regexp = '/^[a-zA-Z0-9]+[_a-zA-Z0-9\-]*(\.[_a-z0-9\-]+)*@(([0-9a-zA-Z\-]+\.)+[a-zA-Z][0-9a-zA-Z\-]+|([0-9]{1,3}\.){3}[0-9]{1,3})$/';
} else {
$regexp = '/^//(.+(:.*)?@)?(([0-9a-zA-Z\-]+\.)+[a-zA-Z][0-9a-zA-Z\-]+|([0-9]{1,3}\.){3}[0-9]{1,3})(:[0-9]{1,5})?(/.*)?$/';
}
return preg_match($regexp, $remain);
break;
case RFC2445_TYPE_BINARY:
if (!is_string($value)) {
return false;
}
$len = strlen($value);
if ($len % 4 != 0) {
return false;
}
for ($i = 0; $i < $len; ++$i) {
$ch = $value[$i];
if (!($ch >= 'a' && $ch <= 'z' || $ch >= 'A' && $ch <= 'Z' || $ch >= '0' && $ch <= '9' || $ch == '-' || $ch == '+')) {
if ($ch == '=' && $len - $i <= 2) {
continue;
}
return false;
}
}
return true;
break;
case RFC2445_TYPE_BOOLEAN:
if (is_bool($value)) {
return true;
}
if (is_string($value)) {
$value = strtoupper($value);
return $value == 'true' || $value == 'false';
}
return false;
break;
case RFC2445_TYPE_DATE:
if (is_int($value)) {
if ($value < 0) {
return false;
}
$value = "$value";
} elseif (!is_string($value)) {
return false;
}
if (strlen($value) != 8) {
return false;
}
$y = (int) (substr($value, 0, 4));
$m = (int) (substr($value, 4, 2));
$d = (int) (substr($value, 6, 2));
return checkdate($m, $d, $y);
break;
case RFC2445_TYPE_DATE_TIME:
if (!is_string($value) || strlen($value) < 15) {
return false;
}
return $value[8] == 'T' &&
static::rfc2445IsValidValue(substr($value, 0, 8), RFC2445_TYPE_DATE) &&
static::rfc2445IsValidValue(substr($value, 9), RFC2445_TYPE_TIME);
break;
case RFC2445_TYPE_DURATION:
if (!is_string($value)) {
return false;
}
$len = strlen($value);
if ($len < 3) {
// Minimum conformant length: "P1W"
return false;
}
if ($value[0] == '+' || $value[0] == '-') {
$value = substr($value, 1);
--$len; // Don't forget to update this!
}
if ($value[0] != 'P') {
return false;
}
// OK, now break it up
$num = '';
$allowed = 'WDT';
for ($i = 1; $i < $len; ++$i) {
$ch = $value[$i];
if ($ch >= '0' && $ch <= '9') {
$num .= $ch;
continue;
}
if (strpos($allowed, $ch) === false) {
// Non-numeric character which shouldn't be here
return false;
}
if ($num === '' && $ch != 'T') {
// Allowed non-numeric character, but no digits came before it
return false;
}
// OK, $ch now holds a character which tells us what $num is
switch ($ch) {
case 'W':
// If duration in weeks is specified, this must end the string
return $i == $len - 1;
break;
case 'D':
// Days specified, now if anything comes after it must be a 'T'
$allowed = 'T';
break;
case 'T':
// Starting to specify time, H M S are now valid delimiters
$allowed = 'HMS';
break;
case 'H':
$allowed = 'M';
break;
case 'M':
$allowed = 'S';
break;
case 'S':
return $i == $len - 1;
break;
}
// If we 're going to continue, reset $num
$num = '';
}
// $num is kept for this reason: if we 're here, we ran out of chars
// therefore $num must be empty for the period to be legal
return $num === '' && $ch != 'T';
break;
case RFC2445_TYPE_FLOAT:
if (is_float($value)) {
return true;
}
if (!is_string($value) || $value === '') {
return false;
}
$dot = false;
$int = false;
$len = strlen($value);
for ($i = 0; $i < $len; ++$i) {
switch ($value[$i]) {
case '-': case '+':
// A sign can only be seen at position 0 and cannot be the only char
if ($i != 0 || $len == 1) {
return false;
}
break;
case '.':
// A second dot is an error
// Make sure we had at least one int before the dot
if ($dot || !$int) {
return false;
}
$dot = true;
// Make also sure that the float doesn't end with a dot
if ($i == $len - 1) {
return false;
}
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
$int = true;
break;
default:
// Any other char is a no-no
return false;
break;
}
}
return true;
break;
case RFC2445_TYPE_INTEGER:
if (is_int($value)) {
return true;
}
if (!is_string($value) || $value === '') {
return false;
}
if ($value[0] == '+' || $value[0] == '-') {
if (strlen($value) == 1) {
return false;
}
$value = substr($value, 1);
}
if (strspn($value, '0123456789') != strlen($value)) {
return false;
}
return $value >= -2147483648 && $value <= 2147483647;
break;
case RFC2445_TYPE_PERIOD:
if (!is_string($value) || empty($value)) {
return false;
}
$parts = explode('/', $value);
if (count($parts) != 2) {
return false;
}
if (!static::rfc2445IsValidValue($parts[0], RFC2445_TYPE_DATE_TIME)) {
return false;
}
// Two legal cases for the second part:
if (static::rfc2445IsValidValue($parts[1], RFC2445_TYPE_DATE_TIME)) {
// It has to be after the start time, so
return $parts[1] > $parts[0];
} elseif (static::rfc2445IsValidValue($parts[1], RFC2445_TYPE_DURATION)) {
// The period MUST NOT be negative
return $parts[1][0]
!= '-';
}
// It seems to be illegal
return false;
break;
case RFC2445_TYPE_RECUR:
if (!is_string($value)) {
return false;
}
$parts = explode(';', strtoupper($value));
// First of all, we need at least a FREQ and a UNTIL or COUNT part, so...
if (count($parts) < 2) {
return false;
}
// Let's get that into a more easily comprehensible format
$vars = [];
foreach ($parts as $part) {
$pieces = explode('=', $part);
// There must be exactly 2 pieces, e.g. FREQ=WEEKLY
if (count($pieces) != 2) {
return false;
}
// It's illegal for a variable to appear twice
if (isset($vars[$pieces[0]])) {
return false;
}
// Sounds good
$vars[$pieces[0]] = $pieces[1];
}
// OK... now to test everything else
// FREQ must be the first thing appearing
reset($vars);
if (key($vars) != 'FREQ') {
return false;
}
// It's illegal to have both UNTIL and COUNT appear
if (isset($vars['UNTIL'], $vars['COUNT'])) {
return false;
}
// Special case: BYWEEKNO is only valid for FREQ=YEARLY
if (isset($vars['BYWEEKNO']) && $vars['FREQ'] != 'YEARLY') {
return false;
}
// Special case: BYSETPOS is only valid if another BY option is specified
if (isset($vars['BYSETPOS'])) {
$options = ['BYSECOND', 'BYMINUTE', 'BYHOUR', 'BYDAY', 'BYMONTHDAY', 'BYYEARDAY', 'BYWEEKNO', 'BYMONTH'];
$defined = array_keys($vars);
$common = array_intersect($options, $defined);
if (empty($common)) {
return false;
}
}
// OK, now simply check if each element has a valid value,
// unsetting them on the way. If at the end the array still
// has some elements, they are illegal.
if ($vars['FREQ'] != 'SECONDLY' && $vars['FREQ'] != 'MINUTELY' && $vars['FREQ'] != 'HOURLY' &&
$vars['FREQ'] != 'DAILY' && $vars['FREQ'] != 'WEEKLY' &&
$vars['FREQ'] != 'MONTHLY' && $vars['FREQ'] != 'YEARLY') {
return false;
}
unset($vars['FREQ']);
// Set this, we may need it later
$weekdays = explode(',', RFC2445_WEEKDAYS);
if (isset($vars['UNTIL'])) {
if (static::rfc2445IsValidValue($vars['UNTIL'], RFC2445_TYPE_DATE_TIME)) {
// The time MUST be in UTC format
if (!(substr($vars['UNTIL'], -1) == 'Z')) {
return false;
}
} elseif (!static::rfc2445IsValidValue($vars['UNTIL'], RFC2445_TYPE_DATE_TIME)) {
return false;
}
}
unset($vars['UNTIL']);
if (isset($vars['COUNT'])) {
if (empty($vars['COUNT'])) {
// This also catches the string '0', which makes no sense
return false;
}
if (strspn($vars['COUNT'], '0123456789') != strlen($vars['COUNT'])) {
return false;
}
}
unset($vars['COUNT']);
if (isset($vars['INTERVAL'])) {
if (empty($vars['INTERVAL'])) {
// This also catches the string '0', which makes no sense
return false;
}
if (strspn($vars['INTERVAL'], '0123456789') != strlen($vars['INTERVAL'])) {
return false;
}
}
unset($vars['INTERVAL']);
if (isset($vars['BYSECOND'])) {
if ($vars['BYSECOND'] == '') {
return false;
}
// Comma also allowed
if (strspn($vars['BYSECOND'], '0123456789,') != strlen($vars['BYSECOND'])) {
return false;
}
$secs = explode(',', $vars['BYSECOND']);
foreach ($secs as $sec) {
if ($sec == '' || $sec < 0 || $sec > 59) {
return false;
}
}
}
unset($vars['BYSECOND']);
if (isset($vars['BYMINUTE'])) {
if ($vars['BYMINUTE'] == '') {
return false;
}
// Comma also allowed
if (strspn($vars['BYMINUTE'], '0123456789,') != strlen($vars['BYMINUTE'])) {
return false;
}
$mins = explode(',', $vars['BYMINUTE']);
foreach ($mins as $min) {
if ($min == '' || $min < 0 || $min > 59) {
return false;
}
}
}
unset($vars['BYMINUTE']);
if (isset($vars['BYHOUR'])) {
if ($vars['BYHOUR'] == '') {
return false;
}
// Comma also allowed
if (strspn($vars['BYHOUR'], '0123456789,') != strlen($vars['BYHOUR'])) {
return false;
}
$hours = explode(',', $vars['BYHOUR']);
foreach ($hours as $hour) {
if ($hour == '' || $hour < 0 || $hour > 23) {
return false;
}
}
}
unset($vars['BYHOUR']);
if (isset($vars['BYDAY'])) {
if (empty($vars['BYDAY'])) {
return false;
}
// First off, split up all values we may have
$days = explode(',', $vars['BYDAY']);
foreach ($days as $day) {
$daypart = substr($day, -2);
if (!in_array($daypart, $weekdays)) {
return false;
}
if (strlen($day) > 2) {
$intpart = substr($day, 0, strlen($day) - 2);
if (!static::rfc2445IsValidValue($intpart, RFC2445_TYPE_INTEGER)) {
return false;
}
if ((int) $intpart == 0) {
return false;
}
}
}
}
unset($vars['BYDAY']);
if (isset($vars['BYMONTHDAY'])) {
if (empty($vars['BYMONTHDAY'])) {
return false;
}
$mdays = explode(',', $vars['BYMONTHDAY']);
foreach ($mdays as $mday) {
if (!static::rfc2445IsValidValue($mday, RFC2445_TYPE_INTEGER)) {
return false;
}
$mday = abs((int) $mday);
if ($mday == 0 || $mday > 31) {
return false;
}
}
}
unset($vars['BYMONTHDAY']);
if (isset($vars['BYYEARDAY'])) {
if (empty($vars['BYYEARDAY'])) {
return false;
}
$ydays = explode(',', $vars['BYYEARDAY']);
foreach ($ydays as $yday) {
if (!static::rfc2445IsValidValue($yday, RFC2445_TYPE_INTEGER)) {
return false;
}
$yday = abs((int) $yday);
if ($yday == 0 || $yday > 366) {
return false;
}
}
}
unset($vars['BYYEARDAY']);
if (isset($vars['BYWEEKNO'])) {
if (empty($vars['BYWEEKNO'])) {
return false;
}
$weeknos = explode(',', $vars['BYWEEKNO']);
foreach ($weeknos as $weekno) {
if (!static::rfc2445IsValidValue($weekno, RFC2445_TYPE_INTEGER)) {
return false;
}
$weekno = abs((int) $weekno);
if ($weekno == 0 || $weekno > 53) {
return false;
}
}
}
unset($vars['BYWEEKNO']);
if (isset($vars['BYMONTH'])) {
if (empty($vars['BYMONTH'])) {
return false;
}
// Comma also allowed
if (strspn($vars['BYMONTH'], '0123456789,') != strlen($vars['BYMONTH'])) {
return false;
}
$months = explode(',', $vars['BYMONTH']);
foreach ($months as $month) {
if ($month == '' || $month < 1 || $month > 12) {
return false;
}
}
}
unset($vars['BYMONTH']);
if (isset($vars['BYSETPOS'])) {
if (empty($vars['BYSETPOS'])) {
return false;
}
$sets = explode(',', $vars['BYSETPOS']);
foreach ($sets as $set) {
if (!static::rfc2445IsValidValue($set, RFC2445_TYPE_INTEGER)) {
return false;
}
$set = abs((int) $set);
if ($set == 0 || $set > 366) {
return false;
}
}
}
unset($vars['BYSETPOS']);
if (isset($vars['WKST'])) {
if (!in_array($vars['WKST'], $weekdays)) {
return false;
}
}
unset($vars['WKST']);
// Any remaining vars must be x-names
if (empty($vars)) {
return true;
}
foreach ($vars as $name => $var) {
if (!static::rfc2445IsXname($name)) {
return false;
}
}
// At last, all is OK!
return true;
break;
case RFC2445_TYPE_TEXT:
return true;
break;
case RFC2445_TYPE_TIME:
if (is_int($value)) {
if ($value < 0) {
return false;
}
$value = "$value";
} elseif (!is_string($value)) {
return false;
}
if (strlen($value) == 7) {
if (strtoupper(substr($value, -1)) != 'Z') {
return false;
}
$value = substr($value, 0, 6);
}
if (strlen($value) != 6) {
return false;
}
$h = (int) (substr($value, 0, 2));
$m = (int) (substr($value, 2, 2));
$s = (int) (substr($value, 4, 2));
return $h <= 23 && $m <= 59 && $s <= 60;
break;
case RFC2445_TYPE_UTC_OFFSET:
if (is_int($value)) {
if ($value >= 0) {
$value = "+$value";
} else {
$value = "$value";
}
} elseif (!is_string($value)) {
return false;
}
if (strlen($value) == 7) {
$s = (int) (substr($value, 5, 2));
$value = substr($value, 0, 5);
}
if (strlen($value) != 5 || $value == '-0000') {
return false;
}
if ($value[0] != '+' && $value[0] != '-') {
return false;
}
$h = (int) (substr($value, 1, 2));
$m = (int) (substr($value, 3, 2));
return $h <= 23 && $m <= 59 && $s <= 59;
break;
}
trigger_error('bad code path', E_USER_WARNING);
return false;
}
public static function rfc2445DoValueFormatting($value, $type)
{
// Note: this does not only do formatting; it also does conversion to string!
switch ($type) {
case RFC2445_TYPE_CAL_ADDRESS:
case RFC2445_TYPE_URI:
// Enclose in double quotes
$value = '"' . $value . '"';
break;
case RFC2445_TYPE_TEXT:
// Escape entities
$value = strtr($value, ["\n" => '\\n', '\\' => '\\\\', ',' => '\\,', ';' => '\\;']);
break;
}
return $value;
}
// This is cheating: GUIDs have nothing to do with RFC 2445
public static function rfc2445Guid()
{
// Implemented as per the Network Working Group draft on UUIDs and GUIDs
// These two octets get special treatment
$time_hi_and_version = sprintf('%02x', (1 << 6) + mt_rand(0, 15)); // 0100 plus 4 random bits
$clock_seq_hi_and_reserved = sprintf('%02x', (1 << 7) + mt_rand(0, 63)); // 10 plus 6 random bits
// Need another 14 random octects
$pool = '';
for ($i = 0; $i < 7; ++$i) {
$pool .= sprintf('%04x', mt_rand(0, 65535));
}
// time_low = 4 octets
$random = substr($pool, 0, 8) . '-';
// time_mid = 2 octets
$random .= substr($pool, 8, 4) . '-';
// time_high_and_version = 2 octets
$random .= $time_hi_and_version . substr($pool, 12, 2) . '-';
// clock_seq_high_and_reserved = 1 octet
$random .= $clock_seq_hi_and_reserved;
// clock_seq_low = 1 octet
$random .= substr($pool, 13, 2) . '-';
// node = 6 octets
$random .= substr($pool, 14, 12);
return $random;
}
}