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 /inbox_settings.php | |
| download | rc-inbox-settings-1c334f7d6760f978df20ed3ff405cfa7dfaf9efd.tar.gz | |
may need to revert this
Diffstat (limited to 'inbox_settings.php')
| -rw-r--r-- | inbox_settings.php | 188 |
1 files changed, 188 insertions, 0 deletions
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; + } +} +?> |
