PageRenderTime 59ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/bennu/iCalendar_rfc2445.php

https://bitbucket.org/ceu/moodle_demo
PHP | 800 lines | 585 code | 135 blank | 80 comment | 235 complexity | 2d7d4c2c0f85c373c957baa13341c6f5 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, LGPL-2.1
  1. <?php // $Id: iCalendar_rfc2445.php,v 1.2.10.3 2009/11/19 10:16:53 skodak Exp $
  2. /**
  3. * BENNU - PHP iCalendar library
  4. * (c) 2005-2006 Ioannis Papaioannou (pj@moodle.org). All rights reserved.
  5. *
  6. * Released under the LGPL.
  7. *
  8. * See http://bennu.sourceforge.net/ for more information and downloads.
  9. *
  10. * @author Ioannis Papaioannou
  11. * @version $Id: iCalendar_rfc2445.php,v 1.2.10.3 2009/11/19 10:16:53 skodak Exp $
  12. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  13. */
  14. /*
  15. All names of properties, property parameters, enumerated property
  16. values and property parameter values are case-insensitive. However,
  17. all other property values are case-sensitive, unless otherwise
  18. stated.
  19. */
  20. define('RFC2445_CRLF', "\r\n");
  21. define('RFC2445_WSP', "\t ");
  22. define('RFC2445_WEEKDAYS', 'MO,TU,WE,TH,FR,SA,SU');
  23. define('RFC2445_FOLDED_LINE_LENGTH', 75);
  24. define('RFC2445_REQUIRED', 0x01);
  25. define('RFC2445_OPTIONAL', 0x02);
  26. define('RFC2445_ONCE', 0x04);
  27. define('RFC2445_PROP_FLAGS', 0);
  28. define('RFC2445_PROP_TYPE', 1);
  29. define('RFC2445_PROP_DEFAULT', 2);
  30. define('RFC2445_XNAME', 'X-');
  31. define('RFC2445_TYPE_BINARY', 0);
  32. define('RFC2445_TYPE_BOOLEAN', 1);
  33. define('RFC2445_TYPE_CAL_ADDRESS', 2);
  34. define('RFC2445_TYPE_DATE', 3);
  35. define('RFC2445_TYPE_DATE_TIME', 4);
  36. define('RFC2445_TYPE_DURATION', 5);
  37. define('RFC2445_TYPE_FLOAT', 6);
  38. define('RFC2445_TYPE_INTEGER', 7);
  39. define('RFC2445_TYPE_PERIOD', 8);
  40. define('RFC2445_TYPE_RECUR', 9);
  41. define('RFC2445_TYPE_TEXT', 10);
  42. define('RFC2445_TYPE_TIME', 11);
  43. define('RFC2445_TYPE_URI', 12); // CAL_ADDRESS === URI
  44. define('RFC2445_TYPE_UTC_OFFSET', 13);
  45. function rfc2445_fold($string) {
  46. if(mb_strlen($string, 'utf-8') <= RFC2445_FOLDED_LINE_LENGTH) {
  47. return $string;
  48. }
  49. $retval = '';
  50. $i=0;
  51. $len_count=0;
  52. //multi-byte string, get the correct length
  53. $section_len = mb_strlen($string, 'utf-8');
  54. while($len_count<$section_len) {
  55. //get the current portion of the line
  56. $section = mb_substr($string, ($i * RFC2445_FOLDED_LINE_LENGTH), (RFC2445_FOLDED_LINE_LENGTH), 'utf-8');
  57. //increment the length we've processed by the length of the new portion
  58. $len_count += mb_strlen($section, 'utf-8');
  59. /* Add the portion to the return value, terminating with CRLF.HTAB
  60. As per RFC 2445, CRLF.HTAB will be replaced by the processor of the
  61. data */
  62. $retval .= $section.RFC2445_CRLF.RFC2445_WSP;
  63. $i++;
  64. }
  65. return $retval;
  66. }
  67. function rfc2445_unfold($string) {
  68. for($i = 0; $i < strlen(RFC2445_WSP); ++$i) {
  69. $string = str_replace(RFC2445_CRLF.substr(RFC2445_WSP, $i, 1), '', $string);
  70. }
  71. return $string;
  72. }
  73. function rfc2445_is_xname($name) {
  74. // If it's less than 3 chars, it cannot be legal
  75. if(strlen($name) < 3) {
  76. return false;
  77. }
  78. // If it contains an illegal char anywhere, reject it
  79. if(strspn($name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-') != strlen($name)) {
  80. return false;
  81. }
  82. // To be legal, it must still start with "X-"
  83. return substr($name, 0, 2) === 'X-';
  84. }
  85. function rfc2445_is_valid_value($value, $type) {
  86. // This branch should only be taken with xname values
  87. if($type === NULL) {
  88. return true;
  89. }
  90. switch($type) {
  91. case RFC2445_TYPE_CAL_ADDRESS:
  92. case RFC2445_TYPE_URI:
  93. if(!is_string($value)) {
  94. return false;
  95. }
  96. $valid_schemes = array('ftp', 'http', 'ldap', 'gopher', 'mailto', 'news', 'nntp', 'telnet', 'wais', 'file', 'prospero');
  97. $pos = strpos($value, ':');
  98. if(!$pos) {
  99. return false;
  100. }
  101. $scheme = strtolower(substr($value, 0, $pos));
  102. $remain = substr($value, $pos + 1);
  103. if(!in_array($scheme, $valid_schemes)) {
  104. return false;
  105. }
  106. if($scheme === 'mailto') {
  107. $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})$';
  108. }
  109. else {
  110. $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})?(/.*)?$';
  111. }
  112. return ereg($regexp, $remain);
  113. break;
  114. case RFC2445_TYPE_BINARY:
  115. if(!is_string($value)) {
  116. return false;
  117. }
  118. $len = strlen($value);
  119. if($len % 4 != 0) {
  120. return false;
  121. }
  122. for($i = 0; $i < $len; ++$i) {
  123. $ch = $value{$i};
  124. if(!($ch >= 'a' && $ch <= 'z' || $ch >= 'A' && $ch <= 'Z' || $ch >= '0' && $ch <= '9' || $ch == '-' || $ch == '+')) {
  125. if($ch == '=' && $len - $i <= 2) {
  126. continue;
  127. }
  128. return false;
  129. }
  130. }
  131. return true;
  132. break;
  133. case RFC2445_TYPE_BOOLEAN:
  134. if(is_bool($value)) {
  135. return true;
  136. }
  137. if(is_string($value)) {
  138. $value = strtoupper($value);
  139. return ($value == 'TRUE' || $value == 'FALSE');
  140. }
  141. return false;
  142. break;
  143. case RFC2445_TYPE_DATE:
  144. if(is_int($value)) {
  145. if($value < 0) {
  146. return false;
  147. }
  148. $value = "$value";
  149. }
  150. else if(!is_string($value)) {
  151. return false;
  152. }
  153. if(strlen($value) != 8) {
  154. return false;
  155. }
  156. $y = intval(substr($value, 0, 4));
  157. $m = intval(substr($value, 4, 2));
  158. $d = intval(substr($value, 6, 2));
  159. return checkdate($m, $d, $y);
  160. break;
  161. case RFC2445_TYPE_DATE_TIME:
  162. if(!is_string($value) || strlen($value) < 15) {
  163. return false;
  164. }
  165. return($value{8} == 'T' &&
  166. rfc2445_is_valid_value(substr($value, 0, 8), RFC2445_TYPE_DATE) &&
  167. rfc2445_is_valid_value(substr($value, 9), RFC2445_TYPE_TIME));
  168. break;
  169. case RFC2445_TYPE_DURATION:
  170. if(!is_string($value)) {
  171. return false;
  172. }
  173. $len = strlen($value);
  174. if($len < 3) {
  175. // Minimum conformant length: "P1W"
  176. return false;
  177. }
  178. if($value{0} == '+' || $value{0} == '-') {
  179. $value = substr($value, 1);
  180. --$len; // Don't forget to update this!
  181. }
  182. if($value{0} != 'P') {
  183. return false;
  184. }
  185. // OK, now break it up
  186. $num = '';
  187. $allowed = 'WDT';
  188. for($i = 1; $i < $len; ++$i) {
  189. $ch = $value{$i};
  190. if($ch >= '0' && $ch <= '9') {
  191. $num .= $ch;
  192. continue;
  193. }
  194. if(strpos($allowed, $ch) === false) {
  195. // Non-numeric character which shouldn't be here
  196. return false;
  197. }
  198. if($num === '' && $ch != 'T') {
  199. // Allowed non-numeric character, but no digits came before it
  200. return false;
  201. }
  202. // OK, $ch now holds a character which tells us what $num is
  203. switch($ch) {
  204. case 'W':
  205. // If duration in weeks is specified, this must end the string
  206. return ($i == $len - 1);
  207. break;
  208. case 'D':
  209. // Days specified, now if anything comes after it must be a 'T'
  210. $allowed = 'T';
  211. break;
  212. case 'T':
  213. // Starting to specify time, H M S are now valid delimiters
  214. $allowed = 'HMS';
  215. break;
  216. case 'H':
  217. $allowed = 'M';
  218. break;
  219. case 'M':
  220. $allowed = 'S';
  221. break;
  222. case 'S':
  223. return ($i == $len - 1);
  224. break;
  225. }
  226. // If we 're going to continue, reset $num
  227. $num = '';
  228. }
  229. // $num is kept for this reason: if we 're here, we ran out of chars
  230. // therefore $num must be empty for the period to be legal
  231. return ($num === '' && $ch != 'T');
  232. break;
  233. case RFC2445_TYPE_FLOAT:
  234. if(is_float($value)) {
  235. return true;
  236. }
  237. if(!is_string($value) || $value === '') {
  238. return false;
  239. }
  240. $dot = false;
  241. $int = false;
  242. $len = strlen($value);
  243. for($i = 0; $i < $len; ++$i) {
  244. switch($value{$i}) {
  245. case '-': case '+':
  246. // A sign can only be seen at position 0 and cannot be the only char
  247. if($i != 0 || $len == 1) {
  248. return false;
  249. }
  250. break;
  251. case '.':
  252. // A second dot is an error
  253. // Make sure we had at least one int before the dot
  254. if($dot || !$int) {
  255. return false;
  256. }
  257. $dot = true;
  258. // Make also sure that the float doesn't end with a dot
  259. if($i == $len - 1) {
  260. return false;
  261. }
  262. break;
  263. case '0': case '1': case '2': case '3': case '4':
  264. case '5': case '6': case '7': case '8': case '9':
  265. $int = true;
  266. break;
  267. default:
  268. // Any other char is a no-no
  269. return false;
  270. break;
  271. }
  272. }
  273. return true;
  274. break;
  275. case RFC2445_TYPE_INTEGER:
  276. if(is_int($value)) {
  277. return true;
  278. }
  279. if(!is_string($value) || $value === '') {
  280. return false;
  281. }
  282. if($value{0} == '+' || $value{0} == '-') {
  283. if(strlen($value) == 1) {
  284. return false;
  285. }
  286. $value = substr($value, 1);
  287. }
  288. if(strspn($value, '0123456789') != strlen($value)) {
  289. return false;
  290. }
  291. return ($value >= -2147483648 && $value <= 2147483647);
  292. break;
  293. case RFC2445_TYPE_PERIOD:
  294. if(!is_string($value) || empty($value)) {
  295. return false;
  296. }
  297. $parts = explode('/', $value);
  298. if(count($parts) != 2) {
  299. return false;
  300. }
  301. if(!rfc2445_is_valid_value($parts[0], RFC2445_TYPE_DATE_TIME)) {
  302. return false;
  303. }
  304. // Two legal cases for the second part:
  305. if(rfc2445_is_valid_value($parts[1], RFC2445_TYPE_DATE_TIME)) {
  306. // It has to be after the start time, so
  307. return ($parts[1] > $parts[0]);
  308. }
  309. else if(rfc2445_is_valid_value($parts[1], RFC2445_TYPE_DURATION)) {
  310. // The period MUST NOT be negative
  311. return ($parts[1]{0} != '-');
  312. }
  313. // It seems to be illegal
  314. return false;
  315. break;
  316. case RFC2445_TYPE_RECUR:
  317. if(!is_string($value)) {
  318. return false;
  319. }
  320. $parts = explode(';', strtoupper($value));
  321. // First of all, we need at least a FREQ and a UNTIL or COUNT part, so...
  322. if(count($parts) < 2) {
  323. return false;
  324. }
  325. // Let's get that into a more easily comprehensible format
  326. $vars = array();
  327. foreach($parts as $part) {
  328. $pieces = explode('=', $part);
  329. // There must be exactly 2 pieces, e.g. FREQ=WEEKLY
  330. if(count($pieces) != 2) {
  331. return false;
  332. }
  333. // It's illegal for a variable to appear twice
  334. if(isset($vars[$pieces[0]])) {
  335. return false;
  336. }
  337. // Sounds good
  338. $vars[$pieces[0]] = $pieces[1];
  339. }
  340. // OK... now to test everything else
  341. // FREQ must be the first thing appearing
  342. reset($vars);
  343. if(key($vars) != 'FREQ') {
  344. return false;
  345. }
  346. // It's illegal to have both UNTIL and COUNT appear
  347. if(isset($vars['UNTIL']) && isset($vars['COUNT'])) {
  348. return false;
  349. }
  350. // Special case: BYWEEKNO is only valid for FREQ=YEARLY
  351. if(isset($vars['BYWEEKNO']) && $vars['FREQ'] != 'YEARLY') {
  352. return false;
  353. }
  354. // Special case: BYSETPOS is only valid if another BY option is specified
  355. if(isset($vars['BYSETPOS'])) {
  356. $options = array('BYSECOND', 'BYMINUTE', 'BYHOUR', 'BYDAY', 'BYMONTHDAY', 'BYYEARDAY', 'BYWEEKNO', 'BYMONTH');
  357. $defined = array_keys($vars);
  358. $common = array_intersect($options, $defined);
  359. if(empty($common)) {
  360. return false;
  361. }
  362. }
  363. // OK, now simply check if each element has a valid value,
  364. // unsetting them on the way. If at the end the array still
  365. // has some elements, they are illegal.
  366. if($vars['FREQ'] != 'SECONDLY' && $vars['FREQ'] != 'MINUTELY' && $vars['FREQ'] != 'HOURLY' &&
  367. $vars['FREQ'] != 'DAILY' && $vars['FREQ'] != 'WEEKLY' &&
  368. $vars['FREQ'] != 'MONTHLY' && $vars['FREQ'] != 'YEARLY') {
  369. return false;
  370. }
  371. unset($vars['FREQ']);
  372. // Set this, we may need it later
  373. $weekdays = explode(',', RFC2445_WEEKDAYS);
  374. if(isset($vars['UNTIL'])) {
  375. if(rfc2445_is_valid_value($vars['UNTIL'], RFC2445_TYPE_DATE_TIME)) {
  376. // The time MUST be in UTC format
  377. if(!(substr($vars['UNTIL'], -1) == 'Z')) {
  378. return false;
  379. }
  380. }
  381. else if(!rfc2445_is_valid_value($vars['UNTIL'], RFC2445_TYPE_DATE_TIME)) {
  382. return false;
  383. }
  384. }
  385. unset($vars['UNTIL']);
  386. if(isset($vars['COUNT'])) {
  387. if(empty($vars['COUNT'])) {
  388. // This also catches the string '0', which makes no sense
  389. return false;
  390. }
  391. if(strspn($vars['COUNT'], '0123456789') != strlen($vars['COUNT'])) {
  392. return false;
  393. }
  394. }
  395. unset($vars['COUNT']);
  396. if(isset($vars['INTERVAL'])) {
  397. if(empty($vars['INTERVAL'])) {
  398. // This also catches the string '0', which makes no sense
  399. return false;
  400. }
  401. if(strspn($vars['INTERVAL'], '0123456789') != strlen($vars['INTERVAL'])) {
  402. return false;
  403. }
  404. }
  405. unset($vars['INTERVAL']);
  406. if(isset($vars['BYSECOND'])) {
  407. if($vars['BYSECOND'] == '') {
  408. return false;
  409. }
  410. // Comma also allowed
  411. if(strspn($vars['BYSECOND'], '0123456789,') != strlen($vars['BYSECOND'])) {
  412. return false;
  413. }
  414. $secs = explode(',', $vars['BYSECOND']);
  415. foreach($secs as $sec) {
  416. if($sec == '' || $sec < 0 || $sec > 59) {
  417. return false;
  418. }
  419. }
  420. }
  421. unset($vars['BYSECOND']);
  422. if(isset($vars['BYMINUTE'])) {
  423. if($vars['BYMINUTE'] == '') {
  424. return false;
  425. }
  426. // Comma also allowed
  427. if(strspn($vars['BYMINUTE'], '0123456789,') != strlen($vars['BYMINUTE'])) {
  428. return false;
  429. }
  430. $mins = explode(',', $vars['BYMINUTE']);
  431. foreach($mins as $min) {
  432. if($min == '' || $min < 0 || $min > 59) {
  433. return false;
  434. }
  435. }
  436. }
  437. unset($vars['BYMINUTE']);
  438. if(isset($vars['BYHOUR'])) {
  439. if($vars['BYHOUR'] == '') {
  440. return false;
  441. }
  442. // Comma also allowed
  443. if(strspn($vars['BYHOUR'], '0123456789,') != strlen($vars['BYHOUR'])) {
  444. return false;
  445. }
  446. $hours = explode(',', $vars['BYHOUR']);
  447. foreach($hours as $hour) {
  448. if($hour == '' || $hour < 0 || $hour > 23) {
  449. return false;
  450. }
  451. }
  452. }
  453. unset($vars['BYHOUR']);
  454. if(isset($vars['BYDAY'])) {
  455. if(empty($vars['BYDAY'])) {
  456. return false;
  457. }
  458. // First off, split up all values we may have
  459. $days = explode(',', $vars['BYDAY']);
  460. foreach($days as $day) {
  461. $daypart = substr($day, -2);
  462. if(!in_array($daypart, $weekdays)) {
  463. return false;
  464. }
  465. if(strlen($day) > 2) {
  466. $intpart = substr($day, 0, strlen($day) - 2);
  467. if(!rfc2445_is_valid_value($intpart, RFC2445_TYPE_INTEGER)) {
  468. return false;
  469. }
  470. if(intval($intpart) == 0) {
  471. return false;
  472. }
  473. }
  474. }
  475. }
  476. unset($vars['BYDAY']);
  477. if(isset($vars['BYMONTHDAY'])) {
  478. if(empty($vars['BYMONTHDAY'])) {
  479. return false;
  480. }
  481. $mdays = explode(',', $vars['BYMONTHDAY']);
  482. foreach($mdays as $mday) {
  483. if(!rfc2445_is_valid_value($mday, RFC2445_TYPE_INTEGER)) {
  484. return false;
  485. }
  486. $mday = abs(intval($mday));
  487. if($mday == 0 || $mday > 31) {
  488. return false;
  489. }
  490. }
  491. }
  492. unset($vars['BYMONTHDAY']);
  493. if(isset($vars['BYYEARDAY'])) {
  494. if(empty($vars['BYYEARDAY'])) {
  495. return false;
  496. }
  497. $ydays = explode(',', $vars['BYYEARDAY']);
  498. foreach($ydays as $yday) {
  499. if(!rfc2445_is_valid_value($yday, RFC2445_TYPE_INTEGER)) {
  500. return false;
  501. }
  502. $yday = abs(intval($yday));
  503. if($yday == 0 || $yday > 366) {
  504. return false;
  505. }
  506. }
  507. }
  508. unset($vars['BYYEARDAY']);
  509. if(isset($vars['BYWEEKNO'])) {
  510. if(empty($vars['BYWEEKNO'])) {
  511. return false;
  512. }
  513. $weeknos = explode(',', $vars['BYWEEKNO']);
  514. foreach($weeknos as $weekno) {
  515. if(!rfc2445_is_valid_value($weekno, RFC2445_TYPE_INTEGER)) {
  516. return false;
  517. }
  518. $weekno = abs(intval($weekno));
  519. if($weekno == 0 || $weekno > 53) {
  520. return false;
  521. }
  522. }
  523. }
  524. unset($vars['BYWEEKNO']);
  525. if(isset($vars['BYMONTH'])) {
  526. if(empty($vars['BYMONTH'])) {
  527. return false;
  528. }
  529. // Comma also allowed
  530. if(strspn($vars['BYMONTH'], '0123456789,') != strlen($vars['BYMONTH'])) {
  531. return false;
  532. }
  533. $months = explode(',', $vars['BYMONTH']);
  534. foreach($months as $month) {
  535. if($month == '' || $month < 1 || $month > 12) {
  536. return false;
  537. }
  538. }
  539. }
  540. unset($vars['BYMONTH']);
  541. if(isset($vars['BYSETPOS'])) {
  542. if(empty($vars['BYSETPOS'])) {
  543. return false;
  544. }
  545. $sets = explode(',', $vars['BYSETPOS']);
  546. foreach($sets as $set) {
  547. if(!rfc2445_is_valid_value($set, RFC2445_TYPE_INTEGER)) {
  548. return false;
  549. }
  550. $set = abs(intval($set));
  551. if($set == 0 || $set > 366) {
  552. return false;
  553. }
  554. }
  555. }
  556. unset($vars['BYSETPOS']);
  557. if(isset($vars['WKST'])) {
  558. if(!in_array($vars['WKST'], $weekdays)) {
  559. return false;
  560. }
  561. }
  562. unset($vars['WKST']);
  563. // Any remaining vars must be x-names
  564. if(empty($vars)) {
  565. return true;
  566. }
  567. foreach($vars as $name => $var) {
  568. if(!rfc2445_is_xname($name)) {
  569. return false;
  570. }
  571. }
  572. // At last, all is OK!
  573. return true;
  574. break;
  575. case RFC2445_TYPE_TEXT:
  576. return true;
  577. break;
  578. case RFC2445_TYPE_TIME:
  579. if(is_int($value)) {
  580. if($value < 0) {
  581. return false;
  582. }
  583. $value = "$value";
  584. }
  585. else if(!is_string($value)) {
  586. return false;
  587. }
  588. if(strlen($value) == 7) {
  589. if(strtoupper(substr($value, -1)) != 'Z') {
  590. return false;
  591. }
  592. $value = substr($value, 0, 6);
  593. }
  594. if(strlen($value) != 6) {
  595. return false;
  596. }
  597. $h = intval(substr($value, 0, 2));
  598. $m = intval(substr($value, 2, 2));
  599. $s = intval(substr($value, 4, 2));
  600. return ($h <= 23 && $m <= 59 && $s <= 60);
  601. break;
  602. case RFC2445_TYPE_UTC_OFFSET:
  603. if(is_int($value)) {
  604. if($value >= 0) {
  605. $value = "+$value";
  606. }
  607. else {
  608. $value = "$value";
  609. }
  610. }
  611. else if(!is_string($value)) {
  612. return false;
  613. }
  614. if(strlen($value) == 7) {
  615. $s = intval(substr($value, 5, 2));
  616. $value = substr($value, 0, 5);
  617. }
  618. if(strlen($value) != 5 || $value == "-0000") {
  619. return false;
  620. }
  621. if($value{0} != '+' && $value{0} != '-') {
  622. return false;
  623. }
  624. $h = intval(substr($value, 1, 2));
  625. $m = intval(substr($value, 3, 2));
  626. return ($h <= 23 && $m <= 59 && $s <= 59);
  627. break;
  628. }
  629. // TODO: remove this assertion
  630. trigger_error('bad code path', E_USER_WARNING);
  631. var_dump($type);
  632. return false;
  633. }
  634. function rfc2445_do_value_formatting($value, $type) {
  635. // Note: this does not only do formatting; it also does conversion to string!
  636. switch($type) {
  637. case RFC2445_TYPE_CAL_ADDRESS:
  638. case RFC2445_TYPE_URI:
  639. // Enclose in double quotes
  640. $value = '"'.$value.'"';
  641. break;
  642. case RFC2445_TYPE_TEXT:
  643. // Escape entities
  644. $value = strtr($value, array("\r\n" => '\\n', "\n" => '\\n', '\\' => '\\\\', ',' => '\\,', ';' => '\\;'));
  645. break;
  646. }
  647. return $value;
  648. }
  649. function rfc2445_undo_value_formatting($value, $type) {
  650. switch($type) {
  651. case RFC2445_TYPE_CAL_ADDRESS:
  652. case RFC2445_TYPE_URI:
  653. // Trim beginning and end double quote
  654. $value = substr($value, 1, strlen($value) - 2);
  655. break;
  656. case RFC2445_TYPE_TEXT:
  657. // Unescape entities
  658. $value = strtr($value, array('\\n' => "\n", '\\N' => "\n", '\\\\' => '\\', '\\,' => ',', '\\;' => ';'));
  659. break;
  660. }
  661. return $value;
  662. }
  663. ?>