Changeset 10063

Show
Ignore:
Timestamp:
12/01/08 15:34:58 (1 month ago)
Author:
afz
Message:

Update PEAR::Net_Socket

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/jaws/html/libraries/pear/Net/Socket.php

    r8996 r10063  
    11<?php 
    2 // 
    3 // +----------------------------------------------------------------------+ 
    4 // | PHP Version 4                                                        | 
    5 // +----------------------------------------------------------------------+ 
    6 // | Copyright (c) 1997-2003 The PHP Group                                | 
    7 // +----------------------------------------------------------------------+ 
    8 // | This source file is subject to version 2.0 of the PHP license,       | 
    9 // | that is bundled with this package in the file LICENSE, and is        | 
    10 // | available at through the world-wide-web at                           | 
    11 // | http://www.php.net/license/2_02.txt.                                 | 
    12 // | If you did not receive a copy of the PHP license and are unable to   | 
    13 // | obtain it through the world-wide-web, please send a note to          | 
    14 // | license@php.net so we can mail you a copy immediately.               | 
    15 // +----------------------------------------------------------------------+ 
    16 // | Authors: Stig Bakken <ssb@php.net>                                   | 
    17 // |          Chuck Hagenbuch <chuck@horde.org>                           | 
    18 // +----------------------------------------------------------------------+ 
    19 // 
    20 // $Id: Socket.php,v 1.38 2008/02/15 18:24:17 chagenbu Exp $ 
     2/** 
     3 * Net_Socket 
     4 * 
     5 * PHP Version 4 
     6 * 
     7 * Copyright (c) 1997-2003 The PHP Group 
     8 * 
     9 * This source file is subject to version 2.0 of the PHP license, 
     10 * that is bundled with this package in the file LICENSE, and is 
     11 * available at through the world-wide-web at 
     12 * http://www.php.net/license/2_02.txt. 
     13 * If you did not receive a copy of the PHP license and are unable to 
     14 * obtain it through the world-wide-web, please send a note to 
     15 * license@php.net so we can mail you a copy immediately. 
     16 * 
     17 * Authors: Stig Bakken <ssb@php.net> 
     18 *          Chuck Hagenbuch <chuck@horde.org> 
     19 * 
     20 * @category  Net 
     21 * @package   Net_Socket 
     22 * @author    Stig Bakken <ssb@php.net> 
     23 * @author    Chuck Hagenbuch <chuck@horde.org> 
     24 * @copyright 1997-2003 The PHP Group 
     25 * @license   http://www.php.net/license/2_02.txt PHP 2.02 
     26 * @version   CVS: $Id: Socket.php,v 1.40 2008/12/01 15:25:01 chagenbu Exp $ 
     27 * @link      http://pear.php.net/packages/Net_Socket 
     28 */ 
    2129 
    2230require_once 'PEAR.php'; 
    2331 
    24 define('NET_SOCKET_READ', 1); 
     32define('NET_SOCKET_READ', 1); 
    2533define('NET_SOCKET_WRITE', 2); 
    2634define('NET_SOCKET_ERROR', 4); 
     
    2937 * Generalized Socket class. 
    3038 * 
    31  * @version 1.1 
    32  * @author Stig Bakken <ssb@php.net> 
    33  * @author Chuck Hagenbuch <chuck@horde.org> 
     39 * @category  Net 
     40 * @package   Net_Socket 
     41 * @author    Stig Bakken <ssb@php.net> 
     42 * @author    Chuck Hagenbuch <chuck@horde.org> 
     43 * @copyright 1997-2003 The PHP Group 
     44 * @license   http://www.php.net/license/2_02.txt PHP 2.02 
     45 * @link      http://pear.php.net/packages/Net_Socket 
    3446 */ 
    35 class Net_Socket extends PEAR { 
    36  
     47class Net_Socket extends PEAR 
     48
    3749    /** 
    3850     * Socket file pointer. 
     
    8395     * already connected, it disconnects and connects again. 
    8496     * 
    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. 
     97     * @param string  $addr       IP address or host name. 
     98     * @param integer $port       TCP port number. 
     99     * @param boolean $persistent (optional) Whether the connection is 
     100     *                            persistent (kept open between requests 
     101     *                            by the web server). 
     102     * @param integer $timeout    (optional) How long to wait for data. 
     103     * @param array   $options    See options for stream_context_create. 
    92104     * 
    93105     * @access public 
     
    95107     * @return boolean | PEAR_Error  True on success or a PEAR_Error on failure. 
    96108     */ 
    97     function connect($addr, $port = 0, $persistent = null, $timeout = null, $options = null) 
     109    function connect($addr, $port = 0, $persistent = null, 
     110                     $timeout = null, $options = null) 
    98111    { 
    99112        if (is_resource($this->fp)) { 
     
    125138 
    126139        $openfunc = $this->persistent ? 'pfsockopen' : 'fsockopen'; 
    127         $errno = 0; 
    128         $errstr = ''; 
     140        $errno    = 0; 
     141        $errstr   = ''; 
     142 
    129143        $old_track_errors = @ini_set('track_errors', 1); 
     144 
    130145        if ($options && function_exists('stream_context_create')) { 
    131146            if ($this->timeout) { 
     
    138153            // Since PHP 5 fsockopen doesn't allow context specification 
    139154            if (function_exists('stream_socket_client')) { 
    140                 $flags = $this->persistent ? STREAM_CLIENT_PERSISTENT : STREAM_CLIENT_CONNECT; 
     155                $flags = STREAM_CLIENT_CONNECT; 
     156 
     157                if ($this->persistent) { 
     158                    $flags = STREAM_CLIENT_PERSISTENT; 
     159                } 
     160 
    141161                $addr = $this->addr . ':' . $this->port; 
    142                 $fp = stream_socket_client($addr, $errno, $errstr, $timeout, $flags, $context); 
     162                $fp   = stream_socket_client($addr, $errno, $errstr, 
     163                                             $timeout, $flags, $context); 
    143164            } else { 
    144                 $fp = @$openfunc($this->addr, $this->port, $errno, $errstr, $timeout, $context); 
     165                $fp = @$openfunc($this->addr, $this->port, $errno, 
     166                                 $errstr, $timeout, $context); 
    145167            } 
    146168        } else { 
    147169            if ($this->timeout) { 
    148                 $fp = @$openfunc($this->addr, $this->port, $errno, $errstr, $this->timeout); 
     170                $fp = @$openfunc($this->addr, $this->port, $errno, 
     171                                 $errstr, $this->timeout); 
    149172            } else { 
    150173                $fp = @$openfunc($this->addr, $this->port, $errno, $errstr); 
     
    153176 
    154177        if (!$fp) { 
    155             if ($errno == 0 && isset($php_errormsg)) { 
     178            if ($errno == 0 && !strlen($errstr) && isset($php_errormsg)) { 
    156179                $errstr = $php_errormsg; 
    157180            } 
     
    200223     * is data for blocking sockets. 
    201224     * 
    202      * @param boolean $mode  True for blocking sockets, false for nonblocking. 
     225     * @param boolean $mode True for blocking sockets, false for nonblocking. 
     226     * 
    203227     * @access public 
    204228     * @return mixed true on success or a PEAR_Error instance otherwise 
     
    219243     * expressed in the sum of seconds and microseconds 
    220244     * 
    221      * @param integer $seconds  Seconds. 
    222      * @param integer $microseconds  Microseconds. 
     245     * @param integer $seconds      Seconds. 
     246     * @param integer $microseconds Microseconds. 
     247     * 
    223248     * @access public 
    224249     * @return mixed true on success or a PEAR_Error instance otherwise 
     
    237262     * See php's stream_set_write_buffer for more information. 
    238263     * 
    239      * @param integer $size     Write buffer size. 
     264     * @param integer $size Write buffer size. 
     265     * 
    240266     * @access public 
    241267     * @return mixed on success or an PEAR_Error object otherwise 
     
    266292     * 
    267293     * @access public 
    268      * @return mixed Array containing information about existing socket resource or a PEAR_Error instance otherwise 
     294     * @return mixed Array containing information about existing socket 
     295     *               resource or a PEAR_Error instance otherwise 
    269296     */ 
    270297    function getStatus() 
     
    279306    /** 
    280307     * Get a specified line of data 
     308     * 
     309     * @param int $size ?? 
    281310     * 
    282311     * @access public 
     
    299328     * beforehand, this is definitely the way to go. 
    300329     * 
    301      * @param integer $size  The number of bytes to read from the socket. 
     330     * @param integer $size The number of bytes to read from the socket. 
     331     * 
    302332     * @access public 
    303333     * @return $size bytes of data from the socket, or a PEAR_Error if 
     
    316346     * Write a specified amount of data. 
    317347     * 
    318      * @param string  $data       Data to write. 
    319      * @param integer $blocksize  Amount of data to write at once. 
    320      *                            NULL means all at once. 
    321      * 
    322      * @access public 
    323      * @return mixed If the socket is not connected, returns an instance of PEAR_Error 
     348     * @param string  $data      Data to write. 
     349     * @param integer $blocksize Amount of data to write at once. 
     350     *                           NULL means all at once. 
     351     * 
     352     * @access public 
     353     * @return mixed If the socket is not connected, returns an instance of 
     354     *               PEAR_Error 
    324355     *               If the write succeeds, returns the number of bytes written 
    325356     *               If the write fails, returns false. 
     
    338369            } 
    339370 
    340             $pos = 0; 
     371            $pos = 0; 
    341372            $size = strlen($data); 
    342373            while ($pos < $size) { 
     
    355386     * Write a line of data to the socket, followed by a trailing "\r\n". 
    356387     * 
     388     * @param string $data Data to write 
     389     * 
    357390     * @access public 
    358391     * @return mixed fputs result, or an error 
     
    445478 
    446479        $string = ''; 
    447         while (($char = @fread($this->fp, 1)) != "\x00")
     480        while (($char = @fread($this->fp, 1)) != "\x00")
    448481            $string .= $char; 
    449482        } 
     
    485518 
    486519        $line = ''; 
     520 
    487521        $timeout = time() + $this->timeout; 
     522 
    488523        while (!feof($this->fp) && (!$this->timeout || time() < $timeout)) { 
    489524            $line .= @fgets($this->fp, $this->lineLength); 
     
    525560     * with a timeout specified by tv_sec and tv_usec. 
    526561     * 
    527      * @param integer $state    Which of read/write/error to check for. 
    528      * @param integer $tv_sec  Number of seconds for timeout. 
    529      * @param integer $tv_usec Number of microseconds for timeout. 
     562     * @param integer $state   Which of read/write/error to check for. 
     563     * @param integer $tv_sec  Number of seconds for timeout. 
     564     * @param integer $tv_usec Number of microseconds for timeout. 
    530565     * 
    531566     * @access public 
     
    539574        } 
    540575 
    541         $read = null; 
    542         $write = null; 
     576        $read   = null; 
     577        $write = null; 
    543578        $except = null; 
    544579        if ($state & NET_SOCKET_READ) { 
     
    551586            $except[] = $this->fp; 
    552587        } 
    553         if (false === ($sr = stream_select($read, $write, $except, $tv_sec, $tv_usec))) { 
     588        if (false === ($sr = stream_select($read, $write, $except, 
     589                                          $tv_sec, $tv_usec))) { 
    554590            return false; 
    555591        } 
     
    571607     * Turns encryption on/off on a connected socket. 
    572608     * 
    573      * @param bool    $enabled  Set this parameter to true to enable encryption 
    574      *                          and false to disable encryption. 
    575      * @param integer $type     Type of encryption. See 
    576      *                          http://se.php.net/manual/en/function.stream-socket-enable-crypto.php for values. 
    577      * 
    578      * @access public 
    579      * @return false on error, true on success and 0 if there isn't enough data and the 
    580      *         user should try again (non-blocking sockets only). A PEAR_Error object 
    581      *         is returned if the socket is not connected 
     609     * @param bool    $enabled Set this parameter to true to enable encryption 
     610     *                         and false to disable encryption. 
     611     * @param integer $type    Type of encryption. See stream_socket_enable_crypto() 
     612     *                         for values. 
     613     * 
     614     * @see    http://se.php.net/manual/en/function.stream-socket-enable-crypto.php 
     615     * @access public 
     616     * @return false on error, true on success and 0 if there isn't enough data 
     617     *         and the user should try again (non-blocking sockets only). 
     618     *         A PEAR_Error object is returned if the socket is not 
     619     *         connected 
    582620     */ 
    583621    function enableCrypto($enabled, $type) 
     
    589627            return @stream_socket_enable_crypto($this->fp, $enabled, $type); 
    590628        } else { 
    591             return $this->raiseError('Net_Socket::enableCrypto() requires php version >= 5.1.0'); 
     629            $msg = 'Net_Socket::enableCrypto() requires php version >= 5.1.0'; 
     630            return $this->raiseError($msg); 
    592631        } 
    593632    }