Horde_Crypt exception handling updates
authorMichael M Slusarz <slusarz@curecanti.org>
Mon, 2 Mar 2009 21:51:06 +0000 (14:51 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Mon, 2 Mar 2009 21:51:06 +0000 (14:51 -0700)
imp/lib/Mime/Viewer/pgp.php
imp/pgp.php

index 2a31d28..6c5a655 100644 (file)
@@ -158,34 +158,46 @@ class IMP_Horde_Mime_Viewer_pgp extends Horde_Mime_Viewer_Driver
         $symmetric_pass = $personal_pass = null;
 
         /* Check if this a symmetrically encrypted message. */
-        $symmetric = $this->_imppgp->encryptedSymmetrically($encrypted_data);
-        if ($symmetric) {
-            $symmetric_id = $this->_getSymmetricID();
-            $symmetric_pass = $this->_imppgp->getPassphrase('symmetric', $symmetric_id);
-
-            if (is_null($symmetric_pass)) {
-                $js_action = '';
-                if (!$resymmetric) {
-                    $status[] = _("The message has been encrypted via PGP.");
-                }
+        try {
+            $symmetric = $this->_imppgp->encryptedSymmetrically($encrypted_data);
+            if ($symmetric) {
+                $symmetric_id = $this->_getSymmetricID();
+                $symmetric_pass = $this->_imppgp->getPassphrase('symmetric', $symmetric_id);
+
+                if (is_null($symmetric_pass)) {
+                    $js_action = '';
+                    if (!$resymmetric) {
+                        $status[] = _("The message has been encrypted via PGP.");
+                    }
 
-                switch ($_SESSION['imp']['view']) {
-                case 'dimp':
-                    $js_action = 'DimpCore.reloadMessage({});';
-                    // Fall through
+                    switch ($_SESSION['imp']['view']) {
+                    case 'dimp':
+                        $js_action = 'DimpCore.reloadMessage({});';
+                        // Fall through
 
-                case 'imp':
-                    /* Ask for the correct passphrase if this is encrypted
-                     * symmetrically. */
-                    $status[] = Horde::link('#', '', '', '', IMP::passphraseDialogJS('PGPSymmetric', $js_action, array('symmetricid' => $symmetric_id)) . ';return false;') . _("You must enter the passphrase used to encrypt this message to view it.") . '</a>';
-                    break;
+                    case 'imp':
+                        /* Ask for the correct passphrase if this is encrypted
+                         * symmetrically. */
+                        $status[] = Horde::link('#', '', '', '', IMP::passphraseDialogJS('PGPSymmetric', $js_action, array('symmetricid' => $symmetric_id)) . ';return false;') . _("You must enter the passphrase used to encrypt this message to view it.") . '</a>';
+                        break;
+                    }
+                    return null;
                 }
-                return null;
             }
+        } catch (Horde_Exception $e) {
+            Horde::logMessage($e, __FILE__, __LINE__);
+            unset(self::$_inlinecache[$base_id]);
+            return null;
         }
 
         /* Check if this is a literal compressed message. */
-        $info = $this->_imppgp->pgpPacketInformation($encrypted_data);
+        try {
+            $info = $this->_imppgp->pgpPacketInformation($encrypted_data);
+        } catch (Horde_Exception $e) {
+            Horde::logMessage($e, __FILE__, __LINE__);
+            unset(self::$_inlinecache[$base_id]);
+            return null;
+        }
         $literal = !empty($info['literal']);
 
         if ($literal) {
@@ -271,9 +283,15 @@ class IMP_Horde_Mime_Viewer_pgp extends Horde_Mime_Viewer_Driver
         }
         $status['text'][] = $this->_params['contents']->linkViewJS($this->_mimepart, 'view_attach', _("View the raw text of the Public Key."), array('params' => array('mode' => IMP_Contents::RENDER_INLINE, 'rawpgpkey' => 1)));
 
+        try {
+            $data = '<span class="fixed">' . nl2br(str_replace(' ', '&nbsp;', $this->_imppgp->pgpPrettyKey($this->_mimepart->getContents()))) . '</span>';
+        } catch (Horde_Exception $e) {
+            $data = $e->getMessage();
+        }
+
         return array(
             $mime_id => array(
-                'data' => '<span class="fixed">' . nl2br(str_replace(' ', '&nbsp;', $this->_imppgp->pgpPrettyKey($this->_mimepart->getContents()))) . '</span>',
+                'data' => $data,
                 'status' => array($status),
                 'type' => 'text/html; charset=' . NLS::getCharset()
             )
index 67474ce..8054c2b 100644 (file)
 
 function _printKeyInfo($key = '')
 {
-    $key_info = $GLOBALS['imp_pgp']->pgpPrettyKey($key);
-    _textWindowOutput('PGP Key Information', empty($key_info) ? _("Invalid key") : $key_info);
+    try {
+        $key_info = $GLOBALS['imp_pgp']->pgpPrettyKey($key);
+    } catch (Horde_Exception $e) {
+        Horde::logMessage($e, __FILE__, __LINE__);
+        $key_info = $e->getMessage();
+    }
+    _textWindowOutput('PGP Key Information', $key_info);
 }
 
 function _importKeyDialog($target)
@@ -147,24 +152,30 @@ case 'process_import_personal_public_key':
     $actionID = 'import_personal_public_key';
     /* Check the public key. */
     if ($publicKey = _getImportKey()) {
-        if (($key_info = $imp_pgp->pgpPacketInformation($publicKey)) &&
-            isset($key_info['public_key'])) {
-            if (isset($key_info['secret_key'])) {
-                /* Key contains private key too, don't allow to add this as
-                 * public key. */
-                $notification->push(_("Imported key contains your PGP private key. Only add your public key in the first step!"), 'horde.error');
-                _importKeyDialog('process_import_personal_public_key');
+        try {
+            if (($key_info = $imp_pgp->pgpPacketInformation($publicKey)) &&
+                isset($key_info['public_key'])) {
+                if (isset($key_info['secret_key'])) {
+                    /* Key contains private key too, don't allow to add this
+                     * as public key. */
+                    $notification->push(_("Imported key contains your PGP private key. Only add your public key in the first step!"), 'horde.error');
+                    _importKeyDialog('process_import_personal_public_key');
+                } else {
+                    /* Success in importing public key - Move on to private
+                     * key now. */
+                    $imp_pgp->addPersonalPublicKey($publicKey);
+                    $notification->push(_("PGP public key successfully added."), 'horde.success');
+                    $actionID = 'import_personal_private_key';
+                    _importKeyDialog('process_import_personal_private_key');
+                }
             } else {
-                /* Success in importing public key - Move on to private key
-                 * now. */
-                $imp_pgp->addPersonalPublicKey($publicKey);
-                $notification->push(_("PGP public key successfully added."), 'horde.success');
-                $actionID = 'import_personal_private_key';
-                _importKeyDialog('process_import_personal_private_key');
+                /* Invalid public key imported - Redo public key import
+                 * screen. */
+                $notification->push(_("Invalid personal PGP public key."), 'horde.error');
+                _importKeyDialog('process_import_personal_public_key');
             }
-        } else {
-            /* Invalid public key imported - Redo public key import screen. */
-            $notification->push(_("Invalid personal PGP public key."), 'horde.error');
+        } catch (Horde_Exception $e) {
+            $notification->push($e->getMessage(), 'horde.error');
             _importKeyDialog('process_import_personal_public_key');
         }
     } else {
@@ -178,17 +189,22 @@ case 'process_import_personal_private_key':
     $actionID = 'import_personal_private_key';
     /* Check the private key. */
     if ($privateKey = _getImportKey()) {
-        if (($key_info = $imp_pgp->pgpPacketInformation($privateKey)) &&
-            isset($key_info['secret_key'])) {
-            /* Personal public and private keys have been imported
-             * successfully - close the import popup window. */
-            $imp_pgp->addPersonalPrivateKey($privateKey);
-            $notification->push(_("PGP private key successfully added."), 'horde.success');
-            _reloadWindow();
-        } else {
-            /* Invalid private key imported - Redo private key import
-             * screen. */
-            $notification->push(_("Invalid personal PGP private key."), 'horde.error');
+        try {
+            if (($key_info = $imp_pgp->pgpPacketInformation($privateKey)) &&
+                isset($key_info['secret_key'])) {
+                /* Personal public and private keys have been imported
+                 * successfully - close the import popup window. */
+                $imp_pgp->addPersonalPrivateKey($privateKey);
+                $notification->push(_("PGP private key successfully added."), 'horde.success');
+                _reloadWindow();
+            } else {
+                /* Invalid private key imported - Redo private key import
+                 * screen. */
+                $notification->push(_("Invalid personal PGP private key."), 'horde.error');
+                _importKeyDialog('process_import_personal_private_key');
+            }
+        } catch (Horde_Exception $e) {
+            $notification->push($e->getMessage(), 'horde.error');
             _importKeyDialog('process_import_personal_private_key');
         }
     } else {