Move passphrase dialog processing to Imple
authorMichael M Slusarz <slusarz@curecanti.org>
Wed, 3 Mar 2010 20:15:30 +0000 (13:15 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Thu, 4 Mar 2010 07:39:58 +0000 (00:39 -0700)
imp/lib/Ajax/Application.php
imp/lib/Ajax/Imple/PassphraseDialog.php [new file with mode: 0644]
imp/lib/IMP.php
imp/lib/Mime/Viewer/Pgp.php
imp/lib/Mime/Viewer/Smime.php
imp/lib/Ui/Compose.php
imp/pgp.php
imp/smime.php

index 70752ef..5f08e10 100644 (file)
@@ -1256,123 +1256,6 @@ class IMP_Ajax_Application extends Horde_Ajax_Application_Base
     }
 
     /**
-     * AJAX action: Process a user-supplied PGP symmetric passphrase.
-     *
-     * Variables used:
-     * <pre>
-     * 'dialog_input' - (string) Input from the dialog screen.
-     * 'symmetricid' - (string) The symmetric ID to process.
-     * </pre>
-     *
-     * @return object  An object with the following entries:
-     * <pre>
-     * 'error' - (string) An error message.
-     * 'success' - (integer) 1 on success, 0 on failure.
-     * </pre>
-     */
-    public function pgpSymmetric()
-    {
-        $result = new stdClass;
-        $result->success = 0;
-
-        try {
-            $imp_pgp = Horde_Crypt::singleton(array('IMP', 'Pgp'));
-            Horde::requireSecureConnection();
-            if ($this->_vars->dialog_input) {
-                if ($imp_pgp->storePassphrase('symmetric', $this->_vars->dialog_input, $this->_vars->symmetricid)) {
-                    $result->success = 1;
-                } else {
-                    $result->error = _("Invalid passphrase entered.");
-                }
-            } else {
-                $result->error = _("No passphrase entered.");
-            }
-        } catch (Horde_Exception $e) {
-            $result->error = $e->getMessage();
-        }
-
-        return $result;
-    }
-
-    /**
-     * AJAX action: Process a user-supplied passphrase for the PGP personal
-     * key.
-     *
-     * Variables used:
-     * <pre>
-     * 'dialog_input' - (string) Input from the dialog screen.
-     * </pre>
-     *
-     * @return object  An object with the following entries:
-     * <pre>
-     * 'error' - (string) An error message.
-     * 'success' - (integer) 1 on success, 0 on failure.
-     * </pre>
-     */
-    public function pgpPersonal()
-    {
-        $result = new stdClass;
-        $result->success = false;
-
-        try {
-            $imp_pgp = Horde_Crypt::singleton(array('IMP', 'Pgp'));
-            Horde::requireSecureConnection();
-            if ($this->_vars->dialog_input) {
-                if ($imp_pgp->storePassphrase('personal', $this->_vars->dialog_input)) {
-                    $result->success = 1;
-                } else {
-                    $result->error = _("Invalid passphrase entered.");
-                }
-            } else {
-                $result->error = _("No passphrase entered.");
-            }
-        } catch (Horde_Exception $e) {
-            $result->error = $e->getMessage();
-        }
-
-        return $result;
-    }
-
-    /**
-     * AJAX action: Process a user-supplied passphrase for the S/MIME
-     * personal key.
-     *
-     * Variables used:
-     * <pre>
-     * 'dialog_input' - (string) Input from the dialog screen.
-     * </pre>
-     *
-     * @return object  An object with the following entries:
-     * <pre>
-     * 'error' - (string) An error message.
-     * 'success' - (integer) 1 on success, 0 on failure.
-     * </pre>
-     */
-    public function smimePersonal()
-    {
-        $result = new stdClass;
-        $result->success = false;
-
-        try {
-            $imp_smime = Horde_Crypt::singleton(array('IMP', 'Smime'));
-            Horde::requireSecureConnection();
-            if ($this->_vars->dialog_input) {
-                if ($imp_smime->storePassphrase($this->_vars->dialog_input)) {
-                    $result->success = 1;
-                } else {
-                    $result->error = _("Invalid passphrase entered.");
-                }
-            } else {
-                $result->error = _("No passphrase entered.");
-            }
-        } catch (Horde_Exception $e) {
-            $result->error = $e->getMessage();
-        }
-
-        return $result;
-    }
-
-    /**
      * AJAX action: Add an attachment to a compose message.
      *
      * Variables used:
diff --git a/imp/lib/Ajax/Imple/PassphraseDialog.php b/imp/lib/Ajax/Imple/PassphraseDialog.php
new file mode 100644 (file)
index 0000000..43e5b09
--- /dev/null
@@ -0,0 +1,180 @@
+<?php
+/**
+ * Attach the passphrase dialog to the page.
+ *
+ * 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_Ajax_Imple_PassphraseDialog extends Horde_Ajax_Imple_Base
+{
+    /**
+     * Passphrase DOM ID counter.
+     *
+     * @var integer
+     */
+    static protected $_passphraseId = 0;
+
+    /**
+     * The passphrase ID used by this instance.
+     *
+     * @var string
+     */
+    protected $_domid;
+
+    /**
+     * Constructor.
+     *
+     * @param array $params  Configuration parameters.
+     * <pre>
+     * 'id' - [OPTIONAL] The DOM ID to attach to.
+     * 'onload' - (boolean) [OPTIONAL] If set, will trigger action on page
+     *            load.
+     * 'params' - (array) [OPTIONAL] Any additional parameters to pass.
+     * 'type' - (string) The dialog type.
+     * </pre>
+     */
+    public function __construct($params)
+    {
+        if (!isset($params['id'])) {
+            $params['id'] = 'imp_passphrase_' . ++self::$_passphraseId;
+        }
+
+        $this->_domid = $params['id'];
+
+        parent::__construct($params);
+    }
+
+    /**
+     * Attach the object to a javascript event.
+     */
+    public function attach()
+    {
+        $params = isset($this->_params['params'])
+            ? $this->_params['params']
+            : array();
+        $params['type'] = $this->_params['type'];
+
+        switch ($this->_params['type']) {
+        case 'pgpPersonal':
+            $text = _("Enter your personal PGP passphrase.");
+            break;
+
+        case 'pgpSymmetric':
+            $text = _("Enter the passphrase used to encrypt this message.");
+            break;
+
+        case 'smimePersonal':
+            $text = _("Enter your personal S/MIME passphrase.");
+            break;
+        }
+
+        if (defined('SID')) {
+            parse_str(SID, $sid);
+            $params = array_merge($params, $sid);
+        }
+
+        $js_params = array(
+            'cancel_text' => _("Cancel"),
+            'ok_text' => _("OK"),
+            'params' => $params,
+            'password' => true,
+            'text' => $text,
+            'type' => $this->_params['type'],
+            'uri' => strval($this->_getUrl('PassphraseDialog', 'imp'))
+        );
+
+        Horde::addScriptFile('effects.js', 'horde');
+        Horde::addScriptFile('redbox.js', 'horde');
+        Horde::addScriptFile('dialog.js', 'imp');
+
+        $js = 'IMPDialog.display(' . Horde::escapeJson($js_params, array('urlencode' => true)) . ');';
+
+        if (empty($this->_params['onload'])) {
+            $js = '$("' . $this->_domid . '").observe("click", function(e) { ' . $js . 'e.stop(); })';
+        }
+
+        Horde::addInlineScript(array($js), 'dom');
+    }
+
+    /**
+     * Perform the given action.
+     *
+     * Variables required in form input:
+     * <pre>
+     * 'dialog_input' - (string) Input from the dialog screen.
+     * 'symmetricid' - (string) The symmetric ID to process.
+     * </pre>
+     *
+     * @param array $args  Not used.
+     * @param array $post  Not used.
+     *
+     * @return object  An object with the following entries:
+     * <pre>
+     * 'error' - (string) An error message.
+     * 'success' - (integer) 1 on success, 0 on failure.
+     * </pre>
+     */
+    public function handle($args, $post)
+    {
+        $result = new stdClass;
+        $result->success = 0;
+
+        $vars = Horde_Variables::getDefaultVariables();
+
+        try {
+            Horde::requireSecureConnection();
+
+            switch ($vars->type) {
+            case 'pgpPersonal':
+            case 'pgpSymmetric':
+                if ($this->_vars->dialog_input) {
+                    $imp_pgp = Horde_Crypt::singleton(array('IMP', 'Pgp'));
+                    if ((($vars->type == 'pgpPersonal') &&
+                         $imp_pgp->storePassphrase('personal', $this->_vars->dialog_input)) ||
+                        (($vars->type == 'pgpSymmeetric') &&
+                         $imp_pgp->storePassphrase('symmetric', $this->_vars->dialog_input, $this->_vars->symmetricid))) {
+                        $result->success = 1;
+                    } else {
+                        $result->error = _("Invalid passphrase entered.");
+                    }
+                } else {
+                    $result->error = _("No passphrase entered.");
+                }
+                break;
+
+            case 'smimePersonal':
+                if ($this->_vars->dialog_input) {
+                $imp_smime = Horde_Crypt::singleton(array('IMP', 'Smime'));
+                    if ($imp_smime->storePassphrase($this->_vars->dialog_input)) {
+                        $result->success = 1;
+                    } else {
+                        $result->error = _("Invalid passphrase entered.");
+                    }
+                } else {
+                    $result->error = _("No passphrase entered.");
+                }
+                break;
+            }
+        } catch (Horde_Exception $e) {
+            $result->error = $e->getMessage();
+        }
+
+        return $result;
+    }
+
+    /**
+     * Generates a unique DOM ID.
+     *
+     * @return string  A unique DOM ID.
+     */
+    public function getPassphraseId()
+    {
+        return $this->_domid;
+    }
+
+}
index 59c45a5..ebdbfc5 100644 (file)
@@ -1051,53 +1051,6 @@ class IMP
     }
 
     /**
-     * Generate the JS code necessary to open a passphrase dialog. Adds the
-     * necessary JS files to open the dialog.
-     *
-     * @param string $type   The dialog type.
-     * @param array $params  Any additional parameters to pass.
-     *
-     * @return string  The generated JS code.
-     */
-    static public function passphraseDialogJS($type, $params = array())
-    {
-        Horde::addScriptFile('effects.js', 'horde');
-        Horde::addScriptFile('redbox.js', 'horde');
-        Horde::addScriptFile('dialog.js', 'imp');
-
-        switch ($type) {
-        case 'pgpPersonal':
-            $text = _("Enter your personal PGP passphrase.");
-            break;
-
-        case 'pgpSymmetric':
-            $text = _("Enter the passphrase used to encrypt this message.");
-            break;
-
-        case 'smimePersonal':
-            $text = _("Enter your personal S/MIME passphrase.");
-            break;
-        }
-
-        if (defined('SID')) {
-            parse_str(SID, $sid);
-            $params = array_merge($params, $sid);
-        }
-
-        $js_params = array(
-            'cancel_text' => _("Cancel"),
-            'ok_text' => _("OK"),
-            'params' => $params,
-            'password' => true,
-            'text' => $text,
-            'type' => $type,
-            'uri' => Horde::getServiceLink('ajax', 'imp') . '/' . $type
-        );
-
-        return 'IMPDialog.display(' . Horde::escapeJson($js_params, array('urlencode' => true)) . ')';
-    }
-
-    /**
      * Return a selfURL that has had index/mailbox/actionID information
      * removed/altered based on an action that has occurred on the present
      * page.
@@ -1190,5 +1143,4 @@ class IMP
         return 'RedBox.overlay = false; RedBox.showHtml(\'' . addcslashes($t_html, "'/") . '\');';
     }
 
-
 }
index 5ec351d..3d2311f 100644 (file)
@@ -222,8 +222,9 @@ class IMP_Horde_Mime_Viewer_Pgp extends Horde_Mime_Viewer_Driver
 
                     /* Ask for the correct passphrase if this is encrypted
                      * symmetrically. */
-                    $status[] = Horde::link('#', '', '', '', IMP::passphraseDialogJS('pgpSymmetric', array('symmetricid' => $symmetric_id)) . ';return false;') . _("You must enter the passphrase used to encrypt this message to view it.") . '</a>';
-
+                    $imple = Horde_Ajax_Imple::factory(array('imp', 'PassphraseDialog'), array('params' => array('symmetricid' => $symmetric_id), 'type' => 'pgpSymmetric'));
+                    $imple->attach();
+                    $status[] = Horde::link('#', '', '', '', '', '', '', array('id' => $imple->getPassphraseId())) . _("You must enter the passphrase used to encrypt this message to view it.") . '</a>';
                     return null;
                 }
             }
@@ -252,7 +253,9 @@ class IMP_Horde_Mime_Viewer_Pgp extends Horde_Mime_Viewer_Driver
                     if (is_null($personal_pass)) {
                         /* Ask for the private key's passphrase if this is
                          * encrypted asymmetrically. */
-                        $status[] = Horde::link('#', '', '', '', IMP::passphraseDialogJS('pgpPersonal') . ';return false;') . _("You must enter the passphrase for your PGP private key to view this message.") . '</a>';
+                        $imple = Horde_Ajax_Imple::factory(array('imp', 'PassphraseDialog'), array('type' => 'pgpPersonal'));
+                        $imple->attach();
+                        $status[] = Horde::link('#', '', '', '', '', '', '', array('id' => $imple->getPassphraseId())) . _("You must enter the passphrase for your PGP private key to view this message.") . '</a>';
                         return null;
                     }
                 } else {
index b3cc1f3..8f0704b 100644 (file)
@@ -195,7 +195,9 @@ class IMP_Horde_Mime_Viewer_Smime extends Horde_Mime_Viewer_Driver
         /* Make sure we have a passphrase. */
         $passphrase = $this->_impsmime->getPassphrase();
         if (is_null($passphrase)) {
-            $status[] = Horde::link('#', '', '', '', IMP::passphraseDialogJS('smimePersonal') . ';return false;') . _("You must enter the passphrase for your S/MIME private key to view this data.") . '</a>';
+            $imple = Horde_Ajax_Imple::factory(array('imp', 'PassphraseDialog'), array('type' => 'smimePersonal'));
+            $imple->attach();
+            $status[] = Horde::link('#', '', '', '', '', '', '', array('id' => $imple->getPassphraseId())) . _("You must enter the passphrase for your S/MIME private key to view this data.") . '</a>';
             return null;
         }
 
index fa794b2..5dc5b8d 100644 (file)
@@ -301,21 +301,24 @@ class IMP_Ui_Compose
      */
     public function passphraseDialog($type, $cacheid = null)
     {
+        $params = array('onload' => true);
+
         switch ($type) {
         case 'pgp':
-            $js = IMP::passphraseDialogJS('pgpPersonal');
+            $type = 'pgpPersonal';
             break;
 
         case 'pgp_symm':
-            $js = IMP::passphraseDialogJS('pgpSymmetric', array('symmetricid' => 'imp_compose_' . $cacheid));
+            $params = array('symmetricid' => 'imp_compose_' . $cacheid);
+            $type = 'pgpSymmetric';
             break;
 
         case 'smime':
-            $js = IMP::passphraseDialogJS('smimePersonal');
+            $type = 'smimePersonal';
             break;
         }
 
-        Horde::addInlineScript(array($js), 'dom');
+        Horde_Ajax_Imple::factory(array('imp', 'PassphraseDialog'), array('params' => $params, 'type' => $type))->attach();
     }
 
     /**
index f8bdd4e..7410291 100644 (file)
@@ -291,8 +291,12 @@ if ($prefs->getValue('use_pgp')) {
             $t->set('infopublic', Horde::link($selfURL->copy()->add('actionID', 'info_personal_public_key'), _("Information on Personal Public Key"), null, 'info_key'));
             $t->set('sendkey', Horde::link($selfURL->copy()->add('actionID', 'send_public_key'), _("Send Key to Public Keyserver")));
             $t->set('personalkey-public-help', Horde_Help::link('imp', 'pgp-personalkey-public'));
+
+            $imple = Horde_Ajax_Imple::factory(array('imp', 'PassphraseDialog'), array('type' => 'pgpPersonal'));
+            $imple->attach();
             $passphrase = $imp_pgp->getPassphrase('personal');
-            $t->set('passphrase', (empty($passphrase)) ? Horde::link('#', _("Enter Passphrase"), null, null, IMP::passphraseDialogJS('pgpPersonal') . ';return false;') . _("Enter Passphrase") : Horde::link($selfURL->copy()->add('actionID', 'unset_passphrase'), _("Unload Passphrase")) . _("Unload Passphrase"));
+            $t->set('passphrase', (empty($passphrase)) ? Horde::link('#', _("Enter Passphrase"), null, null, null, null, null, array('id' => $imple->getPassphraseId())) . _("Enter Passphrase") : Horde::link($selfURL->copy()->add('actionID', 'unset_passphrase'), _("Unload Passphrase")) . _("Unload Passphrase"));
+
             $t->set('viewprivate', Horde::link($selfURL->copy()->add('actionID', 'view_personal_private_key'), _("View Personal Private Key"), null, 'view_key'));
             $t->set('infoprivate', Horde::link($selfURL->copy()->add('actionID', 'info_personal_private_key'), _("Information on Personal Private Key"), null, 'info_key'));
             $t->set('personalkey-private-help', Horde_Help::link('imp', 'pgp-personalkey-private'));
index 74f7ff5..c8737d1 100644 (file)
@@ -202,8 +202,11 @@ if ($openssl_check && $prefs->getValue('use_smime')) {
         if ($t->get('has_key')) {
             $t->set('viewpublic', Horde::link($selfURL->copy()->add('actionID', 'view_personal_public_key'), _("View Personal Public Key"), null, 'view_key'));
             $t->set('infopublic', Horde::link($selfURL->copy()->add('actionID', 'info_personal_public_key'), _("Information on Personal Public Key"), null, 'info_key'));
+            $imple = Horde_Ajax_Imple::factory(array('imp', 'PassphraseDialog'), array('type' => 'smimePersonal'));
+            $imple->attach();
             $passphrase = $imp_smime->getPassphrase();
-            $t->set('passphrase', empty($passphrase) ? Horde::link('#', _("Enter Passphrase"), null, null, IMP::passphraseDialogJS('smimePersonal') . ';return false;') . _("Enter Passphrase") : Horde::link($selfURL->copy()->add('actionID', 'unset_passphrase'), _("Unload Passphrase")) . _("Unload Passphrase"));
+            $t->set('passphrase', (empty($passphrase)) ? Horde::link('#', _("Enter Passphrase"), null, null, null, null, null, array('id' => $imple->getPassphraseId())) . _("Enter Passphrase") : Horde::link($selfURL->copy()->add('actionID', 'unset_passphrase'), _("Unload Passphrase")) . _("Unload Passphrase"));
+
             $t->set('viewprivate', Horde::link($selfURL->copy()->add('actionID', 'view_personal_private_key'), _("View Personal Private Key"), null, 'view_key'));
             $t->set('deletekeypair', addslashes(_("Are you sure you want to delete your keypair? (This is NOT recommended!)")));
             $t->set('personalkey-delete-help', Horde_Help::link('imp', 'smime-delete-personal-certs'));