Changeset a5b5982 in github


Ignore:
Timestamp:
Jan 29, 2008 2:30:48 PM (5 years ago)
Author:
till <till@…>
Branches:
master, HEAD, courier-fix, dev-browser-capabilities, pdo, release-0.6, release-0.7, release-0.8
Children:
3f8d85e
Parents:
c2f407a
Message:
  • updated bundled Net_Socket to 1.0.8
  • updated bundled Net_SMTP to 1.2.10
Location:
program/lib/Net
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • program/lib/Net/SMTP.php

    rb517af4 ra5b5982  
    3737class Net_SMTP 
    3838{ 
    39  
    4039    /** 
    4140     * The server to connect to. 
     
    125124        if (isset($localhost)) $this->localhost = $localhost; 
    126125 
    127         $this->_socket = &new Net_Socket(); 
     126        $this->_socket = new Net_Socket(); 
    128127 
    129128        /* 
     
    270269        } 
    271270 
    272         return PEAR::raiseError('Invalid response code received from server'); 
     271        return PEAR::raiseError('Invalid response code received from server', 
     272                                $this->_code); 
    273273    } 
    274274 
     
    426426    { 
    427427        if (empty($this->_esmtp['AUTH'])) { 
    428             return PEAR::raiseError('SMTP server does no support authentication'); 
     428            if (version_compare(PHP_VERSION, '5.1.0', '>=')) { 
     429                if (!isset($this->_esmtp['STARTTLS'])) { 
     430                    return PEAR::raiseError('SMTP server does not support authentication'); 
     431                } 
     432                if (PEAR::isError($result = $this->_put('STARTTLS'))) { 
     433                    return $result; 
     434                } 
     435                if (PEAR::isError($result = $this->_parseResponse(220))) { 
     436                    return $result; 
     437                } 
     438                if (PEAR::isError($result = $this->_socket->enableCrypto(true, STREAM_CRYPTO_METHOD_TLS_CLIENT))) { 
     439                    return $result; 
     440                } elseif ($result !== true) { 
     441                    return PEAR::raiseError('STARTTLS failed'); 
     442                } 
     443 
     444                /* Send EHLO again to recieve the AUTH string from the 
     445                 * SMTP server. */ 
     446                $this->_negotiate(); 
     447                if (empty($this->_esmtp['AUTH'])) { 
     448                    return PEAR::raiseError('SMTP server does not support authentication'); 
     449                } 
     450            } else { 
     451                return PEAR::raiseError('SMTP server does not support authentication'); 
     452            } 
    429453        } 
    430454 
     
    444468 
    445469        switch ($method) { 
    446             case 'DIGEST-MD5': 
    447                 $result = $this->_authDigest_MD5($uid, $pwd); 
    448                 break; 
    449             case 'CRAM-MD5': 
    450                 $result = $this->_authCRAM_MD5($uid, $pwd); 
    451                 break; 
    452             case 'LOGIN': 
    453                 $result = $this->_authLogin($uid, $pwd); 
    454                 break; 
    455             case 'PLAIN': 
    456                 $result = $this->_authPlain($uid, $pwd); 
    457                 break; 
    458             default: 
    459                 $result = PEAR::raiseError("$method is not a supported authentication method"); 
    460                 break; 
     470        case 'DIGEST-MD5': 
     471            $result = $this->_authDigest_MD5($uid, $pwd); 
     472            break; 
     473 
     474        case 'CRAM-MD5': 
     475            $result = $this->_authCRAM_MD5($uid, $pwd); 
     476            break; 
     477 
     478        case 'LOGIN': 
     479            $result = $this->_authLogin($uid, $pwd); 
     480            break; 
     481 
     482        case 'PLAIN': 
     483            $result = $this->_authPlain($uid, $pwd); 
     484            break; 
     485 
     486        default: 
     487            $result = PEAR::raiseError("$method is not a supported authentication method"); 
     488            break; 
    461489        } 
    462490 
     
    667695     * Send the MAIL FROM: command. 
    668696     * 
    669      * @param string The sender (reverse path) to set. 
    670      * 
    671      * @param array optional arguments. Currently supported: 
    672      *        verp   boolean or string. If true or string 
    673      *               verp is enabled. If string the characters 
    674      *               are considered verp separators. 
     697     * @param string $sender    The sender (reverse path) to set. 
     698     * @param string $params    String containing additional MAIL parameters, 
     699     *                          such as the NOTIFY flags defined by RFC 1891 
     700     *                          or the VERP protocol. 
     701     * 
     702     *                          If $params is an array, only the 'verp' option 
     703     *                          is supported.  If 'verp' is true, the XVERP 
     704     *                          parameter is appended to the MAIL command.  If 
     705     *                          the 'verp' value is a string, the full 
     706     *                          XVERP=value parameter is appended. 
    675707     * 
    676708     * @return mixed Returns a PEAR_Error with an error message on any 
     
    679711     * @since  1.0 
    680712     */ 
    681     function mailFrom($sender, $args = array()) 
    682     { 
    683         $argstr = ''; 
    684  
    685         if (isset($args['verp'])) { 
     713    function mailFrom($sender, $params = null) 
     714    { 
     715        $args = "FROM:<$sender>"; 
     716 
     717        /* Support the deprecated array form of $params. */ 
     718        if (is_array($params) && isset($params['verp'])) { 
    686719            /* XVERP */ 
    687             if ($args['verp'] === true) { 
    688                 $argstr .= ' XVERP'; 
     720            if ($params['verp'] === true) { 
     721                $args .= ' XVERP'; 
    689722 
    690723            /* XVERP=something */ 
    691             } elseif (trim($args['verp'])) { 
    692                 $argstr .= ' XVERP=' . $args['verp']; 
    693             } 
    694         } 
    695  
    696         if (PEAR::isError($error = $this->_put('MAIL', "FROM:<$sender>$argstr"))) { 
     724            } elseif (trim($params['verp'])) { 
     725                $args .= ' XVERP=' . $params['verp']; 
     726            } 
     727        } elseif (is_string($params)) { 
     728            $args .= ' ' . $params; 
     729        } 
     730 
     731        if (PEAR::isError($error = $this->_put('MAIL', $args))) { 
    697732            return $error; 
    698733        } 
     
    707742     * Send the RCPT TO: command. 
    708743     * 
    709      * @param string The recipient (forward path) to add. 
    710      * 
    711      * @return mixed Returns a PEAR_Error with an error message on any 
    712      *               kind of failure, or true on success. 
     744     * @param string $recipient The recipient (forward path) to add. 
     745     * @param string $params    String containing additional RCPT parameters, 
     746     *                          such as the NOTIFY flags defined by RFC 1891. 
     747     * 
     748     * @return mixed Returns a PEAR_Error with an error message on any 
     749     *               kind of failure, or true on success. 
     750     * 
    713751     * @access public 
    714752     * @since  1.0 
    715753     */ 
    716     function rcptTo($recipient) 
    717     { 
    718         if (PEAR::isError($error = $this->_put('RCPT', "TO:<$recipient>"))) { 
     754    function rcptTo($recipient, $params = null) 
     755    { 
     756        $args = "TO:<$recipient>"; 
     757        if (is_string($params)) { 
     758            $args .= ' ' . $params; 
     759        } 
     760 
     761        if (PEAR::isError($error = $this->_put('RCPT', $args))) { 
    719762            return $error; 
    720763        } 
     
    761804     * @since  1.0 
    762805     */ 
    763     function data(&$data) 
     806    function data($data) 
    764807    { 
    765808        /* RFC 1870, section 3, subsection 3 states "a value of zero 
     
    785828        } 
    786829 
    787         $data .= "\r\n.\r\n"; 
    788         if (PEAR::isError($result = $this->_send($data))) { 
     830        if (PEAR::isError($result = $this->_send($data . "\r\n.\r\n"))) { 
    789831            return $result; 
    790832        } 
  • program/lib/Net/Socket.php

    r627330f ra5b5982  
    1919// 
    2020// $Id$ 
    21 // 
    2221 
    2322require_once 'PEAR.php'; 
    2423 
     24define('NET_SOCKET_READ',  1); 
     25define('NET_SOCKET_WRITE', 2); 
     26define('NET_SOCKET_ERROR', 4); 
     27 
    2528/** 
    26  * Generalized Socket class. More docs to be written. 
     29 * Generalized Socket class. 
    2730 * 
    28  * @version 1.0 
     31 * @version 1.1 
    2932 * @author Stig Bakken <ssb@php.net> 
    3033 * @author Chuck Hagenbuch <chuck@horde.org> 
    3134 */ 
    3235class Net_Socket extends PEAR { 
    33     // {{{ properties 
    34  
    35     /** Socket file pointer. */ 
     36 
     37    /** 
     38     * Socket file pointer. 
     39     * @var resource $fp 
     40     */ 
    3641    var $fp = null; 
    3742 
    38     /** Whether the socket is blocking. */ 
     43    /** 
     44     * Whether the socket is blocking. Defaults to true. 
     45     * @var boolean $blocking 
     46     */ 
    3947    var $blocking = true; 
    4048 
    41     /** Whether the socket is persistent. */ 
     49    /** 
     50     * Whether the socket is persistent. Defaults to false. 
     51     * @var boolean $persistent 
     52     */ 
    4253    var $persistent = false; 
    4354 
    44     /** The IP address to connect to. */ 
     55    /** 
     56     * The IP address to connect to. 
     57     * @var string $addr 
     58     */ 
    4559    var $addr = ''; 
    4660 
    47     /** The port number to connect to. */ 
     61    /** 
     62     * The port number to connect to. 
     63     * @var integer $port 
     64     */ 
    4865    var $port = 0; 
    4966 
    50     /** Number of seconds to wait on socket connections before 
    51         assuming there's no more data. */ 
     67    /** 
     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     */ 
    5272    var $timeout = false; 
    5373 
    54     /** Number of bytes to read at a time in readLine() and 
    55         readAll(). */ 
     74    /** 
     75     * Number of bytes to read at a time in readLine() and 
     76     * readAll(). Defaults to 2048. 
     77     * @var integer $lineLength 
     78     */ 
    5679    var $lineLength = 2048; 
    57     // }}} 
    58  
    59     // {{{ constructor 
    60     /** 
    61      * Constructs a new Net_Socket object. 
    62      * 
    63      * @access public 
    64      */ 
    65     function Net_Socket() 
    66     { 
    67         $this->PEAR(); 
    68     } 
    69     // }}} 
    70  
    71     // {{{ connect() 
     80 
    7281    /** 
    7382     * Connect to the specified port. If called when the socket is 
    7483     * already connected, it disconnects and connects again. 
    7584     * 
    76      * @param $addr string IP address or host name 
    77      * @param $port int TCP port number 
    78      * @param $persistent bool (optional) whether the connection is 
    79      *        persistent (kept open between requests by the web server) 
    80      * @param $timeout int (optional) how long to wait for data 
    81      * @param $options array see options for stream_context_create 
    82      * @access public 
    83      * @return mixed true on success or error object 
    84      */ 
    85     function connect($addr, $port, $persistent = null, $timeout = null, $options = null) 
     85     * @param string  $addr        IP address or host name. 
     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     * 
     93     * @access public 
     94     * 
     95     * @return boolean | PEAR_Error  True on success or a PEAR_Error on failure. 
     96     */ 
     97    function connect($addr, $port = 0, $persistent = null, $timeout = null, $options = null) 
    8698    { 
    8799        if (is_resource($this->fp)) { 
     
    90102        } 
    91103 
    92         if (strspn($addr, '.0123456789') == strlen($addr)) { 
     104        if (!$addr) { 
     105            return $this->raiseError('$addr cannot be empty'); 
     106        } elseif (strspn($addr, '.0123456789') == strlen($addr) || 
     107                  strstr($addr, '/') !== false) { 
    93108            $this->addr = $addr; 
    94109        } else { 
    95             $this->addr = gethostbyname($addr); 
    96         } 
     110            $this->addr = @gethostbyname($addr); 
     111        } 
     112 
    97113        $this->port = $port % 65536; 
     114 
    98115        if ($persistent !== null) { 
    99116            $this->persistent = $persistent; 
    100117        } 
     118 
    101119        if ($timeout !== null) { 
    102120            $this->timeout = $timeout; 
    103121        } 
     122 
    104123        $openfunc = $this->persistent ? 'pfsockopen' : 'fsockopen'; 
    105124        $errno = 0; 
     
    112131            } 
    113132            $context = stream_context_create($options); 
    114             $fp = $openfunc($this->addr, $this->port, $errno, $errstr, $timeout, $context); 
     133            $fp = @$openfunc($this->addr, $this->port, $errno, $errstr, $timeout, $context); 
    115134        } else { 
    116135            if ($this->timeout) { 
     
    129148        return $this->setBlocking($this->blocking); 
    130149    } 
    131     // }}} 
    132  
    133     // {{{ disconnect() 
     150 
    134151    /** 
    135152     * Disconnects from the peer, closes the socket. 
     
    140157    function disconnect() 
    141158    { 
    142         if (is_resource($this->fp)) { 
    143             fclose($this->fp); 
    144             $this->fp = null; 
    145             return true; 
    146         } 
    147         return $this->raiseError("not connected"); 
    148     } 
    149     // }}} 
    150  
    151     // {{{ isBlocking() 
     159        if (!is_resource($this->fp)) { 
     160            return $this->raiseError('not connected'); 
     161        } 
     162 
     163        @fclose($this->fp); 
     164        $this->fp = null; 
     165        return true; 
     166    } 
     167 
    152168    /** 
    153169     * Find out if the socket is in blocking mode. 
    154170     * 
    155171     * @access public 
    156      * @return bool the current blocking mode. 
     172     * @return boolean  The current blocking mode. 
    157173     */ 
    158174    function isBlocking() 
     
    160176        return $this->blocking; 
    161177    } 
    162     // }}} 
    163  
    164     // {{{ setBlocking() 
     178 
    165179    /** 
    166180     * Sets whether the socket connection should be blocking or 
     
    169183     * is data for blocking sockets. 
    170184     * 
    171      * @param $mode bool true for blocking sockets, false for nonblocking 
     185     * @param boolean $mode  True for blocking sockets, false for nonblocking. 
    172186     * @access public 
    173187     * @return mixed true on success or an error object otherwise 
     
    175189    function setBlocking($mode) 
    176190    { 
    177         if (is_resource($this->fp)) { 
    178             $this->blocking = $mode; 
    179             socket_set_blocking($this->fp, $this->blocking); 
    180             return true; 
    181         } 
    182         return $this->raiseError("not connected"); 
    183     } 
    184     // }}} 
    185  
    186     // {{{ setTimeout() 
     191        if (!is_resource($this->fp)) { 
     192            return $this->raiseError('not connected'); 
     193        } 
     194 
     195        $this->blocking = $mode; 
     196        socket_set_blocking($this->fp, $this->blocking); 
     197        return true; 
     198    } 
     199 
    187200    /** 
    188201     * Sets the timeout value on socket descriptor, 
    189202     * expressed in the sum of seconds and microseconds 
    190203     * 
    191      * @param $seconds int seconds 
    192      * @param $microseconds int microseconds 
     204     * @param integer $seconds  Seconds. 
     205     * @param integer $microseconds  Microseconds. 
    193206     * @access public 
    194207     * @return mixed true on success or an error object otherwise 
     
    196209    function setTimeout($seconds, $microseconds) 
    197210    { 
    198         if (is_resource($this->fp)) { 
    199             socket_set_timeout($this->fp, $seconds, $microseconds); 
     211        if (!is_resource($this->fp)) { 
     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) { 
    200234            return true; 
    201235        } 
    202         return $this->raiseError("not connected"); 
    203     } 
    204     // }}} 
    205  
    206     // {{{ getStatus() 
     236        return $this->raiseError('Cannot set write buffer.'); 
     237    } 
     238 
    207239    /** 
    208240     * Returns information about an existing socket resource. 
     
    221253    function getStatus() 
    222254    { 
    223         if (is_resource($this->fp)) { 
    224             return socket_get_status($this->fp); 
    225         } 
    226         return $this->raiseError("not connected"); 
    227     } 
    228     // }}} 
    229  
    230     // {{{ gets() 
     255        if (!is_resource($this->fp)) { 
     256            return $this->raiseError('not connected'); 
     257        } 
     258 
     259        return socket_get_status($this->fp); 
     260    } 
     261 
    231262    /** 
    232263     * Get a specified line of data 
     
    238269    function gets($size) 
    239270    { 
    240         if (is_resource($this->fp)) { 
    241             return fgets($this->fp, $size); 
    242         } 
    243         return $this->raiseError("not connected"); 
    244     } 
    245     // }}} 
    246  
    247     // {{{ read() 
     271        if (!is_resource($this->fp)) { 
     272            return $this->raiseError('not connected'); 
     273        } 
     274 
     275        return @fgets($this->fp, $size); 
     276    } 
     277 
    248278    /** 
    249279     * Read a specified amount of data. This is guaranteed to return, 
     
    252282     * beforehand, this is definitely the way to go. 
    253283     * 
    254      * @param $size The number of bytes to read from the socket. 
     284     * @param integer $size The number of bytes to read from the socket. 
    255285     * @access public 
    256286     * @return $size bytes of data from the socket, or a PEAR_Error if 
     
    259289    function read($size) 
    260290    { 
    261         if (is_resource($this->fp)) { 
    262             return fread($this->fp, $size); 
    263         } 
    264         return $this->raiseError("not connected"); 
    265     } 
    266     // }}} 
    267  
    268     // {{{ write() 
     291        if (!is_resource($this->fp)) { 
     292            return $this->raiseError('not connected'); 
     293        } 
     294 
     295        return @fread($this->fp, $size); 
     296    } 
     297 
    269298    /** 
    270299     * Write a specified amount of data. 
    271300     * 
     301     * @param string  $data       Data to write. 
     302     * @param integer $blocksize  Amount of data to write at once. 
     303     *                            NULL means all at once. 
     304     * 
    272305     * @access public 
    273306     * @return mixed true on success or an error object otherwise 
    274307     */ 
    275     function write($data) 
    276     { 
    277         if (is_resource($this->fp)) { 
     308    function write($data, $blocksize = null) 
     309    { 
     310        if (!is_resource($this->fp)) { 
     311            return $this->raiseError('not connected'); 
     312        } 
     313 
     314        if (is_null($blocksize) && !OS_WINDOWS) { 
    278315            return fwrite($this->fp, $data); 
    279         } 
    280         return $this->raiseError("not connected"); 
    281     } 
    282     // }}} 
    283  
    284     // {{{ writeLine() 
     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 
    285335    /** 
    286336     * Write a line of data to the socket, followed by a trailing "\r\n". 
     
    289339     * @return mixed fputs result, or an error 
    290340     */ 
    291     function writeLine ($data) 
    292     { 
    293         if (is_resource($this->fp)) { 
    294             return $this->write($data . "\r\n"); 
    295         } 
    296         return $this->raiseError("not connected"); 
    297     } 
    298     // }}} 
    299  
    300     // {{{ eof() 
    301     /** 
    302      * Tests for end-of-file on a socket descriptor 
     341    function writeLine($data) 
     342    { 
     343        if (!is_resource($this->fp)) { 
     344            return $this->raiseError('not connected'); 
     345        } 
     346 
     347        return fwrite($this->fp, $data . "\r\n"); 
     348    } 
     349 
     350    /** 
     351     * Tests for end-of-file on a socket descriptor. 
     352     * 
     353     * Also returns true if the socket is disconnected. 
    303354     * 
    304355     * @access public 
     
    307358    function eof() 
    308359    { 
    309         return (is_resource($this->fp) && feof($this->fp)); 
    310     } 
    311     // }}} 
    312  
    313     // {{{ readByte() 
     360        return (!is_resource($this->fp) || feof($this->fp)); 
     361    } 
     362 
    314363    /** 
    315364     * Reads a byte of data 
     
    321370    function readByte() 
    322371    { 
    323         if (is_resource($this->fp)) { 
    324             return ord($this->read(1)); 
    325         } 
    326         return $this->raiseError("not connected"); 
    327     } 
    328     // }}} 
    329  
    330     // {{{ readWord() 
     372        if (!is_resource($this->fp)) { 
     373            return $this->raiseError('not connected'); 
     374        } 
     375 
     376        return ord(@fread($this->fp, 1)); 
     377    } 
     378 
    331379    /** 
    332380     * Reads a word of data 
     
    338386    function readWord() 
    339387    { 
    340         if (is_resource($this->fp)) { 
    341             $buf = $this->read(2); 
    342             return (ord($buf[0]) + (ord($buf[1]) << 8)); 
    343         } 
    344         return $this->raiseError("not connected"); 
    345     } 
    346     // }}} 
    347  
    348     // {{{ readInt() 
     388        if (!is_resource($this->fp)) { 
     389            return $this->raiseError('not connected'); 
     390        } 
     391 
     392        $buf = @fread($this->fp, 2); 
     393        return (ord($buf[0]) + (ord($buf[1]) << 8)); 
     394    } 
     395 
    349396    /** 
    350397     * Reads an int of data 
    351398     * 
    352399     * @access public 
    353      * @return 1 int of data from the socket, or a PEAR_Error if 
    354      *         not connected. 
     400     * @return integer  1 int of data from the socket, or a PEAR_Error if 
     401     *                  not connected. 
    355402     */ 
    356403    function readInt() 
    357404    { 
    358         if (is_resource($this->fp)) { 
    359             $buf = $this->read(4); 
    360             return (ord($buf[0]) + (ord($buf[1]) << 8) + 
    361                     (ord($buf[2]) << 16) + (ord($buf[3]) << 24)); 
    362         } 
    363         return $this->raiseError("not connected"); 
    364     } 
    365     // }}} 
    366  
    367     // {{{ readString() 
    368     /** 
    369      * Reads a zeroterminated string of data 
     405        if (!is_resource($this->fp)) { 
     406            return $this->raiseError('not connected'); 
     407        } 
     408 
     409        $buf = @fread($this->fp, 4); 
     410        return (ord($buf[0]) + (ord($buf[1]) << 8) + 
     411                (ord($buf[2]) << 16) + (ord($buf[3]) << 24)); 
     412    } 
     413 
     414    /** 
     415     * Reads a zero-terminated string of data 
    370416     * 
    371417     * @access public 
     
    375421    function readString() 
    376422    { 
    377         if (is_resource($this->fp)) { 
    378             $string = ''; 
    379             while (($char = $this->read(1)) != "\x00")  { 
    380                 $string .= $char; 
    381             } 
    382             return $string; 
    383         } 
    384         return $this->raiseError("not connected"); 
    385     } 
    386     // }}} 
    387  
    388     // {{{ readIPAddress() 
     423        if (!is_resource($this->fp)) { 
     424            return $this->raiseError('not connected'); 
     425        } 
     426 
     427        $string = ''; 
     428        while (($char = @fread($this->fp, 1)) != "\x00")  { 
     429            $string .= $char; 
     430        } 
     431        return $string; 
     432    } 
     433 
    389434    /** 
    390435     * Reads an IP Address and returns it in a dot formated string 
     
    396441    function readIPAddress() 
    397442    { 
    398         if (is_resource($this->fp)) { 
    399             $buf = $this->read(4); 
    400             return sprintf("%s.%s.%s.%s", ord($buf[0]), ord($buf[1]), 
    401                            ord($buf[2]), ord($buf[3])); 
    402         } 
    403         return $this->raiseError("not connected"); 
    404     } 
    405     // }}} 
    406  
    407     // {{{ readLine() 
     443        if (!is_resource($this->fp)) { 
     444            return $this->raiseError('not connected'); 
     445        } 
     446 
     447        $buf = @fread($this->fp, 4); 
     448        return sprintf("%s.%s.%s.%s", ord($buf[0]), ord($buf[1]), 
     449                       ord($buf[2]), ord($buf[3])); 
     450    } 
     451 
    408452    /** 
    409453     * Read until either the end of the socket or a newline, whichever 
     
    417461    function readLine() 
    418462    { 
    419         if (is_resource($this->fp)) { 
    420             $line = ''; 
    421             $timeout = time() + $this->timeout; 
    422             while (!$this->eof() && (!$this->timeout || time() < $timeout)) { 
    423                 $line .= $this->gets($this->lineLength); 
    424                 if (substr($line, -2) == "\r\n" || 
    425                     substr($line, -1) == "\n") { 
    426                     return rtrim($line, "\r\n"); 
    427                 } 
     463        if (!is_resource($this->fp)) { 
     464            return $this->raiseError('not connected'); 
     465        } 
     466 
     467        $line = ''; 
     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"); 
    428473            } 
    429             return $line; 
    430         } 
    431         return $this->raiseError("not connected"); 
    432     } 
    433     // }}} 
    434  
    435     // {{{ readAll() 
    436     /** 
    437      * Read until the socket closes. THIS FUNCTION WILL NOT EXIT if the 
    438      * socket is in blocking mode until the socket closes. 
    439      * 
    440      * @access public 
    441      * @return All data until the socket closes, or a PEAR_Error if 
    442      *         not connected. 
     474        } 
     475        return $line; 
     476    } 
     477 
     478    /** 
     479     * Read until the socket closes, or until there is no more data in 
     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). 
     485     * 
     486     * @access public 
     487     * 
     488     * @return string  All data until the socket closes, or a PEAR_Error if 
     489     *                 not connected. 
    443490     */ 
    444491    function readAll() 
    445492    { 
    446         if (is_resource($this->fp)) { 
    447             $data = ''; 
    448             while (!$this->eof()) 
    449                 $data .= $this->read($this->lineLength); 
    450             return $data; 
    451         } 
    452         return $this->raiseError("not connected"); 
    453     } 
    454     // }}} 
     493        if (!is_resource($this->fp)) { 
     494            return $this->raiseError('not connected'); 
     495        } 
     496 
     497        $data = ''; 
     498        while (!feof($this->fp)) { 
     499            $data .= @fread($this->fp, $this->lineLength); 
     500        } 
     501        return $data; 
     502    } 
     503 
     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    } 
    455575 
    456576} 
Note: See TracChangeset for help on using the changeset viewer.