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.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. <?php
  2. /**
  3. +-------------------------------------------------------------------------+
  4. | Enigma Plugin for Roundcube |
  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. /**
  17. * This class contains only hooks and action handlers.
  18. * Most plugin logic is placed in enigma_engine and enigma_ui classes.
  19. */
  20. class enigma extends rcube_plugin
  21. {
  22. public $task = 'mail|settings';
  23. public $rc;
  24. public $engine;
  25. public $ui;
  26. private $env_loaded = false;
  27. /**
  28. * Plugin initialization.
  29. */
  30. function init()
  31. {
  32. $this->rc = rcube::get_instance();
  33. if ($this->rc->task == 'mail') {
  34. // message parse/display hooks
  35. $this->add_hook('message_part_structure', array($this, 'part_structure'));
  36. $this->add_hook('message_part_body', array($this, 'part_body'));
  37. $this->add_hook('message_body_prefix', array($this, 'status_message'));
  38. $this->register_action('plugin.enigmaimport', array($this, 'import_file'));
  39. // load the Enigma plugin configuration
  40. $this->load_config();
  41. $enabled = $this->rc->config->get('enigma_encryption', true);
  42. // message displaying
  43. if ($this->rc->action == 'show' || $this->rc->action == 'preview' || $this->rc->action == 'print') {
  44. $this->add_hook('message_load', array($this, 'message_load'));
  45. $this->add_hook('template_object_messagebody', array($this, 'message_output'));
  46. }
  47. // message composing
  48. else if ($enabled && $this->rc->action == 'compose') {
  49. $this->add_hook('message_compose_body', array($this, 'message_compose'));
  50. $this->load_ui();
  51. $this->ui->init();
  52. }
  53. // message sending (and draft storing)
  54. else if ($enabled && $this->rc->action == 'send') {
  55. $this->add_hook('message_ready', array($this, 'message_ready'));
  56. }
  57. $this->password_handler();
  58. }
  59. else if ($this->rc->task == 'settings') {
  60. // add hooks for Enigma settings
  61. $this->add_hook('settings_actions', array($this, 'settings_actions'));
  62. $this->add_hook('preferences_sections_list', array($this, 'preferences_sections_list'));
  63. $this->add_hook('preferences_list', array($this, 'preferences_list'));
  64. $this->add_hook('preferences_save', array($this, 'preferences_save'));
  65. // register handler for keys/certs management
  66. $this->register_action('plugin.enigmakeys', array($this, 'preferences_ui'));
  67. // $this->register_action('plugin.enigmacerts', array($this, 'preferences_ui'));
  68. $this->load_ui();
  69. if (empty($_REQUEST['_framed']) || strpos($this->rc->action, 'plugin.enigma') === 0) {
  70. $this->ui->add_css();
  71. }
  72. }
  73. $this->add_hook('refresh', array($this, 'refresh'));
  74. }
  75. /**
  76. * Plugin environment initialization.
  77. */
  78. function load_env()
  79. {
  80. if ($this->env_loaded) {
  81. return;
  82. }
  83. $this->env_loaded = true;
  84. // Add include path for Enigma classes and drivers
  85. $include_path = $this->home . '/lib' . PATH_SEPARATOR;
  86. $include_path .= ini_get('include_path');
  87. set_include_path($include_path);
  88. // load the Enigma plugin configuration
  89. $this->load_config();
  90. // include localization (if wasn't included before)
  91. $this->add_texts('localization/');
  92. }
  93. /**
  94. * Plugin UI initialization.
  95. */
  96. function load_ui($all = false)
  97. {
  98. if (!$this->ui) {
  99. // load config/localization
  100. $this->load_env();
  101. // Load UI
  102. $this->ui = new enigma_ui($this, $this->home);
  103. }
  104. if ($all) {
  105. $this->ui->add_css();
  106. $this->ui->add_js();
  107. }
  108. }
  109. /**
  110. * Plugin engine initialization.
  111. */
  112. function load_engine()
  113. {
  114. if ($this->engine) {
  115. return $this->engine;
  116. }
  117. // load config/localization
  118. $this->load_env();
  119. return $this->engine = new enigma_engine($this);
  120. }
  121. /**
  122. * Handler for message_part_structure hook.
  123. * Called for every part of the message.
  124. *
  125. * @param array Original parameters
  126. *
  127. * @return array Modified parameters
  128. */
  129. function part_structure($p)
  130. {
  131. $this->load_engine();
  132. return $this->engine->part_structure($p);
  133. }
  134. /**
  135. * Handler for message_part_body hook.
  136. * Called to get body of a message part.
  137. *
  138. * @param array Original parameters
  139. *
  140. * @return array Modified parameters
  141. */
  142. function part_body($p)
  143. {
  144. $this->load_engine();
  145. return $this->engine->part_body($p);
  146. }
  147. /**
  148. * Handler for settings_actions hook.
  149. * Adds Enigma settings section into preferences.
  150. *
  151. * @param array Original parameters
  152. *
  153. * @return array Modified parameters
  154. */
  155. function settings_actions($args)
  156. {
  157. // add labels
  158. $this->add_texts('localization/');
  159. // register as settings action
  160. $args['actions'][] = array(
  161. 'action' => 'plugin.enigmakeys',
  162. 'class' => 'enigma keys',
  163. 'label' => 'enigmakeys',
  164. 'title' => 'enigmakeys',
  165. 'domain' => 'enigma',
  166. );
  167. /*
  168. $args['actions'][] = array(
  169. 'action' => 'plugin.enigmacerts',
  170. 'class' => 'enigma certs',
  171. 'label' => 'enigmacerts',
  172. 'title' => 'enigmacerts',
  173. 'domain' => 'enigma',
  174. );
  175. */
  176. return $args;
  177. }
  178. /**
  179. * Handler for preferences_sections_list hook.
  180. * Adds Encryption settings section into preferences sections list.
  181. *
  182. * @param array Original parameters
  183. *
  184. * @return array Modified parameters
  185. */
  186. function preferences_sections_list($p)
  187. {
  188. $p['list']['enigma'] = array(
  189. 'id' => 'enigma', 'section' => $this->gettext('encryption'),
  190. );
  191. return $p;
  192. }
  193. /**
  194. * Handler for preferences_list hook.
  195. * Adds options blocks into Enigma settings sections in Preferences.
  196. *
  197. * @param array Original parameters
  198. *
  199. * @return array Modified parameters
  200. */
  201. function preferences_list($p)
  202. {
  203. if ($p['section'] != 'enigma') {
  204. return $p;
  205. }
  206. $no_override = array_flip((array)$this->rc->config->get('dont_override'));
  207. $p['blocks']['main']['name'] = $this->gettext('mainoptions');
  208. if (!isset($no_override['enigma_encryption'])) {
  209. if (!$p['current']) {
  210. $p['blocks']['main']['content'] = true;
  211. return $p;
  212. }
  213. $field_id = 'rcmfd_enigma_encryption';
  214. $input = new html_checkbox(array(
  215. 'name' => '_enigma_encryption',
  216. 'id' => $field_id,
  217. 'value' => 1,
  218. ));
  219. $p['blocks']['main']['options']['enigma_encryption'] = array(
  220. 'title' => html::label($field_id, $this->gettext('supportencryption')),
  221. 'content' => $input->show(intval($this->rc->config->get('enigma_encryption'))),
  222. );
  223. }
  224. if (!isset($no_override['enigma_signatures'])) {
  225. if (!$p['current']) {
  226. $p['blocks']['main']['content'] = true;
  227. return $p;
  228. }
  229. $field_id = 'rcmfd_enigma_signatures';
  230. $input = new html_checkbox(array(
  231. 'name' => '_enigma_signatures',
  232. 'id' => $field_id,
  233. 'value' => 1,
  234. ));
  235. $p['blocks']['main']['options']['enigma_signatures'] = array(
  236. 'title' => html::label($field_id, $this->gettext('supportsignatures')),
  237. 'content' => $input->show(intval($this->rc->config->get('enigma_signatures'))),
  238. );
  239. }
  240. if (!isset($no_override['enigma_decryption'])) {
  241. if (!$p['current']) {
  242. $p['blocks']['main']['content'] = true;
  243. return $p;
  244. }
  245. $field_id = 'rcmfd_enigma_decryption';
  246. $input = new html_checkbox(array(
  247. 'name' => '_enigma_decryption',
  248. 'id' => $field_id,
  249. 'value' => 1,
  250. ));
  251. $p['blocks']['main']['options']['enigma_decryption'] = array(
  252. 'title' => html::label($field_id, $this->gettext('supportdecryption')),
  253. 'content' => $input->show(intval($this->rc->config->get('enigma_decryption'))),
  254. );
  255. }
  256. if (!isset($no_override['enigma_sign_all'])) {
  257. if (!$p['current']) {
  258. $p['blocks']['main']['content'] = true;
  259. return $p;
  260. }
  261. $field_id = 'rcmfd_enigma_sign_all';
  262. $input = new html_checkbox(array(
  263. 'name' => '_enigma_sign_all',
  264. 'id' => $field_id,
  265. 'value' => 1,
  266. ));
  267. $p['blocks']['main']['options']['enigma_sign_all'] = array(
  268. 'title' => html::label($field_id, $this->gettext('signdefault')),
  269. 'content' => $input->show($this->rc->config->get('enigma_sign_all') ? 1 : 0),
  270. );
  271. }
  272. if (!isset($no_override['enigma_encrypt_all'])) {
  273. if (!$p['current']) {
  274. $p['blocks']['main']['content'] = true;
  275. return $p;
  276. }
  277. $field_id = 'rcmfd_enigma_encrypt_all';
  278. $input = new html_checkbox(array(
  279. 'name' => '_enigma_encrypt_all',
  280. 'id' => $field_id,
  281. 'value' => 1,
  282. ));
  283. $p['blocks']['main']['options']['enigma_encrypt_all'] = array(
  284. 'title' => html::label($field_id, $this->gettext('encryptdefault')),
  285. 'content' => $input->show($this->rc->config->get('enigma_encrypt_all') ? 1 : 0),
  286. );
  287. }
  288. if (!isset($no_override['enigma_attach_pubkey'])) {
  289. if (!$p['current']) {
  290. $p['blocks']['main']['content'] = true;
  291. return $p;
  292. }
  293. $field_id = 'rcmfd_enigma_attach_pubkey';
  294. $input = new html_checkbox(array(
  295. 'name' => '_enigma_attach_pubkey',
  296. 'id' => $field_id,
  297. 'value' => 1,
  298. ));
  299. $p['blocks']['main']['options']['enigma_attach_pubkey'] = array(
  300. 'title' => html::label($field_id, $this->gettext('attachpubkeydefault')),
  301. 'content' => $input->show($this->rc->config->get('enigma_attach_pubkey') ? 1 : 0),
  302. );
  303. }
  304. if (!isset($no_override['enigma_password_time'])) {
  305. if (!$p['current']) {
  306. $p['blocks']['main']['content'] = true;
  307. return $p;
  308. }
  309. $field_id = 'rcmfd_enigma_password_time';
  310. $select = new html_select(array('name' => '_enigma_password_time', 'id' => $field_id));
  311. foreach (array(1, 5, 10, 15, 30) as $m) {
  312. $label = $this->gettext(array('name' => 'nminutes', 'vars' => array('m' => $m)));
  313. $select->add($label, $m);
  314. }
  315. $select->add($this->gettext('wholesession'), 0);
  316. $p['blocks']['main']['options']['enigma_password_time'] = array(
  317. 'title' => html::label($field_id, $this->gettext('passwordtime')),
  318. 'content' => $select->show(intval($this->rc->config->get('enigma_password_time'))),
  319. );
  320. }
  321. return $p;
  322. }
  323. /**
  324. * Handler for preferences_save hook.
  325. * Executed on Enigma settings form submit.
  326. *
  327. * @param array Original parameters
  328. *
  329. * @return array Modified parameters
  330. */
  331. function preferences_save($p)
  332. {
  333. if ($p['section'] == 'enigma') {
  334. $p['prefs'] = array(
  335. 'enigma_signatures' => (bool) rcube_utils::get_input_value('_enigma_signatures', rcube_utils::INPUT_POST),
  336. 'enigma_decryption' => (bool) rcube_utils::get_input_value('_enigma_decryption', rcube_utils::INPUT_POST),
  337. 'enigma_encryption' => (bool) rcube_utils::get_input_value('_enigma_encryption', rcube_utils::INPUT_POST),
  338. 'enigma_sign_all' => (bool) rcube_utils::get_input_value('_enigma_sign_all', rcube_utils::INPUT_POST),
  339. 'enigma_encrypt_all' => (bool) rcube_utils::get_input_value('_enigma_encrypt_all', rcube_utils::INPUT_POST),
  340. 'enigma_attach_pubkey' => (bool) rcube_utils::get_input_value('_enigma_attach_pubkey', rcube_utils::INPUT_POST),
  341. 'enigma_password_time' => intval(rcube_utils::get_input_value('_enigma_password_time', rcube_utils::INPUT_POST)),
  342. );
  343. }
  344. return $p;
  345. }
  346. /**
  347. * Handler for keys/certs management UI template.
  348. */
  349. function preferences_ui()
  350. {
  351. $this->load_ui();
  352. $this->ui->init();
  353. }
  354. /**
  355. * Handler for message_body_prefix hook.
  356. * Called for every displayed (content) part of the message.
  357. * Adds infobox about signature verification and/or decryption
  358. * status above the body.
  359. *
  360. * @param array Original parameters
  361. *
  362. * @return array Modified parameters
  363. */
  364. function status_message($p)
  365. {
  366. $this->load_ui();
  367. return $this->ui->status_message($p);
  368. }
  369. /**
  370. * Handler for message_load hook.
  371. * Check message bodies and attachments for keys/certs.
  372. */
  373. function message_load($p)
  374. {
  375. $this->load_ui();
  376. return $this->ui->message_load($p);
  377. }
  378. /**
  379. * Handler for template_object_messagebody hook.
  380. * This callback function adds a box below the message content
  381. * if there is a key/cert attachment available
  382. */
  383. function message_output($p)
  384. {
  385. $this->load_ui();
  386. return $this->ui->message_output($p);
  387. }
  388. /**
  389. * Handler for attached keys/certs import
  390. */
  391. function import_file()
  392. {
  393. $this->load_ui();
  394. $this->ui->import_file();
  395. }
  396. /**
  397. * Handle password submissions
  398. */
  399. function password_handler()
  400. {
  401. $this->load_engine();
  402. $this->engine->password_handler();
  403. }
  404. /**
  405. * Handle message_ready hook (encryption/signing)
  406. */
  407. function message_ready($p)
  408. {
  409. $this->load_ui();
  410. return $this->ui->message_ready($p);
  411. }
  412. /**
  413. * Handle message_compose_body hook
  414. */
  415. function message_compose($p)
  416. {
  417. $this->load_ui();
  418. return $this->ui->message_compose($p);
  419. }
  420. /**
  421. * Handler for refresh hook.
  422. */
  423. function refresh($p)
  424. {
  425. // calling enigma_engine constructor to remove passwords
  426. // stored in session after expiration time
  427. $this->load_engine();
  428. return $p;
  429. }
  430. }