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 * has to be called after the settings have been set.
34 * loop function that should be calles as many times as possible.
35 * It reads from the socket and BLOCKS the script execution for a
36 * specific time if nothing is received.
38 * void setUplinkHost(String $host, int $port, bool $ssl = false, String $bind = null)
39 * sets the Uplink connection information.
41 * void setLoopTimeout(int $timeout)
42 * sets the maximum time loop() is blocking the script execution.
44 * void setUplinkServer(int $numeric, String $name, String $password, String $description)
45 * sets the own P10 Server information.
47 * void setValidateServer(String $name, String $password)
48 * sets additional security relevant information about the remote server.
50 require_once("Client.class.php");
51 require_once("Numerics.class.php");
52 require_once("P10Formatter.class.php");
55 private $client = new Client();
56 private $settings = array();
59 const FLAG_P10SESSION = 0x0001; //connection is in P10 mode (server is connected)
60 const FLAG_SECURITY_QUIT = 0x0002; //local connection abort because of security issues
61 const FLAG_NOT_CONNECTABLE = 0x0004; //remote server is not connectable
64 public function __construct() {
65 $this->setSettings("recv_timeout", 1000);
68 public function initialize() {
70 trigger_error("Uplink already initialized.", E_USER_ERROR);
73 $self_numeric = $this->getSetting("numeric");
74 $self_name = $this->getSetting("name");
75 $self_description = $this->getSetting("description");
76 if(!$self_numeric || !$self_name) {
77 trigger_error("Server Settings missing.", E_USER_ERROR);
80 $this->server = new P10_Server($self_name, $self_numeric, null, time(), time(), $self_description);
83 public function loop() {
84 if($this->server == null) {
85 trigger_error("Uplink not initialized.", E_USER_ERROR);
88 if(!$this->client->connected()) {
89 if(($this->flags & self::FLAG_P10SESSION)) {
90 //Server got disconnected
91 $this->server->disconnectServer(true);
92 $this->flags &= ~self::FLAG_P10SESSION;
94 $host = $this->getSetting("host");
95 $port = $this->getSetting("port");
96 if($host == null || $port == null) {
97 trigger_error("Uplink Settings missing.", E_USER_ERROR);
100 if(($this->flags & self::FLAG_SECURITY_QUIT) || ($this->flags & self::FLAG_NOT_CONNECTABLE)) {
103 $state = $this->client->connect($host, $port, $this->getSettings("bind"), $this->getSettings("ssl"), $this->getSettings("recv_timeout"));
105 usleep($this->getSetting("recv_timeout") / 1000);
106 $this->flags |= self::FLAG_NOT_CONNECTABLE;
110 $this->loginServer();
112 //try to receive new data from the Uplink
113 $lines = $this->client->recv();
114 if($lines == null) return;
115 foreach($lines as $line) {
116 $this->parseLine($line);
120 public function setUplink($host, $port, $ssl = false, $bind = null) {
121 $this->setSetting("host", $host);
122 $this->setSetting("port", $port);
123 $this->setSetting("ssl", $ssl);
124 $this->setSetting("bind", $bind);
127 public function setLoopTimeout($timeout) {
128 $this->setSetting("recv_timeout", $timeout);
131 public function setUplinkServer($numeric, $name, $password, $description) {
132 $this->setSetting("numeric", Numerics::intToNum($numeric, 2));
133 $this->setSetting("name", $name);
134 $this->setSetting("password", $password);
135 $this->setSetting("description", $description);
138 public function setValidateServer($name, $password) {
139 $this->setSetting("their_name", $name);
140 $this->setSetting("their_password", $password);
143 private function setSetting($setting, $value) {
144 $this->settings[$setting] = $value;
147 private function getSetting($setting) {
148 if(array_key_exists($setting, $this->settings)) {
149 return $this->settings[$setting];
155 private function loginServer() {
156 $password = $this->getSetting("password");
157 $this->send("PASS", $password);
158 $this->send("SERVER", $this->server->getName(), $this->server->getStartTime(), $this->server->getLinkTime(), $this->server->getNumeric(), $this->server->getDescription());
161 private function parseLine($line) {
162 $highExplode = explode(" :", $line, 2);
163 $tokens = explode(" ", $highExplode[0]);
164 if(count($highExplode) > 1)
165 $tokens[] = $highExplode[1];
166 $cmdPos = (($this->flags & self::FLAG_P10SESSION) ? 1 : 0);
167 if($cmdPos == 1) $from = $tokens[0];
169 $arguments = array_slice($tokens, $cmdPos + 1);
170 switch(strtoupper($tokens[$cmdPos])) {
173 $this->recv_pass($from, $arguments);
176 $this->recv_server($from, $arguments);
179 $this->recv_error($from, $arguments);
188 private function send($command) {
189 if(func_num_args() > 1) {
190 $args = array_slice(func_get_args(), 1);
191 $command = P10Formatter::formatCMD($this->getSetting("numeric"), $command, $args);
193 $this->client->send($command);
196 /********************************************************************************************
197 * INCOMING COMMANDS *
198 ********************************************************************************************/
200 private function recv_pass($from, $args) {
201 $their_pass = $this->getSetting("their_password");
203 if($args[0] != $their_pass) {
205 $this->flags |= self::FLAG_SECURITY_QUIT;
206 $this->send("ERROR", "Incorrect password received.");
207 $this->client->disconnect();
212 private function recv_error($from, $args) {
213 //we received an error - the socket is dead for us, now
214 //maybe we want to log this, later
215 $this->client->disconnect();
218 private function recv_server($from, $args) {
221 $their_name = $this->getSetting("their_name");
222 if($their_name && $args[0] != $their_name) {
223 $this->flags |= self::FLAG_SECURITY_QUIT;
224 $this->send("ERROR", "Invalid Server name");
225 $this->client->disconnect();
228 $new_server = new P10_Server($args[0], substr($args[5],0,2), $this->server, $args[2], $args[3], $args[7]);
229 $this->server->add_server($new_server);
230 $this->flags |= self::FLAG_P10SESSION;
232 //another server got a new slave server ^^
233 $server = P10_Server::getServerByNum($from);
234 if($server == null) {
235 trigger_error("Parent Server (".$from.") does not exist or was not found on recv_server.", E_USER_ERROR);
238 $new_server = new P10_Server($args[0], substr($args[5],0,2), $server, $args[2], $args[3], $args[7]);
239 $server->add_server($new_server);