You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

enigma_ui.php 39KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  1. <?php
  2. /**
  3. +-------------------------------------------------------------------------+
  4. | User Interface for the Enigma Plugin |
  5. | |
  6. | Copyright (C) 2010-2015 The Roundcube Dev Team |
  7. | |
  8. | Licensed under the GNU General Public License version 3 or |
  9. | any later version with exceptions for skins & plugins. |
  10. | See the README file for a full license statement. |
  11. | |
  12. +-------------------------------------------------------------------------+
  13. | Author: Aleksander Machniak <alec@alec.pl> |
  14. +-------------------------------------------------------------------------+
  15. */
  16. class enigma_ui
  17. {
  18. private $rc;
  19. private $enigma;
  20. private $home;
  21. private $css_loaded;
  22. private $js_loaded;
  23. private $data;
  24. private $keys_parts = array();
  25. private $keys_bodies = array();
  26. function __construct($enigma_plugin, $home='')
  27. {
  28. $this->enigma = $enigma_plugin;
  29. $this->rc = $enigma_plugin->rc;
  30. $this->home = $home; // we cannot use $enigma_plugin->home here
  31. }
  32. /**
  33. * UI initialization and requests handlers.
  34. *
  35. * @param string Preferences section
  36. */
  37. function init()
  38. {
  39. $this->add_js();
  40. $action = rcube_utils::get_input_value('_a', rcube_utils::INPUT_GPC);
  41. if ($this->rc->action == 'plugin.enigmakeys') {
  42. switch ($action) {
  43. case 'delete':
  44. $this->key_delete();
  45. break;
  46. /*
  47. case 'edit':
  48. $this->key_edit();
  49. break;
  50. */
  51. case 'import':
  52. $this->key_import();
  53. break;
  54. case 'export':
  55. $this->key_export();
  56. break;
  57. case 'generate':
  58. $this->key_generate();
  59. break;
  60. case 'create':
  61. $this->key_create();
  62. break;
  63. case 'search':
  64. case 'list':
  65. $this->key_list();
  66. break;
  67. case 'info':
  68. $this->key_info();
  69. break;
  70. }
  71. $this->rc->output->add_handlers(array(
  72. 'keyslist' => array($this, 'tpl_keys_list'),
  73. 'keyframe' => array($this, 'tpl_key_frame'),
  74. 'countdisplay' => array($this, 'tpl_keys_rowcount'),
  75. 'searchform' => array($this->rc->output, 'search_form'),
  76. ));
  77. $this->rc->output->set_pagetitle($this->enigma->gettext('enigmakeys'));
  78. $this->rc->output->send('enigma.keys');
  79. }
  80. /*
  81. // Preferences UI
  82. else if ($this->rc->action == 'plugin.enigmacerts') {
  83. $this->rc->output->add_handlers(array(
  84. 'keyslist' => array($this, 'tpl_certs_list'),
  85. 'keyframe' => array($this, 'tpl_cert_frame'),
  86. 'countdisplay' => array($this, 'tpl_certs_rowcount'),
  87. 'searchform' => array($this->rc->output, 'search_form'),
  88. ));
  89. $this->rc->output->set_pagetitle($this->enigma->gettext('enigmacerts'));
  90. $this->rc->output->send('enigma.certs');
  91. }
  92. */
  93. // Message composing UI
  94. else if ($this->rc->action == 'compose') {
  95. $this->compose_ui();
  96. }
  97. }
  98. /**
  99. * Adds CSS style file to the page header.
  100. */
  101. function add_css()
  102. {
  103. if ($this->css_loaded)
  104. return;
  105. $skin_path = $this->enigma->local_skin_path();
  106. if (is_file($this->home . "/$skin_path/enigma.css")) {
  107. $this->enigma->include_stylesheet("$skin_path/enigma.css");
  108. }
  109. $this->css_loaded = true;
  110. }
  111. /**
  112. * Adds javascript file to the page header.
  113. */
  114. function add_js()
  115. {
  116. if ($this->js_loaded) {
  117. return;
  118. }
  119. $this->enigma->include_script('enigma.js');
  120. $this->js_loaded = true;
  121. }
  122. /**
  123. * Initializes key password prompt
  124. *
  125. * @param enigma_error $status Error object with key info
  126. * @param array $params Optional prompt parameters
  127. */
  128. function password_prompt($status, $params = array())
  129. {
  130. $data = $status->getData('missing');
  131. if (empty($data)) {
  132. $data = $status->getData('bad');
  133. }
  134. $data = array('keyid' => key($data), 'user' => $data[key($data)]);
  135. if (!empty($params)) {
  136. $data = array_merge($params, $data);
  137. }
  138. if ($this->rc->action == 'send' || $this->rc->action == 'plugin.enigmaimport') {
  139. $this->rc->output->command('enigma_password_request', $data);
  140. }
  141. else {
  142. $this->rc->output->set_env('enigma_password_request', $data);
  143. }
  144. // add some labels to client
  145. $this->rc->output->add_label('enigma.enterkeypasstitle', 'enigma.enterkeypass',
  146. 'save', 'cancel');
  147. $this->add_css();
  148. $this->add_js();
  149. }
  150. /**
  151. * Template object for key info/edit frame.
  152. *
  153. * @param array Object attributes
  154. *
  155. * @return string HTML output
  156. */
  157. function tpl_key_frame($attrib)
  158. {
  159. if (!$attrib['id']) {
  160. $attrib['id'] = 'rcmkeysframe';
  161. }
  162. $attrib['name'] = $attrib['id'];
  163. $this->rc->output->set_env('contentframe', $attrib['name']);
  164. $this->rc->output->set_env('blankpage', $attrib['src'] ?
  165. $this->rc->output->abs_url($attrib['src']) : 'program/resources/blank.gif');
  166. return $this->rc->output->frame($attrib);
  167. }
  168. /**
  169. * Template object for list of keys.
  170. *
  171. * @param array Object attributes
  172. *
  173. * @return string HTML content
  174. */
  175. function tpl_keys_list($attrib)
  176. {
  177. // add id to message list table if not specified
  178. if (!strlen($attrib['id'])) {
  179. $attrib['id'] = 'rcmenigmakeyslist';
  180. }
  181. // define list of cols to be displayed
  182. $a_show_cols = array('name');
  183. // create XHTML table
  184. $out = $this->rc->table_output($attrib, array(), $a_show_cols, 'id');
  185. // set client env
  186. $this->rc->output->add_gui_object('keyslist', $attrib['id']);
  187. $this->rc->output->include_script('list.js');
  188. // add some labels to client
  189. $this->rc->output->add_label('enigma.keyremoveconfirm', 'enigma.keyremoving',
  190. 'enigma.keyexportprompt', 'enigma.withprivkeys', 'enigma.onlypubkeys', 'enigma.exportkeys'
  191. );
  192. return $out;
  193. }
  194. /**
  195. * Key listing (and searching) request handler
  196. */
  197. private function key_list()
  198. {
  199. $this->enigma->load_engine();
  200. $pagesize = $this->rc->config->get('pagesize', 100);
  201. $page = max(intval(rcube_utils::get_input_value('_p', rcube_utils::INPUT_GPC)), 1);
  202. $search = rcube_utils::get_input_value('_q', rcube_utils::INPUT_GPC);
  203. // Get the list
  204. $list = $this->enigma->engine->list_keys($search);
  205. if ($list && ($list instanceof enigma_error))
  206. $this->rc->output->show_message('enigma.keylisterror', 'error');
  207. else if (empty($list))
  208. $this->rc->output->show_message('enigma.nokeysfound', 'notice');
  209. else if (is_array($list)) {
  210. // Save the size
  211. $listsize = count($list);
  212. // Sort the list by key (user) name
  213. usort($list, array('enigma_key', 'cmp'));
  214. // Slice current page
  215. $list = array_slice($list, ($page - 1) * $pagesize, $pagesize);
  216. $size = count($list);
  217. // Add rows
  218. foreach ($list as $key) {
  219. $this->rc->output->command('enigma_add_list_row', array(
  220. 'name' => rcube::Q($key->name),
  221. 'id' => $key->id,
  222. 'flags' => $key->is_private() ? 'p' : ''
  223. ));
  224. }
  225. }
  226. $this->rc->output->set_env('rowcount', $size);
  227. $this->rc->output->set_env('search_request', $search);
  228. $this->rc->output->set_env('pagecount', ceil($listsize/$pagesize));
  229. $this->rc->output->set_env('current_page', $page);
  230. $this->rc->output->command('set_rowcount',
  231. $this->get_rowcount_text($listsize, $size, $page));
  232. $this->rc->output->send();
  233. }
  234. /**
  235. * Template object for list records counter.
  236. *
  237. * @param array Object attributes
  238. *
  239. * @return string HTML output
  240. */
  241. function tpl_keys_rowcount($attrib)
  242. {
  243. if (!$attrib['id'])
  244. $attrib['id'] = 'rcmcountdisplay';
  245. $this->rc->output->add_gui_object('countdisplay', $attrib['id']);
  246. return html::span($attrib, $this->get_rowcount_text());
  247. }
  248. /**
  249. * Returns text representation of list records counter
  250. */
  251. private function get_rowcount_text($all=0, $curr_count=0, $page=1)
  252. {
  253. if (!$curr_count) {
  254. $out = $this->enigma->gettext('nokeysfound');
  255. }
  256. else {
  257. $pagesize = $this->rc->config->get('pagesize', 100);
  258. $first = ($page - 1) * $pagesize;
  259. $out = $this->enigma->gettext(array(
  260. 'name' => 'keysfromto',
  261. 'vars' => array(
  262. 'from' => $first + 1,
  263. 'to' => $first + $curr_count,
  264. 'count' => $all)
  265. ));
  266. }
  267. return $out;
  268. }
  269. /**
  270. * Key information page handler
  271. */
  272. private function key_info()
  273. {
  274. $this->enigma->load_engine();
  275. $id = rcube_utils::get_input_value('_id', rcube_utils::INPUT_GET);
  276. $res = $this->enigma->engine->get_key($id);
  277. if ($res instanceof enigma_key) {
  278. $this->data = $res;
  279. }
  280. else { // error
  281. $this->rc->output->show_message('enigma.keyopenerror', 'error');
  282. $this->rc->output->command('parent.enigma_loadframe');
  283. $this->rc->output->send('iframe');
  284. }
  285. $this->rc->output->add_handlers(array(
  286. 'keyname' => array($this, 'tpl_key_name'),
  287. 'keydata' => array($this, 'tpl_key_data'),
  288. ));
  289. $this->rc->output->set_pagetitle($this->enigma->gettext('keyinfo'));
  290. $this->rc->output->send('enigma.keyinfo');
  291. }
  292. /**
  293. * Template object for key name
  294. */
  295. function tpl_key_name($attrib)
  296. {
  297. return rcube::Q($this->data->name);
  298. }
  299. /**
  300. * Template object for key information page content
  301. */
  302. function tpl_key_data($attrib)
  303. {
  304. $out = '';
  305. $table = new html_table(array('cols' => 2));
  306. // Key user ID
  307. $table->add('title', $this->enigma->gettext('keyuserid'));
  308. $table->add(null, rcube::Q($this->data->name));
  309. // Key ID
  310. $table->add('title', $this->enigma->gettext('keyid'));
  311. $table->add(null, $this->data->subkeys[0]->get_short_id());
  312. // Key type
  313. $keytype = $this->data->get_type();
  314. if ($keytype == enigma_key::TYPE_KEYPAIR) {
  315. $type = $this->enigma->gettext('typekeypair');
  316. }
  317. else if ($keytype == enigma_key::TYPE_PUBLIC) {
  318. $type = $this->enigma->gettext('typepublickey');
  319. }
  320. $table->add('title', $this->enigma->gettext('keytype'));
  321. $table->add(null, $type);
  322. // Key fingerprint
  323. $table->add('title', $this->enigma->gettext('fingerprint'));
  324. $table->add(null, $this->data->subkeys[0]->get_fingerprint());
  325. $out .= html::tag('fieldset', null,
  326. html::tag('legend', null,
  327. $this->enigma->gettext('basicinfo')) . $table->show($attrib));
  328. // Subkeys
  329. $table = new html_table(array('cols' => 5, 'id' => 'enigmasubkeytable', 'class' => 'records-table'));
  330. $table->add_header('id', $this->enigma->gettext('subkeyid'));
  331. $table->add_header('algo', $this->enigma->gettext('subkeyalgo'));
  332. $table->add_header('created', $this->enigma->gettext('subkeycreated'));
  333. $table->add_header('expires', $this->enigma->gettext('subkeyexpires'));
  334. $table->add_header('usage', $this->enigma->gettext('subkeyusage'));
  335. $now = time();
  336. $date_format = $this->rc->config->get('date_format', 'Y-m-d');
  337. $usage_map = array(
  338. enigma_key::CAN_ENCRYPT => $this->enigma->gettext('typeencrypt'),
  339. enigma_key::CAN_SIGN => $this->enigma->gettext('typesign'),
  340. enigma_key::CAN_CERTIFY => $this->enigma->gettext('typecert'),
  341. enigma_key::CAN_AUTHENTICATE => $this->enigma->gettext('typeauth'),
  342. );
  343. foreach ($this->data->subkeys as $subkey) {
  344. $algo = $subkey->get_algorithm();
  345. if ($algo && $subkey->length) {
  346. $algo .= ' (' . $subkey->length . ')';
  347. }
  348. $usage = array();
  349. foreach ($usage_map as $key => $text) {
  350. if ($subkey->usage & $key) {
  351. $usage[] = $text;
  352. }
  353. }
  354. $table->add('id', $subkey->get_short_id());
  355. $table->add('algo', $algo);
  356. $table->add('created', $subkey->created ? $this->rc->format_date($subkey->created, $date_format, false) : '');
  357. $table->add('expires', $subkey->expires ? $this->rc->format_date($subkey->expires, $date_format, false) : $this->enigma->gettext('expiresnever'));
  358. $table->add('usage', implode(',', $usage));
  359. $table->set_row_attribs($subkey->revoked || ($subkey->expires && $subkey->expires < $now) ? 'deleted' : '');
  360. }
  361. $out .= html::tag('fieldset', null,
  362. html::tag('legend', null,
  363. $this->enigma->gettext('subkeys')) . $table->show());
  364. // Additional user IDs
  365. $table = new html_table(array('cols' => 2, 'id' => 'enigmausertable', 'class' => 'records-table'));
  366. $table->add_header('id', $this->enigma->gettext('userid'));
  367. $table->add_header('valid', $this->enigma->gettext('uservalid'));
  368. foreach ($this->data->users as $user) {
  369. $username = $user->name;
  370. if ($user->comment) {
  371. $username .= ' (' . $user->comment . ')';
  372. }
  373. $username .= ' <' . $user->email . '>';
  374. $table->add('id', rcube::Q(trim($username)));
  375. $table->add('valid', $this->enigma->gettext($user->valid ? 'valid' : 'unknown'));
  376. $table->set_row_attribs($user->revoked || !$user->valid ? 'deleted' : '');
  377. }
  378. $out .= html::tag('fieldset', null,
  379. html::tag('legend', null,
  380. $this->enigma->gettext('userids')) . $table->show());
  381. return $out;
  382. }
  383. /**
  384. * Key(s) export handler
  385. */
  386. private function key_export()
  387. {
  388. $this->rc->request_security_check(rcube_utils::INPUT_GET);
  389. $keys = rcube_utils::get_input_value('_keys', rcube_utils::INPUT_GPC);
  390. $priv = rcube_utils::get_input_value('_priv', rcube_utils::INPUT_GPC);
  391. $engine = $this->enigma->load_engine();
  392. $list = $keys == '*' ? $engine->list_keys() : explode(',', $keys);
  393. if (is_array($list)) {
  394. $filename = 'export.pgp';
  395. if (count($list) == 1) {
  396. $filename = (is_object($list[0]) ? $list[0]->id : $list[0]) . '.pgp';
  397. }
  398. // send downlaod headers
  399. header('Content-Type: application/pgp-keys');
  400. header('Content-Disposition: attachment; filename="' . $filename . '"');
  401. if ($fp = fopen('php://output', 'w')) {
  402. foreach ($list as $key) {
  403. $engine->export_key(is_object($key) ? $key->id : $key, $fp, (bool) $priv);
  404. }
  405. }
  406. }
  407. exit;
  408. }
  409. /**
  410. * Key import (page) handler
  411. */
  412. private function key_import()
  413. {
  414. // Import process
  415. if ($data = rcube_utils::get_input_value('_keys', rcube_utils::INPUT_POST)) {
  416. // Import from generation form (ajax request)
  417. $this->enigma->load_engine();
  418. $result = $this->enigma->engine->import_key($data);
  419. if (is_array($result)) {
  420. $this->rc->output->command('enigma_key_create_success');
  421. $this->rc->output->show_message('enigma.keygeneratesuccess', 'confirmation');
  422. }
  423. else {
  424. $this->rc->output->show_message('enigma.keysimportfailed', 'error');
  425. }
  426. $this->rc->output->send();
  427. }
  428. else if ($_FILES['_file']['tmp_name'] && is_uploaded_file($_FILES['_file']['tmp_name'])) {
  429. $this->enigma->load_engine();
  430. $result = $this->enigma->engine->import_key($_FILES['_file']['tmp_name'], true);
  431. if (is_array($result)) {
  432. // reload list if any keys has been added
  433. if ($result['imported']) {
  434. $this->rc->output->command('parent.enigma_list', 1);
  435. }
  436. else {
  437. $this->rc->output->command('parent.enigma_loadframe');
  438. }
  439. $this->rc->output->show_message('enigma.keysimportsuccess', 'confirmation',
  440. array('new' => $result['imported'], 'old' => $result['unchanged']));
  441. $this->rc->output->send('iframe');
  442. }
  443. else {
  444. $this->rc->output->show_message('enigma.keysimportfailed', 'error');
  445. }
  446. }
  447. else if ($err = $_FILES['_file']['error']) {
  448. if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE) {
  449. $this->rc->output->show_message('filesizeerror', 'error',
  450. array('size' => $this->rc->show_bytes(parse_bytes(ini_get('upload_max_filesize')))));
  451. } else {
  452. $this->rc->output->show_message('fileuploaderror', 'error');
  453. }
  454. }
  455. $this->rc->output->add_handlers(array(
  456. 'importform' => array($this, 'tpl_key_import_form'),
  457. ));
  458. $this->rc->output->set_pagetitle($this->enigma->gettext('keyimport'));
  459. $this->rc->output->send('enigma.keyimport');
  460. }
  461. /**
  462. * Template object for key import (upload) form
  463. */
  464. function tpl_key_import_form($attrib)
  465. {
  466. $attrib += array('id' => 'rcmKeyImportForm');
  467. $upload = new html_inputfield(array('type' => 'file', 'name' => '_file',
  468. 'id' => 'rcmimportfile', 'size' => 30));
  469. $form = html::p(null,
  470. rcube::Q($this->enigma->gettext('keyimporttext'), 'show')
  471. . html::br() . html::br() . $upload->show()
  472. );
  473. $this->rc->output->add_label('selectimportfile', 'importwait');
  474. $this->rc->output->add_gui_object('importform', $attrib['id']);
  475. $out = $this->rc->output->form_tag(array(
  476. 'action' => $this->rc->url(array('action' => $this->rc->action, 'a' => 'import')),
  477. 'method' => 'post',
  478. 'enctype' => 'multipart/form-data') + $attrib,
  479. $form);
  480. return $out;
  481. }
  482. /**
  483. * Server-side key pair generation handler
  484. */
  485. private function key_generate()
  486. {
  487. $user = rcube_utils::get_input_value('_user', rcube_utils::INPUT_POST, true);
  488. $pass = rcube_utils::get_input_value('_password', rcube_utils::INPUT_POST, true);
  489. $size = (int) rcube_utils::get_input_value('_size', rcube_utils::INPUT_POST);
  490. if ($size > 4096) {
  491. $size = 4096;
  492. }
  493. $ident = rcube_mime::decode_address_list($user, 1, false);
  494. if (empty($ident)) {
  495. $this->rc->output->show_message('enigma.keygenerateerror', 'error');
  496. $this->rc->output->send();
  497. }
  498. $this->enigma->load_engine();
  499. $result = $this->enigma->engine->generate_key(array(
  500. 'user' => $ident[1]['name'],
  501. 'email' => $ident[1]['mailto'],
  502. 'password' => $pass,
  503. 'size' => $size,
  504. ));
  505. if ($result instanceof enigma_key) {
  506. $this->rc->output->command('enigma_key_create_success');
  507. $this->rc->output->show_message('enigma.keygeneratesuccess', 'confirmation');
  508. }
  509. else {
  510. $this->rc->output->show_message('enigma.keygenerateerror', 'error');
  511. }
  512. $this->rc->output->send();
  513. }
  514. /**
  515. * Key generation page handler
  516. */
  517. private function key_create()
  518. {
  519. $this->enigma->include_script('openpgp.min.js');
  520. $this->rc->output->add_handlers(array(
  521. 'keyform' => array($this, 'tpl_key_create_form'),
  522. ));
  523. $this->rc->output->set_env('enigma_keygen_server', $this->rc->config->get('enigma_keygen_server'));
  524. $this->rc->output->set_pagetitle($this->enigma->gettext('keygenerate'));
  525. $this->rc->output->send('enigma.keycreate');
  526. }
  527. /**
  528. * Template object for key generation form
  529. */
  530. function tpl_key_create_form($attrib)
  531. {
  532. $attrib += array('id' => 'rcmKeyCreateForm');
  533. $table = new html_table(array('cols' => 2));
  534. // get user's identities
  535. $identities = $this->rc->user->list_identities(null, true);
  536. // Identity
  537. $select = new html_select(array('name' => 'identity', 'id' => 'key-ident'));
  538. foreach ((array) $identities as $idx => $ident) {
  539. $name = empty($ident['name']) ? ('<' . $ident['email'] . '>') : $ident['ident'];
  540. $select->add($name, $idx);
  541. }
  542. $table->add('title', html::label('key-name', rcube::Q($this->enigma->gettext('newkeyident'))));
  543. $table->add(null, $select->show(0));
  544. // Key size
  545. $select = new html_select(array('name' => 'size', 'id' => 'key-size'));
  546. $select->add($this->enigma->gettext('key2048'), '2048');
  547. $select->add($this->enigma->gettext('key4096'), '4096');
  548. $table->add('title', html::label('key-size', rcube::Q($this->enigma->gettext('newkeysize'))));
  549. $table->add(null, $select->show());
  550. // Password and confirm password
  551. $table->add('title', html::label('key-pass', rcube::Q($this->enigma->gettext('newkeypass'))));
  552. $table->add(null, rcube_output::get_edit_field('password', '',
  553. array('id' => 'key-pass', 'size' => $attrib['size'], 'required' => true), 'password'));
  554. $table->add('title', html::label('key-pass-confirm', rcube::Q($this->enigma->gettext('newkeypassconfirm'))));
  555. $table->add(null, rcube_output::get_edit_field('password-confirm', '',
  556. array('id' => 'key-pass-confirm', 'size' => $attrib['size'], 'required' => true), 'password'));
  557. $this->rc->output->add_gui_object('keyform', $attrib['id']);
  558. $this->rc->output->add_label('enigma.keygenerating', 'enigma.formerror',
  559. 'enigma.passwordsdiffer', 'enigma.keygenerateerror', 'enigma.nonameident',
  560. 'enigma.keygennosupport');
  561. return $this->rc->output->form_tag(array(), $table->show($attrib));
  562. }
  563. /**
  564. * Key deleting
  565. */
  566. private function key_delete()
  567. {
  568. $keys = rcube_utils::get_input_value('_keys', rcube_utils::INPUT_POST);
  569. $engine = $this->enigma->load_engine();
  570. foreach ((array)$keys as $key) {
  571. $res = $engine->delete_key($key);
  572. if ($res !== true) {
  573. $this->rc->output->show_message('enigma.keyremoveerror', 'error');
  574. $this->rc->output->command('enigma_list');
  575. $this->rc->output->send();
  576. }
  577. }
  578. $this->rc->output->command('enigma_list');
  579. $this->rc->output->show_message('enigma.keyremovesuccess', 'confirmation');
  580. $this->rc->output->send();
  581. }
  582. /**
  583. * Init compose UI (add task button and the menu)
  584. */
  585. private function compose_ui()
  586. {
  587. $this->add_css();
  588. // Options menu button
  589. $this->enigma->add_button(array(
  590. 'type' => 'link',
  591. 'command' => 'plugin.enigma',
  592. 'onclick' => "rcmail.command('menu-open', 'enigmamenu', event.target, event)",
  593. 'class' => 'button enigma',
  594. 'title' => 'encryptionoptions',
  595. 'label' => 'encryption',
  596. 'domain' => $this->enigma->ID,
  597. 'width' => 32,
  598. 'height' => 32
  599. ), 'toolbar');
  600. $locks = (array) $this->rc->config->get('enigma_options_lock');
  601. $menu = new html_table(array('cols' => 2));
  602. $chbox = new html_checkbox(array('value' => 1));
  603. $menu->add(null, html::label(array('for' => 'enigmasignopt'),
  604. rcube::Q($this->enigma->gettext('signmsg'))));
  605. $menu->add(null, $chbox->show($this->rc->config->get('enigma_sign_all') ? 1 : 0,
  606. array(
  607. 'name' => '_enigma_sign',
  608. 'id' => 'enigmasignopt',
  609. 'disabled' => in_array('sign', $locks),
  610. )));
  611. $menu->add(null, html::label(array('for' => 'enigmaencryptopt'),
  612. rcube::Q($this->enigma->gettext('encryptmsg'))));
  613. $menu->add(null, $chbox->show($this->rc->config->get('enigma_encrypt_all') ? 1 : 0,
  614. array(
  615. 'name' => '_enigma_encrypt',
  616. 'id' => 'enigmaencryptopt',
  617. 'disabled' => in_array('encrypt', $locks),
  618. )));
  619. $menu->add(null, html::label(array('for' => 'enigmaattachpubkeyopt'),
  620. rcube::Q($this->enigma->gettext('attachpubkeymsg'))));
  621. $menu->add(null, $chbox->show($this->rc->config->get('enigma_attach_pubkey') ? 1 : 0,
  622. array(
  623. 'name' => '_enigma_attachpubkey',
  624. 'id' => 'enigmaattachpubkeyopt',
  625. 'disabled' => in_array('pubkey', $locks),
  626. )));
  627. $menu = html::div(array('id' => 'enigmamenu', 'class' => 'popupmenu'), $menu->show());
  628. // Options menu contents
  629. $this->rc->output->add_footer($menu);
  630. }
  631. /**
  632. * Handler for message_body_prefix hook.
  633. * Called for every displayed (content) part of the message.
  634. * Adds infobox about signature verification and/or decryption
  635. * status above the body.
  636. *
  637. * @param array Original parameters
  638. *
  639. * @return array Modified parameters
  640. */
  641. function status_message($p)
  642. {
  643. // skip: not a message part
  644. if ($p['part'] instanceof rcube_message) {
  645. return $p;
  646. }
  647. // skip: message has no signed/encoded content
  648. if (!$this->enigma->engine) {
  649. return $p;
  650. }
  651. $engine = $this->enigma->engine;
  652. $part_id = $p['part']->mime_id;
  653. // Decryption status
  654. if (($found = $this->find_part_id($part_id, $engine->decryptions)) !== null
  655. && ($status = $engine->decryptions[$found])
  656. ) {
  657. $attach_scripts = true;
  658. // show the message only once
  659. unset($engine->decryptions[$found]);
  660. // display status info
  661. $attrib['id'] = 'enigma-message';
  662. if ($status instanceof enigma_error) {
  663. $attrib['class'] = 'enigmaerror';
  664. $code = $status->getCode();
  665. if ($code == enigma_error::KEYNOTFOUND) {
  666. $msg = rcube::Q(str_replace('$keyid', enigma_key::format_id($status->getData('id')),
  667. $this->enigma->gettext('decryptnokey')));
  668. }
  669. else if ($code == enigma_error::BADPASS) {
  670. $missing = $status->getData('missing');
  671. $label = 'decrypt' . (!empty($missing) ? 'no' : 'bad') . 'pass';
  672. $msg = rcube::Q($this->enigma->gettext($label));
  673. $this->password_prompt($status);
  674. }
  675. else {
  676. $msg = rcube::Q($this->enigma->gettext('decrypterror'));
  677. }
  678. }
  679. else if ($status === enigma_engine::ENCRYPTED_PARTIALLY) {
  680. $attrib['class'] = 'enigmawarning';
  681. $msg = rcube::Q($this->enigma->gettext('decryptpartial'));
  682. }
  683. else {
  684. $attrib['class'] = 'enigmanotice';
  685. $msg = rcube::Q($this->enigma->gettext('decryptok'));
  686. }
  687. $p['prefix'] .= html::div($attrib, $msg);
  688. }
  689. // Signature verification status
  690. if (($found = $this->find_part_id($part_id, $engine->signatures)) !== null
  691. && ($sig = $engine->signatures[$found])
  692. ) {
  693. $attach_scripts = true;
  694. // show the message only once
  695. unset($engine->signatures[$found]);
  696. // display status info
  697. $attrib['id'] = 'enigma-message';
  698. if ($sig instanceof enigma_signature) {
  699. $sender = ($sig->name ? $sig->name . ' ' : '') . '<' . $sig->email . '>';
  700. if ($sig->valid === enigma_error::UNVERIFIED) {
  701. $attrib['class'] = 'enigmawarning';
  702. $msg = str_replace('$sender', $sender, $this->enigma->gettext('sigunverified'));
  703. $msg = str_replace('$keyid', $sig->id, $msg);
  704. $msg = rcube::Q($msg);
  705. }
  706. else if ($sig->valid) {
  707. $attrib['class'] = $sig->partial ? 'enigmawarning' : 'enigmanotice';
  708. $label = 'sigvalid' . ($sig->partial ? 'partial' : '');
  709. $msg = rcube::Q(str_replace('$sender', $sender, $this->enigma->gettext($label)));
  710. }
  711. else {
  712. $attrib['class'] = 'enigmawarning';
  713. $msg = rcube::Q(str_replace('$sender', $sender, $this->enigma->gettext('siginvalid')));
  714. }
  715. }
  716. else if ($sig && $sig->getCode() == enigma_error::KEYNOTFOUND) {
  717. $attrib['class'] = 'enigmawarning';
  718. $msg = rcube::Q(str_replace('$keyid', enigma_key::format_id($sig->getData('id')),
  719. $this->enigma->gettext('signokey')));
  720. }
  721. else {
  722. $attrib['class'] = 'enigmaerror';
  723. $msg = rcube::Q($this->enigma->gettext('sigerror'));
  724. }
  725. /*
  726. $msg .= '&nbsp;' . html::a(array('href' => "#sigdetails",
  727. 'onclick' => rcmail_output::JS_OBJECT_NAME.".command('enigma-sig-details')"),
  728. rcube::Q($this->enigma->gettext('showdetails')));
  729. */
  730. // test
  731. // $msg .= '<br /><pre>'.$sig->body.'</pre>';
  732. $p['prefix'] .= html::div($attrib, $msg);
  733. }
  734. if ($attach_scripts) {
  735. // add css and js script
  736. $this->add_css();
  737. $this->add_js();
  738. }
  739. return $p;
  740. }
  741. /**
  742. * Handler for message_load hook.
  743. * Check message bodies and attachments for keys/certs.
  744. */
  745. function message_load($p)
  746. {
  747. $engine = $this->enigma->load_engine();
  748. // handle keys/certs in attachments
  749. foreach ((array) $p['object']->attachments as $attachment) {
  750. if ($engine->is_keys_part($attachment)) {
  751. $this->keys_parts[] = $attachment->mime_id;
  752. }
  753. }
  754. // the same with message bodies
  755. foreach ((array) $p['object']->parts as $part) {
  756. if ($engine->is_keys_part($part)) {
  757. $this->keys_parts[] = $part->mime_id;
  758. $this->keys_bodies[] = $part->mime_id;
  759. }
  760. }
  761. // @TODO: inline PGP keys
  762. if ($this->keys_parts) {
  763. $this->enigma->add_texts('localization');
  764. }
  765. return $p;
  766. }
  767. /**
  768. * Handler for template_object_messagebody hook.
  769. * This callback function adds a box below the message content
  770. * if there is a key/cert attachment available
  771. */
  772. function message_output($p)
  773. {
  774. foreach ($this->keys_parts as $part) {
  775. // remove part's body
  776. if (in_array($part, $this->keys_bodies)) {
  777. $p['content'] = '';
  778. }
  779. // add box below message body
  780. $p['content'] .= html::p(array('class' => 'enigmaattachment'),
  781. html::a(array(
  782. 'href' => "#",
  783. 'onclick' => "return ".rcmail_output::JS_OBJECT_NAME.".enigma_import_attachment('".rcube::JQ($part)."')",
  784. 'title' => $this->enigma->gettext('keyattimport')),
  785. html::span(null, $this->enigma->gettext('keyattfound'))));
  786. $attach_scripts = true;
  787. }
  788. if ($attach_scripts) {
  789. // add css and js script
  790. $this->add_css();
  791. $this->add_js();
  792. }
  793. return $p;
  794. }
  795. /**
  796. * Handle message_ready hook (encryption/signing/attach public key)
  797. */
  798. function message_ready($p)
  799. {
  800. $savedraft = !empty($_POST['_draft']) && empty($_GET['_saveonly']);
  801. $sign_enable = (bool) rcube_utils::get_input_value('_enigma_sign', rcube_utils::INPUT_POST);
  802. $encrypt_enable = (bool) rcube_utils::get_input_value('_enigma_encrypt', rcube_utils::INPUT_POST);
  803. $pubkey_enable = (bool) rcube_utils::get_input_value('_enigma_attachpubkey', rcube_utils::INPUT_POST);
  804. $locks = (array) $this->rc->config->get('enigma_options_lock');
  805. if (in_array('sign', $locks)) {
  806. $sign_enable = (bool) $this->rc->config->get('enigma_sign_all');
  807. }
  808. if (in_array('encrypt', $locks)) {
  809. $encrypt_enable = (bool) $this->rc->config->get('enigma_encrypt_all');
  810. }
  811. if (in_array('pubkey', $locks)) {
  812. $pubkey_enable = (bool) $this->rc->config->get('enigma_attach_pubkey');
  813. }
  814. if (!$savedraft && $pubkey_enable) {
  815. $engine = $this->enigma->load_engine();
  816. $engine->attach_public_key($p['message']);
  817. }
  818. if (!$savedraft && $sign_enable) {
  819. $engine = $this->enigma->load_engine();
  820. $status = $engine->sign_message($p['message']);
  821. $mode = 'sign';
  822. }
  823. if ((!$status instanceof enigma_error) && $encrypt_enable) {
  824. $engine = $this->enigma->load_engine();
  825. $status = $engine->encrypt_message($p['message'], null, $savedraft);
  826. $mode = 'encrypt';
  827. }
  828. if ($mode && ($status instanceof enigma_error)) {
  829. $code = $status->getCode();
  830. if ($code == enigma_error::KEYNOTFOUND) {
  831. $vars = array('email' => $status->getData('missing'));
  832. $msg = 'enigma.' . $mode . 'nokey';
  833. }
  834. else if ($code == enigma_error::BADPASS) {
  835. $this->password_prompt($status);
  836. }
  837. else {
  838. $msg = 'enigma.' . $mode . 'error';
  839. }
  840. if ($msg) {
  841. $this->rc->output->show_message($msg, $type ?: 'error', $vars);
  842. }
  843. $this->rc->output->send('iframe');
  844. }
  845. return $p;
  846. }
  847. /**
  848. * Handler for message_compose_body hook
  849. * Display error when the message cannot be encrypted
  850. * and provide a way to try again with a password.
  851. */
  852. function message_compose($p)
  853. {
  854. $engine = $this->enigma->load_engine();
  855. // skip: message has no signed/encoded content
  856. if (!$this->enigma->engine) {
  857. return $p;
  858. }
  859. $engine = $this->enigma->engine;
  860. $locks = (array) $this->rc->config->get('enigma_options_lock');
  861. // Decryption status
  862. foreach ($engine->decryptions as $status) {
  863. if ($status instanceof enigma_error) {
  864. $code = $status->getCode();
  865. if ($code == enigma_error::KEYNOTFOUND) {
  866. $msg = rcube::Q(str_replace('$keyid', enigma_key::format_id($status->getData('id')),
  867. $this->enigma->gettext('decryptnokey')));
  868. }
  869. else if ($code == enigma_error::BADPASS) {
  870. $this->password_prompt($status, array('compose-init' => true));
  871. return $p;
  872. }
  873. else {
  874. $msg = rcube::Q($this->enigma->gettext('decrypterror'));
  875. }
  876. }
  877. }
  878. if ($msg) {
  879. $this->rc->output->show_message($msg, 'error');
  880. }
  881. // Check sign/ecrypt options for signed/encrypted drafts
  882. if (!in_array('encrypt', $locks)) {
  883. $this->rc->output->set_env('enigma_force_encrypt', !empty($engine->decryptions));
  884. }
  885. if (!in_array('sign', $locks)) {
  886. $this->rc->output->set_env('enigma_force_sign', !empty($engine->signatures));
  887. }
  888. return $p;
  889. }
  890. /**
  891. * Handler for keys/certs import request action
  892. */
  893. function import_file()
  894. {
  895. $uid = rcube_utils::get_input_value('_uid', rcube_utils::INPUT_POST);
  896. $mbox = rcube_utils::get_input_value('_mbox', rcube_utils::INPUT_POST);
  897. $mime_id = rcube_utils::get_input_value('_part', rcube_utils::INPUT_POST);
  898. $storage = $this->rc->get_storage();
  899. $engine = $this->enigma->load_engine();
  900. if ($uid && $mime_id) {
  901. // Note: we get the attachment body via rcube_message class
  902. // to support keys inside encrypted messages (#5285)
  903. $message = new rcube_message($uid, $mbox);
  904. // Check if we don't need to ask for password again
  905. foreach ($engine->decryptions as $status) {
  906. if ($status instanceof enigma_error) {
  907. if ($status->getCode() == enigma_error::BADPASS) {
  908. $this->password_prompt($status, array(
  909. 'input_uid' => $uid,
  910. 'input_mbox' => $mbox,
  911. 'input_part' => $mime_id,
  912. 'input_task' => 'mail',
  913. 'input_action' => 'plugin.enigmaimport',
  914. 'action' => '?',
  915. 'iframe' => true,
  916. ));
  917. $this->rc->output->send($this->rc->output->type == 'html' ? 'iframe' : null);
  918. return;
  919. }
  920. }
  921. }
  922. if ($engine->is_keys_part($message->mime_parts[$mime_id])) {
  923. $part = $message->get_part_body($mime_id);
  924. }
  925. }
  926. if ($part && is_array($result = $engine->import_key($part))) {
  927. $this->rc->output->show_message('enigma.keysimportsuccess', 'confirmation',
  928. array('new' => $result['imported'], 'old' => $result['unchanged']));
  929. }
  930. else {
  931. $this->rc->output->show_message('enigma.keysimportfailed', 'error');
  932. }
  933. $this->rc->output->send($this->rc->output->type == 'html' ? 'iframe' : null);
  934. }
  935. /**
  936. * Check if the part or its parent exists in the array
  937. * of decryptions/signatures. Returns found ID.
  938. */
  939. private function find_part_id($part_id, $data)
  940. {
  941. $ids = explode('.', $part_id);
  942. $i = 0;
  943. $count = count($ids);
  944. while ($i < $count && strlen($part = implode('.', array_slice($ids, 0, ++$i)))) {
  945. if (array_key_exists($part, $data)) {
  946. return $part;
  947. }
  948. }
  949. }
  950. }