Linux multi-monitor fullscreen support
[ryzomcore.git] / web / public_php / webtt / vendors / simpletest / socket.php
blob06e8ca62d0098ffe128d3d1854f15d5ba5157ab2
1 <?php
2 /**
3 * base include file for SimpleTest
4 * @package SimpleTest
5 * @subpackage MockObjects
6 * @version $Id: socket.php 1953 2009-09-20 01:26:25Z jsweat $
7 */
9 /**#@+
10 * include SimpleTest files
12 require_once(dirname(__FILE__) . '/compatibility.php');
13 /**#@-*/
15 /**
16 * Stashes an error for later. Useful for constructors
17 * until PHP gets exceptions.
18 * @package SimpleTest
19 * @subpackage WebTester
21 class SimpleStickyError {
22 private $error = 'Constructor not chained';
24 /**
25 * Sets the error to empty.
26 * @access public
28 function __construct() {
29 $this->clearError();
32 /**
33 * Test for an outstanding error.
34 * @return boolean True if there is an error.
35 * @access public
37 function isError() {
38 return ($this->error != '');
41 /**
42 * Accessor for an outstanding error.
43 * @return string Empty string if no error otherwise
44 * the error message.
45 * @access public
47 function getError() {
48 return $this->error;
51 /**
52 * Sets the internal error.
53 * @param string Error message to stash.
54 * @access protected
56 function setError($error) {
57 $this->error = $error;
60 /**
61 * Resets the error state to no error.
62 * @access protected
64 function clearError() {
65 $this->setError('');
69 /**
70 * @package SimpleTest
71 * @subpackage WebTester
73 class SimpleFileSocket extends SimpleStickyError {
74 private $handle;
75 private $is_open = false;
76 private $sent = '';
77 private $block_size;
79 /**
80 * Opens a socket for reading and writing.
81 * @param SimpleUrl $file Target URI to fetch.
82 * @param integer $block_size Size of chunk to read.
83 * @access public
85 function __construct($file, $block_size = 1024) {
86 parent::__construct();
87 if (! ($this->handle = $this->openFile($file, $error))) {
88 $file_string = $file->asString();
89 $this->setError("Cannot open [$file_string] with [$error]");
90 return;
92 $this->is_open = true;
93 $this->block_size = $block_size;
96 /**
97 * Writes some data to the socket and saves alocal copy.
98 * @param string $message String to send to socket.
99 * @return boolean True if successful.
100 * @access public
102 function write($message) {
103 return true;
107 * Reads data from the socket. The error suppresion
108 * is a workaround for PHP4 always throwing a warning
109 * with a secure socket.
110 * @return integer/boolean Incoming bytes. False
111 * on error.
112 * @access public
114 function read() {
115 $raw = @fread($this->handle, $this->block_size);
116 if ($raw === false) {
117 $this->setError('Cannot read from socket');
118 $this->close();
120 return $raw;
124 * Accessor for socket open state.
125 * @return boolean True if open.
126 * @access public
128 function isOpen() {
129 return $this->is_open;
133 * Closes the socket preventing further reads.
134 * Cannot be reopened once closed.
135 * @return boolean True if successful.
136 * @access public
138 function close() {
139 if (!$this->is_open) return false;
140 $this->is_open = false;
141 return fclose($this->handle);
145 * Accessor for content so far.
146 * @return string Bytes sent only.
147 * @access public
149 function getSent() {
150 return $this->sent;
154 * Actually opens the low level socket.
155 * @param SimpleUrl $file SimpleUrl file target.
156 * @param string $error Recipient of error message.
157 * @param integer $timeout Maximum time to wait for connection.
158 * @access protected
160 protected function openFile($file, &$error) {
161 return @fopen($file->asString(), 'r');
166 * Wrapper for TCP/IP socket.
167 * @package SimpleTest
168 * @subpackage WebTester
170 class SimpleSocket extends SimpleStickyError {
171 private $handle;
172 private $is_open = false;
173 private $sent = '';
174 private $lock_size;
177 * Opens a socket for reading and writing.
178 * @param string $host Hostname to send request to.
179 * @param integer $port Port on remote machine to open.
180 * @param integer $timeout Connection timeout in seconds.
181 * @param integer $block_size Size of chunk to read.
182 * @access public
184 function __construct($host, $port, $timeout, $block_size = 255) {
185 parent::__construct();
186 if (! ($this->handle = $this->openSocket($host, $port, $error_number, $error, $timeout))) {
187 $this->setError("Cannot open [$host:$port] with [$error] within [$timeout] seconds");
188 return;
190 $this->is_open = true;
191 $this->block_size = $block_size;
192 SimpleTestCompatibility::setTimeout($this->handle, $timeout);
196 * Writes some data to the socket and saves alocal copy.
197 * @param string $message String to send to socket.
198 * @return boolean True if successful.
199 * @access public
201 function write($message) {
202 if ($this->isError() || ! $this->isOpen()) {
203 return false;
205 $count = fwrite($this->handle, $message);
206 if (! $count) {
207 if ($count === false) {
208 $this->setError('Cannot write to socket');
209 $this->close();
211 return false;
213 fflush($this->handle);
214 $this->sent .= $message;
215 return true;
219 * Reads data from the socket. The error suppresion
220 * is a workaround for PHP4 always throwing a warning
221 * with a secure socket.
222 * @return integer/boolean Incoming bytes. False
223 * on error.
224 * @access public
226 function read() {
227 if ($this->isError() || ! $this->isOpen()) {
228 return false;
230 $raw = @fread($this->handle, $this->block_size);
231 if ($raw === false) {
232 $this->setError('Cannot read from socket');
233 $this->close();
235 return $raw;
239 * Accessor for socket open state.
240 * @return boolean True if open.
241 * @access public
243 function isOpen() {
244 return $this->is_open;
248 * Closes the socket preventing further reads.
249 * Cannot be reopened once closed.
250 * @return boolean True if successful.
251 * @access public
253 function close() {
254 $this->is_open = false;
255 return fclose($this->handle);
259 * Accessor for content so far.
260 * @return string Bytes sent only.
261 * @access public
263 function getSent() {
264 return $this->sent;
268 * Actually opens the low level socket.
269 * @param string $host Host to connect to.
270 * @param integer $port Port on host.
271 * @param integer $error_number Recipient of error code.
272 * @param string $error Recipoent of error message.
273 * @param integer $timeout Maximum time to wait for connection.
274 * @access protected
276 protected function openSocket($host, $port, &$error_number, &$error, $timeout) {
277 return @fsockopen($host, $port, $error_number, $error, $timeout);
282 * Wrapper for TCP/IP socket over TLS.
283 * @package SimpleTest
284 * @subpackage WebTester
286 class SimpleSecureSocket extends SimpleSocket {
289 * Opens a secure socket for reading and writing.
290 * @param string $host Hostname to send request to.
291 * @param integer $port Port on remote machine to open.
292 * @param integer $timeout Connection timeout in seconds.
293 * @access public
295 function __construct($host, $port, $timeout) {
296 parent::__construct($host, $port, $timeout);
300 * Actually opens the low level socket.
301 * @param string $host Host to connect to.
302 * @param integer $port Port on host.
303 * @param integer $error_number Recipient of error code.
304 * @param string $error Recipient of error message.
305 * @param integer $timeout Maximum time to wait for connection.
306 * @access protected
308 function openSocket($host, $port, &$error_number, &$error, $timeout) {
309 return parent::openSocket("tls://$host", $port, $error_number, $error, $timeout);