Skip to content

Commit

Permalink
fix(backend): ensure the load_cache() method is accessible via mailbo…
Browse files Browse the repository at this point in the history
…x object returned by Hm_Mailbox (#1412)

* fix(backend): ensure the load_cache() method is accessible via mailbox object returned by Hm_Mailbox

* fix unit tests
  • Loading branch information
mercihabam authored Dec 21, 2024
1 parent 8c45018 commit 0e8bec3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 25 deletions.
32 changes: 16 additions & 16 deletions modules/core/hm-mailbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
13 changes: 8 additions & 5 deletions modules/imap/hm-imap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions modules/smtp/hm-smtp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, ['type' => 'imap']);
self::$server_list[$id]['object']->connect();
self::$server_list[$id]['connected'] = true;
return self::$server_list[$id]['object'];
}
Expand Down

0 comments on commit 0e8bec3

Please sign in to comment.