2 /********************************* PHP-P10 ******************************
3 * P10 uplink class by pk910 (c)2011 pk910 *
4 ************************************************************************
7 * PHP-P10 is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
17 * You should have received a copy of the GNU General Public License *
18 * along with PHP-P10; if not, write to the Free Software Foundation, *
19 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
21 ************************************************************************
23 * Uplink/Uplink.class.php
25 * This file contains the basic P10 Protocol handler.
27 ************************************************************************
31 * loop function that should be calles as many times as possible.
32 * It reads from the socket and BLOCKS the script execution for a
33 * specific time if nothing is received.
35 * void setUplinkHost(String $host, int $port, bool $ssl = false, String $bind = null)
36 * sets the Uplink connection information.
38 * void setLoopTimeout(int $timeout)
39 * sets the maximum time loop() is blocking the script execution.
41 * void setUplinkServer(int $numeric, String $name, String $password)
42 * sets the own P10 Server information.
44 * void setValidateServer(String $name, String $password)
45 * sets additional security relevant information about the remote server.
47 require_once("Client.class.php");
48 require_once("Numerics.class.php");
49 require_once("P10Formatter.class.php");
52 private $client = new Client();
53 private $settings = array();
55 const FLAG_P10SESSION = 0x0001; //connection is in P10 mode (parser relevant)
56 const FLAG_SECURITY_QUIT = 0x0002; //local connection abort because of security issues
57 const FLAG_NOT_CONNECTABLE = 0x0004; //remote server is not connectable
60 public function __construct() {
61 $this->setSettings("recv_timeout", 1000);
64 public function loop() {
65 if(!$this->client->connected()) {
66 $host = $this->getSetting("host");
67 $port = $this->getSetting("port");
68 if($host == null || $port == null) {
69 Throw new Exception("Uplink Settings missing.");
72 if(($flags & self::FLAG_SECURITY_QUIT) || ($flags & self::FLAG_NOT_CONNECTABLE)) {
75 $state = $this->client->connect($host, $port, $this->getSettings("bind"), $this->getSettings("ssl"), $this->getSettings("recv_timeout"));
77 usleep($this->getSetting("recv_timeout") / 1000);
78 $flags |= self::FLAG_NOT_CONNECTABLE;
83 //try to receive new data from the Uplink
84 $lines = $this->client->recv();
85 if($lines == null) return;
86 foreach($lines as $line) {
87 $this->parseLine($line);
91 public function setUplink($host, $port, $ssl = false, $bind = null) {
92 $this->setSetting("host", $host);
93 $this->setSetting("port", $port);
94 $this->setSetting("ssl", $ssl);
95 $this->setSetting("bind", $bind);
98 public function setLoopTimeout($timeout) {
99 $this->setSetting("recv_timeout", $timeout);
102 public function setUplinkServer($numeric, $name, $password) {
103 $this->setSetting("numeric", Numerics::intToNum($numeric, 2));
104 $this->setSetting("name", $name);
105 $this->setSetting("password", $password);
108 public function setValidateServer($name, $password) {
109 $this->setSetting("their_name", $name);
110 $this->setSetting("their_password", $password);
113 private function setSetting($setting, $value) {
114 $this->settings[$setting] = $value;
117 private function getSetting($setting) {
118 if(array_key_exists($setting, $this->settings)) {
119 return $this->settings[$setting];
125 private function parseLine($line) {
126 $highExplode = explode(" :", $line, 2);
127 $tokens = explode(" ", $highExplode[0]);
128 if(count($highExplode) > 1)
129 $tokens[] = $highExplode[1];
130 $cmdPos = (($this->flags & self::FLAG_P10SESSION) ? 1 : 0);
131 if($cmdPos == 1) $from = $tokens[0];
133 $arguments = array_slice($tokens, $cmdPos + 1);
134 switch(strtoupper($tokens[$cmdPos])) {
137 $this->recv_pass($from, $arguments);
140 $this->recv_server($from, $arguments);
143 $this->recv_error($from, $arguments);
152 private function send($command) {
153 if(func_num_args() > 1) {
154 $args = array_slice(func_get_args(), 1);
155 $command = P10Formatter::formatCMD($this->getSetting("numeric"), $command, $args);
157 $this->client->send($command);
160 /********************************************************************************************
161 * INCOMING COMMANDS *
162 ********************************************************************************************/
164 private function recv_pass($from, $args) {
165 $their_pass = $this->getSetting("their_password");
167 if($args[0] != $their_pass) {
169 $this->flags |= self::FLAG_SECURITY_QUIT;
170 $this->send("ERROR", "Incorrect password received.");
171 $this->client->disconnect();
176 private function recv_error($from, $args) {
177 //we received an error - the socket is dead for us, now
178 //maybe we want to log this, later
179 $this->client->disconnect();
182 private function recv_server($from, $args) {