OSDN Git Service

NP_Moblog v1.17
[nucleus-jp/nucleus-plugins.git] / trunk / NP_Moblog / sharedlibs / Net / Socket.php
index 72cd8a9..30c19b8 100644 (file)
-<?php
-//
-// +----------------------------------------------------------------------+
-// | PHP Version 4                                                        |
-// +----------------------------------------------------------------------+
-// | Copyright (c) 1997-2003 The PHP Group                                |
-// +----------------------------------------------------------------------+
-// | This source file is subject to version 2.0 of the PHP license,       |
-// | that is bundled with this package in the file LICENSE, and is        |
-// | available at through the world-wide-web at                           |
-// | http://www.php.net/license/2_02.txt.                                 |
-// | If you did not receive a copy of the PHP license and are unable to   |
-// | obtain it through the world-wide-web, please send a note to          |
-// | license@php.net so we can mail you a copy immediately.               |
-// +----------------------------------------------------------------------+
-// | Authors: Stig Bakken <ssb@php.net>                                   |
-// |          Chuck Hagenbuch <chuck@horde.org>                           |
-// +----------------------------------------------------------------------+
-//
-// $Id: Socket.php,v 1.1 2008-05-04 07:04:50 hsur Exp $
-
-require_once 'PEAR.php';
-
-define('NET_SOCKET_READ',  1);
-define('NET_SOCKET_WRITE', 2);
-define('NET_SOCKET_ERROR', 3);
-
-/**
- * Generalized Socket class.
- *
- * @version 1.1
- * @author Stig Bakken <ssb@php.net>
- * @author Chuck Hagenbuch <chuck@horde.org>
- */
-class Net_Socket extends PEAR {
-
-    /**
-     * Socket file pointer.
-     * @var resource $fp
-     */
-    var $fp = null;
-
-    /**
-     * Whether the socket is blocking. Defaults to true.
-     * @var boolean $blocking
-     */
-    var $blocking = true;
-
-    /**
-     * Whether the socket is persistent. Defaults to false.
-     * @var boolean $persistent
-     */
-    var $persistent = false;
-
-    /**
-     * The IP address to connect to.
-     * @var string $addr
-     */
-    var $addr = '';
-
-    /**
-     * The port number to connect to.
-     * @var integer $port
-     */
-    var $port = 0;
-
-    /**
-     * Number of seconds to wait on socket connections before assuming
-     * there's no more data. Defaults to no timeout.
-     * @var integer $timeout
-     */
-    var $timeout = false;
-
-    /**
-     * Number of bytes to read at a time in readLine() and
-     * readAll(). Defaults to 2048.
-     * @var integer $lineLength
-     */
-    var $lineLength = 2048;
-
-    /**
-     * Connect to the specified port. If called when the socket is
-     * already connected, it disconnects and connects again.
-     *
-     * @param string  $addr        IP address or host name.
-     * @param integer $port        TCP port number.
-     * @param boolean $persistent  (optional) Whether the connection is
-     *                             persistent (kept open between requests
-     *                             by the web server).
-     * @param integer $timeout     (optional) How long to wait for data.
-     * @param array   $options     See options for stream_context_create.
-     *
-     * @access public
-     *
-     * @return boolean | PEAR_Error  True on success or a PEAR_Error on failure.
-     */
-    function connect($addr, $port = 0, $persistent = null, $timeout = null, $options = null)
-    {
-        if (is_resource($this->fp)) {
-            @fclose($this->fp);
-            $this->fp = null;
-        }
-
-        if (!$addr) {
-            return $this->raiseError('$addr cannot be empty');
-        } elseif (strspn($addr, '.0123456789') == strlen($addr) ||
-                  strstr($addr, '/') !== false) {
-            $this->addr = $addr;
-        } else {
-            $this->addr = @gethostbyname($addr);
-        }
-
-        $this->port = $port % 65536;
-
-        if ($persistent !== null) {
-            $this->persistent = $persistent;
-        }
-
-        if ($timeout !== null) {
-            $this->timeout = $timeout;
-        }
-
-        $openfunc = $this->persistent ? 'pfsockopen' : 'fsockopen';
-        $errno = 0;
-        $errstr = '';
-        if ($options && function_exists('stream_context_create')) {
-            if ($this->timeout) {
-                $timeout = $this->timeout;
-            } else {
-                $timeout = 0;
-            }
-            $context = stream_context_create($options);
-            $fp = @$openfunc($this->addr, $this->port, $errno, $errstr, $timeout, $context);
-        } else {
-            if ($this->timeout) {
-                $fp = @$openfunc($this->addr, $this->port, $errno, $errstr, $this->timeout);
-            } else {
-                $fp = @$openfunc($this->addr, $this->port, $errno, $errstr);
-            }
-        }
-
-        if (!$fp) {
-            return $this->raiseError($errstr, $errno);
-        }
-
-        $this->fp = $fp;
-
-        return $this->setBlocking($this->blocking);
-    }
-
-    /**
-     * Disconnects from the peer, closes the socket.
-     *
-     * @access public
-     * @return mixed true on success or an error object otherwise
-     */
-    function disconnect()
-    {
-        if (!is_resource($this->fp)) {
-            return $this->raiseError('not connected');
-        }
-
-        @fclose($this->fp);
-        $this->fp = null;
-        return true;
-    }
-
-    /**
-     * Find out if the socket is in blocking mode.
-     *
-     * @access public
-     * @return boolean  The current blocking mode.
-     */
-    function isBlocking()
-    {
-        return $this->blocking;
-    }
-
-    /**
-     * Sets whether the socket connection should be blocking or
-     * not. A read call to a non-blocking socket will return immediately
-     * if there is no data available, whereas it will block until there
-     * is data for blocking sockets.
-     *
-     * @param boolean $mode  True for blocking sockets, false for nonblocking.
-     * @access public
-     * @return mixed true on success or an error object otherwise
-     */
-    function setBlocking($mode)
-    {
-        if (!is_resource($this->fp)) {
-            return $this->raiseError('not connected');
-        }
-
-        $this->blocking = $mode;
-        socket_set_blocking($this->fp, $this->blocking);
-        return true;
-    }
-
-    /**
-     * Sets the timeout value on socket descriptor,
-     * expressed in the sum of seconds and microseconds
-     *
-     * @param integer $seconds  Seconds.
-     * @param integer $microseconds  Microseconds.
-     * @access public
-     * @return mixed true on success or an error object otherwise
-     */
-    function setTimeout($seconds, $microseconds)
-    {
-        if (!is_resource($this->fp)) {
-            return $this->raiseError('not connected');
-        }
-
-        return socket_set_timeout($this->fp, $seconds, $microseconds);
-    }
-
-    /**
-     * Returns information about an existing socket resource.
-     * Currently returns four entries in the result array:
-     *
-     * <p>
-     * timed_out (bool) - The socket timed out waiting for data<br>
-     * blocked (bool) - The socket was blocked<br>
-     * eof (bool) - Indicates EOF event<br>
-     * unread_bytes (int) - Number of bytes left in the socket buffer<br>
-     * </p>
-     *
-     * @access public
-     * @return mixed Array containing information about existing socket resource or an error object otherwise
-     */
-    function getStatus()
-    {
-        if (!is_resource($this->fp)) {
-            return $this->raiseError('not connected');
-        }
-
-        return socket_get_status($this->fp);
-    }
-
-    /**
-     * Get a specified line of data
-     *
-     * @access public
-     * @return $size bytes of data from the socket, or a PEAR_Error if
-     *         not connected.
-     */
-    function gets($size)
-    {
-        if (!is_resource($this->fp)) {
-            return $this->raiseError('not connected');
-        }
-
-        return @fgets($this->fp, $size);
-    }
-
-    /**
-     * Read a specified amount of data. This is guaranteed to return,
-     * and has the added benefit of getting everything in one fread()
-     * chunk; if you know the size of the data you're getting
-     * beforehand, this is definitely the way to go.
-     *
-     * @param integer $size  The number of bytes to read from the socket.
-     * @access public
-     * @return $size bytes of data from the socket, or a PEAR_Error if
-     *         not connected.
-     */
-    function read($size)
-    {
-        if (!is_resource($this->fp)) {
-            return $this->raiseError('not connected');
-        }
-
-        return @fread($this->fp, $size);
-    }
-
-    /**
-     * Write a specified amount of data.
-     *
-     * @param string  $data       Data to write.
-     * @param integer $blocksize  Amount of data to write at once.
-     *                            NULL means all at once.
-     *
-     * @access public
-     * @return mixed true on success or an error object otherwise
-     */
-    function write($data, $blocksize = null)
-    {
-        if (!is_resource($this->fp)) {
-            return $this->raiseError('not connected');
-        }
-
-        if (is_null($blocksize) && !OS_WINDOWS) {
-            return fwrite($this->fp, $data);
-        } else {
-            if (is_null($blocksize)) {
-                $blocksize = 1024;
-            }
-
-            $pos = 0;
-            $size = strlen($data);
-            while ($pos < $size) {
-                $written = @fwrite($this->fp, substr($data, $pos, $blocksize));
-                if ($written === false) {
-                    return false;
-                }
-                $pos += $written;
-            }
-
-            return $pos;
-        }
-    }
-
-    /**
-     * Write a line of data to the socket, followed by a trailing "\r\n".
-     *
-     * @access public
-     * @return mixed fputs result, or an error
-     */
-    function writeLine($data)
-    {
-        if (!is_resource($this->fp)) {
-            return $this->raiseError('not connected');
-        }
-
-        return fwrite($this->fp, $data . "\r\n");
-    }
-
-    /**
-     * Tests for end-of-file on a socket descriptor.
-     *
-     * @access public
-     * @return bool
-     */
-    function eof()
-    {
-        return (is_resource($this->fp) && feof($this->fp));
-    }
-
-    /**
-     * Reads a byte of data
-     *
-     * @access public
-     * @return 1 byte of data from the socket, or a PEAR_Error if
-     *         not connected.
-     */
-    function readByte()
-    {
-        if (!is_resource($this->fp)) {
-            return $this->raiseError('not connected');
-        }
-
-        return ord(@fread($this->fp, 1));
-    }
-
-    /**
-     * Reads a word of data
-     *
-     * @access public
-     * @return 1 word of data from the socket, or a PEAR_Error if
-     *         not connected.
-     */
-    function readWord()
-    {
-        if (!is_resource($this->fp)) {
-            return $this->raiseError('not connected');
-        }
-
-        $buf = @fread($this->fp, 2);
-        return (ord($buf[0]) + (ord($buf[1]) << 8));
-    }
-
-    /**
-     * Reads an int of data
-     *
-     * @access public
-     * @return integer  1 int of data from the socket, or a PEAR_Error if
-     *                  not connected.
-     */
-    function readInt()
-    {
-        if (!is_resource($this->fp)) {
-            return $this->raiseError('not connected');
-        }
-
-        $buf = @fread($this->fp, 4);
-        return (ord($buf[0]) + (ord($buf[1]) << 8) +
-                (ord($buf[2]) << 16) + (ord($buf[3]) << 24));
-    }
-
-    /**
-     * Reads a zero-terminated string of data
-     *
-     * @access public
-     * @return string, or a PEAR_Error if
-     *         not connected.
-     */
-    function readString()
-    {
-        if (!is_resource($this->fp)) {
-            return $this->raiseError('not connected');
-        }
-
-        $string = '';
-        while (($char = @fread($this->fp, 1)) != "\x00")  {
-            $string .= $char;
-        }
-        return $string;
-    }
-
-    /**
-     * Reads an IP Address and returns it in a dot formated string
-     *
-     * @access public
-     * @return Dot formated string, or a PEAR_Error if
-     *         not connected.
-     */
-    function readIPAddress()
-    {
-        if (!is_resource($this->fp)) {
-            return $this->raiseError('not connected');
-        }
-
-        $buf = @fread($this->fp, 4);
-        return sprintf("%s.%s.%s.%s", ord($buf[0]), ord($buf[1]),
-                       ord($buf[2]), ord($buf[3]));
-    }
-
-    /**
-     * Read until either the end of the socket or a newline, whichever
-     * comes first. Strips the trailing newline from the returned data.
-     *
-     * @access public
-     * @return All available data up to a newline, without that
-     *         newline, or until the end of the socket, or a PEAR_Error if
-     *         not connected.
-     */
-    function readLine()
-    {
-        if (!is_resource($this->fp)) {
-            return $this->raiseError('not connected');
-        }
-
-        $line = '';
-        $timeout = time() + $this->timeout;
-        while (!feof($this->fp) && (!$this->timeout || time() < $timeout)) {
-            $line .= @fgets($this->fp, $this->lineLength);
-            if (substr($line, -1) == "\n") {
-                return rtrim($line, "\r\n");
-            }
-        }
-        return $line;
-    }
-
-    /**
-     * Read until the socket closes, or until there is no more data in
-     * the inner PHP buffer. If the inner buffer is empty, in blocking
-     * mode we wait for at least 1 byte of data. Therefore, in
-     * blocking mode, if there is no data at all to be read, this
-     * function will never exit (unless the socket is closed on the
-     * remote end).
-     *
-     * @access public
-     *
-     * @return string  All data until the socket closes, or a PEAR_Error if
-     *                 not connected.
-     */
-    function readAll()
-    {
-        if (!is_resource($this->fp)) {
-            return $this->raiseError('not connected');
-        }
-
-        $data = '';
-        while (!feof($this->fp)) {
-            $data .= @fread($this->fp, $this->lineLength);
-        }
-        return $data;
-    }
-
-    /**
-     * Runs the equivalent of the select() system call on the socket
-     * with a timeout specified by tv_sec and tv_usec.
-     *
-     * @param integer $state    Which of read/write/error to check for.
-     * @param integer $tv_sec   Number of seconds for timeout.
-     * @param integer $tv_usec  Number of microseconds for timeout.
-     *
-     * @access public
-     * @return False if select fails, integer describing which of read/write/error
-     *         are ready, or PEAR_Error if not connected.
-     */
-    function select($state, $tv_sec, $tv_usec = 0)
-    {
-        if (!is_resource($this->fp)) {
-            return $this->raiseError('not connected');
-        }
-
-        $read = null;
-        $write = null;
-        $except = null;
-        if ($state & NET_SOCKET_READ) {
-            $read[] = $this->fp;
-        }
-        if ($state & NET_SOCKET_WRITE) {
-            $write[] = $this->fp;
-        }
-        if ($state & NET_SOCKET_ERROR) {
-            $except[] = $this->fp;
-        }
-        if (false === ($sr = stream_select($read, $write, $except, $tv_sec, $tv_usec))) {
-            return false;
-        }
-
-        $result = 0;
-        if (count($read)) {
-            $result |= NET_SOCKET_READ;
-        }
-        if (count($write)) {
-            $result |= NET_SOCKET_WRITE;
-        }
-        if (count($except)) {
-            $result |= NET_SOCKET_ERROR;
-        }
-        return $result;
-    }
-
-}
+<?php\r
+//\r
+// +----------------------------------------------------------------------+\r
+// | PHP Version 4                                                        |\r
+// +----------------------------------------------------------------------+\r
+// | Copyright (c) 1997-2003 The PHP Group                                |\r
+// +----------------------------------------------------------------------+\r
+// | This source file is subject to version 2.0 of the PHP license,       |\r
+// | that is bundled with this package in the file LICENSE, and is        |\r
+// | available at through the world-wide-web at                           |\r
+// | http://www.php.net/license/2_02.txt.                                 |\r
+// | If you did not receive a copy of the PHP license and are unable to   |\r
+// | obtain it through the world-wide-web, please send a note to          |\r
+// | license@php.net so we can mail you a copy immediately.               |\r
+// +----------------------------------------------------------------------+\r
+// | Authors: Stig Bakken <ssb@php.net>                                   |\r
+// |          Chuck Hagenbuch <chuck@horde.org>                           |\r
+// +----------------------------------------------------------------------+\r
+//\r
+// $Id: Socket.php,v 1.8 2006/11/27 17:14:39 hsur Exp $\r
+\r
+require_once 'PEAR.php';\r
+\r
+define('NET_SOCKET_READ',  1);\r
+define('NET_SOCKET_WRITE', 2);\r
+define('NET_SOCKET_ERROR', 3);\r
+\r
+/**\r
+ * Generalized Socket class.\r
+ *\r
+ * @version 1.1\r
+ * @author Stig Bakken <ssb@php.net>\r
+ * @author Chuck Hagenbuch <chuck@horde.org>\r
+ */\r
+class Net_Socket extends PEAR {\r
+\r
+    /**\r
+     * Socket file pointer.\r
+     * @var resource $fp\r
+     */\r
+    var $fp = null;\r
+\r
+    /**\r
+     * Whether the socket is blocking. Defaults to true.\r
+     * @var boolean $blocking\r
+     */\r
+    var $blocking = true;\r
+\r
+    /**\r
+     * Whether the socket is persistent. Defaults to false.\r
+     * @var boolean $persistent\r
+     */\r
+    var $persistent = false;\r
+\r
+    /**\r
+     * The IP address to connect to.\r
+     * @var string $addr\r
+     */\r
+    var $addr = '';\r
+\r
+    /**\r
+     * The port number to connect to.\r
+     * @var integer $port\r
+     */\r
+    var $port = 0;\r
+\r
+    /**\r
+     * Number of seconds to wait on socket connections before assuming\r
+     * there's no more data. Defaults to no timeout.\r
+     * @var integer $timeout\r
+     */\r
+    var $timeout = false;\r
+\r
+    /**\r
+     * Number of bytes to read at a time in readLine() and\r
+     * readAll(). Defaults to 2048.\r
+     * @var integer $lineLength\r
+     */\r
+    var $lineLength = 2048;\r
+\r
+    /**\r
+     * Connect to the specified port. If called when the socket is\r
+     * already connected, it disconnects and connects again.\r
+     *\r
+     * @param string  $addr        IP address or host name.\r
+     * @param integer $port        TCP port number.\r
+     * @param boolean $persistent  (optional) Whether the connection is\r
+     *                             persistent (kept open between requests\r
+     *                             by the web server).\r
+     * @param integer $timeout     (optional) How long to wait for data.\r
+     * @param array   $options     See options for stream_context_create.\r
+     *\r
+     * @access public\r
+     *\r
+     * @return boolean | PEAR_Error  True on success or a PEAR_Error on failure.\r
+     */\r
+    function connect($addr, $port = 0, $persistent = null, $timeout = null, $options = null)\r
+    {\r
+        if (is_resource($this->fp)) {\r
+            @fclose($this->fp);\r
+            $this->fp = null;\r
+        }\r
+\r
+        if (!$addr) {\r
+            return $this->raiseError('$addr cannot be empty');\r
+        } elseif (strspn($addr, '.0123456789') == strlen($addr) ||\r
+                  strstr($addr, '/') !== false) {\r
+            $this->addr = $addr;\r
+        } else {\r
+            $this->addr = @gethostbyname($addr);\r
+        }\r
+\r
+        $this->port = $port % 65536;\r
+\r
+        if ($persistent !== null) {\r
+            $this->persistent = $persistent;\r
+        }\r
+\r
+        if ($timeout !== null) {\r
+            $this->timeout = $timeout;\r
+        }\r
+\r
+        $openfunc = $this->persistent ? 'pfsockopen' : 'fsockopen';\r
+        $errno = 0;\r
+        $errstr = '';\r
+        if ($options && function_exists('stream_context_create')) {\r
+            if ($this->timeout) {\r
+                $timeout = $this->timeout;\r
+            } else {\r
+                $timeout = 0;\r
+            }\r
+            $context = stream_context_create($options);\r
+            $fp = @$openfunc($this->addr, $this->port, $errno, $errstr, $timeout, $context);\r
+        } else {\r
+            if ($this->timeout) {\r
+                $fp = @$openfunc($this->addr, $this->port, $errno, $errstr, $this->timeout);\r
+            } else {\r
+                $fp = @$openfunc($this->addr, $this->port, $errno, $errstr);\r
+            }\r
+        }\r
+\r
+        if (!$fp) {\r
+            return $this->raiseError($errstr, $errno);\r
+        }\r
+\r
+        $this->fp = $fp;\r
+\r
+        return $this->setBlocking($this->blocking);\r
+    }\r
+\r
+    /**\r
+     * Disconnects from the peer, closes the socket.\r
+     *\r
+     * @access public\r
+     * @return mixed true on success or an error object otherwise\r
+     */\r
+    function disconnect()\r
+    {\r
+        if (!is_resource($this->fp)) {\r
+            return $this->raiseError('not connected');\r
+        }\r
+\r
+        @fclose($this->fp);\r
+        $this->fp = null;\r
+        return true;\r
+    }\r
+\r
+    /**\r
+     * Find out if the socket is in blocking mode.\r
+     *\r
+     * @access public\r
+     * @return boolean  The current blocking mode.\r
+     */\r
+    function isBlocking()\r
+    {\r
+        return $this->blocking;\r
+    }\r
+\r
+    /**\r
+     * Sets whether the socket connection should be blocking or\r
+     * not. A read call to a non-blocking socket will return immediately\r
+     * if there is no data available, whereas it will block until there\r
+     * is data for blocking sockets.\r
+     *\r
+     * @param boolean $mode  True for blocking sockets, false for nonblocking.\r
+     * @access public\r
+     * @return mixed true on success or an error object otherwise\r
+     */\r
+    function setBlocking($mode)\r
+    {\r
+        if (!is_resource($this->fp)) {\r
+            return $this->raiseError('not connected');\r
+        }\r
+\r
+        $this->blocking = $mode;\r
+        socket_set_blocking($this->fp, $this->blocking);\r
+        return true;\r
+    }\r
+\r
+    /**\r
+     * Sets the timeout value on socket descriptor,\r
+     * expressed in the sum of seconds and microseconds\r
+     *\r
+     * @param integer $seconds  Seconds.\r
+     * @param integer $microseconds  Microseconds.\r
+     * @access public\r
+     * @return mixed true on success or an error object otherwise\r
+     */\r
+    function setTimeout($seconds, $microseconds)\r
+    {\r
+        if (!is_resource($this->fp)) {\r
+            return $this->raiseError('not connected');\r
+        }\r
+\r
+        return socket_set_timeout($this->fp, $seconds, $microseconds);\r
+    }\r
+\r
+    /**\r
+     * Returns information about an existing socket resource.\r
+     * Currently returns four entries in the result array:\r
+     *\r
+     * <p>\r
+     * timed_out (bool) - The socket timed out waiting for data<br>\r
+     * blocked (bool) - The socket was blocked<br>\r
+     * eof (bool) - Indicates EOF event<br>\r
+     * unread_bytes (int) - Number of bytes left in the socket buffer<br>\r
+     * </p>\r
+     *\r
+     * @access public\r
+     * @return mixed Array containing information about existing socket resource or an error object otherwise\r
+     */\r
+    function getStatus()\r
+    {\r
+        if (!is_resource($this->fp)) {\r
+            return $this->raiseError('not connected');\r
+        }\r
+\r
+        return socket_get_status($this->fp);\r
+    }\r
+\r
+    /**\r
+     * Get a specified line of data\r
+     *\r
+     * @access public\r
+     * @return $size bytes of data from the socket, or a PEAR_Error if\r
+     *         not connected.\r
+     */\r
+    function gets($size)\r
+    {\r
+        if (!is_resource($this->fp)) {\r
+            return $this->raiseError('not connected');\r
+        }\r
+\r
+        return @fgets($this->fp, $size);\r
+    }\r
+\r
+    /**\r
+     * Read a specified amount of data. This is guaranteed to return,\r
+     * and has the added benefit of getting everything in one fread()\r
+     * chunk; if you know the size of the data you're getting\r
+     * beforehand, this is definitely the way to go.\r
+     *\r
+     * @param integer $size  The number of bytes to read from the socket.\r
+     * @access public\r
+     * @return $size bytes of data from the socket, or a PEAR_Error if\r
+     *         not connected.\r
+     */\r
+    function read($size)\r
+    {\r
+        if (!is_resource($this->fp)) {\r
+            return $this->raiseError('not connected');\r
+        }\r
+\r
+        return @fread($this->fp, $size);\r
+    }\r
+\r
+    /**\r
+     * Write a specified amount of data.\r
+     *\r
+     * @param string  $data       Data to write.\r
+     * @param integer $blocksize  Amount of data to write at once.\r
+     *                            NULL means all at once.\r
+     *\r
+     * @access public\r
+     * @return mixed true on success or an error object otherwise\r
+     */\r
+    function write($data, $blocksize = null)\r
+    {\r
+        if (!is_resource($this->fp)) {\r
+            return $this->raiseError('not connected');\r
+        }\r
+\r
+        if (is_null($blocksize) && !OS_WINDOWS) {\r
+            return fwrite($this->fp, $data);\r
+        } else {\r
+            if (is_null($blocksize)) {\r
+                $blocksize = 1024;\r
+            }\r
+\r
+            $pos = 0;\r
+            $size = strlen($data);\r
+            while ($pos < $size) {\r
+                $written = @fwrite($this->fp, substr($data, $pos, $blocksize));\r
+                if ($written === false) {\r
+                    return false;\r
+                }\r
+                $pos += $written;\r
+            }\r
+\r
+            return $pos;\r
+        }\r
+    }\r
+\r
+    /**\r
+     * Write a line of data to the socket, followed by a trailing "\r\n".\r
+     *\r
+     * @access public\r
+     * @return mixed fputs result, or an error\r
+     */\r
+    function writeLine($data)\r
+    {\r
+        if (!is_resource($this->fp)) {\r
+            return $this->raiseError('not connected');\r
+        }\r
+\r
+        return fwrite($this->fp, $data . "\r\n");\r
+    }\r
+\r
+    /**\r
+     * Tests for end-of-file on a socket descriptor.\r
+     *\r
+     * @access public\r
+     * @return bool\r
+     */\r
+    function eof()\r
+    {\r
+        return (is_resource($this->fp) && feof($this->fp));\r
+    }\r
+\r
+    /**\r
+     * Reads a byte of data\r
+     *\r
+     * @access public\r
+     * @return 1 byte of data from the socket, or a PEAR_Error if\r
+     *         not connected.\r
+     */\r
+    function readByte()\r
+    {\r
+        if (!is_resource($this->fp)) {\r
+            return $this->raiseError('not connected');\r
+        }\r
+\r
+        return ord(@fread($this->fp, 1));\r
+    }\r
+\r
+    /**\r
+     * Reads a word of data\r
+     *\r
+     * @access public\r
+     * @return 1 word of data from the socket, or a PEAR_Error if\r
+     *         not connected.\r
+     */\r
+    function readWord()\r
+    {\r
+        if (!is_resource($this->fp)) {\r
+            return $this->raiseError('not connected');\r
+        }\r
+\r
+        $buf = @fread($this->fp, 2);\r
+        return (ord($buf[0]) + (ord($buf[1]) << 8));\r
+    }\r
+\r
+    /**\r
+     * Reads an int of data\r
+     *\r
+     * @access public\r
+     * @return integer  1 int of data from the socket, or a PEAR_Error if\r
+     *                  not connected.\r
+     */\r
+    function readInt()\r
+    {\r
+        if (!is_resource($this->fp)) {\r
+            return $this->raiseError('not connected');\r
+        }\r
+\r
+        $buf = @fread($this->fp, 4);\r
+        return (ord($buf[0]) + (ord($buf[1]) << 8) +\r
+                (ord($buf[2]) << 16) + (ord($buf[3]) << 24));\r
+    }\r
+\r
+    /**\r
+     * Reads a zero-terminated string of data\r
+     *\r
+     * @access public\r
+     * @return string, or a PEAR_Error if\r
+     *         not connected.\r
+     */\r
+    function readString()\r
+    {\r
+        if (!is_resource($this->fp)) {\r
+            return $this->raiseError('not connected');\r
+        }\r
+\r
+        $string = '';\r
+        while (($char = @fread($this->fp, 1)) != "\x00")  {\r
+            $string .= $char;\r
+        }\r
+        return $string;\r
+    }\r
+\r
+    /**\r
+     * Reads an IP Address and returns it in a dot formated string\r
+     *\r
+     * @access public\r
+     * @return Dot formated string, or a PEAR_Error if\r
+     *         not connected.\r
+     */\r
+    function readIPAddress()\r
+    {\r
+        if (!is_resource($this->fp)) {\r
+            return $this->raiseError('not connected');\r
+        }\r
+\r
+        $buf = @fread($this->fp, 4);\r
+        return sprintf("%s.%s.%s.%s", ord($buf[0]), ord($buf[1]),\r
+                       ord($buf[2]), ord($buf[3]));\r
+    }\r
+\r
+    /**\r
+     * Read until either the end of the socket or a newline, whichever\r
+     * comes first. Strips the trailing newline from the returned data.\r
+     *\r
+     * @access public\r
+     * @return All available data up to a newline, without that\r
+     *         newline, or until the end of the socket, or a PEAR_Error if\r
+     *         not connected.\r
+     */\r
+    function readLine()\r
+    {\r
+        if (!is_resource($this->fp)) {\r
+            return $this->raiseError('not connected');\r
+        }\r
+\r
+        $line = '';\r
+        $timeout = time() + $this->timeout;\r
+        while (!feof($this->fp) && (!$this->timeout || time() < $timeout)) {\r
+            $line .= @fgets($this->fp, $this->lineLength);\r
+            if (substr($line, -1) == "\n") {\r
+                return rtrim($line, "\r\n");\r
+            }\r
+        }\r
+        return $line;\r
+    }\r
+\r
+    /**\r
+     * Read until the socket closes, or until there is no more data in\r
+     * the inner PHP buffer. If the inner buffer is empty, in blocking\r
+     * mode we wait for at least 1 byte of data. Therefore, in\r
+     * blocking mode, if there is no data at all to be read, this\r
+     * function will never exit (unless the socket is closed on the\r
+     * remote end).\r
+     *\r
+     * @access public\r
+     *\r
+     * @return string  All data until the socket closes, or a PEAR_Error if\r
+     *                 not connected.\r
+     */\r
+    function readAll()\r
+    {\r
+        if (!is_resource($this->fp)) {\r
+            return $this->raiseError('not connected');\r
+        }\r
+\r
+        $data = '';\r
+        while (!feof($this->fp)) {\r
+            $data .= @fread($this->fp, $this->lineLength);\r
+        }\r
+        return $data;\r
+    }\r
+\r
+    /**\r
+     * Runs the equivalent of the select() system call on the socket\r
+     * with a timeout specified by tv_sec and tv_usec.\r
+     *\r
+     * @param integer $state    Which of read/write/error to check for.\r
+     * @param integer $tv_sec   Number of seconds for timeout.\r
+     * @param integer $tv_usec  Number of microseconds for timeout.\r
+     *\r
+     * @access public\r
+     * @return False if select fails, integer describing which of read/write/error\r
+     *         are ready, or PEAR_Error if not connected.\r
+     */\r
+    function select($state, $tv_sec, $tv_usec = 0)\r
+    {\r
+        if (!is_resource($this->fp)) {\r
+            return $this->raiseError('not connected');\r
+        }\r
+\r
+        $read = null;\r
+        $write = null;\r
+        $except = null;\r
+        if ($state & NET_SOCKET_READ) {\r
+            $read[] = $this->fp;\r
+        }\r
+        if ($state & NET_SOCKET_WRITE) {\r
+            $write[] = $this->fp;\r
+        }\r
+        if ($state & NET_SOCKET_ERROR) {\r
+            $except[] = $this->fp;\r
+        }\r
+        if (false === ($sr = stream_select($read, $write, $except, $tv_sec, $tv_usec))) {\r
+            return false;\r
+        }\r
+\r
+        $result = 0;\r
+        if (count($read)) {\r
+            $result |= NET_SOCKET_READ;\r
+        }\r
+        if (count($write)) {\r
+            $result |= NET_SOCKET_WRITE;\r
+        }\r
+        if (count($except)) {\r
+            $result |= NET_SOCKET_ERROR;\r
+        }\r
+        return $result;\r
+    }\r
+\r
+}\r