cake/libs/socket.php

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