Mercurial > hg > rc1
comparison vendor/pear/net_socket/Net/Socket.php @ 0:1e000243b222
vanilla 1.3.3 distro, I hope
| author | Charlie Root |
|---|---|
| date | Thu, 04 Jan 2018 15:50:29 -0500 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:1e000243b222 |
|---|---|
| 1 <?php | |
| 2 /** | |
| 3 * Net_Socket | |
| 4 * | |
| 5 * PHP Version 5 | |
| 6 * | |
| 7 * LICENSE: | |
| 8 * | |
| 9 * Copyright (c) 1997-2017 The PHP Group | |
| 10 * All rights reserved. | |
| 11 * | |
| 12 * Redistribution and use in source and binary forms, with or without | |
| 13 * modification, are permitted provided that the following conditions | |
| 14 * are met: | |
| 15 * | |
| 16 * o Redistributions of source code must retain the above copyright | |
| 17 * notice, this list of conditions and the following disclaimer. | |
| 18 * o Redistributions in binary form must reproduce the above copyright | |
| 19 * notice, this list of conditions and the following disclaimer in the | |
| 20 * documentation and/or other materials provided with the distribution. | |
| 21 * | |
| 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 26 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 33 * | |
| 34 * @category Net | |
| 35 * @package Net_Socket | |
| 36 * @author Stig Bakken <ssb@php.net> | |
| 37 * @author Chuck Hagenbuch <chuck@horde.org> | |
| 38 * @copyright 1997-2017 The PHP Group | |
| 39 * @license http://opensource.org/licenses/bsd-license.php BSD-2-Clause | |
| 40 * @link http://pear.php.net/packages/Net_Socket | |
| 41 */ | |
| 42 | |
| 43 require_once 'PEAR.php'; | |
| 44 | |
| 45 define('NET_SOCKET_READ', 1); | |
| 46 define('NET_SOCKET_WRITE', 2); | |
| 47 define('NET_SOCKET_ERROR', 4); | |
| 48 | |
| 49 /** | |
| 50 * Generalized Socket class. | |
| 51 * | |
| 52 * @category Net | |
| 53 * @package Net_Socket | |
| 54 * @author Stig Bakken <ssb@php.net> | |
| 55 * @author Chuck Hagenbuch <chuck@horde.org> | |
| 56 * @copyright 1997-2017 The PHP Group | |
| 57 * @license http://opensource.org/licenses/bsd-license.php BSD-2-Clause | |
| 58 * @link http://pear.php.net/packages/Net_Socket | |
| 59 */ | |
| 60 class Net_Socket extends PEAR | |
| 61 { | |
| 62 /** | |
| 63 * Socket file pointer. | |
| 64 * @var resource $fp | |
| 65 */ | |
| 66 public $fp = null; | |
| 67 | |
| 68 /** | |
| 69 * Whether the socket is blocking. Defaults to true. | |
| 70 * @var boolean $blocking | |
| 71 */ | |
| 72 public $blocking = true; | |
| 73 | |
| 74 /** | |
| 75 * Whether the socket is persistent. Defaults to false. | |
| 76 * @var boolean $persistent | |
| 77 */ | |
| 78 public $persistent = false; | |
| 79 | |
| 80 /** | |
| 81 * The IP address to connect to. | |
| 82 * @var string $addr | |
| 83 */ | |
| 84 public $addr = ''; | |
| 85 | |
| 86 /** | |
| 87 * The port number to connect to. | |
| 88 * @var integer $port | |
| 89 */ | |
| 90 public $port = 0; | |
| 91 | |
| 92 /** | |
| 93 * Number of seconds to wait on socket operations before assuming | |
| 94 * there's no more data. Defaults to no timeout. | |
| 95 * @var integer|float $timeout | |
| 96 */ | |
| 97 public $timeout = null; | |
| 98 | |
| 99 /** | |
| 100 * Number of bytes to read at a time in readLine() and | |
| 101 * readAll(). Defaults to 2048. | |
| 102 * @var integer $lineLength | |
| 103 */ | |
| 104 public $lineLength = 2048; | |
| 105 | |
| 106 /** | |
| 107 * The string to use as a newline terminator. Usually "\r\n" or "\n". | |
| 108 * @var string $newline | |
| 109 */ | |
| 110 public $newline = "\r\n"; | |
| 111 | |
| 112 /** | |
| 113 * Connect to the specified port. If called when the socket is | |
| 114 * already connected, it disconnects and connects again. | |
| 115 * | |
| 116 * @param string $addr IP address or host name (may be with protocol prefix). | |
| 117 * @param integer $port TCP port number. | |
| 118 * @param boolean $persistent (optional) Whether the connection is | |
| 119 * persistent (kept open between requests | |
| 120 * by the web server). | |
| 121 * @param integer $timeout (optional) Connection socket timeout. | |
| 122 * @param array $options See options for stream_context_create. | |
| 123 * | |
| 124 * @access public | |
| 125 * | |
| 126 * @return boolean|PEAR_Error True on success or a PEAR_Error on failure. | |
| 127 */ | |
| 128 public function connect( | |
| 129 $addr, | |
| 130 $port = 0, | |
| 131 $persistent = null, | |
| 132 $timeout = null, | |
| 133 $options = null | |
| 134 ) { | |
| 135 if (is_resource($this->fp)) { | |
| 136 @fclose($this->fp); | |
| 137 $this->fp = null; | |
| 138 } | |
| 139 | |
| 140 if (!$addr) { | |
| 141 return $this->raiseError('$addr cannot be empty'); | |
| 142 } else { | |
| 143 if (strspn($addr, ':.0123456789') === strlen($addr)) { | |
| 144 $this->addr = strpos($addr, ':') !== false ? '[' . $addr . ']' : $addr; | |
| 145 } else { | |
| 146 $this->addr = $addr; | |
| 147 } | |
| 148 } | |
| 149 | |
| 150 $this->port = $port % 65536; | |
| 151 | |
| 152 if ($persistent !== null) { | |
| 153 $this->persistent = $persistent; | |
| 154 } | |
| 155 | |
| 156 $openfunc = $this->persistent ? 'pfsockopen' : 'fsockopen'; | |
| 157 $errno = 0; | |
| 158 $errstr = ''; | |
| 159 | |
| 160 $old_track_errors = @ini_set('track_errors', 1); | |
| 161 | |
| 162 if ($timeout <= 0) { | |
| 163 $timeout = @ini_get('default_socket_timeout'); | |
| 164 } | |
| 165 | |
| 166 if ($options && function_exists('stream_context_create')) { | |
| 167 $context = stream_context_create($options); | |
| 168 | |
| 169 // Since PHP 5 fsockopen doesn't allow context specification | |
| 170 if (function_exists('stream_socket_client')) { | |
| 171 $flags = STREAM_CLIENT_CONNECT; | |
| 172 | |
| 173 if ($this->persistent) { | |
| 174 $flags = STREAM_CLIENT_PERSISTENT; | |
| 175 } | |
| 176 | |
| 177 $addr = $this->addr . ':' . $this->port; | |
| 178 $fp = @stream_socket_client($addr, $errno, $errstr, | |
| 179 $timeout, $flags, $context); | |
| 180 } else { | |
| 181 $fp = @$openfunc($this->addr, $this->port, $errno, | |
| 182 $errstr, $timeout, $context); | |
| 183 } | |
| 184 } else { | |
| 185 $fp = @$openfunc($this->addr, $this->port, $errno, $errstr, $timeout); | |
| 186 } | |
| 187 | |
| 188 if (!$fp) { | |
| 189 if ($errno === 0 && !strlen($errstr) && isset($php_errormsg)) { | |
| 190 $errstr = $php_errormsg; | |
| 191 } | |
| 192 @ini_set('track_errors', $old_track_errors); | |
| 193 | |
| 194 return $this->raiseError($errstr, $errno); | |
| 195 } | |
| 196 | |
| 197 @ini_set('track_errors', $old_track_errors); | |
| 198 $this->fp = $fp; | |
| 199 $this->setTimeout(); | |
| 200 | |
| 201 return $this->setBlocking($this->blocking); | |
| 202 } | |
| 203 | |
| 204 /** | |
| 205 * Disconnects from the peer, closes the socket. | |
| 206 * | |
| 207 * @access public | |
| 208 * @return mixed true on success or a PEAR_Error instance otherwise | |
| 209 */ | |
| 210 public function disconnect() | |
| 211 { | |
| 212 if (!is_resource($this->fp)) { | |
| 213 return $this->raiseError('not connected'); | |
| 214 } | |
| 215 | |
| 216 @fclose($this->fp); | |
| 217 $this->fp = null; | |
| 218 | |
| 219 return true; | |
| 220 } | |
| 221 | |
| 222 /** | |
| 223 * Set the newline character/sequence to use. | |
| 224 * | |
| 225 * @param string $newline Newline character(s) | |
| 226 * @return boolean True | |
| 227 */ | |
| 228 public function setNewline($newline) | |
| 229 { | |
| 230 $this->newline = $newline; | |
| 231 | |
| 232 return true; | |
| 233 } | |
| 234 | |
| 235 /** | |
| 236 * Find out if the socket is in blocking mode. | |
| 237 * | |
| 238 * @access public | |
| 239 * @return boolean The current blocking mode. | |
| 240 */ | |
| 241 public function isBlocking() | |
| 242 { | |
| 243 return $this->blocking; | |
| 244 } | |
| 245 | |
| 246 /** | |
| 247 * Sets whether the socket connection should be blocking or | |
| 248 * not. A read call to a non-blocking socket will return immediately | |
| 249 * if there is no data available, whereas it will block until there | |
| 250 * is data for blocking sockets. | |
| 251 * | |
| 252 * @param boolean $mode True for blocking sockets, false for nonblocking. | |
| 253 * | |
| 254 * @access public | |
| 255 * @return mixed true on success or a PEAR_Error instance otherwise | |
| 256 */ | |
| 257 public function setBlocking($mode) | |
| 258 { | |
| 259 if (!is_resource($this->fp)) { | |
| 260 return $this->raiseError('not connected'); | |
| 261 } | |
| 262 | |
| 263 $this->blocking = $mode; | |
| 264 stream_set_blocking($this->fp, (int)$this->blocking); | |
| 265 | |
| 266 return true; | |
| 267 } | |
| 268 | |
| 269 /** | |
| 270 * Sets the timeout value on socket descriptor, | |
| 271 * expressed in the sum of seconds and microseconds | |
| 272 * | |
| 273 * @param integer $seconds Seconds. | |
| 274 * @param integer $microseconds Microseconds, optional. | |
| 275 * | |
| 276 * @access public | |
| 277 * @return mixed True on success or false on failure or | |
| 278 * a PEAR_Error instance when not connected | |
| 279 */ | |
| 280 public function setTimeout($seconds = null, $microseconds = null) | |
| 281 { | |
| 282 if (!is_resource($this->fp)) { | |
| 283 return $this->raiseError('not connected'); | |
| 284 } | |
| 285 | |
| 286 if ($seconds === null && $microseconds === null) { | |
| 287 $seconds = (int)$this->timeout; | |
| 288 $microseconds = (int)(($this->timeout - $seconds) * 1000000); | |
| 289 } else { | |
| 290 $this->timeout = $seconds + $microseconds / 1000000; | |
| 291 } | |
| 292 | |
| 293 if ($this->timeout > 0) { | |
| 294 return stream_set_timeout($this->fp, (int)$seconds, (int)$microseconds); | |
| 295 } else { | |
| 296 return false; | |
| 297 } | |
| 298 } | |
| 299 | |
| 300 /** | |
| 301 * Sets the file buffering size on the stream. | |
| 302 * See php's stream_set_write_buffer for more information. | |
| 303 * | |
| 304 * @param integer $size Write buffer size. | |
| 305 * | |
| 306 * @access public | |
| 307 * @return mixed on success or an PEAR_Error object otherwise | |
| 308 */ | |
| 309 public function setWriteBuffer($size) | |
| 310 { | |
| 311 if (!is_resource($this->fp)) { | |
| 312 return $this->raiseError('not connected'); | |
| 313 } | |
| 314 | |
| 315 $returned = stream_set_write_buffer($this->fp, $size); | |
| 316 if ($returned === 0) { | |
| 317 return true; | |
| 318 } | |
| 319 | |
| 320 return $this->raiseError('Cannot set write buffer.'); | |
| 321 } | |
| 322 | |
| 323 /** | |
| 324 * Returns information about an existing socket resource. | |
| 325 * Currently returns four entries in the result array: | |
| 326 * | |
| 327 * <p> | |
| 328 * timed_out (bool) - The socket timed out waiting for data<br> | |
| 329 * blocked (bool) - The socket was blocked<br> | |
| 330 * eof (bool) - Indicates EOF event<br> | |
| 331 * unread_bytes (int) - Number of bytes left in the socket buffer<br> | |
| 332 * </p> | |
| 333 * | |
| 334 * @access public | |
| 335 * @return mixed Array containing information about existing socket | |
| 336 * resource or a PEAR_Error instance otherwise | |
| 337 */ | |
| 338 public function getStatus() | |
| 339 { | |
| 340 if (!is_resource($this->fp)) { | |
| 341 return $this->raiseError('not connected'); | |
| 342 } | |
| 343 | |
| 344 return stream_get_meta_data($this->fp); | |
| 345 } | |
| 346 | |
| 347 /** | |
| 348 * Get a specified line of data | |
| 349 * | |
| 350 * @param int $size Reading ends when size - 1 bytes have been read, | |
| 351 * or a newline or an EOF (whichever comes first). | |
| 352 * If no size is specified, it will keep reading from | |
| 353 * the stream until it reaches the end of the line. | |
| 354 * | |
| 355 * @access public | |
| 356 * @return mixed $size bytes of data from the socket, or a PEAR_Error if | |
| 357 * not connected. If an error occurs, FALSE is returned. | |
| 358 */ | |
| 359 public function gets($size = null) | |
| 360 { | |
| 361 if (!is_resource($this->fp)) { | |
| 362 return $this->raiseError('not connected'); | |
| 363 } | |
| 364 | |
| 365 if (null === $size) { | |
| 366 return @fgets($this->fp); | |
| 367 } else { | |
| 368 return @fgets($this->fp, $size); | |
| 369 } | |
| 370 } | |
| 371 | |
| 372 /** | |
| 373 * Read a specified amount of data. This is guaranteed to return, | |
| 374 * and has the added benefit of getting everything in one fread() | |
| 375 * chunk; if you know the size of the data you're getting | |
| 376 * beforehand, this is definitely the way to go. | |
| 377 * | |
| 378 * @param integer $size The number of bytes to read from the socket. | |
| 379 * | |
| 380 * @access public | |
| 381 * @return string $size bytes of data from the socket, or a PEAR_Error if | |
| 382 * not connected. | |
| 383 */ | |
| 384 public function read($size) | |
| 385 { | |
| 386 if (!is_resource($this->fp)) { | |
| 387 return $this->raiseError('not connected'); | |
| 388 } | |
| 389 | |
| 390 return @fread($this->fp, $size); | |
| 391 } | |
| 392 | |
| 393 /** | |
| 394 * Write a specified amount of data. | |
| 395 * | |
| 396 * @param string $data Data to write. | |
| 397 * @param integer $blocksize Amount of data to write at once. | |
| 398 * NULL means all at once. | |
| 399 * | |
| 400 * @access public | |
| 401 * @return mixed If the socket is not connected, returns an instance of | |
| 402 * PEAR_Error. | |
| 403 * If the write succeeds, returns the number of bytes written. | |
| 404 * If the write fails, returns false. | |
| 405 * If the socket times out, returns an instance of PEAR_Error. | |
| 406 */ | |
| 407 public function write($data, $blocksize = null) | |
| 408 { | |
| 409 if (!is_resource($this->fp)) { | |
| 410 return $this->raiseError('not connected'); | |
| 411 } | |
| 412 | |
| 413 if (null === $blocksize && !OS_WINDOWS) { | |
| 414 $written = @fwrite($this->fp, $data); | |
| 415 | |
| 416 // Check for timeout or lost connection | |
| 417 if ($written === false) { | |
| 418 $meta_data = $this->getStatus(); | |
| 419 | |
| 420 if (!is_array($meta_data)) { | |
| 421 return $meta_data; // PEAR_Error | |
| 422 } | |
| 423 | |
| 424 if (!empty($meta_data['timed_out'])) { | |
| 425 return $this->raiseError('timed out'); | |
| 426 } | |
| 427 } | |
| 428 | |
| 429 return $written; | |
| 430 } else { | |
| 431 if (null === $blocksize) { | |
| 432 $blocksize = 1024; | |
| 433 } | |
| 434 | |
| 435 $pos = 0; | |
| 436 $size = strlen($data); | |
| 437 while ($pos < $size) { | |
| 438 $written = @fwrite($this->fp, substr($data, $pos, $blocksize)); | |
| 439 | |
| 440 // Check for timeout or lost connection | |
| 441 if ($written === false) { | |
| 442 $meta_data = $this->getStatus(); | |
| 443 | |
| 444 if (!is_array($meta_data)) { | |
| 445 return $meta_data; // PEAR_Error | |
| 446 } | |
| 447 | |
| 448 if (!empty($meta_data['timed_out'])) { | |
| 449 return $this->raiseError('timed out'); | |
| 450 } | |
| 451 | |
| 452 return $written; | |
| 453 } | |
| 454 | |
| 455 $pos += $written; | |
| 456 } | |
| 457 | |
| 458 return $pos; | |
| 459 } | |
| 460 } | |
| 461 | |
| 462 /** | |
| 463 * Write a line of data to the socket, followed by a trailing newline. | |
| 464 * | |
| 465 * @param string $data Data to write | |
| 466 * | |
| 467 * @access public | |
| 468 * @return mixed fwrite() result, or PEAR_Error when not connected | |
| 469 */ | |
| 470 public function writeLine($data) | |
| 471 { | |
| 472 if (!is_resource($this->fp)) { | |
| 473 return $this->raiseError('not connected'); | |
| 474 } | |
| 475 | |
| 476 return fwrite($this->fp, $data . $this->newline); | |
| 477 } | |
| 478 | |
| 479 /** | |
| 480 * Tests for end-of-file on a socket descriptor. | |
| 481 * | |
| 482 * Also returns true if the socket is disconnected. | |
| 483 * | |
| 484 * @access public | |
| 485 * @return bool | |
| 486 */ | |
| 487 public function eof() | |
| 488 { | |
| 489 return (!is_resource($this->fp) || feof($this->fp)); | |
| 490 } | |
| 491 | |
| 492 /** | |
| 493 * Reads a byte of data | |
| 494 * | |
| 495 * @access public | |
| 496 * @return integer 1 byte of data from the socket, or a PEAR_Error if | |
| 497 * not connected. | |
| 498 */ | |
| 499 public function readByte() | |
| 500 { | |
| 501 if (!is_resource($this->fp)) { | |
| 502 return $this->raiseError('not connected'); | |
| 503 } | |
| 504 | |
| 505 return ord(@fread($this->fp, 1)); | |
| 506 } | |
| 507 | |
| 508 /** | |
| 509 * Reads a word of data | |
| 510 * | |
| 511 * @access public | |
| 512 * @return integer 1 word of data from the socket, or a PEAR_Error if | |
| 513 * not connected. | |
| 514 */ | |
| 515 public function readWord() | |
| 516 { | |
| 517 if (!is_resource($this->fp)) { | |
| 518 return $this->raiseError('not connected'); | |
| 519 } | |
| 520 | |
| 521 $buf = @fread($this->fp, 2); | |
| 522 | |
| 523 return (ord($buf[0]) + (ord($buf[1]) << 8)); | |
| 524 } | |
| 525 | |
| 526 /** | |
| 527 * Reads an int of data | |
| 528 * | |
| 529 * @access public | |
| 530 * @return integer 1 int of data from the socket, or a PEAR_Error if | |
| 531 * not connected. | |
| 532 */ | |
| 533 public function readInt() | |
| 534 { | |
| 535 if (!is_resource($this->fp)) { | |
| 536 return $this->raiseError('not connected'); | |
| 537 } | |
| 538 | |
| 539 $buf = @fread($this->fp, 4); | |
| 540 | |
| 541 return (ord($buf[0]) + (ord($buf[1]) << 8) + | |
| 542 (ord($buf[2]) << 16) + (ord($buf[3]) << 24)); | |
| 543 } | |
| 544 | |
| 545 /** | |
| 546 * Reads a zero-terminated string of data | |
| 547 * | |
| 548 * @access public | |
| 549 * @return string, or a PEAR_Error if | |
| 550 * not connected. | |
| 551 */ | |
| 552 public function readString() | |
| 553 { | |
| 554 if (!is_resource($this->fp)) { | |
| 555 return $this->raiseError('not connected'); | |
| 556 } | |
| 557 | |
| 558 $string = ''; | |
| 559 while (($char = @fread($this->fp, 1)) !== "\x00") { | |
| 560 $string .= $char; | |
| 561 } | |
| 562 | |
| 563 return $string; | |
| 564 } | |
| 565 | |
| 566 /** | |
| 567 * Reads an IP Address and returns it in a dot formatted string | |
| 568 * | |
| 569 * @access public | |
| 570 * @return string Dot formatted string, or a PEAR_Error if | |
| 571 * not connected. | |
| 572 */ | |
| 573 public function readIPAddress() | |
| 574 { | |
| 575 if (!is_resource($this->fp)) { | |
| 576 return $this->raiseError('not connected'); | |
| 577 } | |
| 578 | |
| 579 $buf = @fread($this->fp, 4); | |
| 580 | |
| 581 return sprintf('%d.%d.%d.%d', ord($buf[0]), ord($buf[1]), | |
| 582 ord($buf[2]), ord($buf[3])); | |
| 583 } | |
| 584 | |
| 585 /** | |
| 586 * Read until either the end of the socket or a newline, whichever | |
| 587 * comes first. Strips the trailing newline from the returned data. | |
| 588 * | |
| 589 * @access public | |
| 590 * @return string All available data up to a newline, without that | |
| 591 * newline, or until the end of the socket, or a PEAR_Error if | |
| 592 * not connected. | |
| 593 */ | |
| 594 public function readLine() | |
| 595 { | |
| 596 if (!is_resource($this->fp)) { | |
| 597 return $this->raiseError('not connected'); | |
| 598 } | |
| 599 | |
| 600 $line = ''; | |
| 601 | |
| 602 $timeout = time() + $this->timeout; | |
| 603 | |
| 604 while (!feof($this->fp) && (!$this->timeout || time() < $timeout)) { | |
| 605 $line .= @fgets($this->fp, $this->lineLength); | |
| 606 if (substr($line, -1) == "\n") { | |
| 607 return rtrim($line, $this->newline); | |
| 608 } | |
| 609 } | |
| 610 | |
| 611 return $line; | |
| 612 } | |
| 613 | |
| 614 /** | |
| 615 * Read until the socket closes, or until there is no more data in | |
| 616 * the inner PHP buffer. If the inner buffer is empty, in blocking | |
| 617 * mode we wait for at least 1 byte of data. Therefore, in | |
| 618 * blocking mode, if there is no data at all to be read, this | |
| 619 * function will never exit (unless the socket is closed on the | |
| 620 * remote end). | |
| 621 * | |
| 622 * @access public | |
| 623 * | |
| 624 * @return string All data until the socket closes, or a PEAR_Error if | |
| 625 * not connected. | |
| 626 */ | |
| 627 public function readAll() | |
| 628 { | |
| 629 if (!is_resource($this->fp)) { | |
| 630 return $this->raiseError('not connected'); | |
| 631 } | |
| 632 | |
| 633 $data = ''; | |
| 634 $timeout = time() + $this->timeout; | |
| 635 | |
| 636 while (!feof($this->fp) && (!$this->timeout || time() < $timeout)) { | |
| 637 $data .= @fread($this->fp, $this->lineLength); | |
| 638 } | |
| 639 | |
| 640 return $data; | |
| 641 } | |
| 642 | |
| 643 /** | |
| 644 * Runs the equivalent of the select() system call on the socket | |
| 645 * with a timeout specified by tv_sec and tv_usec. | |
| 646 * | |
| 647 * @param integer $state Which of read/write/error to check for. | |
| 648 * @param integer $tv_sec Number of seconds for timeout. | |
| 649 * @param integer $tv_usec Number of microseconds for timeout. | |
| 650 * | |
| 651 * @access public | |
| 652 * @return False if select fails, integer describing which of read/write/error | |
| 653 * are ready, or PEAR_Error if not connected. | |
| 654 */ | |
| 655 public function select($state, $tv_sec, $tv_usec = 0) | |
| 656 { | |
| 657 if (!is_resource($this->fp)) { | |
| 658 return $this->raiseError('not connected'); | |
| 659 } | |
| 660 | |
| 661 $read = null; | |
| 662 $write = null; | |
| 663 $except = null; | |
| 664 if ($state & NET_SOCKET_READ) { | |
| 665 $read[] = $this->fp; | |
| 666 } | |
| 667 if ($state & NET_SOCKET_WRITE) { | |
| 668 $write[] = $this->fp; | |
| 669 } | |
| 670 if ($state & NET_SOCKET_ERROR) { | |
| 671 $except[] = $this->fp; | |
| 672 } | |
| 673 if (false === ($sr = stream_select($read, $write, $except, | |
| 674 $tv_sec, $tv_usec)) | |
| 675 ) { | |
| 676 return false; | |
| 677 } | |
| 678 | |
| 679 $result = 0; | |
| 680 if (count($read)) { | |
| 681 $result |= NET_SOCKET_READ; | |
| 682 } | |
| 683 if (count($write)) { | |
| 684 $result |= NET_SOCKET_WRITE; | |
| 685 } | |
| 686 if (count($except)) { | |
| 687 $result |= NET_SOCKET_ERROR; | |
| 688 } | |
| 689 | |
| 690 return $result; | |
| 691 } | |
| 692 | |
| 693 /** | |
| 694 * Turns encryption on/off on a connected socket. | |
| 695 * | |
| 696 * @param bool $enabled Set this parameter to true to enable encryption | |
| 697 * and false to disable encryption. | |
| 698 * @param integer $type Type of encryption. See stream_socket_enable_crypto() | |
| 699 * for values. | |
| 700 * | |
| 701 * @see http://se.php.net/manual/en/function.stream-socket-enable-crypto.php | |
| 702 * @access public | |
| 703 * @return false on error, true on success and 0 if there isn't enough data | |
| 704 * and the user should try again (non-blocking sockets only). | |
| 705 * A PEAR_Error object is returned if the socket is not | |
| 706 * connected | |
| 707 */ | |
| 708 public function enableCrypto($enabled, $type) | |
| 709 { | |
| 710 if (version_compare(phpversion(), '5.1.0', '>=')) { | |
| 711 if (!is_resource($this->fp)) { | |
| 712 return $this->raiseError('not connected'); | |
| 713 } | |
| 714 | |
| 715 return @stream_socket_enable_crypto($this->fp, $enabled, $type); | |
| 716 } else { | |
| 717 $msg = 'Net_Socket::enableCrypto() requires php version >= 5.1.0'; | |
| 718 | |
| 719 return $this->raiseError($msg); | |
| 720 } | |
| 721 } | |
| 722 | |
| 723 } |
