socket.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: socket.php 8286 2009-08-21 11:34:32Z the_undefined $ */
00003 /**
00004  * Cake Socket connection class.
00005  *
00006  * PHP versions 4 and 5
00007  *
00008  * CakePHP(tm) :  Rapid Development Framework (http://www.cakephp.org)
00009  * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
00010  *
00011  * Licensed under The MIT License
00012  * Redistributions of files must retain the above copyright notice.
00013  *
00014  * @filesource
00015  * @copyright     Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
00016  * @link          http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00017  * @package       cake
00018  * @subpackage    cake.cake.libs
00019  * @since         CakePHP(tm) v 1.2.0
00020  * @version       $Revision: 8286 $
00021  * @modifiedby    $LastChangedBy: the_undefined $
00022  * @lastmodified  $Date: 2009-08-21 07:34:32 -0400 (Fri, 21 Aug 2009) $
00023  * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
00024  */
00025 App::import('Core', 'Validation');
00026 /**
00027  * Cake network socket connection class.
00028  *
00029  * Core base class for network communication.
00030  *
00031  * @package       cake
00032  * @subpackage    cake.cake.libs
00033  */
00034 class CakeSocket extends Object {
00035 /**
00036  * Object description
00037  *
00038  * @var string
00039  * @access public
00040  */
00041     var $description = 'Remote DataSource Network Socket Interface';
00042 /**
00043  * Base configuration settings for the socket connection
00044  *
00045  * @var array
00046  * @access protected
00047  */
00048     var $_baseConfig = array(
00049         'persistent'    => false,
00050         'host'          => 'localhost',
00051         'protocol'      => 'tcp',
00052         'port'          => 80,
00053         'timeout'       => 30
00054     );
00055 /**
00056  * Configuration settings for the socket connection
00057  *
00058  * @var array
00059  * @access public
00060  */
00061     var $config = array();
00062 /**
00063  * Reference to socket connection resource
00064  *
00065  * @var resource
00066  * @access public
00067  */
00068     var $connection = null;
00069 /**
00070  * This boolean contains the current state of the CakeSocket class
00071  *
00072  * @var boolean
00073  * @access public
00074  */
00075     var $connected = false;
00076 /**
00077  * This variable contains an array with the last error number (num) and string (str)
00078  *
00079  * @var array
00080  * @access public
00081  */
00082     var $lastError = array();
00083 /**
00084  * Constructor.
00085  *
00086  * @param array $config Socket configuration, which will be merged with the base configuration
00087  */
00088     function __construct($config = array()) {
00089         parent::__construct();
00090 
00091         $this->config = array_merge($this->_baseConfig, $config);
00092         if (!is_numeric($this->config['protocol'])) {
00093             $this->config['protocol'] = getprotobyname($this->config['protocol']);
00094         }
00095     }
00096 /**
00097  * Connect the socket to the given host and port.
00098  *
00099  * @return boolean Success
00100  * @access public
00101  */
00102     function connect() {
00103         if ($this->connection != null) {
00104             $this->disconnect();
00105         }
00106 
00107         $scheme = null;
00108         if (isset($this->config['request']) && $this->config['request']['uri']['scheme'] == 'https') {
00109             $scheme = 'ssl://';
00110         }
00111 
00112         if ($this->config['persistent'] == true) {
00113             $tmp = null;
00114             $this->connection = @pfsockopen($scheme.$this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']);
00115         } else {
00116             $this->connection = @fsockopen($scheme.$this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']);
00117         }
00118 
00119         if (!empty($errNum) || !empty($errStr)) {
00120             $this->setLastError($errNum, $errStr);
00121         }
00122 
00123         $this->connected = is_resource($this->connection);
00124         if ($this->connected) {
00125             stream_set_timeout($this->connection, $this->config['timeout']);
00126         }
00127         return $this->connected;
00128     }
00129 
00130 /**
00131  * Get the host name of the current connection.
00132  *
00133  * @return string Host name
00134  * @access public
00135  */
00136     function host() {
00137         if (Validation::ip($this->config['host'])) {
00138             return gethostbyaddr($this->config['host']);
00139         } else {
00140             return gethostbyaddr($this->address());
00141         }
00142     }
00143 /**
00144  * Get the IP address of the current connection.
00145  *
00146  * @return string IP address
00147  * @access public
00148  */
00149     function address() {
00150         if (Validation::ip($this->config['host'])) {
00151             return $this->config['host'];
00152         } else {
00153             return gethostbyname($this->config['host']);
00154         }
00155     }
00156 /**
00157  * Get all IP addresses associated with the current connection.
00158  *
00159  * @return array IP addresses
00160  * @access public
00161  */
00162     function addresses() {
00163         if (Validation::ip($this->config['host'])) {
00164             return array($this->config['host']);
00165         } else {
00166             return gethostbynamel($this->config['host']);
00167         }
00168     }
00169 /**
00170  * Get the last error as a string.
00171  *
00172  * @return string Last error
00173  * @access public
00174  */
00175     function lastError() {
00176         if (!empty($this->lastError)) {
00177             return $this->lastError['num'].': '.$this->lastError['str'];
00178         } else {
00179             return null;
00180         }
00181     }
00182 /**
00183  * Set the last error.
00184  *
00185  * @param integer $errNum Error code
00186  * @param string $errStr Error string
00187  * @access public
00188  */
00189     function setLastError($errNum, $errStr) {
00190         $this->lastError = array('num' => $errNum, 'str' => $errStr);
00191     }
00192 /**
00193  * Write data to the socket.
00194  *
00195  * @param string $data The data to write to the socket
00196  * @return boolean Success
00197  * @access public
00198  */
00199     function write($data) {
00200         if (!$this->connected) {
00201             if (!$this->connect()) {
00202                 return false;
00203             }
00204         }
00205 
00206         return fwrite($this->connection, $data, strlen($data));
00207     }
00208 
00209 /**
00210  * Read data from the socket. Returns false if no data is available or no connection could be
00211  * established.
00212  *
00213  * @param integer $length Optional buffer length to read; defaults to 1024
00214  * @return mixed Socket data
00215  * @access public
00216  */
00217     function read($length = 1024) {
00218         if (!$this->connected) {
00219             if (!$this->connect()) {
00220                 return false;
00221             }
00222         }
00223 
00224         if (!feof($this->connection)) {
00225             $buffer = fread($this->connection, $length);
00226             $info = stream_get_meta_data($this->connection);
00227             if ($info['timed_out']) {
00228                 $this->setLastError(E_WARNING, __('Connection timed out', true));
00229                 return false;
00230             }
00231             return $buffer;
00232         } else {
00233             return false;
00234         }
00235     }
00236 /**
00237  * Abort socket operation.
00238  *
00239  * @return boolean Success
00240  * @access public
00241  */
00242     function abort() {
00243     }
00244 /**
00245  * Disconnect the socket from the current connection.
00246  *
00247  * @return boolean Success
00248  * @access public
00249  */
00250     function disconnect() {
00251         if (!is_resource($this->connection)) {
00252             $this->connected = false;
00253             return true;
00254         }
00255         $this->connected = !fclose($this->connection);
00256 
00257         if (!$this->connected) {
00258             $this->connection = null;
00259         }
00260         return !$this->connected;
00261     }
00262 /**
00263  * Destructor, used to disconnect from current connection.
00264  *
00265  * @access private
00266  */
00267     function __destruct() {
00268         $this->disconnect();
00269     }
00270 /**
00271  * Resets the state of this Socket instance to it's initial state (before Object::__construct got executed)
00272  *
00273  * @return boolean True on success
00274  * @access public
00275  */
00276     function reset($state = null) {
00277         if (empty($state)) {
00278             static $initalState = array();
00279             if (empty($initalState)) {
00280                 $initalState = get_class_vars(__CLASS__);
00281             }
00282             $state = $initalState;
00283         }
00284 
00285         foreach ($state as $property => $value) {
00286             $this->{$property} = $value;
00287         }
00288         return true;
00289     }
00290 }
00291 ?>

Generated on Sun Nov 22 00:30:54 2009 for CakePHP 1.2.x.x (v1.2.4.8284) by doxygen 1.4.7