alecpl
2008-10-09 e70b3b24fc8ac7b54a820ae87ce8f4af4043125e
commit | author | age
627330 1 <?php
T 2 //
3 // +----------------------------------------------------------------------+
4 // | PHP Version 4                                                        |
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 1997-2003 The PHP Group                                |
7 // +----------------------------------------------------------------------+
8 // | This source file is subject to version 2.0 of the PHP license,       |
9 // | that is bundled with this package in the file LICENSE, and is        |
10 // | available at through the world-wide-web at                           |
11 // | http://www.php.net/license/2_02.txt.                                 |
12 // | If you did not receive a copy of the PHP license and are unable to   |
13 // | obtain it through the world-wide-web, please send a note to          |
14 // | license@php.net so we can mail you a copy immediately.               |
15 // +----------------------------------------------------------------------+
16 // | Authors: Stig Bakken <ssb@php.net>                                   |
17 // |          Chuck Hagenbuch <chuck@horde.org>                           |
18 // +----------------------------------------------------------------------+
19 //
20 // $Id$
21
22 require_once 'PEAR.php';
23
a5b598 24 define('NET_SOCKET_READ',  1);
T 25 define('NET_SOCKET_WRITE', 2);
26 define('NET_SOCKET_ERROR', 4);
27
627330 28 /**
a5b598 29  * Generalized Socket class.
627330 30  *
a5b598 31  * @version 1.1
627330 32  * @author Stig Bakken <ssb@php.net>
T 33  * @author Chuck Hagenbuch <chuck@horde.org>
34  */
35 class Net_Socket extends PEAR {
36
a5b598 37     /**
T 38      * Socket file pointer.
39      * @var resource $fp
40      */
627330 41     var $fp = null;
T 42
a5b598 43     /**
T 44      * Whether the socket is blocking. Defaults to true.
45      * @var boolean $blocking
46      */
627330 47     var $blocking = true;
T 48
a5b598 49     /**
T 50      * Whether the socket is persistent. Defaults to false.
51      * @var boolean $persistent
52      */
627330 53     var $persistent = false;
T 54
a5b598 55     /**
T 56      * The IP address to connect to.
57      * @var string $addr
58      */
627330 59     var $addr = '';
T 60
a5b598 61     /**
T 62      * The port number to connect to.
63      * @var integer $port
64      */
627330 65     var $port = 0;
T 66
a5b598 67     /**
T 68      * Number of seconds to wait on socket connections before assuming
69      * there's no more data. Defaults to no timeout.
70      * @var integer $timeout
71      */
627330 72     var $timeout = false;
T 73
74     /**
a5b598 75      * Number of bytes to read at a time in readLine() and
T 76      * readAll(). Defaults to 2048.
77      * @var integer $lineLength
627330 78      */
a5b598 79     var $lineLength = 2048;
627330 80
T 81     /**
82      * Connect to the specified port. If called when the socket is
83      * already connected, it disconnects and connects again.
84      *
a5b598 85      * @param string  $addr        IP address or host name.
T 86      * @param integer $port        TCP port number.
87      * @param boolean $persistent  (optional) Whether the connection is
88      *                             persistent (kept open between requests
89      *                             by the web server).
90      * @param integer $timeout     (optional) How long to wait for data.
91      * @param array   $options     See options for stream_context_create.
92      *
627330 93      * @access public
a5b598 94      *
T 95      * @return boolean | PEAR_Error  True on success or a PEAR_Error on failure.
627330 96      */
a5b598 97     function connect($addr, $port = 0, $persistent = null, $timeout = null, $options = null)
627330 98     {
T 99         if (is_resource($this->fp)) {
100             @fclose($this->fp);
101             $this->fp = null;
102         }
103
a5b598 104         if (!$addr) {
T 105             return $this->raiseError('$addr cannot be empty');
106         } elseif (strspn($addr, '.0123456789') == strlen($addr) ||
107                   strstr($addr, '/') !== false) {
627330 108             $this->addr = $addr;
T 109         } else {
a5b598 110             $this->addr = @gethostbyname($addr);
627330 111         }
a5b598 112
627330 113         $this->port = $port % 65536;
a5b598 114
627330 115         if ($persistent !== null) {
T 116             $this->persistent = $persistent;
117         }
a5b598 118
627330 119         if ($timeout !== null) {
T 120             $this->timeout = $timeout;
121         }
a5b598 122
627330 123         $openfunc = $this->persistent ? 'pfsockopen' : 'fsockopen';
T 124         $errno = 0;
125         $errstr = '';
126         if ($options && function_exists('stream_context_create')) {
127             if ($this->timeout) {
128                 $timeout = $this->timeout;
129             } else {
130                 $timeout = 0;
131             }
132             $context = stream_context_create($options);
a5b598 133             $fp = @$openfunc($this->addr, $this->port, $errno, $errstr, $timeout, $context);
627330 134         } else {
T 135             if ($this->timeout) {
136                 $fp = @$openfunc($this->addr, $this->port, $errno, $errstr, $this->timeout);
137             } else {
138                 $fp = @$openfunc($this->addr, $this->port, $errno, $errstr);
139             }
140         }
141
142         if (!$fp) {
143             return $this->raiseError($errstr, $errno);
144         }
145
146         $this->fp = $fp;
147
148         return $this->setBlocking($this->blocking);
149     }
150
151     /**
152      * Disconnects from the peer, closes the socket.
153      *
154      * @access public
155      * @return mixed true on success or an error object otherwise
156      */
157     function disconnect()
158     {
a5b598 159         if (!is_resource($this->fp)) {
T 160             return $this->raiseError('not connected');
627330 161         }
T 162
a5b598 163         @fclose($this->fp);
T 164         $this->fp = null;
165         return true;
166     }
167
627330 168     /**
T 169      * Find out if the socket is in blocking mode.
170      *
171      * @access public
a5b598 172      * @return boolean  The current blocking mode.
627330 173      */
T 174     function isBlocking()
175     {
176         return $this->blocking;
177     }
178
179     /**
180      * Sets whether the socket connection should be blocking or
181      * not. A read call to a non-blocking socket will return immediately
182      * if there is no data available, whereas it will block until there
183      * is data for blocking sockets.
184      *
a5b598 185      * @param boolean $mode  True for blocking sockets, false for nonblocking.
627330 186      * @access public
T 187      * @return mixed true on success or an error object otherwise
188      */
189     function setBlocking($mode)
190     {
a5b598 191         if (!is_resource($this->fp)) {
T 192             return $this->raiseError('not connected');
627330 193         }
T 194
a5b598 195         $this->blocking = $mode;
T 196         socket_set_blocking($this->fp, $this->blocking);
197         return true;
198     }
199
627330 200     /**
T 201      * Sets the timeout value on socket descriptor,
202      * expressed in the sum of seconds and microseconds
203      *
a5b598 204      * @param integer $seconds  Seconds.
T 205      * @param integer $microseconds  Microseconds.
627330 206      * @access public
T 207      * @return mixed true on success or an error object otherwise
208      */
209     function setTimeout($seconds, $microseconds)
210     {
a5b598 211         if (!is_resource($this->fp)) {
T 212             return $this->raiseError('not connected');
213         }
214
215         return socket_set_timeout($this->fp, $seconds, $microseconds);
216     }
217
218     /**
219      * Sets the file buffering size on the stream.
220      * See php's stream_set_write_buffer for more information.
221      *
222      * @param integer $size     Write buffer size.
223      * @access public
224      * @return mixed on success or an PEAR_Error object otherwise
225      */
226     function setWriteBuffer($size)
227     {
228         if (!is_resource($this->fp)) {
229             return $this->raiseError('not connected');
230         }
231
232         $returned = stream_set_write_buffer($this->fp, $code);
233         if ($returned == 0) {
627330 234             return true;
T 235         }
a5b598 236         return $this->raiseError('Cannot set write buffer.');
627330 237     }
T 238
239     /**
240      * Returns information about an existing socket resource.
241      * Currently returns four entries in the result array:
242      *
243      * <p>
244      * timed_out (bool) - The socket timed out waiting for data<br>
245      * blocked (bool) - The socket was blocked<br>
246      * eof (bool) - Indicates EOF event<br>
247      * unread_bytes (int) - Number of bytes left in the socket buffer<br>
248      * </p>
249      *
250      * @access public
251      * @return mixed Array containing information about existing socket resource or an error object otherwise
252      */
253     function getStatus()
254     {
a5b598 255         if (!is_resource($this->fp)) {
T 256             return $this->raiseError('not connected');
627330 257         }
T 258
a5b598 259         return socket_get_status($this->fp);
T 260     }
261
627330 262     /**
T 263      * Get a specified line of data
264      *
265      * @access public
266      * @return $size bytes of data from the socket, or a PEAR_Error if
267      *         not connected.
268      */
269     function gets($size)
270     {
a5b598 271         if (!is_resource($this->fp)) {
T 272             return $this->raiseError('not connected');
627330 273         }
T 274
a5b598 275         return @fgets($this->fp, $size);
T 276     }
277
627330 278     /**
T 279      * Read a specified amount of data. This is guaranteed to return,
280      * and has the added benefit of getting everything in one fread()
281      * chunk; if you know the size of the data you're getting
282      * beforehand, this is definitely the way to go.
283      *
a5b598 284      * @param integer $size  The number of bytes to read from the socket.
627330 285      * @access public
T 286      * @return $size bytes of data from the socket, or a PEAR_Error if
287      *         not connected.
288      */
289     function read($size)
290     {
a5b598 291         if (!is_resource($this->fp)) {
T 292             return $this->raiseError('not connected');
627330 293         }
T 294
a5b598 295         return @fread($this->fp, $size);
T 296     }
297
627330 298     /**
T 299      * Write a specified amount of data.
a5b598 300      *
T 301      * @param string  $data       Data to write.
302      * @param integer $blocksize  Amount of data to write at once.
303      *                            NULL means all at once.
627330 304      *
T 305      * @access public
306      * @return mixed true on success or an error object otherwise
307      */
a5b598 308     function write($data, $blocksize = null)
627330 309     {
a5b598 310         if (!is_resource($this->fp)) {
T 311             return $this->raiseError('not connected');
627330 312         }
T 313
a5b598 314         if (is_null($blocksize) && !OS_WINDOWS) {
T 315             return fwrite($this->fp, $data);
316         } else {
317             if (is_null($blocksize)) {
318                 $blocksize = 1024;
319             }
320
321             $pos = 0;
322             $size = strlen($data);
323             while ($pos < $size) {
324                 $written = @fwrite($this->fp, substr($data, $pos, $blocksize));
325                 if ($written === false) {
326                     return false;
327                 }
328                 $pos += $written;
329             }
330
331             return $pos;
332         }
333     }
334
627330 335     /**
T 336      * Write a line of data to the socket, followed by a trailing "\r\n".
337      *
338      * @access public
339      * @return mixed fputs result, or an error
340      */
a5b598 341     function writeLine($data)
627330 342     {
a5b598 343         if (!is_resource($this->fp)) {
T 344             return $this->raiseError('not connected');
627330 345         }
T 346
a5b598 347         return fwrite($this->fp, $data . "\r\n");
T 348     }
349
627330 350     /**
a5b598 351      * Tests for end-of-file on a socket descriptor.
T 352      *
353      * Also returns true if the socket is disconnected.
627330 354      *
T 355      * @access public
356      * @return bool
357      */
358     function eof()
359     {
a5b598 360         return (!is_resource($this->fp) || feof($this->fp));
627330 361     }
T 362
363     /**
364      * Reads a byte of data
365      *
366      * @access public
367      * @return 1 byte of data from the socket, or a PEAR_Error if
368      *         not connected.
369      */
370     function readByte()
371     {
a5b598 372         if (!is_resource($this->fp)) {
T 373             return $this->raiseError('not connected');
627330 374         }
T 375
a5b598 376         return ord(@fread($this->fp, 1));
T 377     }
378
627330 379     /**
T 380      * Reads a word of data
381      *
382      * @access public
383      * @return 1 word of data from the socket, or a PEAR_Error if
384      *         not connected.
385      */
386     function readWord()
387     {
a5b598 388         if (!is_resource($this->fp)) {
T 389             return $this->raiseError('not connected');
627330 390         }
T 391
a5b598 392         $buf = @fread($this->fp, 2);
T 393         return (ord($buf[0]) + (ord($buf[1]) << 8));
394     }
395
627330 396     /**
T 397      * Reads an int of data
398      *
399      * @access public
a5b598 400      * @return integer  1 int of data from the socket, or a PEAR_Error if
T 401      *                  not connected.
627330 402      */
T 403     function readInt()
404     {
a5b598 405         if (!is_resource($this->fp)) {
T 406             return $this->raiseError('not connected');
627330 407         }
T 408
a5b598 409         $buf = @fread($this->fp, 4);
T 410         return (ord($buf[0]) + (ord($buf[1]) << 8) +
411                 (ord($buf[2]) << 16) + (ord($buf[3]) << 24));
412     }
413
627330 414     /**
a5b598 415      * Reads a zero-terminated string of data
627330 416      *
T 417      * @access public
418      * @return string, or a PEAR_Error if
419      *         not connected.
420      */
421     function readString()
422     {
a5b598 423         if (!is_resource($this->fp)) {
T 424             return $this->raiseError('not connected');
627330 425         }
T 426
a5b598 427         $string = '';
T 428         while (($char = @fread($this->fp, 1)) != "\x00")  {
429             $string .= $char;
430         }
431         return $string;
432     }
433
627330 434     /**
T 435      * Reads an IP Address and returns it in a dot formated string
436      *
437      * @access public
438      * @return Dot formated string, or a PEAR_Error if
439      *         not connected.
440      */
441     function readIPAddress()
442     {
a5b598 443         if (!is_resource($this->fp)) {
T 444             return $this->raiseError('not connected');
627330 445         }
T 446
a5b598 447         $buf = @fread($this->fp, 4);
T 448         return sprintf("%s.%s.%s.%s", ord($buf[0]), ord($buf[1]),
449                        ord($buf[2]), ord($buf[3]));
450     }
451
627330 452     /**
T 453      * Read until either the end of the socket or a newline, whichever
454      * comes first. Strips the trailing newline from the returned data.
455      *
456      * @access public
457      * @return All available data up to a newline, without that
458      *         newline, or until the end of the socket, or a PEAR_Error if
459      *         not connected.
460      */
461     function readLine()
462     {
a5b598 463         if (!is_resource($this->fp)) {
T 464             return $this->raiseError('not connected');
627330 465         }
T 466
a5b598 467         $line = '';
T 468         $timeout = time() + $this->timeout;
469         while (!feof($this->fp) && (!$this->timeout || time() < $timeout)) {
470             $line .= @fgets($this->fp, $this->lineLength);
471             if (substr($line, -1) == "\n") {
472                 return rtrim($line, "\r\n");
473             }
474         }
475         return $line;
476     }
477
627330 478     /**
a5b598 479      * Read until the socket closes, or until there is no more data in
T 480      * the inner PHP buffer. If the inner buffer is empty, in blocking
481      * mode we wait for at least 1 byte of data. Therefore, in
482      * blocking mode, if there is no data at all to be read, this
483      * function will never exit (unless the socket is closed on the
484      * remote end).
627330 485      *
T 486      * @access public
a5b598 487      *
T 488      * @return string  All data until the socket closes, or a PEAR_Error if
489      *                 not connected.
627330 490      */
T 491     function readAll()
492     {
a5b598 493         if (!is_resource($this->fp)) {
T 494             return $this->raiseError('not connected');
627330 495         }
a5b598 496
T 497         $data = '';
498         while (!feof($this->fp)) {
499             $data .= @fread($this->fp, $this->lineLength);
500         }
501         return $data;
627330 502     }
a5b598 503
T 504     /**
505      * Runs the equivalent of the select() system call on the socket
506      * with a timeout specified by tv_sec and tv_usec.
507      *
508      * @param integer $state    Which of read/write/error to check for.
509      * @param integer $tv_sec   Number of seconds for timeout.
510      * @param integer $tv_usec  Number of microseconds for timeout.
511      *
512      * @access public
513      * @return False if select fails, integer describing which of read/write/error
514      *         are ready, or PEAR_Error if not connected.
515      */
516     function select($state, $tv_sec, $tv_usec = 0)
517     {
518         if (!is_resource($this->fp)) {
519             return $this->raiseError('not connected');
520         }
521
522         $read = null;
523         $write = null;
524         $except = null;
525         if ($state & NET_SOCKET_READ) {
526             $read[] = $this->fp;
527         }
528         if ($state & NET_SOCKET_WRITE) {
529             $write[] = $this->fp;
530         }
531         if ($state & NET_SOCKET_ERROR) {
532             $except[] = $this->fp;
533         }
534         if (false === ($sr = stream_select($read, $write, $except, $tv_sec, $tv_usec))) {
535             return false;
536         }
537
538         $result = 0;
539         if (count($read)) {
540             $result |= NET_SOCKET_READ;
541         }
542         if (count($write)) {
543             $result |= NET_SOCKET_WRITE;
544         }
545         if (count($except)) {
546             $result |= NET_SOCKET_ERROR;
547         }
548         return $result;
549     }
550
551     /**
552      * Turns encryption on/off on a connected socket.
553      *
554      * @param bool    $enabled  Set this parameter to true to enable encryption
555      *                          and false to disable encryption.
556      * @param integer $type     Type of encryption. See
557      *                          http://se.php.net/manual/en/function.stream-socket-enable-crypto.php for values.
558      *
559      * @access public
560      * @return false on error, true on success and 0 if there isn't enough data and the
561      *         user should try again (non-blocking sockets only). A PEAR_Error object
562      *         is returned if the socket is not connected
563      */
564     function enableCrypto($enabled, $type)
565     {
566         if (version_compare(phpversion(), "5.1.0", ">=")) {
567             if (!is_resource($this->fp)) {
568                 return $this->raiseError('not connected');
569             }
570             return @stream_socket_enable_crypto($this->fp, $enabled, $type);
571         } else {
572             return $this->raiseError('Net_Socket::enableCrypto() requires php version >= 5.1.0');
573         }
574     }
627330 575
T 576 }