till
2008-01-29 a5b598241f368187e8ddee7372da796e81fe2a90
 * updated bundled Net_Socket to 1.0.8
* updated bundled Net_SMTP to 1.2.10



2 files modified
498 ■■■■■ changed files
program/lib/Net/SMTP.php 88 ●●●● patch | view | raw | blame | history
program/lib/Net/Socket.php 410 ●●●●● patch | view | raw | blame | history
program/lib/Net/SMTP.php
@@ -36,7 +36,6 @@
 */
class Net_SMTP
{
    /**
     * The server to connect to.
     * @var string
@@ -124,7 +123,7 @@
        if (isset($port)) $this->port = $port;
        if (isset($localhost)) $this->localhost = $localhost;
        $this->_socket = &new Net_Socket();
        $this->_socket = new Net_Socket();
        /*
         * Include the Auth_SASL package.  If the package is not available,
@@ -269,7 +268,8 @@
            }
        }
        return PEAR::raiseError('Invalid response code received from server');
        return PEAR::raiseError('Invalid response code received from server',
                                $this->_code);
    }
    /**
@@ -425,7 +425,31 @@
    function auth($uid, $pwd , $method = '')
    {
        if (empty($this->_esmtp['AUTH'])) {
            return PEAR::raiseError('SMTP server does no support authentication');
            if (version_compare(PHP_VERSION, '5.1.0', '>=')) {
                if (!isset($this->_esmtp['STARTTLS'])) {
                    return PEAR::raiseError('SMTP server does not support authentication');
                }
                if (PEAR::isError($result = $this->_put('STARTTLS'))) {
                    return $result;
                }
                if (PEAR::isError($result = $this->_parseResponse(220))) {
                    return $result;
                }
                if (PEAR::isError($result = $this->_socket->enableCrypto(true, STREAM_CRYPTO_METHOD_TLS_CLIENT))) {
                    return $result;
                } elseif ($result !== true) {
                    return PEAR::raiseError('STARTTLS failed');
                }
                /* Send EHLO again to recieve the AUTH string from the
                 * SMTP server. */
                $this->_negotiate();
                if (empty($this->_esmtp['AUTH'])) {
                    return PEAR::raiseError('SMTP server does not support authentication');
                }
            } else {
                return PEAR::raiseError('SMTP server does not support authentication');
            }
        }
        /* If no method has been specified, get the name of the best
@@ -446,15 +470,19 @@
            case 'DIGEST-MD5':
                $result = $this->_authDigest_MD5($uid, $pwd);
                break;
            case 'CRAM-MD5':
                $result = $this->_authCRAM_MD5($uid, $pwd);
                break;
            case 'LOGIN':
                $result = $this->_authLogin($uid, $pwd);
                break;
            case 'PLAIN':
                $result = $this->_authPlain($uid, $pwd);
                break;
            default:
                $result = PEAR::raiseError("$method is not a supported authentication method");
                break;
@@ -666,34 +694,41 @@
    /**
     * Send the MAIL FROM: command.
     *
     * @param string The sender (reverse path) to set.
     * @param string $sender    The sender (reverse path) to set.
     * @param string $params    String containing additional MAIL parameters,
     *                          such as the NOTIFY flags defined by RFC 1891
     *                          or the VERP protocol.
     *
     * @param array optional arguments. Currently supported:
     *        verp   boolean or string. If true or string
     *               verp is enabled. If string the characters
     *               are considered verp separators.
     *                          If $params is an array, only the 'verp' option
     *                          is supported.  If 'verp' is true, the XVERP
     *                          parameter is appended to the MAIL command.  If
     *                          the 'verp' value is a string, the full
     *                          XVERP=value parameter is appended.
     *
     * @return mixed Returns a PEAR_Error with an error message on any
     *               kind of failure, or true on success.
     * @access public
     * @since  1.0
     */
    function mailFrom($sender, $args = array())
    function mailFrom($sender, $params = null)
    {
        $argstr = '';
        $args = "FROM:<$sender>";
        if (isset($args['verp'])) {
        /* Support the deprecated array form of $params. */
        if (is_array($params) && isset($params['verp'])) {
            /* XVERP */
            if ($args['verp'] === true) {
                $argstr .= ' XVERP';
            if ($params['verp'] === true) {
                $args .= ' XVERP';
            /* XVERP=something */
            } elseif (trim($args['verp'])) {
                $argstr .= ' XVERP=' . $args['verp'];
            } elseif (trim($params['verp'])) {
                $args .= ' XVERP=' . $params['verp'];
            }
        } elseif (is_string($params)) {
            $args .= ' ' . $params;
        }
        if (PEAR::isError($error = $this->_put('MAIL', "FROM:<$sender>$argstr"))) {
        if (PEAR::isError($error = $this->_put('MAIL', $args))) {
            return $error;
        }
        if (PEAR::isError($error = $this->_parseResponse(250))) {
@@ -706,16 +741,24 @@
    /**
     * Send the RCPT TO: command.
     *
     * @param string The recipient (forward path) to add.
     * @param string $recipient The recipient (forward path) to add.
     * @param string $params    String containing additional RCPT parameters,
     *                          such as the NOTIFY flags defined by RFC 1891.
     *
     * @return mixed Returns a PEAR_Error with an error message on any
     *               kind of failure, or true on success.
     *
     * @access public
     * @since  1.0
     */
    function rcptTo($recipient)
    function rcptTo($recipient, $params = null)
    {
        if (PEAR::isError($error = $this->_put('RCPT', "TO:<$recipient>"))) {
        $args = "TO:<$recipient>";
        if (is_string($params)) {
            $args .= ' ' . $params;
        }
        if (PEAR::isError($error = $this->_put('RCPT', $args))) {
            return $error;
        }
        if (PEAR::isError($error = $this->_parseResponse(array(250, 251)))) {
@@ -760,7 +803,7 @@
     * @access public
     * @since  1.0
     */
    function data(&$data)
    function data($data)
    {
        /* RFC 1870, section 3, subsection 3 states "a value of zero
         * indicates that no fixed maximum message size is in force".
@@ -784,8 +827,7 @@
            return $error;
        }
        $data .= "\r\n.\r\n";
        if (PEAR::isError($result = $this->_send($data))) {
        if (PEAR::isError($result = $this->_send($data . "\r\n.\r\n"))) {
            return $result;
        }
        if (PEAR::isError($error = $this->_parseResponse(250))) {
program/lib/Net/Socket.php
@@ -18,89 +18,108 @@
// +----------------------------------------------------------------------+
//
// $Id$
//
require_once 'PEAR.php';
define('NET_SOCKET_READ',  1);
define('NET_SOCKET_WRITE', 2);
define('NET_SOCKET_ERROR', 4);
/**
 * Generalized Socket class. More docs to be written.
 * Generalized Socket class.
 *
 * @version 1.0
 * @version 1.1
 * @author Stig Bakken <ssb@php.net>
 * @author Chuck Hagenbuch <chuck@horde.org>
 */
class Net_Socket extends PEAR {
    // {{{ properties
    /** Socket file pointer. */
    /**
     * Socket file pointer.
     * @var resource $fp
     */
    var $fp = null;
    /** Whether the socket is blocking. */
    /**
     * Whether the socket is blocking. Defaults to true.
     * @var boolean $blocking
     */
    var $blocking = true;
    /** Whether the socket is persistent. */
    /**
     * Whether the socket is persistent. Defaults to false.
     * @var boolean $persistent
     */
    var $persistent = false;
    /** The IP address to connect to. */
    /**
     * The IP address to connect to.
     * @var string $addr
     */
    var $addr = '';
    /** The port number to connect to. */
    /**
     * 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. */
    /**
     * 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(). */
    var $lineLength = 2048;
    // }}}
    // {{{ constructor
    /**
     * Constructs a new Net_Socket object.
     *
     * @access public
     * Number of bytes to read at a time in readLine() and
     * readAll(). Defaults to 2048.
     * @var integer $lineLength
     */
    function Net_Socket()
    {
        $this->PEAR();
    }
    // }}}
    var $lineLength = 2048;
    // {{{ connect()
    /**
     * Connect to the specified port. If called when the socket is
     * already connected, it disconnects and connects again.
     *
     * @param $addr string IP address or host name
     * @param $port int TCP port number
     * @param $persistent bool (optional) whether the connection is
     *        persistent (kept open between requests by the web server)
     * @param $timeout int (optional) how long to wait for data
     * @param $options array see options for stream_context_create
     * @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 mixed true on success or error object
     *
     * @return boolean | PEAR_Error  True on success or a PEAR_Error on failure.
     */
    function connect($addr, $port, $persistent = null, $timeout = null, $options = null)
    function connect($addr, $port = 0, $persistent = null, $timeout = null, $options = null)
    {
        if (is_resource($this->fp)) {
            @fclose($this->fp);
            $this->fp = null;
        }
        if (strspn($addr, '.0123456789') == strlen($addr)) {
        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->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 = '';
@@ -111,7 +130,7 @@
                $timeout = 0;
            }
            $context = stream_context_create($options);
            $fp = $openfunc($this->addr, $this->port, $errno, $errstr, $timeout, $context);
            $fp = @$openfunc($this->addr, $this->port, $errno, $errstr, $timeout, $context);
        } else {
            if ($this->timeout) {
                $fp = @$openfunc($this->addr, $this->port, $errno, $errstr, $this->timeout);
@@ -128,9 +147,7 @@
        return $this->setBlocking($this->blocking);
    }
    // }}}
    // {{{ disconnect()
    /**
     * Disconnects from the peer, closes the socket.
     *
@@ -139,71 +156,86 @@
     */
    function disconnect()
    {
        if (is_resource($this->fp)) {
            fclose($this->fp);
        if (!is_resource($this->fp)) {
            return $this->raiseError('not connected');
        }
        @fclose($this->fp);
            $this->fp = null;
            return true;
        }
        return $this->raiseError("not connected");
    }
    // }}}
    // {{{ isBlocking()
    /**
     * Find out if the socket is in blocking mode.
     *
     * @access public
     * @return bool the current blocking mode.
     * @return boolean  The current blocking mode.
     */
    function isBlocking()
    {
        return $this->blocking;
    }
    // }}}
    // {{{ setBlocking()
    /**
     * 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 $mode bool true for blocking sockets, false for nonblocking
     * @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)) {
        if (!is_resource($this->fp)) {
            return $this->raiseError('not connected');
        }
            $this->blocking = $mode;
            socket_set_blocking($this->fp, $this->blocking);
            return true;
        }
        return $this->raiseError("not connected");
    }
    // }}}
    // {{{ setTimeout()
    /**
     * Sets the timeout value on socket descriptor,
     * expressed in the sum of seconds and microseconds
     *
     * @param $seconds int seconds
     * @param $microseconds int 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)) {
            socket_set_timeout($this->fp, $seconds, $microseconds);
        if (!is_resource($this->fp)) {
            return $this->raiseError('not connected');
        }
        return socket_set_timeout($this->fp, $seconds, $microseconds);
    }
    /**
     * Sets the file buffering size on the stream.
     * See php's stream_set_write_buffer for more information.
     *
     * @param integer $size     Write buffer size.
     * @access public
     * @return mixed on success or an PEAR_Error object otherwise
     */
    function setWriteBuffer($size)
    {
        if (!is_resource($this->fp)) {
            return $this->raiseError('not connected');
        }
        $returned = stream_set_write_buffer($this->fp, $code);
        if ($returned == 0) {
            return true;
        }
        return $this->raiseError("not connected");
        return $this->raiseError('Cannot set write buffer.');
    }
    // }}}
    // {{{ getStatus()
    /**
     * Returns information about an existing socket resource.
     * Currently returns four entries in the result array:
@@ -220,14 +252,13 @@
     */
    function getStatus()
    {
        if (is_resource($this->fp)) {
        if (!is_resource($this->fp)) {
            return $this->raiseError('not connected');
        }
            return socket_get_status($this->fp);
        }
        return $this->raiseError("not connected");
    }
    // }}}
    // {{{ gets()
    /**
     * Get a specified line of data
     *
@@ -237,51 +268,70 @@
     */
    function gets($size)
    {
        if (is_resource($this->fp)) {
            return fgets($this->fp, $size);
        if (!is_resource($this->fp)) {
            return $this->raiseError('not connected');
        }
        return $this->raiseError("not connected");
    }
    // }}}
    // {{{ read()
        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 $size The number of bytes to read from the socket.
     * @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 fread($this->fp, $size);
        if (!is_resource($this->fp)) {
            return $this->raiseError('not connected');
        }
        return $this->raiseError("not connected");
    }
    // }}}
    // {{{ write()
        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)
    function write($data, $blocksize = null)
    {
        if (is_resource($this->fp)) {
            return fwrite($this->fp, $data);
        if (!is_resource($this->fp)) {
            return $this->raiseError('not connected');
        }
        return $this->raiseError("not connected");
    }
    // }}}
    // {{{ writeLine()
        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".
     *
@@ -290,27 +340,26 @@
     */
    function writeLine ($data)
    {
        if (is_resource($this->fp)) {
            return $this->write($data . "\r\n");
        if (!is_resource($this->fp)) {
            return $this->raiseError('not connected');
        }
        return $this->raiseError("not connected");
    }
    // }}}
    // {{{ eof()
        return fwrite($this->fp, $data . "\r\n");
    }
    /**
     * Tests for end-of-file on a socket descriptor
     * Tests for end-of-file on a socket descriptor.
     *
     * Also returns true if the socket is disconnected.
     *
     * @access public
     * @return bool
     */
    function eof()
    {
        return (is_resource($this->fp) && feof($this->fp));
        return (!is_resource($this->fp) || feof($this->fp));
    }
    // }}}
    // {{{ readByte()
    /**
     * Reads a byte of data
     *
@@ -320,14 +369,13 @@
     */
    function readByte()
    {
        if (is_resource($this->fp)) {
            return ord($this->read(1));
        if (!is_resource($this->fp)) {
            return $this->raiseError('not connected');
        }
        return $this->raiseError("not connected");
    }
    // }}}
    // {{{ readWord()
        return ord(@fread($this->fp, 1));
    }
    /**
     * Reads a word of data
     *
@@ -337,36 +385,34 @@
     */
    function readWord()
    {
        if (is_resource($this->fp)) {
            $buf = $this->read(2);
        if (!is_resource($this->fp)) {
            return $this->raiseError('not connected');
        }
        $buf = @fread($this->fp, 2);
            return (ord($buf[0]) + (ord($buf[1]) << 8));
        }
        return $this->raiseError("not connected");
    }
    // }}}
    // {{{ readInt()
    /**
     * Reads an int of data
     *
     * @access public
     * @return 1 int of data from the socket, or a PEAR_Error if
     * @return integer  1 int of data from the socket, or a PEAR_Error if
     *         not connected.
     */
    function readInt()
    {
        if (is_resource($this->fp)) {
            $buf = $this->read(4);
        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));
        }
        return $this->raiseError("not connected");
    }
    // }}}
    // {{{ readString()
    /**
     * Reads a zeroterminated string of data
     * Reads a zero-terminated string of data
     *
     * @access public
     * @return string, or a PEAR_Error if
@@ -374,18 +420,17 @@
     */
    function readString()
    {
        if (is_resource($this->fp)) {
        if (!is_resource($this->fp)) {
            return $this->raiseError('not connected');
        }
            $string = '';
            while (($char = $this->read(1)) != "\x00")  {
        while (($char = @fread($this->fp, 1)) != "\x00")  {
                $string .= $char;
            }
            return $string;
        }
        return $this->raiseError("not connected");
    }
    // }}}
    // {{{ readIPAddress()
    /**
     * Reads an IP Address and returns it in a dot formated string
     *
@@ -395,16 +440,15 @@
     */
    function readIPAddress()
    {
        if (is_resource($this->fp)) {
            $buf = $this->read(4);
        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]));
        }
        return $this->raiseError("not connected");
    }
    // }}}
    // {{{ readLine()
    /**
     * Read until either the end of the socket or a newline, whichever
     * comes first. Strips the trailing newline from the returned data.
@@ -416,41 +460,117 @@
     */
    function readLine()
    {
        if (is_resource($this->fp)) {
        if (!is_resource($this->fp)) {
            return $this->raiseError('not connected');
        }
            $line = '';
            $timeout = time() + $this->timeout;
            while (!$this->eof() && (!$this->timeout || time() < $timeout)) {
                $line .= $this->gets($this->lineLength);
                if (substr($line, -2) == "\r\n" ||
                    substr($line, -1) == "\n") {
        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;
        }
        return $this->raiseError("not connected");
    }
    // }}}
    // {{{ readAll()
    /**
     * Read until the socket closes. THIS FUNCTION WILL NOT EXIT if the
     * socket is in blocking mode until the socket closes.
     * 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 All data until the socket closes, or a PEAR_Error if
     *
     * @return string  All data until the socket closes, or a PEAR_Error if
     *         not connected.
     */
    function readAll()
    {
        if (is_resource($this->fp)) {
        if (!is_resource($this->fp)) {
            return $this->raiseError('not connected');
        }
            $data = '';
            while (!$this->eof())
                $data .= $this->read($this->lineLength);
        while (!feof($this->fp)) {
            $data .= @fread($this->fp, $this->lineLength);
        }
            return $data;
        }
        return $this->raiseError("not connected");
    /**
     * 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;
    }
    /**
     * Turns encryption on/off on a connected socket.
     *
     * @param bool    $enabled  Set this parameter to true to enable encryption
     *                          and false to disable encryption.
     * @param integer $type     Type of encryption. See
     *                          http://se.php.net/manual/en/function.stream-socket-enable-crypto.php for values.
     *
     * @access public
     * @return false on error, true on success and 0 if there isn't enough data and the
     *         user should try again (non-blocking sockets only). A PEAR_Error object
     *         is returned if the socket is not connected
     */
    function enableCrypto($enabled, $type)
    {
        if (version_compare(phpversion(), "5.1.0", ">=")) {
            if (!is_resource($this->fp)) {
                return $this->raiseError('not connected');
            }
            return @stream_socket_enable_crypto($this->fp, $enabled, $type);
        } else {
            return $this->raiseError('Net_Socket::enableCrypto() requires php version >= 5.1.0');
        }
    }
}