aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/drivers
diff options
context:
space:
mode:
authorDaniel Lange <DLange@git.local>2016-03-07 15:53:16 +0100
committerDaniel Lange <DLange@git.local>2016-03-07 15:53:16 +0100
commit50569114acdc64e7c7cae1498635d3f821517c30 (patch)
tree13d6fe76af33134fbfb2286930fb6603047f9299 /calendar/drivers
parentc210d30de6c62e7f7867bb32651349ddf455d9e6 (diff)
downloadroundcube_calendar-50569114acdc64e7c7cae1498635d3f821517c30.tar.gz
roundcube_calendar-50569114acdc64e7c7cae1498635d3f821517c30.tar.bz2
roundcube_calendar-50569114acdc64e7c7cae1498635d3f821517c30.zip
Initial commit of the Faster IT roundcube_calendar plugin distribution
This includes: * Kolab plugins 3.2.9 (calendar and libcalendaring) * CalDAV driver 3.2.8 * .htaccess files for at least some security * SabreDAV updated to 1.8.12 (Jan 2015 release) * Support for CURLOPT_SSL_* settings to allow self-signed certificates * Small fixes & improved documentation
Diffstat (limited to 'calendar/drivers')
-rw-r--r--calendar/drivers/caldav/SQL/mysql.initial.sql92
-rw-r--r--calendar/drivers/caldav/SQL/mysql/.keep_dir0
-rw-r--r--calendar/drivers/caldav/SQL/mysql/2014081300.sql24
-rw-r--r--calendar/drivers/caldav/SQL/mysql/2015022500.sql125
-rw-r--r--calendar/drivers/caldav/SQL/mysql/2015022700.sql14
-rw-r--r--calendar/drivers/caldav/SQL/postgres.initial.sql51
-rw-r--r--calendar/drivers/caldav/caldav_driver.php2036
-rw-r--r--calendar/drivers/caldav/caldav_sync.php253
-rw-r--r--calendar/drivers/calendar_driver.php819
-rw-r--r--calendar/drivers/database/SQL/mysql.initial.sql85
-rw-r--r--calendar/drivers/database/SQL/mysql/2012080600.sql3
-rw-r--r--calendar/drivers/database/SQL/mysql/2013011000.sql1
-rw-r--r--calendar/drivers/database/SQL/mysql/2013042700.sql1
-rw-r--r--calendar/drivers/database/SQL/mysql/2013051600.sql3
-rw-r--r--calendar/drivers/database/SQL/mysql/2014040900.sql3
-rw-r--r--calendar/drivers/database/SQL/mysql/2015022700.sql15
-rw-r--r--calendar/drivers/database/SQL/postgres.initial.sql109
-rw-r--r--calendar/drivers/database/SQL/postgres/2012080600.sql3
-rw-r--r--calendar/drivers/database/SQL/postgres/2013011000.sql1
-rw-r--r--calendar/drivers/database/SQL/postgres/2013042700.sql8
-rw-r--r--calendar/drivers/database/SQL/postgres/2013051600.sql3
-rw-r--r--calendar/drivers/database/SQL/postgres/2014040900.sql3
-rw-r--r--calendar/drivers/database/SQL/postgres/2015022700.sql9
-rw-r--r--calendar/drivers/database/SQL/sqlite.initial.sql79
-rw-r--r--calendar/drivers/database/SQL/sqlite/2013011000.sql1
-rw-r--r--calendar/drivers/database/SQL/sqlite/2013042700.sql1
-rw-r--r--calendar/drivers/database/SQL/sqlite/2013051600.sql63
-rw-r--r--calendar/drivers/database/SQL/sqlite/2014040900.sql67
-rw-r--r--calendar/drivers/database/SQL/sqlite/2015022700.sql79
-rw-r--r--calendar/drivers/database/database_driver.php1496
-rw-r--r--calendar/drivers/ical/SQL/mysql.initial.sql91
-rw-r--r--calendar/drivers/ical/SQL/mysql/.keep_dir0
-rw-r--r--calendar/drivers/ical/SQL/mysql/2015022500.sql124
-rw-r--r--calendar/drivers/ical/SQL/mysql/2015022700.sql14
-rw-r--r--calendar/drivers/ical/ical_driver.php1821
-rw-r--r--calendar/drivers/ical/ical_sync.php125
-rw-r--r--calendar/drivers/kolab/SQL/mysql.initial.sql32
-rw-r--r--calendar/drivers/kolab/SQL/mysql/2012080600.sql11
-rw-r--r--calendar/drivers/kolab/SQL/mysql/2013011000.sql1
-rw-r--r--calendar/drivers/kolab/SQL/mysql/2014041700.sql1
-rw-r--r--calendar/drivers/kolab/SQL/mysql/2014082600.sql2
-rw-r--r--calendar/drivers/kolab/SQL/oracle.initial.sql31
-rw-r--r--calendar/drivers/kolab/SQL/postgres.initial.sql32
-rw-r--r--calendar/drivers/kolab/kolab_calendar.php836
-rw-r--r--calendar/drivers/kolab/kolab_driver.php2526
-rw-r--r--calendar/drivers/kolab/kolab_invitation_calendar.php377
-rw-r--r--calendar/drivers/kolab/kolab_user_calendar.php432
-rw-r--r--calendar/drivers/ldap/resources_driver_ldap.php150
-rw-r--r--calendar/drivers/resources_driver.php114
49 files changed, 12167 insertions, 0 deletions
diff --git a/calendar/drivers/caldav/SQL/mysql.initial.sql b/calendar/drivers/caldav/SQL/mysql.initial.sql
new file mode 100644
index 0000000..d60d482
--- /dev/null
+++ b/calendar/drivers/caldav/SQL/mysql.initial.sql
@@ -0,0 +1,92 @@
+/**
+ * CalDAV Client
+ *
+ * @version @package_version@
+ * @author Daniel Morlock <daniel.morlock@awesome-it.de>
+ *
+ * Copyright (C) Awesome IT GbR <info@awesome-it.de>
+ *
+ * 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.
+ *
+ * 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
+ * GNU Affero General Public License for more details.
+ *
+ * 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/>.
+ */
+
+CREATE TABLE IF NOT EXISTS `caldav_calendars` (
+ `calendar_id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
+ `user_id` int(10) UNSIGNED NOT NULL DEFAULT '0',
+ `name` varchar(255) NOT NULL,
+ `color` varchar(8) NOT NULL,
+ `showalarms` tinyint(1) NOT NULL DEFAULT '1',
+
+ `caldav_url` varchar(255) NOT NULL,
+ `caldav_tag` varchar(255) DEFAULT NULL,
+ `caldav_user` varchar(255) DEFAULT NULL,
+ `caldav_pass` varchar(1024) DEFAULT NULL,
+ `caldav_last_change` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
+
+ PRIMARY KEY(`calendar_id`),
+ INDEX `caldav_user_name_idx` (`user_id`, `name`),
+ CONSTRAINT `fk_caldav_calendars_user_id` FOREIGN KEY (`user_id`)
+ REFERENCES `users`(`user_id`) ON DELETE CASCADE ON UPDATE CASCADE
+) /*!40000 ENGINE=INNODB */ /*!40101 CHARACTER SET utf8 COLLATE utf8_general_ci */;
+
+CREATE TABLE IF NOT EXISTS `caldav_events` (
+ `event_id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
+ `calendar_id` int(11) UNSIGNED NOT NULL DEFAULT '0',
+ `recurrence_id` int(11) UNSIGNED NOT NULL DEFAULT '0',
+ `uid` varchar(255) NOT NULL DEFAULT '',
+ `instance` varchar(16) NOT NULL DEFAULT '',
+ `isexception` tinyint(1) NOT NULL DEFAULT '0',
+ `created` datetime NOT NULL DEFAULT '1000-01-01 00:00:00',
+ `changed` datetime NOT NULL DEFAULT '1000-01-01 00:00:00',
+ `sequence` int(1) UNSIGNED NOT NULL DEFAULT '0',
+ `start` datetime NOT NULL DEFAULT '1000-01-01 00:00:00',
+ `end` datetime NOT NULL DEFAULT '1000-01-01 00:00:00',
+ `recurrence` varchar(255) DEFAULT NULL,
+ `title` varchar(255) NOT NULL,
+ `description` text NOT NULL,
+ `location` varchar(255) NOT NULL DEFAULT '',
+ `categories` varchar(255) NOT NULL DEFAULT '',
+ `url` varchar(255) NOT NULL DEFAULT '',
+ `all_day` tinyint(1) NOT NULL DEFAULT '0',
+ `free_busy` tinyint(1) NOT NULL DEFAULT '0',
+ `priority` tinyint(1) NOT NULL DEFAULT '0',
+ `sensitivity` tinyint(1) NOT NULL DEFAULT '0',
+ `status` varchar(32) NOT NULL DEFAULT '',
+ `alarms` text NULL DEFAULT NULL,
+ `attendees` text DEFAULT NULL,
+ `notifyat` datetime DEFAULT NULL,
+
+ `caldav_url` varchar(255) NOT NULL,
+ `caldav_tag` varchar(255) DEFAULT NULL,
+ `caldav_last_change` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
+
+ PRIMARY KEY(`event_id`),
+ INDEX `caldav_uid_idx` (`uid`),
+ INDEX `caldav_recurrence_idx` (`recurrence_id`),
+ INDEX `caldav_calendar_notify_idx` (`calendar_id`,`notifyat`),
+ CONSTRAINT `fk_caldav_events_calendar_id` FOREIGN KEY (`calendar_id`)
+ REFERENCES `caldav_calendars`(`calendar_id`) ON DELETE CASCADE ON UPDATE CASCADE
+) /*!40000 ENGINE=INNODB */ /*!40101 CHARACTER SET utf8 COLLATE utf8_general_ci */;
+
+CREATE TABLE IF NOT EXISTS `caldav_attachments` (
+ `attachment_id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
+ `event_id` int(11) UNSIGNED NOT NULL DEFAULT '0',
+ `filename` varchar(255) NOT NULL DEFAULT '',
+ `mimetype` varchar(255) NOT NULL DEFAULT '',
+ `size` int(11) NOT NULL DEFAULT '0',
+ `data` longtext NOT NULL,
+ PRIMARY KEY(`attachment_id`),
+ CONSTRAINT `fk_caldav_attachments_event_id` FOREIGN KEY (`event_id`)
+ REFERENCES `caldav_events`(`event_id`) ON DELETE CASCADE ON UPDATE CASCADE
+) /*!40000 ENGINE=INNODB */ /*!40101 CHARACTER SET utf8 COLLATE utf8_general_ci */;
+REPLACE INTO `system` (`name`, `value`) VALUES ('calendar-caldav-version', '2015022700'); \ No newline at end of file
diff --git a/calendar/drivers/caldav/SQL/mysql/.keep_dir b/calendar/drivers/caldav/SQL/mysql/.keep_dir
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/calendar/drivers/caldav/SQL/mysql/.keep_dir
diff --git a/calendar/drivers/caldav/SQL/mysql/2014081300.sql b/calendar/drivers/caldav/SQL/mysql/2014081300.sql
new file mode 100644
index 0000000..f1a3c98
--- /dev/null
+++ b/calendar/drivers/caldav/SQL/mysql/2014081300.sql
@@ -0,0 +1,24 @@
+/**
+ * CalDAV Client
+ *
+ * @version @package_version@
+ * @author Daniel Morlock <daniel.morlock@awesome-it.de>
+ *
+ * Copyright (C) Awesome IT GbR <info@awesome-it.de>
+ *
+ * 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.
+ *
+ * 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
+ * GNU Affero General Public License for more details.
+ *
+ * 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/>.
+ */
+
+ALTER TABLE `caldav_props` change `user` `username` varchar(255);
+ALTER TABLE `events` ADD `status` VARCHAR(32) NOT NULL DEFAULT '' AFTER `sensitivity`; \ No newline at end of file
diff --git a/calendar/drivers/caldav/SQL/mysql/2015022500.sql b/calendar/drivers/caldav/SQL/mysql/2015022500.sql
new file mode 100644
index 0000000..df0f613
--- /dev/null
+++ b/calendar/drivers/caldav/SQL/mysql/2015022500.sql
@@ -0,0 +1,125 @@
+/**
+ * CalDAV Client
+ *
+ * @version @package_version@
+ * @author Daniel Morlock <daniel.morlock@awesome-it.de>
+ *
+ * Copyright (C) Awesome IT GbR <info@awesome-it.de>
+ *
+ * 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.
+ *
+ * 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
+ * GNU Affero General Public License for more details.
+ *
+ * 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/>.
+ */
+
+/* Create new tables */
+CREATE TABLE IF NOT EXISTS `caldav_calendars` (
+ `calendar_id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
+ `user_id` int(10) UNSIGNED NOT NULL DEFAULT '0',
+ `name` varchar(255) NOT NULL,
+ `color` varchar(8) NOT NULL,
+ `showalarms` tinyint(1) NOT NULL DEFAULT '1',
+
+ `caldav_url` varchar(255) NOT NULL,
+ `caldav_tag` varchar(255) DEFAULT NULL,
+ `caldav_user` varchar(255) DEFAULT NULL,
+ `caldav_pass` varchar(1024) DEFAULT NULL,
+ `caldav_last_change` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
+
+ PRIMARY KEY(`calendar_id`),
+ INDEX `caldav_user_name_idx` (`user_id`, `name`),
+ CONSTRAINT `fk_caldav_calendars_user_id` FOREIGN KEY (`user_id`)
+ REFERENCES `users`(`user_id`) ON DELETE CASCADE ON UPDATE CASCADE
+) /*!40000 ENGINE=INNODB */ /*!40101 CHARACTER SET utf8 COLLATE utf8_general_ci */;
+
+CREATE TABLE IF NOT EXISTS `caldav_events` (
+ `event_id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
+ `calendar_id` int(11) UNSIGNED NOT NULL DEFAULT '0',
+ `recurrence_id` int(11) UNSIGNED NOT NULL DEFAULT '0',
+ `uid` varchar(255) NOT NULL DEFAULT '',
+ `created` datetime NOT NULL DEFAULT '1000-01-01 00:00:00',
+ `changed` datetime NOT NULL DEFAULT '1000-01-01 00:00:00',
+ `sequence` int(1) UNSIGNED NOT NULL DEFAULT '0',
+ `start` datetime NOT NULL DEFAULT '1000-01-01 00:00:00',
+ `end` datetime NOT NULL DEFAULT '1000-01-01 00:00:00',
+ `recurrence` varchar(255) DEFAULT NULL,
+ `title` varchar(255) NOT NULL,
+ `description` text NOT NULL,
+ `location` varchar(255) NOT NULL DEFAULT '',
+ `categories` varchar(255) NOT NULL DEFAULT '',
+ `url` varchar(255) NOT NULL DEFAULT '',
+ `all_day` tinyint(1) NOT NULL DEFAULT '0',
+ `free_busy` tinyint(1) NOT NULL DEFAULT '0',
+ `priority` tinyint(1) NOT NULL DEFAULT '0',
+ `sensitivity` tinyint(1) NOT NULL DEFAULT '0',
+ `status` varchar(32) NOT NULL DEFAULT '',
+ `alarms` varchar(255) DEFAULT NULL,
+ `attendees` text DEFAULT NULL,
+ `notifyat` datetime DEFAULT NULL,
+
+ `caldav_url` varchar(255) NOT NULL,
+ `caldav_tag` varchar(255) DEFAULT NULL,
+ `caldav_last_change` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
+
+ PRIMARY KEY(`event_id`),
+ INDEX `caldav_uid_idx` (`uid`),
+ INDEX `caldav_recurrence_idx` (`recurrence_id`),
+ INDEX `caldav_calendar_notify_idx` (`calendar_id`,`notifyat`),
+ CONSTRAINT `fk_caldav_events_calendar_id` FOREIGN KEY (`calendar_id`)
+ REFERENCES `calendars`(`calendar_id`) ON DELETE CASCADE ON UPDATE CASCADE
+) /*!40000 ENGINE=INNODB */ /*!40101 CHARACTER SET utf8 COLLATE utf8_general_ci */;
+
+CREATE TABLE IF NOT EXISTS `caldav_attachments` (
+ `attachment_id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
+ `event_id` int(11) UNSIGNED NOT NULL DEFAULT '0',
+ `filename` varchar(255) NOT NULL DEFAULT '',
+ `mimetype` varchar(255) NOT NULL DEFAULT '',
+ `size` int(11) NOT NULL DEFAULT '0',
+ `data` longtext NOT NULL,
+ PRIMARY KEY(`attachment_id`),
+ CONSTRAINT `fk_caldav_attachments_event_id` FOREIGN KEY (`event_id`)
+ REFERENCES `events`(`event_id`) ON DELETE CASCADE ON UPDATE CASCADE
+) /*!40000 ENGINE=INNODB */ /*!40101 CHARACTER SET utf8 COLLATE utf8_general_ci */;
+
+/* Migrate Data */
+INSERT INTO caldav_calendars SELECT calendar_id, user_id, `name`, color, showalarms, url as caldav_url,
+ tag as caldav_tag, username as caldav_user, pass as caldav_pass,
+ last_change as caldav_last_change
+FROM calendars cal, caldav_props dav
+WHERE dav.obj_id = cal.calendar_id
+AND dav.obj_type = 'vcal';
+
+INSERT INTO caldav_events SELECT e.*, dav.url as caldav_url, dav.tag as caldav_tag, dav.last_change as caldav_last_change
+FROM `events` e, caldav_props dav
+WHERE dav.obj_id = e.event_id
+AND dav.obj_type = 'vevent';
+
+INSERT INTO caldav_attachments SELECT * FROM attachments a
+WHERE a.event_id IN (
+ SELECT obj_id FROM caldav_props dav
+ WHERE dav.obj_type = 'vevent'
+);
+
+/* Drop deprecated data */
+DELETE FROM `events` WHERE event_id IN (
+ SELECT obj_id FROM caldav_props dav
+ WHERE dav.obj_type = 'vevent'
+);
+DELETE FROM calendars WHERE calendar_id IN (
+ SELECT obj_id FROM caldav_props dav
+ WHERE dav.obj_type = 'vcal'
+);
+DELETE FROM attachments WHERE event_id IN (
+ SELECT obj_id FROM caldav_props dav
+ WHERE dav.obj_type = 'vevent'
+);
+DROP TABLE caldav_props;
+
diff --git a/calendar/drivers/caldav/SQL/mysql/2015022700.sql b/calendar/drivers/caldav/SQL/mysql/2015022700.sql
new file mode 100644
index 0000000..f44b49e
--- /dev/null
+++ b/calendar/drivers/caldav/SQL/mysql/2015022700.sql
@@ -0,0 +1,14 @@
+-- add identifier for recurring instances and exceptions
+
+ALTER TABLE `caldav_events` ADD `instance` varchar(16) NOT NULL DEFAULT '' AFTER `uid`;
+ALTER TABLE `caldav_events` ADD `isexception` tinyint(1) NOT NULL DEFAULT '0' AFTER `instance`;
+
+UPDATE `caldav_events` SET `instance` = DATE_FORMAT(`start`, '%Y%m%d')
+ WHERE `recurrence_id` != 0 AND `instance` = '' AND `all_day` = 1;
+
+UPDATE `caldav_events` SET `instance` = DATE_FORMAT(`start`, '%Y%m%dT%k%i%s')
+ WHERE `recurrence_id` != 0 AND `instance` = '' AND `all_day` = 0;
+
+-- extend alarms columns for multiple values
+
+ALTER TABLE `caldav_events` CHANGE `alarms` `alarms` TEXT NULL DEFAULT NULL; \ No newline at end of file
diff --git a/calendar/drivers/caldav/SQL/postgres.initial.sql b/calendar/drivers/caldav/SQL/postgres.initial.sql
new file mode 100644
index 0000000..f49da4e
--- /dev/null
+++ b/calendar/drivers/caldav/SQL/postgres.initial.sql
@@ -0,0 +1,51 @@
+/**
+ * CalDAV Client
+ *
+ * @version @package_version@
+ * @author Hugo Slabbert <hugo@slabnet.com>
+ *
+ * Copyright (C) 2014, Hugo Slabbert <hugo@slabnet.com>
+ *
+ * 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.
+ *
+ * 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
+ * GNU Affero General Public License for more details.
+ *
+ * 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/>.
+ */
+
+CREATE TYPE caldav_type AS ENUM ('vcal','vevent','vtodo','');
+
+CREATE TABLE IF NOT EXISTS caldav_props (
+ obj_id int NOT NULL,
+ obj_type caldav_type NOT NULL,
+ url varchar(255) NOT NULL,
+ tag varchar(255) DEFAULT NULL,
+ username varchar(255) DEFAULT NULL,
+ pass varchar(1024) DEFAULT NULL,
+ last_change timestamp without time zone DEFAULT now() NOT NULL,
+ PRIMARY KEY (obj_id, obj_type)
+);
+
+CREATE OR REPLACE FUNCTION upd_timestamp() RETURNS TRIGGER
+LANGUAGE plpgsql
+AS
+$$
+BEGIN
+ NEW.last_change = CURRENT_TIMESTAMP;
+ RETURN NEW;
+END;
+$$;
+
+CREATE TRIGGER update_timestamp
+ BEFORE INSERT OR UPDATE
+ ON caldav_props
+ FOR EACH ROW
+ EXECUTE PROCEDURE upd_timestamp();
+
diff --git a/calendar/drivers/caldav/caldav_driver.php b/calendar/drivers/caldav/caldav_driver.php
new file mode 100644
index 0000000..b39aeff
--- /dev/null
+++ b/calendar/drivers/caldav/caldav_driver.php
@@ -0,0 +1,2036 @@
+<?php
+
+/**
+ * CalDAV driver for the Calendar plugin
+ *
+ * @author Daniel Morlock <daniel.morlock@awesome-it.de>
+ *
+ * Copyright (C) Awesome IT GbR <info@awesome-it.de>
+ *
+ * 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.
+ *
+ * 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
+ * GNU Affero General Public License for more details.
+ *
+ * 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/>.
+ */
+
+require_once (dirname(__FILE__).'/caldav_sync.php');
+require_once (dirname(__FILE__).'/../../lib/encryption.php');
+
+
+class caldav_driver extends calendar_driver
+{
+ const DB_DATE_FORMAT = 'Y-m-d H:i:s';
+
+ public static $scheduling_properties = array('start', 'end', 'allday', 'recurrence', 'location', 'cancelled');
+
+ // features this backend supports
+ public $alarms = true;
+ public $attendees = true;
+ public $freebusy = false;
+ public $attachments = true;
+ public $alarm_types = array('DISPLAY');
+
+ private $rc;
+ private $cal;
+ private $cache = array();
+ private $calendars = array();
+ private $calendar_ids = '';
+ private $free_busy_map = array('free' => 0, 'busy' => 1, 'out-of-office' => 2, 'outofoffice' => 2, 'tentative' => 3);
+ private $sensitivity_map = array('public' => 0, 'private' => 1, 'confidential' => 2);
+ private $server_timezone;
+
+ private $db_events = 'caldav_events';
+ private $db_calendars = 'caldav_calendars';
+ private $db_attachments = 'caldav_attachments';
+
+ // Crypt key for CalDAV auth
+ private $crypt_key;
+
+ // Holds CalDAV sync clients
+ private $sync_clients = array();
+
+ // Min. time period to wait until CalDAV sync check.
+ private $sync_period = 10; // seconds
+
+ // Indicates debug mode for CalDAV
+ static private $debug = null;
+
+ /**
+ * Helper method to log debug msg if debug mode is enabled.
+ */
+ static public function debug_log($msg)
+ {
+ if(self::$debug === true)
+ rcmail::console(__CLASS__.': '.$msg);
+ }
+
+ /**
+ * Helper method to log (if debug mode is enabled) and raise an user error.
+ */
+ private function _raise_error($msg)
+ {
+ self::debug_log($msg);
+ $this->rc->output->show_message($msg, 'error');
+ }
+
+ /**
+ * Default constructor
+ */
+ public function __construct($cal)
+ {
+ $this->cal = $cal;
+ $this->rc = $cal->rc;
+ $this->server_timezone = new DateTimeZone(date_default_timezone_get());
+
+ // read database config
+ $db = $this->rc->get_dbh();
+ $this->db_events = $this->rc->config->get('db_table_caldav_events', $db->table_name($this->db_events));
+ $this->db_calendars = $this->rc->config->get('db_table_caldav_calendars', $db->table_name($this->db_calendars));
+ $this->db_attachments = $this->rc->config->get('db_table_caldav_attachments', $db->table_name($this->db_attachments));
+ $this->crypt_key = $this->rc->config->get("calendar_crypt_key", "%E`c{2;<J2F^4_&._BxfQ<5Pf3qv!m{e");
+
+ // Set debug state
+ if(self::$debug === null)
+ self::$debug = $this->rc->config->get('calendar_caldav_debug', False);
+
+ $this->_read_calendars();
+ }
+
+ /**
+ * Read available calendars for the current user and store them internally
+ */
+ protected function _read_calendars()
+ {
+ $hidden = array_filter(explode(',', $this->rc->config->get('hidden_caldav_calendars', '')));
+
+ if (!empty($this->rc->user->ID)) {
+ $calendar_ids = array();
+ $result = $this->rc->db->query("SELECT *, calendar_id AS id
+ FROM " . $this->db_calendars . "
+ WHERE user_id=?
+ ORDER BY name",
+ $this->rc->user->ID
+ );
+ while ($result && ($arr = $this->rc->db->fetch_assoc($result))) {
+ $arr['showalarms'] = intval($arr['showalarms']);
+ $arr['active'] = !in_array($arr['id'], $hidden);
+ $arr['name'] = html::quote($arr['name']);
+ $arr['listname'] = html::quote($arr['name']);
+ $arr['rights'] = 'lrswikxteav';
+ $arr['editable'] = true;
+ $arr['caldav_pass'] = $this->_decrypt_pass($arr['caldav_pass']);
+ $this->calendars[$arr['calendar_id']] = $arr;
+ $calendar_ids[] = $this->rc->db->quote($arr['calendar_id']);
+
+ // Init sync client
+ $cal_id = $arr['calendar_id'];
+ $this->sync_clients[$cal_id] = new caldav_sync($arr);
+ }
+ $this->calendar_ids = join(',', $calendar_ids);
+ }
+ }
+
+ /**
+ * Get a list of available calendars from this source
+ *
+ * @param integer Bitmask defining filter criterias
+ *
+ * @return array List of calendars
+ */
+ public function list_calendars($filter = 0)
+ {
+ $calendars = $this->calendars;
+
+ // filter active calendars
+ if ($filter & self::FILTER_ACTIVE) {
+ foreach ($calendars as $idx => $cal) {
+ if (!$cal['active']) {
+ unset($calendars[$idx]);
+ }
+ }
+ }
+
+ // 'personal' is unsupported in this driver
+
+ return $calendars;
+ }
+
+ /**
+ * Extracts CalDAV calendar.
+ *
+ * @see database_driver::create_calendar()
+ */
+ public function create_calendar($cal)
+ {
+ $result = false;
+ $cal['caldav_url'] = self::_encode_url($cal["caldav_url"]);
+ if(!isset($cal['color'])) $cal['color'] = 'cc0000';
+
+ $calendars = $this->_autodiscover_calendars($this->_expand_pass($cal));
+ $cal_ids = array();
+
+ if($calendars)
+ {
+ $result = true;
+ foreach ($calendars as $calendar)
+ {
+ // Skip already existent calendars
+ $result = $this->rc->db->query("SELECT * FROM ".$this->db_calendars." WHERE user_id=? and caldav_url LIKE ?", $this->rc->user->ID, $calendar['href']);
+ if($this->rc->db->affected_rows($result)) continue;
+
+ $cal['caldav_url'] = self::_encode_url($calendar['href']);
+
+ // Respect $props['name'] if only a single calendar was found e.g. no auto-discovery.
+ if(sizeof($calendars) > 1 || !isset($cal['name']) || $cal['name'] == "")
+ $cal['name'] = $calendar['name'];
+
+ if (($obj_id = $this->_db_create_calendar($cal)) !== false) {
+ array_push($cal_ids, $obj_id);
+ } else $result = false;
+ }
+ }
+
+ // Sync newly created calendars
+ if($cal_ids) {
+
+ // Re-read calendars to internal buffer.
+ $this->_read_calendars();
+
+ // Initial sync of newly created calendars.
+ foreach ($cal_ids as $cal_id) {
+ $this->_sync_calendar($cal_id);
+ }
+ }
+
+ return $result;
+ }
+
+ /**
+ * Create a new calendar assigned to the current user
+ *
+ * @param array Hash array with calendar properties
+ * name: Calendar name
+ * color: The color of the calendar
+ * caldav_url: CalDAV calendar URL
+ * caldav_tag: CalDAV calendar ctag
+ * caldav_user: CalDAV authentication user
+ * caldav_pass: CalDAV authentication password
+ *
+ * @return mixed ID of the calendar on success, False on error
+ */
+ private function _db_create_calendar($prop)
+ {
+ $result = $this->rc->db->query(
+ "INSERT INTO " . $this->db_calendars . "
+ (user_id, name, color, showalarms, caldav_url, caldav_tag, caldav_user, caldav_pass)
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
+ $this->rc->user->ID,
+ $prop['name'],
+ $prop['color'],
+ $prop['showalarms']?1:0,
+ $prop['caldav_url'],
+ isset($prop["caldav_tag"]) ? $prop["caldav_tag"] : null,
+ isset($prop["caldav_user"]) ? $prop["caldav_user"] : null,
+ isset($prop["caldav_pass"]) ? $this->_encrypt_pass($prop["caldav_pass"]) : null
+ );
+
+ if ($result)
+ return $this->rc->db->insert_id($this->db_calendars);
+
+ return false;
+ }
+
+ /**
+ * Update properties of an existing calendar
+ *
+ * @see calendar_driver::edit_calendar()
+ */
+ public function edit_calendar($cal)
+ {
+ $query = $this->rc->db->query("UPDATE " . $this->db_calendars . "
+ SET name=?, color=?, showalarms=?, caldav_url=?, caldav_tag=?, caldav_user=?
+ WHERE calendar_id=?
+ AND user_id=?",
+ $cal['name'],
+ $cal['color'],
+ $cal['showalarms']?1:0,
+ $cal['caldav_url'],
+ isset($cal["caldav_tag"]) ? $cal["caldav_tag"] : null,
+ isset($cal["caldav_user"]) ? $cal["caldav_user"] : null,
+ $cal['id'],
+ $this->rc->user->ID
+ );
+
+ // Change password if specified
+ if (isset($cal["caldav_pass"])) {
+ $query = $this->rc->db->query("UPDATE " . $this->db_calendars . "
+ SET caldav_pass=?
+ WHERE calendar_id=?
+ AND user_id=?",
+ $this->_encrypt_pass($cal['caldav_pass']),
+ $cal['id'],
+ $this->rc->user->ID
+ );
+ }
+
+ return $this->rc->db->affected_rows($query);
+ }
+
+ /**
+ * Set active/subscribed state of a calendar
+ * Save a list of hidden calendars in user prefs
+ *
+ * @see calendar_driver::subscribe_calendar()
+ */
+ public function subscribe_calendar($prop)
+ {
+ $hidden = array_flip(explode(',', $this->rc->config->get('hidden_caldav_calendars', '')));
+
+ if ($prop['active'])
+ unset($hidden[$prop['id']]);
+ else
+ $hidden[$prop['id']] = 1;
+
+ return $this->rc->user->save_prefs(array('hidden_caldav_calendars' => join(',', array_keys($hidden))));
+ }
+
+ /**
+ * Delete the given calendar with all its contents
+ *
+ * @see calendar_driver::delete_calendar()
+ */
+ public function delete_calendar($prop)
+ {
+ if (!$this->calendars[$prop['id']])
+ return false;
+
+ // events and attachments will be deleted by foreign key cascade
+
+ $query = $this->rc->db->query(
+ "DELETE FROM " . $this->db_calendars . " WHERE calendar_id=?",
+ $prop['id']
+ );
+
+ return $this->rc->db->affected_rows($query);
+ }
+
+ /**
+ * Search for shared or otherwise not listed calendars the user has access
+ *
+ * @param string Search string
+ * @param string Section/source to search
+ * @return array List of calendars
+ */
+ public function search_calendars($query, $source)
+ {
+ // not implemented
+ return array();
+ }
+
+ /**
+ * Add a single event to the database
+ *
+ * @param array Hash array with event properties
+ * @see calendar_driver::new_event()
+ */
+ public function new_event($event)
+ {
+ if (!$this->validate($event))
+ return false;
+
+ if (!empty($this->calendars)) {
+ if ($event['calendar'] && !$this->calendars[$event['calendar']])
+ return false;
+ if (!$event['calendar'])
+ $event['calendar'] = reset(array_keys($this->calendars));
+
+ if($event = $this->_save_preprocess($event)) {
+
+ $sync_client = $this->sync_clients[$event["calendar"]];
+
+ // Only push event if caldav_tag is not set to avoid pushing it twice
+ if (isset($event["caldav_tag"]) || ($event = $sync_client->create_event($event)) !== false) {
+
+ if ($event_id = $this->_insert_event($event)) {
+ $this->_update_recurring($event);
+ }
+ }
+ }
+
+ return $event_id;
+ }
+
+ return false;
+ }
+
+ /**
+ *
+ */
+ private function _insert_event(&$event)
+ {
+ //$event = $this->_save_preprocess($event);
+
+ $this->rc->db->query(sprintf(
+ "INSERT INTO " . $this->db_events . "
+ (calendar_id, created, changed, uid, recurrence_id, instance, isexception, %s, %s, all_day, recurrence,
+ title, description, location, categories, url, free_busy, priority, sensitivity, status, attendees, alarms, notifyat,
+ caldav_url, caldav_tag)
+ VALUES (?, %s, %s, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
+ $this->rc->db->quote_identifier('start'),
+ $this->rc->db->quote_identifier('end'),
+ $this->rc->db->now(),
+ $this->rc->db->now()
+ ),
+ $event['calendar'],
+ strval($event['uid']),
+ intval($event['recurrence_id']),
+ strval($event['_instance']),
+ intval($event['isexception']),
+ $event['start']->format(self::DB_DATE_FORMAT),
+ $event['end']->format(self::DB_DATE_FORMAT),
+ intval($event['all_day']),
+ $event['_recurrence'],
+ strval($event['title']),
+ strval($event['description']),
+ strval($event['location']),
+ join(',', (array)$event['categories']),
+ strval($event['url']),
+ intval($event['free_busy']),
+ intval($event['priority']),
+ intval($event['sensitivity']),
+ strval($event['status']),
+ $event['attendees'],
+ $event['alarms'],
+ $event['notifyat'],
+ $event['caldav_url'],
+ $event['caldav_tag']
+ );
+
+ $event_id = $this->rc->db->insert_id($this->db_events);
+
+ if ($event_id) {
+ $event['id'] = $event_id;
+
+ // add attachments
+ if (!empty($event['attachments'])) {
+ foreach ($event['attachments'] as $attachment) {
+ $this->add_attachment($attachment, $event_id);
+ unset($attachment);
+ }
+ }
+
+ return $event_id;
+ }
+
+ return false;
+ }
+
+ /**
+ * Update the event entry with the given data and sync with caldav server.
+ *
+ * @param array Hash array with event properties
+ * @param array Internal use only, filled with non-modified event if this is second try after a calendar sync was enforced first.
+ * @see caldav_driver::_db_edit_event()
+ * @return bool
+ */
+ public function edit_event($event, $old_event = null)
+ {
+ $sync_enforced = ($old_event != null);
+ $event_id = (int)$event["id"];
+ $cal_id = $event["calendar"];
+
+ if($old_event == null)
+ $old_event = $this->get_event($event);
+
+ if($this->_db_edit_event($event))
+ {
+ // Re-load updated event and push to caldav.
+ $event = $this->get_event(array("id" => $event_id));
+
+ $sync_client = $this->sync_clients[$cal_id];
+ $success = $sync_client->update_event($event);
+
+ if($success === true)
+ {
+ self::debug_log("Successfully updated event \"$event_id\".");
+
+ // Trigger calendar sync to update ctags and etags.
+ $this->_sync_calendar($cal_id);
+
+ return true;
+ }
+ else if($success < 0 && $sync_enforced == false)
+ {
+ self::debug_log("Event \"$event_id\", tag \"".$event["caldav_tag"]."\" not up to date, will update calendar first ...");
+ $this->_sync_calendar($cal_id);
+
+ return $this->edit_event($event, $old_event); // Re-try after re-sync
+ }
+ else
+ {
+ $this->_db_edit_event($old_event);
+ $this->_raise_error("Could not update event: Unexpected CalDAV error.");
+
+ return false;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * Update an event entry with the given data
+ *
+ * @param array Hash array with event properties
+ * @see calendar_driver::edit_event()
+ * @return bool
+ */
+ private function _db_edit_event($event)
+ {
+ if (!empty($this->calendars)) {
+ $update_master = false;
+ $update_recurring = true;
+ $old = $this->get_event($event);
+ $ret = true;
+
+ // check if update affects scheduling and update attendee status accordingly
+ $reschedule = $this->_check_scheduling($event, $old, true);
+
+ // increment sequence number
+ if (empty($event['sequence']) && $reschedule)
+ $event['sequence'] = max($event['sequence'], $old['sequence']) + 1;
+
+ // modify a recurring event, check submitted savemode to do the right things
+ if ($old['recurrence'] || $old['recurrence_id']) {
+ $master = $old['recurrence_id'] ? $this->get_event(array('id' => $old['recurrence_id'])) : $old;
+
+ // keep saved exceptions (not submitted by the client)
+ if ($old['recurrence']['EXDATE'])
+ $event['recurrence']['EXDATE'] = $old['recurrence']['EXDATE'];
+
+ switch ($event['_savemode']) {
+ case 'new':
+ $event['uid'] = $this->cal->generate_uid();
+ return $this->new_event($event);
+
+ case 'current':
+ // save as exception
+ $event['isexception'] = 1;
+ $update_recurring = false;
+
+ // set exception to first instance (= master)
+ if ($event['id'] == $master['id']) {
+ $event += $old;
+ $event['recurrence_id'] = $master['id'];
+ $event['_instance'] = libcalendaring::recurrence_instance_identifier($old);
+ $event['isexception'] = 1;
+ $event_id = $this->_insert_event($event);
+ return $event_id;
+ }
+ break;
+
+ case 'future':
+ if ($master['id'] != $event['id']) {
+ // set until-date on master event, then save this instance as new recurring event
+ $master['recurrence']['UNTIL'] = clone $event['start'];
+ $master['recurrence']['UNTIL']->sub(new DateInterval('P1D'));
+ unset($master['recurrence']['COUNT']);
+ $update_master = true;
+
+ // if recurrence COUNT, update value to the correct number of future occurences
+ if ($event['recurrence']['COUNT']) {
+ $fromdate = clone $event['start'];
+ $fromdate->setTimezone($this->server_timezone);
+ $sqlresult = $this->rc->db->query(sprintf(
+ "SELECT event_id FROM " . $this->db_events . "
+ WHERE calendar_id IN (%s)
+ AND %s >= ?
+ AND recurrence_id=?",
+ $this->calendar_ids,
+ $this->rc->db->quote_identifier('start')
+ ),
+ $fromdate->format(self::DB_DATE_FORMAT),
+ $master['id']);
+ if ($count = $this->rc->db->num_rows($sqlresult))
+ $event['recurrence']['COUNT'] = $count;
+ }
+
+ $update_recurring = true;
+ $event['recurrence_id'] = 0;
+ $event['isexception'] = 0;
+ $event['_instance'] = '';
+ break;
+ }
+ // else: 'future' == 'all' if modifying the master event
+
+ default: // 'all' is default
+ $event['id'] = $master['id'];
+ $event['recurrence_id'] = 0;
+
+ // use start date from master but try to be smart on time or duration changes
+ $old_start_date = $old['start']->format('Y-m-d');
+ $old_start_time = $old['allday'] ? '' : $old['start']->format('H:i');
+ $old_duration = $old['end']->format('U') - $old['start']->format('U');
+
+ $new_start_date = $event['start']->format('Y-m-d');
+ $new_start_time = $event['allday'] ? '' : $event['start']->format('H:i');
+ $new_duration = $event['end']->format('U') - $event['start']->format('U');
+
+ $diff = $old_start_date != $new_start_date || $old_start_time != $new_start_time || $old_duration != $new_duration;
+ $date_shift = $old['start']->diff($event['start']);
+
+ // shifted or resized
+ if ($diff && ($old_start_date == $new_start_date || $old_duration == $new_duration)) {
+ $event['start'] = $master['start']->add($old['start']->diff($event['start']));
+ $event['end'] = clone $event['start'];
+ $event['end']->add(new DateInterval('PT' . $new_duration . 'S'));
+ } // dates did not change, use the ones from master
+ else if ($new_start_date . $new_start_time == $old_start_date . $old_start_time) {
+ $event['start'] = $master['start'];
+ $event['end'] = $master['end'];
+ }
+
+ // adjust recurrence-id when start changed and therefore the entire recurrence chain changes
+ if (is_array($event['recurrence']) && ($old_start_date != $new_start_date || $old_start_time != $new_start_time)
+ && ($exceptions = $this->_load_exceptions($old))
+ ) {
+ $recurrence_id_format = libcalendaring::recurrence_id_format($event);
+ foreach ($exceptions as $exception) {
+ $recurrence_id = rcube_utils::anytodatetime($exception['_instance'], $old['start']->getTimezone());
+ if (is_a($recurrence_id, 'DateTime')) {
+ $recurrence_id->add($date_shift);
+ $exception['_instance'] = $recurrence_id->format($recurrence_id_format);
+ $this->_update_event($exception, false);
+ }
+ }
+ }
+
+ $ret = $event['id']; // return master ID
+ break;
+ }
+ }
+
+ $success = $this->_update_event($event, $update_recurring);
+
+ if ($success && $update_master)
+ $this->_update_event($master, true);
+
+ return $success ? $ret : false;
+ }
+
+ return false;
+ }
+
+ /**
+ * Extended event editing with possible changes to the argument
+ *
+ * @param array Hash array with event properties
+ * @param string New participant status
+ * @param array List of hash arrays with updated attendees
+ * @return boolean True on success, False on error
+ */
+ public function edit_rsvp(&$event, $status, $attendees)
+ {
+ $update_event = $event;
+
+ // apply changes to master (and all exceptions)
+ if ($event['_savemode'] == 'all' && $event['recurrence_id']) {
+ $update_event = $this->get_event(array('id' => $event['recurrence_id']));
+ $update_event['_savemode'] = $event['_savemode'];
+ calendar::merge_attendee_data($update_event, $attendees);
+ }
+
+ if ($ret = $this->update_attendees($update_event, $attendees)) {
+ // replace $event with effectively updated event (for iTip reply)
+ if ($ret !== true && $ret != $update_event['id'] && ($new_event = $this->get_event(array('id' => $ret)))) {
+ $event = $new_event;
+ } else {
+ $event = $update_event;
+ }
+ }
+
+ return $ret;
+ }
+
+ /**
+ * Update the participant status for the given attendees
+ *
+ * @see calendar_driver::update_attendees()
+ */
+ public function update_attendees(&$event, $attendees)
+ {
+ $success = $this->edit_event($event, true);
+
+ // apply attendee updates to recurrence exceptions too
+ if ($success && $event['_savemode'] == 'all' && !empty($event['recurrence']) && empty($event['recurrence_id']) && ($exceptions = $this->_load_exceptions($event))) {
+ foreach ($exceptions as $exception) {
+ calendar::merge_attendee_data($exception, $attendees);
+ $this->_update_event($exception, false);
+ }
+ }
+
+ return $success;
+ }
+
+ /**
+ * Determine whether the current change affects scheduling and reset attendee status accordingly
+ */
+ private function _check_scheduling(&$event, $old, $update = true)
+ {
+ // skip this check when importing iCal/iTip events
+ if (isset($event['sequence']) || !empty($event['_method'])) {
+ return false;
+ }
+
+ $reschedule = false;
+
+ // iterate through the list of properties considered 'significant' for scheduling
+ foreach (self::$scheduling_properties as $prop) {
+ $a = $old[$prop];
+ $b = $event[$prop];
+ if ($event['allday'] && ($prop == 'start' || $prop == 'end') && $a instanceof DateTime && $b instanceof DateTime) {
+ $a = $a->format('Y-m-d');
+ $b = $b->format('Y-m-d');
+ }
+ if ($prop == 'recurrence' && is_array($a) && is_array($b)) {
+ unset($a['EXCEPTIONS'], $b['EXCEPTIONS']);
+ $a = array_filter($a);
+ $b = array_filter($b);
+
+ // advanced rrule comparison: no rescheduling if series was shortened
+ if ($a['COUNT'] && $b['COUNT'] && $b['COUNT'] < $a['COUNT']) {
+ unset($a['COUNT'], $b['COUNT']);
+ } else if ($a['UNTIL'] && $b['UNTIL'] && $b['UNTIL'] < $a['UNTIL']) {
+ unset($a['UNTIL'], $b['UNTIL']);
+ }
+ }
+ if ($a != $b) {
+ $reschedule = true;
+ break;
+ }
+ }
+
+ // reset all attendee status to needs-action (#4360)
+ if ($update && $reschedule && is_array($event['attendees'])) {
+ $is_organizer = false;
+ $emails = $this->cal->get_user_emails();
+ $attendees = $event['attendees'];
+ foreach ($attendees as $i => $attendee) {
+ if ($attendee['role'] == 'ORGANIZER' && $attendee['email'] && in_array(strtolower($attendee['email']), $emails)) {
+ $is_organizer = true;
+ } else if ($attendee['role'] != 'ORGANIZER' && $attendee['role'] != 'NON-PARTICIPANT' && $attendee['status'] != 'DELEGATED') {
+ $attendees[$i]['status'] = 'NEEDS-ACTION';
+ $attendees[$i]['rsvp'] = true;
+ }
+ }
+
+ // update attendees only if I'm the organizer
+ if ($is_organizer || ($event['organizer'] && in_array(strtolower($event['organizer']['email']), $emails))) {
+ $event['attendees'] = $attendees;
+ }
+ }
+
+ return $reschedule;
+ }
+
+ /**
+ * Convert save data to be used in SQL statements
+ */
+ private function _save_preprocess($event)
+ {
+ // shift dates to server's timezone (except for all-day events)
+ if (!$event['allday']) {
+ $event['start'] = clone $event['start'];
+ $event['start']->setTimezone($this->server_timezone);
+ $event['end'] = clone $event['end'];
+ $event['end']->setTimezone($this->server_timezone);
+ }
+
+ // compose vcalendar-style recurrencue rule from structured data
+ $rrule = $event['recurrence'] ? libcalendaring::to_rrule($event['recurrence']) : '';
+ $event['_recurrence'] = rtrim($rrule, ';');
+ $event['free_busy'] = intval($this->free_busy_map[strtolower($event['free_busy'])]);
+ $event['sensitivity'] = intval($this->sensitivity_map[strtolower($event['sensitivity'])]);
+
+ if ($event['free_busy'] == 'tentative') {
+ $event['status'] = 'TENTATIVE';
+ }
+
+ if (isset($event['allday'])) {
+ $event['all_day'] = $event['allday'] ? 1 : 0;
+ }
+
+ // compute absolute time to notify the user
+ $event['notifyat'] = $this->_get_notification($event);
+
+ if (is_array($event['valarms'])) {
+ $event['alarms'] = $this->serialize_alarms($event['valarms']);
+ }
+
+ // process event attendees
+ if (!empty($event['attendees']))
+ $event['attendees'] = json_encode((array)$event['attendees']);
+ else
+ $event['attendees'] = '';
+
+ return $event;
+ }
+
+ /**
+ * Compute absolute time to notify the user
+ */
+ private function _get_notification($event)
+ {
+ if ($event['valarms'] && $event['start'] > new DateTime()) {
+ $alarm = libcalendaring::get_next_alarm($event);
+
+ if ($alarm['time'] && in_array($alarm['action'], $this->alarm_types))
+ return date('Y-m-d H:i:s', $alarm['time']);
+ }
+
+ return null;
+ }
+
+ /**
+ * Save the given event record to database
+ *
+ * @param array Event data
+ * @param boolean True if recurring events instances should be updated, too
+ */
+ private function _update_event($event, $update_recurring = true)
+ {
+ $event = $this->_save_preprocess($event);
+ $sql_set = array();
+ $set_cols = array('start', 'end', 'all_day', 'recurrence_id', 'isexception', 'sequence', 'title', 'description', 'location', 'categories', 'url', 'free_busy', 'priority', 'sensitivity', 'status', 'attendees', 'alarms', 'notifyat', 'caldav_url', 'caldav_tag');
+ foreach ($set_cols as $col) {
+ if (is_object($event[$col]) && is_a($event[$col], 'DateTime'))
+ $sql_set[] = $this->rc->db->quote_identifier($col) . '=' . $this->rc->db->quote($event[$col]->format(self::DB_DATE_FORMAT));
+ else if (is_array($event[$col]))
+ $sql_set[] = $this->rc->db->quote_identifier($col) . '=' . $this->rc->db->quote(join(',', $event[$col]));
+ else if (array_key_exists($col, $event))
+ $sql_set[] = $this->rc->db->quote_identifier($col) . '=' . $this->rc->db->quote($event[$col]);
+ }
+
+ if ($event['_recurrence'])
+ $sql_set[] = $this->rc->db->quote_identifier('recurrence') . '=' . $this->rc->db->quote($event['_recurrence']);
+
+ if ($event['_instance'])
+ $sql_set[] = $this->rc->db->quote_identifier('instance') . '=' . $this->rc->db->quote($event['_instance']);
+
+ if ($event['_fromcalendar'] && $event['_fromcalendar'] != $event['calendar'])
+ $sql_set[] = 'calendar_id=' . $this->rc->db->quote($event['calendar']);
+
+ $query = $this->rc->db->query(sprintf(
+ "UPDATE " . $this->db_events . "
+ SET changed=%s %s
+ WHERE event_id=?
+ AND calendar_id IN (" . $this->calendar_ids . ")",
+ $this->rc->db->now(),
+ ($sql_set ? ', ' . join(', ', $sql_set) : '')
+ ),
+ $event['id']
+ );
+
+ $success = $this->rc->db->affected_rows($query);
+
+ // add attachments
+ if ($success && !empty($event['attachments'])) {
+ foreach ($event['attachments'] as $attachment) {
+ $this->add_attachment($attachment, $event['id']);
+ unset($attachment);
+ }
+ }
+
+ // remove attachments
+ if ($success && !empty($event['deleted_attachments'])) {
+ foreach ($event['deleted_attachments'] as $attachment) {
+ $this->remove_attachment($attachment, $event['id']);
+ }
+ }
+
+ if ($success) {
+ unset($this->cache[$event['id']]);
+ if ($update_recurring)
+ $this->_update_recurring($event);
+ }
+
+ return $success;
+ }
+
+ /**
+ * Insert "fake" entries for recurring occurences of this event
+ */
+ private function _update_recurring($event)
+ {
+ if (empty($this->calendars))
+ return;
+
+ if (!empty($event['recurrence'])) {
+ $exdata = array();
+ $exceptions = $this->_load_exceptions($event);
+
+ foreach ($exceptions as $exception) {
+ $exdate = substr($exception['_instance'], 0, 8);
+ $exdata[$exdate] = $exception;
+ }
+ }
+
+ // clear existing recurrence copies
+ $this->rc->db->query(
+ "DELETE FROM " . $this->db_events . "
+ WHERE recurrence_id=?
+ AND isexception=0
+ AND calendar_id IN (" . $this->calendar_ids . ")",
+ $event['id']
+ );
+
+ // create new fake entries
+ if (!empty($event['recurrence'])) {
+ // include library class
+ require_once($this->cal->home . '/lib/calendar_recurrence.php');
+
+ $recurrence = new calendar_recurrence($this->cal, $event);
+
+ $count = 0;
+ $event['allday'] = $event['all_day'];
+ $duration = $event['start']->diff($event['end']);
+ $recurrence_id_format = libcalendaring::recurrence_id_format($event);
+ while ($next_start = $recurrence->next_start()) {
+ $instance = $next_start->format($recurrence_id_format);
+ $datestr = substr($instance, 0, 8);
+
+ // skip exceptions
+ // TODO: merge updated data from master event
+ if ($exdata[$datestr]) {
+ continue;
+ }
+
+ $next_start->setTimezone($this->server_timezone);
+ $next_end = clone $next_start;
+ $next_end->add($duration);
+
+ $notify_at = $this->_get_notification(array('alarms' => $event['alarms'], 'start' => $next_start, 'end' => $next_end, 'status' => $event['status']));
+ $query = $this->rc->db->query(sprintf(
+ "INSERT INTO " . $this->db_events . "
+ (calendar_id, recurrence_id, created, changed, uid, instance, %s, %s, all_day, sequence, recurrence, title, description, location, categories, url, free_busy, priority, sensitivity, status, alarms, attendees, notifyat, caldav_url, caldav_tag)
+ SELECT calendar_id, ?, %s, %s, uid, ?, ?, ?, all_day, sequence, recurrence, title, description, location, categories, url, free_busy, priority, sensitivity, status, alarms, attendees, ?, caldav_url, caldav_tag
+ FROM " . $this->db_events . " WHERE event_id=? AND calendar_id IN (" . $this->calendar_ids . ")",
+ $this->rc->db->quote_identifier('start'),
+ $this->rc->db->quote_identifier('end'),
+ $this->rc->db->now(),
+ $this->rc->db->now()
+ ),
+ $event['id'],
+ $instance,
+ $next_start->format(self::DB_DATE_FORMAT),
+ $next_end->format(self::DB_DATE_FORMAT),
+ $notify_at,
+ $event['id']
+ );
+
+ if (!$this->rc->db->affected_rows($query))
+ break;
+
+ // stop adding events for inifinite recurrence after 20 years
+ if (++$count > 999 || (!$recurrence->recurEnd && !$recurrence->recurCount && $next_start->format('Y') > date('Y') + 20))
+ break;
+ }
+
+ // remove all exceptions after recurrence end
+ if ($next_end && !empty($exceptions)) {
+ $this->rc->db->query(
+ "DELETE FROM " . $this->db_events . "
+ WHERE `recurrence_id`=?
+ AND `isexception`=1
+ AND `start` > ?
+ AND `calendar_id` IN (" . $this->calendar_ids . ")",
+ $event['id'],
+ $next_end->format(self::DB_DATE_FORMAT)
+ );
+ }
+ }
+ }
+
+ /**
+ *
+ */
+ private function _load_exceptions($event, $instance_id = null)
+ {
+ $sql_add_where = '';
+ if (!empty($instance_id)) {
+ $sql_add_where = 'AND `instance`=?';
+ }
+
+ $result = $this->rc->db->query(
+ "SELECT * FROM " . $this->db_events . "
+ WHERE `recurrence_id`=?
+ AND `isexception`=1
+ AND `calendar_id` IN (" . $this->calendar_ids . ")
+ $sql_add_where
+ ORDER BY `instance`, `start`",
+ $event['id'],
+ $instance_id
+ );
+
+ $exceptions = array();
+ while ($result && ($sql_arr = $this->rc->db->fetch_assoc($result)) && $sql_arr['event_id']) {
+ $exception = $this->_read_postprocess($sql_arr);
+ $instance = $exception['_instance'] ?: $exception['start']->format($exception['allday'] ? 'Ymd' : 'Ymd\THis');
+ $exceptions[$instance] = $exception;
+ }
+
+ return $exceptions;
+ }
+
+ /**
+ * Move a single event
+ *
+ * @param array Hash array with event properties
+ * @see calendar_driver::move_event()
+ * @return bool
+ */
+ public function move_event($event)
+ {
+ // let edit_event() do all the magic
+ return $this->edit_event($event + (array)$this->get_event($event));
+ }
+
+ /**
+ * Resize a single event
+ *
+ * @param array Hash array with event properties
+ * @see calendar_driver::resize_event()
+ * @return bool
+ */
+ public function resize_event($event)
+ {
+ // let edit_event() do all the magic
+ return $this->edit_event($event + (array)$this->get_event($event));
+ }
+
+ /**
+ * Remove a single event from the database and from the CalDAV server.
+ *
+ * @param array Hash array with event properties
+ * @param boolean Remove record irreversible
+ *
+ * @see calendar_driver::remove_event()
+ * @return bool
+ */
+ public function remove_event($event, $force = true)
+ {
+ $event_id = (int)$event["id"];
+ $cal_id = (int)$event["calendar"];
+ $event = $this->get_event($event);
+
+ $sync_client = $this->sync_clients[$cal_id];
+ $success = $sync_client->remove_event($event);
+
+ if($success === true)
+ {
+ $this->_db_remove_event($event, $force);
+ self::debug_log("Successfully removed event \"$event_id\".");
+
+ // Trigger calendar sync to update ctags and etags.
+ $this->_sync_calendar($cal_id);
+
+ return true;
+ }
+
+ $this->_raise_error("Could not remove event: Unexpected CalDAV error.");
+ return false;
+ }
+
+ /**
+ * Remove a single event from the database
+ *
+ * @param array Hash array with event properties
+ * @param boolean Remove record irreversible (@TODO)
+ *
+ * @see calendar_driver::remove_event()
+ * @return bool
+ */
+ private function _db_remove_event($event, $force = true)
+ {
+ if (!empty($this->calendars)) {
+ $event += (array)$this->get_event($event);
+ $master = $event;
+ $update_master = false;
+ $savemode = 'all';
+ $ret = true;
+
+ // read master if deleting a recurring event
+ if ($event['recurrence'] || $event['recurrence_id']) {
+ $master = $event['recurrence_id'] ? $this->get_event(array('id' => $event['recurrence_id'])) : $event;
+ $savemode = $event['_savemode'];
+ }
+
+ switch ($savemode) {
+ case 'current':
+ // add exception to master event
+ $master['recurrence']['EXDATE'][] = $event['start'];
+ $update_master = true;
+
+ // just delete this single occurence
+ $query = $this->rc->db->query(
+ "DELETE FROM " . $this->db_events . "
+ WHERE calendar_id IN (" . $this->calendar_ids . ")
+ AND event_id=?",
+ $event['id']
+ );
+ break;
+
+ case 'future':
+ if ($master['id'] != $event['id']) {
+ // set until-date on master event
+ $master['recurrence']['UNTIL'] = clone $event['start'];
+ $master['recurrence']['UNTIL']->sub(new DateInterval('P1D'));
+ unset($master['recurrence']['COUNT']);
+ $update_master = true;
+
+ // delete this and all future instances
+ $fromdate = clone $event['start'];
+ $fromdate->setTimezone($this->server_timezone);
+ $query = $this->rc->db->query(
+ "DELETE FROM " . $this->db_events . "
+ WHERE calendar_id IN (" . $this->calendar_ids . ")
+ AND " . $this->rc->db->quote_identifier('start') . " >= ?
+ AND recurrence_id=?",
+ $fromdate->format(self::DB_DATE_FORMAT),
+ $master['id']
+ );
+ $ret = $master['id'];
+ break;
+ }
+ // else: future == all if modifying the master event
+
+ default: // 'all' is default
+ $query = $this->rc->db->query(
+ "DELETE FROM " . $this->db_events . "
+ WHERE (event_id=? OR recurrence_id=?)
+ AND calendar_id IN (" . $this->calendar_ids . ")",
+ $master['id'],
+ $master['id']
+ );
+ break;
+ }
+
+ $success = $this->rc->db->affected_rows($query);
+ if ($success && $update_master)
+ $this->_update_event($master, true);
+
+ return $success ? $ret : false;
+ }
+
+ return false;
+ }
+
+ /**
+ * Return data of a specific event
+ * @param mixed Hash array with event properties or event UID
+ * @param integer Bitmask defining the scope to search events in
+ * @param boolean If true, recurrence exceptions shall be added
+ * @return array Hash array with event properties
+ */
+ public function get_event($event, $scope = 0, $full = false)
+ {
+ $id = is_array($event) ? ($event['id'] ?: $event['uid']) : $event;
+ $cal = is_array($event) ? $event['calendar'] : null;
+ $col = is_array($event) && is_numeric($id) ? 'event_id' : 'uid';
+
+ $where_add = '';
+ if (is_array($event) && !$event['id'] && !empty($event['_instance'])) {
+ $where_add = 'AND instance=' . $this->rc->db->quote($event['_instance']);
+ }
+
+ if ($this->cache[$id])
+ return $this->cache[$id];
+
+ if ($scope & self::FILTER_ACTIVE) {
+ $calendars = $this->calendars;
+ foreach ($calendars as $idx => $cal) {
+ if (!$cal['active']) {
+ unset($calendars[$idx]);
+ }
+ }
+ $cals = join(',', $calendars);
+ } else {
+ $cals = $this->calendar_ids;
+ }
+
+ $result = $this->rc->db->query(sprintf(
+ "SELECT e.*, (SELECT COUNT(attachment_id) FROM " . $this->db_attachments . "
+ WHERE event_id = e.event_id OR event_id = e.recurrence_id) AS _attachments
+ FROM " . $this->db_events . " AS e
+ WHERE e.calendar_id IN (%s)
+ AND e.$col=?
+ %s",
+ $cals,
+ $where_add
+ ),
+ $id);
+
+ if ($result && ($sql_arr = $this->rc->db->fetch_assoc($result)) && $sql_arr['event_id']) {
+ $event = $this->_read_postprocess($sql_arr);
+
+ // also load recurrence exceptions
+ if (!empty($event['recurrence']) && $full) {
+ $event['recurrence']['EXCEPTIONS'] = array_values($this->_load_exceptions($event));
+ }
+
+ $this->cache[$id] = $event;
+ return $this->cache[$id];
+ }
+
+ return false;
+ }
+
+ /**
+ * Sync and returns event data
+ *
+ * @see calendar_driver::load_events()
+ */
+ public function load_events($start, $end, $query = null, $calendars = null, $virtual = 1, $modifiedsince = null)
+ {
+ if (empty($calendars))
+ $calendars = array_keys($this->calendars);
+ else if (!is_array($calendars))
+ $calendars = explode(',', strval($calendars));
+
+ // only allow to select from calendars of this use
+ $calendar_ids = array_intersect($calendars, array_keys($this->calendars));
+
+ // Make sure that the calendars are in sync.
+ foreach ($calendar_ids as $cal_id) {
+ if (!$this->_is_synced($cal_id))
+ $this->_sync_calendar($cal_id);
+ }
+
+ return $this->_db_load_events($start, $end, $query, $calendars, $virtual, $modifiedsince);
+ }
+
+ /**
+ * Get event data
+ *
+ * @see calendar_driver::load_events()
+ */
+ private function _db_load_events($start, $end, $query = null, $calendars = null, $virtual = 1, $modifiedsince = null)
+ {
+ if (empty($calendars))
+ $calendars = array_keys($this->calendars);
+ else if (!is_array($calendars))
+ $calendars = explode(',', strval($calendars));
+
+ // only allow to select from calendars of this use
+ $calendar_ids = array_map(array($this->rc->db, 'quote'), array_intersect($calendars, array_keys($this->calendars)));
+
+ // compose (slow) SQL query for searching
+ // FIXME: improve searching using a dedicated col and normalized values
+ if ($query) {
+ foreach (array('title', 'location', 'description', 'categories', 'attendees') as $col)
+ $sql_query[] = $this->rc->db->ilike($col, '%' . $query . '%');
+ $sql_add = 'AND (' . join(' OR ', $sql_query) . ')';
+ }
+
+ if (!$virtual)
+ $sql_add .= ' AND e.recurrence_id = 0';
+
+ if ($modifiedsince)
+ $sql_add .= ' AND e.changed >= ' . $this->rc->db->quote(date('Y-m-d H:i:s', $modifiedsince));
+
+ $events = array();
+ if (!empty($calendar_ids)) {
+ $result = $this->rc->db->query(sprintf(
+ "SELECT e.*, (SELECT COUNT(attachment_id) FROM " . $this->db_attachments . "
+ WHERE event_id = e.event_id OR event_id = e.recurrence_id) AS _attachments
+ FROM " . $this->db_events . " e
+ WHERE e.calendar_id IN (%s)
+ AND e.start <= %s AND e.end >= %s
+ %s",
+ join(',', $calendar_ids),
+ $this->rc->db->fromunixtime($end),
+ $this->rc->db->fromunixtime($start),
+ $sql_add
+ ));
+
+ while ($result && ($sql_arr = $this->rc->db->fetch_assoc($result))) {
+ $event = $this->_read_postprocess($sql_arr);
+ $add = true;
+
+ if (!empty($event['recurrence']) && !$event['recurrence_id']) {
+ // load recurrence exceptions (i.e. for export)
+ if (!$virtual) {
+ $event['recurrence']['EXCEPTIONS'] = $this->_load_exceptions($event);
+ } // check for exception on first instance
+ else {
+ $instance = libcalendaring::recurrence_instance_identifier($event);
+ $exceptions = $this->_load_exceptions($event, $instance);
+ if ($exceptions && is_array($exceptions[$instance])) {
+ $event = $exceptions[$instance];
+ $add = false;
+ }
+ }
+ }
+
+ if ($add)
+ $events[] = $event;
+ }
+ }
+
+ return $events;
+ }
+
+ /**
+ * Get number of events in the given calendar
+ *
+ * @param mixed List of calendar IDs to count events (either as array or comma-separated string)
+ * @param integer Date range start (unix timestamp)
+ * @param integer Date range end (unix timestamp)
+ * @return array Hash array with counts grouped by calendar ID
+ */
+ public function count_events($calendars, $start, $end = null)
+ {
+ // not implemented
+ return array();
+ }
+
+ /**
+ * Convert sql record into a rcube style event object
+ */
+ private function _read_postprocess($event)
+ {
+ $free_busy_map = array_flip($this->free_busy_map);
+ $sensitivity_map = array_flip($this->sensitivity_map);
+
+ $event['id'] = $event['event_id'];
+ $event['start'] = new DateTime($event['start']);
+ $event['end'] = new DateTime($event['end']);
+ $event['allday'] = intval($event['all_day']);
+ $event['created'] = new DateTime($event['created']);
+ $event['changed'] = new DateTime($event['changed']);
+ $event['free_busy'] = $free_busy_map[$event['free_busy']];
+ $event['sensitivity'] = $sensitivity_map[$event['sensitivity']];
+ $event['calendar'] = $event['calendar_id'];
+ $event['recurrence_id'] = intval($event['recurrence_id']);
+ $event['isexception'] = intval($event['isexception']);
+
+ // parse recurrence rule
+ if ($event['recurrence'] && preg_match_all('/([A-Z]+)=([^;]+);?/', $event['recurrence'], $m, PREG_SET_ORDER)) {
+ $event['recurrence'] = array();
+ foreach ($m as $rr) {
+ if (is_numeric($rr[2]))
+ $rr[2] = intval($rr[2]);
+ else if ($rr[1] == 'UNTIL')
+ $rr[2] = date_create($rr[2]);
+ else if ($rr[1] == 'RDATE')
+ $rr[2] = array_map('date_create', explode(',', $rr[2]));
+ else if ($rr[1] == 'EXDATE')
+ $rr[2] = array_map('date_create', explode(',', $rr[2]));
+ $event['recurrence'][$rr[1]] = $rr[2];
+ }
+ }
+
+ if ($event['recurrence_id']) {
+ libcalendaring::identify_recurrence_instance($event);
+ }
+
+ if (strlen($event['instance'])) {
+ $event['_instance'] = $event['instance'];
+
+ if (empty($event['recurrence_id'])) {
+ $event['recurrence_date'] = rcube_utils::anytodatetime($event['_instance'], $event['start']->getTimezone());
+ }
+ }
+
+ if ($event['_attachments'] > 0) {
+ $event['attachments'] = (array)$this->list_attachments($event);
+ }
+
+ // decode serialized event attendees
+ if (strlen($event['attendees'])) {
+ $event['attendees'] = $this->unserialize_attendees($event['attendees']);
+ } else {
+ $event['attendees'] = array();
+ }
+
+ // decode serialized alarms
+ if ($event['alarms']) {
+ $event['valarms'] = $this->unserialize_alarms($event['alarms']);
+ }
+
+ unset($event['event_id'], $event['calendar_id'], $event['notifyat'], $event['all_day'], $event['instance'], $event['_attachments']);
+ return $event;
+ }
+
+ /**
+ * Get a list of pending alarms to be displayed to the user
+ *
+ * @see calendar_driver::pending_alarms()
+ */
+ public function pending_alarms($time, $calendars = null)
+ {
+ if (empty($calendars))
+ $calendars = array_keys($this->calendars);
+ else if (is_string($calendars))
+ $calendars = explode(',', $calendars);
+
+ // only allow to select from calendars with activated alarms
+ $calendar_ids = array();
+ foreach ($calendars as $cid) {
+ if ($this->calendars[$cid] && $this->calendars[$cid]['showalarms'])
+ $calendar_ids[] = $cid;
+ }
+ $calendar_ids = array_map(array($this->rc->db, 'quote'), $calendar_ids);
+
+ $alarms = array();
+ if (!empty($calendar_ids)) {
+ $result = $this->rc->db->query(sprintf(
+ "SELECT * FROM " . $this->db_events . "
+ WHERE calendar_id IN (%s)
+ AND notifyat <= %s AND %s > %s",
+ join(',', $calendar_ids),
+ $this->rc->db->fromunixtime($time),
+ $this->rc->db->quote_identifier('end'),
+ $this->rc->db->fromunixtime($time)
+ ));
+
+ while ($result && ($event = $this->rc->db->fetch_assoc($result)))
+ $alarms[] = $this->_read_postprocess($event);
+ }
+
+ return $alarms;
+ }
+
+ /**
+ * Feedback after showing/sending an alarm notification
+ *
+ * @see calendar_driver::dismiss_alarm()
+ */
+ public function dismiss_alarm($event_id, $snooze = 0)
+ {
+ // set new notifyat time or unset if not snoozed
+ $notify_at = $snooze > 0 ? date(self::DB_DATE_FORMAT, time() + $snooze) : null;
+
+ $query = $this->rc->db->query(sprintf(
+ "UPDATE " . $this->db_events . "
+ SET changed=%s, notifyat=?
+ WHERE event_id=?
+ AND calendar_id IN (" . $this->calendar_ids . ")",
+ $this->rc->db->now()),
+ $notify_at,
+ $event_id
+ );
+
+ return $this->rc->db->affected_rows($query);
+ }
+
+ /**
+ * Save an attachment related to the given event
+ */
+ private function add_attachment($attachment, $event_id)
+ {
+ $data = $attachment['data'] ? $attachment['data'] : file_get_contents($attachment['path']);
+
+ $query = $this->rc->db->query(
+ "INSERT INTO " . $this->db_attachments .
+ " (event_id, filename, mimetype, size, data)" .
+ " VALUES (?, ?, ?, ?, ?)",
+ $event_id,
+ $attachment['name'],
+ $attachment['mimetype'],
+ strlen($data),
+ base64_encode($data)
+ );
+
+ return $this->rc->db->affected_rows($query);
+ }
+
+ /**
+ * Remove a specific attachment from the given event
+ */
+ private function remove_attachment($attachment_id, $event_id)
+ {
+ $query = $this->rc->db->query(
+ "DELETE FROM " . $this->db_attachments .
+ " WHERE attachment_id = ?" .
+ " AND event_id IN (SELECT event_id FROM " . $this->db_events .
+ " WHERE event_id = ?" .
+ " AND calendar_id IN (" . $this->calendar_ids . "))",
+ $attachment_id,
+ $event_id
+ );
+
+ return $this->rc->db->affected_rows($query);
+ }
+
+ /**
+ * List attachments of specified event
+ */
+ public function list_attachments($event)
+ {
+ $attachments = array();
+
+ if (!empty($this->calendar_ids)) {
+ $result = $this->rc->db->query(
+ "SELECT attachment_id AS id, filename AS name, mimetype, size " .
+ " FROM " . $this->db_attachments .
+ " WHERE event_id IN (SELECT event_id FROM " . $this->db_events .
+ " WHERE event_id=?" .
+ " AND calendar_id IN (" . $this->calendar_ids . "))".
+ " ORDER BY filename",
+ $event['recurrence_id'] ? $event['recurrence_id'] : $event['event_id']
+ );
+
+ while ($result && ($arr = $this->rc->db->fetch_assoc($result))) {
+ $attachments[] = $arr;
+ }
+ }
+
+ return $attachments;
+ }
+
+ /**
+ * Get attachment properties
+ */
+ public function get_attachment($id, $event)
+ {
+ if (!empty($this->calendar_ids)) {
+ $result = $this->rc->db->query(
+ "SELECT attachment_id AS id, filename AS name, mimetype, size " .
+ " FROM " . $this->db_attachments .
+ " WHERE attachment_id=?".
+ " AND event_id=?",
+ $id,
+ $event['recurrence_id'] ? $event['recurrence_id'] : $event['id']
+ );
+
+ if ($result && ($arr = $this->rc->db->fetch_assoc($result))) {
+ return $arr;
+ }
+ }
+
+ return null;
+ }
+
+ /**
+ * Get attachment body
+ */
+ public function get_attachment_body($id, $event)
+ {
+ if (!empty($this->calendar_ids)) {
+ $result = $this->rc->db->query(
+ "SELECT data " .
+ " FROM " . $this->db_attachments .
+ " WHERE attachment_id=?".
+ " AND event_id=?",
+ $id,
+ $event['id']
+ );
+
+ if ($result && ($arr = $this->rc->db->fetch_assoc($result))) {
+ return base64_decode($arr['data']);
+ }
+ }
+
+ return null;
+ }
+
+ /**
+ * Remove the given category
+ */
+ public function remove_category($name)
+ {
+ $query = $this->rc->db->query(
+ "UPDATE " . $this->db_events . "
+ SET categories=''
+ WHERE categories=?
+ AND calendar_id IN (" . $this->calendar_ids . ")",
+ $name
+ );
+
+ return $this->rc->db->affected_rows($query);
+ }
+
+ /**
+ * Update/replace a category
+ */
+ public function replace_category($oldname, $name, $color)
+ {
+ $query = $this->rc->db->query(
+ "UPDATE " . $this->db_events . "
+ SET categories=?
+ WHERE categories=?
+ AND calendar_id IN (" . $this->calendar_ids . ")",
+ $name,
+ $oldname
+ );
+
+ return $this->rc->db->affected_rows($query);
+ }
+
+ /**
+ * Helper method to serialize the list of alarms into a string
+ */
+ private function serialize_alarms($valarms)
+ {
+ foreach ((array)$valarms as $i => $alarm) {
+ if ($alarm['trigger'] instanceof DateTime) {
+ $valarms[$i]['trigger'] = '@' . $alarm['trigger']->format('c');
+ }
+ }
+
+ return $valarms ? json_encode($valarms) : null;
+ }
+
+ /**
+ * Helper method to decode a serialized list of alarms
+ */
+ private function unserialize_alarms($alarms)
+ {
+ // decode json serialized alarms
+ if ($alarms && $alarms[0] == '[') {
+ $valarms = json_decode($alarms, true);
+ foreach ($valarms as $i => $alarm) {
+ if ($alarm['trigger'][0] == '@') {
+ try {
+ $valarms[$i]['trigger'] = new DateTime(substr($alarm['trigger'], 1));
+ }
+ catch (Exception $e) {
+ unset($valarms[$i]);
+ }
+ }
+ }
+ }
+ // convert legacy alarms data
+ else if (strlen($alarms)) {
+ list($trigger, $action) = explode(':', $alarms, 2);
+ if ($trigger = libcalendaring::parse_alarm_value($trigger)) {
+ $valarms = array(array('action' => $action, 'trigger' => $trigger[3] ?: $trigger[0]));
+ }
+ }
+
+ return $valarms;
+ }
+
+ /**
+ * Helper method to decode the attendees list from string
+ */
+ private function unserialize_attendees($s_attendees)
+ {
+ $attendees = array();
+
+ // decode json serialized string
+ if ($s_attendees[0] == '[') {
+ $attendees = json_decode($s_attendees, true);
+ } // decode the old serialization format
+ else {
+ foreach (explode("\n", $event['attendees']) as $line) {
+ $att = array();
+ foreach (rcube_utils::explode_quoted_string(';', $line) as $prop) {
+ list($key, $value) = explode("=", $prop);
+ $att[strtolower($key)] = stripslashes(trim($value, '""'));
+ }
+ $attendees[] = $att;
+ }
+ }
+
+ return $attendees;
+ }
+
+ /**
+ * Handler for user_delete plugin hook
+ */
+ public function user_delete($args)
+ {
+ $db = $this->rc->db;
+ $user = $args['user'];
+ $event_ids = array();
+
+ $events = $db->query(
+ "SELECT event_id FROM " . $this->db_events . " AS ev" .
+ " LEFT JOIN " . $this->db_calendars . " cal ON (ev.calendar_id = cal.calendar_id)".
+ " WHERE user_id=?",
+ $user->ID);
+
+ while ($row = $db->fetch_assoc($events)) {
+ $event_ids[] = $row['event_id'];
+ }
+
+ if (!empty($event_ids)) {
+ foreach (array($this->db_attachments, $this->db_events) as $table) {
+ $db->query(sprintf("DELETE FROM $table WHERE event_id IN (%s)", join(',', $event_ids)));
+ }
+ }
+
+ foreach (array($this->db_calendars, 'itipinvitations') as $table) {
+ $db->query("DELETE FROM $table WHERE user_id=?", $user->ID);
+ }
+ }
+
+ /**
+ * Callback function to produce driver-specific calendar create/edit form
+ *
+ * @param string Request action 'form-edit|form-new'
+ * @param array Calendar properties (e.g. id, color)
+ * @param array Edit form fields
+ *
+ * @return string HTML content of the form
+ */
+ public function calendar_form($action, $calendar, $formfields)
+ {
+ // Make sure we have current attributes
+ $calendar = $this->calendars[$calendar["id"]];
+
+ $input_caldav_url = new html_inputfield( array(
+ "name" => "caldav_url",
+ "id" => "caldav_url",
+ "size" => 20
+ ));
+
+ $formfields["caldav_url"] = array(
+ "label" => $this->cal->gettext("caldavurl"),
+ "value" => $input_caldav_url->show($calendar["caldav_url"]),
+ "id" => "caldav_url",
+ );
+
+ $input_caldav_user = new html_inputfield( array(
+ "name" => "caldav_user",
+ "id" => "caldav_user",
+ "size" => 20
+ ));
+
+ $formfields["caldav_user"] = array(
+ "label" => $this->cal->gettext("username"),
+ "value" => $input_caldav_user->show($calendar["caldav_user"]),
+ "id" => "caldav_user",
+ );
+
+ $input_caldav_pass = new html_passwordfield( array(
+ "name" => "caldav_pass",
+ "id" => "caldav_pass",
+ "size" => 20
+ ));
+
+ $formfields["caldav_pass"] = array(
+ "label" => $this->cal->gettext("password"),
+ "value" => $input_caldav_pass->show(null), // Don't send plain text password to GUI
+ "id" => "caldav_pass",
+ );
+
+ return parent::calendar_form($action, $calendar, $formfields);
+ }
+
+ /**
+ * Encodes directory- and filenames using rawurlencode().
+ *
+ * @see http://stackoverflow.com/questions/7973790/urlencode-only-the-directory-and-file-names-of-a-url
+ * @param string Unencoded URL to be encoded.
+ * @return Encoded URL.
+ */
+ private static function _encode_url($url)
+ {
+ // Don't encode if "%" is already used.
+ if(strstr($url, "%") === false)
+ {
+ return preg_replace_callback('#://([^/]+)/([^?]+)#', function ($matches) {
+ return '://' . $matches[1] . '/' . join('/', array_map('rawurlencode', explode('/', $matches[2])));
+ }, $url);
+ }
+ else return $url;
+ }
+
+ /**
+ * Expand all "%p" occurrences in 'caldav_pass' element of calendar object
+ * properties array with RC (imap) password.
+ * Other elements are left untouched.
+ *
+ * @param array List of properties
+ * @return array List of properties, with expanded 'caldav_pass' attribute
+ *
+ */
+ private function _expand_pass($props)
+ {
+ if (isset($props['caldav_pass']))
+ $props['caldav_pass'] = str_replace('%p', $this->rc->get_user_password(), $props['caldav_pass']);
+
+ return $props;
+ }
+
+ /**
+ * Auto discover calenders available to the user on the caldav server
+ * @param array $props
+ * caldav_url: Absolute URL to CalDAV server
+ * caldav_user: Username
+ * caldav_pass: Password
+ * @return False on error or an array with the following calendar props:
+ * name: Calendar display name
+ * href: Absolute calendar URL
+ */
+ private function _autodiscover_calendars($props)
+ {
+ $calendars = array();
+ $current_user_principal = array('{DAV:}current-user-principal');
+ $calendar_home_set = array('{urn:ietf:params:xml:ns:caldav}calendar-home-set');
+ $cal_attribs = array('{DAV:}resourcetype', '{DAV:}displayname');
+
+ require_once ($this->cal->home.'/lib/caldav-client.php');
+ $caldav = new caldav_client($props["caldav_url"], $props["caldav_user"], $props["caldav_pass"]);
+
+ $tokens = parse_url($props["caldav_url"]);
+ $base_uri = $tokens['scheme']."://".$tokens['host'].($tokens['port'] ? ":".$tokens['port'] : null);
+ $caldav_url = $props["caldav_url"];
+ $response = $caldav->prop_find($caldav_url, array_merge($current_user_principal,$cal_attribs), 0);
+ if (!$response) {
+ $this->_raise_error("Resource \"$caldav_url\" has no collections");
+ return false;
+ }
+ else if (array_key_exists ('{DAV:}resourcetype', $response) &&
+ $response['{DAV:}resourcetype'] instanceof OldSabre\DAV\Property\ResourceType &&
+ in_array('{urn:ietf:params:xml:ns:caldav}calendar',
+ $response['{DAV:}resourcetype']->getValue())) {
+
+ $name = '';
+ if (array_key_exists ('{DAV:}displayname', $response)) {
+ $name = $response['{DAV:}displayname'];
+ }
+
+ array_push($calendars, array(
+ 'name' => $name,
+ 'href' => $caldav_url,
+ ));
+ return $calendars;
+ // directly return given url as it is a calendar
+ }
+ // probe further for principal url and user home set
+ $caldav_url = $base_uri . $response[$current_user_principal[0]];
+ $response = $caldav->prop_find($caldav_url, $calendar_home_set, 0);
+ if (!$response) {
+ $this->_raise_error("Resource \"$caldav_url\" contains no calendars.");
+ return false;
+ }
+ $caldav_url = $base_uri . $response[$calendar_home_set[0]];
+ $response = $caldav->prop_find($caldav_url, $cal_attribs, 1);
+ foreach($response as $collection => $attribs)
+ {
+ $found = false;
+ $name = '';
+ foreach($attribs as $key => $value)
+ {
+ if ($key == '{DAV:}resourcetype' && is_object($value)) {
+ if ($value instanceof OldSabre\DAV\Property\ResourceType) {
+ $values = $value->getValue();
+ if (in_array('{urn:ietf:params:xml:ns:caldav}calendar', $values))
+ $found = true;
+ }
+ }
+ else if ($key == '{DAV:}displayname') {
+ $name = $value;
+ }
+ }
+ if ($found) {
+ array_push($calendars, array(
+ 'name' => $name,
+ 'href' => $base_uri.$collection,
+ ));
+ }
+ }
+
+ return $calendars;
+ }
+
+ /**
+ * Synchronizes events of given calendar.
+ *
+ * @param int Calendar ID to sync
+ */
+ private function _sync_calendar($cal_id)
+ {
+ self::debug_log("Syncing calendar id \"$cal_id\".");
+
+ $cal_sync = $this->sync_clients[$cal_id];
+ $events = array();
+
+ // Ignore recurrence events and read caldav props
+ foreach($this->_load_all_events($cal_id) as $event) {
+ if($event["recurrence_id"] == 0) {
+ array_push($events, $event);
+ }
+ }
+
+ $updates = $cal_sync->get_updates($events);
+ if($updates)
+ {
+ list($updates, $synced_event_ids) = $updates;
+ $updated_event_ids = $this->_perform_updates($updates);
+
+ // Delete events that are not in sync or updated.
+ foreach($events as $event)
+ {
+ if(array_search($event["id"], $updated_event_ids) === false && // No updated event
+ array_search($event["id"], $synced_event_ids) === false) // No in-sync event
+ {
+ // Assume: Event not in sync and not updated, so delete!
+ $this->_db_remove_event($event, true);
+ self::debug_log("Remove event \"".$event["id"]."\".");
+ }
+ }
+
+ // Update calendar ctag ...
+ $calendar = $this->calendars[$cal_id];
+ $calendar["caldav_tag"] = $cal_sync->get_ctag();
+ $this->edit_calendar($calendar);
+ }
+
+ self::debug_log("Successfully synced calendar id \"$cal_id\".");
+ }
+
+ /**
+ * Return all events from the given calendar.
+ *
+ * @param int Calendar id.
+ * @return array
+ */
+ private function _load_all_events($cal_id)
+ {
+ // FIXME: This is kind of ugly but a way to get _all_ events without touching the database driver.
+
+ // Get the event with the maximum end time.
+ $result = $this->rc->db->query(
+ "SELECT MAX(e.end) as end FROM ".$this->db_events." e ".
+ "WHERE e.calendar_id = ? ", $cal_id);
+
+ if($result && ($arr = $this->rc->db->fetch_assoc($result))) {
+ $end = new DateTime($arr["end"]);
+
+ // Don't use load_events() which is doing another sync while this method might be already invoked in an sync.
+ return $this->_db_load_events(0, $end->getTimestamp(), null, array($cal_id));
+ }
+ else return array();
+ }
+
+ /**
+ * Performs caldav updates on given events.
+ *
+ * @param array Caldav and event properties to update. See caldav_sync::get_updates().
+ * @return array List of event ids.
+ */
+ private function _perform_updates($updates)
+ {
+ $event_ids = array();
+
+ $num_created = 0;
+ $num_updated = 0;
+
+ foreach($updates as $update)
+ {
+ // local event -> update event
+ if(isset($update["local_event"]))
+ {
+ // Overwrite local event attributes with new event, url + etag.
+ $event = array_merge((array)$update["local_event"], $update["remote_event"], array(
+ "caldav_url" => $update["url"],
+ "caldav_tag" => $update["etag"]));
+
+ // let edit_event() do all the magic
+ if($this->_db_edit_event($event))
+ {
+ $event_id = $event["id"];
+ array_push($event_ids, $event_id);
+ $num_updated ++;
+ }
+ else
+ {
+ self::debug_log("Could not perform event update: ".print_r($update, true));
+ }
+ }
+
+ // no local event -> create event
+ else
+ {
+ $event = array_merge($update["remote_event"], array(
+ "caldav_url" => $update["url"],
+ "caldav_tag" => $update["etag"]));
+
+ $event_id = $this->new_event($event);
+ if($event_id)
+ {
+ self::debug_log("Created event \"$event_id\".");
+ array_push($event_ids, $event_id);
+ $num_created ++;
+ }
+ else
+ {
+ self::debug_log("Could not perform event creation: ".print_r($update, true));
+ }
+ }
+ }
+
+ self::debug_log("Created $num_created new events, updated $num_updated event.");
+ return $event_ids;
+ }
+
+ /**
+ * Determines whether the given calendar is in sync regarding
+ * calendar's ctag and the configured sync period.
+ *
+ * @param int Calender id.
+ * @return boolean True if calendar is in sync, true otherwise.
+ */
+ private function _is_synced($cal_id)
+ {
+ // Atomic sql: Check for exceeded sync period and update last_change.
+ $query = $this->rc->db->query(
+ "UPDATE ".$this->db_calendars." ".
+ "SET caldav_last_change = NOW() WHERE calendar_id = ? AND ".
+ $this->_unix_timestamp('caldav_last_change') ." + ? <= ".$this->_unix_timestamp('NOW()'),
+ $cal_id, $this->sync_period);
+
+ if($query->rowCount() > 0)
+ {
+ $is_synced = $this->sync_clients[$cal_id]->is_synced();
+ self::debug_log("Calendar \"$cal_id\" ".($is_synced ? "is in sync" : "needs update").".");
+ return $is_synced;
+ }
+ else
+ {
+ self::debug_log("Sync period active: Assuming calendar \"$cal_id\" to be in sync.");
+ return true;
+ }
+ }
+
+ /**
+ * Returns db-specific timestamp queries for epoch format
+ *
+ * @param str column name or valid timestamp (e.g. NOW())
+ * @return str db-specific timestamp query for epoch format
+ */
+ private function _unix_timestamp($field)
+ {
+ switch ($this->rc->db->db_provider) {
+ case 'postgres':
+ return "EXTRACT (EPOCH FROM $field)";
+ default:
+ return "UNIX_TIMESTAMP($field)";
+ }
+ }
+
+ private function _decrypt_pass($pass) {
+ $p = base64_decode($pass);
+ $e = new Encryption(MCRYPT_BlOWFISH, MCRYPT_MODE_CBC);
+ return $e->decrypt($p, $this->crypt_key);
+ }
+
+ private function _encrypt_pass($pass) {
+ $e = new Encryption(MCRYPT_BlOWFISH, MCRYPT_MODE_CBC);
+ $p = $e->encrypt($pass, $this->crypt_key);
+ return base64_encode($p);
+ }
+}
diff --git a/calendar/drivers/caldav/caldav_sync.php b/calendar/drivers/caldav/caldav_sync.php
new file mode 100644
index 0000000..efe92c2
--- /dev/null
+++ b/calendar/drivers/caldav/caldav_sync.php
@@ -0,0 +1,253 @@
+<?php
+/**
+ * CalDAV sync for the Calendar plugin
+ *
+ * @version @package_version@
+ * @author Daniel Morlock <daniel.morlock@awesome-it.de>
+ *
+ * Copyright (C) Awesome IT GbR <info@awesome-it.de>
+ *
+ * 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.
+ *
+ * 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
+ * GNU Affero General Public License for more details.
+ *
+ * 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/>.
+ */
+
+require_once (dirname(__FILE__).'/../../lib/caldav-client.php');
+
+class caldav_sync
+{
+ const ACTION_NONE = 1;
+ const ACTION_UPDATE = 2;
+ const ACTION_CREATE = 4;
+
+ private $cal_id = null;
+ private $ctag = null;
+ private $username = null;
+ private $pass = null;
+ private $url = null;
+
+ /**
+ * Default constructor for calendar synchronization adapter.
+ *
+ * @param array Hash array with caldav properties at least the following:
+ * id: Calendar ID
+ * caldav_url: Caldav calendar URL.
+ * caldav_user: Caldav http basic auth user.
+ * caldav_pass: Password für caldav user.
+ * caldav_tag: Caldav ctag for calendar.
+ */
+ public function __construct($cal)
+ {
+ $this->cal_id = $cal["id"];
+ $this->url = $cal["caldav_url"];
+ $this->ctag = isset($cal["caldav_tag"]) ? $cal["caldav_tag"] : null;
+ $this->username = isset($cal["caldav_user"]) ? $cal["caldav_user"] : null;
+ $this->pass = isset($cal["caldav_pass"]) ? $cal["caldav_pass"] : null;
+
+ $this->caldav = new caldav_client($this->url, $this->username, $this->pass);
+ }
+
+ /**
+ * Getter for current calendar ctag.
+ * @return string
+ */
+ public function get_ctag()
+ {
+ return $this->ctag;
+ }
+
+ /**
+ * Determines whether current calendar needs to be synced
+ * regarding the CalDAV ctag.
+ *
+ * @return True if the current calendar ctag differs from the CalDAV tag which
+ * indicates that there are changes that must be synched. Returns false
+ * if the calendar is up to date, no sync necesarry.
+ */
+ public function is_synced()
+ {
+ $is_synced = $this->ctag == $this->caldav->get_ctag() && $this->ctag;
+ caldav_driver::debug_log("Ctag indicates that calendar \"$this->cal_id\" ".($is_synced ? "is synced." : "needs update!"));
+
+ return $is_synced;
+ }
+
+ /**
+ * Synchronizes given events with caldav server and returns updates.
+ *
+ * @param array List of hash arrays with event properties, must include "caldav_url" and "tag".
+ * @return array Tuple containing the following lists:
+ *
+ * Caldav properties for events to be created or to be updated with the keys:
+ * url: Event ical URL relative to calendar URL
+ * etag: Remote etag of the event
+ * local_event: The local event in case of an update.
+ * remote_event: The current event retrieved from caldav server.
+ *
+ * A list of event ids that are in sync.
+ */
+ public function get_updates($events)
+ {
+ $ctag = $this->caldav->get_ctag();
+
+ if($ctag)
+ {
+ $this->ctag = $ctag;
+ $etags = $this->caldav->get_etags();
+
+ list($updates, $synced_event_ids) = $this->_get_event_updates($events, $etags);
+ return array($this->_get_event_data($updates), $synced_event_ids);
+ }
+ else
+ {
+ caldav_driver::debug_log("Unkown error while fetching calendar ctag for calendar \"$this->cal_id\"!");
+ }
+
+ return null;
+ }
+
+ /**
+ * Determines sync status and requried updates for the given events using given list of etags.
+ *
+ * @param array List of hash arrays with event properties, must include "caldav_url" and "caldav_tag".
+ * @param array List of current remote etags.
+ * @return array Tuple containing the following lists:
+ *
+ * Caldav properties for events to be created or to be updated with the keys:
+ * url: Event ical URL relative to calendar URL
+ * etag: Remote etag of the event
+ * local_event: The local event in case of an update.
+ *
+ * A list of event ids that are in sync.
+ */
+ private function _get_event_updates($events, $etags)
+ {
+ $updates = array();
+ $in_sync = array();
+
+ foreach ($etags as $etag)
+ {
+ $url = $etag["url"];
+ $etag = $etag["etag"];
+ $event_found = false;
+ foreach($events as $event)
+ {
+ if ($event["caldav_url"] == $url)
+ {
+ $event_found = true;
+
+ if ($event["caldav_tag"] != $etag)
+ {
+ caldav_driver::debug_log("Event ".$event["uid"]." needs update.");
+
+ array_push($updates, array(
+ "local_event" => $event,
+ "etag" => $etag,
+ "url" => $url
+ ));
+ }
+ else
+ {
+ array_push($in_sync, $event["id"]);
+ }
+ }
+ }
+
+ if (!$event_found)
+ {
+ caldav_driver::debug_log("Found new event ".$url);
+
+ array_push($updates, array(
+ "url" => $url,
+ "etag" => $etag
+ ));
+ }
+ }
+
+ return array($updates, $in_sync);
+ }
+
+ /**
+ * Fetches event data and attaches it to the given update properties.
+ *
+ * @param $updates List of update properties.
+ * @return array List of update properties with additional key "remote_event" containing the current caldav event.
+ */
+ private function _get_event_data($updates)
+ {
+ $urls = array();
+
+ foreach ($updates as $update)
+ {
+ array_push($urls, $update["url"]);
+ }
+
+ $events = $this->caldav->get_events($urls);
+ foreach($updates as &$update)
+ {
+ // Attach remote events to the appropriate updates.
+ // Note that this assumes unique event URL's!
+ $url = $update["url"];
+ if($events[$url]) {
+ $update["remote_event"] = $events[$url];
+ $update["remote_event"]["calendar"] = $this->cal_id;
+ }
+ }
+
+ return $updates;
+ }
+
+ /**
+ * Creates the given event on the CalDAV server.
+ *
+ * @param array Hash array with event properties.
+ * @return Event with updated "caldav_url" and "caldav_tag" attributes, false on error.
+ */
+ public function create_event($event)
+ {
+ $props = array(
+ "caldav_url" => parse_url($this->url, PHP_URL_PATH)."/".$event["uid"].".ics",
+ "caldav_tag" => null
+ );
+
+ caldav_driver::debug_log("Push new event to url ".$props["caldav_url"]);
+ $result = $this->caldav->put_event($props["caldav_url"], $event);
+
+ if($result == false || $result < 0) return false;
+ return array_merge($event, $props);
+ }
+
+ /**
+ * Updates the given event on the CalDAV server.
+ *
+ * @param array Hash array with event properties to update, must include "uid", "caldav_url" and "caldav_tag".
+ * @return True on success, false on error, -1 if the given event/etag is not up to date.
+ */
+ public function update_event($event)
+ {
+ caldav_driver::debug_log("Updating event uid \"".$event["uid"]."\".");
+ return $this->caldav->put_event($event["caldav_url"], $event, $event["caldav_tag"]);
+ }
+
+ /**
+ * Removes the given event from the caldav server.
+ *
+ * @param array Hash array with events properties, must include "caldav_url".
+ * @return True on success, false on error.
+ */
+ public function remove_event($event)
+ {
+ caldav_driver::debug_log("Removing event uid \"".$event["uid"]."\".");
+ return $this->caldav->remove_event($event["caldav_url"]);
+ }
+};
+?>
diff --git a/calendar/drivers/calendar_driver.php b/calendar/drivers/calendar_driver.php
new file mode 100644
index 0000000..3202003
--- /dev/null
+++ b/calendar/drivers/calendar_driver.php
@@ -0,0 +1,819 @@
+<?php
+
+/**
+ * Driver interface for the Calendar plugin
+ *
+ * @version @package_version@
+ * @author Lazlo Westerhof <hello@lazlo.me>
+ * @author Thomas Bruederli <bruederli@kolabsys.com>
+ *
+ * Copyright (C) 2010, Lazlo Westerhof <hello@lazlo.me>
+ * Copyright (C) 2012-2015, Kolab Systems AG <contact@kolabsys.com>
+ *
+ * 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.
+ *
+ * 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
+ * GNU Affero General Public License for more details.
+ *
+ * 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/>.
+ */
+
+
+/**
+ * Struct of an internal event object how it is passed from/to the driver classes:
+ *
+ * $event = array(
+ * 'id' => 'Event ID used for editing',
+ * 'uid' => 'Unique identifier of this event',
+ * 'calendar' => 'Calendar identifier to add event to or where the event is stored',
+ * 'start' => DateTime, // Event start date/time as DateTime object
+ * 'end' => DateTime, // Event end date/time as DateTime object
+ * 'allday' => true|false, // Boolean flag if this is an all-day event
+ * 'changed' => DateTime, // Last modification date of event
+ * 'title' => 'Event title/summary',
+ * 'location' => 'Location string',
+ * 'description' => 'Event description',
+ * 'url' => 'URL to more information',
+ * 'recurrence' => array( // Recurrence definition according to iCalendar (RFC 2445) specification as list of key-value pairs
+ * 'FREQ' => 'DAILY|WEEKLY|MONTHLY|YEARLY',
+ * 'INTERVAL' => 1...n,
+ * 'UNTIL' => DateTime,
+ * 'COUNT' => 1..n, // number of times
+ * // + more properties (see http://www.kanzaki.com/docs/ical/recur.html)
+ * 'EXDATE' => array(), // list of DateTime objects of exception Dates/Times
+ * 'EXCEPTIONS' => array(<event>), list of event objects which denote exceptions in the recurrence chain
+ * ),
+ * 'recurrence_id' => 'ID of the recurrence group', // usually the ID of the starting event
+ * '_instance' => 'ID of the recurring instance', // identifies an instance within a recurrence chain
+ * 'categories' => 'Event category',
+ * 'free_busy' => 'free|busy|outofoffice|tentative', // Show time as
+ * 'status' => 'TENTATIVE|CONFIRMED|CANCELLED', // event status according to RFC 2445
+ * 'priority' => 0-9, // Event priority (0=undefined, 1=highest, 9=lowest)
+ * 'sensitivity' => 'public|private|confidential', // Event sensitivity
+ * 'alarms' => '-15M:DISPLAY', // DEPRECATED Reminder settings inspired by valarm definition (e.g. display alert 15 minutes before event)
+ * 'valarms' => array( // List of reminders (new format), each represented as a hash array:
+ * array(
+ * 'trigger' => '-PT90M', // ISO 8601 period string prefixed with '+' or '-', or DateTime object
+ * 'action' => 'DISPLAY|EMAIL|AUDIO',
+ * 'duration' => 'PT15M', // ISO 8601 period string
+ * 'repeat' => 0, // number of repetitions
+ * 'description' => '', // text to display for DISPLAY actions
+ * 'summary' => '', // message text for EMAIL actions
+ * 'attendees' => array(), // list of email addresses to receive alarm messages
+ * ),
+ * ),
+ * 'attachments' => array( // List of attachments
+ * 'name' => 'File name',
+ * 'mimetype' => 'Content type',
+ * 'size' => 1..n, // in bytes
+ * 'id' => 'Attachment identifier'
+ * ),
+ * 'deleted_attachments' => array(), // array of attachment identifiers to delete when event is updated
+ * 'attendees' => array( // List of event participants
+ * 'name' => 'Participant name',
+ * 'email' => 'Participant e-mail address', // used as identifier
+ * 'role' => 'ORGANIZER|REQ-PARTICIPANT|OPT-PARTICIPANT|CHAIR',
+ * 'status' => 'NEEDS-ACTION|UNKNOWN|ACCEPTED|TENTATIVE|DECLINED'
+ * 'rsvp' => true|false,
+ * ),
+ *
+ * '_savemode' => 'all|future|current|new', // How changes on recurring event should be handled
+ * '_notify' => true|false, // whether to notify event attendees about changes
+ * '_fromcalendar' => 'Calendar identifier where the event was stored before',
+ * );
+ */
+
+/**
+ * Interface definition for calendar driver classes
+ */
+abstract class calendar_driver
+{
+ const FILTER_ALL = 0;
+ const FILTER_WRITEABLE = 1;
+ const FILTER_INSERTABLE = 2;
+ const FILTER_ACTIVE = 4;
+ const FILTER_PERSONAL = 8;
+ const FILTER_PRIVATE = 16;
+ const FILTER_CONFIDENTIAL = 32;
+ const BIRTHDAY_CALENDAR_ID = '__bdays__';
+
+ // features supported by backend
+ public $alarms = false;
+ public $attendees = false;
+ public $freebusy = false;
+ public $attachments = false;
+ public $undelete = false;
+ public $history = false;
+ public $categoriesimmutable = false;
+ public $alarm_types = array('DISPLAY');
+ public $alarm_absolute = true;
+ public $last_error;
+
+ protected $default_categories = array(
+ 'Personal' => 'c0c0c0',
+ 'Work' => 'ff0000',
+ 'Family' => '00ff00',
+ 'Holiday' => 'ff6600',
+ );
+
+ /**
+ * Get a list of available calendars from this source
+ *
+ * @param integer Bitmask defining filter criterias.
+ * See FILTER_* constants for possible values.
+ * @return array List of calendars
+ */
+ abstract function list_calendars($filter = 0);
+
+ /**
+ * Create a new calendar assigned to the current user
+ *
+ * @param array Hash array with calendar properties
+ * name: Calendar name
+ * color: The color of the calendar
+ * showalarms: True if alarms are enabled
+ * @return mixed ID of the calendar on success, False on error
+ */
+ abstract function create_calendar($prop);
+
+ /**
+ * Update properties of an existing calendar
+ *
+ * @param array Hash array with calendar properties
+ * id: Calendar Identifier
+ * name: Calendar name
+ * color: The color of the calendar
+ * showalarms: True if alarms are enabled (if supported)
+ * @return boolean True on success, Fales on failure
+ */
+ abstract function edit_calendar($prop);
+
+ /**
+ * Set active/subscribed state of a calendar
+ *
+ * @param array Hash array with calendar properties
+ * id: Calendar Identifier
+ * active: True if calendar is active, false if not
+ * @return boolean True on success, Fales on failure
+ */
+ abstract function subscribe_calendar($prop);
+
+ /**
+ * Delete the given calendar with all its contents
+ *
+ * @param array Hash array with calendar properties
+ * id: Calendar Identifier
+ * @return boolean True on success, Fales on failure
+ */
+ abstract function delete_calendar($prop);
+
+ /**
+ * Search for shared or otherwise not listed calendars the user has access
+ *
+ * @param string Search string
+ * @param string Section/source to search
+ * @return array List of calendars
+ */
+ abstract function search_calendars($query, $source);
+
+ /**
+ * Add a single event to the database
+ *
+ * @param array Hash array with event properties (see header of this file)
+ * @return mixed New event ID on success, False on error
+ */
+ abstract function new_event($event);
+
+ /**
+ * Update an event entry with the given data
+ *
+ * @param array Hash array with event properties (see header of this file)
+ * @return boolean True on success, False on error
+ */
+ abstract function edit_event($event);
+
+ /**
+ * Extended event editing with possible changes to the argument
+ *
+ * @param array Hash array with event properties
+ * @param string New participant status
+ * @param array List of hash arrays with updated attendees
+ * @return boolean True on success, False on error
+ */
+ public function edit_rsvp(&$event, $status, $attendees)
+ {
+ return $this->edit_event($event);
+ }
+
+ /**
+ * Update the participant status for the given attendee
+ *
+ * @param array Hash array with event properties
+ * @param array List of hash arrays each represeting an updated attendee
+ * @return boolean True on success, False on error
+ */
+ public function update_attendees(&$event, $attendees)
+ {
+ return $this->edit_event($event);
+ }
+
+ /**
+ * Move a single event
+ *
+ * @param array Hash array with event properties:
+ * id: Event identifier
+ * start: Event start date/time as DateTime object
+ * end: Event end date/time as DateTime object
+ * allday: Boolean flag if this is an all-day event
+ * @return boolean True on success, False on error
+ */
+ abstract function move_event($event);
+
+ /**
+ * Resize a single event
+ *
+ * @param array Hash array with event properties:
+ * id: Event identifier
+ * start: Event start date/time as DateTime object with timezone
+ * end: Event end date/time as DateTime object with timezone
+ * @return boolean True on success, False on error
+ */
+ abstract function resize_event($event);
+
+ /**
+ * Remove a single event from the database
+ *
+ * @param array Hash array with event properties:
+ * id: Event identifier
+ * @param boolean Remove event irreversible (mark as deleted otherwise,
+ * if supported by the backend)
+ *
+ * @return boolean True on success, False on error
+ */
+ abstract function remove_event($event, $force = true);
+
+ /**
+ * Restores a single deleted event (if supported)
+ *
+ * @param array Hash array with event properties:
+ * id: Event identifier
+ *
+ * @return boolean True on success, False on error
+ */
+ public function restore_event($event)
+ {
+ return false;
+ }
+
+ /**
+ * Return data of a single event
+ *
+ * @param mixed UID string or hash array with event properties:
+ * id: Event identifier
+ * uid: Event UID
+ * _instance: Instance identifier in combination with uid (optional)
+ * calendar: Calendar identifier (optional)
+ * @param integer Bitmask defining the scope to search events in.
+ * See FILTER_* constants for possible values.
+ * @param boolean If true, recurrence exceptions shall be added
+ *
+ * @return array Event object as hash array
+ */
+ abstract function get_event($event, $scope = 0, $full = false);
+
+ /**
+ * Get events from source.
+ *
+ * @param integer Date range start (unix timestamp)
+ * @param integer Date range end (unix timestamp)
+ * @param string Search query (optional)
+ * @param mixed List of calendar IDs to load events from (either as array or comma-separated string)
+ * @param boolean Include virtual/recurring events (optional)
+ * @param integer Only list events modified since this time (unix timestamp)
+ * @return array A list of event objects (see header of this file for struct of an event)
+ */
+ abstract function load_events($start, $end, $query = null, $calendars = null, $virtual = 1, $modifiedsince = null);
+
+ /**
+ * Get number of events in the given calendar
+ *
+ * @param mixed List of calendar IDs to count events (either as array or comma-separated string)
+ * @param integer Date range start (unix timestamp)
+ * @param integer Date range end (unix timestamp)
+ * @return array Hash array with counts grouped by calendar ID
+ */
+ abstract function count_events($calendars, $start, $end = null);
+
+ /**
+ * Get a list of pending alarms to be displayed to the user
+ *
+ * @param integer Current time (unix timestamp)
+ * @param mixed List of calendar IDs to show alarms for (either as array or comma-separated string)
+ * @return array A list of alarms, each encoded as hash array:
+ * id: Event identifier
+ * uid: Unique identifier of this event
+ * start: Event start date/time as DateTime object
+ * end: Event end date/time as DateTime object
+ * allday: Boolean flag if this is an all-day event
+ * title: Event title/summary
+ * location: Location string
+ */
+ abstract function pending_alarms($time, $calendars = null);
+
+ /**
+ * (User) feedback after showing an alarm notification
+ * This should mark the alarm as 'shown' or snooze it for the given amount of time
+ *
+ * @param string Event identifier
+ * @param integer Suspend the alarm for this number of seconds
+ */
+ abstract function dismiss_alarm($event_id, $snooze = 0);
+
+ /**
+ * Check the given event object for validity
+ *
+ * @param array Event object as hash array
+ * @return boolean True if valid, false if not
+ */
+ public function validate($event)
+ {
+ $valid = true;
+
+ if (!is_object($event['start']) || !is_a($event['start'], 'DateTime'))
+ $valid = false;
+ if (!is_object($event['end']) || !is_a($event['end'], 'DateTime'))
+ $valid = false;
+
+ return $valid;
+ }
+
+
+ /**
+ * Get list of event's attachments.
+ * Drivers can return list of attachments as event property.
+ * If they will do not do this list_attachments() method will be used.
+ *
+ * @param array $event Hash array with event properties:
+ * id: Event identifier
+ * calendar: Calendar identifier
+ *
+ * @return array List of attachments, each as hash array:
+ * id: Attachment identifier
+ * name: Attachment name
+ * mimetype: MIME content type of the attachment
+ * size: Attachment size
+ */
+ public function list_attachments($event) { }
+
+ /**
+ * Get attachment properties
+ *
+ * @param string $id Attachment identifier
+ * @param array $event Hash array with event properties:
+ * id: Event identifier
+ * calendar: Calendar identifier
+ *
+ * @return array Hash array with attachment properties:
+ * id: Attachment identifier
+ * name: Attachment name
+ * mimetype: MIME content type of the attachment
+ * size: Attachment size
+ */
+ public function get_attachment($id, $event) { }
+
+ /**
+ * Get attachment body
+ *
+ * @param string $id Attachment identifier
+ * @param array $event Hash array with event properties:
+ * id: Event identifier
+ * calendar: Calendar identifier
+ *
+ * @return string Attachment body
+ */
+ public function get_attachment_body($id, $event) { }
+
+ /**
+ * Build a struct representing the given message reference
+ *
+ * @param object|string $uri_or_headers rcube_message_header instance holding the message headers
+ * or an URI from a stored link referencing a mail message.
+ * @param string $folder IMAP folder the message resides in
+ *
+ * @return array An struct referencing the given IMAP message
+ */
+ public function get_message_reference($uri_or_headers, $folder = null)
+ {
+ // to be implemented by the derived classes
+ return false;
+ }
+
+ /**
+ * List availabale categories
+ * The default implementation reads them from config/user prefs
+ */
+ public function list_categories()
+ {
+ $rcmail = rcube::get_instance();
+ return $rcmail->config->get('calendar_categories', $this->default_categories);
+ }
+
+ /**
+ * Create a new category
+ */
+ public function add_category($name, $color) { }
+
+ /**
+ * Remove the given category
+ */
+ public function remove_category($name) { }
+
+ /**
+ * Update/replace a category
+ */
+ public function replace_category($oldname, $name, $color) { }
+
+ /**
+ * Fetch free/busy information from a person within the given range
+ *
+ * @param string E-mail address of attendee
+ * @param integer Requested period start date/time as unix timestamp
+ * @param integer Requested period end date/time as unix timestamp
+ *
+ * @return array List of busy timeslots within the requested range
+ */
+ public function get_freebusy_list($email, $start, $end)
+ {
+ return false;
+ }
+
+ /**
+ * Create instances of a recurring event
+ *
+ * @param array Hash array with event properties
+ * @param object DateTime Start date of the recurrence window
+ * @param object DateTime End date of the recurrence window
+ * @return array List of recurring event instances
+ */
+ public function get_recurring_events($event, $start, $end = null)
+ {
+ $events = array();
+
+ if ($event['recurrence']) {
+ // include library class
+ require_once(dirname(__FILE__) . '/../lib/calendar_recurrence.php');
+
+ $rcmail = rcmail::get_instance();
+ $recurrence = new calendar_recurrence($rcmail->plugins->get_plugin('calendar'), $event);
+ $recurrence_id_format = libcalendaring::recurrence_id_format($event);
+
+ // determine a reasonable end date if none given
+ if (!$end) {
+ switch ($event['recurrence']['FREQ']) {
+ case 'YEARLY': $intvl = 'P100Y'; break;
+ case 'MONTHLY': $intvl = 'P20Y'; break;
+ default: $intvl = 'P10Y'; break;
+ }
+
+ $end = clone $event['start'];
+ $end->add(new DateInterval($intvl));
+ }
+
+ $i = 0;
+ while ($next_event = $recurrence->next_instance()) {
+ // add to output if in range
+ if (($next_event['start'] <= $end && $next_event['end'] >= $start)) {
+ $next_event['_instance'] = $next_event['start']->format($recurrence_id_format);
+ $next_event['id'] = $next_event['uid'] . '-' . $next_event['_instance'];
+ $next_event['recurrence_id'] = $event['uid'];
+ $events[] = $next_event;
+ }
+ else if ($next_event['start'] > $end) { // stop loop if out of range
+ break;
+ }
+
+ // avoid endless recursion loops
+ if (++$i > 1000) {
+ break;
+ }
+ }
+ }
+
+ return $events;
+ }
+
+ /**
+ * Provide a list of revisions for the given event
+ *
+ * @param array $event Hash array with event properties:
+ * id: Event identifier
+ * calendar: Calendar identifier
+ *
+ * @return array List of changes, each as a hash array:
+ * rev: Revision number
+ * type: Type of the change (create, update, move, delete)
+ * date: Change date
+ * user: The user who executed the change
+ * ip: Client IP
+ * destination: Destination calendar for 'move' type
+ */
+ public function get_event_changelog($event)
+ {
+ return false;
+ }
+
+ /**
+ * Get a list of property changes beteen two revisions of an event
+ *
+ * @param array $event Hash array with event properties:
+ * id: Event identifier
+ * calendar: Calendar identifier
+ * @param mixed $rev Revisions: "from:to"
+ *
+ * @return array List of property changes, each as a hash array:
+ * property: Revision number
+ * old: Old property value
+ * new: Updated property value
+ */
+ public function get_event_diff($event, $rev)
+ {
+ return false;
+ }
+
+ /**
+ * Return full data of a specific revision of an event
+ *
+ * @param mixed UID string or hash array with event properties:
+ * id: Event identifier
+ * calendar: Calendar identifier
+ * @param mixed $rev Revision number
+ *
+ * @return array Event object as hash array
+ * @see self::get_event()
+ */
+ public function get_event_revison($event, $rev)
+ {
+ return false;
+ }
+
+ /**
+ * Command the backend to restore a certain revision of an event.
+ * This shall replace the current event with an older version.
+ *
+ * @param mixed UID string or hash array with event properties:
+ * id: Event identifier
+ * calendar: Calendar identifier
+ * @param mixed $rev Revision number
+ *
+ * @return boolean True on success, False on failure
+ */
+ public function restore_event_revision($event, $rev)
+ {
+ return false;
+ }
+
+
+ /**
+ * Callback function to produce driver-specific calendar create/edit form
+ *
+ * @param string Request action 'form-edit|form-new'
+ * @param array Calendar properties (e.g. id, color)
+ * @param array Edit form fields
+ *
+ * @return string HTML content of the form
+ */
+ public function calendar_form($action, $calendar, $formfields)
+ {
+ $html = '';
+ foreach ($formfields as $field) {
+ $html .= html::div('form-section',
+ html::label($field['id'], $field['label']) .
+ $field['value']);
+ }
+
+ return $html;
+ }
+
+ /**
+ * Compose a list of birthday events from the contact records in the user's address books.
+ *
+ * This is a default implementation using Roundcube's address book API.
+ * It can be overriden with a more optimized version by the individual drivers.
+ *
+ * @param integer Event's new start (unix timestamp)
+ * @param integer Event's new end (unix timestamp)
+ * @param string Search query (optional)
+ * @param integer Only list events modified since this time (unix timestamp)
+ * @return array A list of event records
+ */
+ public function load_birthday_events($start, $end, $search = null, $modifiedsince = null)
+ {
+ // ignore update requests for simplicity reasons
+ if (!empty($modifiedsince)) {
+ return array();
+ }
+
+ // convert to DateTime for comparisons
+ $start = new DateTime('@'.$start);
+ $end = new DateTime('@'.$end);
+ // extract the current year
+ $year = $start->format('Y');
+ $year2 = $end->format('Y');
+
+ $events = array();
+ $search = mb_strtolower($search);
+ $rcmail = rcmail::get_instance();
+ $cache = $rcmail->get_cache('calendar.birthdays', 'db', 3600);
+ $cache->expunge();
+
+ $alarm_type = $rcmail->config->get('calendar_birthdays_alarm_type', '');
+ $alarm_offset = $rcmail->config->get('calendar_birthdays_alarm_offset', '-1D');
+ $alarms = $alarm_type ? $alarm_offset . ':' . $alarm_type : null;
+
+ // let the user select the address books to consider in prefs
+ $selected_sources = $rcmail->config->get('calendar_birthday_adressbooks');
+ $sources = $selected_sources ?: array_keys($rcmail->get_address_sources(false, true));
+ foreach ($sources as $source) {
+ $abook = $rcmail->get_address_book($source);
+
+ // skip LDAP address books unless selected by the user
+ if (!$abook || ($abook instanceof rcube_ldap && empty($selected_sources))) {
+ continue;
+ }
+
+ $abook->set_pagesize(10000);
+
+ // check for cached results
+ $cache_records = array();
+ $cached = $cache->get($source);
+
+ // iterate over (cached) contacts
+ foreach (($cached ?: $abook->search('*', '', 2, true, true, array('birthday'))) as $contact) {
+ if (is_array($contact) && !empty($contact['birthday'])) {
+ try {
+ if (is_array($contact['birthday']))
+ $contact['birthday'] = reset($contact['birthday']);
+
+ $bday = $contact['birthday'] instanceof DateTime ? $contact['birthday'] :
+ new DateTime($contact['birthday'], new DateTimezone('UTC'));
+ $birthyear = $bday->format('Y');
+ }
+ catch (Exception $e) {
+ rcube::raise_error(array(
+ 'code' => 600, 'type' => 'php',
+ 'file' => __FILE__, 'line' => __LINE__,
+ 'message' => 'BIRTHDAY PARSE ERROR: ' . $e),
+ true, false);
+ continue;
+ }
+
+ $display_name = rcube_addressbook::compose_display_name($contact);
+ $event_title = $rcmail->gettext(array('name' => 'birthdayeventtitle', 'vars' => array('name' => $display_name)), 'calendar');
+
+ // add stripped record to cache
+ if (empty($cached)) {
+ $cache_records[] = array(
+ 'ID' => $contact['ID'],
+ 'name' => $display_name,
+ 'birthday' => $bday->format('Y-m-d'),
+ );
+ }
+
+ // filter by search term (only name is involved here)
+ if (!empty($search) && strpos(mb_strtolower($event_title), $search) === false) {
+ continue;
+ }
+
+ // quick-and-dirty recurrence computation: just replace the year
+ $bday->setDate($year, $bday->format('n'), $bday->format('j'));
+ $bday->setTime(12, 0, 0);
+
+ // date range reaches over multiple years: use end year if not in range
+ if (($bday > $end || $bday < $start) && $year2 != $year) {
+ $bday->setDate($year2, $bday->format('n'), $bday->format('j'));
+ $year = $year2;
+ }
+
+ // birthday is within requested range
+ if ($bday <= $end && $bday >= $start) {
+ $age = $year - $birthyear;
+ $event = array(
+ 'id' => rcube_ldap::dn_encode('bday:' . $source . ':' . $contact['ID'] . ':' . $year),
+ 'calendar' => self::BIRTHDAY_CALENDAR_ID,
+ 'title' => $event_title,
+ 'description' => $rcmail->gettext(array('name' => 'birthdayage', 'vars' => array('age' => $age)), 'calendar'),
+ // Add more contact information to description block?
+ 'allday' => true,
+ 'start' => $bday,
+ 'alarms' => $alarms,
+ );
+ $event['end'] = clone $bday;
+ $event['end']->add(new DateInterval('PT1H'));
+
+ $events[] = $event;
+ }
+ }
+ }
+
+ // store collected contacts in cache
+ if (empty($cached)) {
+ $cache->write($source, $cache_records);
+ }
+ }
+
+ return $events;
+ }
+
+ /**
+ * Get a single birthday calendar event
+ */
+ public function get_birthday_event($id)
+ {
+ // decode $id
+ list(,$source,$contact_id,$year) = explode(':', rcube_ldap::dn_decode($id));
+
+ $rcmail = rcmail::get_instance();
+
+ if ($source && $contact_id && ($abook = $rcmail->get_address_book($source))) {
+ $contact = $abook->get_record($contact_id, true);
+
+ if (is_array($contact) && !empty($contact['birthday'])) {
+ try {
+ if (is_array($contact['birthday']))
+ $contact['birthday'] = reset($contact['birthday']);
+
+ $bday = $contact['birthday'] instanceof DateTime ? $contact['birthday'] :
+ new DateTime($contact['birthday'], new DateTimezone('UTC'));
+ $birthyear = $bday->format('Y');
+ }
+ catch (Exception $e) {
+ rcube::raise_error(array(
+ 'code' => 600, 'type' => 'php',
+ 'file' => __FILE__, 'line' => __LINE__,
+ 'message' => 'BIRTHDAY PARSE ERROR: ' . $e),
+ true, false);
+
+ return null;
+ }
+
+ $display_name = rcube_addressbook::compose_display_name($contact);
+ $event_title = $rcmail->gettext(array('name' => 'birthdayeventtitle', 'vars' => array('name' => $display_name)), 'calendar');
+
+ $event = array(
+ 'id' => rcube_ldap::dn_encode('bday:' . $source . ':' . $contact['ID'] . ':' . $year),
+ 'uid' => rcube_ldap::dn_encode('bday:' . $source . ':' . $contact['ID'] . ':' . $birthyear),
+ 'calendar' => self::BIRTHDAY_CALENDAR_ID,
+ 'title' => $event_title,
+ 'description' => '',
+ 'allday' => true,
+ 'start' => $bday,
+ 'recurrence' => array('FREQ' => 'YEARLY', 'INTERVAL' => 1),
+ 'free_busy' => 'free',
+ );
+ $event['end'] = clone $bday;
+ $event['end']->add(new DateInterval('PT1H'));
+
+ return $event;
+ }
+ }
+
+ return null;
+ }
+
+ /**
+ * Store alarm dismissal for birtual birthay events
+ *
+ * @param string Event identifier
+ * @param integer Suspend the alarm for this number of seconds
+ */
+ public function dismiss_birthday_alarm($event_id, $snooze = 0)
+ {
+ $rcmail = rcmail::get_instance();
+ $cache = $rcmail->get_cache('calendar.birthdayalarms', 'db', 86400 * 30);
+ $cache->remove($event_id);
+
+ // compute new notification time or disable if not snoozed
+ $notifyat = $snooze > 0 ? time() + $snooze : null;
+ $cache->set($event_id, array('snooze' => $snooze, 'notifyat' => $notifyat));
+
+ return true;
+ }
+
+ /**
+ * Handler for user_delete plugin hook
+ *
+ * @param array Hash array with hook arguments
+ * @return array Return arguments for plugin hooks
+ */
+ public function user_delete($args)
+ {
+ // TO BE OVERRIDDEN
+ return $args;
+ }
+}
diff --git a/calendar/drivers/database/SQL/mysql.initial.sql b/calendar/drivers/database/SQL/mysql.initial.sql
new file mode 100644
index 0000000..5f6dd60
--- /dev/null
+++ b/calendar/drivers/database/SQL/mysql.initial.sql
@@ -0,0 +1,85 @@
+/**
+ * Roundcube Calendar
+ *
+ * Plugin to add a calendar to Roundcube.
+ *
+ * @author Lazlo Westerhof
+ * @author Thomas Bruederli
+ * @licence GNU AGPL
+ * @copyright (c) 2010 Lazlo Westerhof - Netherlands
+ * @copyright (c) 2014 Kolab Systems AG
+ *
+ **/
+
+CREATE TABLE IF NOT EXISTS `calendars` (
+ `calendar_id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
+ `user_id` int(10) UNSIGNED NOT NULL DEFAULT '0',
+ `name` varchar(255) NOT NULL,
+ `color` varchar(8) NOT NULL,
+ `showalarms` tinyint(1) NOT NULL DEFAULT '1',
+ PRIMARY KEY(`calendar_id`),
+ INDEX `user_name_idx` (`user_id`, `name`),
+ CONSTRAINT `fk_calendars_user_id` FOREIGN KEY (`user_id`)
+ REFERENCES `users`(`user_id`) ON DELETE CASCADE ON UPDATE CASCADE
+) /*!40000 ENGINE=INNODB */ /*!40101 CHARACTER SET utf8 COLLATE utf8_general_ci */;
+
+CREATE TABLE IF NOT EXISTS `events` (
+ `event_id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
+ `calendar_id` int(11) UNSIGNED NOT NULL DEFAULT '0',
+ `recurrence_id` int(11) UNSIGNED NOT NULL DEFAULT '0',
+ `uid` varchar(255) NOT NULL DEFAULT '',
+ `instance` varchar(16) NOT NULL DEFAULT '',
+ `isexception` tinyint(1) NOT NULL DEFAULT '0',
+ `created` datetime NOT NULL DEFAULT '1000-01-01 00:00:00',
+ `changed` datetime NOT NULL DEFAULT '1000-01-01 00:00:00',
+ `sequence` int(1) UNSIGNED NOT NULL DEFAULT '0',
+ `start` datetime NOT NULL DEFAULT '1000-01-01 00:00:00',
+ `end` datetime NOT NULL DEFAULT '1000-01-01 00:00:00',
+ `recurrence` varchar(255) DEFAULT NULL,
+ `title` varchar(255) NOT NULL,
+ `description` text NOT NULL,
+ `location` varchar(255) NOT NULL DEFAULT '',
+ `categories` varchar(255) NOT NULL DEFAULT '',
+ `url` varchar(255) NOT NULL DEFAULT '',
+ `all_day` tinyint(1) NOT NULL DEFAULT '0',
+ `free_busy` tinyint(1) NOT NULL DEFAULT '0',
+ `priority` tinyint(1) NOT NULL DEFAULT '0',
+ `sensitivity` tinyint(1) NOT NULL DEFAULT '0',
+ `status` varchar(32) NOT NULL DEFAULT '',
+ `alarms` text DEFAULT NULL,
+ `attendees` text DEFAULT NULL,
+ `notifyat` datetime DEFAULT NULL,
+ PRIMARY KEY(`event_id`),
+ INDEX `uid_idx` (`uid`),
+ INDEX `recurrence_idx` (`recurrence_id`),
+ INDEX `calendar_notify_idx` (`calendar_id`,`notifyat`),
+ CONSTRAINT `fk_events_calendar_id` FOREIGN KEY (`calendar_id`)
+ REFERENCES `calendars`(`calendar_id`) ON DELETE CASCADE ON UPDATE CASCADE
+) /*!40000 ENGINE=INNODB */ /*!40101 CHARACTER SET utf8 COLLATE utf8_general_ci */;
+
+CREATE TABLE IF NOT EXISTS `attachments` (
+ `attachment_id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
+ `event_id` int(11) UNSIGNED NOT NULL DEFAULT '0',
+ `filename` varchar(255) NOT NULL DEFAULT '',
+ `mimetype` varchar(255) NOT NULL DEFAULT '',
+ `size` int(11) NOT NULL DEFAULT '0',
+ `data` longtext NOT NULL,
+ PRIMARY KEY(`attachment_id`),
+ CONSTRAINT `fk_attachments_event_id` FOREIGN KEY (`event_id`)
+ REFERENCES `events`(`event_id`) ON DELETE CASCADE ON UPDATE CASCADE
+) /*!40000 ENGINE=INNODB */ /*!40101 CHARACTER SET utf8 COLLATE utf8_general_ci */;
+
+CREATE TABLE IF NOT EXISTS `itipinvitations` (
+ `token` VARCHAR(64) NOT NULL,
+ `event_uid` VARCHAR(255) NOT NULL,
+ `user_id` int(10) UNSIGNED NOT NULL DEFAULT '0',
+ `event` TEXT NOT NULL,
+ `expires` DATETIME DEFAULT NULL,
+ `cancelled` TINYINT(3) UNSIGNED NOT NULL DEFAULT '0',
+ PRIMARY KEY(`token`),
+ INDEX `uid_idx` (`user_id`,`event_uid`),
+ CONSTRAINT `fk_itipinvitations_user_id` FOREIGN KEY (`user_id`)
+ REFERENCES `users`(`user_id`) ON DELETE CASCADE ON UPDATE CASCADE
+) /*!40000 ENGINE=INNODB */ /*!40101 CHARACTER SET utf8 COLLATE utf8_general_ci */;
+
+REPLACE INTO system (name, value) VALUES ('calendar-database-version', '2015022700');
diff --git a/calendar/drivers/database/SQL/mysql/2012080600.sql b/calendar/drivers/database/SQL/mysql/2012080600.sql
new file mode 100644
index 0000000..f38e1cf
--- /dev/null
+++ b/calendar/drivers/database/SQL/mysql/2012080600.sql
@@ -0,0 +1,3 @@
+-- MySQL database updates since version 0.7/0.8
+
+ALTER TABLE `events` ADD `sequence` int(1) UNSIGNED NOT NULL DEFAULT '0' AFTER `changed`;
diff --git a/calendar/drivers/database/SQL/mysql/2013011000.sql b/calendar/drivers/database/SQL/mysql/2013011000.sql
new file mode 100644
index 0000000..fe6741a
--- /dev/null
+++ b/calendar/drivers/database/SQL/mysql/2013011000.sql
@@ -0,0 +1 @@
+-- empty \ No newline at end of file
diff --git a/calendar/drivers/database/SQL/mysql/2013042700.sql b/calendar/drivers/database/SQL/mysql/2013042700.sql
new file mode 100644
index 0000000..fe6741a
--- /dev/null
+++ b/calendar/drivers/database/SQL/mysql/2013042700.sql
@@ -0,0 +1 @@
+-- empty \ No newline at end of file
diff --git a/calendar/drivers/database/SQL/mysql/2013051600.sql b/calendar/drivers/database/SQL/mysql/2013051600.sql
new file mode 100644
index 0000000..4de44d6
--- /dev/null
+++ b/calendar/drivers/database/SQL/mysql/2013051600.sql
@@ -0,0 +1,3 @@
+-- MySQL database updates since version 0.9-beta
+
+ALTER TABLE `events` ADD `url` VARCHAR(255) NOT NULL AFTER `categories`; \ No newline at end of file
diff --git a/calendar/drivers/database/SQL/mysql/2014040900.sql b/calendar/drivers/database/SQL/mysql/2014040900.sql
new file mode 100644
index 0000000..814e10d
--- /dev/null
+++ b/calendar/drivers/database/SQL/mysql/2014040900.sql
@@ -0,0 +1,3 @@
+-- MySQL database updates since version 1.0
+
+ALTER TABLE `events` ADD `status` VARCHAR(32) NOT NULL AFTER `sensitivity`;
diff --git a/calendar/drivers/database/SQL/mysql/2015022700.sql b/calendar/drivers/database/SQL/mysql/2015022700.sql
new file mode 100644
index 0000000..06d30fe
--- /dev/null
+++ b/calendar/drivers/database/SQL/mysql/2015022700.sql
@@ -0,0 +1,15 @@
+-- add identifier for recurring instances and exceptions
+
+ALTER TABLE `events` ADD `instance` varchar(16) NOT NULL DEFAULT '' AFTER `uid`;
+ALTER TABLE `events` ADD `isexception` tinyint(1) NOT NULL DEFAULT '0' AFTER `instance`;
+
+UPDATE `events` SET `instance` = DATE_FORMAT(`start`, '%Y%m%d')
+ WHERE `recurrence_id` != 0 AND `instance` = '' AND `all_day` = 1;
+
+UPDATE `events` SET `instance` = DATE_FORMAT(`start`, '%Y%m%dT%k%i%s')
+ WHERE `recurrence_id` != 0 AND `instance` = '' AND `all_day` = 0;
+
+-- extend alarms columns for multiple values
+
+ALTER TABLE `events` CHANGE `alarms` `alarms` TEXT NULL DEFAULT NULL;
+
diff --git a/calendar/drivers/database/SQL/postgres.initial.sql b/calendar/drivers/database/SQL/postgres.initial.sql
new file mode 100644
index 0000000..b170086
--- /dev/null
+++ b/calendar/drivers/database/SQL/postgres.initial.sql
@@ -0,0 +1,109 @@
+/**
+ * RoundCube Calendar
+ *
+ * Plugin to add a calendar to RoundCube.
+ *
+ * @author Lazlo Westerhof
+ * @author Albert Lee
+ * @author Aleksander Machniak <machniak@kolabsys.com>
+ * @licence GNU AGPL
+ * @copyright (c) 2010 Lazlo Westerhof - Netherlands
+ * @copyright (c) 2014 Kolab Systems AG
+ *
+ **/
+
+
+CREATE SEQUENCE calendars_seq
+ INCREMENT BY 1
+ NO MAXVALUE
+ NO MINVALUE
+ CACHE 1;
+
+CREATE TABLE calendars (
+ calendar_id integer DEFAULT nextval('calendars_seq'::regclass) NOT NULL,
+ user_id integer NOT NULL
+ REFERENCES users (user_id) ON UPDATE CASCADE ON DELETE CASCADE,
+ name varchar(255) NOT NULL,
+ color varchar(8) NOT NULL,
+ showalarms smallint NOT NULL DEFAULT 1,
+ PRIMARY KEY (calendar_id)
+);
+
+CREATE INDEX calendars_user_id_idx ON calendars (user_id, name);
+
+
+CREATE SEQUENCE events_seq
+ INCREMENT BY 1
+ NO MAXVALUE
+ NO MINVALUE
+ CACHE 1;
+
+CREATE TABLE events (
+ event_id integer DEFAULT nextval('events_seq'::regclass) NOT NULL,
+ calendar_id integer NOT NULL
+ REFERENCES calendars (calendar_id) ON UPDATE CASCADE ON DELETE CASCADE,
+ recurrence_id integer NOT NULL DEFAULT 0,
+ uid varchar(255) NOT NULL DEFAULT '',
+ instance varchar(16) NOT NULL DEFAULT '',
+ isexception smallint NOT NULL DEFAULT '0',
+ created timestamp without time zone DEFAULT now() NOT NULL,
+ changed timestamp without time zone DEFAULT now(),
+ sequence integer NOT NULL DEFAULT 0,
+ "start" timestamp without time zone DEFAULT now() NOT NULL,
+ "end" timestamp without time zone DEFAULT now() NOT NULL,
+ recurrence varchar(255) DEFAULT NULL,
+ title character varying(255) NOT NULL DEFAULT '',
+ description text NOT NULL DEFAULT '',
+ location character varying(255) NOT NULL DEFAULT '',
+ categories character varying(255) NOT NULL DEFAULT '',
+ url character varying(255) NOT NULL DEFAULT '',
+ all_day smallint NOT NULL DEFAULT 0,
+ free_busy smallint NOT NULL DEFAULT 0,
+ priority smallint NOT NULL DEFAULT 0,
+ sensitivity smallint NOT NULL DEFAULT 0,
+ status character varying(32) NOT NULL DEFAULT '',
+ alarms text DEFAULT NULL,
+ attendees text DEFAULT NULL,
+ notifyat timestamp without time zone DEFAULT NULL,
+ PRIMARY KEY (event_id)
+);
+
+CREATE INDEX events_calendar_id_notifyat_idx ON events (calendar_id, notifyat);
+CREATE INDEX events_uid_idx ON events (uid);
+CREATE INDEX events_recurrence_id_idx ON events (recurrence_id);
+
+
+CREATE SEQUENCE attachments_seq
+ INCREMENT BY 1
+ NO MAXVALUE
+ NO MINVALUE
+ CACHE 1;
+
+CREATE TABLE attachments (
+ attachment_id integer DEFAULT nextval('attachments_seq'::regclass) NOT NULL,
+ event_id integer NOT NULL
+ REFERENCES events (event_id) ON DELETE CASCADE ON UPDATE CASCADE,
+ filename varchar(255) NOT NULL DEFAULT '',
+ mimetype varchar(255) NOT NULL DEFAULT '',
+ size integer NOT NULL DEFAULT 0,
+ data text NOT NULL DEFAULT '',
+ PRIMARY KEY (attachment_id)
+);
+
+CREATE INDEX attachments_user_id_idx ON attachments (event_id);
+
+
+CREATE TABLE itipinvitations (
+ token varchar(64) NOT NULL,
+ event_uid varchar(255) NOT NULL,
+ user_id integer NOT NULL
+ REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE CASCADE,
+ event TEXT NOT NULL,
+ expires timestamp without time zone DEFAULT NULL,
+ cancelled smallint NOT NULL DEFAULT 0,
+ PRIMARY KEY (token)
+);
+
+CREATE INDEX itipinvitations_user_id_event_uid_idx ON itipinvitations (user_id, event_uid);
+
+INSERT INTO system (name, value) VALUES ('calendar-database-version', '2015022700');
diff --git a/calendar/drivers/database/SQL/postgres/2012080600.sql b/calendar/drivers/database/SQL/postgres/2012080600.sql
new file mode 100644
index 0000000..9a273e6
--- /dev/null
+++ b/calendar/drivers/database/SQL/postgres/2012080600.sql
@@ -0,0 +1,3 @@
+-- Postgres database updates since version 0.7/0.8
+
+ALTER TABLE events ADD sequence integer NOT NULL DEFAULT 0;
diff --git a/calendar/drivers/database/SQL/postgres/2013011000.sql b/calendar/drivers/database/SQL/postgres/2013011000.sql
new file mode 100644
index 0000000..fe6741a
--- /dev/null
+++ b/calendar/drivers/database/SQL/postgres/2013011000.sql
@@ -0,0 +1 @@
+-- empty \ No newline at end of file
diff --git a/calendar/drivers/database/SQL/postgres/2013042700.sql b/calendar/drivers/database/SQL/postgres/2013042700.sql
new file mode 100644
index 0000000..d644c39
--- /dev/null
+++ b/calendar/drivers/database/SQL/postgres/2013042700.sql
@@ -0,0 +1,8 @@
+ALTER SEQUENCE calendar_ids RENAME TO calendars_seq;
+ALTER TABLE calendars ALTER COLUMN calendar_id SET DEFAULT nextval('calendars_seq'::text);
+
+ALTER SEQUENCE event_ids RENAME TO events_seq;
+ALTER TABLE events ALTER COLUMN event_id SET DEFAULT nextval('events_seq'::text);
+
+ALTER SEQUENCE attachment_ids RENAME TO attachments_seq;
+ALTER TABLE attachments ALTER COLUMN attachment_id SET DEFAULT nextval('attachments_seq'::text);
diff --git a/calendar/drivers/database/SQL/postgres/2013051600.sql b/calendar/drivers/database/SQL/postgres/2013051600.sql
new file mode 100644
index 0000000..3c1da43
--- /dev/null
+++ b/calendar/drivers/database/SQL/postgres/2013051600.sql
@@ -0,0 +1,3 @@
+-- Postgres database updates since version 0.9-beta
+
+ALTER TABLE events ADD url character varying(255) NOT NULL;
diff --git a/calendar/drivers/database/SQL/postgres/2014040900.sql b/calendar/drivers/database/SQL/postgres/2014040900.sql
new file mode 100644
index 0000000..310744c
--- /dev/null
+++ b/calendar/drivers/database/SQL/postgres/2014040900.sql
@@ -0,0 +1,3 @@
+-- Postgres database updates since version 1.0
+
+ALTER TABLE events ADD status character varying(32) NOT NULL;
diff --git a/calendar/drivers/database/SQL/postgres/2015022700.sql b/calendar/drivers/database/SQL/postgres/2015022700.sql
new file mode 100644
index 0000000..0de989e
--- /dev/null
+++ b/calendar/drivers/database/SQL/postgres/2015022700.sql
@@ -0,0 +1,9 @@
+-- add identifier for recurring instances and exceptions
+
+ALTER TABLE events ADD instance character varying(16) NOT NULL;
+ALTER TABLE events ADD isexception smallint NOT NULL DEFAULT '0';
+
+-- extend alarms columns for multiple values
+
+ALTER TABLE events ALTER COLUMN alarms TYPE text;
+
diff --git a/calendar/drivers/database/SQL/sqlite.initial.sql b/calendar/drivers/database/SQL/sqlite.initial.sql
new file mode 100644
index 0000000..c8aa971
--- /dev/null
+++ b/calendar/drivers/database/SQL/sqlite.initial.sql
@@ -0,0 +1,79 @@
+/**
+ * Roundcube Calendar
+ *
+ * Plugin to add a calendar to Roundcube.
+ *
+ * @author Lazlo Westerhof
+ * @author Thomas Bruederli
+ * @author Albert Lee
+ * @licence GNU AGPL
+ * @copyright (c) 2010 Lazlo Westerhof - Netherlands
+ * @copyright (c) 2014 Kolab Systems AG
+ *
+ **/
+
+CREATE TABLE calendars (
+ calendar_id integer NOT NULL PRIMARY KEY,
+ user_id integer NOT NULL default '0',
+ name varchar(255) NOT NULL default '',
+ color varchar(255) NOT NULL default '',
+ showalarms tinyint(1) NOT NULL default '1',
+ CONSTRAINT fk_calendars_user_id FOREIGN KEY (user_id)
+ REFERENCES users(user_id)
+);
+
+CREATE TABLE events (
+ event_id integer NOT NULL PRIMARY KEY,
+ calendar_id integer NOT NULL default '0',
+ recurrence_id integer NOT NULL default '0',
+ uid varchar(255) NOT NULL default '',
+ instance varchar(16) NOT NULL default '',
+ isexception tinyint(1) NOT NULL default '0',
+ created datetime NOT NULL default '1000-01-01 00:00:00',
+ changed datetime NOT NULL default '1000-01-01 00:00:00',
+ sequence integer NOT NULL default '0',
+ start datetime NOT NULL default '1000-01-01 00:00:00',
+ end datetime NOT NULL default '1000-01-01 00:00:00',
+ recurrence varchar(255) default NULL,
+ title varchar(255) NOT NULL,
+ description text NOT NULL,
+ location varchar(255) NOT NULL default '',
+ categories varchar(255) NOT NULL default '',
+ url varchar(255) NOT NULL default '',
+ all_day tinyint(1) NOT NULL default '0',
+ free_busy tinyint(1) NOT NULL default '0',
+ priority tinyint(1) NOT NULL default '0',
+ sensitivity tinyint(1) NOT NULL default '0',
+ status varchar(32) NOT NULL default '',
+ alarms text default NULL,
+ attendees text default NULL,
+ notifyat datetime default NULL,
+ CONSTRAINT fk_events_calendar_id FOREIGN KEY (calendar_id)
+ REFERENCES calendars(calendar_id)
+);
+
+CREATE TABLE attachments (
+ attachment_id integer NOT NULL PRIMARY KEY,
+ event_id integer NOT NULL default '0',
+ filename varchar(255) NOT NULL default '',
+ mimetype varchar(255) NOT NULL default '',
+ size integer NOT NULL default '0',
+ data text NOT NULL default '',
+ CONSTRAINT fk_attachment_event_id FOREIGN KEY (event_id)
+ REFERENCES events(event_id)
+);
+
+CREATE TABLE itipinvitations (
+ token varchar(64) NOT NULL PRIMARY KEY,
+ event_uid varchar(255) NOT NULL,
+ user_id integer NOT NULL default '0',
+ event text NOT NULL,
+ expires datetime NOT NULL default '1000-01-01 00:00:00',
+ cancelled tinyint(1) NOT NULL default '0',
+ CONSTRAINT fk_itipinvitations_user_id FOREIGN KEY (user_id)
+ REFERENCES users(user_id)
+);
+
+CREATE INDEX ix_itipinvitations_uid ON itipinvitations(user_id, event_uid);
+
+INSERT INTO system (name, value) VALUES ('calendar-database-version', '2015022700');
diff --git a/calendar/drivers/database/SQL/sqlite/2013011000.sql b/calendar/drivers/database/SQL/sqlite/2013011000.sql
new file mode 100644
index 0000000..fe6741a
--- /dev/null
+++ b/calendar/drivers/database/SQL/sqlite/2013011000.sql
@@ -0,0 +1 @@
+-- empty \ No newline at end of file
diff --git a/calendar/drivers/database/SQL/sqlite/2013042700.sql b/calendar/drivers/database/SQL/sqlite/2013042700.sql
new file mode 100644
index 0000000..fe6741a
--- /dev/null
+++ b/calendar/drivers/database/SQL/sqlite/2013042700.sql
@@ -0,0 +1 @@
+-- empty \ No newline at end of file
diff --git a/calendar/drivers/database/SQL/sqlite/2013051600.sql b/calendar/drivers/database/SQL/sqlite/2013051600.sql
new file mode 100644
index 0000000..850fae3
--- /dev/null
+++ b/calendar/drivers/database/SQL/sqlite/2013051600.sql
@@ -0,0 +1,63 @@
+-- SQLite database updates since version 0.9-beta
+
+-- ALTER TABLE events ADD url varchar(255) NOT NULL AFTER categories;
+
+CREATE TABLE temp_events (
+ event_id integer NOT NULL PRIMARY KEY,
+ calendar_id integer NOT NULL default '0',
+ recurrence_id integer NOT NULL default '0',
+ uid varchar(255) NOT NULL default '',
+ created datetime NOT NULL default '1000-01-01 00:00:00',
+ changed datetime NOT NULL default '1000-01-01 00:00:00',
+ sequence integer NOT NULL default '0',
+ start datetime NOT NULL default '1000-01-01 00:00:00',
+ end datetime NOT NULL default '1000-01-01 00:00:00',
+ recurrence varchar(255) default NULL,
+ title varchar(255) NOT NULL,
+ description text NOT NULL,
+ location varchar(255) NOT NULL default '',
+ categories varchar(255) NOT NULL default '',
+ all_day tinyint(1) NOT NULL default '0',
+ free_busy tinyint(1) NOT NULL default '0',
+ priority tinyint(1) NOT NULL default '0',
+ sensitivity tinyint(1) NOT NULL default '0',
+ alarms varchar(255) default NULL,
+ attendees text default NULL,
+ notifyat datetime default NULL
+);
+
+INSERT INTO temp_events (event_id, calendar_id, recurrence_id, uid, created, changed, sequence, start, end, recurrence, title, description, location, categories, all_day, free_busy, priority, sensitivity, alarms, attendees, notifyat)
+ SELECT event_id, calendar_id, recurrence_id, uid, created, changed, sequence, start, end, recurrence, title, description, location, categories, all_day, free_busy, priority, sensitivity, alarms, attendees, notifyat FROM events;
+
+DROP TABLE events;
+
+CREATE TABLE events (
+ event_id integer NOT NULL PRIMARY KEY,
+ calendar_id integer NOT NULL default '0',
+ recurrence_id integer NOT NULL default '0',
+ uid varchar(255) NOT NULL default '',
+ created datetime NOT NULL default '1000-01-01 00:00:00',
+ changed datetime NOT NULL default '1000-01-01 00:00:00',
+ sequence integer NOT NULL default '0',
+ start datetime NOT NULL default '1000-01-01 00:00:00',
+ end datetime NOT NULL default '1000-01-01 00:00:00',
+ recurrence varchar(255) default NULL,
+ title varchar(255) NOT NULL,
+ description text NOT NULL,
+ location varchar(255) NOT NULL default '',
+ categories varchar(255) NOT NULL default '',
+ url varchar(255) NOT NULL default '',
+ all_day tinyint(1) NOT NULL default '0',
+ free_busy tinyint(1) NOT NULL default '0',
+ priority tinyint(1) NOT NULL default '0',
+ sensitivity tinyint(1) NOT NULL default '0',
+ alarms varchar(255) default NULL,
+ attendees text default NULL,
+ notifyat datetime default NULL,
+ CONSTRAINT fk_events_calendar_id FOREIGN KEY (calendar_id)
+ REFERENCES calendars(calendar_id)
+);
+
+INSERT INTO events (event_id, calendar_id, recurrence_id, uid, created, changed, sequence, start, end, recurrence, title, description, location, categories, all_day, free_busy, priority, sensitivity, alarms, attendees, notifyat)
+ SELECT event_id, calendar_id, recurrence_id, uid, created, changed, sequence, start, end, recurrence, title, description, location, categories, all_day, free_busy, priority, sensitivity, alarms, attendees, notifyat FROM temp_events;
+
diff --git a/calendar/drivers/database/SQL/sqlite/2014040900.sql b/calendar/drivers/database/SQL/sqlite/2014040900.sql
new file mode 100644
index 0000000..ff8ed17
--- /dev/null
+++ b/calendar/drivers/database/SQL/sqlite/2014040900.sql
@@ -0,0 +1,67 @@
+-- SQLite database updates since version 0.9-beta
+
+-- ALTER TABLE events ADD url varchar(255) NOT NULL AFTER categories;
+
+CREATE TABLE temp_events (
+ event_id integer NOT NULL PRIMARY KEY,
+ calendar_id integer NOT NULL default '0',
+ recurrence_id integer NOT NULL default '0',
+ uid varchar(255) NOT NULL default '',
+ created datetime NOT NULL default '1000-01-01 00:00:00',
+ changed datetime NOT NULL default '1000-01-01 00:00:00',
+ sequence integer NOT NULL default '0',
+ start datetime NOT NULL default '1000-01-01 00:00:00',
+ end datetime NOT NULL default '1000-01-01 00:00:00',
+ recurrence varchar(255) default NULL,
+ title varchar(255) NOT NULL,
+ description text NOT NULL,
+ location varchar(255) NOT NULL default '',
+ categories varchar(255) NOT NULL default '',
+ url varchar(255) NOT NULL default '',
+ all_day tinyint(1) NOT NULL default '0',
+ free_busy tinyint(1) NOT NULL default '0',
+ priority tinyint(1) NOT NULL default '0',
+ sensitivity tinyint(1) NOT NULL default '0',
+ alarms varchar(255) default NULL,
+ attendees text default NULL,
+ notifyat datetime default NULL
+);
+
+INSERT INTO temp_events (event_id, calendar_id, recurrence_id, uid, created, changed, sequence, start, end, recurrence, title, description, location, categories, url, all_day, free_busy, priority, sensitivity, alarms, attendees, notifyat)
+ SELECT event_id, calendar_id, recurrence_id, uid, created, changed, sequence, start, end, recurrence, title, description, location, categories, url, all_day, free_busy, priority, sensitivity, alarms, attendees, notifyat
+ FROM events;
+
+DROP TABLE events;
+
+CREATE TABLE events (
+ event_id integer NOT NULL PRIMARY KEY,
+ calendar_id integer NOT NULL default '0',
+ recurrence_id integer NOT NULL default '0',
+ uid varchar(255) NOT NULL default '',
+ created datetime NOT NULL default '1000-01-01 00:00:00',
+ changed datetime NOT NULL default '1000-01-01 00:00:00',
+ sequence integer NOT NULL default '0',
+ start datetime NOT NULL default '1000-01-01 00:00:00',
+ end datetime NOT NULL default '1000-01-01 00:00:00',
+ recurrence varchar(255) default NULL,
+ title varchar(255) NOT NULL,
+ description text NOT NULL,
+ location varchar(255) NOT NULL default '',
+ categories varchar(255) NOT NULL default '',
+ url varchar(255) NOT NULL default '',
+ all_day tinyint(1) NOT NULL default '0',
+ free_busy tinyint(1) NOT NULL default '0',
+ priority tinyint(1) NOT NULL default '0',
+ sensitivity tinyint(1) NOT NULL default '0',
+ status varchar(32) NOT NULL default '',
+ alarms varchar(255) default NULL,
+ attendees text default NULL,
+ notifyat datetime default NULL,
+ CONSTRAINT fk_events_calendar_id FOREIGN KEY (calendar_id)
+ REFERENCES calendars(calendar_id)
+);
+
+INSERT INTO events (event_id, calendar_id, recurrence_id, uid, created, changed, sequence, start, end, recurrence, title, description, location, categories, url, all_day, free_busy, priority, sensitivity, alarms, attendees, notifyat)
+ SELECT event_id, calendar_id, recurrence_id, uid, created, changed, sequence, start, end, recurrence, title, description, location, categories, url, all_day, free_busy, priority, sensitivity, alarms, attendees, notifyat
+ FROM temp_events;
+
diff --git a/calendar/drivers/database/SQL/sqlite/2015022700.sql b/calendar/drivers/database/SQL/sqlite/2015022700.sql
new file mode 100644
index 0000000..9770701
--- /dev/null
+++ b/calendar/drivers/database/SQL/sqlite/2015022700.sql
@@ -0,0 +1,79 @@
+-- ALTER TABLE `events` ADD `instance` varchar(16) NOT NULL DEFAULT '' AFTER `uid`;
+-- ALTER TABLE `events` ADD `isexception` tinyint(3) NOT NULL DEFAULT '0' AFTER `instance`;
+-- ALTER TABLE `events` CHANGE `alarms` `alarms` TEXT NULL DEFAULT NULL;
+
+CREATE TABLE temp_events (
+ event_id integer NOT NULL PRIMARY KEY,
+ calendar_id integer NOT NULL default '0',
+ recurrence_id integer NOT NULL default '0',
+ uid varchar(255) NOT NULL default '',
+ created datetime NOT NULL default '1000-01-01 00:00:00',
+ changed datetime NOT NULL default '1000-01-01 00:00:00',
+ sequence integer NOT NULL default '0',
+ start datetime NOT NULL default '1000-01-01 00:00:00',
+ end datetime NOT NULL default '1000-01-01 00:00:00',
+ recurrence varchar(255) default NULL,
+ title varchar(255) NOT NULL,
+ description text NOT NULL,
+ location varchar(255) NOT NULL default '',
+ categories varchar(255) NOT NULL default '',
+ url varchar(255) NOT NULL default '',
+ all_day tinyint(1) NOT NULL default '0',
+ free_busy tinyint(1) NOT NULL default '0',
+ priority tinyint(1) NOT NULL default '0',
+ sensitivity tinyint(1) NOT NULL default '0',
+ status varchar(32) NOT NULL default '',
+ alarms varchar(255) default NULL,
+ attendees text default NULL,
+ notifyat datetime default NULL
+);
+
+INSERT INTO temp_events (event_id, calendar_id, recurrence_id, uid, created, changed, sequence, start, end, recurrence, title, description, location, categories, url, all_day, free_busy, priority, sensitivity, alarms, attendees, notifyat)
+ SELECT event_id, calendar_id, recurrence_id, uid, created, changed, sequence, start, end, recurrence, title, description, location, categories, url, all_day, free_busy, priority, sensitivity, alarms, attendees, notifyat
+ FROM events;
+
+DROP TABLE events;
+
+CREATE TABLE events (
+ event_id integer NOT NULL PRIMARY KEY,
+ calendar_id integer NOT NULL default '0',
+ recurrence_id integer NOT NULL default '0',
+ uid varchar(255) NOT NULL default '',
+ instance varchar(16) NOT NULL default '',
+ isexception tinyint(1) NOT NULL default '0',
+ created datetime NOT NULL default '1000-01-01 00:00:00',
+ changed datetime NOT NULL default '1000-01-01 00:00:00',
+ sequence integer NOT NULL default '0',
+ start datetime NOT NULL default '1000-01-01 00:00:00',
+ end datetime NOT NULL default '1000-01-01 00:00:00',
+ recurrence varchar(255) default NULL,
+ title varchar(255) NOT NULL,
+ description text NOT NULL,
+ location varchar(255) NOT NULL default '',
+ categories varchar(255) NOT NULL default '',
+ url varchar(255) NOT NULL default '',
+ all_day tinyint(1) NOT NULL default '0',
+ free_busy tinyint(1) NOT NULL default '0',
+ priority tinyint(1) NOT NULL default '0',
+ sensitivity tinyint(1) NOT NULL default '0',
+ status varchar(32) NOT NULL default '',
+ alarms text default NULL,
+ attendees text default NULL,
+ notifyat datetime default NULL,
+ CONSTRAINT fk_events_calendar_id FOREIGN KEY (calendar_id)
+ REFERENCES calendars(calendar_id)
+);
+
+INSERT INTO events (event_id, calendar_id, recurrence_id, uid, created, changed, sequence, start, end, recurrence, title, description, location, categories, url, all_day, free_busy, priority, sensitivity, alarms, attendees, notifyat)
+ SELECT event_id, calendar_id, recurrence_id, uid, created, changed, sequence, start, end, recurrence, title, description, location, categories, url, all_day, free_busy, priority, sensitivity, alarms, attendees, notifyat
+ FROM temp_events;
+
+DROP TABLE temp_events;
+
+-- Derrive instance columns from start date/time
+
+UPDATE events SET instance = strftime('%Y%m%d', start)
+ WHERE recurrence_id != 0 AND instance = '' AND all_day = 1;
+
+UPDATE events SET instance = strftime('%Y%m%dT%H%M%S', start)
+ WHERE recurrence_id != 0 AND instance = '' AND all_day = 0;
diff --git a/calendar/drivers/database/database_driver.php b/calendar/drivers/database/database_driver.php
new file mode 100644
index 0000000..51c5afc
--- /dev/null
+++ b/calendar/drivers/database/database_driver.php
@@ -0,0 +1,1496 @@
+<?php
+
+/**
+ * Database driver for the Calendar plugin
+ *
+ * @author Lazlo Westerhof <hello@lazlo.me>
+ * @author Thomas Bruederli <bruederli@kolabsys.com>
+ *
+ * Copyright (C) 2010, Lazlo Westerhof <hello@lazlo.me>
+ * Copyright (C) 2012-2015, Kolab Systems AG <contact@kolabsys.com>
+ *
+ * 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.
+ *
+ * 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
+ * GNU Affero General Public License for more details.
+ *
+ * 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/>.
+ */
+
+
+class database_driver extends calendar_driver
+{
+ const DB_DATE_FORMAT = 'Y-m-d H:i:s';
+
+ public static $scheduling_properties = array('start', 'end', 'allday', 'recurrence', 'location', 'cancelled');
+
+ // features this backend supports
+ public $alarms = true;
+ public $attendees = true;
+ public $freebusy = false;
+ public $attachments = true;
+ public $alarm_types = array('DISPLAY');
+
+ private $rc;
+ private $cal;
+ private $cache = array();
+ private $calendars = array();
+ private $calendar_ids = '';
+ private $free_busy_map = array('free' => 0, 'busy' => 1, 'out-of-office' => 2, 'outofoffice' => 2, 'tentative' => 3);
+ private $sensitivity_map = array('public' => 0, 'private' => 1, 'confidential' => 2);
+ private $server_timezone;
+
+ private $db_events = 'events';
+ private $db_calendars = 'calendars';
+ private $db_attachments = 'attachments';
+
+
+ /**
+ * Default constructor
+ */
+ public function __construct($cal)
+ {
+ $this->cal = $cal;
+ $this->rc = $cal->rc;
+ $this->server_timezone = new DateTimeZone(date_default_timezone_get());
+
+ // read database config
+ $db = $this->rc->get_dbh();
+ $this->db_events = $this->rc->config->get('db_table_events', $db->table_name($this->db_events));
+ $this->db_calendars = $this->rc->config->get('db_table_calendars', $db->table_name($this->db_calendars));
+ $this->db_attachments = $this->rc->config->get('db_table_attachments', $db->table_name($this->db_attachments));
+
+ $this->_read_calendars();
+ }
+
+ /**
+ * Read available calendars for the current user and store them internally
+ */
+ protected function _read_calendars()
+ {
+ $hidden = array_filter(explode(',', $this->rc->config->get('hidden_calendars', '')));
+
+ if (!empty($this->rc->user->ID)) {
+ $calendar_ids = array();
+ $result = $this->rc->db->query(
+ "SELECT *, calendar_id AS id FROM " . $this->db_calendars . "
+ WHERE user_id=?
+ ORDER BY name",
+ $this->rc->user->ID
+ );
+ while ($result && ($arr = $this->rc->db->fetch_assoc($result))) {
+ $arr['showalarms'] = intval($arr['showalarms']);
+ $arr['active'] = !in_array($arr['id'], $hidden);
+ $arr['name'] = html::quote($arr['name']);
+ $arr['listname'] = html::quote($arr['name']);
+ $arr['rights'] = 'lrswikxteav';
+ $arr['editable'] = true;
+ $this->calendars[$arr['calendar_id']] = $arr;
+ $calendar_ids[] = $this->rc->db->quote($arr['calendar_id']);
+ }
+ $this->calendar_ids = join(',', $calendar_ids);
+ }
+ }
+
+ /**
+ * Get a list of available calendars from this source
+ *
+ * @param integer Bitmask defining filter criterias
+ *
+ * @return array List of calendars
+ */
+ public function list_calendars($filter = 0)
+ {
+ // attempt to create a default calendar for this user
+ if (empty($this->calendars) && get_class($this) == "database_driver") {
+ if ($this->create_calendar(array('name' => 'Default', 'color' => 'cc0000', 'showalarms' => true)))
+ $this->_read_calendars();
+ }
+
+ $calendars = $this->calendars;
+
+ // filter active calendars
+ if ($filter & self::FILTER_ACTIVE) {
+ foreach ($calendars as $idx => $cal) {
+ if (!$cal['active']) {
+ unset($calendars[$idx]);
+ }
+ }
+ }
+
+ // 'personal' is unsupported in this driver
+
+ // append the virtual birthdays calendar
+ if ($this->rc->config->get('calendar_contact_birthdays', false)) {
+ $prefs = $this->rc->config->get('birthday_calendar', array('color' => '87CEFA'));
+ $hidden = array_filter(explode(',', $this->rc->config->get('hidden_calendars', '')));
+
+ $id = self::BIRTHDAY_CALENDAR_ID;
+ if (!$active || !in_array($id, $hidden)) {
+ $calendars[$id] = array(
+ 'id' => $id,
+ 'name' => $this->cal->gettext('birthdays'),
+ 'listname' => $this->cal->gettext('birthdays'),
+ 'color' => $prefs['color'],
+ 'showalarms' => (bool)$this->rc->config->get('calendar_birthdays_alarm_type'),
+ 'active' => !in_array($id, $hidden),
+ 'group' => 'x-birthdays',
+ 'editable' => false,
+ 'default' => false,
+ 'children' => false,
+ );
+ }
+ }
+
+ return $calendars;
+ }
+
+ /**
+ * Create a new calendar assigned to the current user
+ *
+ * @param array Hash array with calendar properties
+ * name: Calendar name
+ * color: The color of the calendar
+ * @return mixed ID of the calendar on success, False on error
+ */
+ public function create_calendar($prop)
+ {
+ $result = $this->rc->db->query(
+ "INSERT INTO " . $this->db_calendars . "
+ (user_id, name, color, showalarms)
+ VALUES (?, ?, ?, ?)",
+ $this->rc->user->ID,
+ $prop['name'],
+ $prop['color'],
+ $prop['showalarms']?1:0
+ );
+
+ if ($result)
+ return $this->rc->db->insert_id($this->db_calendars);
+
+ return false;
+ }
+
+ /**
+ * Update properties of an existing calendar
+ *
+ * @see calendar_driver::edit_calendar()
+ */
+ public function edit_calendar($prop)
+ {
+ // birthday calendar properties are saved in user prefs
+ if ($prop['id'] == self::BIRTHDAY_CALENDAR_ID) {
+ $prefs['birthday_calendar'] = $this->rc->config->get('birthday_calendar', array('color' => '87CEFA'));
+ if (isset($prop['color']))
+ $prefs['birthday_calendar']['color'] = $prop['color'];
+ if (isset($prop['showalarms']))
+ $prefs['calendar_birthdays_alarm_type'] = $prop['showalarms'] ? $this->alarm_types[0] : '';
+ $this->rc->user->save_prefs($prefs);
+ return true;
+ }
+
+ $query = $this->rc->db->query(
+ "UPDATE " . $this->db_calendars . "
+ SET name=?, color=?, showalarms=?
+ WHERE calendar_id=?
+ AND user_id=?",
+ $prop['name'],
+ $prop['color'],
+ $prop['showalarms']?1:0,
+ $prop['id'],
+ $this->rc->user->ID
+ );
+
+ return $this->rc->db->affected_rows($query);
+ }
+
+ /**
+ * Set active/subscribed state of a calendar
+ * Save a list of hidden calendars in user prefs
+ *
+ * @see calendar_driver::subscribe_calendar()
+ */
+ public function subscribe_calendar($prop)
+ {
+ $hidden = array_flip(explode(',', $this->rc->config->get('hidden_calendars', '')));
+
+ if ($prop['active'])
+ unset($hidden[$prop['id']]);
+ else
+ $hidden[$prop['id']] = 1;
+
+ return $this->rc->user->save_prefs(array('hidden_calendars' => join(',', array_keys($hidden))));
+ }
+
+ /**
+ * Delete the given calendar with all its contents
+ *
+ * @see calendar_driver::delete_calendar()
+ */
+ public function delete_calendar($prop)
+ {
+ if (!$this->calendars[$prop['id']])
+ return false;
+
+ // events and attachments will be deleted by foreign key cascade
+
+ $query = $this->rc->db->query(
+ "DELETE FROM " . $this->db_calendars . "
+ WHERE calendar_id=?",
+ $prop['id']
+ );
+
+ return $this->rc->db->affected_rows($query);
+ }
+
+ /**
+ * Search for shared or otherwise not listed calendars the user has access
+ *
+ * @param string Search string
+ * @param string Section/source to search
+ * @return array List of calendars
+ */
+ public function search_calendars($query, $source)
+ {
+ // not implemented
+ return array();
+ }
+
+ /**
+ * Add a single event to the database
+ *
+ * @param array Hash array with event properties
+ * @see calendar_driver::new_event()
+ */
+ public function new_event($event)
+ {
+ if (!$this->validate($event))
+ return false;
+
+ if (!empty($this->calendars)) {
+ if ($event['calendar'] && !$this->calendars[$event['calendar']])
+ return false;
+ if (!$event['calendar'])
+ $event['calendar'] = reset(array_keys($this->calendars));
+
+ if ($event_id = $this->_insert_event($event)) {
+ $this->_update_recurring($event);
+ }
+
+ return $event_id;
+ }
+
+ return false;
+ }
+
+ /**
+ *
+ */
+ private function _insert_event(&$event)
+ {
+ $event = $this->_save_preprocess($event);
+
+ $this->rc->db->query(sprintf(
+ "INSERT INTO " . $this->db_events . "
+ (calendar_id, created, changed, uid, recurrence_id, instance, isexception, %s, %s, all_day, recurrence,
+ title, description, location, categories, url, free_busy, priority, sensitivity, status, attendees, alarms, notifyat)
+ VALUES (?, %s, %s, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
+ $this->rc->db->quote_identifier('start'),
+ $this->rc->db->quote_identifier('end'),
+ $this->rc->db->now(),
+ $this->rc->db->now()
+ ),
+ $event['calendar'],
+ strval($event['uid']),
+ intval($event['recurrence_id']),
+ strval($event['_instance']),
+ intval($event['isexception']),
+ $event['start']->format(self::DB_DATE_FORMAT),
+ $event['end']->format(self::DB_DATE_FORMAT),
+ intval($event['all_day']),
+ $event['_recurrence'],
+ strval($event['title']),
+ strval($event['description']),
+ strval($event['location']),
+ join(',', (array)$event['categories']),
+ strval($event['url']),
+ intval($event['free_busy']),
+ intval($event['priority']),
+ intval($event['sensitivity']),
+ strval($event['status']),
+ $event['attendees'],
+ $event['alarms'],
+ $event['notifyat']
+ );
+
+ $event_id = $this->rc->db->insert_id($this->db_events);
+
+ if ($event_id) {
+ $event['id'] = $event_id;
+
+ // add attachments
+ if (!empty($event['attachments'])) {
+ foreach ($event['attachments'] as $attachment) {
+ $this->add_attachment($attachment, $event_id);
+ unset($attachment);
+ }
+ }
+
+ return $event_id;
+ }
+
+ return false;
+ }
+
+ /**
+ * Update an event entry with the given data
+ *
+ * @param array Hash array with event properties
+ * @see calendar_driver::edit_event()
+ */
+ public function edit_event($event)
+ {
+ if (!empty($this->calendars)) {
+ $update_master = false;
+ $update_recurring = true;
+ $old = $this->get_event($event);
+ $ret = true;
+
+ // check if update affects scheduling and update attendee status accordingly
+ $reschedule = $this->_check_scheduling($event, $old, true);
+
+ // increment sequence number
+ if (empty($event['sequence']) && $reschedule)
+ $event['sequence'] = max($event['sequence'], $old['sequence']) + 1;
+
+ // modify a recurring event, check submitted savemode to do the right things
+ if ($old['recurrence'] || $old['recurrence_id']) {
+ $master = $old['recurrence_id'] ? $this->get_event(array('id' => $old['recurrence_id'])) : $old;
+
+ // keep saved exceptions (not submitted by the client)
+ if ($old['recurrence']['EXDATE'])
+ $event['recurrence']['EXDATE'] = $old['recurrence']['EXDATE'];
+
+ switch ($event['_savemode']) {
+ case 'new':
+ $event['uid'] = $this->cal->generate_uid();
+ return $this->new_event($event);
+
+ case 'current':
+ // save as exception
+ $event['isexception'] = 1;
+ $update_recurring = false;
+
+ // set exception to first instance (= master)
+ if ($event['id'] == $master['id']) {
+ $event += $old;
+ $event['recurrence_id'] = $master['id'];
+ $event['_instance'] = libcalendaring::recurrence_instance_identifier($old);
+ $event['isexception'] = 1;
+ $event_id = $this->_insert_event($event);
+ return $event_id;
+ }
+ break;
+
+ case 'future':
+ if ($master['id'] != $event['id']) {
+ // set until-date on master event, then save this instance as new recurring event
+ $master['recurrence']['UNTIL'] = clone $event['start'];
+ $master['recurrence']['UNTIL']->sub(new DateInterval('P1D'));
+ unset($master['recurrence']['COUNT']);
+ $update_master = true;
+
+ // if recurrence COUNT, update value to the correct number of future occurences
+ if ($event['recurrence']['COUNT']) {
+ $fromdate = clone $event['start'];
+ $fromdate->setTimezone($this->server_timezone);
+ $sqlresult = $this->rc->db->query(sprintf(
+ "SELECT event_id FROM " . $this->db_events . "
+ WHERE calendar_id IN (%s)
+ AND %s >= ?
+ AND recurrence_id=?",
+ $this->calendar_ids,
+ $this->rc->db->quote_identifier('start')
+ ),
+ $fromdate->format(self::DB_DATE_FORMAT),
+ $master['id']);
+ if ($count = $this->rc->db->num_rows($sqlresult))
+ $event['recurrence']['COUNT'] = $count;
+ }
+
+ $update_recurring = true;
+ $event['recurrence_id'] = 0;
+ $event['isexception'] = 0;
+ $event['_instance'] = '';
+ break;
+ }
+ // else: 'future' == 'all' if modifying the master event
+
+ default: // 'all' is default
+ $event['id'] = $master['id'];
+ $event['recurrence_id'] = 0;
+
+ // use start date from master but try to be smart on time or duration changes
+ $old_start_date = $old['start']->format('Y-m-d');
+ $old_start_time = $old['allday'] ? '' : $old['start']->format('H:i');
+ $old_duration = $old['end']->format('U') - $old['start']->format('U');
+
+ $new_start_date = $event['start']->format('Y-m-d');
+ $new_start_time = $event['allday'] ? '' : $event['start']->format('H:i');
+ $new_duration = $event['end']->format('U') - $event['start']->format('U');
+
+ $diff = $old_start_date != $new_start_date || $old_start_time != $new_start_time || $old_duration != $new_duration;
+ $date_shift = $old['start']->diff($event['start']);
+
+ // shifted or resized
+ if ($diff && ($old_start_date == $new_start_date || $old_duration == $new_duration)) {
+ $event['start'] = $master['start']->add($old['start']->diff($event['start']));
+ $event['end'] = clone $event['start'];
+ $event['end']->add(new DateInterval('PT'.$new_duration.'S'));
+ }
+ // dates did not change, use the ones from master
+ else if ($new_start_date . $new_start_time == $old_start_date . $old_start_time) {
+ $event['start'] = $master['start'];
+ $event['end'] = $master['end'];
+ }
+
+ // adjust recurrence-id when start changed and therefore the entire recurrence chain changes
+ if (is_array($event['recurrence']) && ($old_start_date != $new_start_date || $old_start_time != $new_start_time)
+ && ($exceptions = $this->_load_exceptions($old))) {
+ $recurrence_id_format = libcalendaring::recurrence_id_format($event);
+ foreach ($exceptions as $exception) {
+ $recurrence_id = rcube_utils::anytodatetime($exception['_instance'], $old['start']->getTimezone());
+ if (is_a($recurrence_id, 'DateTime')) {
+ $recurrence_id->add($date_shift);
+ $exception['_instance'] = $recurrence_id->format($recurrence_id_format);
+ $this->_update_event($exception, false);
+ }
+ }
+ }
+
+ $ret = $event['id']; // return master ID
+ break;
+ }
+ }
+
+ $success = $this->_update_event($event, $update_recurring);
+
+ if ($success && $update_master)
+ $this->_update_event($master, true);
+
+ return $success ? $ret : false;
+ }
+
+ return false;
+ }
+
+ /**
+ * Extended event editing with possible changes to the argument
+ *
+ * @param array Hash array with event properties
+ * @param string New participant status
+ * @param array List of hash arrays with updated attendees
+ * @return boolean True on success, False on error
+ */
+ public function edit_rsvp(&$event, $status, $attendees)
+ {
+ $update_event = $event;
+
+ // apply changes to master (and all exceptions)
+ if ($event['_savemode'] == 'all' && $event['recurrence_id']) {
+ $update_event = $this->get_event(array('id' => $event['recurrence_id']));
+ $update_event['_savemode'] = $event['_savemode'];
+ calendar::merge_attendee_data($update_event, $attendees);
+ }
+
+ if ($ret = $this->update_attendees($update_event, $attendees)) {
+ // replace $event with effectively updated event (for iTip reply)
+ if ($ret !== true && $ret != $update_event['id'] && ($new_event = $this->get_event(array('id' => $ret)))) {
+ $event = $new_event;
+ }
+ else {
+ $event = $update_event;
+ }
+ }
+
+ return $ret;
+ }
+
+ /**
+ * Update the participant status for the given attendees
+ *
+ * @see calendar_driver::update_attendees()
+ */
+ public function update_attendees(&$event, $attendees)
+ {
+ $success = $this->edit_event($event, true);
+
+ // apply attendee updates to recurrence exceptions too
+ if ($success && $event['_savemode'] == 'all' && !empty($event['recurrence']) && empty($event['recurrence_id']) && ($exceptions = $this->_load_exceptions($event))) {
+ foreach ($exceptions as $exception) {
+ calendar::merge_attendee_data($exception, $attendees);
+ $this->_update_event($exception, false);
+ }
+ }
+
+ return $success;
+ }
+
+ /**
+ * Determine whether the current change affects scheduling and reset attendee status accordingly
+ */
+ private function _check_scheduling(&$event, $old, $update = true)
+ {
+ // skip this check when importing iCal/iTip events
+ if (isset($event['sequence']) || !empty($event['_method'])) {
+ return false;
+ }
+
+ $reschedule = false;
+
+ // iterate through the list of properties considered 'significant' for scheduling
+ foreach (self::$scheduling_properties as $prop) {
+ $a = $old[$prop];
+ $b = $event[$prop];
+ if ($event['allday'] && ($prop == 'start' || $prop == 'end') && $a instanceof DateTime && $b instanceof DateTime) {
+ $a = $a->format('Y-m-d');
+ $b = $b->format('Y-m-d');
+ }
+ if ($prop == 'recurrence' && is_array($a) && is_array($b)) {
+ unset($a['EXCEPTIONS'], $b['EXCEPTIONS']);
+ $a = array_filter($a);
+ $b = array_filter($b);
+
+ // advanced rrule comparison: no rescheduling if series was shortened
+ if ($a['COUNT'] && $b['COUNT'] && $b['COUNT'] < $a['COUNT']) {
+ unset($a['COUNT'], $b['COUNT']);
+ }
+ else if ($a['UNTIL'] && $b['UNTIL'] && $b['UNTIL'] < $a['UNTIL']) {
+ unset($a['UNTIL'], $b['UNTIL']);
+ }
+ }
+ if ($a != $b) {
+ $reschedule = true;
+ break;
+ }
+ }
+
+ // reset all attendee status to needs-action (#4360)
+ if ($update && $reschedule && is_array($event['attendees'])) {
+ $is_organizer = false;
+ $emails = $this->cal->get_user_emails();
+ $attendees = $event['attendees'];
+ foreach ($attendees as $i => $attendee) {
+ if ($attendee['role'] == 'ORGANIZER' && $attendee['email'] && in_array(strtolower($attendee['email']), $emails)) {
+ $is_organizer = true;
+ }
+ else if ($attendee['role'] != 'ORGANIZER' && $attendee['role'] != 'NON-PARTICIPANT' && $attendee['status'] != 'DELEGATED') {
+ $attendees[$i]['status'] = 'NEEDS-ACTION';
+ $attendees[$i]['rsvp'] = true;
+ }
+ }
+
+ // update attendees only if I'm the organizer
+ if ($is_organizer || ($event['organizer'] && in_array(strtolower($event['organizer']['email']), $emails))) {
+ $event['attendees'] = $attendees;
+ }
+ }
+
+ return $reschedule;
+ }
+
+ /**
+ * Convert save data to be used in SQL statements
+ */
+ private function _save_preprocess($event)
+ {
+ // shift dates to server's timezone (except for all-day events)
+ if (!$event['allday']) {
+ $event['start'] = clone $event['start'];
+ $event['start']->setTimezone($this->server_timezone);
+ $event['end'] = clone $event['end'];
+ $event['end']->setTimezone($this->server_timezone);
+ }
+
+ // compose vcalendar-style recurrencue rule from structured data
+ $rrule = $event['recurrence'] ? libcalendaring::to_rrule($event['recurrence']) : '';
+ $event['_recurrence'] = rtrim($rrule, ';');
+ $event['free_busy'] = intval($this->free_busy_map[strtolower($event['free_busy'])]);
+ $event['sensitivity'] = intval($this->sensitivity_map[strtolower($event['sensitivity'])]);
+
+ if ($event['free_busy'] == 'tentative') {
+ $event['status'] = 'TENTATIVE';
+ }
+
+ if (isset($event['allday'])) {
+ $event['all_day'] = $event['allday'] ? 1 : 0;
+ }
+
+ // compute absolute time to notify the user
+ $event['notifyat'] = $this->_get_notification($event);
+
+ if (is_array($event['valarms'])) {
+ $event['alarms'] = $this->serialize_alarms($event['valarms']);
+ }
+
+ // process event attendees
+ if (!empty($event['attendees']))
+ $event['attendees'] = json_encode((array)$event['attendees']);
+ else
+ $event['attendees'] = '';
+
+ return $event;
+ }
+
+ /**
+ * Compute absolute time to notify the user
+ */
+ private function _get_notification($event)
+ {
+ if ($event['valarms'] && $event['start'] > new DateTime()) {
+ $alarm = libcalendaring::get_next_alarm($event);
+
+ if ($alarm['time'] && in_array($alarm['action'], $this->alarm_types))
+ return date('Y-m-d H:i:s', $alarm['time']);
+ }
+
+ return null;
+ }
+
+ /**
+ * Save the given event record to database
+ *
+ * @param array Event data
+ * @param boolean True if recurring events instances should be updated, too
+ */
+ private function _update_event($event, $update_recurring = true)
+ {
+ $event = $this->_save_preprocess($event);
+ $sql_set = array();
+ $set_cols = array('start', 'end', 'all_day', 'recurrence_id', 'isexception', 'sequence', 'title', 'description', 'location', 'categories', 'url', 'free_busy', 'priority', 'sensitivity', 'status', 'attendees', 'alarms', 'notifyat');
+ foreach ($set_cols as $col) {
+ if (is_object($event[$col]) && is_a($event[$col], 'DateTime'))
+ $sql_set[] = $this->rc->db->quote_identifier($col) . '=' . $this->rc->db->quote($event[$col]->format(self::DB_DATE_FORMAT));
+ else if (is_array($event[$col]))
+ $sql_set[] = $this->rc->db->quote_identifier($col) . '=' . $this->rc->db->quote(join(',', $event[$col]));
+ else if (array_key_exists($col, $event))
+ $sql_set[] = $this->rc->db->quote_identifier($col) . '=' . $this->rc->db->quote($event[$col]);
+ }
+
+ if ($event['_recurrence'])
+ $sql_set[] = $this->rc->db->quote_identifier('recurrence') . '=' . $this->rc->db->quote($event['_recurrence']);
+
+ if ($event['_instance'])
+ $sql_set[] = $this->rc->db->quote_identifier('instance') . '=' . $this->rc->db->quote($event['_instance']);
+
+ if ($event['_fromcalendar'] && $event['_fromcalendar'] != $event['calendar'])
+ $sql_set[] = 'calendar_id=' . $this->rc->db->quote($event['calendar']);
+
+ $query = $this->rc->db->query(sprintf(
+ "UPDATE " . $this->db_events . "
+ SET changed=%s %s
+ WHERE event_id=?
+ AND calendar_id IN (" . $this->calendar_ids . ")",
+ $this->rc->db->now(),
+ ($sql_set ? ', ' . join(', ', $sql_set) : '')
+ ),
+ $event['id']
+ );
+
+ $success = $this->rc->db->affected_rows($query);
+
+ // add attachments
+ if ($success && !empty($event['attachments'])) {
+ foreach ($event['attachments'] as $attachment) {
+ $this->add_attachment($attachment, $event['id']);
+ unset($attachment);
+ }
+ }
+
+ // remove attachments
+ if ($success && !empty($event['deleted_attachments'])) {
+ foreach ($event['deleted_attachments'] as $attachment) {
+ $this->remove_attachment($attachment, $event['id']);
+ }
+ }
+
+ if ($success) {
+ unset($this->cache[$event['id']]);
+ if ($update_recurring)
+ $this->_update_recurring($event);
+ }
+
+ return $success;
+ }
+
+ /**
+ * Insert "fake" entries for recurring occurences of this event
+ */
+ private function _update_recurring($event)
+ {
+ if (empty($this->calendars))
+ return;
+
+ if (!empty($event['recurrence'])) {
+ $exdata = array();
+ $exceptions = $this->_load_exceptions($event);
+
+ foreach ($exceptions as $exception) {
+ $exdate = substr($exception['_instance'], 0, 8);
+ $exdata[$exdate] = $exception;
+ }
+ }
+
+ // clear existing recurrence copies
+ $this->rc->db->query(
+ "DELETE FROM " . $this->db_events . "
+ WHERE recurrence_id=?
+ AND isexception=0
+ AND calendar_id IN (" . $this->calendar_ids . ")",
+ $event['id']
+ );
+
+ // create new fake entries
+ if (!empty($event['recurrence'])) {
+ // include library class
+ require_once($this->cal->home . '/lib/calendar_recurrence.php');
+
+ $recurrence = new calendar_recurrence($this->cal, $event);
+
+ $count = 0;
+ $event['allday'] = $event['all_day'];
+ $duration = $event['start']->diff($event['end']);
+ $recurrence_id_format = libcalendaring::recurrence_id_format($event);
+ while ($next_start = $recurrence->next_start()) {
+ $instance = $next_start->format($recurrence_id_format);
+ $datestr = substr($instance, 0, 8);
+
+ // skip exceptions
+ // TODO: merge updated data from master event
+ if ($exdata[$datestr]) {
+ continue;
+ }
+
+ $next_start->setTimezone($this->server_timezone);
+ $next_end = clone $next_start;
+ $next_end->add($duration);
+
+ $notify_at = $this->_get_notification(array('alarms' => $event['alarms'], 'start' => $next_start, 'end' => $next_end, 'status' => $event['status']));
+ $query = $this->rc->db->query(sprintf(
+ "INSERT INTO " . $this->db_events . "
+ (calendar_id, recurrence_id, created, changed, uid, instance, %s, %s, all_day, sequence, recurrence, title, description, location, categories, url, free_busy, priority, sensitivity, status, alarms, attendees, notifyat)
+ SELECT calendar_id, ?, %s, %s, uid, ?, ?, ?, all_day, sequence, recurrence, title, description, location, categories, url, free_busy, priority, sensitivity, status, alarms, attendees, ?
+ FROM " . $this->db_events . " WHERE event_id=? AND calendar_id IN (" . $this->calendar_ids . ")",
+ $this->rc->db->quote_identifier('start'),
+ $this->rc->db->quote_identifier('end'),
+ $this->rc->db->now(),
+ $this->rc->db->now()
+ ),
+ $event['id'],
+ $instance,
+ $next_start->format(self::DB_DATE_FORMAT),
+ $next_end->format(self::DB_DATE_FORMAT),
+ $notify_at,
+ $event['id']
+ );
+
+ if (!$this->rc->db->affected_rows($query))
+ break;
+
+ // stop adding events for inifinite recurrence after 20 years
+ if (++$count > 999 || (!$recurrence->recurEnd && !$recurrence->recurCount && $next_start->format('Y') > date('Y') + 20))
+ break;
+ }
+
+ // remove all exceptions after recurrence end
+ if ($next_end && !empty($exceptions)) {
+ $this->rc->db->query(
+ "DELETE FROM " . $this->db_events . "
+ WHERE `recurrence_id`=?
+ AND `isexception`=1
+ AND `start` > ?
+ AND `calendar_id` IN (" . $this->calendar_ids . ")",
+ $event['id'],
+ $next_end->format(self::DB_DATE_FORMAT)
+ );
+ }
+ }
+ }
+
+ /**
+ *
+ */
+ private function _load_exceptions($event, $instance_id = null)
+ {
+ $sql_add_where = '';
+ if (!empty($instance_id)) {
+ $sql_add_where = 'AND `instance`=?';
+ }
+
+ $result = $this->rc->db->query(
+ "SELECT * FROM " . $this->db_events . "
+ WHERE `recurrence_id`=?
+ AND `isexception`=1
+ AND `calendar_id` IN (" . $this->calendar_ids . ")
+ $sql_add_where
+ ORDER BY `instance`, `start`",
+ $event['id'],
+ $instance_id
+ );
+
+ $exceptions = array();
+ while ($result && ($sql_arr = $this->rc->db->fetch_assoc($result)) && $sql_arr['event_id']) {
+ $exception = $this->_read_postprocess($sql_arr);
+ $instance = $exception['_instance'] ?: $exception['start']->format($exception['allday'] ? 'Ymd' : 'Ymd\THis');
+ $exceptions[$instance] = $exception;
+ }
+
+ return $exceptions;
+ }
+
+ /**
+ * Move a single event
+ *
+ * @param array Hash array with event properties
+ * @see calendar_driver::move_event()
+ */
+ public function move_event($event)
+ {
+ // let edit_event() do all the magic
+ return $this->edit_event($event + (array)$this->get_event($event));
+ }
+
+ /**
+ * Resize a single event
+ *
+ * @param array Hash array with event properties
+ * @see calendar_driver::resize_event()
+ */
+ public function resize_event($event)
+ {
+ // let edit_event() do all the magic
+ return $this->edit_event($event + (array)$this->get_event($event));
+ }
+
+ /**
+ * Remove a single event from the database
+ *
+ * @param array Hash array with event properties
+ * @param boolean Remove record irreversible (@TODO)
+ *
+ * @see calendar_driver::remove_event()
+ */
+ public function remove_event($event, $force = true)
+ {
+ if (!empty($this->calendars)) {
+ $event += (array)$this->get_event($event);
+ $master = $event;
+ $update_master = false;
+ $savemode = 'all';
+ $ret = true;
+
+ // read master if deleting a recurring event
+ if ($event['recurrence'] || $event['recurrence_id']) {
+ $master = $event['recurrence_id'] ? $this->get_event(array('id' => $event['recurrence_id'])) : $event;
+ $savemode = $event['_savemode'];
+ }
+
+ switch ($savemode) {
+ case 'current':
+ // add exception to master event
+ $master['recurrence']['EXDATE'][] = $event['start'];
+ $update_master = true;
+
+ // just delete this single occurence
+ $query = $this->rc->db->query(
+ "DELETE FROM " . $this->db_events . "
+ WHERE calendar_id IN (" . $this->calendar_ids . ")
+ AND event_id=?",
+ $event['id']
+ );
+ break;
+
+ case 'future':
+ if ($master['id'] != $event['id']) {
+ // set until-date on master event
+ $master['recurrence']['UNTIL'] = clone $event['start'];
+ $master['recurrence']['UNTIL']->sub(new DateInterval('P1D'));
+ unset($master['recurrence']['COUNT']);
+ $update_master = true;
+
+ // delete this and all future instances
+ $fromdate = clone $event['start'];
+ $fromdate->setTimezone($this->server_timezone);
+ $query = $this->rc->db->query(
+ "DELETE FROM " . $this->db_events . "
+ WHERE calendar_id IN (" . $this->calendar_ids . ")
+ AND " . $this->rc->db->quote_identifier('start') . " >= ?
+ AND recurrence_id=?",
+ $fromdate->format(self::DB_DATE_FORMAT),
+ $master['id']
+ );
+ $ret = $master['id'];
+ break;
+ }
+ // else: future == all if modifying the master event
+
+ default: // 'all' is default
+ $query = $this->rc->db->query(
+ "DELETE FROM " . $this->db_events . "
+ WHERE (event_id=? OR recurrence_id=?)
+ AND calendar_id IN (" . $this->calendar_ids . ")",
+ $master['id'],
+ $master['id']
+ );
+ break;
+ }
+
+ $success = $this->rc->db->affected_rows($query);
+ if ($success && $update_master)
+ $this->_update_event($master, true);
+
+ return $success ? $ret : false;
+ }
+
+ return false;
+ }
+
+ /**
+ * Return data of a specific event
+ * @param mixed Hash array with event properties or event UID
+ * @param integer Bitmask defining the scope to search events in
+ * @param boolean If true, recurrence exceptions shall be added
+ * @return array Hash array with event properties
+ */
+ public function get_event($event, $scope = 0, $full = false)
+ {
+ $id = is_array($event) ? ($event['id'] ?: $event['uid']) : $event;
+ $cal = is_array($event) ? $event['calendar'] : null;
+ $col = is_array($event) && is_numeric($id) ? 'event_id' : 'uid';
+
+ $where_add = '';
+ if (is_array($event) && !$event['id'] && !empty($event['_instance'])) {
+ $where_add = 'AND instance=' . $this->rc->db->quote($event['_instance']);
+ }
+
+ if ($this->cache[$id])
+ return $this->cache[$id];
+
+ // get event from the address books birthday calendar
+ if ($cal == self::BIRTHDAY_CALENDAR_ID) {
+ return $this->get_birthday_event($id);
+ }
+
+ if ($scope & self::FILTER_ACTIVE) {
+ $calendars = $this->calendars;
+ foreach ($calendars as $idx => $cal) {
+ if (!$cal['active']) {
+ unset($calendars[$idx]);
+ }
+ }
+ $cals = join(',', $calendars);
+ }
+ else {
+ $cals = $this->calendar_ids;
+ }
+
+ $result = $this->rc->db->query(sprintf(
+ "SELECT e.*, (SELECT COUNT(attachment_id) FROM " . $this->db_attachments . "
+ WHERE event_id = e.event_id OR event_id = e.recurrence_id) AS _attachments
+ FROM " . $this->db_events . " AS e
+ WHERE e.calendar_id IN (%s)
+ AND e.$col=?
+ %s",
+ $cals,
+ $where_add
+ ),
+ $id);
+
+ if ($result && ($sql_arr = $this->rc->db->fetch_assoc($result)) && $sql_arr['event_id']) {
+ $event = $this->_read_postprocess($sql_arr);
+
+ // also load recurrence exceptions
+ if (!empty($event['recurrence']) && $full) {
+ $event['recurrence']['EXCEPTIONS'] = array_values($this->_load_exceptions($event));
+ }
+
+ $this->cache[$id] = $event;
+ return $this->cache[$id];
+ }
+
+ return false;
+ }
+
+ /**
+ * Get event data
+ *
+ * @see calendar_driver::load_events()
+ */
+ public function load_events($start, $end, $query = null, $calendars = null, $virtual = 1, $modifiedsince = null)
+ {
+ if (empty($calendars))
+ $calendars = array_keys($this->calendars);
+ else if (!is_array($calendars))
+ $calendars = explode(',', strval($calendars));
+
+ // only allow to select from calendars of this use
+ $calendar_ids = array_map(array($this->rc->db, 'quote'), array_intersect($calendars, array_keys($this->calendars)));
+
+ // compose (slow) SQL query for searching
+ // FIXME: improve searching using a dedicated col and normalized values
+ if ($query) {
+ foreach (array('title','location','description','categories','attendees') as $col)
+ $sql_query[] = $this->rc->db->ilike($col, '%'.$query.'%');
+ $sql_add = 'AND (' . join(' OR ', $sql_query) . ')';
+ }
+
+ if (!$virtual)
+ $sql_add .= ' AND e.recurrence_id = 0';
+
+ if ($modifiedsince)
+ $sql_add .= ' AND e.changed >= ' . $this->rc->db->quote(date('Y-m-d H:i:s', $modifiedsince));
+
+ $events = array();
+ if (!empty($calendar_ids)) {
+ $result = $this->rc->db->query(sprintf(
+ "SELECT e.*, (SELECT COUNT(attachment_id) FROM " . $this->db_attachments . "
+ WHERE event_id = e.event_id OR event_id = e.recurrence_id) AS _attachments
+ FROM " . $this->db_events . " e
+ WHERE e.calendar_id IN (%s)
+ AND e.start <= %s AND e.end >= %s
+ %s",
+ join(',', $calendar_ids),
+ $this->rc->db->fromunixtime($end),
+ $this->rc->db->fromunixtime($start),
+ $sql_add
+ ));
+
+ while ($result && ($sql_arr = $this->rc->db->fetch_assoc($result))) {
+ $event = $this->_read_postprocess($sql_arr);
+ $add = true;
+
+ if (!empty($event['recurrence']) && !$event['recurrence_id']) {
+ // load recurrence exceptions (i.e. for export)
+ if (!$virtual) {
+ $event['recurrence']['EXCEPTIONS'] = $this->_load_exceptions($event);
+ }
+ // check for exception on first instance
+ else {
+ $instance = libcalendaring::recurrence_instance_identifier($event);
+ $exceptions = $this->_load_exceptions($event, $instance);
+ if ($exceptions && is_array($exceptions[$instance])) {
+ $event = $exceptions[$instance];
+ $add = false;
+ }
+ }
+ }
+
+ if ($add)
+ $events[] = $event;
+ }
+ }
+
+ // add events from the address books birthday calendar
+ if (in_array(self::BIRTHDAY_CALENDAR_ID, $calendars) && empty($query)) {
+ $events = array_merge($events, $this->load_birthday_events($start, $end, $search, $modifiedsince));
+ }
+
+ return $events;
+ }
+
+ /**
+ * Get number of events in the given calendar
+ *
+ * @param mixed List of calendar IDs to count events (either as array or comma-separated string)
+ * @param integer Date range start (unix timestamp)
+ * @param integer Date range end (unix timestamp)
+ * @return array Hash array with counts grouped by calendar ID
+ */
+ public function count_events($calendars, $start, $end = null)
+ {
+ // not implemented
+ return array();
+ }
+
+ /**
+ * Convert sql record into a rcube style event object
+ */
+ private function _read_postprocess($event)
+ {
+ $free_busy_map = array_flip($this->free_busy_map);
+ $sensitivity_map = array_flip($this->sensitivity_map);
+
+ $event['id'] = $event['event_id'];
+ $event['start'] = new DateTime($event['start']);
+ $event['end'] = new DateTime($event['end']);
+ $event['allday'] = intval($event['all_day']);
+ $event['created'] = new DateTime($event['created']);
+ $event['changed'] = new DateTime($event['changed']);
+ $event['free_busy'] = $free_busy_map[$event['free_busy']];
+ $event['sensitivity'] = $sensitivity_map[$event['sensitivity']];
+ $event['calendar'] = $event['calendar_id'];
+ $event['recurrence_id'] = intval($event['recurrence_id']);
+ $event['isexception'] = intval($event['isexception']);
+
+ // parse recurrence rule
+ if ($event['recurrence'] && preg_match_all('/([A-Z]+)=([^;]+);?/', $event['recurrence'], $m, PREG_SET_ORDER)) {
+ $event['recurrence'] = array();
+ foreach ($m as $rr) {
+ if (is_numeric($rr[2]))
+ $rr[2] = intval($rr[2]);
+ else if ($rr[1] == 'UNTIL')
+ $rr[2] = date_create($rr[2]);
+ else if ($rr[1] == 'RDATE')
+ $rr[2] = array_map('date_create', explode(',', $rr[2]));
+ else if ($rr[1] == 'EXDATE')
+ $rr[2] = array_map('date_create', explode(',', $rr[2]));
+ $event['recurrence'][$rr[1]] = $rr[2];
+ }
+ }
+
+ if ($event['recurrence_id']) {
+ libcalendaring::identify_recurrence_instance($event);
+ }
+
+ if (strlen($event['instance'])) {
+ $event['_instance'] = $event['instance'];
+
+ if (empty($event['recurrence_id'])) {
+ $event['recurrence_date'] = rcube_utils::anytodatetime($event['_instance'], $event['start']->getTimezone());
+ }
+ }
+
+ if ($event['_attachments'] > 0) {
+ $event['attachments'] = (array)$this->list_attachments($event);
+ }
+
+ // decode serialized event attendees
+ if (strlen($event['attendees'])) {
+ $event['attendees'] = $this->unserialize_attendees($event['attendees']);
+ }
+ else {
+ $event['attendees'] = array();
+ }
+
+ // decode serialized alarms
+ if ($event['alarms']) {
+ $event['valarms'] = $this->unserialize_alarms($event['alarms']);
+ }
+
+ unset($event['event_id'], $event['calendar_id'], $event['notifyat'], $event['all_day'], $event['instance'], $event['_attachments']);
+ return $event;
+ }
+
+ /**
+ * Get a list of pending alarms to be displayed to the user
+ *
+ * @see calendar_driver::pending_alarms()
+ */
+ public function pending_alarms($time, $calendars = null)
+ {
+ if (empty($calendars))
+ $calendars = array_keys($this->calendars);
+ else if (is_string($calendars))
+ $calendars = explode(',', $calendars);
+
+ // only allow to select from calendars with activated alarms
+ $calendar_ids = array();
+ foreach ($calendars as $cid) {
+ if ($this->calendars[$cid] && $this->calendars[$cid]['showalarms'])
+ $calendar_ids[] = $cid;
+ }
+ $calendar_ids = array_map(array($this->rc->db, 'quote'), $calendar_ids);
+
+ $alarms = array();
+ if (!empty($calendar_ids)) {
+ $result = $this->rc->db->query(sprintf(
+ "SELECT * FROM " . $this->db_events . "
+ WHERE calendar_id IN (%s)
+ AND notifyat <= %s AND %s > %s",
+ join(',', $calendar_ids),
+ $this->rc->db->fromunixtime($time),
+ $this->rc->db->quote_identifier('end'),
+ $this->rc->db->fromunixtime($time)
+ ));
+
+ while ($result && ($event = $this->rc->db->fetch_assoc($result)))
+ $alarms[] = $this->_read_postprocess($event);
+ }
+
+ return $alarms;
+ }
+
+ /**
+ * Feedback after showing/sending an alarm notification
+ *
+ * @see calendar_driver::dismiss_alarm()
+ */
+ public function dismiss_alarm($event_id, $snooze = 0)
+ {
+ // set new notifyat time or unset if not snoozed
+ $notify_at = $snooze > 0 ? date(self::DB_DATE_FORMAT, time() + $snooze) : null;
+
+ $query = $this->rc->db->query(sprintf(
+ "UPDATE " . $this->db_events . "
+ SET changed=%s, notifyat=?
+ WHERE event_id=?
+ AND calendar_id IN (" . $this->calendar_ids . ")",
+ $this->rc->db->now()),
+ $notify_at,
+ $event_id
+ );
+
+ return $this->rc->db->affected_rows($query);
+ }
+
+ /**
+ * Save an attachment related to the given event
+ */
+ private function add_attachment($attachment, $event_id)
+ {
+ $data = $attachment['data'] ? $attachment['data'] : file_get_contents($attachment['path']);
+
+ $query = $this->rc->db->query(
+ "INSERT INTO " . $this->db_attachments .
+ " (event_id, filename, mimetype, size, data)" .
+ " VALUES (?, ?, ?, ?, ?)",
+ $event_id,
+ $attachment['name'],
+ $attachment['mimetype'],
+ strlen($data),
+ base64_encode($data)
+ );
+
+ return $this->rc->db->affected_rows($query);
+ }
+
+ /**
+ * Remove a specific attachment from the given event
+ */
+ private function remove_attachment($attachment_id, $event_id)
+ {
+ $query = $this->rc->db->query(
+ "DELETE FROM " . $this->db_attachments .
+ " WHERE attachment_id = ?" .
+ " AND event_id IN (SELECT event_id FROM " . $this->db_events .
+ " WHERE event_id = ?" .
+ " AND calendar_id IN (" . $this->calendar_ids . "))",
+ $attachment_id,
+ $event_id
+ );
+
+ return $this->rc->db->affected_rows($query);
+ }
+
+ /**
+ * List attachments of specified event
+ */
+ public function list_attachments($event)
+ {
+ $attachments = array();
+
+ if (!empty($this->calendar_ids)) {
+ $result = $this->rc->db->query(
+ "SELECT attachment_id AS id, filename AS name, mimetype, size " .
+ " FROM " . $this->db_attachments .
+ " WHERE event_id IN (SELECT event_id FROM " . $this->db_events .
+ " WHERE event_id=?" .
+ " AND calendar_id IN (" . $this->calendar_ids . "))".
+ " ORDER BY filename",
+ $event['recurrence_id'] ? $event['recurrence_id'] : $event['event_id']
+ );
+
+ while ($result && ($arr = $this->rc->db->fetch_assoc($result))) {
+ $attachments[] = $arr;
+ }
+ }
+
+ return $attachments;
+ }
+
+ /**
+ * Get attachment properties
+ */
+ public function get_attachment($id, $event)
+ {
+ if (!empty($this->calendar_ids)) {
+ $result = $this->rc->db->query(
+ "SELECT attachment_id AS id, filename AS name, mimetype, size " .
+ " FROM " . $this->db_attachments .
+ " WHERE attachment_id=?".
+ " AND event_id=?",
+ $id,
+ $event['recurrence_id'] ? $event['recurrence_id'] : $event['id']
+ );
+
+ if ($result && ($arr = $this->rc->db->fetch_assoc($result))) {
+ return $arr;
+ }
+ }
+
+ return null;
+ }
+
+ /**
+ * Get attachment body
+ */
+ public function get_attachment_body($id, $event)
+ {
+ if (!empty($this->calendar_ids)) {
+ $result = $this->rc->db->query(
+ "SELECT data " .
+ " FROM " . $this->db_attachments .
+ " WHERE attachment_id=?".
+ " AND event_id=?",
+ $id,
+ $event['id']
+ );
+
+ if ($result && ($arr = $this->rc->db->fetch_assoc($result))) {
+ return base64_decode($arr['data']);
+ }
+ }
+
+ return null;
+ }
+
+ /**
+ * Remove the given category
+ */
+ public function remove_category($name)
+ {
+ $query = $this->rc->db->query(
+ "UPDATE " . $this->db_events . "
+ SET categories=''
+ WHERE categories=?
+ AND calendar_id IN (" . $this->calendar_ids . ")",
+ $name
+ );
+
+ return $this->rc->db->affected_rows($query);
+ }
+
+ /**
+ * Update/replace a category
+ */
+ public function replace_category($oldname, $name, $color)
+ {
+ $query = $this->rc->db->query(
+ "UPDATE " . $this->db_events . "
+ SET categories=?
+ WHERE categories=?
+ AND calendar_id IN (" . $this->calendar_ids . ")",
+ $name,
+ $oldname
+ );
+
+ return $this->rc->db->affected_rows($query);
+ }
+
+ /**
+ * Helper method to serialize the list of alarms into a string
+ */
+ private function serialize_alarms($valarms)
+ {
+ foreach ((array)$valarms as $i => $alarm) {
+ if ($alarm['trigger'] instanceof DateTime) {
+ $valarms[$i]['trigger'] = '@' . $alarm['trigger']->format('c');
+ }
+ }
+
+ return $valarms ? json_encode($valarms) : null;
+ }
+
+ /**
+ * Helper method to decode a serialized list of alarms
+ */
+ private function unserialize_alarms($alarms)
+ {
+ // decode json serialized alarms
+ if ($alarms && $alarms[0] == '[') {
+ $valarms = json_decode($alarms, true);
+ foreach ($valarms as $i => $alarm) {
+ if ($alarm['trigger'][0] == '@') {
+ try {
+ $valarms[$i]['trigger'] = new DateTime(substr($alarm['trigger'], 1));
+ }
+ catch (Exception $e) {
+ unset($valarms[$i]);
+ }
+ }
+ }
+ }
+ // convert legacy alarms data
+ else if (strlen($alarms)) {
+ list($trigger, $action) = explode(':', $alarms, 2);
+ if ($trigger = libcalendaring::parse_alarm_value($trigger)) {
+ $valarms = array(array('action' => $action, 'trigger' => $trigger[3] ?: $trigger[0]));
+ }
+ }
+
+ return $valarms;
+ }
+
+ /**
+ * Helper method to decode the attendees list from string
+ */
+ private function unserialize_attendees($s_attendees)
+ {
+ $attendees = array();
+
+ // decode json serialized string
+ if ($s_attendees[0] == '[') {
+ $attendees = json_decode($s_attendees, true);
+ }
+ // decode the old serialization format
+ else {
+ foreach (explode("\n", $event['attendees']) as $line) {
+ $att = array();
+ foreach (rcube_utils::explode_quoted_string(';', $line) as $prop) {
+ list($key, $value) = explode("=", $prop);
+ $att[strtolower($key)] = stripslashes(trim($value, '""'));
+ }
+ $attendees[] = $att;
+ }
+ }
+
+ return $attendees;
+ }
+
+ /**
+ * Handler for user_delete plugin hook
+ */
+ public function user_delete($args)
+ {
+ $db = $this->rc->db;
+ $user = $args['user'];
+ $event_ids = array();
+
+ $events = $db->query(
+ "SELECT event_id FROM " . $this->db_events . " AS ev" .
+ " LEFT JOIN " . $this->db_calendars . " cal ON (ev.calendar_id = cal.calendar_id)".
+ " WHERE user_id=?",
+ $user->ID);
+
+ while ($row = $db->fetch_assoc($events)) {
+ $event_ids[] = $row['event_id'];
+ }
+
+ if (!empty($event_ids)) {
+ foreach (array($this->db_attachments, $this->db_events) as $table) {
+ $db->query(sprintf("DELETE FROM $table WHERE event_id IN (%s)", join(',', $event_ids)));
+ }
+ }
+
+ foreach (array($this->db_calendars, 'itipinvitations') as $table) {
+ $db->query("DELETE FROM $table WHERE user_id=?", $user->ID);
+ }
+ }
+
+}
diff --git a/calendar/drivers/ical/SQL/mysql.initial.sql b/calendar/drivers/ical/SQL/mysql.initial.sql
new file mode 100644
index 0000000..6ae4dec
--- /dev/null
+++ b/calendar/drivers/ical/SQL/mysql.initial.sql
@@ -0,0 +1,91 @@
+/**
+ * iCAL Client
+ *
+ * @version @package_version@
+ * @author Daniel Morlock <daniel.morlock@awesome-it.de>
+ *
+ * Copyright (C) Awesome IT GbR <info@awesome-it.de>
+ *
+ * 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.
+ *
+ * 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
+ * GNU Affero General Public License for more details.
+ *
+ * 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/>.
+ */
+
+CREATE TABLE IF NOT EXISTS `ical_calendars` (
+ `calendar_id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
+ `user_id` int(10) UNSIGNED NOT NULL DEFAULT '0',
+ `name` varchar(255) NOT NULL,
+ `color` varchar(8) NOT NULL,
+ `showalarms` tinyint(1) NOT NULL DEFAULT '1',
+
+ `ical_url` varchar(255) NOT NULL,
+ `ical_user` varchar(255) DEFAULT NULL,
+ `ical_pass` varchar(1024) DEFAULT NULL,
+ `ical_last_change` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
+
+ PRIMARY KEY(`calendar_id`),
+ INDEX `ical_user_name_idx` (`user_id`, `name`),
+ CONSTRAINT `fk_ical_calendars_user_id` FOREIGN KEY (`user_id`)
+ REFERENCES `users`(`user_id`) ON DELETE CASCADE ON UPDATE CASCADE
+) /*!40000 ENGINE=INNODB */ /*!40101 CHARACTER SET utf8 COLLATE utf8_general_ci */;
+
+CREATE TABLE IF NOT EXISTS `ical_events` (
+ `event_id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
+ `calendar_id` int(11) UNSIGNED NOT NULL DEFAULT '0',
+ `recurrence_id` int(11) UNSIGNED NOT NULL DEFAULT '0',
+ `uid` varchar(255) NOT NULL DEFAULT '',
+ `instance` varchar(16) NOT NULL DEFAULT '',
+ `isexception` tinyint(1) NOT NULL DEFAULT '0',
+ `created` datetime NOT NULL DEFAULT '1000-01-01 00:00:00',
+ `changed` datetime NOT NULL DEFAULT '1000-01-01 00:00:00',
+ `sequence` int(1) UNSIGNED NOT NULL DEFAULT '0',
+ `start` datetime NOT NULL DEFAULT '1000-01-01 00:00:00',
+ `end` datetime NOT NULL DEFAULT '1000-01-01 00:00:00',
+ `recurrence` varchar(255) DEFAULT NULL,
+ `title` varchar(255) NOT NULL,
+ `description` text NOT NULL,
+ `location` varchar(255) NOT NULL DEFAULT '',
+ `categories` varchar(255) NOT NULL DEFAULT '',
+ `url` varchar(255) NOT NULL DEFAULT '',
+ `all_day` tinyint(1) NOT NULL DEFAULT '0',
+ `free_busy` tinyint(1) NOT NULL DEFAULT '0',
+ `priority` tinyint(1) NOT NULL DEFAULT '0',
+ `sensitivity` tinyint(1) NOT NULL DEFAULT '0',
+ `status` varchar(32) NOT NULL DEFAULT '',
+ `alarms` text NULL DEFAULT NULL,
+ `attendees` text DEFAULT NULL,
+ `notifyat` datetime DEFAULT NULL,
+
+ `ical_url` varchar(255) NOT NULL,
+ `ical_last_change` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
+
+ PRIMARY KEY(`event_id`),
+ INDEX `ical_uid_idx` (`uid`),
+ INDEX `ical_recurrence_idx` (`recurrence_id`),
+ INDEX `ical_calendar_notify_idx` (`calendar_id`,`notifyat`),
+ CONSTRAINT `fk_ical_events_calendar_id` FOREIGN KEY (`calendar_id`)
+ REFERENCES `ical_calendars`(`calendar_id`) ON DELETE CASCADE ON UPDATE CASCADE
+) /*!40000 ENGINE=INNODB */ /*!40101 CHARACTER SET utf8 COLLATE utf8_general_ci */;
+
+CREATE TABLE IF NOT EXISTS `ical_attachments` (
+ `attachment_id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
+ `event_id` int(11) UNSIGNED NOT NULL DEFAULT '0',
+ `filename` varchar(255) NOT NULL DEFAULT '',
+ `mimetype` varchar(255) NOT NULL DEFAULT '',
+ `size` int(11) NOT NULL DEFAULT '0',
+ `data` longtext NOT NULL,
+ PRIMARY KEY(`attachment_id`),
+ CONSTRAINT `fk_ical_attachments_event_id` FOREIGN KEY (`event_id`)
+ REFERENCES `ical_events`(`event_id`) ON DELETE CASCADE ON UPDATE CASCADE
+) /*!40000 ENGINE=INNODB */ /*!40101 CHARACTER SET utf8 COLLATE utf8_general_ci */;
+
+REPLACE INTO `system` (`name`, `value`) VALUES ('calendar-ical-version', '2015022700'); \ No newline at end of file
diff --git a/calendar/drivers/ical/SQL/mysql/.keep_dir b/calendar/drivers/ical/SQL/mysql/.keep_dir
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/calendar/drivers/ical/SQL/mysql/.keep_dir
diff --git a/calendar/drivers/ical/SQL/mysql/2015022500.sql b/calendar/drivers/ical/SQL/mysql/2015022500.sql
new file mode 100644
index 0000000..6dc8727
--- /dev/null
+++ b/calendar/drivers/ical/SQL/mysql/2015022500.sql
@@ -0,0 +1,124 @@
+/**
+ * iCAL Client
+ *
+ * @version @package_version@
+ * @author Daniel Morlock <daniel.morlock@awesome-it.de>
+ *
+ * Copyright (C) Awesome IT GbR <info@awesome-it.de>
+ *
+ * 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.
+ *
+ * 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
+ * GNU Affero General Public License for more details.
+ *
+ * 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/>.
+ */
+
+/* Create new tables */
+CREATE TABLE IF NOT EXISTS `ical_calendars` (
+ `calendar_id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
+ `user_id` int(10) UNSIGNED NOT NULL DEFAULT '0',
+ `name` varchar(255) NOT NULL,
+ `color` varchar(8) NOT NULL,
+ `showalarms` tinyint(1) NOT NULL DEFAULT '1',
+
+ `ical_url` varchar(255) NOT NULL,
+ `ical_user` varchar(255) DEFAULT NULL,
+ `ical_pass` varchar(1024) DEFAULT NULL,
+ `ical_last_change` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
+
+ PRIMARY KEY(`calendar_id`),
+ INDEX `ical_user_name_idx` (`user_id`, `name`),
+ CONSTRAINT `fk_ical_calendars_user_id` FOREIGN KEY (`user_id`)
+ REFERENCES `users`(`user_id`) ON DELETE CASCADE ON UPDATE CASCADE
+) /*!40000 ENGINE=INNODB */ /*!40101 CHARACTER SET utf8 COLLATE utf8_general_ci */;
+
+CREATE TABLE IF NOT EXISTS `ical_events` (
+ `event_id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
+ `calendar_id` int(11) UNSIGNED NOT NULL DEFAULT '0',
+ `recurrence_id` int(11) UNSIGNED NOT NULL DEFAULT '0',
+ `uid` varchar(255) NOT NULL DEFAULT '',
+ `created` datetime NOT NULL DEFAULT '1000-01-01 00:00:00',
+ `changed` datetime NOT NULL DEFAULT '1000-01-01 00:00:00',
+ `sequence` int(1) UNSIGNED NOT NULL DEFAULT '0',
+ `start` datetime NOT NULL DEFAULT '1000-01-01 00:00:00',
+ `end` datetime NOT NULL DEFAULT '1000-01-01 00:00:00',
+ `recurrence` varchar(255) DEFAULT NULL,
+ `title` varchar(255) NOT NULL,
+ `description` text NOT NULL,
+ `location` varchar(255) NOT NULL DEFAULT '',
+ `categories` varchar(255) NOT NULL DEFAULT '',
+ `url` varchar(255) NOT NULL DEFAULT '',
+ `all_day` tinyint(1) NOT NULL DEFAULT '0',
+ `free_busy` tinyint(1) NOT NULL DEFAULT '0',
+ `priority` tinyint(1) NOT NULL DEFAULT '0',
+ `sensitivity` tinyint(1) NOT NULL DEFAULT '0',
+ `status` varchar(32) NOT NULL DEFAULT '',
+ `alarms` varchar(255) DEFAULT NULL,
+ `attendees` text DEFAULT NULL,
+ `notifyat` datetime DEFAULT NULL,
+
+ PRIMARY KEY(`event_id`),
+ INDEX `ical_uid_idx` (`uid`),
+ INDEX `ical_recurrence_idx` (`recurrence_id`),
+ INDEX `ical_calendar_notify_idx` (`calendar_id`,`notifyat`),
+ CONSTRAINT `fk_ical_events_calendar_id` FOREIGN KEY (`calendar_id`)
+ REFERENCES `calendars`(`calendar_id`) ON DELETE CASCADE ON UPDATE CASCADE
+) /*!40000 ENGINE=INNODB */ /*!40101 CHARACTER SET utf8 COLLATE utf8_general_ci */;
+
+CREATE TABLE IF NOT EXISTS `ical_attachments` (
+ `attachment_id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
+ `event_id` int(11) UNSIGNED NOT NULL DEFAULT '0',
+ `filename` varchar(255) NOT NULL DEFAULT '',
+ `mimetype` varchar(255) NOT NULL DEFAULT '',
+ `size` int(11) NOT NULL DEFAULT '0',
+ `data` longtext NOT NULL,
+ PRIMARY KEY(`attachment_id`),
+ CONSTRAINT `fk_ical_attachments_event_id` FOREIGN KEY (`event_id`)
+ REFERENCES `events`(`event_id`) ON DELETE CASCADE ON UPDATE CASCADE
+) /*!40000 ENGINE=INNODB */ /*!40101 CHARACTER SET utf8 COLLATE utf8_general_ci */;
+
+/* Migrate Data */
+INSERT INTO ical_calendars
+ SELECT calendar_id, user_id, `name`, color, showalarms,
+ url as ical_url, NULL as ical_user, NULL as ical_pass, last_change as ical_last_change
+ FROM calendars cal, ical_props dav
+ WHERE dav.obj_id = cal.calendar_id
+ AND dav.obj_type = 'ical';
+
+INSERT INTO ical_events SELECT e.* FROM `events` e
+ WHERE e.calendar_id IN (
+ SELECT obj_id FROM ical_props
+ WHERE obj_type = 'ical'
+ );
+
+INSERT INTO ical_attachments SELECT * FROM attachments a
+WHERE a.event_id IN (
+ SELECT e.event_id FROM `events` e
+ WHERE e.calendar_id IN (
+ SELECT obj_id FROM ical_props
+ WHERE obj_type = 'ical'
+ )
+);
+
+/* Drop deprecated data */
+DELETE FROM `events` WHERE event_id IN (
+ SELECT obj_id FROM ical_props dav
+ WHERE dav.obj_type = 'vevent'
+);
+DELETE FROM calendars WHERE calendar_id IN (
+ SELECT obj_id FROM ical_props dav
+ WHERE dav.obj_type = 'ical'
+);
+DELETE FROM attachments WHERE event_id IN (
+ SELECT obj_id FROM ical_props dav
+ WHERE dav.obj_type = 'vevent'
+);
+DROP TABLE ical_props;
+
diff --git a/calendar/drivers/ical/SQL/mysql/2015022700.sql b/calendar/drivers/ical/SQL/mysql/2015022700.sql
new file mode 100644
index 0000000..3acb892
--- /dev/null
+++ b/calendar/drivers/ical/SQL/mysql/2015022700.sql
@@ -0,0 +1,14 @@
+-- add identifier for recurring instances and exceptions
+
+ALTER TABLE `ical_events` ADD `instance` varchar(16) NOT NULL DEFAULT '' AFTER `uid`;
+ALTER TABLE `ical_events` ADD `isexception` tinyint(1) NOT NULL DEFAULT '0' AFTER `instance`;
+
+UPDATE `ical_events` SET `instance` = DATE_FORMAT(`start`, '%Y%m%d')
+ WHERE `recurrence_id` != 0 AND `instance` = '' AND `all_day` = 1;
+
+UPDATE `ical_events` SET `instance` = DATE_FORMAT(`start`, '%Y%m%dT%k%i%s')
+ WHERE `recurrence_id` != 0 AND `instance` = '' AND `all_day` = 0;
+
+-- extend alarms columns for multiple values
+
+ALTER TABLE `ical_events` CHANGE `alarms` `alarms` TEXT NULL DEFAULT NULL; \ No newline at end of file
diff --git a/calendar/drivers/ical/ical_driver.php b/calendar/drivers/ical/ical_driver.php
new file mode 100644
index 0000000..0916a11
--- /dev/null
+++ b/calendar/drivers/ical/ical_driver.php
@@ -0,0 +1,1821 @@
+<?php
+
+/**
+ * iCalendar driver for the Calendar plugin
+ *
+ * @author Daniel Morlock <daniel.morlock@awesome-it.de>
+ *
+ * Copyright (C) Awesome IT GbR <info@awesome-it.de>
+ *
+ * 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.
+ *
+ * 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
+ * GNU Affero General Public License for more details.
+ *
+ * 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/>.
+ */
+
+require_once(dirname(__FILE__) . '/ical_sync.php');
+require_once (dirname(__FILE__).'/../../lib/encryption.php');
+
+/**
+ * TODO
+ * - Postgresql, Sqlite scripts.
+ *
+ */
+class ical_driver extends calendar_driver
+{
+ const DB_DATE_FORMAT = 'Y-m-d H:i:s';
+
+ public static $scheduling_properties = array('start', 'end', 'allday', 'recurrence', 'location', 'cancelled');
+
+ // features this backend supports
+ public $alarms = true;
+ public $attendees = true;
+ public $freebusy = false;
+ public $attachments = true;
+ public $alarm_types = array('DISPLAY');
+
+ private $rc;
+ private $cal;
+ private $cache = array();
+ private $calendars = array();
+ private $calendar_ids = '';
+ private $free_busy_map = array('free' => 0, 'busy' => 1, 'out-of-office' => 2, 'outofoffice' => 2, 'tentative' => 3);
+ private $sensitivity_map = array('public' => 0, 'private' => 1, 'confidential' => 2);
+ private $server_timezone;
+
+ private $db_events = 'ical_events';
+ private $db_calendars = 'ical_calendars';
+ private $db_attachments = 'ical_attachments';
+
+
+ private $sync_clients = array();
+
+ // Min. time period to wait until sync check.
+ private $sync_period = 10; // TODO: 600; // seconds
+
+ // Crypt key for CalDAV auth
+ private $crypt_key;
+
+ // Indicates debug mode for iCAL
+ static private $debug = null;
+
+ /**
+ * Helper method to log debug msg if debug mode is enabled.
+ */
+ static public function debug_log($msg)
+ {
+ if(self::$debug === true)
+ rcmail::console(__CLASS__.': '.$msg);
+ }
+
+ /**
+ * Helper method to log (if debug mode is enabled) and raise an user error.
+ */
+ private function _raise_error($msg)
+ {
+ self::debug_log($msg);
+ $this->rc->output->show_message($msg, 'error');
+ }
+
+ /**
+ * Default constructor
+ */
+ public function __construct($cal)
+ {
+ $this->cal = $cal;
+ $this->rc = $cal->rc;
+ $this->server_timezone = new DateTimeZone(date_default_timezone_get());
+
+ // read database config
+ $db = $this->rc->get_dbh();
+ $this->db_events = $this->rc->config->get('db_table_events', $db->table_name($this->db_events));
+ $this->db_calendars = $this->rc->config->get('db_table_calendars', $db->table_name($this->db_calendars));
+ $this->db_attachments = $this->rc->config->get('db_table_attachments', $db->table_name($this->db_attachments));
+ $this->crypt_key = $this->rc->config->get("calendar_crypt_key", "%E`c{2;<J2F^4_&._BxfQ<5Pf3qv!m{e");
+
+ // Set debug state
+ if (self::$debug === null)
+ self::$debug = $this->rc->config->get('calendar_ical_debug', false);
+
+ // PHP's fopen wrappers must be allowed
+ if(!ini_get("allow_url_fopen"))
+ self::_raise_error("iCAL driver needs PHP's fopen wrappers to be allowed!");
+
+ $this->_read_calendars();
+ }
+
+ /**
+ * Read available calendars for the current user and store them internally
+ */
+ protected function _read_calendars()
+ {
+ $hidden = array_filter(explode(',', $this->rc->config->get('ical_hidden_calendars', '')));
+
+ if (!empty($this->rc->user->ID)) {
+ $calendar_ids = array();
+ $result = $this->rc->db->query("
+ SELECT *, calendar_id AS id FROM " . $this->db_calendars . "
+ WHERE user_id=?
+ ORDER BY name",
+ $this->rc->user->ID
+ );
+ while ($result && ($arr = $this->rc->db->fetch_assoc($result))) {
+ $arr['showalarms'] = intval($arr['showalarms']);
+ $arr['active'] = !in_array($arr['id'], $hidden);
+ $arr['name'] = html::quote($arr['name']);
+ $arr['listname'] = html::quote($arr['name']);
+ $arr['rights'] = 'lrswikxteav';
+ $arr['editable'] = true;
+ $arr['ical_pass'] = $this->_decrypt_pass($arr['ical_pass']);
+ $this->calendars[$arr['calendar_id']] = $arr;
+ $calendar_ids[] = $this->rc->db->quote($arr['calendar_id']);
+
+ // Init sync client
+ $cal_id = $arr['calendar_id'];
+ $this->sync_clients[$cal_id] = new ical_sync($cal_id, $arr);
+ }
+ $this->calendar_ids = join(',', $calendar_ids);
+ }
+ }
+
+ /**
+ * Get a list of available calendars from this source
+ *
+ * @param integer Bitmask defining filter criterias
+ *
+ * @return array List of calendars
+ */
+ public function list_calendars($filter = 0)
+ {
+ $calendars = $this->calendars;
+
+ // filter active calendars
+ if ($filter & self::FILTER_ACTIVE) {
+ foreach ($calendars as $idx => $cal) {
+ if (!$cal['active']) {
+ unset($calendars[$idx]);
+ }
+ }
+ }
+
+ // 'personal' is unsupported in this driver
+
+ return array_map(function($cal) {
+
+ // Make calendar readonly
+ $cal["readonly"] = true;
+
+ // Readonly but deletable
+ $cal["deletable"] = true;
+
+ // But name should be editable!
+ $cal["editable_name"] = true;
+
+ return $cal;
+
+ }, $calendars);
+ }
+
+ /**
+ * Create a new calendar assigned to the current user
+ *
+ * @param array Hash array with calendar properties
+ * name: Calendar name
+ * color: The color of the calendar
+ * @return mixed ID of the calendar on success, False on error
+ */
+ public function create_calendar($prop)
+ {
+ $result = $this->rc->db->query(
+ "INSERT INTO " . $this->db_calendars . "
+ (user_id, name, color, showalarms, ical_url, ical_user)
+ VALUES (?, ?, ?, ?, ?, ?)",
+ $this->rc->user->ID,
+ $prop['name'],
+ $prop['color'],
+ $prop['showalarms'] ? 1 : 0,
+ self::_encode_url($prop["ical_url"]),
+ isset($props["ical_user"]) ? $props["ical_user"] : null,
+ isset($props["ical_pass"]) ? $this->_encrypt_pass($props["ical_pass"]) : null
+ );
+
+ if ($result) {
+
+ $cal_id = $this->rc->db->insert_id($this->db_calendars);
+
+ // Initial sync of newly created calendars.
+ $this->sync_clients[$cal_id] = new ical_sync($cal_id, $prop);
+ $this->_sync_calendar($cal_id);
+
+ return $cal_id;
+ }
+
+ return false;
+ }
+
+ /**
+ * Update properties of an existing calendar
+ *
+ * @see calendar_driver::edit_calendar()
+ */
+ public function edit_calendar($prop)
+ {
+ $query = $this->rc->db->query(
+ "UPDATE " . $this->db_calendars . "
+ SET name=?, color=?, showalarms=?, ical_url=?, ical_user=?
+ WHERE calendar_id=?
+ AND user_id=?",
+ $prop['name'],
+ $prop['color'],
+ $prop['showalarms'] ? 1 : 0,
+ isset($prop["ical_url"]) ? $prop["ical_url"] : null,
+ isset($prop["ical_user"]) ? $prop["ical_user"] : null,
+ $prop['id'],
+ $this->rc->user->ID
+ );
+
+ // Change password if specified
+ if (isset($prop["ical_pass"])) {
+ $query = $this->rc->db->query("UPDATE " . $this->db_calendars . "
+ SET ical_pass=?
+ WHERE calendar_id=?
+ AND user_id=?",
+ $this->_encrypt_pass($prop['ical_pass']),
+ $prop['id'],
+ $this->rc->user->ID
+ );
+ }
+
+ return $this->rc->db->affected_rows($query);
+ }
+
+ /**
+ * Set active/subscribed state of a calendar
+ * Save a list of hidden calendars in user prefs
+ *
+ * @see calendar_driver::subscribe_calendar()
+ */
+ public function subscribe_calendar($prop)
+ {
+ $hidden = array_flip(explode(',', $this->rc->config->get('ical_hidden_calendars', '')));
+
+ if ($prop['active'])
+ unset($hidden[$prop['id']]);
+ else
+ $hidden[$prop['id']] = 1;
+
+ return $this->rc->user->save_prefs(array('ical_hidden_calendars' => join(',', array_keys($hidden))));
+ }
+
+ /**
+ * Delete the given calendar with all its contents
+ *
+ * @see calendar_driver::delete_calendar()
+ */
+ public function delete_calendar($prop)
+ {
+ if (!$this->calendars[$prop['id']])
+ return false;
+
+ // events and attachments will be deleted by foreign key cascade
+
+ $query = $this->rc->db->query(
+ "DELETE FROM " . $this->db_calendars . "
+ WHERE calendar_id=?",
+ $prop['id']
+ );
+
+ return $this->rc->db->affected_rows($query);
+ }
+
+ /**
+ * Search for shared or otherwise not listed calendars the user has access
+ *
+ * @param string Search string
+ * @param string Section/source to search
+ * @return array List of calendars
+ */
+ public function search_calendars($query, $source)
+ {
+ // not implemented
+ return array();
+ }
+
+ /**
+ * Not supported by iCAL.
+ *
+ * @param $event
+ * @return bool
+ */
+ public function new_event($event)
+ {
+ return false;
+ }
+
+ /**
+ * Add a single event to the database
+ *
+ * @param array Hash array with event properties
+ * @see calendar_driver::new_event()
+ */
+ private function _db_new_event($event)
+ {
+ if (!$this->validate($event))
+ return false;
+
+ if (!empty($this->calendars)) {
+ if ($event['calendar'] && !$this->calendars[$event['calendar']])
+ return false;
+ if (!$event['calendar'])
+ $event['calendar'] = reset(array_keys($this->calendars));
+
+ if ($event_id = $this->_insert_event($event)) {
+ $this->_update_recurring($event);
+ }
+
+ return $event_id;
+ }
+
+ return false;
+ }
+
+ /**
+ *
+ */
+ private function _insert_event(&$event)
+ {
+ $event = $this->_save_preprocess($event);
+
+ $this->rc->db->query(sprintf(
+ "INSERT INTO " . $this->db_events . "
+ (calendar_id, created, changed, uid, recurrence_id, instance, isexception, %s, %s, all_day, recurrence,
+ title, description, location, categories, url, free_busy, priority, sensitivity, status, attendees, alarms, notifyat)
+ VALUES (?, %s, %s, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
+ $this->rc->db->quote_identifier('start'),
+ $this->rc->db->quote_identifier('end'),
+ $this->rc->db->now(),
+ $this->rc->db->now()
+ ),
+ $event['calendar'],
+ strval($event['uid']),
+ intval($event['recurrence_id']),
+ strval($event['_instance']),
+ intval($event['isexception']),
+ $event['start']->format(self::DB_DATE_FORMAT),
+ $event['end']->format(self::DB_DATE_FORMAT),
+ intval($event['all_day']),
+ $event['_recurrence'],
+ strval($event['title']),
+ strval($event['description']),
+ strval($event['location']),
+ join(',', (array)$event['categories']),
+ strval($event['url']),
+ intval($event['free_busy']),
+ intval($event['priority']),
+ intval($event['sensitivity']),
+ strval($event['status']),
+ $event['attendees'],
+ $event['alarms'],
+ $event['notifyat']
+ );
+
+ $event_id = $this->rc->db->insert_id($this->db_events);
+
+ if ($event_id) {
+ $event['id'] = $event_id;
+
+ // add attachments
+ if (!empty($event['attachments'])) {
+ foreach ($event['attachments'] as $attachment) {
+ $this->add_attachment($attachment, $event_id);
+ unset($attachment);
+ }
+ }
+
+ return $event_id;
+ }
+
+ return false;
+ }
+
+ /**
+ * Not supported for iCAL
+ *
+ * @param $event
+ * @return bool
+ */
+ public function edit_event($event)
+ {
+ return false;
+ }
+
+ /**
+ * Update an event entry with the given data
+ *
+ * @param array Hash array with event properties
+ * @see calendar_driver::edit_event()
+ */
+ private function _db_edit_event($event)
+ {
+ if (!empty($this->calendars)) {
+ $update_master = false;
+ $update_recurring = true;
+ $old = $this->get_event($event);
+ $ret = true;
+
+ // check if update affects scheduling and update attendee status accordingly
+ $reschedule = $this->_check_scheduling($event, $old, true);
+
+ // increment sequence number
+ if (empty($event['sequence']) && $reschedule)
+ $event['sequence'] = max($event['sequence'], $old['sequence']) + 1;
+
+ // modify a recurring event, check submitted savemode to do the right things
+ if ($old['recurrence'] || $old['recurrence_id']) {
+ $master = $old['recurrence_id'] ? $this->get_event(array('id' => $old['recurrence_id'])) : $old;
+
+ // keep saved exceptions (not submitted by the client)
+ if ($old['recurrence']['EXDATE'])
+ $event['recurrence']['EXDATE'] = $old['recurrence']['EXDATE'];
+
+ switch ($event['_savemode']) {
+ case 'new':
+ $event['uid'] = $this->cal->generate_uid();
+ return $this->new_event($event);
+
+ case 'current':
+ // save as exception
+ $event['isexception'] = 1;
+ $update_recurring = false;
+
+ // set exception to first instance (= master)
+ if ($event['id'] == $master['id']) {
+ $event += $old;
+ $event['recurrence_id'] = $master['id'];
+ $event['_instance'] = libcalendaring::recurrence_instance_identifier($old);
+ $event['isexception'] = 1;
+ $event_id = $this->_insert_event($event);
+ return $event_id;
+ }
+ break;
+
+ case 'future':
+ if ($master['id'] != $event['id']) {
+ // set until-date on master event, then save this instance as new recurring event
+ $master['recurrence']['UNTIL'] = clone $event['start'];
+ $master['recurrence']['UNTIL']->sub(new DateInterval('P1D'));
+ unset($master['recurrence']['COUNT']);
+ $update_master = true;
+
+ // if recurrence COUNT, update value to the correct number of future occurences
+ if ($event['recurrence']['COUNT']) {
+ $fromdate = clone $event['start'];
+ $fromdate->setTimezone($this->server_timezone);
+ $sqlresult = $this->rc->db->query(sprintf(
+ "SELECT event_id FROM " . $this->db_events . "
+ WHERE calendar_id IN (%s)
+ AND %s >= ?
+ AND recurrence_id=?",
+ $this->calendar_ids,
+ $this->rc->db->quote_identifier('start')
+ ),
+ $fromdate->format(self::DB_DATE_FORMAT),
+ $master['id']);
+ if ($count = $this->rc->db->num_rows($sqlresult))
+ $event['recurrence']['COUNT'] = $count;
+ }
+
+ $update_recurring = true;
+ $event['recurrence_id'] = 0;
+ $event['isexception'] = 0;
+ $event['_instance'] = '';
+ break;
+ }
+ // else: 'future' == 'all' if modifying the master event
+
+ default: // 'all' is default
+ $event['id'] = $master['id'];
+ $event['recurrence_id'] = 0;
+
+ // use start date from master but try to be smart on time or duration changes
+ $old_start_date = $old['start']->format('Y-m-d');
+ $old_start_time = $old['allday'] ? '' : $old['start']->format('H:i');
+ $old_duration = $old['end']->format('U') - $old['start']->format('U');
+
+ $new_start_date = $event['start']->format('Y-m-d');
+ $new_start_time = $event['allday'] ? '' : $event['start']->format('H:i');
+ $new_duration = $event['end']->format('U') - $event['start']->format('U');
+
+ $diff = $old_start_date != $new_start_date || $old_start_time != $new_start_time || $old_duration != $new_duration;
+ $date_shift = $old['start']->diff($event['start']);
+
+ // shifted or resized
+ if ($diff && ($old_start_date == $new_start_date || $old_duration == $new_duration)) {
+ $event['start'] = $master['start']->add($old['start']->diff($event['start']));
+ $event['end'] = clone $event['start'];
+ $event['end']->add(new DateInterval('PT' . $new_duration . 'S'));
+ } // dates did not change, use the ones from master
+ else if ($new_start_date . $new_start_time == $old_start_date . $old_start_time) {
+ $event['start'] = $master['start'];
+ $event['end'] = $master['end'];
+ }
+
+ // adjust recurrence-id when start changed and therefore the entire recurrence chain changes
+ if (is_array($event['recurrence']) && ($old_start_date != $new_start_date || $old_start_time != $new_start_time)
+ && ($exceptions = $this->_load_exceptions($old))
+ ) {
+ $recurrence_id_format = libcalendaring::recurrence_id_format($event);
+ foreach ($exceptions as $exception) {
+ $recurrence_id = rcube_utils::anytodatetime($exception['_instance'], $old['start']->getTimezone());
+ if (is_a($recurrence_id, 'DateTime')) {
+ $recurrence_id->add($date_shift);
+ $exception['_instance'] = $recurrence_id->format($recurrence_id_format);
+ $this->_update_event($exception, false);
+ }
+ }
+ }
+
+ $ret = $event['id']; // return master ID
+ break;
+ }
+ }
+
+ $success = $this->_update_event($event, $update_recurring);
+
+ if ($success && $update_master)
+ $this->_update_event($master, true);
+
+ return $success ? $ret : false;
+ }
+
+ return false;
+ }
+
+ /**
+ * Extended event editing with possible changes to the argument
+ *
+ * @param array Hash array with event properties
+ * @param string New participant status
+ * @param array List of hash arrays with updated attendees
+ * @return boolean True on success, False on error
+ */
+ public function edit_rsvp(&$event, $status, $attendees)
+ {
+ $update_event = $event;
+
+ // apply changes to master (and all exceptions)
+ if ($event['_savemode'] == 'all' && $event['recurrence_id']) {
+ $update_event = $this->get_event(array('id' => $event['recurrence_id']));
+ $update_event['_savemode'] = $event['_savemode'];
+ calendar::merge_attendee_data($update_event, $attendees);
+ }
+
+ if ($ret = $this->update_attendees($update_event, $attendees)) {
+ // replace $event with effectively updated event (for iTip reply)
+ if ($ret !== true && $ret != $update_event['id'] && ($new_event = $this->get_event(array('id' => $ret)))) {
+ $event = $new_event;
+ } else {
+ $event = $update_event;
+ }
+ }
+
+ return $ret;
+ }
+
+ /**
+ * Update the participant status for the given attendees
+ *
+ * @see calendar_driver::update_attendees()
+ */
+ public function update_attendees(&$event, $attendees)
+ {
+ $success = $this->edit_event($event, true);
+
+ // apply attendee updates to recurrence exceptions too
+ if ($success && $event['_savemode'] == 'all' && !empty($event['recurrence']) && empty($event['recurrence_id']) && ($exceptions = $this->_load_exceptions($event))) {
+ foreach ($exceptions as $exception) {
+ calendar::merge_attendee_data($exception, $attendees);
+ $this->_update_event($exception, false);
+ }
+ }
+
+ return $success;
+ }
+
+ /**
+ * Determine whether the current change affects scheduling and reset attendee status accordingly
+ */
+ private function _check_scheduling(&$event, $old, $update = true)
+ {
+ // skip this check when importing iCal/iTip events
+ if (isset($event['sequence']) || !empty($event['_method'])) {
+ return false;
+ }
+
+ $reschedule = false;
+
+ // iterate through the list of properties considered 'significant' for scheduling
+ foreach (self::$scheduling_properties as $prop) {
+ $a = $old[$prop];
+ $b = $event[$prop];
+ if ($event['allday'] && ($prop == 'start' || $prop == 'end') && $a instanceof DateTime && $b instanceof DateTime) {
+ $a = $a->format('Y-m-d');
+ $b = $b->format('Y-m-d');
+ }
+ if ($prop == 'recurrence' && is_array($a) && is_array($b)) {
+ unset($a['EXCEPTIONS'], $b['EXCEPTIONS']);
+ $a = array_filter($a);
+ $b = array_filter($b);
+
+ // advanced rrule comparison: no rescheduling if series was shortened
+ if ($a['COUNT'] && $b['COUNT'] && $b['COUNT'] < $a['COUNT']) {
+ unset($a['COUNT'], $b['COUNT']);
+ } else if ($a['UNTIL'] && $b['UNTIL'] && $b['UNTIL'] < $a['UNTIL']) {
+ unset($a['UNTIL'], $b['UNTIL']);
+ }
+ }
+ if ($a != $b) {
+ $reschedule = true;
+ break;
+ }
+ }
+
+ // reset all attendee status to needs-action (#4360)
+ if ($update && $reschedule && is_array($event['attendees'])) {
+ $is_organizer = false;
+ $emails = $this->cal->get_user_emails();
+ $attendees = $event['attendees'];
+ foreach ($attendees as $i => $attendee) {
+ if ($attendee['role'] == 'ORGANIZER' && $attendee['email'] && in_array(strtolower($attendee['email']), $emails)) {
+ $is_organizer = true;
+ } else if ($attendee['role'] != 'ORGANIZER' && $attendee['role'] != 'NON-PARTICIPANT' && $attendee['status'] != 'DELEGATED') {
+ $attendees[$i]['status'] = 'NEEDS-ACTION';
+ $attendees[$i]['rsvp'] = true;
+ }
+ }
+
+ // update attendees only if I'm the organizer
+ if ($is_organizer || ($event['organizer'] && in_array(strtolower($event['organizer']['email']), $emails))) {
+ $event['attendees'] = $attendees;
+ }
+ }
+
+ return $reschedule;
+ }
+
+ /**
+ * Convert save data to be used in SQL statements
+ */
+ private function _save_preprocess($event)
+ {
+ // shift dates to server's timezone (except for all-day events)
+ if (!$event['allday']) {
+ $event['start'] = clone $event['start'];
+ $event['start']->setTimezone($this->server_timezone);
+ $event['end'] = clone $event['end'];
+ $event['end']->setTimezone($this->server_timezone);
+ }
+
+ // compose vcalendar-style recurrencue rule from structured data
+ $rrule = $event['recurrence'] ? libcalendaring::to_rrule($event['recurrence']) : '';
+ $event['_recurrence'] = rtrim($rrule, ';');
+ $event['free_busy'] = intval($this->free_busy_map[strtolower($event['free_busy'])]);
+ $event['sensitivity'] = intval($this->sensitivity_map[strtolower($event['sensitivity'])]);
+
+ if ($event['free_busy'] == 'tentative') {
+ $event['status'] = 'TENTATIVE';
+ }
+
+ if (isset($event['allday'])) {
+ $event['all_day'] = $event['allday'] ? 1 : 0;
+ }
+
+ // compute absolute time to notify the user
+ $event['notifyat'] = $this->_get_notification($event);
+
+ if (is_array($event['valarms'])) {
+ $event['alarms'] = $this->serialize_alarms($event['valarms']);
+ }
+
+ // process event attendees
+ if (!empty($event['attendees']))
+ $event['attendees'] = json_encode((array)$event['attendees']);
+ else
+ $event['attendees'] = '';
+
+ return $event;
+ }
+
+ /**
+ * Compute absolute time to notify the user
+ */
+ private function _get_notification($event)
+ {
+ if ($event['valarms'] && $event['start'] > new DateTime()) {
+ $alarm = libcalendaring::get_next_alarm($event);
+
+ if ($alarm['time'] && in_array($alarm['action'], $this->alarm_types))
+ return date('Y-m-d H:i:s', $alarm['time']);
+ }
+
+ return null;
+ }
+
+ /**
+ * Save the given event record to database
+ *
+ * @param array Event data
+ * @param boolean True if recurring events instances should be updated, too
+ */
+ private function _update_event($event, $update_recurring = true)
+ {
+ $event = $this->_save_preprocess($event);
+ $sql_set = array();
+ $set_cols = array('start', 'end', 'all_day', 'recurrence_id', 'isexception', 'sequence', 'title', 'description', 'location', 'categories', 'url', 'free_busy', 'priority', 'sensitivity', 'status', 'attendees', 'alarms', 'notifyat');
+ foreach ($set_cols as $col) {
+ if (is_object($event[$col]) && is_a($event[$col], 'DateTime'))
+ $sql_set[] = $this->rc->db->quote_identifier($col) . '=' . $this->rc->db->quote($event[$col]->format(self::DB_DATE_FORMAT));
+ else if (is_array($event[$col]))
+ $sql_set[] = $this->rc->db->quote_identifier($col) . '=' . $this->rc->db->quote(join(',', $event[$col]));
+ else if (array_key_exists($col, $event))
+ $sql_set[] = $this->rc->db->quote_identifier($col) . '=' . $this->rc->db->quote($event[$col]);
+ }
+
+ if ($event['_recurrence'])
+ $sql_set[] = $this->rc->db->quote_identifier('recurrence') . '=' . $this->rc->db->quote($event['_recurrence']);
+
+ if ($event['_instance'])
+ $sql_set[] = $this->rc->db->quote_identifier('instance') . '=' . $this->rc->db->quote($event['_instance']);
+
+ if ($event['_fromcalendar'] && $event['_fromcalendar'] != $event['calendar'])
+ $sql_set[] = 'calendar_id=' . $this->rc->db->quote($event['calendar']);
+
+ $query = $this->rc->db->query(sprintf(
+ "UPDATE " . $this->db_events . "
+ SET changed=%s %s
+ WHERE event_id=?
+ AND calendar_id IN (" . $this->calendar_ids . ")",
+ $this->rc->db->now(),
+ ($sql_set ? ', ' . join(', ', $sql_set) : '')
+ ),
+ $event['id']
+ );
+
+ $success = $this->rc->db->affected_rows($query);
+
+ // add attachments
+ if ($success && !empty($event['attachments'])) {
+ foreach ($event['attachments'] as $attachment) {
+ $this->add_attachment($attachment, $event['id']);
+ unset($attachment);
+ }
+ }
+
+ // remove attachments
+ if ($success && !empty($event['deleted_attachments'])) {
+ foreach ($event['deleted_attachments'] as $attachment) {
+ $this->remove_attachment($attachment, $event['id']);
+ }
+ }
+
+ if ($success) {
+ unset($this->cache[$event['id']]);
+ if ($update_recurring)
+ $this->_update_recurring($event);
+ }
+
+ return $success;
+ }
+
+ /**
+ * Insert "fake" entries for recurring occurences of this event
+ */
+ private function _update_recurring($event)
+ {
+ if (empty($this->calendars))
+ return;
+
+ if (!empty($event['recurrence'])) {
+ $exdata = array();
+ $exceptions = $this->_load_exceptions($event);
+
+ foreach ($exceptions as $exception) {
+ $exdate = substr($exception['_instance'], 0, 8);
+ $exdata[$exdate] = $exception;
+ }
+ }
+
+ // clear existing recurrence copies
+ $this->rc->db->query(
+ "DELETE FROM " . $this->db_events . "
+ WHERE recurrence_id=?
+ AND isexception=0
+ AND calendar_id IN (" . $this->calendar_ids . ")",
+ $event['id']
+ );
+
+ // create new fake entries
+ if (!empty($event['recurrence'])) {
+ // include library class
+ require_once($this->cal->home . '/lib/calendar_recurrence.php');
+
+ $recurrence = new calendar_recurrence($this->cal, $event);
+
+ $count = 0;
+ $event['allday'] = $event['all_day'];
+ $duration = $event['start']->diff($event['end']);
+ $recurrence_id_format = libcalendaring::recurrence_id_format($event);
+ while ($next_start = $recurrence->next_start()) {
+ $instance = $next_start->format($recurrence_id_format);
+ $datestr = substr($instance, 0, 8);
+
+ // skip exceptions
+ // TODO: merge updated data from master event
+ if ($exdata[$datestr]) {
+ continue;
+ }
+
+ $next_start->setTimezone($this->server_timezone);
+ $next_end = clone $next_start;
+ $next_end->add($duration);
+
+ $notify_at = $this->_get_notification(array('alarms' => $event['alarms'], 'start' => $next_start, 'end' => $next_end, 'status' => $event['status']));
+ $query = $this->rc->db->query(sprintf(
+ "INSERT INTO " . $this->db_events . "
+ (calendar_id, recurrence_id, created, changed, uid, instance, %s, %s, all_day, sequence, recurrence, title, description, location, categories, url, free_busy, priority, sensitivity, status, alarms, attendees, notifyat)
+ SELECT calendar_id, ?, %s, %s, uid, ?, ?, ?, all_day, sequence, recurrence, title, description, location, categories, url, free_busy, priority, sensitivity, status, alarms, attendees, ?
+ FROM " . $this->db_events . " WHERE event_id=? AND calendar_id IN (" . $this->calendar_ids . ")",
+ $this->rc->db->quote_identifier('start'),
+ $this->rc->db->quote_identifier('end'),
+ $this->rc->db->now(),
+ $this->rc->db->now()
+ ),
+ $event['id'],
+ $instance,
+ $next_start->format(self::DB_DATE_FORMAT),
+ $next_end->format(self::DB_DATE_FORMAT),
+ $notify_at,
+ $event['id']
+ );
+
+ if (!$this->rc->db->affected_rows($query))
+ break;
+
+ // stop adding events for inifinite recurrence after 20 years
+ if (++$count > 999 || (!$recurrence->recurEnd && !$recurrence->recurCount && $next_start->format('Y') > date('Y') + 20))
+ break;
+ }
+
+ // remove all exceptions after recurrence end
+ if ($next_end && !empty($exceptions)) {
+ $this->rc->db->query(
+ "DELETE FROM " . $this->db_events . "
+ WHERE `recurrence_id`=?
+ AND `isexception`=1
+ AND `start` > ?
+ AND `calendar_id` IN (" . $this->calendar_ids . ")",
+ $event['id'],
+ $next_end->format(self::DB_DATE_FORMAT)
+ );
+ }
+ }
+ }
+
+ /**
+ *
+ */
+ private function _load_exceptions($event, $instance_id = null)
+ {
+ $sql_add_where = '';
+ if (!empty($instance_id)) {
+ $sql_add_where = 'AND `instance`=?';
+ }
+
+ $result = $this->rc->db->query(
+ "SELECT * FROM " . $this->db_events . "
+ WHERE `recurrence_id`=?
+ AND `isexception`=1
+ AND `calendar_id` IN (" . $this->calendar_ids . ")
+ $sql_add_where
+ ORDER BY `instance`, `start`",
+ $event['id'],
+ $instance_id
+ );
+
+ $exceptions = array();
+ while ($result && ($sql_arr = $this->rc->db->fetch_assoc($result)) && $sql_arr['event_id']) {
+ $exception = $this->_read_postprocess($sql_arr);
+ $instance = $exception['_instance'] ?: $exception['start']->format($exception['allday'] ? 'Ymd' : 'Ymd\THis');
+ $exceptions[$instance] = $exception;
+ }
+
+ return $exceptions;
+ }
+
+ /**
+ * Move a single event
+ *
+ * @param array Hash array with event properties
+ * @see calendar_driver::move_event()
+ */
+ public function move_event($event)
+ {
+ // let edit_event() do all the magic
+ return $this->edit_event($event + (array)$this->get_event($event));
+ }
+
+ /**
+ * Resize a single event
+ *
+ * @param array Hash array with event properties
+ * @see calendar_driver::resize_event()
+ */
+ public function resize_event($event)
+ {
+ // let edit_event() do all the magic
+ return $this->edit_event($event + (array)$this->get_event($event));
+ }
+
+ /**
+ * Not supported by iCAL
+ *
+ * @param $event
+ * @param bool $force
+ * @return bool
+ */
+ public function remove_event($event, $force = true)
+ {
+ return false;
+ }
+
+ /**
+ * Remove a single event from the database
+ *
+ * @param array Hash array with event properties
+ * @param boolean Remove record irreversible (@TODO)
+ *
+ * @see calendar_driver::remove_event()
+ */
+ private function _db_remove_event($event, $force = true)
+ {
+ if (!empty($this->calendars)) {
+ $event += (array)$this->get_event($event);
+ $master = $event;
+ $update_master = false;
+ $savemode = 'all';
+ $ret = true;
+
+ // read master if deleting a recurring event
+ if ($event['recurrence'] || $event['recurrence_id']) {
+ $master = $event['recurrence_id'] ? $this->get_event(array('id' => $event['recurrence_id'])) : $event;
+ $savemode = $event['_savemode'];
+ }
+
+ switch ($savemode) {
+ case 'current':
+ // add exception to master event
+ $master['recurrence']['EXDATE'][] = $event['start'];
+ $update_master = true;
+
+ // just delete this single occurence
+ $query = $this->rc->db->query(
+ "DELETE FROM " . $this->db_events . "
+ WHERE calendar_id IN (" . $this->calendar_ids . ")
+ AND event_id=?",
+ $event['id']
+ );
+ break;
+
+ case 'future':
+ if ($master['id'] != $event['id']) {
+ // set until-date on master event
+ $master['recurrence']['UNTIL'] = clone $event['start'];
+ $master['recurrence']['UNTIL']->sub(new DateInterval('P1D'));
+ unset($master['recurrence']['COUNT']);
+ $update_master = true;
+
+ // delete this and all future instances
+ $fromdate = clone $event['start'];
+ $fromdate->setTimezone($this->server_timezone);
+ $query = $this->rc->db->query(
+ "DELETE FROM " . $this->db_events . "
+ WHERE calendar_id IN (" . $this->calendar_ids . ")
+ AND " . $this->rc->db->quote_identifier('start') . " >= ?
+ AND recurrence_id=?",
+ $fromdate->format(self::DB_DATE_FORMAT),
+ $master['id']
+ );
+ $ret = $master['id'];
+ break;
+ }
+ // else: future == all if modifying the master event
+
+ default: // 'all' is default
+ $query = $this->rc->db->query(
+ "DELETE FROM " . $this->db_events . "
+ WHERE (event_id=? OR recurrence_id=?)
+ AND calendar_id IN (" . $this->calendar_ids . ")",
+ $master['id'],
+ $master['id']
+ );
+ break;
+ }
+
+ $success = $this->rc->db->affected_rows($query);
+ if ($success && $update_master)
+ $this->_update_event($master, true);
+
+ return $success ? $ret : false;
+ }
+
+ return false;
+ }
+
+ /**
+ * Return data of a specific event
+ * @param mixed Hash array with event properties or event UID
+ * @param integer Bitmask defining the scope to search events in
+ * @param boolean If true, recurrence exceptions shall be added
+ * @return array Hash array with event properties
+ */
+ public function get_event($event, $scope = 0, $full = false)
+ {
+ $id = is_array($event) ? ($event['id'] ?: $event['uid']) : $event;
+ $cal = is_array($event) ? $event['calendar'] : null;
+ $col = is_array($event) && is_numeric($id) ? 'event_id' : 'uid';
+
+ $where_add = '';
+ if (is_array($event) && !$event['id'] && !empty($event['_instance'])) {
+ $where_add = 'AND instance=' . $this->rc->db->quote($event['_instance']);
+ }
+
+ if ($this->cache[$id])
+ return $this->cache[$id];
+
+ if ($scope & self::FILTER_ACTIVE) {
+ $calendars = $this->calendars;
+ foreach ($calendars as $idx => $cal) {
+ if (!$cal['active']) {
+ unset($calendars[$idx]);
+ }
+ }
+ $cals = join(',', $calendars);
+ } else {
+ $cals = $this->calendar_ids;
+ }
+
+ $result = $this->rc->db->query(sprintf(
+ "SELECT e.*, (SELECT COUNT(attachment_id) FROM " . $this->db_attachments . "
+ WHERE event_id = e.event_id OR event_id = e.recurrence_id) AS _attachments
+ FROM " . $this->db_events . " AS e
+ WHERE e.calendar_id IN (%s)
+ AND e.$col=?
+ %s",
+ $cals,
+ $where_add
+ ),
+ $id);
+
+ if ($result && ($sql_arr = $this->rc->db->fetch_assoc($result)) && $sql_arr['event_id']) {
+ $event = $this->_read_postprocess($sql_arr);
+
+ // also load recurrence exceptions
+ if (!empty($event['recurrence']) && $full) {
+ $event['recurrence']['EXCEPTIONS'] = array_values($this->_load_exceptions($event));
+ }
+
+ $this->cache[$id] = $event;
+ return $this->cache[$id];
+ }
+
+ return false;
+ }
+
+ /**
+ * Sync and returns event data
+ *
+ * @see calendar_driver::load_events()
+ */
+ public function load_events($start, $end, $query = null, $calendars = null, $virtual = 1, $modifiedsince = null)
+ {
+ if (empty($calendars))
+ $calendars = array_keys($this->calendars);
+ else if (!is_array($calendars))
+ $calendars = explode(',', strval($calendars));
+
+ // only allow to select from calendars of this use
+ $calendar_ids = array_intersect($calendars, array_keys($this->calendars));
+
+ // Make sure that the calendars are in sync.
+ foreach ($calendar_ids as $cal_id) {
+ if (!$this->_is_synced($cal_id))
+ $this->_sync_calendar($cal_id);
+ }
+
+ return $this->_db_load_events($start, $end, $query, $calendars, $virtual, $modifiedsince);
+ }
+
+ /**
+ * Get event data
+ *
+ * @see calendar_driver::load_events()
+ */
+ private function _db_load_events($start, $end, $query = null, $calendars = null, $virtual = 1, $modifiedsince = null)
+ {
+ if (empty($calendars))
+ $calendars = array_keys($this->calendars);
+ else if (!is_array($calendars))
+ $calendars = explode(',', strval($calendars));
+
+ // only allow to select from calendars of this use
+ $calendar_ids = array_map(array($this->rc->db, 'quote'), array_intersect($calendars, array_keys($this->calendars)));
+
+ // compose (slow) SQL query for searching
+ // FIXME: improve searching using a dedicated col and normalized values
+ if ($query) {
+ foreach (array('title', 'location', 'description', 'categories', 'attendees') as $col)
+ $sql_query[] = $this->rc->db->ilike($col, '%' . $query . '%');
+ $sql_add = 'AND (' . join(' OR ', $sql_query) . ')';
+ }
+
+ if (!$virtual)
+ $sql_add .= ' AND e.recurrence_id = 0';
+
+ if ($modifiedsince)
+ $sql_add .= ' AND e.changed >= ' . $this->rc->db->quote(date('Y-m-d H:i:s', $modifiedsince));
+
+ $events = array();
+ if (!empty($calendar_ids)) {
+ $result = $this->rc->db->query(sprintf(
+ "SELECT e.*, (SELECT COUNT(attachment_id) FROM " . $this->db_attachments . "
+ WHERE event_id = e.event_id OR event_id = e.recurrence_id) AS _attachments
+ FROM " . $this->db_events . " e
+ WHERE e.calendar_id IN (%s)
+ AND e.start <= %s AND e.end >= %s
+ %s",
+ join(',', $calendar_ids),
+ $this->rc->db->fromunixtime($end),
+ $this->rc->db->fromunixtime($start),
+ $sql_add
+ ));
+
+ while ($result && ($sql_arr = $this->rc->db->fetch_assoc($result))) {
+ $event = $this->_read_postprocess($sql_arr);
+ $add = true;
+
+ if (!empty($event['recurrence']) && !$event['recurrence_id']) {
+ // load recurrence exceptions (i.e. for export)
+ if (!$virtual) {
+ $event['recurrence']['EXCEPTIONS'] = $this->_load_exceptions($event);
+ } // check for exception on first instance
+ else {
+ $instance = libcalendaring::recurrence_instance_identifier($event);
+ $exceptions = $this->_load_exceptions($event, $instance);
+ if ($exceptions && is_array($exceptions[$instance])) {
+ $event = $exceptions[$instance];
+ $add = false;
+ }
+ }
+ }
+
+ if ($add)
+ $events[] = $event;
+ }
+ }
+
+ return $events;
+ }
+
+ /**
+ * Get number of events in the given calendar
+ *
+ * @param mixed List of calendar IDs to count events (either as array or comma-separated string)
+ * @param integer Date range start (unix timestamp)
+ * @param integer Date range end (unix timestamp)
+ * @return array Hash array with counts grouped by calendar ID
+ */
+ public function count_events($calendars, $start, $end = null)
+ {
+ // not implemented
+ return array();
+ }
+
+ /**
+ * Convert sql record into a rcube style event object
+ */
+ private function _read_postprocess($event)
+ {
+ $free_busy_map = array_flip($this->free_busy_map);
+ $sensitivity_map = array_flip($this->sensitivity_map);
+
+ $event['id'] = $event['event_id'];
+ $event['start'] = new DateTime($event['start']);
+ $event['end'] = new DateTime($event['end']);
+ $event['allday'] = intval($event['all_day']);
+ $event['created'] = new DateTime($event['created']);
+ $event['changed'] = new DateTime($event['changed']);
+ $event['free_busy'] = $free_busy_map[$event['free_busy']];
+ $event['sensitivity'] = $sensitivity_map[$event['sensitivity']];
+ $event['calendar'] = $event['calendar_id'];
+ $event['recurrence_id'] = intval($event['recurrence_id']);
+ $event['isexception'] = intval($event['isexception']);
+
+ // parse recurrence rule
+ if ($event['recurrence'] && preg_match_all('/([A-Z]+)=([^;]+);?/', $event['recurrence'], $m, PREG_SET_ORDER)) {
+ $event['recurrence'] = array();
+ foreach ($m as $rr) {
+ if (is_numeric($rr[2]))
+ $rr[2] = intval($rr[2]);
+ else if ($rr[1] == 'UNTIL')
+ $rr[2] = date_create($rr[2]);
+ else if ($rr[1] == 'RDATE')
+ $rr[2] = array_map('date_create', explode(',', $rr[2]));
+ else if ($rr[1] == 'EXDATE')
+ $rr[2] = array_map('date_create', explode(',', $rr[2]));
+ $event['recurrence'][$rr[1]] = $rr[2];
+ }
+ }
+
+ if ($event['recurrence_id']) {
+ libcalendaring::identify_recurrence_instance($event);
+ }
+
+ if (strlen($event['instance'])) {
+ $event['_instance'] = $event['instance'];
+
+ if (empty($event['recurrence_id'])) {
+ $event['recurrence_date'] = rcube_utils::anytodatetime($event['_instance'], $event['start']->getTimezone());
+ }
+ }
+
+ if ($event['_attachments'] > 0) {
+ $event['attachments'] = (array)$this->list_attachments($event);
+ }
+
+ // decode serialized event attendees
+ if (strlen($event['attendees'])) {
+ $event['attendees'] = $this->unserialize_attendees($event['attendees']);
+ } else {
+ $event['attendees'] = array();
+ }
+
+ // decode serialized alarms
+ if ($event['alarms']) {
+ $event['valarms'] = $this->unserialize_alarms($event['alarms']);
+ }
+
+ unset($event['event_id'], $event['calendar_id'], $event['notifyat'], $event['all_day'], $event['instance'], $event['_attachments']);
+ return $event;
+ }
+
+ /**
+ * Get a list of pending alarms to be displayed to the user
+ *
+ * @see calendar_driver::pending_alarms()
+ */
+ public function pending_alarms($time, $calendars = null)
+ {
+ if (empty($calendars))
+ $calendars = array_keys($this->calendars);
+ else if (is_string($calendars))
+ $calendars = explode(',', $calendars);
+
+ // only allow to select from calendars with activated alarms
+ $calendar_ids = array();
+ foreach ($calendars as $cid) {
+ if ($this->calendars[$cid] && $this->calendars[$cid]['showalarms'])
+ $calendar_ids[] = $cid;
+ }
+ $calendar_ids = array_map(array($this->rc->db, 'quote'), $calendar_ids);
+
+ $alarms = array();
+ if (!empty($calendar_ids)) {
+ $result = $this->rc->db->query(sprintf(
+ "SELECT * FROM " . $this->db_events . "
+ WHERE calendar_id IN (%s)
+ AND notifyat <= %s AND %s > %s",
+ join(',', $calendar_ids),
+ $this->rc->db->fromunixtime($time),
+ $this->rc->db->quote_identifier('end'),
+ $this->rc->db->fromunixtime($time)
+ ));
+
+ while ($result && ($event = $this->rc->db->fetch_assoc($result)))
+ $alarms[] = $this->_read_postprocess($event);
+ }
+
+ return $alarms;
+ }
+
+ /**
+ * Feedback after showing/sending an alarm notification
+ *
+ * @see calendar_driver::dismiss_alarm()
+ */
+ public function dismiss_alarm($event_id, $snooze = 0)
+ {
+ // set new notifyat time or unset if not snoozed
+ $notify_at = $snooze > 0 ? date(self::DB_DATE_FORMAT, time() + $snooze) : null;
+
+ $query = $this->rc->db->query(sprintf(
+ "UPDATE " . $this->db_events . "
+ SET changed=%s, notifyat=?
+ WHERE event_id=?
+ AND calendar_id IN (" . $this->calendar_ids . ")",
+ $this->rc->db->now()),
+ $notify_at,
+ $event_id
+ );
+
+ return $this->rc->db->affected_rows($query);
+ }
+
+ /**
+ * Save an attachment related to the given event
+ */
+ private function add_attachment($attachment, $event_id)
+ {
+ $data = $attachment['data'] ? $attachment['data'] : file_get_contents($attachment['path']);
+
+ $query = $this->rc->db->query(
+ "INSERT INTO " . $this->db_attachments .
+ " (event_id, filename, mimetype, size, data)" .
+ " VALUES (?, ?, ?, ?, ?)",
+ $event_id,
+ $attachment['name'],
+ $attachment['mimetype'],
+ strlen($data),
+ base64_encode($data)
+ );
+
+ return $this->rc->db->affected_rows($query);
+ }
+
+ /**
+ * Remove a specific attachment from the given event
+ */
+ private function remove_attachment($attachment_id, $event_id)
+ {
+ $query = $this->rc->db->query(
+ "DELETE FROM " . $this->db_attachments .
+ " WHERE attachment_id = ?" .
+ " AND event_id IN (SELECT event_id FROM " . $this->db_events .
+ " WHERE event_id = ?" .
+ " AND calendar_id IN (" . $this->calendar_ids . "))",
+ $attachment_id,
+ $event_id
+ );
+
+ return $this->rc->db->affected_rows($query);
+ }
+
+ /**
+ * List attachments of specified event
+ */
+ public function list_attachments($event)
+ {
+ $attachments = array();
+
+ if (!empty($this->calendar_ids)) {
+ $result = $this->rc->db->query(
+ "SELECT attachment_id AS id, filename AS name, mimetype, size " .
+ " FROM " . $this->db_attachments .
+ " WHERE event_id IN (SELECT event_id FROM " . $this->db_events .
+ " WHERE event_id=?" .
+ " AND calendar_id IN (" . $this->calendar_ids . "))" .
+ " ORDER BY filename",
+ $event['recurrence_id'] ? $event['recurrence_id'] : $event['event_id']
+ );
+
+ while ($result && ($arr = $this->rc->db->fetch_assoc($result))) {
+ $attachments[] = $arr;
+ }
+ }
+
+ return $attachments;
+ }
+
+ /**
+ * Get attachment properties
+ */
+ public function get_attachment($id, $event)
+ {
+ if (!empty($this->calendar_ids)) {
+ $result = $this->rc->db->query(
+ "SELECT attachment_id AS id, filename AS name, mimetype, size " .
+ " FROM " . $this->db_attachments .
+ " WHERE attachment_id=?" .
+ " AND event_id=?",
+ $id,
+ $event['recurrence_id'] ? $event['recurrence_id'] : $event['id']
+ );
+
+ if ($result && ($arr = $this->rc->db->fetch_assoc($result))) {
+ return $arr;
+ }
+ }
+
+ return null;
+ }
+
+ /**
+ * Get attachment body
+ */
+ public function get_attachment_body($id, $event)
+ {
+ if (!empty($this->calendar_ids)) {
+ $result = $this->rc->db->query(
+ "SELECT data " .
+ " FROM " . $this->db_attachments .
+ " WHERE attachment_id=?" .
+ " AND event_id=?",
+ $id,
+ $event['id']
+ );
+
+ if ($result && ($arr = $this->rc->db->fetch_assoc($result))) {
+ return base64_decode($arr['data']);
+ }
+ }
+
+ return null;
+ }
+
+ /**
+ * Remove the given category
+ */
+ public function remove_category($name)
+ {
+ $query = $this->rc->db->query(
+ "UPDATE " . $this->db_events . "
+ SET categories=''
+ WHERE categories=?
+ AND calendar_id IN (" . $this->calendar_ids . ")",
+ $name
+ );
+
+ return $this->rc->db->affected_rows($query);
+ }
+
+ /**
+ * Update/replace a category
+ */
+ public function replace_category($oldname, $name, $color)
+ {
+ $query = $this->rc->db->query(
+ "UPDATE " . $this->db_events . "
+ SET categories=?
+ WHERE categories=?
+ AND calendar_id IN (" . $this->calendar_ids . ")",
+ $name,
+ $oldname
+ );
+
+ return $this->rc->db->affected_rows($query);
+ }
+
+ /**
+ * Helper method to serialize the list of alarms into a string
+ */
+ private function serialize_alarms($valarms)
+ {
+ foreach ((array)$valarms as $i => $alarm) {
+ if ($alarm['trigger'] instanceof DateTime) {
+ $valarms[$i]['trigger'] = '@' . $alarm['trigger']->format('c');
+ }
+ }
+
+ return $valarms ? json_encode($valarms) : null;
+ }
+
+ /**
+ * Helper method to decode a serialized list of alarms
+ */
+ private function unserialize_alarms($alarms)
+ {
+ // decode json serialized alarms
+ if ($alarms && $alarms[0] == '[') {
+ $valarms = json_decode($alarms, true);
+ foreach ($valarms as $i => $alarm) {
+ if ($alarm['trigger'][0] == '@') {
+ try {
+ $valarms[$i]['trigger'] = new DateTime(substr($alarm['trigger'], 1));
+ } catch (Exception $e) {
+ unset($valarms[$i]);
+ }
+ }
+ }
+ } // convert legacy alarms data
+ else if (strlen($alarms)) {
+ list($trigger, $action) = explode(':', $alarms, 2);
+ if ($trigger = libcalendaring::parse_alaram_value($trigger)) {
+ $valarms = array(array('action' => $action, 'trigger' => $trigger[3] ?: $trigger[0]));
+ }
+ }
+
+ return $valarms;
+ }
+
+ /**
+ * Helper method to decode the attendees list from string
+ */
+ private function unserialize_attendees($s_attendees)
+ {
+ $attendees = array();
+
+ // decode json serialized string
+ if ($s_attendees[0] == '[') {
+ $attendees = json_decode($s_attendees, true);
+ } // decode the old serialization format
+ else {
+ foreach (explode("\n", $event['attendees']) as $line) {
+ $att = array();
+ foreach (rcube_utils::explode_quoted_string(';', $line) as $prop) {
+ list($key, $value) = explode("=", $prop);
+ $att[strtolower($key)] = stripslashes(trim($value, '""'));
+ }
+ $attendees[] = $att;
+ }
+ }
+
+ return $attendees;
+ }
+
+ /**
+ * Handler for user_delete plugin hook
+ */
+ public function user_delete($args)
+ {
+ $db = $this->rc->db;
+ $user = $args['user'];
+ $event_ids = array();
+
+ $events = $db->query(
+ "SELECT event_id FROM " . $this->db_events . " AS ev" .
+ " LEFT JOIN " . $this->db_calendars . " cal ON (ev.calendar_id = cal.calendar_id)" .
+ " WHERE user_id=?",
+ $user->ID);
+
+ while ($row = $db->fetch_assoc($events)) {
+ $event_ids[] = $row['event_id'];
+ }
+
+ if (!empty($event_ids)) {
+ foreach (array($this->db_attachments, $this->db_events) as $table) {
+ $db->query(sprintf("DELETE FROM $table WHERE event_id IN (%s)", join(',', $event_ids)));
+ }
+ }
+
+ foreach (array($this->db_calendars, 'itipinvitations') as $table) {
+ $db->query("DELETE FROM $table WHERE user_id=?", $user->ID);
+ }
+ }
+
+ /**
+ * Callback function to produce driver-specific calendar create/edit form
+ *
+ * @param string Request action 'form-edit|form-new'
+ * @param array Calendar properties (e.g. id, color)
+ * @param array Edit form fields
+ *
+ * @return string HTML content of the form
+ */
+ public function calendar_form($action, $calendar, $formfields)
+ {
+ // Make sure we have current attributes
+ $calendar = $this->calendars[$calendar["id"]];
+
+ $input_ical_url = new html_inputfield(array(
+ "name" => "ical_url",
+ "id" => "ical_url",
+ "size" => 20
+ ));
+
+ $formfields["ical_url"] = array(
+ "label" => $this->cal->gettext("icalurl"),
+ "value" => $input_ical_url->show($calendar["ical_url"]),
+ "id" => "ical_url",
+ );
+
+ $input_ical_user = new html_inputfield( array(
+ "name" => "ical_user",
+ "id" => "ical_user",
+ "size" => 20
+ ));
+
+ $formfields["ical_user"] = array(
+ "label" => $this->cal->gettext("username"),
+ "value" => $input_ical_user->show($calendar["ical_user"]),
+ "id" => "ical_user",
+ );
+
+ $input_ical_pass = new html_passwordfield( array(
+ "name" => "ical_pass",
+ "id" => "ical_pass",
+ "size" => 20
+ ));
+
+ $formfields["ical_pass"] = array(
+ "label" => $this->cal->gettext("password"),
+ "value" => $input_ical_pass->show(null), // Don't send plain text password to GUI
+ "id" => "ical_pass",
+ );
+
+ return parent::calendar_form($action, $calendar, $formfields);
+ }
+
+ /**
+ * Determines whether the given calendar is in sync regarding the configured sync period.
+ *
+ * @param int Calender id.
+ * @return boolean True if calendar is in sync, true otherwise.
+ */
+ private function _is_synced($cal_id)
+ {
+ // Atomic sql: Check for exceeded sync period and update last_change.
+ $query = $this->rc->db->query(
+ "UPDATE ".$this->db_calendars." ".
+ "SET ical_last_change = NOW() WHERE calendar_id = ? AND ".
+ $this->_unix_timestamp('ical_last_change') ." + ? <= ".$this->_unix_timestamp('NOW()'),
+ $cal_id, $this->sync_period);
+
+ if($query->rowCount() > 0)
+ {
+ $is_synced = $this->sync_clients[$cal_id]->is_synced();
+ self::debug_log("Calendar \"$cal_id\" ".($is_synced ? "is in sync" : "needs update").".");
+ return $is_synced;
+ }
+ else
+ {
+ self::debug_log("Sync period active: Assuming calendar \"$cal_id\" to be in sync.");
+ return true;
+ }
+ }
+
+ /**
+ * Returns db-specific timestamp queries for epoch format
+ *
+ * @param str column name or valid timestamp (e.g. NOW())
+ * @return str db-specific timestamp query for epoch format
+ */
+ private function _unix_timestamp($field)
+ {
+ switch ($this->rc->db->db_provider) {
+ case 'postgres':
+ return "EXTRACT (EPOCH FROM $field)";
+ default:
+ return "UNIX_TIMESTAMP($field)";
+ }
+ }
+
+ /**
+ * Encodes directory- and filenames using rawurlencode().
+ *
+ * @see http://stackoverflow.com/questions/7973790/urlencode-only-the-directory-and-file-names-of-a-url
+ * @param string Unencoded URL to be encoded.
+ * @return Encoded URL.
+ */
+ private static function _encode_url($url)
+ {
+ // Don't encode if "%" is already used.
+ if (strstr($url, "%") === false) {
+ return preg_replace_callback('#://([^/]+)/([^?]+)#', function ($matches) {
+ return '://' . $matches[1] . '/' . join('/', array_map('rawurlencode', explode('/', $matches[2])));
+ }, $url);
+ } else return $url;
+ }
+
+ /**
+ * Performs iCAL updates on given events.
+ *
+ * @param array ical and event properties to update. See ical_sync::get_updates().
+ * @return array List of event ids.
+ */
+ private function _perform_updates($updates)
+ {
+ $event_ids = array();
+
+ $num_created = 0;
+ $num_updated = 0;
+
+ foreach ($updates as $update) {
+ // local event -> update event
+ if (isset($update["local_event"])) {
+ // let edit_event() do all the magic
+ if ($this->_db_edit_event($update["remote_event"] + (array)$update["local_event"])) {
+
+ $event_id = $update["local_event"]["id"];
+ array_push($event_ids, $event_id);
+
+ $num_updated++;
+
+ self::debug_log("Updated event \"$event_id\".");
+
+ } else {
+ self::debug_log("Could not perform event update: " . print_r($update, true));
+ }
+ } // no local event -> create event
+ else {
+ $event_id = $this->_db_new_event($update["remote_event"]);
+ if ($event_id) {
+
+ array_push($event_ids, $event_id);
+
+ $num_created++;
+
+ self::debug_log("Created event \"$event_id\".");
+
+ } else {
+ self::debug_log("Could not perform event creation: " . print_r($update, true));
+ }
+ }
+ }
+
+ self::debug_log("Created $num_created new events, updated $num_updated event.");
+ return $event_ids;
+ }
+
+ /**
+ * Return all events from the given calendar.
+ *
+ * @param int Calendar id.
+ * @return array
+ */
+ private function _load_all_events($cal_id)
+ {
+ // FIXME: This is kind of ugly but a way to get _all_ events without touching the database driver.
+
+ // Get the event with the maximum end time.
+ $result = $this->rc->db->query(
+ "SELECT MAX(e.end) as end FROM " . $this->db_events . " e " .
+ "WHERE e.calendar_id = ? ", $cal_id);
+
+ if ($result && ($arr = $this->rc->db->fetch_assoc($result))) {
+ $end = new DateTime($arr["end"]);
+
+ // Don't use load_events() which is doing another sync while this method might be already invoked in an sync.
+ return $this->_db_load_events(0, $end->getTimestamp(), null, array($cal_id));
+ }
+ else return array();
+ }
+
+ /**
+ * Synchronizes events of given calendar.
+ *
+ * @param int Calendar id.
+ */
+ private function _sync_calendar($cal_id)
+ {
+ self::debug_log("Syncing calendar id \"$cal_id\".");
+
+ $cal_sync = $this->sync_clients[$cal_id];
+ $events = array();
+
+ // Ignore recurrence events
+ foreach ($this->_load_all_events($cal_id) as $event) {
+ if ($event["recurrence_id"] == 0) {
+ array_push($events, $event);
+ }
+ }
+
+ $updates = $cal_sync->get_updates($events);
+ if($updates)
+ {
+ list($updates, $synced_event_ids) = $updates;
+ $updated_event_ids = $this->_perform_updates($updates);
+
+ // Delete events that are not in sync or updated.
+ foreach ($events as $event) {
+ if (array_search($event["id"], $updated_event_ids) === false &&
+ array_search($event["id"], $synced_event_ids) === false)
+ {
+ // Assume: Event was not updated, so delete!
+ $this->_db_remove_event($event, true);
+ self::debug_log("Remove event \"" . $event["id"] . "\".");
+ }
+ }
+ }
+
+ self::debug_log("Successfully synced calendar id \"$cal_id\".");
+ }
+
+ private function _decrypt_pass($pass) {
+ $p = base64_decode($pass);
+ $e = new Encryption(MCRYPT_BlOWFISH, MCRYPT_MODE_CBC);
+ return $e->decrypt($p, $this->crypt_key);
+ }
+
+ private function _encrypt_pass($pass) {
+ $e = new Encryption(MCRYPT_BlOWFISH, MCRYPT_MODE_CBC);
+ $p = $e->encrypt($pass, $this->crypt_key);
+ return base64_encode($p);
+ }
+}
diff --git a/calendar/drivers/ical/ical_sync.php b/calendar/drivers/ical/ical_sync.php
new file mode 100644
index 0000000..2b50af1
--- /dev/null
+++ b/calendar/drivers/ical/ical_sync.php
@@ -0,0 +1,125 @@
+<?php
+/**
+ * iCalendar sync for the Calendar plugin
+ *
+ * @version @package_version@
+ * @author Daniel Morlock <daniel.morlock@awesome-it.de>
+ *
+ * Copyright (C) Awesome IT GbR <info@awesome-it.de>
+ *
+ * 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.
+ *
+ * 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
+ * GNU Affero General Public License for more details.
+ *
+ * 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/>.
+ */
+
+class ical_sync
+{
+ const ACTION_NONE = 1;
+ const ACTION_UPDATE = 2;
+ const ACTION_CREATE = 4;
+
+ private $cal_id = null;
+ private $url = null;
+ private $user = null;
+ private $pass = null;
+ private $ical = null;
+
+ /**
+ * Default constructor for calendar synchronization adapter.
+ *
+ * @param int Calendar id.
+ * @param array Hash array with ical properties:
+ * url: Absolute URL to iCAL resource.
+ */
+ public function __construct($cal_id, $props)
+ {
+ $this->ical = libcalendaring::get_ical();
+ $this->cal_id = $cal_id;
+
+ $this->url = $props["ical_url"];
+ $this->user = isset($props["ical_user"]) ? $props["ical_user"] : null;
+ $this->pass = isset($props["ical_pass"]) ? $props["ical_pass"] : null;
+ }
+
+ /**
+ * Determines whether current calendar needs to be synced.
+ *
+ * @return True if the current calendar needs to be synced, false otherwise.
+ */
+ public function is_synced()
+ {
+ // No change to check that so far.
+ return false;
+ }
+
+ /**
+ * Fetches events from iCAL resource and returns updates.
+ *
+ * @param array List of local events.
+ * @return array Tuple containing the following lists:
+ *
+ * Hash list for iCAL events to be created or to be updated with the keys:
+ * local_event: The local event in case of an update.
+ * remote_event: The current event retrieved from caldav server.
+ *
+ * A list of event ids that are in sync.
+ */
+ public function get_updates($events)
+ {
+ $context = null;
+ if($this->user != null && $this->pass != null)
+ {
+ $context = stream_context_create(array(
+ 'http' => array(
+ 'header' => "Authorization: Basic " . base64_encode("$this->user:$this->pass")
+ )
+ ));
+ }
+
+ $vcal = file_get_contents($this->url, false, $context);
+ $updates = array();
+ $synced = array();
+ if($vcal !== false) {
+
+ // Hash existing events by uid.
+ $events_hash = array();
+ foreach($events as $event) {
+ $events_hash[$event['uid']] = $event;
+ }
+
+ foreach ($this->ical->import($vcal) as $remote_event) {
+
+ // Attach remote event to current calendar
+ $remote_event["calendar"] = $this->cal_id;
+
+ $local_event = null;
+ if($events_hash[$remote_event['uid']])
+ $local_event = $events_hash[$remote_event['uid']];
+
+ // Determine whether event don't need an update.
+ if($local_event && $local_event["changed"] >= $remote_event["changed"])
+ {
+ array_push($synced, $local_event["id"]);
+ }
+ else
+ {
+ array_push($updates, array('local_event' => $local_event, 'remote_event' => $remote_event));
+ }
+ }
+ }
+
+ return array($updates, $synced);
+ }
+}
+
+;
+?> \ No newline at end of file
diff --git a/calendar/drivers/kolab/SQL/mysql.initial.sql b/calendar/drivers/kolab/SQL/mysql.initial.sql
new file mode 100644
index 0000000..d500961
--- /dev/null
+++ b/calendar/drivers/kolab/SQL/mysql.initial.sql
@@ -0,0 +1,32 @@
+/**
+ * Roundcube Calendar Kolab backend
+ *
+ * @version @package_version@
+ * @author Thomas Bruederli
+ * @licence GNU AGPL
+ **/
+
+CREATE TABLE IF NOT EXISTS `kolab_alarms` (
+ `alarm_id` VARCHAR(255) NOT NULL,
+ `user_id` int(10) UNSIGNED NOT NULL,
+ `notifyat` DATETIME DEFAULT NULL,
+ `dismissed` TINYINT(3) UNSIGNED NOT NULL DEFAULT '0',
+ PRIMARY KEY(`alarm_id`,`user_id`),
+ CONSTRAINT `fk_kolab_alarms_user_id` FOREIGN KEY (`user_id`)
+ REFERENCES `users`(`user_id`) ON DELETE CASCADE ON UPDATE CASCADE
+) /*!40000 ENGINE=INNODB */;
+
+CREATE TABLE IF NOT EXISTS `itipinvitations` (
+ `token` VARCHAR(64) NOT NULL,
+ `event_uid` VARCHAR(255) NOT NULL,
+ `user_id` int(10) UNSIGNED NOT NULL DEFAULT '0',
+ `event` TEXT NOT NULL,
+ `expires` DATETIME DEFAULT NULL,
+ `cancelled` TINYINT(3) UNSIGNED NOT NULL DEFAULT '0',
+ PRIMARY KEY(`token`),
+ INDEX `uid_idx` (`event_uid`,`user_id`),
+ CONSTRAINT `fk_itipinvitations_user_id` FOREIGN KEY (`user_id`)
+ REFERENCES `users`(`user_id`) ON DELETE CASCADE ON UPDATE CASCADE
+) /*!40000 ENGINE=INNODB */ /*!40101 CHARACTER SET utf8 COLLATE utf8_general_ci */;
+
+REPLACE INTO system (name, value) VALUES ('calendar-kolab-version', '2014041700');
diff --git a/calendar/drivers/kolab/SQL/mysql/2012080600.sql b/calendar/drivers/kolab/SQL/mysql/2012080600.sql
new file mode 100644
index 0000000..5c9f1ae
--- /dev/null
+++ b/calendar/drivers/kolab/SQL/mysql/2012080600.sql
@@ -0,0 +1,11 @@
+DROP TABLE IF EXISTS `kolab_alarms`;
+
+CREATE TABLE `kolab_alarms` (
+ `event_id` VARCHAR(255) NOT NULL,
+ `user_id` int(10) UNSIGNED NOT NULL,
+ `notifyat` DATETIME DEFAULT NULL,
+ `dismissed` TINYINT(3) UNSIGNED NOT NULL DEFAULT '0',
+ PRIMARY KEY(`event_id`),
+ CONSTRAINT `fk_kolab_alarms_user_id` FOREIGN KEY (`user_id`)
+ REFERENCES `users`(`user_id`) ON DELETE CASCADE ON UPDATE CASCADE
+) /*!40000 ENGINE=INNODB */;
diff --git a/calendar/drivers/kolab/SQL/mysql/2013011000.sql b/calendar/drivers/kolab/SQL/mysql/2013011000.sql
new file mode 100644
index 0000000..fe6741a
--- /dev/null
+++ b/calendar/drivers/kolab/SQL/mysql/2013011000.sql
@@ -0,0 +1 @@
+-- empty \ No newline at end of file
diff --git a/calendar/drivers/kolab/SQL/mysql/2014041700.sql b/calendar/drivers/kolab/SQL/mysql/2014041700.sql
new file mode 100644
index 0000000..9175b55
--- /dev/null
+++ b/calendar/drivers/kolab/SQL/mysql/2014041700.sql
@@ -0,0 +1 @@
+ALTER TABLE `kolab_alarms` CHANGE `event_id` `alarm_id` VARCHAR(255) NOT NULL; \ No newline at end of file
diff --git a/calendar/drivers/kolab/SQL/mysql/2014082600.sql b/calendar/drivers/kolab/SQL/mysql/2014082600.sql
new file mode 100644
index 0000000..501eb5c
--- /dev/null
+++ b/calendar/drivers/kolab/SQL/mysql/2014082600.sql
@@ -0,0 +1,2 @@
+ALTER TABLE `kolab_alarms` DROP PRIMARY KEY;
+ALTER TABLE `kolab_alarms` ADD PRIMARY KEY (`alarm_id`, `user_id`);
diff --git a/calendar/drivers/kolab/SQL/oracle.initial.sql b/calendar/drivers/kolab/SQL/oracle.initial.sql
new file mode 100644
index 0000000..d6d882b
--- /dev/null
+++ b/calendar/drivers/kolab/SQL/oracle.initial.sql
@@ -0,0 +1,31 @@
+/**
+ * Roundcube Calendar Kolab backend
+ *
+ * @author Aleksander Machniak
+ * @licence GNU AGPL
+ **/
+
+CREATE TABLE "kolab_alarms" (
+ "alarm_id" varchar(255) NOT NULL PRIMARY KEY,
+ "user_id" integer NOT NULL
+ REFERENCES "users" ("user_id") ON DELETE CASCADE,
+ "notifyat" timestamp DEFAULT NULL,
+ "dismissed" smallint DEFAULT 0 NOT NULL
+);
+
+CREATE INDEX "kolab_alarms_user_id_idx" ON "kolab_alarms" ("user_id");
+
+
+CREATE TABLE "itipinvitations" (
+ "token" varchar(64) NOT NULL PRIMARY KEY,
+ "event_uid" varchar(255) NOT NULL,
+ "user_id" integer NOT NULL
+ REFERENCES "users" ("user_id") ON DELETE CASCADE,
+ "event" long NOT NULL,
+ "expires" timestamp DEFAULT NULL,
+ "cancelled" smallint DEFAULT 0 NOT NULL
+);
+
+CREATE INDEX "itipinvitations_user_id_idx" ON "itipinvitations" ("user_id", "event_uid");
+
+INSERT INTO "system" ("name", "value") VALUES ('calendar-kolab-version', '2014041700');
diff --git a/calendar/drivers/kolab/SQL/postgres.initial.sql b/calendar/drivers/kolab/SQL/postgres.initial.sql
new file mode 100644
index 0000000..11dff41
--- /dev/null
+++ b/calendar/drivers/kolab/SQL/postgres.initial.sql
@@ -0,0 +1,32 @@
+/**
+ * Roundcube Calendar Kolab backend
+ *
+ * @author Sergey Sidlyarenko
+ * @licence GNU AGPL
+ **/
+
+CREATE TABLE IF NOT EXISTS kolab_alarms (
+ alarm_id character varying(255) NOT NULL,
+ user_id integer NOT NULL
+ REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE CASCADE,
+ notifyat timestamp without time zone DEFAULT NULL,
+ dismissed smallint NOT NULL DEFAULT 0,
+ PRIMARY KEY(alarm_id)
+);
+
+CREATE INDEX kolab_alarms_user_id_idx ON kolab_alarms (user_id);
+
+CREATE TABLE IF NOT EXISTS itipinvitations (
+ token character varying(64) NOT NULL,
+ event_uid character varying(255) NOT NULL,
+ user_id integer NOT NULL
+ REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE CASCADE,
+ event text NOT NULL,
+ expires timestamp without time zone DEFAULT NULL,
+ cancelled smallint NOT NULL DEFAULT 0,
+ PRIMARY KEY(token)
+);
+
+CREATE INDEX itipinvitations_user_id_event_uid_idx ON itipinvitations (user_id, event_uid);
+
+INSERT INTO system (name, value) VALUES ('calendar-kolab-version', '2014041700');
diff --git a/calendar/drivers/kolab/kolab_calendar.php b/calendar/drivers/kolab/kolab_calendar.php
new file mode 100644
index 0000000..19a03e1
--- /dev/null
+++ b/calendar/drivers/kolab/kolab_calendar.php
@@ -0,0 +1,836 @@
+<?php
+
+/**
+ * Kolab calendar storage class
+ *
+ * @version @package_version@
+ * @author Thomas Bruederli <bruederli@kolabsys.com>
+ * @author Aleksander Machniak <machniak@kolabsys.com>
+ *
+ * Copyright (C) 2012-2015, Kolab Systems AG <contact@kolabsys.com>
+ *
+ * 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.
+ *
+ * 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
+ * GNU Affero General Public License for more details.
+ *
+ * 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/>.
+ */
+
+
+class kolab_calendar extends kolab_storage_folder_api
+{
+ public $ready = false;
+ public $rights = 'lrs';
+ public $editable = false;
+ public $attachments = true;
+ public $alarms = false;
+ public $history = false;
+ public $subscriptions = true;
+ public $categories = array();
+ public $storage;
+
+ public $type = 'event';
+
+ protected $cal;
+ protected $events = array();
+ protected $search_fields = array('title', 'description', 'location', 'attendees');
+
+ /**
+ * Factory method to instantiate a kolab_calendar object
+ *
+ * @param string Calendar ID (encoded IMAP folder name)
+ * @param object calendar plugin object
+ * @return object kolab_calendar instance
+ */
+ public static function factory($id, $calendar)
+ {
+ $imap = $calendar->rc->get_storage();
+ $imap_folder = kolab_storage::id_decode($id);
+ $info = $imap->folder_info($imap_folder, true);
+ if (empty($info) || $info['noselect'] || strpos(kolab_storage::folder_type($imap_folder), 'event') !== 0) {
+ return new kolab_user_calendar($imap_folder, $calendar);
+ }
+ else {
+ return new kolab_calendar($imap_folder, $calendar);
+ }
+ }
+
+ /**
+ * Default constructor
+ */
+ public function __construct($imap_folder, $calendar)
+ {
+ $this->cal = $calendar;
+ $this->imap = $calendar->rc->get_storage();
+ $this->name = $imap_folder;
+
+ // ID is derrived from folder name
+ $this->id = kolab_storage::folder_id($this->name, true);
+ $old_id = kolab_storage::folder_id($this->name, false);
+
+ // fetch objects from the given IMAP folder
+ $this->storage = kolab_storage::get_folder($this->name);
+ $this->ready = $this->storage && $this->storage->valid;
+
+ // Set writeable and alarms flags according to folder permissions
+ if ($this->ready) {
+ if ($this->storage->get_namespace() == 'personal') {
+ $this->editable = true;
+ $this->rights = 'lrswikxteav';
+ $this->alarms = true;
+ }
+ else {
+ $rights = $this->storage->get_myrights();
+ if ($rights && !PEAR::isError($rights)) {
+ $this->rights = $rights;
+ if (strpos($rights, 't') !== false || strpos($rights, 'd') !== false)
+ $this->editable = strpos($rights, 'i');;
+ }
+ }
+
+ // user-specific alarms settings win
+ $prefs = $this->cal->rc->config->get('kolab_calendars', array());
+ if (isset($prefs[$this->id]['showalarms']))
+ $this->alarms = $prefs[$this->id]['showalarms'];
+ else if (isset($prefs[$old_id]['showalarms']))
+ $this->alarms = $prefs[$old_id]['showalarms'];
+ }
+
+ $this->default = $this->storage->default;
+ $this->subtype = $this->storage->subtype;
+ }
+
+
+ /**
+ * Getter for the IMAP folder name
+ *
+ * @return string Name of the IMAP folder
+ */
+ public function get_realname()
+ {
+ return $this->name;
+ }
+
+ /**
+ *
+ */
+ public function get_title()
+ {
+ return null;
+ }
+
+
+ /**
+ * Return color to display this calendar
+ */
+ public function get_color()
+ {
+ // color is defined in folder METADATA
+ if ($color = $this->storage->get_color()) {
+ return $color;
+ }
+
+ // calendar color is stored in user prefs (temporary solution)
+ $prefs = $this->cal->rc->config->get('kolab_calendars', array());
+
+ if (!empty($prefs[$this->id]) && !empty($prefs[$this->id]['color']))
+ return $prefs[$this->id]['color'];
+
+ return 'cc0000';
+ }
+
+ /**
+ * Compose an URL for CalDAV access to this calendar (if configured)
+ */
+ public function get_caldav_url()
+ {
+ if ($template = $this->cal->rc->config->get('calendar_caldav_url', null)) {
+ return strtr($template, array(
+ '%h' => $_SERVER['HTTP_HOST'],
+ '%u' => urlencode($this->cal->rc->get_user_name()),
+ '%i' => urlencode($this->storage->get_uid()),
+ '%n' => urlencode($this->name),
+ ));
+ }
+
+ return false;
+ }
+
+
+ /**
+ * Update properties of this calendar folder
+ *
+ * @see calendar_driver::edit_calendar()
+ */
+ public function update(&$prop)
+ {
+ $prop['oldname'] = $this->get_realname();
+ $newfolder = kolab_storage::folder_update($prop);
+
+ if ($newfolder === false) {
+ $this->cal->last_error = $this->cal->gettext(kolab_storage::$last_error);
+ return false;
+ }
+
+ // create ID
+ return kolab_storage::folder_id($newfolder);
+ }
+
+ /**
+ * Getter for a single event object
+ */
+ public function get_event($id)
+ {
+ // directly access storage object
+ if (!$this->events[$id] && ($record = $this->storage->get_object($id)))
+ $this->events[$id] = $this->_to_driver_event($record, true);
+
+ // event not found, maybe a recurring instance is requested
+ if (!$this->events[$id]) {
+ $master_id = preg_replace('/-\d+(T\d{6})?$/', '', $id);
+ $instance_id = substr($id, strlen($master_id) + 1);
+
+ if ($master_id != $id && ($record = $this->storage->get_object($master_id))) {
+ $master = $this->_to_driver_event($record);
+ }
+
+ // check for match in top-level exceptions (aka loose single occurrences)
+ if ($master && $master['_formatobj'] && ($instance = $master['_formatobj']->get_instance($instance_id))) {
+ $this->events[$id] = $this->_to_driver_event($instance);
+ }
+ // check for match on the first instance already
+ else if ($master['_instance'] && $master['_instance'] == $instance_id) {
+ $this->events[$id] = $master;
+ }
+ else if ($master && is_array($master['recurrence'])) {
+ $this->get_recurring_events($record, $master['start'], null, $id);
+ }
+ }
+
+ return $this->events[$id];
+ }
+
+ /**
+ * Get attachment body
+ * @see calendar_driver::get_attachment_body()
+ */
+ public function get_attachment_body($id, $event)
+ {
+ if (!$this->ready)
+ return false;
+
+ $data = $this->storage->get_attachment($event['id'], $id);
+
+ if ($data == null) {
+ // try again with master UID
+ $uid = preg_replace('/-\d+(T\d{6})?$/', '', $event['id']);
+ if ($uid != $event['id']) {
+ $data = $this->storage->get_attachment($uid, $id);
+ }
+ }
+
+ return $data;
+ }
+
+ /**
+ * @param integer Event's new start (unix timestamp)
+ * @param integer Event's new end (unix timestamp)
+ * @param string Search query (optional)
+ * @param boolean Include virtual events (optional)
+ * @param array Additional parameters to query storage
+ * @param array Additional query to filter events
+ * @return array A list of event records
+ */
+ public function list_events($start, $end, $search = null, $virtual = 1, $query = array(), $filter_query = null)
+ {
+ // convert to DateTime for comparisons
+ try {
+ $start = new DateTime('@'.$start);
+ }
+ catch (Exception $e) {
+ $start = new DateTime('@0');
+ }
+ try {
+ $end = new DateTime('@'.$end);
+ }
+ catch (Exception $e) {
+ $end = new DateTime('today +10 years');
+ }
+
+ // get email addresses of the current user
+ $user_emails = $this->cal->get_user_emails();
+
+ // query Kolab storage
+ $query[] = array('dtstart', '<=', $end);
+ $query[] = array('dtend', '>=', $start);
+
+ if (is_array($filter_query)) {
+ $query = array_merge($query, $filter_query);
+ }
+
+ if (!empty($search)) {
+ $search = mb_strtolower($search);
+ $words = rcube_utils::tokenize_string($search, 1);
+ foreach (rcube_utils::normalize_string($search, true) as $word) {
+ $query[] = array('words', 'LIKE', $word);
+ }
+ }
+ else {
+ $words = array();
+ }
+
+ // set partstat filter to skip pending and declined invitations
+ if (empty($filter_query) && $this->get_namespace() != 'other') {
+ $partstat_exclude = array('NEEDS-ACTION','DECLINED');
+ }
+ else {
+ $partstat_exclude = array();
+ }
+
+ $events = array();
+ foreach ($this->storage->select($query) as $record) {
+ $event = $this->_to_driver_event($record, !$virtual);
+
+ // remember seen categories
+ if ($event['categories']) {
+ $cat = is_array($event['categories']) ? $event['categories'][0] : $event['categories'];
+ $this->categories[$cat]++;
+ }
+
+ // list events in requested time window
+ if ($event['start'] <= $end && $event['end'] >= $start) {
+ unset($event['_attendees']);
+ $add = true;
+
+ // skip the first instance of a recurring event if listed in exdate
+ if ($virtual && !empty($event['recurrence']['EXDATE'])) {
+ $event_date = $event['start']->format('Ymd');
+ $exdates = (array)$event['recurrence']['EXDATE'];
+
+ foreach ($exdates as $exdate) {
+ if ($exdate->format('Ymd') == $event_date) {
+ $add = false;
+ break;
+ }
+ }
+ }
+
+ // find and merge exception for the first instance
+ if ($virtual && !empty($event['recurrence']) && is_array($event['recurrence']['EXCEPTIONS'])) {
+ foreach ($event['recurrence']['EXCEPTIONS'] as $exception) {
+ if ($event['_instance'] == $exception['_instance']) {
+ // clone date objects from main event before adjusting them with exception data
+ if (is_object($event['start'])) $event['start'] = clone $record['start'];
+ if (is_object($event['end'])) $event['end'] = clone $record['end'];
+ kolab_driver::merge_exception_data($event, $exception);
+ }
+ }
+ }
+
+ if ($add)
+ $events[] = $event;
+ }
+
+ // resolve recurring events
+ if ($record['recurrence'] && $virtual == 1) {
+ $events = array_merge($events, $this->get_recurring_events($record, $start, $end));
+ }
+ // add top-level exceptions (aka loose single occurrences)
+ else if (is_array($record['exceptions'])) {
+ foreach ($record['exceptions'] as $ex) {
+ $component = $this->_to_driver_event($ex);
+ if ($component['start'] <= $end && $component['end'] >= $start) {
+ $events[] = $component;
+ }
+ }
+ }
+ }
+
+ // post-filter all events by fulltext search and partstat values
+ $me = $this;
+ $events = array_filter($events, function($event) use ($words, $partstat_exclude, $user_emails, $me) {
+ // fulltext search
+ if (count($words)) {
+ $hits = 0;
+ foreach ($words as $word) {
+ $hits += $me->fulltext_match($event, $word, false);
+ }
+ if ($hits < count($words)) {
+ return false;
+ }
+ }
+
+ // partstat filter
+ if (count($partstat_exclude) && is_array($event['attendees'])) {
+ foreach ($event['attendees'] as $attendee) {
+ if (in_array($attendee['email'], $user_emails) && in_array($attendee['status'], $partstat_exclude)) {
+ return false;
+ }
+ }
+ }
+
+ return true;
+ });
+
+ // avoid session race conditions that will loose temporary subscriptions
+ $this->cal->rc->session->nowrite = true;
+
+ return $events;
+ }
+
+ /**
+ *
+ * @param integer Date range start (unix timestamp)
+ * @param integer Date range end (unix timestamp)
+ * @param array Additional query to filter events
+ * @return integer Count
+ */
+ public function count_events($start, $end = null, $filter_query = null)
+ {
+ // convert to DateTime for comparisons
+ try {
+ $start = new DateTime('@'.$start);
+ }
+ catch (Exception $e) {
+ $start = new DateTime('@0');
+ }
+ if ($end) {
+ try {
+ $end = new DateTime('@'.$end);
+ }
+ catch (Exception $e) {
+ $end = null;
+ }
+ }
+
+ // query Kolab storage
+ $query[] = array('dtend', '>=', $start);
+
+ if ($end)
+ $query[] = array('dtstart', '<=', $end);
+
+ // add query to exclude pending/declined invitations
+ if (empty($filter_query)) {
+ foreach ($this->cal->get_user_emails() as $email) {
+ $query[] = array('tags', '!=', 'x-partstat:' . $email . ':needs-action');
+ $query[] = array('tags', '!=', 'x-partstat:' . $email . ':declined');
+ }
+ }
+ else if (is_array($filter_query)) {
+ $query = array_merge($query, $filter_query);
+ }
+
+ // we rely the Kolab storage query (no post-filtering)
+ return $this->storage->count($query);
+ }
+
+ /**
+ * Create a new event record
+ *
+ * @see calendar_driver::new_event()
+ *
+ * @return mixed The created record ID on success, False on error
+ */
+ public function insert_event($event)
+ {
+ if (!is_array($event))
+ return false;
+
+ // email links are stored separately
+ $links = $event['links'];
+ unset($event['links']);
+
+ //generate new event from RC input
+ $object = $this->_from_driver_event($event);
+ $saved = $this->storage->save($object, 'event');
+
+ if (!$saved) {
+ rcube::raise_error(array(
+ 'code' => 600, 'type' => 'php',
+ 'file' => __FILE__, 'line' => __LINE__,
+ 'message' => "Error saving event object to Kolab server"),
+ true, false);
+ $saved = false;
+ }
+ else {
+ // save links in configuration.relation object
+ $this->save_links($event['uid'], $links);
+
+ $this->events = array($event['uid'] => $this->_to_driver_event($object, true));
+ }
+
+ return $saved;
+ }
+
+ /**
+ * Update a specific event record
+ *
+ * @see calendar_driver::new_event()
+ * @return boolean True on success, False on error
+ */
+
+ public function update_event($event, $exception_id = null)
+ {
+ $updated = false;
+ $old = $this->storage->get_object($event['uid'] ?: $event['id']);
+ if (!$old || PEAR::isError($old))
+ return false;
+
+ // email links are stored separately
+ $links = $event['links'];
+ unset($event['links']);
+
+ $object = $this->_from_driver_event($event, $old);
+ $saved = $this->storage->save($object, 'event', $old['uid']);
+
+ if (!$saved) {
+ rcube::raise_error(array(
+ 'code' => 600, 'type' => 'php',
+ 'file' => __FILE__, 'line' => __LINE__,
+ 'message' => "Error saving event object to Kolab server"),
+ true, false);
+ }
+ else {
+ // save links in configuration.relation object
+ $this->save_links($event['uid'], $links);
+
+ $updated = true;
+ $this->events = array($event['uid'] => $this->_to_driver_event($object, true));
+
+ // refresh local cache with recurring instances
+ if ($exception_id) {
+ $this->get_recurring_events($object, $event['start'], $event['end'], $exception_id);
+ }
+ }
+
+ return $updated;
+ }
+
+ /**
+ * Delete an event record
+ *
+ * @see calendar_driver::remove_event()
+ * @return boolean True on success, False on error
+ */
+ public function delete_event($event, $force = true)
+ {
+ $deleted = $this->storage->delete($event['uid'] ?: $event['id'], $force);
+
+ if (!$deleted) {
+ rcube::raise_error(array(
+ 'code' => 600, 'type' => 'php',
+ 'file' => __FILE__, 'line' => __LINE__,
+ 'message' => sprintf("Error deleting event object '%s' from Kolab server", $event['id'])),
+ true, false);
+ }
+
+ return $deleted;
+ }
+
+ /**
+ * Restore deleted event record
+ *
+ * @see calendar_driver::undelete_event()
+ * @return boolean True on success, False on error
+ */
+ public function restore_event($event)
+ {
+ if ($this->storage->undelete($event['id'])) {
+ return true;
+ }
+ else {
+ rcube::raise_error(array(
+ 'code' => 600, 'type' => 'php',
+ 'file' => __FILE__, 'line' => __LINE__,
+ 'message' => "Error undeleting the event object $event[id] from the Kolab server"),
+ true, false);
+ }
+
+ return false;
+ }
+
+ /**
+ * Find messages linked with an event
+ */
+ protected function get_links($uid)
+ {
+ $storage = kolab_storage_config::get_instance();
+ return $storage->get_object_links($uid);
+ }
+
+ /**
+ *
+ */
+ protected function save_links($uid, $links)
+ {
+ // make sure we have a valid array
+ if (empty($links)) {
+ $links = array();
+ }
+
+ $storage = kolab_storage_config::get_instance();
+ $remove = array_diff($storage->get_object_links($uid), $links);
+ return $storage->save_object_links($uid, $links, $remove);
+ }
+
+ /**
+ * Create instances of a recurring event
+ *
+ * @param array Hash array with event properties
+ * @param object DateTime Start date of the recurrence window
+ * @param object DateTime End date of the recurrence window
+ * @param string ID of a specific recurring event instance
+ * @return array List of recurring event instances
+ */
+ public function get_recurring_events($event, $start, $end = null, $event_id = null)
+ {
+ $object = $event['_formatobj'];
+ if (!$object) {
+ $rec = $this->storage->get_object($event['id']);
+ $object = $rec['_formatobj'];
+ }
+ if (!is_object($object))
+ return array();
+
+ // determine a reasonable end date if none given
+ if (!$end) {
+ switch ($event['recurrence']['FREQ']) {
+ case 'YEARLY': $intvl = 'P100Y'; break;
+ case 'MONTHLY': $intvl = 'P20Y'; break;
+ default: $intvl = 'P10Y'; break;
+ }
+
+ $end = clone $event['start'];
+ $end->add(new DateInterval($intvl));
+ }
+
+ // copy the recurrence rule from the master event (to be used in the UI)
+ $recurrence_rule = $event['recurrence'];
+ unset($recurrence_rule['EXCEPTIONS'], $recurrence_rule['EXDATE']);
+
+ // read recurrence exceptions first
+ $events = array();
+ $exdata = array();
+ $futuredata = array();
+ $recurrence_id_format = libcalendaring::recurrence_id_format($event);
+
+ if (is_array($event['recurrence']['EXCEPTIONS'])) {
+ foreach ($event['recurrence']['EXCEPTIONS'] as $exception) {
+ if (!$exception['_instance'])
+ $exception['_instance'] = libcalendaring::recurrence_instance_identifier($exception);
+
+ $rec_event = $this->_to_driver_event($exception);
+ $rec_event['id'] = $event['uid'] . '-' . $exception['_instance'];
+ $rec_event['isexception'] = 1;
+
+ // found the specifically requested instance: register exception (single occurrence wins)
+ if ($rec_event['id'] == $event_id && (!$this->events[$event_id] || $this->events[$event_id]['thisandfuture'])) {
+ $rec_event['recurrence'] = $recurrence_rule;
+ $rec_event['recurrence_id'] = $event['uid'];
+ $this->events[$rec_event['id']] = $rec_event;
+ }
+
+ // remember this exception's date
+ $exdate = substr($exception['_instance'], 0, 8);
+ if (!$exdata[$exdate] || $exdata[$exdate]['thisandfuture']) {
+ $exdata[$exdate] = $rec_event;
+ }
+ if ($rec_event['thisandfuture']) {
+ $futuredata[$exdate] = $rec_event;
+ }
+ }
+ }
+
+ // found the specifically requested instance, exiting...
+ if ($event_id && !empty($this->events[$event_id])) {
+ return array($this->events[$event_id]);
+ }
+
+ // use libkolab to compute recurring events
+ if (class_exists('kolabcalendaring')) {
+ $recurrence = new kolab_date_recurrence($object);
+ }
+ else {
+ // fallback to local recurrence implementation
+ require_once($this->cal->home . '/lib/calendar_recurrence.php');
+ $recurrence = new calendar_recurrence($this->cal, $event);
+ }
+
+ $i = 0;
+ while ($next_event = $recurrence->next_instance()) {
+ $datestr = $next_event['start']->format('Ymd');
+ $instance_id = $next_event['start']->format($recurrence_id_format);
+
+ // use this event data for future recurring instances
+ if ($futuredata[$datestr])
+ $overlay_data = $futuredata[$datestr];
+
+ // add to output if in range
+ $rec_id = $event['uid'] . '-' . $instance_id;
+ if (($next_event['start'] <= $end && $next_event['end'] >= $start) || ($event_id && $rec_id == $event_id)) {
+ $rec_event = $this->_to_driver_event($next_event);
+ $rec_event['_instance'] = $instance_id;
+ $rec_event['_count'] = $i + 1;
+
+ if ($overlay_data || $exdata[$datestr]) // copy data from exception
+ kolab_driver::merge_exception_data($rec_event, $exdata[$datestr] ?: $overlay_data);
+
+ $rec_event['id'] = $rec_id;
+ $rec_event['recurrence_id'] = $event['uid'];
+ $rec_event['recurrence'] = $recurrence_rule;
+ unset($rec_event['_attendees']);
+ $events[] = $rec_event;
+
+ if ($rec_id == $event_id) {
+ $this->events[$rec_id] = $rec_event;
+ break;
+ }
+ }
+ else if ($next_event['start'] > $end) // stop loop if out of range
+ break;
+
+ // avoid endless recursion loops
+ if (++$i > 1000)
+ break;
+ }
+
+ return $events;
+ }
+
+ /**
+ * Convert from Kolab_Format to internal representation
+ */
+ private function _to_driver_event($record, $noinst = false)
+ {
+ $record['calendar'] = $this->id;
+ $record['links'] = $this->get_links($record['uid']);
+
+ if ($this->get_namespace() == 'other') {
+ $record['className'] = 'fc-event-ns-other';
+ $record = kolab_driver::add_partstat_class($record, array('NEEDS-ACTION','DECLINED'), $this->get_owner());
+ }
+
+ // add instance identifier to first occurrence (master event)
+ $recurrence_id_format = libcalendaring::recurrence_id_format($record);
+ if (!$noinst && $record['recurrence'] && !$record['recurrence_id'] && !$record['_instance']) {
+ $record['_instance'] = $record['start']->format($recurrence_id_format);
+ }
+ else if (is_a($record['recurrence_date'], 'DateTime')) {
+ $record['_instance'] = $record['recurrence_date']->format($recurrence_id_format);
+ }
+
+ // clean up exception data
+ if ($record['recurrence'] && is_array($record['recurrence']['EXCEPTIONS'])) {
+ array_walk($record['recurrence']['EXCEPTIONS'], function(&$exception) {
+ unset($exception['_mailbox'], $exception['_msguid'], $exception['_formatobj'], $exception['_attachments']);
+ });
+ }
+
+ return $record;
+ }
+
+ /**
+ * Convert the given event record into a data structure that can be passed to Kolab_Storage backend for saving
+ * (opposite of self::_to_driver_event())
+ */
+ private function _from_driver_event($event, $old = array())
+ {
+ // set current user as ORGANIZER
+ $identity = $this->cal->rc->user->list_emails(true);
+ if (empty($event['attendees']) && $identity['email'])
+ $event['attendees'] = array(array('role' => 'ORGANIZER', 'name' => $identity['name'], 'email' => $identity['email']));
+
+ $event['_owner'] = $identity['email'];
+
+ // remove EXDATE values if RDATE is given
+ if (!empty($event['recurrence']['RDATE'])) {
+ $event['recurrence']['EXDATE'] = array();
+ }
+
+ // remove recurrence information (e.g. EXDATES and EXCEPTIONS) entirely
+ if ($event['recurrence'] && empty($event['recurrence']['FREQ']) && empty($event['recurrence']['RDATE'])) {
+ $event['recurrence'] = array();
+ }
+
+ // keep 'comment' from initial itip invitation
+ if (!empty($old['comment'])) {
+ $event['comment'] = $old['comment'];
+ }
+
+ // clean up exception data
+ if (is_array($event['exceptions'])) {
+ array_walk($event['exceptions'], function(&$exception) {
+ unset($exception['_mailbox'], $exception['_msguid'], $exception['_formatobj'], $exception['_attachments'],
+ $event['attachments'], $event['deleted_attachments'], $event['recurrence_id']);
+ });
+ }
+
+
+ // remove some internal properties which should not be saved
+ unset($event['_savemode'], $event['_fromcalendar'], $event['_identity'], $event['_folder_id'],
+ $event['recurrence_id'], $event['attachments'], $event['deleted_attachments'], $event['className']);
+
+ // copy meta data (starting with _) from old object
+ foreach ((array)$old as $key => $val) {
+ if (!isset($event[$key]) && $key[0] == '_')
+ $event[$key] = $val;
+ }
+
+ return $event;
+ }
+
+ /**
+ * Match the given word in the event contents
+ */
+ public function fulltext_match($event, $word, $recursive = true)
+ {
+ $hits = 0;
+ foreach ($this->search_fields as $col) {
+ $sval = is_array($event[$col]) ? self::_complex2string($event[$col]) : $event[$col];
+ if (empty($sval))
+ continue;
+
+ // do a simple substring matching (to be improved)
+ $val = mb_strtolower($sval);
+ if (strpos($val, $word) !== false) {
+ $hits++;
+ break;
+ }
+ }
+
+ return $hits;
+ }
+
+ /**
+ * Convert a complex event attribute to a string value
+ */
+ private static function _complex2string($prop)
+ {
+ static $ignorekeys = array('role','status','rsvp');
+
+ $out = '';
+ if (is_array($prop)) {
+ foreach ($prop as $key => $val) {
+ if (is_numeric($key)) {
+ $out .= self::_complex2string($val);
+ }
+ else if (!in_array($key, $ignorekeys)) {
+ $out .= $val . ' ';
+ }
+ }
+ }
+ else if (is_string($prop) || is_numeric($prop)) {
+ $out .= $prop . ' ';
+ }
+
+ return rtrim($out);
+ }
+
+}
diff --git a/calendar/drivers/kolab/kolab_driver.php b/calendar/drivers/kolab/kolab_driver.php
new file mode 100644
index 0000000..d4f9a19
--- /dev/null
+++ b/calendar/drivers/kolab/kolab_driver.php
@@ -0,0 +1,2526 @@
+<?php
+
+/**
+ * Kolab driver for the Calendar plugin
+ *
+ * @version @package_version@
+ * @author Thomas Bruederli <bruederli@kolabsys.com>
+ * @author Aleksander Machniak <machniak@kolabsys.com>
+ *
+ * Copyright (C) 2012-2015, Kolab Systems AG <contact@kolabsys.com>
+ *
+ * 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.
+ *
+ * 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
+ * GNU Affero General Public License for more details.
+ *
+ * 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/>.
+ */
+
+class kolab_driver extends calendar_driver
+{
+ const INVITATIONS_CALENDAR_PENDING = '--invitation--pending';
+ const INVITATIONS_CALENDAR_DECLINED = '--invitation--declined';
+
+ // features this backend supports
+ public $alarms = true;
+ public $attendees = true;
+ public $freebusy = true;
+ public $attachments = true;
+ public $undelete = true;
+ public $alarm_types = array('DISPLAY','AUDIO');
+ public $categoriesimmutable = true;
+
+ private $rc;
+ private $cal;
+ private $calendars;
+ private $has_writeable = false;
+ private $freebusy_trigger = false;
+ private $bonnie_api = false;
+
+ /**
+ * Default constructor
+ */
+ public function __construct($cal)
+ {
+ $cal->require_plugin('libkolab');
+
+ // load helper classes *after* libkolab has been loaded (#3248)
+ require_once(dirname(__FILE__) . '/kolab_calendar.php');
+ require_once(dirname(__FILE__) . '/kolab_user_calendar.php');
+ require_once(dirname(__FILE__) . '/kolab_invitation_calendar.php');
+
+ $this->cal = $cal;
+ $this->rc = $cal->rc;
+ $this->_read_calendars();
+
+ $this->cal->register_action('push-freebusy', array($this, 'push_freebusy'));
+ $this->cal->register_action('calendar-acl', array($this, 'calendar_acl'));
+
+ $this->freebusy_trigger = $this->rc->config->get('calendar_freebusy_trigger', false);
+
+ if (kolab_storage::$version == '2.0') {
+ $this->alarm_types = array('DISPLAY');
+ $this->alarm_absolute = false;
+ }
+
+ // get configuration for the Bonnie API
+ if ($bonnie_config = $this->cal->rc->config->get('kolab_bonnie_api', false))
+ $this->bonnie_api = new kolab_bonnie_api($bonnie_config);
+
+ // calendar uses fully encoded identifiers
+ kolab_storage::$encode_ids = true;
+ }
+
+
+ /**
+ * Read available calendars from server
+ */
+ private function _read_calendars()
+ {
+ // already read sources
+ if (isset($this->calendars))
+ return $this->calendars;
+
+ // get all folders that have "event" type, sorted by namespace/name
+ $folders = kolab_storage::sort_folders(kolab_storage::get_folders('event') + kolab_storage::get_user_folders('event', true));
+ $this->calendars = array();
+
+ foreach ($folders as $folder) {
+ if ($folder instanceof kolab_storage_folder_user) {
+ $calendar = new kolab_user_calendar($folder->name, $this->cal);
+ $calendar->subscriptions = count($folder->children) > 0;
+ }
+ else {
+ $calendar = new kolab_calendar($folder->name, $this->cal);
+ }
+
+ if ($calendar->ready) {
+ $this->calendars[$calendar->id] = $calendar;
+ if ($calendar->editable)
+ $this->has_writeable = true;
+ }
+ }
+
+ return $this->calendars;
+ }
+
+ /**
+ * Get a list of available calendars from this source
+ *
+ * @param integer $filter Bitmask defining filter criterias
+ * @param object $tree Reference to hierarchical folder tree object
+ *
+ * @return array List of calendars
+ */
+ public function list_calendars($filter = 0, &$tree = null)
+ {
+ // attempt to create a default calendar for this user
+ if (!$this->has_writeable) {
+ if ($this->create_calendar(array('name' => 'Calendar', 'color' => 'cc0000'))) {
+ unset($this->calendars);
+ $this->_read_calendars();
+ }
+ }
+
+ $delim = $this->rc->get_storage()->get_hierarchy_delimiter();
+ $folders = $this->filter_calendars($filter);
+ $calendars = array();
+
+ // include virtual folders for a full folder tree
+ if (!is_null($tree))
+ $folders = kolab_storage::folder_hierarchy($folders, $tree);
+
+ foreach ($folders as $id => $cal) {
+ $fullname = $cal->get_name();
+ $listname = $cal->get_foldername();
+ $imap_path = explode($delim, $cal->name);
+
+ // find parent
+ do {
+ array_pop($imap_path);
+ $parent_id = kolab_storage::folder_id(join($delim, $imap_path));
+ }
+ while (count($imap_path) > 1 && !$this->calendars[$parent_id]);
+
+ // restore "real" parent ID
+ if ($parent_id && !$this->calendars[$parent_id]) {
+ $parent_id = kolab_storage::folder_id($cal->get_parent());
+ }
+
+ // turn a kolab_storage_folder object into a kolab_calendar
+ if ($cal instanceof kolab_storage_folder) {
+ $cal = new kolab_calendar($cal->name, $this->cal);
+ $this->calendars[$cal->id] = $cal;
+ }
+
+ // special handling for user or virtual folders
+ if ($cal instanceof kolab_storage_folder_user) {
+ $calendars[$cal->id] = array(
+ 'id' => $cal->id,
+ 'name' => kolab_storage::object_name($fullname),
+ 'listname' => $listname,
+ 'editname' => $cal->get_foldername(),
+ 'color' => $cal->get_color(),
+ 'active' => $cal->is_active(),
+ 'title' => $cal->get_owner(),
+ 'owner' => $cal->get_owner(),
+ 'history' => false,
+ 'virtual' => false,
+ 'editable' => false,
+ 'group' => 'other',
+ 'class' => 'user',
+ 'removable' => true,
+ );
+ }
+ else if ($cal->virtual) {
+ $calendars[$cal->id] = array(
+ 'id' => $cal->id,
+ 'name' => $fullname,
+ 'listname' => $listname,
+ 'editname' => $cal->get_foldername(),
+ 'virtual' => true,
+ 'editable' => false,
+ 'group' => $cal->get_namespace(),
+ 'class' => 'folder',
+ );
+ }
+ else {
+ $calendars[$cal->id] = array(
+ 'id' => $cal->id,
+ 'name' => $fullname,
+ 'listname' => $listname,
+ 'editname' => $cal->get_foldername(),
+ 'title' => $cal->get_title(),
+ 'color' => $cal->get_color(),
+ 'editable' => $cal->editable,
+ 'rights' => $cal->rights,
+ 'showalarms' => $cal->alarms,
+ 'history' => !empty($this->bonnie_api),
+ 'group' => $cal->get_namespace(),
+ 'default' => $cal->default,
+ 'active' => $cal->is_active(),
+ 'owner' => $cal->get_owner(),
+ 'children' => true, // TODO: determine if that folder indeed has child folders
+ 'parent' => $parent_id,
+ 'subtype' => $cal->subtype,
+ 'caldavurl' => $cal->get_caldav_url(),
+ 'removable' => !$cal->default,
+ );
+ }
+
+ if ($cal->subscriptions) {
+ $calendars[$cal->id]['subscribed'] = $cal->is_subscribed();
+ }
+ }
+
+ // list virtual calendars showing invitations
+ if ($this->rc->config->get('kolab_invitation_calendars')) {
+ foreach (array(self::INVITATIONS_CALENDAR_PENDING, self::INVITATIONS_CALENDAR_DECLINED) as $id) {
+ $cal = new kolab_invitation_calendar($id, $this->cal);
+ $this->calendars[$cal->id] = $cal;
+ if (!($filter & self::FILTER_ACTIVE) || $cal->is_active()) {
+ $calendars[$id] = array(
+ 'id' => $cal->id,
+ 'name' => $cal->get_name(),
+ 'listname' => $cal->get_name(),
+ 'editname' => $cal->get_foldername(),
+ 'title' => $cal->get_title(),
+ 'color' => $cal->get_color(),
+ 'editable' => $cal->editable,
+ 'rights' => $cal->rights,
+ 'showalarms' => $cal->alarms,
+ 'history' => !empty($this->bonnie_api),
+ 'group' => 'x-invitations',
+ 'default' => false,
+ 'active' => $cal->is_active(),
+ 'owner' => $cal->get_owner(),
+ 'children' => false,
+ );
+
+ if ($id == self::INVITATIONS_CALENDAR_PENDING) {
+ $calendars[$id]['counts'] = true;
+ }
+
+ if (is_object($tree)) {
+ $tree->children[] = $cal;
+ }
+ }
+ }
+ }
+
+ // append the virtual birthdays calendar
+ if ($this->rc->config->get('calendar_contact_birthdays', false)) {
+ $id = self::BIRTHDAY_CALENDAR_ID;
+ $prefs = $this->rc->config->get('kolab_calendars', array()); // read local prefs
+ if (!($filter & self::FILTER_ACTIVE) || $prefs[$id]['active']) {
+ $calendars[$id] = array(
+ 'id' => $id,
+ 'name' => $this->cal->gettext('birthdays'),
+ 'listname' => $this->cal->gettext('birthdays'),
+ 'color' => $prefs[$id]['color'] ?: '87CEFA',
+ 'active' => (bool)$prefs[$id]['active'],
+ 'showalarms' => (bool)$this->rc->config->get('calendar_birthdays_alarm_type'),
+ 'group' => 'x-birthdays',
+ 'editable' => false,
+ 'default' => false,
+ 'children' => false,
+ 'history' => false,
+ );
+ }
+ }
+
+ return $calendars;
+ }
+
+ /**
+ * Get list of calendars according to specified filters
+ *
+ * @param integer Bitmask defining restrictions. See FILTER_* constants for possible values.
+ *
+ * @return array List of calendars
+ */
+ protected function filter_calendars($filter)
+ {
+ $calendars = array();
+
+ $plugin = $this->rc->plugins->exec_hook('calendar_list_filter', array(
+ 'list' => $this->calendars,
+ 'calendars' => $calendars,
+ 'filter' => $filter,
+ 'editable' => ($filter & self::FILTER_WRITEABLE),
+ 'insert' => ($filter & self::FILTER_INSERTABLE),
+ 'active' => ($filter & self::FILTER_ACTIVE),
+ 'personal' => ($filter & self::FILTER_PERSONAL)
+ ));
+
+ if ($plugin['abort']) {
+ return $plugin['calendars'];
+ }
+
+ foreach ($this->calendars as $cal) {
+ if (!$cal->ready) {
+ continue;
+ }
+ if (($filter & self::FILTER_WRITEABLE) && !$cal->editable) {
+ continue;
+ }
+ if (($filter & self::FILTER_INSERTABLE) && !$cal->insert) {
+ continue;
+ }
+ if (($filter & self::FILTER_ACTIVE) && !$cal->is_active()) {
+ continue;
+ }
+ if (($filter & self::FILTER_PRIVATE) && $cal->subtype != 'private') {
+ continue;
+ }
+ if (($filter & self::FILTER_CONFIDENTIAL) && $cal->subtype != 'confidential') {
+ continue;
+ }
+ if (($filter & self::FILTER_PERSONAL) && $cal->get_namespace() != 'personal') {
+ continue;
+ }
+ $calendars[$cal->id] = $cal;
+ }
+
+ return $calendars;
+ }
+
+
+ /**
+ * Get the kolab_calendar instance for the given calendar ID
+ *
+ * @param string Calendar identifier (encoded imap folder name)
+ * @return object kolab_calendar Object nor null if calendar doesn't exist
+ */
+ public function get_calendar($id)
+ {
+ // create calendar object if necesary
+ if (!$this->calendars[$id] && in_array($id, array(self::INVITATIONS_CALENDAR_PENDING, self::INVITATIONS_CALENDAR_DECLINED))) {
+ $this->calendars[$id] = new kolab_invitation_calendar($id, $this->cal);
+ }
+ else if (!$this->calendars[$id] && $id !== self::BIRTHDAY_CALENDAR_ID) {
+ $calendar = kolab_calendar::factory($id, $this->cal);
+ if ($calendar->ready)
+ $this->calendars[$calendar->id] = $calendar;
+ }
+
+ return $this->calendars[$id];
+ }
+
+ /**
+ * Create a new calendar assigned to the current user
+ *
+ * @param array Hash array with calendar properties
+ * name: Calendar name
+ * color: The color of the calendar
+ * @return mixed ID of the calendar on success, False on error
+ */
+ public function create_calendar($prop)
+ {
+ $prop['type'] = 'event';
+ $prop['active'] = true;
+ $prop['subscribed'] = true;
+ $folder = kolab_storage::folder_update($prop);
+
+ if ($folder === false) {
+ $this->last_error = $this->cal->gettext(kolab_storage::$last_error);
+ return false;
+ }
+
+ // create ID
+ $id = kolab_storage::folder_id($folder);
+
+ // save color in user prefs (temp. solution)
+ $prefs['kolab_calendars'] = $this->rc->config->get('kolab_calendars', array());
+
+ if (isset($prop['color']))
+ $prefs['kolab_calendars'][$id]['color'] = $prop['color'];
+ if (isset($prop['showalarms']))
+ $prefs['kolab_calendars'][$id]['showalarms'] = $prop['showalarms'] ? true : false;
+
+ if ($prefs['kolab_calendars'][$id])
+ $this->rc->user->save_prefs($prefs);
+
+ return $id;
+ }
+
+
+ /**
+ * Update properties of an existing calendar
+ *
+ * @see calendar_driver::edit_calendar()
+ */
+ public function edit_calendar($prop)
+ {
+ if ($prop['id'] && ($cal = $this->get_calendar($prop['id']))) {
+ $id = $cal->update($prop);
+ }
+ else {
+ $id = $prop['id'];
+ }
+
+ // fallback to local prefs
+ $prefs['kolab_calendars'] = $this->rc->config->get('kolab_calendars', array());
+ unset($prefs['kolab_calendars'][$prop['id']]['color'], $prefs['kolab_calendars'][$prop['id']]['showalarms']);
+
+ if (isset($prop['color']))
+ $prefs['kolab_calendars'][$id]['color'] = $prop['color'];
+
+ if (isset($prop['showalarms']) && $id == self::BIRTHDAY_CALENDAR_ID)
+ $prefs['calendar_birthdays_alarm_type'] = $prop['showalarms'] ? $this->alarm_types[0] : '';
+ else if (isset($prop['showalarms']))
+ $prefs['kolab_calendars'][$id]['showalarms'] = $prop['showalarms'] ? true : false;
+
+ if (!empty($prefs['kolab_calendars'][$id]))
+ $this->rc->user->save_prefs($prefs);
+
+ return true;
+ }
+
+
+ /**
+ * Set active/subscribed state of a calendar
+ *
+ * @see calendar_driver::subscribe_calendar()
+ */
+ public function subscribe_calendar($prop)
+ {
+ if ($prop['id'] && ($cal = $this->get_calendar($prop['id'])) && is_object($cal->storage)) {
+ $ret = false;
+ if (isset($prop['permanent']))
+ $ret |= $cal->storage->subscribe(intval($prop['permanent']));
+ if (isset($prop['active']))
+ $ret |= $cal->storage->activate(intval($prop['active']));
+
+ // apply to child folders, too
+ if ($prop['recursive']) {
+ foreach ((array)kolab_storage::list_folders($cal->storage->name, '*', 'event') as $subfolder) {
+ if (isset($prop['permanent']))
+ ($prop['permanent'] ? kolab_storage::folder_subscribe($subfolder) : kolab_storage::folder_unsubscribe($subfolder));
+ if (isset($prop['active']))
+ ($prop['active'] ? kolab_storage::folder_activate($subfolder) : kolab_storage::folder_deactivate($subfolder));
+ }
+ }
+ return $ret;
+ }
+ else {
+ // save state in local prefs
+ $prefs['kolab_calendars'] = $this->rc->config->get('kolab_calendars', array());
+ $prefs['kolab_calendars'][$prop['id']]['active'] = (bool)$prop['active'];
+ $this->rc->user->save_prefs($prefs);
+ return true;
+ }
+
+ return false;
+ }
+
+
+ /**
+ * Delete the given calendar with all its contents
+ *
+ * @see calendar_driver::delete_calendar()
+ */
+ public function delete_calendar($prop)
+ {
+ if ($prop['id'] && ($cal = $this->get_calendar($prop['id']))) {
+ $folder = $cal->get_realname();
+ // TODO: unsubscribe if no admin rights
+ if (kolab_storage::folder_delete($folder)) {
+ // remove color in user prefs (temp. solution)
+ $prefs['kolab_calendars'] = $this->rc->config->get('kolab_calendars', array());
+ unset($prefs['kolab_calendars'][$prop['id']]);
+
+ $this->rc->user->save_prefs($prefs);
+ return true;
+ }
+ else
+ $this->last_error = kolab_storage::$last_error;
+ }
+
+ return false;
+ }
+
+
+ /**
+ * Search for shared or otherwise not listed calendars the user has access
+ *
+ * @param string Search string
+ * @param string Section/source to search
+ * @return array List of calendars
+ */
+ public function search_calendars($query, $source)
+ {
+ if (!kolab_storage::setup())
+ return array();
+
+ $this->calendars = array();
+ $this->search_more_results = false;
+
+ // find unsubscribed IMAP folders that have "event" type
+ if ($source == 'folders') {
+ foreach ((array)kolab_storage::search_folders('event', $query, array('other')) as $folder) {
+ $calendar = new kolab_calendar($folder->name, $this->cal);
+ $this->calendars[$calendar->id] = $calendar;
+ }
+ }
+ // find other user's virtual calendars
+ else if ($source == 'users') {
+ $limit = $this->rc->config->get('autocomplete_max', 15) * 2; // we have slightly more space, so display twice the number
+ foreach (kolab_storage::search_users($query, 0, array(), $limit, $count) as $user) {
+ $calendar = new kolab_user_calendar($user, $this->cal);
+ $this->calendars[$calendar->id] = $calendar;
+
+ // search for calendar folders shared by this user
+ foreach (kolab_storage::list_user_folders($user, 'event', false) as $foldername) {
+ $cal = new kolab_calendar($foldername, $this->cal);
+ $this->calendars[$cal->id] = $cal;
+ $calendar->subscriptions = true;
+ }
+ }
+
+ if ($count > $limit) {
+ $this->search_more_results = true;
+ }
+ }
+
+ // don't list the birthday calendar
+ $this->rc->config->set('calendar_contact_birthdays', false);
+ $this->rc->config->set('kolab_invitation_calendars', false);
+
+ return $this->list_calendars();
+ }
+
+
+ /**
+ * Fetch a single event
+ *
+ * @see calendar_driver::get_event()
+ * @return array Hash array with event properties, false if not found
+ */
+ public function get_event($event, $scope = 0, $full = false)
+ {
+ if (is_array($event)) {
+ $id = $event['id'] ?: $event['uid'];
+ $cal = $event['calendar'];
+
+ // we're looking for a recurring instance: expand the ID to our internal convention for recurring instances
+ if (!$event['id'] && $event['_instance']) {
+ $id .= '-' . $event['_instance'];
+ }
+ }
+ else {
+ $id = $event;
+ }
+
+ if ($cal) {
+ if ($storage = $this->get_calendar($cal)) {
+ $result = $storage->get_event($id);
+ return self::to_rcube_event($result);
+ }
+ // get event from the address books birthday calendar
+ else if ($cal == self::BIRTHDAY_CALENDAR_ID) {
+ return $this->get_birthday_event($id);
+ }
+ }
+ // iterate over all calendar folders and search for the event ID
+ else {
+ foreach ($this->filter_calendars($scope) as $calendar) {
+ if ($result = $calendar->get_event($id)) {
+ return self::to_rcube_event($result);
+ }
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * Add a single event to the database
+ *
+ * @see calendar_driver::new_event()
+ */
+ public function new_event($event)
+ {
+ if (!$this->validate($event))
+ return false;
+
+ $event = self::from_rcube_event($event);
+
+ $cid = $event['calendar'] ? $event['calendar'] : reset(array_keys($this->calendars));
+ if ($storage = $this->get_calendar($cid)) {
+ // if this is a recurrence instance, append as exception to an already existing object for this UID
+ if (!empty($event['recurrence_date']) && ($master = $storage->get_event($event['uid']))) {
+ self::add_exception($master, $event);
+ $success = $storage->update_event($master);
+ }
+ else {
+ $success = $storage->insert_event($event);
+ }
+
+ if ($success && $this->freebusy_trigger) {
+ $this->rc->output->command('plugin.ping_url', array('action' => 'calendar/push-freebusy', 'source' => $storage->id));
+ $this->freebusy_trigger = false; // disable after first execution (#2355)
+ }
+
+ return $success;
+ }
+
+ return false;
+ }
+
+ /**
+ * Update an event entry with the given data
+ *
+ * @see calendar_driver::new_event()
+ * @return boolean True on success, False on error
+ */
+ public function edit_event($event)
+ {
+ if (!($storage = $this->get_calendar($event['calendar'])))
+ return false;
+
+ return $this->update_event(self::from_rcube_event($event, $storage->get_event($event['id'])));
+ }
+
+ /**
+ * Extended event editing with possible changes to the argument
+ *
+ * @param array Hash array with event properties
+ * @param string New participant status
+ * @param array List of hash arrays with updated attendees
+ * @return boolean True on success, False on error
+ */
+ public function edit_rsvp(&$event, $status, $attendees)
+ {
+ $update_event = $event;
+
+ // apply changes to master (and all exceptions)
+ if ($event['_savemode'] == 'all' && $event['recurrence_id']) {
+ if ($storage = $this->get_calendar($event['calendar'])) {
+ $update_event = $storage->get_event($event['recurrence_id']);
+ $update_event['_savemode'] = $event['_savemode'];
+ $update_event['id'] = $update_event['uid'];
+ unset($update_event['recurrence_id']);
+ calendar::merge_attendee_data($update_event, $attendees);
+ }
+ }
+
+ if ($ret = $this->update_attendees($update_event, $attendees)) {
+ // replace with master event (for iTip reply)
+ $event = self::to_rcube_event($update_event);
+
+ // re-assign to the according (virtual) calendar
+ if ($this->rc->config->get('kolab_invitation_calendars')) {
+ if (strtoupper($status) == 'DECLINED')
+ $event['calendar'] = self::INVITATIONS_CALENDAR_DECLINED;
+ else if (strtoupper($status) == 'NEEDS-ACTION')
+ $event['calendar'] = self::INVITATIONS_CALENDAR_PENDING;
+ else if ($event['_folder_id'])
+ $event['calendar'] = $event['_folder_id'];
+ }
+ }
+
+ return $ret;
+ }
+
+ /**
+ * Update the participant status for the given attendees
+ *
+ * @see calendar_driver::update_attendees()
+ */
+ public function update_attendees(&$event, $attendees)
+ {
+ // for this-and-future updates, merge the updated attendees onto all exceptions in range
+ if (($event['_savemode'] == 'future' && $event['recurrence_id']) || (!empty($event['recurrence']) && !$event['recurrence_id'])) {
+ if (!($storage = $this->get_calendar($event['calendar'])))
+ return false;
+
+ // load master event
+ $master = $event['recurrence_id'] ? $storage->get_event($event['recurrence_id']) : $event;
+
+ // apply attendee update to each existing exception
+ if ($master['recurrence'] && !empty($master['recurrence']['EXCEPTIONS'])) {
+ $saved = false;
+ foreach ($master['recurrence']['EXCEPTIONS'] as $i => $exception) {
+ // merge the new event properties onto future exceptions
+ if ($exception['_instance'] >= strval($event['_instance'])) {
+ calendar::merge_attendee_data($master['recurrence']['EXCEPTIONS'][$i], $attendees);
+ }
+ // update a specific instance
+ if ($exception['_instance'] == $event['_instance'] && $exception['thisandfuture']) {
+ $saved = true;
+ }
+ }
+
+ // add the given event as new exception
+ if (!$saved && $event['id'] != $master['id']) {
+ $event['thisandfuture'] = true;
+ $master['recurrence']['EXCEPTIONS'][] = $event;
+ }
+
+ // set link to top-level exceptions
+ $master['exceptions'] = &$master['recurrence']['EXCEPTIONS'];
+
+ return $this->update_event($master);
+ }
+ }
+
+ // just update the given event (instance)
+ return $this->update_event($event);
+ }
+
+ /**
+ * Move a single event
+ *
+ * @see calendar_driver::move_event()
+ * @return boolean True on success, False on error
+ */
+ public function move_event($event)
+ {
+ if (($storage = $this->get_calendar($event['calendar'])) && ($ev = $storage->get_event($event['id']))) {
+ unset($ev['sequence']);
+ self::clear_attandee_noreply($ev);
+ return $this->update_event($event + $ev);
+ }
+
+ return false;
+ }
+
+ /**
+ * Resize a single event
+ *
+ * @see calendar_driver::resize_event()
+ * @return boolean True on success, False on error
+ */
+ public function resize_event($event)
+ {
+ if (($storage = $this->get_calendar($event['calendar'])) && ($ev = $storage->get_event($event['id']))) {
+ unset($ev['sequence']);
+ self::clear_attandee_noreply($ev);
+ return $this->update_event($event + $ev);
+ }
+
+ return false;
+ }
+
+ /**
+ * Remove a single event
+ *
+ * @param array Hash array with event properties:
+ * id: Event identifier
+ * @param boolean Remove record(s) irreversible (mark as deleted otherwise)
+ *
+ * @return boolean True on success, False on error
+ */
+ public function remove_event($event, $force = true)
+ {
+ $ret = true;
+ $success = false;
+ $savemode = $event['_savemode'];
+ $decline = $event['_decline'];
+
+ if (($storage = $this->get_calendar($event['calendar'])) && ($event = $storage->get_event($event['id']))) {
+ $event['_savemode'] = $savemode;
+ $savemode = 'all';
+ $master = $event;
+
+ $this->rc->session->remove('calendar_restore_event_data');
+
+ // read master if deleting a recurring event
+ if ($event['recurrence'] || $event['recurrence_id'] || $event['isexception']) {
+ $master = $storage->get_event($event['uid']);
+ $savemode = $event['_savemode'] ?: ($event['_instance'] || $event['isexception'] ? 'current' : 'all');
+
+ // force 'current' mode for single occurrences stored as exception
+ if (!$event['recurrence'] && !$event['recurrence_id'] && $event['isexception'])
+ $savemode = 'current';
+ }
+
+ // removing an exception instance
+ if (($event['recurrence_id'] || $event['isexception']) && is_array($master['exceptions'])) {
+ foreach ($master['exceptions'] as $i => $exception) {
+ if ($exception['_instance'] == $event['_instance']) {
+ unset($master['exceptions'][$i]);
+ // set event date back to the actual occurrence
+ if ($exception['recurrence_date'])
+ $event['start'] = $exception['recurrence_date'];
+ }
+ }
+
+ if (is_array($master['recurrence'])) {
+ $master['recurrence']['EXCEPTIONS'] = &$master['exceptions'];
+ }
+ }
+
+ switch ($savemode) {
+ case 'current':
+ $_SESSION['calendar_restore_event_data'] = $master;
+
+ // removing the first instance => just move to next occurence
+ if ($master['recurrence'] && $event['_instance'] == libcalendaring::recurrence_instance_identifier($master)) {
+ $recurring = reset($storage->get_recurring_events($event, $event['start'], null, $event['id'].'-1'));
+
+ // no future instances found: delete the master event (bug #1677)
+ if (!$recurring['start']) {
+ $success = $storage->delete_event($master, $force);
+ break;
+ }
+
+ $master['start'] = $recurring['start'];
+ $master['end'] = $recurring['end'];
+ if ($master['recurrence']['COUNT'])
+ $master['recurrence']['COUNT']--;
+ }
+ // remove the matching RDATE entry
+ else if ($master['recurrence']['RDATE']) {
+ foreach ($master['recurrence']['RDATE'] as $j => $rdate) {
+ if ($rdate->format('Ymd') == $event['start']->format('Ymd')) {
+ unset($master['recurrence']['RDATE'][$j]);
+ break;
+ }
+ }
+ }
+ else { // add exception to master event
+ $master['recurrence']['EXDATE'][] = $event['start'];
+ }
+ $success = $storage->update_event($master);
+ break;
+
+ case 'future':
+ $master['_instance'] = libcalendaring::recurrence_instance_identifier($master);
+ if ($master['_instance'] != $event['_instance']) {
+ $_SESSION['calendar_restore_event_data'] = $master;
+
+ // set until-date on master event
+ $master['recurrence']['UNTIL'] = clone $event['start'];
+ $master['recurrence']['UNTIL']->sub(new DateInterval('P1D'));
+ unset($master['recurrence']['COUNT']);
+
+ // if all future instances are deleted, remove recurrence rule entirely (bug #1677)
+ if ($master['recurrence']['UNTIL']->format('Ymd') == $master['start']->format('Ymd')) {
+ $master['recurrence'] = array();
+ }
+ // remove matching RDATE entries
+ else if ($master['recurrence']['RDATE']) {
+ foreach ($master['recurrence']['RDATE'] as $j => $rdate) {
+ if ($rdate->format('Ymd') == $event['start']->format('Ymd')) {
+ $master['recurrence']['RDATE'] = array_slice($master['recurrence']['RDATE'], 0, $j);
+ break;
+ }
+ }
+ }
+
+ $success = $storage->update_event($master);
+ $ret = $master['uid'];
+ break;
+ }
+
+ default: // 'all' is default
+ // removing the master event with loose exceptions (not recurring though)
+ if (!empty($event['recurrence_date']) && empty($master['recurrence']) && !empty($master['exceptions'])) {
+ // make the first exception the new master
+ $newmaster = array_shift($master['exceptions']);
+ $newmaster['exceptions'] = $master['exceptions'];
+ $newmaster['_attachments'] = $master['_attachments'];
+ $newmaster['_mailbox'] = $master['_mailbox'];
+ $newmaster['_msguid'] = $master['_msguid'];
+
+ $success = $storage->update_event($newmaster);
+ }
+ else if ($decline && $this->rc->config->get('kolab_invitation_calendars')) {
+ // don't delete but set PARTSTAT=DECLINED
+ if ($this->cal->lib->set_partstat($master, 'DECLINED')) {
+ $success = $storage->update_event($master);
+ }
+ }
+
+ if (!$success)
+ $success = $storage->delete_event($master, $force);
+ break;
+ }
+ }
+
+ if ($success && $this->freebusy_trigger)
+ $this->rc->output->command('plugin.ping_url', array('action' => 'calendar/push-freebusy', 'source' => $storage->id));
+
+ return $success ? $ret : false;
+ }
+
+ /**
+ * Restore a single deleted event
+ *
+ * @param array Hash array with event properties:
+ * id: Event identifier
+ * @return boolean True on success, False on error
+ */
+ public function restore_event($event)
+ {
+ if ($storage = $this->get_calendar($event['calendar'])) {
+ if (!empty($_SESSION['calendar_restore_event_data']))
+ $success = $storage->update_event($_SESSION['calendar_restore_event_data']);
+ else
+ $success = $storage->restore_event($event);
+
+ if ($success && $this->freebusy_trigger)
+ $this->rc->output->command('plugin.ping_url', array('action' => 'calendar/push-freebusy', 'source' => $storage->id));
+
+ return $success;
+ }
+
+ return false;
+ }
+
+ /**
+ * Wrapper to update an event object depending on the given savemode
+ */
+ private function update_event($event)
+ {
+ if (!($storage = $this->get_calendar($event['calendar'])))
+ return false;
+
+ // move event to another folder/calendar
+ if ($event['_fromcalendar'] && $event['_fromcalendar'] != $event['calendar']) {
+ if (!($fromcalendar = $this->get_calendar($event['_fromcalendar'])))
+ return false;
+
+ $old = $fromcalendar->get_event($event['id']);
+
+ if ($event['_savemode'] != 'new') {
+ if (!$fromcalendar->storage->move($old['uid'], $storage->storage)) {
+ return false;
+ }
+
+ $fromcalendar = $storage;
+ }
+ }
+ else
+ $fromcalendar = $storage;
+
+ $success = false;
+ $savemode = 'all';
+ $attachments = array();
+ $old = $master = $storage->get_event($event['id']);
+
+ if (!$old || !$old['start']) {
+ rcube::raise_error(array(
+ 'code' => 600, 'type' => 'php',
+ 'file' => __FILE__, 'line' => __LINE__,
+ 'message' => "Failed to load event object to update: id=" . $event['id']),
+ true, false);
+ return false;
+ }
+
+ // modify a recurring event, check submitted savemode to do the right things
+ if ($old['recurrence'] || $old['recurrence_id'] || $old['isexception']) {
+ $master = $storage->get_event($old['uid']);
+ $savemode = $event['_savemode'] ?: ($old['recurrence_id'] || $old['isexception'] ? 'current' : 'all');
+
+ // this-and-future on the first instance equals to 'all'
+ if ($savemode == 'future' && $master['start'] && $old['_instance'] == libcalendaring::recurrence_instance_identifier($master))
+ $savemode = 'all';
+ // force 'current' mode for single occurrences stored as exception
+ else if (!$old['recurrence'] && !$old['recurrence_id'] && $old['isexception'])
+ $savemode = 'current';
+ }
+
+ // check if update affects scheduling and update attendee status accordingly
+ $reschedule = $this->check_scheduling($event, $old, true);
+
+ // keep saved exceptions (not submitted by the client)
+ if ($old['recurrence']['EXDATE'] && !isset($event['recurrence']['EXDATE']))
+ $event['recurrence']['EXDATE'] = $old['recurrence']['EXDATE'];
+ if (isset($event['recurrence']['EXCEPTIONS']))
+ $with_exceptions = true; // exceptions already provided (e.g. from iCal import)
+ else if ($old['recurrence']['EXCEPTIONS'])
+ $event['recurrence']['EXCEPTIONS'] = $old['recurrence']['EXCEPTIONS'];
+ else if ($old['exceptions'])
+ $event['exceptions'] = $old['exceptions'];
+
+ // remove some internal properties which should not be saved
+ unset($event['_savemode'], $event['_fromcalendar'], $event['_identity'], $event['_owner'],
+ $event['_notify'], $event['_method'], $event['_sender'], $event['_sender_utf'], $event['_size']);
+
+ switch ($savemode) {
+ case 'new':
+ // save submitted data as new (non-recurring) event
+ $event['recurrence'] = array();
+ $event['_copyfrom'] = $master['_msguid'];
+ $event['_mailbox'] = $master['_mailbox'];
+ $event['uid'] = $this->cal->generate_uid();
+ unset($event['recurrence_id'], $event['recurrence_date'], $event['_instance'], $event['id']);
+
+ // copy attachment metadata to new event
+ $event = self::from_rcube_event($event, $master);
+
+ self::clear_attandee_noreply($event);
+ if ($success = $storage->insert_event($event))
+ $success = $event['uid'];
+ break;
+
+ case 'future':
+ // create a new recurring event
+ $event['_copyfrom'] = $master['_msguid'];
+ $event['_mailbox'] = $master['_mailbox'];
+ $event['uid'] = $this->cal->generate_uid();
+ unset($event['recurrence_id'], $event['recurrence_date'], $event['_instance'], $event['id']);
+
+ // copy attachment metadata to new event
+ $event = self::from_rcube_event($event, $master);
+
+ // remove recurrence exceptions on re-scheduling
+ if ($reschedule) {
+ unset($event['recurrence']['EXCEPTIONS'], $event['exceptions'], $master['recurrence']['EXDATE']);
+ }
+ else if (is_array($event['recurrence']['EXCEPTIONS'])) {
+ // only keep relevant exceptions
+ $event['recurrence']['EXCEPTIONS'] = array_filter($event['recurrence']['EXCEPTIONS'], function($exception) use ($event) {
+ return $exception['start'] > $event['start'];
+ });
+ if (is_array($event['recurrence']['EXDATE'])) {
+ $event['recurrence']['EXDATE'] = array_filter($event['recurrence']['EXDATE'], function($exdate) use ($event) {
+ return $exdate > $event['start'];
+ });
+ }
+ // set link to top-level exceptions
+ $event['exceptions'] = &$event['recurrence']['EXCEPTIONS'];
+ }
+
+ // compute remaining occurrences
+ if ($event['recurrence']['COUNT']) {
+ if (!$old['_count'])
+ $old['_count'] = $this->get_recurrence_count($master, $old['start']);
+ $event['recurrence']['COUNT'] -= intval($old['_count']);
+ }
+
+ // remove fixed weekday when date changed
+ if ($old['start']->format('Y-m-d') != $event['start']->format('Y-m-d')) {
+ if (strlen($event['recurrence']['BYDAY']) == 2)
+ unset($event['recurrence']['BYDAY']);
+ if ($old['recurrence']['BYMONTH'] == $old['start']->format('n'))
+ unset($event['recurrence']['BYMONTH']);
+ }
+
+ // set until-date on master event
+ $master['recurrence']['UNTIL'] = clone $old['start'];
+ $master['recurrence']['UNTIL']->sub(new DateInterval('P1D'));
+ unset($master['recurrence']['COUNT']);
+
+ // remove all exceptions after $event['start']
+ if (is_array($master['recurrence']['EXCEPTIONS'])) {
+ $master['recurrence']['EXCEPTIONS'] = array_filter($master['recurrence']['EXCEPTIONS'], function($exception) use ($event) {
+ return $exception['start'] < $event['start'];
+ });
+ // set link to top-level exceptions
+ $master['exceptions'] = &$master['recurrence']['EXCEPTIONS'];
+ }
+ if (is_array($master['recurrence']['EXDATE'])) {
+ $master['recurrence']['EXDATE'] = array_filter($master['recurrence']['EXDATE'], function($exdate) use ($event) {
+ return $exdate < $event['start'];
+ });
+ }
+
+ // save new event
+ if ($success = $storage->insert_event($event)) {
+ $success = $event['uid'];
+
+ // update master event (no rescheduling!)
+ self::clear_attandee_noreply($master);
+ $storage->update_event($master);
+ }
+ break;
+
+ case 'current':
+ // recurring instances shall not store recurrence rules and attachments
+ $event['recurrence'] = array();
+ $event['thisandfuture'] = $savemode == 'future';
+ unset($event['attachments'], $event['id']);
+
+ // increment sequence of this instance if scheduling is affected
+ if ($reschedule) {
+ $event['sequence'] = max($old['sequence'], $master['sequence']) + 1;
+ }
+ else if (!isset($event['sequence'])) {
+ $event['sequence'] = $old['sequence'] ?: $master['sequence'];
+ }
+
+ // save properties to a recurrence exception instance
+ if ($old['_instance'] && is_array($master['recurrence']['EXCEPTIONS'])) {
+ if ($this->update_recurrence_exceptions($master, $event, $old, $savemode)) {
+ $success = $storage->update_event($master, $old['id']);
+ break;
+ }
+ }
+
+ $add_exception = true;
+
+ // adjust matching RDATE entry if dates changed
+ if (is_array($master['recurrence']['RDATE']) && ($old_date = $old['start']->format('Ymd')) != $event['start']->format('Ymd')) {
+ foreach ($master['recurrence']['RDATE'] as $j => $rdate) {
+ if ($rdate->format('Ymd') == $old_date) {
+ $master['recurrence']['RDATE'][$j] = $event['start'];
+ sort($master['recurrence']['RDATE']);
+ $add_exception = false;
+ break;
+ }
+ }
+ }
+
+ // save as new exception to master event
+ if ($add_exception) {
+ self::add_exception($master, $event, $old);
+ }
+
+ $success = $storage->update_event($master);
+ break;
+
+ default: // 'all' is default
+ $event['id'] = $master['uid'];
+ $event['uid'] = $master['uid'];
+
+ // use start date from master but try to be smart on time or duration changes
+ $old_start_date = $old['start']->format('Y-m-d');
+ $old_start_time = $old['allday'] ? '' : $old['start']->format('H:i');
+ $old_duration = $old['end']->format('U') - $old['start']->format('U');
+
+ $new_start_date = $event['start']->format('Y-m-d');
+ $new_start_time = $event['allday'] ? '' : $event['start']->format('H:i');
+ $new_duration = $event['end']->format('U') - $event['start']->format('U');
+
+ $diff = $old_start_date != $new_start_date || $old_start_time != $new_start_time || $old_duration != $new_duration;
+ $date_shift = $old['start']->diff($event['start']);
+
+ // shifted or resized
+ if ($diff && ($old_start_date == $new_start_date || $old_duration == $new_duration)) {
+ $event['start'] = $master['start']->add($date_shift);
+ $event['end'] = clone $event['start'];
+ $event['end']->add(new DateInterval('PT'.$new_duration.'S'));
+
+ // remove fixed weekday, will be re-set to the new weekday in kolab_calendar::update_event()
+ if ($old_start_date != $new_start_date) {
+ if (strlen($event['recurrence']['BYDAY']) == 2)
+ unset($event['recurrence']['BYDAY']);
+ if ($old['recurrence']['BYMONTH'] == $old['start']->format('n'))
+ unset($event['recurrence']['BYMONTH']);
+ }
+ }
+ // dates did not change, use the ones from master
+ else if ($new_start_date . $new_start_time == $old_start_date . $old_start_time) {
+ $event['start'] = $master['start'];
+ $event['end'] = $master['end'];
+ }
+
+ // when saving an instance in 'all' mode, copy recurrence exceptions over
+ if ($old['recurrence_id']) {
+ $event['recurrence']['EXCEPTIONS'] = $master['recurrence']['EXCEPTIONS'];
+ }
+ else if ($master['_instance']) {
+ $event['_instance'] = $master['_instance'];
+ $event['recurrence_date'] = $master['recurrence_date'];
+ }
+
+ // TODO: forward changes to exceptions (which do not yet have differing values stored)
+ if (is_array($event['recurrence']) && is_array($event['recurrence']['EXCEPTIONS']) && !$with_exceptions) {
+ // determine added and removed attendees
+ $old_attendees = $current_attendees = $added_attendees = array();
+ foreach ((array)$old['attendees'] as $attendee) {
+ $old_attendees[] = $attendee['email'];
+ }
+ foreach ((array)$event['attendees'] as $attendee) {
+ $current_attendees[] = $attendee['email'];
+ if (!in_array($attendee['email'], $old_attendees)) {
+ $added_attendees[] = $attendee;
+ }
+ }
+ $removed_attendees = array_diff($old_attendees, $current_attendees);
+
+ foreach ($event['recurrence']['EXCEPTIONS'] as $i => $exception) {
+ calendar::merge_attendee_data($event['recurrence']['EXCEPTIONS'][$i], $added_attendees, $removed_attendees);
+ }
+
+ // adjust recurrence-id when start changed and therefore the entire recurrence chain changes
+ if ($old_start_date != $new_start_date || $old_start_time != $new_start_time) {
+ $recurrence_id_format = libcalendaring::recurrence_id_format($event);
+ foreach ($event['recurrence']['EXCEPTIONS'] as $i => $exception) {
+ $recurrence_id = is_a($exception['recurrence_date'], 'DateTime') ? $exception['recurrence_date'] :
+ rcube_utils::anytodatetime($exception['_instance'], $old['start']->getTimezone());
+ if (is_a($recurrence_id, 'DateTime')) {
+ $recurrence_id->add($date_shift);
+ $event['recurrence']['EXCEPTIONS'][$i]['recurrence_date'] = $recurrence_id;
+ $event['recurrence']['EXCEPTIONS'][$i]['_instance'] = $recurrence_id->format($recurrence_id_format);
+ }
+ }
+ }
+
+ // set link to top-level exceptions
+ $event['exceptions'] = &$event['recurrence']['EXCEPTIONS'];
+ }
+
+ // unset _dateonly flags in (cached) date objects
+ unset($event['start']->_dateonly, $event['end']->_dateonly);
+
+ $success = $storage->update_event($event) ? $event['id'] : false; // return master UID
+ break;
+ }
+
+ if ($success && $this->freebusy_trigger)
+ $this->rc->output->command('plugin.ping_url', array('action' => 'calendar/push-freebusy', 'source' => $storage->id));
+
+ return $success;
+ }
+
+ /**
+ * Determine whether the current change affects scheduling and reset attendee status accordingly
+ */
+ public function check_scheduling(&$event, $old, $update = true)
+ {
+ // skip this check when importing iCal/iTip events
+ if (isset($event['sequence']) || !empty($event['_method'])) {
+ return false;
+ }
+
+ // iterate through the list of properties considered 'significant' for scheduling
+ $kolab_event = $old['_formatobj'] ?: new kolab_format_event();
+ $reschedule = $kolab_event->check_rescheduling($event, $old);
+
+ // reset all attendee status to needs-action (#4360)
+ if ($update && $reschedule && is_array($event['attendees'])) {
+ $is_organizer = false;
+ $emails = $this->cal->get_user_emails();
+ $attendees = $event['attendees'];
+ foreach ($attendees as $i => $attendee) {
+ if ($attendee['role'] == 'ORGANIZER' && $attendee['email'] && in_array(strtolower($attendee['email']), $emails)) {
+ $is_organizer = true;
+ }
+ else if ($attendee['role'] != 'ORGANIZER' && $attendee['role'] != 'NON-PARTICIPANT' && $attendee['status'] != 'DELEGATED') {
+ $attendees[$i]['status'] = 'NEEDS-ACTION';
+ $attendees[$i]['rsvp'] = true;
+ }
+ }
+
+ // update attendees only if I'm the organizer
+ if ($is_organizer || ($event['organizer'] && in_array(strtolower($event['organizer']['email']), $emails))) {
+ $event['attendees'] = $attendees;
+ }
+ }
+
+ return $reschedule;
+ }
+
+ /**
+ * Apply the given changes to already existing exceptions
+ */
+ protected function update_recurrence_exceptions(&$master, $event, $old, $savemode)
+ {
+ $saved = false;
+ $existing = null;
+
+ // determine added and removed attendees
+ $added_attendees = $removed_attendees = array();
+ if ($savemode == 'future') {
+ $old_attendees = $current_attendees = array();
+ foreach ((array)$old['attendees'] as $attendee) {
+ $old_attendees[] = $attendee['email'];
+ }
+ foreach ((array)$event['attendees'] as $attendee) {
+ $current_attendees[] = $attendee['email'];
+ if (!in_array($attendee['email'], $old_attendees)) {
+ $added_attendees[] = $attendee;
+ }
+ }
+ $removed_attendees = array_diff($old_attendees, $current_attendees);
+ }
+
+ foreach ($master['recurrence']['EXCEPTIONS'] as $i => $exception) {
+ // update a specific instance
+ if ($exception['_instance'] == $old['_instance']) {
+ $existing = $i;
+
+ // check savemode against existing exception mode.
+ // if matches, we can update this existing exception
+ if ((bool)$exception['thisandfuture'] === ($savemode == 'future')) {
+ $event['_instance'] = $old['_instance'];
+ $event['thisandfuture'] = $old['thisandfuture'];
+ $event['recurrence_date'] = $old['recurrence_date'];
+ $master['recurrence']['EXCEPTIONS'][$i] = $event;
+ $saved = true;
+ }
+ }
+ // merge the new event properties onto future exceptions
+ if ($savemode == 'future' && $exception['_instance'] >= $old['_instance']) {
+ unset($event['thisandfuture']);
+ self::merge_exception_data($master['recurrence']['EXCEPTIONS'][$i], $event, array('attendees'));
+
+ if (!empty($added_attendees) || !empty($removed_attendees)) {
+ calendar::merge_attendee_data($master['recurrence']['EXCEPTIONS'][$i], $added_attendees, $removed_attendees);
+ }
+ }
+ }
+/*
+ // we could not update the existing exception due to savemode mismatch...
+ if (!$saved && $existing !== null && $master['recurrence']['EXCEPTIONS'][$existing]['thisandfuture']) {
+ // ... try to move the existing this-and-future exception to the next occurrence
+ foreach ($this->get_recurring_events($master, $existing['start']) as $candidate) {
+ // our old this-and-future exception is obsolete
+ if ($candidate['thisandfuture']) {
+ unset($master['recurrence']['EXCEPTIONS'][$existing]);
+ $saved = true;
+ break;
+ }
+ // this occurrence doesn't yet have an exception
+ else if (!$candidate['isexception']) {
+ $event['_instance'] = $candidate['_instance'];
+ $event['recurrence_date'] = $candidate['recurrence_date'];
+ $master['recurrence']['EXCEPTIONS'][$i] = $event;
+ $saved = true;
+ break;
+ }
+ }
+ }
+*/
+
+ // set link to top-level exceptions
+ $master['exceptions'] = &$master['recurrence']['EXCEPTIONS'];
+
+ // returning false here will add a new exception
+ return $saved;
+ }
+
+ /**
+ * Add or update the given event as an exception to $master
+ */
+ public static function add_exception(&$master, $event, $old = null)
+ {
+ if ($old) {
+ $event['_instance'] = $old['_instance'];
+ if (!$event['recurrence_date'])
+ $event['recurrence_date'] = $old['recurrence_date'] ?: $old['start'];
+ }
+ else if (!$event['recurrence_date']) {
+ $event['recurrence_date'] = $event['start'];
+ }
+
+ if (!$event['_instance'] && is_a($event['recurrence_date'], 'DateTime')) {
+ $event['_instance'] = libcalendaring::recurrence_instance_identifier($event);
+ }
+
+ if (!is_array($master['exceptions']) && is_array($master['recurrence']['EXCEPTIONS'])) {
+ $master['exceptions'] = &$master['recurrence']['EXCEPTIONS'];
+ }
+
+ $existing = false;
+ foreach ((array)$master['exceptions'] as $i => $exception) {
+ if ($exception['_instance'] == $event['_instance']) {
+ $master['exceptions'][$i] = $event;
+ $existing = true;
+ }
+ }
+
+ if (!$existing) {
+ $master['exceptions'][] = $event;
+ }
+
+ return true;
+ }
+
+ /**
+ * Remove the noreply flags from attendees
+ */
+ public static function clear_attandee_noreply(&$event)
+ {
+ foreach ((array)$event['attendees'] as $i => $attendee) {
+ unset($event['attendees'][$i]['noreply']);
+ }
+ }
+
+
+ /**
+ * Merge certain properties from the overlay event to the base event object
+ *
+ * @param array The event object to be altered
+ * @param array The overlay event object to be merged over $event
+ * @param array List of properties not allowed to be overwritten
+ */
+ public static function merge_exception_data(&$event, $overlay, $blacklist = null)
+ {
+ $forbidden = array('id','uid','recurrence','recurrence_date','thisandfuture','organizer','_attachments');
+
+ if (is_array($blacklist))
+ $forbidden = array_merge($forbidden, $blacklist);
+
+ // compute date offset from the exception
+ if ($overlay['start'] instanceof DateTime && $overlay['recurrence_date'] instanceof DateTime) {
+ $date_offset = $overlay['recurrence_date']->diff($overlay['start']);
+ }
+
+ foreach ($overlay as $prop => $value) {
+ if ($prop == 'start' || $prop == 'end') {
+ if (is_object($event[$prop]) && $event[$prop] instanceof DateTime) {
+ // set date value if overlay is an exception of the current instance
+ if (substr($overlay['_instance'], 0, 8) == substr($event['_instance'], 0, 8)) {
+ $event[$prop]->setDate(intval($value->format('Y')), intval($value->format('n')), intval($value->format('j')));
+ }
+ // apply date offset
+ else if ($date_offset) {
+ $event[$prop]->add($date_offset);
+ }
+ // adjust time of the recurring event instance
+ $event[$prop]->setTime($value->format('G'), intval($value->format('i')), intval($value->format('s')));
+ }
+ }
+ else if ($prop == 'thisandfuture' && $overlay['_instance'] == $event['_instance']) {
+ $event[$prop] = $value;
+ }
+ else if ($prop[0] != '_' && !in_array($prop, $forbidden))
+ $event[$prop] = $value;
+ }
+ }
+
+ /**
+ * Get events from source.
+ *
+ * @param integer Event's new start (unix timestamp)
+ * @param integer Event's new end (unix timestamp)
+ * @param string Search query (optional)
+ * @param mixed List of calendar IDs to load events from (either as array or comma-separated string)
+ * @param boolean Include virtual events (optional)
+ * @param integer Only list events modified since this time (unix timestamp)
+ * @return array A list of event records
+ */
+ public function load_events($start, $end, $search = null, $calendars = null, $virtual = 1, $modifiedsince = null)
+ {
+ if ($calendars && is_string($calendars))
+ $calendars = explode(',', $calendars);
+ else if (!$calendars)
+ $calendars = array_keys($this->calendars);
+
+ $query = array();
+ if ($modifiedsince)
+ $query[] = array('changed', '>=', $modifiedsince);
+
+ $events = $categories = array();
+ foreach ($calendars as $cid) {
+ if ($storage = $this->get_calendar($cid)) {
+ $events = array_merge($events, $storage->list_events($start, $end, $search, $virtual, $query));
+ $categories += $storage->categories;
+ }
+ }
+
+ // add events from the address books birthday calendar
+ if (in_array(self::BIRTHDAY_CALENDAR_ID, $calendars)) {
+ $events = array_merge($events, $this->load_birthday_events($start, $end, $search, $modifiedsince));
+ }
+
+ // add new categories to user prefs
+ $old_categories = $this->rc->config->get('calendar_categories', $this->default_categories);
+ if ($newcats = array_udiff(array_keys($categories), array_keys($old_categories), function($a, $b){ return strcasecmp($a, $b); })) {
+ foreach ($newcats as $category)
+ $old_categories[$category] = ''; // no color set yet
+ $this->rc->user->save_prefs(array('calendar_categories' => $old_categories));
+ }
+
+ array_walk($events, 'kolab_driver::to_rcube_event');
+ return $events;
+ }
+
+ /**
+ * Get number of events in the given calendar
+ *
+ * @param mixed List of calendar IDs to count events (either as array or comma-separated string)
+ * @param integer Date range start (unix timestamp)
+ * @param integer Date range end (unix timestamp)
+ * @return array Hash array with counts grouped by calendar ID
+ */
+ public function count_events($calendars, $start, $end = null)
+ {
+ $counts = array();
+
+ if ($calendars && is_string($calendars))
+ $calendars = explode(',', $calendars);
+ else if (!$calendars)
+ $calendars = array_keys($this->calendars);
+
+ foreach ($calendars as $cid) {
+ if ($storage = $this->get_calendar($cid)) {
+ $counts[$cid] = $storage->count_events($start, $end);
+ }
+ }
+
+ return $counts;
+ }
+
+ /**
+ * Get a list of pending alarms to be displayed to the user
+ *
+ * @see calendar_driver::pending_alarms()
+ */
+ public function pending_alarms($time, $calendars = null)
+ {
+ $interval = 300;
+ $time -= $time % 60;
+
+ $slot = $time;
+ $slot -= $slot % $interval;
+
+ $last = $time - max(60, $this->rc->config->get('refresh_interval', 0));
+ $last -= $last % $interval;
+
+ // only check for alerts once in 5 minutes
+ if ($last == $slot)
+ return array();
+
+ if ($calendars && is_string($calendars))
+ $calendars = explode(',', $calendars);
+
+ $time = $slot + $interval;
+
+ $candidates = array();
+ $query = array(array('tags', '=', 'x-has-alarms'));
+ foreach ($this->calendars as $cid => $calendar) {
+ // skip calendars with alarms disabled
+ if (!$calendar->alarms || ($calendars && !in_array($cid, $calendars)))
+ continue;
+
+ foreach ($calendar->list_events($time, $time + 86400 * 365, null, 1, $query) as $e) {
+ // add to list if alarm is set
+ $alarm = libcalendaring::get_next_alarm($e);
+ if ($alarm && $alarm['time'] && $alarm['time'] >= $last && in_array($alarm['action'], $this->alarm_types)) {
+ $id = $alarm['id']; // use alarm-id as primary identifier
+ $candidates[$id] = array(
+ 'id' => $id,
+ 'title' => $e['title'],
+ 'location' => $e['location'],
+ 'start' => $e['start'],
+ 'end' => $e['end'],
+ 'notifyat' => $alarm['time'],
+ 'action' => $alarm['action'],
+ );
+ }
+ }
+ }
+
+ // get alarm information stored in local database
+ if (!empty($candidates)) {
+ $alarm_ids = array_map(array($this->rc->db, 'quote'), array_keys($candidates));
+ $result = $this->rc->db->query("SELECT *"
+ . " FROM " . $this->rc->db->table_name('kolab_alarms', true)
+ . " WHERE `alarm_id` IN (" . join(',', $alarm_ids) . ")"
+ . " AND `user_id` = ?",
+ $this->rc->user->ID
+ );
+
+ while ($result && ($e = $this->rc->db->fetch_assoc($result))) {
+ $dbdata[$e['alarm_id']] = $e;
+ }
+ }
+
+ $alarms = array();
+ foreach ($candidates as $id => $alarm) {
+ // skip dismissed alarms
+ if ($dbdata[$id]['dismissed'])
+ continue;
+
+ // snooze function may have shifted alarm time
+ $notifyat = $dbdata[$id]['notifyat'] ? strtotime($dbdata[$id]['notifyat']) : $alarm['notifyat'];
+ if ($notifyat <= $time)
+ $alarms[] = $alarm;
+ }
+
+ return $alarms;
+ }
+
+ /**
+ * Feedback after showing/sending an alarm notification
+ *
+ * @see calendar_driver::dismiss_alarm()
+ */
+ public function dismiss_alarm($alarm_id, $snooze = 0)
+ {
+ $alarms_table = $this->rc->db->table_name('kolab_alarms', true);
+ // delete old alarm entry
+ $this->rc->db->query("DELETE FROM $alarms_table"
+ . " WHERE `alarm_id` = ? AND `user_id` = ?",
+ $alarm_id,
+ $this->rc->user->ID
+ );
+
+ // set new notifyat time or unset if not snoozed
+ $notifyat = $snooze > 0 ? date('Y-m-d H:i:s', time() + $snooze) : null;
+
+ $query = $this->rc->db->query("INSERT INTO $alarms_table"
+ . " (`alarm_id`, `user_id`, `dismissed`, `notifyat`)"
+ . " VALUES (?, ?, ?, ?)",
+ $alarm_id,
+ $this->rc->user->ID,
+ $snooze > 0 ? 0 : 1,
+ $notifyat
+ );
+
+ return $this->rc->db->affected_rows($query);
+ }
+
+ /**
+ * List attachments from the given event
+ */
+ public function list_attachments($event)
+ {
+ if (!($storage = $this->get_calendar($event['calendar'])))
+ return false;
+
+ $event = $storage->get_event($event['id']);
+
+ return $event['attachments'];
+ }
+
+ /**
+ * Get attachment properties
+ */
+ public function get_attachment($id, $event)
+ {
+ if (!($storage = $this->get_calendar($event['calendar'])))
+ return false;
+
+ // get old revision of event
+ if ($event['rev']) {
+ $event = $this->get_event_revison($event, $event['rev'], true);
+ }
+ else {
+ $event = $storage->get_event($event['id']);
+ }
+
+ if ($event && !empty($event['_attachments'])) {
+ foreach ($event['_attachments'] as $att) {
+ if ($att['id'] == $id) {
+ return $att;
+ }
+ }
+ }
+
+ return null;
+ }
+
+ /**
+ * Get attachment body
+ * @see calendar_driver::get_attachment_body()
+ */
+ public function get_attachment_body($id, $event)
+ {
+ if (!($cal = $this->get_calendar($event['calendar'])))
+ return false;
+
+ // get old revision of event
+ if ($event['rev']) {
+ if (empty($this->bonnie_api)) {
+ return false;
+ }
+
+ $cid = substr($id, 4);
+
+ // call Bonnie API and get the raw mime message
+ list($uid, $mailbox, $msguid) = $this->_resolve_event_identity($event);
+ if ($msg_raw = $this->bonnie_api->rawdata('event', $uid, $event['rev'], $mailbox, $msguid)) {
+ // parse the message and find the part with the matching content-id
+ $message = rcube_mime::parse_message($msg_raw);
+ foreach ((array)$message->parts as $part) {
+ if ($part->headers['content-id'] && trim($part->headers['content-id'], '<>') == $cid) {
+ return $part->body;
+ }
+ }
+ }
+
+ return false;
+ }
+
+ return $cal->get_attachment_body($id, $event);
+ }
+
+ /**
+ * Build a struct representing the given message reference
+ *
+ * @see calendar_driver::get_message_reference()
+ */
+ public function get_message_reference($uri_or_headers, $folder = null)
+ {
+ if (is_object($uri_or_headers)) {
+ $uri_or_headers = kolab_storage_config::get_message_uri($uri_or_headers, $folder);
+ }
+
+ if (is_string($uri_or_headers)) {
+ return kolab_storage_config::get_message_reference($uri_or_headers, 'event');
+ }
+
+ return false;
+ }
+
+ /**
+ * List availabale categories
+ * The default implementation reads them from config/user prefs
+ */
+ public function list_categories()
+ {
+ // FIXME: complete list with categories saved in config objects (KEP:12)
+ return $this->rc->config->get('calendar_categories', $this->default_categories);
+ }
+
+ /**
+ * Create instances of a recurring event
+ *
+ * @param array Hash array with event properties
+ * @param object DateTime Start date of the recurrence window
+ * @param object DateTime End date of the recurrence window
+ * @return array List of recurring event instances
+ */
+ public function get_recurring_events($event, $start, $end = null)
+ {
+ // load the given event data into a libkolabxml container
+ if (!$event['_formatobj']) {
+ $event_xml = new kolab_format_event();
+ $event_xml->set($event);
+ $event['_formatobj'] = $event_xml;
+ }
+
+ $this->_read_calendars();
+ $storage = reset($this->calendars);
+ return $storage->get_recurring_events($event, $start, $end);
+ }
+
+ /**
+ *
+ */
+ private function get_recurrence_count($event, $dtstart)
+ {
+ // use libkolab to compute recurring events
+ if (class_exists('kolabcalendaring') && $event['_formatobj']) {
+ $recurrence = new kolab_date_recurrence($event['_formatobj']);
+ }
+ else {
+ // fallback to local recurrence implementation
+ require_once($this->cal->home . '/lib/calendar_recurrence.php');
+ $recurrence = new calendar_recurrence($this->cal, $event);
+ }
+
+ $count = 0;
+ while (($next_event = $recurrence->next_instance()) && $next_event['start'] <= $dtstart && $count < 1000) {
+ $count++;
+ }
+
+ return $count;
+ }
+
+ /**
+ * Fetch free/busy information from a person within the given range
+ */
+ public function get_freebusy_list($email, $start, $end)
+ {
+ if (empty($email)/* || $end < time()*/)
+ return false;
+
+ // map vcalendar fbtypes to internal values
+ $fbtypemap = array(
+ 'FREE' => calendar::FREEBUSY_FREE,
+ 'BUSY-TENTATIVE' => calendar::FREEBUSY_TENTATIVE,
+ 'X-OUT-OF-OFFICE' => calendar::FREEBUSY_OOF,
+ 'OOF' => calendar::FREEBUSY_OOF);
+
+ // ask kolab server first
+ try {
+ $request_config = array(
+ 'store_body' => true,
+ 'follow_redirects' => true,
+ );
+ $request = libkolab::http_request(kolab_storage::get_freebusy_url($email), 'GET', $request_config);
+ $response = $request->send();
+
+ // authentication required
+ if ($response->getStatus() == 401) {
+ $request->setAuth($this->rc->user->get_username(), $this->rc->decrypt($_SESSION['password']));
+ $response = $request->send();
+ }
+
+ if ($response->getStatus() == 200)
+ $fbdata = $response->getBody();
+
+ unset($request, $response);
+ }
+ catch (Exception $e) {
+ PEAR::raiseError("Error fetching free/busy information: " . $e->getMessage());
+ }
+
+ // get free-busy url from contacts
+ if (!$fbdata) {
+ $fburl = null;
+ foreach ((array)$this->rc->config->get('autocomplete_addressbooks', 'sql') as $book) {
+ $abook = $this->rc->get_address_book($book);
+
+ if ($result = $abook->search(array('email'), $email, true, true, true/*, 'freebusyurl'*/)) {
+ while ($contact = $result->iterate()) {
+ if ($fburl = $contact['freebusyurl']) {
+ $fbdata = @file_get_contents($fburl);
+ break;
+ }
+ }
+ }
+
+ if ($fbdata)
+ break;
+ }
+ }
+
+ // parse free-busy information using Horde classes
+ if ($fbdata) {
+ $ical = $this->cal->get_ical();
+ $ical->import($fbdata);
+ if ($fb = $ical->freebusy) {
+ $result = array();
+ foreach ($fb['periods'] as $tuple) {
+ list($from, $to, $type) = $tuple;
+ $result[] = array($from->format('U'), $to->format('U'), isset($fbtypemap[$type]) ? $fbtypemap[$type] : calendar::FREEBUSY_BUSY);
+ }
+
+ // we take 'dummy' free-busy lists as "unknown"
+ if (empty($result) && !empty($fb['comment']) && stripos($fb['comment'], 'dummy'))
+ return false;
+
+ // set period from $start till the begin of the free-busy information as 'unknown'
+ if ($fb['start'] && ($fbstart = $fb['start']->format('U')) && $start < $fbstart) {
+ array_unshift($result, array($start, $fbstart, calendar::FREEBUSY_UNKNOWN));
+ }
+ // pad period till $end with status 'unknown'
+ if ($fb['end'] && ($fbend = $fb['end']->format('U')) && $fbend < $end) {
+ $result[] = array($fbend, $end, calendar::FREEBUSY_UNKNOWN);
+ }
+
+ return $result;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * Handler to push folder triggers when sent from client.
+ * Used to push free-busy changes asynchronously after updating an event
+ */
+ public function push_freebusy()
+ {
+ // make shure triggering completes
+ set_time_limit(0);
+ ignore_user_abort(true);
+
+ $cal = rcube_utils::get_input_value('source', rcube_utils::INPUT_GPC);
+ if (!($cal = $this->get_calendar($cal)))
+ return false;
+
+ // trigger updates on folder
+ $trigger = $cal->storage->trigger();
+ if (is_object($trigger) && is_a($trigger, 'PEAR_Error')) {
+ rcube::raise_error(array(
+ 'code' => 900, 'type' => 'php',
+ 'file' => __FILE__, 'line' => __LINE__,
+ 'message' => "Failed triggering folder. Error was " . $trigger->getMessage()),
+ true, false);
+ }
+
+ exit;
+ }
+
+
+ /**
+ * Convert from driver format to external caledar app data
+ */
+ public static function to_rcube_event(&$record)
+ {
+ if (!is_array($record))
+ return $record;
+
+ $record['id'] = $record['uid'];
+
+ if ($record['_instance']) {
+ $record['id'] .= '-' . $record['_instance'];
+
+ if (!$record['recurrence_id'] && !empty($record['recurrence']))
+ $record['recurrence_id'] = $record['uid'];
+ }
+
+ // all-day events go from 12:00 - 13:00
+ if (is_a($record['start'], 'DateTime') && $record['end'] <= $record['start'] && $record['allday']) {
+ $record['end'] = clone $record['start'];
+ $record['end']->add(new DateInterval('PT1H'));
+ }
+
+ // translate internal '_attachments' to external 'attachments' list
+ if (!empty($record['_attachments'])) {
+ foreach ($record['_attachments'] as $key => $attachment) {
+ if ($attachment !== false) {
+ if (!$attachment['name'])
+ $attachment['name'] = $key;
+
+ unset($attachment['path'], $attachment['content']);
+ $attachments[] = $attachment;
+ }
+ }
+
+ $record['attachments'] = $attachments;
+ }
+
+ if (!empty($record['attendees'])) {
+ foreach ((array)$record['attendees'] as $i => $attendee) {
+ if (is_array($attendee['delegated-from'])) {
+ $record['attendees'][$i]['delegated-from'] = join(', ', $attendee['delegated-from']);
+ }
+ if (is_array($attendee['delegated-to'])) {
+ $record['attendees'][$i]['delegated-to'] = join(', ', $attendee['delegated-to']);
+ }
+ }
+ }
+
+ // Roundcube only supports one category assignment
+ if (is_array($record['categories']))
+ $record['categories'] = $record['categories'][0];
+
+ // the cancelled flag transltes into status=CANCELLED
+ if ($record['cancelled'])
+ $record['status'] = 'CANCELLED';
+
+ // The web client only supports DISPLAY type of alarms
+ if (!empty($record['alarms']))
+ $record['alarms'] = preg_replace('/:[A-Z]+$/', ':DISPLAY', $record['alarms']);
+
+ // remove empty recurrence array
+ if (empty($record['recurrence']))
+ unset($record['recurrence']);
+
+ // clean up exception data
+ if (is_array($record['recurrence']['EXCEPTIONS'])) {
+ array_walk($record['recurrence']['EXCEPTIONS'], function(&$exception) {
+ unset($exception['_mailbox'], $exception['_msguid'], $exception['_formatobj'], $exception['_attachments']);
+ });
+ }
+
+ unset($record['_mailbox'], $record['_msguid'], $record['_type'], $record['_size'],
+ $record['_formatobj'], $record['_attachments'], $record['exceptions'], $record['x-custom']);
+
+ return $record;
+ }
+
+ /**
+ *
+ */
+ public static function from_rcube_event($event, $old = array())
+ {
+ // in kolab_storage attachments are indexed by content-id
+ if (is_array($event['attachments']) || !empty($event['deleted_attachments'])) {
+ $event['_attachments'] = array();
+
+ foreach ($event['attachments'] as $attachment) {
+ $key = null;
+ // Roundcube ID has nothing to do with the storage ID, remove it
+ if ($attachment['content'] || $attachment['path']) {
+ unset($attachment['id']);
+ }
+ else {
+ foreach ((array)$old['_attachments'] as $cid => $oldatt) {
+ if ($attachment['id'] == $oldatt['id'])
+ $key = $cid;
+ }
+ }
+
+ // flagged for deletion => set to false
+ if ($attachment['_deleted'] || in_array($attachment['id'], (array)$event['deleted_attachments'])) {
+ $event['_attachments'][$key] = false;
+ }
+ // replace existing entry
+ else if ($key) {
+ $event['_attachments'][$key] = $attachment;
+ }
+ // append as new attachment
+ else {
+ $event['_attachments'][] = $attachment;
+ }
+ }
+
+ $event['_attachments'] = array_merge((array)$old['_attachments'], $event['_attachments']);
+
+ // attachments flagged for deletion => set to false
+ foreach ($event['_attachments'] as $key => $attachment) {
+ if ($attachment['_deleted'] || in_array($attachment['id'], (array)$event['deleted_attachments'])) {
+ $event['_attachments'][$key] = false;
+ }
+ }
+ }
+
+ return $event;
+ }
+
+
+ /**
+ * Set CSS class according to the event's attendde partstat
+ */
+ public static function add_partstat_class($event, $partstats, $user = null)
+ {
+ // set classes according to PARTSTAT
+ if (is_array($event['attendees'])) {
+ $user_emails = libcalendaring::get_instance()->get_user_emails($user);
+ $partstat = 'UNKNOWN';
+ foreach ($event['attendees'] as $attendee) {
+ if (in_array($attendee['email'], $user_emails)) {
+ $partstat = $attendee['status'];
+ break;
+ }
+ }
+
+ if (in_array($partstat, $partstats)) {
+ $event['className'] = trim($event['className'] . ' fc-invitation-' . strtolower($partstat));
+ }
+ }
+
+ return $event;
+ }
+
+ /**
+ * Provide a list of revisions for the given event
+ *
+ * @param array $event Hash array with event properties
+ *
+ * @return array List of changes, each as a hash array
+ * @see calendar_driver::get_event_changelog()
+ */
+ public function get_event_changelog($event)
+ {
+ if (empty($this->bonnie_api)) {
+ return false;
+ }
+
+ list($uid, $mailbox, $msguid) = $this->_resolve_event_identity($event);
+
+ $result = $this->bonnie_api->changelog('event', $uid, $mailbox, $msguid);
+ if (is_array($result) && $result['uid'] == $uid) {
+ return $result['changes'];
+ }
+
+ return false;
+ }
+
+ /**
+ * Get a list of property changes beteen two revisions of an event
+ *
+ * @param array $event Hash array with event properties
+ * @param mixed $rev1 Old Revision
+ * @param mixed $rev2 New Revision
+ *
+ * @return array List of property changes, each as a hash array
+ * @see calendar_driver::get_event_diff()
+ */
+ public function get_event_diff($event, $rev1, $rev2)
+ {
+ if (empty($this->bonnie_api)) {
+ return false;
+ }
+
+ list($uid, $mailbox, $msguid) = $this->_resolve_event_identity($event);
+
+ // get diff for the requested recurrence instance
+ $instance_id = $event['id'] != $uid ? substr($event['id'], strlen($uid) + 1) : null;
+
+ // call Bonnie API
+ $result = $this->bonnie_api->diff('event', $uid, $rev1, $rev2, $mailbox, $msguid, $instance_id);
+ if (is_array($result) && $result['uid'] == $uid) {
+ $result['rev1'] = $rev1;
+ $result['rev2'] = $rev2;
+
+ $keymap = array(
+ 'dtstart' => 'start',
+ 'dtend' => 'end',
+ 'dstamp' => 'changed',
+ 'summary' => 'title',
+ 'alarm' => 'alarms',
+ 'attendee' => 'attendees',
+ 'attach' => 'attachments',
+ 'rrule' => 'recurrence',
+ 'transparency' => 'free_busy',
+ 'classification' => 'sensitivity',
+ 'lastmodified-date' => 'changed',
+ );
+ $prop_keymaps = array(
+ 'attachments' => array('fmttype' => 'mimetype', 'label' => 'name'),
+ 'attendees' => array('partstat' => 'status'),
+ );
+ $special_changes = array();
+
+ // map kolab event properties to keys the client expects
+ array_walk($result['changes'], function(&$change, $i) use ($keymap, $prop_keymaps, $special_changes) {
+ if (array_key_exists($change['property'], $keymap)) {
+ $change['property'] = $keymap[$change['property']];
+ }
+ // translate free_busy values
+ if ($change['property'] == 'free_busy') {
+ $change['old'] = $old['old'] ? 'free' : 'busy';
+ $change['new'] = $old['new'] ? 'free' : 'busy';
+ }
+ // map alarms trigger value
+ if ($change['property'] == 'alarms') {
+ if (is_array($change['old']) && is_array($change['old']['trigger']))
+ $change['old']['trigger'] = $change['old']['trigger']['value'];
+ if (is_array($change['new']) && is_array($change['new']['trigger']))
+ $change['new']['trigger'] = $change['new']['trigger']['value'];
+ }
+ // make all property keys uppercase
+ if ($change['property'] == 'recurrence') {
+ $special_changes['recurrence'] = $i;
+ foreach (array('old','new') as $m) {
+ if (is_array($change[$m])) {
+ $props = array();
+ foreach ($change[$m] as $k => $v)
+ $props[strtoupper($k)] = $v;
+ $change[$m] = $props;
+ }
+ }
+ }
+ // map property keys names
+ if (is_array($prop_keymaps[$change['property']])) {
+ foreach ($prop_keymaps[$change['property']] as $k => $dest) {
+ if (is_array($change['old']) && array_key_exists($k, $change['old'])) {
+ $change['old'][$dest] = $change['old'][$k];
+ unset($change['old'][$k]);
+ }
+ if (is_array($change['new']) && array_key_exists($k, $change['new'])) {
+ $change['new'][$dest] = $change['new'][$k];
+ unset($change['new'][$k]);
+ }
+ }
+ }
+
+ if ($change['property'] == 'exdate') {
+ $special_changes['exdate'] = $i;
+ }
+ else if ($change['property'] == 'rdate') {
+ $special_changes['rdate'] = $i;
+ }
+ });
+
+ // merge some recurrence changes
+ foreach (array('exdate','rdate') as $prop) {
+ if (array_key_exists($prop, $special_changes)) {
+ $exdate = $result['changes'][$special_changes[$prop]];
+ if (array_key_exists('recurrence', $special_changes)) {
+ $recurrence = &$result['changes'][$special_changes['recurrence']];
+ }
+ else {
+ $i = count($result['changes']);
+ $result['changes'][$i] = array('property' => 'recurrence', 'old' => array(), 'new' => array());
+ $recurrence = &$result['changes'][$i]['recurrence'];
+ }
+ $key = strtoupper($prop);
+ $recurrence['old'][$key] = $exdate['old'];
+ $recurrence['new'][$key] = $exdate['new'];
+ unset($result['changes'][$special_changes[$prop]]);
+ }
+ }
+
+ return $result;
+ }
+
+ return false;
+ }
+
+ /**
+ * Return full data of a specific revision of an event
+ *
+ * @param array Hash array with event properties
+ * @param mixed $rev Revision number
+ *
+ * @return array Event object as hash array
+ * @see calendar_driver::get_event_revison()
+ */
+ public function get_event_revison($event, $rev, $internal = false)
+ {
+ if (empty($this->bonnie_api)) {
+ return false;
+ }
+
+ $eventid = $event['id'];
+ $calid = $event['calendar'];
+ list($uid, $mailbox, $msguid) = $this->_resolve_event_identity($event);
+
+ // call Bonnie API
+ $result = $this->bonnie_api->get('event', $uid, $rev, $mailbox, $msguid);
+ if (is_array($result) && $result['uid'] == $uid && !empty($result['xml'])) {
+ $format = kolab_format::factory('event');
+ $format->load($result['xml']);
+ $event = $format->to_array();
+ $format->get_attachments($event, true);
+
+ // get the right instance from a recurring event
+ if ($eventid != $event['uid']) {
+ $instance_id = substr($eventid, strlen($event['uid']) + 1);
+
+ // check for recurrence exception first
+ if ($instance = $format->get_instance($instance_id)) {
+ $event = $instance;
+ }
+ else {
+ // not a exception, compute recurrence...
+ $event['_formatobj'] = $format;
+ $recurrence_date = rcube_utils::anytodatetime($instance_id, $event['start']->getTimezone());
+ foreach ($this->get_recurring_events($event, $event['start'], $recurrence_date) as $instance) {
+ if ($instance['id'] == $eventid) {
+ $event = $instance;
+ break;
+ }
+ }
+ }
+ }
+
+ if ($format->is_valid()) {
+ $event['calendar'] = $calid;
+ $event['rev'] = $result['rev'];
+ return $internal ? $event : self::to_rcube_event($event);
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * Command the backend to restore a certain revision of an event.
+ * This shall replace the current event with an older version.
+ *
+ * @param mixed UID string or hash array with event properties:
+ * id: Event identifier
+ * calendar: Calendar identifier
+ * @param mixed $rev Revision number
+ *
+ * @return boolean True on success, False on failure
+ */
+ public function restore_event_revision($event, $rev)
+ {
+ if (empty($this->bonnie_api)) {
+ return false;
+ }
+
+ list($uid, $mailbox, $msguid) = $this->_resolve_event_identity($event);
+ $calendar = $this->get_calendar($event['calendar']);
+ $success = false;
+
+ if ($calendar && $calendar->storage && $calendar->editable) {
+ if ($raw_msg = $this->bonnie_api->rawdata('event', $uid, $rev, $mailbox)) {
+ $imap = $this->rc->get_storage();
+
+ // insert $raw_msg as new message
+ if ($imap->save_message($calendar->storage->name, $raw_msg, null, false)) {
+ $success = true;
+
+ // delete old revision from imap and cache
+ $imap->delete_message($msguid, $calendar->storage->name);
+ $calendar->storage->cache->set($msguid, false);
+ }
+ }
+ }
+
+ return $success;
+ }
+
+ /**
+ * Helper method to resolved the given event identifier into uid and folder
+ *
+ * @return array (uid,folder,msguid) tuple
+ */
+ private function _resolve_event_identity($event)
+ {
+ $mailbox = $msguid = null;
+ if (is_array($event)) {
+ $uid = $event['uid'] ?: $event['id'];
+ if (($cal = $this->get_calendar($event['calendar'])) && !($cal instanceof kolab_invitation_calendar)) {
+ $mailbox = $cal->get_mailbox_id();
+
+ // get event object from storage in order to get the real object uid an msguid
+ if ($ev = $cal->get_event($event['id'])) {
+ $msguid = $ev['_msguid'];
+ $uid = $ev['uid'];
+ }
+ }
+ }
+ else {
+ $uid = $event;
+
+ // get event object from storage in order to get the real object uid an msguid
+ if ($ev = $this->get_event($event)) {
+ $mailbox = $ev['_mailbox'];
+ $msguid = $ev['_msguid'];
+ $uid = $ev['uid'];
+ }
+ }
+
+ return array($uid, $mailbox, $msguid);
+ }
+
+ /**
+ * Callback function to produce driver-specific calendar create/edit form
+ *
+ * @param string Request action 'form-edit|form-new'
+ * @param array Calendar properties (e.g. id, color)
+ * @param array Edit form fields
+ *
+ * @return string HTML content of the form
+ */
+ public function calendar_form($action, $calendar, $formfields)
+ {
+ // show default dialog for birthday calendar
+ if (in_array($calendar['id'], array(self::BIRTHDAY_CALENDAR_ID, self::INVITATIONS_CALENDAR_PENDING, self::INVITATIONS_CALENDAR_DECLINED))) {
+ if ($calendar['id'] != self::BIRTHDAY_CALENDAR_ID)
+ unset($formfields['showalarms']);
+ return parent::calendar_form($action, $calendar, $formfields);
+ }
+
+ if ($calendar['id'] && ($cal = $this->calendars[$calendar['id']])) {
+ $folder = $cal->get_realname(); // UTF7
+ $color = $cal->get_color();
+ }
+ else {
+ $folder = '';
+ $color = '';
+ }
+
+ $hidden_fields[] = array('name' => 'oldname', 'value' => $folder);
+
+ $storage = $this->rc->get_storage();
+ $delim = $storage->get_hierarchy_delimiter();
+ $form = array();
+
+ if (strlen($folder)) {
+ $path_imap = explode($delim, $folder);
+ array_pop($path_imap); // pop off name part
+ $path_imap = implode($path_imap, $delim);
+
+ $options = $storage->folder_info($folder);
+ }
+ else {
+ $path_imap = '';
+ }
+
+ // General tab
+ $form['props'] = array(
+ 'name' => $this->rc->gettext('properties'),
+ );
+
+ // Disable folder name input
+ if (!empty($options) && ($options['norename'] || $options['protected'])) {
+ $input_name = new html_hiddenfield(array('name' => 'name', 'id' => 'calendar-name'));
+ $formfields['name']['value'] = kolab_storage::object_name($folder)
+ . $input_name->show($folder);
+ }
+
+ // calendar name (default field)
+ $form['props']['fieldsets']['location'] = array(
+ 'name' => $this->rc->gettext('location'),
+ 'content' => array(
+ 'name' => $formfields['name']
+ ),
+ );
+
+ if (!empty($options) && ($options['norename'] || $options['protected'])) {
+ // prevent user from moving folder
+ $hidden_fields[] = array('name' => 'parent', 'value' => $path_imap);
+ }
+ else {
+ $select = kolab_storage::folder_selector('event', array('name' => 'parent', 'id' => 'calendar-parent'), $folder);
+ $form['props']['fieldsets']['location']['content']['path'] = array(
+ 'id' => 'calendar-parent',
+ 'label' => $this->cal->gettext('parentcalendar'),
+ 'value' => $select->show(strlen($folder) ? $path_imap : ''),
+ );
+ }
+
+ // calendar color (default field)
+ $form['props']['fieldsets']['settings'] = array(
+ 'name' => $this->rc->gettext('settings'),
+ 'content' => array(
+ 'color' => $formfields['color'],
+ 'showalarms' => $formfields['showalarms'],
+ ),
+ );
+
+
+ if ($action != 'form-new') {
+ $form['sharing'] = array(
+ 'name' => Q($this->cal->gettext('tabsharing')),
+ 'content' => html::tag('iframe', array(
+ 'src' => $this->cal->rc->url(array('_action' => 'calendar-acl', 'id' => $calendar['id'], 'framed' => 1)),
+ 'width' => '100%',
+ 'height' => 350,
+ 'border' => 0,
+ 'style' => 'border:0'),
+ ''),
+ );
+ }
+
+ $this->form_html = '';
+ if (is_array($hidden_fields)) {
+ foreach ($hidden_fields as $field) {
+ $hiddenfield = new html_hiddenfield($field);
+ $this->form_html .= $hiddenfield->show() . "\n";
+ }
+ }
+
+ // Create form output
+ foreach ($form as $tab) {
+ if (!empty($tab['fieldsets']) && is_array($tab['fieldsets'])) {
+ $content = '';
+ foreach ($tab['fieldsets'] as $fieldset) {
+ $subcontent = $this->get_form_part($fieldset);
+ if ($subcontent) {
+ $content .= html::tag('fieldset', null, html::tag('legend', null, Q($fieldset['name'])) . $subcontent) ."\n";
+ }
+ }
+ }
+ else {
+ $content = $this->get_form_part($tab);
+ }
+
+ if ($content) {
+ $this->form_html .= html::tag('fieldset', null, html::tag('legend', null, Q($tab['name'])) . $content) ."\n";
+ }
+ }
+
+ // Parse form template for skin-dependent stuff
+ $this->rc->output->add_handler('calendarform', array($this, 'calendar_form_html'));
+ return $this->rc->output->parse('calendar.kolabform', false, false);
+ }
+
+ /**
+ * Handler for template object
+ */
+ public function calendar_form_html()
+ {
+ return $this->form_html;
+ }
+
+ /**
+ * Helper function used in calendar_form_content(). Creates a part of the form.
+ */
+ private function get_form_part($form)
+ {
+ $content = '';
+
+ if (is_array($form['content']) && !empty($form['content'])) {
+ $table = new html_table(array('cols' => 2));
+ foreach ($form['content'] as $col => $colprop) {
+ $label = !empty($colprop['label']) ? $colprop['label'] : rcube_label($col);
+
+ $table->add('title', html::label($colprop['id'], Q($label)));
+ $table->add(null, $colprop['value']);
+ }
+ $content = $table->show();
+ }
+ else {
+ $content = $form['content'];
+ }
+
+ return $content;
+ }
+
+
+ /**
+ * Handler to render ACL form for a calendar folder
+ */
+ public function calendar_acl()
+ {
+ $this->rc->output->add_handler('folderacl', array($this, 'calendar_acl_form'));
+ $this->rc->output->send('calendar.kolabacl');
+ }
+
+ /**
+ * Handler for ACL form template object
+ */
+ public function calendar_acl_form()
+ {
+ $calid = rcube_utils::get_input_value('_id', rcube_utils::INPUT_GPC);
+ if ($calid && ($cal = $this->get_calendar($calid))) {
+ $folder = $cal->get_realname(); // UTF7
+ $color = $cal->get_color();
+ }
+ else {
+ $folder = '';
+ $color = '';
+ }
+
+ $storage = $this->rc->get_storage();
+ $delim = $storage->get_hierarchy_delimiter();
+ $form = array();
+
+ if (strlen($folder)) {
+ $path_imap = explode($delim, $folder);
+ array_pop($path_imap); // pop off name part
+ $path_imap = implode($path_imap, $delim);
+
+ $options = $storage->folder_info($folder);
+
+ // Allow plugins to modify the form content (e.g. with ACL form)
+ $plugin = $this->rc->plugins->exec_hook('calendar_form_kolab',
+ array('form' => $form, 'options' => $options, 'name' => $folder));
+ }
+
+ if (!$plugin['form']['sharing']['content'])
+ $plugin['form']['sharing']['content'] = html::div('hint', $this->cal->gettext('aclnorights'));
+
+ return $plugin['form']['sharing']['content'];
+ }
+
+ /**
+ * Handler for user_delete plugin hook
+ */
+ public function user_delete($args)
+ {
+ $db = $this->rc->get_dbh();
+ foreach (array('kolab_alarms', 'itipinvitations') as $table) {
+ $db->query("DELETE FROM " . $this->rc->db->table_name($table, true)
+ . " WHERE `user_id` = ?", $args['user']->ID);
+ }
+ }
+}
diff --git a/calendar/drivers/kolab/kolab_invitation_calendar.php b/calendar/drivers/kolab/kolab_invitation_calendar.php
new file mode 100644
index 0000000..3ec82ac
--- /dev/null
+++ b/calendar/drivers/kolab/kolab_invitation_calendar.php
@@ -0,0 +1,377 @@
+<?php
+
+/**
+ * Kolab calendar storage class simulating a virtual calendar listing pedning/declined invitations
+ *
+ * @author Thomas Bruederli <bruederli@kolabsys.com>
+ *
+ * Copyright (C) 2014-2015, Kolab Systems AG <contact@kolabsys.com>
+ *
+ * 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.
+ *
+ * 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
+ * GNU Affero General Public License for more details.
+ *
+ * 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/>.
+ */
+
+class kolab_invitation_calendar
+{
+ public $id = '__invitation__';
+ public $ready = true;
+ public $alarms = false;
+ public $rights = 'lrsv';
+ public $editable = false;
+ public $attachments = false;
+ public $subscriptions = false;
+ public $partstats = array('unknown');
+ public $categories = array();
+ public $name = 'Invitations';
+
+ /**
+ * Default constructor
+ */
+ public function __construct($id, $calendar)
+ {
+ $this->cal = $calendar;
+ $this->id = $id;
+
+ switch ($this->id) {
+ case kolab_driver::INVITATIONS_CALENDAR_PENDING:
+ $this->partstats = array('NEEDS-ACTION');
+ $this->name = $this->cal->gettext('invitationspending');
+ if (!empty($_REQUEST['_quickview']))
+ $this->partstats[] = 'TENTATIVE';
+ break;
+
+ case kolab_driver::INVITATIONS_CALENDAR_DECLINED:
+ $this->partstats = array('DECLINED');
+ $this->name = $this->cal->gettext('invitationsdeclined');
+ break;
+ }
+
+ // user-specific alarms settings win
+ $prefs = $this->cal->rc->config->get('kolab_calendars', array());
+ if (isset($prefs[$this->id]['showalarms']))
+ $this->alarms = $prefs[$this->id]['showalarms'];
+ }
+
+
+ /**
+ * Getter for a nice and human readable name for this calendar
+ *
+ * @return string Name of this calendar
+ */
+ public function get_name()
+ {
+ return $this->name;
+ }
+
+
+ /**
+ * Getter for the IMAP folder owner
+ *
+ * @return string Name of the folder owner
+ */
+ public function get_owner()
+ {
+ return $this->cal->rc->get_user_name();
+ }
+
+
+ /**
+ *
+ */
+ public function get_title()
+ {
+ return $this->get_name();
+ }
+
+
+ /**
+ * Getter for the name of the namespace to which the IMAP folder belongs
+ *
+ * @return string Name of the namespace (personal, other, shared)
+ */
+ public function get_namespace()
+ {
+ return 'x-special';
+ }
+
+
+ /**
+ * Getter for the top-end calendar folder name (not the entire path)
+ *
+ * @return string Name of this calendar
+ */
+ public function get_foldername()
+ {
+ return $this->get_name();
+ }
+
+ /**
+ * Getter for the Cyrus mailbox identifier corresponding to this folder
+ *
+ * @return string Mailbox ID
+ */
+ public function get_mailbox_id()
+ {
+ // this is a virtual collection and has no concrete mailbox ID
+ return null;
+ }
+
+ /**
+ * Return color to display this calendar
+ */
+ public function get_color()
+ {
+ // calendar color is stored in local user prefs
+ $prefs = $this->cal->rc->config->get('kolab_calendars', array());
+
+ if (!empty($prefs[$this->id]) && !empty($prefs[$this->id]['color']))
+ return $prefs[$this->id]['color'];
+
+ return 'ffffff';
+ }
+
+ /**
+ * Compose an URL for CalDAV access to this calendar (if configured)
+ */
+ public function get_caldav_url()
+ {
+ return false;
+ }
+
+ /**
+ * Check activation status of this folder
+ *
+ * @return boolean True if enabled, false if not
+ */
+ public function is_active()
+ {
+ $prefs = $this->cal->rc->config->get('kolab_calendars', array()); // read local prefs
+ return (bool)$prefs[$this->id]['active'];
+ }
+
+ /**
+ * Update properties of this calendar folder
+ *
+ * @see calendar_driver::edit_calendar()
+ */
+ public function update(&$prop)
+ {
+ // don't change anything.
+ // let kolab_driver save props in local prefs
+ return $prop['id'];
+ }
+
+
+ /**
+ * Getter for a single event object
+ */
+ public function get_event($id)
+ {
+ // redirect call to kolab_driver::get_event()
+ $event = $this->cal->driver->get_event($id, calendar_driver::FILTER_WRITEABLE);
+
+ if (is_array($event)) {
+ // add pointer to original calendar folder
+ $event['_folder_id'] = $event['calendar'];
+ $event = $this->_mod_event($event);
+ }
+
+ return $event;
+ }
+
+ /**
+ * Get attachment body
+ * @see calendar_driver::get_attachment_body()
+ */
+ public function get_attachment_body($id, $event)
+ {
+ // find the actual folder this event resides in
+ if (!empty($event['_folder_id'])) {
+ $cal = $this->cal->driver->get_calendar($event['_folder_id']);
+ }
+ else {
+ $cal = null;
+ foreach (kolab_storage::list_folders('', '*', 'event', null) as $foldername) {
+ $cal = new kolab_calendar($foldername, $this->cal);
+ if ($cal->ready && $cal->storage && $cal->get_event($event['id'])) {
+ break;
+ }
+ }
+ }
+
+ if ($cal && $cal->storage) {
+ return $cal->get_attachment_body($id, $event);
+ }
+
+ return false;
+ }
+
+
+ /**
+ * @param integer Event's new start (unix timestamp)
+ * @param integer Event's new end (unix timestamp)
+ * @param string Search query (optional)
+ * @param boolean Include virtual events (optional)
+ * @param array Additional parameters to query storage
+ * @return array A list of event records
+ */
+ public function list_events($start, $end, $search = null, $virtual = 1, $query = array())
+ {
+ // get email addresses of the current user
+ $user_emails = $this->cal->get_user_emails();
+ $subquery = array();
+ foreach ($user_emails as $email) {
+ foreach ($this->partstats as $partstat) {
+ $subquery[] = array('tags', '=', 'x-partstat:' . $email . ':' . strtolower($partstat));
+ }
+ }
+
+ // aggregate events from all calendar folders
+ $events = array();
+ foreach (kolab_storage::list_folders('', '*', 'event', null) as $foldername) {
+ $cal = new kolab_calendar($foldername, $this->cal);
+ if ($cal->get_namespace() == 'other')
+ continue;
+
+ foreach ($cal->list_events($start, $end, $search, 1, $query, array(array($subquery, 'OR'))) as $event) {
+ $match = false;
+
+ // post-filter events to match out partstats
+ if (is_array($event['attendees'])) {
+ foreach ($event['attendees'] as $attendee) {
+ if (in_array($attendee['email'], $user_emails) && in_array($attendee['status'], $this->partstats)) {
+ $match = true;
+ break;
+ }
+ }
+ }
+
+ if ($match) {
+ $events[$event['id']] = $this->_mod_event($event);
+ }
+ }
+
+ // merge list of event categories (really?)
+ $this->categories += $cal->categories;
+ }
+
+ return $events;
+ }
+
+ /**
+ *
+ * @param integer Date range start (unix timestamp)
+ * @param integer Date range end (unix timestamp)
+ * @return integer Count
+ */
+ public function count_events($start, $end = null)
+ {
+ // get email addresses of the current user
+ $user_emails = $this->cal->get_user_emails();
+ $subquery = array();
+ foreach ($user_emails as $email) {
+ foreach ($this->partstats as $partstat) {
+ $subquery[] = array('tags', '=', 'x-partstat:' . $email . ':' . strtolower($partstat));
+ }
+ }
+
+ $filter = array(
+ array('tags','!=','x-status:cancelled'),
+ array($subquery, 'OR')
+ );
+
+ // aggregate counts from all calendar folders
+ $count = 0;
+ foreach (kolab_storage::list_folders('', '*', 'event', null) as $foldername) {
+ $cal = new kolab_calendar($foldername, $this->cal);
+ if ($cal->get_namespace() == 'other')
+ continue;
+
+ $count += $cal->count_events($start, $end, $filter);
+ }
+
+ return $count;
+ }
+
+ /**
+ * Helper method to modify some event properties
+ */
+ private function _mod_event($event)
+ {
+ // set classes according to PARTSTAT
+ $event = kolab_driver::add_partstat_class($event, $this->partstats);
+
+ if (strpos($event['className'], 'fc-invitation-') !== false) {
+ $event['calendar'] = $this->id;
+ }
+
+ return $event;
+ }
+
+
+ /**
+ * Create a new event record
+ *
+ * @see calendar_driver::new_event()
+ *
+ * @return mixed The created record ID on success, False on error
+ */
+ public function insert_event($event)
+ {
+ return false;
+ }
+
+ /**
+ * Update a specific event record
+ *
+ * @see calendar_driver::new_event()
+ * @return boolean True on success, False on error
+ */
+
+ public function update_event($event, $exception_id = null)
+ {
+ // forward call to the actual storage folder
+ if ($event['_folder_id']) {
+ $cal = $this->cal->driver->get_calendar($event['_folder_id']);
+ if ($cal && $cal->ready) {
+ return $cal->update_event($event, $exception_id);
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * Delete an event record
+ *
+ * @see calendar_driver::remove_event()
+ * @return boolean True on success, False on error
+ */
+ public function delete_event($event, $force = true)
+ {
+ return false;
+ }
+
+ /**
+ * Restore deleted event record
+ *
+ * @see calendar_driver::undelete_event()
+ * @return boolean True on success, False on error
+ */
+ public function restore_event($event)
+ {
+ return false;
+ }
+
+
+}
diff --git a/calendar/drivers/kolab/kolab_user_calendar.php b/calendar/drivers/kolab/kolab_user_calendar.php
new file mode 100644
index 0000000..00f1dfc
--- /dev/null
+++ b/calendar/drivers/kolab/kolab_user_calendar.php
@@ -0,0 +1,432 @@
+<?php
+
+/**
+ * Kolab calendar storage class simulating a virtual user calendar
+ *
+ * @author Thomas Bruederli <bruederli@kolabsys.com>
+ *
+ * Copyright (C) 2014-2015, Kolab Systems AG <contact@kolabsys.com>
+ *
+ * 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.
+ *
+ * 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
+ * GNU Affero General Public License for more details.
+ *
+ * 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/>.
+ */
+
+class kolab_user_calendar extends kolab_calendar
+{
+ public $id = 'unknown';
+ public $ready = false;
+ public $editable = false;
+ public $attachments = false;
+ public $subscriptions = false;
+
+ protected $userdata = array();
+ protected $timeindex = array();
+
+
+ /**
+ * Default constructor
+ */
+ public function __construct($user_or_folder, $calendar)
+ {
+ $this->cal = $calendar;
+
+ // full user record is provided
+ if (is_array($user_or_folder)) {
+ $this->userdata = $user_or_folder;
+ $this->storage = new kolab_storage_folder_user($this->userdata['kolabtargetfolder'], '', $this->userdata);
+ }
+ else { // get user record from LDAP
+ $this->storage = new kolab_storage_folder_user($user_or_folder);
+ $this->userdata = $this->storage->ldaprec;
+ }
+
+ $this->ready = !empty($this->userdata['kolabtargetfolder']);
+ $this->storage->type = 'event';
+
+ if ($this->ready) {
+ // ID is derrived from the user's kolabtargetfolder attribute
+ $this->id = kolab_storage::folder_id($this->userdata['kolabtargetfolder'], true);
+ $this->imap_folder = $this->userdata['kolabtargetfolder'];
+ $this->name = $this->storage->get_name();
+ $this->parent = ''; // user calendars are top level
+
+ // user-specific alarms settings win
+ $prefs = $this->cal->rc->config->get('kolab_calendars', array());
+ if (isset($prefs[$this->id]['showalarms']))
+ $this->alarms = $prefs[$this->id]['showalarms'];
+ }
+ }
+
+
+ /**
+ * Getter for a nice and human readable name for this calendar
+ *
+ * @return string Name of this calendar
+ */
+ public function get_name()
+ {
+ return $this->userdata['displayname'] ?: ($this->userdata['name'] ?: $this->userdata['mail']);
+ }
+
+
+ /**
+ * Getter for the IMAP folder owner
+ *
+ * @return string Name of the folder owner
+ */
+ public function get_owner()
+ {
+ return $this->userdata['mail'];
+ }
+
+
+ /**
+ *
+ */
+ public function get_title()
+ {
+ return trim($this->userdata['displayname'] . '; ' . $this->userdata['mail'], '; ');
+ }
+
+
+ /**
+ * Getter for the name of the namespace to which the IMAP folder belongs
+ *
+ * @return string Name of the namespace (personal, other, shared)
+ */
+ public function get_namespace()
+ {
+ return 'other user';
+ }
+
+
+ /**
+ * Getter for the top-end calendar folder name (not the entire path)
+ *
+ * @return string Name of this calendar
+ */
+ public function get_foldername()
+ {
+ return $this->get_name();
+ }
+
+ /**
+ * Return color to display this calendar
+ */
+ public function get_color()
+ {
+ // calendar color is stored in local user prefs
+ $prefs = $this->cal->rc->config->get('kolab_calendars', array());
+
+ if (!empty($prefs[$this->id]) && !empty($prefs[$this->id]['color']))
+ return $prefs[$this->id]['color'];
+
+ return 'cc0000';
+ }
+
+ /**
+ * Compose an URL for CalDAV access to this calendar (if configured)
+ */
+ public function get_caldav_url()
+ {
+ return false;
+ }
+
+ /**
+ * Check subscription status of this folder
+ *
+ * @return boolean True if subscribed, false if not
+ */
+ public function is_subscribed()
+ {
+ return $this->storage->is_subscribed();
+ }
+
+ /**
+ * Update properties of this calendar folder
+ *
+ * @see calendar_driver::edit_calendar()
+ */
+ public function update(&$prop)
+ {
+ // don't change anything.
+ // let kolab_driver save props in local prefs
+ return $prop['id'];
+ }
+
+
+ /**
+ * Getter for a single event object
+ */
+ public function get_event($id)
+ {
+ // TODO: implement this
+ return $this->events[$id];
+ }
+
+ /**
+ * Get attachment body
+ * @see calendar_driver::get_attachment_body()
+ */
+ public function get_attachment_body($id, $event)
+ {
+ if (!$event['calendar'] && ($ev = $this->get_event($event['id']))) {
+ $event['calendar'] = $ev['calendar'];
+ }
+
+ if ($event['calendar'] && ($cal = $this->cal->get_calendar($event['calendar']))) {
+ return $cal->get_attachment_body($id, $event);
+ }
+
+ return false;
+ }
+
+ /**
+ * @param integer Event's new start (unix timestamp)
+ * @param integer Event's new end (unix timestamp)
+ * @param string Search query (optional)
+ * @param boolean Include virtual events (optional)
+ * @param array Additional parameters to query storage
+ * @return array A list of event records
+ */
+ public function list_events($start, $end, $search = null, $virtual = 1, $query = array())
+ {
+ // convert to DateTime for comparisons
+ try {
+ $start_dt = new DateTime('@'.$start);
+ }
+ catch (Exception $e) {
+ $start_dt = new DateTime('@0');
+ }
+ try {
+ $end_dt = new DateTime('@'.$end);
+ }
+ catch (Exception $e) {
+ $end_dt = new DateTime('today +10 years');
+ }
+
+ $limit_changed = null;
+ if (!empty($query)) {
+ foreach ($query as $q) {
+ if ($q[0] == 'changed' && $q[1] == '>=') {
+ try { $limit_changed = new DateTime('@'.$q[2]); }
+ catch (Exception $e) { /* ignore */ }
+ }
+ }
+ }
+
+ // aggregate all calendar folders the user shares (but are not subscribed)
+ foreach (kolab_storage::list_user_folders($this->userdata, 'event', false) as $foldername) {
+ $cal = new kolab_calendar($foldername, $this->cal);
+ foreach ($cal->list_events($start, $end, $search, 1) as $event) {
+ $this->events[$event['id']] = $event;
+ $this->timeindex[$this->time_key($event)] = $event['id'];
+ }
+ }
+
+ // get events from the user's free/busy feed (for quickview only)
+ $fbview = $this->cal->rc->config->get('calendar_include_freebusy_data', 1);
+ if ($fbview && ($fbview == 1 || !empty($_REQUEST['_quickview'])) && empty($search)) {
+ $this->fetch_freebusy($limit_changed);
+ }
+
+ $events = array();
+ foreach ($this->events as $event) {
+ // list events in requested time window
+ if ($event['start'] <= $end_dt && $event['end'] >= $start_dt &&
+ (!$limit_changed || !$event['changed'] || $event['changed'] >= $limit_changed)) {
+ $events[] = $event;
+ }
+ }
+
+ // avoid session race conditions that will loose temporary subscriptions
+ $this->cal->rc->session->nowrite = true;
+
+ return $events;
+ }
+
+ /**
+ *
+ * @param integer Date range start (unix timestamp)
+ * @param integer Date range end (unix timestamp)
+ * @return integer Count
+ */
+ public function count_events($start, $end = null)
+ {
+ // not implemented
+ return 0;
+ }
+
+ /**
+ * Helper method to fetch free/busy data for the user and turn it into calendar data
+ */
+ private function fetch_freebusy($limit_changed = null)
+ {
+ // ask kolab server first
+ try {
+ $request_config = array(
+ 'store_body' => true,
+ 'follow_redirects' => true,
+ );
+ $request = libkolab::http_request(kolab_storage::get_freebusy_url($this->userdata['mail']), 'GET', $request_config);
+ $response = $request->send();
+
+ // authentication required
+ if ($response->getStatus() == 401) {
+ $request->setAuth($this->cal->rc->user->get_username(), $this->cal->rc->decrypt($_SESSION['password']));
+ $response = $request->send();
+ }
+
+ if ($response->getStatus() == 200)
+ $fbdata = $response->getBody();
+
+ unset($request, $response);
+ }
+ catch (Exception $e) {
+ rcube::raise_error(array(
+ 'code' => 900,
+ 'type' => 'php',
+ 'file' => __FILE__,
+ 'line' => __LINE__,
+ 'message' => "Error fetching free/busy information: " . $e->getMessage()),
+ true, false);
+
+ return false;
+ }
+
+ $statusmap = array(
+ 'FREE' => 'free',
+ 'BUSY' => 'busy',
+ 'BUSY-TENTATIVE' => 'tentative',
+ 'X-OUT-OF-OFFICE' => 'outofoffice',
+ 'OOF' => 'outofoffice',
+ );
+ $titlemap = array(
+ 'FREE' => $this->cal->gettext('availfree'),
+ 'BUSY' => $this->cal->gettext('availbusy'),
+ 'BUSY-TENTATIVE' => $this->cal->gettext('availtentative'),
+ 'X-OUT-OF-OFFICE' => $this->cal->gettext('availoutofoffice'),
+ );
+
+ // console('_fetch_freebusy', kolab_storage::get_freebusy_url($this->userdata['mail']), $fbdata);
+
+ // parse free-busy information
+ $count = 0;
+ if ($fbdata) {
+ $ical = $this->cal->get_ical();
+ $ical->import($fbdata);
+ if ($fb = $ical->freebusy) {
+ // consider 'changed >= X' queries
+ if ($limit_changed && $fb['created'] && $fb['created'] < $limit_changed) {
+ return 0;
+ }
+
+ foreach ($fb['periods'] as $tuple) {
+ list($from, $to, $type) = $tuple;
+ $event = array(
+ 'id' => md5($this->id . $from->format('U') . '/' . $to->format('U')),
+ 'calendar' => $this->id,
+ 'changed' => $fb['created'] ?: new DateTime(),
+ 'title' => $this->get_name() . ' ' . ($titlemap[$type] ?: $type),
+ 'start' => $from,
+ 'end' => $to,
+ 'free_busy' => $statusmap[$type] ?: 'busy',
+ 'className' => 'fc-type-freebusy',
+ 'organizer' => array(
+ 'email' => $this->userdata['mail'],
+ 'name' => $this->userdata['displayname'],
+ ),
+ );
+
+ // avoid duplicate entries
+ $key = $this->time_key($event);
+ if (!$this->timeindex[$key]) {
+ $this->events[$event['id']] = $event;
+ $this->timeindex[$key] = $event['id'];
+ $count++;
+ }
+ }
+ }
+ }
+
+ return $count;
+ }
+
+ /**
+ * Helper to build a key for the absolute time slot the given event convers
+ */
+ private function time_key($event)
+ {
+ return sprintf('%s/%s', $event['start']->format('U'), is_object($event['end']->format('U')) ?: '0');
+ }
+
+
+ /**
+ * Create a new event record
+ *
+ * @see calendar_driver::new_event()
+ *
+ * @return mixed The created record ID on success, False on error
+ */
+ public function insert_event($event)
+ {
+ return false;
+ }
+
+ /**
+ * Update a specific event record
+ *
+ * @see calendar_driver::new_event()
+ * @return boolean True on success, False on error
+ */
+
+ public function update_event($event, $exception_id = null)
+ {
+ return false;
+ }
+
+ /**
+ * Delete an event record
+ *
+ * @see calendar_driver::remove_event()
+ * @return boolean True on success, False on error
+ */
+ public function delete_event($event, $force = true)
+ {
+ return false;
+ }
+
+ /**
+ * Restore deleted event record
+ *
+ * @see calendar_driver::undelete_event()
+ * @return boolean True on success, False on error
+ */
+ public function restore_event($event)
+ {
+ return false;
+ }
+
+
+ /**
+ * Convert from Kolab_Format to internal representation
+ */
+ private function _to_rcube_event($record)
+ {
+ $record['id'] = $record['uid'];
+ $record['calendar'] = $this->id;
+
+ return kolab_driver::to_rcube_event($record);
+ }
+
+}
diff --git a/calendar/drivers/ldap/resources_driver_ldap.php b/calendar/drivers/ldap/resources_driver_ldap.php
new file mode 100644
index 0000000..c377393
--- /dev/null
+++ b/calendar/drivers/ldap/resources_driver_ldap.php
@@ -0,0 +1,150 @@
+<?php
+
+/**
+ * LDAP-based resource directory class using rcube_ldap functionality
+ *
+ * @author Thomas Bruederli <bruederli@kolabsys.com>
+ *
+ * Copyright (C) 2014, Kolab Systems AG <contact@kolabsys.com>
+ *
+ * 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.
+ *
+ * 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
+ * GNU Affero General Public License for more details.
+ *
+ * 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/>.
+ */
+
+/**
+ * LDAP-based resource directory implementation
+ */
+class resources_driver_ldap extends resources_driver
+{
+ private $rc;
+ private $ldap;
+
+ /**
+ * Default constructor
+ */
+ function __construct($cal)
+ {
+ $this->cal = $cal;
+ $this->rc = $cal->rc;
+ }
+
+ /**
+ * Fetch resource objects to be displayed for booking
+ *
+ * @param string Search query (optional)
+ * @return array List of resource records available for booking
+ */
+ public function load_resources($query = null, $num = 5000)
+ {
+ if (!($ldap = $this->connect())) {
+ return array();
+ }
+
+ // TODO: apply paging
+ $ldap->set_pagesize($num);
+
+ if (isset($query)) {
+ $results = $ldap->search('*', $query, 0, true, true);
+ }
+ else {
+ $results = $ldap->list_records();
+ }
+
+ if ($results instanceof ArrayAccess) {
+ foreach ($results as $i => $rec) {
+ $results[$i] = $this->decode_resource($rec);
+ }
+ }
+
+ return $results;
+ }
+
+ /**
+ * Return properties of a single resource
+ *
+ * @param string Unique resource identifier
+ * @return array Resource object as hash array
+ */
+ public function get_resource($dn)
+ {
+ $rec = null;
+
+ if ($ldap = $this->connect()) {
+ $rec = $ldap->get_record(rcube_ldap::dn_encode($dn), true);
+
+ if (!empty($rec)) {
+ $rec = $this->decode_resource($rec);
+ }
+ }
+
+ return $rec;
+ }
+
+ /**
+ * Return properties of a resource owner
+ *
+ * @param string Owner identifier
+ * @return array Resource object as hash array
+ */
+ public function get_resource_owner($dn)
+ {
+ $owner = null;
+
+ if ($ldap = $this->connect()) {
+ $owner = $ldap->get_record(rcube_ldap::dn_encode($dn), true);
+ $owner['ID'] = rcube_ldap::dn_decode($owner['ID']);
+ unset($owner['_raw_attrib'], $owner['_type']);
+ }
+
+ return $owner;
+ }
+
+ /**
+ * Extract JSON-serialized attributes
+ */
+ private function decode_resource($rec)
+ {
+ $rec['ID'] = rcube_ldap::dn_decode($rec['ID']);
+
+ if (is_array($rec['attributes']) && $rec['attributes'][0]) {
+ $attributes = array();
+
+ foreach ($rec['attributes'] as $sattr) {
+ $attr = @json_decode($sattr, true);
+ $attributes += $attr;
+ }
+
+ $rec['attributes'] = $attributes;
+ }
+
+ // force $rec['members'] to be an array
+ if (!empty($rec['members']) && !is_array($rec['members'])) {
+ $rec['members'] = array($rec['members']);
+ }
+
+ // remove unused cruft
+ unset($rec['_raw_attrib']);
+
+ return $rec;
+ }
+
+ private function connect()
+ {
+ if (!isset($this->ldap)) {
+ $this->ldap = new rcube_ldap($this->rc->config->get('calendar_resources_directory'), true);
+ }
+
+ return $this->ldap->ready ? $this->ldap : null;
+ }
+
+} \ No newline at end of file
diff --git a/calendar/drivers/resources_driver.php b/calendar/drivers/resources_driver.php
new file mode 100644
index 0000000..c51e922
--- /dev/null
+++ b/calendar/drivers/resources_driver.php
@@ -0,0 +1,114 @@
+<?php
+
+/**
+ * Resources directory interface definition
+ *
+ * @author Thomas Bruederli <bruederli@kolabsys.com>
+ *
+ * Copyright (C) 2014, Kolab Systems AG <contact@kolabsys.com>
+ *
+ * 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.
+ *
+ * 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
+ * GNU Affero General Public License for more details.
+ *
+ * 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/>.
+ */
+
+
+/**
+ * Interface definition for a resources directory driver classe
+ */
+abstract class resources_driver
+{
+ protected$cal;
+
+ /**
+ * Default constructor
+ */
+ function __construct($cal)
+ {
+ $this->cal = $cal;
+ }
+
+ /**
+ * Fetch resource objects to be displayed for booking
+ *
+ * @param string Search query (optional)
+ * @return array List of resource records available for booking
+ */
+ abstract public function load_resources($query = null);
+
+ /**
+ * Return properties of a single resource
+ *
+ * @param string Unique resource identifier
+ * @return array Resource object as hash array
+ */
+ abstract public function get_resource($id);
+
+ /**
+ * Return properties of a resource owner
+ *
+ * @param string Owner identifier
+ * @return array Resource object as hash array
+ */
+ public function get_resource_owner($id)
+ {
+ return null;
+ }
+
+ /**
+ * Get event data to display a resource's calendar
+ *
+ * The default implementation extracts the resource's email address
+ * and fetches free-busy data using the calendar backend driver.
+ *
+ * @param integer Event's new start (unix timestamp)
+ * @param integer Event's new end (unix timestamp)
+ * @return array A list of event objects (see calendar_driver specification)
+ */
+ public function get_resource_calendar($id, $start, $end)
+ {
+ $events = array();
+ $rec = $this->get_resource($id);
+ if ($rec && !empty($rec['email']) && $this->cal->driver) {
+ $fbtypemap = array(
+ calendar::FREEBUSY_BUSY => 'busy',
+ calendar::FREEBUSY_TENTATIVE => 'tentative',
+ calendar::FREEBUSY_OOF => 'outofoffice',
+ );
+
+ // if the backend has free-busy information
+ $fblist = $this->cal->driver->get_freebusy_list($rec['email'], $start, $end);
+ if (is_array($fblist)) {
+ foreach ($fblist as $slot) {
+ list($from, $to, $type) = $slot;
+ if ($type == calendar::FREEBUSY_FREE || $type == calendar::FREEBUSY_UNKNOWN) {
+ continue;
+ }
+ if ($from < $end && $to > $start) {
+ $event = array(
+ 'id' => sha1($id . $from . $to),
+ 'title' => $rec['name'],
+ 'start' => new DateTime('@' . $from),
+ 'end' => new DateTime('@' . $to),
+ 'status' => $fbtypemap[$type],
+ 'calendar' => '_resource',
+ );
+ $events[] = $event;
+ }
+ }
+ }
+ }
+
+ return $events;
+ }
+
+}

© 2014-2024 Faster IT GmbH | imprint | privacy policy