Upgrade CakePHP from 2.2.5 to 2.9.5

This commit is contained in:
Brm Ko 2017-02-26 15:29:44 +01:00
parent 5a580df460
commit 235a541597
793 changed files with 60746 additions and 23753 deletions

View file

@ -1,26 +1,25 @@
<?php
/**
* Cake Socket connection class.
*
* PHP 5
* CakePHP Socket connection class.
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.Network
* @since CakePHP(tm) v 1.2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('Validation', 'Utility');
/**
* Cake network socket connection class.
* CakePHP network socket connection class.
*
* Core base class for network communication.
*
@ -29,7 +28,7 @@ App::uses('Validation', 'Utility');
class CakeSocket {
/**
* Object description
* CakeSocket description
*
* @var string
*/
@ -41,11 +40,11 @@ class CakeSocket {
* @var array
*/
protected $_baseConfig = array(
'persistent' => false,
'host' => 'localhost',
'protocol' => 'tcp',
'port' => 80,
'timeout' => 30
'persistent' => false,
'host' => 'localhost',
'protocol' => 'tcp',
'port' => 80,
'timeout' => 30
);
/**
@ -65,7 +64,7 @@ class CakeSocket {
/**
* This boolean contains the current state of the CakeSocket class
*
* @var boolean
* @var bool
*/
public $connected = false;
@ -76,6 +75,39 @@ class CakeSocket {
*/
public $lastError = array();
/**
* True if the socket stream is encrypted after a CakeSocket::enableCrypto() call
*
* @var bool
*/
public $encrypted = false;
/**
* Contains all the encryption methods available
*
* @var array
*/
protected $_encryptMethods = array(
// @codingStandardsIgnoreStart
'sslv2_client' => STREAM_CRYPTO_METHOD_SSLv2_CLIENT,
'sslv3_client' => STREAM_CRYPTO_METHOD_SSLv3_CLIENT,
'sslv23_client' => STREAM_CRYPTO_METHOD_SSLv23_CLIENT,
'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
// @codingStandardsIgnoreEnd
);
/**
* Used to capture connection warnings which can happen when there are
* SSL errors for example.
*
* @var array
*/
protected $_connectionErrors = array();
/**
* Constructor.
*
@ -84,49 +116,169 @@ class CakeSocket {
*/
public function __construct($config = array()) {
$this->config = array_merge($this->_baseConfig, $config);
if (!is_numeric($this->config['protocol'])) {
$this->config['protocol'] = getprotobyname($this->config['protocol']);
}
}
/**
* Connect the socket to the given host and port.
* Connects the socket to the given host and port.
*
* @return boolean Success
* @return bool Success
* @throws SocketException
*/
public function connect() {
if ($this->connection != null) {
if ($this->connection) {
$this->disconnect();
}
$hasProtocol = strpos($this->config['host'], '://') !== false;
if ($hasProtocol) {
list($this->config['protocol'], $this->config['host']) = explode('://', $this->config['host']);
}
$scheme = null;
if (isset($this->config['request']) && $this->config['request']['uri']['scheme'] == 'https') {
$scheme = 'ssl://';
if (!empty($this->config['protocol'])) {
$scheme = $this->config['protocol'] . '://';
}
if (!empty($this->config['proxy'])) {
$scheme = 'tcp://';
}
//@codingStandardsIgnoreStart
if ($this->config['persistent'] == true) {
$this->connection = @pfsockopen($scheme . $this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']);
} else {
$this->connection = @fsockopen($scheme . $this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']);
$host = $this->config['host'];
if (isset($this->config['request']['uri']['host'])) {
$host = $this->config['request']['uri']['host'];
}
//@codingStandardsIgnoreEnd
$this->_setSslContext($host);
if (!empty($this->config['context'])) {
$context = stream_context_create($this->config['context']);
} else {
$context = stream_context_create();
}
$connectAs = STREAM_CLIENT_CONNECT;
if ($this->config['persistent']) {
$connectAs |= STREAM_CLIENT_PERSISTENT;
}
set_error_handler(array($this, '_connectionErrorHandler'));
$this->connection = stream_socket_client(
$scheme . $this->config['host'] . ':' . $this->config['port'],
$errNum,
$errStr,
$this->config['timeout'],
$connectAs,
$context
);
restore_error_handler();
if (!empty($errNum) || !empty($errStr)) {
$this->setLastError($errNum, $errStr);
throw new SocketException($errStr, $errNum);
}
if (!$this->connection && $this->_connectionErrors) {
$message = implode("\n", $this->_connectionErrors);
throw new SocketException($message, E_WARNING);
}
$this->connected = is_resource($this->connection);
if ($this->connected) {
stream_set_timeout($this->connection, $this->config['timeout']);
if (!empty($this->config['request']) &&
$this->config['request']['uri']['scheme'] === 'https' &&
!empty($this->config['proxy'])
) {
$req = array();
$req[] = 'CONNECT ' . $this->config['request']['uri']['host'] . ':' .
$this->config['request']['uri']['port'] . ' HTTP/1.1';
$req[] = 'Host: ' . $this->config['host'];
$req[] = 'User-Agent: php proxy';
if (!empty($this->config['proxyauth'])) {
$req[] = 'Proxy-Authorization: ' . $this->config['proxyauth'];
}
fwrite($this->connection, implode("\r\n", $req) . "\r\n\r\n");
while (!feof($this->connection)) {
$s = rtrim(fgets($this->connection, 4096));
if (preg_match('/^$/', $s)) {
break;
}
}
$this->enableCrypto('tls', 'client');
}
}
return $this->connected;
}
/**
* Get the host name of the current connection.
* Configure the SSL context options.
*
* @param string $host The host name being connected to.
* @return void
*/
protected function _setSslContext($host) {
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']);
}
/**
* 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.
*
* @param int $code Code.
* @param string $message Message.
* @return void
*/
protected function _connectionErrorHandler($code, $message) {
$this->_connectionErrors[] = $message;
}
/**
* Gets the connection context.
*
* @return null|array Null when there is no connection, an array when there is.
*/
public function context() {
if (!$this->connection) {
return null;
}
return stream_context_get_options($this->connection);
}
/**
* Gets the host name of the current connection.
*
* @return string Host name
*/
@ -138,7 +290,7 @@ class CakeSocket {
}
/**
* Get the IP address of the current connection.
* Gets the IP address of the current connection.
*
* @return string IP address
*/
@ -150,7 +302,7 @@ class CakeSocket {
}
/**
* Get all IP addresses associated with the current connection.
* Gets all IP addresses associated with the current connection.
*
* @return array IP addresses
*/
@ -162,9 +314,9 @@ class CakeSocket {
}
/**
* Get the last error as a string.
* Gets the last error as a string.
*
* @return string Last error
* @return string|null Last error
*/
public function lastError() {
if (!empty($this->lastError)) {
@ -174,9 +326,9 @@ class CakeSocket {
}
/**
* Set the last error.
* Sets the last error.
*
* @param integer $errNum Error code
* @param int $errNum Error code
* @param string $errStr Error string
* @return void
*/
@ -185,10 +337,10 @@ class CakeSocket {
}
/**
* Write data to the socket.
* Writes data to the socket.
*
* @param string $data The data to write to the socket
* @return boolean Success
* @return bool Success
*/
public function write($data) {
if (!$this->connected) {
@ -207,10 +359,10 @@ class CakeSocket {
}
/**
* Read data from the socket. Returns false if no data is available or no connection could be
* Reads data from the socket. Returns false if no data is available or no connection could be
* established.
*
* @param integer $length Optional buffer length to read; defaults to 1024
* @param int $length Optional buffer length to read; defaults to 1024
* @return mixed Socket data
*/
public function read($length = 1024) {
@ -233,9 +385,9 @@ class CakeSocket {
}
/**
* Disconnect the socket from the current connection.
* Disconnects the socket from the current connection.
*
* @return boolean Success
* @return bool Success
*/
public function disconnect() {
if (!is_resource($this->connection)) {
@ -252,17 +404,16 @@ class CakeSocket {
/**
* Destructor, used to disconnect from current connection.
*
*/
public function __destruct() {
$this->disconnect();
}
/**
* Resets the state of this Socket instance to it's initial state (before Object::__construct got executed)
* Resets the state of this Socket instance to it's initial state (before CakeObject::__construct got executed)
*
* @param array $state Array with key and values to reset
* @return boolean True on success
* @return bool True on success
*/
public function reset($state = null) {
if (empty($state)) {
@ -279,4 +430,35 @@ class CakeSocket {
return true;
}
/**
* Encrypts current stream socket, using one of the defined encryption methods.
*
* @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)
* @return bool True on success
* @throws InvalidArgumentException When an invalid encryption scheme is chosen.
* @throws SocketException When attempting to enable SSL/TLS fails.
* @see stream_socket_enable_crypto
*/
public function enableCrypto($type, $clientOrServer = 'client', $enable = true) {
if (!array_key_exists($type . '_' . $clientOrServer, $this->_encryptMethods)) {
throw new InvalidArgumentException(__d('cake_dev', 'Invalid encryption scheme chosen'));
}
$enableCryptoResult = false;
try {
$enableCryptoResult = stream_socket_enable_crypto($this->connection, $enable,
$this->_encryptMethods[$type . '_' . $clientOrServer]);
} catch (Exception $e) {
$this->setLastError(null, $e->getMessage());
throw new SocketException($e->getMessage());
}
if ($enableCryptoResult === true) {
$this->encrypted = $enable;
return true;
}
$errorMessage = __d('cake_dev', 'Unable to perform enableCrypto operation on CakeSocket');
$this->setLastError(null, $errorMessage);
throw new SocketException($errorMessage);
}
}