From c16292ee564fc1962d8e103be10759864c4cfbba Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Tue, 13 Jul 2010 15:59:44 -0600 Subject: [PATCH] Remove horde/Core dependency in horde/Crypt --- framework/Core/lib/Horde/Core/Binder/Crypt.php | 17 ++++++ framework/Core/lib/Horde/Core/Factory/Crypt.php | 70 +++++++++++++++++++++++++ framework/Core/lib/Horde/Registry.php | 1 + framework/Core/package.xml | 8 +++ framework/Crypt/lib/Horde/Crypt.php | 21 +++++++- framework/Crypt/lib/Horde/Crypt/Pgp.php | 24 ++++----- framework/Crypt/lib/Horde/Crypt/Smime.php | 7 +-- framework/Ui/lib/Horde/Ui/VarRenderer/Html.php | 8 +-- imp/lib/Injector/Binder/Pgp.php | 5 +- imp/lib/Injector/Binder/Smime.php | 4 +- mnemo/lib/Driver.php | 8 +-- 11 files changed, 139 insertions(+), 34 deletions(-) create mode 100644 framework/Core/lib/Horde/Core/Binder/Crypt.php create mode 100644 framework/Core/lib/Horde/Core/Factory/Crypt.php diff --git a/framework/Core/lib/Horde/Core/Binder/Crypt.php b/framework/Core/lib/Horde/Core/Binder/Crypt.php new file mode 100644 index 000000000..608f9dba9 --- /dev/null +++ b/framework/Core/lib/Horde/Core/Binder/Crypt.php @@ -0,0 +1,17 @@ + + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @link http://pear.horde.org/index.php?package=Core + */ + +/** + * A Horde_Injector:: based Horde_Crypt:: factory. + * + * 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. + * + * @category Horde + * @package Core + * @author Michael Slusarz + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @link http://pear.horde.org/index.php?package=Core + */ +class Horde_Core_Factory_Crypt +{ + /** + * The injector. + * + * @var Horde_Injector + */ + private $_injector; + + /** + * Constructor. + * + * @param Horde_Injector $injector The injector to use. + */ + public function __construct(Horde_Injector $injector) + { + $this->_injector = $injector; + } + + /** + * Return the Horde_Crypt:: instance. + * + * @param string $driver The driver name. + * @param array $params Any parameters needed by the driver. + * + * @return Horde_Crypt The instance. + * @throws Horde_Exception + */ + public function getCrypt($driver, $params = array()) + { + global $registry; + + $params = array_merge(array( + 'charset' => $registry->getCharset(), + 'email_charset' => $registry->getEmailCharset(), + 'external_charset' => $registry->getExternalCharset(), + 'temp' => Horde::getTempDir() + ), $params); + + return Horde_Crypt::factory($driver, $params); + } + +} diff --git a/framework/Core/lib/Horde/Registry.php b/framework/Core/lib/Horde/Registry.php index 23f14a017..9d56dc820 100644 --- a/framework/Core/lib/Horde/Registry.php +++ b/framework/Core/lib/Horde/Registry.php @@ -264,6 +264,7 @@ class Horde_Registry // 'Horde_Browser' - initialized below 'Horde_Cache' => new Horde_Core_Binder_Cache(), 'Horde_Core_Auth_Signup' => new Horde_Core_Binder_AuthSignup(), + 'Horde_Crypt' => new Horde_Core_Binder_Crypt(), 'Horde_Data' => new Horde_Core_Binder_Data(), 'Horde_Db' => new Horde_Core_Binder_Db(), 'Horde_Db_Adapter_Base' => new Horde_Core_Binder_DbBase(), diff --git a/framework/Core/package.xml b/framework/Core/package.xml index f13530bac..6263c99c9 100644 --- a/framework/Core/package.xml +++ b/framework/Core/package.xml @@ -84,6 +84,7 @@ Application Framework. + @@ -112,6 +113,7 @@ Application Framework. + @@ -253,6 +255,10 @@ Application Framework. pear.horde.org + Crypt + pear.php.net + + DB pear.php.net @@ -313,6 +319,7 @@ Application Framework. + @@ -339,6 +346,7 @@ Application Framework. + diff --git a/framework/Crypt/lib/Horde/Crypt.php b/framework/Crypt/lib/Horde/Crypt.php index bcafb38ff..9f9a16f72 100644 --- a/framework/Crypt/lib/Horde/Crypt.php +++ b/framework/Crypt/lib/Horde/Crypt.php @@ -14,6 +14,13 @@ class Horde_Crypt { /** + * Configuration parameters. + * + * @var array + */ + protected $_params = array(); + + /** * The temporary directory to use. * * @var string @@ -58,18 +65,30 @@ class Horde_Crypt * * @param array $params Configuration parameters: *
+     * 'charset' - (string) The default charset.
+     *             DEFAULT: NONE
+     * 'email_charset' - (string) The default email charset.
+     *                   DEFAULT: NONE
+     * 'external_charset' - (string) The default external charset.
+     *                      DEFAULT: NONE
      * 'temp' - (string) [REQUIRED] Location of temporary directory.
      * 
* * @throws InvalidArgumentException */ - public function __construct($params = array()) + public function __construct(array $params = array()) { if (empty($params['temp'])) { throw new InvalidArgumentException('A temporary directory must be provided.'); } $this->_tempdir = Horde_Util::createTempDir(true, $params['temp']); + + $this->_params = array_merge(array( + 'charset' => null, + 'email_charset' => null, + 'external_charset' => null, + ), $params); } /** diff --git a/framework/Crypt/lib/Horde/Crypt/Pgp.php b/framework/Crypt/lib/Horde/Crypt/Pgp.php index 53570127f..721cdf270 100644 --- a/framework/Crypt/lib/Horde/Crypt/Pgp.php +++ b/framework/Crypt/lib/Horde/Crypt/Pgp.php @@ -936,7 +936,7 @@ class Horde_Crypt_Pgp extends Horde_Crypt if ($errno == 0) { throw new Horde_Exception(_("Connection refused to the public keyserver.")); } else { - throw new Horde_Exception(sprintf(_("Connection refused to the public keyserver. Reason: %s (%s)"), Horde_String::convertCharset($errstr, $GLOBALS['registry']->getExternalCharset()), $errno)); + throw new Horde_Exception(sprintf(_("Connection refused to the public keyserver. Reason: %s (%s)"), Horde_String::convertCharset($errstr, $this->_params['external_charset']), $errno)); } } @@ -1316,7 +1316,7 @@ class Horde_Crypt_Pgp extends Horde_Crypt '--armor', '--always-trust', '--batch', - '--charset ' . $GLOBALS['registry']->getCharset(), + '--charset ' . $this->_params['charset'], $keyring, '--verify' ); @@ -1388,12 +1388,11 @@ class Horde_Crypt_Pgp extends Horde_Crypt $msg_sign = $this->encrypt($mime_part->toString(array('headers' => true, 'canonical' => true, 'encode' => Horde_Mime_Part::ENCODE_7BIT)), $params); /* Add the PGP signature. */ - $charset = $GLOBALS['registry']->getEmailCharset(); $pgp_sign = new Horde_Mime_Part(); $pgp_sign->setType('application/pgp-signature'); - $pgp_sign->setCharset($charset); + $pgp_sign->setCharset($this->_params['email_charset']); $pgp_sign->setDisposition('inline'); - $pgp_sign->setDescription(Horde_String::convertCharset(_("PGP Digital Signature"), $GLOBALS['registry']->getCharset(), $charset)); + $pgp_sign->setDescription(Horde_String::convertCharset(_("PGP Digital Signature"), $this->_params['charset'], $this->_params['email_charset'])); $pgp_sign->setContents($msg_sign); /* Get the algorithim information from the signature. Since we are @@ -1433,12 +1432,11 @@ class Horde_Crypt_Pgp extends Horde_Crypt $message_encrypt = $this->encrypt($signenc_body, $params); /* Set up MIME Structure according to RFC 3156. */ - $charset = $GLOBALS['registry']->getEmailCharset(); $part = new Horde_Mime_Part(); $part->setType('multipart/encrypted'); - $part->setCharset($charset); + $part->setCharset($this->_params['email_charset']); $part->setContentTypeParameter('protocol', 'application/pgp-encrypted'); - $part->setDescription(Horde_String::convertCharset(_("PGP Encrypted Data"), $GLOBALS['registry']->getCharset(), $charset)); + $part->setDescription(Horde_String::convertCharset(_("PGP Encrypted Data"), $this->_params['charset'], $this->_params['email_charset'])); $part->setContents("This message is in MIME format and has been PGP encrypted.\n"); $part1 = new Horde_Mime_Part(); @@ -1480,9 +1478,8 @@ class Horde_Crypt_Pgp extends Horde_Crypt $part = $this->encryptMIMEPart($part, $encrypt_params); $part->setContents("This message is in MIME format and has been PGP signed and encrypted.\n"); - $charset = $GLOBALS['registry']->getEmailCharset(); - $part->setCharset($charset); - $part->setDescription(Horde_String::convertCharset(_("PGP Signed/Encrypted Data"), $GLOBALS['registry']->getCharset(), $charset)); + $part->setCharset($this->_params['email_charset']); + $part->setDescription(Horde_String::convertCharset(_("PGP Signed/Encrypted Data"), $this->_params['charset'], $this->_params['email_charset'])); return $part; } @@ -1497,11 +1494,10 @@ class Horde_Crypt_Pgp extends Horde_Crypt */ public function publicKeyMIMEPart($key) { - $charset = $GLOBALS['registry']->getEmailCharset(); $part = new Horde_Mime_Part(); $part->setType('application/pgp-keys'); - $part->setCharset($charset); - $part->setDescription(Horde_String::convertCharset(_("PGP Public Key"), $GLOBALS['registry']->getCharset(), $charset)); + $part->setCharset($this->_params['email_charset']); + $part->setDescription(Horde_String::convertCharset(_("PGP Public Key"), $this->_params['charset'], $this->_params['charset'])); $part->setContents($key); return $part; diff --git a/framework/Crypt/lib/Horde/Crypt/Smime.php b/framework/Crypt/lib/Horde/Crypt/Smime.php index f8c7a28fe..f5d3c49af 100644 --- a/framework/Crypt/lib/Horde/Crypt/Smime.php +++ b/framework/Crypt/lib/Horde/Crypt/Smime.php @@ -275,12 +275,9 @@ class Horde_Crypt_Smime extends Horde_Crypt /* Sign the part as a message */ $message = $this->encrypt($mime_part->toString(array('headers' => true, 'canonical' => true)), $params); - /* Get charset for mime part description. */ - $charset = $GLOBALS['registry']->getEmailCharset(); - $msg = new Horde_Mime_Part(); - $msg->setCharset($charset); - $msg->setDescription(Horde_String::convertCharset(_("S/MIME Encrypted Message"), $GLOBALS['registry']->getCharset(), $charset)); + $msg->setCharset($this->_params['email_charset']); + $msg->setDescription(Horde_String::convertCharset(_("S/MIME Encrypted Message"), $this->_params['charset'], $this->_params['email_charset'])); $msg->setDisposition('inline'); $msg->setType('application/pkcs7-mime'); $msg->setContentTypeParameter('smime-type', 'enveloped-data'); diff --git a/framework/Ui/lib/Horde/Ui/VarRenderer/Html.php b/framework/Ui/lib/Horde/Ui/VarRenderer/Html.php index afbe270b0..2a7b4fe63 100644 --- a/framework/Ui/lib/Horde/Ui/VarRenderer/Html.php +++ b/framework/Ui/lib/Horde/Ui/VarRenderer/Html.php @@ -1306,8 +1306,9 @@ EOT; if (empty($key)) { return ''; } - $pgp = Horde_Crypt::factory('pgp', $var->type->getPGPParams()); - return '
' . $pgp->pgpPrettyKey($key) . '
'; + return '
' .
+            $GLOBALS['injector']->getInstance('Horde_Crypt')->getCrypr('Pgp', $var->type->getPGPParams())->pgpPrettyKey($key) .
+            '
'; } protected function _renderVarDisplay_smime($form, &$var, &$vars) @@ -1316,8 +1317,7 @@ EOT; if (empty($cert)) { return ''; } - $smime = Horde_Crypt::factory('smime', $var->type->getSMIMEParams()); - return $smime->certToHTML($cert); + return $GLOBALS['injector']->getInstance('Horde_Crypt')->getCrypt('Smime', $var->type->getSMIMEParams())->certToHTML($cert); } protected function _renderVarDisplay_country($form, &$var, &$vars) diff --git a/imp/lib/Injector/Binder/Pgp.php b/imp/lib/Injector/Binder/Pgp.php index da94bbc3d..8e8b16ffc 100644 --- a/imp/lib/Injector/Binder/Pgp.php +++ b/imp/lib/Injector/Binder/Pgp.php @@ -19,8 +19,7 @@ class IMP_Injector_Binder_Pgp implements Horde_Injector_Binder public function create(Horde_Injector $injector) { $params = array( - 'program' => $GLOBALS['conf']['gnupg']['path'], - 'temp' => Horde::getTempDir() + 'program' => $GLOBALS['conf']['gnupg']['path'] ); if (isset($GLOBALS['conf']['http']['proxy']['proxy_host'])) { @@ -30,7 +29,7 @@ class IMP_Injector_Binder_Pgp implements Horde_Injector_Binder } } - return Horde_Crypt::factory('IMP_Crypt_Pgp', $params); + return $injector->getInstance('Horde_Crypt')->getCrypt('IMP_Crypt_Pgp', $params); } /** diff --git a/imp/lib/Injector/Binder/Smime.php b/imp/lib/Injector/Binder/Smime.php index 9166cc013..b15a95cda 100644 --- a/imp/lib/Injector/Binder/Smime.php +++ b/imp/lib/Injector/Binder/Smime.php @@ -18,9 +18,7 @@ class IMP_Injector_Binder_Smime implements Horde_Injector_Binder */ public function create(Horde_Injector $injector) { - return Horde_Crypt::factory('IMP_Crypt_Smime', array( - 'temp' => Horde::getTempDir() - )); + return $injector->getInstance('Horde_Crypt')->getCrypt('IMP_Crypt_Smime'); } /** diff --git a/mnemo/lib/Driver.php b/mnemo/lib/Driver.php index 4149b24b0..e81df7e58 100644 --- a/mnemo/lib/Driver.php +++ b/mnemo/lib/Driver.php @@ -118,10 +118,10 @@ class Mnemo_Driver { $this->_pgp = PEAR::raiseError(_("Encryption support has not been configured, please contact your administrator.")); return; } - require_once 'Horde/Crypt.php'; - $this->_pgp = Horde_Crypt::factory('pgp', - array('program' => $GLOBALS['conf']['utils']['gnupg'], - 'temp' => Horde::getTempDir())); + + $this->_pgp = $GLOBALS['injector']->getInstance('Horde_Crypt')->getCrypt('pgp', array( + 'program' => $GLOBALS['conf']['utils']['gnupg'] + )); } /** -- 2.11.0