Remove horde/Core dependency in horde/Crypt
authorMichael M Slusarz <slusarz@curecanti.org>
Tue, 13 Jul 2010 21:59:44 +0000 (15:59 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Wed, 14 Jul 2010 04:31:23 +0000 (22:31 -0600)
framework/Core/lib/Horde/Core/Binder/Crypt.php [new file with mode: 0644]
framework/Core/lib/Horde/Core/Factory/Crypt.php [new file with mode: 0644]
framework/Core/lib/Horde/Registry.php
framework/Core/package.xml
framework/Crypt/lib/Horde/Crypt.php
framework/Crypt/lib/Horde/Crypt/Pgp.php
framework/Crypt/lib/Horde/Crypt/Smime.php
framework/Ui/lib/Horde/Ui/VarRenderer/Html.php
imp/lib/Injector/Binder/Pgp.php
imp/lib/Injector/Binder/Smime.php
mnemo/lib/Driver.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 (file)
index 0000000..608f9db
--- /dev/null
@@ -0,0 +1,17 @@
+<?php
+/**
+ * @category Horde
+ * @package  Core
+ */
+class Horde_Core_Binder_Crypt implements Horde_Injector_Binder
+{
+    public function create(Horde_Injector $injector)
+    {
+        return new Horde_Core_Factory_Crypt($injector);
+    }
+
+    public function equals(Horde_Injector_Binder $binder)
+    {
+        return false;
+    }
+}
diff --git a/framework/Core/lib/Horde/Core/Factory/Crypt.php b/framework/Core/lib/Horde/Core/Factory/Crypt.php
new file mode 100644 (file)
index 0000000..7168f24
--- /dev/null
@@ -0,0 +1,70 @@
+<?php
+/**
+ * A Horde_Injector:: based Horde_Crypt:: factory.
+ *
+ * PHP version 5
+ *
+ * @category Horde
+ * @package  Core
+ * @author   Michael Slusarz <slusarz@horde.org>
+ * @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 <slusarz@horde.org>
+ * @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);
+    }
+
+}
index 23f14a0..9d56dc8 100644 (file)
@@ -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(),
index f13530b..6263c99 100644 (file)
@@ -84,6 +84,7 @@ Application Framework.</description>
        <file name="Auth.php" role="php" />
        <file name="AuthSignup.php" role="php" />
        <file name="Cache.php" role="php" />
+       <file name="Crypt.php" role="php" />
        <file name="Data.php" role="php" />
        <file name="Db.php" role="php" />
        <file name="DbBase.php" role="php" />
@@ -112,6 +113,7 @@ Application Framework.</description>
       </dir> <!-- /lib/Horde/Core/Binder -->
       <dir name="Factory">
        <file name="Auth.php" role="php" />
+       <file name="Crypt.php" role="php" />
        <file name="Data.php" role="php" />
        <file name="Db.php" role="php" />
        <file name="DbPear.php" role="php" />
@@ -253,6 +255,10 @@ Application Framework.</description>
     <channel>pear.horde.org</channel>
    </package>
    <package>
+    <name>Crypt</name>
+    <channel>pear.php.net</channel>
+   </package>
+   <package>
     <name>DB</name>
     <channel>pear.php.net</channel>
    </package>
@@ -313,6 +319,7 @@ Application Framework.</description>
    <install as="Horde/Core/Binder/Auth.php" name="lib/Horde/Core/Binder/Auth.php" />
    <install as="Horde/Core/Binder/AuthSignup.php" name="lib/Horde/Core/Binder/AuthSignup.php" />
    <install as="Horde/Core/Binder/Cache.php" name="lib/Horde/Core/Binder/Cache.php" />
+   <install as="Horde/Core/Binder/Crypt.php" name="lib/Horde/Core/Binder/Crypt.php" />
    <install as="Horde/Core/Binder/Data.php" name="lib/Horde/Core/Binder/Data.php" />
    <install as="Horde/Core/Binder/Db.php" name="lib/Horde/Core/Binder/Db.php" />
    <install as="Horde/Core/Binder/DbBase.php" name="lib/Horde/Core/Binder/DbBase.php" />
@@ -339,6 +346,7 @@ Application Framework.</description>
    <install as="Horde/Core/Binder/Twitter.php" name="lib/Horde/Core/Binder/Twitter.php" />
    <install as="Horde/Core/Binder/Vfs.php" name="lib/Horde/Core/Binder/Vfs.php" />
    <install as="Horde/Core/Factory/Auth.php" name="lib/Horde/Core/Factory/Auth.php" />
+   <install as="Horde/Core/Factory/Crypt.php" name="lib/Horde/Core/Factory/Crypt.php" />
    <install as="Horde/Core/Factory/Data.php" name="lib/Horde/Core/Factory/Data.php" />
    <install as="Horde/Core/Factory/Db.php" name="lib/Horde/Core/Factory/Db.php" />
    <install as="Horde/Core/Factory/DbPear.php" name="lib/Horde/Core/Factory/DbPear.php" />
index bcafb38..9f9a16f 100644 (file)
 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:
      * <pre>
+     * '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.
      * </pre>
      *
      * @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);
     }
 
     /**
index 5357012..721cdf2 100644 (file)
@@ -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;
index f8c7a28..f5d3c49 100644 (file)
@@ -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');
index afbe270..2a7b4fe 100644 (file)
@@ -1306,8 +1306,9 @@ EOT;
         if (empty($key)) {
             return '';
         }
-        $pgp = Horde_Crypt::factory('pgp', $var->type->getPGPParams());
-        return '<pre>' . $pgp->pgpPrettyKey($key) . '</pre>';
+        return '<pre>' .
+            $GLOBALS['injector']->getInstance('Horde_Crypt')->getCrypr('Pgp', $var->type->getPGPParams())->pgpPrettyKey($key) .
+            '</pre>';
     }
 
     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)
index da94bbc..8e8b16f 100644 (file)
@@ -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);
     }
 
     /**
index 9166cc0..b15a95c 100644 (file)
@@ -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');
     }
 
     /**
index 4149b24..e81df7e 100644 (file)
@@ -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']
+        ));
     }
 
     /**