class Horde_Crypt
{
/**
- * Singleton instances.
- *
- * @var array
- */
- static protected $_instances = array();
-
- /**
* The temporary directory to use.
*
* @var string
}
/**
- * Attempts to return a reference to a concrete Horde_Crypt instance
- * based on $driver. It will only create a new instance if no
- * Horde_Crypt instance with the same parameters currently exists.
+ * Constructor.
*
- * This should be used if multiple crypto backends (and, thus,
- * multiple Horde_Crypt instances) are required.
+ * @param array $params Configuration parameters:
+ * <pre>
+ * 'temp' - (string) [REQUIRED] Location of temporary directory.
+ * </pre>
*
- * This method must be invoked as: $var = Horde_Crypt::singleton()
- *
- * @param mixed $driver The type of concrete Horde_Crypt subclass to
- * return. If $driver is an array, then we will look
- * in $driver[0]/lib/Crypt/ for the subclass
- * implementation named $driver[1].php.
- * @param array $params A hash containing any additional configuration or
- * connection parameters a subclass might need.
- *
- * @return Horde_Crypt The concrete Horde_Crypt reference.
- * @throws Horde_Exception
+ * @throws InvalidArgumentException
*/
- static public function singleton($driver, $params = array())
+ public function __construct($params = array())
{
- ksort($params);
- $signature = hash('md5', serialize(array($driver, $params)));
-
- if (!isset(self::$_instances[$signature])) {
- self::$_instances[$signature] = self::factory($driver, $params);
+ if (empty($params['temp'])) {
+ throw new InvalidArgumentException('A temporary directory must be provided.');
}
- return self::$_instances[$signature];
+ $this->_tempdir = Horde_Util::createTempDir(true, $params['temp']);
}
/**
protected $_privateKeyring;
/**
+ * Configuration parameters.
+ *
+ * @var array
+ */
+ protected $_params = array();
+
+ /**
* Constructor.
*
* @param array $params The following parameters:
* <pre>
* 'program' - (string) [REQUIRED] The path to the GnuPG binary.
- * 'temp' - (string) [OPTIONAL] Path to a temporary directory.
+ * 'proxy_host - (string) Proxy host.
+ * 'proxy_port - (integer) Proxy port.
* </pre>
*
- * @throws Horde_Exception
+ * @throws InvalidArgumentException
*/
- protected function __construct($params = array())
+ public function __construct($params = array())
{
- $this->_tempdir = Horde_Util::createTempDir(true, $params['temp']);
+ parent::__construct($params);
if (empty($params['program'])) {
- throw new Horde_Exception('The location of the GnuPG binary must be given to the Horde_Crypt_Pgp:: class.');
+ throw new InvalidArgumentException('The location of the GnuPG binary must be given to the Horde_Crypt_Pgp:: class.');
}
/* Store the location of GnuPG and set common options. */
if (strncasecmp(PHP_OS, 'WIN', 3)) {
array_unshift($this->_gnupg, 'LANG= ;');
}
+
+ $this->_params = $params;
}
/**
/**
* Connects to a public key server via HKP (Horrowitz Keyserver Protocol).
*
- * @param string $method POST, GET, etc.
- * @param string $server The keyserver to use.
- * @param string $uri The URI to access (relative to the server).
- * @param string $command The PGP command to run.
- * @param float $timeout The timeout value.
+ * @param string $method POST, GET, etc.
+ * @param string $server The keyserver to use.
+ * @param string $resource The URI to access (relative to the server).
+ * @param string $command The PGP command to run.
+ * @param float $timeout The timeout value.
*
* @return string The text from standard output on success.
* @throws Horde_Exception
$output = '';
$port = '11371';
- if (!empty($GLOBALS['conf']['http']['proxy']['proxy_host'])) {
+ if (!empty($this->_params['proxy_host'])) {
$resource = 'http://' . $server . ':' . $port . $resource;
- $server = $GLOBALS['conf']['http']['proxy']['proxy_host'];
- if (!empty($GLOBALS['conf']['http']['proxy']['proxy_port'])) {
- $port = $GLOBALS['conf']['http']['proxy']['proxy_port'];
- } else {
- $port = 80;
- }
+ $server = $this->_params['proxy_host'];
+ $port = isset($this->_params['proxy_port'])
+ ? $this->_params['proxy_port']
+ : 80;
}
$command = $method . ' ' . $resource . ' HTTP/1.0' . ($command ? "\r\n" . $command : '');
);
/**
- * Constructor.
- *
- * @param array $params Parameter array.
- * 'temp' => Location of temporary directory.
- */
- protected function __construct($params)
- {
- $this->_tempdir = $params['temp'];
- }
-
- /**
* Verify a passphrase for a given private key.
*
* @param string $private_key The user's private key.
<channel>pear.horde.org</channel>
</package>
<package>
+ <name>Core</name>
+ <channel>pear.horde.org</channel>
+ </package>
+ <package>
<name>Util</name>
<channel>pear.horde.org</channel>
</package>
<min>1.7.0</min>
</pearinstaller>
<package>
+ <name>Core</name>
+ <channel>pear.horde.org</channel>
+ </package>
+ <package>
<name>Util</name>
<channel>pear.horde.org</channel>
</package>
try {
$addrs = $imp_compose->recipientList($header);
if (!empty($addrs['list'])) {
- $imp_pgp = Horde_Crypt::singleton(array('IMP', 'Pgp'));
+ $imp_pgp = $injector->getInstance('IMP_Crypt_Pgp');
foreach ($addrs['list'] as $val) {
$imp_pgp->getPublicKey($val);
}
case 'pgpPersonal':
case 'pgpSymmetric':
if ($this->_vars->dialog_input) {
- $imp_pgp = Horde_Crypt::singleton(array('IMP', 'Pgp'));
+ $imp_pgp = $GLOBALS['injector']->getInstance('IMP_Crypt_Pgp');
if ((($vars->type == 'pgpPersonal') &&
$imp_pgp->storePassphrase('personal', $this->_vars->dialog_input)) ||
(($vars->type == 'pgpSymmeetric') &&
case 'smimePersonal':
if ($this->_vars->dialog_input) {
- $imp_smime = Horde_Crypt::singleton(array('IMP', 'Smime'));
+ $imp_smime = $GLOBALS['injector']->getInstance('IMP_Crypt_Smime');
if ($imp_smime->storePassphrase($this->_vars->dialog_input)) {
$result->success = 1;
} else {
{
/* Add IMP-specific binders. */
$binders = array(
+ 'IMP_Crypt_Pgp' => new IMP_Injector_Binder_Pgp(),
+ 'IMP_Crypt_Smime' => new IMP_Injector_Binder_Smime(),
'IMP_Folder' => new IMP_Injector_Binder_Folder(),
'IMP_Imap_Tree' => new IMP_Injector_Binder_Imaptree(),
'IMP_Sentmail' => new IMP_Injector_Binder_Sentmail()
if ($attach_flag) {
if ($this->_pgpAttachPubkey) {
- $imp_pgp = Horde_Crypt::singleton(array('IMP', 'Pgp'));
+ $imp_pgp = $GLOBALS['injector']->getInstance('IMP_Crypt_Pgp');
$base->addPart($imp_pgp->publicKeyMIMEPart());
}
if ($GLOBALS['prefs']->getValue('use_pgp') &&
!empty($GLOBALS['conf']['gnupg']['path']) &&
in_array($encrypt, array(IMP::PGP_ENCRYPT, IMP::PGP_SIGN, IMP::PGP_SIGNENC, IMP::PGP_SYM_ENCRYPT, IMP::PGP_SYM_SIGNENC))) {
- $imp_pgp = Horde_Crypt::singleton(array('IMP', 'Pgp'));
+ $imp_pgp = $GLOBALS['injector']->getInstance('IMP_Crypt_Pgp');
switch ($encrypt) {
case IMP::PGP_SIGN:
}
} elseif ($GLOBALS['prefs']->getValue('use_smime') &&
in_array($encrypt, array(IMP::SMIME_ENCRYPT, IMP::SMIME_SIGN, IMP::SMIME_SIGNENC))) {
- $imp_smime = Horde_Crypt::singleton(array('IMP', 'Smime'));
+ $imp_smime = $GLOBALS['injector']->getInstance('IMP_Crypt_Smime');
/* Check to see if we have the user's passphrase yet. */
if (in_array($encrypt, array(IMP::SMIME_SIGN, IMP::SMIME_SIGNENC))) {
const PUBKEY_FIELD = 'pgpPublicKey';
/**
- * Constructor
- */
- public function __construct()
- {
- parent::__construct(array(
- 'program' => $GLOBALS['conf']['gnupg']['path'],
- 'temp' => Horde::getTempDir()
- ));
- }
-
- /**
* Generate the personal Public/Private keypair and store in prefs.
*
* @param string $realname See Horde_Crypt_Pgp::
const PUBKEY_FIELD = 'smimePublicKey';
/**
- * Constructor.
- */
- public function __construct()
- {
- parent::__construct(array('temp' => Horde::getTempDir()));
- }
-
- /**
* Add the personal public key to the prefs.
*
* @param mixed $key The public key to add (either string or array).
--- /dev/null
+<?php
+/**
+ * Binder for IMP_Crypt_Pgp.
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (GPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
+ *
+ * @author Michael Slusarz <slusarz@horde.org>
+ * @package IMP
+ */
+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()
+ );
+
+ if (isset($GLOBALS['conf']['http']['proxy']['proxy_host'])) {
+ $params['proxy_host'] = $GLOBALS['conf']['http']['proxy']['proxy_host'];
+ if (isset($GLOBALS['conf']['http']['proxy']['proxy_port'])) {
+ $params['proxy_port'] = $GLOBALS['conf']['http']['proxy']['proxy_port'];
+ }
+ }
+
+ return Horde_Crypt::factory(array('IMP', 'Pgp'), $params);
+ }
+
+ /**
+ */
+ public function equals(Horde_Injector_Binder $binder)
+ {
+ return false;
+ }
+
+}
--- /dev/null
+<?php
+/**
+ * Binder for IMP_Crypt_Smime.
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (GPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
+ *
+ * @author Michael Slusarz <slusarz@horde.org>
+ * @package IMP
+ */
+class IMP_Injector_Binder_Smime implements Horde_Injector_Binder
+{
+ /**
+ */
+ public function create(Horde_Injector $injector)
+ {
+ return Horde_Crypt::factory(array('IMP', 'Smime'), array(
+ 'temp' => Horde::getTempDir()
+ ));
+ }
+
+ /**
+ */
+ public function equals(Horde_Injector_Binder $binder)
+ {
+ return false;
+ }
+
+}
);
/**
- * IMP_Crypt_Pgp object.
- *
- * @var IMP_Crypt_Pgp
- */
- protected $_imppgp;
-
- /**
* The address of the sender.
*
* @var string
)
);
- if (empty($this->_imppgp)) {
- $this->_imppgp = Horde_Crypt::singleton(array('IMP', 'Pgp'));
- }
-
- $parts = $this->_imppgp->parsePGPData($this->_mimepart->getContents());
+ $parts = $GLOBALS['injector']->getInstance('IMP_Crypt_Pgp')->parsePGPData($this->_mimepart->getContents());
foreach (array_keys($parts) as $key) {
if ($parts[$key]['type'] == Horde_Crypt_Pgp::ARMOR_SIGNATURE) {
$ret[$id]['data'] = implode("\r\n", $parts[$key]['data']);
{
$id = $this->_mimepart->getMimeId();
- if (empty($this->_imppgp) &&
- !empty($GLOBALS['conf']['gnupg']['path'])) {
- $this->_imppgp = Horde_Crypt::singleton(array('IMP', 'Pgp'));
- }
-
if (Horde_Util::getFormData('rawpgpkey')) {
return array(
$id => array(
return null;
}
- if (empty($this->_imppgp)) {
- $this->_imppgp = Horde_Crypt::singleton(array('IMP', 'Pgp'));
- }
-
/* PGP version information appears in the first MIME subpart. We
* don't currently need to do anything with this information. The
* encrypted data appears in the second MIME subpart. */
/* Check if this a symmetrically encrypted message. */
try {
- $symmetric = $this->_imppgp->encryptedSymmetrically($encrypted_data);
+ $imp_pgp = $GLOBALS['injector']->getInstance('IMP_Crypt_Pgp');
+ $symmetric = $imp_pgp->encryptedSymmetrically($encrypted_data);
if ($symmetric) {
$symmetric_id = $this->_getSymmetricID();
- $symmetric_pass = $this->_imppgp->getPassphrase('symmetric', $symmetric_id);
+ $symmetric_pass = $imp_pgp->getPassphrase('symmetric', $symmetric_id);
if (is_null($symmetric_pass)) {
$status[] = _("The data in this part has been encrypted via PGP.");
/* Check if this is a literal compressed message. */
try {
- $info = $this->_imppgp->pgpPacketInformation($encrypted_data);
+ $info = $imp_pgp->pgpPacketInformation($encrypted_data);
} catch (Horde_Exception $e) {
Horde::logMessage($e, 'INFO');
return null;
$status[] = _("The data in this part has been encrypted via PGP.");
if (!$symmetric) {
- if ($this->_imppgp->getPersonalPrivateKey()) {
- $personal_pass = $this->_imppgp->getPassphrase('personal');
+ if ($imp_pgp->getPersonalPrivateKey()) {
+ $personal_pass = $imp_pgp->getPassphrase('personal');
if (is_null($personal_pass)) {
/* Ask for the private key's passphrase if this is
* encrypted asymmetrically. */
try {
if (!is_null($symmetric_pass)) {
- $decrypted_data = $this->_imppgp->decryptMessage($encrypted_data, 'symmetric', $symmetric_pass);
+ $decrypted_data = $imp_pgp->decryptMessage($encrypted_data, 'symmetric', $symmetric_pass);
} elseif (!is_null($personal_pass)) {
- $decrypted_data = $this->_imppgp->decryptMessage($encrypted_data, 'personal', $personal_pass);
+ $decrypted_data = $imp_pgp->decryptMessage($encrypted_data, 'personal', $personal_pass);
} else {
- $decrypted_data = $this->_imppgp->decryptMessage($encrypted_data, 'literal');
+ $decrypted_data = $imp_pgp->decryptMessage($encrypted_data, 'literal');
}
} catch (Horde_Exception $e) {
$status[] = _("The data in this part does not appear to be a valid PGP encrypted message. Error: ") . $e->getMessage();
if (!is_null($symmetric_pass)) {
- $this->_imppgp->unsetPassphrase('symmetric', $this->_getSymmetricID());
+ $imp_pgp->unsetPassphrase('symmetric', $this->_getSymmetricID());
return $this->_getEmbeddedMimeParts();
}
return null;
);
$mime_id = $this->_mimepart->getMimeId();
+ $imp_pgp = $GLOBALS['injector']->getInstance('IMP_Crypt_Pgp');
if ($GLOBALS['prefs']->getValue('use_pgp') &&
$GLOBALS['prefs']->getValue('add_source') &&
$GLOBALS['registry']->hasMethod('contacts/addField')) {
- $status['text'][] = Horde::link('#', '', '', '', $this->_imppgp->savePublicKeyURL($this->_params['contents']->getMailbox(), $this->_params['contents']->getUid(), $mime_id) . 'return false;') . _("Save the key to your address book.") . '</a>';
+ $status['text'][] = Horde::link('#', '', '', '', $imp_pgp->savePublicKeyURL($this->_params['contents']->getMailbox(), $this->_params['contents']->getUid(), $mime_id) . 'return false;') . _("Save the key to your address book.") . '</a>';
}
$status['text'][] = $this->_params['contents']->linkViewJS($this->_mimepart, 'view_attach', _("View the raw text of the Public Key."), array('jstext' => _("View Public Key"), 'params' => array('mode' => IMP_Contents::RENDER_INLINE, 'rawpgpkey' => 1)));
try {
- $data = '<span class="fixed">' . nl2br(str_replace(' ', ' ', $this->_imppgp->pgpPrettyKey($this->_mimepart->getContents()))) . '</span>';
+ $data = '<span class="fixed">' . nl2br(str_replace(' ', ' ', $imp_pgp->pgpPrettyKey($this->_mimepart->getContents()))) . '</span>';
} catch (Horde_Exception $e) {
$data = $e->getMessage();
}
$sig_part = $this->_params['contents']->getMIMEPart($sig_id);
try {
+ $imp_pgp = $GLOBALS['injector']->getInstance('IMP_Crypt_Pgp');
$sig_result = $sig_part->getMetadata('imp-pgp-signature')
- ? $this->_imppgp->verifySignature($sig_part->getContents(array('canonical' => true)), $this->_address)
- : $this->_imppgp->verifySignature($sig_part->replaceEOL($this->_params['contents']->getBodyPart($signed_id, array('mimeheaders' => true)), Horde_Mime_Part::RFC_EOL), $this->_address, $sig_part->getContents());
+ ? $imp_pgp->verifySignature($sig_part->getContents(array('canonical' => true)), $this->_address)
+ : $imp_pgp->verifySignature($sig_part->replaceEOL($this->_params['contents']->getBodyPart($signed_id, array('mimeheaders' => true)), Horde_Mime_Part::RFC_EOL), $this->_address, $sig_part->getContents());
$icon = Horde::img('alerts/success.png', _("Success"));
$sig_text = $sig_result->message;
*/
protected function _getSymmetricID()
{
- return $this->_imppgp->getSymmetricID($this->_params['contents']->getMailbox(), $this->_params['contents']->getUid(), $this->_mimepart->getMimeId());
+ return $GLOBALS['injector']->getInstance('IMP_Crypt_Pgp')->getSymmetricID($this->_params['contents']->getMailbox(), $this->_params['contents']->getUid(), $this->_mimepart->getMimeId());
}
/**
protected function _parsePGP()
{
/* Avoid infinite loop. */
- $imp_pgp = Horde_Crypt::singleton(array('IMP', 'Pgp'));
- $parts = $imp_pgp->parsePGPData($this->_mimepart->getContents());
+ $parts = $GLOBALS['injector']->getInstance('IMP_Crypt_Pgp')->parsePGPData($this->_mimepart->getContents());
if (empty($parts) ||
((count($parts) == 1) &&
($parts[0]['type'] == Horde_Crypt_Pgp::ARMOR_TEXT))) {
if (is_null($this->_impsmime) &&
$GLOBALS['prefs']->getValue('use_smime')) {
try {
- $this->_impsmime = Horde_Crypt::singleton(array('IMP', 'Smime'));
+ $this->_impsmime = $GLOBALS['injector']->getInstance('IMP_Crypt_Smime');
$this->_impsmime->checkForOpenSSL();
} catch (Horde_Exception $e) {
$this->_impsmime = null;
require_once dirname(__FILE__) . '/lib/Application.php';
Horde_Registry::appInit('imp');
-$imp_pgp = Horde_Crypt::singleton(array('IMP', 'Pgp'));
+$imp_pgp = $GLOBALS['injector']->getInstance('IMP_Crypt_Pgp');
$secure_check = Horde::isConnectionSecure();
/* Run through the action handlers */
require_once dirname(__FILE__) . '/lib/Application.php';
Horde_Registry::appInit('imp');
-$imp_smime = Horde_Crypt::singleton(array('IMP', 'Smime'));
+$imp_smime = $GLOBALS['injector']->getInstance('IMP_Crypt_Smime');
/* Run through the action handlers */
$actionID = Horde_Util::getFormData('actionID');