Changeset 20998
- Timestamp:
- 2011/06/30 18:55:05 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/version-2_11-dev/data/module/Net/Socket.php
r20116 r20998 1 1 <?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 30 require_once 'PEAR.php'; 31 32 define('NET_SOCKET_READ', 1); 29 33 define('NET_SOCKET_WRITE', 2); 30 define('NET_SOCKET_ERROR', 3);34 define('NET_SOCKET_ERROR', 4); 31 35 32 36 /** 33 37 * Generalized Socket class. 34 38 * 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 38 46 */ 39 class Net_Socket extends PEAR {40 47 class Net_Socket extends PEAR 48 { 41 49 /** 42 50 * Socket file pointer. … … 84 92 85 93 /** 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 /** 86 100 * Connect to the specified port. If called when the socket is 87 101 * already connected, it disconnects and connects again. 88 102 * 89 * @param string $addr 90 * @param integer $port 91 * @param boolean $persistent 92 * 93 * 94 * @param integer $timeout 95 * @param array $options 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. 96 110 * 97 111 * @access public … … 99 113 * @return boolean | PEAR_Error True on success or a PEAR_Error on failure. 100 114 */ 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) 102 117 { 103 118 if (is_resource($this->fp)) { … … 126 141 127 142 $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 130 148 if ($options && function_exists('stream_context_create')) { 131 149 if ($this->timeout) { … … 135 153 } 136 154 $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 } 138 171 } else { 139 172 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); 141 175 } else { 142 176 $fp = @$openfunc($this->addr, $this->port, $errno, $errstr); … … 145 179 146 180 if (!$fp) { 181 if ($errno == 0 && !strlen($errstr) && isset($php_errormsg)) { 182 $errstr = $php_errormsg; 183 } 184 @ini_set('track_errors', $old_track_errors); 147 185 return $this->raiseError($errstr, $errno); 148 186 } 149 187 188 @ini_set('track_errors', $old_track_errors); 150 189 $this->fp = $fp; 151 190 … … 157 196 * 158 197 * @access public 159 * @return mixed true on success or a n error objectotherwise198 * @return mixed true on success or a PEAR_Error instance otherwise 160 199 */ 161 200 function disconnect() … … 167 206 @fclose($this->fp); 168 207 $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; 169 220 return true; 170 221 } … … 187 238 * is data for blocking sockets. 188 239 * 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 192 244 */ 193 245 function setBlocking($mode) … … 198 250 199 251 $this->blocking = $mode; 200 s ocket_set_blocking($this->fp,$this->blocking);252 stream_set_blocking($this->fp, (int)$this->blocking); 201 253 return true; 202 254 } … … 206 258 * expressed in the sum of seconds and microseconds 207 259 * 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 212 265 */ 213 266 function setTimeout($seconds, $microseconds) … … 218 271 219 272 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.'); 220 295 } 221 296 … … 232 307 * 233 308 * @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 235 311 */ 236 312 function getStatus() … … 245 321 /** 246 322 * Get a specified line of data 323 * 324 * @param int $size ?? 247 325 * 248 326 * @access public … … 250 328 * not connected. 251 329 */ 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 } 259 341 } 260 342 … … 265 347 * beforehand, this is definitely the way to go. 266 348 * 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 * 268 351 * @access public 269 352 * @return $size bytes of data from the socket, or a PEAR_Error if … … 282 365 * Write a specified amount of data. 283 366 * 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. 290 376 */ 291 377 function write($data, $blocksize = null) … … 296 382 297 383 if (is_null($blocksize) && !OS_WINDOWS) { 298 return fwrite($this->fp, $data);384 return @fwrite($this->fp, $data); 299 385 } else { 300 386 if (is_null($blocksize)) { … … 302 388 } 303 389 304 $pos = 0;390 $pos = 0; 305 391 $size = strlen($data); 306 392 while ($pos < $size) { 307 393 $written = @fwrite($this->fp, substr($data, $pos, $blocksize)); 308 if ( $written === false) {309 return false;394 if (!$written) { 395 return $written; 310 396 } 311 397 $pos += $written; … … 317 403 318 404 /** 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 320 408 * 321 409 * @access public … … 328 416 } 329 417 330 return fwrite($this->fp, $data . "\r\n");418 return fwrite($this->fp, $data . $this->newline); 331 419 } 332 420 … … 334 422 * Tests for end-of-file on a socket descriptor. 335 423 * 424 * Also returns true if the socket is disconnected. 425 * 336 426 * @access public 337 427 * @return bool … … 339 429 function eof() 340 430 { 341 return ( is_resource($this->fp) &&feof($this->fp));431 return (!is_resource($this->fp) || feof($this->fp)); 342 432 } 343 433 … … 407 497 408 498 $string = ''; 409 while (($char = @fread($this->fp, 1)) != "\x00") 499 while (($char = @fread($this->fp, 1)) != "\x00") { 410 500 $string .= $char; 411 501 } … … 414 504 415 505 /** 416 * Reads an IP Address and returns it in a dot format ed string417 * 418 * @access public 419 * @return Dot format ed string, or a PEAR_Error if506 * 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 420 510 * not connected. 421 511 */ … … 427 517 428 518 $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]), 430 520 ord($buf[2]), ord($buf[3])); 431 521 } … … 447 537 448 538 $line = ''; 539 449 540 $timeout = time() + $this->timeout; 450 541 451 542 while (!feof($this->fp) && (!$this->timeout || time() < $timeout)) { 452 543 $line .= @fgets($this->fp, $this->lineLength); 453 544 if (substr($line, -1) == "\n") { 454 return rtrim($line, "\r\n"); 455 } 456 } 457 545 return rtrim($line, $this->newline); 546 } 547 } 458 548 return $line; 459 549 } 460 550 461 551 /** 462 552 * Read until the socket closes, or until there is no more data in … … 489 579 * with a timeout specified by tv_sec and tv_usec. 490 580 * 491 * @param integer $state 492 * @param integer $tv_sec 493 * @param integer $tv_usec 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. 494 584 * 495 585 * @access public … … 503 593 } 504 594 505 $read = null;506 $write = null;595 $read = null; 596 $write = null; 507 597 $except = null; 508 598 if ($state & NET_SOCKET_READ) { … … 515 605 $except[] = $this->fp; 516 606 } 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))) { 518 609 return false; 519 610 } … … 532 623 } 533 624 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 534 653 }
Note: See TracChangeset
for help on using the changeset viewer.