diff options
| author | Carson Fleming <[email protected]> | 2024-11-24 21:27:12 -0800 |
|---|---|---|
| committer | Carson Fleming <[email protected]> | 2024-11-24 21:27:12 -0800 |
| commit | 1c334f7d6760f978df20ed3ff405cfa7dfaf9efd (patch) | |
| tree | a0907869eb52429276901c2dec5a3e8bb6c21102 | |
| download | rc-inbox-settings-1c334f7d6760f978df20ed3ff405cfa7dfaf9efd.tar.gz | |
may need to revert this
| -rw-r--r-- | .gitignore | 5 | ||||
| -rw-r--r-- | config.inc.php.dist | 65 | ||||
| -rw-r--r-- | inbox_settings.php | 188 | ||||
| -rw-r--r-- | localization/en_US.inc | 8 |
4 files changed, 266 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a9e8e02 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +composer.phar +/vendor/ + +# Config +config.inc.php diff --git a/config.inc.php.dist b/config.inc.php.dist new file mode 100644 index 0000000..7e5f055 --- /dev/null +++ b/config.inc.php.dist @@ -0,0 +1,65 @@ +<?php +$config = []; + +// PEAR database DSN for performing the query. By default +// Roundcube DB settings are used. +// Supported replacement variables: +// %h - user's IMAP hostname +// %n - hostname ($_SERVER['SERVER_NAME']) +// %t - hostname without the first part +// %d - domain (http hostname $_SERVER['HTTP_HOST'] without the first part) +// %z - IMAP domain (IMAP hostname without the first part) +$config['inbox_settings_db_dsn'] = ''; + +// Whether we should scrub the "Encryption" tab from the preferences list. +// This may be confusing to some users since this plugin adds additional +// encryption settings in a different place. +$config['inbox_settings_scrub_encryption_preference'] = true; + +// The SQL query used to select whether a user's inbox is encrypted. +// Supported replacement variables: +// %h - user's IMAP hostname +// %u - the username (from the session info) +// %l - the local part of the username +// (in case the username is an email address) +// %d - the domain part of the username +// (in case the username is an email address) +$config['inbox_settings_encryption_enabled_query'] = 'SELECT encrypt FROM users WHERE username = %u'; + +// The SQL query used to update whether a user's inbox is encrypted. +// Supported replacement variables: +// %h - user's IMAP hostname +// %u - the username (from the session info) +// %l - the local part of the username +// (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) +$config['inbox_settings_encryption_enabled_query'] = 'UPDATE users SET encrypt = ? WHERE username = %u'; + +// The SQL query used to select a user's forwarding addresses. +// Supported replacement variables: +// %h - user's IMAP hostname +// %u - the username (from the session info) +// %l - the local part of the username +// (in case the username is an email address) +// %d - the domain part of the username +// (in case the username is an email address) +$config['inbox_settings_forwarding_addresses_query'] = 'SELECT addr, fwd_addr, active FROM forwarders WHERE username = %u'; + +// The SQL query used to update a user's forwarding setting for an address. +// Supported replacement variables: +// %h - user's IMAP hostname +// %u - the username (from the session info) +// %l - the local part of the username +// (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 +// (returned as `addr` from the above query) +// %fl - the local part of the forwarding address +// %fd - 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_forwarding_update_query'] = 'UPDATE forwarders SET fwd_addr = %sf, active = %sa WHERE addr = %fa'; +?> diff --git a/inbox_settings.php b/inbox_settings.php new file mode 100644 index 0000000..2da26d8 --- /dev/null +++ b/inbox_settings.php @@ -0,0 +1,188 @@ +<?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')); + // notices and messages + $this->rcmail->output->send('plugin'); + } + + function render_settings_form() { + $page_title = html::tag('h1', 'voice', $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]); + $encryption_form->add_row(['class' => 'form-group row']); + $encryption_form->add('title col-sm-6', html::label([ + 'class' => 'col-form-label', + 'for' => 'rcmfd_encrypt_inbox' + ], $this->gettext('encrypt_inbox'))); + $encryption_form->add(null, + html::div(['class' => 'custom-control custom-switch'], + (new html_checkbox( + ['id' => 'rcmfd_encrypt_inbox', + 'name' => 'encrypt_inbox', + 'class' => 'form-check-input custom-control-input', + 'value' => 1] + ))->show($encrypt_inbox) + . html::label([ + 'class' => 'custom-control-label', + 'for' => 'rcmfd_encrypt_inbox' + ], ''))); + + $forwarding_form = 'forwarding here'; + + $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'))); + $form = html::div(null, + html::tag('fieldset', null, + html::tag('legend', null, $this->gettext('encryption')) + . $encryption_form->show(null) + ) + . html::tag('fieldset', null, + html::tag('legend', null, $this->gettext('forwarding')) + . $forwarding_form)); + $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' + ], html::div(['class' => 'formcontent'], $form) . $form_buttons)); + return $page_content; + } + + 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 function get_permitted_addresses() { + if (!($sql = $this->rcmail->config->get('identity_addresses_query'))) { + return []; + } + + if ($dsn = $this->rcmail->config->get('identity_addresses_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); + + $result = $db->query($sql); + if ($db->is_error($result)) + return []; + + $permitted_addresses = []; + while ($row = $db->fetch_array($result)) + $permitted_addresses[] = $row[0]; + + return $permitted_addresses; + } + + 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; + } +} +?> diff --git a/localization/en_US.inc b/localization/en_US.inc new file mode 100644 index 0000000..80d21ee --- /dev/null +++ b/localization/en_US.inc @@ -0,0 +1,8 @@ +<?php +$labels = []; +$labels['inbox_settings'] = 'Inbox Settings'; +$labels['encryption'] = 'Encryption'; +$labels['encrypt_inbox'] = 'Encrypt incoming mail to my inbox'; +$labels['forwarding'] = 'Forwarding'; +$labels['fwd_addr'] = 'Forward mail to this address'; +?> |
