Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set stream context before opening socket #330

Merged
merged 2 commits into from
Mar 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 24 additions & 20 deletions library/Zend/Mail/Protocol/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,42 +285,43 @@ protected function _connect($remote)
$errorNum = 0;
$errorStr = '';

// open connection
$this->_socket = @stream_socket_client($remote, $errorNum, $errorStr, self::TIMEOUT_CONNECTION);
$context = stream_context_create();

if ($this->_socket === false) {
if ($errorNum == 0) {
$errorStr = 'Could not open socket';
}
if (($result = $this->_setStreamContextVerifyPeer($this->_verifyPeer, $context)) === false) {
/**
* @see Zend_Mail_Protocol_Exception
*/
require_once 'Zend/Mail/Protocol/Exception.php';
throw new Zend_Mail_Protocol_Exception($errorStr);
throw new Zend_Mail_Protocol_Exception('Could not set stream context verify_peer value');
}

if (($result = $this->_setStreamTimeout(self::TIMEOUT_CONNECTION)) === false) {
if (($result = $this->_setStreamContextVerifyPeerName($this->_verifyPeerName, $context)) === false) {
/**
* @see Zend_Mail_Protocol_Exception
*/
require_once 'Zend/Mail/Protocol/Exception.php';
throw new Zend_Mail_Protocol_Exception('Could not set stream timeout');
throw new Zend_Mail_Protocol_Exception('Could not set stream context verify_peer_name value');
}

if (($result = $this->_setStreamContextVerifyPeer($this->_verifyPeer)) === false) {
$this->_socket = stream_socket_client($remote, $errorNum, $errorStr, self::TIMEOUT_CONNECTION, STREAM_CLIENT_CONNECT, $context);

if ($this->_socket === false) {
if ($errorNum == 0) {
$errorStr = 'Could not open socket';
}
/**
* @see Zend_Mail_Protocol_Exception
*/
require_once 'Zend/Mail/Protocol/Exception.php';
throw new Zend_Mail_Protocol_Exception('Could not set stream context verify_peer value');
throw new Zend_Mail_Protocol_Exception($errorStr);
}

if (($result = $this->_setStreamContextVerifyPeerName($this->_verifyPeerName)) === false) {
if (($result = $this->_setStreamTimeout(self::TIMEOUT_CONNECTION)) === false) {
/**
* @see Zend_Mail_Protocol_Exception
*/
require_once 'Zend/Mail/Protocol/Exception.php';
throw new Zend_Mail_Protocol_Exception('Could not set stream context verify_peer_name value');
throw new Zend_Mail_Protocol_Exception('Could not set stream timeout');
}

return $result;
Expand Down Expand Up @@ -476,32 +477,35 @@ protected function _expect($code, $timeout = null)
* Set stream timeout
*
* @param integer $timeout
* @param resource|null $context
* @return boolean
*/
protected function _setStreamTimeout($timeout)
protected function _setStreamTimeout($timeout, $context = null)
{
return stream_set_timeout($this->_socket, $timeout);
return stream_set_timeout($context ?? $this->_socket, $timeout);
}

/**
* Set stream SSL context verify_peer value
*
* @param bool $value
* @param resource|null $context
* @return boolean
*/
protected function _setStreamContextVerifyPeer($value)
protected function _setStreamContextVerifyPeer($value, $context = null)
{
return stream_context_set_option($this->_socket, 'ssl', 'verify_peer', $value);
return stream_context_set_option($context ?? $this->_socket, 'ssl', 'verify_peer', $value);
}

/**
* Set stream SSL context verify_peer_name value
*
* @param bool $value
* @param resource|null $context
* @return boolean
*/
protected function _setStreamContextVerifyPeerName($value)
protected function _setStreamContextVerifyPeerName($value, $context = null)
{
return stream_context_set_option($this->_socket, 'ssl', 'verify_peer_name', $value);
return stream_context_set_option($context ?? $this->_socket, 'ssl', 'verify_peer_name', $value);
}
}