Added functionality for preconfigured mail settings. Closes #7.

This commit is contained in:
Boris Gulay
2016-06-23 00:34:22 +03:00
parent deac908a77
commit b3adf0842f
3 changed files with 130 additions and 2 deletions

View File

@@ -1,2 +1,29 @@
<?php
<?php
/*
* Preconfigured settings for different mail domains.
* Appropriate set of values is searched by mapping domain of email (from identity) to array key.
*/
$rcmail_config['ident_switch.preconfig'] = array(
# Domain part of email address
'domain.tld' => 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',
),
);

View File

@@ -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) {

View File

@@ -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))