Remove horde/Core dependency in Horde_Prefs_Identity
authorMichael M Slusarz <slusarz@curecanti.org>
Tue, 3 Aug 2010 05:18:04 +0000 (23:18 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Tue, 3 Aug 2010 05:22:15 +0000 (23:22 -0600)
framework/Core/lib/Horde.php
framework/Core/lib/Horde/Core/Factory/Identity.php
framework/Core/lib/Horde/Core/Prefs/Identity.php [new file with mode: 0644]
framework/Core/lib/Horde/Core/Prefs/Ui.php
framework/Core/package.xml
framework/Prefs/lib/Horde/Prefs/Identity.php
framework/Prefs/package.xml
horde/services/confirm.php
imp/lib/Prefs/Identity.php

index f67df8c..4c521c0 100644 (file)
@@ -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');
 
index e52f644..fd4311e 100644 (file)
@@ -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 (file)
index 0000000..92a0d77
--- /dev/null
@@ -0,0 +1,115 @@
+<?php
+/**
+ * This class provides Horde-specific functions for the Horde_Prefs_Identity
+ * class.
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * 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 <jan@horde.org>
+ * @author   Michael Slusarz <slusarz@horde.org>
+ * @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');
+    }
+
+}
index 87b9d11..3f34b99 100644 (file)
@@ -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');
index 1341a40..0c864b0 100644 (file)
@@ -177,6 +177,7 @@ Application Framework.</description>
        <dir name="Ui">
         <file name="Widgets.php" role="php" />
        </dir> <!-- /lib/Horde/Core/Prefs/Ui -->
+       <file name="Identity.php" role="php" />
        <file name="Ui.php" role="php" />
       </dir> <!-- /lib/Horde/Core/Prefs -->
       <dir name="Text">
@@ -444,6 +445,7 @@ Application Framework.</description>
    <install as="Horde/Core/Notification/Hordelog.php" name="lib/Horde/Core/Notification/Hordelog.php" />
    <install as="Horde/Core/Notification/Status.php" name="lib/Horde/Core/Notification/Status.php" />
    <install as="Horde/Core/Perms/Ui.php" name="lib/Horde/Core/Perms/Ui.php" />
+   <install as="Horde/Core/Prefs/Identity.php" name="lib/Horde/Core/Prefs/Identity.php" />
    <install as="Horde/Core/Prefs/Ui.php" name="lib/Horde/Core/Prefs/Ui.php" />
    <install as="Horde/Core/Prefs/Ui/Widgets.php" name="lib/Horde/Core/Prefs/Ui/Widgets.php" />
    <install as="Horde/Core/Text/Filter/Bbcode.php" name="lib/Horde/Core/Text/Filter/Bbcode.php" />
index a5c2abe..8dbb7c3 100644 (file)
@@ -1,10 +1,6 @@
 <?php
 /**
- * This class provides an interface to all identities a user might have. Its
- * methods take care of any site-specific restrictions configured in prefs.php
- * and conf.php.
- *
- * @todo Remove notification and gettext references.
+ * This class provides an interface to all identities a user might have.
  *
  * Copyright 2001-2010 The Horde Project (http://www.horde.org/)
  *
@@ -13,6 +9,7 @@
  *
  * @author   Jan Schneider <jan@horde.org>
  * @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:
      * <pre>
+     * '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.
      * </pre>
      */
     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');
-    }
-
 }
index 9f5b63a..ce6e50f 100644 (file)
@@ -70,6 +70,10 @@ http://pear.php.net/dtd/package-2.0.xsd">
     <channel>pear.horde.org</channel>
    </package>
    <package>
+    <name>Mime</name>
+    <channel>pear.horde.org</channel>
+   </package>
+   <package>
     <name>Util</name>
     <channel>pear.horde.org</channel>
    </package>
@@ -79,10 +83,6 @@ http://pear.php.net/dtd/package-2.0.xsd">
     <name>Db</name>
     <channel>pear.horde.org</channel>
    </package>
-   <package>
-    <name>Mime</name>
-    <channel>pear.horde.org</channel>
-   </package>
    <extension>
     <name>gettext</name>
    </extension>
index d96136d..3cb75a7 100644 (file)
@@ -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 <jan@horde.org>
+ * @author   Jan Schneider <jan@horde.org>
+ * @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();
index e297c77..0e45d80 100644 (file)
@@ -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];
     }