d9ac2211afdaf715f3ec34ab65bda4143a6f4292
[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 loop() 
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.
34  *
35  * void setUplinkHost(String $host, int $port, bool $ssl = false, String $bind = null)
36  *     sets the Uplink connection information.
37  *
38  * void setLoopTimeout(int $timeout)
39  *     sets the maximum time loop() is blocking the script execution.
40  *
41  * void setUplinkServer(int $numeric, String $name, String $password)
42  *     sets the own P10 Server information.
43  *
44  * void setValidateServer(String $name, String $password)
45  *     sets additional security relevant information about the remote server.
46  */
47 require_once("Client.class.php");
48 require_once("Numerics.class.php");
49 require_once("P10Formatter.class.php");
50
51 class Uplink {
52         private $client = new Client();
53         private $settings = array();
54         
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
58         private $flags = 0;
59         
60         public function __construct() {
61                 $this->setSettings("recv_timeout", 1000);
62         }
63         
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.");
70                                 return;
71                         }
72                         if(($flags & self::FLAG_SECURITY_QUIT) || ($flags & self::FLAG_NOT_CONNECTABLE)) {
73                                 sleep(1);
74                         }
75                         $state = $this->client->connect($host, $port, $this->getSettings("bind"), $this->getSettings("ssl"), $this->getSettings("recv_timeout"));
76                         if(!$state) {
77                                 usleep($this->getSetting("recv_timeout") / 1000);
78                                 $flags |= self::FLAG_NOT_CONNECTABLE;
79                                 return;
80                         }
81                         $this->flags = 0;
82                 }
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);
88                 }
89         }
90         
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);
96         }
97         
98         public function setLoopTimeout($timeout) {
99                 $this->setSetting("recv_timeout", $timeout);
100         }
101         
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);
106         }
107         
108         public function setValidateServer($name, $password) {
109                 $this->setSetting("their_name", $name);
110                 $this->setSetting("their_password", $password);
111         }
112         
113         private function setSetting($setting, $value) {
114                 $this->settings[$setting] = $value;
115         }
116         
117         private function getSetting($setting) {
118                 if(array_key_exists($setting, $this->settings)) {
119                         return $this->settings[$setting];
120                 } else {
121                         return null;
122                 }
123         }
124         
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];
132                 else $from = null;
133                 $arguments = array_slice($tokens, $cmdPos + 1);
134                 switch(strtoupper($tokens[$cmdPos])) {
135                 //pre P10 Session
136                         case "PASS":
137                                 $this->recv_pass($from, $arguments);
138                                 break;
139                         case "SERVER":
140                                 $this->recv_server($from, $arguments);
141                                 break;
142                         case "ERROR":
143                                 $this->recv_error($from, $arguments);
144                                 break;
145                 //P10 Session
146                         default:
147                                 //unknown cmd
148                                 break;
149                 }
150         }
151         
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);
156                 }
157                 $this->client->send($command);
158         }
159         
160         /********************************************************************************************
161          *                                     INCOMING COMMANDS                                    *
162          ********************************************************************************************/
163         
164         private function recv_pass($from, $args) {
165                 $their_pass = $this->getSetting("their_password");
166                 if($their_pass) {
167                         if($args[0] != $their_pass) {
168                                 //security QUIT
169                                 $this->flags |= self::FLAG_SECURITY_QUIT;
170                                 $this->send("ERROR", "Incorrect password received.");
171                                 $this->client->disconnect();
172                         }
173                 }
174         }
175         
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();
180         }
181         
182         private function recv_server($from, $args) {
183                 
184         }
185         
186 }
187
188 ?>