--- /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/Client.class.php
+ *
+ * This file contains the basic Client Socket.
+ *
+ ************************************************************************
+ * accessable methods:
+ *
+ * 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
+ *
+ * bool connected()
+ * returns the state of the socket (true if the socket is connected with the server)
+ *
+ * String[] recv()
+ * tries to receive datas from the socket. Returns null or a array containing the lines received.
+ * this method BLOCKS THE SCRIPT EXECUTION for the defined time if $blocking is set on connect().
+ *
+ * void send(String $line, String $newline = "\r\n")
+ * tries to send datas through the socket.
+ *
+ * Array[] getTraffic()
+ * returns an array containing the traffic the socket has received and sent.
+ * index in = incoming (received) traffic count
+ index out = outgoing (sent) traffic count
+ */
+
+class Client {
+ const CLIENT_RECV_MAX_LINES = 20; //maximum Lines to receive within one recv() call
+
+ private $socket;
+ private $traffic = array("in" => 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
--- /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/Numerics.class.php
+ *
+ * P10 numeric functions
+ *
+ ************************************************************************
+ * accessable functions
+ *
+ * static String intToNum(int $int, int $length)
+ * returns the numeric representing $int
+ *
+ * static int numToInt(String $numeric)
+ * returns the integer value, the numeric represents
+ */
+
+class Numerics {
+ private static $base64chars = array(
+ 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
+ 'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
+ 'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
+ 'w','x','y','z','0','1','2','3','4','5','6','7','8','9','[',']'
+ );
+ private static $base64charsLength = 64;
+
+ public static function intToNum($int, $length) {
+ //fix a small "bug": normaly 0 = AAAAA but we need 1 = AAAAA
+ $int = $int - 1;
+
+ $numeric = "";
+ for($pos = $length-1; $pos >= 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
--- /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/Uplink.class.php
+ *
+ * This file contains the basic P10 Protocol handler.
+ *
+ ************************************************************************
+ * accessable methods:
+ *
+ * void loop()
+ * loop function that should be calles as many times as possible.
+ * It reads from the socket and BLOCKS the script execution for a
+ * specific time if nothing is received.
+ *
+ * void setUplinkHost(String $host, int $port, bool $ssl = false, String $bind = null)
+ * sets the Uplink connection information.
+ *
+ * void setLoopTimeout(int $timeout)
+ * sets the maximum time loop() is blocking the script execution.
+ *
+ * void setUplinkServer(int $numeric, String $name, String $password)
+ * sets the own P10 Server information.
+ *
+ *
+ */
+require_once("Client.class.php");
+require_once("Numerics.class.php");
+
+class Uplink {
+ private $client = new Client();
+ private $settings = array();
+
+ public function __construct() {
+ $this->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
--- /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. *
+ * *
+ ************************************************************************
+ *
+ * main.php
+ *
+ * initial php file
+ *
+ */
+require_once("Uplink/Uplink.class.php");
+
+//basicly here is nothing, yet :D
+$uplink = new Uplink();
+$uplink->setUplinkHost("localhost", 4402);
+$uplink->setUplinkServer(5, "php.local.TestNet", "very_weak_password");
+
+while(true) {
+ $uplink->loop();
+}
+?>
\ No newline at end of file