Changeset 20998


Ignore:
Timestamp:
2011/06/30 18:55:05 (13 years ago)
Author:
Seasoft
Message:

#1380 (Net_SMTP と互換性のない Net_Socket が使用されている)
#1374 (依存ライブラリのアップデート)

  • Net_Socket 1.0.10
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/version-2_11-dev/data/module/Net/Socket.php

    r20116 r20998  
    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$ 
    21  
    22 if(!defined('SOCKET_PHP_DIR')) { 
    23     $SOCKET_PHP_DIR = realpath(dirname( __FILE__)); 
    24     define("SOCKET_PHP_DIR", $SOCKET_PHP_DIR);   
    25 } 
    26 require_once SOCKET_PHP_DIR. '/../PEAR.php'; 
    27  
    28 define('NET_SOCKET_READ',  1); 
     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$ 
     27 * @link      http://pear.php.net/packages/Net_Socket 
     28 */ 
     29 
     30require_once 'PEAR.php'; 
     31 
     32define('NET_SOCKET_READ', 1); 
    2933define('NET_SOCKET_WRITE', 2); 
    30 define('NET_SOCKET_ERROR', 3); 
     34define('NET_SOCKET_ERROR', 4); 
    3135 
    3236/** 
    3337 * Generalized Socket class. 
    3438 * 
    35  * @version 1.1 
    36  * @author Stig Bakken <ssb@php.net> 
    37  * @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 
    3846 */ 
    39 class Net_Socket extends PEAR { 
    40  
     47class Net_Socket extends PEAR 
     48{ 
    4149    /** 
    4250     * Socket file pointer. 
     
    8492 
    8593    /** 
     94     * The string to use as a newline terminator. Usually "\r\n" or "\n". 
     95     * @var string $newline 
     96     */ 
     97    var $newline = "\r\n"; 
     98 
     99    /** 
    86100     * Connect to the specified port. If called when the socket is 
    87101     * already connected, it disconnects and connects again. 
    88102     * 
    89      * @param string  $addr        IP address or host name. 
    90      * @param integer $port        TCP port number. 
    91      * @param boolean $persistent  (optional) Whether the connection is 
    92      *                             persistent (kept open between requests 
    93      *                             by the web server). 
    94      * @param integer $timeout     (optional) How long to wait for data. 
    95      * @param array   $options     See options for stream_context_create. 
     103     * @param string  $addr       IP address or host name. 
     104     * @param integer $port       TCP port number. 
     105     * @param boolean $persistent (optional) Whether the connection is 
     106     *                            persistent (kept open between requests 
     107     *                            by the web server). 
     108     * @param integer $timeout    (optional) How long to wait for data. 
     109     * @param array   $options    See options for stream_context_create. 
    96110     * 
    97111     * @access public 
     
    99113     * @return boolean | PEAR_Error  True on success or a PEAR_Error on failure. 
    100114     */ 
    101     function connect($addr, $port = 0, $persistent = null, $timeout = null, $options = null) 
     115    function connect($addr, $port = 0, $persistent = null, 
     116                     $timeout = null, $options = null) 
    102117    { 
    103118        if (is_resource($this->fp)) { 
     
    126141 
    127142        $openfunc = $this->persistent ? 'pfsockopen' : 'fsockopen'; 
    128         $errno = 0; 
    129         $errstr = ''; 
     143        $errno    = 0; 
     144        $errstr   = ''; 
     145 
     146        $old_track_errors = @ini_set('track_errors', 1); 
     147 
    130148        if ($options && function_exists('stream_context_create')) { 
    131149            if ($this->timeout) { 
     
    135153            } 
    136154            $context = stream_context_create($options); 
    137             $fp = @$openfunc($this->addr, $this->port, $errno, $errstr, $timeout, $context); 
     155 
     156            // Since PHP 5 fsockopen doesn't allow context specification 
     157            if (function_exists('stream_socket_client')) { 
     158                $flags = STREAM_CLIENT_CONNECT; 
     159 
     160                if ($this->persistent) { 
     161                    $flags = STREAM_CLIENT_PERSISTENT; 
     162                } 
     163 
     164                $addr = $this->addr . ':' . $this->port; 
     165                $fp   = stream_socket_client($addr, $errno, $errstr, 
     166                                             $timeout, $flags, $context); 
     167            } else { 
     168                $fp = @$openfunc($this->addr, $this->port, $errno, 
     169                                 $errstr, $timeout, $context); 
     170            } 
    138171        } else { 
    139172            if ($this->timeout) { 
    140                 $fp = @$openfunc($this->addr, $this->port, $errno, $errstr, $this->timeout); 
     173                $fp = @$openfunc($this->addr, $this->port, $errno, 
     174                                 $errstr, $this->timeout); 
    141175            } else { 
    142176                $fp = @$openfunc($this->addr, $this->port, $errno, $errstr); 
     
    145179 
    146180        if (!$fp) { 
     181            if ($errno == 0 && !strlen($errstr) && isset($php_errormsg)) { 
     182                $errstr = $php_errormsg; 
     183            } 
     184            @ini_set('track_errors', $old_track_errors); 
    147185            return $this->raiseError($errstr, $errno); 
    148186        } 
    149187 
     188        @ini_set('track_errors', $old_track_errors); 
    150189        $this->fp = $fp; 
    151190 
     
    157196     * 
    158197     * @access public 
    159      * @return mixed true on success or an error object otherwise 
     198     * @return mixed true on success or a PEAR_Error instance otherwise 
    160199     */ 
    161200    function disconnect() 
     
    167206        @fclose($this->fp); 
    168207        $this->fp = null; 
     208        return true; 
     209    } 
     210 
     211    /** 
     212     * Set the newline character/sequence to use. 
     213     * 
     214     * @param string $newline  Newline character(s) 
     215     * @return boolean True 
     216     */ 
     217    function setNewline($newline) 
     218    { 
     219        $this->newline = $newline; 
    169220        return true; 
    170221    } 
     
    187238     * is data for blocking sockets. 
    188239     * 
    189      * @param boolean $mode  True for blocking sockets, false for nonblocking. 
    190      * @access public 
    191      * @return mixed true on success or an error object otherwise 
     240     * @param boolean $mode True for blocking sockets, false for nonblocking. 
     241     * 
     242     * @access public 
     243     * @return mixed true on success or a PEAR_Error instance otherwise 
    192244     */ 
    193245    function setBlocking($mode) 
     
    198250 
    199251        $this->blocking = $mode; 
    200         socket_set_blocking($this->fp, $this->blocking); 
     252        stream_set_blocking($this->fp, (int)$this->blocking); 
    201253        return true; 
    202254    } 
     
    206258     * expressed in the sum of seconds and microseconds 
    207259     * 
    208      * @param integer $seconds  Seconds. 
    209      * @param integer $microseconds  Microseconds. 
    210      * @access public 
    211      * @return mixed true on success or an error object otherwise 
     260     * @param integer $seconds      Seconds. 
     261     * @param integer $microseconds Microseconds. 
     262     * 
     263     * @access public 
     264     * @return mixed true on success or a PEAR_Error instance otherwise 
    212265     */ 
    213266    function setTimeout($seconds, $microseconds) 
     
    218271 
    219272        return socket_set_timeout($this->fp, $seconds, $microseconds); 
     273    } 
     274 
     275    /** 
     276     * Sets the file buffering size on the stream. 
     277     * See php's stream_set_write_buffer for more information. 
     278     * 
     279     * @param integer $size Write buffer size. 
     280     * 
     281     * @access public 
     282     * @return mixed on success or an PEAR_Error object otherwise 
     283     */ 
     284    function setWriteBuffer($size) 
     285    { 
     286        if (!is_resource($this->fp)) { 
     287            return $this->raiseError('not connected'); 
     288        } 
     289 
     290        $returned = stream_set_write_buffer($this->fp, $size); 
     291        if ($returned == 0) { 
     292            return true; 
     293        } 
     294        return $this->raiseError('Cannot set write buffer.'); 
    220295    } 
    221296 
     
    232307     * 
    233308     * @access public 
    234      * @return mixed Array containing information about existing socket resource or an error object otherwise 
     309     * @return mixed Array containing information about existing socket 
     310     *               resource or a PEAR_Error instance otherwise 
    235311     */ 
    236312    function getStatus() 
     
    245321    /** 
    246322     * Get a specified line of data 
     323     * 
     324     * @param int $size ?? 
    247325     * 
    248326     * @access public 
     
    250328     *         not connected. 
    251329     */ 
    252     function gets($size) 
    253     { 
    254         if (!is_resource($this->fp)) { 
    255             return $this->raiseError('not connected'); 
    256         } 
    257  
    258         return @fgets($this->fp, $size); 
     330    function gets($size = null) 
     331    { 
     332        if (!is_resource($this->fp)) { 
     333            return $this->raiseError('not connected'); 
     334        } 
     335 
     336        if (is_null($size)) { 
     337            return @fgets($this->fp); 
     338        } else { 
     339            return @fgets($this->fp, $size); 
     340        } 
    259341    } 
    260342 
     
    265347     * beforehand, this is definitely the way to go. 
    266348     * 
    267      * @param integer $size  The number of bytes to read from the socket. 
     349     * @param integer $size The number of bytes to read from the socket. 
     350     * 
    268351     * @access public 
    269352     * @return $size bytes of data from the socket, or a PEAR_Error if 
     
    282365     * Write a specified amount of data. 
    283366     * 
    284      * @param string  $data       Data to write. 
    285      * @param integer $blocksize  Amount of data to write at once. 
    286      *                            NULL means all at once. 
    287      * 
    288      * @access public 
    289      * @return mixed true on success or an error object otherwise 
     367     * @param string  $data      Data to write. 
     368     * @param integer $blocksize Amount of data to write at once. 
     369     *                           NULL means all at once. 
     370     * 
     371     * @access public 
     372     * @return mixed If the socket is not connected, returns an instance of 
     373     *               PEAR_Error 
     374     *               If the write succeeds, returns the number of bytes written 
     375     *               If the write fails, returns false. 
    290376     */ 
    291377    function write($data, $blocksize = null) 
     
    296382 
    297383        if (is_null($blocksize) && !OS_WINDOWS) { 
    298             return fwrite($this->fp, $data); 
     384            return @fwrite($this->fp, $data); 
    299385        } else { 
    300386            if (is_null($blocksize)) { 
     
    302388            } 
    303389 
    304             $pos = 0; 
     390            $pos  = 0; 
    305391            $size = strlen($data); 
    306392            while ($pos < $size) { 
    307393                $written = @fwrite($this->fp, substr($data, $pos, $blocksize)); 
    308                 if ($written === false) { 
    309                     return false; 
     394                if (!$written) { 
     395                    return $written; 
    310396                } 
    311397                $pos += $written; 
     
    317403 
    318404    /** 
    319      * Write a line of data to the socket, followed by a trailing "\r\n". 
     405     * Write a line of data to the socket, followed by a trailing newline. 
     406     * 
     407     * @param string $data Data to write 
    320408     * 
    321409     * @access public 
     
    328416        } 
    329417 
    330         return fwrite($this->fp, $data . "\r\n"); 
     418        return fwrite($this->fp, $data . $this->newline); 
    331419    } 
    332420 
     
    334422     * Tests for end-of-file on a socket descriptor. 
    335423     * 
     424     * Also returns true if the socket is disconnected. 
     425     * 
    336426     * @access public 
    337427     * @return bool 
     
    339429    function eof() 
    340430    { 
    341         return (is_resource($this->fp) && feof($this->fp)); 
     431        return (!is_resource($this->fp) || feof($this->fp)); 
    342432    } 
    343433 
     
    407497 
    408498        $string = ''; 
    409         while (($char = @fread($this->fp, 1)) != "\x00")  { 
     499        while (($char = @fread($this->fp, 1)) != "\x00") { 
    410500            $string .= $char; 
    411501        } 
     
    414504 
    415505    /** 
    416      * Reads an IP Address and returns it in a dot formated string 
    417      * 
    418      * @access public 
    419      * @return Dot formated string, or a PEAR_Error if 
     506     * Reads an IP Address and returns it in a dot formatted string 
     507     * 
     508     * @access public 
     509     * @return Dot formatted string, or a PEAR_Error if 
    420510     *         not connected. 
    421511     */ 
     
    427517 
    428518        $buf = @fread($this->fp, 4); 
    429         return sprintf("%s.%s.%s.%s", ord($buf[0]), ord($buf[1]), 
     519        return sprintf('%d.%d.%d.%d', ord($buf[0]), ord($buf[1]), 
    430520                       ord($buf[2]), ord($buf[3])); 
    431521    } 
     
    447537 
    448538        $line = ''; 
     539 
    449540        $timeout = time() + $this->timeout; 
    450          
     541 
    451542        while (!feof($this->fp) && (!$this->timeout || time() < $timeout)) { 
    452543            $line .= @fgets($this->fp, $this->lineLength); 
    453544            if (substr($line, -1) == "\n") { 
    454                 return rtrim($line, "\r\n"); 
    455             } 
    456         } 
    457  
     545                return rtrim($line, $this->newline); 
     546            } 
     547        } 
    458548        return $line; 
    459549    } 
    460      
     550 
    461551    /** 
    462552     * Read until the socket closes, or until there is no more data in 
     
    489579     * with a timeout specified by tv_sec and tv_usec. 
    490580     * 
    491      * @param integer $state    Which of read/write/error to check for. 
    492      * @param integer $tv_sec   Number of seconds for timeout. 
    493      * @param integer $tv_usec  Number of microseconds for timeout. 
     581     * @param integer $state   Which of read/write/error to check for. 
     582     * @param integer $tv_sec  Number of seconds for timeout. 
     583     * @param integer $tv_usec Number of microseconds for timeout. 
    494584     * 
    495585     * @access public 
     
    503593        } 
    504594 
    505         $read = null; 
    506         $write = null; 
     595        $read   = null; 
     596        $write  = null; 
    507597        $except = null; 
    508598        if ($state & NET_SOCKET_READ) { 
     
    515605            $except[] = $this->fp; 
    516606        } 
    517         if (false === ($sr = stream_select($read, $write, $except, $tv_sec, $tv_usec))) { 
     607        if (false === ($sr = stream_select($read, $write, $except, 
     608                                          $tv_sec, $tv_usec))) { 
    518609            return false; 
    519610        } 
     
    532623    } 
    533624 
     625    /** 
     626     * Turns encryption on/off on a connected socket. 
     627     * 
     628     * @param bool    $enabled Set this parameter to true to enable encryption 
     629     *                         and false to disable encryption. 
     630     * @param integer $type    Type of encryption. See stream_socket_enable_crypto() 
     631     *                         for values. 
     632     * 
     633     * @see    http://se.php.net/manual/en/function.stream-socket-enable-crypto.php 
     634     * @access public 
     635     * @return false on error, true on success and 0 if there isn't enough data 
     636     *         and the user should try again (non-blocking sockets only). 
     637     *         A PEAR_Error object is returned if the socket is not 
     638     *         connected 
     639     */ 
     640    function enableCrypto($enabled, $type) 
     641    { 
     642        if (version_compare(phpversion(), "5.1.0", ">=")) { 
     643            if (!is_resource($this->fp)) { 
     644                return $this->raiseError('not connected'); 
     645            } 
     646            return @stream_socket_enable_crypto($this->fp, $enabled, $type); 
     647        } else { 
     648            $msg = 'Net_Socket::enableCrypto() requires php version >= 5.1.0'; 
     649            return $this->raiseError($msg); 
     650        } 
     651    } 
     652 
    534653} 
Note: See TracChangeset for help on using the changeset viewer.