From: Michael M Slusarz Date: Tue, 21 Dec 2010 21:12:13 +0000 (-0700) Subject: Limit encryption options based on presence of personal keys X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=957e11bc7dc56d0ab5ea9bb73fb43a4f71406950;p=horde.git Limit encryption options based on presence of personal keys --- diff --git a/imp/compose.php b/imp/compose.php index 2028740cb..68f99ae98 100644 --- a/imp/compose.php +++ b/imp/compose.php @@ -653,7 +653,7 @@ if ($prefs->getValue('use_pgp') && $prefs->getValue('pgp_reply_pubkey')) { $default_encrypt = $prefs->getValue('default_encrypt'); if (!$vars->compose_formToken && - in_array($default_encrypt, array(IMP::PGP_ENCRYPT, IMP::PGP_SIGNENC))) { + in_array($default_encrypt, array(IMP_Crypt_Pgp::ENCRYPT, IMP_Crypt_Pgp::SIGNENC))) { try { $addrs = $imp_compose->recipientList($header); if (!empty($addrs['list'])) { @@ -665,7 +665,7 @@ if ($prefs->getValue('use_pgp') && } catch (IMP_Compose_Exception $e) { } catch (Horde_Exception $e) { $notification->push(_("PGP encryption cannot be used by default as public keys cannot be found for all recipients."), 'horde.warning'); - $encrypt_options = ($default_encrypt == IMP::PGP_ENCRYPT) ? IMP::ENCRYPT_NONE : IMP::PGP_SIGN; + $encrypt_options = ($default_encrypt == IMP_Crypt_Pgp::ENCRYPT) ? IMP::ENCRYPT_NONE : IMP_Crypt_Pgp::SIGN; } } } diff --git a/imp/lib/Compose.php b/imp/lib/Compose.php index 0e80a487b..546abdb1d 100644 --- a/imp/lib/Compose.php +++ b/imp/lib/Compose.php @@ -464,8 +464,8 @@ class IMP_Compose implements ArrayAccess, Countable, Iterator * @param array $opts An array of options w/the following keys: *
      * encrypt - (integer) A flag whether to encrypt or sign the message.
-     *           One of IMP::PGP_ENCRYPT, IMP::PGP_SIGNENC,
-     *           IMP::SMIME_ENCRYPT, or IMP::SMIME_SIGNENC.
+     *           One of IMP_Crypt_Pgp::ENCRYPT, IMP_Crypt_Pgp::SIGNENC,
+     *           IMP_Crypt_Smime::ENCRYPT, or IMP_Crypt_Smime::SIGNENC.
      * html - (boolean) Whether this is an HTML message.
      *        DEFAULT: false
      * identity - (IMP_Prefs_Identity) If set, checks for proper tie-to
@@ -522,7 +522,7 @@ class IMP_Compose implements ArrayAccess, Countable, Iterator
 
         /* Must encrypt & send the message one recipient at a time. */
         if ($prefs->getValue('use_smime') &&
-            in_array($encrypt, array(IMP::SMIME_ENCRYPT, IMP::SMIME_SIGNENC))) {
+            in_array($encrypt, array(IMP_Crypt_Smime::ENCRYPT, IMP_Crypt_Smime::SIGNENC))) {
             foreach ($recip['list'] as $val) {
                 $send_msgs[] = $this->_createMimeMessage(array($val), $body, $msg_options);
             }
@@ -1180,14 +1180,14 @@ class IMP_Compose implements ArrayAccess, Countable, Iterator
         $encrypt = empty($options['encrypt']) ? 0 : $options['encrypt'];
         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))) {
+            in_array($encrypt, array(IMP_Crypt_Pgp::ENCRYPT, IMP_Crypt_Pgp::SIGN, IMP_Crypt_Pgp::SIGNENC, IMP_Crypt_Pgp::SYM_ENCRYPT, IMP_Crypt_Pgp::SYM_SIGNENC))) {
             $imp_pgp = $GLOBALS['injector']->getInstance('IMP_Crypt_Pgp');
             $symmetric_passphrase = null;
 
             switch ($encrypt) {
-            case IMP::PGP_SIGN:
-            case IMP::PGP_SIGNENC:
-            case IMP::PGP_SYM_SIGNENC:
+            case IMP_Crypt_Pgp::SIGN:
+            case IMP_Crypt_Pgp::SIGNENC:
+            case IMP_Crypt_Pgp::SYM_SIGNENC:
                 /* Check to see if we have the user's passphrase yet. */
                 $passphrase = $imp_pgp->getPassphrase('personal');
                 if (empty($passphrase)) {
@@ -1197,8 +1197,8 @@ class IMP_Compose implements ArrayAccess, Countable, Iterator
                 }
                 break;
 
-            case IMP::PGP_SYM_ENCRYPT:
-            case IMP::PGP_SYM_SIGNENC:
+            case IMP_Crypt_Pgp::SYM_ENCRYPT:
+            case IMP_Crypt_Pgp::SYM_SIGNENC:
                 /* Check to see if we have the user's symmetric passphrase
                  * yet. */
                 $symmetric_passphrase = $imp_pgp->getPassphrase('symmetric', 'imp_compose_' . $this->_cacheid);
@@ -1213,35 +1213,35 @@ class IMP_Compose implements ArrayAccess, Countable, Iterator
             /* Do the encryption/signing requested. */
             try {
                 switch ($encrypt) {
-                case IMP::PGP_SIGN:
+                case IMP_Crypt_Pgp::SIGN:
                     $base = $imp_pgp->IMPsignMIMEPart($base);
                     break;
 
-                case IMP::PGP_ENCRYPT:
-                case IMP::PGP_SYM_ENCRYPT:
+                case IMP_Crypt_Pgp::ENCRYPT:
+                case IMP_Crypt_Pgp::SYM_ENCRYPT:
                     $to_list = empty($options['from'])
                         ? $to
                         : array_keys(array_flip(array_merge($to, array($options['from']))));
-                    $base = $imp_pgp->IMPencryptMIMEPart($base, $to_list, ($encrypt == IMP::PGP_SYM_ENCRYPT) ? $symmetric_passphrase : null);
+                    $base = $imp_pgp->IMPencryptMIMEPart($base, $to_list, ($encrypt == IMP_Crypt_Pgp::SYM_ENCRYPT) ? $symmetric_passphrase : null);
                     break;
 
-                case IMP::PGP_SIGNENC:
-                case IMP::PGP_SYM_SIGNENC:
+                case IMP_Crypt_Pgp::SIGNENC:
+                case IMP_Crypt_Pgp::SYM_SIGNENC:
                     $to_list = empty($options['from'])
                         ? $to
                         : array_keys(array_flip(array_merge($to, array($options['from']))));
-                    $base = $imp_pgp->IMPsignAndEncryptMIMEPart($base, $to_list, ($encrypt == IMP::PGP_SYM_SIGNENC) ? $symmetric_passphrase : null);
+                    $base = $imp_pgp->IMPsignAndEncryptMIMEPart($base, $to_list, ($encrypt == IMP_Crypt_Pgp::SYM_SIGNENC) ? $symmetric_passphrase : null);
                     break;
                 }
             } catch (Horde_Exception $e) {
                 throw new IMP_Compose_Exception(_("PGP Error: ") . $e->getMessage(), $e->getCode());
             }
         } elseif ($GLOBALS['prefs']->getValue('use_smime') &&
-                  in_array($encrypt, array(IMP::SMIME_ENCRYPT, IMP::SMIME_SIGN, IMP::SMIME_SIGNENC))) {
+                  in_array($encrypt, array(IMP_Crypt_Smime::ENCRYPT, IMP_Crypt_Smime::SIGN, IMP_Crypt_Smime::SIGNENC))) {
             $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))) {
+            if (in_array($encrypt, array(IMP_Crypt_Smime::SIGN, IMP_Crypt_Smime::SIGNENC))) {
                 $passphrase = $imp_smime->getPassphrase();
                 if ($passphrase === false) {
                     $e = new IMP_Compose_Exception(_("S/MIME Error: Need passphrase for personal private key."));
@@ -1253,15 +1253,15 @@ class IMP_Compose implements ArrayAccess, Countable, Iterator
             /* Do the encryption/signing requested. */
             try {
                 switch ($encrypt) {
-                case IMP::SMIME_SIGN:
+                case IMP_Crypt_Smime::SIGN:
                     $base = $imp_smime->IMPsignMIMEPart($base);
                     break;
 
-                case IMP::SMIME_ENCRYPT:
+                case IMP_Crypt_Smime::ENCRYPT:
                     $base = $imp_smime->IMPencryptMIMEPart($base, $to[0]);
                     break;
 
-                case IMP::SMIME_SIGNENC:
+                case IMP_Crypt_Smime::SIGNENC:
                     $base = $imp_smime->IMPsignAndEncryptMIMEPart($base, $to[0]);
                     break;
                 }
diff --git a/imp/lib/Crypt/Pgp.php b/imp/lib/Crypt/Pgp.php
index 451d00dac..242379d88 100644
--- a/imp/lib/Crypt/Pgp.php
+++ b/imp/lib/Crypt/Pgp.php
@@ -18,6 +18,38 @@ class IMP_Crypt_Pgp extends Horde_Crypt_Pgp
     /* Name of PGP public key field in addressbook. */
     const PUBKEY_FIELD = 'pgpPublicKey';
 
+    /* Encryption type constants. */
+    const ENCRYPT = 'pgp_encrypt';
+    const SIGN = 'pgp_sign';
+    const SIGNENC = 'pgp_signenc';
+    const SYM_ENCRYPT = 'pgp_sym_enc';
+    const SYM_SIGNENC = 'pgp_syn_sign';
+
+    /**
+     * Return the list of available encryption options for composing.
+     *
+     * @return array  Keys are encryption type constants, values are gettext
+     *                strings describing the encryption type.
+     */
+    public function encryptList()
+    {
+        $ret = array(
+            self::ENCRYPT => _("PGP Encrypt Message")
+        );
+
+        if ($this->getPersonalPrivateKey()) {
+            $ret += array(
+                self::SIGN => _("PGP Sign Message"),
+                self::SIGNENC => _("PGP Sign/Encrypt Message")
+            );
+        }
+
+        return $ret + array(
+            self::SYM_ENCRYPT => _("PGP Encrypt Message with passphrase"),
+            self::SYM_SIGNENC => _("PGP Sign/Encrypt Message with passphrase")
+        );
+    }
+
     /**
      * Generate the personal Public/Private keypair and store in prefs.
      *
diff --git a/imp/lib/Crypt/Smime.php b/imp/lib/Crypt/Smime.php
index e86bdec16..078bb9319 100644
--- a/imp/lib/Crypt/Smime.php
+++ b/imp/lib/Crypt/Smime.php
@@ -18,6 +18,33 @@ class IMP_Crypt_Smime extends Horde_Crypt_Smime
     /* Name of the S/MIME public key field in addressbook. */
     const PUBKEY_FIELD = 'smimePublicKey';
 
+    /* Encryption type constants. */
+    const ENCRYPT = 'smime_encrypt';
+    const SIGN = 'smime_sign';
+    const SIGNENC = 'smime_signenc';
+
+    /**
+     * Return the list of available encryption options for composing.
+     *
+     * @return array  Keys are encryption type constants, values are gettext
+     *                strings describing the encryption type.
+     */
+    public function encryptList()
+    {
+        $ret = array(
+            self::ENCRYPT => _("S/MIME Encrypt Message")
+        );
+
+        if ($this->getPersonalPrivateKey()) {
+            $ret += array(
+                self::SIGN => _("S/MIME Sign Message"),
+                self::SIGNENC => _("S/MIME Sign/Encrypt Message")
+            );
+        }
+
+        return $ret;
+    }
+
     /**
      * Add the personal public key to the prefs.
      *
diff --git a/imp/lib/IMP.php b/imp/lib/IMP.php
index b06717de1..834cbe9ed 100644
--- a/imp/lib/IMP.php
+++ b/imp/lib/IMP.php
@@ -17,15 +17,7 @@
 class IMP
 {
     /* Encrypt constants. */
-    const ENCRYPT_NONE = 1;
-    const PGP_ENCRYPT = 2;
-    const PGP_SIGN = 3;
-    const PGP_SIGNENC = 4;
-    const SMIME_ENCRYPT = 5;
-    const SMIME_SIGN = 6;
-    const SMIME_SIGNENC = 7;
-    const PGP_SYM_ENCRYPT = 8;
-    const PGP_SYM_SIGNENC = 9;
+    const ENCRYPT_NONE = 'encrypt_none';
 
     /* IMP Mailbox view constants. */
     const MAILBOX_START_FIRSTUNSEEN = 1;
@@ -686,20 +678,11 @@ class IMP
 
         if (!empty($GLOBALS['conf']['gnupg']['path']) &&
             $GLOBALS['prefs']->getValue('use_pgp')) {
-            $enc_opts += array(
-                self::PGP_ENCRYPT => _("PGP Encrypt Message"),
-                self::PGP_SIGN => _("PGP Sign Message"),
-                self::PGP_SIGNENC => _("PGP Sign/Encrypt Message"),
-                self::PGP_SYM_ENCRYPT => _("PGP Encrypt Message with passphrase"),
-                self::PGP_SYM_SIGNENC => _("PGP Sign/Encrypt Message with passphrase")
-            );
+            $enc_opts += $GLOBALS['injector']->getInstance('IMP_Crypt_Pgp')->encryptList();
         }
+
         if ($GLOBALS['prefs']->getValue('use_smime')) {
-            $enc_opts += array(
-                self::SMIME_ENCRYPT => _("S/MIME Encrypt Message"),
-                self::SMIME_SIGN => _("S/MIME Sign Message"),
-                self::SMIME_SIGNENC => _("S/MIME Sign/Encrypt Message")
-            );
+            $enc_opts += $GLOBALS['injector']->getInstance('IMP_Crypt_Smime')->encryptList();
         }
 
         if ($returnList) {
diff --git a/imp/templates/dimp/javascript_defs.php b/imp/templates/dimp/javascript_defs.php
index d5cd6f1f3..19f7cad19 100644
--- a/imp/templates/dimp/javascript_defs.php
+++ b/imp/templates/dimp/javascript_defs.php
@@ -217,17 +217,18 @@ if ($compose_page) {
         );
     }
 
-    if (!($GLOBALS['prefs']->isLocked('default_encrypt')) &&
-        ($GLOBALS['prefs']->getValue('use_pgp') ||
-         $GLOBALS['prefs']->getValue('use_smime'))) {
+    if (!$GLOBALS['prefs']->isLocked('default_encrypt')) {
         $encrypt = array();
         foreach (IMP::encryptList(null, true) as $key => $val) {
             $encrypt[] = array(
                 'l' => htmlspecialchars($val),
-                'v' => intval($key)
+                'v' => $key
             );
         }
-        $code['conf_compose']['encrypt'] = $encrypt;
+
+        if (!empty($encrypt)) {
+            $code['conf_compose']['encrypt'] = $encrypt;
+        }
     }
 
     $stationery = $GLOBALS['injector']->getInstance('IMP_Compose_Stationery');