From ad8ba1acd6613650868771e2600f5b4189408a1a Mon Sep 17 00:00:00 2001 From: abulhol Date: Fri, 18 Oct 2019 12:41:59 +0200 Subject: [PATCH 1/7] Add option novalidatecert to connect() --- src/Protocol/Imap.php | 20 +++++++++++++++++--- src/Protocol/Pop3.php | 20 +++++++++++++++++--- src/Storage/Imap.php | 3 ++- src/Storage/Pop3.php | 3 ++- 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/Protocol/Imap.php b/src/Protocol/Imap.php index 0f27995d..f272141d 100644 --- a/src/Protocol/Imap.php +++ b/src/Protocol/Imap.php @@ -62,7 +62,7 @@ public function __destruct() * @throws Exception\RuntimeException * @return string welcome message */ - public function connect($host, $port = null, $ssl = false) + public function connect($host, $port = null, $ssl = false, $novalidatecert = false) { $isTls = false; @@ -85,9 +85,23 @@ public function connect($host, $port = null, $ssl = false) $port = 143; } } - + + $socket_options = []; + + if ($novalidatecert) { + $socket_options = [ + 'ssl' => [ + 'verify_peer_name' => false, + 'verify_peer' => false, + ] + ]; + } + + $socket_context = stream_context_create($socket_options); + ErrorHandler::start(); - $this->socket = fsockopen($host, $port, $errno, $errstr, self::TIMEOUT_CONNECTION); + $this->socket = @stream_socket_client($host . ":" . $port, $errno, $errstr, self::TIMEOUT_CONNECTION, STREAM_CLIENT_CONNECT, $socket_context); + $error = ErrorHandler::stop(); if (! $this->socket) { throw new Exception\RuntimeException(sprintf( diff --git a/src/Protocol/Pop3.php b/src/Protocol/Pop3.php index dcc9895a..4ad5208e 100644 --- a/src/Protocol/Pop3.php +++ b/src/Protocol/Pop3.php @@ -67,7 +67,7 @@ public function __destruct() * @throws Exception\RuntimeException * @return string welcome message */ - public function connect($host, $port = null, $ssl = false) + public function connect($host, $port = null, $ssl = false, $novalidatecert = false) { $isTls = false; @@ -90,9 +90,23 @@ public function connect($host, $port = null, $ssl = false) $port = 110; } } - + + $socket_options = []; + + if ($novalidatecert) { + $socket_options = [ + 'ssl' => [ + 'verify_peer_name' => false, + 'verify_peer' => false, + ] + ]; + } + + $socket_context = stream_context_create($socket_options); + ErrorHandler::start(); - $this->socket = fsockopen($host, $port, $errno, $errstr, self::TIMEOUT_CONNECTION); + $this->socket = @stream_socket_client($host . ":" . $port, $errno, $errstr, self::TIMEOUT_CONNECTION, STREAM_CLIENT_CONNECT, $socket_context); + $error = ErrorHandler::stop(); if (! $this->socket) { throw new Exception\RuntimeException(sprintf( diff --git a/src/Storage/Imap.php b/src/Storage/Imap.php index 5324f9d7..ce89a5dd 100644 --- a/src/Storage/Imap.php +++ b/src/Storage/Imap.php @@ -210,9 +210,10 @@ public function __construct($params) $password = isset($params->password) ? $params->password : ''; $port = isset($params->port) ? $params->port : null; $ssl = isset($params->ssl) ? $params->ssl : false; + $novalidatecert = isset($params->novalidatecert) ? $params->novalidatecert : false; $this->protocol = new Protocol\Imap(); - $this->protocol->connect($host, $port, $ssl); + $this->protocol->connect($host, $port, $ssl, $novalidatecert); if (! $this->protocol->login($params->user, $password)) { throw new Exception\RuntimeException('cannot login, user or password wrong'); } diff --git a/src/Storage/Pop3.php b/src/Storage/Pop3.php index a08b33b2..555623e4 100644 --- a/src/Storage/Pop3.php +++ b/src/Storage/Pop3.php @@ -143,9 +143,10 @@ public function __construct($params) $password = isset($params->password) ? $params->password : ''; $port = isset($params->port) ? $params->port : null; $ssl = isset($params->ssl) ? $params->ssl : false; + $novalidatecert = isset($params->novalidatecert) ? $params->novalidatecert : false; $this->protocol = new Protocol\Pop3(); - $this->protocol->connect($host, $port, $ssl); + $this->protocol->connect($host, $port, $ssl, $novalidatecert); $this->protocol->login($params->user, $password); } From 36d133bae36c5ff9bd36d0adc49a74157d2f283b Mon Sep 17 00:00:00 2001 From: abulhol Date: Fri, 18 Oct 2019 13:24:36 +0200 Subject: [PATCH 2/7] New implementation of novalidatecert --- src/Protocol/Imap.php | 10 ++++++++-- src/Protocol/Pop3.php | 10 ++++++++-- src/Storage/Imap.php | 8 ++++++-- src/Storage/Pop3.php | 10 +++++++--- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/Protocol/Imap.php b/src/Protocol/Imap.php index f272141d..3a5c8963 100644 --- a/src/Protocol/Imap.php +++ b/src/Protocol/Imap.php @@ -18,6 +18,12 @@ class Imap */ const TIMEOUT_CONNECTION = 30; + /** + * Do not validate the SSL certificate if set to true + * @var null|bool + */ + public $novalidatecert; + /** * socket to imap server * @var resource|null @@ -62,7 +68,7 @@ public function __destruct() * @throws Exception\RuntimeException * @return string welcome message */ - public function connect($host, $port = null, $ssl = false, $novalidatecert = false) + public function connect($host, $port = null, $ssl = false) { $isTls = false; @@ -88,7 +94,7 @@ public function connect($host, $port = null, $ssl = false, $novalidatecert = fal $socket_options = []; - if ($novalidatecert) { + if ($this->novalidatecert) { $socket_options = [ 'ssl' => [ 'verify_peer_name' => false, diff --git a/src/Protocol/Pop3.php b/src/Protocol/Pop3.php index 4ad5208e..e23147c9 100644 --- a/src/Protocol/Pop3.php +++ b/src/Protocol/Pop3.php @@ -24,6 +24,12 @@ class Pop3 */ public $hasTop = null; + /** + * Do not validate the SSL certificate if set to true + * @var null|bool + */ + public $novalidatecert; + /** * socket to pop3 * @var null|resource @@ -67,7 +73,7 @@ public function __destruct() * @throws Exception\RuntimeException * @return string welcome message */ - public function connect($host, $port = null, $ssl = false, $novalidatecert = false) + public function connect($host, $port = null, $ssl = false) { $isTls = false; @@ -93,7 +99,7 @@ public function connect($host, $port = null, $ssl = false, $novalidatecert = fal $socket_options = []; - if ($novalidatecert) { + if ($this->novalidatecert) { $socket_options = [ 'ssl' => [ 'verify_peer_name' => false, diff --git a/src/Storage/Imap.php b/src/Storage/Imap.php index ce89a5dd..67f10c9a 100644 --- a/src/Storage/Imap.php +++ b/src/Storage/Imap.php @@ -210,10 +210,14 @@ public function __construct($params) $password = isset($params->password) ? $params->password : ''; $port = isset($params->port) ? $params->port : null; $ssl = isset($params->ssl) ? $params->ssl : false; - $novalidatecert = isset($params->novalidatecert) ? $params->novalidatecert : false; $this->protocol = new Protocol\Imap(); - $this->protocol->connect($host, $port, $ssl, $novalidatecert); + + if (isset($params->novalidatecert)) { + $this->protocol->novalidatecert = $params->novalidatecert; + } + + $this->protocol->connect($host, $port, $ssl); if (! $this->protocol->login($params->user, $password)) { throw new Exception\RuntimeException('cannot login, user or password wrong'); } diff --git a/src/Storage/Pop3.php b/src/Storage/Pop3.php index 555623e4..40f1f63f 100644 --- a/src/Storage/Pop3.php +++ b/src/Storage/Pop3.php @@ -143,10 +143,14 @@ public function __construct($params) $password = isset($params->password) ? $params->password : ''; $port = isset($params->port) ? $params->port : null; $ssl = isset($params->ssl) ? $params->ssl : false; - $novalidatecert = isset($params->novalidatecert) ? $params->novalidatecert : false; - + $this->protocol = new Protocol\Pop3(); - $this->protocol->connect($host, $port, $ssl, $novalidatecert); + + if (isset($params->novalidatecert)) { + $this->protocol->novalidatecert = $params->novalidatecert; + } + + $this->protocol->connect($host, $port, $ssl); $this->protocol->login($params->user, $password); } From cbc3e4fc6d5cb0c267885daaa8e1516d1fe81644 Mon Sep 17 00:00:00 2001 From: abulhol Date: Mon, 21 Oct 2019 09:20:59 +0200 Subject: [PATCH 3/7] Added setter method for new variable; removed @ for socket_connect --- src/Protocol/Imap.php | 38 +++++++++++++++++++++++++------------- src/Protocol/Pop3.php | 37 ++++++++++++++++++++++++------------- src/Storage/Imap.php | 2 +- src/Storage/Pop3.php | 2 +- 4 files changed, 51 insertions(+), 28 deletions(-) diff --git a/src/Protocol/Imap.php b/src/Protocol/Imap.php index 3a5c8963..0cd4fcf9 100644 --- a/src/Protocol/Imap.php +++ b/src/Protocol/Imap.php @@ -19,10 +19,10 @@ class Imap const TIMEOUT_CONNECTION = 30; /** - * Do not validate the SSL certificate if set to true + * If set to true, do not validate the SSL certificate * @var null|bool */ - public $novalidatecert; + protected $novalidatecert; /** * socket to imap server @@ -39,13 +39,16 @@ class Imap /** * Public constructor * - * @param string $host hostname or IP address of IMAP server, if given connect() is called - * @param int|null $port port of IMAP server, null for default (143 or 993 for ssl) - * @param bool $ssl use ssl? 'SSL', 'TLS' or false + * @param string $host hostname or IP address of IMAP server, if given connect() is called + * @param int|null $port port of IMAP server, null for default (143 or 993 for ssl) + * @param bool $ssl use ssl? 'SSL', 'TLS' or false + * @param bool $novalidatecert set to true to skip SSL certificate validation * @throws \Zend\Mail\Protocol\Exception\ExceptionInterface */ - public function __construct($host = '', $port = null, $ssl = false) + public function __construct($host = '', $port = null, $ssl = false, $novalidatecert = false) { + $this->novalidatecert = $novalidatecert; + if ($host) { $this->connect($host, $port, $ssl); } @@ -59,6 +62,14 @@ public function __destruct() $this->logout(); } + public function setNoValidateCert($novalidatecert) { + + if (is_bool($novalidatecert)) { + $this->novalidatecert = $novalidatecert; + } + + } + /** * Open connection to IMAP server * @@ -70,6 +81,7 @@ public function __destruct() */ public function connect($host, $port = null, $ssl = false) { + $isTls = false; if ($ssl) { @@ -95,18 +107,18 @@ public function connect($host, $port = null, $ssl = false) $socket_options = []; if ($this->novalidatecert) { - $socket_options = [ - 'ssl' => [ - 'verify_peer_name' => false, - 'verify_peer' => false, - ] - ]; + $socket_options = [ + 'ssl' => [ + 'verify_peer_name' => false, + 'verify_peer' => false, + ] + ]; } $socket_context = stream_context_create($socket_options); ErrorHandler::start(); - $this->socket = @stream_socket_client($host . ":" . $port, $errno, $errstr, self::TIMEOUT_CONNECTION, STREAM_CLIENT_CONNECT, $socket_context); + $this->socket = stream_socket_client($host . ":" . $port, $errno, $errstr, self::TIMEOUT_CONNECTION, STREAM_CLIENT_CONNECT, $socket_context); $error = ErrorHandler::stop(); if (! $this->socket) { diff --git a/src/Protocol/Pop3.php b/src/Protocol/Pop3.php index e23147c9..6f45c8b5 100644 --- a/src/Protocol/Pop3.php +++ b/src/Protocol/Pop3.php @@ -25,10 +25,10 @@ class Pop3 public $hasTop = null; /** - * Do not validate the SSL certificate if set to true + * If set to true, do not validate the SSL certificate * @var null|bool */ - public $novalidatecert; + protected $novalidatecert; /** * socket to pop3 @@ -45,12 +45,15 @@ class Pop3 /** * Public constructor * - * @param string $host hostname or IP address of POP3 server, if given connect() is called - * @param int|null $port port of POP3 server, null for default (110 or 995 for ssl) - * @param bool|string $ssl use ssl? 'SSL', 'TLS' or false + * @param string $host hostname or IP address of POP3 server, if given connect() is called + * @param int|null $port port of POP3 server, null for default (110 or 995 for ssl) + * @param bool|string $ssl use ssl? 'SSL', 'TLS' or false + * @param bool $novalidatecert set to true to skip SSL certificate validation */ - public function __construct($host = '', $port = null, $ssl = false) + public function __construct($host = '', $port = null, $ssl = false, $novalidatecert = false) { + $this->novalidatecert = $novalidatecert; + if ($host) { $this->connect($host, $port, $ssl); } @@ -64,6 +67,14 @@ public function __destruct() $this->logout(); } + + public function setNoValidateCert($novalidatecert) { + + if (is_bool($novalidatecert)) { + $this->novalidatecert = $novalidatecert; + } + } + /** * Open connection to POP3 server * @@ -100,18 +111,18 @@ public function connect($host, $port = null, $ssl = false) $socket_options = []; if ($this->novalidatecert) { - $socket_options = [ - 'ssl' => [ - 'verify_peer_name' => false, - 'verify_peer' => false, - ] - ]; + $socket_options = [ + 'ssl' => [ + 'verify_peer_name' => false, + 'verify_peer' => false, + ] + ]; } $socket_context = stream_context_create($socket_options); ErrorHandler::start(); - $this->socket = @stream_socket_client($host . ":" . $port, $errno, $errstr, self::TIMEOUT_CONNECTION, STREAM_CLIENT_CONNECT, $socket_context); + $this->socket = stream_socket_client($host . ":" . $port, $errno, $errstr, self::TIMEOUT_CONNECTION, STREAM_CLIENT_CONNECT, $socket_context); $error = ErrorHandler::stop(); if (! $this->socket) { diff --git a/src/Storage/Imap.php b/src/Storage/Imap.php index 67f10c9a..fb38b89c 100644 --- a/src/Storage/Imap.php +++ b/src/Storage/Imap.php @@ -214,7 +214,7 @@ public function __construct($params) $this->protocol = new Protocol\Imap(); if (isset($params->novalidatecert)) { - $this->protocol->novalidatecert = $params->novalidatecert; + $this->protocol->setNoValidateCert(true); } $this->protocol->connect($host, $port, $ssl); diff --git a/src/Storage/Pop3.php b/src/Storage/Pop3.php index 40f1f63f..ea7469a3 100644 --- a/src/Storage/Pop3.php +++ b/src/Storage/Pop3.php @@ -147,7 +147,7 @@ public function __construct($params) $this->protocol = new Protocol\Pop3(); if (isset($params->novalidatecert)) { - $this->protocol->novalidatecert = $params->novalidatecert; + $this->protocol->setNoValidateCert($params->novalidatecert); } $this->protocol->connect($host, $port, $ssl); From 767e170728b3b186b5ab9ae5b385e15b0446bece Mon Sep 17 00:00:00 2001 From: abulhol Date: Mon, 21 Oct 2019 09:27:57 +0200 Subject: [PATCH 4/7] Added new param to docs --- docs/book/read.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/book/read.md b/docs/book/read.md index c737e3ba..87a17e2e 100644 --- a/docs/book/read.md +++ b/docs/book/read.md @@ -117,6 +117,10 @@ $mail = new Pop3([ ]); ``` +If you are connecting to a mail server with a self-signed certificate and want to +skip the SSL verification, you can also pass an additional argument `novalidatecert` +with the value `true`. + Both constructors throw `Zend\Mail\Exception` or `Zend\Mail\Protocol\Exception` (extends `Zend\Mail\Exception`) for connection errors, depending on the type of error encountered. From c2eae3c5ddfd2a6db62b9e99c662a77695c5cbed Mon Sep 17 00:00:00 2001 From: abulhol Date: Mon, 21 Oct 2019 09:39:09 +0200 Subject: [PATCH 5/7] Fix empty lines and line lengths --- src/Protocol/Imap.php | 17 +++++++++-------- src/Protocol/Pop3.php | 19 ++++++++++--------- src/Storage/Imap.php | 4 ++-- src/Storage/Pop3.php | 6 +++--- 4 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/Protocol/Imap.php b/src/Protocol/Imap.php index 0cd4fcf9..c564f92a 100644 --- a/src/Protocol/Imap.php +++ b/src/Protocol/Imap.php @@ -23,7 +23,7 @@ class Imap * @var null|bool */ protected $novalidatecert; - + /** * socket to imap server * @var resource|null @@ -62,12 +62,12 @@ public function __destruct() $this->logout(); } - public function setNoValidateCert($novalidatecert) { + public function setNoValidateCert($novalidatecert) + { if (is_bool($novalidatecert)) { $this->novalidatecert = $novalidatecert; } - } /** @@ -103,9 +103,9 @@ public function connect($host, $port = null, $ssl = false) $port = 143; } } - + $socket_options = []; - + if ($this->novalidatecert) { $socket_options = [ 'ssl' => [ @@ -114,11 +114,12 @@ public function connect($host, $port = null, $ssl = false) ] ]; } - + $socket_context = stream_context_create($socket_options); - + ErrorHandler::start(); - $this->socket = stream_socket_client($host . ":" . $port, $errno, $errstr, self::TIMEOUT_CONNECTION, STREAM_CLIENT_CONNECT, $socket_context); + $this->socket = stream_socket_client($host . ":" . $port, $errno, $errstr, self::TIMEOUT_CONNECTION, + STREAM_CLIENT_CONNECT, $socket_context); $error = ErrorHandler::stop(); if (! $this->socket) { diff --git a/src/Protocol/Pop3.php b/src/Protocol/Pop3.php index 6f45c8b5..e1e90382 100644 --- a/src/Protocol/Pop3.php +++ b/src/Protocol/Pop3.php @@ -29,7 +29,7 @@ class Pop3 * @var null|bool */ protected $novalidatecert; - + /** * socket to pop3 * @var null|resource @@ -67,14 +67,14 @@ public function __destruct() $this->logout(); } - - public function setNoValidateCert($novalidatecert) { + public function setNoValidateCert($novalidatecert) + { if (is_bool($novalidatecert)) { $this->novalidatecert = $novalidatecert; } } - + /** * Open connection to POP3 server * @@ -107,9 +107,9 @@ public function connect($host, $port = null, $ssl = false) $port = 110; } } - + $socket_options = []; - + if ($this->novalidatecert) { $socket_options = [ 'ssl' => [ @@ -118,11 +118,12 @@ public function connect($host, $port = null, $ssl = false) ] ]; } - + $socket_context = stream_context_create($socket_options); - + ErrorHandler::start(); - $this->socket = stream_socket_client($host . ":" . $port, $errno, $errstr, self::TIMEOUT_CONNECTION, STREAM_CLIENT_CONNECT, $socket_context); + $this->socket = stream_socket_client($host . ":" . $port, $errno, $errstr, self::TIMEOUT_CONNECTION, + STREAM_CLIENT_CONNECT, $socket_context); $error = ErrorHandler::stop(); if (! $this->socket) { diff --git a/src/Storage/Imap.php b/src/Storage/Imap.php index fb38b89c..cf8aa92d 100644 --- a/src/Storage/Imap.php +++ b/src/Storage/Imap.php @@ -212,11 +212,11 @@ public function __construct($params) $ssl = isset($params->ssl) ? $params->ssl : false; $this->protocol = new Protocol\Imap(); - + if (isset($params->novalidatecert)) { $this->protocol->setNoValidateCert(true); } - + $this->protocol->connect($host, $port, $ssl); if (! $this->protocol->login($params->user, $password)) { throw new Exception\RuntimeException('cannot login, user or password wrong'); diff --git a/src/Storage/Pop3.php b/src/Storage/Pop3.php index ea7469a3..e1159f7d 100644 --- a/src/Storage/Pop3.php +++ b/src/Storage/Pop3.php @@ -143,13 +143,13 @@ public function __construct($params) $password = isset($params->password) ? $params->password : ''; $port = isset($params->port) ? $params->port : null; $ssl = isset($params->ssl) ? $params->ssl : false; - + $this->protocol = new Protocol\Pop3(); - + if (isset($params->novalidatecert)) { $this->protocol->setNoValidateCert($params->novalidatecert); } - + $this->protocol->connect($host, $port, $ssl); $this->protocol->login($params->user, $password); } From 76aa3e0d1ee6dee7248f7850a6db986d45d6b1cb Mon Sep 17 00:00:00 2001 From: abulhol Date: Mon, 21 Oct 2019 09:49:27 +0200 Subject: [PATCH 6/7] Fix formatting multi line function call --- src/Protocol/Imap.php | 12 +++++++++--- src/Protocol/Pop3.php | 10 ++++++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/Protocol/Imap.php b/src/Protocol/Imap.php index c564f92a..fb8b75e4 100644 --- a/src/Protocol/Imap.php +++ b/src/Protocol/Imap.php @@ -62,7 +62,7 @@ public function __destruct() $this->logout(); } - public function setNoValidateCert($novalidatecert) + public function setNoValidateCert($novalidatecert) { if (is_bool($novalidatecert)) { @@ -118,8 +118,14 @@ public function connect($host, $port = null, $ssl = false) $socket_context = stream_context_create($socket_options); ErrorHandler::start(); - $this->socket = stream_socket_client($host . ":" . $port, $errno, $errstr, self::TIMEOUT_CONNECTION, - STREAM_CLIENT_CONNECT, $socket_context); + $this->socket = stream_socket_client( + $host . ":" . $port, + $errno, + $errstr, + self::TIMEOUT_CONNECTION, + STREAM_CLIENT_CONNECT, + $socket_context + ); $error = ErrorHandler::stop(); if (! $this->socket) { diff --git a/src/Protocol/Pop3.php b/src/Protocol/Pop3.php index e1e90382..47c1460a 100644 --- a/src/Protocol/Pop3.php +++ b/src/Protocol/Pop3.php @@ -122,8 +122,14 @@ public function connect($host, $port = null, $ssl = false) $socket_context = stream_context_create($socket_options); ErrorHandler::start(); - $this->socket = stream_socket_client($host . ":" . $port, $errno, $errstr, self::TIMEOUT_CONNECTION, - STREAM_CLIENT_CONNECT, $socket_context); + $this->socket = stream_socket_client( + $host . ":" . $port, + $errno, + $errstr, + self::TIMEOUT_CONNECTION, + STREAM_CLIENT_CONNECT, + $socket_context + ); $error = ErrorHandler::stop(); if (! $this->socket) { From 48e9ee1cdc3590d40648418a46bc40b14fb8c767 Mon Sep 17 00:00:00 2001 From: abulhol Date: Mon, 21 Oct 2019 10:04:35 +0200 Subject: [PATCH 7/7] Final indentation fix --- src/Protocol/Imap.php | 2 +- src/Protocol/Pop3.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Protocol/Imap.php b/src/Protocol/Imap.php index fb8b75e4..1449a106 100644 --- a/src/Protocol/Imap.php +++ b/src/Protocol/Imap.php @@ -125,7 +125,7 @@ public function connect($host, $port = null, $ssl = false) self::TIMEOUT_CONNECTION, STREAM_CLIENT_CONNECT, $socket_context - ); + ); $error = ErrorHandler::stop(); if (! $this->socket) { diff --git a/src/Protocol/Pop3.php b/src/Protocol/Pop3.php index 47c1460a..8a94bb32 100644 --- a/src/Protocol/Pop3.php +++ b/src/Protocol/Pop3.php @@ -129,7 +129,7 @@ public function connect($host, $port = null, $ssl = false) self::TIMEOUT_CONNECTION, STREAM_CLIENT_CONNECT, $socket_context - ); + ); $error = ErrorHandler::stop(); if (! $this->socket) {