Fixes: centralize helpers, remove dead code, fix JS issues
This commit is contained in:
@@ -46,7 +46,7 @@ $(function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// IMAP host → update SMTP/Sieve host placeholders
|
// IMAP host → update SMTP/Sieve host placeholders
|
||||||
$("INPUT[name='_ident_switch.form.imap.host']").on('input change blur', function() {
|
$("INPUT[name='_ident_switch.form.imap.host']").on('change blur', function() {
|
||||||
var imapHost = $(this).val() || 'localhost';
|
var imapHost = $(this).val() || 'localhost';
|
||||||
$("INPUT[name='_ident_switch.form.smtp.host']").attr('placeholder', imapHost);
|
$("INPUT[name='_ident_switch.form.smtp.host']").attr('placeholder', imapHost);
|
||||||
$("INPUT[name='_ident_switch.form.sieve.host']").attr('placeholder', imapHost);
|
$("INPUT[name='_ident_switch.form.sieve.host']").attr('placeholder', imapHost);
|
||||||
@@ -294,7 +294,7 @@ function plugin_switchIdent_applyJsPreconfig(cfg, email) {
|
|||||||
var port = cfg[proto].port;
|
var port = cfg[proto].port;
|
||||||
var defaultPort = ident_switch_portDefaults[proto][security] || '';
|
var defaultPort = ident_switch_portDefaults[proto][security] || '';
|
||||||
$("INPUT[name='_ident_switch.form." + proto + ".port']").val(
|
$("INPUT[name='_ident_switch.form." + proto + ".port']").val(
|
||||||
(port && port != defaultPort) ? port : ''
|
(port && parseInt(port) !== defaultPort) ? port : ''
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ class ident_switch extends rcube_plugin
|
|||||||
/** @var int Flag: account switching is enabled. */
|
/** @var int Flag: account switching is enabled. */
|
||||||
public const DB_ENABLED = 1;
|
public const DB_ENABLED = 1;
|
||||||
|
|
||||||
/** @var int Flag: use TLS for IMAP connection. */
|
/** @var int Legacy flag: use TLS for IMAP. Read-only for backward compat; new records store scheme in host. */
|
||||||
public const DB_SECURE_IMAP_TLS = 4;
|
public const DB_SECURE_IMAP_TLS = 4;
|
||||||
|
|
||||||
/** @var int SMTP authentication: use same credentials as IMAP. */
|
/** @var int SMTP authentication: use same credentials as IMAP. */
|
||||||
@@ -199,13 +199,7 @@ class ident_switch extends rcube_plugin
|
|||||||
$accSelected = $r['id'];
|
$accSelected = $r['id'];
|
||||||
}
|
}
|
||||||
|
|
||||||
$lbl = $r['label'];
|
$lbl = $r['label'] ?: $r['username'] ?: $r['email'];
|
||||||
if (!$lbl) {
|
|
||||||
$username = $r['username'] ?: $r['email'];
|
|
||||||
$lbl = str_contains($username, '@')
|
|
||||||
? $username
|
|
||||||
: $username . '@' . ($r['host'] ?: 'localhost');
|
|
||||||
}
|
|
||||||
$accNames[] = rcube::Q($lbl);
|
$accNames[] = rcube::Q($lbl);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -464,6 +458,44 @@ class ident_switch extends rcube_plugin
|
|||||||
return $s !== '' ? $s : null;
|
return $s !== '' ? $s : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse scheme prefix (ssl://, tls://) from a host string.
|
||||||
|
*
|
||||||
|
* @param string $host Host string, optionally prefixed with ssl:// or tls://.
|
||||||
|
* @return array{scheme: string, host: string} Parsed scheme and bare host.
|
||||||
|
*/
|
||||||
|
public static function parse_host_scheme(string $host): array
|
||||||
|
{
|
||||||
|
$lower = strtolower($host);
|
||||||
|
if (str_starts_with($lower, 'ssl://')) {
|
||||||
|
return ['scheme' => 'ssl', 'host' => substr($host, 6)];
|
||||||
|
}
|
||||||
|
if (str_starts_with($lower, 'tls://')) {
|
||||||
|
return ['scheme' => 'tls', 'host' => substr($host, 6)];
|
||||||
|
}
|
||||||
|
return ['scheme' => '', 'host' => $host];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolve username for an identity: use stored username or fall back to email.
|
||||||
|
*
|
||||||
|
* @param int $iid Identity ID.
|
||||||
|
* @param string|null $username Stored username (may be empty).
|
||||||
|
* @return string Resolved username.
|
||||||
|
*/
|
||||||
|
public static function resolve_username(int $iid, ?string $username): string
|
||||||
|
{
|
||||||
|
if (!empty($username)) {
|
||||||
|
return $username;
|
||||||
|
}
|
||||||
|
|
||||||
|
$rc = rcmail::get_instance();
|
||||||
|
$sql = 'SELECT email FROM ' . $rc->db->table_name('identities') . ' WHERE identity_id = ?';
|
||||||
|
$q = $rc->db->query($sql, $iid);
|
||||||
|
$r = $rc->db->fetch_assoc($q);
|
||||||
|
return $r['email'] ?? '';
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write a message to the plugin's log file.
|
* Write a message to the plugin's log file.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -111,20 +111,12 @@ class IdentSwitchChecker
|
|||||||
{
|
{
|
||||||
$imap = new rcube_imap_generic();
|
$imap = new rcube_imap_generic();
|
||||||
|
|
||||||
$host = $identity['imap_host'] ?: 'localhost';
|
$parsed = ident_switch::parse_host_scheme($identity['imap_host'] ?: 'localhost');
|
||||||
$ssl = null;
|
$host = $parsed['host'];
|
||||||
|
$ssl = $parsed['scheme'] ?: null;
|
||||||
|
|
||||||
// Parse scheme from host field
|
if (!$ssl && !empty($identity['flags']) && ($identity['flags'] & ident_switch::DB_SECURE_IMAP_TLS)) {
|
||||||
$hostLower = strtolower($host);
|
$ssl = 'tls'; // Backward compat: old records without scheme in host
|
||||||
if (str_starts_with($hostLower, 'ssl://')) {
|
|
||||||
$ssl = 'ssl';
|
|
||||||
$host = substr($host, 6);
|
|
||||||
} elseif (str_starts_with($hostLower, 'tls://')) {
|
|
||||||
$ssl = 'tls';
|
|
||||||
$host = substr($host, 6);
|
|
||||||
} elseif (!empty($identity['flags']) && ($identity['flags'] & ident_switch::DB_SECURE_IMAP_TLS)) {
|
|
||||||
// Backward compat: old records without scheme in host
|
|
||||||
$ssl = 'tls';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$def_port = ($ssl === 'ssl') ? 993 : 143;
|
$def_port = ($ssl === 'ssl') ? 993 : 143;
|
||||||
|
|||||||
@@ -364,21 +364,11 @@ class IdentSwitchForm
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse scheme prefix from a host string.
|
* Delegate to ident_switch::parse_host_scheme().
|
||||||
*
|
|
||||||
* @param string $host Host string, optionally prefixed with ssl:// or tls://.
|
|
||||||
* @return array{scheme: string, host: string} Parsed scheme and bare host.
|
|
||||||
*/
|
*/
|
||||||
private static function parse_host_scheme(string $host): array
|
private static function parse_host_scheme(string $host): array
|
||||||
{
|
{
|
||||||
$lower = strtolower($host);
|
return ident_switch::parse_host_scheme($host);
|
||||||
if (str_starts_with($lower, 'ssl://')) {
|
|
||||||
return ['scheme' => 'ssl', 'host' => substr($host, 6)];
|
|
||||||
}
|
|
||||||
if (str_starts_with($lower, 'tls://')) {
|
|
||||||
return ['scheme' => 'tls', 'host' => substr($host, 6)];
|
|
||||||
}
|
|
||||||
return ['scheme' => '', 'host' => $host];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -66,13 +66,7 @@ class IdentSwitchSwitcher
|
|||||||
$q = $rc->db->query($sql, $identId, $rc->user->ID);
|
$q = $rc->db->query($sql, $identId, $rc->user->ID);
|
||||||
$r = $rc->db->fetch_assoc($q);
|
$r = $rc->db->fetch_assoc($q);
|
||||||
if (is_array($r)) {
|
if (is_array($r)) {
|
||||||
if (!$r['username']) {
|
$r['username'] = ident_switch::resolve_username((int)$r['iid'], $r['username']);
|
||||||
// Load email from identity
|
|
||||||
$sql = 'SELECT email FROM ' . $rc->db->table_name('identities') . ' WHERE identity_id = ?';
|
|
||||||
$q = $rc->db->query($sql, $r['iid']);
|
|
||||||
$rIid = $rc->db->fetch_assoc($q);
|
|
||||||
$r['username'] = $rIid['email'];
|
|
||||||
}
|
|
||||||
|
|
||||||
ident_switch::write_log("Switching mailbox to one for identity with ID = {$r['iid']} (username = '{$r['username']}').");
|
ident_switch::write_log("Switching mailbox to one for identity with ID = {$r['iid']} (username = '{$r['username']}').");
|
||||||
|
|
||||||
@@ -95,20 +89,12 @@ class IdentSwitchSwitcher
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$host = $r['imap_host'] ?: 'localhost';
|
$parsed = ident_switch::parse_host_scheme($r['imap_host'] ?: 'localhost');
|
||||||
$ssl = null;
|
$host = $parsed['host'];
|
||||||
|
$ssl = $parsed['scheme'] ?: null;
|
||||||
|
|
||||||
// Parse scheme from host field
|
if (!$ssl && ($r['flags'] & ident_switch::DB_SECURE_IMAP_TLS)) {
|
||||||
$hostLower = strtolower($host);
|
$ssl = 'tls'; // Backward compat: old records without scheme in host
|
||||||
if (str_starts_with($hostLower, 'ssl://')) {
|
|
||||||
$ssl = 'ssl';
|
|
||||||
$host = substr($host, 6);
|
|
||||||
} elseif (str_starts_with($hostLower, 'tls://')) {
|
|
||||||
$ssl = 'tls';
|
|
||||||
$host = substr($host, 6);
|
|
||||||
} elseif ($r['flags'] & ident_switch::DB_SECURE_IMAP_TLS) {
|
|
||||||
// Backward compat: old records without scheme in host
|
|
||||||
$ssl = 'tls';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$def_port = ($ssl === 'ssl') ? 993 : 143;
|
$def_port = ($ssl === 'ssl') ? 993 : 143;
|
||||||
@@ -188,13 +174,7 @@ class IdentSwitchSwitcher
|
|||||||
$iid = $r['iid'];
|
$iid = $r['iid'];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$r['username']) {
|
$r['username'] = ident_switch::resolve_username($iid, $r['username']);
|
||||||
// Load email from identity
|
|
||||||
$sql = 'SELECT email FROM ' . $rc->db->table_name('identities') . ' WHERE identity_id = ?';
|
|
||||||
$q = $rc->db->query($sql, $iid);
|
|
||||||
$rIid = $rc->db->fetch_assoc($q);
|
|
||||||
$r['username'] = $rIid['email'];
|
|
||||||
}
|
|
||||||
|
|
||||||
$authMode = (int)$r['smtp_auth'];
|
$authMode = (int)$r['smtp_auth'];
|
||||||
if ($authMode === ident_switch::SMTP_AUTH_CUSTOM) {
|
if ($authMode === ident_switch::SMTP_AUTH_CUSTOM) {
|
||||||
@@ -264,12 +244,7 @@ class IdentSwitchSwitcher
|
|||||||
return $args;
|
return $args;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$r['username']) {
|
$r['username'] = ident_switch::resolve_username($iid, $r['username']);
|
||||||
$sql = 'SELECT email FROM ' . $rc->db->table_name('identities') . ' WHERE identity_id = ?';
|
|
||||||
$q = $rc->db->query($sql, $iid);
|
|
||||||
$rIid = $rc->db->fetch_assoc($q);
|
|
||||||
$r['username'] = $rIid['email'];
|
|
||||||
}
|
|
||||||
|
|
||||||
$sieveHost = $r['sieve_host'];
|
$sieveHost = $r['sieve_host'];
|
||||||
$sievePort = $r['sieve_port'] ?: 4190;
|
$sievePort = $r['sieve_port'] ?: 4190;
|
||||||
|
|||||||
Reference in New Issue
Block a user