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
12 * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
17 All names of properties, property parameters, enumerated property
18 values and property parameter values are case-insensitive. However,
19 all other property values are case-sensitive, unless otherwise
24 define('RFC2445_CRLF', "\r\n");
25 define('RFC2445_WSP', "\t ");
26 define('RFC2445_WEEKDAYS', 'MO,TU,WE,TH,FR,SA,SU');
27 define('RFC2445_FOLDED_LINE_LENGTH', 75);
29 define('RFC2445_PARAMETER_SEPARATOR', ';');
30 define('RFC2445_VALUE_SEPARATOR', ':');
32 define('RFC2445_REQUIRED', 0x01);
33 define('RFC2445_OPTIONAL', 0x02);
34 define('RFC2445_ONCE', 0x04);
36 define('RFC2445_PROP_FLAGS', 0);
37 define('RFC2445_PROP_TYPE', 1);
38 define('RFC2445_PROP_DEFAULT', 2);
40 define('RFC2445_XNAME', 'X-');
42 define('RFC2445_TYPE_BINARY', 0);
43 define('RFC2445_TYPE_BOOLEAN', 1);
44 define('RFC2445_TYPE_CAL_ADDRESS', 2);
45 define('RFC2445_TYPE_DATE', 3);
46 define('RFC2445_TYPE_DATE_TIME', 4);
47 define('RFC2445_TYPE_DURATION', 5);
48 define('RFC2445_TYPE_FLOAT', 6);
49 define('RFC2445_TYPE_INTEGER', 7);
50 define('RFC2445_TYPE_PERIOD', 8);
51 define('RFC2445_TYPE_RECUR', 9);
52 define('RFC2445_TYPE_TEXT', 10);
53 define('RFC2445_TYPE_TIME', 11);
54 define('RFC2445_TYPE_URI', 12); // CAL_ADDRESS === URI
55 define('RFC2445_TYPE_UTC_OFFSET', 13);
58 function rfc2445_fold($string) {
59 if(core_text
::strlen($string, 'utf-8') <= RFC2445_FOLDED_LINE_LENGTH
) {
68 //multi-byte string, get the correct length
69 $section_len = core_text
::strlen($string, 'utf-8');
71 while($len_count<$section_len) {
73 //get the current portion of the line
74 $section = core_text
::substr($string, ($i * RFC2445_FOLDED_LINE_LENGTH
), (RFC2445_FOLDED_LINE_LENGTH
), 'utf-8');
76 //increment the length we've processed by the length of the new portion
77 $len_count +
= core_text
::strlen($section, 'utf-8');
79 /* Add the portion to the return value, terminating with CRLF.HTAB
80 As per RFC 2445, CRLF.HTAB will be replaced by the processor of the
82 $retval .= $section.RFC2445_CRLF
.RFC2445_WSP
;
91 function rfc2445_unfold($string) {
92 for($i = 0; $i < strlen(RFC2445_WSP
); ++
$i) {
93 $string = str_replace(RFC2445_CRLF
.substr(RFC2445_WSP
, $i, 1), '', $string);
99 function rfc2445_is_xname($name) {
101 // If it's less than 3 chars, it cannot be legal
102 if(strlen($name) < 3) {
106 // If it contains an illegal char anywhere, reject it
107 if(strspn($name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-') != strlen($name)) {
111 // To be legal, it must still start with "X-"
112 return substr($name, 0, 2) === 'X-';
115 function rfc2445_is_valid_value($value, $type) {
117 // This branch should only be taken with xname values
123 case RFC2445_TYPE_CAL_ADDRESS
:
124 case RFC2445_TYPE_URI
:
125 if(!is_string($value)) {
129 $valid_schemes = array('ftp', 'http', 'ldap', 'gopher', 'mailto', 'news', 'nntp', 'telnet', 'wais', 'file', 'prospero');
131 $pos = strpos($value, ':');
136 $scheme = strtolower(substr($value, 0, $pos));
137 $remain = substr($value, $pos +
1);
139 if(!in_array($scheme, $valid_schemes)) {
143 if($scheme === 'mailto') {
144 $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})$#';
147 $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})?(/.*)?$#';
150 return preg_match($regexp, $remain);
153 case RFC2445_TYPE_BINARY
:
154 if(!is_string($value)) {
158 $len = strlen($value);
164 for($i = 0; $i < $len; ++
$i) {
166 if(!($ch >= 'a' && $ch <= 'z' ||
$ch >= 'A' && $ch <= 'Z' ||
$ch >= '0' && $ch <= '9' ||
$ch == '-' ||
$ch == '+')) {
167 if($ch == '=' && $len - $i <= 2) {
176 case RFC2445_TYPE_BOOLEAN
:
177 if(is_bool($value)) {
180 if(is_string($value)) {
181 $value = strtoupper($value);
182 return ($value == 'TRUE' ||
$value == 'FALSE');
187 case RFC2445_TYPE_DATE
:
194 else if(!is_string($value)) {
198 if(strlen($value) != 8) {
202 $y = intval(substr($value, 0, 4));
203 $m = intval(substr($value, 4, 2));
204 $d = intval(substr($value, 6, 2));
206 return checkdate($m, $d, $y);
209 case RFC2445_TYPE_DATE_TIME
:
210 if(!is_string($value) ||
strlen($value) < 15) {
214 return($value{8} == 'T' &&
215 rfc2445_is_valid_value(substr($value, 0, 8), RFC2445_TYPE_DATE
) &&
216 rfc2445_is_valid_value(substr($value, 9), RFC2445_TYPE_TIME
));
219 case RFC2445_TYPE_DURATION
:
220 if(!is_string($value)) {
224 $len = strlen($value);
227 // Minimum conformant length: "P1W"
231 if($value{0} == '+' ||
$value{0} == '-') {
232 $value = substr($value, 1);
233 --$len; // Don't forget to update this!
236 if($value{0} != 'P') {
240 // OK, now break it up
244 for($i = 1; $i < $len; ++
$i) {
246 if($ch >= '0' && $ch <= '9') {
250 if(strpos($allowed, $ch) === false) {
251 // Non-numeric character which shouldn't be here
254 if($num === '' && $ch != 'T') {
255 // Allowed non-numeric character, but no digits came before it
259 // OK, $ch now holds a character which tells us what $num is
262 // If duration in weeks is specified, this must end the string
263 return ($i == $len - 1);
267 // Days specified, now if anything comes after it must be a 'T'
272 // Starting to specify time, H M S are now valid delimiters
285 return ($i == $len - 1);
289 // If we 're going to continue, reset $num
294 // $num is kept for this reason: if we 're here, we ran out of chars
295 // therefore $num must be empty for the period to be legal
296 return ($num === '' && $ch != 'T');
300 case RFC2445_TYPE_FLOAT
:
301 if(is_float($value)) {
304 if(!is_string($value) ||
$value === '') {
310 $len = strlen($value);
311 for($i = 0; $i < $len; ++
$i) {
314 // A sign can only be seen at position 0 and cannot be the only char
315 if($i != 0 ||
$len == 1) {
320 // A second dot is an error
321 // Make sure we had at least one int before the dot
326 // Make also sure that the float doesn't end with a dot
331 case '0': case '1': case '2': case '3': case '4':
332 case '5': case '6': case '7': case '8': case '9':
336 // Any other char is a no-no
344 case RFC2445_TYPE_INTEGER
:
348 if(!is_string($value) ||
$value === '') {
352 if($value{0} == '+' ||
$value{0} == '-') {
353 if(strlen($value) == 1) {
356 $value = substr($value, 1);
359 if(strspn($value, '0123456789') != strlen($value)) {
363 return ($value >= -2147483648 && $value <= 2147483647);
366 case RFC2445_TYPE_PERIOD
:
367 if(!is_string($value) ||
empty($value)) {
371 $parts = explode('/', $value);
372 if(count($parts) != 2) {
376 if(!rfc2445_is_valid_value($parts[0], RFC2445_TYPE_DATE_TIME
)) {
380 // Two legal cases for the second part:
381 if(rfc2445_is_valid_value($parts[1], RFC2445_TYPE_DATE_TIME
)) {
382 // It has to be after the start time, so
383 return ($parts[1] > $parts[0]);
385 else if(rfc2445_is_valid_value($parts[1], RFC2445_TYPE_DURATION
)) {
386 // The period MUST NOT be negative
387 return ($parts[1]{0} != '-');
390 // It seems to be illegal
394 case RFC2445_TYPE_RECUR
:
395 if(!is_string($value)) {
399 $parts = explode(';', strtoupper($value));
401 // We need at least one part for a valid rule, for example: "FREQ=DAILY".
406 // Let's get that into a more easily comprehensible format
408 foreach($parts as $part) {
410 $pieces = explode('=', $part);
411 // There must be exactly 2 pieces, e.g. FREQ=WEEKLY
412 if(count($pieces) != 2) {
416 // It's illegal for a variable to appear twice
417 if(isset($vars[$pieces[0]])) {
422 $vars[$pieces[0]] = $pieces[1];
425 // OK... now to test everything else
427 // FREQ must be the first thing appearing
429 if(key($vars) != 'FREQ') {
433 // It's illegal to have both UNTIL and COUNT appear
434 if(isset($vars['UNTIL']) && isset($vars['COUNT'])) {
438 // Special case: BYWEEKNO is only valid for FREQ=YEARLY
439 if(isset($vars['BYWEEKNO']) && $vars['FREQ'] != 'YEARLY') {
443 // Special case: BYSETPOS is only valid if another BY option is specified
444 if(isset($vars['BYSETPOS'])) {
445 $options = array('BYSECOND', 'BYMINUTE', 'BYHOUR', 'BYDAY', 'BYMONTHDAY', 'BYYEARDAY', 'BYWEEKNO', 'BYMONTH');
446 $defined = array_keys($vars);
447 $common = array_intersect($options, $defined);
453 // OK, now simply check if each element has a valid value,
454 // unsetting them on the way. If at the end the array still
455 // has some elements, they are illegal.
457 if($vars['FREQ'] != 'SECONDLY' && $vars['FREQ'] != 'MINUTELY' && $vars['FREQ'] != 'HOURLY' &&
458 $vars['FREQ'] != 'DAILY' && $vars['FREQ'] != 'WEEKLY' &&
459 $vars['FREQ'] != 'MONTHLY' && $vars['FREQ'] != 'YEARLY') {
462 unset($vars['FREQ']);
464 // Set this, we may need it later
465 $weekdays = explode(',', RFC2445_WEEKDAYS
);
467 if(isset($vars['UNTIL'])) {
468 if(rfc2445_is_valid_value($vars['UNTIL'], RFC2445_TYPE_DATE_TIME
)) {
469 // The time MUST be in UTC format
470 if(!(substr($vars['UNTIL'], -1) == 'Z')) {
474 else if(!rfc2445_is_valid_value($vars['UNTIL'], RFC2445_TYPE_DATE_TIME
)) {
478 unset($vars['UNTIL']);
481 if(isset($vars['COUNT'])) {
482 if(empty($vars['COUNT'])) {
483 // This also catches the string '0', which makes no sense
486 if(strspn($vars['COUNT'], '0123456789') != strlen($vars['COUNT'])) {
490 unset($vars['COUNT']);
493 if(isset($vars['INTERVAL'])) {
494 if(empty($vars['INTERVAL'])) {
495 // This also catches the string '0', which makes no sense
498 if(strspn($vars['INTERVAL'], '0123456789') != strlen($vars['INTERVAL'])) {
502 unset($vars['INTERVAL']);
505 if(isset($vars['BYSECOND'])) {
506 if($vars['BYSECOND'] == '') {
509 // Comma also allowed
510 if(strspn($vars['BYSECOND'], '0123456789,') != strlen($vars['BYSECOND'])) {
513 $secs = explode(',', $vars['BYSECOND']);
514 foreach($secs as $sec) {
515 if($sec == '' ||
$sec < 0 ||
$sec > 59) {
520 unset($vars['BYSECOND']);
523 if(isset($vars['BYMINUTE'])) {
524 if($vars['BYMINUTE'] == '') {
527 // Comma also allowed
528 if(strspn($vars['BYMINUTE'], '0123456789,') != strlen($vars['BYMINUTE'])) {
531 $mins = explode(',', $vars['BYMINUTE']);
532 foreach($mins as $min) {
533 if($min == '' ||
$min < 0 ||
$min > 59) {
538 unset($vars['BYMINUTE']);
541 if(isset($vars['BYHOUR'])) {
542 if($vars['BYHOUR'] == '') {
545 // Comma also allowed
546 if(strspn($vars['BYHOUR'], '0123456789,') != strlen($vars['BYHOUR'])) {
549 $hours = explode(',', $vars['BYHOUR']);
550 foreach($hours as $hour) {
551 if($hour == '' ||
$hour < 0 ||
$hour > 23) {
556 unset($vars['BYHOUR']);
559 if(isset($vars['BYDAY'])) {
560 if(empty($vars['BYDAY'])) {
564 // First off, split up all values we may have
565 $days = explode(',', $vars['BYDAY']);
567 foreach($days as $day) {
568 $daypart = substr($day, -2);
569 if(!in_array($daypart, $weekdays)) {
573 if(strlen($day) > 2) {
574 $intpart = substr($day, 0, strlen($day) - 2);
575 if(!rfc2445_is_valid_value($intpart, RFC2445_TYPE_INTEGER
)) {
578 if(intval($intpart) == 0) {
584 unset($vars['BYDAY']);
587 if(isset($vars['BYMONTHDAY'])) {
588 if(empty($vars['BYMONTHDAY'])) {
591 $mdays = explode(',', $vars['BYMONTHDAY']);
592 foreach($mdays as $mday) {
593 if(!rfc2445_is_valid_value($mday, RFC2445_TYPE_INTEGER
)) {
596 $mday = abs(intval($mday));
597 if($mday == 0 ||
$mday > 31) {
602 unset($vars['BYMONTHDAY']);
605 if(isset($vars['BYYEARDAY'])) {
606 if(empty($vars['BYYEARDAY'])) {
609 $ydays = explode(',', $vars['BYYEARDAY']);
610 foreach($ydays as $yday) {
611 if(!rfc2445_is_valid_value($yday, RFC2445_TYPE_INTEGER
)) {
614 $yday = abs(intval($yday));
615 if($yday == 0 ||
$yday > 366) {
620 unset($vars['BYYEARDAY']);
623 if(isset($vars['BYWEEKNO'])) {
624 if(empty($vars['BYWEEKNO'])) {
627 $weeknos = explode(',', $vars['BYWEEKNO']);
628 foreach($weeknos as $weekno) {
629 if(!rfc2445_is_valid_value($weekno, RFC2445_TYPE_INTEGER
)) {
632 $weekno = abs(intval($weekno));
633 if($weekno == 0 ||
$weekno > 53) {
638 unset($vars['BYWEEKNO']);
641 if(isset($vars['BYMONTH'])) {
642 if(empty($vars['BYMONTH'])) {
645 // Comma also allowed
646 if(strspn($vars['BYMONTH'], '0123456789,') != strlen($vars['BYMONTH'])) {
649 $months = explode(',', $vars['BYMONTH']);
650 foreach($months as $month) {
651 if($month == '' ||
$month < 1 ||
$month > 12) {
656 unset($vars['BYMONTH']);
659 if(isset($vars['BYSETPOS'])) {
660 if(empty($vars['BYSETPOS'])) {
663 $sets = explode(',', $vars['BYSETPOS']);
664 foreach($sets as $set) {
665 if(!rfc2445_is_valid_value($set, RFC2445_TYPE_INTEGER
)) {
668 $set = abs(intval($set));
669 if($set == 0 ||
$set > 366) {
674 unset($vars['BYSETPOS']);
677 if(isset($vars['WKST'])) {
678 if(!in_array($vars['WKST'], $weekdays)) {
682 unset($vars['WKST']);
685 // Any remaining vars must be x-names
690 foreach($vars as $name => $var) {
691 if(!rfc2445_is_xname($name)) {
696 // At last, all is OK!
701 case RFC2445_TYPE_TEXT
:
705 case RFC2445_TYPE_TIME
:
712 else if(!is_string($value)) {
716 if(strlen($value) == 7) {
717 if(strtoupper(substr($value, -1)) != 'Z') {
720 $value = substr($value, 0, 6);
722 if(strlen($value) != 6) {
726 $h = intval(substr($value, 0, 2));
727 $m = intval(substr($value, 2, 2));
728 $s = intval(substr($value, 4, 2));
730 return ($h <= 23 && $m <= 59 && $s <= 60);
733 case RFC2445_TYPE_UTC_OFFSET
:
742 else if(!is_string($value)) {
747 if(strlen($value) == 7) {
748 $s = intval(substr($value, 5, 2));
749 $value = substr($value, 0, 5);
751 if(strlen($value) != 5 ||
$value == "-0000") {
755 if($value{0} != '+' && $value{0} != '-') {
759 $h = intval(substr($value, 1, 2));
760 $m = intval(substr($value, 3, 2));
762 return ($h <= 23 && $m <= 59 && $s <= 59);
766 // TODO: remove this assertion
767 trigger_error('bad code path', E_USER_WARNING
);
772 function rfc2445_do_value_formatting($value, $type) {
773 // Note: this does not only do formatting; it also does conversion to string!
775 case RFC2445_TYPE_CAL_ADDRESS
:
776 case RFC2445_TYPE_URI
:
777 // Enclose in double quotes
778 $value = '"'.$value.'"';
780 case RFC2445_TYPE_TEXT
:
782 $value = strtr($value, array("\r\n" => '\\n', "\n" => '\\n', '\\' => '\\\\', ',' => '\\,', ';' => '\\;'));
788 function rfc2445_undo_value_formatting($value, $type) {
790 case RFC2445_TYPE_CAL_ADDRESS
:
791 case RFC2445_TYPE_URI
:
792 // Trim beginning and end double quote
793 $value = substr($value, 1, strlen($value) - 2);
795 case RFC2445_TYPE_TEXT
:
797 $value = strtr($value, array('\\n' => "\n", '\\N' => "\n", '\\\\' => '\\', '\\,' => ',', '\\;' => ';'));