From 4a9fc60dcb0286d04378fcf14b1ba4fcba6918d1 Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Mon, 2 Aug 2010 23:18:04 -0600 Subject: [PATCH] Remove horde/Core dependency in Horde_Prefs_Identity --- framework/Core/lib/Horde.php | 4 + framework/Core/lib/Horde/Core/Factory/Identity.php | 2 +- framework/Core/lib/Horde/Core/Prefs/Identity.php | 115 +++++++++++++ framework/Core/lib/Horde/Core/Prefs/Ui.php | 5 +- framework/Core/package.xml | 2 + framework/Prefs/lib/Horde/Prefs/Identity.php | 177 ++++++--------------- framework/Prefs/package.xml | 8 +- horde/services/confirm.php | 12 +- imp/lib/Prefs/Identity.php | 14 +- 9 files changed, 186 insertions(+), 153 deletions(-) create mode 100644 framework/Core/lib/Horde/Core/Prefs/Identity.php diff --git a/framework/Core/lib/Horde.php b/framework/Core/lib/Horde.php index f67df8c6e..4c521c0ea 100644 --- a/framework/Core/lib/Horde.php +++ b/framework/Core/lib/Horde.php @@ -445,6 +445,7 @@ HTML; * 'ajax' * 'cache' * 'download' + * 'emailconfirm' * 'go' * 'help' * 'imple' @@ -475,6 +476,9 @@ HTML; return self::url($webroot . '/services/download/') ->add('module', $app); + case 'emailconfirm': + return self::url($webroot . '/services/confirm.php'); + case 'go': return self::url($webroot . '/services/go.php'); diff --git a/framework/Core/lib/Horde/Core/Factory/Identity.php b/framework/Core/lib/Horde/Core/Factory/Identity.php index e52f6441b..fd4311e57 100644 --- a/framework/Core/lib/Horde/Core/Factory/Identity.php +++ b/framework/Core/lib/Horde/Core/Factory/Identity.php @@ -66,7 +66,7 @@ class Horde_Core_Factory_Identity global $injector, $prefs, $registry; $class = empty($driver) - ? 'Horde_Prefs_Identity' + ? 'Horde_Core_Prefs_Identity' : Horde_String::ucfirst($driver) . '_Prefs_Identity'; $key = $class . '|' . $user; diff --git a/framework/Core/lib/Horde/Core/Prefs/Identity.php b/framework/Core/lib/Horde/Core/Prefs/Identity.php new file mode 100644 index 000000000..92a0d77a0 --- /dev/null +++ b/framework/Core/lib/Horde/Core/Prefs/Identity.php @@ -0,0 +1,115 @@ + + * @author Michael Slusarz + * @category Horde + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @package Core + */ +class Horde_Core_Prefs_Identity extends Horde_Prefs_Identity +{ + /** + * Sends a message to an email address supposed to be added to the + * identity. + * A message is send to this address containing a link to confirm that the + * address really belongs to that user. + * + * @param integer $id The identity's ID. + * @param string $old_addr The old From: address. + * + * @throws Horde_Mime_Exception + */ + public function verifyIdentity($id, $old_addr) + { + global $conf; + + $hash = strval(new Horde_Support_Randomid()); + + if (!($pref = @unserialize($this->_prefs->getValue('confirm_email')))) { + $pref = array(); + } + $pref[$hash] = $this->get($id); + $this->_prefs->setValue('confirm_email', serialize($pref)); + + $new_addr = $this->getValue($this->_prefnames['from_addr'], $id); + $confirm = Horde::getServiceLink('emailconfirm')->add('h', $hash)->setRaw(true); + $message = sprintf(_("You have requested to add the email address \"%s\" to the list of your personal email addresses.\n\nGo to the following link to confirm that this is really your address:\n%s\n\nIf you don't know what this message means, you can delete it."), + $new_addr, + $confirm); + + $msg_headers = new Horde_Mime_Headers(); + $msg_headers->addMessageIdHeader(); + $msg_headers->addUserAgentHeader(); + $msg_headers->addHeader('Date', date('r')); + $msg_headers->addHeader('To', $new_addr); + $msg_headers->addHeader('From', $old_addr); + $msg_headers->addHeader('Subject', _("Confirm new email address")); + + $body = new Horde_Mime_Part(); + $body->setType('text/plain'); + $body->setContents(Horde_String::wrap($message, 76, "\n")); + $body->setCharset($GLOBALS['registry']->getCharset()); + + $body->send($new_addr, $msg_headers, $GLOBALS['injector']->getInstance('Horde_Mail')); + + $GLOBALS['notification']->push(sprintf(_("A message has been sent to \"%s\" to verify that this is really your address. The new email address is activated as soon as you confirm this message."), $new_addr), 'horde.message'); + } + + /** + * Checks whether an identity confirmation is valid, and adds the + * validated identity. + * + * @param string $hash The saved hash of the identity being validated. + */ + public function confirmIdentity($hash) + { + global $notification; + + $confirm = $this->_prefs->getValue('confirm_email'); + if (empty($confirm)) { + $notification->push(_("There are no email addresses to confirm."), 'horde.message'); + return; + } + + $confirm = @unserialize($confirm); + if (empty($confirm)) { + $notification->push(_("There are no email addresses to confirm."), 'horde.message'); + return; + } elseif (!isset($confirm[$hash])) { + $notifcation->push(_("Email addresses to confirm not found."), 'horde.message'); + return; + } + + $identity = $confirm[$hash]; + $id = array_search($identity['id'], $this->getAll($this->_prefnames['id'])); + if ($id === false) { + /* Adding a new identity. */ + $verified = array(); + foreach ($identity as $key => $value) { + if (!$this->_prefs->isLocked($key)) { + $verified[$key] = $value; + } + } + $this->add($verified); + } else { + /* Updating an existing identity. */ + foreach ($identity as $key => $value) { + $this->setValue($key, $value, $id); + } + } + $this->save(); + unset($confirm[$hash]); + $this->_prefs->setValue('confirm_email', serialize($confirm)); + + $notification->push(sprintf(_("The email address %s has been added to your identities. You can close this window now."), $verified[$this->_prefnames['from_addr']]), 'horde.success'); + } + +} diff --git a/framework/Core/lib/Horde/Core/Prefs/Ui.php b/framework/Core/lib/Horde/Core/Prefs/Ui.php index 87b9d118e..3f34b9945 100644 --- a/framework/Core/lib/Horde/Core/Prefs/Ui.php +++ b/framework/Core/lib/Horde/Core/Prefs/Ui.php @@ -837,10 +837,7 @@ class Horde_Core_Prefs_Ui ($current_from != $new_from) && !in_array($new_from, $from_addresses)) { try { - $result = $identity->verifyIdentity($id, empty($current_from) ? $new_from : $current_from); - if ($result instanceof Notification_Event) { - $notification->push($result, 'horde.message'); - } + $identity->verifyIdentity($id, empty($current_from) ? $new_from : $current_from); } catch (Horde_Exception $e) { $notification->push(_("The new from address can't be verified, try again later: ") . $e->getMessage(), 'horde.error'); Horde::logMessage($e, 'ERR'); diff --git a/framework/Core/package.xml b/framework/Core/package.xml index 1341a406e..0c864b03a 100644 --- a/framework/Core/package.xml +++ b/framework/Core/package.xml @@ -177,6 +177,7 @@ Application Framework. + @@ -444,6 +445,7 @@ Application Framework. + diff --git a/framework/Prefs/lib/Horde/Prefs/Identity.php b/framework/Prefs/lib/Horde/Prefs/Identity.php index a5c2abe7e..8dbb7c395 100644 --- a/framework/Prefs/lib/Horde/Prefs/Identity.php +++ b/framework/Prefs/lib/Horde/Prefs/Identity.php @@ -1,10 +1,6 @@ * @category Horde + * @license http://www.fsf.org/copyleft/lgpl.html LGPL * @package Prefs */ class Horde_Prefs_Identity @@ -41,11 +38,18 @@ class Horde_Prefs_Identity protected $_user = null; /** - * Array containing all of the properties in this identity. + * Preference names. * * @var array */ - protected $_properties = array('id', 'fullname', 'from_addr'); + protected $_prefnames = array( + 'default_identity' => 'default_identity', + 'from_addr' => 'from_addr', + 'fullname' => 'fullname', + 'id' => 'id', + 'identities' => 'identities', + 'properties' => array('id', 'fullname', 'from_addr') + ); /** * The prefs object that this Identity points to. @@ -59,22 +63,39 @@ class Horde_Prefs_Identity * * @param array $params Parameters: *
+     * 'default_identity' - (string) The preference name for the default
+     *                      identity.
+     *                      DEFAULT: 'default_identity'
+     * 'from_addr' - (string) The preference name for the user's from e-mail
+     *               address.
+     *               DEFAULT: 'from_addr'
+     * 'fullname' - (string) The preference name for the user's full name.
+     *              DEFAULT: 'fullname'
+     * 'id' - (string) The preference name for the identity name.
+     *        DEFAULT: 'id'
+     * 'identities' - (string) The preference name for the identity store.
+     *                DEFAULT: 'identities'
      * 'prefs' - (Horde_Prefs) [REQUIRED] The prefs object to use.
+     * 'properties' - (array) The list of properties for the identity.
+     *                DEFAULT: array('from_addr', 'fullname', 'id')
      * 'user' - (string) [REQUIRED] The user whose prefs we are handling.
      * 
*/ public function __construct($params = array()) { + foreach (array_keys($this->_prefnames) as $val) { + if (isset($params[$val])) { + $this->_prefnames[$val] = $params[$val]; + } + } $this->_prefs = $params['prefs']; $this->_user = $params['user']; - if (!($this->_identities = @unserialize($this->_prefs->getValue('identities', false)))) { - $this->_identities = $this->_prefs->getDefault('identities'); - } else { - $this->_identities = $this->_prefs->convertFromDriver($this->_identities); + if (!($this->_identities = @unserialize($this->_prefs->getValue($this->_prefnames['identities'])))) { + $this->_identities = $this->_prefs->getDefault($this->_prefnames['identities']); } - $this->setDefault($this->_prefs->getValue('default_identity')); + $this->setDefault($this->_prefs->getValue($this->_prefnames['default_identity'])); } /** @@ -95,7 +116,7 @@ class Horde_Prefs_Identity $this->verify(0); } - if ($this->_prefs->isLocked('default_identity')) { + if ($this->_prefs->isLocked($this->_prefnames['default_identity'])) { foreach ($this->_properties as $key) { $value = $this->getValue($key); if (is_array($value)) { @@ -112,13 +133,8 @@ class Horde_Prefs_Identity */ public function save() { - $identities = $this->_identities; - if (is_array($identities)) { - $identities = $this->_prefs->convertToDriver($identities); - } - - $this->_prefs->setValue('identities', serialize($identities), false); - $this->_prefs->setValue('default_identity', $this->_default); + $this->_prefs->setValue($this->_prefnames['identities'], serialize($identities)); + $this->_prefs->setValue($this->_prefnames['default_identity'], $this->_default); } /** @@ -305,7 +321,7 @@ class Horde_Prefs_Identity * * @param integer $identity The identity to verify. * - * @throws Horde_Exception + * @throws Horde_Prefs_Exception */ public function verify($identity = null) { @@ -319,19 +335,14 @@ class Horde_Prefs_Identity /* RFC 2822 [3.2.5] does not allow the '\' character to be used in the * personal portion of an e-mail string. */ - if (strpos($this->getValue('fullname', $identity), '\\') !== false) { - throw new Horde_Exception('You cannot have the \ character in your full name.'); + if (strpos($this->getValue($this->_prefnames['fullname'], $identity), '\\') !== false) { + throw new Horde_Prefs_Exception('You cannot have the \ character in your full name.'); } - /* Prepare email validator */ - require_once 'Horde/Form.php'; - $email = new Horde_Form_Type_email(); - $vars = new Horde_Variables(); - $var = new Horde_Form_Variable('', 'replyto_addr', $email, false); - - /* Verify From address. */ - if (!$email->isValid($var, $vars, $this->getValue('from_addr', $identity), $error_message)) { - throw new Horde_Exception($error_message); + try { + Horde_Mime_Address::parseAddressList($this->getValue($this->_prefnames['from_addr'], $identity), array('validate' => true)); + } catch (Horde_Mime_Exception $e) { + throw new Horde_Prefs_Exception($e); } } @@ -349,7 +360,7 @@ class Horde_Prefs_Identity return $this->_names[$ident]; } - $this->_names[$ident] = $this->getValue('fullname', $ident); + $this->_names[$ident] = $this->getValue($this->_prefnames['fullname'], $ident); if (!strlen($this->_names[$ident])) { $this->_names[$ident] = $this->_user; } @@ -369,13 +380,13 @@ class Horde_Prefs_Identity $from_addr = ''; if ($fullname) { - $name = $this->getValue('fullname'); + $name = $this->getValue($this->_prefnames['fullname']); if (!empty($name)) { $from_addr = $name . ' '; } } - $addr = $this->getValue('from_addr'); + $addr = $this->getValue($this->_prefnames['from_addr']); if (empty($addr)) { $addr = $this->_user; if (empty($from_addr)) { @@ -386,100 +397,4 @@ class Horde_Prefs_Identity return $from_addr . '<' . $addr . '>'; } - /** - * Sends a message to an email address supposed to be added to the - * identity. - * A message is send to this address containing a link to confirm that the - * address really belongs to that user. - * - * @param integer $id The identity's ID. - * @param string $old_addr The old From: address. - * - * @return TODO - * @throws Horde_Mime_Exception - */ - public function verifyIdentity($id, $old_addr) - { - global $conf; - - $hash = base_convert(strval(new Horde_Support_Uuid()), 10, 36); - - $pref = @unserialize($this->_prefs->getValue('confirm_email', false)); - $pref = $pref - ? $this->_prefs->convertFromDriver($pref) - : array(); - $pref[$hash] = $this->get($id); - $pref = $this->_prefs->convertToDriver($pref); - $this->_prefs->setValue('confirm_email', serialize($pref), false); - - $new_addr = $this->getValue('from_addr', $id); - $confirm = Horde_Util::addParameter(Horde::url($GLOBALS['registry']->get('webroot', 'horde') . '/services/confirm.php', true, -1), 'h', $hash, false); - $message = sprintf(_("You have requested to add the email address \"%s\" to the list of your personal email addresses.\n\nGo to the following link to confirm that this is really your address:\n%s\n\nIf you don't know what this message means, you can delete it."), - $new_addr, - $confirm); - - $msg_headers = new Horde_Mime_Headers(); - $msg_headers->addMessageIdHeader(); - $msg_headers->addUserAgentHeader(); - $msg_headers->addHeader('Date', date('r')); - $msg_headers->addHeader('To', $new_addr); - $msg_headers->addHeader('From', $old_addr); - $msg_headers->addHeader('Subject', _("Confirm new email address")); - - $body = new Horde_Mime_Part(); - $body->setType('text/plain'); - $body->setContents(Horde_String::wrap($message, 76, "\n")); - $body->setCharset($GLOBALS['registry']->getCharset()); - - $body->send($new_addr, $msg_headers, $GLOBALS['injector']->getInstance('Horde_Mail')); - - return new Horde_Notification_Event(sprintf(_("A message has been sent to \"%s\" to verify that this is really your address. The new email address is activated as soon as you confirm this message."), $new_addr)); - } - - /** - * Checks whether an identity confirmation is valid, and adds the - * validated identity. - * - * @param string $hash The saved hash of the identity being validated. - * - * @return array A message for the user, and the message level. - */ - public function confirmIdentity($hash) - { - $confirm = $this->_prefs->getValue('confirm_email', false); - if (empty($confirm)) { - return array(_("There are no email addresses to confirm."), 'horde.message'); - } - - $confirm = @unserialize($confirm); - if (empty($confirm)) { - return array(_("There are no email addresses to confirm."), 'horde.message'); - } elseif (!isset($confirm[$hash])) { - return array(_("Email addresses to confirm not found."), 'horde.message'); - } - - $identity = $this->_prefs->convertFromDriver($confirm[$hash]); - $id = array_search($identity['id'], $this->getAll('id')); - if ($id === false) { - /* Adding a new identity. */ - $verified = array(); - foreach ($identity as $key => $value) { - if (!$this->_prefs->isLocked($key)) { - $verified[$key] = $value; - } - } - $this->add($verified); - } else { - /* Updating an existing identity. */ - foreach ($identity as $key => $value) { - $this->setValue($key, $value, $id); - } - } - $this->save(); - unset($confirm[$hash]); - $this->_prefs->setValue('confirm_email', serialize($confirm), false); - - return array(sprintf(_("The email address %s has been added to your identities. You can close this window now."), $verified['from_addr']), 'horde.success'); - } - } diff --git a/framework/Prefs/package.xml b/framework/Prefs/package.xml index 9f5b63a1e..ce6e50f4e 100644 --- a/framework/Prefs/package.xml +++ b/framework/Prefs/package.xml @@ -70,6 +70,10 @@ http://pear.php.net/dtd/package-2.0.xsd"> pear.horde.org + Mime + pear.horde.org + + Util pear.horde.org @@ -79,10 +83,6 @@ http://pear.php.net/dtd/package-2.0.xsd"> Db pear.horde.org - - Mime - pear.horde.org - gettext diff --git a/horde/services/confirm.php b/horde/services/confirm.php index d96136dd2..3cb75a7ad 100644 --- a/horde/services/confirm.php +++ b/horde/services/confirm.php @@ -5,15 +5,15 @@ * See the enclosed file COPYING for license information (LGPL). If you * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. * - * @author Jan Schneider + * @author Jan Schneider + * @category Horde + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @package Horde */ require_once dirname(__FILE__) . '/../lib/Application.php'; Horde_Registry::appInit('horde', array('nologintasks' => true)); -$identity = $injector->getInstance('Horde_Prefs_Identity')->getIdentity(); -list($message, $type) = $identity->confirmIdentity(Horde_Util::getFormData('h')); -$notification->push($message, $type); +$identity = $injector->getInstance('Horde_Prefs_Identity')->getIdentity()->confirmIdentity(Horde_Util::getFormData('h')); -$url = Horde_Util::addParameter(Horde::url('services/prefs.php', true), array('app' => 'horde', 'group' => 'identities'), null, false); -header('Location: ' . $url); +Horde::getServiceLink('options')->add('group', 'identities')->redirect(); diff --git a/imp/lib/Prefs/Identity.php b/imp/lib/Prefs/Identity.php index e297c7766..0e45d8038 100644 --- a/imp/lib/Prefs/Identity.php +++ b/imp/lib/Prefs/Identity.php @@ -15,7 +15,7 @@ * @license http://www.fsf.org/copyleft/gpl.html GPL * @package IMP */ -class Imp_Prefs_Identity extends Horde_Prefs_Identity +class Imp_Prefs_Identity extends Horde_Core_Prefs_Identity { /** * Cached data. @@ -42,8 +42,8 @@ class Imp_Prefs_Identity extends Horde_Prefs_Identity { parent::__construct($params); - $this->_properties = array_merge( - $this->_properties, + $this->_prefnames['properties'] = array_merge( + $this->_prefnames['properties'], array( 'replyto_addr', 'alias_addr', 'tieto_addr', 'bcc_addr', 'signature', 'signature_html', 'sig_first', 'sig_dashes', @@ -121,7 +121,7 @@ class Imp_Prefs_Identity extends Horde_Prefs_Identity $address = $from_address; } - if (empty($address) || $this->_prefs->isLocked('from_addr')) { + if (empty($address) || $this->_prefs->isLocked($this->_prefnames['from_addr'])) { $address = $this->getFromAddress($ident); $name = $this->getFullname($ident); } @@ -168,7 +168,7 @@ class Imp_Prefs_Identity extends Horde_Prefs_Identity */ public function getSelectList() { - $ids = $this->getAll('id'); + $ids = $this->getAll($this->_prefnames['id']); foreach ($ids as $key => $id) { $list[$key] = $this->getFromAddress($key) . ' (' . $id . ')'; } @@ -201,7 +201,7 @@ class Imp_Prefs_Identity extends Horde_Prefs_Identity public function getFromAddress($ident = null) { if (!isset($this->_cached['fromList'][$ident])) { - $val = $this->getValue('from_addr', $ident); + $val = $this->getValue($this->_prefnames['from_addr'], $ident); if (empty($val)) { $val = $GLOBALS['registry']->getAuth('bare'); } @@ -407,7 +407,7 @@ class Imp_Prefs_Identity extends Horde_Prefs_Identity return $this->_cached['names'][$ident]; } - $this->_cached['names'][$ident] = $this->getValue('fullname', $ident); + $this->_cached['names'][$ident] = $this->getValue($this->_prefnames['fullname'], $ident); return $this->_cached['names'][$ident]; } -- 2.11.0