From b3adf0842fead6674e83fdb152a12873b6c3f21c Mon Sep 17 00:00:00 2001 From: Boris Gulay Date: Thu, 23 Jun 2016 00:34:22 +0300 Subject: [PATCH] Added functionality for preconfigured mail settings. Closes #7. --- config.inc.php.dist | 31 +++++++++++++++-- ident_switch.js | 16 +++++++++ ident_switch.php | 85 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 130 insertions(+), 2 deletions(-) diff --git a/config.inc.php.dist b/config.inc.php.dist index ab4fa67..6446811 100644 --- a/config.inc.php.dist +++ b/config.inc.php.dist @@ -1,2 +1,29 @@ - array( + + # Hostname, use ssl:// or tls:// notation if needed, for no security use imap:// (must always start with scheme). + # Required. + 'host' => 'imap://mail.domain.tld', + + # Login name, can be 'email' (full address from identity), 'mbox' (only mailbox part). + # Any other value is threated ad 'not specified' (default). + 'user' => 'email', + + # Are specified settings locked in interface or not. + # Default - false. + 'readonly' => true + ), + + 'another.tld' => array( + 'host' => 'tls://mail.another.tld', + 'user' => 'mbox', + ), +); diff --git a/ident_switch.js b/ident_switch.js index d0bcd12..b923639 100644 --- a/ident_switch.js +++ b/ident_switch.js @@ -16,11 +16,27 @@ $(function() { $("INPUT[name='_ident_switch.form.enabled']").change(); $("SELECT[name='_ident_switch.form.secure']").change(); + + plugin_switchIdent_processPreconfig(); }); +function plugin_switchIdent_processPreconfig() { + var disVal = $("INPUT[name='_ident_switch.form.readonly']").val(); + if (disVal > 0) { + $("INPUT[name='_ident_switch.form.host']").prop("disabled", true); + $("SELECT[name='_ident_switch.form.secure']").prop("disabled", true); + $("INPUT[name='_ident_switch.form.port']").prop("disabled", true); + } + if (2 == disVal) { + $("INPUT[name='_ident_switch.form.username']").prop("disabled", true); + } + +} + function plugin_switchIdent_enabled_onChange(e) { var $enFld = $("INPUT[name='_ident_switch.form.enabled']"); $("INPUT[name!='_ident_switch.form.enabled'], SELECT", $enFld.parents("FIELDSET")).prop("disabled", !$enFld.is(":checked")); + plugin_switchIdent_processPreconfig(); } function plugin_switchIdent_secure_onChange(e) { diff --git a/ident_switch.php b/ident_switch.php index f13d24c..002e4e5 100644 --- a/ident_switch.php +++ b/ident_switch.php @@ -179,6 +179,7 @@ class ident_switch extends rcube_plugin 'ident_switch.form.username' => array('type' => 'text', 'size' => 64, 'placeholder' => $args['record']['email']), 'ident_switch.form.password' => array('type' => 'password', 'size' => 64), 'ident_switch.form.delimiter' => array('type' => 'text', 'size' => 1, 'placeholder' => '.'), + 'ident_switch.form.readonly' => array('type' => 'hidden'), ), ); @@ -200,7 +201,18 @@ class ident_switch extends rcube_plugin $args['record']['ident_switch.form.secure'] = 'tls'; elseif ($r['flags'] & $this->db_secure_ssl) $args['record']['ident_switch.form.secure'] = 'ssl'; + + // Set readonly if needed + $cfg = $this->get_preconfig($args['record']['email']); + if (is_array($cfg) && $cfg['readonly']) + { + $args['record']['ident_switch.form.readonly'] = 1; + if (in_array(strtoupper($cfg['user']), array('EMAIL', 'MBOX'))) + $args['record']['ident_switch.form.readonly'] = 2; + } } + else + $this->apply_preconfig($args['record']); } return $args; @@ -457,6 +469,79 @@ class ident_switch extends rcube_plugin $rc->db->query($sql, ~$this->db_enabled, $iid, $rc->user->ID); } + protected function get_preconfig($email) + { + $dom = substr(strstr($email, '@'), 1); + if (!$dom) + return false; + + //$this->load_config('config.inc.php.dist'); Don't need it yet + $this->load_config(); // config.inc.php + + $cfg = rcmail::get_instance()->config->get('ident_switch.preconfig', array()); + $cfg = $cfg[$dom]; + + if ($cfg) + { + if (!$cfg['host']) + return false; # Host must be specified! + } + return $cfg; + } + + protected function apply_preconfig(&$record) + { + $email = $record['email']; + $cfg = $this->get_preconfig($email); + if (is_array($cfg)) + { + rcmail::get_instance()->write_log( + $this->my_log, + 'Applying predefined configuration for \'' . $email . '\'.' + ); + + if ($cfg['host']) + { // Parse and set host and related + $urlArr = parse_url($cfg['host']); + + $record['ident_switch.form.host'] = $urlArr['host'] ? rcube::Q($urlArr['host'], 'url') : ''; + $record['ident_switch.form.port'] = $urlArr['port'] ? intval($urlArr['port']) : ''; + + if (strcasecmp('tls', $urlArr['scheme']) === 0) + $record['ident_switch.form.secure'] = 'tls'; + elseif (strcasecmp('ssl', $urlArr['scheme']) === 0) + $record['ident_switch.form.secure'] = 'ssl'; + else + $record['ident_switch.form.secure'] = ''; + } + + $loginSet = false; + if ($cfg['user']) + { // Set up user name + switch (strtoupper($cfg['user'])) + { + case 'EMAIL': + $record['ident_switch.form.username'] = $email; + $loginSet = true; + break; + case 'MBOX': + $record['ident_switch.form.username'] = strstr($email, '@', true); + $loginSet = true; + break; + } + } + + if ($cfg['readonly']) + { + $record['ident_switch.form.readonly'] = 1; + if ($loginSet) + $record['ident_switch.form.readonly'] = 2; + } + + return $cfg['readonly']; + } + } + protected static function ntrim($str) { if (is_null($str))