summaryrefslogtreecommitdiff
path: root/inbox_settings.php
blob: a9797a1c7c498635a1d091338756c46c2a2765e9 (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
<?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('preferences_sections_list',
                        array($this, 'scrub_encryption_preference'));
        $this->add_hook('settings_actions', array($this, 'amend_settings_list'));

        $this->register_action('plugin.inbox_settings', array($this, 'render_settings_ui'));
    }

    function scrub_encryption_preference($params) {
        if ($this->rcmail->config->get('inbox_settings_scrub_encryption_preference', true))
            unset($params['list']['encryption']);
        return $params;
    }

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

    function render_settings_ui() {
        $this->rcmail->output->set_pagetitle($this->gettext('inbox_settings'));
        $this->register_handler('plugin.body', array($this, 'render_settings_form'));
        $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_row(['class' => 'form-group row']);
        $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_row(['class' => 'form-group row']);
            $forwarding_form->add('title col-sm-4',
                html::label(['class' => 'col-form-label'], rcube::Q($row[0]))
                . self::get_hiddenfield('addr[]', $row[0])
            );
            $forwarding_form->add('col-sm-4',
                self::get_textfield(
                    'fwd_addr[]', $row[1], ['placeholder' => 'anyone@gmail.com']));
            $forwarding_form->add('col-sm-2 offset-1',
                self::get_checkbox(
                    'do_forward_' . $idx,
                    'do_forward[]',
                    $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([
            'class' => 'btn btn-primary submit',
            'onclick' => 'return rcmail.command(\'plugin.inbox_settings_save\', \'\', this, event)'
        ]);
        $form_buttons = html::div(['class' => 'formbuttons'],
                            $save_button->show($this->rcmail->gettext('save')));
        $page_content = html::div(['class' => 'formcontainer'],
                            $page_title
                            . $this->rcmail->output->form_tag([
                                'action' => $this->rcmail->url(
                                    ['action' => $this->rcmail->action, 'a' => 'save']),
                                'method' => 'post'                            
                            ], $form . $form_buttons));
        return $page_content;
    }

    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($name, $value, $attrs = []) {
        return (new html_inputfield([
            'type' => 'text',
            'name' => $name,
            'size' => 40,
        ] + $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 = []) {
        if (!($sql = $this->rcmail->config->get('inbox_settings_' . $qid . '_query'))) {
            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()) {
            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, $db->quote($value, 'text'), $sql);
        }

        $result = $db->query($sql);
        if ($db->is_error($result))
            return [];

        $ndresult = [];
        while ($row = $db->fetch_array($result))
            $ndresult[] = $row;

        return $ndresult;
    }

    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;
    }
}
?>