finished Server Handshake (script should be able to connect to another server now)
[PHP-P10.git] / Uplink / Uplink.class.php
1 <?php
2 /********************************* PHP-P10 ******************************
3  *    P10 uplink class by pk910   (c)2011 pk910                         *
4  ************************************************************************
5  *                          Version 2 (OOP)                             *
6  *                                                                      *
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.                                  *
11  *                                                                      *
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.                         *
16  *                                                                      *
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.       *
20  *                                                                      *
21  ************************************************************************
22  * 
23  *  Uplink/Uplink.class.php
24  *
25  * This file contains the basic P10 Protocol handler.
26  *
27  ************************************************************************
28  * accessable methods:
29  *
30  * void initialize()
31  *     has to be called after the settings have been set.
32  *
33  * void loop() 
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.
37  *
38  * void setUplinkHost(String $host, int $port, bool $ssl = false, String $bind = null)
39  *     sets the Uplink connection information.
40  *
41  * void setLoopTimeout(int $timeout)
42  *     sets the maximum time loop() is blocking the script execution.
43  *
44  * void setUplinkServer(int $numeric, String $name, String $password, String $description)
45  *     sets the own P10 Server information.
46  *
47  * void setValidateServer(String $name, String $password)
48  *     sets additional security relevant information about the remote server.
49  */
50 require_once("Client.class.php");
51 require_once("Numerics.class.php");
52 require_once("P10Formatter.class.php");
53
54 class Uplink {
55         private $client = new Client();
56         private $settings = array();
57         private $server;
58         
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
62         private $flags = 0;
63         
64         public function __construct() {
65                 $this->setSettings("recv_timeout", 1000);
66         }
67         
68         public function initialize() {
69                 if($this->server) {
70                         trigger_error("Uplink already initialized.", E_USER_ERROR);
71                         return;
72                 }
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);
78                         return;
79                 }
80                 $this->server = new P10_Server($self_name, $self_numeric, null, time(), time(), $self_description);
81         }
82         
83         public function loop() {
84                 if($this->server == null) {
85                         trigger_error("Uplink not initialized.", E_USER_ERROR);
86                         return;
87                 }
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;
93                         }
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);
98                                 return;
99                         }
100                         if(($this->flags & self::FLAG_SECURITY_QUIT) || ($this->flags & self::FLAG_NOT_CONNECTABLE)) {
101                                 sleep(1);
102                         }
103                         $state = $this->client->connect($host, $port, $this->getSettings("bind"), $this->getSettings("ssl"), $this->getSettings("recv_timeout"));
104                         if(!$state) {
105                                 usleep($this->getSetting("recv_timeout") / 1000);
106                                 $this->flags |= self::FLAG_NOT_CONNECTABLE;
107                                 return;
108                         }
109                         $this->flags = 0;
110                         $this->loginServer();
111                 }
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);
117                 }
118         }
119         
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);
125         }
126         
127         public function setLoopTimeout($timeout) {
128                 $this->setSetting("recv_timeout", $timeout);
129         }
130         
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);
136         }
137         
138         public function setValidateServer($name, $password) {
139                 $this->setSetting("their_name", $name);
140                 $this->setSetting("their_password", $password);
141         }
142         
143         private function setSetting($setting, $value) {
144                 $this->settings[$setting] = $value;
145         }
146         
147         private function getSetting($setting) {
148                 if(array_key_exists($setting, $this->settings)) {
149                         return $this->settings[$setting];
150                 } else {
151                         return null;
152                 }
153         }
154         
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());
159         }
160         
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];
168                 else $from = null;
169                 $arguments = array_slice($tokens, $cmdPos + 1);
170                 switch(strtoupper($tokens[$cmdPos])) {
171                 //pre P10 Session
172                         case "PASS":
173                                 $this->recv_pass($from, $arguments);
174                                 break;
175                         case "SERVER":
176                                 $this->recv_server($from, $arguments);
177                                 break;
178                         case "ERROR":
179                                 $this->recv_error($from, $arguments);
180                                 break;
181                 //P10 Session
182                         default:
183                                 //unknown cmd
184                                 break;
185                 }
186         }
187         
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);
192                 }
193                 $this->client->send($command);
194         }
195         
196         /********************************************************************************************
197          *                                     INCOMING COMMANDS                                    *
198          ********************************************************************************************/
199         
200         private function recv_pass($from, $args) {
201                 $their_pass = $this->getSetting("their_password");
202                 if($their_pass) {
203                         if($args[0] != $their_pass) {
204                                 //security QUIT
205                                 $this->flags |= self::FLAG_SECURITY_QUIT;
206                                 $this->send("ERROR", "Incorrect password received.");
207                                 $this->client->disconnect();
208                         }
209                 }
210         }
211         
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();
216         }
217         
218         private function recv_server($from, $args) {
219                 if($from == null) {
220                         //our uplink Server
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();
226                                 return;
227                         }
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;
231                 } else {
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);
236                                 return;
237                         }
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);
240                 }
241         }
242         
243 }
244
245 ?>