summaryrefslogtreecommitdiff
path: root/inbox_settings.php
blob: eb096d8f80ae490b1a074644d82a67fe5c85d780 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
<?php
class inbox_settings extends rcube_plugin {
    public $task = 'settings';
    private $rcmail;

    function init() {
        $this->rcmail = rcmail::get_instance();

        $this->load_config();
        $this->add_texts('localization/');

        $this->add_hook('settings_actions', [$this, 'amend_settings_list']);
        if ($this->rcmail->config->get('inbox_settings_scrub_encryption_preference', true))
            $this->add_hook(
                'preferences_sections_list',
                [$this, 'scrub_encryption_preference']);

        $this->register_action('plugin.inbox_settings', [$this, 'render_settings_ui']);
        $this->register_action('plugin.inbox_keys', [$this, 'render_keys_ui']);
        $this->register_action('plugin.inbox_keys_generate', [$this, 'render_generate_ui']);
        $this->register_action('plugin.inbox_keys_import', [$this, 'render_import_ui']);
        $this->register_action('plugin.inbox_keys_delete', [$this, 'delete_selected_keys']);
    }

    function scrub_encryption_preference($params) {
        unset($params['list']['encryption']);
        return $params;
    }

    function amend_settings_list($params) {
        $params['actions'][] = [
            'action' => 'plugin.inbox_settings',
            'class' => 'inbox_settings',
            'label' => 'inbox_settings',
            'title' => 'inbox_settings',
            'domain' => 'inbox_settings'
        ];
        $params['actions'][] = [
            'action' => 'plugin.inbox_keys',
            'class' => 'enigma keys',
            'label' => 'inbox_keys',
            'title' => 'inbox_keys',
            'domain' => 'inbox_settings'
        ];
        return $params;
    }

    private function save_settings() {
        if (!isset($_POST['save']))
            return;

        $encrypt_inbox = isset($_POST['encrypt_inbox']) ? '1' : '0';
        $this->run_query('update_encryption', ['%se' => $encrypt_inbox]);

        if (empty($_POST['addr']) || !is_array($_POST['addr']))
            return;

        foreach ($_POST['addr'] as $idx => $addr) {
            if (!empty($_POST['fwd_addr'][$idx])
                && !rcube_utils::check_email($_POST['fwd_addr'][$idx]))
                continue;

            $fwd_addr = $_POST['fwd_addr'][$idx];
            $fwd_addr = empty($fwd_addr) ? '' : $fwd_addr;
            $do_forward = isset($_POST['do_fwd'][$idx]) ? '1' : '0';
            
            $local_part = $addr;
            $domain_part = '';
            if ($at_separator = strrpos($addr, '@')) {
                $local_part = substr($addr, 0, $at_separator);
                $domain_part = substr($addr, $at_separator + 1);
            }

            $this->run_query('update_forwarder', [
                '%aa' => $addr,
                '%al' => $local_part,
                '%ad' => $domain_part,
                '%sf' => $do_forward,
                '%sa' => $fwd_addr
            ]);
        }

        $this->rcmail->output->command(
            'display_message', $this->gettext('settings_saved'), 'notice');
    }

    function render_settings_ui() {
        $this->register_handler('plugin.body', [$this, 'render_settings_form']);
        $this->rcmail->output->set_pagetitle($this->gettext('inbox_settings'));
        $this->save_settings();
        $this->rcmail->output->send('plugin');
    }

    function render_settings_form() {
        $page_title = html::tag('h1', 'voice', rcube::Q($this->gettext('inbox_settings')));
        $encryption_result = $this->run_query('encryption_enabled');
        $encrypt_inbox = 0;
        if (!empty($encryption_result) && !empty($encryption_result[0]))
            $encrypt_inbox = $encryption_result[0][0] ? 1 : 0;

        $encryption_form = new html_table(['cols' => 2, 'class' => 'propform']);
        $encryption_form->add('title col-sm-8', html::label(
            ['class' => 'col-form-label', 'for' => 'rcmfd_encrypt_inbox'],
            rcube::Q($this->gettext('encrypt_inbox'))));
        $encryption_form->add('col-sm-2 offset-1', self::get_checkbox(
            'rcmfd_encrypt_inbox', 'encrypt_inbox', $encrypt_inbox));

        $forwarders = $this->run_query('forwarding_addresses');
        $forwarding_form = new html_table(['cols' => 3, 'class' => 'propform']);
        foreach ($forwarders as $idx => $row) {
            $forwarding_form->add('title col-sm-4',
                html::label(
                    ['class' => 'col-form-label', 'for' => 'fwd_addr_' . $idx],
                    rcube::Q($row[0]))
                . self::get_hiddenfield('addr[' . $idx . ']', $row[0]));
            $forwarding_form->add('col-sm-4',
                self::get_textfield(
                    'fwd_addr_' . $idx,
                    'fwd_addr[' . $idx . ']',
                    $row[1],
                    ['placeholder' => 'anyone@gmail.com']));
            $forwarding_form->add('col-sm-2 offset-1',
                self::get_checkbox(
                    'do_fwd_' . $idx,
                    'do_fwd[' . $idx . ']',
                    $row[2],
                    ['title' => $this->gettext('fwd_addr')]));
        }

        $form = html::div(['class' => 'formcontent'],
                    html::tag('fieldset', null,
                        html::tag('legend', null, rcube::Q($this->gettext('encryption')))
                        . $encryption_form->show(null)
                    )
                  . html::tag('fieldset', null,
                        html::tag('legend', null, rcube::Q($this->gettext('forwarding')))
                        . $forwarding_form->show(null)));
        $save_button = (new html_button([
                'type' => 'submit',
                'name' => 'save',
                'class' => 'button mainaction submit'
            ]))->show($this->rcmail->gettext('save'));
        $form_buttons = html::div(['class' => 'formbuttons'], $save_button);
        $page_content = html::div(['class' => 'formcontainer'],
                            $page_title
                            . $this->rcmail->output->form_tag([
                                'action' => $this->rcmail->url(
                                    ['action' => 'plugin.inbox_settings']),
                                'method' => 'post'                            
                            ], $form . $form_buttons));
        return $page_content;
    }

    function render_keys_ui() {
        $this->rcmail->output->add_handler('keyslist', [$this, 'render_keys_list']);
        $this->rcmail->output->set_pagetitle($this->gettext('inbox_keys'));
        $this->rcmail->output->send('inbox_settings.keys');
    }

    function render_keys_list($attrib) {
        if (empty($attrib['id']))
            $attrib['id'] = 'rcpgpkeyslist';

        // info the js needs access to
        $this->rcmail->output->add_gui_object('keyslist', $attrib['id']);
        $this->rcmail->output->add_label(
            'inbox_settings.generating_key',
            'inbox_settings.importing_key',
            'inbox_settings.confirm_delete_key',
            'inbox_settings.deleting_key');

        $this->rcmail->output->include_script('list.js');
        $this->include_script('openpgp.min.js');
        $this->include_script('keys.js');

        $data = self::label_assoc_2d($this->run_query('keys', [], true));
        if (empty($data)) {
            $this->rcmail->output->command(
                'display_message', $this->gettext('no_keys'), 'notice');
        }

        return rcmail_action::table_output(
            $attrib,
            $data,
            ['inbox_settings.fingerprint', 'inbox_settings.comment'],
            'inbox_settings.id');
    }

    function render_generate_ui() {
        $this->rcmail->output->add_handler('plugin.body', [$this, 'render_generate_form']);
        $this->rcmail->output->set_pagetitle($this->gettext('keygen'));
        $this->rcmail->output->send('plugin');
    }

    function render_generate_form() {
        return '<p>Not yet implemented.</p>';
    }

    private function import_keys() {
        if (!isset($_POST['import']))
            return false;

        try {
            $dearmored_key_data = self::parse_keys($_POST['key_data']);
        } catch (Throwable $e) {
            $this->rcmail->output->command(
                'display_message', $this->gettext('invalid_pgp_data'), 'error');
            return false;
        }
        if (empty($dearmored_key_data)) {
            $this->rcmail->output->command(
                'display_message', $this->gettext('no_key_data'), 'error');
            return false;
        }

        $comment = $_POST['comment'];
        if (empty($comment)) $comment = null;

        foreach ($dearmored_key_data as $parsed_key) {
            $this->run_query(
                'add_key',
                ['%f' => $parsed_key['fingerprint'],
                 '%k' => $parsed_key['data_blob'],
                 '%c' => $comment]);
        }

        $this->rcmail->output->command(
            'display_message', $this->gettext('keys_imported'), 'notice');
        return true;
    }

    function render_import_ui() {
        $this->rcmail->output->set_pagetitle($this->gettext('key_import'));
        if ($this->import_keys())
            $this->rcmail->output->redirect('plugin.inbox_keys');
        else
            $this->rcmail->output->send('inbox_settings.key_import');
    }

    function delete_selected_keys() {
        $this->run_query('delete_keys', ['%k' => $_POST['keys']]);
        $this->rcmail->output->command(
            'display_message', $this->gettext('keys_deleted'), 'notice');
        $this->rcmail->output->send();
    }

    private static function label_assoc_2d($data) {
        $labeled_data = [];
        foreach ($data as $row) {
            $labeled_row = [];
            foreach ($row as $col_name => $value) {
                $labeled_row['inbox_settings.' . $col_name] = $value;
            }
            $labeled_data[] = $labeled_row;
        }
        return $labeled_data;
    }

    private static function get_checkbox($id, $name, $checked, $attrs = []) {
        return html::div(['class' => 'custom-control custom-switch'],
            (new html_checkbox([
                'id' => $id,
                'name' => $name,
                'class' => 'form-check-input custom-control-input',
                'value' => '1'
            ] + $attrs))->show($checked ? '1' : '0')
            . html::label(['class' => 'custom-control-label', 'for' => $id] + $attrs, ''));
    }

    private static function get_textfield($id, $name, $value = '', $attrs = []) {
        return (new html_inputfield([
            'id' => $id,
            'type' => 'text',
            'name' => $name,
            'size' => 40,
        ] + $attrs))->show($value);
    }

    private static function get_textarea($id, $name, $value = '', $attrs = []) {
        return (new html_textarea([
            'id' => $id,
            'rows' => 10,
            'name' => $name,
        ] + $attrs))->show($value);
    }

    private static function get_hiddenfield($name, $value) {
        return (new html_inputfield(['type' => 'hidden', 'name' => $name]))->show($value);
    }

    private function run_query($qid, $substitutions = [], $assoc = false) {
        if (!($sql = $this->rcmail->config->get('inbox_settings_' . $qid . '_query'))) {
            $this->rcmail->output->command(
                'display_message', $this->gettext('no_query'), 'error');
            return [];
        }

        if ($dsn = $this->rcmail->config->get('inbox_settings_db_dsn')) {
            $db = rcube_db::factory(self::parse_dsn($dsn), '', false);
            $db->set_debug((bool) $this->rcmail->config->get('sql_debug'));
        } else {
            $db = $this->rcmail->get_dbh();
        }

        if ($db->is_error()) {
            $this->rcmail->output->command(
                'display_message', $this->gettext('no_db'), 'error');
            return [];
        }

        $local_part = $this->rcmail->user->get_username('local');
        $domain_part = rcube_utils::idn_to_utf8($this->rcmail->user->get_username('domain'));
        $username = rcube_utils::idn_to_utf8($_SESSION['username']);
        $host = rcube_utils::idn_to_utf8($_SESSION['imap_host']);

        $sql = str_replace('%l', $db->quote($local_part, 'text'), $sql);
        $sql = str_replace('%d', $db->quote($domain_part, 'text'), $sql);
        $sql = str_replace('%u', $db->quote($username, 'text'), $sql);
        $sql = str_replace('%h', $db->quote($host, 'text'), $sql);

        foreach ($substitutions as $key => $value) {
            $sql = str_replace($key, self::quote_replacement($db, $value), $sql);
        }

        $result = $db->query($sql);
        if ($db->is_error($result)) {
            $this->rcmail->output->command(
                'display_message', $this->gettext('sql_error'), 'error');
            return [];
        }

        $ndresult = [];
        while ($row = $assoc ? $db->fetch_assoc($result) : $db->fetch_array($result))
            $ndresult[] = $row;

        return $ndresult;
    }

    private static function quote_replacement($db, $value) {
        if (!is_array($value)) return $db->quote($value, 'text');
        $quoted = [];
        foreach ($value as $item) {
            $quoted[] = self::quote_replacement($db, $item);
        }
        return '(' . implode(', ', $quoted) . ')';
    }

    private static function parse_dsn($dsn) {
        if (strpos($dsn, '%')) {
            // parse DSN and replace variables in hostname
            $parsed = rcube_db::parse_dsn($dsn);
            $host = rcube_utils::parse_host($parsed['hostspec']);

            // build back the DSN string
            if ($host != $parsed['hostspec']) {
                $dsn = str_replace('@' . $parsed['hostspec'], '@' . $host, $dsn);
            }
        }

        return $dsn;
    }

    private static function parse_keys($armored) {
        require_once(__DIR__ . '/openpgp.php');

        $dearmored = OpenPGP::unarmor($armored);
        if (empty($dearmored))
            return null;

        $parsed_data = OpenPGP_Message::parse($dearmored);
        if (!($parsed_data instanceof OpenPGP_Message) || !is_array($parsed_data->packets))
            throw new IllegalArgumentException(
                'Input data was not a PGP message with data packets.');

        $parsed_keys = [];
        $fingerprint = null;
        $data_message = new OpenPGP_Message();
        foreach ($parsed_data->packets as $data_packet) {
            if ($data_packet instanceof OpenPGP_SecretKeyPacket)
                throw new IllegalArgumentException('Input data contains private keys.');

            if ($data_packet instanceof OpenPGP_PublicKeyPacket
                && !($data_packet instanceof OpenPGP_PublicSubkeyPacket)) {
                $parsed_keys[] = [
                    'fingerprint' => $fingerprint, 'data_blob' => $data_message->to_bytes()];
                $fingerprint = $data_packet->fingerprint;
                $data_message = new OpenPGP_Message();
            } else if (!($data_packet instanceof OpenPGP_UserIDPacket)
                && !($data_packet instanceof OpenPGP_SignaturePacket)
                && !($data_packet instanceof OpenPGP_PublicSubkeyPacket)) {
                // ignore unknown packet types for the time being
                continue;
            }

            $data_message[] = $data_packet;
        }

        if (!is_null($fingerprint) && !empty($data_message->packets)) {
            $parsed_keys[] = [
                'fingerprint' => $fingerprint, 'data_blob' => $data_message->to_bytes()];
        }
        return $parsed_keys;
    }
}
?>