diff options
| author | Carson Fleming <[email protected]> | 2024-11-25 00:31:59 -0800 |
|---|---|---|
| committer | Carson Fleming <[email protected]> | 2024-11-25 00:31:59 -0800 |
| commit | ff3facb2614aa3e6641b125710b526f92ba421df (patch) | |
| tree | ad6385e2bd9e95e981a4c9a01f5f36a3baa135ce | |
| parent | a46266049550491849a309357270cf9aa113ee54 (diff) | |
| download | rc-inbox-settings-ff3facb2614aa3e6641b125710b526f92ba421df.tar.gz | |
working prototype, still need key management and maybe some json for smoother experience
| -rw-r--r-- | config.inc.php.dist | 8 | ||||
| -rw-r--r-- | inbox_settings.php | 76 | ||||
| -rw-r--r-- | localization/en_US.inc | 3 |
3 files changed, 67 insertions, 20 deletions
diff --git a/config.inc.php.dist b/config.inc.php.dist index b65d30d..af79ba0 100644 --- a/config.inc.php.dist +++ b/config.inc.php.dist @@ -34,7 +34,7 @@ $config['inbox_settings_encryption_enabled_query'] = 'SELECT encrypt FROM users // (in case the username is an email address) // %d - the domain part of the username // (in case the username is an email address) -// %e - whether the inbox should be encrypted (boolean) +// %se - whether the inbox should be encrypted (boolean) $config['inbox_settings_update_encryption_query'] = 'UPDATE users SET encrypt = ? WHERE username = %u'; // The SQL query used to select a user's forwarding addresses. @@ -55,10 +55,10 @@ $config['inbox_settings_forwarding_addresses_query'] = 'SELECT addr, fwd_addr, a // (in case the username is an email address) // %d - the domain part of the username // (in case the username is an email address) -// %fa - the email address being forwarded +// %aa - the email address being forwarded // (returned as `addr` from the above query) -// %fl - the local part of the forwarding address -// %fd - the domain part of the forwarding address +// %al - the local part of the forwarding address +// %ad - the domain part of the forwarding address // %sf - whether forwarding is set for this address (boolean) // %sa - the new forwarding address (string) $config['inbox_settings_update_forwarder_query'] = 'UPDATE forwarders SET fwd_addr = %sf, active = %sa WHERE addr = %fa'; diff --git a/inbox_settings.php b/inbox_settings.php index a9797a1..25e3cd0 100644 --- a/inbox_settings.php +++ b/inbox_settings.php @@ -33,9 +33,46 @@ class inbox_settings extends rcube_plugin { return $params; } + private function save_settings() { + if (empty($_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 + ]); + } + } + 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->set_pagetitle($this->gettext('inbox_settings')); + $this->save_settings(); $this->rcmail->output->send('plugin'); } @@ -48,9 +85,9 @@ class inbox_settings extends rcube_plugin { $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')))); + '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)); @@ -60,15 +97,15 @@ class inbox_settings extends rcube_plugin { $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]) + . self::get_hiddenfield('addr[' . $idx . ']', $row[0]) ); $forwarding_form->add('col-sm-4', self::get_textfield( - 'fwd_addr[]', $row[1], ['placeholder' => '[email protected]'])); + 'fwd_addr[' . $idx . ']', $row[1], ['placeholder' => '[email protected]'])); $forwarding_form->add('col-sm-2 offset-1', self::get_checkbox( - 'do_forward_' . $idx, - 'do_forward[]', + 'do_fwd_' . $idx, + 'do_fwd[' . $idx . ']', $row[2], ['title' => $this->gettext('fwd_addr')])); } @@ -81,17 +118,17 @@ class inbox_settings extends rcube_plugin { . 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'))); + $save_button = (new html_inputfield([ + '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' => $this->rcmail->action, 'a' => 'save']), + ['action' => 'plugin.inbox_settings']), 'method' => 'post' ], $form . $form_buttons)); return $page_content; @@ -122,6 +159,8 @@ class inbox_settings extends rcube_plugin { private function run_query($qid, $substitutions = []) { if (!($sql = $this->rcmail->config->get('inbox_settings_' . $qid . '_query'))) { + $this->rcmail->output->command( + 'display_message', $this->gettext('no_query'), 'error'); return []; } @@ -133,6 +172,8 @@ class inbox_settings extends rcube_plugin { } if ($db->is_error()) { + $this->rcmail->output->command( + 'display_message', $this->gettext('no_db'), 'error'); return []; } @@ -151,8 +192,11 @@ class inbox_settings extends rcube_plugin { } $result = $db->query($sql); - if ($db->is_error($result)) + if ($db->is_error($result)) { + $this->rcmail->output->command( + 'display_message', $this->gettext('sql_error'), 'error'); return []; + } $ndresult = []; while ($row = $db->fetch_array($result)) diff --git a/localization/en_US.inc b/localization/en_US.inc index 80d21ee..e13e32d 100644 --- a/localization/en_US.inc +++ b/localization/en_US.inc @@ -5,4 +5,7 @@ $labels['encryption'] = 'Encryption'; $labels['encrypt_inbox'] = 'Encrypt incoming mail to my inbox'; $labels['forwarding'] = 'Forwarding'; $labels['fwd_addr'] = 'Forward mail to this address'; +$labels['no_query'] = 'One or more database queries are not yet configured'; +$labels['no_db'] = 'Failed to connect to the database'; +$labels['sql_error'] = 'Encountered an error while performing the operation; please check your SQL syntax and database DSN'; ?> |
