diff --git a/modules/core/hm-mailbox.php b/modules/core/hm-mailbox.php index 1a5a9ea63..61d934025 100644 --- a/modules/core/hm-mailbox.php +++ b/modules/core/hm-mailbox.php @@ -23,33 +23,33 @@ class Hm_Mailbox { protected $server_id; protected $user_config; protected $session; + protected $config; - public function __construct($server_id, $user_config, $session) { + public function __construct($server_id, $user_config, $session, $config) { $this->server_id = $server_id; $this->user_config = $user_config; $this->session = $session; - } - - public function connect(array $config) { - if (array_key_exists('type', $config) && $config['type'] == 'smtp') { - $this->type = self::TYPE_SMTP; - $this->connection = new Hm_SMTP($config); - return $this->connection->connect(); - } elseif (array_key_exists('type', $config) && $config['type'] == 'jmap') { + $this->config = $config; + $type = $config['type'] ?? ''; + if ($type == 'imap') { + $this->type = self::TYPE_IMAP; + $this->connection = new Hm_IMAP(); + } elseif ($type == 'jmap') { $this->type = self::TYPE_JMAP; $this->connection = new Hm_JMAP(); - return $this->connection->connect($config); - } elseif (array_key_exists('type', $config) && $config['type'] == 'ews') { + } elseif ($type == 'ews') { $this->type = self::TYPE_EWS; $this->connection = new Hm_EWS(); - return $this->connection->connect($config); - } else { - $this->type = self::TYPE_IMAP; - $this->connection = new Hm_IMAP(); - return $this->connection->connect($config); + } elseif ($type == 'smtp') { + $this->type = self::TYPE_SMTP; + $this->connection = new Hm_SMTP($config); } } + public function connect() { + return $this->connection->connect($this->config); + } + public function get_connection() { return $this->connection; } diff --git a/modules/imap/hm-imap.php b/modules/imap/hm-imap.php index 3e8759c32..9d50ad435 100644 --- a/modules/imap/hm-imap.php +++ b/modules/imap/hm-imap.php @@ -32,10 +32,6 @@ public static function init($user_config, $session) { } public static function service_connect($id, $server, $user, $pass, $cache=false) { - self::$server_list[$id]['object'] = new Hm_Mailbox($id, self::$user_config, self::$session); - if (self::$use_cache && $cache && is_array($cache)) { - self::$server_list[$id]['object']->load_cache($cache, 'array'); - } $config = array( 'server' => $server['server'], 'port' => $server['port'], @@ -45,10 +41,17 @@ public static function service_connect($id, $server, $user, $pass, $cache=false) 'password' => $pass, 'use_cache' => self::$use_cache ); + if (array_key_exists('auth', $server)) { $config['auth'] = $server['auth']; } - return self::$server_list[$id]['object']->connect($config); + + self::$server_list[$id]['object'] = new Hm_Mailbox($id, self::$user_config, self::$session, $config); + if (self::$use_cache && $cache && is_array($cache)) { + self::$server_list[$id]['object']->get_connection()->load_cache($cache, 'array'); + } + + return self::$server_list[$id]['object']->connect(); } public static function get_cache($hm_cache, $id) { diff --git a/modules/smtp/hm-smtp.php b/modules/smtp/hm-smtp.php index 779bb4c5e..bd8c19972 100644 --- a/modules/smtp/hm-smtp.php +++ b/modules/smtp/hm-smtp.php @@ -39,8 +39,8 @@ public static function service_connect($id, $server, $user, $pass, $cache=false) if (array_key_exists('no_auth', $server)) { $config['no_auth'] = true; } - self::$server_list[$id]['object'] = new Hm_Mailbox($id, self::$user_config, self::$session); - if (! self::$server_list[$id]['object']->connect($config)) { + self::$server_list[$id]['object'] = new Hm_Mailbox($id, self::$user_config, self::$session, $config); + if (! self::$server_list[$id]['object']->connect()) { return self::$server_list[$id]['object']; } return false; diff --git a/tests/phpunit/helpers.php b/tests/phpunit/helpers.php index 07028b61f..cc50e1ebc 100644 --- a/tests/phpunit/helpers.php +++ b/tests/phpunit/helpers.php @@ -101,8 +101,8 @@ public static function connect($id, $cache=false, $user=false, $pass=false, $sav global $user_config, $session; Hm_IMAP::$allow_connection = self::$state; Hm_IMAP::$allow_auth = self::$state; - self::$server_list[$id]['object'] = new Hm_Mailbox($id, $user_config, $session); - self::$server_list[$id]['object']->connect([]); + self::$server_list[$id]['object'] = new Hm_Mailbox($id, $user_config, $session, []); + self::$server_list[$id]['object']->connect(); self::$server_list[$id]['connected'] = true; return self::$server_list[$id]['object']; }