1<?php
2
3/**
4 +-----------------------------------------------------------------------+
5 | This file is part of the Roundcube Webmail client                     |
6 |                                                                       |
7 | Copyright (C) The Roundcube Dev Team                                  |
8 |                                                                       |
9 | Licensed under the GNU General Public License version 3 or            |
10 | any later version with exceptions for skins & plugins.                |
11 | See the README file for a full license statement.                     |
12 |                                                                       |
13 | PURPOSE:                                                              |
14 |   Show edit form for an identity record                               |
15 +-----------------------------------------------------------------------+
16 | Author: Thomas Bruederli <roundcube@gmail.com>                        |
17 +-----------------------------------------------------------------------+
18*/
19
20class rcmail_action_settings_identity_edit extends rcmail_action
21{
22    protected static $mode = self::MODE_HTTP;
23    protected static $record;
24
25    /**
26     * Request handler.
27     *
28     * @param array $args Arguments from the previous step(s)
29     */
30    public function run($args = [])
31    {
32        $rcmail = rcmail::get_instance();
33
34        $IDENTITIES_LEVEL = intval($rcmail->config->get('identities_level', 0));
35
36        // edit-identity
37        if ((!empty($_GET['_iid']) || !empty($_POST['_iid'])) && $rcmail->action == 'edit-identity') {
38            $id = rcube_utils::get_input_value('_iid', rcube_utils::INPUT_GPC);
39            self::$record = $rcmail->user->get_identity($id);
40
41            if (!is_array(self::$record)) {
42                $rcmail->output->show_message('dberror', 'error');
43                // go to identities page
44                $rcmail->overwrite_action('identities');
45                return;
46            }
47
48            $rcmail->output->set_env('iid', self::$record['identity_id']);
49            $rcmail->output->set_env('mailvelope_main_keyring', $rcmail->config->get('mailvelope_main_keyring'));
50            $rcmail->output->set_env('mailvelope_keysize', $rcmail->config->get('mailvelope_keysize'));
51        }
52        // add-identity
53        else {
54            if ($IDENTITIES_LEVEL > 1) {
55                $rcmail->output->show_message('opnotpermitted', 'error');
56                // go to identities page
57                $rcmail->overwrite_action('identities');
58                return;
59            }
60
61            if ($IDENTITIES_LEVEL == 1) {
62                self::$record['email'] = $rcmail->get_user_email();
63            }
64        }
65
66        $rcmail->output->add_handler('identityform', [$this, 'identity_form']);
67        $rcmail->output->set_env('identities_level', $IDENTITIES_LEVEL);
68        $rcmail->output->add_label('deleteidentityconfirm', 'generate',
69            'encryptioncreatekey', 'openmailvelopesettings', 'encryptionprivkeysinmailvelope',
70            'encryptionnoprivkeysinmailvelope', 'keypaircreatesuccess');
71
72        $rcmail->output->set_pagetitle($rcmail->gettext(($rcmail->action == 'add-identity' ? 'addidentity' : 'editidentity')));
73
74        if ($rcmail->action == 'add-identity' && $rcmail->output->template_exists('identityadd')) {
75            $rcmail->output->send('identityadd');
76        }
77
78        $rcmail->output->send('identityedit');
79    }
80
81    public static function identity_form($attrib)
82    {
83        $rcmail = rcmail::get_instance();
84
85        $IDENTITIES_LEVEL = intval($rcmail->config->get('identities_level', 0));
86
87        // Add HTML editor script(s)
88        self::html_editor('identity');
89
90        // add some labels to client
91        $rcmail->output->add_label('noemailwarning', 'converting', 'editorwarning');
92
93        $i_size = !empty($attrib['size']) ? $attrib['size'] : 40;
94        $t_rows = !empty($attrib['textarearows']) ? $attrib['textarearows'] : 6;
95        $t_cols = !empty($attrib['textareacols']) ? $attrib['textareacols'] : 40;
96
97        // list of available cols
98        $form = [
99            'addressing' => [
100                'name'    => $rcmail->gettext('settings'),
101                'content' => [
102                    'name'         => ['type' => 'text', 'size' => $i_size],
103                    'email'        => ['type' => 'text', 'size' => $i_size],
104                    'organization' => ['type' => 'text', 'size' => $i_size],
105                    'reply-to'     => ['type' => 'text', 'size' => $i_size],
106                    'bcc'          => ['type' => 'text', 'size' => $i_size],
107                    'standard'     => ['type' => 'checkbox', 'label' => $rcmail->gettext('setdefault')],
108                ]
109            ],
110            'signature' => [
111                'name'    => $rcmail->gettext('signature'),
112                'content' => [
113                    'signature'      => [
114                        'type'       => 'textarea',
115                        'size'       => $t_cols,
116                        'rows'       => $t_rows,
117                        'spellcheck' => true,
118                        'data-html-editor' => true
119                    ],
120                    'html_signature' => [
121                        'type' => 'checkbox',
122                        'label'   => $rcmail->gettext('htmlsignature'),
123                        'onclick' => "return rcmail.command('toggle-editor', {id: 'rcmfd_signature', html: this.checked}, '', event)"
124                    ],
125                ]
126            ],
127            'encryption' => [
128                'name'    => $rcmail->gettext('identityencryption'),
129                'attrs'   => ['class' => 'identity-encryption', 'style' => 'display:none'],
130                'content' => html::div('identity-encryption-block', '')
131            ]
132        ];
133
134        // Enable TinyMCE editor
135        if (!empty(self::$record['html_signature'])) {
136            $form['signature']['content']['signature']['class']      = 'mce_editor';
137            $form['signature']['content']['signature']['is_escaped'] = true;
138
139            // Correctly handle HTML entities in HTML editor (#1488483)
140            self::$record['signature'] = htmlspecialchars(self::$record['signature'], ENT_NOQUOTES, RCUBE_CHARSET);
141        }
142
143        // hide "default" checkbox if only one identity is allowed
144        if ($IDENTITIES_LEVEL > 1) {
145            unset($form['addressing']['content']['standard']);
146        }
147
148        // disable some field according to access level
149        if ($IDENTITIES_LEVEL == 1 || $IDENTITIES_LEVEL == 3) {
150            $form['addressing']['content']['email']['disabled'] = true;
151            $form['addressing']['content']['email']['class']    = 'disabled';
152        }
153
154        if ($IDENTITIES_LEVEL == 4) {
155            foreach ($form['addressing']['content'] as $formfield => $value){
156                $form['addressing']['content'][$formfield]['disabled'] = true;
157                $form['addressing']['content'][$formfield]['class']    = 'disabled';
158            }
159        }
160
161        if (!empty(self::$record['email'])) {
162            self::$record['email'] = rcube_utils::idn_to_utf8(self::$record['email']);
163        }
164
165        // Allow plugins to modify identity form content
166        $plugin = $rcmail->plugins->exec_hook('identity_form', [
167                'form'   => $form,
168                'record' => self::$record
169        ]);
170
171        $form = $plugin['form'];
172        self::$record = $plugin['record'];
173
174        // Set form tags and hidden fields
175        list($form_start, $form_end) = self::get_form_tags($attrib, 'save-identity',
176            isset(self::$record['identity_id']) ? intval(self::$record['identity_id']) : 0,
177            ['name' => '_iid', 'value' => isset(self::$record['identity_id']) ? self::$record['identity_id'] : 0]
178        );
179
180        unset($plugin);
181        unset($attrib['form'], $attrib['id']);
182
183        // return the complete edit form as table
184        $out = "$form_start\n";
185
186        foreach ($form as $fieldset) {
187            if (empty($fieldset['content'])) {
188                continue;
189            }
190
191            $content = '';
192            if (is_array($fieldset['content'])) {
193                $table = new html_table(['cols' => 2]);
194
195                foreach ($fieldset['content'] as $col => $colprop) {
196                    $colprop['id'] = 'rcmfd_'.$col;
197
198                    if (!empty($colprop['label'])) {
199                        $label = $colprop['label'];
200                    }
201                    else {
202                        $label = $rcmail->gettext(str_replace('-', '', $col));
203                    }
204
205                    if (!empty($colprop['value'])) {
206                        $value = $colprop['value'];
207                    }
208                    else {
209                        $val   = isset(self::$record[$col]) ? self::$record[$col] : '';
210                        $value = rcube_output::get_edit_field($col, $val, $colprop, $colprop['type']);
211                    }
212
213                    $table->add('title', html::label($colprop['id'], rcube::Q($label)));
214                    $table->add(null, $value);
215                }
216
217                $content = $table->show($attrib);
218            }
219            else {
220                $content = $fieldset['content'];
221            }
222
223            $content = html::tag('legend', null, rcube::Q($fieldset['name'])) . $content;
224            $out .= html::tag('fieldset', !empty($fieldset['attrs']) ? $fieldset['attrs'] : [], $content) . "\n";
225        }
226
227        $out .= $form_end;
228
229        // add image upload form
230        $max_size = self::upload_init($rcmail->config->get('identity_image_size', 64) * 1024);
231        $form_id  = 'identityImageUpload';
232
233        $out .= '<form id="' . $form_id . '" style="display: none">'
234            . html::div('hint', $rcmail->gettext(['name' => 'maxuploadsize', 'vars' => ['size' => $max_size]]))
235            . '</form>';
236
237        $rcmail->output->add_gui_object('uploadform', $form_id);
238
239        return $out;
240    }
241}
242