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 45KB

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