/** * Connect to an IMAP server * * @param string $host Host to connect * @param string $user Username for IMAP account * @param string $pass Password for IMAP account * @param integer $port Port to connect to * @param string $use_ssl SSL schema (either ssl or tls) or null if plain connection * * @return boolean True on success, False on failure */ public function connect($host, $user, $pass, $port = 143, $use_ssl = null) { // check for OpenSSL support in PHP build if ($use_ssl && extension_loaded('openssl')) { $this->options['ssl_mode'] = $use_ssl == 'imaps' ? 'ssl' : $use_ssl; } else { if ($use_ssl) { rcube::raise_error(array('code' => 403, 'type' => 'imap', 'file' => __FILE__, 'line' => __LINE__, 'message' => "OpenSSL not available"), true, false); $port = 143; } } $this->options['port'] = $port; if ($this->options['debug']) { $this->set_debug(true); $this->options['ident'] = array('name' => 'Roundcube', 'version' => RCUBE_VERSION, 'php' => PHP_VERSION, 'os' => PHP_OS, 'command' => $_SERVER['REQUEST_URI']); } $attempt = 0; do { $data = $this->plugins->exec_hook('storage_connect', array_merge($this->options, array('host' => $host, 'user' => $user, 'attempt' => ++$attempt))); if (!empty($data['pass'])) { $pass = $data['pass']; } // Handle per-host socket options rcube_utils::parse_socket_options($data['socket_options'], $data['host']); $this->conn->connect($data['host'], $data['user'], $pass, $data); } while (!$this->conn->connected() && $data['retry']); $config = array('host' => $data['host'], 'user' => $data['user'], 'password' => $pass, 'port' => $port, 'ssl' => $use_ssl); $this->options = array_merge($this->options, $config); $this->connect_done = true; if ($this->conn->connected()) { // check for session identifier $session = null; if (preg_match('/\\s+SESSIONID=([^=\\s]+)/', $this->conn->result, $m)) { $session = $m[1]; } // get namespace and delimiter $this->set_env(); // trigger post-connect hook $this->plugins->exec_hook('storage_connected', array('host' => $host, 'user' => $user, 'session' => $session)); return true; } else { if ($this->conn->error) { if ($pass && $user) { $message = sprintf("Login failed for %s from %s. %s", $user, rcube_utils::remote_ip(), $this->conn->error); rcube::raise_error(array('code' => 403, 'type' => 'imap', 'file' => __FILE__, 'line' => __LINE__, 'message' => $message), true, false); } } } return false; }
/** * Create the backend driver. * * @return mixed The backend driver. */ public function createBackend() { $config = $this->getParams(); $client = new rcube_imap_generic(); if (isset($config['debug'])) { if ($config['debug'] == 'STDOUT') { $client->setDebug(true); } else { $this->_debug_log = fopen($config['debug'], 'a'); $client->setDebug(true, array($this, 'debugLog')); } } $client->connect($config['host'], $config['username'], $config['password'], array('ssl_mode' => $config['secure'], 'port' => $config['port'], 'timeout' => 0, 'force_caps' => false)); return $client; }
/** * Connect to an IMAP server * * @param string $host Host to connect * @param string $user Username for IMAP account * @param string $pass Password for IMAP account * @param integer $port Port to connect to * @param string $use_ssl SSL schema (either ssl or tls) or null if plain connection * @return boolean TRUE on success, FALSE on failure * @access public */ function connect($host, $user, $pass, $port = 143, $use_ssl = null) { // check for OpenSSL support in PHP build if ($use_ssl && extension_loaded('openssl')) { $this->options['ssl_mode'] = $use_ssl == 'imaps' ? 'ssl' : $use_ssl; } else { if ($use_ssl) { raise_error(array('code' => 403, 'type' => 'imap', 'file' => __FILE__, 'line' => __LINE__, 'message' => "OpenSSL not available"), true, false); $port = 143; } } $this->options['port'] = $port; if ($this->options['debug']) { $this->conn->setDebug(true, array($this, 'debug_handler')); $this->options['ident'] = array('name' => 'Roundcube Webmail', 'version' => RCMAIL_VERSION, 'php' => PHP_VERSION, 'os' => PHP_OS, 'command' => $_SERVER['REQUEST_URI']); } $attempt = 0; do { $data = rcmail::get_instance()->plugins->exec_hook('imap_connect', array('host' => $host, 'user' => $user, 'attempt' => ++$attempt)); if (!empty($data['pass'])) { $pass = $data['pass']; } $this->conn->connect($data['host'], $data['user'], $pass, $this->options); } while (!$this->conn->connected() && $data['retry']); $this->host = $data['host']; $this->user = $data['user']; $this->pass = $pass; $this->port = $port; $this->ssl = $use_ssl; if ($this->conn->connected()) { // get namespace and delimiter $this->set_env(); return true; } else { if ($this->conn->error) { if ($pass && $user) { $message = sprintf("Login failed for %s from %s. %s", $user, rcmail_remote_ip(), $this->conn->error); raise_error(array('code' => 403, 'type' => 'imap', 'file' => __FILE__, 'line' => __LINE__, 'message' => $message), true, false); } } } return false; }