From 92befb78fac3bc173afa31225f1e065ea8612e58 Mon Sep 17 00:00:00 2001 From: Jan Schneider Date: Fri, 27 Feb 2009 00:05:51 +0100 Subject: [PATCH] Remove global $kronolith_driver object. We still need a singleton though. Move almost all configuration, globals, etc. out of drivers. Work further towards Ical/Holidays being standalone, independent drivers. --- kronolith/add.php | 5 +- kronolith/attend.php | 5 +- kronolith/data.php | 1 + kronolith/delete.php | 4 +- kronolith/edit.php | 1 + kronolith/ics.php | 4 +- kronolith/lib/Driver.php | 11 +- kronolith/lib/Driver/Holidays.php | 31 ++--- kronolith/lib/Driver/Ical.php | 8 +- kronolith/lib/Driver/Sql.php | 6 +- kronolith/lib/Event.php | 18 ++- kronolith/lib/Forms/DeleteCalendar.php | 2 +- kronolith/lib/Forms/EditCalendar.php | 2 +- kronolith/lib/Imple/TagActions.php | 7 +- kronolith/lib/Kronolith.php | 153 +++++++++++++----------- kronolith/lib/Maintenance/Task/purge_events.php | 3 +- kronolith/lib/api.php | 56 ++++----- kronolith/lib/base.php | 3 - kronolith/lib/tests/bug6031.phpt | 13 +- kronolith/new.php | 2 +- kronolith/scripts/agenda.php | 7 +- kronolith/search.php | 4 +- 22 files changed, 158 insertions(+), 188 deletions(-) diff --git a/kronolith/add.php b/kronolith/add.php index 0a84ba5b5..0030a8afe 100644 --- a/kronolith/add.php +++ b/kronolith/add.php @@ -28,8 +28,7 @@ if (!Util::getFormData('cancel')) { $notification->push(sprintf(_("You do not have permission to add events to %s."), $share->get('name')), 'horde.warning'); } elseif (Kronolith::hasPermission('max_events') === true || Kronolith::hasPermission('max_events') > Kronolith::countEvents()) { - $kronolith_driver->open($calendar_id); - $event = &$kronolith_driver->getEvent(); + $event = Kronolith::getDriver(null, $calendar_id)->getEvent(); $event->readForm(); $result = $event->save(); if (is_a($result, 'PEAR_Error')) { @@ -42,7 +41,7 @@ if (!Util::getFormData('cancel')) { $notification->push(sprintf(_("There was an error adding the event: %s"), $message), 'horde.error'); } else { if (Util::getFormData('sendupdates', false)) { - $event = &$kronolith_driver->getEvent($result); + $event = Kronolith::getDriver()->getEvent($result); if (is_a($event, 'PEAR_Error')) { $notification->push($event, 'horde.error'); } else { diff --git a/kronolith/attend.php b/kronolith/attend.php index d8b272ec9..79b4bfb9f 100644 --- a/kronolith/attend.php +++ b/kronolith/attend.php @@ -45,10 +45,9 @@ if (((empty($cal) || empty($id)) && empty($uid)) || empty($user)) { $title = ''; } else { if (empty($uid)) { - $kronolith_driver->open($cal); - $event = $kronolith_driver->getEvent($id); + $event = Kronolith::getDriver(null, $cal)->getEvent($id); } else { - $event = $kronolith_driver->getByUID($uid); + $event = Kronolith::getDriver()->getByUID($uid); } if (is_a($event, 'PEAR_Error')) { $notification->push($event, 'horde.error'); diff --git a/kronolith/data.php b/kronolith/data.php index 789080d11..8f3d257db 100644 --- a/kronolith/data.php +++ b/kronolith/data.php @@ -73,6 +73,7 @@ $param = array('time_fields' => $time_fields, 'file_types' => $file_types); $import_format = Util::getFormData('import_format', ''); $error = false; +$kronolith_driver = Kronolith::getDriver(); /* Loop through the action handlers. */ switch ($actionID) { diff --git a/kronolith/delete.php b/kronolith/delete.php index 90dec9969..140475113 100644 --- a/kronolith/delete.php +++ b/kronolith/delete.php @@ -12,9 +12,9 @@ @define('KRONOLITH_BASE', dirname(__FILE__)); require_once KRONOLITH_BASE . '/lib/base.php'; -$kronolith_driver->open(Util::getFormData('calendar')); +$kronolith_driver = Kronolith::getDriver(null, Util::getFormData('calendar')); if ($eventID = Util::getFormData('eventID')) { - $event = &$kronolith_driver->getEvent($eventID); + $event = $kronolith_driver->getEvent($eventID); if (is_a($event, 'PEAR_Error')) { if (($url = Util::getFormData('url')) === null) { $url = Horde::applicationUrl($prefs->getValue('defaultview') . '.php', true); diff --git a/kronolith/edit.php b/kronolith/edit.php index ebf157689..6868f035e 100644 --- a/kronolith/edit.php +++ b/kronolith/edit.php @@ -39,6 +39,7 @@ function _check_max() require_once KRONOLITH_BASE . '/lib/base.php'; $url = Util::getFormData('url'); +$kronolith_driver = Kronolith::getDriver(); if ($exception = Util::getFormData('del_exception')) { $calendar = Util::getFormData('calendar'); diff --git a/kronolith/ics.php b/kronolith/ics.php index 6ccdfa7c5..6236e5b20 100644 --- a/kronolith/ics.php +++ b/kronolith/ics.php @@ -56,14 +56,14 @@ $key = 'kronolith.ics.' . $calendar; $ics = $cache->get($key, 360); if (!$ics) { - $kronolith_driver->open($calendar); + $kronolith_driver = Kronolith::getDriver(null, $calendar); $events = $kronolith_driver->listEvents(); $iCal = new Horde_iCalendar(); $iCal->setAttribute('X-WR-CALNAME', String::convertCharset($share->get('name'), NLS::getCharset(), 'utf-8')); foreach ($events as $id) { - $event = &$kronolith_driver->getEvent($id); + $event = $kronolith_driver->getEvent($id); if (is_a($event, 'PEAR_Error')) { continue; } diff --git a/kronolith/lib/Driver.php b/kronolith/lib/Driver.php index c0340d2d7..ef561fa09 100644 --- a/kronolith/lib/Driver.php +++ b/kronolith/lib/Driver.php @@ -193,18 +193,9 @@ class Kronolith_Driver */ public function factory($driver = null, $params = null) { - if ($driver === null) { - $driver = $GLOBALS['conf']['calendar']['driver']; - } $driver = basename($driver); - - if ($params === null) { - $params = Horde::getDriverConfig('calendar', $driver); - } - - $driver = String::ucfirst($driver); - $class = 'Kronolith_Driver_' . $driver; + if (class_exists($class)) { $driver = new $class($params); $result = $driver->initialize(); diff --git a/kronolith/lib/Driver/Holidays.php b/kronolith/lib/Driver/Holidays.php index 32a0faee7..6c7769dfc 100644 --- a/kronolith/lib/Driver/Holidays.php +++ b/kronolith/lib/Driver/Holidays.php @@ -43,31 +43,25 @@ class Kronolith_Driver_Holidays extends Kronolith_Driver return array(); } - global $language; - - $events = array(); - if (is_null($startDate)) { $startDate = new Horde_Date($_SERVER['REQUEST_TIME']); } - if (is_null($endDate)) { $endDate = new Horde_Date($_SERVER['REQUEST_TIME']); } - Date_Holidays::staticSetProperty('DIE_ON_MISSING_LOCALE', false); - foreach (unserialize($GLOBALS['prefs']->getValue('holiday_drivers')) as $driver) { - for ($year = $startDate->year; $year <= $endDate->year; $year++) { - $dh = Date_Holidays::factory($driver, $year, $language); - if (Date_Holidays::isError($dh)) { - Horde::logMessage(sprintf('Factory was unable to produce driver object for driver %s in year %s with locale %s', - $driver, $year, $language), - __FILE__, __LINE__, PEAR_LOG_ERR); - continue; - } - $dh->addTranslation($language); - $events = array_merge($events, $this->_getEvents($dh, $startDate, $endDate)); + + $events = array(); + for ($year = $startDate->year; $year <= $endDate->year; $year++) { + $dh = Date_Holidays::factory($this->_calendar, $year, $this->_params['language']); + if (Date_Holidays::isError($dh)) { + Horde::logMessage(sprintf('Factory was unable to produce driver object for driver %s in year %s with locale %s', + $this->_calendar, $year, $this->_params['language']), + __FILE__, __LINE__, PEAR_LOG_ERR); + continue; } + $dh->addTranslation($this->_params['language']); + $events = array_merge($events, $this->_getEvents($dh, $startDate, $endDate)); } return $events; @@ -103,7 +97,6 @@ class Kronolith_Driver_Holidays extends Kronolith_Driver { static $data_dir; if (!isset($data_dir)) { - include_once 'PEAR/Config.php'; $pear_config = new PEAR_Config(); $data_dir = $pear_config->get('data_dir'); } @@ -114,7 +107,7 @@ class Kronolith_Driver_Holidays extends Kronolith_Driver foreach (array('', '_' . $driver) as $pkg_ext) { foreach (array('ser', 'xml') as $format) { $location = $data_dir . '/Date_Holidays' . $pkg_ext . '/lang/' - . $driver . '/' . $GLOBALS['language'] . '.' . $format; + . $driver . '/' . $this->_params['language'] . '.' . $format; if (file_exists($location)) { return array($format, $location); } diff --git a/kronolith/lib/Driver/Ical.php b/kronolith/lib/Driver/Ical.php index 7b5c5fdeb..96e1f49ea 100644 --- a/kronolith/lib/Driver/Ical.php +++ b/kronolith/lib/Driver/Ical.php @@ -14,6 +14,8 @@ * See the enclosed file COPYING for license information (GPL). If you * did not receive this file, see http://www.fsf.org/copyleft/gpl.html. * + * @todo Replace session cache + * * @author Chuck Hagenbuch * @author Jan Schneider * @package Kronolith @@ -67,7 +69,7 @@ class Kronolith_Driver_Ical extends Kronolith_Driver $event = new Kronolith_Event_Ical($this); $event->status = Kronolith::STATUS_FREE; $event->fromiCalendar($component); - $event->remoteCal = $this->_params['url']; + $event->remoteCal = $this->_calendar; $event->eventID = $i; /* Catch RECURRENCE-ID attributes which mark single recurrence @@ -129,7 +131,7 @@ class Kronolith_Driver_Ical extends Kronolith_Driver $event = new Kronolith_Event_Ical($this); $event->status = Kronolith::STATUS_FREE; $event->fromiCalendar($components[$eventId]); - $event->remoteCal = $this->_params['url']; + $event->remoteCal = $this->_calendar; $event->eventID = $eventId; return $event; @@ -145,7 +147,7 @@ class Kronolith_Driver_Ical extends Kronolith_Driver */ private function _getRemoteCalendar() { - $url = trim($this->_params['url']); + $url = trim($this->_calendar); /* Treat webcal:// URLs as http://. */ if (substr($url, 0, 9) == 'webcal://') { diff --git a/kronolith/lib/Driver/Sql.php b/kronolith/lib/Driver/Sql.php index ba3a56857..b55a779a7 100644 --- a/kronolith/lib/Driver/Sql.php +++ b/kronolith/lib/Driver/Sql.php @@ -40,8 +40,6 @@ class Kronolith_Driver_Sql extends Kronolith_Driver public function listAlarms($date, $fullevent = false) { - require_once 'Date/Calc.php'; - $allevents = $this->listEvents($date, null, true); if (is_a($allevents, 'PEAR_Error')) { return $allevents; @@ -102,8 +100,6 @@ class Kronolith_Driver_Sql extends Kronolith_Driver public function search($query) { - require_once 'Horde/SQL.php'; - /* Build SQL conditions based on the query string. */ $cond = '(('; $values = array(); @@ -861,6 +857,8 @@ class Kronolith_Driver_Sql extends Kronolith_Driver $this->deleteEvent($event->getId()); } } + return true; } + } diff --git a/kronolith/lib/Event.php b/kronolith/lib/Event.php index 5b8e1629d..a6e156d00 100644 --- a/kronolith/lib/Event.php +++ b/kronolith/lib/Event.php @@ -209,7 +209,7 @@ class Kronolith_Event * @param mixed $eventObject Backend specific event object * that this will represent. */ - public function __construct(&$driver, $eventObject = null) + public function __construct($driver, $eventObject = null) { static $alarm; @@ -220,7 +220,9 @@ class Kronolith_Event $this->alarm = $alarm; $this->_calendar = $driver->getCalendar(); - if (!empty($this->_calendar)) { + // FIXME: Move color definitions anywhere else. + if (!empty($this->_calendar) && + isset($GLOBALS['all_calendars'][$this->_calendar])) { $share = $GLOBALS['all_calendars'][$this->_calendar]; $this->_backgroundColor = $share->get('color'); if (empty($this->_backgroundColor)) { @@ -244,12 +246,7 @@ class Kronolith_Event */ public function getDriver() { - global $kronolith_driver; - if ($kronolith_driver->getCalendar() != $this->_calendar) { - $kronolith_driver->open($this->_calendar); - } - - return $kronolith_driver; + return Kronolith::getDriver(null, $this->_calendar); } /** @@ -309,8 +306,7 @@ class Kronolith_Event } $this->toDriver(); - $driver = &$this->getDriver(); - $result = $driver->saveEvent($this); + $result = $this->getDriver()->saveEvent($this); if (!is_a($result, 'PEAR_Error') && !empty($GLOBALS['conf']['alarms']['driver'])) { $alarm = $this->toAlarm(new Horde_Date($_SERVER['REQUEST_TIME'])); @@ -1039,7 +1035,7 @@ class Kronolith_Event return false; } - $eventID = $GLOBALS['kronolith_driver']->exists($this->_uid, $this->_calendar); + $eventID = $this->getDriver()->exists($this->_uid, $this->_calendar); if (is_a($eventID, 'PEAR_Error') || !$eventID) { return false; } else { diff --git a/kronolith/lib/Forms/DeleteCalendar.php b/kronolith/lib/Forms/DeleteCalendar.php index 2235fb00c..31cd5f54d 100644 --- a/kronolith/lib/Forms/DeleteCalendar.php +++ b/kronolith/lib/Forms/DeleteCalendar.php @@ -54,7 +54,7 @@ class Kronolith_DeleteCalendarForm extends Horde_Form { } // Delete the calendar. - $result = $GLOBALS['kronolith_driver']->delete($this->_calendar->getName()); + $result = Kronolith::getDriver()->delete($this->_calendar->getName()); if (is_a($result, 'PEAR_Error')) { return PEAR::raiseError(sprintf(_("Unable to delete \"%s\": %s"), $this->_calendar->get('name'), $result->getMessage())); } else { diff --git a/kronolith/lib/Forms/EditCalendar.php b/kronolith/lib/Forms/EditCalendar.php index 8b5951767..1698c25ae 100644 --- a/kronolith/lib/Forms/EditCalendar.php +++ b/kronolith/lib/Forms/EditCalendar.php @@ -52,7 +52,7 @@ class Kronolith_EditCalendarForm extends Horde_Form { $this->_calendar->set('color', $this->_vars->get('color')); $this->_calendar->set('desc', $this->_vars->get('description')); if ($original_name != $new_name) { - $result = $GLOBALS['kronolith_driver']->rename($original_name, $new_name); + $result = Kronolith::getDriver()->rename($original_name, $new_name); if (is_a($result, 'PEAR_Error')) { return PEAR::raiseError(sprintf(_("Unable to rename \"%s\": %s"), $original_name, $result->getMessage())); } diff --git a/kronolith/lib/Imple/TagActions.php b/kronolith/lib/Imple/TagActions.php index c6732d1c3..ee2273961 100644 --- a/kronolith/lib/Imple/TagActions.php +++ b/kronolith/lib/Imple/TagActions.php @@ -55,7 +55,7 @@ class Kronolith_Imple_TagActions extends Kronolith_Imple $cal = $GLOBALS['kronolith_shares']->getShare($args['resource']); $perm = $cal->hasPermission(Auth::getAuth(), PERMS_EDIT); } elseif($args['type'] == 'event') { - $event = $GLOBALS['kronolith_driver']->getByUID($args['resource']); + $event = Kronolith::getDriver()->getByUID($args['resource']); $perm = $event->hasPermission(PERMS_EDIT, Auth::getAuth()); } @@ -96,10 +96,7 @@ class Kronolith_Imple_TagActions extends Kronolith_Imple $cal = $GLOBALS['kronolith_shares']->getShare($id); $hasEdit = $cal->hasPermission(Auth::getAuth(), PERMS_EDIT); } elseif ($type == 'event') { - if ($kronolith_driver->getCalendar() != $cal) { - $kronolith_driver->open($cal); - } - $event = $GLOBALS['kronolith_driver']->getByUID($id); + $event = Kronolith::getDriver()->getByUID($id); $hasEdit = $event->hasPermission(PERMS_EDIT, Auth::getAuth()); } diff --git a/kronolith/lib/Kronolith.php b/kronolith/lib/Kronolith.php index 9dd21e502..f64b85a17 100644 --- a/kronolith/lib/Kronolith.php +++ b/kronolith/lib/Kronolith.php @@ -487,9 +487,9 @@ class Kronolith * @return array The events happening in this time period. */ public static function listEventIds($startDate = null, $endDate = null, - $calendars = null, $alarmsOnly = false) + $calendars = null, $alarmsOnly = false) { - global $kronolith_driver; + $kronolith_driver = self::getDriver(); if (!empty($startDate)) { $startDate = new Horde_Date($startDate); @@ -506,11 +506,10 @@ class Kronolith $eventIds = array(); foreach ($calendars as $cal) { - if ($kronolith_driver->getCalendar() != $cal) { - $kronolith_driver->open($cal); - } - $eventIds[$cal] = $GLOBALS['kronolith_driver']->listEvents( - $startDate, $endDate, $alarmsOnly); + $kronolith_driver->open($cal); + $eventIds[$cal] = $kronolith_driver->listEvents($startDate, + $endDate, + $alarmsOnly); } return $eventIds; @@ -528,13 +527,11 @@ class Kronolith */ public static function listAlarms($date, $calendars, $fullevent = false) { - global $kronolith_driver; + $kronolith_driver = self::getDriver(); $alarms = array(); foreach ($calendars as $cal) { - if ($kronolith_driver->getCalendar() != $cal) { - $kronolith_driver->open($cal); - } + $kronolith_driver->open($cal); $alarms[$cal] = $kronolith_driver->listAlarms($date, $fullevent); if (is_a($alarms[$cal], 'PEAR_Error')) { return $alarms[$cal]; @@ -553,7 +550,7 @@ class Kronolith */ public static function search($query) { - global $kronolith_driver; + $kronolith_driver = self::getDriver(); if (!isset($query->calendars)) { $calendars = $GLOBALS['display_calendars']; @@ -563,9 +560,7 @@ class Kronolith $events = array(); foreach ($calendars as $cal) { - if ($kronolith_driver->getCalendar() != $cal) { - $kronolith_driver->open($cal); - } + $kronolith_driver->open($cal); $retevents = $kronolith_driver->search($query); foreach ($retevents as $event) { $events[] = $event; @@ -829,7 +824,7 @@ class Kronolith $showRecurrence = true, $alarmsOnly = false, $showRemote = true) { - global $kronolith_driver, $registry; + global $registry; if (!empty($startDate)) { $startDate = new Horde_Date($startDate); @@ -841,6 +836,7 @@ class Kronolith $calendars = $GLOBALS['display_calendars']; } + $kronolith_driver = self::getDriver(); $eventIds = Kronolith::listEventIds($startDate, $endDate, $calendars, $alarmsOnly); $startOfPeriod = clone $startDate; @@ -855,11 +851,9 @@ class Kronolith return $events; } - if ($kronolith_driver->getCalendar() != $cal) { - $kronolith_driver->open($cal); - } + $kronolith_driver->open($cal); foreach ($events as $id) { - $event = &$kronolith_driver->getEvent($id); + $event = $kronolith_driver->getEvent($id); if (is_a($event, 'PEAR_Error')) { return $event; } @@ -975,8 +969,9 @@ class Kronolith } /* Remote Calendars. */ + $driver = self::getDriver('Ical'); foreach ($GLOBALS['display_remote_calendars'] as $url) { - $driver = self::getDriver('Ical', array('url' => $url)); + $driver->open($url); $events = $driver->listEvents($startOfPeriod, $endOfPeriod); if (!is_a($events, 'PEAR_Error')) { $kronolith_driver->open(Kronolith::getDefaultCalendar(PERMS_SHOW)); @@ -991,14 +986,17 @@ class Kronolith /* Holidays */ if (!empty($GLOBALS['conf']['holidays']['enable'])) { - $dhDriver = Kronolith_Driver::factory('Holidays'); - $events = $dhDriver->listEvents($startDate, $endDate); - if (!is_a($events, 'PEAR_Error')) { - $kronolith_driver->open(Kronolith::getDefaultCalendar(PERMS_SHOW)); - foreach ($events as $event) { - Kronolith::_getEvents($results, $event, $startDate, - $endDate, $startOfPeriod, - $endOfPeriod, $showRecurrence); + $dhDriver = self::getDriver('Holidays'); + foreach (unserialize($GLOBALS['prefs']->getValue('holiday_drivers')) as $driver) { + $dhDriver->open($driver); + $events = $dhDriver->listEvents($startDate, $endDate); + if (!is_a($events, 'PEAR_Error')) { + $kronolith_driver->open(Kronolith::getDefaultCalendar(PERMS_SHOW)); + foreach ($events as $event) { + Kronolith::_getEvents($results, $event, $startDate, + $endDate, $startOfPeriod, + $endOfPeriod, $showRecurrence); + } } } } @@ -1022,8 +1020,6 @@ class Kronolith $startOfPeriod, $endOfPeriod, $showRecurrence) { - global $kronolith_driver; - if ($event->recurs() && $showRecurrence) { /* Recurring Event. */ @@ -1215,30 +1211,25 @@ class Kronolith */ public static function countEvents() { - global $kronolith_driver; - static $count; if (isset($count)) { return $count; } + $kronolith_driver = self::getDriver(); $calendars = Kronolith::listCalendars(true, PERMS_ALL); $current_calendar = $kronolith_driver->getCalendar(); $count = 0; foreach (array_keys($calendars) as $calendar) { - if ($kronolith_driver->getCalendar() != $calendar) { - $kronolith_driver->open($calendar); - } + $kronolith_driver->open($calendar); /* Retrieve the event list from storage. */ $count += count($kronolith_driver->listEvents()); } /* Reopen last calendar. */ - if ($kronolith_driver->getCalendar() != $current_calendar) { - $kronolith_driver->open($current_calendar); - } + $kronolith_driver->open($current_calendar); return $count; } @@ -2037,25 +2028,37 @@ class Kronolith } /** - * Attempts to return a concrete Kronolith_Driver instance based on - * $driver. + * Attempts to return a single, concrete Kronolith_Driver instance based + * on a driver name. * - * @param string $driver The type of concrete Kronolith_Driver subclass - * to return. + * This singleton method automatically retrieves all parameters required + * for the specified driver. * - * @param array $params A hash containing any additional configuration or - * connection parameters a subclass might need. + * @param string $driver The type of concrete Kronolith_Driver subclass + * to return. + * @param string $calendar The calendar name. The format depends on the + * driver being used. * * @return Kronolith_Driver The newly created concrete Kronolith_Driver * instance, or a PEAR_Error on error. */ - public static function getDriver($driver, $params) + public static function getDriver($driver = null, $calendar = null) { - ksort($params); - $sig = hash('md5', serialize(array($driver, $params))); + if (empty($driver)) { + $driver = String::ucfirst($GLOBALS['conf']['calendar']['driver']); + } - if (!isset(self::$_instances[$sig])) { + if (!isset(self::$_instances[$driver])) { + $params = array(); switch ($driver) { + case 'Sql': + $params = Horde::getDriverConfig('calendar', 'sql'); + break; + + case 'Kolab': + $params = Horde::getDriverConfig('calendar', 'kolab'); + break; + case 'Ical': /* Check for HTTP proxy configuration */ if (!empty($GLOBALS['conf']['http']['proxy']['proxy_host'])) { @@ -2065,7 +2068,7 @@ class Kronolith /* Check for HTTP authentication credentials */ $cals = unserialize($GLOBALS['prefs']->getValue('remote_cals')); foreach ($cals as $cal) { - if ($cal['url'] == $params['url']) { + if ($cal['url'] == $calendar) { $user = isset($cal['user']) ? $cal['user'] : ''; $password = isset($cal['password']) ? $cal['password'] : ''; $key = Auth::getCredential('password'); @@ -2081,12 +2084,22 @@ class Kronolith break; } } + + break; + + case 'Holidays': + $params['language'] = $GLOBALS['language']; + break; } - self::$_instances[$sig] = Kronolith_Driver::factory($driver, $params); + self::$_instances[$driver] = Kronolith_Driver::factory($driver, $params); + } + + if (!is_null($calendar)) { + self::$_instances[$driver]->open($calendar); } - return self::$_instances[$sig]; + return self::$_instances[$driver]; } /** @@ -2111,14 +2124,13 @@ class Kronolith require_once KRONOLITH_BASE . '/lib/Views/Event.php'; if (Util::getFormData('calendar') == '**remote') { - $driver = self::getDriver('Ical', array('url' => Util::getFormData('remoteCal'))); - $event = $driver->getEvent(Util::getFormData('eventID')); + $event = self::getDriver('Ical', Util::getFormData('remoteCal')) + ->getEvent(Util::getFormData('eventID')); } elseif ($uid = Util::getFormData('uid')) { - $event = $GLOBALS['kronolith_driver']->getByUID($uid); + $event = self::getDriver()->getByUID($uid); } else { - $GLOBALS['kronolith_driver']->open(Util::getFormData('calendar')); - $event = $GLOBALS['kronolith_driver']->getEvent( - Util::getFormData('eventID')); + $event = self::getDriver(null, Util::getFormData('calendar')) + ->getEvent(Util::getFormData('eventID')); } if (!is_a($event, 'PEAR_Error') && !$event->hasPermission(PERMS_READ)) { @@ -2131,12 +2143,11 @@ class Kronolith require_once KRONOLITH_BASE . '/lib/Views/EditEvent.php'; if (Util::getFormData('calendar') == '**remote') { - $driver = self::getDriver('Ical', array('url' => Util::getFormData('remoteCal'))); - $event = $driver->getEvent(Util::getFormData('eventID')); + $event = self::getDriver('Ical', Util::getFormData('remoteCal')) + ->getEvent(Util::getFormData('eventID')); } else { - $GLOBALS['kronolith_driver']->open(Util::getFormData('calendar')); - $event = $GLOBALS['kronolith_driver']->getEvent( - Util::getFormData('eventID')); + $event = self::getDriver(null, Util::getFormData('calendar')) + ->getEvent(Util::getFormData('eventID')); } if (!is_a($event, 'PEAR_Error') && !$event->hasPermission(PERMS_EDIT)) { @@ -2148,9 +2159,8 @@ class Kronolith case 'DeleteEvent': require_once KRONOLITH_BASE . '/lib/Views/DeleteEvent.php'; - $GLOBALS['kronolith_driver']->open(Util::getFormData('calendar')); - $event = $GLOBALS['kronolith_driver']->getEvent - (Util::getFormData('eventID')); + $event = self::getDriver(null, Util::getFormData('calendar')) + ->getEvent(Util::getFormData('eventID')); if (!is_a($event, 'PEAR_Error') && !$event->hasPermission(PERMS_DELETE)) { $event = PEAR::raiseError(_("Permission Denied")); @@ -2162,14 +2172,13 @@ class Kronolith require_once KRONOLITH_BASE . '/lib/Views/ExportEvent.php'; if (Util::getFormData('calendar') == '**remote') { - $driver = self::getDriver('Ical', array('url' => Util::getFormData('remoteCal'))); - $event = $driver->getEvent(Util::getFormData('eventID')); + $event = self::getDriver('Ical', Util::getFormData('remoteCal')) + ->getEvent(Util::getFormData('eventID')); } elseif ($uid = Util::getFormData('uid')) { - $event = $GLOBALS['kronolith_driver']->getByUID($uid); + $event = self::getDriver()->getByUID($uid); } else { - $GLOBALS['kronolith_driver']->open(Util::getFormData('calendar')); - $event = $GLOBALS['kronolith_driver']->getEvent( - Util::getFormData('eventID')); + $event = self::getDriver(null, Util::getFormData('calendar')) + ->getEvent(Util::getFormData('eventID')); } if (!is_a($event, 'PEAR_Error') && !$event->hasPermission(PERMS_READ)) { diff --git a/kronolith/lib/Maintenance/Task/purge_events.php b/kronolith/lib/Maintenance/Task/purge_events.php index 659f1deb6..c37fc4954 100644 --- a/kronolith/lib/Maintenance/Task/purge_events.php +++ b/kronolith/lib/Maintenance/Task/purge_events.php @@ -21,7 +21,7 @@ class Maintenance_Task_purge_events extends Maintenance_Task { */ function doMaintenance() { - global $prefs, $kronolith_driver, $notification; + global $prefs, $notification; /* Get the current time minus the number of days specified in * 'purge_events_keep'. An event will be deleted if it has an end @@ -33,6 +33,7 @@ class Maintenance_Task_purge_events extends Maintenance_Task { $calendars = Kronolith::listCalendars(false, PERMS_DELETE); /* Start building an event object to use for the search */ + $kronolith_driver = Kronolith::getDriver(); $query = &$kronolith_driver->getEvent(); $query->start = null; $query->end = $del_time; diff --git a/kronolith/lib/api.php b/kronolith/lib/api.php index 274156deb..7824df491 100644 --- a/kronolith/lib/api.php +++ b/kronolith/lib/api.php @@ -170,10 +170,9 @@ function _kronolith_removeUserData($user) } require_once dirname(__FILE__) . '/base.php'; - global $kronolith_driver; /* Remove all events owned by the user in all calendars. */ - $result = $kronolith_driver->removeUserData($user); + $result = Kronolith::getDriver()->removeUserData($user); /* Now delete history as well. */ $history = &Horde_History::singleton(); @@ -258,7 +257,7 @@ function __kronolith_modified($uid) function _kronolith_browse($path = '', $properties = array()) { require_once dirname(__FILE__) . '/base.php'; - global $registry, $kronolith_driver; + global $registry; // Default properties. if (!$properties) { @@ -382,7 +381,7 @@ function _kronolith_browse($path = '', $properties = array()) // This request is browsing into a specific calendar. Generate the list // of items and represent them as files within the directory. // - $kronolith_driver->open($parts[1]); + $kronolith_driver = Kronolith::getDriver(null, $parts[1]); $events = $kronolith_driver->listEvents(); if (is_a($events, 'PEAR_Error')) { return $events; @@ -436,11 +435,7 @@ function _kronolith_browse($path = '', $properties = array()) // // This request is for a specific item within a given calendar. // - global $kronolith_driver; - if ($kronolith_driver->getCalendar() != $parts[1]) { - $kronolith_driver->open($parts[1]); - } - $event = &$kronolith_driver->getEvent($parts[2]); + $event = Kronolith::getCalendar(null, $parts[1])->getEvent($parts[2]); if (is_a($event, 'PEAR_Error')) { return $event; } @@ -489,7 +484,6 @@ function _kronolith_browse($path = '', $properties = array()) function _kronolith_put($path, $content, $content_type) { require_once dirname(__FILE__) . '/base.php'; - global $kronolith_driver; if (substr($path, 0, 9) == 'kronolith') { $path = substr($path, 9); @@ -541,9 +535,10 @@ function _kronolith_put($path, $content, $content_type) $iCal->addComponent($content); } + $kronolith_driver = Kronolith::getDriver(); foreach ($iCal->getComponents() as $content) { if (is_a($content, 'Horde_iCalendar_vevent')) { - $event = &$kronolith_driver->getEvent(); + $event = $kronolith_driver->getEvent(); $event->fromiCalendar($content); $event->setCalendar($calendar); $uid = $event->getUID(); @@ -552,8 +547,7 @@ function _kronolith_put($path, $content, $content_type) if (isset($uids_remove[$uid])) { unset($uids_remove[$uid]); } - $existing_event = &$kronolith_driver->getByUID( - $uid, array($calendar)); + $existing_event = $kronolith_driver->getByUID($uid, array($calendar)); if (!is_a($existing_event, 'PEAR_Error')) { // Check if our event is newer then the existing - get the // event's history. @@ -621,7 +615,6 @@ function _kronolith_put($path, $content, $content_type) function _kronolith_path_delete($path) { require_once dirname(__FILE__) . '/base.php'; - global $kronolith_driver; if (substr($path, 0, 9) == 'kronolith') { $path = substr($path, 9); @@ -642,11 +635,10 @@ function _kronolith_path_delete($path) if (count($parts) == 3) { // Delete just a single entry - $kronolith_driver->open($calendarId); - return $kronolith_driver->deleteEvent($parts[2]); + return Kronolith::getDriver(null, $calendarId)->deleteEvent($parts[2]); } else { // Delete the entire calendar - $result = $kronolith_driver->delete($calendarId); + $result = Kronolith::getDriver()->delete($calendarId); if (is_a($result, 'PEAR_Error')) { return PEAR::raiseError(sprintf(_("Unable to delete calendar \"%s\": %s"), $calendarId, $result->getMessage())); } else { @@ -789,7 +781,6 @@ function _kronolith_getActionTimestamp($uid, $action, $calendar = null) function _kronolith_import($content, $contentType, $calendar = null) { require_once dirname(__FILE__) . '/base.php'; - global $kronolith_driver; if (!isset($calendar)) { $calendar = Kronolith::getDefaultCalendar(PERMS_EDIT); @@ -798,7 +789,6 @@ function _kronolith_import($content, $contentType, $calendar = null) Kronolith::listCalendars(false, PERMS_EDIT))) { return PEAR::raiseError(_("Permission Denied")); } - $kronolith_driver->open($calendar); switch ($contentType) { case 'text/calendar': @@ -817,17 +807,17 @@ function _kronolith_import($content, $contentType, $calendar = null) return PEAR::raiseError(_("No iCalendar data was found.")); } + $kronolith_driver = Kronolith::getDriver(null, $calendar); $ids = array(); foreach ($components as $content) { if (is_a($content, 'Horde_iCalendar_vevent')) { - $event = &$kronolith_driver->getEvent(); + $event = $kronolith_driver->getEvent(); $event->fromiCalendar($content); $event->setCalendar($calendar); // Check if the entry already exists in the data source, first // by UID. $uid = $event->getUID(); - $existing_event = &$kronolith_driver->getByUID( - $uid, array($calendar)); + $existing_event = $kronolith_driver->getByUID($uid, array($calendar)); if (!is_a($existing_event, 'PEAR_Error')) { return PEAR::raiseError(_("Already Exists"), 'horde.message', null, null, $uid); @@ -882,9 +872,9 @@ function _kronolith_import($content, $contentType, $calendar = null) function _kronolith_export($uid, $contentType) { require_once dirname(__FILE__) . '/base.php'; - global $kronolith_driver, $kronolith_shares; + global $kronolith_shares; - $event = $kronolith_driver->getByUID($uid); + $event = Kronolith::getDriver()->getByUID($uid); if (is_a($event, 'PEAR_Error')) { return $event; } @@ -931,17 +921,14 @@ function _kronolith_export($uid, $contentType) function _kronolith_exportCalendar($calendar, $contentType) { require_once dirname(__FILE__) . '/base.php'; - global $kronolith_driver, $kronolith_shares; + global $kronolith_shares; if (!array_key_exists($calendar, Kronolith::listCalendars(false, PERMS_READ))) { return PEAR::raiseError(_("Permission Denied")); } - if ($kronolith_driver->getCalendar() != $calendar) { - $kronolith_driver->open($calendar); - } - + $kronolith_driver = Kronolith::getDriver(null, $calendar); $events = $kronolith_driver->listEvents(); $version = '2.0'; @@ -955,7 +942,7 @@ function _kronolith_exportCalendar($calendar, $contentType) $iCal->setAttribute('X-WR-CALNAME', String::convertCharset($share->get('name'), NLS::getCharset(), 'utf-8')); foreach ($events as $id) { - $event = &$kronolith_driver->getEvent($id); + $event = $kronolith_driver->getEvent($id); if (is_a($event, 'PEAR_Error')) { return $event; } @@ -993,8 +980,8 @@ function _kronolith_delete($uid) } require_once dirname(__FILE__) . '/base.php'; - global $kronolith_driver; + $kronolith_driver = Kronolith::getDriver(); $events = $kronolith_driver->getByUID($uid, null, true); if (is_a($events, 'PEAR_Error')) { return $events; @@ -1052,9 +1039,8 @@ function _kronolith_delete($uid) function _kronolith_replace($uid, $content, $contentType) { require_once dirname(__FILE__) . '/base.php'; - global $kronolith_driver; - $event = $kronolith_driver->getByUID($uid); + $event = Kronolith::getDriver()->getByUID($uid); if (is_a($event, 'PEAR_Error')) { return $event; } @@ -1143,7 +1129,7 @@ function &_kronolith_eventFromUID($uid) { require_once dirname(__FILE__) . '/base.php'; - $event = $GLOBALS['kronolith_driver']->getByUID($uid); + $event = Kronolith::getDriver()->getByUID($uid); if (is_a($event, 'PEAR_Error')) { return $event; } @@ -1180,7 +1166,7 @@ function _kronolith_updateAttendee($response, $sender = null) return $uid; } - $events = $GLOBALS['kronolith_driver']->getByUID($uid, null, true); + $events = Kronolith::getDriver()->getByUID($uid, null, true); if (is_a($events, 'PEAR_Error')) { return $events; } diff --git a/kronolith/lib/base.php b/kronolith/lib/base.php index 0d30d21a6..f40a299c4 100644 --- a/kronolith/lib/base.php +++ b/kronolith/lib/base.php @@ -74,9 +74,6 @@ Horde::compressOutput(); /* Set the timezone variable, if available. */ NLS::setTimeZone(); -/* Create a calendar backend object. */ -$GLOBALS['kronolith_driver'] = Kronolith_Driver::factory(); - /* Create a share instance. */ $GLOBALS['kronolith_shares'] = &Horde_Share::singleton($registry->getApp()); diff --git a/kronolith/lib/tests/bug6031.phpt b/kronolith/lib/tests/bug6031.phpt index 35d16ba11..ff6c064fa 100644 --- a/kronolith/lib/tests/bug6031.phpt +++ b/kronolith/lib/tests/bug6031.phpt @@ -30,7 +30,7 @@ $GLOBALS['registry']->pushApp('kronolith'); $test->prepareNewFolder($world['storage'], 'Calendar', 'event', true); /* Pretend that we are kronolith */ -$kolab = &new Kolab(); +$kolab = new Kolab(); /* Open our calendar */ $kolab->open('INBOX/Calendar', 1); @@ -56,11 +56,10 @@ $object = array( var_dump($kolab->_storage->save($object)); // Check that the driver can be created -$kron = Kronolith_Driver::factory('Kolab'); -$kron->open('wrobel@example.org'); +$kron = Kronolith::getDriver('Kolab', 'wrobel@example.org'); -$start = &new Horde_Date(86400); -$end = &new Horde_Date(172800); +$start = new Horde_Date(86400); +$end = new Horde_Date(172800); // List the events of tomorrow (none, since recurrence has exception) $a = $kron->listEvents($start, $end); @@ -70,8 +69,8 @@ if (is_a($a, 'PEAR_Error')) { var_dump($a); } -$start = &new Horde_Date(259200); -$end = &new Horde_Date(345600); +$start = new Horde_Date(259200); +$end = new Horde_Date(345600); // List the events in three days (recurring event) $a = $kron->listEvents($start, $end); diff --git a/kronolith/new.php b/kronolith/new.php index 06e2a3186..61585a683 100644 --- a/kronolith/new.php +++ b/kronolith/new.php @@ -32,7 +32,7 @@ if (!$calendar_id) { header('Location: ' . Horde::applicationUrl($url, true)); } -$event = $kronolith_driver->getEvent(); +$event = Kronolith::getDriver()->getEvent(); $_SESSION['kronolith']['attendees'] = $event->getAttendees(); $date = Util::getFormData('datetime'); diff --git a/kronolith/scripts/agenda.php b/kronolith/scripts/agenda.php index f2841f707..a8894d892 100755 --- a/kronolith/scripts/agenda.php +++ b/kronolith/scripts/agenda.php @@ -68,6 +68,7 @@ function send_agendas() $runtime = new Horde_Date($runtime); $default_timezone = date_default_timezone_get(); + $kronolith_driver = Kronolith::getDriver(); // Loop through the users and generate an agenda for them foreach ($users as $user) { @@ -122,10 +123,10 @@ function send_agendas() // Get a list of events for today $eventlist = array(); foreach ($calendars as $calId => $calendar) { - $GLOBALS['kronolith_driver']->open($calId); - $events = $GLOBALS['kronolith_driver']->listEvents($runtime, $runtime); + $kronolith_driver->open($calId); + $events = $kronolith_driver->listEvents($runtime, $runtime); foreach ($events as $eventId) { - $event = $GLOBALS['kronolith_driver']->getEvent($eventId); + $event = $kronolith_driver->getEvent($eventId); if (is_a($event, 'PEAR_Error')) { return $event; } diff --git a/kronolith/search.php b/kronolith/search.php index 9c1e71419..ba1455599 100644 --- a/kronolith/search.php +++ b/kronolith/search.php @@ -31,7 +31,7 @@ $search_mode = Util::getFormData('search_mode', 'basic'); if ($search_mode != 'basic') { /* Make a new empty event object with default values. */ - $event = &$kronolith_driver->getEvent(); + $event = Kronolith::getDriver()->getEvent(); $event->title = $event->calendars = $event->location = $event->status = $event->description = null; @@ -59,7 +59,7 @@ $desc = Util::getFormData('pattern_desc'); $title = Util::getFormData('pattern_title'); if ($desc || $title) { /* We're doing a simple search. */ - $event = &$kronolith_driver->getEvent(); + $event = Kronolith::getDriver()->getEvent(); $event->setDescription($desc); $event->setTitle($title); $event->status = null; -- 2.11.0