268 public function parse($uri, $usecachefile =
'', $delaycache = 3600)
270 $this->cal = array();
272 $this->event_count = -1;
273 $this->file_text =
'';
275 $stalecachetext =
'';
280 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
284 if ($datefile && $datefile > ($now - $delaycache)) {
286 $this->file_text = file_get_contents($usecachefile);
288 $this->file_text =
'';
290 } elseif ($datefile) {
292 $oldtext = (string) file_get_contents($usecachefile);
294 $stalecachetext = $oldtext;
300 if (empty($this->file_text)) {
302 $failfile = ($usecachefile ? $usecachefile.
'.fail' :
'');
304 $this->error = (string) file_get_contents($failfile);
305 $this->file_text =
null;
307 $this->file_text = $this->
read_file($uri);
308 if (!is_null($this->file_text) && !$this->
isIcalText($this->file_text)) {
310 $this->error =
'Error: the server did not return an iCal file (received: '.dol_trunc(trim((
string) preg_replace(
'/\s+/',
' ',
dol_string_nohtmltag($this->file_text))), 100).
')';
311 $this->file_text =
null;
314 if (is_null($this->file_text)) {
315 file_put_contents($failfile, $this->error, LOCK_EX);
323 if ($usecachefile && !is_null($this->file_text)) {
326 $tmpcachefile = $usecachefile.
'.'.getmypid().
'.tmp';
327 if (file_put_contents($tmpcachefile, $this->file_text, LOCK_EX) !==
false) {
329 rename($tmpcachefile, $usecachefile);
334 if (is_null($this->file_text) && $stalecachetext !==
'') {
336 $this->file_text = $stalecachetext;
337 $this->error .=
' (the copy downloaded on '.dol_print_date((
int) $datefile,
'dayhour',
'gmt').
' UTC is used)';
340 $file_text_array = preg_split(
"[\n]", ltrim((
string) $this->file_text,
"\xEF\xBB\xBF \t\r\n"));
343 if (!stristr($file_text_array[0],
'BEGIN:VCALENDAR')) {
344 return 'error not VCALENDAR';
351 foreach ($file_text_array as $text) {
360 $this->todo_count += 1;
365 $this->event_count += 1;
369 case "BEGIN:VFREEBUSY":
370 $this->freebusy_count += 1;
374 case "BEGIN:VJOURNAL":
375 $this->journal_count += 1;
379 case "BEGIN:VCALENDAR":
380 case "BEGIN:DAYLIGHT":
381 case "BEGIN:VTIMEZONE":
382 case "BEGIN:STANDARD":
388 case "END:VFREEBUSY":
390 case "END:VCALENDAR":
392 case "END:VTIMEZONE":
408 if (!preg_match(
'/=$/', $text)) {
410 $value =
quotedPrintDecode(preg_replace(
'/^ENCODING=QUOTED-PRINTABLE:/i',
'', $tmpvalue));
414 } elseif (preg_match(
'/^ENCODING=QUOTED-PRINTABLE:/i', $value)) {
415 if (preg_match(
'/=$/', $value)) {
417 $tmpvalue .= preg_replace(
'/=$/',
"", $value);
419 $value =
quotedPrintDecode(preg_replace(
'/^ENCODING=QUOTED-PRINTABLE:/i',
'', $tmpvalue.$value));
422 if (!$insidealarm && !$tmpkey) {
431 if (!empty($events)) {
436 foreach ($events as $evkey => $event) {
437 if (empty($event[
'UID'])) {
440 $eventstart = ($event[
'DTSTART'] ?? ($event[
'DTSTART;VALUE=DATE'] ??
''));
442 $duplicatekey = $event[
'UID'].
'|'.(empty($event[
'RECURRENCE-ID']) ? json_encode($eventstart) :
'RECURRENCE-ID='.$event[
'RECURRENCE-ID']);
443 $rank = [(int) ($event[
'SEQUENCE'] ?? 0), (int) ($event[
'LAST-MODIFIED'] ?? ($event[
'DTSTAMP'] ?? 0))];
444 if (isset($keptevents[$duplicatekey])) {
445 if ($rank >= $keptevents[$duplicatekey][
'rank']) {
446 unset($events[$keptevents[$duplicatekey][
'key']]);
447 $keptevents[$duplicatekey] = [
'key' => $evkey,
'rank' => $rank];
449 unset($events[$evkey]);
452 $keptevents[$duplicatekey] = [
'key' => $evkey,
'rank' => $rank];
459 foreach ($events as $evkey => $event) {
460 if (empty($event[
'RECURRENCE-ID']) && !empty($event[
'UID'])) {
461 $mainkeys[$event[
'UID']] = $evkey;
464 foreach ($events as $evkeyoverride => $event) {
465 if (!empty($event[
'RECURRENCE-ID']) && !empty($event[
'UID']) && isset($mainkeys[$event[
'UID']])) {
466 $mainkey = $mainkeys[$event[
'UID']];
467 if (($event[
'X-RECURRENCE-RANGE'] ??
'') ==
'THISANDFUTURE' && !empty($events[$mainkey][
'RRULE']) && is_array($events[$mainkey][
'RRULE'])) {
470 $rule = $events[$mainkey][
'RRULE'];
471 $daystamp = gmmktime(0, 0, 0, (
int) substr($event[
'RECURRENCE-ID'], 4, 2), (
int) substr($event[
'RECURRENCE-ID'], 6, 2), (
int) substr($event[
'RECURRENCE-ID'], 0, 4));
473 unset($newrule[
'UNTIL']);
474 if (!empty($rule[
'COUNT'])) {
477 unset($newrule[
'COUNT']);
479 $remaining = (int) $rule[
'COUNT'] - count($this->
expandRRule($rule, $mainstart, $daystamp));
480 if ($remaining > 0) {
481 $newrule[
'COUNT'] = (string) $remaining;
485 if (!empty($rule[
'UNTIL'])) {
486 $newrule[
'UNTIL'] = $rule[
'UNTIL'];
488 unset($events[$mainkey][
'RRULE'][
'COUNT']);
489 $events[$mainkey][
'RRULE'][
'UNTIL'] = gmdate(
'Ymd', $daystamp - 86400).
'T235959Z';
490 if (empty($events[$evkeyoverride][
'RRULE'])) {
491 $events[$evkeyoverride][
'RRULE'] = $newrule;
493 unset($events[$evkeyoverride][
'RECURRENCE-ID'], $events[$evkeyoverride][
'X-RECURRENCE-RANGE']);
496 $exdates = (isset($events[$mainkey][
'EXDATE']) && is_array($events[$mainkey][
'EXDATE'])) ? $events[$mainkey][
'EXDATE'] : [];
497 $exdates[] = $event[
'RECURRENCE-ID'];
498 $events[$mainkey][
'EXDATE'] = $exdates;
502 foreach ($events as $evkey => $event) {
503 if (empty($event[
'DURATION']) || !is_string($event[
'DURATION']) || isset($event[
'DTEND']) || isset($event[
'DTEND;VALUE=DATE'])) {
507 if ($seconds ===
null) {
510 if (!empty($event[
'DTSTART;VALUE=DATE']) && is_string($event[
'DTSTART;VALUE=DATE'])) {
512 $start = gmmktime(0, 0, 0, (
int) substr($event[
'DTSTART;VALUE=DATE'], 4, 2), (
int) substr($event[
'DTSTART;VALUE=DATE'], 6, 2), (
int) substr($event[
'DTSTART;VALUE=DATE'], 0, 4));
513 $events[$evkey][
'DTEND;VALUE=DATE'] = gmdate(
'Ymd', $start + max(1, (
int) ceil($seconds / 86400)) * 86400);
514 } elseif (isset($event[
'DTSTART']) && is_array($event[
'DTSTART']) && isset($event[
'DTSTART'][
'unixtime'])) {
515 $events[$evkey][
'DTEND'] = array(
'TZID' => ($event[
'DTSTART'][
'TZID'] ??
''),
'unixtime' => (
int) $event[
'DTSTART'][
'unixtime'] + $seconds);
516 } elseif (!empty($event[
'DTSTART']) && !is_array($event[
'DTSTART'])) {
517 $events[$evkey][
'DTEND'] = (int) $event[
'DTSTART'] + $seconds;
521 foreach ($events as $evkey => $event) {
522 if (isset($event[
'STATUS']) && is_string($event[
'STATUS']) && strtoupper(trim($event[
'STATUS'])) ==
'CANCELLED') {
523 unset($events[$evkey]);
526 $this->cal[
'VEVENT'] = $events;
549 if ($type ==
'VEVENT' && !empty($key) && strtoupper(explode(
';', $key, 2)[0]) ==
'RDATE') {
550 $rdates = (isset($this->cal[$type][$this->event_count][
'RDATE']) && is_array($this->cal[$type][$this->event_count][
'RDATE'])) ? $this->cal[$type][$this->event_count][
'RDATE'] : [];
551 foreach (explode(
',', $value) as $rdate) {
552 $rdate = trim(explode(
'/', $rdate)[0]);
553 if (preg_match(
'/^[0-9]{8}(T[0-9]{6}Z?)?$/', $rdate)) {
557 $this->cal[$type][$this->event_count][
'RDATE'] = $rdates;
558 $this->last_key =
'RDATE';
563 if ($type ==
'VEVENT' && !empty($key) && strtoupper(explode(
';', $key, 2)[0]) ==
'EXDATE') {
564 $exdates = (isset($this->cal[$type][$this->event_count][
'EXDATE']) && is_array($this->cal[$type][$this->event_count][
'EXDATE'])) ? $this->cal[$type][$this->event_count][
'EXDATE'] : [];
565 foreach (explode(
',', $value) as $exdate) {
566 if (preg_match(
'/^\s*([0-9]{8})/', $exdate, $reg)) {
567 $exdates[] = $reg[1];
570 $this->cal[$type][$this->event_count][
'EXDATE'] = $exdates;
571 $this->last_key =
'EXDATE';
575 $iscontinuation =
false;
577 $iscontinuation =
true;
578 $key = $this->last_key;
582 $value = $this->cal[$type][$this->event_count][$key].$value;
586 $value = $this->cal[$type][$this->freebusy_count][$key].$value;
590 $value = $this->cal[$type][$this->todo_count][$key].$value;
594 $value = $this->cal[$type][$this->journal_count][$key].$value;
599 if (!$iscontinuation && $type ==
'VEVENT' && preg_match(
'/^(ATTENDEE|ORGANIZER)(;|$)/i', $key, $reg)) {
601 $name = (preg_match(
'/;CN=("[^"]*"|[^;]*)/i', $key, $regcn) ? trim($regcn[1],
'"') :
'');
602 $email = (string) preg_replace(
'/^mailto:/i',
'', trim($value));
603 $person = (($name !==
'' && $name != $email) ? $name.
' <'.$email.
'>' : $email);
604 if (strtoupper($reg[1]) ==
'ATTENDEE') {
605 $attendees = (isset($this->cal[$type][$this->event_count][
'ATTENDEE']) && is_array($this->cal[$type][$this->event_count][
'ATTENDEE'])) ? $this->cal[$type][$this->event_count][
'ATTENDEE'] : [];
606 $attendees[] = $person;
607 $this->cal[$type][$this->event_count][
'ATTENDEE'] = $attendees;
608 $this->last_key =
'ATTENDEE';
615 if (!$iscontinuation) {
617 if ($type ==
'VEVENT' && preg_match(
'/^RECURRENCE-ID;.*RANGE=THISANDFUTURE/i', $key)) {
618 $this->cal[$type][$this->event_count][
'X-RECURRENCE-RANGE'] =
'THISANDFUTURE';
621 if (strpos($key,
';') !==
false && !stristr($key,
"DTSTART") && !stristr($key,
"DTEND")) {
622 if (preg_match(
'/;ENCODING=(QUOTED-PRINTABLE|Q)(;|$)/i', $key)) {
625 if ($value !==
'' && function_exists(
'mb_check_encoding') && !mb_check_encoding($value,
'UTF-8')) {
626 $value = $this->
convertToUtf8($value, (preg_match(
'/;CHARSET=([^;]+)/i', $key, $reg) ?
'charset='.trim($reg[1],
'"') :
''));
630 if (strpos($key,
';') !==
false && !stristr($key,
"DTSTART") && !stristr($key,
"DTEND") && !stristr($key,
"DUE")) {
631 $key = explode(
';', $key, 2)[0];
634 if (in_array($key, [
'SUMMARY',
'DESCRIPTION',
'LOCATION',
'COMMENT',
'CATEGORIES'])) {
635 $value = strtr($value, [
'\\\\' =>
'\\',
'\\,' =>
',',
'\\;' =>
';',
'\\n' =>
"\n",
'\\N' =>
"\n"]);
639 if (($key ==
"DTSTAMP") || ($key ==
"LAST-MODIFIED") || ($key ==
"CREATED")) {
644 if (preg_match(
'/^(DTSTART|DTEND|DUE)(;|$)/i', $key, $reg)) {
646 if (isset($params[
'VALUE']) && strtoupper($params[
'VALUE']) ==
'DATE') {
648 $key = strtoupper($reg[1]).
';VALUE=DATE';
658 $this->cal[$type][$this->todo_count][$key] = $value;
663 $this->cal[$type][$this->journal_count][$key] = $value;
667 if (strtoupper(explode(
';', $key, 2)[0]) ==
'RECURRENCE-ID') {
669 $key =
'RECURRENCE-ID';
670 if (preg_match(
'/^\s*([0-9]{8})/', $value, $reg)) {
675 $this->cal[$type][$this->event_count][$key] = ($key ==
'RRULE' ? $this->
ical_rrule($value) : $value);
680 $this->cal[$type][$this->freebusy_count][$key] = $value;
685 $this->cal[$type][$key] = $value;
688 $this->last_key = $key;
849 $nbdays = (int) gmdate(
't', gmmktime(0, 0, 0, $month, 1, $year));
850 $fromdayofmonth = array();
851 foreach ($bymonthday as $monthday) {
852 $day = ($monthday > 0 ? $monthday : $nbdays + $monthday + 1);
853 if ($day >= 1 && $day <= $nbdays) {
854 $fromdayofmonth[$day] = $day;
857 $fromdayofweek = array();
858 foreach ($byday as $item) {
860 for ($day = 1; $day <= $nbdays; $day++) {
861 if ((
int) gmdate(
'w', gmmktime(0, 0, 0, $month, $day, $year)) == $item[1]) {
866 foreach ($matches as $day) {
867 $fromdayofweek[$day] = $day;
870 $index = ($item[0] > 0 ? $item[0] - 1 : count($matches) + $item[0]);
871 if (isset($matches[$index])) {
872 $fromdayofweek[$matches[$index]] = $matches[$index];
876 if (!empty($bymonthday) && !empty($byday)) {
877 $days = array_intersect($fromdayofmonth, $fromdayofweek);
878 } elseif (!empty($bymonthday)) {
879 $days = $fromdayofmonth;
880 } elseif (!empty($byday)) {
881 $days = $fromdayofweek;
883 $days = ($defaultday <= $nbdays ? array($defaultday => $defaultday) : array());
887 foreach ($days as $day) {
888 $result[] = gmmktime(0, 0, 0, $month, $day, $year);
971 $daysofweek = array(
'SU' => 0,
'MO' => 1,
'TU' => 2,
'WE' => 3,
'TH' => 4,
'FR' => 5,
'SA' => 6);
972 $freq = ($rrule[
'FREQ'] ??
'');
973 $interval = max(1, (
int) ($rrule[
'INTERVAL'] ?? 1));
974 $maxcount = (int) ($rrule[
'COUNT'] ?? 0);
975 $wkst = $daysofweek[strtoupper($rrule[
'WKST'] ??
'MO')] ?? 1;
976 $byday = (isset($rrule[
'BYDAY']) ? (array) $this->
parseByDay($rrule[
'BYDAY']) : array());
977 $bymonthday = (isset($rrule[
'BYMONTHDAY']) ? array_map(
'intval', explode(
',', $rrule[
'BYMONTHDAY'])) : array());
978 $bymonth = (isset($rrule[
'BYMONTH']) ? array_map(
'intval', explode(
',', $rrule[
'BYMONTH'])) : array());
979 $byyearday = (isset($rrule[
'BYYEARDAY']) ? array_map(
'intval', explode(
',', $rrule[
'BYYEARDAY'])) : array());
980 $byweekno = (isset($rrule[
'BYWEEKNO']) ? array_map(
'intval', explode(
',', $rrule[
'BYWEEKNO'])) : array());
981 $bysetpos = (isset($rrule[
'BYSETPOS']) ? array_map(
'intval', explode(
',', $rrule[
'BYSETPOS'])) : array());
982 $byhour = (isset($rrule[
'BYHOUR']) ? array_map(
'intval', explode(
',', $rrule[
'BYHOUR'])) : array());
983 $byminute = (isset($rrule[
'BYMINUTE']) ? array_map(
'intval', explode(
',', $rrule[
'BYMINUTE'])) : array());
984 $bysecond = (isset($rrule[
'BYSECOND']) ? array_map(
'intval', explode(
',', $rrule[
'BYSECOND'])) : array());
986 $timeofday = (($dtstart % 86400) + 86400) % 86400;
987 $firstday = $dtstart - $timeofday;
988 $year0 = (int) gmdate(
'Y', $firstday);
989 $month0 = (int) gmdate(
'n', $firstday);
990 $day0 = (int) gmdate(
'j', $firstday);
991 $dayofweek0 = (int) gmdate(
'w', $firstday);
992 $hour0 = intdiv($timeofday, 3600);
993 $minute0 = intdiv($timeofday % 3600, 60);
994 $second0 = $timeofday % 60;
997 foreach ((empty($byhour) ? array($hour0) : $byhour) as $hour) {
998 foreach ((empty($byminute) ? array($minute0) : $byminute) as $minute) {
999 foreach ((empty($bysecond) ? array($second0) : $bysecond) as $second) {
1000 $times[] = $hour * 3600 + $minute * 60 + $second;
1004 $times = array_values(array_unique($times));
1006 $skip = ($maxcount == 0 && $from > $dtstart);
1010 for ($i = 0; $i < 20000; $i++) {
1011 $candidates = array();
1012 if ($freq ==
'MINUTELY' || $freq ==
'HOURLY') {
1013 $step = ($freq ==
'HOURLY' ? 3600 : 60) * $interval;
1014 $index = $i + ($skip ? max(0, intdiv($from - $dtstart, $step) - 1) : 0);
1015 $time = $dtstart + $index * $step;
1016 $periodstart = $time;
1017 $day = $time - ((($time % 86400) + 86400) % 86400);
1018 $ok = (empty($bymonth) || in_array((
int) gmdate(
'n', $day), $bymonth));
1019 if ($ok && !empty($bymonthday)) {
1020 $nbdays = (int) gmdate(
't', $day);
1021 $dayofmonth = (int) gmdate(
'j', $day);
1022 $ok = (in_array($dayofmonth, $bymonthday) || in_array($dayofmonth - $nbdays - 1, $bymonthday));
1024 if ($ok && !empty($byday)) {
1026 foreach ($byday as $item) {
1027 if ($item[1] == (
int) gmdate(
'w', $day)) {
1032 if ($ok && !empty($byhour)) {
1033 $ok = in_array((
int) gmdate(
'G', $time), $byhour);
1036 $candidates[] = $time;
1040 if ($freq ==
'DAILY') {
1041 $index = $i + ($skip ? max(0, intdiv($from - $firstday, $interval * 86400) - 1) : 0);
1042 $day = $firstday + $index * $interval * 86400;
1043 $periodstart = $day;
1044 $ok = (empty($bymonth) || in_array((
int) gmdate(
'n', $day), $bymonth));
1045 if ($ok && !empty($bymonthday)) {
1046 $nbdays = (int) gmdate(
't', $day);
1047 $dayofmonth = (int) gmdate(
'j', $day);
1048 $ok = (in_array($dayofmonth, $bymonthday) || in_array($dayofmonth - $nbdays - 1, $bymonthday));
1050 if ($ok && !empty($byday)) {
1052 foreach ($byday as $item) {
1053 if ($item[1] == (
int) gmdate(
'w', $day)) {
1061 } elseif ($freq ==
'WEEKLY') {
1062 $weekstart0 = $firstday - ((($dayofweek0 - $wkst + 7) % 7) * 86400);
1063 $index = $i + ($skip ? max(0, intdiv($from - $weekstart0, $interval * 7 * 86400) - 1) : 0);
1064 $periodstart = $weekstart0 + $index * $interval * 7 * 86400;
1065 $weekdays = array();
1066 foreach ($byday as $item) {
1067 $weekdays[] = $item[1];
1069 if (empty($weekdays)) {
1070 $weekdays[] = $dayofweek0;
1072 foreach ($weekdays as $weekday) {
1073 $day = $periodstart + ((($weekday - $wkst + 7) % 7) * 86400);
1074 if (empty($bymonth) || in_array((
int) gmdate(
'n', $day), $bymonth)) {
1078 } elseif ($freq ==
'MONTHLY') {
1079 $monthindex = $month0 - 1 + $i * $interval;
1081 $monthsfrom = ((int) gmdate(
'Y', $from) - $year0) * 12 + ((
int) gmdate(
'n', $from)) - $month0;
1082 $monthindex += max(0, intdiv($monthsfrom, $interval) - 1) * $interval;
1084 $year = $year0 + intdiv($monthindex, 12);
1085 $month = $monthindex % 12 + 1;
1086 $periodstart = gmmktime(0, 0, 0, $month, 1, $year);
1087 if (empty($bymonth) || in_array($month, $bymonth)) {
1088 $days = $this->
getDaysOfMonth($year, $month, $byday, $bymonthday, $day0);
1091 $index = $i + ($skip ? max(0, intdiv((
int) gmdate(
'Y', $from) - $year0, $interval) - 1) : 0);
1092 $year = $year0 + $index * $interval;
1093 $periodstart = gmmktime(0, 0, 0, 1, 1, $year);
1094 $nbdaysofyear = (int) gmdate(
'z', gmmktime(0, 0, 0, 12, 31, $year)) + 1;
1095 if (!empty($byweekno)) {
1097 $nbweeks = intdiv($this->
getFirstWeekOfYear($year + 1, $wkst) - $firstweek, 7 * 86400);
1098 $weekdays = array();
1099 foreach ($byday as $item) {
1100 $weekdays[] = $item[1];
1102 if (empty($weekdays)) {
1103 $weekdays[] = $dayofweek0;
1105 foreach ($byweekno as $weekno) {
1106 $number = ($weekno > 0 ? $weekno : $nbweeks + $weekno + 1);
1107 if ($number < 1 || $number > $nbweeks) {
1110 foreach ($weekdays as $weekday) {
1111 $days[] = $firstweek + ($number - 1) * 7 * 86400 + ((($weekday - $wkst + 7) % 7) * 86400);
1114 } elseif (!empty($byyearday)) {
1115 foreach ($byyearday as $yearday) {
1116 $number = ($yearday > 0 ? $yearday : $nbdaysofyear + $yearday + 1);
1117 if ($number >= 1 && $number <= $nbdaysofyear) {
1118 $days[] = $periodstart + ($number - 1) * 86400;
1122 $months = (empty($bymonth) ? array($month0) : $bymonth);
1124 foreach ($months as $month) {
1125 $days = array_merge($days, $this->
getDaysOfMonth($year, $month, $byday, $bymonthday, $day0));
1129 foreach ($days as $day) {
1130 foreach ($times as $timevalue) {
1131 $candidates[] = $day + $timevalue;
1135 if ($periodstart >= $to) {
1139 $candidates = array_values(array_unique($candidates));
1141 if (!empty($bysetpos)) {
1142 $selected = array();
1143 foreach ($bysetpos as $position) {
1144 $position = ($position > 0 ? $position - 1 : count($candidates) + $position);
1145 if (isset($candidates[$position])) {
1146 $selected[$candidates[$position]] = $candidates[$position];
1150 $candidates = $selected;
1153 foreach ($candidates as $start) {
1154 if ($start < $dtstart) {
1157 if ($start >= $to || ($until && $start > $until)) {
1162 if ($maxcount && $count >= $maxcount) {