2011-05-20 19:04:25 +02:00
|
|
|
<?php
|
2011-08-21 12:48:33 +02:00
|
|
|
|
|
|
|
/**
|
2013-07-23 17:14:11 +02:00
|
|
|
* iCalendar functions for the libcalendaring plugin
|
2011-08-21 12:48:33 +02:00
|
|
|
*
|
2011-12-07 12:51:23 +01:00
|
|
|
* @author Thomas Bruederli <bruederli@kolabsys.com>
|
2011-08-21 12:48:33 +02:00
|
|
|
*
|
2013-02-21 17:41:41 +01:00
|
|
|
* Copyright (C) 2013, Kolab Systems AG <contact@kolabsys.com>
|
2011-08-21 12:48:33 +02:00
|
|
|
*
|
2011-10-27 10:20:46 +02:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
2011-08-21 12:48:33 +02:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2011-10-27 10:20:46 +02:00
|
|
|
* GNU Affero General Public License for more details.
|
2011-08-21 12:48:33 +02:00
|
|
|
*
|
2011-10-27 10:20:46 +02:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2011-08-21 12:48:33 +02:00
|
|
|
*/
|
2011-05-20 19:04:25 +02:00
|
|
|
|
2013-07-23 17:14:11 +02:00
|
|
|
use \Sabre\VObject;
|
2011-07-29 09:19:18 +02:00
|
|
|
|
2013-09-13 09:47:40 +02:00
|
|
|
// load Sabre\VObject classes
|
|
|
|
if (!class_exists('\Sabre\VObject\Reader')) {
|
|
|
|
require_once __DIR__ . '/lib/Sabre/VObject/includes.php';
|
|
|
|
}
|
|
|
|
|
2011-07-29 09:19:18 +02:00
|
|
|
/**
|
|
|
|
* Class to parse and build vCalendar (iCalendar) files
|
|
|
|
*
|
2013-07-23 17:14:11 +02:00
|
|
|
* Uses the SabreTooth VObject library, version 2.1.
|
|
|
|
*
|
|
|
|
* Download from https://github.com/fruux/sabre-vobject/archive/2.1.0.zip
|
|
|
|
* and place the lib files in this plugin's lib directory
|
2011-07-29 09:19:18 +02:00
|
|
|
*
|
|
|
|
*/
|
2013-10-17 13:10:19 +02:00
|
|
|
class libvcalendar implements Iterator
|
2011-05-20 19:04:25 +02:00
|
|
|
{
|
2013-07-23 17:14:11 +02:00
|
|
|
private $timezone;
|
|
|
|
private $attach_uri = null;
|
|
|
|
private $prodid = '-//Roundcube//Roundcube libcalendaring//Sabre//Sabre VObject//EN';
|
|
|
|
private $type_component_map = array('event' => 'VEVENT', 'task' => 'VTODO');
|
2014-02-27 23:32:15 +01:00
|
|
|
private $attendee_keymap = array('name' => 'CN', 'status' => 'PARTSTAT', 'role' => 'ROLE',
|
|
|
|
'cutype' => 'CUTYPE', 'rsvp' => 'RSVP', 'delegated-from' => 'DELEGATED-FROM', 'delegated-to' => 'DELEGATED-TO');
|
2013-10-17 13:10:19 +02:00
|
|
|
private $iteratorkey = 0;
|
|
|
|
private $charset;
|
|
|
|
private $forward_exceptions;
|
2014-02-28 12:41:31 +01:00
|
|
|
private $vhead;
|
2013-10-17 13:10:19 +02:00
|
|
|
private $fp;
|
2013-07-23 17:14:11 +02:00
|
|
|
|
|
|
|
public $method;
|
2013-09-12 10:57:22 +02:00
|
|
|
public $agent = '';
|
2013-07-23 17:14:11 +02:00
|
|
|
public $objects = array();
|
2013-08-24 16:43:02 +02:00
|
|
|
public $freebusy = array();
|
2013-07-23 17:14:11 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Default constructor
|
|
|
|
*/
|
|
|
|
function __construct($tz = null)
|
|
|
|
{
|
2013-07-24 18:05:04 +02:00
|
|
|
$this->timezone = $tz;
|
2013-07-23 17:14:11 +02:00
|
|
|
$this->prodid = '-//Roundcube//Roundcube libcalendaring ' . RCUBE_VERSION . '//Sabre//Sabre VObject ' . VObject\Version::VERSION . '//EN';
|
2011-07-29 09:19:18 +02:00
|
|
|
}
|
2013-07-23 17:14:11 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Setter for timezone information
|
|
|
|
*/
|
|
|
|
public function set_timezone($tz)
|
|
|
|
{
|
|
|
|
$this->timezone = $tz;
|
2011-10-12 19:44:38 +02:00
|
|
|
}
|
|
|
|
|
2013-07-23 17:14:11 +02:00
|
|
|
/**
|
|
|
|
* Setter for URI template for attachment links
|
|
|
|
*/
|
|
|
|
public function set_attach_uri($uri)
|
|
|
|
{
|
|
|
|
$this->attach_uri = $uri;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setter for a custom PRODID attribute
|
|
|
|
*/
|
|
|
|
public function set_prodid($prodid)
|
|
|
|
{
|
|
|
|
$this->prodid = $prodid;
|
|
|
|
}
|
|
|
|
|
2013-09-12 10:57:22 +02:00
|
|
|
/**
|
|
|
|
* Setter for a user-agent string to tweak input/output accordingly
|
|
|
|
*/
|
|
|
|
public function set_agent($agent)
|
|
|
|
{
|
|
|
|
$this->agent = $agent;
|
|
|
|
}
|
|
|
|
|
2013-07-23 17:14:11 +02:00
|
|
|
/**
|
|
|
|
* Free resources by clearing member vars
|
|
|
|
*/
|
|
|
|
public function reset()
|
|
|
|
{
|
2014-02-28 12:41:31 +01:00
|
|
|
$this->vhead = '';
|
2013-07-23 17:14:11 +02:00
|
|
|
$this->method = '';
|
|
|
|
$this->objects = array();
|
2013-10-17 13:10:19 +02:00
|
|
|
$this->freebusy = array();
|
|
|
|
$this->iteratorkey = 0;
|
|
|
|
|
|
|
|
if ($this->fp) {
|
|
|
|
fclose($this->fp);
|
|
|
|
$this->fp = null;
|
|
|
|
}
|
2013-07-23 17:14:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Import events from iCalendar format
|
|
|
|
*
|
|
|
|
* @param string vCalendar input
|
|
|
|
* @param string Input charset (from envelope)
|
2013-09-19 11:01:13 +02:00
|
|
|
* @param boolean True if parsing exceptions should be forwarded to the caller
|
2013-07-23 17:14:11 +02:00
|
|
|
* @return array List of events extracted from the input
|
|
|
|
*/
|
2013-10-17 13:10:19 +02:00
|
|
|
public function import($vcal, $charset = 'UTF-8', $forward_exceptions = false, $memcheck = true)
|
2013-07-23 17:14:11 +02:00
|
|
|
{
|
|
|
|
// TODO: convert charset to UTF-8 if other
|
|
|
|
|
|
|
|
try {
|
2013-10-16 12:46:13 +02:00
|
|
|
// estimate the memory usage and try to avoid fatal errors when allowed memory gets exhausted
|
2013-10-17 13:10:19 +02:00
|
|
|
if ($memcheck) {
|
2014-03-18 12:08:54 +01:00
|
|
|
$count = substr_count($vcal, 'BEGIN:VEVENT') + substr_count($vcal, 'BEGIN:VTODO');
|
2013-10-17 13:10:19 +02:00
|
|
|
$expected_memory = $count * 70*1024; // assume ~ 70K per event (empirically determined)
|
2013-10-16 12:46:13 +02:00
|
|
|
|
2013-10-17 13:10:19 +02:00
|
|
|
if (!rcube_utils::mem_check($expected_memory)) {
|
|
|
|
throw new Exception("iCal file too big");
|
|
|
|
}
|
2013-10-16 12:46:13 +02:00
|
|
|
}
|
|
|
|
|
2013-07-23 17:14:11 +02:00
|
|
|
$vobject = VObject\Reader::read($vcal, VObject\Reader::OPTION_FORGIVING | VObject\Reader::OPTION_IGNORE_INVALID_LINES);
|
|
|
|
if ($vobject)
|
|
|
|
return $this->import_from_vobject($vobject);
|
|
|
|
}
|
|
|
|
catch (Exception $e) {
|
2013-09-19 11:01:13 +02:00
|
|
|
if ($forward_exceptions) {
|
|
|
|
throw $e;
|
|
|
|
}
|
2013-10-17 13:10:19 +02:00
|
|
|
else {
|
|
|
|
rcube::raise_error(array(
|
|
|
|
'code' => 600, 'type' => 'php',
|
|
|
|
'file' => __FILE__, 'line' => __LINE__,
|
|
|
|
'message' => "iCal data parse error: " . $e->getMessage()),
|
|
|
|
true, false);
|
|
|
|
}
|
2011-11-11 13:22:40 +01:00
|
|
|
}
|
2013-07-23 17:14:11 +02:00
|
|
|
|
|
|
|
return array();
|
2011-10-12 19:44:38 +02:00
|
|
|
}
|
|
|
|
|
2013-07-23 17:14:11 +02:00
|
|
|
/**
|
|
|
|
* Read iCalendar events from a file
|
|
|
|
*
|
2013-09-19 11:01:13 +02:00
|
|
|
* @param string File path to read from
|
|
|
|
* @param string Input charset (from envelope)
|
|
|
|
* @param boolean True if parsing exceptions should be forwarded to the caller
|
2013-07-23 17:14:11 +02:00
|
|
|
* @return array List of events extracted from the file
|
|
|
|
*/
|
2013-09-19 11:01:13 +02:00
|
|
|
public function import_from_file($filepath, $charset = 'UTF-8', $forward_exceptions = false)
|
2013-07-23 17:14:11 +02:00
|
|
|
{
|
2013-10-17 13:10:19 +02:00
|
|
|
if ($this->fopen($filepath, $charset, $forward_exceptions)) {
|
|
|
|
while ($this->_parse_next(false)) {
|
|
|
|
// nop
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose($this->fp);
|
|
|
|
$this->fp = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->objects;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Open a file to read iCalendar events sequentially
|
|
|
|
*
|
|
|
|
* @param string File path to read from
|
|
|
|
* @param string Input charset (from envelope)
|
|
|
|
* @param boolean True if parsing exceptions should be forwarded to the caller
|
|
|
|
* @return boolean True if file contents are considered valid
|
|
|
|
*/
|
|
|
|
public function fopen($filepath, $charset = 'UTF-8', $forward_exceptions = false)
|
|
|
|
{
|
|
|
|
$this->reset();
|
|
|
|
|
|
|
|
// just to be sure...
|
|
|
|
@ini_set('auto_detect_line_endings', true);
|
|
|
|
|
|
|
|
$this->charset = $charset;
|
|
|
|
$this->forward_exceptions = $forward_exceptions;
|
|
|
|
$this->fp = fopen($filepath, 'r');
|
2013-07-23 17:14:11 +02:00
|
|
|
|
|
|
|
// check file content first
|
2013-10-17 13:10:19 +02:00
|
|
|
$begin = fread($this->fp, 1024);
|
2013-07-23 17:14:11 +02:00
|
|
|
if (!preg_match('/BEGIN:VCALENDAR/i', $begin)) {
|
2013-10-17 13:10:19 +02:00
|
|
|
return false;
|
2013-07-23 17:14:11 +02:00
|
|
|
}
|
|
|
|
|
2013-10-17 13:10:19 +02:00
|
|
|
fseek($this->fp, 0);
|
|
|
|
return $this->_parse_next();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse the next event/todo/freebusy object from the input file
|
|
|
|
*/
|
|
|
|
private function _parse_next($reset = true)
|
|
|
|
{
|
|
|
|
if ($reset) {
|
|
|
|
$this->iteratorkey = 0;
|
|
|
|
$this->objects = array();
|
|
|
|
$this->freebusy = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$next = $this->_next_component();
|
|
|
|
$buffer = $next;
|
|
|
|
|
|
|
|
// load the next component(s) too, as they could contain recurrence exceptions
|
|
|
|
while (preg_match('/(RRULE|RECURRENCE-ID)[:;]/i', $next)) {
|
|
|
|
$next = $this->_next_component();
|
|
|
|
$buffer .= $next;
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse the vevent block surrounded with the vcalendar heading
|
|
|
|
if (strlen($buffer) && preg_match('/BEGIN:(VEVENT|VTODO|VFREEBUSY)/i', $buffer)) {
|
|
|
|
try {
|
|
|
|
$this->import($this->vhead . $buffer . "END:VCALENDAR", $this->charset, true, false);
|
|
|
|
}
|
|
|
|
catch (Exception $e) {
|
|
|
|
if ($this->forward_exceptions) {
|
|
|
|
throw new VObject\ParseException($e->getMessage() . " in\n" . $buffer);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// write the failing section to error log
|
|
|
|
rcube::raise_error(array(
|
|
|
|
'code' => 600, 'type' => 'php',
|
|
|
|
'file' => __FILE__, 'line' => __LINE__,
|
|
|
|
'message' => $e->getMessage() . " in\n" . $buffer),
|
|
|
|
true, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// advance to next
|
|
|
|
return $this->_parse_next($reset);
|
|
|
|
}
|
|
|
|
|
|
|
|
return count($this->objects) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method to read the next calendar component from the file
|
|
|
|
*/
|
|
|
|
private function _next_component()
|
|
|
|
{
|
|
|
|
$buffer = '';
|
2014-02-28 12:41:31 +01:00
|
|
|
$vcalendar_head = false;
|
2013-10-17 13:10:19 +02:00
|
|
|
while (($line = fgets($this->fp, 1024)) !== false) {
|
2014-02-28 12:41:31 +01:00
|
|
|
// ignore END:VCALENDAR lines
|
|
|
|
if (preg_match('/END:VCALENDAR/i', $line)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// read vcalendar header (with timezone defintion)
|
|
|
|
if (preg_match('/BEGIN:VCALENDAR/i', $line)) {
|
|
|
|
$this->vhead = '';
|
|
|
|
$vcalendar_head = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// end of VCALENDAR header part
|
|
|
|
if ($vcalendar_head && preg_match('/BEGIN:(VEVENT|VTODO|VFREEBUSY)/i', $line)) {
|
|
|
|
$vcalendar_head = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($vcalendar_head) {
|
|
|
|
$this->vhead .= $line;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$buffer .= $line;
|
|
|
|
if (preg_match('/END:(VEVENT|VTODO|VFREEBUSY)/i', $line)) {
|
|
|
|
break;
|
|
|
|
}
|
2013-10-17 13:10:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $buffer;
|
2011-07-29 17:51:04 +02:00
|
|
|
}
|
2011-10-12 22:39:14 +02:00
|
|
|
|
2013-07-23 17:14:11 +02:00
|
|
|
/**
|
|
|
|
* Import objects from an already parsed Sabre\VObject\Component object
|
|
|
|
*
|
|
|
|
* @param object Sabre\VObject\Component to read from
|
|
|
|
* @return array List of events extracted from the file
|
|
|
|
*/
|
|
|
|
public function import_from_vobject($vobject)
|
|
|
|
{
|
2013-10-17 13:10:19 +02:00
|
|
|
$seen = array();
|
2013-07-23 17:14:11 +02:00
|
|
|
|
|
|
|
if ($vobject->name == 'VCALENDAR') {
|
|
|
|
$this->method = strval($vobject->METHOD);
|
2013-09-12 10:57:22 +02:00
|
|
|
$this->agent = strval($vobject->PRODID);
|
2013-07-23 17:14:11 +02:00
|
|
|
|
2014-02-06 10:54:57 +01:00
|
|
|
foreach ($vobject->getBaseComponents() ?: $vobject->getComponents() as $ve) {
|
2013-07-23 17:14:11 +02:00
|
|
|
if ($ve->name == 'VEVENT' || $ve->name == 'VTODO') {
|
|
|
|
// convert to hash array representation
|
|
|
|
$object = $this->_to_array($ve);
|
|
|
|
|
|
|
|
if (!$seen[$object['uid']]++) {
|
|
|
|
// parse recurrence exceptions
|
|
|
|
if ($object['recurrence']) {
|
|
|
|
foreach ($vobject->children as $i => $component) {
|
|
|
|
if ($component->name == 'VEVENT' && isset($component->{'RECURRENCE-ID'})) {
|
|
|
|
$object['recurrence']['EXCEPTIONS'][] = $this->_to_array($component);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->objects[] = $object;
|
|
|
|
}
|
|
|
|
}
|
2013-08-24 16:43:02 +02:00
|
|
|
else if ($ve->name == 'VFREEBUSY') {
|
2013-08-25 12:35:41 +02:00
|
|
|
$this->objects[] = $this->_parse_freebusy($ve);
|
2013-08-24 16:43:02 +02:00
|
|
|
}
|
2011-10-12 22:39:14 +02:00
|
|
|
}
|
2013-07-23 17:14:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->objects;
|
|
|
|
}
|
|
|
|
|
2013-08-24 16:43:02 +02:00
|
|
|
/**
|
|
|
|
* Getter for free-busy periods
|
|
|
|
*/
|
|
|
|
public function get_busy_periods()
|
|
|
|
{
|
|
|
|
$out = array();
|
|
|
|
foreach ((array)$this->freebusy['periods'] as $period) {
|
|
|
|
if ($period[2] != 'FREE') {
|
|
|
|
$out[] = $period;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
2013-09-12 10:57:22 +02:00
|
|
|
/**
|
|
|
|
* Helper method to determine whether the connected client is an Apple device
|
|
|
|
*/
|
|
|
|
private function is_apple()
|
|
|
|
{
|
|
|
|
return stripos($this->agent, 'Apple') !== false
|
|
|
|
|| stripos($this->agent, 'Mac OS X') !== false
|
|
|
|
|| stripos($this->agent, 'iOS/') !== false;
|
|
|
|
}
|
|
|
|
|
2013-07-23 17:14:11 +02:00
|
|
|
/**
|
|
|
|
* Convert the given VEvent object to a libkolab compatible array representation
|
|
|
|
*
|
|
|
|
* @param object Vevent object to convert
|
|
|
|
* @return array Hash array with object properties
|
|
|
|
*/
|
|
|
|
private function _to_array($ve)
|
|
|
|
{
|
|
|
|
$event = array(
|
2014-02-28 16:12:24 +01:00
|
|
|
'uid' => self::convert_string($ve->UID),
|
|
|
|
'title' => self::convert_string($ve->SUMMARY),
|
2013-07-23 17:14:11 +02:00
|
|
|
'_type' => $ve->name == 'VTODO' ? 'task' : 'event',
|
|
|
|
// set defaults
|
|
|
|
'priority' => 0,
|
|
|
|
'attendees' => array(),
|
2013-09-19 11:01:13 +02:00
|
|
|
'x-custom' => array(),
|
2013-07-23 17:14:11 +02:00
|
|
|
);
|
|
|
|
|
2013-08-30 14:58:16 +02:00
|
|
|
// Catch possible exceptions when date is invalid (Bug #2144)
|
|
|
|
// We can skip these fields, they aren't critical
|
2013-09-04 09:19:26 +02:00
|
|
|
foreach (array('CREATED' => 'created', 'LAST-MODIFIED' => 'changed', 'DTSTAMP' => 'changed') as $attr => $field) {
|
2013-08-30 14:58:16 +02:00
|
|
|
try {
|
2013-09-04 09:19:26 +02:00
|
|
|
if (!$event[$field] && $ve->{$attr}) {
|
|
|
|
$event[$field] = $ve->{$attr}->getDateTime();
|
|
|
|
}
|
|
|
|
} catch (Exception $e) {}
|
2013-07-23 17:14:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// map other attributes to internal fields
|
|
|
|
$_attendees = array();
|
|
|
|
foreach ($ve->children as $prop) {
|
|
|
|
if (!($prop instanceof VObject\Property))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
switch ($prop->name) {
|
|
|
|
case 'DTSTART':
|
|
|
|
case 'DTEND':
|
|
|
|
case 'DUE':
|
|
|
|
$propmap = array('DTSTART' => 'start', 'DTEND' => 'end', 'DUE' => 'due');
|
|
|
|
$event[$propmap[$prop->name]] = self::convert_datetime($prop);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'TRANSP':
|
|
|
|
$event['free_busy'] = $prop->value == 'TRANSPARENT' ? 'free' : 'busy';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'STATUS':
|
|
|
|
if ($prop->value == 'TENTATIVE')
|
|
|
|
$event['free_busy'] = 'tentative';
|
|
|
|
else if ($prop->value == 'CANCELLED')
|
|
|
|
$event['cancelled'] = true;
|
|
|
|
else if ($prop->value == 'COMPLETED')
|
|
|
|
$event['complete'] = 100;
|
2014-04-03 15:07:47 +02:00
|
|
|
else
|
|
|
|
$event['status'] = strval($prop->value);
|
2013-07-23 17:14:11 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'PRIORITY':
|
|
|
|
if (is_numeric($prop->value))
|
|
|
|
$event['priority'] = $prop->value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'RRULE':
|
2014-05-15 12:21:47 +02:00
|
|
|
$params = is_array($event['recurrence']) ? $event['recurrence'] : array();
|
2013-07-23 17:14:11 +02:00
|
|
|
// parse recurrence rule attributes
|
|
|
|
foreach (explode(';', $prop->value) as $par) {
|
|
|
|
list($k, $v) = explode('=', $par);
|
|
|
|
$params[$k] = $v;
|
|
|
|
}
|
|
|
|
if ($params['UNTIL'])
|
|
|
|
$params['UNTIL'] = date_create($params['UNTIL']);
|
|
|
|
if (!$params['INTERVAL'])
|
|
|
|
$params['INTERVAL'] = 1;
|
|
|
|
|
|
|
|
$event['recurrence'] = $params;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'EXDATE':
|
2014-02-28 16:12:24 +01:00
|
|
|
$event['recurrence']['EXDATE'] = array_merge((array)$event['recurrence']['EXDATE'], self::convert_datetime($prop, true));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'RDATE':
|
|
|
|
$event['recurrence']['RDATE'] = array_merge((array)$event['recurrence']['RDATE'], self::convert_datetime($prop, true));
|
2013-07-23 17:14:11 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'RECURRENCE-ID':
|
2014-02-06 10:54:57 +01:00
|
|
|
$event['recurrence_date'] = self::convert_datetime($prop);
|
2013-07-23 17:14:11 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'RELATED-TO':
|
|
|
|
if ($prop->offsetGet('RELTYPE') == 'PARENT') {
|
|
|
|
$event['parent_id'] = $prop->value;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'SEQUENCE':
|
|
|
|
$event['sequence'] = intval($prop->value);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'PERCENT-COMPLETE':
|
|
|
|
$event['complete'] = intval($prop->value);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'LOCATION':
|
2013-09-12 10:57:22 +02:00
|
|
|
case 'DESCRIPTION':
|
2013-07-23 17:14:11 +02:00
|
|
|
case 'URL':
|
2014-02-28 16:12:24 +01:00
|
|
|
$event[strtolower($prop->name)] = self::convert_string($prop);
|
2013-07-23 17:14:11 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'CATEGORY':
|
|
|
|
case 'CATEGORIES':
|
|
|
|
$event['categories'] = $prop->getParts();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'CLASS':
|
|
|
|
case 'X-CALENDARSERVER-ACCESS':
|
|
|
|
$event['sensitivity'] = strtolower($prop->value);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'X-MICROSOFT-CDO-BUSYSTATUS':
|
|
|
|
if ($prop->value == 'OOF')
|
2014-01-23 15:38:27 +01:00
|
|
|
$event['free_busy'] = 'outofoffice';
|
2013-07-23 17:14:11 +02:00
|
|
|
else if (in_array($prop->value, array('FREE', 'BUSY', 'TENTATIVE')))
|
|
|
|
$event['free_busy'] = strtolower($prop->value);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'ATTENDEE':
|
|
|
|
case 'ORGANIZER':
|
|
|
|
$params = array();
|
|
|
|
foreach ($prop->parameters as $param) {
|
|
|
|
switch ($param->name) {
|
|
|
|
case 'RSVP': $params[$param->name] = strtolower($param->value) == 'true'; break;
|
|
|
|
default: $params[$param->name] = $param->value; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$attendee = self::map_keys($params, array_flip($this->attendee_keymap));
|
|
|
|
$attendee['email'] = preg_replace('/^mailto:/i', '', $prop->value);
|
|
|
|
|
|
|
|
if ($prop->name == 'ORGANIZER') {
|
|
|
|
$attendee['role'] = 'ORGANIZER';
|
|
|
|
$attendee['status'] = 'ACCEPTED';
|
|
|
|
$event['organizer'] = $attendee;
|
|
|
|
}
|
|
|
|
else if ($attendee['email'] != $event['organizer']['email']) {
|
|
|
|
$event['attendees'][] = $attendee;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'ATTACH':
|
|
|
|
$params = self::parameters_array($prop);
|
|
|
|
if (substr($prop->value, 0, 4) == 'http' && !strpos($prop->value, ':attachment:')) {
|
|
|
|
$event['links'][] = $prop->value;
|
|
|
|
}
|
|
|
|
else if (strlen($prop->value) && strtoupper($params['VALUE']) == 'BINARY') {
|
|
|
|
$attachment = self::map_keys($params, array('FMTTYPE' => 'mimetype', 'X-LABEL' => 'name'));
|
|
|
|
$attachment['data'] = base64_decode($prop->value);
|
2013-07-24 18:05:04 +02:00
|
|
|
$attachment['size'] = strlen($attachment['data']);
|
2013-07-23 17:14:11 +02:00
|
|
|
$event['attachments'][] = $attachment;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-03-18 18:05:48 +01:00
|
|
|
case 'COMMENT':
|
|
|
|
$event['comment'] = $prop->value;
|
|
|
|
break;
|
|
|
|
|
2013-07-23 17:14:11 +02:00
|
|
|
default:
|
|
|
|
if (substr($prop->name, 0, 2) == 'X-')
|
|
|
|
$event['x-custom'][] = array($prop->name, strval($prop->value));
|
|
|
|
break;
|
2011-10-12 22:39:14 +02:00
|
|
|
}
|
2013-07-23 17:14:11 +02:00
|
|
|
}
|
2011-10-12 22:39:14 +02:00
|
|
|
|
2013-07-23 17:14:11 +02:00
|
|
|
// check DURATION property if no end date is set
|
|
|
|
if (empty($event['end']) && $ve->DURATION) {
|
|
|
|
try {
|
|
|
|
$duration = new DateInterval(strval($ve->DURATION));
|
|
|
|
$end = clone $event['start'];
|
|
|
|
$end->add($duration);
|
|
|
|
$event['end'] = $end;
|
|
|
|
}
|
|
|
|
catch (\Exception $e) {
|
|
|
|
trigger_error(strval($e), E_USER_WARNING);
|
|
|
|
}
|
2011-10-12 22:39:14 +02:00
|
|
|
}
|
|
|
|
|
2013-07-24 18:59:09 +02:00
|
|
|
// validate event dates
|
|
|
|
if ($event['_type'] == 'event') {
|
|
|
|
// check for all-day dates
|
|
|
|
if ($event['start']->_dateonly) {
|
|
|
|
$event['allday'] = true;
|
|
|
|
}
|
2013-02-28 10:44:15 +01:00
|
|
|
|
2014-03-04 08:55:06 +01:00
|
|
|
// all-day events may lack the DTEND property
|
|
|
|
if ($event['allday'] && empty($event['end'])) {
|
|
|
|
$event['end'] = clone $event['start'];
|
|
|
|
}
|
2013-07-24 18:59:09 +02:00
|
|
|
// shift end-date by one day (except Thunderbird)
|
2014-03-04 08:55:06 +01:00
|
|
|
else if ($event['allday'] && is_object($event['end'])) {
|
2013-07-24 18:59:09 +02:00
|
|
|
$event['end']->sub(new \DateInterval('PT23H'));
|
|
|
|
}
|
2013-02-28 10:44:15 +01:00
|
|
|
|
2013-07-24 18:59:09 +02:00
|
|
|
// sanity-check and fix end date
|
2013-09-19 11:01:13 +02:00
|
|
|
if (!empty($event['end']) && $event['end'] < $event['start']) {
|
2013-07-24 18:59:09 +02:00
|
|
|
$event['end'] = clone $event['start'];
|
|
|
|
}
|
2013-07-23 17:14:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// make organizer part of the attendees list for compatibility reasons
|
|
|
|
if (!empty($event['organizer']) && is_array($event['attendees'])) {
|
|
|
|
array_unshift($event['attendees'], $event['organizer']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// find alarms
|
2013-10-29 14:55:02 +01:00
|
|
|
foreach ($ve->select('VALARM') as $valarm) {
|
2013-07-23 17:14:11 +02:00
|
|
|
$action = 'DISPLAY';
|
|
|
|
$trigger = null;
|
2014-04-03 17:39:26 +02:00
|
|
|
$alarm = array();
|
2013-07-23 17:14:11 +02:00
|
|
|
|
|
|
|
foreach ($valarm->children as $prop) {
|
|
|
|
switch ($prop->name) {
|
|
|
|
case 'TRIGGER':
|
|
|
|
foreach ($prop->parameters as $param) {
|
|
|
|
if ($param->name == 'VALUE' && $param->value == 'DATE-TIME') {
|
|
|
|
$trigger = '@' . $prop->getDateTime()->format('U');
|
2014-04-03 17:39:26 +02:00
|
|
|
$alarm['trigger'] = $prop->getDateTime();
|
2013-07-23 17:14:11 +02:00
|
|
|
}
|
|
|
|
}
|
2013-10-16 14:28:17 +02:00
|
|
|
if (!$trigger && ($values = libcalendaring::parse_alaram_value($prop->value))) {
|
|
|
|
$trigger = $values[2];
|
2014-04-03 18:38:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$alarm['trigger']) {
|
2014-04-03 17:39:26 +02:00
|
|
|
$alarm['trigger'] = rtrim(preg_replace('/([A-Z])0[WDHMS]/', '\\1', $prop->value), 'T');
|
2014-04-03 18:38:26 +02:00
|
|
|
// if all 0-values have been stripped, assume 'at time'
|
|
|
|
if ($alarm['trigger'] == 'P')
|
|
|
|
$alarm['trigger'] = 'PT0S';
|
2013-07-23 17:14:11 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'ACTION':
|
2014-04-03 17:39:26 +02:00
|
|
|
$action = $alarm['action'] = strtoupper($prop->value);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'SUMMARY':
|
|
|
|
case 'DESCRIPTION':
|
|
|
|
case 'DURATION':
|
|
|
|
$alarm[strtolower($prop->name)] = self::convert_string($prop);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'REPEAT':
|
|
|
|
$alarm['repeat'] = intval($prop->value);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'ATTENDEE':
|
|
|
|
$alarm['attendees'][] = preg_replace('/^mailto:/i', '', $prop->value);
|
2013-07-23 17:14:11 +02:00
|
|
|
break;
|
2014-04-03 19:19:14 +02:00
|
|
|
|
|
|
|
case 'ATTACH':
|
|
|
|
$params = self::parameters_array($prop);
|
|
|
|
if (strlen($prop->value) && (preg_match('/^[a-z]+:/', $prop->value) || strtoupper($params['VALUE']) == 'URI')) {
|
|
|
|
// we only support URI-type of attachments here
|
|
|
|
$alarm['uri'] = $prop->value;
|
|
|
|
}
|
|
|
|
break;
|
2013-07-23 17:14:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-03 18:38:26 +02:00
|
|
|
if ($action != 'NONE') {
|
|
|
|
if ($trigger && !$event['alarms']) // store first alarm in legacy property
|
2014-04-03 17:39:26 +02:00
|
|
|
$event['alarms'] = $trigger . ':' . $action;
|
2014-04-03 18:38:26 +02:00
|
|
|
|
|
|
|
if ($alarm['trigger'])
|
|
|
|
$event['valarms'][] = $alarm;
|
2013-10-29 14:55:02 +01:00
|
|
|
}
|
2013-07-23 17:14:11 +02:00
|
|
|
}
|
2013-07-24 18:59:09 +02:00
|
|
|
|
2013-07-23 17:14:11 +02:00
|
|
|
// assign current timezone to event start/end
|
|
|
|
if ($event['start'] instanceof DateTime) {
|
2013-07-24 18:05:04 +02:00
|
|
|
if ($this->timezone)
|
|
|
|
$event['start']->setTimezone($this->timezone);
|
2011-07-29 09:19:18 +02:00
|
|
|
}
|
|
|
|
else {
|
2013-07-23 17:14:11 +02:00
|
|
|
unset($event['start']);
|
2011-07-29 09:19:18 +02:00
|
|
|
}
|
2013-07-23 17:14:11 +02:00
|
|
|
|
|
|
|
if ($event['end'] instanceof DateTime) {
|
2013-07-24 18:05:04 +02:00
|
|
|
if ($this->timezone)
|
|
|
|
$event['end']->setTimezone($this->timezone);
|
2013-07-23 17:14:11 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
unset($event['end']);
|
2011-07-27 17:42:45 +02:00
|
|
|
}
|
|
|
|
|
2013-07-23 17:14:11 +02:00
|
|
|
// minimal validation
|
2013-09-19 11:01:13 +02:00
|
|
|
if (empty($event['uid']) || ($event['_type'] == 'event' && empty($event['start']) != empty($event['end']))) {
|
2013-07-23 17:14:11 +02:00
|
|
|
throw new VObject\ParseException('Object validation failed: missing mandatory object properties');
|
2011-05-20 19:04:25 +02:00
|
|
|
}
|
2013-07-23 17:14:11 +02:00
|
|
|
|
|
|
|
return $event;
|
|
|
|
}
|
|
|
|
|
2013-08-24 16:43:02 +02:00
|
|
|
/**
|
|
|
|
* Parse the given vfreebusy component into an array representation
|
|
|
|
*/
|
|
|
|
private function _parse_freebusy($ve)
|
|
|
|
{
|
2013-08-25 12:35:41 +02:00
|
|
|
$this->freebusy = array('_type' => 'freebusy', 'periods' => array());
|
|
|
|
$seen = array();
|
2013-08-24 16:43:02 +02:00
|
|
|
|
|
|
|
foreach ($ve->children as $prop) {
|
|
|
|
if (!($prop instanceof VObject\Property))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
switch ($prop->name) {
|
2014-05-19 12:18:39 +02:00
|
|
|
case 'CREATED':
|
|
|
|
case 'LAST-MODIFIED':
|
|
|
|
case 'DTSTAMP':
|
2013-08-24 16:43:02 +02:00
|
|
|
case 'DTSTART':
|
|
|
|
case 'DTEND':
|
2014-05-19 12:18:39 +02:00
|
|
|
$propmap = array('DTSTART' => 'start', 'DTEND' => 'end', 'CREATED' => 'created', 'LAST-MODIFIED' => 'changed', 'DTSTAMP' => 'changed');
|
2013-08-24 16:43:02 +02:00
|
|
|
$this->freebusy[$propmap[$prop->name]] = self::convert_datetime($prop);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'ORGANIZER':
|
|
|
|
$this->freebusy['organizer'] = preg_replace('/^mailto:/i', '', $prop->value);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'FREEBUSY':
|
|
|
|
// The freebusy component can hold more than 1 value, separated by commas.
|
|
|
|
$periods = explode(',', $prop->value);
|
|
|
|
$fbtype = strval($prop['FBTYPE']) ?: 'BUSY';
|
|
|
|
|
2013-08-25 12:35:41 +02:00
|
|
|
// skip dupes
|
|
|
|
if ($seen[$prop->value.':'.$fbtype]++)
|
|
|
|
continue;
|
|
|
|
|
2013-08-24 16:43:02 +02:00
|
|
|
foreach ($periods as $period) {
|
|
|
|
// Every period is formatted as [start]/[end]. The start is an
|
|
|
|
// absolute UTC time, the end may be an absolute UTC time, or
|
|
|
|
// duration (relative) value.
|
|
|
|
list($busyStart, $busyEnd) = explode('/', $period);
|
|
|
|
|
|
|
|
$busyStart = VObject\DateTimeParser::parse($busyStart);
|
|
|
|
$busyEnd = VObject\DateTimeParser::parse($busyEnd);
|
|
|
|
if ($busyEnd instanceof \DateInterval) {
|
|
|
|
$tmp = clone $busyStart;
|
|
|
|
$tmp->add($busyEnd);
|
|
|
|
$busyEnd = $tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($busyEnd && $busyEnd > $busyStart)
|
|
|
|
$this->freebusy['periods'][] = array($busyStart, $busyEnd, $fbtype);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'COMMENT':
|
|
|
|
$this->freebusy['comment'] = $prop->value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->freebusy;
|
|
|
|
}
|
|
|
|
|
2014-02-28 16:12:24 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function convert_string($prop)
|
|
|
|
{
|
|
|
|
return str_replace('\,', ',', strval($prop->value));
|
|
|
|
}
|
|
|
|
|
2013-07-23 17:14:11 +02:00
|
|
|
/**
|
|
|
|
* Helper method to correctly interpret an all-day date value
|
|
|
|
*/
|
2014-02-28 16:12:24 +01:00
|
|
|
public static function convert_datetime($prop, $as_array = false)
|
2013-07-23 17:14:11 +02:00
|
|
|
{
|
|
|
|
if (empty($prop)) {
|
2014-02-28 16:12:24 +01:00
|
|
|
return $as_array ? array() : null;
|
2013-05-16 13:32:01 +02:00
|
|
|
}
|
2013-07-23 17:14:11 +02:00
|
|
|
else if ($prop instanceof VObject\Property\MultiDateTime) {
|
|
|
|
$dt = array();
|
|
|
|
$dateonly = ($prop->getDateType() & VObject\Property\DateTime::DATE);
|
|
|
|
foreach ($prop->getDateTimes() as $item) {
|
|
|
|
$item->_dateonly = $dateonly;
|
|
|
|
$dt[] = $item;
|
|
|
|
}
|
2011-07-06 22:42:23 +02:00
|
|
|
}
|
2013-07-23 17:14:11 +02:00
|
|
|
else if ($prop instanceof VObject\Property\DateTime) {
|
|
|
|
$dt = $prop->getDateTime();
|
|
|
|
if ($prop->getDateType() & VObject\Property\DateTime::DATE) {
|
|
|
|
$dt->_dateonly = true;
|
|
|
|
}
|
2011-05-20 19:04:25 +02:00
|
|
|
}
|
2014-02-28 16:12:24 +01:00
|
|
|
else if ($prop instanceof VObject\Property && ($prop['VALUE'] == 'DATE' || $prop['VALUE'] == 'DATE-TIME')) {
|
|
|
|
try {
|
|
|
|
list($type, $dt) = VObject\Property\DateTime::parseData($prop->value, $prop);
|
|
|
|
$dt->_dateonly = ($type & VObject\Property\DateTime::DATE);
|
|
|
|
}
|
|
|
|
catch (Exception $e) {
|
|
|
|
// ignore date parse errors
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ($prop instanceof VObject\Property && $prop['VALUE'] == 'PERIOD') {
|
|
|
|
$dt = array();
|
|
|
|
foreach(explode(',', $prop->value) as $val) {
|
|
|
|
try {
|
|
|
|
list($start, $end) = explode('/', $val);
|
|
|
|
list($type, $item) = VObject\Property\DateTime::parseData($start, $prop);
|
|
|
|
$item->_dateonly = ($type & VObject\Property\DateTime::DATE);
|
|
|
|
$dt[] = $item;
|
|
|
|
}
|
|
|
|
catch (Exception $e) {
|
|
|
|
// ignore single date parse errors
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-23 17:14:11 +02:00
|
|
|
else if ($prop instanceof DateTime) {
|
|
|
|
$dt = $prop;
|
2011-07-06 22:42:23 +02:00
|
|
|
}
|
2013-07-23 17:14:11 +02:00
|
|
|
|
2014-02-28 16:12:24 +01:00
|
|
|
// force return value to array if requested
|
|
|
|
if ($as_array && !is_array($dt)) {
|
|
|
|
$dt = empty($dt) ? array() : array($dt);
|
|
|
|
}
|
|
|
|
|
2013-07-23 17:14:11 +02:00
|
|
|
return $dt;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a Sabre\VObject\Property instance from a PHP DateTime object
|
|
|
|
*
|
|
|
|
* @param string Property name
|
|
|
|
* @param object DateTime
|
|
|
|
*/
|
2013-09-12 10:01:56 +02:00
|
|
|
public static function datetime_prop($name, $dt, $utc = false, $dateonly = null)
|
2013-07-23 17:14:11 +02:00
|
|
|
{
|
2013-09-25 17:12:27 +02:00
|
|
|
$is_utc = $utc || (($tz = $dt->getTimezone()) && in_array($tz->getName(), array('UTC','GMT','Z')));
|
2013-10-03 10:54:05 +02:00
|
|
|
$is_dateonly = $dateonly === null ? (bool)$dt->_dateonly : (bool)$dateonly;
|
2013-07-23 17:14:11 +02:00
|
|
|
$vdt = new VObject\Property\DateTime($name);
|
2013-10-03 10:54:05 +02:00
|
|
|
$vdt->setDateTime($dt, $is_dateonly ? VObject\Property\DateTime::DATE :
|
2013-09-25 17:12:27 +02:00
|
|
|
($is_utc ? VObject\Property\DateTime::UTC : VObject\Property\DateTime::LOCALTZ));
|
2013-07-23 17:14:11 +02:00
|
|
|
return $vdt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy values from one hash array to another using a key-map
|
|
|
|
*/
|
|
|
|
public static function map_keys($values, $map)
|
|
|
|
{
|
|
|
|
$out = array();
|
|
|
|
foreach ($map as $from => $to) {
|
|
|
|
if (isset($values[$from]))
|
2014-02-27 23:32:15 +01:00
|
|
|
$out[$to] = is_array($values[$from]) ? join(',', $values[$from]) : $values[$from];
|
2011-05-20 19:04:25 +02:00
|
|
|
}
|
2013-07-23 17:14:11 +02:00
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
private static function parameters_array($prop)
|
|
|
|
{
|
|
|
|
$params = array();
|
|
|
|
foreach ($prop->parameters as $param) {
|
|
|
|
$params[strtoupper($param->name)] = $param->value;
|
2011-08-04 23:54:29 +02:00
|
|
|
}
|
2013-07-23 17:14:11 +02:00
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Export events to iCalendar format
|
|
|
|
*
|
|
|
|
* @param array Events as array
|
|
|
|
* @param string VCalendar method to advertise
|
|
|
|
* @param boolean Directly send data to stdout instead of returning
|
|
|
|
* @param callable Callback function to fetch attachment contents, false if no attachment export
|
|
|
|
* @return string Events in iCalendar format (http://tools.ietf.org/html/rfc5545)
|
|
|
|
*/
|
|
|
|
public function export($objects, $method = null, $write = false, $get_attachment = false, $recurrence_id = null)
|
|
|
|
{
|
|
|
|
$memory_limit = parse_bytes(ini_get('memory_limit'));
|
2013-10-01 11:55:10 +02:00
|
|
|
$this->method = $method;
|
2013-07-23 17:14:11 +02:00
|
|
|
|
|
|
|
// encapsulate in VCALENDAR container
|
|
|
|
$vcal = VObject\Component::create('VCALENDAR');
|
|
|
|
$vcal->version = '2.0';
|
|
|
|
$vcal->prodid = $this->prodid;
|
|
|
|
$vcal->calscale = 'GREGORIAN';
|
|
|
|
|
|
|
|
if (!empty($method)) {
|
|
|
|
$vcal->METHOD = $method;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: include timezone information
|
|
|
|
|
|
|
|
// write vcalendar header
|
|
|
|
if ($write) {
|
|
|
|
echo preg_replace('/END:VCALENDAR[\r\n]*$/m', '', $vcal->serialize());
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($objects as $object) {
|
|
|
|
$this->_to_ical($object, !$write?$vcal:false, $get_attachment);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($write) {
|
|
|
|
echo "END:VCALENDAR\r\n";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return $vcal->serialize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build a valid iCal format block from the given event
|
|
|
|
*
|
|
|
|
* @param array Hash array with event/task properties from libkolab
|
|
|
|
* @param object VCalendar object to append event to or false for directly sending data to stdout
|
|
|
|
* @param callable Callback function to fetch attachment contents, false if no attachment export
|
|
|
|
* @param object RECURRENCE-ID property when serializing a recurrence exception
|
|
|
|
*/
|
|
|
|
private function _to_ical($event, $vcal, $get_attachment, $recurrence_id = null)
|
|
|
|
{
|
|
|
|
$type = $event['_type'] ?: 'event';
|
|
|
|
$ve = VObject\Component::create($this->type_component_map[$type]);
|
|
|
|
$ve->add('UID', $event['uid']);
|
2013-10-01 11:55:10 +02:00
|
|
|
|
|
|
|
// set DTSTAMP according to RFC 5545, 3.8.7.2.
|
|
|
|
$dtstamp = !empty($event['changed']) && !empty($this->method) ? $event['changed'] : new DateTime();
|
|
|
|
$ve->add(self::datetime_prop('DTSTAMP', $dtstamp, true));
|
2013-07-23 17:14:11 +02:00
|
|
|
|
|
|
|
// all-day events end the next day
|
|
|
|
if ($event['allday'] && !empty($event['end'])) {
|
|
|
|
$event['end'] = clone $event['end'];
|
|
|
|
$event['end']->add(new \DateInterval('P1D'));
|
|
|
|
$event['end']->_dateonly = true;
|
|
|
|
}
|
|
|
|
if (!empty($event['created']))
|
|
|
|
$ve->add(self::datetime_prop('CREATED', $event['created'], true));
|
|
|
|
if (!empty($event['changed']))
|
2013-09-25 17:34:51 +02:00
|
|
|
$ve->add(self::datetime_prop('LAST-MODIFIED', $event['changed'], true));
|
2013-07-23 17:14:11 +02:00
|
|
|
if (!empty($event['start']))
|
2013-10-03 10:54:05 +02:00
|
|
|
$ve->add(self::datetime_prop('DTSTART', $event['start'], false, (bool)$event['allday']));
|
2013-07-23 17:14:11 +02:00
|
|
|
if (!empty($event['end']))
|
2013-10-03 10:54:05 +02:00
|
|
|
$ve->add(self::datetime_prop('DTEND', $event['end'], false, (bool)$event['allday']));
|
2013-07-23 17:14:11 +02:00
|
|
|
if (!empty($event['due']))
|
|
|
|
$ve->add(self::datetime_prop('DUE', $event['due'], false));
|
|
|
|
|
|
|
|
if ($recurrence_id)
|
|
|
|
$ve->add($recurrence_id);
|
|
|
|
|
|
|
|
$ve->add('SUMMARY', $event['title']);
|
|
|
|
|
|
|
|
if ($event['location'])
|
2013-09-12 10:57:22 +02:00
|
|
|
$ve->add($this->is_apple() ? new vobject_location_property('LOCATION', $event['location']) : new VObject\Property('LOCATION', $event['location']));
|
2013-07-23 17:14:11 +02:00
|
|
|
if ($event['description'])
|
|
|
|
$ve->add('DESCRIPTION', strtr($event['description'], array("\r\n" => "\n", "\r" => "\n"))); // normalize line endings
|
|
|
|
|
2014-07-08 18:30:12 +02:00
|
|
|
if (isset($event['sequence']))
|
2013-07-23 17:14:11 +02:00
|
|
|
$ve->add('SEQUENCE', $event['sequence']);
|
|
|
|
|
|
|
|
if ($event['recurrence'] && !$recurrence_id) {
|
|
|
|
if ($exdates = $event['recurrence']['EXDATE']) {
|
|
|
|
unset($event['recurrence']['EXDATE']); // don't serialize EXDATEs into RRULE value
|
|
|
|
}
|
2014-02-28 16:12:24 +01:00
|
|
|
if ($rdates = $event['recurrence']['RDATE']) {
|
|
|
|
unset($event['recurrence']['RDATE']); // don't serialize RDATEs into RRULE value
|
|
|
|
}
|
2013-07-23 17:14:11 +02:00
|
|
|
|
2014-02-28 16:12:24 +01:00
|
|
|
if ($event['recurrence']['FREQ']) {
|
|
|
|
$ve->add('RRULE', libcalendaring::to_rrule($event['recurrence']));
|
|
|
|
}
|
2013-07-23 17:14:11 +02:00
|
|
|
|
|
|
|
// add EXDATEs each one per line (for Thunderbird Lightning)
|
|
|
|
if ($exdates) {
|
|
|
|
foreach ($exdates as $ex) {
|
|
|
|
if ($ex instanceof \DateTime) {
|
|
|
|
$exd = clone $event['start'];
|
|
|
|
$exd->setDate($ex->format('Y'), $ex->format('n'), $ex->format('j'));
|
|
|
|
$exd->setTimeZone(new \DateTimeZone('UTC'));
|
|
|
|
$ve->add(new VObject\Property('EXDATE', $exd->format('Ymd\\THis\\Z')));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-28 16:12:24 +01:00
|
|
|
// add RDATEs
|
|
|
|
if (!empty($rdates)) {
|
|
|
|
$sample = self::datetime_prop('RDATE', $rdates[0]);
|
|
|
|
$rdprop = new VObject\Property\MultiDateTime('RDATE', null);
|
|
|
|
$rdprop->setDateTimes($rdates, $sample->getDateType());
|
|
|
|
$ve->add($rdprop);
|
|
|
|
}
|
2013-07-23 17:14:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($event['categories']) {
|
|
|
|
$cat = VObject\Property::create('CATEGORIES');
|
|
|
|
$cat->setParts((array)$event['categories']);
|
|
|
|
$ve->add($cat);
|
|
|
|
}
|
|
|
|
|
2014-01-23 15:50:04 +01:00
|
|
|
if (!empty($event['free_busy'])) {
|
2013-07-24 18:59:09 +02:00
|
|
|
$ve->add('TRANSP', $event['free_busy'] == 'free' ? 'TRANSPARENT' : 'OPAQUE');
|
2013-07-23 17:14:11 +02:00
|
|
|
|
2014-01-23 15:50:04 +01:00
|
|
|
// for Outlook clients we provide the X-MICROSOFT-CDO-BUSYSTATUS property
|
|
|
|
if (stripos($this->agent, 'outlook') !== false) {
|
|
|
|
$ve->add('X-MICROSOFT-CDO-BUSYSTATUS', $event['free_busy'] == 'outofoffice' ? 'OOF' : strtoupper($event['free_busy']));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-23 17:14:11 +02:00
|
|
|
if ($event['priority'])
|
|
|
|
$ve->add('PRIORITY', $event['priority']);
|
|
|
|
|
2011-07-29 18:41:43 +02:00
|
|
|
if ($event['cancelled'])
|
2013-07-23 17:14:11 +02:00
|
|
|
$ve->add('STATUS', 'CANCELLED');
|
2011-07-29 18:41:43 +02:00
|
|
|
else if ($event['free_busy'] == 'tentative')
|
2013-07-23 17:14:11 +02:00
|
|
|
$ve->add('STATUS', 'TENTATIVE');
|
|
|
|
else if ($event['complete'] == 100)
|
|
|
|
$ve->add('STATUS', 'COMPLETED');
|
2014-04-03 15:07:47 +02:00
|
|
|
else if (!empty($event['status']))
|
|
|
|
$ve->add('STATUS', $event['status']);
|
2013-07-23 17:14:11 +02:00
|
|
|
|
|
|
|
if (!empty($event['sensitivity']))
|
|
|
|
$ve->add('CLASS', strtoupper($event['sensitivity']));
|
|
|
|
|
2013-07-24 18:59:09 +02:00
|
|
|
if (!empty($event['complete'])) {
|
2013-07-23 17:14:11 +02:00
|
|
|
$ve->add('PERCENT-COMPLETE', intval($event['complete']));
|
|
|
|
// Apple iCal required the COMPLETED date to be set in order to consider a task complete
|
|
|
|
if ($event['complete'] == 100)
|
|
|
|
$ve->add(self::datetime_prop('COMPLETED', $event['changed'] ?: new DateTime('now - 1 hour'), true));
|
|
|
|
}
|
|
|
|
|
2014-04-03 17:39:26 +02:00
|
|
|
if ($event['valarms']) {
|
|
|
|
foreach ($event['valarms'] as $alarm) {
|
|
|
|
$va = VObject\Component::create('VALARM');
|
|
|
|
$va->action = $alarm['action'];
|
|
|
|
if ($alarm['trigger'] instanceof DateTime) {
|
|
|
|
$va->add(self::datetime_prop('TRIGGER', $alarm['trigger'], true));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$va->add('TRIGGER', $alarm['trigger']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($alarm['action'] == 'EMAIL') {
|
|
|
|
foreach ((array)$alarm['attendees'] as $attendee) {
|
|
|
|
$va->add('ATTENDEE', 'mailto:' . $attendee);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($alarm['description']) {
|
|
|
|
$va->add('DESCRIPTION', $alarm['description'] ?: $event['title']);
|
|
|
|
}
|
|
|
|
if ($alarm['summary']) {
|
|
|
|
$va->add('SUMMARY', $alarm['summary']);
|
|
|
|
}
|
|
|
|
if ($alarm['duration']) {
|
|
|
|
$va->add('DURATION', $alarm['duration']);
|
|
|
|
$va->add('REPEAT', intval($alarm['repeat']));
|
|
|
|
}
|
2014-04-03 19:19:14 +02:00
|
|
|
if ($alarm['uri']) {
|
|
|
|
$va->add('ATTACH', $alarm['uri'], array('VALUE' => 'URI'));
|
|
|
|
}
|
2014-04-03 17:39:26 +02:00
|
|
|
$ve->add($va);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// legacy support
|
|
|
|
else if ($event['alarms']) {
|
2013-07-23 17:14:11 +02:00
|
|
|
$va = VObject\Component::create('VALARM');
|
|
|
|
list($trigger, $va->action) = explode(':', $event['alarms']);
|
|
|
|
$val = libcalendaring::parse_alaram_value($trigger);
|
2014-04-17 17:49:00 +02:00
|
|
|
if ($val[3])
|
|
|
|
$va->add('TRIGGER', $val[3]);
|
|
|
|
else if ($val[0] instanceof DateTime)
|
|
|
|
$va->add(self::datetime_prop('TRIGGER', $val[0]));
|
2013-07-23 17:14:11 +02:00
|
|
|
$ve->add($va);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ((array)$event['attendees'] as $attendee) {
|
|
|
|
if ($attendee['role'] == 'ORGANIZER') {
|
|
|
|
if (empty($event['organizer']))
|
|
|
|
$event['organizer'] = $attendee;
|
2013-07-04 17:07:58 +02:00
|
|
|
}
|
2013-07-24 18:05:04 +02:00
|
|
|
else if (!empty($attendee['email'])) {
|
|
|
|
$attendee['rsvp'] = $attendee['rsvp'] ? 'TRUE' : null;
|
2014-03-19 11:10:33 +01:00
|
|
|
$ve->add('ATTENDEE', 'mailto:' . $attendee['email'], array_filter(self::map_keys($attendee, $this->attendee_keymap)));
|
2013-07-24 18:05:04 +02:00
|
|
|
}
|
2013-07-04 17:07:58 +02:00
|
|
|
}
|
2013-02-28 10:44:15 +01:00
|
|
|
|
2013-07-23 17:14:11 +02:00
|
|
|
if ($event['organizer']) {
|
|
|
|
$ve->add('ORGANIZER', 'mailto:' . $event['organizer']['email'], self::map_keys($event['organizer'], array('name' => 'CN')));
|
2013-02-28 10:44:15 +01:00
|
|
|
}
|
2011-05-20 19:04:25 +02:00
|
|
|
|
2013-07-23 17:14:11 +02:00
|
|
|
foreach ((array)$event['url'] as $url) {
|
2013-07-24 18:05:04 +02:00
|
|
|
if (!empty($url)) {
|
|
|
|
$ve->add('URL', $url);
|
|
|
|
}
|
2013-07-23 17:14:11 +02:00
|
|
|
}
|
2012-07-11 09:59:53 +02:00
|
|
|
|
2013-07-23 17:14:11 +02:00
|
|
|
if (!empty($event['parent_id'])) {
|
|
|
|
$ve->add('RELATED-TO', $event['parent_id'], array('RELTYPE' => 'PARENT'));
|
|
|
|
}
|
2012-07-11 09:59:53 +02:00
|
|
|
|
2014-03-18 18:05:48 +01:00
|
|
|
if ($event['comment'])
|
|
|
|
$ve->add('COMMENT', $event['comment']);
|
|
|
|
|
2013-07-23 17:14:11 +02:00
|
|
|
// export attachments
|
|
|
|
if (!empty($event['attachments'])) {
|
|
|
|
foreach ((array)$event['attachments'] as $attach) {
|
|
|
|
// check available memory and skip attachment export if we can't buffer it
|
|
|
|
if (is_callable($get_attachment) && $memory_limit > 0 && ($memory_used = function_exists('memory_get_usage') ? memory_get_usage() : 16*1024*1024)
|
|
|
|
&& $attach['size'] && $memory_used + $attach['size'] * 3 > $memory_limit) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// embed attachments using the given callback function
|
|
|
|
if (is_callable($get_attachment) && ($data = call_user_func($get_attachment, $attach['id'], $event))) {
|
|
|
|
// embed attachments for iCal
|
|
|
|
$ve->add('ATTACH',
|
|
|
|
base64_encode($data),
|
2014-03-19 11:10:33 +01:00
|
|
|
array_filter(array('VALUE' => 'BINARY', 'ENCODING' => 'BASE64', 'FMTTYPE' => $attach['mimetype'], 'X-LABEL' => $attach['name'])));
|
2013-07-23 17:14:11 +02:00
|
|
|
unset($data); // attempt to free memory
|
|
|
|
}
|
|
|
|
// list attachments as absolute URIs
|
|
|
|
else if (!empty($this->attach_uri)) {
|
|
|
|
$ve->add('ATTACH',
|
|
|
|
strtr($this->attach_uri, array(
|
|
|
|
'{{id}}' => urlencode($attach['id']),
|
|
|
|
'{{name}}' => urlencode($attach['name']),
|
|
|
|
'{{mimetype}}' => urlencode($attach['mimetype']),
|
|
|
|
)),
|
|
|
|
array('FMTTYPE' => $attach['mimetype'], 'VALUE' => 'URI'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-07-06 17:15:45 +02:00
|
|
|
|
2013-07-23 17:14:11 +02:00
|
|
|
foreach ((array)$event['links'] as $uri) {
|
|
|
|
$ve->add('ATTACH', $uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
// add custom properties
|
|
|
|
foreach ((array)$event['x-custom'] as $prop) {
|
|
|
|
$ve->add($prop[0], $prop[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// append to vcalendar container
|
|
|
|
if ($vcal) {
|
|
|
|
$vcal->add($ve);
|
|
|
|
}
|
|
|
|
else { // serialize and send to stdout
|
|
|
|
echo $ve->serialize();
|
|
|
|
}
|
2011-07-27 17:42:45 +02:00
|
|
|
|
2013-07-23 17:14:11 +02:00
|
|
|
// append recurrence exceptions
|
2014-04-09 13:32:37 +02:00
|
|
|
if (is_array($event['recurrence']) && $event['recurrence']['EXCEPTIONS']) {
|
2013-07-23 17:14:11 +02:00
|
|
|
foreach ($event['recurrence']['EXCEPTIONS'] as $ex) {
|
|
|
|
$exdate = clone $event['start'];
|
|
|
|
$exdate->setDate($ex['start']->format('Y'), $ex['start']->format('n'), $ex['start']->format('j'));
|
|
|
|
$recurrence_id = self::datetime_prop('RECURRENCE-ID', $exdate, true);
|
|
|
|
// if ($ex['thisandfuture']) // not supported by any client :-(
|
|
|
|
// $recurrence_id->add('RANGE', 'THISANDFUTURE');
|
|
|
|
$this->_to_ical($ex, $vcal, $get_attachment, $recurrence_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-07-27 17:42:45 +02:00
|
|
|
|
2013-10-17 13:10:19 +02:00
|
|
|
|
|
|
|
/*** Implement PHP 5 Iterator interface to make foreach work ***/
|
|
|
|
|
|
|
|
function current()
|
|
|
|
{
|
|
|
|
return $this->objects[$this->iteratorkey];
|
|
|
|
}
|
|
|
|
|
|
|
|
function key()
|
|
|
|
{
|
|
|
|
return $this->iteratorkey;
|
|
|
|
}
|
|
|
|
|
|
|
|
function next()
|
|
|
|
{
|
|
|
|
$this->iteratorkey++;
|
|
|
|
|
|
|
|
// read next chunk if we're reading from a file
|
|
|
|
if (!$this->objects[$this->iteratorkey] && $this->fp) {
|
|
|
|
$this->_parse_next(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->valid();
|
|
|
|
}
|
|
|
|
|
|
|
|
function rewind()
|
|
|
|
{
|
|
|
|
$this->iteratorkey = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function valid()
|
|
|
|
{
|
|
|
|
return !empty($this->objects[$this->iteratorkey]);
|
|
|
|
}
|
|
|
|
|
2011-10-27 10:20:46 +02:00
|
|
|
}
|
2013-09-12 10:57:22 +02:00
|
|
|
|
2013-09-13 09:47:40 +02:00
|
|
|
|
2013-09-12 10:57:22 +02:00
|
|
|
/**
|
|
|
|
* Override Sabre\VObject\Property that quotes commas in the location property
|
|
|
|
* because Apple clients treat that property as list.
|
|
|
|
*/
|
|
|
|
class vobject_location_property extends VObject\Property
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Turns the object back into a serialized blob.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function serialize()
|
|
|
|
{
|
|
|
|
$str = $this->name;
|
|
|
|
|
|
|
|
foreach ($this->parameters as $param) {
|
|
|
|
$str.=';' . $param->serialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
$src = array(
|
|
|
|
'\\',
|
|
|
|
"\n",
|
|
|
|
',',
|
|
|
|
);
|
|
|
|
$out = array(
|
|
|
|
'\\\\',
|
|
|
|
'\n',
|
|
|
|
'\,',
|
|
|
|
);
|
|
|
|
$str.=':' . str_replace($src, $out, $this->value);
|
|
|
|
|
|
|
|
$out = '';
|
|
|
|
while (strlen($str) > 0) {
|
|
|
|
if (strlen($str) > 75) {
|
|
|
|
$out.= mb_strcut($str, 0, 75, 'utf-8') . "\r\n";
|
|
|
|
$str = ' ' . mb_strcut($str, 75, strlen($str), 'utf-8');
|
|
|
|
} else {
|
|
|
|
$out.= $str . "\r\n";
|
|
|
|
$str = '';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
}
|