added P10_Server.class.php & recv_server
[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                 $this->server = new P10_Server($self_name, $self_numeric, null, time(), time(), $self_description);
77         }
78         
79         public function loop() {
80                 if($this->server == null) {
81                         trigger_error("Uplink not initialized.", E_USER_ERROR);
82                         return;
83                 }
84                 if(!$this->client->connected()) {
85                         if(($this->flags & self::FLAG_P10SESSION)) {
86                                 //Server got disconnected
87                                 $this->server->disconnectServer(true);
88                                 $this->flags &= ~self::FLAG_P10SESSION;
89                         }
90                         $host = $this->getSetting("host");
91                         $port = $this->getSetting("port");
92                         if($host == null || $port == null) {
93                                 trigger_error("Uplink Settings missing.", E_USER_ERROR);
94                                 return;
95                         }
96                         if(($this->flags & self::FLAG_SECURITY_QUIT) || ($this->flags & self::FLAG_NOT_CONNECTABLE)) {
97                                 sleep(1);
98                         }
99                         $state = $this->client->connect($host, $port, $this->getSettings("bind"), $this->getSettings("ssl"), $this->getSettings("recv_timeout"));
100                         if(!$state) {
101                                 usleep($this->getSetting("recv_timeout") / 1000);
102                                 $flags |= self::FLAG_NOT_CONNECTABLE;
103                                 return;
104                         }
105                         $this->flags = 0;
106                 }
107                 //try to receive new data from the Uplink
108                 $lines = $this->client->recv();
109                 if($lines == null) return;
110                 foreach($lines as $line) {
111                         $this->parseLine($line);
112                 }
113         }
114         
115         public function setUplink($host, $port, $ssl = false, $bind = null) {
116                 $this->setSetting("host", $host);
117                 $this->setSetting("port", $port);
118                 $this->setSetting("ssl", $ssl);
119                 $this->setSetting("bind", $bind);
120         }
121         
122         public function setLoopTimeout($timeout) {
123                 $this->setSetting("recv_timeout", $timeout);
124         }
125         
126         public function setUplinkServer($numeric, $name, $password, $description) {
127                 $this->setSetting("numeric", Numerics::intToNum($numeric, 2));
128                 $this->setSetting("name", $name);
129                 $this->setSetting("password", $password);
130                 $this->setSetting("description", $description);
131         }
132         
133         public function setValidateServer($name, $password) {
134                 $this->setSetting("their_name", $name);
135                 $this->setSetting("their_password", $password);
136         }
137         
138         private function setSetting($setting, $value) {
139                 $this->settings[$setting] = $value;
140         }
141         
142         private function getSetting($setting) {
143                 if(array_key_exists($setting, $this->settings)) {
144                         return $this->settings[$setting];
145                 } else {
146                         return null;
147                 }
148         }
149         
150         private function parseLine($line) {
151                 $highExplode = explode(" :", $line, 2);
152                 $tokens = explode(" ", $highExplode[0]);
153                 if(count($highExplode) > 1)
154                         $tokens[] = $highExplode[1];
155                 $cmdPos = (($this->flags & self::FLAG_P10SESSION) ? 1 : 0);
156                 if($tokens[0] == "ERROR") $cmdPos = 0; //override
157                 if($cmdPos == 1) $from = $tokens[0];
158                 else $from = null;
159                 $arguments = array_slice($tokens, $cmdPos + 1);
160                 switch(strtoupper($tokens[$cmdPos])) {
161                 //pre P10 Session
162                         case "PASS":
163                                 $this->recv_pass($from, $arguments);
164                                 break;
165                         case "SERVER":
166                                 $this->recv_server($from, $arguments);
167                                 break;
168                         case "ERROR":
169                                 $this->recv_error($from, $arguments);
170                                 break;
171                 //P10 Session
172                         default:
173                                 //unknown cmd
174                                 break;
175                 }
176         }
177         
178         private function send($command) {
179                 if(func_num_args() > 1) {
180                         $args = array_slice(func_get_args(), 1);
181                         $command = P10Formatter::formatCMD($this->getSetting("numeric"), $command, $args);
182                 }
183                 $this->client->send($command);
184         }
185         
186         /********************************************************************************************
187          *                                     INCOMING COMMANDS                                    *
188          ********************************************************************************************/
189         
190         private function recv_pass($from, $args) {
191                 $their_pass = $this->getSetting("their_password");
192                 if($their_pass) {
193                         if($args[0] != $their_pass) {
194                                 //security QUIT
195                                 $this->flags |= self::FLAG_SECURITY_QUIT;
196                                 $this->send("ERROR", "Incorrect password received.");
197                                 $this->client->disconnect();
198                         }
199                 }
200         }
201         
202         private function recv_error($from, $args) {
203                 //we received an error - the socket is dead for us, now
204                 //maybe we want to log this, later
205                 $this->client->disconnect();
206         }
207         
208         private function recv_server($from, $args) {
209                 if($from == null) {
210                         //our uplink Server
211                         $their_name = $this->getSetting("their_name");
212                         if($their_name && $args[0] != $their_name) {
213                                 $this->flags |= self::FLAG_SECURITY_QUIT;
214                                 $this->send("ERROR", "Invalid Server name");
215                                 $this->client->disconnect();
216                                 return;
217                         }
218                         $new_server = new P10_Server($args[0], substr($args[5],0,2), $this->server, $args[2], $args[3], $args[7]);
219                         $this->server->add_server($new_server);
220                 } else {
221                         //another server got a new slave server ^^
222                         $server = P10_Server::getServerByNum($from);
223                         if($server == null) {
224                                 trigger_error("Parent Server (".$from.") does not exist or was not found on recv_server.", E_USER_ERROR);
225                                 return;
226                         }
227                         $new_server = new P10_Server($args[0], substr($args[5],0,2), $server, $args[2], $args[3], $args[7]);
228                         $server->add_server($new_server);
229                 }
230         }
231         
232 }
233
234 ?>