diff --git a/plugins/calendar/drivers/caldav/caldav_calendar.php b/plugins/calendar/drivers/caldav/caldav_calendar.php index 46b0278e..ac0d8b41 100644 --- a/plugins/calendar/drivers/caldav/caldav_calendar.php +++ b/plugins/calendar/drivers/caldav/caldav_calendar.php @@ -1,897 +1,897 @@ * * Copyright (C) 2012-2022, Apheleia IT AG * * 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 . */ class caldav_calendar extends kolab_storage_dav_folder { public $ready = false; public $rights = 'lrs'; public $editable = false; public $attachments = false; // TODO public $alarms = false; public $history = false; public $subscriptions = false; public $categories = []; public $storage; public $type = 'event'; protected $cal; protected $events = []; protected $search_fields = ['title', 'description', 'location', 'attendees', 'categories']; /** * Factory method to instantiate a caldav_calendar object * * @param string $id Calendar ID (encoded IMAP folder name) * @param object $calendar Calendar plugin object * * @return caldav_calendar Self instance */ public static function factory($id, $calendar) { return new caldav_calendar($id, $calendar); } /** * Default constructor */ public function __construct($folder_or_id, $calendar) { if ($folder_or_id instanceof kolab_storage_dav_folder) { $this->storage = $folder_or_id; } else { - // $this->storage = kolab_storage_dav::get_folder($folder_or_id); + // $this->storage = kolab_storage_dav::get_folder($folder_or_id, 'event'); } $this->cal = $calendar; $this->id = $this->storage->id; $this->attributes = $this->storage->attributes; $this->ready = true; $this->alarms = !isset($this->storage->attributes['alarms']) || $this->storage->attributes['alarms']; // Set writeable and alarms flags according to folder permissions if ($this->ready) { if ($this->storage->get_namespace() == 'personal') { $this->editable = true; $this->rights = 'lrswikxteav'; } else { $rights = $this->storage->get_myrights(); if ($rights) { $this->rights = $rights; if (strpos($rights, 't') !== false || strpos($rights, 'd') !== false) { $this->editable = strpos($rights, 'i');; } } } } $this->default = $this->storage->default; $this->subtype = $this->storage->subtype; } /** * Getter for the folder name * * @return string Name of the folder */ public function get_realname() { return $this->get_name(); } /** * Return color to display this calendar */ public function get_color($default = null) { if ($color = $this->storage->get_color()) { return $color; } return $default ?: '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, [ '%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 caldav_driver::edit_calendar() */ public function update(&$prop) { return false; // NOP } /** * Getter for a single event object */ public function get_event($id) { // remove our occurrence identifier if it's there $master_id = preg_replace('/-\d{8}(T\d{6})?$/', '', $id); // directly access storage object if (empty($this->events[$id]) && $master_id == $id && ($record = $this->storage->get_object($id))) { $this->events[$id] = $record = $this->_to_driver_event($record, true); } // maybe a recurring instance is requested if (empty($this->events[$id]) && $master_id != $id) { $instance_id = substr($id, strlen($master_id) + 1); if ($record = $this->storage->get_object($master_id)) { $master = $record = $this->_to_driver_event($record); } if (!empty($master)) { // check for match in top-level exceptions (aka loose single occurrences) if (!empty($master['_formatobj']) && ($instance = $master['_formatobj']->get_instance($instance_id))) { $this->events[$id] = $this->_to_driver_event($instance, false, true, $master); } // check for match on the first instance already else if (!empty($master['_instance']) && $master['_instance'] == $instance_id) { $this->events[$id] = $master; } else if (!empty($master['recurrence'])) { $start_date = $master['start']; // For performance reasons we'll get only the specific instance if (($date = substr($id, strlen($master_id) + 1, 8)) && strlen($date) == 8 && is_numeric($date)) { $start_date = new DateTime($date . 'T000000', $master['start']->getTimezone()); } $this->get_recurring_events($record, $start_date, null, $id, 1); } } } 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 int Event's new start (unix timestamp) * @param int Event's new end (unix timestamp) * @param string Search query (optional) * @param bool 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 = [], $filter_query = null) { // convert to DateTime for comparisons // #5190: make the range a little bit wider // to workaround possible timezone differences try { $start = new DateTime('@' . ($start - 12 * 3600)); } catch (Exception $e) { $start = new DateTime('@0'); } try { $end = new DateTime('@' . ($end + 12 * 3600)); } 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[] = ['dtstart', '<=', $end]; $query[] = ['dtend', '>=', $start]; if (is_array($filter_query)) { $query = array_merge($query, $filter_query); } $words = []; $partstat_exclude = []; $events = []; if (!empty($search)) { $search = mb_strtolower($search); $words = rcube_utils::tokenize_string($search, 1); foreach (rcube_utils::normalize_string($search, true) as $word) { $query[] = ['words', 'LIKE', $word]; } } // set partstat filter to skip pending and declined invitations if (empty($filter_query) && $this->cal->rc->config->get('kolab_invitation_calendars') && $this->get_namespace() != 'other' ) { $partstat_exclude = ['NEEDS-ACTION', 'DECLINED']; } foreach ($this->storage->select($query) as $record) { $event = $this->_to_driver_event($record, !$virtual, false); // remember seen categories if (!empty($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'); $event_tz = $event['start']->getTimezone(); foreach ((array) $event['recurrence']['EXDATE'] as $exdate) { $ex = clone $exdate; $ex->setTimezone($event_tz); if ($ex->format('Ymd') == $event_date) { $add = false; break; } } } // find and merge exception for the first instance if ($virtual && !empty($event['recurrence']) && !empty($event['recurrence']['EXCEPTIONS'])) { foreach ($event['recurrence']['EXCEPTIONS'] as $exception) { if ($event['_instance'] == $exception['_instance']) { unset($exception['calendar'], $exception['className'], $exception['_folder_id']); // 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 (!empty($event['recurrence']) && $virtual == 1) { $events = array_merge($events, $this->get_recurring_events($event, $start, $end)); } // add top-level exceptions (aka loose single occurrences) else if (!empty($record['exceptions'])) { foreach ($record['exceptions'] as $ex) { $component = $this->_to_driver_event($ex, false, false, $record); 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) && !empty($event['attendees'])) { foreach ($event['attendees'] as $attendee) { if ( in_array($attendee['email'], $user_emails) && in_array($attendee['status'], $partstat_exclude) ) { return false; } } } return true; }); // Apply event-to-mail relations // $config = kolab_storage_config::get_instance(); // $config->apply_links($events); - // avoid session race conditions that will loose temporary subscriptions - $this->cal->rc->session->nowrite = true; + // Avoid session race conditions that will loose temporary subscriptions + // $this->cal->rc->session->nowrite = true; return $events; } /** * Get number of events in the given calendar * * @param int Date range start (unix timestamp) * @param int Date range end (unix timestamp) * @param array Additional query to filter events * * @return int Number of events */ 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[] = ['dtend', '>=', $start]; if ($end) { $query[] = ['dtstart', '<=', $end]; } // add query to exclude pending/declined invitations if (empty($filter_query)) { foreach ($this->cal->get_user_emails() as $email) { $query[] = ['tags', '!=', 'x-partstat:' . $email . ':needs-action']; $query[] = ['tags', '!=', 'x-partstat:' . $email . ':declined']; } } else if (is_array($filter_query)) { $query = array_merge($query, $filter_query); } return $this->storage->count($query); } /** * Create a new event record * * @see calendar_driver::new_event() * * @return array|false 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 = !empty($event['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([ 'code' => 600, 'file' => __FILE__, 'line' => __LINE__, 'message' => "Error saving event object to DAV server" ], true, false ); return false; } // save links in configuration.relation object // if ($this->save_links($event['uid'], $links)) { // $object['links'] = $links; // } $this->events = [$event['uid'] => $this->_to_driver_event($object, true)]; return true; } /** * Update a specific event record * * @return bool True on success, False on error */ public function update_event($event, $exception_id = null) { $updated = false; $old = $this->storage->get_object(!empty($event['uid']) ? $event['uid'] : $event['id']); if (!$old || PEAR::isError($old)) { return false; } // email links are stored separately // $links = !empty($event['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([ 'code' => 600, 'file' => __FILE__, 'line' => __LINE__, 'message' => "Error saving event object to CalDAV server" ], true, false ); } else { // save links in configuration.relation object // if ($this->save_links($event['uid'], $links)) { // $object['links'] = $links; // } $updated = true; $this->events = [$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 bool True on success, False on error */ public function delete_event($event, $force = true) { $uid = !empty($event['uid']) ? $event['uid'] : $event['id']; $deleted = $this->storage->delete($uid, $force); if (!$deleted) { rcube::raise_error([ 'code' => 600, 'file' => __FILE__, 'line' => __LINE__, 'message' => "Error deleting event '{$uid}' from CalDAV server" ], true, false ); } return $deleted; } /** * Restore deleted event record * * @see calendar_driver::undelete_event() * * @return bool True on success, False on error */ public function restore_event($event) { // TODO return false; } /** * Find messages linked with an event */ protected function get_links($uid) { return []; // TODO $storage = kolab_storage_config::get_instance(); return $storage->get_object_links($uid); } /** * Save message references (links) to an event */ protected function save_links($uid, $links) { return false; // TODO $storage = kolab_storage_config::get_instance(); return $storage->save_object_links($uid, (array) $links); } /** * Create instances of a recurring event * * @param array $event Hash array with event properties * @param DateTime $start Start date of the recurrence window * @param DateTime $end End date of the recurrence window * @param string $event_id ID of a specific recurring event instance * @param int $limit Max. number of instances to return * * @return array List of recurring event instances */ public function get_recurring_events($event, $start, $end = null, $event_id = null, $limit = null) { $object = $event['_formatobj']; if (!is_object($object)) { return []; } // determine a reasonable end date if none given if (!$end) { $end = clone $event['start']; $end->add(new DateInterval('P100Y')); } // read recurrence exceptions first $events = []; $exdata = []; $futuredata = []; $recurrence_id_format = libcalendaring::recurrence_id_format($event); if (!empty($event['recurrence'])) { // 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']); if (!empty($event['recurrence']['EXCEPTIONS'])) { foreach ($event['recurrence']['EXCEPTIONS'] as $exception) { if (empty($exception['_instance'])) { $exception['_instance'] = libcalendaring::recurrence_instance_identifier($exception, !empty($event['allday'])); } $rec_event = $this->_to_driver_event($exception, false, false, $event); $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 && (empty($this->events[$event_id]) || !empty($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 (empty($exdata[$exdate]) || !empty($exdata[$exdate]['thisandfuture'])) { $exdata[$exdate] = $rec_event; } if (!empty($rec_event['thisandfuture'])) { $futuredata[$exdate] = $rec_event; } } } } // found the specifically requested instance, exiting... if ($event_id && !empty($this->events[$event_id])) { return [$this->events[$event_id]]; } // Check first occurrence, it might have been moved if ($first = $exdata[$event['start']->format('Ymd')]) { // return it only if not already in the result, but in the requested period if (!($event['start'] <= $end && $event['end'] >= $start) && ($first['start'] <= $end && $first['end'] >= $start) ) { $events[] = $first; } } if ($limit && count($events) >= $limit) { return $events; } // use libkolab to compute recurring events $recurrence = new kolab_date_recurrence($object); $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 (!empty($futuredata[$datestr])) { $overlay_data = $futuredata[$datestr]; } $rec_id = $event['uid'] . '-' . $instance_id; $exception = !empty($exdata[$datestr]) ? $exdata[$datestr] : $overlay_data; $event_start = $next_event['start']; $event_end = $next_event['end']; // copy some event from exception to get proper start/end dates if ($exception) { $event_copy = $next_event; caldav_driver::merge_exception_dates($event_copy, $exception); $event_start = $event_copy['start']; $event_end = $event_copy['end']; } // add to output if in range if (($event_start <= $end && $event_end >= $start) || ($event_id && $rec_id == $event_id)) { $rec_event = $this->_to_driver_event($next_event, false, false, $event); $rec_event['_instance'] = $instance_id; $rec_event['_count'] = $i + 1; if ($exception) { // copy data from exception caldav_driver::merge_exception_data($rec_event, $exception); } $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; } if ($limit && count($events) >= $limit) { return $events; } } else if ($next_event['start'] > $end) { // stop loop if out of range break; } // avoid endless recursion loops if (++$i > 100000) { break; } } return $events; } /** * Convert from storage format to internal representation */ private function _to_driver_event($record, $noinst = false, $links = true, $master_event = null) { $record['calendar'] = $this->id; // remove (possibly outdated) cached parameters unset($record['_folder_id'], $record['className']); // if ($links && !array_key_exists('links', $record)) { // $record['links'] = $this->get_links($record['uid']); // } $ns = $this->get_namespace(); if ($ns == 'other') { $record['className'] = 'fc-event-ns-other'; } if ($ns == 'other' || !$this->cal->rc->config->get('kolab_invitation_calendars')) { $record = caldav_driver::add_partstat_class($record, ['NEEDS-ACTION', 'DECLINED'], $this->get_owner()); // Modify invitation status class name, when invitation calendars are disabled // we'll use opacity only for declined/needs-action events $record['className'] = str_replace('-invitation', '', $record['className']); } // add instance identifier to first occurrence (master event) $recurrence_id_format = libcalendaring::recurrence_id_format($master_event ? $master_event : $record); if (!$noinst && !empty($record['recurrence']) && empty($record['recurrence_id']) && empty($record['_instance'])) { $record['_instance'] = $record['start']->format($recurrence_id_format); } else if (isset($record['recurrence_date']) && is_a($record['recurrence_date'], 'DateTime')) { $record['_instance'] = $record['recurrence_date']->format($recurrence_id_format); } // clean up exception data if (!empty($record['recurrence']) && !empty($record['recurrence']['EXCEPTIONS'])) { array_walk($record['recurrence']['EXCEPTIONS'], function(&$exception) { unset($exception['_mailbox'], $exception['_msguid'], $exception['_formatobj'], $exception['_attachments']); }); } // Load the given event data into a libkolabxml container // it's needed for recurrence resolving, which uses libcalendaring // TODO: Drop dependency on libkolabxml? $event_xml = new kolab_format_event(); $event_xml->set($record); $record['_formatobj'] = $event_xml; return $record; } /** * Convert the given event record into a data structure that can be passed to the storage backend for saving * (opposite of self::_to_driver_event()) */ private function _from_driver_event($event, $old = []) { // set current user as ORGANIZER if ($identity = $this->cal->rc->user->list_emails(true)) { $event['attendees'] = !empty($event['attendees']) ? $event['attendees'] : []; $found = false; // there can be only resources on attendees list (T1484) // let's check the existence of an organizer foreach ($event['attendees'] as $attendee) { if (!empty($attendee['role']) && $attendee['role'] == 'ORGANIZER') { $found = true; break; } } if (!$found) { $event['attendees'][] = ['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'] = []; } // remove recurrence information (e.g. EXDATES and EXCEPTIONS) entirely if (!empty($event['recurrence']) && empty($event['recurrence']['FREQ']) && empty($event['recurrence']['RDATE'])) { $event['recurrence'] = []; } // keep 'comment' from initial itip invitation if (!empty($old['comment'])) { $event['comment'] = $old['comment']; } // remove some internal properties which should not be cached $cleanup_fn = function(&$event) { unset($event['_savemode'], $event['_fromcalendar'], $event['_identity'], $event['_folder_id'], $event['calendar'], $event['className'], $event['recurrence_id'], $event['attachments'], $event['deleted_attachments']); }; $cleanup_fn($event); // clean up exception data if (!empty($event['exceptions'])) { array_walk($event['exceptions'], function(&$exception) use ($cleanup_fn) { unset($exception['_mailbox'], $exception['_msguid'], $exception['_formatobj']); $cleanup_fn($exception); }); } // 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) { if (empty($event[$col])) { continue; } $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 = ['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/plugins/calendar/drivers/caldav/caldav_driver.php b/plugins/calendar/drivers/caldav/caldav_driver.php index f7e1638f..b26c2f23 100644 --- a/plugins/calendar/drivers/caldav/caldav_driver.php +++ b/plugins/calendar/drivers/caldav/caldav_driver.php @@ -1,629 +1,603 @@ * * Copyright (C) 2012-2022, Apheleia IT AG * * 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 . */ require_once(__DIR__ . '/../kolab/kolab_driver.php'); class caldav_driver extends kolab_driver { // features this backend supports public $alarms = true; public $attendees = true; public $freebusy = true; public $attachments = false; // TODO public $undelete = false; // TODO public $alarm_types = ['DISPLAY', 'AUDIO']; public $categoriesimmutable = true; /** * Default constructor */ public function __construct($cal) { $cal->require_plugin('libkolab'); // load helper classes *after* libkolab has been loaded (#3248) require_once(__DIR__ . '/caldav_calendar.php'); // require_once(__DIR__ . '/kolab_user_calendar.php'); // require_once(__DIR__ . '/caldav_invitation_calendar.php'); $this->cal = $cal; $this->rc = $cal->rc; // Initialize the CalDAV storage $url = $this->rc->config->get('calendar_caldav_server', 'http://localhost'); $this->storage = new kolab_storage_dav($url); $this->cal->register_action('push-freebusy', [$this, 'push_freebusy']); $this->cal->register_action('calendar-acl', [$this, 'calendar_acl']); // $this->freebusy_trigger = $this->rc->config->get('calendar_freebusy_trigger', false); // TODO: get configuration for the Bonnie API // $this->bonnie_api = libkolab::get_bonnie_api(); } /** * Read available calendars from server */ protected function _read_calendars() { // already read sources if (isset($this->calendars)) { return $this->calendars; } // get all folders that support VEVENT, sorted by namespace/name $folders = $this->storage->get_folders('event'); // + $this->storage->get_user_folders('event', true); $this->calendars = []; foreach ($folders as $folder) { $calendar = $this->_to_calendar($folder); if ($calendar->ready) { $this->calendars[$calendar->id] = $calendar; if ($calendar->editable) { $this->has_writeable = true; } } } return $this->calendars; } /** * Convert kolab_storage_folder into caldav_calendar */ protected function _to_calendar($folder) { if ($folder instanceof caldav_calendar) { return $folder; } if ($folder instanceof kolab_storage_folder_user) { $calendar = new kolab_user_calendar($folder, $this->cal); $calendar->subscriptions = count($folder->children) > 0; } else { $calendar = new caldav_calendar($folder, $this->cal); } return $calendar; } /** * Get a list of available calendars from this source. * * @param int $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) { $this->_read_calendars(); $folders = $this->filter_calendars($filter); $calendars = []; + $prefs = $this->rc->config->get('kolab_calendars', []); // include virtual folders for a full folder tree /* if (!is_null($tree)) { $folders = $this->storage->folder_hierarchy($folders, $tree); } */ $parents = array_keys($this->calendars); foreach ($folders as $id => $cal) { /* $path = explode('/', $cal->name); // find parent do { array_pop($path); $parent_id = $this->storage->folder_id(implode('/', $path)); } while (count($path) > 1 && !in_array($parent_id, $parents)); // restore "real" parent ID if ($parent_id && !in_array($parent_id, $parents)) { $parent_id = $this->storage->folder_id($cal->get_parent()); } $parents[] = $cal->id; if ($cal->virtual) { $calendars[$cal->id] = [ 'id' => $cal->id, 'name' => $cal->get_name(), 'listname' => $cal->get_foldername(), 'editname' => $cal->get_foldername(), 'virtual' => true, 'editable' => false, 'group' => $cal->get_namespace(), ]; } else { */ // additional folders may come from kolab_storage_dav::folder_hierarchy() above // make sure we deal with caldav_calendar instances $cal = $this->_to_calendar($cal); $this->calendars[$cal->id] = $cal; $is_user = ($cal instanceof caldav_user_calendar); $calendars[$cal->id] = [ 'id' => $cal->id, 'name' => $cal->get_name(), 'listname' => $cal->get_foldername(), 'editname' => $cal->get_foldername(), 'title' => '', // $cal->get_title(), 'color' => $cal->get_color(), 'editable' => $cal->editable, 'group' => $is_user ? 'other user' : $cal->get_namespace(), - 'active' => $cal->is_active(), + 'active' => !isset($prefs[$cal->id]['active']) || !empty($prefs[$cal->id]['active']), 'owner' => $cal->get_owner(), 'removable' => !$cal->default, // extras to hide some elements in the UI - 'subscriptions' => false, + 'subscriptions' => $cal->subscriptions, 'driver' => 'caldav', ]; if (!$is_user) { $calendars[$cal->id] += [ 'default' => $cal->default, 'rights' => $cal->rights, 'showalarms' => $cal->alarms, 'history' => !empty($this->bonnie_api), 'children' => true, // TODO: determine if that folder indeed has child folders 'parent' => $parent_id, 'subtype' => $cal->subtype, 'caldavurl' => '', // $cal->get_caldav_url(), ]; } /* } */ if ($cal->subscriptions) { $calendars[$cal->id]['subscribed'] = $cal->is_subscribed(); } } /* // list virtual calendars showing invitations if ($this->rc->config->get('kolab_invitation_calendars') && !($filter & self::FILTER_INSERTABLE)) { foreach ([self::INVITATIONS_CALENDAR_PENDING, self::INVITATIONS_CALENDAR_DECLINED] as $id) { $cal = new caldav_invitation_calendar($id, $this->cal); if (!($filter & self::FILTER_ACTIVE) || $cal->is_active()) { $calendars[$id] = [ '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, 'counts' => $id == self::INVITATIONS_CALENDAR_PENDING, ]; if (is_object($tree)) { $tree->children[] = $cal; } } } } */ // append the virtual birthdays calendar if ($this->rc->config->get('calendar_contact_birthdays', false) && !($filter & self::FILTER_INSERTABLE)) { $id = self::BIRTHDAY_CALENDAR_ID; $prefs = $this->rc->config->get('kolab_calendars', []); // read local prefs if (!($filter & self::FILTER_ACTIVE) || !empty($prefs[$id]['active'])) { $calendars[$id] = [ 'id' => $id, 'name' => $this->cal->gettext('birthdays'), 'listname' => $this->cal->gettext('birthdays'), 'color' => !empty($prefs[$id]['color']) ? $prefs[$id]['color'] : '87CEFA', 'active' => !empty($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 the caldav_calendar instance for the given calendar ID * * @param string Calendar identifier * * @return ?caldav_calendar Object nor null if calendar doesn't exist */ public function get_calendar($id) { $this->_read_calendars(); // create calendar object if necessary if (empty($this->calendars[$id])) { if (in_array($id, [self::INVITATIONS_CALENDAR_PENDING, self::INVITATIONS_CALENDAR_DECLINED])) { return new caldav_invitation_calendar($id, $this->cal); } // for unsubscribed calendar folders if ($id !== self::BIRTHDAY_CALENDAR_ID) { $calendar = caldav_calendar::factory($id, $this->cal); if ($calendar->ready) { $this->calendars[$calendar->id] = $calendar; } } } return !empty($this->calendars[$id]) ? $this->calendars[$id] : null; } /** * 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; // TODO - $prop['subscribed'] = true; - $prop['alarms'] = !empty($prop['showalarms']); + $prop['type'] = 'event'; + $prop['alarms'] = !empty($prop['showalarms']); $id = $this->storage->folder_update($prop); if ($id === false) { return false; } + $prefs['kolab_calendars'] = $this->rc->config->get('kolab_calendars', []); + $prefs['kolab_calendars'][$id]['active'] = true; + + $this->rc->user->save_prefs($prefs); return $id; } /** * Update properties of an existing calendar * * @see calendar_driver::edit_calendar() */ public function edit_calendar($prop) { $id = $prop['id']; if (!in_array($id, [self::BIRTHDAY_CALENDAR_ID, self::INVITATIONS_CALENDAR_PENDING, self::INVITATIONS_CALENDAR_DECLINED])) { $prop['type'] = 'event'; $prop['alarms'] = !empty($prop['showalarms']); return $this->storage->folder_update($prop) !== false; } // fallback to local prefs for special calendars $prefs['kolab_calendars'] = $this->rc->config->get('kolab_calendars', []); unset($prefs['kolab_calendars'][$id]['showalarms']); 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'] = !empty($prop['showalarms']); } 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 (!empty($prop['id']) && ($cal = $this->get_calendar($prop['id'])) && !empty($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 (!empty($prop['recursive'])) { - foreach ((array) $this->storage->list_folders($cal->storage->name, '*', 'event') as $subfolder) { - if (isset($prop['permanent'])) { - if ($prop['permanent']) { - $this->storage->folder_subscribe($subfolder); - } - else { - $this->storage->folder_unsubscribe($subfolder); - } - } - - if (isset($prop['active'])) { - if ($prop['active']) { - $this->storage->folder_activate($subfolder); - } - else { - $this->storage->folder_deactivate($subfolder); - } - } - } - } - return $ret; + if (empty($prop['id'])) { + return false; } - else { - // save state in local prefs + + // save state in local prefs + if (isset($prop['active'])) { $prefs['kolab_calendars'] = $this->rc->config->get('kolab_calendars', []); $prefs['kolab_calendars'][$prop['id']]['active'] = !empty($prop['active']); + $this->rc->user->save_prefs($prefs); - return true; } - return false; + return true; } /** * Delete the given calendar with all its contents * * @see calendar_driver::delete_calendar() */ public function delete_calendar($prop) { if (!empty($prop['id'])) { if ($this->storage->folder_delete($prop['id'], 'event')) { // remove folder from user prefs $prefs['kolab_calendars'] = $this->rc->config->get('kolab_calendars', []); if (isset($prefs['kolab_calendars'][$prop['id']])) { unset($prefs['kolab_calendars'][$prop['id']]); $this->rc->user->save_prefs($prefs); } return true; } } 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) { $this->calendars = []; $this->search_more_results = false; /* // find unsubscribed IMAP folders that have "event" type if ($source == 'folders') { foreach ((array) $this->storage->search_folders('event', $query, ['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') { // we have slightly more space, so display twice the number $limit = $this->rc->config->get('autocomplete_max', 15) * 2; foreach ($this->storage->search_users($query, 0, [], $limit, $count) as $user) { $calendar = new caldav_user_calendar($user, $this->cal); $this->calendars[$calendar->id] = $calendar; // search for calendar folders shared by this user foreach ($this->storage->list_user_folders($user, 'event', false) as $foldername) { $cal = new caldav_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(); } /** * Get events from source. * * @param int Event's new start (unix timestamp) * @param int 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 bool Include virtual events (optional) * @param int 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) { $this->_read_calendars(); $calendars = array_keys($this->calendars); } $query = []; $events = []; $categories = []; if ($modifiedsince) { $query[] = ['changed', '>=', $modifiedsince]; } 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); $newcats = array_udiff( array_keys($categories), array_keys($old_categories), function($a, $b) { return strcasecmp($a, $b); } ); if (!empty($newcats)) { foreach ($newcats as $category) { $old_categories[$category] = ''; // no color set yet } $this->rc->user->save_prefs(['calendar_categories' => $old_categories]); } array_walk($events, 'caldav_driver::to_rcube_event'); return $events; } /** * Create instances of a recurring event * * @param array Hash array with event properties * @param DateTime Start date of the recurrence window * @param 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 $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); } /** * */ protected function get_recurrence_count($event, $dtstart) { // load the given event data into a libkolabxml container $event_xml = new kolab_format_event(); $event_xml->set($event); $event['_formatobj'] = $event_xml; // use libkolab to compute recurring events $recurrence = new kolab_date_recurrence($event['_formatobj']); $count = 0; while (($next_event = $recurrence->next_instance()) && $next_event['start'] <= $dtstart && $count < 1000) { $count++; } return $count; } /** * 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) { $special_calendars = [ self::BIRTHDAY_CALENDAR_ID, self::INVITATIONS_CALENDAR_PENDING, self::INVITATIONS_CALENDAR_DECLINED ]; // show default dialog for birthday calendar if (in_array($calendar['id'], $special_calendars)) { if ($calendar['id'] != self::BIRTHDAY_CALENDAR_ID) { unset($formfields['showalarms']); } // General tab $form['props'] = [ 'name' => $this->rc->gettext('properties'), 'fields' => $formfields, ]; return kolab_utils::folder_form($form, '', 'calendar'); } $form['props'] = [ 'name' => $this->rc->gettext('properties'), 'fields' => [ 'location' => $formfields['name'], 'color' => $formfields['color'], 'alarms' => $formfields['showalarms'], ], ]; return kolab_utils::folder_form($form, $folder, 'calendar', [], true); } } diff --git a/plugins/calendar/lib/calendar_ui.php b/plugins/calendar/lib/calendar_ui.php index 1d4daef2..83779834 100644 --- a/plugins/calendar/lib/calendar_ui.php +++ b/plugins/calendar/lib/calendar_ui.php @@ -1,1011 +1,1011 @@ * @author Thomas Bruederli * * Copyright (C) 2010, Lazlo Westerhof * Copyright (C) 2014, Kolab Systems AG * * 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 . */ class calendar_ui { private $rc; private $cal; private $ready = false; public $screen; function __construct($cal) { $this->cal = $cal; $this->rc = $cal->rc; $this->screen = $this->rc->task == 'calendar' ? ($this->rc->action ?: 'calendar') : 'other'; } /** * Calendar UI initialization and requests handlers */ public function init() { if ($this->ready) { // already done return; } // add taskbar button $this->cal->add_button([ 'command' => 'calendar', 'class' => 'button-calendar', 'classsel' => 'button-calendar button-selected', 'innerclass' => 'button-inner', 'label' => 'calendar.calendar', 'type' => 'link' ], 'taskbar' ); // load basic client script if ($this->rc->action != 'print') { $this->cal->include_script('calendar_base.js'); } $this->addCSS(); $this->ready = true; } /** * Register handler methods for the template engine */ public function init_templates() { $this->cal->register_handler('plugin.calendar_css', [$this, 'calendar_css']); $this->cal->register_handler('plugin.calendar_list', [$this, 'calendar_list']); $this->cal->register_handler('plugin.calendar_select', [$this, 'calendar_select']); $this->cal->register_handler('plugin.identity_select', [$this, 'identity_select']); $this->cal->register_handler('plugin.category_select', [$this, 'category_select']); $this->cal->register_handler('plugin.status_select', [$this, 'status_select']); $this->cal->register_handler('plugin.freebusy_select', [$this, 'freebusy_select']); $this->cal->register_handler('plugin.priority_select', [$this, 'priority_select']); $this->cal->register_handler('plugin.alarm_select', [$this, 'alarm_select']); $this->cal->register_handler('plugin.recurrence_form', [$this->cal->lib, 'recurrence_form']); $this->cal->register_handler('plugin.attendees_list', [$this, 'attendees_list']); $this->cal->register_handler('plugin.attendees_form', [$this, 'attendees_form']); $this->cal->register_handler('plugin.resources_form', [$this, 'resources_form']); $this->cal->register_handler('plugin.resources_list', [$this, 'resources_list']); $this->cal->register_handler('plugin.resources_searchform', [$this, 'resources_search_form']); $this->cal->register_handler('plugin.resource_info', [$this, 'resource_info']); $this->cal->register_handler('plugin.resource_calendar', [$this, 'resource_calendar']); $this->cal->register_handler('plugin.attendees_freebusy_table', [$this, 'attendees_freebusy_table']); $this->cal->register_handler('plugin.edit_attendees_notify', [$this, 'edit_attendees_notify']); $this->cal->register_handler('plugin.edit_recurrence_sync', [$this, 'edit_recurrence_sync']); $this->cal->register_handler('plugin.edit_recurring_warning', [$this, 'recurring_event_warning']); $this->cal->register_handler('plugin.event_rsvp_buttons', [$this, 'event_rsvp_buttons']); $this->cal->register_handler('plugin.agenda_options', [$this, 'agenda_options']); $this->cal->register_handler('plugin.events_import_form', [$this, 'events_import_form']); $this->cal->register_handler('plugin.events_export_form', [$this, 'events_export_form']); $this->cal->register_handler('plugin.object_changelog_table', ['libkolab', 'object_changelog_table']); $this->cal->register_handler('plugin.searchform', [$this->rc->output, 'search_form']); kolab_attachments_handler::ui(); } /** * Adds CSS stylesheets to the page header */ public function addCSS() { $skin_path = $this->cal->local_skin_path(); if ( $this->rc->task == 'calendar' && (!$this->rc->action || in_array($this->rc->action, ['index', 'print'])) ) { // Include fullCalendar style before skin file for simpler style overriding $this->cal->include_stylesheet($skin_path . '/fullcalendar.css'); } $this->cal->include_stylesheet($skin_path . '/calendar.css'); if ($this->rc->task == 'calendar' && $this->rc->action == 'print') { $this->cal->include_stylesheet($skin_path . '/print.css'); } } /** * Adds JS files to the page header */ public function addJS() { $this->cal->include_script('lib/js/moment.js'); $this->cal->include_script('lib/js/fullcalendar.js'); if ($this->rc->task == 'calendar' && $this->rc->action == 'print') { $this->cal->include_script('print.js'); } else { $this->rc->output->include_script('treelist.js'); $this->cal->api->include_script('libkolab/libkolab.js'); $this->cal->include_script('calendar_ui.js'); jqueryui::miniColors(); } } /** * Add custom style for the calendar UI */ function calendar_css($attrib = []) { $categories = $this->cal->driver->list_categories(); $calendars = $this->cal->driver->list_calendars(); $js_categories = []; $mode = $this->rc->config->get('calendar_event_coloring', $this->cal->defaults['calendar_event_coloring']); $css = "\n"; foreach ((array) $categories as $class => $color) { if (!empty($color)) { $js_categories[$class] = $color; $color = ltrim($color, '#'); $class = 'cat-' . asciiwords(strtolower($class), true); $css .= ".$class { color: #$color; }\n"; } } $this->rc->output->set_env('calendar_categories', $js_categories); foreach ((array) $calendars as $id => $prop) { if (!empty($prop['color'])) { $css .= $this->calendar_css_classes($id, $prop, $mode, $attrib); } } return html::tag('style', ['type' => 'text/css'], $css); } /** * Calendar folder specific CSS classes */ public function calendar_css_classes($id, $prop, $mode, $attrib = []) { $color = $folder_color = $prop['color']; // replace white with skin-defined color if (!empty($attrib['folder-fallback-color']) && preg_match('/^f+$/i', $folder_color)) { $folder_color = ltrim($attrib['folder-fallback-color'], '#'); } $class = 'cal-' . asciiwords($id, true); $css = "li .$class"; if (!empty($attrib['folder-class'])) { $css = str_replace('$class', $class, $attrib['folder-class']); } $css .= " { color: #$folder_color; }\n"; return $css . ".$class .handle { background-color: #$color; }\n"; } /** * Generate HTML content of the calendars list (or metadata only) */ function calendar_list($attrib = [], $js_only = false) { $html = ''; $jsenv = []; $tree = true; $calendars = $this->cal->driver->list_calendars(0, $tree); // walk folder tree if (is_object($tree)) { $html = $this->list_tree_html($tree, $calendars, $jsenv, $attrib); // append birthdays calendar which isn't part of $tree if (!empty($calendars[calendar_driver::BIRTHDAY_CALENDAR_ID])) { $bdaycal = $calendars[calendar_driver::BIRTHDAY_CALENDAR_ID]; $calendars = [calendar_driver::BIRTHDAY_CALENDAR_ID => $bdaycal]; } else { $calendars = []; // clear array for flat listing } } else if (isset($attrib['class'])) { // fall-back to flat folder listing $attrib['class'] .= ' flat'; } foreach ((array) $calendars as $id => $prop) { if (!empty($attrib['activeonly']) && empty($prop['active'])) { continue; } $li_content = $this->calendar_list_item($id, $prop, $jsenv, !empty($attrib['activeonly'])); $li_attr = [ 'id' => 'rcmlical' . $id, 'class' => isset($prop['group']) ? $prop['group'] : null, ]; $html .= html::tag('li', $li_attr, $li_content); } $this->rc->output->set_env('calendars', $jsenv); if ($js_only) { return; } $this->rc->output->set_env('source', rcube_utils::get_input_value('source', rcube_utils::INPUT_GET)); $this->rc->output->add_gui_object('calendarslist', !empty($attrib['id']) ? $attrib['id'] : 'rccalendarlist'); return html::tag('ul', $attrib, $html, html::$common_attrib); } /** * Return html for a structured list
    for the folder tree */ public function list_tree_html($node, $data, &$jsenv, $attrib) { $out = ''; foreach ($node->children as $folder) { $id = $folder->id; $prop = $data[$id]; $is_collapsed = false; // TODO: determine this somehow? $content = $this->calendar_list_item($id, $prop, $jsenv, !empty($attrib['activeonly'])); if (!empty($folder->children)) { $content .= html::tag('ul', ['style' => $is_collapsed ? "display:none;" : null], $this->list_tree_html($folder, $data, $jsenv, $attrib) ); } if (strlen($content)) { $li_attr = [ 'id' => 'rcmlical' . rcube_utils::html_identifier($id), 'class' => $prop['group'] . (!empty($prop['virtual']) ? ' virtual' : ''), ]; $out .= html::tag('li', $li_attr, $content); } } return $out; } /** * Helper method to build a calendar list item (HTML content and js data) */ public function calendar_list_item($id, $prop, &$jsenv, $activeonly = false) { // enrich calendar properties with settings from the driver if (empty($prop['virtual'])) { unset($prop['user_id']); $prop['alarms'] = $this->cal->driver->alarms; $prop['attendees'] = $this->cal->driver->attendees; $prop['freebusy'] = $this->cal->driver->freebusy; $prop['attachments'] = $this->cal->driver->attachments; $prop['undelete'] = $this->cal->driver->undelete; $prop['feedurl'] = $this->cal->get_url([ '_cal' => $this->cal->ical_feed_hash($id) . '.ics', 'action' => 'feed' ] ); $jsenv[$id] = $prop; } if (!empty($prop['title'])) { $title = $prop['title']; } else if ($prop['name'] != $prop['listname'] || strlen($prop['name']) > 25) { $title = html_entity_decode($prop['name'], ENT_COMPAT, RCUBE_CHARSET); } else { $title = ''; } $classes = ['calendar', 'cal-' . asciiwords($id, true)]; if (!empty($prop['virtual'])) { $classes[] = 'virtual'; } else if (empty($prop['editable'])) { $classes[] = 'readonly'; } if (!empty($prop['subscribed'])) { $classes[] = 'subscribed'; if ($prop['subscribed'] === 2) { $classes[] = 'partial'; } } if (!empty($prop['class'])) { $classes[] = $prop['class']; } $content = ''; if (!$activeonly || !empty($prop['active'])) { $label_id = 'cl:' . $id; $content = html::a( ['class' => 'calname', 'id' => $label_id, 'title' => $title, 'href' => '#'], rcube::Q(!empty($prop['editname']) ? $prop['editname'] : $prop['listname']) ); if (empty($prop['virtual'])) { $color = !empty($prop['color']) ? $prop['color'] : 'f00'; $actions = ''; - if (!EMPTY($prop['removable'])) { + if (!empty($prop['removable'])) { $actions .= html::a([ 'href' => '#', 'class' => 'remove', 'title' => $this->cal->gettext('removelist') ], ' ' ); } $actions .= html::a([ 'href' => '#', 'class' => 'quickview', 'title' => $this->cal->gettext('quickview'), 'role' => 'checkbox', 'aria-checked' => 'false' ], '' ); - if (!empty($prop['subscribed'])) { - $actions .= html::a([ - 'href' => '#', - 'class' => 'subscribed', - 'title' => $this->cal->gettext('calendarsubscribe'), - 'role' => 'checkbox', - 'aria-checked' => !empty($prop['subscribed']) ? 'true' : 'false' - ], ' ' - ); - } - if (!isset($prop['subscriptions']) || $prop['subscriptions'] !== false) { - $content .= html::tag('input', [ - 'type' => 'checkbox', - 'name' => '_cal[]', - 'value' => $id, - 'checked' => !empty($prop['active']), - 'aria-labelledby' => $label_id - ]) - . html::span('actions', $actions) - . html::span(['class' => 'handle', 'style' => "background-color: #$color"], ' '); + if (!empty($prop['subscribed'])) { + $actions .= html::a([ + 'href' => '#', + 'class' => 'subscribed', + 'title' => $this->cal->gettext('calendarsubscribe'), + 'role' => 'checkbox', + 'aria-checked' => !empty($prop['subscribed']) ? 'true' : 'false' + ], ' ' + ); + } } + + $content .= html::tag('input', [ + 'type' => 'checkbox', + 'name' => '_cal[]', + 'value' => $id, + 'checked' => !empty($prop['active']), + 'aria-labelledby' => $label_id + ]) + . html::span('actions', $actions) + . html::span(['class' => 'handle', 'style' => "background-color: #$color"], ' '); } $content = html::div(join(' ', $classes), $content); } return $content; } /** * Render a HTML for agenda options form */ function agenda_options($attrib = []) { $attrib += ['id' => 'agendaoptions']; $attrib['style'] = 'display:none'; $select_range = new html_select(['name' => 'listrange', 'id' => 'agenda-listrange', 'class' => 'form-control custom-select']); $select_range->add(1 . ' ' . preg_replace('/\(.+\)/', '', $this->cal->lib->gettext('days')), ''); foreach ([2,5,7,14,30,60,90,180,365] as $days) { $select_range->add($days . ' ' . preg_replace('/\(|\)/', '', $this->cal->lib->gettext('days')), $days); } $html = html::span('input-group', html::label(['for' => 'agenda-listrange', 'class' => 'input-group-prepend'], html::span('input-group-text', $this->cal->gettext('listrange')) ) . $select_range->show($this->rc->config->get('calendar_agenda_range', $this->cal->defaults['calendar_agenda_range'])) ); return html::div($attrib, $html); } /** * Render a HTML select box for calendar selection */ function calendar_select($attrib = []) { $attrib['name'] = 'calendar'; $attrib['is_escaped'] = true; $select = new html_select($attrib); foreach ((array) $this->cal->driver->list_calendars() as $id => $prop) { if ( !empty($prop['editable']) || (!empty($prop['rights']) && strpos($prop['rights'], 'i') !== false) ) { $select->add($prop['name'], $id); } } return $select->show(null); } /** * Render a HTML select box for user identity selection */ function identity_select($attrib = []) { $attrib['name'] = 'identity'; $select = new html_select($attrib); $identities = $this->rc->user->list_emails(); foreach ($identities as $ident) { $select->add(format_email_recipient($ident['email'], $ident['name']), $ident['identity_id']); } return $select->show(null); } /** * Render a HTML select box to select an event category */ function category_select($attrib = []) { $attrib['name'] = 'categories'; $select = new html_select($attrib); $select->add('---', ''); foreach (array_keys((array) $this->cal->driver->list_categories()) as $cat) { $select->add($cat, $cat); } return $select->show(null); } /** * Render a HTML select box for status property */ function status_select($attrib = []) { $attrib['name'] = 'status'; $select = new html_select($attrib); $select->add('---', ''); $select->add($this->cal->gettext('status-confirmed'), 'CONFIRMED'); $select->add($this->cal->gettext('status-cancelled'), 'CANCELLED'); $select->add($this->cal->gettext('status-tentative'), 'TENTATIVE'); return $select->show(null); } /** * Render a HTML select box for free/busy/out-of-office property */ function freebusy_select($attrib = []) { $attrib['name'] = 'freebusy'; $select = new html_select($attrib); $select->add($this->cal->gettext('free'), 'free'); $select->add($this->cal->gettext('busy'), 'busy'); // out-of-office is not supported by libkolabxml (#3220) // $select->add($this->cal->gettext('outofoffice'), 'outofoffice'); $select->add($this->cal->gettext('tentative'), 'tentative'); return $select->show(null); } /** * Render a HTML select for event priorities */ function priority_select($attrib = []) { $attrib['name'] = 'priority'; $select = new html_select($attrib); $select->add('---', '0'); $select->add('1 ' . $this->cal->gettext('highest'), '1'); $select->add('2 ' . $this->cal->gettext('high'), '2'); $select->add('3 ', '3'); $select->add('4 ', '4'); $select->add('5 ' . $this->cal->gettext('normal'), '5'); $select->add('6 ', '6'); $select->add('7 ', '7'); $select->add('8 ' . $this->cal->gettext('low'), '8'); $select->add('9 ' . $this->cal->gettext('lowest'), '9'); return $select->show(null); } /** * Render HTML form for alarm configuration */ function alarm_select($attrib = []) { return $this->cal->lib->alarm_select($attrib, $this->cal->driver->alarm_types, $this->cal->driver->alarm_absolute); } /** * Render HTML for attendee notification warning */ function edit_attendees_notify($attrib = []) { $checkbox = new html_checkbox(['name' => '_notify', 'id' => 'edit-attendees-donotify', 'value' => 1, 'class' => 'pretty-checkbox']); return html::div($attrib, html::label(null, $checkbox->show(1) . ' ' . $this->cal->gettext('sendnotifications'))); } /** * Render HTML for recurrence option to align start date with the recurrence rule */ function edit_recurrence_sync($attrib = []) { $checkbox = new html_checkbox(['name' => '_start_sync', 'value' => 1, 'class' => 'pretty-checkbox']); return html::div($attrib, html::label(null, $checkbox->show(1) . ' ' . $this->cal->gettext('eventstartsync'))); } /** * Generate the form for recurrence settings */ function recurring_event_warning($attrib = []) { $attrib['id'] = 'edit-recurring-warning'; $radio = new html_radiobutton(['name' => '_savemode', 'class' => 'edit-recurring-savemode']); $form = html::label(null, $radio->show('', ['value' => 'current']) . $this->cal->gettext('currentevent')) . ' ' . html::label(null, $radio->show('', ['value' => 'future']) . $this->cal->gettext('futurevents')) . ' ' . html::label(null, $radio->show('all', ['value' => 'all']) . $this->cal->gettext('allevents')) . ' ' . html::label(null, $radio->show('', ['value' => 'new']) . $this->cal->gettext('saveasnew')); return html::div($attrib, html::div('message', $this->cal->gettext('changerecurringeventwarning')) . html::div('savemode', $form) ); } /** * Form for uploading and importing events */ function events_import_form($attrib = []) { if (empty($attrib['id'])) { $attrib['id'] = 'rcmImportForm'; } // Get max filesize, enable upload progress bar $max_filesize = $this->rc->upload_init(); $accept = '.ics, text/calendar, text/x-vcalendar, application/ics'; if (class_exists('ZipArchive', false)) { $accept .= ', .zip, application/zip'; } $input = new html_inputfield([ 'id' => 'importfile', 'type' => 'file', 'name' => '_data', 'size' => !empty($attrib['uploadfieldsize']) ? $attrib['uploadfieldsize'] : null, 'accept' => $accept ]); $select = new html_select(['name' => '_range', 'id' => 'event-import-range']); $select->add([ $this->cal->gettext('onemonthback'), $this->cal->gettext(['name' => 'nmonthsback', 'vars' => ['nr'=>2]]), $this->cal->gettext(['name' => 'nmonthsback', 'vars' => ['nr'=>3]]), $this->cal->gettext(['name' => 'nmonthsback', 'vars' => ['nr'=>6]]), $this->cal->gettext(['name' => 'nmonthsback', 'vars' => ['nr'=>12]]), $this->cal->gettext('all'), ], ['1','2','3','6','12',0] ); $html = html::div('form-section form-group row', html::label(['class' => 'col-sm-4 col-form-label', 'for' => 'importfile'], rcube::Q($this->rc->gettext('importfromfile')) ) . html::div('col-sm-8', $input->show() . html::div('hint', $this->rc->gettext(['name' => 'maxuploadsize', 'vars' => ['size' => $max_filesize]])) ) ); $html .= html::div('form-section form-group row', html::label(['for' => 'event-import-calendar', 'class' => 'col-form-label col-sm-4'], $this->cal->gettext('calendar') ) . html::div('col-sm-8', $this->calendar_select(['name' => 'calendar', 'id' => 'event-import-calendar'])) ); $html .= html::div('form-section form-group row', html::label(['for' => 'event-import-range', 'class' => 'col-form-label col-sm-4'], $this->cal->gettext('importrange') ) . html::div('col-sm-8', $select->show(1)) ); $this->rc->output->add_gui_object('importform', $attrib['id']); $this->rc->output->add_label('import'); return html::tag('p', null, $this->cal->gettext('importtext')) . html::tag('form', [ 'action' => $this->rc->url(['task' => 'calendar', 'action' => 'import_events']), 'method' => 'post', 'enctype' => 'multipart/form-data', 'id' => $attrib['id'] ], $html ); } /** * Form to select options for exporting events */ function events_export_form($attrib = []) { if (empty($attrib['id'])) { $attrib['id'] = 'rcmExportForm'; } $html = html::div('form-section form-group row', html::label(['for' => 'event-export-calendar', 'class' => 'col-sm-4 col-form-label'], $this->cal->gettext('calendar') ) . html::div('col-sm-8', $this->calendar_select(['name' => 'calendar', 'id' => 'event-export-calendar', 'class' => 'form-control custom-select'])) ); $select = new html_select([ 'name' => 'range', 'id' => 'event-export-range', 'class' => 'form-control custom-select rounded-right' ]); $select->add([ $this->cal->gettext('all'), $this->cal->gettext('onemonthback'), $this->cal->gettext(['name' => 'nmonthsback', 'vars' => ['nr' => 2]]), $this->cal->gettext(['name' => 'nmonthsback', 'vars' => ['nr' => 3]]), $this->cal->gettext(['name' => 'nmonthsback', 'vars' => ['nr' => 6]]), $this->cal->gettext(['name' => 'nmonthsback', 'vars' => ['nr' => 12]]), $this->cal->gettext('customdate'), ], [0,'1','2','3','6','12','custom'] ); $startdate = new html_inputfield([ 'name' => 'start', 'size' => 11, 'id' => 'event-export-startdate', 'style' => 'display:none' ]); $html .= html::div('form-section form-group row', html::label(['for' => 'event-export-range', 'class' => 'col-sm-4 col-form-label'], $this->cal->gettext('exportrange') ) . html::div('col-sm-8 input-group', $select->show(0) . $startdate->show()) ); $checkbox = new html_checkbox([ 'name' => 'attachments', 'id' => 'event-export-attachments', 'value' => 1, 'class' => 'form-check-input pretty-checkbox' ]); $html .= html::div('form-section form-check row', html::label(['for' => 'event-export-attachments', 'class' => 'col-sm-4 col-form-label'], $this->cal->gettext('exportattachments') ) . html::div('col-sm-8', $checkbox->show(1)) ); $this->rc->output->add_gui_object('exportform', $attrib['id']); return html::tag('form', $attrib + [ 'action' => $this->rc->url(['task' => 'calendar', 'action' => 'export_events']), 'method' => 'post', 'id' => $attrib['id'] ], $html ); } /** * Handler for calendar form template. * The form content could be overriden by the driver */ function calendar_editform($action, $calendar = []) { $this->action = $action; $this->calendar = $calendar; // load miniColors js/css files jqueryui::miniColors(); $this->rc->output->set_env('pagetitle', $this->cal->gettext('calendarprops')); $this->rc->output->add_handler('folderform', [$this, 'calendarform']); $this->rc->output->send('libkolab.folderform'); } /** * Handler for calendar form template. * The form content could be overriden by the driver */ function calendarform($attrib) { // compose default calendar form fields $input_name = new html_inputfield(['name' => 'name', 'id' => 'calendar-name', 'size' => 20]); $input_color = new html_inputfield(['name' => 'color', 'id' => 'calendar-color', 'size' => 7, 'class' => 'colors']); $formfields = [ 'name' => [ 'label' => $this->cal->gettext('name'), 'value' => $input_name->show(isset($this->calendar['name']) ? $this->calendar['name'] : ''), 'id' => 'calendar-name', ], 'color' => [ 'label' => $this->cal->gettext('color'), 'value' => $input_color->show(isset($this->calendar['color']) ? $this->calendar['color'] : ''), 'id' => 'calendar-color', ], ]; if (!empty($this->cal->driver->alarms)) { $checkbox = new html_checkbox(['name' => 'showalarms', 'id' => 'calendar-showalarms', 'value' => 1]); $formfields['showalarms'] = [ 'label' => $this->cal->gettext('showalarms'), 'value' => $checkbox->show(!empty($this->calendar['showalarms']) ? 1 : 0), 'id' => 'calendar-showalarms', ]; } // allow driver to extend or replace the form content return html::tag('form', $attrib + ['action' => '#', 'method' => 'get', 'id' => 'calendarpropform'], $this->cal->driver->calendar_form($this->action, $this->calendar, $formfields) ); } /** * Render HTML for attendees table */ function attendees_list($attrib = []) { // add "noreply" checkbox to attendees table only $invitations = strpos($attrib['id'], 'attend') !== false; $invite = new html_checkbox(['value' => 1, 'id' => 'edit-attendees-invite']); $table = new html_table(['cols' => 5 + intval($invitations), 'border' => 0, 'cellpadding' => 0, 'class' => 'rectable']); $table->add_header('role', $this->cal->gettext('role')); $table->add_header('name', $this->cal->gettext(!empty($attrib['coltitle']) ? $attrib['coltitle'] : 'attendee')); $table->add_header('availability', $this->cal->gettext('availability')); $table->add_header('confirmstate', $this->cal->gettext('confirmstate')); if ($invitations) { $table->add_header(['class' => 'invite', 'title' => $this->cal->gettext('sendinvitations')], $invite->show(1) . html::label('edit-attendees-invite', html::span('inner', $this->cal->gettext('sendinvitations'))) ); } $table->add_header('options', ''); // hide invite column if disabled by config $itip_notify = (int)$this->rc->config->get('calendar_itip_send_option', $this->cal->defaults['calendar_itip_send_option']); if ($invitations && !($itip_notify & 2)) { $css = sprintf('#%s td.invite, #%s th.invite { display:none !important }', $attrib['id'], $attrib['id']); $this->rc->output->add_footer(html::tag('style', ['type' => 'text/css'], $css)); } return $table->show($attrib); } /** * Render HTML for attendees adding form */ function attendees_form($attrib = []) { $input = new html_inputfield([ 'name' => 'participant', 'id' => 'edit-attendee-name', 'class' => 'form-control' ]); $textarea = new html_textarea([ 'name' => 'comment', 'id' => 'edit-attendees-comment', 'class' => 'form-control', 'rows' => 4, 'cols' => 55, 'title' => $this->cal->gettext('itipcommenttitle') ]); return html::div($attrib, html::div('form-searchbar', $input->show() . ' ' . html::tag('input', [ 'type' => 'button', 'class' => 'button', 'id' => 'edit-attendee-add', 'value' => $this->cal->gettext('addattendee') ]) . ' ' . html::tag('input', [ 'type' => 'button', 'class' => 'button', 'id' => 'edit-attendee-schedule', 'value' => $this->cal->gettext('scheduletime') . '...' ]) ) . html::p('attendees-commentbox', html::label('edit-attendees-comment', $this->cal->gettext('itipcomment')) . $textarea->show()) ); } /** * Render HTML for resources adding form */ function resources_form($attrib = []) { $input = new html_inputfield(['name' => 'resource', 'id' => 'edit-resource-name', 'class' => 'form-control']); return html::div($attrib, html::div('form-searchbar', $input->show() . ' ' . html::tag('input', [ 'type' => 'button', 'class' => 'button', 'id' => 'edit-resource-add', 'value' => $this->cal->gettext('addresource') ]) . ' ' . html::tag('input', [ 'type' => 'button', 'class' => 'button', 'id' => 'edit-resource-find', 'value' => $this->cal->gettext('findresources') . '...' ]) ) ); } /** * Render HTML for resources list */ function resources_list($attrib = []) { $attrib += ['id' => 'calendar-resources-list']; $this->rc->output->add_gui_object('resourceslist', $attrib['id']); return html::tag('ul', $attrib, '', html::$common_attrib); } /** * */ public function resource_info($attrib = []) { $attrib += ['id' => 'calendar-resources-info']; $this->rc->output->add_gui_object('resourceinfo', $attrib['id']); $this->rc->output->add_gui_object('resourceownerinfo', $attrib['id'] . '-owner'); // copy address book labels for owner details to client $this->rc->output->add_label('name','firstname','surname','department','jobtitle','email','phone','address'); $table_attrib = ['id','class','style','width','summary','cellpadding','cellspacing','border']; return html::tag('table', $attrib, html::tag('tbody', null, ''), $table_attrib) . html::tag('table', ['id' => $attrib['id'] . '-owner', 'style' => 'display:none'] + $attrib, html::tag('thead', null, html::tag('tr', null, html::tag('td', ['colspan' => 2], rcube::Q($this->cal->gettext('resourceowner'))) ) ) . html::tag('tbody', null, ''), $table_attrib ); } /** * */ public function resource_calendar($attrib = []) { $attrib += ['id' => 'calendar-resources-calendar']; $this->rc->output->add_gui_object('resourceinfocalendar', $attrib['id']); return html::div($attrib, ''); } /** * GUI object 'searchform' for the resource finder dialog * * @param array $attrib Named parameters * * @return string HTML code for the gui object */ function resources_search_form($attrib) { $attrib += [ 'command' => 'search-resource', 'reset-command' => 'reset-resource-search', 'id' => 'rcmcalresqsearchbox', 'autocomplete' => 'off', 'form-name' => 'rcmcalresoursqsearchform', 'gui-object' => 'resourcesearchform', ]; // add form tag around text field return $this->rc->output->search_form($attrib); } /** * */ function attendees_freebusy_table($attrib = []) { $table = new html_table(['cols' => 2, 'border' => 0, 'cellspacing' => 0]); $table->add('attendees', html::tag('h3', 'boxtitle', $this->cal->gettext('tabattendees')) . html::div('timesheader', ' ') . html::div(['id' => 'schedule-attendees-list', 'class' => 'attendees-list'], '') ); $table->add('times', html::div('scroll', html::tag('table', ['id' => 'schedule-freebusy-times', 'border' => 0, 'cellspacing' => 0], html::tag('thead') . html::tag('tbody') ) . html::div(['id' => 'schedule-event-time', 'style' => 'display:none'], ' ') ) ); return $table->show($attrib); } /** * */ function event_invitebox($attrib = []) { if (!empty($this->cal->event)) { return html::div($attrib, $this->cal->itip->itip_object_details_table($this->cal->event, $this->cal->itip->gettext('itipinvitation')) . $this->cal->invitestatus ); } return ''; } function event_rsvp_buttons($attrib = []) { $actions = ['accepted', 'tentative', 'declined']; if (empty($attrib['delegate']) || $attrib['delegate'] !== 'false') { $actions[] = 'delegated'; } return $this->cal->itip->itip_rsvp_buttons($attrib, $actions); } } diff --git a/plugins/libkolab/lib/kolab_dav_client.php b/plugins/libkolab/lib/kolab_dav_client.php index bf8dc7ac..79c23be8 100644 --- a/plugins/libkolab/lib/kolab_dav_client.php +++ b/plugins/libkolab/lib/kolab_dav_client.php @@ -1,777 +1,776 @@ * * Copyright (C) 2022, Apheleia IT AG * * 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 . */ class kolab_dav_client { public $url; protected $user; protected $password; protected $rc; protected $responseHeaders = []; /** * Object constructor */ public function __construct($url) { $this->rc = rcube::get_instance(); $parsedUrl = parse_url($url); if (!empty($parsedUrl['user']) && !empty($parsedUrl['pass'])) { $this->user = rawurldecode($parsedUrl['user']); $this->password = rawurldecode($parsedUrl['pass']); $url = str_replace(rawurlencode($this->user) . ':' . rawurlencode($this->password) . '@', '', $url); } else { $this->user = $this->rc->user->get_username(); $this->password = $this->rc->decrypt($_SESSION['password']); } $this->url = $url; } /** * Execute HTTP request to a DAV server */ protected function request($path, $method, $body = '', $headers = []) { $rcube = rcube::get_instance(); $debug = (array) $rcube->config->get('dav_debug'); $request_config = [ 'store_body' => true, 'follow_redirects' => true, ]; $this->responseHeaders = []; if ($path && ($rootPath = parse_url($this->url, PHP_URL_PATH)) && strpos($path, $rootPath) === 0) { $path = substr($path, strlen($rootPath)); } try { $request = $this->initRequest($this->url . $path, $method, $request_config); $request->setAuth($this->user, $this->password); if ($body) { $request->setBody($body); $request->setHeader(['Content-Type' => 'application/xml; charset=utf-8']); } if (!empty($headers)) { $request->setHeader($headers); } if ($debug) { rcube::write_log('dav', "C: {$method}: " . (string) $request->getUrl() . "\n" . $this->debugBody($body, $request->getHeaders())); } $response = $request->send(); $body = $response->getBody(); $code = $response->getStatus(); if ($debug) { rcube::write_log('dav', "S: [{$code}]\n" . $this->debugBody($body, $response->getHeader())); } if ($code >= 300) { throw new Exception("DAV Error ($code):\n{$body}"); } $this->responseHeaders = $response->getHeader(); return $this->parseXML($body); } catch (Exception $e) { rcube::raise_error($e, true, false); return false; } } /** * Discover DAV home (root) collection of specified type. * * @param string $component Component to filter by (VEVENT, VTODO, VCARD) * * @return string|false Home collection location or False on error */ public function discover($component = 'VEVENT') { $roots = [ 'VEVENT' => 'calendars', 'VTODO' => 'calendars', 'VCARD' => 'addressbooks', ]; $path = parse_url($this->url, PHP_URL_PATH); $body = '' . '' . '' . '' . '' . ''; // Note: Cyrus CardDAV service requires Depth:1 (CalDAV works without it) $response = $this->request('/' . $roots[$component], 'PROPFIND', $body, ['Depth' => 1, 'Prefer' => 'return-minimal']); if (empty($response)) { return false; } $elements = $response->getElementsByTagName('response'); foreach ($elements as $element) { foreach ($element->getElementsByTagName('prop') as $prop) { $principal_href = $prop->nodeValue; break; } } if ($path && strpos($principal_href, $path) === 0) { $principal_href = substr($principal_href, strlen($path)); } $homes = [ 'VEVENT' => 'calendar-home-set', 'VTODO' => 'calendar-home-set', 'VCARD' => 'addressbook-home-set', ]; $ns = [ 'VEVENT' => 'caldav', 'VTODO' => 'caldav', 'VCARD' => 'carddav', ]; $body = '' . '' . '' . '' . '' . ''; $response = $this->request($principal_href, 'PROPFIND', $body); if (empty($response)) { return false; } $elements = $response->getElementsByTagName('response'); foreach ($elements as $element) { foreach ($element->getElementsByTagName('prop') as $prop) { $root_href = $prop->nodeValue; break; } } if (!empty($root_href)) { if ($path && strpos($root_href, $path) === 0) { $root_href = substr($root_href, strlen($path)); } } else { // Kolab iRony's calendar root $root_href = '/' . $roots[$component] . '/' . rawurlencode($this->user); } return $root_href; } /** * Get list of folders of specified type. * * @param string $component Component to filter by (VEVENT, VTODO, VCARD) * * @return false|array List of folders' metadata or False on error */ public function listFolders($component = 'VEVENT') { $root_href = $this->discover($component); if ($root_href === false) { return false; } $ns = 'xmlns:d="DAV:" xmlns:cs="http://calendarserver.org/ns/"'; $props = ''; if ($component != 'VCARD') { $ns .= ' xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:a="http://apple.com/ns/ical/" xmlns:k="Kolab:"'; $props = '' . '' - . '' - . ''; + . ''; } $body = '' . '' . '' . '' . '' // . '' . '' . $props . '' . ''; // Note: Cyrus CardDAV service requires Depth:1 (CalDAV works without it) $response = $this->request($root_href, 'PROPFIND', $body, ['Depth' => 1, 'Prefer' => 'return-minimal']); if (empty($response)) { return false; } $folders = []; foreach ($response->getElementsByTagName('response') as $element) { $folder = $this->getFolderPropertiesFromResponse($element); // Note: Addressbooks don't have 'type' specified if (($component == 'VCARD' && in_array('addressbook', $folder['resource_type'])) || $folder['type'] === $component ) { $folders[] = $folder; } } return $folders; } /** * Create a DAV object in a folder * * @param string $location Object location * @param string $content Object content * @param string $component Content type (VEVENT, VTODO, VCARD) * * @return false|string|null ETag string (or NULL) on success, False on error */ public function create($location, $content, $component = 'VEVENT') { $ctype = [ 'VEVENT' => 'text/calendar', 'VTODO' => 'text/calendar', 'VCARD' => 'text/vcard', ]; $headers = ['Content-Type' => $ctype[$component] . '; charset=utf-8']; $response = $this->request($location, 'PUT', $content, $headers); if ($response !== false) { $etag = $this->responseHeaders['etag']; if (preg_match('|^".*"$|', $etag)) { $etag = substr($etag, 1, -1); } return $etag; } return false; } /** * Update a DAV object in a folder * * @param string $location Object location * @param string $content Object content * @param string $component Content type (VEVENT, VTODO, VCARD) * * @return false|string|null ETag string (or NULL) on success, False on error */ public function update($location, $content, $component = 'VEVENT') { return $this->create($location, $content, $component); } /** * Delete a DAV object from a folder * * @param string $location Object location * * @return bool True on success, False on error */ public function delete($location) { $response = $this->request($location, 'DELETE', '', ['Depth' => 1, 'Prefer' => 'return-minimal']); return $response !== false; } /** * Get folder properties. * * @param string $location Object location * * @return false|array Folder metadata or False on error */ public function folderInfo($location) { $body = '' . '' . '' . ''; // Note: Cyrus CardDAV service requires Depth:1 (CalDAV works without it) $response = $this->request($location, 'PROPFIND', $body, ['Depth' => 1, 'Prefer' => 'return-minimal']); if (!empty($response) && ($element = $response->getElementsByTagName('response')) && ($folder = $this->getFolderPropertiesFromResponse($element)) ) { return $folder; } return false; } /** * Create a DAV folder * * @param string $location Object location (relative to the user home) * @param string $component Content type (VEVENT, VTODO, VCARD) * @param array $properties Object content * * @return bool True on success, False on error */ public function folderCreate($location, $component, $properties = []) { // Create the collection $response = $this->request($location, 'MKCOL'); if (empty($response)) { return false; } // Update collection properties return $this->folderUpdate($location, $component, $properties); } /** * Delete a DAV folder * * @param string $location Folder location * * @return bool True on success, False on error */ public function folderDelete($location) { $response = $this->request($location, 'DELETE'); return $response !== false; } /** * Update a DAV folder * * @param string $location Object location * @param string $component Content type (VEVENT, VTODO, VCARD) * @param array $properties Object content * * @return bool True on success, False on error */ public function folderUpdate($location, $component, $properties = []) { $ns = 'xmlns:d="DAV:"'; $props = ''; if ($component == 'VCARD') { $ns .= ' xmlns:c="urn:ietf:params:xml:ns:carddav"'; // Resourcetype property is protected // $props = ''; } else { $ns .= ' xmlns:c="urn:ietf:params:xml:ns:caldav"'; // Resourcetype property is protected // $props = ''; /* // Note: These are set by Cyrus automatically for calendars . '' . '' . '' . '' . '' . '' . ''; */ } foreach ($properties as $name => $value) { if ($name == 'name') { $props .= '' . htmlspecialchars($value, ENT_XML1, 'UTF-8') . ''; } else if ($name == 'color' && strlen($value)) { if ($value[0] != '#') { $value = '#' . $value; } $ns .= ' xmlns:a="http://apple.com/ns/ical/"'; $props .= '' . htmlspecialchars($value, ENT_XML1, 'UTF-8') . ''; } - else if ($name == 'subscribed' || $name == 'alarms') { + else if ($name == 'alarms') { if (!strpos($ns, 'Kolab:')) { $ns .= ' xmlns:k="Kolab:"'; } $props .= "" . ($value ? 'true' : 'false') . ""; } } if (empty($props)) { return true; } $body = '' . '' . '' . '' . $props . '' . '' . ''; $response = $this->request($location, 'PROPPATCH', $body); // TODO: Should we make sure "200 OK" status is set for all requested properties? return $response !== false; } /** * Fetch DAV objects metadata (ETag, href) a folder * * @param string $location Folder location * @param string $component Object type (VEVENT, VTODO, VCARD) * * @return false|array Objects metadata on success, False on error */ public function getIndex($location, $component = 'VEVENT') { $queries = [ 'VEVENT' => 'calendar-query', 'VTODO' => 'calendar-query', 'VCARD' => 'addressbook-query', ]; $ns = [ 'VEVENT' => 'caldav', 'VTODO' => 'caldav', 'VCARD' => 'carddav', ]; $filter = ''; if ($component != 'VCARD') { $filter = '' . '' . ''; } $body = '' .' ' . '' . '' . '' . ($filter ? "$filter" : '') . ''; $response = $this->request($location, 'REPORT', $body, ['Depth' => 1, 'Prefer' => 'return-minimal']); if (empty($response)) { return false; } $objects = []; foreach ($response->getElementsByTagName('response') as $element) { $objects[] = $this->getObjectPropertiesFromResponse($element); } return $objects; } /** * Fetch DAV objects data from a folder * * @param string $location Folder location * @param string $component Object type (VEVENT, VTODO, VCARD) * @param array $hrefs List of objects' locations to fetch (empty for all objects) * * @return false|array Objects metadata on success, False on error */ public function getData($location, $component = 'VEVENT', $hrefs = []) { if (empty($hrefs)) { return []; } $body = ''; foreach ($hrefs as $href) { $body .= '' . $href . ''; } $queries = [ 'VEVENT' => 'calendar-multiget', 'VTODO' => 'calendar-multiget', 'VCARD' => 'addressbook-multiget', ]; $ns = [ 'VEVENT' => 'caldav', 'VTODO' => 'caldav', 'VCARD' => 'carddav', ]; $types = [ 'VEVENT' => 'calendar-data', 'VTODO' => 'calendar-data', 'VCARD' => 'address-data', ]; $body = '' .' ' . '' . '' . '' . '' . $body . ''; $response = $this->request($location, 'REPORT', $body, ['Depth' => 1, 'Prefer' => 'return-minimal']); if (empty($response)) { return false; } $objects = []; foreach ($response->getElementsByTagName('response') as $element) { $objects[] = $this->getObjectPropertiesFromResponse($element); } return $objects; } /** * Parse XML content */ protected function parseXML($xml) { $doc = new DOMDocument('1.0', 'UTF-8'); if (stripos($xml, 'loadXML($xml)) { throw new Exception("Failed to parse XML"); } $doc->formatOutput = true; } return $doc; } /** * Parse request/response body for debug purposes */ protected function debugBody($body, $headers) { $head = ''; foreach ($headers as $header_name => $header_value) { $head .= "{$header_name}: {$header_value}\n"; } if (stripos($body, 'formatOutput = true; $doc->preserveWhiteSpace = false; if (!$doc->loadXML($body)) { throw new Exception("Failed to parse XML"); } $body = $doc->saveXML(); } return $head . "\n" . rtrim($body); } /** * Extract folder properties from a server 'response' element */ protected function getFolderPropertiesFromResponse(DOMNode $element) { if ($href = $element->getElementsByTagName('href')->item(0)) { $href = $href->nodeValue; /* $path = parse_url($this->url, PHP_URL_PATH); if ($path && strpos($href, $path) === 0) { $href = substr($href, strlen($path)); } */ } if ($color = $element->getElementsByTagName('calendar-color')->item(0)) { if (preg_match('/^#[0-9a-fA-F]{6,8}$/', $color->nodeValue)) { $color = substr($color->nodeValue, 1); } else { $color = null; } } if ($name = $element->getElementsByTagName('displayname')->item(0)) { $name = $name->nodeValue; } if ($ctag = $element->getElementsByTagName('getctag')->item(0)) { $ctag = $ctag->nodeValue; } $component = null; if ($set_element = $element->getElementsByTagName('supported-calendar-component-set')->item(0)) { if ($comp_element = $set_element->getElementsByTagName('comp')->item(0)) { $component = $comp_element->attributes->getNamedItem('name')->nodeValue; } } $types = []; if ($type_element = $element->getElementsByTagName('resourcetype')->item(0)) { foreach ($type_element->childNodes as $node) { $_type = explode(':', $node->nodeName); $types[] = count($_type) > 1 ? $_type[1] : $_type[0]; } } $result = [ 'href' => $href, 'name' => $name, 'ctag' => $ctag, 'color' => $color, 'type' => $component, 'resource_type' => $types, ]; - foreach (['subscribed', 'alarms'] as $tag) { + foreach (['alarms'] as $tag) { if ($el = $element->getElementsByTagName($tag)->item(0)) { if (strlen($el->nodeValue) > 0) { $result[$tag] = strtolower($el->nodeValue) === 'true'; } } } return $result; } /** * Extract object properties from a server 'response' element */ protected function getObjectPropertiesFromResponse(DOMNode $element) { $uid = null; if ($href = $element->getElementsByTagName('href')->item(0)) { $href = $href->nodeValue; /* $path = parse_url($this->url, PHP_URL_PATH); if ($path && strpos($href, $path) === 0) { $href = substr($href, strlen($path)); } */ // Extract UID from the URL $href_parts = explode('/', $href); $uid = preg_replace('/\.[a-z]+$/', '', $href_parts[count($href_parts)-1]); } if ($data = $element->getElementsByTagName('calendar-data')->item(0)) { $data = $data->nodeValue; } else if ($data = $element->getElementsByTagName('address-data')->item(0)) { $data = $data->nodeValue; } if ($etag = $element->getElementsByTagName('getetag')->item(0)) { $etag = $etag->nodeValue; if (preg_match('|^".*"$|', $etag)) { $etag = substr($etag, 1, -1); } } return [ 'href' => $href, 'data' => $data, 'etag' => $etag, 'uid' => $uid, ]; } /** * Initialize HTTP request object */ protected function initRequest($url = '', $method = 'GET', $config = array()) { $rcube = rcube::get_instance(); $http_config = (array) $rcube->config->get('kolab_http_request'); // deprecated configuration options if (empty($http_config)) { foreach (array('ssl_verify_peer', 'ssl_verify_host') as $option) { $value = $rcube->config->get('kolab_' . $option, true); if (is_bool($value)) { $http_config[$option] = $value; } } } if (!empty($config)) { $http_config = array_merge($http_config, $config); } // load HTTP_Request2 (support both composer-installed and system-installed package) if (!class_exists('HTTP_Request2')) { require_once 'HTTP/Request2.php'; } try { $request = new HTTP_Request2(); $request->setConfig($http_config); // proxy User-Agent string $request->setHeader('user-agent', $_SERVER['HTTP_USER_AGENT']); // cleanup $request->setBody(''); $request->setUrl($url); $request->setMethod($method); return $request; } catch (Exception $e) { rcube::raise_error($e, true, true); } } } diff --git a/plugins/libkolab/lib/kolab_storage_dav.php b/plugins/libkolab/lib/kolab_storage_dav.php index ea7e117c..fecff647 100644 --- a/plugins/libkolab/lib/kolab_storage_dav.php +++ b/plugins/libkolab/lib/kolab_storage_dav.php @@ -1,548 +1,547 @@ * * Copyright (C) 2022, Apheleia IT AG * * 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 . */ class kolab_storage_dav { const ERROR_DAV_CONN = 1; const ERROR_CACHE_DB = 2; const ERROR_NO_PERMISSION = 3; const ERROR_INVALID_FOLDER = 4; protected $dav; protected $url; /** * Object constructor */ public function __construct($url) { $this->url = $url; $this->setup(); } /** * Setup the environment */ public function setup() { $rcmail = rcube::get_instance(); $this->config = $rcmail->config; $this->dav = new kolab_dav_client($this->url); } /** * Get a list of storage folders for the given data type * * @param string Data type to list folders for (contact,distribution-list,event,task,note) * * @return array List of kolab_storage_dav_folder objects */ public function get_folders($type) { // TODO: This should be cached $folders = $this->dav->listFolders($this->get_dav_type($type)); if (is_array($folders)) { foreach ($folders as $idx => $folder) { // Exclude some special folders if (in_array('schedule-inbox', $folder['resource_type']) || in_array('schedule-outbox', $folder['resource_type'])) { unset($folders[$idx]); continue; } $folders[$idx] = new kolab_storage_dav_folder($this->dav, $folder, $type); } usort($folders, function ($a, $b) { return strcoll($a->get_foldername(), $b->get_foldername()); }); } return $folders ?: []; } /** * Getter for the storage folder for the given type * * @param string Data type to list folders for (contact,distribution-list,event,task,note) * * @return object kolab_storage_dav_folder The folder object */ public function get_default_folder($type) { // TODO: Not used } /** * Getter for a specific storage folder * * @param string $id Folder to access * @param string $type Expected folder type * * @return ?object kolab_storage_folder The folder object */ public function get_folder($id, $type) { foreach ($this->get_folders($type) as $folder) { if ($folder->id == $id) { return $folder; } } } /** * Getter for a single Kolab object, identified by its UID. * This will search all folders storing objects of the given type. * * @param string Object UID * @param string Object type (contact,event,task,journal,file,note,configuration) * * @return array The Kolab object represented as hash array or false if not found */ public function get_object($uid, $type) { // TODO return false; } /** * Execute cross-folder searches with the given query. * * @param array Pseudo-SQL query as list of filter parameter triplets * @param string Folder type (contact,event,task,journal,file,note,configuration) * @param int Expected number of records or limit (for performance reasons) * * @return array List of Kolab data objects (each represented as hash array) */ public function select($query, $type, $limit = null) { $result = []; foreach ($this->get_folders($type) as $folder) { if ($limit) { $folder->set_order_and_limit(null, $limit); } foreach ($folder->select($query) as $object) { $result[] = $object; } } return $result; } /** * Compose an URL to query the free/busy status for the given user * * @param string Email address of the user to get free/busy data for * @param object DateTime Start of the query range (optional) * @param object DateTime End of the query range (optional) * * @return string Fully qualified URL to query free/busy data */ public static function get_freebusy_url($email, $start = null, $end = null) { return kolab_storage::get_freebusy_url($email, $start, $end); } /** * Deletes a folder * * @param string $id Folder ID * @param string $type Folder type (contact,event,task,journal,file,note,configuration) * * @return bool True on success, false on failure */ public function folder_delete($id, $type) { if ($folder = $this->get_folder($id, $type)) { return $this->dav->folderDelete($folder->href); } return false; } /** * Creates a folder * * @param string $name Folder name (UTF7-IMAP) * @param string $type Folder type * @param bool $subscribed Sets folder subscription * @param bool $active Sets folder state (client-side subscription) * * @return bool True on success, false on failure */ public function folder_create($name, $type = null, $subscribed = false, $active = false) { // TODO } /** * Renames DAV folder * * @param string $oldname Old folder name (UTF7-IMAP) * @param string $newname New folder name (UTF7-IMAP) * * @return bool True on success, false on failure */ public function folder_rename($oldname, $newname) { // TODO ?? } /** * Update or Create a new folder. * * Does additional checks for permissions and folder name restrictions * * @param array &$prop Hash array with folder properties and metadata * - name: Folder name * - oldname: Old folder name when changed * - parent: Parent folder to create the new one in * - type: Folder type to create * - subscribed: Subscribed flag (IMAP subscription) * - active: Activation flag (client-side subscription) * * @return string|false New folder ID or False on failure */ public function folder_update(&$prop) { // TODO: Folder hierarchies, active and subscribed state // sanity checks if (!isset($prop['name']) || !is_string($prop['name']) || !strlen($prop['name'])) { self::$last_error = 'cannotbeempty'; return false; } else if (strlen($prop['name']) > 256) { self::$last_error = 'nametoolong'; return false; } if (!empty($prop['id'])) { if ($folder = $this->get_folder($prop['id'], $prop['type'])) { $result = $this->dav->folderUpdate($folder->href, $folder->get_dav_type(), $prop); } else { $result = false; } } else { $rcube = rcube::get_instance(); $uid = rtrim(chunk_split(md5($prop['name'] . $rcube->get_user_name() . uniqid('-', true)), 12, '-'), '-'); $type = $this->get_dav_type($prop['type']); $home = $this->dav->discover($type); if ($home === false) { return false; } $location = unslashify($home) . '/' . $uid; $result = $this->dav->folderCreate($location, $type, $prop); if ($result !== false) { $result = md5($this->dav->url . '/' . $location); } } return $result; } /** * Getter for human-readable name of a folder * * @param string $folder Folder name (UTF7-IMAP) * @param string $folder_ns Will be set to namespace name of the folder * * @return string Name of the folder-object */ public static function object_name($folder, &$folder_ns = null) { // TODO: Shared folders $folder_ns = 'personal'; return $folder; } /** * Creates a SELECT field with folders list * * @param string $type Folder type * @param array $attrs SELECT field attributes (e.g. name) * @param string $current The name of current folder (to skip it) * * @return html_select SELECT object */ public function folder_selector($type, $attrs, $current = '') { // TODO } /** * Returns a list of folder names * * @param string Optional root folder * @param string Optional name pattern * @param string Data type to list folders for (contact,event,task,journal,file,note,mail,configuration) * @param bool Enable to return subscribed folders only (null to use configured subscription mode) * @param array Will be filled with folder-types data * * @return array List of folders */ public function list_folders($root = '', $mbox = '*', $filter = null, $subscribed = null, &$folderdata = array()) { // TODO } /** * Search for shared or otherwise not listed groupware folders the user has access * * @param string Folder type of folders to search for * @param string Search string * @param array Namespace(s) to exclude results from * * @return array List of matching kolab_storage_folder objects */ public function search_folders($type, $query, $exclude_ns = []) { // TODO return []; } /** * Sort the given list of folders by namespace/name * * @param array List of kolab_storage_dav_folder objects * * @return array Sorted list of folders */ public static function sort_folders($folders) { // TODO return $folders; } /** * Returns folder types indexed by folder name * * @param string $prefix Folder prefix (Default '*' for all folders) * * @return array|bool List of folders, False on failure */ public function folders_typedata($prefix = '*') { // TODO: Used by kolab_folders, kolab_activesync, kolab_delegation return []; } /** * Returns type of a DAV folder * * @param string $folder Folder name (UTF7-IMAP) * * @return string Folder type */ public function folder_type($folder) { // TODO: Used by kolab_folders, kolab_activesync, kolab_delegation return ''; } /** * Sets folder content-type. * * @param string $folder Folder name * @param string $type Content type * * @return bool True on success, False otherwise */ public function set_folder_type($folder, $type = 'mail') { // NOP: Used by kolab_folders, kolab_activesync, kolab_delegation return false; } /** * Check subscription status of this folder * * @param string $folder Folder name * @param bool $temp Include temporary/session subscriptions * * @return bool True if subscribed, false if not */ public function folder_is_subscribed($folder, $temp = false) { // NOP return true; } /** * Change subscription status of this folder * * @param string $folder Folder name * @param bool $temp Only subscribe temporarily for the current session * * @return True on success, false on error */ public function folder_subscribe($folder, $temp = false) { // NOP return true; } /** * Change subscription status of this folder * * @param string $folder Folder name * @param bool $temp Only remove temporary subscription * * @return True on success, false on error */ public function folder_unsubscribe($folder, $temp = false) { // NOP return false; } /** * Check activation status of this folder * * @param string $folder Folder name * * @return bool True if active, false if not */ public function folder_is_active($folder) { - // TODO - return true; + return true; // TODO } /** * Change activation status of this folder * * @param string $folder Folder name * * @return True on success, false on error */ public function folder_activate($folder) { - return true; + return true; // TODO } /** * Change activation status of this folder * * @param string $folder Folder name * * @return True on success, false on error */ public function folder_deactivate($folder) { - return false; + return false; // TODO } /** * Creates default folder of specified type * To be run when none of subscribed folders (of specified type) is found * * @param string $type Folder type * @param string $props Folder properties (color, etc) * * @return string Folder name */ public function create_default_folder($type, $props = []) { // TODO: For kolab_addressbook?? return ''; } /** * Returns a list of IMAP folders shared by the given user * * @param array User entry from LDAP * @param string Data type to list folders for (contact,event,task,journal,file,note,mail,configuration) * @param int 1 - subscribed folders only, 0 - all folders, 2 - all non-active * @param array Will be filled with folder-types data * * @return array List of folders */ public function list_user_folders($user, $type, $subscribed = 0, &$folderdata = []) { // TODO return []; } /** * Get a list of (virtual) top-level folders from the other users namespace * * @param string Data type to list folders for (contact,event,task,journal,file,note,mail,configuration) * @param bool Enable to return subscribed folders only (null to use configured subscription mode) * * @return array List of kolab_storage_folder_user objects */ public function get_user_folders($type, $subscribed) { // TODO return []; } /** * Handler for user_delete plugin hooks * * Remove all cache data from the local database related to the given user. */ public static function delete_user_folders($args) { $db = rcmail::get_instance()->get_dbh(); $table = $db->table_name('kolab_folders', true); $prefix = 'dav://' . urlencode($args['username']) . '@' . $args['host'] . '/%'; $db->query("DELETE FROM $table WHERE `resource` LIKE ?", $prefix); } /** * Get folder METADATA for all supported keys * Do this in one go for better caching performance */ public function folder_metadata($folder) { // TODO ? return []; } /** * Get a folder DAV content type */ public static function get_dav_type($type) { $types = [ 'event' => 'VEVENT', 'task' => 'VTODO', 'contact' => 'VCARD', ]; return $types[$type]; } } diff --git a/plugins/libkolab/lib/kolab_storage_dav_folder.php b/plugins/libkolab/lib/kolab_storage_dav_folder.php index 258b6d6e..a588b95b 100644 --- a/plugins/libkolab/lib/kolab_storage_dav_folder.php +++ b/plugins/libkolab/lib/kolab_storage_dav_folder.php @@ -1,590 +1,587 @@ * * Copyright (C) 2014-2022, Apheleia IT AG * * 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 . */ class kolab_storage_dav_folder extends kolab_storage_folder { public $dav; public $href; public $attributes; /** * Object constructor */ public function __construct($dav, $attributes, $type_annotation = '') { $this->attributes = $attributes; $this->href = $this->attributes['href']; $this->id = md5($this->dav->url . '/' . $this->href); $this->dav = $dav; $this->valid = true; list($this->type, $suffix) = explode('.', $type_annotation); $this->default = $suffix == 'default'; $this->subtype = $this->default ? '' : $suffix; // Init cache $this->cache = kolab_storage_dav_cache::factory($this); } /** * Returns the owner of the folder. * * @param bool Return a fully qualified owner name (i.e. including domain for shared folders) * * @return string The owner of this folder. */ public function get_owner($fully_qualified = false) { // return cached value if (isset($this->owner)) { return $this->owner; } $rcube = rcube::get_instance(); $this->owner = $rcube->get_user_name(); $this->valid = true; // TODO: Support shared folders return $this->owner; } /** * Get a folder Etag identifier */ public function get_ctag() { return $this->attributes['ctag']; } /** * Getter for the name of the namespace to which the folder belongs * * @return string Name of the namespace (personal, other, shared) */ public function get_namespace() { // TODO: Support shared folders return 'personal'; } /** * Get the display name value of this folder * * @return string Folder name */ public function get_name() { return kolab_storage_dav::object_name($this->attributes['name']); } /** * Getter for the top-end folder name (not the entire path) * * @return string Name of this folder */ public function get_foldername() { return $this->attributes['name']; } public function get_folder_info() { return []; // todo ? } /** * Getter for parent folder path * * @return string Full path to parent folder */ public function get_parent() { // TODO return ''; } /** * Compose a unique resource URI for this folder */ public function get_resource_uri() { if (!empty($this->resource_uri)) { return $this->resource_uri; } // compose fully qualified resource uri for this instance $host = preg_replace('|^https?://|', 'dav://' . urlencode($this->get_owner(true)) . '@', $this->dav->url); $path = $this->href[0] == '/' ? $this->href : "/{$this->href}"; $host_path = parse_url($host, PHP_URL_PATH); if ($host_path && strpos($path, $host_path) === 0) { $path = substr($path, strlen($host_path)); } $this->resource_uri = unslashify($host) . $path; return $this->resource_uri; } /** * Getter for the Cyrus mailbox identifier corresponding to this folder * (e.g. user/john.doe/Calendar/Personal@example.org) * * @return string Mailbox ID */ public function get_mailbox_id() { // TODO: This is used with Bonnie related features return ''; } /** * Get the color value stored in metadata * * @param string Default color value to return if not set * * @return mixed Color value from the folder metadata or $default if not set */ public function get_color($default = null) { return !empty($this->attributes['color']) ? $this->attributes['color'] : $default; } /** * Get ACL information for this folder * * @return string Permissions as string */ public function get_myrights() { // TODO return ''; } /** * Helper method to extract folder UID * * @return string Folder's UID */ public function get_uid() { // TODO ??? return ''; } /** * Check activation status of this folder * * @return bool True if enabled, false if not */ public function is_active() { - // TODO - return true; + return true; // Unused } /** * Change activation status of this folder * * @param bool The desired subscription status: true = active, false = not active * * @return bool True on success, false on error */ public function activate($active) { - // TODO - return true; + return true; // Unused } /** * Check subscription status of this folder * * @return bool True if subscribed, false if not */ public function is_subscribed() { - return !isset($this->attributes['subscribed']) || $this->attributes['subscribed']; + return true; // TODO } /** * Change subscription status of this folder * * @param bool The desired subscription status: true = subscribed, false = not subscribed * * @return True on success, false on error */ public function subscribe($subscribed) { - // TODO - return true; + return true; // TODO } /** * Delete the specified object from this folder. * * @param array|string $object The Kolab object to delete or object UID * @param bool $expunge Should the folder be expunged? * * @return bool True if successful, false on error */ public function delete($object, $expunge = true) { if (!$this->valid) { return false; } $uid = is_array($object) ? $object['uid'] : $object; $success = $this->dav->delete($this->object_location($uid)); if ($success) { $this->cache->set($uid, false); } return $success; } /** * Delete all objects in a folder. * * Note: This method is used by kolab_addressbook plugin only * * @return bool True if successful, false on error */ public function delete_all() { if (!$this->valid) { return false; } // TODO: Maybe just deleting and re-creating a folder would be // better, but probably might not always work (ACL) $this->cache->synchronize(); foreach (array_keys($this->cache->folder_index()) as $uid) { $this->dav->delete($this->object_location($uid)); } $this->cache->purge(); return true; } /** * Restore a previously deleted object * * @param string $uid Object UID * * @return mixed Message UID on success, false on error */ public function undelete($uid) { if (!$this->valid) { return false; } // TODO return false; } /** * Move a Kolab object message to another IMAP folder * * @param string Object UID * @param string IMAP folder to move object to * * @return bool True on success, false on failure */ public function move($uid, $target_folder) { if (!$this->valid) { return false; } // TODO return false; } /** * Save an object in this folder. * * @param array $object The array that holds the data of the object. * @param string $type The type of the kolab object. * @param string $uid The UID of the old object if it existed before * * @return mixed False on error or object UID on success */ public function save(&$object, $type = null, $uid = null) { if (!$this->valid || empty($object)) { return false; } if (!$type) { $type = $this->type; } /* // copy attachments from old message $copyfrom = $object['_copyfrom'] ?: $object['_msguid']; if (!empty($copyfrom) && ($old = $this->cache->get($copyfrom, $type, $object['_mailbox']))) { foreach ((array)$old['_attachments'] as $key => $att) { if (!isset($object['_attachments'][$key])) { $object['_attachments'][$key] = $old['_attachments'][$key]; } // unset deleted attachment entries if ($object['_attachments'][$key] == false) { unset($object['_attachments'][$key]); } // load photo.attachment from old Kolab2 format to be directly embedded in xcard block else if ($type == 'contact' && ($key == 'photo.attachment' || $key == 'kolab-picture.png') && $att['id']) { if (!isset($object['photo'])) $object['photo'] = $this->get_attachment($copyfrom, $att['id'], $object['_mailbox']); unset($object['_attachments'][$key]); } } } // process attachments if (is_array($object['_attachments'])) { $numatt = count($object['_attachments']); foreach ($object['_attachments'] as $key => $attachment) { // FIXME: kolab_storage and Roundcube attachment hooks use different fields! if (empty($attachment['content']) && !empty($attachment['data'])) { $attachment['content'] = $attachment['data']; unset($attachment['data'], $object['_attachments'][$key]['data']); } // make sure size is set, so object saved in cache contains this info if (!isset($attachment['size'])) { if (!empty($attachment['content'])) { if (is_resource($attachment['content'])) { // this need to be a seekable resource, otherwise // fstat() failes and we're unable to determine size // here nor in rcube_imap_generic before IMAP APPEND $stat = fstat($attachment['content']); $attachment['size'] = $stat ? $stat['size'] : 0; } else { $attachment['size'] = strlen($attachment['content']); } } else if (!empty($attachment['path'])) { $attachment['size'] = filesize($attachment['path']); } $object['_attachments'][$key] = $attachment; } // generate unique keys (used as content-id) for attachments if (is_numeric($key) && $key < $numatt) { // derrive content-id from attachment file name $ext = preg_match('/(\.[a-z0-9]{1,6})$/i', $attachment['name'], $m) ? $m[1] : null; $basename = preg_replace('/[^a-z0-9_.-]/i', '', basename($attachment['name'], $ext)); // to 7bit ascii if (!$basename) $basename = 'noname'; $cid = $basename . '.' . microtime(true) . $key . $ext; $object['_attachments'][$cid] = $attachment; unset($object['_attachments'][$key]); } } } */ $rcmail = rcube::get_instance(); $result = false; // generate and save object message if ($content = $this->to_dav($object)) { $method = $uid ? 'update' : 'create'; $dav_type = $this->get_dav_type(); $result = $this->dav->{$method}($this->object_location($object['uid']), $content, $dav_type); // Note: $result can be NULL if the request was successful, but ETag wasn't returned if ($result !== false) { // insert/update object in the cache $object['etag'] = $result; $this->cache->save($object, $uid); $result = true; } } return $result; } /** * Fetch the object the DAV server and convert to internal format * * @param string The object UID to fetch * @param string The object type expected (use wildcard '*' to accept all types) * @param string Unused (kept for compat. with the parent class) * * @return mixed Hash array representing the Kolab object, a kolab_format instance or false if not found */ public function read_object($uid, $type = null, $folder = null) { if (!$this->valid) { return false; } $href = $this->object_location($uid); $objects = $this->dav->getData($this->href, $this->get_dav_type(), [$href]); if (!is_array($objects) || count($objects) != 1) { rcube::raise_error([ 'code' => 900, 'message' => "Failed to fetch {$href}" ], true); return false; } return $this->from_dav($objects[0]); } /** * Convert DAV object into PHP array * * @param array Object data in kolab_dav_client::fetchData() format * * @return array Object properties */ public function from_dav($object) { if ($this->type == 'event') { $ical = libcalendaring::get_ical(); $events = $ical->import($object['data']); if (!count($events) || empty($events[0]['uid'])) { return false; } $result = $events[0]; } else if ($this->type == 'contact') { if (stripos($object['data'], 'BEGIN:VCARD') !== 0) { return false; } $vcard = new rcube_vcard($object['data'], RCUBE_CHARSET, false); if (!empty($vcard->displayname) || !empty($vcard->surname) || !empty($vcard->firstname) || !empty($vcard->email)) { $result = $vcard->get_assoc(); } else { return false; } } $result['etag'] = $object['etag']; $result['href'] = $object['href']; $result['uid'] = $object['uid'] ?: $result['uid']; return $result; } /** * Convert Kolab object into DAV format (iCalendar) */ public function to_dav($object) { $result = ''; if ($this->type == 'event') { $ical = libcalendaring::get_ical(); if (!empty($object['exceptions'])) { $object['recurrence']['EXCEPTIONS'] = $object['exceptions']; } $result = $ical->export([$object]); } else if ($this->type == 'contact') { // copy values into vcard object $vcard = new rcube_vcard('', RCUBE_CHARSET, false, ['uid' => 'UID']); $vcard->set('groups', null); foreach ($object as $key => $values) { list($field, $section) = rcube_utils::explode(':', $key); // avoid casting DateTime objects to array if (is_object($values) && is_a($values, 'DateTime')) { $values = [$values]; } foreach ((array) $values as $value) { if (isset($value)) { $vcard->set($field, $value, $section); } } } $result = $vcard->export(false); } if ($result) { // The content must be UTF-8, otherwise if we try to fetch the object // from server XML parsing would fail. $result = rcube_charset::clean($result); } return $result; } protected function object_location($uid) { return unslashify($this->href) . '/' . urlencode($uid) . '.' . $this->get_dav_ext(); } /** * Get a folder DAV content type */ public function get_dav_type() { return kolab_storage_dav::get_dav_type($this->type); } /** * Get a DAV file extension for specified Kolab type */ public function get_dav_ext() { $types = [ 'event' => 'ics', 'task' => 'ics', 'contact' => 'vcf', ]; return $types[$this->type]; } /** * Return folder name as string representation of this object * * @return string Full IMAP folder name */ public function __toString() { return $this->attributes['name']; } }