added Line parser and recv_pass, recv_error; added send command with a P10Formatter
authorpk910 <philipp@zoelle1.de>
Tue, 26 Jul 2011 03:46:51 +0000 (05:46 +0200)
committerpk910 <philipp@zoelle1.de>
Tue, 26 Jul 2011 03:46:51 +0000 (05:46 +0200)
Uplink/Client.class.php
Uplink/P10Formatter.class.php [new file with mode: 0644]
Uplink/Uplink.class.php

index 834448c4ba362fdd67c0b87d328a93b83119e2e0..c7ecc4cf40440bf942ccac22cd9d0003288a8510 100644 (file)
@@ -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 (file)
index 0000000..d280bf2
--- /dev/null
@@ -0,0 +1,54 @@
+<?php
+/********************************* PHP-P10 ******************************
+ *    P10 uplink class by pk910   (c)2011 pk910                         *
+ ************************************************************************
+ *                          Version 2 (OOP)                             *
+ *                                                                      *
+ * PHP-P10 is free software; you can redistribute it and/or modify      *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or    *
+ * (at your option) any later version.                                  *
+ *                                                                      *
+ * This program is distributed in the hope that it will be useful,      *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
+ * GNU General Public License for more details.                         *
+ *                                                                      *
+ * You should have received a copy of the GNU General Public License    *
+ * along with PHP-P10; if not, write to the Free Software Foundation,   *
+ * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.       *
+ *                                                                      *
+ ************************************************************************
+ * 
+ *  Uplink/P10Formatter.class.php
+ *
+ * This file contains the command formatter.
+ *
+ ************************************************************************
+ * accessable methods:
+ *
+ * static String formatCMD(String $numeric, String $command, String[] $args) 
+ *     builds the command with the given arguments
+ *
+ */
+
+class P10Formatter {
+       
+       private static $commands = array(
+               "PASS"   => "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
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