From: pk910 Date: Tue, 26 Jul 2011 03:46:51 +0000 (+0200) Subject: added Line parser and recv_pass, recv_error; added send command with a P10Formatter X-Git-Url: http://git.pk910.de/?p=PHP-P10.git;a=commitdiff_plain;h=642e5fa114096506072cbc9ef6f16e31eecd7abf added Line parser and recv_pass, recv_error; added send command with a P10Formatter --- diff --git a/Uplink/Client.class.php b/Uplink/Client.class.php index 834448c..c7ecc4c 100644 --- a/Uplink/Client.class.php +++ b/Uplink/Client.class.php @@ -30,6 +30,9 @@ * bool connect(String $host, int $port, String $bind = null, bool $ssl = false, int $blocking = 0) * connects the socket to $host:$port with the provided options * + * void disconnect() + * disconnects the socket + * * bool connected() * returns the state of the socket (true if the socket is connected with the server) * @@ -72,6 +75,12 @@ class Client { return false; } + public function disconnect() { + if($this->socket == null) return; + fclose($this->socket); + $this->socket = null; + } + public function connected() { if($this->socket == null) return false; $read = array($this->socket); diff --git a/Uplink/P10Formatter.class.php b/Uplink/P10Formatter.class.php new file mode 100644 index 0000000..d280bf2 --- /dev/null +++ b/Uplink/P10Formatter.class.php @@ -0,0 +1,54 @@ + "PASS :%s", + "SERVER" => "SERVER %s 1 %s %s J10 %s]]] +s :%s", + "ERROR" => "ERROR :%s" + ); + + public static function formatCMD($numeric, $command, $args) { + if(array_key_exists($command, self::$commands)) { + $command = self::$commands[$command]; + $command = vsprintf($command, $args); + } else { + $command = vsprintf($command, $args); + } + return str_replace("{num}", $numeric, $command); + } +} + +?> \ No newline at end of file diff --git a/Uplink/Uplink.class.php b/Uplink/Uplink.class.php index a7b2cbd..d9ac221 100644 --- a/Uplink/Uplink.class.php +++ b/Uplink/Uplink.class.php @@ -41,35 +41,51 @@ * void setUplinkServer(int $numeric, String $name, String $password) * sets the own P10 Server information. * - * + * void setValidateServer(String $name, String $password) + * sets additional security relevant information about the remote server. */ require_once("Client.class.php"); require_once("Numerics.class.php"); +require_once("P10Formatter.class.php"); class Uplink { private $client = new Client(); private $settings = array(); + const FLAG_P10SESSION = 0x0001; //connection is in P10 mode (parser relevant) + const FLAG_SECURITY_QUIT = 0x0002; //local connection abort because of security issues + const FLAG_NOT_CONNECTABLE = 0x0004; //remote server is not connectable + private $flags = 0; + public function __construct() { $this->setSettings("recv_timeout", 1000); } public function loop() { - if(!$client->connected()) { + if(!$this->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(($flags & self::FLAG_SECURITY_QUIT) || ($flags & self::FLAG_NOT_CONNECTABLE)) { + sleep(1); + } + $state = $this->client->connect($host, $port, $this->getSettings("bind"), $this->getSettings("ssl"), $this->getSettings("recv_timeout")); if(!$state) { usleep($this->getSetting("recv_timeout") / 1000); + $flags |= self::FLAG_NOT_CONNECTABLE; return; } + $this->flags = 0; } //try to receive new data from the Uplink - //null + $lines = $this->client->recv(); + if($lines == null) return; + foreach($lines as $line) { + $this->parseLine($line); + } } public function setUplink($host, $port, $ssl = false, $bind = null) { @@ -89,6 +105,11 @@ class Uplink { $this->setSetting("password", $password); } + public function setValidateServer($name, $password) { + $this->setSetting("their_name", $name); + $this->setSetting("their_password", $password); + } + private function setSetting($setting, $value) { $this->settings[$setting] = $value; } @@ -101,6 +122,67 @@ class Uplink { } } + private function parseLine($line) { + $highExplode = explode(" :", $line, 2); + $tokens = explode(" ", $highExplode[0]); + if(count($highExplode) > 1) + $tokens[] = $highExplode[1]; + $cmdPos = (($this->flags & self::FLAG_P10SESSION) ? 1 : 0); + if($cmdPos == 1) $from = $tokens[0]; + else $from = null; + $arguments = array_slice($tokens, $cmdPos + 1); + switch(strtoupper($tokens[$cmdPos])) { + //pre P10 Session + case "PASS": + $this->recv_pass($from, $arguments); + break; + case "SERVER": + $this->recv_server($from, $arguments); + break; + case "ERROR": + $this->recv_error($from, $arguments); + break; + //P10 Session + default: + //unknown cmd + break; + } + } + + private function send($command) { + if(func_num_args() > 1) { + $args = array_slice(func_get_args(), 1); + $command = P10Formatter::formatCMD($this->getSetting("numeric"), $command, $args); + } + $this->client->send($command); + } + + /******************************************************************************************** + * INCOMING COMMANDS * + ********************************************************************************************/ + + private function recv_pass($from, $args) { + $their_pass = $this->getSetting("their_password"); + if($their_pass) { + if($args[0] != $their_pass) { + //security QUIT + $this->flags |= self::FLAG_SECURITY_QUIT; + $this->send("ERROR", "Incorrect password received."); + $this->client->disconnect(); + } + } + } + + private function recv_error($from, $args) { + //we received an error - the socket is dead for us, now + //maybe we want to log this, later + $this->client->disconnect(); + } + + private function recv_server($from, $args) { + + } + } ?> \ No newline at end of file