123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- <?php
-
-
-
- require_once(dirname(__FILE__) . '/compatibility.php');
-
-
-
- class SimpleStickyError {
- private $error = 'Constructor not chained';
-
-
-
- function __construct() {
- $this->clearError();
- }
-
-
-
- function isError() {
- return ($this->error != '');
- }
-
-
-
- function getError() {
- return $this->error;
- }
-
-
-
- function setError($error) {
- $this->error = $error;
- }
-
-
-
- function clearError() {
- $this->setError('');
- }
- }
-
- class SimpleFileSocket extends SimpleStickyError {
- private $handle;
- private $is_open = false;
- private $sent = '';
- private $block_size;
-
-
-
- function __construct($file, $block_size = 1024) {
- parent::__construct();
- if (! ($this->handle = $this->openFile($file, $error))) {
- $file_string = $file->asString();
- $this->setError("Cannot open [$file_string] with [$error]");
- return;
- }
- $this->is_open = true;
- $this->block_size = $block_size;
- }
-
-
-
- function write($message) {
- return true;
- }
-
-
-
- function read() {
- $raw = @fread($this->handle, $this->block_size);
- if ($raw === false) {
- $this->setError('Cannot read from socket');
- $this->close();
- }
- return $raw;
- }
-
-
-
- function isOpen() {
- return $this->is_open;
- }
-
-
-
- function close() {
- if (!$this->is_open) return false;
- $this->is_open = false;
- return fclose($this->handle);
- }
-
-
-
- function getSent() {
- return $this->sent;
- }
-
-
-
- protected function openFile($file, &$error) {
- return @fopen($file->asString(), 'r');
- }
- }
-
-
- class SimpleSocket extends SimpleStickyError {
- private $handle;
- private $is_open = false;
- private $sent = '';
- private $lock_size;
-
-
-
- function __construct($host, $port, $timeout, $block_size = 255) {
- parent::__construct();
- if (! ($this->handle = $this->openSocket($host, $port, $error_number, $error, $timeout))) {
- $this->setError("Cannot open [$host:$port] with [$error] within [$timeout] seconds");
- return;
- }
- $this->is_open = true;
- $this->block_size = $block_size;
- SimpleTestCompatibility::setTimeout($this->handle, $timeout);
- }
-
-
-
- function write($message) {
- if ($this->isError() || ! $this->isOpen()) {
- return false;
- }
- $count = fwrite($this->handle, $message);
- if (! $count) {
- if ($count === false) {
- $this->setError('Cannot write to socket');
- $this->close();
- }
- return false;
- }
- fflush($this->handle);
- $this->sent .= $message;
- return true;
- }
-
-
-
- function read() {
- if ($this->isError() || ! $this->isOpen()) {
- return false;
- }
- $raw = @fread($this->handle, $this->block_size);
- if ($raw === false) {
- $this->setError('Cannot read from socket');
- $this->close();
- }
- return $raw;
- }
-
-
-
- function isOpen() {
- return $this->is_open;
- }
-
-
-
- function close() {
- $this->is_open = false;
- return fclose($this->handle);
- }
-
-
-
- function getSent() {
- return $this->sent;
- }
-
-
-
- protected function openSocket($host, $port, &$error_number, &$error, $timeout) {
- return @fsockopen($host, $port, $error_number, $error, $timeout);
- }
- }
-
-
- class SimpleSecureSocket extends SimpleSocket {
-
-
-
- function __construct($host, $port, $timeout) {
- parent::__construct($host, $port, $timeout);
- }
-
-
-
- function openSocket($host, $port, &$error_number, &$error, $timeout) {
- return parent::openSocket("tls://$host", $port, $error_number, $error, $timeout);
- }
- }
- ?>
|