From 4cd087ea2eb8d1dbc8a063b41cffcd5df35d7df6 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Sun, 13 Sep 2015 08:50:16 -0400
Subject: [PATCH] Fixed mailvelope-generated messages structure (required Mail_Mime-1.10.0)
---
plugins/enigma/lib/enigma_ui.php | 381 ++++++++++++++++++++++++++++++++++++++++++++----------
1 files changed, 309 insertions(+), 72 deletions(-)
diff --git a/plugins/enigma/lib/enigma_ui.php b/plugins/enigma/lib/enigma_ui.php
index 26396f1..455821b 100644
--- a/plugins/enigma/lib/enigma_ui.php
+++ b/plugins/enigma/lib/enigma_ui.php
@@ -1,20 +1,14 @@
<?php
-/*
+
+/**
+-------------------------------------------------------------------------+
| User Interface for the Enigma Plugin |
| |
- | This program is free software; you can redistribute it and/or modify |
- | it under the terms of the GNU General Public License version 2 |
- | as published by the Free Software Foundation. |
+ | Copyright (C) 2010-2015 The Roundcube Dev Team |
| |
- | This program is distributed in the hope that it will be useful, |
- | but WITHOUT ANY WARRANTY; without even the implied warranty of |
- | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
- | GNU General Public License for more details. |
- | |
- | You should have received a copy of the GNU General Public License along |
- | with this program; if not, write to the Free Software Foundation, Inc., |
- | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
+ | Licensed under the GNU General Public License version 3 or |
+ | any later version with exceptions for skins & plugins. |
+ | See the README file for a full license statement. |
| |
+-------------------------------------------------------------------------+
| Author: Aleksander Machniak <alec@alec.pl> |
@@ -45,7 +39,7 @@
*
* @param string Preferences section
*/
- function init($section='')
+ function init()
{
$this->add_js();
@@ -63,6 +57,18 @@
*/
case 'import':
$this->key_import();
+ break;
+
+ case 'export':
+ $this->key_export();
+ break;
+
+ case 'generate':
+ $this->key_generate();
+ break;
+
+ case 'create':
+ $this->key_create();
break;
case 'search':
@@ -138,9 +144,10 @@
/**
* Initializes key password prompt
*
- * @param enigma_error Error object with key info
+ * @param enigma_error $status Error object with key info
+ * @param array $params Optional prompt parameters
*/
- function password_prompt($status)
+ function password_prompt($status, $params = array())
{
$data = $status->getData('missing');
@@ -150,7 +157,16 @@
$data = array('keyid' => key($data), 'user' => $data[key($data)]);
- $this->rc->output->set_env('enigma_password_request', $data);
+ if (!empty($params)) {
+ $data = array_merge($params, $data);
+ }
+
+ if ($this->rc->action == 'send') {
+ $this->rc->output->command('enigma_password_request', $data);
+ }
+ else {
+ $this->rc->output->set_env('enigma_password_request', $data);
+ }
// add some labels to client
$this->rc->output->add_label('enigma.enterkeypasstitle', 'enigma.enterkeypass',
@@ -176,7 +192,7 @@
$attrib['name'] = $attrib['id'];
$this->rc->output->set_env('contentframe', $attrib['name']);
- $this->rc->output->set_env('blankpage', $attrib['src'] ?
+ $this->rc->output->set_env('blankpage', $attrib['src'] ?
$this->rc->output->abs_url($attrib['src']) : 'program/resources/blank.gif');
return $this->rc->output->frame($attrib);
@@ -223,9 +239,6 @@
$page = max(intval(rcube_utils::get_input_value('_p', rcube_utils::INPUT_GPC)), 1);
$search = rcube_utils::get_input_value('_q', rcube_utils::INPUT_GPC);
- // define list of cols to be displayed
-// $a_show_cols = array('name');
-
// Get the list
$list = $this->enigma->engine->list_keys($search);
@@ -233,27 +246,25 @@
$this->rc->output->show_message('enigma.keylisterror', 'error');
else if (empty($list))
$this->rc->output->show_message('enigma.nokeysfound', 'notice');
- else {
- if (is_array($list)) {
- // Save the size
- $listsize = count($list);
+ else if (is_array($list)) {
+ // Save the size
+ $listsize = count($list);
- // Sort the list by key (user) name
- usort($list, array('enigma_key', 'cmp'));
+ // Sort the list by key (user) name
+ usort($list, array('enigma_key', 'cmp'));
- // Slice current page
- $list = array_slice($list, ($page - 1) * $pagesize, $pagesize);
+ // Slice current page
+ $list = array_slice($list, ($page - 1) * $pagesize, $pagesize);
+ $size = count($list);
- $size = count($list);
-
- // Add rows
- foreach ($list as $key) {
- $this->rc->output->command('enigma_add_list_row',
- array('name' => rcube::Q($key->name), 'id' => $key->id));
- }
+ // Add rows
+ foreach ($list as $key) {
+ $this->rc->output->command('enigma_add_list_row',
+ array('name' => rcube::Q($key->name), 'id' => $key->id));
}
}
+ $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);
@@ -285,11 +296,12 @@
*/
private function get_rowcount_text($all=0, $curr_count=0, $page=1)
{
- if (!$curr_count)
+ if (!$curr_count) {
$out = $this->enigma->gettext('nokeysfound');
+ }
else {
$pagesize = $this->rc->config->get('pagesize', 100);
- $first = ($page - 1) * $pagesize;
+ $first = ($page - 1) * $pagesize;
$out = $this->enigma->gettext(array(
'name' => 'keysfromto',
@@ -344,23 +356,28 @@
*/
function tpl_key_data($attrib)
{
- $out = '';
+ $out = '';
$table = new html_table(array('cols' => 2));
// Key user ID
$table->add('title', $this->enigma->gettext('keyuserid'));
$table->add(null, rcube::Q($this->data->name));
+
// Key ID
$table->add('title', $this->enigma->gettext('keyid'));
$table->add(null, $this->data->subkeys[0]->get_short_id());
+
// Key type
$keytype = $this->data->get_type();
- if ($keytype == enigma_key::TYPE_KEYPAIR)
+ if ($keytype == enigma_key::TYPE_KEYPAIR) {
$type = $this->enigma->gettext('typekeypair');
- else if ($keytype == enigma_key::TYPE_PUBLIC)
+ }
+ else if ($keytype == enigma_key::TYPE_PUBLIC) {
$type = $this->enigma->gettext('typepublickey');
+ }
$table->add('title', $this->enigma->gettext('keytype'));
$table->add(null, $type);
+
// Key fingerprint
$table->add('title', $this->enigma->gettext('fingerprint'));
$table->add(null, $this->data->subkeys[0]->get_fingerprint());
@@ -389,12 +406,56 @@
}
/**
- * Key import page handler
+ * 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;
+ }
+
+ /**
+ * Key import (page) handler
*/
private function key_import()
{
// Import process
- if ($_FILES['_file']['tmp_name'] && is_uploaded_file($_FILES['_file']['tmp_name'])) {
+ if ($data = rcube_utils::get_input_value('_keys', rcube_utils::INPUT_POST)) {
+ // Import from generation form (ajax request)
+ $this->enigma->load_engine();
+ $result = $this->enigma->engine->import_key($data);
+
+ if (is_array($result)) {
+ $this->rc->output->command('enigma_key_create_success');
+ $this->rc->output->show_message('enigma.keygeneratesuccess', 'confirmation');
+ }
+ else {
+ $this->rc->output->show_message('enigma.keysimportfailed', 'error');
+ }
+
+ $this->rc->output->send();
+ }
+ else if ($_FILES['_file']['tmp_name'] && is_uploaded_file($_FILES['_file']['tmp_name'])) {
$this->enigma->load_engine();
$result = $this->enigma->engine->import_key($_FILES['_file']['tmp_name'], true);
@@ -403,8 +464,9 @@
if ($result['imported']) {
$this->rc->output->command('parent.enigma_list', 1);
}
- else
+ else {
$this->rc->output->command('parent.enigma_loadframe');
+ }
$this->rc->output->show_message('enigma.keysimportsuccess', 'confirmation',
array('new' => $result['imported'], 'old' => $result['unchanged']));
@@ -460,16 +522,117 @@
}
/**
+ * Server-side key pair generation handler
+ */
+ private function key_generate()
+ {
+ $user = rcube_utils::get_input_value('_user', rcube_utils::INPUT_POST, true);
+ $pass = rcube_utils::get_input_value('_password', rcube_utils::INPUT_POST, true);
+ $size = (int) rcube_utils::get_input_value('_size', rcube_utils::INPUT_POST);
+
+ if ($size > 4096) {
+ $size = 4096;
+ }
+
+ $ident = rcube_mime::decode_address_list($user, 1, false);
+
+ if (empty($ident)) {
+ $this->rc->output->show_message('enigma.keygenerateerror', 'error');
+ $this->rc->output->send();
+ }
+
+ $this->enigma->load_engine();
+ $result = $this->enigma->engine->generate_key(array(
+ 'user' => $ident[1]['name'],
+ 'email' => $ident[1]['mailto'],
+ 'password' => $pass,
+ 'size' => $size,
+ ));
+
+ if ($result instanceof enigma_key) {
+ $this->rc->output->command('enigma_key_create_success');
+ $this->rc->output->show_message('enigma.keygeneratesuccess', 'confirmation');
+ }
+ else {
+ $this->rc->output->show_message('enigma.keygenerateerror', 'error');
+ }
+
+ $this->rc->output->send();
+ }
+
+ /**
+ * Key generation page handler
+ */
+ private function key_create()
+ {
+ $this->enigma->include_script('openpgp.min.js');
+
+ $this->rc->output->add_handlers(array(
+ 'keyform' => array($this, 'tpl_key_create_form'),
+ ));
+
+ $this->rc->output->set_env('enigma_keygen_server', $this->rc->config->get('enigma_keygen_server'));
+
+ $this->rc->output->set_pagetitle($this->enigma->gettext('keygenerate'));
+ $this->rc->output->send('enigma.keycreate');
+ }
+
+ /**
+ * Template object for key generation form
+ */
+ function tpl_key_create_form($attrib)
+ {
+ $attrib += array('id' => 'rcmKeyCreateForm');
+ $table = new html_table(array('cols' => 2));
+
+ // get user's identities
+ $identities = $this->rc->user->list_identities(null, true);
+
+ // Identity
+ $select = new html_select(array('name' => 'identity', 'id' => 'key-ident'));
+ foreach ((array) $identities as $idx => $ident) {
+ $name = empty($ident['name']) ? ('<' . $ident['email'] . '>') : $ident['ident'];
+ $select->add($name, $idx);
+ }
+
+ $table->add('title', html::label('key-name', rcube::Q($this->enigma->gettext('newkeyident'))));
+ $table->add(null, $select->show(0));
+
+ // Key size
+ $select = new html_select(array('name' => 'size', 'id' => 'key-size'));
+ $select->add($this->enigma->gettext('key2048'), '2048');
+ $select->add($this->enigma->gettext('key4096'), '4096');
+
+ $table->add('title', html::label('key-size', rcube::Q($this->enigma->gettext('newkeysize'))));
+ $table->add(null, $select->show());
+
+ // Password and confirm password
+ $table->add('title', html::label('key-pass', rcube::Q($this->enigma->gettext('newkeypass'))));
+ $table->add(null, rcube_output::get_edit_field('password', '',
+ array('id' => 'key-pass', 'size' => $attrib['size'], 'required' => true), 'password'));
+
+ $table->add('title', html::label('key-pass-confirm', rcube::Q($this->enigma->gettext('newkeypassconfirm'))));
+ $table->add(null, rcube_output::get_edit_field('password-confirm', '',
+ array('id' => 'key-pass-confirm', 'size' => $attrib['size'], 'required' => true), 'password'));
+
+ $this->rc->output->add_gui_object('keyform', $attrib['id']);
+ $this->rc->output->add_label('enigma.keygenerating', 'enigma.formerror',
+ 'enigma.passwordsdiffer', 'enigma.keygenerateerror', 'enigma.nonameident',
+ 'enigma.keygennosupport');
+
+ return $this->rc->output->form_tag(array(), $table->show($attrib));
+ }
+
+ /**
* Key deleting
*/
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');
@@ -483,53 +646,43 @@
$this->rc->output->send();
}
+ /**
+ * Init compose UI (add task button and the menu)
+ */
private function compose_ui()
{
-/*
$this->add_css();
// Options menu button
- // @TODO: make this work with non-default skins
$this->enigma->add_button(array(
'type' => 'link',
'command' => 'plugin.enigma',
'onclick' => "rcmail.command('menu-open', 'enigmamenu', event.target, event)",
'class' => 'button enigma',
- 'title' => 'securityoptions',
- 'label' => 'securityoptions',
+ 'title' => 'encryptionoptions',
+ 'label' => 'encryption',
'domain' => $this->enigma->ID,
'width' => 32,
'height' => 32
), 'toolbar');
- // Options menu contents
- $this->enigma->add_hook('render_page', array($this, 'compose_menu'));
-*/
- }
-
- function compose_menu($p)
- {
- $menu = new html_table(array('cols' => 2));
+ $menu = new html_table(array('cols' => 2));
$chbox = new html_checkbox(array('value' => 1));
-
- $menu->add(null, html::label(array('for' => 'enigmadefaultopt'),
- rcube::Q($this->enigma->gettext('identdefault'))));
- $menu->add(null, $chbox->show(1, array('name' => '_enigma_default', 'id' => 'enigmadefaultopt')));
$menu->add(null, html::label(array('for' => 'enigmasignopt'),
rcube::Q($this->enigma->gettext('signmsg'))));
- $menu->add(null, $chbox->show(1, array('name' => '_enigma_sign', 'id' => 'enigmasignopt')));
+ $menu->add(null, $chbox->show($this->rc->config->get('enigma_sign_all') ? 1 : 0,
+ array('name' => '_enigma_sign', 'id' => 'enigmasignopt')));
- $menu->add(null, html::label(array('for' => 'enigmacryptopt'),
+ $menu->add(null, html::label(array('for' => 'enigmaencryptopt'),
rcube::Q($this->enigma->gettext('encryptmsg'))));
- $menu->add(null, $chbox->show(1, array('name' => '_enigma_crypt', 'id' => 'enigmacryptopt')));
+ $menu->add(null, $chbox->show($this->rc->config->get('enigma_encrypt_all') ? 1 : 0,
+ array('name' => '_enigma_encrypt', 'id' => 'enigmaencryptopt')));
- $menu = html::div(array('id' => 'enigmamenu', 'class' => 'popupmenu'),
- $menu->show());
+ $menu = html::div(array('id' => 'enigmamenu', 'class' => 'popupmenu'), $menu->show());
- $p['content'] = preg_replace('/(<form name="form"[^>]+>)/i', '\\1'."\n$menu", $p['content']);
-
- return $p;
+ // Options menu contents
+ $this->rc->output->add_footer($menu);
}
/**
@@ -658,7 +811,7 @@
{
$engine = $this->enigma->load_engine();
- // handle attachments vcard attachments
+ // handle keys/certs in attachments
foreach ((array) $p['object']->attachments as $attachment) {
if ($engine->is_keys_part($attachment)) {
$this->keys_parts[] = $attachment->mime_id;
@@ -715,4 +868,88 @@
return $p;
}
+ /**
+ * Handle message_ready hook (encryption/signing)
+ */
+ function message_ready($p)
+ {
+ $savedraft = !empty($_POST['_draft']) && empty($_GET['_saveonly']);
+
+ if (!$savedraft && rcube_utils::get_input_value('_enigma_sign', rcube_utils::INPUT_POST)) {
+ $this->enigma->load_engine();
+ $status = $this->enigma->engine->sign_message($p['message']);
+ $mode = 'sign';
+ }
+
+ if ((!$status instanceof enigma_error) && rcube_utils::get_input_value('_enigma_encrypt', rcube_utils::INPUT_POST)) {
+ $this->enigma->load_engine();
+ $status = $this->enigma->engine->encrypt_message($p['message'], null, $savedraft);
+ $mode = 'encrypt';
+ }
+
+ if ($mode && ($status instanceof enigma_error)) {
+ $code = $status->getCode();
+
+ if ($code == enigma_error::E_KEYNOTFOUND) {
+ $vars = array('email' => $status->getData('missing'));
+ $msg = 'enigma.' . $mode . 'nokey';
+ }
+ else if ($code == enigma_error::E_BADPASS) {
+ $msg = 'enigma.' . $mode . 'badpass';
+ $type = 'warning';
+
+ $this->password_prompt($status);
+ }
+ else {
+ $msg = 'enigma.' . $mode . 'error';
+ }
+
+ $this->rc->output->show_message($msg, $type ?: 'error', $vars);
+ $this->rc->output->send('iframe');
+ }
+
+ return $p;
+ }
+
+ /**
+ * Handler for message_compose_body hook
+ * Display error when the message cannot be encrypted
+ * and provide a way to try again with a password.
+ */
+ function message_compose($p)
+ {
+ $engine = $this->enigma->load_engine();
+
+ // skip: message has no signed/encoded content
+ if (!$this->enigma->engine) {
+ return $p;
+ }
+
+ $engine = $this->enigma->engine;
+
+ // Decryption status
+ foreach ($engine->decryptions as $status) {
+ if ($status instanceof enigma_error) {
+ $code = $status->getCode();
+
+ if ($code == enigma_error::E_KEYNOTFOUND) {
+ $msg = rcube::Q(str_replace('$keyid', enigma_key::format_id($status->getData('id')),
+ $this->enigma->gettext('decryptnokey')));
+ }
+ else if ($code == enigma_error::E_BADPASS) {
+ $this->password_prompt($status, array('compose-init' => true));
+ return $p;
+ }
+ else {
+ $msg = rcube::Q($this->enigma->gettext('decrypterror'));
+ }
+ }
+ }
+
+ if ($msg) {
+ $this->rc->output->show_message($msg, 'error');
+ }
+
+ return $p;
+ }
}
--
Gitblit v1.9.1