From b3b443e8bb9b41be970f9d058143f4c8ba953b9a Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Wed, 25 Aug 2010 22:00:50 -0600 Subject: [PATCH] Update encode/decode attribute value hook in turba --- turba/config/hooks.php.dist | 49 +++++++++++++++++++++++++++++---------------- turba/docs/UPGRADING | 10 +++++++++ turba/lib/Object.php | 47 ++++++++----------------------------------- 3 files changed, 50 insertions(+), 56 deletions(-) diff --git a/turba/config/hooks.php.dist b/turba/config/hooks.php.dist index 7cb0cf6a9..4902e21da 100644 --- a/turba/config/hooks.php.dist +++ b/turba/config/hooks.php.dist @@ -28,13 +28,13 @@ class Turba_Hooks // if (!$username || empty($_SESSION['turba']['has_share'])) { // return; // } -// +// // require TURBA_BASE . '/config/sources.php'; // $shares = Turba::listShares(true); // if (is_a($shares, 'PEAR_Error')) { // return; // } -// +// // foreach ($shares as $uid => $share) { // $params = @unserialize($share->get('params')); // if (empty($params['source'])) { @@ -54,33 +54,48 @@ class Turba_Hooks /** * Called when we store a value. * - * Passwords should be MD5 encoded, but not displayed. + * @param string $attribute Attribute name. + * @param string $new New value. + * @param string $old Old value. + * @param Turba_Object $contact Contact object. * - * @todo Make this a generic encode() method. + * @return string The encoded value. */ -// public function encode_password($new_password, $old_password, &$contact) +// public function encode_attribute($attribute, $new, $old, $contact) // { -// if (is_null($new_password) || $new_password == '' || -// $new_password == '[Not Displayed]') { -// return $old_password; -// } else { -// return md5($new_password); +// switch ($attribute) { +// case 'password': +// /* Passwords should be MD5 encoded, but not displayed. */ +// return (is_null($new) || ($new == '') || ($new == '[Not Displayed'])) +// ? $old +// : hash('md5', $new); // } +// +// return $new; // } /** * Called when we display a value. * - * Passwords should be MD5 encoded, but not displayed. + * @param string $attribute Attribute name. + * @param string $new New value. + * @param string $old Old value. + * @param Turba_Object $contact Contact object. * - * @todo Make this a generic decode() method. + * @return string The decoded value. + * @throws Turba_Exception Thrown if there is nothing to decode. */ -// public function decode_password($password, &$contact) +// public function decode_attribute($attribute, $value, $contact) // { -// if (strstr($_SERVER['PHP_SELF'], 'editobject')) { -// return null; -// } else { -// return '[Not Displayed]'; +// switch ($attribute) { +// case 'password': +// /* Passwords should be MD5 encoded, but not displayed. */ +// return (strstr($_SERVER['PHP_SELF'], 'editobject')) +// ? null +// : '[Not Displayed]'; // } +// +// throw new Turba_Exception('No decode handler.'); // } + } diff --git a/turba/docs/UPGRADING b/turba/docs/UPGRADING index 6d356897a..4cb2a7501 100644 --- a/turba/docs/UPGRADING +++ b/turba/docs/UPGRADING @@ -10,6 +10,16 @@ your existing data before running any of the steps described below. You can't use the updated data with your old Turba version anymore. +Upgrading Turba from 2.x to 3.x +=============================== + +The ``_turba_hook_encode_{attribute}`` hook has been moved to the +'encode_attribute' hook. + +The ``_turba_hook_decode_{attribute}`` hook has been moved to the +'decode_attribute' hook. + + Upgrading Turba from 2.3.x to 2.3.3 =================================== diff --git a/turba/lib/Object.php b/turba/lib/Object.php index 68c3dc47e..c54373b61 100644 --- a/turba/lib/Object.php +++ b/turba/lib/Object.php @@ -90,27 +90,11 @@ class Turba_Object { */ function getValue($attribute) { - /* Cache hooks to avoid multiple file_exists() calls. */ - static $hooks; - if (!isset($hooks)) { - $hooks = array(); + if (isset($this->attributes[$attribute])) { try { - Horde::loadConfiguration('hooks.php', null, 'turba'); - } catch (Horde_Exception $e) {} - } - if (!isset($hooks[$attribute])) { - $function = '_turba_hook_decode_' . $attribute; - if (function_exists($function)) { - $hooks[$attribute] = $function; - } else { - $hooks[$attribute] = false; - } - } - - if (isset($this->attributes[$attribute]) && - !empty($hooks[$attribute])) { - return call_user_func_array($hooks[$attribute], - array($this->attributes[$attribute], &$this)); + return Horde::callHook('decode_attribute', array($attribute, $this->attributes[$attribute], $this), 'turba'); + } catch (Horde_Exception_HookNotSet $e) {} + } catch (Turba_Exception $e) {} } if (isset($this->driver->map[$attribute]) && @@ -141,25 +125,10 @@ class Turba_Object { */ function setValue($attribute, $value) { - /* Cache hooks to avoid multiple file_exists() calls. */ - static $hooks; - if (!isset($hooks)) { - $hooks = array(); - try { - Horde::loadConfiguration('hooks.php', null, 'turba'); - } catch (Horde_Exception $e) {} - } - if (!isset($hooks[$attribute])) { - $function = '_turba_hook_encode_' . $attribute; - if (function_exists($function)) { - $hooks[$attribute] = $function; - } else { - $hooks[$attribute] = false; - } - } - if ($hooks[$attribute]) { - $value = call_user_func_array($hooks[$attribute], array($value, $this->attributes[$attribute], &$this)); - } + try { + $value = Horde::callHook('encode_attribute', array($attribute, $value, $this->attributes[$attribute], $this), 'turba'); + } catch (Horde_Exception_HookNotSet $e) {} + } catch (Turba_Exception $e) {} if (isset($this->driver->map[$attribute]) && is_array($this->driver->map[$attribute]) && -- 2.11.0