From 0a7847ad1cfade7d64f5660ddbcd62999c35782e Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Fri, 21 Jan 2011 11:36:50 -0700 Subject: [PATCH] Break key info parsing into separate method --- imp/lib/Crypt/Smime.php | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/imp/lib/Crypt/Smime.php b/imp/lib/Crypt/Smime.php index 09718affa..7436a6f45 100644 --- a/imp/lib/Crypt/Smime.php +++ b/imp/lib/Crypt/Smime.php @@ -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. * -- 2.11.0