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");
53 require_once("P10_Server.class.php");
54 require_once("P10_User.class.php");
55 require_once("P10_ModeSets.class.php");
58 private $client = new Client();
59 private $settings = array();
62 const FLAG_P10SESSION = 0x0001; //connection is in P10 mode (server is connected)
63 const FLAG_SECURITY_QUIT = 0x0002; //local connection abort because of security issues
64 const FLAG_NOT_CONNECTABLE = 0x0004; //remote server is not connectable
65 const FLAG_BURST_PENDING = 0x0008; //we still have to burst
68 public function __construct() {
69 $this->setSettings("recv_timeout", 1000);
72 public function initialize() {
74 trigger_error("Uplink already initialized.", E_USER_ERROR);
77 $self_numeric = $this->getSetting("numeric");
78 $self_name = $this->getSetting("name");
79 $self_description = $this->getSetting("description");
80 if(!$self_numeric || !$self_name) {
81 trigger_error("Server Settings missing.", E_USER_ERROR);
84 $this->server = new P10_Server($self_name, $self_numeric, null, time(), time(), $self_description);
87 public function loop() {
88 if($this->server == null) {
89 trigger_error("Uplink not initialized.", E_USER_ERROR);
92 if(!$this->client->connected()) {
93 if(($this->flags & self::FLAG_P10SESSION)) {
94 //Server got disconnected
95 $this->server->disconnectServer(true);
96 $this->flags &= ~self::FLAG_P10SESSION;
98 $host = $this->getSetting("host");
99 $port = $this->getSetting("port");
100 if($host == null || $port == null) {
101 trigger_error("Uplink Settings missing.", E_USER_ERROR);
104 if(($this->flags & self::FLAG_SECURITY_QUIT) || ($this->flags & self::FLAG_NOT_CONNECTABLE)) {
107 $state = $this->client->connect($host, $port, $this->getSettings("bind"), $this->getSettings("ssl"), $this->getSettings("recv_timeout"));
109 usleep($this->getSetting("recv_timeout") / 1000);
110 $this->flags |= self::FLAG_NOT_CONNECTABLE;
114 $this->loginServer();
116 //try to receive new data from the Uplink
117 $lines = $this->client->recv();
118 if($lines == null) return;
119 foreach($lines as $line) {
120 $this->parseLine($line);
124 public function setUplink($host, $port, $ssl = false, $bind = null) {
125 $this->setSetting("host", $host);
126 $this->setSetting("port", $port);
127 $this->setSetting("ssl", $ssl);
128 $this->setSetting("bind", $bind);
131 public function setLoopTimeout($timeout) {
132 $this->setSetting("recv_timeout", $timeout);
135 public function setUplinkServer($numeric, $name, $password, $description) {
136 $this->setSetting("numeric", Numerics::intToNum($numeric, 2));
137 $this->setSetting("name", $name);
138 $this->setSetting("password", $password);
139 $this->setSetting("description", $description);
142 public function setValidateServer($name, $password) {
143 $this->setSetting("their_name", $name);
144 $this->setSetting("their_password", $password);
147 private function setSetting($setting, $value) {
148 $this->settings[$setting] = $value;
151 private function getSetting($setting) {
152 if(array_key_exists($setting, $this->settings)) {
153 return $this->settings[$setting];
159 private function loginServer() {
160 $password = $this->getSetting("password");
161 $this->send("PASS", $password);
162 $this->send("SERVER", $this->server->getName(), $this->server->getStartTime(), $this->server->getLinkTime(), $this->server->getNumeric(), $this->server->getDescription());
165 private function parseLine($line) {
166 $highExplode = explode(" :", $line, 2);
167 $tokens = explode(" ", $highExplode[0]);
168 if(count($highExplode) > 1)
169 $tokens[] = $highExplode[1];
170 $cmdPos = (($this->flags & self::FLAG_P10SESSION) ? 1 : 0);
171 if($cmdPos == 1) $from = $tokens[0];
173 $arguments = array_slice($tokens, $cmdPos + 1);
174 switch(strtoupper($tokens[$cmdPos])) {
177 $this->recv_pass($from, $arguments);
180 $this->recv_server($from, $arguments);
183 $this->recv_error($from, $arguments);
187 $this->recv_server($from, $arguments);
190 $this->recv_ping($from, $arguments);
193 $this->recv_nick($from, $arguments);
196 $this->recv_end_of_burst($from, $arguments);
199 $this->recv_end_of_burst_ack($from, $arguments);
208 private function send($command) {
209 if(func_num_args() > 1) {
210 $args = array_slice(func_get_args(), 1);
211 $command = P10Formatter::formatCMD($this->getSetting("numeric"), $command, $args);
213 $this->client->send($command);
216 /********************************************************************************************
217 * INCOMING COMMANDS *
218 ********************************************************************************************/
220 private function recv_pass($from, $args) {
221 $their_pass = $this->getSetting("their_password");
223 if($args[0] != $their_pass) {
225 $this->flags |= self::FLAG_SECURITY_QUIT;
226 $this->send("ERROR", "Incorrect password received.");
227 $this->client->disconnect();
232 private function recv_error($from, $args) {
233 //we received an error - the socket is dead for us, now
234 //maybe we want to log this, later
235 $this->client->disconnect();
238 private function recv_server($from, $args) {
241 $their_name = $this->getSetting("their_name");
242 if($their_name && $args[0] != $their_name) {
243 $this->flags |= self::FLAG_SECURITY_QUIT;
244 $this->send("ERROR", "Invalid Server name");
245 $this->client->disconnect();
248 $new_server = new P10_Server($args[0], substr($args[5],0,2), $this->server, $args[2], $args[3], $args[7]);
249 $this->server->add_server($new_server);
250 $this->flags |= self::FLAG_P10SESSION | self::FLAG_BURST_PENDING;
252 //another server got a new slave server ^^
253 $server = P10_Server::getServerByNum($from);
254 if($server == null) {
255 trigger_error("Parent Server (".$from.") does not exist or was not found on recv_server.", E_USER_ERROR);
258 $new_server = new P10_Server($args[0], substr($args[5],0,2), $server, $args[2], $args[3], $args[7]);
259 $server->add_server($new_server);
263 private function recv_ping($from, $args) {
264 $this->send("Z", $args[0]); //simply PONG
267 private function recv_nick($from, $args) {
268 if(count($args) == 2) {
270 $user = P10_User::getUserByNum($from);
272 trigger_error("Server tries to change the nick of an user that does not exist or was not found on recv_nick.", E_USER_ERROR);
275 $nick->setNick($args[0]);
278 $numeric = $args[count($args)-2];
280 $server = P10_Server::getServerByNum($from);
281 if($server == null) {
282 trigger_error("Server (".$from.") the User (".$nick.") is coming from does not exist or was not found on recv_nick.", E_USER_ERROR);
285 if(substr($numeric,0,2) != $from) {
286 trigger_error("A Server (".$from.") tries to connect a User with an invalid User numeric ('".$numeric."' does not belong to the Server)", E_USER_WARNING);
288 $connect_time = $args[2];
291 $modes = implode(" ",array_slice($args, 5, count($args)-8));
292 $modes = new UserModeSet($modes);
293 $ip = Numerics::parseIP($args[count($args)-3]);
294 $realname = $args[count($args)-1];
295 new P10_User($nick, $numeric, $server, $connect_time, $ident, $host, $ip, $realname, $modes);
299 private function recv_end_of_burst($from, $args) {
300 if(($this->flags & self::FLAG_BURST_PENDING)) {
303 $this->flags &= ~self::FLAG_BURST_PENDING;
307 private function recv_end_of_burst_ack($from, $args) {
308 //nothing to do here?
311 /********************************************************************************************
313 ********************************************************************************************/
315 private function burst() {
316 foreach($this->server->getUsers() as $user) {
317 $nick = $user->getNick();
318 $connect_time = $user->getConnectTime();
319 $ident = $user->getIdent();
320 $host = $user->getHost();
321 $modes = $user->getModes()->getModeString();
322 $ip = Numerics::numericFromIP($user->getIP());
323 $numeric = $user->getNumeric();
324 $realname = $user->getRealname();
325 $this->send("N", $nick, $connect_time, $ident, $host, $modes, $ip, $numeric, $realname);