From d50b17d7f0ebc21c1c70f2ba513e0973ce9fc789 Mon Sep 17 00:00:00 2001 From: pk910 Date: Tue, 26 Jul 2011 04:42:01 +0200 Subject: [PATCH] start writing PHP P10 pack v2 (hopefully without bugs :D) --- Uplink/Client.class.php | 124 ++++++++++++++++++++++++++++++++++++++ Uplink/Numerics.class.php | 83 +++++++++++++++++++++++++ Uplink/Uplink.class.php | 106 ++++++++++++++++++++++++++++++++ main.php | 38 ++++++++++++ 4 files changed, 351 insertions(+) create mode 100644 Uplink/Client.class.php create mode 100644 Uplink/Numerics.class.php create mode 100644 Uplink/Uplink.class.php create mode 100644 main.php diff --git a/Uplink/Client.class.php b/Uplink/Client.class.php new file mode 100644 index 0000000..834448c --- /dev/null +++ b/Uplink/Client.class.php @@ -0,0 +1,124 @@ + 0, "out" => 0); + + public function connect($host, $port, $bind = null, $ssl = false, $blocking = 0) { + if($bind) + $options = array('socket' => array('bindto' => $bind.":0")); + else + $options = array(); + $context = stream_context_create($options); + $sock = stream_socket_client(($ssl ? 'ssl://' : '').$host.':'.$port, $errno, $errstr, 3, STREAM_CLIENT_CONNECT, $context); + if($sock) { + if($blocking) { + stream_set_blocking($sock, true); + stream_set_timeout($sock, 0, ($blocking / 1000)); + } else { + stream_set_blocking($sock, false); + } + $this->socket = $sock; + return true; + } else + return false; + } + + public function connected() { + if($this->socket == null) return false; + $read = array($this->socket); + $write= null; + $except=null; + $n=@stream_select($read, $write, $except, 0); + if($n === FALSE || feof($this->socket)) { + $this->socket = false; + return false; + } + return true; + } + + public function recv() { + $read = array($this->socket); + $write= null; + $except=null; + $n=@stream_select($read, $write, $except, 0); + if($n === FALSE || feof($this->socket)) { + $this->socket = false; + return null; + } elseif($n > 0) { + $lines = array(); + while(($line = @fgets($this->socket)) != null) { + $line=trim($line); + if(!empty($line)) { + if(DEBUG_RAW) echo"[recv] ".$line."\n"; + $this->traffic['in'] += strlen($line); + $lines[] = $line; + if(count($lines) >= self::CLIENT_RECV_MAX_LINES) break; + } + } + return $lines; + } + return null; + } + + public function send($line, $newline = "\r\n") { + if($this->socket == null) return; + if(DEBUG_RAW) echo"[send] ".$line."\n"; + $this->traffic['out'] += strlen($line); + fwrite($this->socket,$line.$newline); + } + + public function getTraffic() { + return $this->traffic; + } +} + +?> \ No newline at end of file diff --git a/Uplink/Numerics.class.php b/Uplink/Numerics.class.php new file mode 100644 index 0000000..55db209 --- /dev/null +++ b/Uplink/Numerics.class.php @@ -0,0 +1,83 @@ += 0; $pos--) { + //current position represents floor($int / ($base64charsLength ^ $pos)) + $base = 1; + for($i = 0; $i < $pos; $i++) { + $base = $base * self::$base64charsLength; + } + $posValue = floor($int / $base); + //get the char representing $posValue + $posChar = self::$base64chars[$posValue]; + $numeric .= $posChar; + } + + return $numeric; + } + + public static function numToInt($numeric) { + $base = 1; + $int = 0; + for($pos = strlen($numeric)-1; $pos >= 0; $pos--) { + $posValue = array_search($numeric[$pos], self::$base64chars); + $int = ($posValue * $base); + $base = $base * self::$base64charsLength; + } + + //fix a small "bug": normaly 0 = AAAAA but we need 1 = AAAAA + $int = $int + 1; + + return $int; + } + +} + +?> \ No newline at end of file diff --git a/Uplink/Uplink.class.php b/Uplink/Uplink.class.php new file mode 100644 index 0000000..a7b2cbd --- /dev/null +++ b/Uplink/Uplink.class.php @@ -0,0 +1,106 @@ +setSettings("recv_timeout", 1000); + } + + public function loop() { + if(!$client->connected()) { + $host = $this->getSetting("host"); + $port = $this->getSetting("port"); + if($host == null || $port == null) { + Throw new Exception("Uplink Settings missing."); + return; + } + $state = $client->connect($host, $port, $this->getSettings("bind"), $this->getSettings("ssl"), $this->getSettings("recv_timeout")); + if(!$state) { + usleep($this->getSetting("recv_timeout") / 1000); + return; + } + } + //try to receive new data from the Uplink + //null + } + + public function setUplink($host, $port, $ssl = false, $bind = null) { + $this->setSetting("host", $host); + $this->setSetting("port", $port); + $this->setSetting("ssl", $ssl); + $this->setSetting("bind", $bind); + } + + public function setLoopTimeout($timeout) { + $this->setSetting("recv_timeout", $timeout); + } + + public function setUplinkServer($numeric, $name, $password) { + $this->setSetting("numeric", Numerics::intToNum($numeric, 2)); + $this->setSetting("name", $name); + $this->setSetting("password", $password); + } + + private function setSetting($setting, $value) { + $this->settings[$setting] = $value; + } + + private function getSetting($setting) { + if(array_key_exists($setting, $this->settings)) { + return $this->settings[$setting]; + } else { + return null; + } + } + +} + +?> \ No newline at end of file diff --git a/main.php b/main.php new file mode 100644 index 0000000..954aa6b --- /dev/null +++ b/main.php @@ -0,0 +1,38 @@ +setUplinkHost("localhost", 4402); +$uplink->setUplinkServer(5, "php.local.TestNet", "very_weak_password"); + +while(true) { + $uplink->loop(); +} +?> \ No newline at end of file -- 2.20.1