Mercurial > hg > rc1
annotate plugins/libcalendaring/libvcalendar.php @ 55:2bf0fc326ee2
fix symlink
| author | Charlie Root |
|---|---|
| date | Wed, 08 Oct 2025 09:09:32 -0400 |
| parents | 91f005a4f7e9 |
| children | 082a19037887 |
| rev | line source |
|---|---|
| 4 | 1 <?php |
| 2 | |
| 3 /** | |
| 4 * iCalendar functions for the libcalendaring plugin | |
| 5 * | |
| 6 * @author Thomas Bruederli <bruederli@kolabsys.com> | |
| 7 * | |
| 8 * Copyright (C) 2013-2015, Kolab Systems AG <contact@kolabsys.com> | |
| 9 * | |
| 10 * This program is free software: you can redistribute it and/or modify | |
| 11 * it under the terms of the GNU Affero General Public License as | |
| 12 * published by the Free Software Foundation, either version 3 of the | |
| 13 * License, or (at your option) any later version. | |
| 14 * | |
| 15 * This program is distributed in the hope that it will be useful, | |
| 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 18 * GNU Affero General Public License for more details. | |
| 19 * | |
| 20 * You should have received a copy of the GNU Affero General Public License | |
| 21 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 22 */ | |
| 23 | |
| 24 use \Sabre\VObject; | |
| 25 use \Sabre\VObject\DateTimeParser; | |
| 26 | |
| 27 /** | |
| 28 * Class to parse and build vCalendar (iCalendar) files | |
| 29 * | |
| 30 * Uses the Sabre VObject library, version 3.x. | |
| 31 * | |
| 32 */ | |
| 33 class libvcalendar implements Iterator | |
| 34 { | |
| 35 private $timezone; | |
| 36 private $attach_uri = null; | |
| 37 private $prodid = '-//Roundcube libcalendaring//Sabre//Sabre VObject//EN'; | |
| 38 private $type_component_map = array('event' => 'VEVENT', 'task' => 'VTODO'); | |
| 39 private $attendee_keymap = array( | |
| 40 'name' => 'CN', | |
| 41 'status' => 'PARTSTAT', | |
| 42 'role' => 'ROLE', | |
| 43 'cutype' => 'CUTYPE', | |
| 44 'rsvp' => 'RSVP', | |
| 45 'delegated-from' => 'DELEGATED-FROM', | |
| 46 'delegated-to' => 'DELEGATED-TO', | |
| 47 'schedule-status' => 'SCHEDULE-STATUS', | |
| 48 'schedule-agent' => 'SCHEDULE-AGENT', | |
| 49 'sent-by' => 'SENT-BY', | |
| 50 ); | |
| 51 private $organizer_keymap = array( | |
| 52 'name' => 'CN', | |
| 53 'schedule-status' => 'SCHEDULE-STATUS', | |
| 54 'schedule-agent' => 'SCHEDULE-AGENT', | |
| 55 'sent-by' => 'SENT-BY', | |
| 56 ); | |
| 57 private $iteratorkey = 0; | |
| 58 private $charset; | |
| 59 private $forward_exceptions; | |
| 60 private $vhead; | |
| 61 private $fp; | |
| 62 private $vtimezones = array(); | |
| 63 | |
| 64 public $method; | |
| 65 public $agent = ''; | |
| 66 public $objects = array(); | |
| 67 public $freebusy = array(); | |
| 68 | |
| 69 | |
| 70 /** | |
| 71 * Default constructor | |
| 72 */ | |
| 73 function __construct($tz = null) | |
| 74 { | |
| 75 $this->timezone = $tz; | |
|
49
91f005a4f7e9
Slowly cleaning up more php8 Warnings/deprecations
Charlie Root
parents:
4
diff
changeset
|
76 $this->prodid = '-//Roundcube libcalendaring ' . RCUBE_VERSION . '//Sabre//Sabre VObject ' . '3.0.0' . '//EN'; /* VObject\Version::VERSION */ |
| 4 | 77 } |
| 78 | |
| 79 /** | |
| 80 * Setter for timezone information | |
| 81 */ | |
| 82 public function set_timezone($tz) | |
| 83 { | |
| 84 $this->timezone = $tz; | |
| 85 } | |
| 86 | |
| 87 /** | |
| 88 * Setter for URI template for attachment links | |
| 89 */ | |
| 90 public function set_attach_uri($uri) | |
| 91 { | |
| 92 $this->attach_uri = $uri; | |
| 93 } | |
| 94 | |
| 95 /** | |
| 96 * Setter for a custom PRODID attribute | |
| 97 */ | |
| 98 public function set_prodid($prodid) | |
| 99 { | |
| 100 $this->prodid = $prodid; | |
| 101 } | |
| 102 | |
| 103 /** | |
| 104 * Setter for a user-agent string to tweak input/output accordingly | |
| 105 */ | |
| 106 public function set_agent($agent) | |
| 107 { | |
| 108 $this->agent = $agent; | |
| 109 } | |
| 110 | |
| 111 /** | |
| 112 * Free resources by clearing member vars | |
| 113 */ | |
| 114 public function reset() | |
| 115 { | |
| 116 $this->vhead = ''; | |
| 117 $this->method = ''; | |
| 118 $this->objects = array(); | |
| 119 $this->freebusy = array(); | |
| 120 $this->vtimezones = array(); | |
| 121 $this->iteratorkey = 0; | |
| 122 | |
| 123 if ($this->fp) { | |
| 124 fclose($this->fp); | |
| 125 $this->fp = null; | |
| 126 } | |
| 127 } | |
| 128 | |
| 129 /** | |
| 130 * Import events from iCalendar format | |
| 131 * | |
| 132 * @param string vCalendar input | |
| 133 * @param string Input charset (from envelope) | |
| 134 * @param boolean True if parsing exceptions should be forwarded to the caller | |
| 135 * @return array List of events extracted from the input | |
| 136 */ | |
| 137 public function import($vcal, $charset = 'UTF-8', $forward_exceptions = false, $memcheck = true) | |
| 138 { | |
| 139 // TODO: convert charset to UTF-8 if other | |
| 140 | |
| 141 try { | |
| 142 // estimate the memory usage and try to avoid fatal errors when allowed memory gets exhausted | |
| 143 if ($memcheck) { | |
| 144 $count = substr_count($vcal, 'BEGIN:VEVENT') + substr_count($vcal, 'BEGIN:VTODO'); | |
| 145 $expected_memory = $count * 70*1024; // assume ~ 70K per event (empirically determined) | |
| 146 | |
| 147 if (!rcube_utils::mem_check($expected_memory)) { | |
| 148 throw new Exception("iCal file too big"); | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 $vobject = VObject\Reader::read($vcal, VObject\Reader::OPTION_FORGIVING | VObject\Reader::OPTION_IGNORE_INVALID_LINES); | |
| 153 if ($vobject) | |
| 154 return $this->import_from_vobject($vobject); | |
| 155 } | |
| 156 catch (Exception $e) { | |
| 157 if ($forward_exceptions) { | |
| 158 throw $e; | |
| 159 } | |
| 160 else { | |
| 161 rcube::raise_error(array( | |
| 162 'code' => 600, 'type' => 'php', | |
| 163 'file' => __FILE__, 'line' => __LINE__, | |
| 164 'message' => "iCal data parse error: " . $e->getMessage()), | |
| 165 true, false); | |
| 166 } | |
| 167 } | |
| 168 | |
| 169 return array(); | |
| 170 } | |
| 171 | |
| 172 /** | |
| 173 * Read iCalendar events from a file | |
| 174 * | |
| 175 * @param string File path to read from | |
| 176 * @param string Input charset (from envelope) | |
| 177 * @param boolean True if parsing exceptions should be forwarded to the caller | |
| 178 * @return array List of events extracted from the file | |
| 179 */ | |
| 180 public function import_from_file($filepath, $charset = 'UTF-8', $forward_exceptions = false) | |
| 181 { | |
| 182 if ($this->fopen($filepath, $charset, $forward_exceptions)) { | |
| 183 while ($this->_parse_next(false)) { | |
| 184 // nop | |
| 185 } | |
| 186 | |
| 187 fclose($this->fp); | |
| 188 $this->fp = null; | |
| 189 } | |
| 190 | |
| 191 return $this->objects; | |
| 192 } | |
| 193 | |
| 194 /** | |
| 195 * Open a file to read iCalendar events sequentially | |
| 196 * | |
| 197 * @param string File path to read from | |
| 198 * @param string Input charset (from envelope) | |
| 199 * @param boolean True if parsing exceptions should be forwarded to the caller | |
| 200 * @return boolean True if file contents are considered valid | |
| 201 */ | |
| 202 public function fopen($filepath, $charset = 'UTF-8', $forward_exceptions = false) | |
| 203 { | |
| 204 $this->reset(); | |
| 205 | |
| 206 // just to be sure... | |
| 207 @ini_set('auto_detect_line_endings', true); | |
| 208 | |
| 209 $this->charset = $charset; | |
| 210 $this->forward_exceptions = $forward_exceptions; | |
| 211 $this->fp = fopen($filepath, 'r'); | |
| 212 | |
| 213 // check file content first | |
| 214 $begin = fread($this->fp, 1024); | |
| 215 if (!preg_match('/BEGIN:VCALENDAR/i', $begin)) { | |
| 216 return false; | |
| 217 } | |
| 218 | |
| 219 fseek($this->fp, 0); | |
| 220 return $this->_parse_next(); | |
| 221 } | |
| 222 | |
| 223 /** | |
| 224 * Parse the next event/todo/freebusy object from the input file | |
| 225 */ | |
| 226 private function _parse_next($reset = true) | |
| 227 { | |
| 228 if ($reset) { | |
| 229 $this->iteratorkey = 0; | |
| 230 $this->objects = array(); | |
| 231 $this->freebusy = array(); | |
| 232 } | |
| 233 | |
| 234 $next = $this->_next_component(); | |
| 235 $buffer = $next; | |
| 236 | |
| 237 // load the next component(s) too, as they could contain recurrence exceptions | |
| 238 while (preg_match('/(RRULE|RECURRENCE-ID)[:;]/i', $next)) { | |
| 239 $next = $this->_next_component(); | |
| 240 $buffer .= $next; | |
| 241 } | |
| 242 | |
| 243 // parse the vevent block surrounded with the vcalendar heading | |
| 244 if (strlen($buffer) && preg_match('/BEGIN:(VEVENT|VTODO|VFREEBUSY)/i', $buffer)) { | |
| 245 try { | |
| 246 $this->import($this->vhead . $buffer . "END:VCALENDAR", $this->charset, true, false); | |
| 247 } | |
| 248 catch (Exception $e) { | |
| 249 if ($this->forward_exceptions) { | |
| 250 throw new VObject\ParseException($e->getMessage() . " in\n" . $buffer); | |
| 251 } | |
| 252 else { | |
| 253 // write the failing section to error log | |
| 254 rcube::raise_error(array( | |
| 255 'code' => 600, 'type' => 'php', | |
| 256 'file' => __FILE__, 'line' => __LINE__, | |
| 257 'message' => $e->getMessage() . " in\n" . $buffer), | |
| 258 true, false); | |
| 259 } | |
| 260 | |
| 261 // advance to next | |
| 262 return $this->_parse_next($reset); | |
| 263 } | |
| 264 | |
| 265 return count($this->objects) > 0; | |
| 266 } | |
| 267 | |
| 268 return false; | |
| 269 } | |
| 270 | |
| 271 /** | |
| 272 * Helper method to read the next calendar component from the file | |
| 273 */ | |
| 274 private function _next_component() | |
| 275 { | |
| 276 $buffer = ''; | |
| 277 $vcalendar_head = false; | |
| 278 while (($line = fgets($this->fp, 1024)) !== false) { | |
| 279 // ignore END:VCALENDAR lines | |
| 280 if (preg_match('/END:VCALENDAR/i', $line)) { | |
| 281 continue; | |
| 282 } | |
| 283 // read vcalendar header (with timezone defintion) | |
| 284 if (preg_match('/BEGIN:VCALENDAR/i', $line)) { | |
| 285 $this->vhead = ''; | |
| 286 $vcalendar_head = true; | |
| 287 } | |
| 288 | |
| 289 // end of VCALENDAR header part | |
| 290 if ($vcalendar_head && preg_match('/BEGIN:(VEVENT|VTODO|VFREEBUSY)/i', $line)) { | |
| 291 $vcalendar_head = false; | |
| 292 } | |
| 293 | |
| 294 if ($vcalendar_head) { | |
| 295 $this->vhead .= $line; | |
| 296 } | |
| 297 else { | |
| 298 $buffer .= $line; | |
| 299 if (preg_match('/END:(VEVENT|VTODO|VFREEBUSY)/i', $line)) { | |
| 300 break; | |
| 301 } | |
| 302 } | |
| 303 } | |
| 304 | |
| 305 return $buffer; | |
| 306 } | |
| 307 | |
| 308 /** | |
| 309 * Import objects from an already parsed Sabre\VObject\Component object | |
| 310 * | |
| 311 * @param object Sabre\VObject\Component to read from | |
| 312 * @return array List of events extracted from the file | |
| 313 */ | |
| 314 public function import_from_vobject($vobject) | |
| 315 { | |
| 316 $seen = array(); | |
| 317 $exceptions = array(); | |
| 318 | |
| 319 if ($vobject->name == 'VCALENDAR') { | |
| 320 $this->method = strval($vobject->METHOD); | |
| 321 $this->agent = strval($vobject->PRODID); | |
| 322 | |
| 323 foreach ($vobject->getComponents() as $ve) { | |
| 324 if ($ve->name == 'VEVENT' || $ve->name == 'VTODO') { | |
| 325 // convert to hash array representation | |
| 326 $object = $this->_to_array($ve); | |
| 327 | |
| 328 // temporarily store this as exception | |
| 329 if ($object['recurrence_date']) { | |
| 330 $exceptions[] = $object; | |
| 331 } | |
| 332 else if (!$seen[$object['uid']]++) { | |
| 333 $this->objects[] = $object; | |
| 334 } | |
| 335 } | |
| 336 else if ($ve->name == 'VFREEBUSY') { | |
| 337 $this->objects[] = $this->_parse_freebusy($ve); | |
| 338 } | |
| 339 } | |
| 340 | |
| 341 // add exceptions to the according master events | |
| 342 foreach ($exceptions as $exception) { | |
| 343 $uid = $exception['uid']; | |
| 344 | |
| 345 // make this exception the master | |
| 346 if (!$seen[$uid]++) { | |
| 347 $this->objects[] = $exception; | |
| 348 } | |
| 349 else { | |
| 350 foreach ($this->objects as $i => $object) { | |
| 351 // add as exception to existing entry with a matching UID | |
| 352 if ($object['uid'] == $uid) { | |
| 353 $this->objects[$i]['exceptions'][] = $exception; | |
| 354 | |
| 355 if (!empty($object['recurrence'])) { | |
| 356 $this->objects[$i]['recurrence']['EXCEPTIONS'] = &$this->objects[$i]['exceptions']; | |
| 357 } | |
| 358 break; | |
| 359 } | |
| 360 } | |
| 361 } | |
| 362 } | |
| 363 } | |
| 364 | |
| 365 return $this->objects; | |
| 366 } | |
| 367 | |
| 368 /** | |
| 369 * Getter for free-busy periods | |
| 370 */ | |
| 371 public function get_busy_periods() | |
| 372 { | |
| 373 $out = array(); | |
| 374 foreach ((array)$this->freebusy['periods'] as $period) { | |
| 375 if ($period[2] != 'FREE') { | |
| 376 $out[] = $period; | |
| 377 } | |
| 378 } | |
| 379 | |
| 380 return $out; | |
| 381 } | |
| 382 | |
| 383 /** | |
| 384 * Helper method to determine whether the connected client is an Apple device | |
| 385 */ | |
| 386 private function is_apple() | |
| 387 { | |
| 388 return stripos($this->agent, 'Apple') !== false | |
| 389 || stripos($this->agent, 'Mac OS X') !== false | |
| 390 || stripos($this->agent, 'iOS/') !== false; | |
| 391 } | |
| 392 | |
| 393 /** | |
| 394 * Convert the given VEvent object to a libkolab compatible array representation | |
| 395 * | |
| 396 * @param object Vevent object to convert | |
| 397 * @return array Hash array with object properties | |
| 398 */ | |
| 399 private function _to_array($ve) | |
| 400 { | |
| 401 $event = array( | |
| 402 'uid' => self::convert_string($ve->UID), | |
| 403 'title' => self::convert_string($ve->SUMMARY), | |
| 404 '_type' => $ve->name == 'VTODO' ? 'task' : 'event', | |
| 405 // set defaults | |
| 406 'priority' => 0, | |
| 407 'attendees' => array(), | |
| 408 'x-custom' => array(), | |
| 409 ); | |
| 410 | |
| 411 // Catch possible exceptions when date is invalid (Bug #2144) | |
| 412 // We can skip these fields, they aren't critical | |
| 413 foreach (array('CREATED' => 'created', 'LAST-MODIFIED' => 'changed', 'DTSTAMP' => 'changed') as $attr => $field) { | |
| 414 try { | |
| 415 if (!$event[$field] && $ve->{$attr}) { | |
| 416 $event[$field] = $ve->{$attr}->getDateTime(); | |
| 417 } | |
| 418 } catch (Exception $e) {} | |
| 419 } | |
| 420 | |
| 421 // map other attributes to internal fields | |
| 422 foreach ($ve->children as $prop) { | |
| 423 if (!($prop instanceof VObject\Property)) | |
| 424 continue; | |
| 425 | |
| 426 $value = strval($prop); | |
| 427 | |
| 428 switch ($prop->name) { | |
| 429 case 'DTSTART': | |
| 430 case 'DTEND': | |
| 431 case 'DUE': | |
| 432 $propmap = array('DTSTART' => 'start', 'DTEND' => 'end', 'DUE' => 'due'); | |
| 433 $event[$propmap[$prop->name]] = self::convert_datetime($prop); | |
| 434 break; | |
| 435 | |
| 436 case 'TRANSP': | |
| 437 $event['free_busy'] = strval($prop) == 'TRANSPARENT' ? 'free' : 'busy'; | |
| 438 break; | |
| 439 | |
| 440 case 'STATUS': | |
| 441 if ($value == 'TENTATIVE') | |
| 442 $event['free_busy'] = 'tentative'; | |
| 443 else if ($value == 'CANCELLED') | |
| 444 $event['cancelled'] = true; | |
| 445 else if ($value == 'COMPLETED') | |
| 446 $event['complete'] = 100; | |
| 447 | |
| 448 $event['status'] = $value; | |
| 449 break; | |
| 450 | |
| 451 case 'COMPLETED': | |
| 452 if (self::convert_datetime($prop)) { | |
| 453 $event['status'] = 'COMPLETED'; | |
| 454 $event['complete'] = 100; | |
| 455 } | |
| 456 break; | |
| 457 | |
| 458 case 'PRIORITY': | |
| 459 if (is_numeric($value)) | |
| 460 $event['priority'] = $value; | |
| 461 break; | |
| 462 | |
| 463 case 'RRULE': | |
| 464 $params = is_array($event['recurrence']) ? $event['recurrence'] : array(); | |
| 465 // parse recurrence rule attributes | |
| 466 foreach ($prop->getParts() as $k => $v) { | |
| 467 $params[strtoupper($k)] = is_array($v) ? implode(',', $v) : $v; | |
| 468 } | |
| 469 if ($params['UNTIL']) | |
| 470 $params['UNTIL'] = date_create($params['UNTIL']); | |
| 471 if (!$params['INTERVAL']) | |
| 472 $params['INTERVAL'] = 1; | |
| 473 | |
| 474 $event['recurrence'] = array_filter($params); | |
| 475 break; | |
| 476 | |
| 477 case 'EXDATE': | |
| 478 if (!empty($value)) { | |
| 479 $exdates = array_map(function($_) { return is_array($_) ? $_[0] : $_; }, self::convert_datetime($prop, true)); | |
| 480 $event['recurrence']['EXDATE'] = array_merge((array)$event['recurrence']['EXDATE'], $exdates); | |
| 481 } | |
| 482 break; | |
| 483 | |
| 484 case 'RDATE': | |
| 485 if (!empty($value)) { | |
| 486 $rdates = array_map(function($_) { return is_array($_) ? $_[0] : $_; }, self::convert_datetime($prop, true)); | |
| 487 $event['recurrence']['RDATE'] = array_merge((array)$event['recurrence']['RDATE'], $rdates); | |
| 488 } | |
| 489 break; | |
| 490 | |
| 491 case 'RECURRENCE-ID': | |
| 492 $event['recurrence_date'] = self::convert_datetime($prop); | |
| 493 if ($prop->offsetGet('RANGE') == 'THISANDFUTURE' || $prop->offsetGet('THISANDFUTURE') !== null) { | |
| 494 $event['thisandfuture'] = true; | |
| 495 } | |
| 496 break; | |
| 497 | |
| 498 case 'RELATED-TO': | |
| 499 $reltype = $prop->offsetGet('RELTYPE'); | |
| 500 if ($reltype == 'PARENT' || $reltype === null) { | |
| 501 $event['parent_id'] = $value; | |
| 502 } | |
| 503 break; | |
| 504 | |
| 505 case 'SEQUENCE': | |
| 506 $event['sequence'] = intval($value); | |
| 507 break; | |
| 508 | |
| 509 case 'PERCENT-COMPLETE': | |
| 510 $event['complete'] = intval($value); | |
| 511 break; | |
| 512 | |
| 513 case 'LOCATION': | |
| 514 case 'DESCRIPTION': | |
| 515 case 'URL': | |
| 516 case 'COMMENT': | |
| 517 $event[strtolower($prop->name)] = self::convert_string($prop); | |
| 518 break; | |
| 519 | |
| 520 case 'CATEGORY': | |
| 521 case 'CATEGORIES': | |
| 522 $event['categories'] = array_merge((array)$event['categories'], $prop->getParts()); | |
| 523 break; | |
| 524 | |
| 525 case 'CLASS': | |
| 526 case 'X-CALENDARSERVER-ACCESS': | |
| 527 $event['sensitivity'] = strtolower($value); | |
| 528 break; | |
| 529 | |
| 530 case 'X-MICROSOFT-CDO-BUSYSTATUS': | |
| 531 if ($value == 'OOF') | |
| 532 $event['free_busy'] = 'outofoffice'; | |
| 533 else if (in_array($value, array('FREE', 'BUSY', 'TENTATIVE'))) | |
| 534 $event['free_busy'] = strtolower($value); | |
| 535 break; | |
| 536 | |
| 537 case 'ATTENDEE': | |
| 538 case 'ORGANIZER': | |
| 539 $params = array('RSVP' => false); | |
| 540 foreach ($prop->parameters() as $pname => $pvalue) { | |
| 541 switch ($pname) { | |
| 542 case 'RSVP': $params[$pname] = strtolower($pvalue) == 'true'; break; | |
| 543 case 'CN': $params[$pname] = self::unescape($pvalue); break; | |
| 544 default: $params[$pname] = strval($pvalue); break; | |
| 545 } | |
| 546 } | |
| 547 $attendee = self::map_keys($params, array_flip($this->attendee_keymap)); | |
| 548 $attendee['email'] = preg_replace('!^mailto:!i', '', $value); | |
| 549 | |
| 550 if ($prop->name == 'ORGANIZER') { | |
| 551 $attendee['role'] = 'ORGANIZER'; | |
| 552 $attendee['status'] = 'ACCEPTED'; | |
| 553 $event['organizer'] = $attendee; | |
| 554 | |
| 555 if (array_key_exists('schedule-agent', $attendee)) { | |
| 556 $schedule_agent = $attendee['schedule-agent']; | |
| 557 } | |
| 558 } | |
| 559 else if ($attendee['email'] != $event['organizer']['email']) { | |
| 560 $event['attendees'][] = $attendee; | |
| 561 } | |
| 562 break; | |
| 563 | |
| 564 case 'ATTACH': | |
| 565 $params = self::parameters_array($prop); | |
| 566 if (substr($value, 0, 4) == 'http' && !strpos($value, ':attachment:')) { | |
| 567 $event['links'][] = $value; | |
| 568 } | |
| 569 else if (strlen($value) && strtoupper($params['VALUE']) == 'BINARY') { | |
| 570 $attachment = self::map_keys($params, array('FMTTYPE' => 'mimetype', 'X-LABEL' => 'name', 'X-APPLE-FILENAME' => 'name')); | |
| 571 $attachment['data'] = $value; | |
| 572 $attachment['size'] = strlen($value); | |
| 573 $event['attachments'][] = $attachment; | |
| 574 } | |
| 575 break; | |
| 576 | |
| 577 default: | |
| 578 if (substr($prop->name, 0, 2) == 'X-') | |
| 579 $event['x-custom'][] = array($prop->name, strval($value)); | |
| 580 break; | |
| 581 } | |
| 582 } | |
| 583 | |
| 584 // check DURATION property if no end date is set | |
| 585 if (empty($event['end']) && $ve->DURATION) { | |
| 586 try { | |
| 587 $duration = new DateInterval(strval($ve->DURATION)); | |
| 588 $end = clone $event['start']; | |
| 589 $end->add($duration); | |
| 590 $event['end'] = $end; | |
| 591 } | |
| 592 catch (\Exception $e) { | |
| 593 trigger_error(strval($e), E_USER_WARNING); | |
| 594 } | |
| 595 } | |
| 596 | |
| 597 // validate event dates | |
| 598 if ($event['_type'] == 'event') { | |
| 599 $event['allday'] = false; | |
| 600 | |
| 601 // check for all-day dates | |
| 602 if ($event['start']->_dateonly) { | |
| 603 $event['allday'] = true; | |
| 604 } | |
| 605 | |
| 606 // events may lack the DTEND property, set it to DTSTART (RFC5545 3.6.1) | |
| 607 if (empty($event['end'])) { | |
| 608 $event['end'] = clone $event['start']; | |
| 609 } | |
| 610 // shift end-date by one day (except Thunderbird) | |
| 611 else if ($event['allday'] && is_object($event['end'])) { | |
| 612 $event['end']->sub(new \DateInterval('PT23H')); | |
| 613 } | |
| 614 | |
| 615 // sanity-check and fix end date | |
| 616 if (!empty($event['end']) && $event['end'] < $event['start']) { | |
| 617 $event['end'] = clone $event['start']; | |
| 618 } | |
| 619 } | |
| 620 | |
| 621 // make organizer part of the attendees list for compatibility reasons | |
| 622 if (!empty($event['organizer']) && is_array($event['attendees']) && $event['_type'] == 'event') { | |
| 623 array_unshift($event['attendees'], $event['organizer']); | |
| 624 } | |
| 625 | |
| 626 // find alarms | |
| 627 foreach ($ve->select('VALARM') as $valarm) { | |
| 628 $action = 'DISPLAY'; | |
| 629 $trigger = null; | |
| 630 $alarm = array(); | |
| 631 | |
| 632 foreach ($valarm->children as $prop) { | |
| 633 $value = strval($prop); | |
| 634 | |
| 635 switch ($prop->name) { | |
| 636 case 'TRIGGER': | |
| 637 foreach ($prop->parameters as $param) { | |
| 638 if ($param->name == 'VALUE' && $param->getValue() == 'DATE-TIME') { | |
| 639 $trigger = '@' . $prop->getDateTime()->format('U'); | |
| 640 $alarm['trigger'] = $prop->getDateTime(); | |
| 641 } | |
| 642 else if ($param->name == 'RELATED') { | |
| 643 $alarm['related'] = $param->getValue(); | |
| 644 } | |
| 645 } | |
| 646 if (!$trigger && ($values = libcalendaring::parse_alarm_value($value))) { | |
| 647 $trigger = $values[2]; | |
| 648 } | |
| 649 | |
| 650 if (!$alarm['trigger']) { | |
| 651 $alarm['trigger'] = rtrim(preg_replace('/([A-Z])0[WDHMS]/', '\\1', $value), 'T'); | |
| 652 // if all 0-values have been stripped, assume 'at time' | |
| 653 if ($alarm['trigger'] == 'P') | |
| 654 $alarm['trigger'] = 'PT0S'; | |
| 655 } | |
| 656 break; | |
| 657 | |
| 658 case 'ACTION': | |
| 659 $action = $alarm['action'] = strtoupper($value); | |
| 660 break; | |
| 661 | |
| 662 case 'SUMMARY': | |
| 663 case 'DESCRIPTION': | |
| 664 case 'DURATION': | |
| 665 $alarm[strtolower($prop->name)] = self::convert_string($prop); | |
| 666 break; | |
| 667 | |
| 668 case 'REPEAT': | |
| 669 $alarm['repeat'] = intval($value); | |
| 670 break; | |
| 671 | |
| 672 case 'ATTENDEE': | |
| 673 $alarm['attendees'][] = preg_replace('!^mailto:!i', '', $value); | |
| 674 break; | |
| 675 | |
| 676 case 'ATTACH': | |
| 677 $params = self::parameters_array($prop); | |
| 678 if (strlen($value) && (preg_match('/^[a-z]+:/', $value) || strtoupper($params['VALUE']) == 'URI')) { | |
| 679 // we only support URI-type of attachments here | |
| 680 $alarm['uri'] = $value; | |
| 681 } | |
| 682 break; | |
| 683 } | |
| 684 } | |
| 685 | |
| 686 if ($action != 'NONE') { | |
| 687 if ($trigger && !$event['alarms']) // store first alarm in legacy property | |
| 688 $event['alarms'] = $trigger . ':' . $action; | |
| 689 | |
| 690 if ($alarm['trigger']) | |
| 691 $event['valarms'][] = $alarm; | |
| 692 } | |
| 693 } | |
| 694 | |
| 695 // assign current timezone to event start/end | |
| 696 if ($event['start'] instanceof DateTime) { | |
| 697 if ($this->timezone) | |
| 698 $event['start']->setTimezone($this->timezone); | |
| 699 } | |
| 700 else { | |
| 701 unset($event['start']); | |
| 702 } | |
| 703 | |
| 704 if ($event['end'] instanceof DateTime) { | |
| 705 if ($this->timezone) | |
| 706 $event['end']->setTimezone($this->timezone); | |
| 707 } | |
| 708 else { | |
| 709 unset($event['end']); | |
| 710 } | |
| 711 | |
| 712 // some iTip CANCEL messages only contain the start date | |
| 713 if (!$event['end'] && $event['start'] && $this->method == 'CANCEL') { | |
| 714 $event['end'] = clone $event['start']; | |
| 715 } | |
| 716 | |
| 717 // T2531: Remember SCHEDULE-AGENT in custom property to properly | |
| 718 // support event updates via CalDAV when SCHEDULE-AGENT=CLIENT is used | |
| 719 if (isset($schedule_agent)) { | |
| 720 $event['x-custom'][] = array('SCHEDULE-AGENT', $schedule_agent); | |
| 721 } | |
| 722 | |
| 723 // minimal validation | |
| 724 if (empty($event['uid']) || ($event['_type'] == 'event' && empty($event['start']) != empty($event['end']))) { | |
| 725 throw new VObject\ParseException('Object validation failed: missing mandatory object properties'); | |
| 726 } | |
| 727 | |
| 728 return $event; | |
| 729 } | |
| 730 | |
| 731 /** | |
| 732 * Parse the given vfreebusy component into an array representation | |
| 733 */ | |
| 734 private function _parse_freebusy($ve) | |
| 735 { | |
| 736 $this->freebusy = array('_type' => 'freebusy', 'periods' => array()); | |
| 737 $seen = array(); | |
| 738 | |
| 739 foreach ($ve->children as $prop) { | |
| 740 if (!($prop instanceof VObject\Property)) | |
| 741 continue; | |
| 742 | |
| 743 $value = strval($prop); | |
| 744 | |
| 745 switch ($prop->name) { | |
| 746 case 'CREATED': | |
| 747 case 'LAST-MODIFIED': | |
| 748 case 'DTSTAMP': | |
| 749 case 'DTSTART': | |
| 750 case 'DTEND': | |
| 751 $propmap = array('DTSTART' => 'start', 'DTEND' => 'end', 'CREATED' => 'created', 'LAST-MODIFIED' => 'changed', 'DTSTAMP' => 'changed'); | |
| 752 $this->freebusy[$propmap[$prop->name]] = self::convert_datetime($prop); | |
| 753 break; | |
| 754 | |
| 755 case 'ORGANIZER': | |
| 756 $this->freebusy['organizer'] = preg_replace('!^mailto:!i', '', $value); | |
| 757 break; | |
| 758 | |
| 759 case 'FREEBUSY': | |
| 760 // The freebusy component can hold more than 1 value, separated by commas. | |
| 761 $periods = explode(',', $value); | |
| 762 $fbtype = strval($prop['FBTYPE']) ?: 'BUSY'; | |
| 763 | |
| 764 // skip dupes | |
| 765 if ($seen[$value.':'.$fbtype]++) | |
| 766 continue; | |
| 767 | |
| 768 foreach ($periods as $period) { | |
| 769 // Every period is formatted as [start]/[end]. The start is an | |
| 770 // absolute UTC time, the end may be an absolute UTC time, or | |
| 771 // duration (relative) value. | |
| 772 list($busyStart, $busyEnd) = explode('/', $period); | |
| 773 | |
| 774 $busyStart = DateTimeParser::parse($busyStart); | |
| 775 $busyEnd = DateTimeParser::parse($busyEnd); | |
| 776 if ($busyEnd instanceof \DateInterval) { | |
| 777 $tmp = clone $busyStart; | |
| 778 $tmp->add($busyEnd); | |
| 779 $busyEnd = $tmp; | |
| 780 } | |
| 781 | |
| 782 if ($busyEnd && $busyEnd > $busyStart) | |
| 783 $this->freebusy['periods'][] = array($busyStart, $busyEnd, $fbtype); | |
| 784 } | |
| 785 break; | |
| 786 | |
| 787 case 'COMMENT': | |
| 788 $this->freebusy['comment'] = $value; | |
| 789 } | |
| 790 } | |
| 791 | |
| 792 return $this->freebusy; | |
| 793 } | |
| 794 | |
| 795 /** | |
| 796 * | |
| 797 */ | |
| 798 public static function convert_string($prop) | |
| 799 { | |
| 800 return strval($prop); | |
| 801 } | |
| 802 | |
| 803 /** | |
| 804 * | |
| 805 */ | |
| 806 public static function unescape($prop) | |
| 807 { | |
| 808 return str_replace('\,', ',', strval($prop)); | |
| 809 } | |
| 810 | |
| 811 /** | |
| 812 * Helper method to correctly interpret an all-day date value | |
| 813 */ | |
| 814 public static function convert_datetime($prop, $as_array = false) | |
| 815 { | |
| 816 if (empty($prop)) { | |
| 817 return $as_array ? array() : null; | |
| 818 } | |
| 819 | |
| 820 else if ($prop instanceof VObject\Property\iCalendar\DateTime) { | |
| 821 if (count($prop->getDateTimes()) > 1) { | |
| 822 $dt = array(); | |
| 823 $dateonly = !$prop->hasTime(); | |
| 824 foreach ($prop->getDateTimes() as $item) { | |
| 825 $item->_dateonly = $dateonly; | |
| 826 $dt[] = $item; | |
| 827 } | |
| 828 } | |
| 829 else { | |
| 830 $dt = $prop->getDateTime(); | |
| 831 if (!$prop->hasTime()) { | |
| 832 $dt->_dateonly = true; | |
| 833 } | |
| 834 } | |
| 835 } | |
| 836 else if ($prop instanceof VObject\Property\iCalendar\Period) { | |
| 837 $dt = array(); | |
| 838 foreach ($prop->getParts() as $val) { | |
| 839 try { | |
| 840 list($start, $end) = explode('/', $val); | |
| 841 $start = DateTimeParser::parseDateTime($start); | |
| 842 | |
| 843 // This is a duration value. | |
| 844 if ($end[0] === 'P') { | |
| 845 $dur = DateTimeParser::parseDuration($end); | |
| 846 $end = clone $start; | |
| 847 $end->add($dur); | |
| 848 } | |
| 849 else { | |
| 850 $end = DateTimeParser::parseDateTime($end); | |
| 851 } | |
| 852 $dt[] = array($start, $end); | |
| 853 } | |
| 854 catch (Exception $e) { | |
| 855 // ignore single date parse errors | |
| 856 } | |
| 857 } | |
| 858 } | |
| 859 else if ($prop instanceof \DateTime) { | |
| 860 $dt = $prop; | |
| 861 } | |
| 862 | |
| 863 // force return value to array if requested | |
| 864 if ($as_array && !is_array($dt)) { | |
| 865 $dt = empty($dt) ? array() : array($dt); | |
| 866 } | |
| 867 | |
| 868 return $dt; | |
| 869 } | |
| 870 | |
| 871 | |
| 872 /** | |
| 873 * Create a Sabre\VObject\Property instance from a PHP DateTime object | |
| 874 * | |
| 875 * @param object VObject\Document parent node to create property for | |
| 876 * @param string Property name | |
| 877 * @param object DateTime | |
| 878 * @param boolean Set as UTC date | |
| 879 * @param boolean Set as VALUE=DATE property | |
| 880 */ | |
| 881 public function datetime_prop($cal, $name, $dt, $utc = false, $dateonly = null, $set_type = false) | |
| 882 { | |
| 883 if ($utc) { | |
| 884 $dt->setTimeZone(new \DateTimeZone('UTC')); | |
| 885 $is_utc = true; | |
| 886 } | |
| 887 else { | |
| 888 $is_utc = ($tz = $dt->getTimezone()) && in_array($tz->getName(), array('UTC','GMT','Z')); | |
| 889 } | |
| 890 $is_dateonly = $dateonly === null ? (bool)$dt->_dateonly : (bool)$dateonly; | |
| 891 $vdt = $cal->createProperty($name, $dt, null, $is_dateonly ? 'DATE' : 'DATE-TIME'); | |
| 892 | |
| 893 if ($is_dateonly) { | |
| 894 $vdt['VALUE'] = 'DATE'; | |
| 895 } | |
| 896 else if ($set_type) { | |
| 897 $vdt['VALUE'] = 'DATE-TIME'; | |
| 898 } | |
| 899 | |
| 900 // register timezone for VTIMEZONE block | |
| 901 if (!$is_utc && !$dateonly && $tz && ($tzname = $tz->getName())) { | |
| 902 $ts = $dt->format('U'); | |
| 903 if (is_array($this->vtimezones[$tzname])) { | |
| 904 $this->vtimezones[$tzname][0] = min($this->vtimezones[$tzname][0], $ts); | |
| 905 $this->vtimezones[$tzname][1] = max($this->vtimezones[$tzname][1], $ts); | |
| 906 } | |
| 907 else { | |
| 908 $this->vtimezones[$tzname] = array($ts, $ts); | |
| 909 } | |
| 910 } | |
| 911 | |
| 912 return $vdt; | |
| 913 } | |
| 914 | |
| 915 /** | |
| 916 * Copy values from one hash array to another using a key-map | |
| 917 */ | |
| 918 public static function map_keys($values, $map) | |
| 919 { | |
| 920 $out = array(); | |
| 921 foreach ($map as $from => $to) { | |
| 922 if (isset($values[$from])) | |
| 923 $out[$to] = is_array($values[$from]) ? join(',', $values[$from]) : $values[$from]; | |
| 924 } | |
| 925 return $out; | |
| 926 } | |
| 927 | |
| 928 /** | |
| 929 * | |
| 930 */ | |
| 931 private static function parameters_array($prop) | |
| 932 { | |
| 933 $params = array(); | |
| 934 foreach ($prop->parameters() as $name => $value) { | |
| 935 $params[strtoupper($name)] = strval($value); | |
| 936 } | |
| 937 return $params; | |
| 938 } | |
| 939 | |
| 940 | |
| 941 /** | |
| 942 * Export events to iCalendar format | |
| 943 * | |
| 944 * @param array Events as array | |
| 945 * @param string VCalendar method to advertise | |
| 946 * @param boolean Directly send data to stdout instead of returning | |
| 947 * @param callable Callback function to fetch attachment contents, false if no attachment export | |
| 948 * @param boolean Add VTIMEZONE block with timezone definitions for the included events | |
| 949 * @return string Events in iCalendar format (http://tools.ietf.org/html/rfc5545) | |
| 950 */ | |
| 951 public function export($objects, $method = null, $write = false, $get_attachment = false, $with_timezones = true) | |
| 952 { | |
| 953 $this->method = $method; | |
| 954 | |
| 955 // encapsulate in VCALENDAR container | |
| 956 $vcal = new VObject\Component\VCalendar(); | |
| 957 $vcal->VERSION = '2.0'; | |
| 958 $vcal->PRODID = $this->prodid; | |
| 959 $vcal->CALSCALE = 'GREGORIAN'; | |
| 960 | |
| 961 if (!empty($method)) { | |
| 962 $vcal->METHOD = $method; | |
| 963 } | |
| 964 | |
| 965 // write vcalendar header | |
| 966 if ($write) { | |
| 967 echo preg_replace('/END:VCALENDAR[\r\n]*$/m', '', $vcal->serialize()); | |
| 968 } | |
| 969 | |
| 970 foreach ($objects as $object) { | |
| 971 $this->_to_ical($object, !$write?$vcal:false, $get_attachment); | |
| 972 } | |
| 973 | |
| 974 // include timezone information | |
| 975 if ($with_timezones || !empty($method)) { | |
| 976 foreach ($this->vtimezones as $tzid => $range) { | |
| 977 $vt = self::get_vtimezone($tzid, $range[0], $range[1], $vcal); | |
| 978 if (empty($vt)) { | |
| 979 continue; // no timezone information found | |
| 980 } | |
| 981 | |
| 982 if ($write) { | |
| 983 echo $vt->serialize(); | |
| 984 } | |
| 985 else { | |
| 986 $vcal->add($vt); | |
| 987 } | |
| 988 } | |
| 989 } | |
| 990 | |
| 991 if ($write) { | |
| 992 echo "END:VCALENDAR\r\n"; | |
| 993 return true; | |
| 994 } | |
| 995 else { | |
| 996 return $vcal->serialize(); | |
| 997 } | |
| 998 } | |
| 999 | |
| 1000 /** | |
| 1001 * Build a valid iCal format block from the given event | |
| 1002 * | |
| 1003 * @param array Hash array with event/task properties from libkolab | |
| 1004 * @param object VCalendar object to append event to or false for directly sending data to stdout | |
| 1005 * @param callable Callback function to fetch attachment contents, false if no attachment export | |
| 1006 * @param object RECURRENCE-ID property when serializing a recurrence exception | |
| 1007 */ | |
| 1008 private function _to_ical($event, $vcal, $get_attachment, $recurrence_id = null) | |
| 1009 { | |
| 1010 $type = $event['_type'] ?: 'event'; | |
| 1011 | |
| 1012 $cal = $vcal ?: new VObject\Component\VCalendar(); | |
| 1013 $ve = $cal->create($this->type_component_map[$type]); | |
| 1014 $ve->UID = $event['uid']; | |
| 1015 | |
| 1016 // set DTSTAMP according to RFC 5545, 3.8.7.2. | |
| 1017 $dtstamp = !empty($event['changed']) && empty($this->method) ? $event['changed'] : new DateTime('now', new \DateTimeZone('UTC')); | |
| 1018 $ve->DTSTAMP = $this->datetime_prop($cal, 'DTSTAMP', $dtstamp, true); | |
| 1019 | |
| 1020 // all-day events end the next day | |
| 1021 if ($event['allday'] && !empty($event['end'])) { | |
| 1022 $event['end'] = clone $event['end']; | |
| 1023 $event['end']->add(new \DateInterval('P1D')); | |
| 1024 $event['end']->_dateonly = true; | |
| 1025 } | |
| 1026 if (!empty($event['created'])) | |
| 1027 $ve->add($this->datetime_prop($cal, 'CREATED', $event['created'], true)); | |
| 1028 if (!empty($event['changed'])) | |
| 1029 $ve->add($this->datetime_prop($cal, 'LAST-MODIFIED', $event['changed'], true)); | |
| 1030 if (!empty($event['start'])) | |
| 1031 $ve->add($this->datetime_prop($cal, 'DTSTART', $event['start'], false, (bool)$event['allday'])); | |
| 1032 if (!empty($event['end'])) | |
| 1033 $ve->add($this->datetime_prop($cal, 'DTEND', $event['end'], false, (bool)$event['allday'])); | |
| 1034 if (!empty($event['due'])) | |
| 1035 $ve->add($this->datetime_prop($cal, 'DUE', $event['due'], false)); | |
| 1036 | |
| 1037 // we're exporting a recurrence instance only | |
| 1038 if (!$recurrence_id && $event['recurrence_date'] && $event['recurrence_date'] instanceof DateTime) { | |
| 1039 $recurrence_id = $this->datetime_prop($cal, 'RECURRENCE-ID', $event['recurrence_date'], false, (bool)$event['allday']); | |
| 1040 if ($event['thisandfuture']) | |
| 1041 $recurrence_id->add('RANGE', 'THISANDFUTURE'); | |
| 1042 } | |
| 1043 | |
| 1044 if ($recurrence_id) { | |
| 1045 $ve->add($recurrence_id); | |
| 1046 } | |
| 1047 | |
| 1048 $ve->add('SUMMARY', $event['title']); | |
| 1049 | |
| 1050 if ($event['location']) | |
| 1051 $ve->add($this->is_apple() ? new vobject_location_property($cal, 'LOCATION', $event['location']) : $cal->create('LOCATION', $event['location'])); | |
| 1052 if ($event['description']) | |
| 1053 $ve->add('DESCRIPTION', strtr($event['description'], array("\r\n" => "\n", "\r" => "\n"))); // normalize line endings | |
| 1054 | |
| 1055 if (isset($event['sequence'])) | |
| 1056 $ve->add('SEQUENCE', $event['sequence']); | |
| 1057 | |
| 1058 if ($event['recurrence'] && !$recurrence_id) { | |
| 1059 $exdates = $rdates = null; | |
| 1060 if (isset($event['recurrence']['EXDATE'])) { | |
| 1061 $exdates = $event['recurrence']['EXDATE']; | |
| 1062 unset($event['recurrence']['EXDATE']); // don't serialize EXDATEs into RRULE value | |
| 1063 } | |
| 1064 if (isset($event['recurrence']['RDATE'])) { | |
| 1065 $rdates = $event['recurrence']['RDATE']; | |
| 1066 unset($event['recurrence']['RDATE']); // don't serialize RDATEs into RRULE value | |
| 1067 } | |
| 1068 | |
| 1069 if ($event['recurrence']['FREQ']) { | |
| 1070 $ve->add('RRULE', libcalendaring::to_rrule($event['recurrence'], (bool)$event['allday'])); | |
| 1071 } | |
| 1072 | |
| 1073 // add EXDATEs each one per line (for Thunderbird Lightning) | |
| 1074 if (is_array($exdates)) { | |
| 1075 foreach ($exdates as $ex) { | |
| 1076 if ($ex instanceof \DateTime) { | |
| 1077 $exd = clone $event['start']; | |
| 1078 $exd->setDate($ex->format('Y'), $ex->format('n'), $ex->format('j')); | |
| 1079 $exd->setTimeZone(new \DateTimeZone('UTC')); | |
| 1080 $ve->add($this->datetime_prop($cal, 'EXDATE', $exd, true)); | |
| 1081 } | |
| 1082 } | |
| 1083 } | |
| 1084 // add RDATEs | |
| 1085 if (!empty($rdates)) { | |
| 1086 foreach ((array)$rdates as $rdate) { | |
| 1087 $ve->add($this->datetime_prop($cal, 'RDATE', $rdate)); | |
| 1088 } | |
| 1089 } | |
| 1090 } | |
| 1091 | |
| 1092 if ($event['categories']) { | |
| 1093 $cat = $cal->create('CATEGORIES'); | |
| 1094 $cat->setParts((array)$event['categories']); | |
| 1095 $ve->add($cat); | |
| 1096 } | |
| 1097 | |
| 1098 if (!empty($event['free_busy'])) { | |
| 1099 $ve->add('TRANSP', $event['free_busy'] == 'free' ? 'TRANSPARENT' : 'OPAQUE'); | |
| 1100 | |
| 1101 // for Outlook clients we provide the X-MICROSOFT-CDO-BUSYSTATUS property | |
| 1102 if (stripos($this->agent, 'outlook') !== false) { | |
| 1103 $ve->add('X-MICROSOFT-CDO-BUSYSTATUS', $event['free_busy'] == 'outofoffice' ? 'OOF' : strtoupper($event['free_busy'])); | |
| 1104 } | |
| 1105 } | |
| 1106 | |
| 1107 if ($event['priority']) | |
| 1108 $ve->add('PRIORITY', $event['priority']); | |
| 1109 | |
| 1110 if ($event['cancelled']) | |
| 1111 $ve->add('STATUS', 'CANCELLED'); | |
| 1112 else if ($event['free_busy'] == 'tentative') | |
| 1113 $ve->add('STATUS', 'TENTATIVE'); | |
| 1114 else if ($event['complete'] == 100) | |
| 1115 $ve->add('STATUS', 'COMPLETED'); | |
| 1116 else if (!empty($event['status'])) | |
| 1117 $ve->add('STATUS', $event['status']); | |
| 1118 | |
| 1119 if (!empty($event['sensitivity'])) | |
| 1120 $ve->add('CLASS', strtoupper($event['sensitivity'])); | |
| 1121 | |
| 1122 if (!empty($event['complete'])) { | |
| 1123 $ve->add('PERCENT-COMPLETE', intval($event['complete'])); | |
| 1124 } | |
| 1125 | |
| 1126 // Apple iCal and BusyCal required the COMPLETED date to be set in order to consider a task complete | |
| 1127 if ($event['status'] == 'COMPLETED' || $event['complete'] == 100) { | |
| 1128 $ve->add($this->datetime_prop($cal, 'COMPLETED', $event['changed'] ?: new DateTime('now - 1 hour'), true)); | |
| 1129 } | |
| 1130 | |
| 1131 if ($event['valarms']) { | |
| 1132 foreach ($event['valarms'] as $alarm) { | |
| 1133 $va = $cal->createComponent('VALARM'); | |
| 1134 $va->action = $alarm['action']; | |
| 1135 if ($alarm['trigger'] instanceof DateTime) { | |
| 1136 $va->add($this->datetime_prop($cal, 'TRIGGER', $alarm['trigger'], true, null, true)); | |
| 1137 } | |
| 1138 else { | |
| 1139 $alarm_props = array(); | |
| 1140 if (strtoupper($alarm['related']) == 'END') { | |
| 1141 $alarm_props['RELATED'] = 'END'; | |
| 1142 } | |
| 1143 $va->add('TRIGGER', $alarm['trigger'], $alarm_props); | |
| 1144 } | |
| 1145 | |
| 1146 if ($alarm['action'] == 'EMAIL') { | |
| 1147 foreach ((array)$alarm['attendees'] as $attendee) { | |
| 1148 $va->add('ATTENDEE', 'mailto:' . $attendee); | |
| 1149 } | |
| 1150 } | |
| 1151 if ($alarm['description']) { | |
| 1152 $va->add('DESCRIPTION', $alarm['description'] ?: $event['title']); | |
| 1153 } | |
| 1154 if ($alarm['summary']) { | |
| 1155 $va->add('SUMMARY', $alarm['summary']); | |
| 1156 } | |
| 1157 if ($alarm['duration']) { | |
| 1158 $va->add('DURATION', $alarm['duration']); | |
| 1159 $va->add('REPEAT', intval($alarm['repeat'])); | |
| 1160 } | |
| 1161 if ($alarm['uri']) { | |
| 1162 $va->add('ATTACH', $alarm['uri'], array('VALUE' => 'URI')); | |
| 1163 } | |
| 1164 $ve->add($va); | |
| 1165 } | |
| 1166 } | |
| 1167 // legacy support | |
| 1168 else if ($event['alarms']) { | |
| 1169 $va = $cal->createComponent('VALARM'); | |
| 1170 list($trigger, $va->action) = explode(':', $event['alarms']); | |
| 1171 $val = libcalendaring::parse_alarm_value($trigger); | |
| 1172 if ($val[3]) | |
| 1173 $va->add('TRIGGER', $val[3]); | |
| 1174 else if ($val[0] instanceof DateTime) | |
| 1175 $va->add($this->datetime_prop($cal, 'TRIGGER', $val[0], true, null, true)); | |
| 1176 $ve->add($va); | |
| 1177 } | |
| 1178 | |
| 1179 // Find SCHEDULE-AGENT | |
| 1180 foreach ((array)$event['x-custom'] as $prop) { | |
| 1181 if ($prop[0] === 'SCHEDULE-AGENT') { | |
| 1182 $schedule_agent = $prop[1]; | |
| 1183 } | |
| 1184 } | |
| 1185 | |
| 1186 foreach ((array)$event['attendees'] as $attendee) { | |
| 1187 if ($attendee['role'] == 'ORGANIZER') { | |
| 1188 if (empty($event['organizer'])) | |
| 1189 $event['organizer'] = $attendee; | |
| 1190 } | |
| 1191 else if (!empty($attendee['email'])) { | |
| 1192 if (isset($attendee['rsvp'])) | |
| 1193 $attendee['rsvp'] = $attendee['rsvp'] ? 'TRUE' : null; | |
| 1194 | |
| 1195 $mailto = $attendee['email']; | |
| 1196 $attendee = array_filter(self::map_keys($attendee, $this->attendee_keymap)); | |
| 1197 | |
| 1198 if ($schedule_agent !== null && !isset($attendee['SCHEDULE-AGENT'])) { | |
| 1199 $attendee['SCHEDULE-AGENT'] = $schedule_agent; | |
| 1200 } | |
| 1201 | |
| 1202 $ve->add('ATTENDEE', 'mailto:' . $mailto, $attendee); | |
| 1203 } | |
| 1204 } | |
| 1205 | |
| 1206 if ($event['organizer']) { | |
| 1207 $organizer = array_filter(self::map_keys($event['organizer'], $this->organizer_keymap)); | |
| 1208 | |
| 1209 if ($schedule_agent !== null && !isset($organizer['SCHEDULE-AGENT'])) { | |
| 1210 $organizer['SCHEDULE-AGENT'] = $schedule_agent; | |
| 1211 } | |
| 1212 | |
| 1213 $ve->add('ORGANIZER', 'mailto:' . $event['organizer']['email'], $organizer); | |
| 1214 } | |
| 1215 | |
| 1216 foreach ((array)$event['url'] as $url) { | |
| 1217 if (!empty($url)) { | |
| 1218 $ve->add('URL', $url); | |
| 1219 } | |
| 1220 } | |
| 1221 | |
| 1222 if (!empty($event['parent_id'])) { | |
| 1223 $ve->add('RELATED-TO', $event['parent_id'], array('RELTYPE' => 'PARENT')); | |
| 1224 } | |
| 1225 | |
| 1226 if ($event['comment']) | |
| 1227 $ve->add('COMMENT', $event['comment']); | |
| 1228 | |
| 1229 $memory_limit = parse_bytes(ini_get('memory_limit')); | |
| 1230 | |
| 1231 // export attachments | |
| 1232 if (!empty($event['attachments'])) { | |
| 1233 foreach ((array)$event['attachments'] as $attach) { | |
| 1234 // check available memory and skip attachment export if we can't buffer it | |
| 1235 // @todo: use rcube_utils::mem_check() | |
| 1236 if (is_callable($get_attachment) && $memory_limit > 0 && ($memory_used = function_exists('memory_get_usage') ? memory_get_usage() : 16*1024*1024) | |
| 1237 && $attach['size'] && $memory_used + $attach['size'] * 3 > $memory_limit) { | |
| 1238 continue; | |
| 1239 } | |
| 1240 // embed attachments using the given callback function | |
| 1241 if (is_callable($get_attachment) && ($data = call_user_func($get_attachment, $attach['id'], $event))) { | |
| 1242 // embed attachments for iCal | |
| 1243 $ve->add('ATTACH', | |
| 1244 $data, | |
| 1245 array_filter(array('VALUE' => 'BINARY', 'ENCODING' => 'BASE64', 'FMTTYPE' => $attach['mimetype'], 'X-LABEL' => $attach['name']))); | |
| 1246 unset($data); // attempt to free memory | |
| 1247 } | |
| 1248 // list attachments as absolute URIs | |
| 1249 else if (!empty($this->attach_uri)) { | |
| 1250 $ve->add('ATTACH', | |
| 1251 strtr($this->attach_uri, array( | |
| 1252 '{{id}}' => urlencode($attach['id']), | |
| 1253 '{{name}}' => urlencode($attach['name']), | |
| 1254 '{{mimetype}}' => urlencode($attach['mimetype']), | |
| 1255 )), | |
| 1256 array('FMTTYPE' => $attach['mimetype'], 'VALUE' => 'URI')); | |
| 1257 } | |
| 1258 } | |
| 1259 } | |
| 1260 | |
| 1261 foreach ((array)$event['links'] as $uri) { | |
| 1262 $ve->add('ATTACH', $uri); | |
| 1263 } | |
| 1264 | |
| 1265 // add custom properties | |
| 1266 foreach ((array)$event['x-custom'] as $prop) { | |
| 1267 $ve->add($prop[0], $prop[1]); | |
| 1268 } | |
| 1269 | |
| 1270 // append to vcalendar container | |
| 1271 if ($vcal) { | |
| 1272 $vcal->add($ve); | |
| 1273 } | |
| 1274 else { // serialize and send to stdout | |
| 1275 echo $ve->serialize(); | |
| 1276 } | |
| 1277 | |
| 1278 // append recurrence exceptions | |
| 1279 if (is_array($event['recurrence']) && $event['recurrence']['EXCEPTIONS']) { | |
| 1280 foreach ($event['recurrence']['EXCEPTIONS'] as $ex) { | |
| 1281 $exdate = $ex['recurrence_date'] ?: $ex['start']; | |
| 1282 $recurrence_id = $this->datetime_prop($cal, 'RECURRENCE-ID', $exdate, false, (bool)$event['allday']); | |
| 1283 if ($ex['thisandfuture']) | |
| 1284 $recurrence_id->add('RANGE', 'THISANDFUTURE'); | |
| 1285 $this->_to_ical($ex, $vcal, $get_attachment, $recurrence_id); | |
| 1286 } | |
| 1287 } | |
| 1288 } | |
| 1289 | |
| 1290 /** | |
| 1291 * Returns a VTIMEZONE component for a Olson timezone identifier | |
| 1292 * with daylight transitions covering the given date range. | |
| 1293 * | |
| 1294 * @param string Timezone ID as used in PHP's Date functions | |
| 1295 * @param integer Unix timestamp with first date/time in this timezone | |
| 1296 * @param integer Unix timestap with last date/time in this timezone | |
| 1297 * | |
| 1298 * @return mixed A Sabre\VObject\Component object representing a VTIMEZONE definition | |
| 1299 * or false if no timezone information is available | |
| 1300 */ | |
| 1301 public static function get_vtimezone($tzid, $from = 0, $to = 0, $cal = null) | |
| 1302 { | |
| 1303 if (!$from) $from = time(); | |
| 1304 if (!$to) $to = $from; | |
| 1305 if (!$cal) $cal = new VObject\Component\VCalendar(); | |
| 1306 | |
| 1307 if (is_string($tzid)) { | |
| 1308 try { | |
| 1309 $tz = new \DateTimeZone($tzid); | |
| 1310 } | |
| 1311 catch (\Exception $e) { | |
| 1312 return false; | |
| 1313 } | |
| 1314 } | |
| 1315 else if (is_a($tzid, '\\DateTimeZone')) { | |
| 1316 $tz = $tzid; | |
| 1317 } | |
| 1318 | |
| 1319 if (!is_a($tz, '\\DateTimeZone')) { | |
| 1320 return false; | |
| 1321 } | |
| 1322 | |
| 1323 $year = 86400 * 360; | |
| 1324 $transitions = $tz->getTransitions($from - $year, $to + $year); | |
| 1325 | |
| 1326 $vt = $cal->createComponent('VTIMEZONE'); | |
| 1327 $vt->TZID = $tz->getName(); | |
| 1328 | |
| 1329 $std = null; $dst = null; | |
| 1330 foreach ($transitions as $i => $trans) { | |
| 1331 $cmp = null; | |
| 1332 | |
| 1333 if ($i == 0) { | |
| 1334 $tzfrom = $trans['offset'] / 3600; | |
| 1335 continue; | |
| 1336 } | |
| 1337 | |
| 1338 if ($trans['isdst']) { | |
| 1339 $t_dst = $trans['ts']; | |
| 1340 $dst = $cal->createComponent('DAYLIGHT'); | |
| 1341 $cmp = $dst; | |
| 1342 } | |
| 1343 else { | |
| 1344 $t_std = $trans['ts']; | |
| 1345 $std = $cal->createComponent('STANDARD'); | |
| 1346 $cmp = $std; | |
| 1347 } | |
| 1348 | |
| 1349 if ($cmp) { | |
| 1350 $dt = new DateTime($trans['time']); | |
| 1351 $offset = $trans['offset'] / 3600; | |
| 1352 | |
| 1353 $cmp->DTSTART = $dt->format('Ymd\THis'); | |
| 1354 $cmp->TZOFFSETFROM = sprintf('%+03d%02d', floor($tzfrom), ($tzfrom - floor($tzfrom)) * 60); | |
| 1355 $cmp->TZOFFSETTO = sprintf('%+03d%02d', floor($offset), ($offset - floor($offset)) * 60); | |
| 1356 | |
| 1357 if (!empty($trans['abbr'])) { | |
| 1358 $cmp->TZNAME = $trans['abbr']; | |
| 1359 } | |
| 1360 | |
| 1361 $tzfrom = $offset; | |
| 1362 $vt->add($cmp); | |
| 1363 } | |
| 1364 | |
| 1365 // we covered the entire date range | |
| 1366 if ($std && $dst && min($t_std, $t_dst) < $from && max($t_std, $t_dst) > $to) { | |
| 1367 break; | |
| 1368 } | |
| 1369 } | |
| 1370 | |
| 1371 // add X-MICROSOFT-CDO-TZID if available | |
| 1372 $microsoftExchangeMap = array_flip(VObject\TimeZoneUtil::$microsoftExchangeMap); | |
| 1373 if (array_key_exists($tz->getName(), $microsoftExchangeMap)) { | |
| 1374 $vt->add('X-MICROSOFT-CDO-TZID', $microsoftExchangeMap[$tz->getName()]); | |
| 1375 } | |
| 1376 | |
| 1377 return $vt; | |
| 1378 } | |
| 1379 | |
| 1380 | |
| 1381 /*** Implement PHP 5 Iterator interface to make foreach work ***/ | |
| 1382 | |
| 1383 function current() | |
| 1384 { | |
| 1385 return $this->objects[$this->iteratorkey]; | |
| 1386 } | |
| 1387 | |
| 1388 function key() | |
| 1389 { | |
| 1390 return $this->iteratorkey; | |
| 1391 } | |
| 1392 | |
| 1393 function next() | |
| 1394 { | |
| 1395 $this->iteratorkey++; | |
| 1396 | |
| 1397 // read next chunk if we're reading from a file | |
| 1398 if (!$this->objects[$this->iteratorkey] && $this->fp) { | |
| 1399 $this->_parse_next(true); | |
| 1400 } | |
| 1401 | |
| 1402 return $this->valid(); | |
| 1403 } | |
| 1404 | |
| 1405 function rewind() | |
| 1406 { | |
| 1407 $this->iteratorkey = 0; | |
| 1408 } | |
| 1409 | |
| 1410 function valid() | |
| 1411 { | |
| 1412 return !empty($this->objects[$this->iteratorkey]); | |
| 1413 } | |
| 1414 | |
| 1415 } | |
| 1416 |
