From 211929876b84beba330a8fed2130feaed616cd9f Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Sun, 06 Sep 2015 09:52:31 -0400
Subject: [PATCH] Enigma: GPG keys export

---
 plugins/enigma/skins/larry/templates/keys.html   |   15 ++++-
 plugins/enigma/lib/enigma_driver_gnupg.php       |   10 +++
 plugins/enigma/lib/enigma_ui.php                 |   41 ++++++++++++-
 plugins/enigma/localization/en_US.inc            |    1 
 plugins/enigma/README                            |   18 +-----
 plugins/enigma/skins/classic/enigma.css          |    2 
 plugins/enigma/lib/enigma_driver.php             |    9 +++
 plugins/enigma/skins/larry/enigma.css            |    2 
 plugins/enigma/enigma.js                         |   38 +++++++++---
 plugins/enigma/lib/enigma_engine.php             |   31 ++++++++++
 plugins/enigma/skins/classic/templates/keys.html |   13 +++
 11 files changed, 145 insertions(+), 35 deletions(-)

diff --git a/plugins/enigma/README b/plugins/enigma/README
index 1cd0e2d..ac20b79 100644
--- a/plugins/enigma/README
+++ b/plugins/enigma/README
@@ -8,25 +8,19 @@
 Encryption/decryption is done server-side. So, this plugin
 is for users that trust the server.
 
-WARNING! The plugin is in very early state. See below for a list
-of missing features and known issues.
-
 
 Implemented features:
 ---------------------
 + PGP: signatures verification
 + PGP: messages decryption
 + PGP: Sending of encrypted/signed messages
-+ PGP: keys management UI (key import, delete)
++ PGP: keys management UI (key import, export, delete)
 + PGP: key generation (client- or server-side)
 + Handling of PGP keys attached to incoming messages
 + User preferences to disable plugin features
 
-TODO (must have):
------------------
-- Keys export to file
 
-TODO (later):
+TODO:
 -------------
 - Handling of big messages with temp files
 - Key info in contact details page (optional)
@@ -35,6 +29,7 @@
    - revoke,
    - change expiration date, change passphrase, add photo,
    - manage user IDs
+   - export private keys
 - Generate revocation certs
 - Search filter to see invalid/expired keys
 - Key server(s) support (search, import, upload, refresh)
@@ -53,10 +48,3 @@
 - S/MIME: Sending signed/encrypted messages
 - S/MIME: Handling of certs attached to incoming messages
 - S/MIME: Certificate info in Contacts details page (optional)
-
-Known issues:
--------------
-1. There are Crypt_GPG issues when using gnupg >= 2.0
-   - http://pear.php.net/bugs/bug.php?id=19914
-   - http://pear.php.net/bugs/bug.php?id=20453
-   - http://pear.php.net/bugs/bug.php?id=20527
diff --git a/plugins/enigma/enigma.js b/plugins/enigma/enigma.js
index 8479c79..c9a2a75 100644
--- a/plugins/enigma/enigma.js
+++ b/plugins/enigma/enigma.js
@@ -6,7 +6,7 @@
 
         if (rcmail.gui_objects.keyslist) {
             rcmail.keys_list = new rcube_list_widget(rcmail.gui_objects.keyslist,
-                {multiselect:false, draggable:false, keyboard:false});
+                {multiselect:true, draggable:false, keyboard:false});
             rcmail.keys_list
                 .addEventListener('select', function(o) { rcmail.enigma_keylist_select(o); })
                 .addEventListener('keypress', function(o) { rcmail.enigma_keylist_keypress(o); })
@@ -25,11 +25,16 @@
             rcmail.register_command('search', function(props) {return rcmail.enigma_search(props); }, true);
             rcmail.register_command('reset-search', function(props) {return rcmail.enigma_search_reset(props); }, true);
             rcmail.register_command('plugin.enigma-import', function() { rcmail.enigma_import(); }, true);
-//            rcmail.register_command('plugin.enigma-export', function() { rcmail.enigma_key_export(); }, true);
-            rcmail.register_command('plugin.enigma-key-import', function() { rcmail.enigma_key_import() }, true);
-            rcmail.register_command('plugin.enigma-key-delete', function(props) { return rcmail.enigma_key_delete(); });
+            rcmail.register_command('plugin.enigma-key-export', function() { rcmail.enigma_export(); });
+            rcmail.register_command('plugin.enigma-key-export-selected', function() { rcmail.enigma_export(true); });
+            rcmail.register_command('plugin.enigma-key-import', function() { rcmail.enigma_key_import(); }, true);
+            rcmail.register_command('plugin.enigma-key-delete', function(props) { return rcmail.enigma_delete(); });
             rcmail.register_command('plugin.enigma-key-create', function(props) { return rcmail.enigma_key_create(); }, true);
             rcmail.register_command('plugin.enigma-key-save', function(props) { return rcmail.enigma_key_create_save(); }, true);
+
+            rcmail.addEventListener('responseafterplugin.enigmakeys', function() {
+                rcmail.enable_command('plugin.enigma-key-export', rcmail.env.rowcount > 0);
+            });
         }
     }
     else if (rcmail.env.task == 'mail') {
@@ -125,7 +130,7 @@
 };
 
 // Delete key(s)
-rcube_webmail.prototype.enigma_key_delete = function()
+rcube_webmail.prototype.enigma_delete = function()
 {
     var keys = this.keys_list.get_selection();
 
@@ -137,6 +142,17 @@
 
     // send request to server
     this.http_post('plugin.enigmakeys', post, lock);
+};
+
+// Export key(s)
+rcube_webmail.prototype.enigma_export = function(selected)
+{
+    var keys = selected ? this.keys_list.get_selection().join(',') : '*';
+
+    if (!keys.length)
+        return;
+
+    this.goto_url('plugin.enigmakeys', {_a: 'export', _keys: keys});
 };
 
 // Submit key(s) import form
@@ -163,11 +179,13 @@
 // list row selection handler
 rcube_webmail.prototype.enigma_keylist_select = function(list)
 {
-    var id;
-    if (id = list.get_single_selection())
-        this.enigma_loadframe('&_action=plugin.enigmakeys&_a=info&_id=' + id);
+    var id = list.get_single_selection(), url;
 
-    this.enable_command('plugin.enigma-key-delete', list.selection.length > 0);
+    if (id)
+        url = '&_action=plugin.enigmakeys&_a=info&_id=' + id;
+
+    this.enigma_loadframe(url);
+    this.enable_command('plugin.enigma-key-delete', 'plugin.enigma-key-export-selected', list.selection.length > 0);
 };
 
 rcube_webmail.prototype.enigma_keylist_keypress = function(list)
@@ -275,6 +293,8 @@
     this.enigma_loadframe();
     if (this.keys_list)
         this.keys_list.clear(true);
+
+    this.enable_command('plugin.enigma-key-delete', 'plugin.enigma-key-delete-selected', false);
 }
 
 // Adds a row to the list
diff --git a/plugins/enigma/lib/enigma_driver.php b/plugins/enigma/lib/enigma_driver.php
index 6326ef7..10c6d1c 100644
--- a/plugins/enigma/lib/enigma_driver.php
+++ b/plugins/enigma/lib/enigma_driver.php
@@ -70,6 +70,15 @@
     abstract function import($content, $isfile = false);
 
     /**
+     * Key/Cert export.
+     *
+     * @param string Key ID
+     *
+     * @return mixed Key content or enigma_error
+     */
+    abstract function export($key);
+
+    /**
      * Keys listing.
      *
      * @param string Optional pattern for key ID, user ID or fingerprint
diff --git a/plugins/enigma/lib/enigma_driver_gnupg.php b/plugins/enigma/lib/enigma_driver_gnupg.php
index c7fc2dc..1339c7b 100644
--- a/plugins/enigma/lib/enigma_driver_gnupg.php
+++ b/plugins/enigma/lib/enigma_driver_gnupg.php
@@ -158,6 +158,16 @@
         }
     }
 
+    public function export($keyid)
+    {
+        try {
+            return $this->gpg->exportPublicKey($keyid, true);
+        }
+        catch (Exception $e) {
+            return $this->get_error_from_exception($e);
+        }
+    }
+
     public function list_keys($pattern='')
     {
         try {
diff --git a/plugins/enigma/lib/enigma_engine.php b/plugins/enigma/lib/enigma_engine.php
index 58982ca..aa6a728 100644
--- a/plugins/enigma/lib/enigma_engine.php
+++ b/plugins/enigma/lib/enigma_engine.php
@@ -1003,6 +1003,37 @@
     }
 
     /**
+     * PGP keys/certs export..
+     *
+     * @param string   Key ID
+     * @param resource Optional output stream
+     *
+     * @return mixed Key content or enigma_error
+     */
+    function export_key($key, $fp = null)
+    {
+        $this->load_pgp_driver();
+        $result = $this->pgp_driver->export($key, $fp);
+
+        if ($result instanceof enigma_error) {
+            rcube::raise_error(array(
+                'code' => 600, 'type' => 'php',
+                'file' => __FILE__, 'line' => __LINE__,
+                'message' => "Enigma plugin: " . $result->getMessage()
+                ), true, false);
+
+            return $result;
+        }
+
+        if ($fp) {
+            fwrite($fp, $result);
+        }
+        else {
+            return $result;
+        }
+    }
+
+    /**
      * Registers password for specified key/cert sent by the password prompt.
      */
     function password_handler()
diff --git a/plugins/enigma/lib/enigma_ui.php b/plugins/enigma/lib/enigma_ui.php
index 8bb29d6..455821b 100644
--- a/plugins/enigma/lib/enigma_ui.php
+++ b/plugins/enigma/lib/enigma_ui.php
@@ -59,6 +59,10 @@
                     $this->key_import();
                     break;
 
+                case 'export':
+                    $this->key_export();
+                    break;
+
                 case 'generate':
                     $this->key_generate();
                     break;
@@ -260,6 +264,7 @@
             }
         }
 
+        $this->rc->output->set_env('rowcount', $size);
         $this->rc->output->set_env('search_request', $search);
         $this->rc->output->set_env('pagecount', ceil($listsize/$pagesize));
         $this->rc->output->set_env('current_page', $page);
@@ -398,6 +403,35 @@
                 $this->enigma->gettext('userids')) . $table->show($attrib));
 */
         return $out;
+    }
+
+    /**
+     * Key(s) export handler
+     */
+    private function key_export()
+    {
+        $keys   = rcube_utils::get_input_value('_keys', rcube_utils::INPUT_GPC);
+        $engine = $this->enigma->load_engine();
+        $list   = $keys == '*' ? $engine->list_keys() : explode(',', $keys);
+
+        if (is_array($list)) {
+            $filename = 'export.pgp';
+            if (count($list) == 1) {
+                $filename = (is_object($list[0]) ? $list[0]->id : $list[0]) . '.pgp';
+            }
+
+            // send downlaod headers
+            header('Content-Type: application/pgp-keys');
+            header('Content-Disposition: attachment; filename="' . $filename . '"');
+
+            if ($fp = fopen('php://output', 'w')) {
+                foreach ($list as $key) {
+                    $engine->export_key(is_object($key) ? $key->id : $key, $fp);
+                }
+            }
+        }
+
+        exit;
     }
 
     /**
@@ -594,12 +628,11 @@
      */
     private function key_delete()
     {
-        $keys = rcube_utils::get_input_value('_keys', rcube_utils::INPUT_POST);
-
-        $this->enigma->load_engine();
+        $keys   = rcube_utils::get_input_value('_keys', rcube_utils::INPUT_POST);
+        $engine = $this->enigma->load_engine();
 
         foreach ((array)$keys as $key) {
-            $res = $this->enigma->engine->delete_key($key);
+            $res = $engine->delete_key($key);
 
             if ($res !== true) {
                 $this->rc->output->show_message('enigma.keyremoveerror', 'error');
diff --git a/plugins/enigma/localization/en_US.inc b/plugins/enigma/localization/en_US.inc
index f4f6d54..4e4984a 100644
--- a/plugins/enigma/localization/en_US.inc
+++ b/plugins/enigma/localization/en_US.inc
@@ -66,6 +66,7 @@
 
 $labels['enterkeypasstitle'] = 'Enter key passphrase';
 $labels['enterkeypass'] = 'A passphrase is needed to unlock the secret key ($keyid) for user: $user.';
+$labels['arialabelkeyexportoptions'] = 'Keys export options';
 
 $messages = array();
 $messages['sigvalid'] = 'Verified signature from $sender.';
diff --git a/plugins/enigma/skins/classic/enigma.css b/plugins/enigma/skins/classic/enigma.css
index bbc4af9..2f5d331 100644
--- a/plugins/enigma/skins/classic/enigma.css
+++ b/plugins/enigma/skins/classic/enigma.css
@@ -163,7 +163,7 @@
   width: 32px;
   height: 32px;
   padding: 0;
-  margin-right: 10px;
+  margin: 0 5px;
   overflow: hidden;
   background: url(keys_toolbar.png) 0 0 no-repeat transparent;
   opacity: 0.99; /* this is needed to make buttons appear correctly in Chrome */
diff --git a/plugins/enigma/skins/classic/templates/keys.html b/plugins/enigma/skins/classic/templates/keys.html
index 5de74fd..fea7340 100644
--- a/plugins/enigma/skins/classic/templates/keys.html
+++ b/plugins/enigma/skins/classic/templates/keys.html
@@ -24,9 +24,10 @@
 <div id="keystoolbar">
     <roundcube:button command="plugin.enigma-key-create" type="link" class="buttonPas create" classAct="button create" classSel="button createSel" title="enigma.createkeys" content=" " />
     <roundcube:button command="plugin.enigma-key-import" type="link" class="buttonPas import" classAct="button import" classSel="button importSel" title="enigma.importkeys" content=" " />
-<!--
+    <span class="dropbutton">
     <roundcube:button command="plugin.enigma-key-export" type="link" class="buttonPas export" classAct="button export" classSel="button exportSel" title="enigma.exportkeys" content=" " />
--->
+    <span id="exportmenulink" onclick="rcmail_ui.show_popup('exportmenu');return false"></span>
+    </span>
     <roundcube:button name="messagemenulink" id="messagemenulink" type="link" class="button keymenu" title="enigma.keyactions" onclick="rcmail_ui.show_popup('messagemenu');return false" content=" " />
 </div>
 
@@ -35,6 +36,7 @@
     <roundcube:object name="searchform" id="quicksearchbox" />
     <roundcube:button command="reset-search" id="searchreset" image="/images/icons/reset.gif" title="resetsearch" />
 </div>
+
 
 <div class="enigmascreen">
 
@@ -78,6 +80,13 @@
     </ul>
 </div>
 
+<div id="exportmenu" class="popupmenu">
+    <ul>
+        <li><roundcube:button command="plugin.enigma-key-export" label="exportall" prop="sub" classAct="exportalllink active" class="exportalllink" /></li>
+        <li><roundcube:button command="plugin.enigma-key-export-selected" label="exportsel" prop="sub" classAct="exportsellink active" class="exportsellink" /></li>
+    </ul>
+</div>
+
 <script type="text/javascript">
 rcube_init_mail_ui();
 </script>
diff --git a/plugins/enigma/skins/larry/enigma.css b/plugins/enigma/skins/larry/enigma.css
index 0f393a0..f9ea499 100644
--- a/plugins/enigma/skins/larry/enigma.css
+++ b/plugins/enigma/skins/larry/enigma.css
@@ -161,5 +161,5 @@
 }
 
 #keystoolbar a.export {
-  background-position: center 0;
+  background-position: center -40px;
 }
diff --git a/plugins/enigma/skins/larry/templates/keys.html b/plugins/enigma/skins/larry/templates/keys.html
index 35b137e..6996904 100644
--- a/plugins/enigma/skins/larry/templates/keys.html
+++ b/plugins/enigma/skins/larry/templates/keys.html
@@ -16,9 +16,18 @@
     <h2 id="aria-label-toolbar" class="voice"><roundcube:label name="arialabeltoolbar" /></h2>
     <div id="keystoolbar" class="toolbar" role="toolbar" aria-labelledby="aria-label-toolbar">
         <roundcube:button command="plugin.enigma-key-import" type="link" class="button import disabled" classAct="button import" classSel="button import pressed" label="import" title="enigma.importkeys" />
-<!--
-        <roundcube:button command="plugin.enigma-key-export" type="link" class="button export disabled" classAct="button export" classSel="button export pressed" label="export" title="enigma.exportkeys" />
--->
+        <span class="dropbutton">
+            <roundcube:button command="plugin.enigma-key-export" type="link" class="button export disabled" classAct="button export" classSel="button export pressed" label="export" title="enigma.exportkeys" />
+            <a href="#export" class="dropbuttontip" id="exportmenulink" onclick="return UI.toggle_popup('exportmenu',event)" aria-haspopup="true" aria-expanded="false" aria-owns="exportmenu-menu" tabindex="0"><roundcube:label name="enigma.arialabelkeyexportoptions" /></a>
+        </span>
+    </div>
+
+    <div id="exportmenu" class="popupmenu" aria-hidden="true">
+        <h3 id="aria-label-exportmenu" class="voice"><roundcube:label name="enigma.arialabelkeyexportoptions" /></h3>
+        <ul id="exportmenu-menu" class="toolbarmenu" role="menu" aria-labelledby="aria-label-exportmenu">
+            <roundcube:button type="link-menuitem" command="plugin.enigma-key-export" label="exportall" prop="sub" class="exportalllink" classAct="exportalllink active" />
+            <roundcube:button type="link-menuitem" command="plugin.enigma-key-export-selected" label="exportsel" prop="sub" class="exportsellink" classAct="exportsellink active" />
+        </ul>
     </div>
 
     <div id="quicksearchbar" class="searchbox" role="search" aria-labelledby="aria-label-searchform">

--
Gitblit v1.9.1