7  * Copyright (c) 1997-2013 The PHP Group
 
   9  * This source file is subject to version 2.0 of the PHP license,
 
  10  * that is bundled with this package in the file LICENSE, and is
 
  11  * available at through the world-wide-web at
 
  12  * http://www.php.net/license/2_02.txt.
 
  13  * If you did not receive a copy of the PHP license and are unable to
 
  14  * obtain it through the world-wide-web, please send a note to
 
  15  * license@php.net so we can mail you a copy immediately.
 
  17  * Authors: Stig Bakken <ssb@php.net>
 
  18  *          Chuck Hagenbuch <chuck@horde.org>
 
  22  * @author    Stig Bakken <ssb@php.net>
 
  23  * @author    Chuck Hagenbuch <chuck@horde.org>
 
  24  * @copyright 1997-2003 The PHP Group
 
  25  * @license   http://www.php.net/license/2_02.txt PHP 2.02
 
  26  * @link      http://pear.php.net/packages/Net_Socket
 
  29 require_once 'PEAR.php';
 
  31 define('NET_SOCKET_READ', 1);
 
  32 define('NET_SOCKET_WRITE', 2);
 
  33 define('NET_SOCKET_ERROR', 4);
 
  36  * Generalized Socket class.
 
  40  * @author    Stig Bakken <ssb@php.net>
 
  41  * @author    Chuck Hagenbuch <chuck@horde.org>
 
  42  * @copyright 1997-2003 The PHP Group
 
  43  * @license   http://www.php.net/license/2_02.txt PHP 2.02
 
  44  * @link      http://pear.php.net/packages/Net_Socket
 
  46 class Net_Socket extends PEAR
 
  49      * Socket file pointer.
 
  55      * Whether the socket is blocking. Defaults to true.
 
  56      * @var boolean $blocking
 
  61      * Whether the socket is persistent. Defaults to false.
 
  62      * @var boolean $persistent
 
  64     var $persistent = false;
 
  67      * The IP address to connect to.
 
  73      * The port number to connect to.
 
  79      * Number of seconds to wait on socket operations before assuming
 
  80      * there's no more data. Defaults to no timeout.
 
  81      * @var integer|float $timeout
 
  86      * Number of bytes to read at a time in readLine() and
 
  87      * readAll(). Defaults to 2048.
 
  88      * @var integer $lineLength
 
  90     var $lineLength = 2048;
 
  93      * The string to use as a newline terminator. Usually "\r\n" or "\n".
 
  94      * @var string $newline
 
  96     var $newline = "\r\n";
 
  99      * Connect to the specified port. If called when the socket is
 
 100      * already connected, it disconnects and connects again.
 
 102      * @param string  $addr       IP address or host name (may be with protocol prefix).
 
 103      * @param integer $port       TCP port number.
 
 104      * @param boolean $persistent (optional) Whether the connection is
 
 105      *                            persistent (kept open between requests
 
 106      *                            by the web server).
 
 107      * @param integer $timeout    (optional) Connection socket timeout.
 
 108      * @param array   $options    See options for stream_context_create.
 
 112      * @return boolean|PEAR_Error  True on success or a PEAR_Error on failure.
 
 114     function connect($addr, $port = 0, $persistent = null,
 
 115                      $timeout = null, $options = null)
 
 117         if (is_resource($this->fp)) {
 
 123             return $this->raiseError('$addr cannot be empty');
 
 124         } else if (strspn($addr, ':.0123456789') == strlen($addr)) {
 
 125             $this->addr = strpos($addr, ':') !== false ? '['.$addr.']' : $addr;
 
 130         $this->port = $port % 65536;
 
 132         if ($persistent !== null) {
 
 133             $this->persistent = $persistent;
 
 136         $openfunc = $this->persistent ? 'pfsockopen' : 'fsockopen';
 
 140         $old_track_errors = @ini_set('track_errors', 1);
 
 143             $timeout = @ini_get('default_socket_timeout');
 
 146         if ($options && function_exists('stream_context_create')) {
 
 147             $context = stream_context_create($options);
 
 149             // Since PHP 5 fsockopen doesn't allow context specification
 
 150             if (function_exists('stream_socket_client')) {
 
 151                 $flags = STREAM_CLIENT_CONNECT;
 
 153                 if ($this->persistent) {
 
 154                     $flags = STREAM_CLIENT_PERSISTENT;
 
 157                 $addr = $this->addr . ':' . $this->port;
 
 158                 $fp   = stream_socket_client($addr, $errno, $errstr,
 
 159                                              $timeout, $flags, $context);
 
 161                 $fp = @$openfunc($this->addr, $this->port, $errno,
 
 162                                  $errstr, $timeout, $context);
 
 165             $fp = @$openfunc($this->addr, $this->port, $errno, $errstr, $timeout);
 
 169             if ($errno == 0 && !strlen($errstr) && isset($php_errormsg)) {
 
 170                 $errstr = $php_errormsg;
 
 172             @ini_set('track_errors', $old_track_errors);
 
 173             return $this->raiseError($errstr, $errno);
 
 176         @ini_set('track_errors', $old_track_errors);
 
 179         return $this->setBlocking($this->blocking);
 
 183      * Disconnects from the peer, closes the socket.
 
 186      * @return mixed true on success or a PEAR_Error instance otherwise
 
 188     function disconnect()
 
 190         if (!is_resource($this->fp)) {
 
 191             return $this->raiseError('not connected');
 
 200      * Set the newline character/sequence to use.
 
 202      * @param string $newline  Newline character(s)
 
 203      * @return boolean True
 
 205     function setNewline($newline)
 
 207         $this->newline = $newline;
 
 212      * Find out if the socket is in blocking mode.
 
 215      * @return boolean  The current blocking mode.
 
 217     function isBlocking()
 
 219         return $this->blocking;
 
 223      * Sets whether the socket connection should be blocking or
 
 224      * not. A read call to a non-blocking socket will return immediately
 
 225      * if there is no data available, whereas it will block until there
 
 226      * is data for blocking sockets.
 
 228      * @param boolean $mode True for blocking sockets, false for nonblocking.
 
 231      * @return mixed true on success or a PEAR_Error instance otherwise
 
 233     function setBlocking($mode)
 
 235         if (!is_resource($this->fp)) {
 
 236             return $this->raiseError('not connected');
 
 239         $this->blocking = $mode;
 
 240         stream_set_blocking($this->fp, (int)$this->blocking);
 
 245      * Sets the timeout value on socket descriptor,
 
 246      * expressed in the sum of seconds and microseconds
 
 248      * @param integer $seconds      Seconds.
 
 249      * @param integer $microseconds Microseconds, optional.
 
 252      * @return mixed True on success or false on failure or
 
 253      *               a PEAR_Error instance when not connected
 
 255     function setTimeout($seconds = null, $microseconds = null)
 
 257         if (!is_resource($this->fp)) {
 
 258             return $this->raiseError('not connected');
 
 261         if ($seconds === null && $microseconds === null) {
 
 262             $seconds      = (int) $this->timeout;
 
 263             $microseconds = (int) (($this->timeout - $seconds) * 1000000);
 
 265             $this->timeout = $seconds + $microseconds/1000000;
 
 268         if ($this->timeout > 0) {
 
 269             return stream_set_timeout($this->fp, (int) $seconds, (int) $microseconds);
 
 277      * Sets the file buffering size on the stream.
 
 278      * See php's stream_set_write_buffer for more information.
 
 280      * @param integer $size Write buffer size.
 
 283      * @return mixed on success or an PEAR_Error object otherwise
 
 285     function setWriteBuffer($size)
 
 287         if (!is_resource($this->fp)) {
 
 288             return $this->raiseError('not connected');
 
 291         $returned = stream_set_write_buffer($this->fp, $size);
 
 292         if ($returned == 0) {
 
 295         return $this->raiseError('Cannot set write buffer.');
 
 299      * Returns information about an existing socket resource.
 
 300      * Currently returns four entries in the result array:
 
 303      * timed_out (bool) - The socket timed out waiting for data<br>
 
 304      * blocked (bool) - The socket was blocked<br>
 
 305      * eof (bool) - Indicates EOF event<br>
 
 306      * unread_bytes (int) - Number of bytes left in the socket buffer<br>
 
 310      * @return mixed Array containing information about existing socket
 
 311      *               resource or a PEAR_Error instance otherwise
 
 315         if (!is_resource($this->fp)) {
 
 316             return $this->raiseError('not connected');
 
 319         return stream_get_meta_data($this->fp);
 
 323      * Get a specified line of data
 
 325      * @param int $size Reading ends when size - 1 bytes have been read,
 
 326      *                  or a newline or an EOF (whichever comes first).
 
 327      *                  If no size is specified, it will keep reading from
 
 328      *                  the stream until it reaches the end of the line.
 
 331      * @return mixed $size bytes of data from the socket, or a PEAR_Error if
 
 332      *         not connected. If an error occurs, FALSE is returned.
 
 334     function gets($size = null)
 
 336         if (!is_resource($this->fp)) {
 
 337             return $this->raiseError('not connected');
 
 340         if (is_null($size)) {
 
 341             return @fgets($this->fp);
 
 343             return @fgets($this->fp, $size);
 
 348      * Read a specified amount of data. This is guaranteed to return,
 
 349      * and has the added benefit of getting everything in one fread()
 
 350      * chunk; if you know the size of the data you're getting
 
 351      * beforehand, this is definitely the way to go.
 
 353      * @param integer $size The number of bytes to read from the socket.
 
 356      * @return $size bytes of data from the socket, or a PEAR_Error if
 
 361         if (!is_resource($this->fp)) {
 
 362             return $this->raiseError('not connected');
 
 365         return @fread($this->fp, $size);
 
 369      * Write a specified amount of data.
 
 371      * @param string  $data      Data to write.
 
 372      * @param integer $blocksize Amount of data to write at once.
 
 373      *                           NULL means all at once.
 
 376      * @return mixed If the socket is not connected, returns an instance of
 
 378      *               If the write succeeds, returns the number of bytes written.
 
 379      *               If the write fails, returns false.
 
 380      *               If the socket times out, returns an instance of PEAR_Error.
 
 382     function write($data, $blocksize = null)
 
 384         if (!is_resource($this->fp)) {
 
 385             return $this->raiseError('not connected');
 
 388         if (is_null($blocksize) && !OS_WINDOWS) {
 
 389             $written = @fwrite($this->fp, $data);
 
 391             // Check for timeout or lost connection
 
 393                 $meta_data = $this->getStatus();
 
 395                 if (!is_array($meta_data)) {
 
 396                     return $meta_data; // PEAR_Error
 
 399                 if (!empty($meta_data['timed_out'])) {
 
 400                     return $this->raiseError('timed out');
 
 406             if (is_null($blocksize)) {
 
 411             $size = strlen($data);
 
 412             while ($pos < $size) {
 
 413                 $written = @fwrite($this->fp, substr($data, $pos, $blocksize));
 
 415                 // Check for timeout or lost connection
 
 417                     $meta_data = $this->getStatus();
 
 419                     if (!is_array($meta_data)) {
 
 420                         return $meta_data; // PEAR_Error
 
 423                     if (!empty($meta_data['timed_out'])) {
 
 424                         return $this->raiseError('timed out');
 
 438      * Write a line of data to the socket, followed by a trailing newline.
 
 440      * @param string $data Data to write
 
 443      * @return mixed fwrite() result, or PEAR_Error when not connected
 
 445     function writeLine($data)
 
 447         if (!is_resource($this->fp)) {
 
 448             return $this->raiseError('not connected');
 
 451         return fwrite($this->fp, $data . $this->newline);
 
 455      * Tests for end-of-file on a socket descriptor.
 
 457      * Also returns true if the socket is disconnected.
 
 464         return (!is_resource($this->fp) || feof($this->fp));
 
 468      * Reads a byte of data
 
 471      * @return 1 byte of data from the socket, or a PEAR_Error if
 
 476         if (!is_resource($this->fp)) {
 
 477             return $this->raiseError('not connected');
 
 480         return ord(@fread($this->fp, 1));
 
 484      * Reads a word of data
 
 487      * @return 1 word of data from the socket, or a PEAR_Error if
 
 492         if (!is_resource($this->fp)) {
 
 493             return $this->raiseError('not connected');
 
 496         $buf = @fread($this->fp, 2);
 
 497         return (ord($buf[0]) + (ord($buf[1]) << 8));
 
 501      * Reads an int of data
 
 504      * @return integer  1 int of data from the socket, or a PEAR_Error if
 
 509         if (!is_resource($this->fp)) {
 
 510             return $this->raiseError('not connected');
 
 513         $buf = @fread($this->fp, 4);
 
 514         return (ord($buf[0]) + (ord($buf[1]) << 8) +
 
 515                 (ord($buf[2]) << 16) + (ord($buf[3]) << 24));
 
 519      * Reads a zero-terminated string of data
 
 522      * @return string, or a PEAR_Error if
 
 525     function readString()
 
 527         if (!is_resource($this->fp)) {
 
 528             return $this->raiseError('not connected');
 
 532         while (($char = @fread($this->fp, 1)) != "\x00") {
 
 539      * Reads an IP Address and returns it in a dot formatted string
 
 542      * @return Dot formatted string, or a PEAR_Error if
 
 545     function readIPAddress()
 
 547         if (!is_resource($this->fp)) {
 
 548             return $this->raiseError('not connected');
 
 551         $buf = @fread($this->fp, 4);
 
 552         return sprintf('%d.%d.%d.%d', ord($buf[0]), ord($buf[1]),
 
 553                        ord($buf[2]), ord($buf[3]));
 
 557      * Read until either the end of the socket or a newline, whichever
 
 558      * comes first. Strips the trailing newline from the returned data.
 
 561      * @return All available data up to a newline, without that
 
 562      *         newline, or until the end of the socket, or a PEAR_Error if
 
 567         if (!is_resource($this->fp)) {
 
 568             return $this->raiseError('not connected');
 
 573         $timeout = time() + $this->timeout;
 
 575         while (!feof($this->fp) && (!$this->timeout || time() < $timeout)) {
 
 576             $line .= @fgets($this->fp, $this->lineLength);
 
 577             if (substr($line, -1) == "\n") {
 
 578                 return rtrim($line, $this->newline);
 
 585      * Read until the socket closes, or until there is no more data in
 
 586      * the inner PHP buffer. If the inner buffer is empty, in blocking
 
 587      * mode we wait for at least 1 byte of data. Therefore, in
 
 588      * blocking mode, if there is no data at all to be read, this
 
 589      * function will never exit (unless the socket is closed on the
 
 594      * @return string  All data until the socket closes, or a PEAR_Error if
 
 599         if (!is_resource($this->fp)) {
 
 600             return $this->raiseError('not connected');
 
 604         while (!feof($this->fp)) {
 
 605             $data .= @fread($this->fp, $this->lineLength);
 
 611      * Runs the equivalent of the select() system call on the socket
 
 612      * with a timeout specified by tv_sec and tv_usec.
 
 614      * @param integer $state   Which of read/write/error to check for.
 
 615      * @param integer $tv_sec  Number of seconds for timeout.
 
 616      * @param integer $tv_usec Number of microseconds for timeout.
 
 619      * @return False if select fails, integer describing which of read/write/error
 
 620      *         are ready, or PEAR_Error if not connected.
 
 622     function select($state, $tv_sec, $tv_usec = 0)
 
 624         if (!is_resource($this->fp)) {
 
 625             return $this->raiseError('not connected');
 
 631         if ($state & NET_SOCKET_READ) {
 
 634         if ($state & NET_SOCKET_WRITE) {
 
 635             $write[] = $this->fp;
 
 637         if ($state & NET_SOCKET_ERROR) {
 
 638             $except[] = $this->fp;
 
 640         if (false === ($sr = stream_select($read, $write, $except,
 
 641                                           $tv_sec, $tv_usec))) {
 
 647             $result |= NET_SOCKET_READ;
 
 650             $result |= NET_SOCKET_WRITE;
 
 652         if (count($except)) {
 
 653             $result |= NET_SOCKET_ERROR;
 
 659      * Turns encryption on/off on a connected socket.
 
 661      * @param bool    $enabled Set this parameter to true to enable encryption
 
 662      *                         and false to disable encryption.
 
 663      * @param integer $type    Type of encryption. See stream_socket_enable_crypto()
 
 666      * @see    http://se.php.net/manual/en/function.stream-socket-enable-crypto.php
 
 668      * @return false on error, true on success and 0 if there isn't enough data
 
 669      *         and the user should try again (non-blocking sockets only).
 
 670      *         A PEAR_Error object is returned if the socket is not
 
 673     function enableCrypto($enabled, $type)
 
 675         if (version_compare(phpversion(), "5.1.0", ">=")) {
 
 676             if (!is_resource($this->fp)) {
 
 677                 return $this->raiseError('not connected');
 
 679             return @stream_socket_enable_crypto($this->fp, $enabled, $type);
 
 681             $msg = 'Net_Socket::enableCrypto() requires php version >= 5.1.0';
 
 682             return $this->raiseError($msg);