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