4 * BENNU - PHP iCalendar library
5 * (c) 2005-2006 Ioannis Papaioannou (pj@moodle.org). All rights reserved.
7 * Released under the LGPL.
9 * See http://bennu.sourceforge.net/ for more information and downloads.
11 * @author Ioannis Papaioannou
13 * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
18 All names of properties, property parameters, enumerated property
19 values and property parameter values are case-insensitive. However,
20 all other property values are case-sensitive, unless otherwise
25 define('RFC2445_CRLF', "\r\n");
26 define('RFC2445_WSP', "\t ");
27 define('RFC2445_WEEKDAYS', 'MO,TU,WE,TH,FR,SA,SU');
28 define('RFC2445_FOLDED_LINE_LENGTH', 75);
30 define('RFC2445_REQUIRED', 0x01);
31 define('RFC2445_OPTIONAL', 0x02);
32 define('RFC2445_ONCE', 0x04);
34 define('RFC2445_PROP_FLAGS', 0);
35 define('RFC2445_PROP_TYPE', 1);
36 define('RFC2445_PROP_DEFAULT', 2);
38 define('RFC2445_XNAME', 'X-');
40 define('RFC2445_TYPE_BINARY', 0);
41 define('RFC2445_TYPE_BOOLEAN', 1);
42 define('RFC2445_TYPE_CAL_ADDRESS', 2);
43 define('RFC2445_TYPE_DATE', 3);
44 define('RFC2445_TYPE_DATE_TIME', 4);
45 define('RFC2445_TYPE_DURATION', 5);
46 define('RFC2445_TYPE_FLOAT', 6);
47 define('RFC2445_TYPE_INTEGER', 7);
48 define('RFC2445_TYPE_PERIOD', 8);
49 define('RFC2445_TYPE_RECUR', 9);
50 define('RFC2445_TYPE_TEXT', 10);
51 define('RFC2445_TYPE_TIME', 11);
52 define('RFC2445_TYPE_URI', 12); // CAL_ADDRESS === URI
53 define('RFC2445_TYPE_UTC_OFFSET', 13);
56 function rfc2445_fold($string) {
57 if(strlen($string) <= RFC2445_FOLDED_LINE_LENGTH
) {
63 while(strlen($string) > RFC2445_FOLDED_LINE_LENGTH
) {
64 $retval .= substr($string, 0, RFC2445_FOLDED_LINE_LENGTH
- 1) . RFC2445_CRLF
. ' ';
65 $string = substr($string, RFC2445_FOLDED_LINE_LENGTH
- 1);
74 function rfc2445_unfold($string) {
75 for($i = 0; $i < strlen(RFC2445_WSP
); ++
$i) {
76 $string = str_replace(RFC2445_CRLF
.substr(RFC2445_WSP
, $i, 1), '', $string);
82 function rfc2445_is_xname($name) {
84 // If it's less than 3 chars, it cannot be legal
85 if(strlen($name) < 3) {
89 // If it contains an illegal char anywhere, reject it
90 if(strspn($name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-') != strlen($name)) {
94 // To be legal, it must still start with "X-"
95 return substr($name, 0, 2) === 'X-';
98 function rfc2445_is_valid_value($value, $type) {
100 // This branch should only be taken with xname values
106 case RFC2445_TYPE_CAL_ADDRESS
:
107 case RFC2445_TYPE_URI
:
108 if(!is_string($value)) {
112 $valid_schemes = array('ftp', 'http', 'ldap', 'gopher', 'mailto', 'news', 'nntp', 'telnet', 'wais', 'file', 'prospero');
114 $pos = strpos($value, ':');
119 $scheme = strtolower(substr($value, 0, $pos));
120 $remain = substr($value, $pos +
1);
122 if(!in_array($scheme, $valid_schemes)) {
126 if($scheme === 'mailto') {
127 $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})$';
130 $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})?(/.*)?$';
133 return ereg($regexp, $remain);
136 case RFC2445_TYPE_BINARY
:
137 if(!is_string($value)) {
141 $len = strlen($value);
147 for($i = 0; $i < $len; ++
$i) {
149 if(!($ch >= 'a' && $ch <= 'z' ||
$ch >= 'A' && $ch <= 'Z' ||
$ch >= '0' && $ch <= '9' ||
$ch == '-' ||
$ch == '+')) {
150 if($ch == '=' && $len - $i <= 2) {
159 case RFC2445_TYPE_BOOLEAN
:
160 if(is_bool($value)) {
163 if(is_string($value)) {
164 $value = strtoupper($value);
165 return ($value == 'TRUE' ||
$value == 'FALSE');
170 case RFC2445_TYPE_DATE
:
177 else if(!is_string($value)) {
181 if(strlen($value) != 8) {
185 $y = intval(substr($value, 0, 4));
186 $m = intval(substr($value, 4, 2));
187 $d = intval(substr($value, 6, 2));
189 return checkdate($m, $d, $y);
192 case RFC2445_TYPE_DATE_TIME
:
193 if(!is_string($value) ||
strlen($value) < 15) {
197 return($value{8} == 'T' &&
198 rfc2445_is_valid_value(substr($value, 0, 8), RFC2445_TYPE_DATE
) &&
199 rfc2445_is_valid_value(substr($value, 9), RFC2445_TYPE_TIME
));
202 case RFC2445_TYPE_DURATION
:
203 if(!is_string($value)) {
207 $len = strlen($value);
210 // Minimum conformant length: "P1W"
214 if($value{0} == '+' ||
$value{0} == '-') {
215 $value = substr($value, 1);
216 --$len; // Don't forget to update this!
219 if($value{0} != 'P') {
223 // OK, now break it up
227 for($i = 1; $i < $len; ++
$i) {
229 if($ch >= '0' && $ch <= '9') {
233 if(strpos($allowed, $ch) === false) {
234 // Non-numeric character which shouldn't be here
237 if($num === '' && $ch != 'T') {
238 // Allowed non-numeric character, but no digits came before it
242 // OK, $ch now holds a character which tells us what $num is
245 // If duration in weeks is specified, this must end the string
246 return ($i == $len - 1);
250 // Days specified, now if anything comes after it must be a 'T'
255 // Starting to specify time, H M S are now valid delimiters
268 return ($i == $len - 1);
272 // If we 're going to continue, reset $num
277 // $num is kept for this reason: if we 're here, we ran out of chars
278 // therefore $num must be empty for the period to be legal
279 return ($num === '' && $ch != 'T');
283 case RFC2445_TYPE_FLOAT
:
284 if(is_float($value)) {
287 if(!is_string($value) ||
$value === '') {
293 $len = strlen($value);
294 for($i = 0; $i < $len; ++
$i) {
297 // A sign can only be seen at position 0 and cannot be the only char
298 if($i != 0 ||
$len == 1) {
303 // A second dot is an error
304 // Make sure we had at least one int before the dot
309 // Make also sure that the float doesn't end with a dot
314 case '0': case '1': case '2': case '3': case '4':
315 case '5': case '6': case '7': case '8': case '9':
319 // Any other char is a no-no
327 case RFC2445_TYPE_INTEGER
:
331 if(!is_string($value) ||
$value === '') {
335 if($value{0} == '+' ||
$value{0} == '-') {
336 if(strlen($value) == 1) {
339 $value = substr($value, 1);
342 if(strspn($value, '0123456789') != strlen($value)) {
346 return ($value >= -2147483648 && $value <= 2147483647);
349 case RFC2445_TYPE_PERIOD
:
350 if(!is_string($value) ||
empty($value)) {
354 $parts = explode('/', $value);
355 if(count($parts) != 2) {
359 if(!rfc2445_is_valid_value($parts[0], RFC2445_TYPE_DATE_TIME
)) {
363 // Two legal cases for the second part:
364 if(rfc2445_is_valid_value($parts[1], RFC2445_TYPE_DATE_TIME
)) {
365 // It has to be after the start time, so
366 return ($parts[1] > $parts[0]);
368 else if(rfc2445_is_valid_value($parts[1], RFC2445_TYPE_DURATION
)) {
369 // The period MUST NOT be negative
370 return ($parts[1]{0} != '-');
373 // It seems to be illegal
377 case RFC2445_TYPE_RECUR
:
378 if(!is_string($value)) {
382 $parts = explode(';', strtoupper($value));
384 // First of all, we need at least a FREQ and a UNTIL or COUNT part, so...
385 if(count($parts) < 2) {
389 // Let's get that into a more easily comprehensible format
391 foreach($parts as $part) {
393 $pieces = explode('=', $part);
394 // There must be exactly 2 pieces, e.g. FREQ=WEEKLY
395 if(count($pieces) != 2) {
399 // It's illegal for a variable to appear twice
400 if(isset($vars[$pieces[0]])) {
405 $vars[$pieces[0]] = $pieces[1];
408 // OK... now to test everything else
410 // FREQ must be the first thing appearing
412 if(key($vars) != 'FREQ') {
416 // It's illegal to have both UNTIL and COUNT appear
417 if(isset($vars['UNTIL']) && isset($vars['COUNT'])) {
421 // Special case: BYWEEKNO is only valid for FREQ=YEARLY
422 if(isset($vars['BYWEEKNO']) && $vars['FREQ'] != 'YEARLY') {
426 // Special case: BYSETPOS is only valid if another BY option is specified
427 if(isset($vars['BYSETPOS'])) {
428 $options = array('BYSECOND', 'BYMINUTE', 'BYHOUR', 'BYDAY', 'BYMONTHDAY', 'BYYEARDAY', 'BYWEEKNO', 'BYMONTH');
429 $defined = array_keys($vars);
430 $common = array_intersect($options, $defined);
436 // OK, now simply check if each element has a valid value,
437 // unsetting them on the way. If at the end the array still
438 // has some elements, they are illegal.
440 if($vars['FREQ'] != 'SECONDLY' && $vars['FREQ'] != 'MINUTELY' && $vars['FREQ'] != 'HOURLY' &&
441 $vars['FREQ'] != 'DAILY' && $vars['FREQ'] != 'WEEKLY' &&
442 $vars['FREQ'] != 'MONTHLY' && $vars['FREQ'] != 'YEARLY') {
445 unset($vars['FREQ']);
447 // Set this, we may need it later
448 $weekdays = explode(',', RFC2445_WEEKDAYS
);
450 if(isset($vars['UNTIL'])) {
451 if(rfc2445_is_valid_value($vars['UNTIL'], RFC2445_TYPE_DATE_TIME
)) {
452 // The time MUST be in UTC format
453 if(!(substr($vars['UNTIL'], -1) == 'Z')) {
457 else if(!rfc2445_is_valid_value($vars['UNTIL'], RFC2445_TYPE_DATE_TIME
)) {
461 unset($vars['UNTIL']);
464 if(isset($vars['COUNT'])) {
465 if(empty($vars['COUNT'])) {
466 // This also catches the string '0', which makes no sense
469 if(strspn($vars['COUNT'], '0123456789') != strlen($vars['COUNT'])) {
473 unset($vars['COUNT']);
476 if(isset($vars['INTERVAL'])) {
477 if(empty($vars['INTERVAL'])) {
478 // This also catches the string '0', which makes no sense
481 if(strspn($vars['INTERVAL'], '0123456789') != strlen($vars['INTERVAL'])) {
485 unset($vars['INTERVAL']);
488 if(isset($vars['BYSECOND'])) {
489 if($vars['BYSECOND'] == '') {
492 // Comma also allowed
493 if(strspn($vars['BYSECOND'], '0123456789,') != strlen($vars['BYSECOND'])) {
496 $secs = explode(',', $vars['BYSECOND']);
497 foreach($secs as $sec) {
498 if($sec == '' ||
$sec < 0 ||
$sec > 59) {
503 unset($vars['BYSECOND']);
506 if(isset($vars['BYMINUTE'])) {
507 if($vars['BYMINUTE'] == '') {
510 // Comma also allowed
511 if(strspn($vars['BYMINUTE'], '0123456789,') != strlen($vars['BYMINUTE'])) {
514 $mins = explode(',', $vars['BYMINUTE']);
515 foreach($mins as $min) {
516 if($min == '' ||
$min < 0 ||
$min > 59) {
521 unset($vars['BYMINUTE']);
524 if(isset($vars['BYHOUR'])) {
525 if($vars['BYHOUR'] == '') {
528 // Comma also allowed
529 if(strspn($vars['BYHOUR'], '0123456789,') != strlen($vars['BYHOUR'])) {
532 $hours = explode(',', $vars['BYHOUR']);
533 foreach($hours as $hour) {
534 if($hour == '' ||
$hour < 0 ||
$hour > 23) {
539 unset($vars['BYHOUR']);
542 if(isset($vars['BYDAY'])) {
543 if(empty($vars['BYDAY'])) {
547 // First off, split up all values we may have
548 $days = explode(',', $vars['BYDAY']);
550 foreach($days as $day) {
551 $daypart = substr($day, -2);
552 if(!in_array($daypart, $weekdays)) {
556 if(strlen($day) > 2) {
557 $intpart = substr($day, 0, strlen($day) - 2);
558 if(!rfc2445_is_valid_value($intpart, RFC2445_TYPE_INTEGER
)) {
561 if(intval($intpart) == 0) {
567 unset($vars['BYDAY']);
570 if(isset($vars['BYMONTHDAY'])) {
571 if(empty($vars['BYMONTHDAY'])) {
574 $mdays = explode(',', $vars['BYMONTHDAY']);
575 foreach($mdays as $mday) {
576 if(!rfc2445_is_valid_value($mday, RFC2445_TYPE_INTEGER
)) {
579 $mday = abs(intval($mday));
580 if($mday == 0 ||
$mday > 31) {
585 unset($vars['BYMONTHDAY']);
588 if(isset($vars['BYYEARDAY'])) {
589 if(empty($vars['BYYEARDAY'])) {
592 $ydays = explode(',', $vars['BYYEARDAY']);
593 foreach($ydays as $yday) {
594 if(!rfc2445_is_valid_value($yday, RFC2445_TYPE_INTEGER
)) {
597 $yday = abs(intval($yday));
598 if($yday == 0 ||
$yday > 366) {
603 unset($vars['BYYEARDAY']);
606 if(isset($vars['BYWEEKNO'])) {
607 if(empty($vars['BYWEEKNO'])) {
610 $weeknos = explode(',', $vars['BYWEEKNO']);
611 foreach($weeknos as $weekno) {
612 if(!rfc2445_is_valid_value($weekno, RFC2445_TYPE_INTEGER
)) {
615 $weekno = abs(intval($weekno));
616 if($weekno == 0 ||
$weekno > 53) {
621 unset($vars['BYWEEKNO']);
624 if(isset($vars['BYMONTH'])) {
625 if(empty($vars['BYMONTH'])) {
628 // Comma also allowed
629 if(strspn($vars['BYMONTH'], '0123456789,') != strlen($vars['BYMONTH'])) {
632 $months = explode(',', $vars['BYMONTH']);
633 foreach($months as $month) {
634 if($month == '' ||
$month < 1 ||
$month > 12) {
639 unset($vars['BYMONTH']);
642 if(isset($vars['BYSETPOS'])) {
643 if(empty($vars['BYSETPOS'])) {
646 $sets = explode(',', $vars['BYSETPOS']);
647 foreach($sets as $set) {
648 if(!rfc2445_is_valid_value($set, RFC2445_TYPE_INTEGER
)) {
651 $set = abs(intval($set));
652 if($set == 0 ||
$set > 366) {
657 unset($vars['BYSETPOS']);
660 if(isset($vars['WKST'])) {
661 if(!in_array($vars['WKST'], $weekdays)) {
665 unset($vars['WKST']);
668 // Any remaining vars must be x-names
673 foreach($vars as $name => $var) {
674 if(!rfc2445_is_xname($name)) {
679 // At last, all is OK!
684 case RFC2445_TYPE_TEXT
:
688 case RFC2445_TYPE_TIME
:
695 else if(!is_string($value)) {
699 if(strlen($value) == 7) {
700 if(strtoupper(substr($value, -1)) != 'Z') {
703 $value = substr($value, 0, 6);
705 if(strlen($value) != 6) {
709 $h = intval(substr($value, 0, 2));
710 $m = intval(substr($value, 2, 2));
711 $s = intval(substr($value, 4, 2));
713 return ($h <= 23 && $m <= 59 && $s <= 60);
716 case RFC2445_TYPE_UTC_OFFSET
:
725 else if(!is_string($value)) {
729 if(strlen($value) == 7) {
730 $s = intval(substr($value, 5, 2));
731 $value = substr($value, 0, 5);
733 if(strlen($value) != 5 ||
$value == "-0000") {
737 if($value{0} != '+' && $value{0} != '-') {
741 $h = intval(substr($value, 1, 2));
742 $m = intval(substr($value, 3, 2));
744 return ($h <= 23 && $m <= 59 && $s <= 59);
748 // TODO: remove this assertion
749 trigger_error('bad code path', E_USER_WARNING
);
754 function rfc2445_do_value_formatting($value, $type) {
755 // Note: this does not only do formatting; it also does conversion to string!
757 case RFC2445_TYPE_CAL_ADDRESS
:
758 case RFC2445_TYPE_URI
:
759 // Enclose in double quotes
760 $value = '"'.$value.'"';
762 case RFC2445_TYPE_TEXT
:
764 $value = strtr($value, array("\r\n" => '\\n', "\n" => '\\n', '\\' => '\\\\', ',' => '\\,', ';' => '\\;'));
770 function rfc2445_undo_value_formatting($value, $type) {
772 case RFC2445_TYPE_CAL_ADDRESS
:
773 case RFC2445_TYPE_URI
:
774 // Trim beginning and end double quote
775 $value = substr($value, 1, strlen($value) - 2);
777 case RFC2445_TYPE_TEXT
:
779 $value = strtr($value, array('\\n' => "\n", '\\N' => "\n", '\\\\' => '\\', '\\,' => ',', '\\;' => ';'));