1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
<?php
/* SVN FILE: $Id: socket.php 7296 2008-06-27 09:09:03Z gwoo $ */
/**
* Cake Socket connection class.
*
* PHP versions 4 and 5
*
* CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
* Copyright 2005-2008, Cake Software Foundation, Inc.
* 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
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc.
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
* @package cake
* @subpackage cake.cake.libs
* @since CakePHP(tm) v 1.2.0
* @version $Revision: 7296 $
* @modifiedby $LastChangedBy: gwoo $
* @lastmodified $Date: 2008-06-27 02:09:03 -0700 (Fri, 27 Jun 2008) $
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
App::import('Core', 'Validation');
/**
* 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
* @access public
*/
var $description = 'Remote DataSource Network Socket Interface';
/**
* Base configuration settings for the socket connection
*
* @var array
* @access protected
*/
var $_baseConfig = array(
'persistent' => false,
'host' => 'localhost',
'protocol' => 'tcp',
'port' => 80,
'timeout' => 30
);
/**
* Configuration settings for the socket connection
*
* @var array
* @access public
*/
var $config = array();
/**
* Reference to socket connection resource
*
* @var resource
* @access public
*/
var $connection = null;
/**
* This boolean contains the current state of the CakeSocket class
*
* @var boolean
* @access public
*/
var $connected = false;
/**
* This variable contains an array with the last error number (num) and string (str)
*
* @var array
* @access public
*/
var $lastError = array();
/**
* Constructor.
*
* @param array $config Socket configuration, which will be merged with the base configuration
*/
function __construct($config = array()) {
parent::__construct();
$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.
*
* @return boolean Success
* @access public
*/
function connect() {
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) {
$tmp = null;
$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)) {
$this->setLastError($errStr, $errNum);
}
return $this->connected = is_resource($this->connection);
}
/**
* Get the host name of the current connection.
*
* @return string Host name
* @access public
*/
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
* @access public
*/
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
* @access public
*/
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.
*
* @return string Last error
* @access public
*/
function lastError() {
if (!empty($this->lastError)) {
return $this->lastError['num'].': '.$this->lastError['str'];
} else {
return null;
}
}
/**
* Set the last error.
*
* @param integer $errNum Error code
* @param string $errStr Error string
* @access public
*/
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
* @return boolean Success
* @access public
*/
function write($data) {
if (!$this->connected) {
if (!$this->connect()) {
return false;
}
}
return fwrite($this->connection, $data, strlen($data));
}
/**
* 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
* @access public
*/
function read($length = 1024) {
if (!$this->connected) {
if (!$this->connect()) {
return false;
}
}
if (!feof($this->connection)) {
return fread($this->connection, $length);
} else {
return false;
}
}
/**
* Abort socket operation.
*
* @return boolean Success
* @access public
*/
function abort() {
}
/**
* Disconnect the socket from the current connection.
*
* @return boolean Success
* @access public
*/
function disconnect() {
if (!is_resource($this->connection)) {
$this->connected = false;
return true;
}
$this->connected = !fclose($this->connection);
if (!$this->connected) {
$this->connection = null;
}
return !$this->connected;
}
/**
* Destructor, used to disconnect from current connection.
*
* @access private
*/
function __destruct() {
$this->disconnect();
}
/**
* 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($state = null) {
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;
}
}
?>
|