Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/bennu/iCalendar_rfc2445.php
Go to the documentation of this file.
00001 <?php
00002 
00015 /*
00016 
00017    All names of properties, property parameters, enumerated property
00018    values and property parameter values are case-insensitive. However,
00019    all other property values are case-sensitive, unless otherwise
00020    stated.
00021 
00022 */
00023 
00024 define('RFC2445_CRLF',               "\r\n");
00025 define('RFC2445_WSP',                "\t ");
00026 define('RFC2445_WEEKDAYS',           'MO,TU,WE,TH,FR,SA,SU');
00027 define('RFC2445_FOLDED_LINE_LENGTH', 75);
00028 
00029 define('RFC2445_PARAMETER_SEPARATOR',   ';');
00030 define('RFC2445_VALUE_SEPARATOR',       ':');
00031 
00032 define('RFC2445_REQUIRED', 0x01);
00033 define('RFC2445_OPTIONAL', 0x02);
00034 define('RFC2445_ONCE',     0x04);
00035 
00036 define('RFC2445_PROP_FLAGS',       0);
00037 define('RFC2445_PROP_TYPE',        1);
00038 define('RFC2445_PROP_DEFAULT',     2);
00039 
00040 define('RFC2445_XNAME', 'X-');
00041 
00042 define('RFC2445_TYPE_BINARY',       0);
00043 define('RFC2445_TYPE_BOOLEAN',      1);
00044 define('RFC2445_TYPE_CAL_ADDRESS',  2);
00045 define('RFC2445_TYPE_DATE',         3);
00046 define('RFC2445_TYPE_DATE_TIME',    4);
00047 define('RFC2445_TYPE_DURATION',     5);
00048 define('RFC2445_TYPE_FLOAT',        6);
00049 define('RFC2445_TYPE_INTEGER',      7);
00050 define('RFC2445_TYPE_PERIOD',       8);
00051 define('RFC2445_TYPE_RECUR',        9);
00052 define('RFC2445_TYPE_TEXT',        10);
00053 define('RFC2445_TYPE_TIME',        11);
00054 define('RFC2445_TYPE_URI',         12); // CAL_ADDRESS === URI
00055 define('RFC2445_TYPE_UTC_OFFSET',  13);
00056 
00057 
00058 function rfc2445_fold($string) {
00059     if(mb_strlen($string, 'utf-8') <= RFC2445_FOLDED_LINE_LENGTH) {
00060         return $string;
00061     }
00062 
00063     $retval = '';
00064   
00065     $i=0;
00066     $len_count=0;
00067 
00068     //multi-byte string, get the correct length
00069     $section_len = mb_strlen($string, 'utf-8');
00070 
00071     while($len_count<$section_len) {
00072         
00073         //get the current portion of the line
00074         $section = mb_substr($string, ($i * RFC2445_FOLDED_LINE_LENGTH), (RFC2445_FOLDED_LINE_LENGTH), 'utf-8');
00075 
00076         //increment the length we've processed by the length of the new portion
00077         $len_count += mb_strlen($section, 'utf-8');
00078         
00079         /* Add the portion to the return value, terminating with CRLF.HTAB
00080            As per RFC 2445, CRLF.HTAB will be replaced by the processor of the 
00081            data */
00082         $retval .= $section.RFC2445_CRLF.RFC2445_WSP;
00083         
00084         $i++;
00085     }
00086 
00087     return $retval;
00088 
00089 }
00090 
00091 function rfc2445_unfold($string) {
00092     for($i = 0; $i < strlen(RFC2445_WSP); ++$i) {
00093         $string = str_replace(RFC2445_CRLF.substr(RFC2445_WSP, $i, 1), '', $string);
00094     }
00095 
00096     return $string;
00097 }
00098 
00099 function rfc2445_is_xname($name) {
00100 
00101     // If it's less than 3 chars, it cannot be legal
00102     if(strlen($name) < 3) {
00103         return false;
00104     }
00105 
00106     // If it contains an illegal char anywhere, reject it
00107     if(strspn($name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-') != strlen($name)) {
00108         return false;
00109     }
00110 
00111     // To be legal, it must still start with "X-"
00112     return substr($name, 0, 2) === 'X-';
00113 }
00114 
00115 function rfc2445_is_valid_value($value, $type) {
00116 
00117     // This branch should only be taken with xname values
00118     if($type === NULL) {
00119         return true;
00120     }
00121 
00122     switch($type) {
00123         case RFC2445_TYPE_CAL_ADDRESS:
00124         case RFC2445_TYPE_URI:
00125             if(!is_string($value)) {
00126                 return false;
00127             }
00128 
00129             $valid_schemes = array('ftp', 'http', 'ldap', 'gopher', 'mailto', 'news', 'nntp', 'telnet', 'wais', 'file', 'prospero');
00130 
00131             $pos = strpos($value, ':');
00132             if(!$pos) {
00133                 return false;
00134             }
00135         
00136             $scheme = strtolower(substr($value, 0, $pos));
00137             $remain = substr($value, $pos + 1);
00138             
00139             if(!in_array($scheme, $valid_schemes)) {
00140                 return false;
00141             }
00142         
00143             if($scheme === 'mailto') {
00144                 $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})$#';
00145             }
00146             else {
00147                 $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})?(/.*)?$#';
00148             }
00149         
00150             return preg_match($regexp, $remain);
00151         break;
00152 
00153         case RFC2445_TYPE_BINARY:
00154             if(!is_string($value)) {
00155                 return false;
00156             }
00157 
00158             $len = strlen($value);
00159             
00160             if($len % 4 != 0) {
00161                 return false;
00162             }
00163 
00164             for($i = 0; $i < $len; ++$i) {
00165                 $ch = $value{$i};
00166                 if(!($ch >= 'a' && $ch <= 'z' || $ch >= 'A' && $ch <= 'Z' || $ch >= '0' && $ch <= '9' || $ch == '-' || $ch == '+')) {
00167                     if($ch == '=' && $len - $i <= 2) {
00168                         continue;
00169                     }
00170                     return false;
00171                 }
00172             }
00173             return true;
00174         break;
00175 
00176         case RFC2445_TYPE_BOOLEAN:
00177             if(is_bool($value)) {
00178                 return true;
00179             }
00180             if(is_string($value)) {
00181                 $value = strtoupper($value);
00182                 return ($value == 'TRUE' || $value == 'FALSE');
00183             }
00184             return false;
00185         break;
00186 
00187         case RFC2445_TYPE_DATE:
00188             if(is_int($value)) {
00189                 if($value < 0) {
00190                     return false;
00191                 }
00192                 $value = "$value";
00193             }
00194             else if(!is_string($value)) {
00195                 return false;
00196             }
00197 
00198             if(strlen($value) != 8) {
00199                 return false;
00200             }
00201 
00202             $y = intval(substr($value, 0, 4));
00203             $m = intval(substr($value, 4, 2));
00204             $d = intval(substr($value, 6, 2));
00205 
00206             return checkdate($m, $d, $y);
00207         break;
00208 
00209         case RFC2445_TYPE_DATE_TIME:
00210             if(!is_string($value) || strlen($value) < 15) {
00211                 return false;
00212             }
00213 
00214             return($value{8} == 'T' && 
00215                    rfc2445_is_valid_value(substr($value, 0, 8), RFC2445_TYPE_DATE) &&
00216                    rfc2445_is_valid_value(substr($value, 9), RFC2445_TYPE_TIME));
00217         break;
00218 
00219         case RFC2445_TYPE_DURATION:
00220             if(!is_string($value)) {
00221                 return false;
00222             }
00223 
00224             $len = strlen($value);
00225 
00226             if($len < 3) {
00227                 // Minimum conformant length: "P1W"
00228                 return false;
00229             }
00230 
00231             if($value{0} == '+' || $value{0} == '-') {
00232                 $value = substr($value, 1);
00233                 --$len; // Don't forget to update this!
00234             }
00235 
00236             if($value{0} != 'P') {
00237                 return false;
00238             }
00239 
00240             // OK, now break it up
00241             $num = '';
00242             $allowed = 'WDT';
00243 
00244             for($i = 1; $i < $len; ++$i) {
00245                 $ch = $value{$i};
00246                 if($ch >= '0' && $ch <= '9') {
00247                     $num .= $ch;
00248                     continue;
00249                 }
00250                 if(strpos($allowed, $ch) === false) {
00251                     // Non-numeric character which shouldn't be here
00252                     return false;
00253                 }
00254                 if($num === '' && $ch != 'T') {
00255                     // Allowed non-numeric character, but no digits came before it
00256                     return false;
00257                 }
00258 
00259                 // OK, $ch now holds a character which tells us what $num is
00260                 switch($ch) {
00261                     case 'W':
00262                         // If duration in weeks is specified, this must end the string
00263                         return ($i == $len - 1);
00264                     break;
00265 
00266                     case 'D':
00267                         // Days specified, now if anything comes after it must be a 'T'
00268                         $allowed = 'T';
00269                     break;
00270 
00271                     case 'T':
00272                         // Starting to specify time, H M S are now valid delimiters
00273                         $allowed = 'HMS';
00274                     break;
00275 
00276                     case 'H':
00277                         $allowed = 'M';
00278                     break;
00279 
00280                     case 'M':
00281                         $allowed = 'S';
00282                     break;
00283 
00284                     case 'S':
00285                         return ($i == $len - 1);
00286                     break;
00287                 }
00288 
00289                 // If we 're going to continue, reset $num
00290                 $num = '';
00291 
00292             }
00293 
00294             // $num is kept for this reason: if we 're here, we ran out of chars
00295             // therefore $num must be empty for the period to be legal
00296             return ($num === '' && $ch != 'T');
00297 
00298         break;
00299         
00300         case RFC2445_TYPE_FLOAT:
00301             if(is_float($value)) {
00302                 return true;
00303             }
00304             if(!is_string($value) || $value === '') {
00305                 return false;
00306             }
00307 
00308             $dot = false;
00309             $int = false;
00310             $len = strlen($value);
00311             for($i = 0; $i < $len; ++$i) {
00312                 switch($value{$i}) {
00313                     case '-': case '+':
00314                         // A sign can only be seen at position 0 and cannot be the only char
00315                         if($i != 0 || $len == 1) {
00316                             return false;
00317                         }
00318                     break;
00319                     case '.':
00320                         // A second dot is an error
00321                         // Make sure we had at least one int before the dot
00322                         if($dot || !$int) {
00323                             return false;
00324                         }
00325                         $dot = true;
00326                         // Make also sure that the float doesn't end with a dot
00327                         if($i == $len - 1) {
00328                             return false;
00329                         }
00330                     break;
00331                     case '0': case '1': case '2': case '3': case '4':
00332                     case '5': case '6': case '7': case '8': case '9':
00333                         $int = true;
00334                     break;
00335                     default:
00336                         // Any other char is a no-no
00337                         return false;
00338                     break;
00339                 }
00340             }
00341             return true;
00342         break;
00343 
00344         case RFC2445_TYPE_INTEGER:
00345             if(is_int($value)) {
00346                 return true;
00347             }
00348             if(!is_string($value) || $value === '') {
00349                 return false;
00350             }
00351 
00352             if($value{0} == '+' || $value{0} == '-') {
00353                 if(strlen($value) == 1) {
00354                     return false;
00355                 }
00356                 $value = substr($value, 1);
00357             }
00358 
00359             if(strspn($value, '0123456789') != strlen($value)) {
00360                 return false;
00361             }
00362 
00363             return ($value >= -2147483648 && $value <= 2147483647);
00364         break;
00365 
00366         case RFC2445_TYPE_PERIOD:
00367             if(!is_string($value) || empty($value)) {
00368                 return false;
00369             }
00370 
00371             $parts = explode('/', $value);
00372             if(count($parts) != 2) {
00373                 return false;
00374             }
00375 
00376             if(!rfc2445_is_valid_value($parts[0], RFC2445_TYPE_DATE_TIME)) {
00377                 return false;
00378             }
00379 
00380             // Two legal cases for the second part:
00381             if(rfc2445_is_valid_value($parts[1], RFC2445_TYPE_DATE_TIME)) {
00382                 // It has to be after the start time, so
00383                 return ($parts[1] > $parts[0]);
00384             }
00385             else if(rfc2445_is_valid_value($parts[1], RFC2445_TYPE_DURATION)) {
00386                 // The period MUST NOT be negative
00387                 return ($parts[1]{0} != '-');
00388             }
00389 
00390             // It seems to be illegal
00391             return false;
00392         break;
00393 
00394         case RFC2445_TYPE_RECUR:
00395             if(!is_string($value)) {
00396                 return false;
00397             }
00398 
00399             $parts = explode(';', strtoupper($value));
00400 
00401             // First of all, we need at least a FREQ and a UNTIL or COUNT part, so...
00402             if(count($parts) < 2) {
00403                 return false;
00404             }
00405 
00406             // Let's get that into a more easily comprehensible format
00407             $vars = array();
00408             foreach($parts as $part) {
00409 
00410                 $pieces = explode('=', $part);
00411                 // There must be exactly 2 pieces, e.g. FREQ=WEEKLY
00412                 if(count($pieces) != 2) {
00413                     return false;
00414                 }
00415 
00416                 // It's illegal for a variable to appear twice
00417                 if(isset($vars[$pieces[0]])) {
00418                     return false;
00419                 }
00420 
00421                 // Sounds good
00422                 $vars[$pieces[0]] = $pieces[1];
00423             }
00424 
00425             // OK... now to test everything else
00426 
00427             // FREQ must be the first thing appearing
00428             reset($vars);
00429             if(key($vars) != 'FREQ') {
00430                 return false;
00431             }
00432 
00433             // It's illegal to have both UNTIL and COUNT appear
00434             if(isset($vars['UNTIL']) && isset($vars['COUNT'])) {
00435                 return false;
00436             }
00437 
00438             // Special case: BYWEEKNO is only valid for FREQ=YEARLY
00439             if(isset($vars['BYWEEKNO']) && $vars['FREQ'] != 'YEARLY') {
00440                 return false;
00441             }
00442 
00443             // Special case: BYSETPOS is only valid if another BY option is specified
00444             if(isset($vars['BYSETPOS'])) {
00445                 $options = array('BYSECOND', 'BYMINUTE', 'BYHOUR', 'BYDAY', 'BYMONTHDAY', 'BYYEARDAY', 'BYWEEKNO', 'BYMONTH');
00446                 $defined = array_keys($vars);
00447                 $common  = array_intersect($options, $defined);
00448                 if(empty($common)) {
00449                     return false;
00450                 }
00451             }
00452 
00453             // OK, now simply check if each element has a valid value,
00454             // unsetting them on the way. If at the end the array still
00455             // has some elements, they are illegal.
00456 
00457             if($vars['FREQ'] != 'SECONDLY' && $vars['FREQ'] != 'MINUTELY' && $vars['FREQ'] != 'HOURLY' && 
00458                $vars['FREQ'] != 'DAILY'    && $vars['FREQ'] != 'WEEKLY' &&
00459                $vars['FREQ'] != 'MONTHLY'  && $vars['FREQ'] != 'YEARLY') {
00460                 return false;
00461             }
00462             unset($vars['FREQ']);
00463 
00464             // Set this, we may need it later
00465             $weekdays = explode(',', RFC2445_WEEKDAYS);
00466 
00467             if(isset($vars['UNTIL'])) {
00468                 if(rfc2445_is_valid_value($vars['UNTIL'], RFC2445_TYPE_DATE_TIME)) {
00469                     // The time MUST be in UTC format
00470                     if(!(substr($vars['UNTIL'], -1) == 'Z')) {
00471                         return false;
00472                     }
00473                 }
00474                 else if(!rfc2445_is_valid_value($vars['UNTIL'], RFC2445_TYPE_DATE_TIME)) {
00475                     return false;
00476                 }
00477             }
00478             unset($vars['UNTIL']);
00479 
00480 
00481             if(isset($vars['COUNT'])) {
00482                 if(empty($vars['COUNT'])) {
00483                     // This also catches the string '0', which makes no sense
00484                     return false;
00485                 }
00486                 if(strspn($vars['COUNT'], '0123456789') != strlen($vars['COUNT'])) {
00487                     return false;
00488                 }
00489             }
00490             unset($vars['COUNT']);
00491 
00492             
00493             if(isset($vars['INTERVAL'])) {
00494                 if(empty($vars['INTERVAL'])) {
00495                     // This also catches the string '0', which makes no sense
00496                     return false;
00497                 }
00498                 if(strspn($vars['INTERVAL'], '0123456789') != strlen($vars['INTERVAL'])) {
00499                     return false;
00500                 }
00501             }
00502             unset($vars['INTERVAL']);
00503 
00504             
00505             if(isset($vars['BYSECOND'])) {
00506                 if($vars['BYSECOND'] == '') {
00507                     return false;
00508                 }
00509                 // Comma also allowed
00510                 if(strspn($vars['BYSECOND'], '0123456789,') != strlen($vars['BYSECOND'])) {
00511                     return false;
00512                 }
00513                 $secs = explode(',', $vars['BYSECOND']);
00514                 foreach($secs as $sec) {
00515                     if($sec == '' || $sec < 0 || $sec > 59) {
00516                         return false;
00517                     }
00518                 }
00519             }
00520             unset($vars['BYSECOND']);
00521 
00522             
00523             if(isset($vars['BYMINUTE'])) {
00524                 if($vars['BYMINUTE'] == '') {
00525                     return false;
00526                 }
00527                 // Comma also allowed
00528                 if(strspn($vars['BYMINUTE'], '0123456789,') != strlen($vars['BYMINUTE'])) {
00529                     return false;
00530                 }
00531                 $mins = explode(',', $vars['BYMINUTE']);
00532                 foreach($mins as $min) {
00533                     if($min == '' || $min < 0 || $min > 59) {
00534                         return false;
00535                     }
00536                 }
00537             }
00538             unset($vars['BYMINUTE']);
00539 
00540             
00541             if(isset($vars['BYHOUR'])) {
00542                 if($vars['BYHOUR'] == '') {
00543                     return false;
00544                 }
00545                 // Comma also allowed
00546                 if(strspn($vars['BYHOUR'], '0123456789,') != strlen($vars['BYHOUR'])) {
00547                     return false;
00548                 }
00549                 $hours = explode(',', $vars['BYHOUR']);
00550                 foreach($hours as $hour) {
00551                     if($hour == '' || $hour < 0 || $hour > 23) {
00552                         return false;
00553                     }
00554                 }
00555             }
00556             unset($vars['BYHOUR']);
00557             
00558 
00559             if(isset($vars['BYDAY'])) {
00560                 if(empty($vars['BYDAY'])) {
00561                     return false;
00562                 }
00563 
00564                 // First off, split up all values we may have
00565                 $days = explode(',', $vars['BYDAY']);
00566                 
00567                 foreach($days as $day) {
00568                     $daypart = substr($day, -2);
00569                     if(!in_array($daypart, $weekdays)) {
00570                         return false;
00571                     }
00572 
00573                     if(strlen($day) > 2) {
00574                         $intpart = substr($day, 0, strlen($day) - 2);
00575                         if(!rfc2445_is_valid_value($intpart, RFC2445_TYPE_INTEGER)) {
00576                             return false;
00577                         }
00578                         if(intval($intpart) == 0) {
00579                             return false;
00580                         }
00581                     }
00582                 }
00583             }
00584             unset($vars['BYDAY']);
00585 
00586 
00587             if(isset($vars['BYMONTHDAY'])) {
00588                 if(empty($vars['BYMONTHDAY'])) {
00589                     return false;
00590                 }
00591                 $mdays = explode(',', $vars['BYMONTHDAY']);
00592                 foreach($mdays as $mday) {
00593                     if(!rfc2445_is_valid_value($mday, RFC2445_TYPE_INTEGER)) {
00594                         return false;
00595                     }
00596                     $mday = abs(intval($mday));
00597                     if($mday == 0 || $mday > 31) {
00598                         return false;
00599                     }
00600                 }
00601             }
00602             unset($vars['BYMONTHDAY']);
00603 
00604 
00605             if(isset($vars['BYYEARDAY'])) {
00606                 if(empty($vars['BYYEARDAY'])) {
00607                     return false;
00608                 }
00609                 $ydays = explode(',', $vars['BYYEARDAY']);
00610                 foreach($ydays as $yday) {
00611                     if(!rfc2445_is_valid_value($yday, RFC2445_TYPE_INTEGER)) {
00612                         return false;
00613                     }
00614                     $yday = abs(intval($yday));
00615                     if($yday == 0 || $yday > 366) {
00616                         return false;
00617                     }
00618                 }
00619             }
00620             unset($vars['BYYEARDAY']);
00621 
00622 
00623             if(isset($vars['BYWEEKNO'])) {
00624                 if(empty($vars['BYWEEKNO'])) {
00625                     return false;
00626                 }
00627                 $weeknos = explode(',', $vars['BYWEEKNO']);
00628                 foreach($weeknos as $weekno) {
00629                     if(!rfc2445_is_valid_value($weekno, RFC2445_TYPE_INTEGER)) {
00630                         return false;
00631                     }
00632                     $weekno = abs(intval($weekno));
00633                     if($weekno == 0 || $weekno > 53) {
00634                         return false;
00635                     }
00636                 }
00637             }
00638             unset($vars['BYWEEKNO']);
00639 
00640 
00641             if(isset($vars['BYMONTH'])) {
00642                 if(empty($vars['BYMONTH'])) {
00643                     return false;
00644                 }
00645                 // Comma also allowed
00646                 if(strspn($vars['BYMONTH'], '0123456789,') != strlen($vars['BYMONTH'])) {
00647                     return false;
00648                 }
00649                 $months = explode(',', $vars['BYMONTH']);
00650                 foreach($months as $month) {
00651                     if($month == '' || $month < 1 || $month > 12) {
00652                         return false;
00653                     }
00654                 }
00655             }
00656             unset($vars['BYMONTH']);
00657 
00658 
00659             if(isset($vars['BYSETPOS'])) {
00660                 if(empty($vars['BYSETPOS'])) {
00661                     return false;
00662                 }
00663                 $sets = explode(',', $vars['BYSETPOS']);
00664                 foreach($sets as $set) {
00665                     if(!rfc2445_is_valid_value($set, RFC2445_TYPE_INTEGER)) {
00666                         return false;
00667                     }
00668                     $set = abs(intval($set));
00669                     if($set == 0 || $set > 366) {
00670                         return false;
00671                     }
00672                 }
00673             }
00674             unset($vars['BYSETPOS']);
00675 
00676 
00677             if(isset($vars['WKST'])) {
00678                 if(!in_array($vars['WKST'], $weekdays)) {
00679                     return false;
00680                 }
00681             }
00682             unset($vars['WKST']);
00683 
00684 
00685             // Any remaining vars must be x-names
00686             if(empty($vars)) {
00687                 return true;
00688             }
00689 
00690             foreach($vars as $name => $var) {
00691                 if(!rfc2445_is_xname($name)) {
00692                     return false;
00693                 }
00694             }
00695 
00696             // At last, all is OK!
00697             return true;
00698 
00699         break;
00700 
00701         case RFC2445_TYPE_TEXT:
00702             return true;
00703         break;
00704 
00705         case RFC2445_TYPE_TIME:
00706             if(is_int($value)) {
00707                 if($value < 0) {
00708                     return false;
00709                 }
00710                 $value = "$value";
00711             }
00712             else if(!is_string($value)) {
00713                 return false;
00714             }
00715 
00716             if(strlen($value) == 7) {
00717                 if(strtoupper(substr($value, -1)) != 'Z') {
00718                     return false;
00719                 }
00720                 $value = substr($value, 0, 6);
00721             }
00722             if(strlen($value) != 6) {
00723                 return false;
00724             }
00725 
00726             $h = intval(substr($value, 0, 2));
00727             $m = intval(substr($value, 2, 2));
00728             $s = intval(substr($value, 4, 2));
00729 
00730             return ($h <= 23 && $m <= 59 && $s <= 60);
00731         break;
00732 
00733         case RFC2445_TYPE_UTC_OFFSET:
00734             if(is_int($value)) {
00735                 if($value >= 0) {
00736                     $value = "+$value";
00737                 }
00738                 else {
00739                     $value = "$value";
00740                 }
00741             }
00742             else if(!is_string($value)) {
00743                 return false;
00744             }
00745 
00746             $s = 0;
00747             if(strlen($value) == 7) {
00748                 $s = intval(substr($value, 5, 2));
00749                 $value = substr($value, 0, 5);
00750             }
00751             if(strlen($value) != 5 || $value == "-0000") {
00752                 return false;
00753             }
00754 
00755             if($value{0} != '+' && $value{0} != '-') {
00756                 return false;
00757             }
00758 
00759             $h = intval(substr($value, 1, 2));
00760             $m = intval(substr($value, 3, 2));
00761 
00762             return ($h <= 23 && $m <= 59 && $s <= 59);
00763         break;
00764     }
00765 
00766     // TODO: remove this assertion
00767     trigger_error('bad code path', E_USER_WARNING);
00768     var_dump($type);
00769     return false;
00770 }
00771 
00772 function rfc2445_do_value_formatting($value, $type) {
00773     // Note: this does not only do formatting; it also does conversion to string!
00774     switch($type) {
00775         case RFC2445_TYPE_CAL_ADDRESS:
00776         case RFC2445_TYPE_URI:
00777             // Enclose in double quotes
00778             $value = '"'.$value.'"';
00779         break;
00780         case RFC2445_TYPE_TEXT:
00781             // Escape entities
00782             $value = strtr($value, array("\r\n" => '\\n', "\n" => '\\n', '\\' => '\\\\', ',' => '\\,', ';' => '\\;'));
00783         break;
00784     }
00785     return $value;
00786 }
00787 
00788 function rfc2445_undo_value_formatting($value, $type) {
00789     switch($type) {
00790         case RFC2445_TYPE_CAL_ADDRESS:
00791         case RFC2445_TYPE_URI:
00792             // Trim beginning and end double quote
00793             $value = substr($value, 1, strlen($value) - 2);
00794         break;
00795         case RFC2445_TYPE_TEXT:
00796             // Unescape entities
00797             $value = strtr($value, array('\\n' => "\n", '\\N' => "\n", '\\\\' => '\\', '\\,' => ',', '\\;' => ';'));
00798         break;
00799     }
00800     return $value;
00801 }
 All Data Structures Namespaces Files Functions Variables Enumerations