2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Cake Socket connection class.
|
|
|
|
*
|
2010-10-03 16:38:58 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2011-05-29 21:31:39 +00:00
|
|
|
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2011-05-29 21:31:39 +00:00
|
|
|
* @copyright Copyright 2005-2011, 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
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +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
|
|
|
/**
|
|
|
|
* Cake network socket connection class.
|
|
|
|
*
|
|
|
|
* 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(
|
2008-05-30 11:40:08 +00:00
|
|
|
'persistent' => false,
|
|
|
|
'host' => 'localhost',
|
|
|
|
'protocol' => 'tcp',
|
|
|
|
'port' => 80,
|
|
|
|
'timeout' => 30
|
|
|
|
);
|
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
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
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
|
|
|
|
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);
|
|
|
|
if (!is_numeric($this->config['protocol'])) {
|
|
|
|
$this->config['protocol'] = getprotobyname($this->config['protocol']);
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Connect the socket to the given host and port.
|
|
|
|
*
|
|
|
|
* @return boolean 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() {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($this->connection != null) {
|
|
|
|
$this->disconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
$scheme = null;
|
|
|
|
if (isset($this->config['request']) && $this->config['request']['uri']['scheme'] == 'https') {
|
|
|
|
$scheme = 'ssl://';
|
|
|
|
}
|
|
|
|
|
|
|
|
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']);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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']);
|
|
|
|
}
|
|
|
|
return $this->connected;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the host name of the current connection.
|
|
|
|
*
|
|
|
|
* @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
|
|
|
/**
|
|
|
|
* Get the IP address of the current connection.
|
|
|
|
*
|
|
|
|
* @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
|
|
|
/**
|
|
|
|
* Get all IP addresses associated with the current connection.
|
|
|
|
*
|
|
|
|
* @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
|
|
|
/**
|
|
|
|
* Get the last error as a string.
|
|
|
|
*
|
|
|
|
* @return string Last error
|
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* Set the last error.
|
|
|
|
*
|
|
|
|
* @param integer $errNum Error code
|
|
|
|
* @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
|
|
|
/**
|
|
|
|
* Write data to the socket.
|
|
|
|
*
|
|
|
|
* @param string $data The data to write to the socket
|
|
|
|
* @return boolean Success
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read 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
|
|
|
|
* @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
|
|
|
/**
|
|
|
|
* Disconnect the socket from the current connection.
|
|
|
|
*
|
|
|
|
* @return boolean Success
|
|
|
|
*/
|
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
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return boolean True on success
|
|
|
|
*/
|
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;
|
|
|
|
}
|
|
|
|
}
|