--- /dev/null
+<?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
* 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) {
$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;
}
}
}
+ 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