enigma_ui.php 44 KB

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