Remove global $kronolith_driver object. We still need a singleton though.
authorJan Schneider <jan@horde.org>
Thu, 26 Feb 2009 23:05:51 +0000 (00:05 +0100)
committerJan Schneider <jan@horde.org>
Thu, 26 Feb 2009 23:05:51 +0000 (00:05 +0100)
Move almost all configuration, globals, etc. out of drivers.
Work further towards Ical/Holidays being standalone, independent drivers.

22 files changed:
kronolith/add.php
kronolith/attend.php
kronolith/data.php
kronolith/delete.php
kronolith/edit.php
kronolith/ics.php
kronolith/lib/Driver.php
kronolith/lib/Driver/Holidays.php
kronolith/lib/Driver/Ical.php
kronolith/lib/Driver/Sql.php
kronolith/lib/Event.php
kronolith/lib/Forms/DeleteCalendar.php
kronolith/lib/Forms/EditCalendar.php
kronolith/lib/Imple/TagActions.php
kronolith/lib/Kronolith.php
kronolith/lib/Maintenance/Task/purge_events.php
kronolith/lib/api.php
kronolith/lib/base.php
kronolith/lib/tests/bug6031.phpt
kronolith/new.php
kronolith/scripts/agenda.php
kronolith/search.php

index 0a84ba5..0030a8a 100644 (file)
@@ -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 {
index d8b272e..79b4bfb 100644 (file)
@@ -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');
index 789080d..8f3d257 100644 (file)
@@ -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) {
index 90dec99..1404751 100644 (file)
@@ -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);
index ebf1576..6868f03 100644 (file)
@@ -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');
index 6ccdfa7..6236e5b 100644 (file)
@@ -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;
         }
index c0340d2..ef561fa 100644 (file)
@@ -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();
index 32a0fae..6c7769d 100644 (file)
@@ -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);
                 }
index 7b5c5fd..96e1f49 100644 (file)
@@ -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 <chuck@horde.org>
  * @author  Jan Schneider <jan@horde.org>
  * @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://') {
index ba3a568..b55a779 100644 (file)
@@ -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;
     }
+
 }
index 5b8e162..a6e156d 100644 (file)
@@ -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 {
index 2235fb0..31cd5f5 100644 (file)
@@ -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 {
index 8b59517..1698c25 100644 (file)
@@ -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()));
             }
index c6732d1..ee22739 100644 (file)
@@ -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());
         }
 
index 9dd21e5..f64b85a 100644 (file)
@@ -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)) {
index 659f1de..c37fc49 100644 (file)
@@ -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;
index 274156d..7824df4 100644 (file)
@@ -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;
     }
index 0d30d21..f40a299 100644 (file)
@@ -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());
 
index 35d16ba..ff6c064 100644 (file)
@@ -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);
index 06e2a31..61585a6 100644 (file)
@@ -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');
index f2841f7..a8894d8 100755 (executable)
@@ -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;
                 }
index 9c1e714..ba14555 100644 (file)
@@ -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;