MDL-11517 reserved word MOD used in table alias in questions backup code
[moodle-pu.git] / lib / bennu / iCalendar_rfc2445.php
blobefb6e4bdfcbb4b1d9d9697c3d4d4e8688d334aea
1 <?php // $Id$
3 /**
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 * @version $Id$
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
21 stated.
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) {
58 return $string;
61 $retval = '';
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);
68 $retval .= $string;
70 return $retval;
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);
79 return $string;
82 function rfc2445_is_xname($name) {
84 // If it's less than 3 chars, it cannot be legal
85 if(strlen($name) < 3) {
86 return false;
89 // If it contains an illegal char anywhere, reject it
90 if(strspn($name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-') != strlen($name)) {
91 return false;
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
101 if($type === NULL) {
102 return true;
105 switch($type) {
106 case RFC2445_TYPE_CAL_ADDRESS:
107 case RFC2445_TYPE_URI:
108 if(!is_string($value)) {
109 return false;
112 $valid_schemes = array('ftp', 'http', 'ldap', 'gopher', 'mailto', 'news', 'nntp', 'telnet', 'wais', 'file', 'prospero');
114 $pos = strpos($value, ':');
115 if(!$pos) {
116 return false;
119 $scheme = strtolower(substr($value, 0, $pos));
120 $remain = substr($value, $pos + 1);
122 if(!in_array($scheme, $valid_schemes)) {
123 return false;
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})$';
129 else {
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);
134 break;
136 case RFC2445_TYPE_BINARY:
137 if(!is_string($value)) {
138 return false;
141 $len = strlen($value);
143 if($len % 4 != 0) {
144 return false;
147 for($i = 0; $i < $len; ++$i) {
148 $ch = $value{$i};
149 if(!($ch >= 'a' && $ch <= 'z' || $ch >= 'A' && $ch <= 'Z' || $ch >= '0' && $ch <= '9' || $ch == '-' || $ch == '+')) {
150 if($ch == '=' && $len - $i <= 2) {
151 continue;
153 return false;
156 return true;
157 break;
159 case RFC2445_TYPE_BOOLEAN:
160 if(is_bool($value)) {
161 return true;
163 if(is_string($value)) {
164 $value = strtoupper($value);
165 return ($value == 'TRUE' || $value == 'FALSE');
167 return false;
168 break;
170 case RFC2445_TYPE_DATE:
171 if(is_int($value)) {
172 if($value < 0) {
173 return false;
175 $value = "$value";
177 else if(!is_string($value)) {
178 return false;
181 if(strlen($value) != 8) {
182 return false;
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);
190 break;
192 case RFC2445_TYPE_DATE_TIME:
193 if(!is_string($value) || strlen($value) < 15) {
194 return false;
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));
200 break;
202 case RFC2445_TYPE_DURATION:
203 if(!is_string($value)) {
204 return false;
207 $len = strlen($value);
209 if($len < 3) {
210 // Minimum conformant length: "P1W"
211 return false;
214 if($value{0} == '+' || $value{0} == '-') {
215 $value = substr($value, 1);
216 --$len; // Don't forget to update this!
219 if($value{0} != 'P') {
220 return false;
223 // OK, now break it up
224 $num = '';
225 $allowed = 'WDT';
227 for($i = 1; $i < $len; ++$i) {
228 $ch = $value{$i};
229 if($ch >= '0' && $ch <= '9') {
230 $num .= $ch;
231 continue;
233 if(strpos($allowed, $ch) === false) {
234 // Non-numeric character which shouldn't be here
235 return false;
237 if($num === '' && $ch != 'T') {
238 // Allowed non-numeric character, but no digits came before it
239 return false;
242 // OK, $ch now holds a character which tells us what $num is
243 switch($ch) {
244 case 'W':
245 // If duration in weeks is specified, this must end the string
246 return ($i == $len - 1);
247 break;
249 case 'D':
250 // Days specified, now if anything comes after it must be a 'T'
251 $allowed = 'T';
252 break;
254 case 'T':
255 // Starting to specify time, H M S are now valid delimiters
256 $allowed = 'HMS';
257 break;
259 case 'H':
260 $allowed = 'M';
261 break;
263 case 'M':
264 $allowed = 'S';
265 break;
267 case 'S':
268 return ($i == $len - 1);
269 break;
272 // If we 're going to continue, reset $num
273 $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');
281 break;
283 case RFC2445_TYPE_FLOAT:
284 if(is_float($value)) {
285 return true;
287 if(!is_string($value) || $value === '') {
288 return false;
291 $dot = false;
292 $int = false;
293 $len = strlen($value);
294 for($i = 0; $i < $len; ++$i) {
295 switch($value{$i}) {
296 case '-': case '+':
297 // A sign can only be seen at position 0 and cannot be the only char
298 if($i != 0 || $len == 1) {
299 return false;
301 break;
302 case '.':
303 // A second dot is an error
304 // Make sure we had at least one int before the dot
305 if($dot || !$int) {
306 return false;
308 $dot = true;
309 // Make also sure that the float doesn't end with a dot
310 if($i == $len - 1) {
311 return false;
313 break;
314 case '0': case '1': case '2': case '3': case '4':
315 case '5': case '6': case '7': case '8': case '9':
316 $int = true;
317 break;
318 default:
319 // Any other char is a no-no
320 return false;
321 break;
324 return true;
325 break;
327 case RFC2445_TYPE_INTEGER:
328 if(is_int($value)) {
329 return true;
331 if(!is_string($value) || $value === '') {
332 return false;
335 if($value{0} == '+' || $value{0} == '-') {
336 if(strlen($value) == 1) {
337 return false;
339 $value = substr($value, 1);
342 if(strspn($value, '0123456789') != strlen($value)) {
343 return false;
346 return ($value >= -2147483648 && $value <= 2147483647);
347 break;
349 case RFC2445_TYPE_PERIOD:
350 if(!is_string($value) || empty($value)) {
351 return false;
354 $parts = explode('/', $value);
355 if(count($parts) != 2) {
356 return false;
359 if(!rfc2445_is_valid_value($parts[0], RFC2445_TYPE_DATE_TIME)) {
360 return false;
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
374 return false;
375 break;
377 case RFC2445_TYPE_RECUR:
378 if(!is_string($value)) {
379 return false;
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) {
386 return false;
389 // Let's get that into a more easily comprehensible format
390 $vars = array();
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) {
396 return false;
399 // It's illegal for a variable to appear twice
400 if(isset($vars[$pieces[0]])) {
401 return false;
404 // Sounds good
405 $vars[$pieces[0]] = $pieces[1];
408 // OK... now to test everything else
410 // FREQ must be the first thing appearing
411 reset($vars);
412 if(key($vars) != 'FREQ') {
413 return false;
416 // It's illegal to have both UNTIL and COUNT appear
417 if(isset($vars['UNTIL']) && isset($vars['COUNT'])) {
418 return false;
421 // Special case: BYWEEKNO is only valid for FREQ=YEARLY
422 if(isset($vars['BYWEEKNO']) && $vars['FREQ'] != 'YEARLY') {
423 return false;
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);
431 if(empty($common)) {
432 return false;
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') {
443 return false;
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')) {
454 return false;
457 else if(!rfc2445_is_valid_value($vars['UNTIL'], RFC2445_TYPE_DATE_TIME)) {
458 return false;
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
467 return false;
469 if(strspn($vars['COUNT'], '0123456789') != strlen($vars['COUNT'])) {
470 return false;
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
479 return false;
481 if(strspn($vars['INTERVAL'], '0123456789') != strlen($vars['INTERVAL'])) {
482 return false;
485 unset($vars['INTERVAL']);
488 if(isset($vars['BYSECOND'])) {
489 if($vars['BYSECOND'] == '') {
490 return false;
492 // Comma also allowed
493 if(strspn($vars['BYSECOND'], '0123456789,') != strlen($vars['BYSECOND'])) {
494 return false;
496 $secs = explode(',', $vars['BYSECOND']);
497 foreach($secs as $sec) {
498 if($sec == '' || $sec < 0 || $sec > 59) {
499 return false;
503 unset($vars['BYSECOND']);
506 if(isset($vars['BYMINUTE'])) {
507 if($vars['BYMINUTE'] == '') {
508 return false;
510 // Comma also allowed
511 if(strspn($vars['BYMINUTE'], '0123456789,') != strlen($vars['BYMINUTE'])) {
512 return false;
514 $mins = explode(',', $vars['BYMINUTE']);
515 foreach($mins as $min) {
516 if($min == '' || $min < 0 || $min > 59) {
517 return false;
521 unset($vars['BYMINUTE']);
524 if(isset($vars['BYHOUR'])) {
525 if($vars['BYHOUR'] == '') {
526 return false;
528 // Comma also allowed
529 if(strspn($vars['BYHOUR'], '0123456789,') != strlen($vars['BYHOUR'])) {
530 return false;
532 $hours = explode(',', $vars['BYHOUR']);
533 foreach($hours as $hour) {
534 if($hour == '' || $hour < 0 || $hour > 23) {
535 return false;
539 unset($vars['BYHOUR']);
542 if(isset($vars['BYDAY'])) {
543 if(empty($vars['BYDAY'])) {
544 return false;
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)) {
553 return false;
556 if(strlen($day) > 2) {
557 $intpart = substr($day, 0, strlen($day) - 2);
558 if(!rfc2445_is_valid_value($intpart, RFC2445_TYPE_INTEGER)) {
559 return false;
561 if(intval($intpart) == 0) {
562 return false;
567 unset($vars['BYDAY']);
570 if(isset($vars['BYMONTHDAY'])) {
571 if(empty($vars['BYMONTHDAY'])) {
572 return false;
574 $mdays = explode(',', $vars['BYMONTHDAY']);
575 foreach($mdays as $mday) {
576 if(!rfc2445_is_valid_value($mday, RFC2445_TYPE_INTEGER)) {
577 return false;
579 $mday = abs(intval($mday));
580 if($mday == 0 || $mday > 31) {
581 return false;
585 unset($vars['BYMONTHDAY']);
588 if(isset($vars['BYYEARDAY'])) {
589 if(empty($vars['BYYEARDAY'])) {
590 return false;
592 $ydays = explode(',', $vars['BYYEARDAY']);
593 foreach($ydays as $yday) {
594 if(!rfc2445_is_valid_value($yday, RFC2445_TYPE_INTEGER)) {
595 return false;
597 $yday = abs(intval($yday));
598 if($yday == 0 || $yday > 366) {
599 return false;
603 unset($vars['BYYEARDAY']);
606 if(isset($vars['BYWEEKNO'])) {
607 if(empty($vars['BYWEEKNO'])) {
608 return false;
610 $weeknos = explode(',', $vars['BYWEEKNO']);
611 foreach($weeknos as $weekno) {
612 if(!rfc2445_is_valid_value($weekno, RFC2445_TYPE_INTEGER)) {
613 return false;
615 $weekno = abs(intval($weekno));
616 if($weekno == 0 || $weekno > 53) {
617 return false;
621 unset($vars['BYWEEKNO']);
624 if(isset($vars['BYMONTH'])) {
625 if(empty($vars['BYMONTH'])) {
626 return false;
628 // Comma also allowed
629 if(strspn($vars['BYMONTH'], '0123456789,') != strlen($vars['BYMONTH'])) {
630 return false;
632 $months = explode(',', $vars['BYMONTH']);
633 foreach($months as $month) {
634 if($month == '' || $month < 1 || $month > 12) {
635 return false;
639 unset($vars['BYMONTH']);
642 if(isset($vars['BYSETPOS'])) {
643 if(empty($vars['BYSETPOS'])) {
644 return false;
646 $sets = explode(',', $vars['BYSETPOS']);
647 foreach($sets as $set) {
648 if(!rfc2445_is_valid_value($set, RFC2445_TYPE_INTEGER)) {
649 return false;
651 $set = abs(intval($set));
652 if($set == 0 || $set > 366) {
653 return false;
657 unset($vars['BYSETPOS']);
660 if(isset($vars['WKST'])) {
661 if(!in_array($vars['WKST'], $weekdays)) {
662 return false;
665 unset($vars['WKST']);
668 // Any remaining vars must be x-names
669 if(empty($vars)) {
670 return true;
673 foreach($vars as $name => $var) {
674 if(!rfc2445_is_xname($name)) {
675 return false;
679 // At last, all is OK!
680 return true;
682 break;
684 case RFC2445_TYPE_TEXT:
685 return true;
686 break;
688 case RFC2445_TYPE_TIME:
689 if(is_int($value)) {
690 if($value < 0) {
691 return false;
693 $value = "$value";
695 else if(!is_string($value)) {
696 return false;
699 if(strlen($value) == 7) {
700 if(strtoupper(substr($value, -1)) != 'Z') {
701 return false;
703 $value = substr($value, 0, 6);
705 if(strlen($value) != 6) {
706 return false;
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);
714 break;
716 case RFC2445_TYPE_UTC_OFFSET:
717 if(is_int($value)) {
718 if($value >= 0) {
719 $value = "+$value";
721 else {
722 $value = "$value";
725 else if(!is_string($value)) {
726 return false;
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") {
734 return false;
737 if($value{0} != '+' && $value{0} != '-') {
738 return false;
741 $h = intval(substr($value, 1, 2));
742 $m = intval(substr($value, 3, 2));
744 return ($h <= 23 && $m <= 59 && $s <= 59);
745 break;
748 // TODO: remove this assertion
749 trigger_error('bad code path', E_USER_WARNING);
750 var_dump($type);
751 return false;
754 function rfc2445_do_value_formatting($value, $type) {
755 // Note: this does not only do formatting; it also does conversion to string!
756 switch($type) {
757 case RFC2445_TYPE_CAL_ADDRESS:
758 case RFC2445_TYPE_URI:
759 // Enclose in double quotes
760 $value = '"'.$value.'"';
761 break;
762 case RFC2445_TYPE_TEXT:
763 // Escape entities
764 $value = strtr($value, array("\r\n" => '\\n', "\n" => '\\n', '\\' => '\\\\', ',' => '\\,', ';' => '\\;'));
765 break;
767 return $value;
770 function rfc2445_undo_value_formatting($value, $type) {
771 switch($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);
776 break;
777 case RFC2445_TYPE_TEXT:
778 // Unescape entities
779 $value = strtr($value, array('\\n' => "\n", '\\N' => "\n", '\\\\' => '\\', '\\,' => ',', '\\;' => ';'));
780 break;
782 return $value;