Break key info parsing into separate method
authorMichael M Slusarz <slusarz@curecanti.org>
Fri, 21 Jan 2011 18:36:50 +0000 (11:36 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Fri, 21 Jan 2011 18:40:57 +0000 (11:40 -0700)
imp/lib/Crypt/Smime.php

index 09718af..7436a6f 100644 (file)
@@ -125,18 +125,27 @@ class IMP_Crypt_Smime extends Horde_Crypt_Smime
      */
     public function addPublicKey($cert)
     {
+        list($key_info, $mail) = $this->publicKeyInfo($cert);
+
+        $GLOBALS['registry']->call('contacts/addField', array($email, $name, self::PUBKEY_FIELD, $cert, $GLOBALS['prefs']->getValue('add_source')));
+    }
+
+    /**
+     * Get information about a public certificate.
+     *
+     * @param string $cert  The public certificate.
+     *
+     * @return array  Two element array: the name and e-mail for the cert.
+     * @throws Horde_Crypt_Exception
+     */
+    public function publicKeyInfo($cert)
+    {
         /* Make sure the certificate is valid. */
         $key_info = openssl_x509_parse($cert);
         if (!is_array($key_info) || !isset($key_info['subject'])) {
             throw new Horde_Crypt_Exception(_("Not a valid public key."));
         }
 
-        /* Add key to the user's address book. */
-        $email = $this->getEmailFromKey($cert);
-        if (is_null($email)) {
-            throw new Horde_Crypt_Exception(_("No email information located in the public key."));
-        }
-
         /* Get the name corresponding to this key. */
         if (isset($key_info['subject']['CN'])) {
             $name = $key_info['subject']['CN'];
@@ -146,7 +155,13 @@ class IMP_Crypt_Smime extends Horde_Crypt_Smime
             throw new Horde_Crypt_Exception(_("Not a valid public key."));
         }
 
-        $GLOBALS['registry']->call('contacts/addField', array($email, $name, self::PUBKEY_FIELD, $cert, $GLOBALS['prefs']->getValue('add_source')));
+        /* Add key to the user's address book. */
+        $email = $this->getEmailFromKey($cert);
+        if (is_null($email)) {
+            throw new Horde_Crypt_Exception(_("No email information located in the public key."));
+        }
+
+        return array($key_info, $mail);
     }
 
     /**
@@ -269,7 +284,6 @@ class IMP_Crypt_Smime extends Horde_Crypt_Smime
         return $this->verify($text, empty($GLOBALS['conf']['openssl']['cafile']) ? array() : $GLOBALS['conf']['openssl']['cafile']);
     }
 
-
     /**
      * Decrypt a message with user's public/private keypair.
      *