From: Michael M Slusarz Date: Thu, 26 Aug 2010 19:26:00 +0000 (-0600) Subject: Use injector to get Turba_Driver object X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=65431c8d3240607bcf566197b0876b93cbc6552b;p=horde.git Use injector to get Turba_Driver object Throw Turba_Exceptions if driver init fails. --- diff --git a/turba/add.php b/turba/add.php index 2688b858f..6303d3c11 100644 --- a/turba/add.php +++ b/turba/add.php @@ -33,10 +33,14 @@ if (!$addSources) { /* A source has been selected, connect and set up the fields. */ if ($source) { - $driver = Turba_Driver::singleton($source); - if ($driver instanceof PEAR_Error) { - $notification->push(sprintf(_("Failed to access the address book: %s"), $driver->getMessage()), 'horde.error'); - } else { + try { + $driver = $injector->getInstance('Turba_Driver')->getDriver($source); + } catch (Turba_Exception $e) { + $notification->push($e, 'horde.error'); + $driver = null; + } + + if (!is_null($driver)) { /* Check permissions. */ $max_contacts = Turba::getExtendedPermission($driver, 'max_contacts'); if ($max_contacts !== true && diff --git a/turba/contact.php b/turba/contact.php index dfa8959e2..08d3ed161 100644 --- a/turba/contact.php +++ b/turba/contact.php @@ -21,9 +21,10 @@ if (!isset($GLOBALS['cfgSources'][$source])) { } /* Set the contact from the key requested. */ -$driver = Turba_Driver::singleton($source); -if ($driver instanceof PEAR_Error) { - $notification->push($driver->getMessage(), 'horde.error'); +try { + $driver = $injector->getInstance('Turba_Driver')->getDriver($source); +} catch (Turba_Exception $e) { + $notification->push($e, 'horde.error'); Horde::applicationUrl($prefs->getValue('initial_page'), true)->redirect(); } diff --git a/turba/data.php b/turba/data.php index 166686a4c..01b7520d5 100644 --- a/turba/data.php +++ b/turba/data.php @@ -241,9 +241,10 @@ case 'export': $all_fields = array(); foreach ($sources as $source => $objectkeys) { /* Create a Turba storage instance. */ - $driver = Turba_Driver::singleton($source); - if ($driver instanceof PEAR_Error) { - $notification->push(sprintf(_("Failed to access the address book: %s"), $driver->getMessage()), 'horde.error'); + try { + $driver = $injector->getInstance('Turba_Driver')->getDriver($source); + } catch (Turba_Exception $e) { + $notification->push($e, 'horde.error'); $error = true; break; } @@ -337,9 +338,10 @@ case 'export': case Horde_Data::IMPORT_FILE: $dest = Horde_Util::getFormData('dest'); - $driver = Turba_Driver::singleton($dest); - if ($driver instanceof PEAR_Error) { - $notification->push(sprintf(_("Failed to access the address book: %s"), $driver->getMessage()), 'horde.error'); + try { + $driver = $injector->getInstance('Turba_Driver')->getDriver($source); + } catch (Turba_Exception $e) { + $notification->push($e, 'horde.error'); $error = true; break; } @@ -419,13 +421,17 @@ if (is_array($next_step)) { /* Create a Turba storage instance. */ $dest = $_SESSION['import_data']['target']; - $driver = Turba_Driver::singleton($dest); - if ($driver instanceof PEAR_Error) { - $notification->push(sprintf(_("Failed to access the address book: %s"), $driver->getMessage()), 'horde.error'); - } elseif (!count($next_step)) { + try { + $driver = $injector->getInstance('Turba_Driver')->getDriver($source); + } catch (Turba_Exception $e) { + $notification->push($e, 'horde.error'); + $driver = null; + } + + if (!count($next_step)) { $notification->push(sprintf(_("The %s file didn't contain any contacts."), $file_types[$_SESSION['import_data']['format']]), 'horde.error'); - } else { + } elseif ($driver) { /* Purge old address book if requested. */ if ($_SESSION['import_data']['purge']) { $result = $driver->deleteAll(); diff --git a/turba/delete.php b/turba/delete.php index 0ea481e59..63c9519ac 100644 --- a/turba/delete.php +++ b/turba/delete.php @@ -18,7 +18,7 @@ Horde_Registry::appInit('turba'); $source = Horde_Util::getFormData('source'); $key = Horde_Util::getFormData('key'); -$driver = Turba_Driver::singleton($source); +$driver = $injector->getInstance('Turba_Driver')->getDriver($source); if ($conf['documents']['type'] != 'none') { $object = $driver->getObject($key); diff --git a/turba/deletefile.php b/turba/deletefile.php index 775acc12f..f49aa2eb1 100644 --- a/turba/deletefile.php +++ b/turba/deletefile.php @@ -23,7 +23,7 @@ if ($source === null || !isset($cfgSources[$source])) { Horde::applicationUrl($prefs->getValue('initial_page'), true)->redirect(); } -$driver = Turba_Driver::singleton($source); +$driver = $injector->getInstance('Turba_Driver')->getDriver($source); $contact = $driver->getObject(Horde_Util::getPost('key')); if (is_a($contact, 'PEAR_Error')) { $notification->push($contact, 'horde.error'); diff --git a/turba/edit.php b/turba/edit.php index d4ac0500a..899aae415 100644 --- a/turba/edit.php +++ b/turba/edit.php @@ -45,7 +45,7 @@ if ($source === null || !isset($cfgSources[$source])) { $url->redirect(); } -$driver = Turba_Driver::singleton($source); +$driver = $injector->getInstance('Turba_Driver')->getDriver($source); /* Set the contact from the requested key. */ $contact = $driver->getObject($key); diff --git a/turba/lib/Api.php b/turba/lib/Api.php index 270336c20..8ab43e8e3 100644 --- a/turba/lib/Api.php +++ b/turba/lib/Api.php @@ -50,13 +50,13 @@ class Turba_Api extends Horde_Registry_Api @list($source, $key) = explode('.', $id, 2); if (isset($GLOBALS['cfgSources'][$source]) && $key) { - $driver = Turba_Driver::singleton($source); - if (!($driver instanceof PEAR_Error)) { + try { + $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($source); $object = $driver->getObject($key); if (!($object instanceof PEAR_Error)) { return $object->getValue('name'); } - } + } catch (Turba_Exception $e) {} } return false; @@ -322,10 +322,7 @@ class Turba_Api extends Horde_Registry_Api } // Load the Turba driver. - $driver = Turba_Driver::singleton($parts[1]); - if ($driver instanceof PEAR_Error) { - throw new Turba_Exception(sprintf(_("Connection failed: %s"), $driver->getMessage())); - } + $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($parts[1]); $contacts = $driver->search(array()); if ($contacts instanceof PEAR_Error) { @@ -376,10 +373,7 @@ class Turba_Api extends Horde_Registry_Api } // Load the Turba driver. - $driver = Turba_Driver::singleton($parts[1]); - if ($driver instanceof PEAR_Error) { - throw new Turba_Exception(sprintf(_("Connection failed: %s"), $driver->getMessage())); - } + $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($parts[1]); $contact = $driver->getObject($parts[2]); if ($contact instanceof PEAR_Error) { @@ -429,10 +423,7 @@ class Turba_Api extends Horde_Registry_Api } // Load the Turba driver. - $driver = Turba_Driver::singleton($parts[1]); - if ($driveri instanceof PEAR_Error) { - throw new Turba_Exception(sprintf(_("Connection failed: %s"), $driver->getMessage())); - } + $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($parts[1]); $ret = $driver->delete($parts[2]); if ($ret instanceof PEAR_Error) { @@ -477,13 +468,9 @@ class Turba_Api extends Horde_Registry_Api throw new Turba_Exception(sprintf(_("Invalid address book: %s"), $source)); } - $storage = Turba_Driver::singleton($source); - if ($storage instanceof PEAR_Error) { - throw new Turba_Exception(sprintf(_("Connection failed: %s"), $storage->getMessage())); - } + $storage = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($source); $results = $storage->search(array()); - if ($results instanceof PEAR_Error) { throw new Turba_Exception(sprintf(_("Error searching the address book: %s"), $results->getMessage())); } @@ -540,10 +527,7 @@ class Turba_Api extends Horde_Registry_Api throw new Turba_Exception(sprintf(_("Invalid address book: %s"), $source)); } - $driver = Turba_Driver::singleton($source); - if ($driver instanceof PEAR_Error) { - throw new Turba_Exception(sprintf(_("Connection failed: %s"), $driver->getMessage())); - } + $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($source); $histories = $history->getByTimestamp( '>', $timestamp, $filter, @@ -597,10 +581,7 @@ class Turba_Api extends Horde_Registry_Api throw new Turba_Exception(sprintf(_("Invalid address book: %s"), $source)); } - $driver = Turba_Driver::singleton($source); - if ($driver instanceof PEAR_Error) { - throw new Turba_Exception(sprintf(_("Connection failed: %s"), $driver->getMessage())); - } + $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($source); $ts = $history->getActionTimestamp('turba:' . $driver->getName() . ':' . $uid, @@ -646,10 +627,7 @@ class Turba_Api extends Horde_Registry_Api throw new Turba_Exception(sprintf(_("Invalid address book: %s"), $import_source)); } - $driver = Turba_Driver::singleton($import_source); - if ($driver instanceof PEAR_Error) { - throw new Turba_Exception(sprintf(_("Connection failed: %s"), $driver->getMessage())); - } + $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($import_source); if (!$driver->hasPermission(Horde_Perms::EDIT)) { throw new Turba_Exception(_("Permission denied")); @@ -789,10 +767,7 @@ class Turba_Api extends Horde_Registry_Api throw new Turba_Exception(_("Invalid ID")); } - $driver = Turba_Driver::singleton($source); - if ($driver instanceof PEAR_Error) { - throw new Turba_Exception(sprintf(_("Connection failed: %s"), $driver->getMessage())); - } + $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($source); if (!$driver->hasPermission(Horde_Perms::READ)) { continue; @@ -859,13 +834,8 @@ class Turba_Api extends Horde_Registry_Api public function ownVCard() { $contact = $this->getOwnContactObject(); - if ($contact instanceof PEAR_Error) { - throw new Turba_Exception($contact); - } - $driver = Turba_Driver::singleton($contact['source']); - if ($driver instanceof PEAR_Error) { - throw new Turba_Exception(sprintf(_("Connection failed: %s"), $driver->getMessage())); - } + $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($contact['source']); + $vcard = $driver->tovCard($contact['contact'], '3.0', null, true); $vcard->setAttribute('VERSION', '3.0'); @@ -905,10 +875,7 @@ class Turba_Api extends Horde_Registry_Api throw new Turba_Exception(_("The address book with your own contact doesn't exist anymore.")); } - $driver = Turba_Driver::singleton($source); - if ($driver instanceof PEAR_Error) { - throw new Turba_Exception(sprintf(_("Connection failed: %s"), $driver->getMessage())); - } + $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($source); if (!$driver->hasPermission(Horde_Perms::READ)) { throw new Turba_Exception(_("You don't have sufficient permissions to read the address book that contains your own contact.")); @@ -972,10 +939,7 @@ class Turba_Api extends Horde_Registry_Api throw new Turba_Exception(_("Invalid ID")); } - $driver = Turba_Driver::singleton($source); - if ($driver instanceof PEAR_Error) { - throw new Turba_Exception(sprintf(_("Connection failed: %s"), $driver->getMessage())); - } + $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($source); if (!$GLOBALS['registry']->isAdmin() && !$driver->hasPermission(Horde_Perms::DELETE)) { @@ -1040,10 +1004,7 @@ class Turba_Api extends Horde_Registry_Api } // Check permissions. - $driver = Turba_Driver::singleton($source); - if ($driver instanceof PEAR_Error) { - throw new Turba_Exception(sprintf(_("Connection failed: %s"), $driver->getMessage())); - } + $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($source); if (!$driver->hasPermission(Horde_Perms::EDIT)) { continue; } @@ -1170,10 +1131,7 @@ class Turba_Api extends Horde_Registry_Api continue; } - $driver = Turba_Driver::singleton($source); - if ($driver instanceof PEAR_Error) { - throw new Turba_Exception(sprintf(_("Connection failed: %s"), $driver->getMessage())); - } + $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($source); // Determine the name of the column to sort by. $columns = isset($sort_columns[$source]) @@ -1326,10 +1284,7 @@ class Turba_Api extends Horde_Registry_Api } if (isset($cfgSources[$source])) { - $driver = Turba_Driver::singleton($source); - if ($driver instanceof PEAR_Error) { - throw new Turba_Exception($driver); - } + $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($source); $object = $driver->getObject($objectId); if ($object instanceof PEAR_Error) { @@ -1368,14 +1323,11 @@ class Turba_Api extends Horde_Registry_Api } if (isset($cfgSources[$source])) { - $driver = Turba_Driver::singleton($source); - if ($driver instanceof PEAR_Error) { - throw new Turba_Exception($driver->getMessage()); - } + $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($source); $objects = $driver->getObjects($objectIds); if ($objects instanceof PEAR_Error) { - throw new Turba_Exception($objects->getMessage()); + throw new Turba_Exception($objects); } foreach ($objects as $object) { @@ -1414,10 +1366,7 @@ class Turba_Api extends Horde_Registry_Api $results = array(); foreach ($sources as $source) { if (isset($cfgSources[$source])) { - $driver = Turba_Driver::singleton($source); - if ($driver instanceof PEAR_Error) { - throw new Turba_Exception(sprintf(_("Connection failed: %s"), $driver->getMessage())); - } + $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($source); $res = $driver->search(array()); if (!($res instanceof Turba_List)) { @@ -1485,10 +1434,7 @@ class Turba_Api extends Horde_Registry_Api $objects = array(); foreach ($time_categories as $category) { list($category, $source) = explode('/', $category, 2); - $driver = Turba_Driver::singleton($source); - if ($driver instanceof PEAR_Error) { - throw new Turba_Exception(sprintf(_("Connection failed: %s"), $driver->getMessage())); - } + $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($source); $new_objects = $driver->listTimeObjects($start, $end, $category); if ($new_objects instanceof PEAR_Error) { throw new Turba_Exception($new_objects); @@ -1632,10 +1578,7 @@ class Turba_Api extends Horde_Registry_Api throw new Turba_Exception(_("Invalid entry")); } - $driver = Turba_Driver::singleton($source); - if ($driver instanceof PEAR_Error) { - throw new Turba_Exception(sprintf(_("Connection failed: %s"), $driver->getMessage())); - } + $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($source); if (!$driver->hasPermission(Horde_Perms::EDIT)) { throw new Turba_Exception(_("Permission denied")); @@ -1724,10 +1667,7 @@ class Turba_Api extends Horde_Registry_Api continue; } - $driver = Turba_Driver::singleton($source); - if ($driver instanceof PEAR_Error) { - throw new Turba_Exception($driver); - } + $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($source); $list = $driver->search(array('email' => $address), null, 'AND', array(), $strict ? array('email') : array()); if (!($list instanceof Turba_List)) { @@ -1783,10 +1723,7 @@ class Turba_Api extends Horde_Registry_Api foreach ($sources as $source) { if (isset($cfgSources[$source])) { - $driver = Turba_Driver::singleton($source); - if ($driver instanceof PEAR_Error) { - throw new Turba_Exception($driver->getMessage()); - } + $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($source); if (!$driver->hasPermission(Horde_Perms::EDIT)) { continue; } diff --git a/turba/lib/Application.php b/turba/lib/Application.php index e67bd485a..57cabf1a9 100644 --- a/turba/lib/Application.php +++ b/turba/lib/Application.php @@ -7,10 +7,12 @@ * * Copyright 2010 The Horde Project (http://www.horde.org/) * - * See the enclosed file COPYING for license information (GPL). If you - * did not receive this file, see http://www.fsf.org/copyleft/gpl.html. + * See the enclosed file COPYING for license information (APL). If you + * did not receive this file, see http://www.horde.org/licenses/apl.html. * - * @package Turba + * @category Horde + * @license http://www.horde.org/licenses/apl.html APL + * @package Turba */ /* Determine the base directories. */ @@ -57,6 +59,15 @@ class Turba_Application extends Horde_Registry_Application */ protected function _init() { + /* Add Turba-specific binders. */ + $binders = array( + 'Turba_Driver' => new Turba_Injector_Binder_Driver() + ); + + foreach ($binders as $key => $val) { + $GLOBALS['injector']->addBinder($key, $val); + } + // Turba source and attribute configuration. $attributes = Horde::loadConfiguration('attributes.php', 'attributes', 'turba'); include TURBA_BASE . '/config/sources.php'; @@ -392,15 +403,17 @@ class Turba_Application extends Horde_Registry_Application foreach ($cfgSources as $source) { if (empty($source['use_shares'])) { // Shares not enabled for this source - $driver = Turba_Driver::singleton($source); - if (is_a($driver, 'PEAR_Error')) { - Horde::logMessage($driver, 'ERR'); + try { + $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($source); + } catch (Turba_Exception $e) { + Horde::logMessage($e, 'ERR'); $hasError = true; - } else { - $result = $driver->removeUserData($user); - if (is_a($result, 'PEAR_Error')) { - Horde::logMessage($result, 'ERR'); - } + continue; + } + + $result = $driver->removeUserData($user); + if ($result instanceof PEAR_Error) { + Horde::logMessage($result, 'ERR'); } } } @@ -416,9 +429,14 @@ class Turba_Application extends Horde_Registry_Application /* Only attempt to delete the user's default share */ if (!empty($params['default'])) { $config = Turba::getSourceFromShare($share); - $driver = Turba_Driver::singleton($config); + try { + $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($config); + } catch (Turba_Exception $e) { + continue; + } + $result = $driver->removeUserData($user); - if (is_a($result, 'PEAR_Error')) { + if ($result instanceof PEAR_Error) { Horde::logMessage($result, 'ERR'); $hasError = true; } diff --git a/turba/lib/Driver.php b/turba/lib/Driver.php index 1d6b818c5..29faf2711 100644 --- a/turba/lib/Driver.php +++ b/turba/lib/Driver.php @@ -2593,28 +2593,24 @@ class Turba_Driver implements Countable } /** - * Static method to construct Turba_Driver objects. Use this so that we - * can return PEAR_Error objects if anything goes wrong. - * - * Should only be called by Turba_Driver::singleton(). - * - * @see Turba_Driver::singleton() - * @access private + * Static method to construct Turba_Driver objects. * * @param string $name String containing the internal name of this * source. * @param array $config Array containing the configuration information for * this source. + * + * @return Turba_Driver The concrete driver object. + * @throws Turba_Exception */ - function &factory($name, $config) + static public function factory($name, $config) { - $class = 'Turba_Driver_' . ucfirst(basename($config['type'])); + $class = __CLASS__ . '_' . ucfirst(basename($config['type'])); if (class_exists($class)) { $driver = new $class($config['params']); } else { - $driver = PEAR::raiseError(sprintf(_("Unable to load the definition of %s."), $class)); - return $driver; + throw new Turba_Exception(sprintf(_("Unable to load the definition of %s."), $class)); } /* Store name and title. */ @@ -2622,11 +2618,7 @@ class Turba_Driver implements Countable $driver->title = $config['title']; /* Initialize */ - $result = $driver->_init(); - if (is_a($result, 'PEAR_Error')) { - $driver = PEAR::raiseError($result->getMessage()); - return $driver; - } + $driver->_init(); /* Store and translate the map at the Source level. */ $driver->map = $config['map']; @@ -2659,55 +2651,12 @@ class Turba_Driver implements Countable } /** - * Attempts to return a reference to a concrete Turba_Driver instance - * based on the $config array. It will only create a new instance if no - * Turba_Driver instance with the same parameters currently exists. - * - * This method must be invoked as: - * $driver = &Turba_Driver::singleton() - * - * @param mixed $name Either a string containing the internal name of this - * source, or a config array describing the source. - * - * @return Turba_Driver The concrete Turba_Driver reference, or a - * PEAR_Error on error. - */ - function &singleton($name) - { - static $instances = array(); - - if (is_array($name)) { - $key = md5(serialize($name)); - $srcName = ''; - $srcConfig = $name; - } else { - $key = $name; - $srcName = $name; - if (!empty($GLOBALS['cfgSources'][$name])) { - $srcConfig = $GLOBALS['cfgSources'][$name]; - } else { - $error = PEAR::raiseError('Source not found'); - return $error; - } - } - - if (!isset($instances[$key])) { - if (!is_array($name) && !isset($GLOBALS['cfgSources'][$name])) { - $error = PEAR::raiseError(sprintf(_("The address book \"%s\" does not exist."), $name)); - return $error; - } - $instances[$key] = Turba_Driver::factory($srcName, $srcConfig); - } - - return $instances[$key]; - } - - /** * Initialize the driver. + * + * @throws Turba_Exception */ - function _init() + protected function _init() { - return true; } /** diff --git a/turba/lib/Driver/Facebook.php b/turba/lib/Driver/Facebook.php index e5ac8b3e7..bed0d7462 100644 --- a/turba/lib/Driver/Facebook.php +++ b/turba/lib/Driver/Facebook.php @@ -18,13 +18,6 @@ class Turba_Driver_Facebook extends Turba_Driver private $_facebook; /** - */ - function _init() - { - return true; - } - - /** * Checks if the current user has the requested permissions on this * source. * diff --git a/turba/lib/Driver/Favourites.php b/turba/lib/Driver/Favourites.php index 149aac6d8..18bb85f00 100644 --- a/turba/lib/Driver/Favourites.php +++ b/turba/lib/Driver/Favourites.php @@ -9,13 +9,6 @@ class Turba_Driver_Favourites extends Turba_Driver { /** - */ - function _init() - { - return true; - } - - /** * Checks if the current user has the requested permissions on this * source. * diff --git a/turba/lib/Driver/Group.php b/turba/lib/Driver/Group.php index 2251b52b3..a1dedf8a5 100644 --- a/turba/lib/Driver/Group.php +++ b/turba/lib/Driver/Group.php @@ -21,14 +21,6 @@ class Turba_Driver_Group extends Turba_Driver } /** - * Initialize the group driver. - */ - function _init() - { - return true; - } - - /** * Checks if the current user has the requested permissions on this * source. This source is always read only. * diff --git a/turba/lib/Driver/Imsp.php b/turba/lib/Driver/Imsp.php index 9f27cdc08..7cbd4c838 100644 --- a/turba/lib/Driver/Imsp.php +++ b/turba/lib/Driver/Imsp.php @@ -70,30 +70,31 @@ class Turba_Driver_Imsp extends Turba_Driver /** * Initialize the IMSP connection and check for error. + * + * @throws Turba_Exception */ - function _init() + protected function _init() { global $conf; $this->_bookName = $this->getContactOwner(); $this->_imsp = Net_IMSP::singleton('Book', $this->params); $result = $this->_imsp->init(); - if (is_a($result, 'PEAR_Error')) { + if ($result instanceof PEAR_Error) { $this->_authenticated = false; - return $result; + throw new Turba_Exception($result); } if (!empty($conf['log'])) { $logParams = $conf['log']; $result = $this->_imsp->setLogger($conf['log']); - if (is_a($result, 'PEAR_Error')) { - return $result; + if ($result instanceof PEAR_Error) { + throw new Turba_Exception($result); } } Horde::logMessage('IMSP Driver initialized for ' . $this->_bookName, 'DEBUG'); $this->_authenticated = true; - return true; } /** diff --git a/turba/lib/Driver/Kolab.php b/turba/lib/Driver/Kolab.php index a4af43a99..19e1ca1d9 100644 --- a/turba/lib/Driver/Kolab.php +++ b/turba/lib/Driver/Kolab.php @@ -38,19 +38,15 @@ class Turba_Driver_Kolab extends Turba_Driver /** * Attempts to open a Kolab Groupware folder. - * - * @return boolean True on success, PEAR_Error on failure. */ - function _init() + protected function _init() { $this->_kolab = new Kolab(); - if (empty($this->_kolab->version)) { - $wrapper = "Turba_Driver_kolab_wrapper_old"; - } else { - $wrapper = "Turba_Driver_kolab_wrapper_new"; - } + $wrapper = empty($this->_kolab->version) + ? 'Turba_Driver_kolab_wrapper_old' + : 'Turba_Driver_kolab_wrapper_new'; - $this->_wrapper = &new $wrapper($this->name, $this->_kolab); + $this->_wrapper = new $wrapper($this->name, $this->_kolab); } /** diff --git a/turba/lib/Driver/Ldap.php b/turba/lib/Driver/Ldap.php index 02c790c7d..0e2024970 100644 --- a/turba/lib/Driver/Ldap.php +++ b/turba/lib/Driver/Ldap.php @@ -56,14 +56,17 @@ class Turba_Driver_Ldap extends Turba_Driver parent::__construct($params); } + /** + * @throws Turba_Exception + */ function _init() { if (!Horde_Util::extensionExists('ldap')) { - return PEAR::raiseError(_("LDAP support is required but the LDAP module is not available or not loaded.")); + throw new Turba_Exception(_("LDAP support is required but the LDAP module is not available or not loaded.")); } if (!($this->_ds = @ldap_connect($this->_params['server'], $this->_params['port']))) { - return PEAR::raiseError(_("Connection failure")); + throw new Turba_Exception(_("Connection failure")); } /* Set the LDAP protocol version. */ @@ -82,23 +85,22 @@ class Turba_Driver_Ldap extends Turba_Driver } /* Start TLS if we're using it. */ - if (!empty($this->_params['tls'])) { - if (!@ldap_start_tls($this->_ds)) { - return PEAR::raiseError(sprintf(_("STARTTLS failed: (%s) %s"), ldap_errno($this->_ds), ldap_error($this->_ds))); - } + if (!empty($this->_params['tls']) && + !@ldap_start_tls($this->_ds)) { + throw new Turba_Exception(sprintf(_("STARTTLS failed: (%s) %s"), ldap_errno($this->_ds), ldap_error($this->_ds))); } /* Bind to the server. */ if (isset($this->_params['bind_dn']) && isset($this->_params['bind_password'])) { - if (!@ldap_bind($this->_ds, $this->_params['bind_dn'], $this->_params['bind_password'])) { - return PEAR::raiseError(sprintf(_("Bind failed: (%s) %s"), ldap_errno($this->_ds), ldap_error($this->_ds))); - } - } elseif (!(@ldap_bind($this->_ds))) { - return PEAR::raiseError(sprintf(_("Bind failed: (%s) %s"), ldap_errno($this->_ds), ldap_error($this->_ds))); + $error = !@ldap_bind($this->_ds, $this->_params['bind_dn'], $this->_params['bind_password']); + } else { + $error = !(@ldap_bind($this->_ds)); } - return true; + if ($error) { + throw new Turba_Exception(sprintf(_("Bind failed: (%s) %s"), ldap_errno($this->_ds), ldap_error($this->_ds))); + } } /** diff --git a/turba/lib/Driver/Prefs.php b/turba/lib/Driver/Prefs.php index 7b9f1d0f9..14161343a 100644 --- a/turba/lib/Driver/Prefs.php +++ b/turba/lib/Driver/Prefs.php @@ -9,13 +9,6 @@ class Turba_Driver_Prefs extends Turba_Driver { /** - */ - function _init() - { - return true; - } - - /** * Returns all entries - searching isn't implemented here for now. The * parameters are simply ignored. * diff --git a/turba/lib/Driver/Share.php b/turba/lib/Driver/Share.php index bda4b12f7..0278e691d 100644 --- a/turba/lib/Driver/Share.php +++ b/turba/lib/Driver/Share.php @@ -76,15 +76,12 @@ class Turba_Driver_Share extends Turba_Driver } /** - * Initialize + * @throws Turba_Exception */ - function _init() + protected function _init() { $this->_share = &$this->_params['config']['params']['share']; - $this->_driver = &Turba_Driver::factory($this->name, $this->_params['config']); - if (is_a($this->_driver, 'PEAR_Error')) { - return $this->_driver; - } + $this->_driver = Turba_Driver::factory($this->name, $this->_params['config']); $this->_driver->_contact_owner = $this->_getContactOwner(); } diff --git a/turba/lib/Driver/Sql.php b/turba/lib/Driver/Sql.php index 113ec13bb..6d2669178 100644 --- a/turba/lib/Driver/Sql.php +++ b/turba/lib/Driver/Sql.php @@ -33,56 +33,17 @@ class Turba_Driver_Sql extends Turba_Driver */ var $_write_db; - function _init() + /** + * @throws Turba_Exception + */ + protected function _init() { - $this->_write_db = &DB::connect($this->_params, - array('persistent' => !empty($this->_params['persistent']), - 'ssl' => !empty($this->_params['ssl']))); - if (is_a($this->_write_db, 'PEAR_Error')) { - return $this->_write_db; - } - - // Set DB portability options. - switch ($this->_write_db->phptype) { - case 'mssql': - $this->_write_db->setOption('portability', DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_ERRORS | DB_PORTABILITY_RTRIM); - break; - default: - $this->_write_db->setOption('portability', DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_ERRORS); - } - - if ($this->_params['phptype'] == 'oci8') { - $this->_write_db->query('ALTER SESSION SET NLS_DATE_FORMAT = \'YYYY-MM-DD\''); - } - - /* Check if we need to set up the read DB connection - * seperately. */ - if (!empty($this->_params['splitread'])) { - $params = array_merge($this->_params, $this->_params['read']); - $this->_db = &DB::connect($params, - array('persistent' => !empty($params['persistent']), - 'ssl' => !empty($params['ssl']))); - if (is_a($this->_db, 'PEAR_Error')) { - return $this->_db; - } - - // Set DB portability options. - switch ($this->_db->phptype) { - case 'mssql': - $this->_db->setOption('portability', DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_ERRORS | DB_PORTABILITY_RTRIM); - break; - default: - $this->_db->setOption('portability', DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_ERRORS); - } - if ($params['phptype'] == 'oci8') { - $this->_db->query('ALTER SESSION SET NLS_DATE_FORMAT = \'YYYY-MM-DD\''); - } - } else { - /* Default to the same DB handle for reads. */ - $this->_db =& $this->_write_db; + try { + $this->_db = $GLOBALS['injector']->getInstance('Horde_Db_Pear')->getDb('read', 'turba', $this->_params); + $this->_write_db = $GLOBALS['injector']->getInstance('Horde_Db_Pear')->getDb('rw', 'turba', $this->_params); + } catch (Horde_Exception $e) { + throw new Turba_Exception($e); } - - return true; } /** diff --git a/turba/lib/Driver/Vbook.php b/turba/lib/Driver/Vbook.php index c8c83b2f5..d51dd595c 100644 --- a/turba/lib/Driver/Vbook.php +++ b/turba/lib/Driver/Vbook.php @@ -50,26 +50,22 @@ class Turba_Driver_Vbook extends Turba_Driver } /** + * @throws Turba_Exception */ - function _init() + protected function _init() { /* Grab a reference to the share for this vbook. */ $this->_share = $this->_params['share']; /* Load the underlying driver. */ - $this->_driver = &Turba_Driver::singleton($this->_params['source']); - if (is_a($this->_driver, 'PEAR_Error')) { - return $this->_driver; - } + $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($this->_params['source']); - if (!empty($this->_params['criteria'])) { - $this->searchCriteria = $this->_params['criteria']; - } else { - $this->searchCriteria = array(); - } - $this->searchType = count($this->searchCriteria) > 1 ? 'advanced' : 'basic'; - - return true; + $this->searchCriteria = empty($this->_params['criteria']) + ? array() + : $this->_params['criteria']; + $this->searchType = (count($this->searchCriteria) > 1) + ? 'advanced' + : 'basic'; } /** diff --git a/turba/lib/Form/CreateAddressBook.php b/turba/lib/Form/CreateAddressBook.php index fe682b255..3ffc4976a 100644 --- a/turba/lib/Form/CreateAddressBook.php +++ b/turba/lib/Form/CreateAddressBook.php @@ -27,15 +27,15 @@ class Turba_Form_CreateAddressBook extends Horde_Form $this->setButtons(array(_("Create"))); } + /** + * @throws Turba_Exception + */ function execute() { // Need a clean cfgSources array include TURBA_BASE . '/config/sources.php'; - $driver = Turba_Driver::singleton($cfgSources[$GLOBALS['conf']['shares']['source']]); - if ($driver instanceof PEAR_Error) { - return $driver; - } + $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($cfgSources[$GLOBALS['conf']['shares']['source']]); $params = array( 'params' => array('source' => $GLOBALS['conf']['shares']['source']), diff --git a/turba/lib/Form/DeleteAddressBook.php b/turba/lib/Form/DeleteAddressBook.php index e01c36e9d..3075da208 100644 --- a/turba/lib/Form/DeleteAddressBook.php +++ b/turba/lib/Form/DeleteAddressBook.php @@ -35,6 +35,8 @@ class Turba_Form_DeleteAddressBook extends Horde_Form /** * @TODO Remove share from 'addressbooks' pref + * + * @throws Turba_Exception */ function execute() { @@ -45,18 +47,15 @@ class Turba_Form_DeleteAddressBook extends Horde_Form if (!$GLOBALS['registry']->getAuth() || $this->_addressbook->get('owner') != $GLOBALS['registry']->getAuth()) { - return PEAR::raiseError(_("You do not have permissions to delete this address book.")); + throw new Turba_Exception(_("You do not have permissions to delete this address book.")); } - $driver = &Turba_Driver::singleton($this->_addressbook->getName()); - if (is_a($driver, 'PEAR_Error')) { - return $driver; - } + $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($this->_addressbook->getName()); // We have a Turba_Driver, try to delete the address book. $result = $driver->deleteAll(); if (is_a($result, 'PEAR_Error')) { - return $result; + throw new Turba_Exception($result); } // Address book successfully deleted from backend, remove the diff --git a/turba/lib/Injector/Binder/Driver.php b/turba/lib/Injector/Binder/Driver.php new file mode 100644 index 000000000..51e1f3f27 --- /dev/null +++ b/turba/lib/Injector/Binder/Driver.php @@ -0,0 +1,31 @@ + + * @category Horde + * @license http://www.horde.org/licenses/asl.html ASL + * @package Turba + */ +class Turba_Injector_Binder_Driver implements Horde_Injector_Binder +{ + /** + */ + public function create(Horde_Injector $injector) + { + return new Turba_Injector_Factory_Driver($injector); + } + + /** + */ + public function equals(Horde_Injector_Binder $binder) + { + return false; + } + +} diff --git a/turba/lib/Injector/Factory/Driver.php b/turba/lib/Injector/Factory/Driver.php new file mode 100644 index 000000000..43dcf66ef --- /dev/null +++ b/turba/lib/Injector/Factory/Driver.php @@ -0,0 +1,85 @@ + + * @category Horde + * @license http://www.horde.org/licenses/apl.html APL + * @link http://pear.horde.org/index.php?package=Turba + * @package Turba + */ + +/** + * A Horde_Injector:: based Turba_Driver:: factory. + * + * Copyright 2010 The Horde Project (http://www.horde.org/) + * + * See the enclosed file COPYING for license information (APL). If you + * did not receive this file, see http://www.horde.org/licenses/apl.html. + * + * @author Michael Slusarz + * @category Horde + * @license http://www.horde.org/licenses/apl.html APL + * @link http://pear.horde.org/index.php?package=Turba + * @package Turba + */ +class Turba_Injector_Factory_Driver +{ + /** + * Instances. + * + * @var array + */ + private $_instances = array(); + + /** + * The injector. + * + * @var Horde_Injector + */ + private $_injector; + + /** + * Constructor. + * + * @param Horde_Injector $injector The injector to use. + */ + public function __construct(Horde_Injector $injector) + { + $this->_injector = $injector; + } + + /** + * Return the Turba_Driver:: instance. + * + * @param mixed $name Either a string containing the internal name of this + * source, or a config array describing the source. + * + * @return Turba_Driver The singleton instance. + * @throws Turba_Exception + */ + public function getDriver($name) + { + if (is_array($name)) { + $key = md5(serialize($name)); + $srcName = ''; + $srcConfig = $name; + } else { + $key = $name; + $srcName = $name; + if (empty($GLOBALS['cfgSources'][$name])) { + throw new Turba_Exception(sprintf(_("The address book \"%s\" does not exist."), $name)); + } + $srcConfig = $GLOBALS['cfgSources'][$name]; + } + + if (!isset($this->_instances[$key])) { + $this->_instances[$key] = Turba_Driver::factory($srcName, $srcConfig); + } + + return $this->_instances[$key]; + } + +} diff --git a/turba/lib/List.php b/turba/lib/List.php index 31be0ffa0..4b616e783 100644 --- a/turba/lib/List.php +++ b/turba/lib/List.php @@ -37,10 +37,10 @@ class Turba_List implements Countable { foreach ($ids as $value) { list($source, $key) = explode(':', $value); - $driver = Turba_Driver::singleton($source); - if ($driver instanceof Turba_Driver) { + try { + $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($source); $this->insert($driver->getObject($key)); - } + } catch (Turba_Exception $e) {} } } diff --git a/turba/lib/LoginTasks/SystemTask/UpgradeLists.php b/turba/lib/LoginTasks/SystemTask/UpgradeLists.php index 45bad2f0f..6a298680d 100644 --- a/turba/lib/LoginTasks/SystemTask/UpgradeLists.php +++ b/turba/lib/LoginTasks/SystemTask/UpgradeLists.php @@ -31,7 +31,7 @@ class Turba_LoginTasks_SystemTask_UpgradeLists extends Horde_LoginTasks_SystemTa /** * Perform all functions for this task. * - * @return mixed True | PEAR_Error + * @return boolean Success. */ public function execute() { @@ -39,10 +39,15 @@ class Turba_LoginTasks_SystemTask_UpgradeLists extends Horde_LoginTasks_SystemTa $criteria = array('__type' => 'Group'); $sources = array_keys($GLOBALS['cfgSources']); foreach ($sources as $sourcekey) { - $driver = Turba_Driver::singleton($sourcekey); + try { + $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($sourcekey); + } catch (Turba_Exception $e) { + return false; + } + $lists = $driver->search($criteria); - if (is_a($lists, 'PEAR_Error')) { - return $lists; + if ($lists instanceof PEAR_Error) { + return false; } for ($j = 0, $cnt = count($lists); $j < $cnt; ++$j) { diff --git a/turba/lib/Object/Group.php b/turba/lib/Object/Group.php index 4e9b914ff..359425329 100644 --- a/turba/lib/Object/Group.php +++ b/turba/lib/Object/Group.php @@ -48,6 +48,8 @@ class Turba_Object_Group extends Turba_Object { * * @param string $contactId The id of the contact to add. * @param string $sourceId The source $contactId is from. + * + * @throws Turba_Exception */ function addMember($contactId, $sourceId = null) { @@ -58,24 +60,21 @@ class Turba_Object_Group extends Turba_Object { // Can't add a group to itself. if ($contactId == $this->attributes['__key']) { - return PEAR::raiseError(_("Can't add a group to itself.")); + throw new Turba_Exception(_("Can't add a group to itself.")); } // Try to find the contact being added. if ($sourceId == $this->getSource()) { $contact = $this->driver->getObject($contactId); } else { - $driver = Turba_Driver::singleton($sourceId); - if (is_a($driver, 'PEAR_Error')) { - return $driver; - } + $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($sourceId); $contact = $driver->getObject($contactId); } // Bail out if the contact being added doesn't exist or can't // be retrieved. if (is_a($contact, 'PEAR_Error')) { - return $contact; + throw new Turba_Exception($contact); } // Explode members. @@ -174,16 +173,18 @@ class Turba_Object_Group extends Turba_Object { list($owner, $contactId) = explode(':', $contactId, 2); $sourceId .= ':' . $owner; } - $driver = Turba_Driver::singleton($sourceId); - if (!is_a($driver, 'PEAR_Error')) { - $contact = $driver->getObject($contactId); - if (is_a($contact, 'PEAR_Error')) { - // Remove the contact if it no longer exists - $this->removeMember($member); - $modified = true; - continue; - } - } else { + + try { + $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($sourceId); + } catch (Turba_Exception $e) { + continue; + } + + $contact = $driver->getObject($contactId); + if (is_a($contact, 'PEAR_Error')) { + // Remove the contact if it no longer exists + $this->removeMember($member); + $modified = true; continue; } } diff --git a/turba/lib/Turba.php b/turba/lib/Turba.php index 63faa61f6..e23322920 100644 --- a/turba/lib/Turba.php +++ b/turba/lib/Turba.php @@ -342,9 +342,10 @@ class Turba { $out = array(); foreach ($in as $sourceId => $source) { - $driver = Turba_Driver::singleton($sourceId); - if (is_a($driver, 'PEAR_Error')) { - Horde::logMessage(sprintf("Could not instantiate the %s source: %s", $sourceId, $driver->getMessage()), 'ERR'); + try { + $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($sourceId); + } catch (Turba_Exception $e) { + Horde::logMessage($e, 'ERR'); continue; } @@ -406,14 +407,16 @@ class Turba { // Default share? if (empty($defaults[$params['source']])) { - $driver = Turba_Driver::singleton($params['source']); - if (!is_a($driver, 'PEAR_Error') && $driver->hasPermission(Horde_Perms::EDIT)) { - $defaults[$params['source']] = - $driver->checkDefaultShare( + try { + $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($params['source']); + if ($driver->hasPermission(Horde_Perms::EDIT)) { + $defaults[$params['source']] = $driver->checkDefaultShare( $shares[$name], - $sources[$params['source']]); - } else { - $notification->push($driver, 'horde.error'); + $sources[$params['source']] + ); + } + } catch (Turba_Exception $e) { + $notification->push($e, 'horde.error'); } } @@ -440,26 +443,33 @@ class Turba { } if ($GLOBALS['registry']->getAuth() && empty($defaults[$source])) { // User's default share is missing. - $driver = Turba_Driver::singleton($source); - if (!($driver instanceof PEAR_Error)) { - $sourceKey = md5(mt_rand()); - try { - $share = $driver->createShare( - $sourceKey, - array('params' => array('source' => $source, - 'default' => true, - 'name' => $GLOBALS['registry']->getAuth()))); - } catch (Horde_Share_Exception $e) { - Horde::logMessage($e, 'ERR'); - continue; - } - - $source_config = $sources[$source]; - $source_config['params']['share'] = $share; - $newSources[$sourceKey] = $source_config; - } else { + try { + $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($sourceId); + } catch (Turba_Exception $e) { $notification->push($driver, 'horde.error'); + continue; } + + $sourceKey = md5(mt_rand()); + try { + $share = $driver->createShare( + $sourceKey, + array( + 'params' => array( + 'source' => $source, + 'default' => true, + 'name' => $GLOBALS['registry']->getAuth() + ) + ) + ); + } catch (Horde_Share_Exception $e) { + Horde::logMessage($e, 'ERR'); + continue; + } + + $source_config = $sources[$source]; + $source_config['params']['share'] = $share; + $newSources[$sourceKey] = $source_config; } } diff --git a/turba/lib/View/Browse.php b/turba/lib/View/Browse.php index 9fda375c1..4e43386dc 100644 --- a/turba/lib/View/Browse.php +++ b/turba/lib/View/Browse.php @@ -68,9 +68,10 @@ class Turba_View_Browse { if (!$browse_source_count && $vars->get('key') != '**search') { $notification->push(_("There are no browseable address books."), 'horde.warning'); } else { - $driver = Turba_Driver::singleton($source); - if (is_a($driver, 'PEAR_Error')) { - $notification->push(sprintf(_("Failed to access the address book: %s"), $driver->getMessage()), 'horde.error'); + try { + $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($source); + } catch (Turba_Exception $e) { + $notification->push($e, 'horde.error'); unset($driver); } } @@ -136,9 +137,11 @@ class Turba_View_Browse { // If we have data, try loading the target address book driver. $targetSource = $vars->get('targetAddressbook'); - $targetDriver = Turba_Driver::singleton($targetSource); - if (is_a($targetDriver, 'PEAR_Error')) { - $notification->push(sprintf(_("Failed to access the address book: %s"), $targetDriver->getMessage()), 'horde.error'); + + try { + $targetDriver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($targetSource); + } catch (Turba_Exception $e) { + $notification->push($e, 'horde.error'); break; } @@ -165,9 +168,10 @@ class Turba_View_Browse { } // Try and load the driver for the source. - $sourceDriver = Turba_Driver::singleton($objectSource); - if (is_a($sourceDriver, 'PEAR_Error')) { - $notification->push(sprintf(_("Failed to access the address book: %s"), $sourceDriver->getMessage()), 'horde.error'); + try { + $sourceDriver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($objectSource); + } catch (Turba_Exception $e) { + $notification->push($e, 'horde.error'); continue; } @@ -243,9 +247,11 @@ class Turba_View_Browse { if (!isset($cfgSources[$targetSource])) { break; } - $targetDriver = Turba_Driver::singleton($targetSource); - if ($targetDriver instanceof PEAR_Error) { - $notification->push(sprintf_("Could not load driver for %s", $targetSource), 'horde.error'); + + try { + $targetDriver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($targetSource); + } catch (Turba_Exception $e) { + $notification->push($e, 'horde.error'); break; } $target = &$targetDriver->getObject($targetKey); @@ -255,9 +261,10 @@ class Turba_View_Browse { } } else { $targetSource = $vars->get('targetAddressbook'); - $targetDriver = Turba_Driver::singleton($targetSource); - if ($targetDriver instanceof PEAR_Error) { - $notification->push(sprintf_("Could not load driver for %s", $targetSource), 'horde.error'); + try { + $targetDriver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($targetSource); + } catch (Turba_Exception $e) { + $notification->push($e, 'horde.error'); break; } } diff --git a/turba/lib/View/List.php b/turba/lib/View/List.php index 7807fc2a4..5611d0fa5 100644 --- a/turba/lib/View/List.php +++ b/turba/lib/View/List.php @@ -145,22 +145,12 @@ class Turba_View_List implements Countable { global $prefs, $default_source, $copymove_source_options; - $driver = Turba_Driver::singleton($default_source); - $hasDelete = false; - $hasEdit = false; - $hasExport = false; - if (!is_a($driver, 'PEAR_Error')) { - if ($driver->hasPermission(Horde_Perms::DELETE)) { - $hasDelete = true; - } - if ($driver->hasPermission(Horde_Perms::EDIT)) { - $hasEdit = true; - } - if ($GLOBALS['conf']['menu']['import_export'] - && !empty($GLOBALS['cfgSources'][$default_source]['export'])) { - $hasExport = true; - } - } + $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($default_source); + + $hasDelete = $driver->hasPermission(Horde_Perms::DELETE); + $hasEdit = $driver->hasPermission(Horde_Perms::EDIT); + $hasExport = ($GLOBALS['conf']['menu']['import_export'] && !empty($GLOBALS['cfgSources'][$default_source]['export'])); + list($addToList, $addToListSources) = $this->getAddSources(); $viewurl = Horde::applicationUrl('browse.php')->add(array( @@ -404,7 +394,7 @@ class Turba_View_List implements Countable 'name' => '  ' . htmlspecialchars($srcConfig['title']), 'source' => htmlspecialchars($src)); - $srcDriver = &Turba_Driver::singleton($src); + $srcDriver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($src); $listList = $srcDriver->search(array('__type' => 'Group'), array(array('field' => 'name', 'ascending' => true)), diff --git a/turba/lib/tests/KolabTest.php b/turba/lib/tests/KolabTest.php index a1fb2f51b..0635f4846 100644 --- a/turba/lib/tests/KolabTest.php +++ b/turba/lib/tests/KolabTest.php @@ -47,14 +47,14 @@ class Turba_KolabTest extends Turba_KolabTestBase { $this->_kolab->_storage->save($object); // Check that the driver can be created - $turba = Turba_Driver::singleton('wrobel@example.org'); - $this->assertNoError($turba); + $turba = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver('wrobel@example.org'); + //$this->assertNoError($turba); $result = $turba->search(array(), array('last-name')); $this->assertNoError($result); $this->assertEquals(2, count($result)); - $turba = Turba_Driver::singleton('INBOX%2Ftest2'); + $turba = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver('INBOX%2Ftest2'); $result = $turba->search(array(), array('last-name')); $this->assertEquals(0, count($result)); @@ -75,8 +75,8 @@ class Turba_KolabTest extends Turba_KolabTestBase { ); // Save the contact - $turba = Turba_Driver::singleton('wrobel@example.org'); - $this->assertNoError($turba); + $turba = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver('wrobel@example.org'); + //$this->assertNoError($turba); $this->assertNoError($turba->_add($object)); @@ -102,8 +102,8 @@ class Turba_KolabTest extends Turba_KolabTestBase { ); // Save the contact - $turba = Turba_Driver::singleton('wrobel@example.org'); - $this->assertNoError($turba); + $turba = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver('wrobel@example.org'); + //$this->assertNoError($turba); $this->assertNoError($turba->_add($object)); diff --git a/turba/merge.php b/turba/merge.php index 10a285247..ece63441e 100644 --- a/turba/merge.php +++ b/turba/merge.php @@ -16,7 +16,7 @@ Horde_Registry::appInit('turba'); $source = Horde_Util::getFormData('source'); $key = Horde_Util::getFormData('key'); $mergeInto = Horde_Util::getFormData('merge_into'); -$driver = Turba_Driver::singleton($source); +$driver = $injector->getInstance('Turba_Driver')->getDriver($source); if ($url = Horde_Util::getFormData('url')) { $url = new Horde_Url($url, true)->unique(); diff --git a/turba/minisearch.php b/turba/minisearch.php index 542a482a6..2e43101b9 100644 --- a/turba/minisearch.php +++ b/turba/minisearch.php @@ -21,11 +21,16 @@ $source = Horde_Util::getFormData('source', Turba::getDefaultAddressBook()); // Do the search if we have one. if (!is_null($search)) { - $driver = Turba_Driver::singleton($source); - if (!is_a($driver, 'PEAR_Error')) { + try { + $driver = $injector->getInstance('Turba_Driver')->getDriver($source); + } catch (Turba_Exception $e) { + $driver = null; + } + + if ($driver) { $criteria['name'] = trim($search); $res = $driver->search($criteria); - if (is_a($res, 'Turba_List')) { + if ($res instanceof Turba_List) { while ($ob = $res->next()) { if ($ob->isGroup()) { continue; diff --git a/turba/scripts/import_squirrelmail_file_abook.php b/turba/scripts/import_squirrelmail_file_abook.php index 789e398aa..f8dc02372 100755 --- a/turba/scripts/import_squirrelmail_file_abook.php +++ b/turba/scripts/import_squirrelmail_file_abook.php @@ -84,9 +84,10 @@ foreach($files as $file) { } // Initiate driver - $driver = &Turba_Driver::singleton($import_source); - if (is_a($driver, 'PEAR_Error')) { - PEAR::raiseError(sprintf(_("Connection failed: %s"), $driver->getMessage()), 'horde.error', null, null, $import_source); + try { + $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($import_source); + } catch (Turba_Exception $e) { + PEAR::raiseError(sprintf(_("Connection failed: %s"), $e->getMessage()), 'horde.error', null, null, $import_source); continue; } diff --git a/turba/scripts/import_squirrelmail_sql_abook.php b/turba/scripts/import_squirrelmail_sql_abook.php index dd3668337..b7882faef 100755 --- a/turba/scripts/import_squirrelmail_sql_abook.php +++ b/turba/scripts/import_squirrelmail_sql_abook.php @@ -93,9 +93,10 @@ while ($row = $handle->fetchRow(DB_FETCHMODE_ASSOC)) { } // Initiate driver - $driver = &Turba_Driver::singleton($import_source); - if (is_a($driver, 'PEAR_Error')) { - $cli->message(' ' . sprintf(_("Connection failed: %s"), $driver->getMessage()), 'cli.error'); + try { + $driver = $injector->getInstance('Turba_Driver')->getDriver($import_source); + } catch (Turba_Exception $e) { + $cli->message(' ' . sprintf(_("Connection failed: %s"), $e->getMessage()), 'cli.error'); continue; } } diff --git a/turba/scripts/upgrades/public_to_horde_share.php b/turba/scripts/upgrades/public_to_horde_share.php index 7771b3feb..8ab075f3d 100755 --- a/turba/scripts/upgrades/public_to_horde_share.php +++ b/turba/scripts/upgrades/public_to_horde_share.php @@ -76,12 +76,9 @@ $share->save(); $CLI->message('Created new Horde_Share object for the shared address book.', 'cli.success'); // Share created, now get a Turba_Driver and make the changes. -$driver = &Turba_Driver::singleton($sourceKey); -if (is_a($driver, 'PEAR_Error')) { - var_dump($driver); - exit; -} -$db = & $driver->_db; +$driver = $injector->getInstance('Turba_Driver')->getDriver($sourceKey); + +$db = &$driver->_db; if (is_a($db, 'PEAR_Error')) { var_dump($db); } diff --git a/turba/search.php b/turba/search.php index 4e3608e6b..cb84fd2f1 100644 --- a/turba/search.php +++ b/turba/search.php @@ -106,11 +106,16 @@ if (!isset($addressBooks[$source])) { $criteria = Horde_Util::getFormData('criteria'); $val = Horde_Util::getFormData('val'); $action = Horde_Util::getFormData('actionID'); -$driver = Turba_Driver::singleton($source); -if (is_a($driver, 'PEAR_Error')) { - $notification->push(sprintf(_("Failed to access the address book: %s"), $driver->getMessage()), 'horde.error'); + +try { + $driver = $injector->getInstance('Turba_Driver')->getDriver($source); +} catch (Turba_Exception $e) { + $notification->push($e, 'horde.error'); + $driver = null; $map = array(); -} else { +} + +if ($driver) { $map = $driver->getCriteria(); if ($_SESSION['turba']['search_mode'] == 'advanced') { $criteria = array(); diff --git a/turba/vcard.php b/turba/vcard.php index e2168da44..745268c0f 100644 --- a/turba/vcard.php +++ b/turba/vcard.php @@ -19,7 +19,7 @@ if (!isset($cfgSources[$source])) { Horde::applicationUrl($prefs->getValue('initial_page'), true)->redirect(); } -$driver = Turba_Driver::singleton($source); +$driver = $injector->getInstance('Turba_Driver')->getDriver($source); /* Set the contact from the key requested. */ $key = Horde_Util::getFormData('key'); diff --git a/turba/view.php b/turba/view.php index 25390cc9e..51f039b28 100644 --- a/turba/view.php +++ b/turba/view.php @@ -27,7 +27,8 @@ $type = Horde_Util::getFormData('type'); if (!isset($cfgSources[$source])) { throw new Turba_Exception(_("The contact you requested does not exist.")); } -$driver = Turba_Driver::singleton($source); + +$driver = $injector->getInstance('Turba_Driver')->getDriver($source); $object = $driver->getObject($key); if (is_a($object, 'PEAR_Error')) { throw new Turba_Exception($object);