Throw Turba_Exceptions if driver init fails.
/* 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 &&
}
/* 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();
}
$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;
}
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;
}
/* 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();
$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);
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');
$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);
@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;
}
// 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) {
}
// 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) {
}
// 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) {
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()));
}
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,
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,
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"));
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;
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');
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."));
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)) {
}
// 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;
}
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])
}
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) {
}
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) {
$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)) {
$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);
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"));
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)) {
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;
}
*
* 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. */
*/
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';
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');
}
}
}
/* 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;
}
}
/**
- * 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. */
$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'];
}
/**
- * 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;
}
/**
private $_facebook;
/**
- */
- function _init()
- {
- return true;
- }
-
- /**
* Checks if the current user has the requested permissions on this
* source.
*
class Turba_Driver_Favourites extends Turba_Driver
{
/**
- */
- function _init()
- {
- return true;
- }
-
- /**
* Checks if the current user has the requested permissions on this
* source.
*
}
/**
- * 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.
*
/**
* 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;
}
/**
/**
* 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);
}
/**
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. */
}
/* 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)));
+ }
}
/**
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.
*
}
/**
- * 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();
}
*/
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;
}
/**
}
/**
+ * @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';
}
/**
$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']),
/**
* @TODO Remove share from 'addressbooks' pref
+ *
+ * @throws Turba_Exception
*/
function execute()
{
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
--- /dev/null
+<?php
+/**
+ * Binder for Turba_Driver::.
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (ASL). If you
+ * did not receive this file, see http://www.horde.org/licenses/asl.html.
+ *
+ * @author Michael Slusarz <slusarz@horde.org>
+ * @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;
+ }
+
+}
--- /dev/null
+<?php
+/**
+ * A Horde_Injector:: based Turba_Driver:: factory.
+ *
+ * PHP version 5
+ *
+ * @author Michael Slusarz <slusarz@horde.org>
+ * @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 <slusarz@horde.org>
+ * @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];
+ }
+
+}
{
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) {}
}
}
/**
* Perform all functions for this task.
*
- * @return mixed True | PEAR_Error
+ * @return boolean Success.
*/
public function execute()
{
$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) {
*
* @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)
{
// 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.
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;
}
}
$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;
}
// 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');
}
}
}
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;
}
}
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);
}
}
// 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;
}
}
// 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;
}
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);
}
} 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;
}
}
{
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(
'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)),
$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));
);
// 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));
);
// 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));
$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();
// 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;
}
// 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;
}
}
// 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;
}
}
$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);
}
$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();
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');
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);