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