2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2013-10-09 23:38:16 +00:00
|
|
|
* CakePHP Socket connection class.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2008-05-30 11:40:08 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Network
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-12-08 15:35:02 +00:00
|
|
|
|
2010-12-15 05:50:02 +00:00
|
|
|
App::uses('Validation', 'Utility');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2013-10-09 23:38:16 +00:00
|
|
|
* CakePHP network socket connection class.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Core base class for network communication.
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Network
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-24 04:41:20 +00:00
|
|
|
class CakeSocket {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Object description
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $description = 'Remote DataSource Network Socket Interface';
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Base configuration settings for the socket connection
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 06:36:12 +00:00
|
|
|
protected $_baseConfig = array(
|
2012-08-10 00:51:29 +00:00
|
|
|
'persistent' => false,
|
|
|
|
'host' => 'localhost',
|
|
|
|
'protocol' => 'tcp',
|
|
|
|
'port' => 80,
|
|
|
|
'timeout' => 30
|
2008-05-30 11:40:08 +00:00
|
|
|
);
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Configuration settings for the socket connection
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $config = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Reference to socket connection resource
|
|
|
|
*
|
|
|
|
* @var resource
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $connection = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* This boolean contains the current state of the CakeSocket class
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var bool
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $connected = false;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* This variable contains an array with the last error number (num) and string (str)
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $lastError = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2012-07-21 13:23:47 +00:00
|
|
|
/**
|
|
|
|
* True if the socket stream is encrypted after a CakeSocket::enableCrypto() call
|
2012-08-10 00:51:29 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var bool
|
2012-08-10 00:51:29 +00:00
|
|
|
*/
|
2012-07-21 13:23:47 +00:00
|
|
|
public $encrypted = false;
|
2012-08-10 00:51:29 +00:00
|
|
|
|
2012-07-21 13:23:47 +00:00
|
|
|
/**
|
|
|
|
* Contains all the encryption methods available
|
2012-08-10 00:51:29 +00:00
|
|
|
*
|
|
|
|
* @var array
|
2012-07-21 13:23:47 +00:00
|
|
|
*/
|
|
|
|
protected $_encryptMethods = array(
|
2012-08-10 00:54:04 +00:00
|
|
|
// @codingStandardsIgnoreStart
|
2012-07-21 13:23:47 +00:00
|
|
|
'sslv2_client' => STREAM_CRYPTO_METHOD_SSLv2_CLIENT,
|
|
|
|
'sslv3_client' => STREAM_CRYPTO_METHOD_SSLv3_CLIENT,
|
2012-07-24 13:08:54 +00:00
|
|
|
'sslv23_client' => STREAM_CRYPTO_METHOD_SSLv23_CLIENT,
|
2012-07-21 13:23:47 +00:00
|
|
|
'tls_client' => STREAM_CRYPTO_METHOD_TLS_CLIENT,
|
|
|
|
'sslv2_server' => STREAM_CRYPTO_METHOD_SSLv2_SERVER,
|
|
|
|
'sslv3_server' => STREAM_CRYPTO_METHOD_SSLv3_SERVER,
|
|
|
|
'sslv23_server' => STREAM_CRYPTO_METHOD_SSLv23_SERVER,
|
|
|
|
'tls_server' => STREAM_CRYPTO_METHOD_TLS_SERVER
|
2012-08-10 00:54:04 +00:00
|
|
|
// @codingStandardsIgnoreEnd
|
2012-07-21 13:23:47 +00:00
|
|
|
);
|
|
|
|
|
2012-11-11 19:23:17 +00:00
|
|
|
/**
|
|
|
|
* Used to capture connection warnings which can happen when there are
|
|
|
|
* SSL errors for example.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_connectionErrors = array();
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param array $config Socket configuration, which will be merged with the base configuration
|
2010-03-28 16:53:57 +00:00
|
|
|
* @see CakeSocket::$_baseConfig
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-05-28 20:38:46 +00:00
|
|
|
public function __construct($config = array()) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->config = array_merge($this->_baseConfig, $config);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2015-01-15 22:39:09 +00:00
|
|
|
* Connects the socket to the given host and port.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool Success
|
2010-12-15 04:01:00 +00:00
|
|
|
* @throws SocketException
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function connect() {
|
2012-09-21 22:30:43 +00:00
|
|
|
if ($this->connection) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->disconnect();
|
|
|
|
}
|
|
|
|
|
2015-12-01 23:24:16 +00:00
|
|
|
$hasProtocol = strpos($this->config['host'], '://') !== false;
|
|
|
|
if ($hasProtocol) {
|
|
|
|
list($this->config['protocol'], $this->config['host']) = explode('://', $this->config['host']);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
$scheme = null;
|
2015-12-01 23:24:16 +00:00
|
|
|
if (!empty($this->config['protocol'])) {
|
2014-06-12 13:47:40 +00:00
|
|
|
$scheme = $this->config['protocol'] . '://';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2015-12-11 09:28:53 +00:00
|
|
|
if (!empty($this->config['proxy'])) {
|
|
|
|
$scheme = 'tcp://';
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2015-11-12 03:51:17 +00:00
|
|
|
$host = $this->config['host'];
|
|
|
|
if (isset($this->config['request']['uri']['host'])) {
|
|
|
|
$host = $this->config['request']['uri']['host'];
|
|
|
|
}
|
|
|
|
$this->_setSslContext($host);
|
|
|
|
|
2012-11-11 19:23:17 +00:00
|
|
|
if (!empty($this->config['context'])) {
|
|
|
|
$context = stream_context_create($this->config['context']);
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2012-11-11 03:58:36 +00:00
|
|
|
$context = stream_context_create();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2012-11-11 03:58:36 +00:00
|
|
|
$connectAs = STREAM_CLIENT_CONNECT;
|
|
|
|
if ($this->config['persistent']) {
|
2012-11-11 19:23:17 +00:00
|
|
|
$connectAs |= STREAM_CLIENT_PERSISTENT;
|
2012-11-11 03:58:36 +00:00
|
|
|
}
|
2012-11-11 19:23:17 +00:00
|
|
|
|
|
|
|
set_error_handler(array($this, '_connectionErrorHandler'));
|
|
|
|
$this->connection = stream_socket_client(
|
2012-11-11 03:58:36 +00:00
|
|
|
$scheme . $this->config['host'] . ':' . $this->config['port'],
|
|
|
|
$errNum,
|
|
|
|
$errStr,
|
|
|
|
$this->config['timeout'],
|
|
|
|
$connectAs,
|
|
|
|
$context
|
|
|
|
);
|
2012-11-11 19:23:17 +00:00
|
|
|
restore_error_handler();
|
2012-11-11 03:58:36 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!empty($errNum) || !empty($errStr)) {
|
2011-03-04 01:46:43 +00:00
|
|
|
$this->setLastError($errNum, $errStr);
|
2010-12-15 04:01:00 +00:00
|
|
|
throw new SocketException($errStr, $errNum);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2012-11-11 19:23:17 +00:00
|
|
|
if (!$this->connection && $this->_connectionErrors) {
|
|
|
|
$message = implode("\n", $this->_connectionErrors);
|
|
|
|
throw new SocketException($message, E_WARNING);
|
|
|
|
}
|
|
|
|
|
2009-07-15 19:17:41 +00:00
|
|
|
$this->connected = is_resource($this->connection);
|
|
|
|
if ($this->connected) {
|
|
|
|
stream_set_timeout($this->connection, $this->config['timeout']);
|
2015-03-17 12:40:36 +00:00
|
|
|
|
2015-03-24 02:28:16 +00:00
|
|
|
if (!empty($this->config['request']) &&
|
|
|
|
$this->config['request']['uri']['scheme'] === 'https' &&
|
|
|
|
!empty($this->config['proxy'])
|
|
|
|
) {
|
2015-03-19 08:13:14 +00:00
|
|
|
$req = array();
|
2015-03-24 02:28:16 +00:00
|
|
|
$req[] = 'CONNECT ' . $this->config['request']['uri']['host'] . ':' .
|
|
|
|
$this->config['request']['uri']['port'] . ' HTTP/1.1';
|
2015-03-19 08:13:14 +00:00
|
|
|
$req[] = 'Host: ' . $this->config['host'];
|
|
|
|
$req[] = 'User-Agent: php proxy';
|
2015-12-11 14:01:05 +00:00
|
|
|
if (!empty($this->config['proxyauth'])) {
|
2015-12-11 13:44:46 +00:00
|
|
|
$req[] = 'Proxy-Authorization: ' . $this->config['proxyauth'];
|
|
|
|
}
|
2015-03-17 12:40:36 +00:00
|
|
|
|
2015-03-19 08:13:14 +00:00
|
|
|
fwrite($this->connection, implode("\r\n", $req) . "\r\n\r\n");
|
2015-03-17 12:40:36 +00:00
|
|
|
|
2015-03-19 08:13:14 +00:00
|
|
|
while (!feof($this->connection)) {
|
|
|
|
$s = rtrim(fgets($this->connection, 4096));
|
|
|
|
if (preg_match('/^$/', $s)) {
|
|
|
|
break;
|
|
|
|
}
|
2015-03-17 12:40:36 +00:00
|
|
|
}
|
|
|
|
|
2015-03-19 08:13:14 +00:00
|
|
|
$this->enableCrypto('tls', 'client');
|
|
|
|
}
|
2015-03-17 12:40:36 +00:00
|
|
|
}
|
2009-07-15 19:17:41 +00:00
|
|
|
return $this->connected;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2015-10-13 01:56:20 +00:00
|
|
|
/**
|
|
|
|
* Configure the SSL context options.
|
|
|
|
*
|
|
|
|
* @param string $host The host name being connected to.
|
2015-10-30 03:54:22 +00:00
|
|
|
* @return void
|
2015-10-13 01:56:20 +00:00
|
|
|
*/
|
2015-10-13 02:20:15 +00:00
|
|
|
protected function _setSslContext($host) {
|
2015-10-13 01:56:20 +00:00
|
|
|
foreach ($this->config as $key => $value) {
|
|
|
|
if (substr($key, 0, 4) !== 'ssl_') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$contextKey = substr($key, 4);
|
|
|
|
if (empty($this->config['context']['ssl'][$contextKey])) {
|
|
|
|
$this->config['context']['ssl'][$contextKey] = $value;
|
|
|
|
}
|
|
|
|
unset($this->config[$key]);
|
|
|
|
}
|
|
|
|
if (version_compare(PHP_VERSION, '5.3.2', '>=')) {
|
|
|
|
if (!isset($this->config['context']['ssl']['SNI_enabled'])) {
|
|
|
|
$this->config['context']['ssl']['SNI_enabled'] = true;
|
|
|
|
}
|
|
|
|
if (version_compare(PHP_VERSION, '5.6.0', '>=')) {
|
|
|
|
if (empty($this->config['context']['ssl']['peer_name'])) {
|
|
|
|
$this->config['context']['ssl']['peer_name'] = $host;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (empty($this->config['context']['ssl']['SNI_server_name'])) {
|
|
|
|
$this->config['context']['ssl']['SNI_server_name'] = $host;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (empty($this->config['context']['ssl']['cafile'])) {
|
|
|
|
$this->config['context']['ssl']['cafile'] = CAKE . 'Config' . DS . 'cacert.pem';
|
|
|
|
}
|
|
|
|
if (!empty($this->config['context']['ssl']['verify_host'])) {
|
|
|
|
$this->config['context']['ssl']['CN_match'] = $host;
|
|
|
|
}
|
|
|
|
unset($this->config['context']['ssl']['verify_host']);
|
|
|
|
}
|
|
|
|
|
2012-11-11 19:23:17 +00:00
|
|
|
/**
|
|
|
|
* socket_stream_client() does not populate errNum, or $errStr when there are
|
|
|
|
* connection errors, as in the case of SSL verification failure.
|
|
|
|
*
|
|
|
|
* Instead we need to handle those errors manually.
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int $code Code.
|
2014-05-31 21:36:05 +00:00
|
|
|
* @param string $message Message.
|
2013-07-05 15:19:22 +00:00
|
|
|
* @return void
|
2012-11-11 19:23:17 +00:00
|
|
|
*/
|
|
|
|
protected function _connectionErrorHandler($code, $message) {
|
|
|
|
$this->_connectionErrors[] = $message;
|
|
|
|
}
|
|
|
|
|
2012-11-11 03:58:36 +00:00
|
|
|
/**
|
2015-01-15 22:39:09 +00:00
|
|
|
* Gets the connection context.
|
2012-11-11 03:58:36 +00:00
|
|
|
*
|
2013-03-05 07:05:14 +00:00
|
|
|
* @return null|array Null when there is no connection, an array when there is.
|
2012-11-11 03:58:36 +00:00
|
|
|
*/
|
|
|
|
public function context() {
|
2012-11-11 19:23:17 +00:00
|
|
|
if (!$this->connection) {
|
2015-09-25 15:11:20 +00:00
|
|
|
return null;
|
2012-11-11 19:23:17 +00:00
|
|
|
}
|
2012-11-11 03:58:36 +00:00
|
|
|
return stream_context_get_options($this->connection);
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2015-01-15 22:39:09 +00:00
|
|
|
* Gets the host name of the current connection.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @return string Host name
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function host() {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (Validation::ip($this->config['host'])) {
|
|
|
|
return gethostbyaddr($this->config['host']);
|
|
|
|
}
|
2010-11-09 21:07:29 +00:00
|
|
|
return gethostbyaddr($this->address());
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2015-01-15 22:39:09 +00:00
|
|
|
* Gets the IP address of the current connection.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @return string IP address
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function address() {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (Validation::ip($this->config['host'])) {
|
|
|
|
return $this->config['host'];
|
|
|
|
}
|
2010-11-09 21:07:29 +00:00
|
|
|
return gethostbyname($this->config['host']);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2015-01-15 22:39:09 +00:00
|
|
|
* Gets all IP addresses associated with the current connection.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @return array IP addresses
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function addresses() {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (Validation::ip($this->config['host'])) {
|
|
|
|
return array($this->config['host']);
|
|
|
|
}
|
2010-11-09 21:07:29 +00:00
|
|
|
return gethostbynamel($this->config['host']);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2015-01-15 22:39:09 +00:00
|
|
|
* Gets the last error as a string.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2014-11-05 12:03:27 +00:00
|
|
|
* @return string|null Last error
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function lastError() {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!empty($this->lastError)) {
|
2010-03-28 16:53:57 +00:00
|
|
|
return $this->lastError['num'] . ': ' . $this->lastError['str'];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-11-09 21:07:29 +00:00
|
|
|
return null;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2015-01-15 22:39:09 +00:00
|
|
|
* Sets the last error.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int $errNum Error code
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $errStr Error string
|
2010-11-09 21:07:29 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function setLastError($errNum, $errStr) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->lastError = array('num' => $errNum, 'str' => $errStr);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2015-01-15 22:39:09 +00:00
|
|
|
* Writes data to the socket.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $data The data to write to the socket
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool Success
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function write($data) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!$this->connected) {
|
|
|
|
if (!$this->connect()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2011-04-08 00:07:36 +00:00
|
|
|
$totalBytes = strlen($data);
|
|
|
|
for ($written = 0, $rv = 0; $written < $totalBytes; $written += $rv) {
|
|
|
|
$rv = fwrite($this->connection, substr($data, $written));
|
|
|
|
if ($rv === false || $rv === 0) {
|
|
|
|
return $written;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $written;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-01-15 22:39:09 +00:00
|
|
|
* Reads data from the socket. Returns false if no data is available or no connection could be
|
2008-05-30 11:40:08 +00:00
|
|
|
* established.
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int $length Optional buffer length to read; defaults to 1024
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return mixed Socket data
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function read($length = 1024) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!$this->connected) {
|
|
|
|
if (!$this->connect()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-12 23:02:39 +00:00
|
|
|
if (!feof($this->connection)) {
|
2009-07-15 19:17:41 +00:00
|
|
|
$buffer = fread($this->connection, $length);
|
|
|
|
$info = stream_get_meta_data($this->connection);
|
|
|
|
if ($info['timed_out']) {
|
2011-03-20 15:35:43 +00:00
|
|
|
$this->setLastError(E_WARNING, __d('cake_dev', 'Connection timed out'));
|
2009-07-15 19:17:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $buffer;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-11-09 21:07:29 +00:00
|
|
|
return false;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2015-01-15 22:39:09 +00:00
|
|
|
* Disconnects the socket from the current connection.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool Success
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function disconnect() {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!is_resource($this->connection)) {
|
|
|
|
$this->connected = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$this->connected = !fclose($this->connection);
|
|
|
|
|
|
|
|
if (!$this->connected) {
|
|
|
|
$this->connection = null;
|
|
|
|
}
|
|
|
|
return !$this->connected;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Destructor, used to disconnect from current connection.
|
|
|
|
*/
|
2011-05-28 20:38:46 +00:00
|
|
|
public function __destruct() {
|
2008-06-20 20:17:23 +00:00
|
|
|
$this->disconnect();
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Resets the state of this Socket instance to it's initial state (before Object::__construct got executed)
|
|
|
|
*
|
2010-11-09 21:07:29 +00:00
|
|
|
* @param array $state Array with key and values to reset
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool True on success
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function reset($state = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (empty($state)) {
|
|
|
|
static $initalState = array();
|
|
|
|
if (empty($initalState)) {
|
|
|
|
$initalState = get_class_vars(__CLASS__);
|
|
|
|
}
|
|
|
|
$state = $initalState;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($state as $property => $value) {
|
|
|
|
$this->{$property} = $value;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2012-03-04 16:18:20 +00:00
|
|
|
|
2012-07-21 13:23:47 +00:00
|
|
|
/**
|
2015-01-15 22:39:09 +00:00
|
|
|
* Encrypts current stream socket, using one of the defined encryption methods.
|
2012-08-10 00:51:29 +00:00
|
|
|
*
|
2015-01-15 22:39:09 +00:00
|
|
|
* @param string $type Type which can be one of 'sslv2', 'sslv3', 'sslv23' or 'tls'.
|
|
|
|
* @param string $clientOrServer Can be one of 'client', 'server'. Default is 'client'.
|
|
|
|
* @param bool $enable Enable or disable encryption. Default is true (enable)
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool True on success
|
2012-08-10 00:51:29 +00:00
|
|
|
* @throws InvalidArgumentException When an invalid encryption scheme is chosen.
|
2015-01-15 22:39:09 +00:00
|
|
|
* @throws SocketException When attempting to enable SSL/TLS fails.
|
2012-08-10 00:51:29 +00:00
|
|
|
* @see stream_socket_enable_crypto
|
2012-07-21 13:23:47 +00:00
|
|
|
*/
|
|
|
|
public function enableCrypto($type, $clientOrServer = 'client', $enable = true) {
|
|
|
|
if (!array_key_exists($type . '_' . $clientOrServer, $this->_encryptMethods)) {
|
2012-07-25 18:20:30 +00:00
|
|
|
throw new InvalidArgumentException(__d('cake_dev', 'Invalid encryption scheme chosen'));
|
2012-07-21 13:23:47 +00:00
|
|
|
}
|
|
|
|
$enableCryptoResult = false;
|
|
|
|
try {
|
2015-01-15 22:39:09 +00:00
|
|
|
$enableCryptoResult = stream_socket_enable_crypto($this->connection, $enable,
|
|
|
|
$this->_encryptMethods[$type . '_' . $clientOrServer]);
|
2012-07-21 13:23:47 +00:00
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->setLastError(null, $e->getMessage());
|
|
|
|
throw new SocketException($e->getMessage());
|
|
|
|
}
|
|
|
|
if ($enableCryptoResult === true) {
|
|
|
|
$this->encrypted = $enable;
|
|
|
|
return true;
|
|
|
|
}
|
2013-07-02 23:14:41 +00:00
|
|
|
$errorMessage = __d('cake_dev', 'Unable to perform enableCrypto operation on CakeSocket');
|
|
|
|
$this->setLastError(null, $errorMessage);
|
|
|
|
throw new SocketException($errorMessage);
|
2012-08-10 00:51:29 +00:00
|
|
|
}
|
|
|
|
}
|