Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
26 changes: 20 additions & 6 deletions src/Protocol/Imap.php
Expand Up @@ -70,20 +70,34 @@ public function __destruct()
*/
public function connect($host, $port = null, $ssl = false)
{
if ($ssl == 'SSL') {
$host = 'ssl://' . $host;
$isTls = false;

if ($ssl) {
$ssl = strtolower($ssl);
}

if ($port === null) {
$port = $ssl === 'SSL' ? 993 : 143;
switch ($ssl) {
case 'ssl':
$host = 'ssl://' . $host;
if (!$port) {
$port = 993;
}
break;
case 'tls':
$isTls = true;
// break intentionally omitted
default:
if (!$port) {
$port = 143;
}
}

ErrorHandler::start();
$this->socket = fsockopen($host, $port, $errno, $errstr, self::TIMEOUT_CONNECTION);
$error = ErrorHandler::stop();
if (!$this->socket) {
throw new Exception\RuntimeException(sprintf(
'cannot connect to host%s',
'cannot connect to host %s',
($error ? sprintf('; error = %s (errno = %d )', $error->getMessage(), $error->getCode()) : '')
), 0, $error);
}
Expand All @@ -92,7 +106,7 @@ public function connect($host, $port = null, $ssl = false)
throw new Exception\RuntimeException('host doesn\'t allow connection');
}

if ($ssl === 'TLS') {
if ($isTls) {
$result = $this->requestAndResponse('STARTTLS');
$result = $result && stream_socket_enable_crypto($this->socket, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
if (!$result) {
Expand Down
26 changes: 20 additions & 6 deletions src/Protocol/Pop3.php
Expand Up @@ -78,20 +78,34 @@ public function __destruct()
*/
public function connect($host, $port = null, $ssl = false)
{
if ($ssl == 'SSL') {
$host = 'ssl://' . $host;
$isTls = false;

if ($ssl) {
$ssl = strtolower($ssl);
}

if ($port === null) {
$port = $ssl == 'SSL' ? 995 : 110;
switch ($ssl) {
case 'ssl':
$host = 'ssl://' . $host;
if (!$port) {
$port = 995;
}
break;
case 'tls':
$isTls = true;
// break intentionally omitted
default:
if (!$port) {
$port = 110;
}
}

ErrorHandler::start();
$this->socket = fsockopen($host, $port, $errno, $errstr, self::TIMEOUT_CONNECTION);
$error = ErrorHandler::stop();
if (!$this->socket) {
throw new Exception\RuntimeException(sprintf(
'cannot connect to host%s',
'cannot connect to host %s',
($error ? sprintf('; error = %s (errno = %d )', $error->getMessage(), $error->getCode()) : '')
), 0, $error);
}
Expand All @@ -106,7 +120,7 @@ public function connect($host, $port = null, $ssl = false)
$this->timestamp = '<' . $this->timestamp . '>';
}

if ($ssl === 'TLS') {
if ($isTls) {
$this->request('STLS');
$result = stream_socket_enable_crypto($this->socket, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
if (!$result) {
Expand Down

0 comments on commit 3aa0ee9

Please sign in to comment.