Skip to content

Commit

Permalink
Provide function in rcube to connect to IMAP
Browse files Browse the repository at this point in the history
This is useful for plugins like FreeBusy [1] that previously
connected to IMAP via plain rcube_imap::connect().

[1] https://git.kolab.org/source/freebusy/browse/master/lib/Kolab/FreeBusy/SourceIMAP.php
  • Loading branch information
jaudriga committed Apr 17, 2024
1 parent 690b364 commit cf7e4c9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
8 changes: 1 addition & 7 deletions program/include/rcmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -764,13 +764,7 @@ public function login($username, $password, $host = null, $cookiecheck = false,
$storage = $this->get_storage();

// try to log in
if (!$storage->connect($host, $username, $password, $port, $ssl)) {
if ($user) {
$user->failed_login();
}

// Wait a second to slow down brute-force attacks (#1490549)
sleep(1);
if (!$this->imap_connect($storage, $host, $username, $password, $port, $ssl, $user)) {
return false;
}

Expand Down
35 changes: 35 additions & 0 deletions program/lib/Roundcube/rcube.php
Original file line number Diff line number Diff line change
Expand Up @@ -1857,6 +1857,41 @@ public function deliver_message($message, $from, $mailto, &$error,

return $sent;
}

/**
* Helper method to establish connection to an IMAP backend.
*
* @param rcube_storage $imap IMAP storage handler
* @param string $host IMAP host
* @param string $username IMAP username
* @param string $password IMAP password
* @param int $port IMAP port to connect to
* @param string $ssl SSL schema or false if plain connection
* @param rcube_user $user Roundcube user (if it already exists)
* @param array $imap_options Additional IMAP options
*
* @return bool Return true on successful login.
*/
public function imap_connect($imap, $host, $username, $password, $port, $ssl, $user = null, $imap_options = array())
{
// enable proxy authentication
if (!empty($imap_options)) {
$imap->set_options($imap_options);
}

// try to log in
if (!$imap->connect($host, $username, $password, $port, $ssl)) {
if ($user) {
$user->failed_login();
}

// Wait a second to slow down brute-force attacks (#1490549)
sleep(1);
return false;
}

return true;
}
}

/**
Expand Down

0 comments on commit cf7e4c9

Please sign in to comment.