2007-01-25 14:34:11 +00:00
|
|
|
<?php
|
|
|
|
/* SVN FILE: $Id$ */
|
|
|
|
/**
|
|
|
|
* Cake Socket connection class.
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
2007-02-02 10:39:45 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
|
2008-01-01 22:18:17 +00:00
|
|
|
* Copyright 2005-2008, Cake Software Foundation, Inc.
|
2007-01-25 14:34:11 +00:00
|
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
|
|
|
* Las Vegas, Nevada 89104
|
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @filesource
|
2008-01-01 22:18:17 +00:00
|
|
|
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc.
|
2007-02-02 10:39:45 +00:00
|
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
2007-01-25 14:34:11 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs
|
2007-02-02 10:39:45 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0
|
2007-01-25 14:34:11 +00:00
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
|
*/
|
2008-01-08 03:50:19 +00:00
|
|
|
App::import('Core', 'Validation');
|
2007-01-25 14:34:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cake network socket connection class.
|
|
|
|
*
|
|
|
|
* Core base class for network communication.
|
|
|
|
*
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs
|
|
|
|
*/
|
|
|
|
class CakeSocket extends Object {
|
|
|
|
/**
|
|
|
|
* Object description
|
|
|
|
*
|
|
|
|
* @var string
|
2007-05-26 19:33:55 +00:00
|
|
|
* @access public
|
2007-01-25 14:34:11 +00:00
|
|
|
*/
|
|
|
|
var $description = 'Remote DataSource Network Socket Interface';
|
|
|
|
/**
|
|
|
|
* Base configuration settings for the socket connection
|
|
|
|
*
|
|
|
|
* @var array
|
2007-05-26 19:33:55 +00:00
|
|
|
* @access protected
|
2007-01-25 14:34:11 +00:00
|
|
|
*/
|
|
|
|
var $_baseConfig = array(
|
|
|
|
'persistent' => false,
|
|
|
|
'host' => 'localhost',
|
|
|
|
'protocol' => 'tcp',
|
|
|
|
'port' => 80,
|
|
|
|
'timeout' => 30
|
|
|
|
);
|
|
|
|
/**
|
|
|
|
* Configuration settings for the socket connection
|
|
|
|
*
|
|
|
|
* @var array
|
2007-05-26 19:33:55 +00:00
|
|
|
* @access public
|
2007-01-25 14:34:11 +00:00
|
|
|
*/
|
|
|
|
var $config = array();
|
|
|
|
/**
|
|
|
|
* Reference to socket connection resource
|
|
|
|
*
|
|
|
|
* @var resource
|
2007-05-26 19:33:55 +00:00
|
|
|
* @access public
|
2007-01-25 14:34:11 +00:00
|
|
|
*/
|
|
|
|
var $connection = null;
|
|
|
|
/**
|
|
|
|
* This boolean contains the current state of the CakeSocket class
|
|
|
|
*
|
|
|
|
* @var boolean
|
2007-05-26 19:33:55 +00:00
|
|
|
* @access public
|
2007-01-25 14:34:11 +00:00
|
|
|
*/
|
|
|
|
var $connected = false;
|
|
|
|
/**
|
|
|
|
* This variable contains an array with the last error number (num) and string (str)
|
|
|
|
*
|
|
|
|
* @var array
|
2007-05-26 19:33:55 +00:00
|
|
|
* @access public
|
2007-01-25 14:34:11 +00:00
|
|
|
*/
|
2008-05-05 14:33:35 +00:00
|
|
|
var $lastError = array();
|
2007-01-25 14:34:11 +00:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param array $config Socket configuration, which will be merged with the base configuration
|
|
|
|
*/
|
|
|
|
function __construct($config = array()) {
|
|
|
|
parent::__construct();
|
2007-05-26 19:33:55 +00:00
|
|
|
|
2008-01-08 03:50:19 +00:00
|
|
|
$this->config = array_merge($this->_baseConfig, $config);
|
2007-01-25 14:34:11 +00:00
|
|
|
if (!is_numeric($this->config['protocol'])) {
|
|
|
|
$this->config['protocol'] = getprotobyname($this->config['protocol']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Connect the socket to the given host and port.
|
|
|
|
*
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean Success
|
2007-05-26 19:33:55 +00:00
|
|
|
* @access public
|
2007-01-25 14:34:11 +00:00
|
|
|
*/
|
|
|
|
function connect() {
|
|
|
|
if ($this->connection != null) {
|
|
|
|
$this->disconnect();
|
|
|
|
}
|
2007-12-08 06:08:03 +00:00
|
|
|
|
2007-11-01 21:37:52 +00:00
|
|
|
$scheme = null;
|
|
|
|
if (isset($this->config['request']) && $this->config['request']['uri']['scheme'] == 'https') {
|
|
|
|
$scheme = 'ssl://';
|
|
|
|
}
|
2007-01-25 14:34:11 +00:00
|
|
|
|
|
|
|
if ($this->config['persistent'] == true) {
|
2007-05-26 19:33:55 +00:00
|
|
|
$tmp = null;
|
2007-11-01 21:37:52 +00:00
|
|
|
$this->connection = @pfsockopen($scheme.$this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']);
|
2007-01-25 14:34:11 +00:00
|
|
|
} else {
|
2008-05-05 14:33:35 +00:00
|
|
|
$this->connection = @fsockopen($scheme.$this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']);
|
2007-01-25 14:34:11 +00:00
|
|
|
}
|
2007-05-26 19:33:55 +00:00
|
|
|
|
2007-01-25 14:34:11 +00:00
|
|
|
if (!empty($errNum) || !empty($errStr)) {
|
|
|
|
$this->setLastError($errStr, $errNum);
|
|
|
|
}
|
2007-05-26 19:33:55 +00:00
|
|
|
|
2008-05-05 14:33:35 +00:00
|
|
|
return $this->connected = is_resource($this->connection);
|
2007-01-25 14:34:11 +00:00
|
|
|
}
|
2007-05-26 19:33:55 +00:00
|
|
|
|
2007-01-25 14:34:11 +00:00
|
|
|
/**
|
|
|
|
* Get the host name of the current connection.
|
|
|
|
*
|
|
|
|
* @return string Host name
|
2007-05-26 19:33:55 +00:00
|
|
|
* @access public
|
2007-01-25 14:34:11 +00:00
|
|
|
*/
|
|
|
|
function host() {
|
|
|
|
if (Validation::ip($this->config['host'])) {
|
|
|
|
return gethostbyaddr($this->config['host']);
|
|
|
|
} else {
|
|
|
|
return gethostbyaddr($this->address());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Get the IP address of the current connection.
|
|
|
|
*
|
|
|
|
* @return string IP address
|
2007-05-26 19:33:55 +00:00
|
|
|
* @access public
|
2007-01-25 14:34:11 +00:00
|
|
|
*/
|
|
|
|
function address() {
|
|
|
|
if (Validation::ip($this->config['host'])) {
|
|
|
|
return $this->config['host'];
|
|
|
|
} else {
|
|
|
|
return gethostbyname($this->config['host']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Get all IP addresses associated with the current connection.
|
|
|
|
*
|
|
|
|
* @return array IP addresses
|
2007-05-26 19:33:55 +00:00
|
|
|
* @access public
|
2007-01-25 14:34:11 +00:00
|
|
|
*/
|
|
|
|
function addresses() {
|
|
|
|
if (Validation::ip($this->config['host'])) {
|
|
|
|
return array($this->config['host']);
|
|
|
|
} else {
|
|
|
|
return gethostbynamel($this->config['host']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Get the last error as a string.
|
|
|
|
*
|
2007-05-26 19:33:55 +00:00
|
|
|
* @return string Last error
|
|
|
|
* @access public
|
2007-01-25 14:34:11 +00:00
|
|
|
*/
|
|
|
|
function lastError() {
|
2008-05-05 14:33:35 +00:00
|
|
|
if (!empty($this->lastError)) {
|
|
|
|
return $this->lastError['num'].': '.$this->lastError['str'];
|
2007-01-25 14:34:11 +00:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Set the last error.
|
|
|
|
*
|
2007-10-22 16:11:12 +00:00
|
|
|
* @param integer $errNum Error code
|
2007-01-25 14:34:11 +00:00
|
|
|
* @param string $errStr Error string
|
2007-05-26 19:33:55 +00:00
|
|
|
* @access public
|
2007-01-25 14:34:11 +00:00
|
|
|
*/
|
|
|
|
function setLastError($errNum, $errStr) {
|
|
|
|
$this->lastError = array('num' => $errNum, 'str' => $errStr);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Write data to the socket.
|
|
|
|
*
|
|
|
|
* @param string $data The data to write to the socket
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean Success
|
2007-05-26 19:33:55 +00:00
|
|
|
* @access public
|
2007-01-25 14:34:11 +00:00
|
|
|
*/
|
|
|
|
function write($data) {
|
|
|
|
if (!$this->connected) {
|
|
|
|
if (!$this->connect()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2007-05-26 19:33:55 +00:00
|
|
|
|
2007-01-25 14:34:11 +00:00
|
|
|
return fwrite($this->connection, $data, strlen($data));
|
|
|
|
}
|
2007-05-26 19:33:55 +00:00
|
|
|
|
2007-01-25 14:34:11 +00:00
|
|
|
/**
|
|
|
|
* Read data from the socket. Returns false if no data is available or no connection could be
|
|
|
|
* established.
|
|
|
|
*
|
2007-10-22 16:11:12 +00:00
|
|
|
* @param integer $length Optional buffer length to read; defaults to 1024
|
2007-01-25 14:34:11 +00:00
|
|
|
* @return mixed Socket data
|
2007-05-26 19:33:55 +00:00
|
|
|
* @access public
|
2007-01-25 14:34:11 +00:00
|
|
|
*/
|
|
|
|
function read($length = 1024) {
|
|
|
|
if (!$this->connected) {
|
|
|
|
if (!$this->connect()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2007-05-26 19:33:55 +00:00
|
|
|
|
2007-01-25 14:34:11 +00:00
|
|
|
if (!feof($this->connection)) {
|
|
|
|
return fread($this->connection, $length);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Abort socket operation.
|
|
|
|
*
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean Success
|
2007-05-26 19:33:55 +00:00
|
|
|
* @access public
|
2007-01-25 14:34:11 +00:00
|
|
|
*/
|
|
|
|
function abort() {
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Disconnect the socket from the current connection.
|
|
|
|
*
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean Success
|
2007-05-26 19:33:55 +00:00
|
|
|
* @access public
|
2007-01-25 14:34:11 +00:00
|
|
|
*/
|
|
|
|
function disconnect() {
|
2007-05-15 19:59:59 +00:00
|
|
|
if (!is_resource($this->connection)) {
|
|
|
|
$this->connected = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$this->connected = !fclose($this->connection);
|
|
|
|
|
2007-01-25 14:34:11 +00:00
|
|
|
if (!$this->connected) {
|
|
|
|
$this->connection = null;
|
|
|
|
}
|
|
|
|
return !$this->connected;
|
|
|
|
}
|
|
|
|
/**
|
2007-05-26 19:33:55 +00:00
|
|
|
* Destructor, used to disconnect from current connection.
|
2007-01-25 14:34:11 +00:00
|
|
|
*
|
2007-05-26 19:33:55 +00:00
|
|
|
* @access private
|
2007-01-25 14:34:11 +00:00
|
|
|
*/
|
|
|
|
function __destruct() {
|
|
|
|
$this->disconnect();
|
|
|
|
}
|
2008-05-05 14:33:35 +00:00
|
|
|
/**
|
|
|
|
* Resets the state of this Socket instance to it's initial state (before Object::__construct got executed)
|
|
|
|
*
|
|
|
|
* @return boolean True on success
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function reset($initialState = null) {
|
|
|
|
if (empty($initalState)) {
|
|
|
|
static $initalState = array();
|
|
|
|
if (empty($initalState)) {
|
|
|
|
$initalState = get_class_vars(__CLASS__);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($initalState as $property => $value) {
|
|
|
|
$this->{$property} = $value;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2007-01-25 14:34:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|