Subversion-Projekte lars-tiefland.cakephp

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/* SVN FILE: $Id: socket.php 7945 2008-12-19 02:16:01Z gwoo $ */
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: 7945 $
21
 * @modifiedby    $LastChangedBy: gwoo $
22
 * @lastmodified  $Date: 2008-12-18 18:16:01 -0800 (Thu, 18 Dec 2008) $
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
		return $this->connected = is_resource($this->connection);
124
	}
125
 
126
/**
127
 * Get the host name of the current connection.
128
 *
129
 * @return string Host name
130
 * @access public
131
 */
132
	function host() {
133
		if (Validation::ip($this->config['host'])) {
134
			return gethostbyaddr($this->config['host']);
135
		} else {
136
			return gethostbyaddr($this->address());
137
		}
138
	}
139
/**
140
 * Get the IP address of the current connection.
141
 *
142
 * @return string IP address
143
 * @access public
144
 */
145
	function address() {
146
		if (Validation::ip($this->config['host'])) {
147
			return $this->config['host'];
148
		} else {
149
			return gethostbyname($this->config['host']);
150
		}
151
	}
152
/**
153
 * Get all IP addresses associated with the current connection.
154
 *
155
 * @return array IP addresses
156
 * @access public
157
 */
158
	function addresses() {
159
		if (Validation::ip($this->config['host'])) {
160
			return array($this->config['host']);
161
		} else {
162
			return gethostbynamel($this->config['host']);
163
		}
164
	}
165
/**
166
 * Get the last error as a string.
167
 *
168
 * @return string Last error
169
 * @access public
170
 */
171
	function lastError() {
172
		if (!empty($this->lastError)) {
173
			return $this->lastError['num'].': '.$this->lastError['str'];
174
		} else {
175
			return null;
176
		}
177
	}
178
/**
179
 * Set the last error.
180
 *
181
 * @param integer $errNum Error code
182
 * @param string $errStr Error string
183
 * @access public
184
 */
185
	function setLastError($errNum, $errStr) {
186
		$this->lastError = array('num' => $errNum, 'str' => $errStr);
187
	}
188
/**
189
 * Write data to the socket.
190
 *
191
 * @param string $data The data to write to the socket
192
 * @return boolean Success
193
 * @access public
194
 */
195
	function write($data) {
196
		if (!$this->connected) {
197
			if (!$this->connect()) {
198
				return false;
199
			}
200
		}
201
 
202
		return fwrite($this->connection, $data, strlen($data));
203
	}
204
 
205
/**
206
 * Read data from the socket. Returns false if no data is available or no connection could be
207
 * established.
208
 *
209
 * @param integer $length Optional buffer length to read; defaults to 1024
210
 * @return mixed Socket data
211
 * @access public
212
 */
213
	function read($length = 1024) {
214
		if (!$this->connected) {
215
			if (!$this->connect()) {
216
				return false;
217
			}
218
		}
219
 
220
		if (!feof($this->connection)) {
221
			return fread($this->connection, $length);
222
		} else {
223
			return false;
224
		}
225
	}
226
/**
227
 * Abort socket operation.
228
 *
229
 * @return boolean Success
230
 * @access public
231
 */
232
	function abort() {
233
	}
234
/**
235
 * Disconnect the socket from the current connection.
236
 *
237
 * @return boolean Success
238
 * @access public
239
 */
240
	function disconnect() {
241
		if (!is_resource($this->connection)) {
242
			$this->connected = false;
243
			return true;
244
		}
245
		$this->connected = !fclose($this->connection);
246
 
247
		if (!$this->connected) {
248
			$this->connection = null;
249
		}
250
		return !$this->connected;
251
	}
252
/**
253
 * Destructor, used to disconnect from current connection.
254
 *
255
 * @access private
256
 */
257
	function __destruct() {
258
		$this->disconnect();
259
	}
260
/**
261
 * Resets the state of this Socket instance to it's initial state (before Object::__construct got executed)
262
 *
263
 * @return boolean True on success
264
 * @access public
265
 */
266
	function reset($state = null) {
267
		if (empty($state)) {
268
			static $initalState = array();
269
			if (empty($initalState)) {
270
				$initalState = get_class_vars(__CLASS__);
271
			}
272
			$state = $initalState;
273
		}
274
 
275
		foreach ($state as $property => $value) {
276
			$this->{$property} = $value;
277
		}
278
		return true;
279
	}
280
}
281
?>