added Line parser and recv_pass, recv_error; added send command with a P10Formatter
[PHP-P10.git] / Uplink / Uplink.class.php
index a7b2cbd2999e515ee873594ac771b95c4115e05d..d9ac2211afdaf715f3ec34ab65bda4143a6f4292 100644 (file)
  * 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