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 * void setEventHandler(EventHandler $event_handler)
51 * sets the EventHandlder
53 require_once("Client.class.php");
54 require_once("Numerics.class.php");
55 require_once("P10Formatter.class.php");
56 require_once("P10_Server.class.php");
57 require_once("P10_User.class.php");
58 require_once("P10_Channel.class.php");
59 require_once("P10_ModeSets.class.php");
63 private $settings = array();
66 const FLAG_P10SESSION = 0x0001; //connection is in P10 mode (server is connected)
67 const FLAG_SECURITY_QUIT = 0x0002; //local connection abort because of security issues
68 const FLAG_NOT_CONNECTABLE = 0x0004; //remote server is not connectable
69 const FLAG_BURST_PENDING = 0x0008; //we still have to burst
70 const FLAG_CONNECTED = 0x0010; //connected and synced (ready)
71 const FLAG_GOT_PASS = 0x0020; //got PASS from the remote Server
74 public function __construct() {
75 $this->client = new Client();
76 $this->setSetting("recv_timeout", 1000);
79 public function initialize() {
81 trigger_error("Uplink already initialized.", E_USER_ERROR);
84 $self_numeric = $this->getSetting("numeric");
85 $self_name = $this->getSetting("name");
86 $self_description = $this->getSetting("description");
87 if(!$self_numeric || !$self_name) {
88 trigger_error("Server Settings missing.", E_USER_ERROR);
91 $this->server = new P10_Server($self_name, $self_numeric, null, time(), time(), $self_description);
94 public function loop() {
95 if($this->server == null) {
96 trigger_error("Uplink not initialized.", E_USER_ERROR);
99 if(!$this->client->connected()) {
100 if(($this->flags & self::FLAG_P10SESSION)) {
101 //Server got disconnected
102 $this->server->disconnectServer(true);
103 $this->flags &= ~self::FLAG_P10SESSION;
105 $host = $this->getSetting("host");
106 $port = $this->getSetting("port");
107 if($host == null || $port == null) {
108 trigger_error("Uplink Settings missing.", E_USER_ERROR);
111 if(($this->flags & self::FLAG_SECURITY_QUIT) || ($this->flags & self::FLAG_NOT_CONNECTABLE)) {
114 $state = $this->client->connect($host, $port, $this->getSetting("bind"), $this->getSetting("ssl"), $this->getSetting("recv_timeout"));
116 usleep($this->getSetting("recv_timeout") / 1000);
117 $this->flags |= self::FLAG_NOT_CONNECTABLE;
121 $this->loginServer();
123 //try to receive new data from the Uplink
124 $lines = $this->client->recv();
125 if($lines == null) return;
126 foreach($lines as $line) {
127 $this->parseLine($line);
131 public function setUplinkHost($host, $port, $ssl = false, $bind = null) {
132 $this->setSetting("host", $host);
133 $this->setSetting("port", $port);
134 $this->setSetting("ssl", $ssl);
135 $this->setSetting("bind", $bind);
138 public function setLoopTimeout($timeout) {
139 $this->setSetting("recv_timeout", $timeout);
142 public function setUplinkServer($numeric, $name, $password, $description) {
143 $this->setSetting("numeric", Numerics::intToNum($numeric, 2));
144 $this->setSetting("name", $name);
145 $this->setSetting("password", $password);
146 $this->setSetting("description", $description);
149 public function setValidateServer($name, $password) {
150 $this->setSetting("their_name", $name);
151 $this->setSetting("their_password", $password);
154 private function setSetting($setting, $value) {
155 $this->settings[$setting] = $value;
158 private function getSetting($setting) {
159 if(array_key_exists($setting, $this->settings)) {
160 return $this->settings[$setting];
166 private function loginServer() {
167 $password = $this->getSetting("password");
168 $this->send("PASS", $password);
169 $this->send("SERVER", $this->server->getName(), $this->server->getStartTime(), $this->server->getLinkTime(), $this->server->getNumeric(), $this->server->getDescription());
172 private function parseLine($line) {
173 $highExplode = explode(" :", $line, 2);
174 $tokens = explode(" ", $highExplode[0]);
175 if(count($highExplode) > 1)
176 $tokens[] = $highExplode[1];
177 $cmdPos = (($this->flags & self::FLAG_P10SESSION) ? 1 : 0);
178 if($cmdPos == 1) $from = $tokens[0];
180 $arguments = array_slice($tokens, $cmdPos + 1);
181 switch(strtoupper($tokens[$cmdPos])) {
184 $this->recv_pass($from, $arguments);
187 $this->recv_server($from, $arguments);
190 $this->recv_error($from, $arguments);
194 $this->recv_server($from, $arguments);
197 $this->recv_ping($from, $arguments);
200 $this->recv_nick($from, $arguments);
203 $this->recv_end_of_burst($from, $arguments);
206 $this->recv_end_of_burst_ack($from, $arguments);
209 $this->recv_server_quit($from, $arguments);
212 $this->recv_quit($from, $arguments);
215 $this->recv_burst($from, $arguments);
224 private function send($command) {
225 if(func_num_args() > 1) {
226 $args = array_slice(func_get_args(), 1);
227 $command = P10Formatter::formatCMD($this->getSetting("numeric"), $command, $args);
229 $command = P10Formatter::formatCMD($this->getSetting("numeric"), $command, array());
231 $this->client->send($command);
234 /********************************************************************************************
235 * INCOMING COMMANDS *
236 ********************************************************************************************/
238 private function recv_pass($from, $args) {
239 $their_pass = $this->getSetting("their_password");
241 if($args[0] != $their_pass) {
243 $this->flags |= self::FLAG_SECURITY_QUIT;
244 $this->send("ERROR", "Incorrect password received.");
245 $this->client->disconnect();
248 $this->flags |= self::FLAG_GOT_PASS;
252 private function recv_error($from, $args) {
253 //we received an error - the socket is dead for us, now
254 //maybe we want to log this, later
255 $this->client->disconnect();
258 private function recv_server($from, $args) {
261 $their_name = $this->getSetting("their_name");
262 if($their_name && $args[0] != $their_name) {
263 $this->flags |= self::FLAG_SECURITY_QUIT;
264 $this->send("ERROR", "Invalid Server name");
265 $this->client->disconnect();
268 if($this->getSetting("their_password") && !($this->flags & self::FLAG_GOT_PASS)) {
269 $this->flags |= self::FLAG_SECURITY_QUIT;
270 $this->send("ERROR", "PASS missing.");
271 $this->client->disconnect();
274 $new_server = new P10_Server($args[0], substr($args[5],0,2), $this->server, $args[2], $args[3], $args[7]);
275 $this->server->addServer($new_server);
276 $this->flags |= self::FLAG_P10SESSION | self::FLAG_BURST_PENDING;
278 //another server got a new slave server ^^
279 $server = P10_Server::getServerByNum($from);
280 if($server == null) {
281 trigger_error("Parent Server (".$from.") does not exist or was not found on recv_server.", E_USER_ERROR);
284 $new_server = new P10_Server($args[0], substr($args[5],0,2), $server, $args[2], $args[3], $args[7]);
285 $server->addServer($new_server);
289 private function recv_ping($from, $args) {
290 $this->send("Z", $args[0]); //simply PONG
293 private function recv_nick($from, $args) {
294 if(count($args) <= 2) {
296 $user = P10_User::getUserByNum($from);
298 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);
301 $nick->setNick($args[0]);
304 $numeric = $args[count($args)-2];
306 $server = P10_Server::getServerByNum($from);
307 if($server == null) {
308 trigger_error("Server (".$from.") the User (".$nick.") is coming from does not exist or was not found on recv_nick.", E_USER_ERROR);
311 if(substr($numeric,0,2) != $from) {
312 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);
314 $connect_time = $args[2];
317 $modes = implode(" ",array_slice($args, 5, count($args)-8));
318 $modes = new P10_UserModeSet($modes);
319 $ip = Numerics::parseIP($args[count($args)-3]);
320 $realname = $args[count($args)-1];
321 new P10_User($nick, $numeric, $server, $connect_time, $ident, $host, $ip, $realname, $modes);
325 private function recv_end_of_burst($from, $args) {
326 if(($this->flags & self::FLAG_BURST_PENDING)) {
329 $this->flags &= ~self::FLAG_BURST_PENDING;
333 private function recv_end_of_burst_ack($from, $args) {
334 $this->flags |= self::FLAG_CONNECTED;
337 private function recv_server_quit($from, $args) {
338 $server = P10_Server::getServerByName($args[0]);
339 if($server == null) {
340 trigger_error("Server (".$args[0].") not found.", E_USER_ERROR);
343 $server->disconnectServer();
346 private function recv_quit($from, $args) {
347 $user = P10_User::getUserByNum($from);
349 trigger_error("Server tries to quit an user that does not exist or was not found on recv_quit.", E_USER_ERROR);
352 $user->quit($args[0]);
355 private function recv_burst($from, $args) {
357 $create_time = $args[1];
358 $channel = P10_Channel::getChannelByName($name);
360 $channel = new P10_Channel($name);
361 $modes = $channel->getModes();
362 $userstr = $args[count($args)-1];
363 $modeparamcount = count($args)-3;
364 if($userstr[0] == "%") {
366 $banlist = explode(" ", substr($userstr, 1));
367 foreach($banlist as $ban) {
370 $userstr = $args[count($args)-2];
373 if($userstr[0] == "+") { //MODE String
377 $users = explode(",",$userstr);
378 $isop = false; $isvoice = false;
379 foreach($users as $user) {
380 $uexp = explode(":", $user);
381 if(strlen($uexp[0]) != 5) {
382 trigger_error("burst parse error: '".$uexp[0]."' is not an user numeric.", E_USER_ERROR);
385 if(count($uexp) > 1) {
388 for($i = 0; $i < strlen($uexp[1]); $i++) {
389 if($uexp[1][0] == "@") $isop = true;
390 if($uexp[1][0] == "+") $isvoice = true;
393 $user = P10_User::getUserByNum($uexp[0]);
395 trigger_error("burst parse error: cant find User '".$uexp[0]."'.", E_USER_ERROR);
398 $channel->burstUser($user, $isop, $isvoice);
400 $modes->parseModes(implode(array_slice($args, 2, $modeparamcount)));
403 /********************************************************************************************
405 ********************************************************************************************/
407 private function burst() {
408 foreach($this->server->getUsers() as $user) {
409 $nick = $user->getNick();
410 $connect_time = $user->getConnectTime();
411 $ident = $user->getIdent();
412 $host = $user->getHost();
413 $modes = $user->getModes()->getModeString();
414 $ip = Numerics::numericFromIP($user->getIP());
415 $numeric = $user->getNumeric();
416 $realname = $user->getRealname();
417 $this->send("N", $nick, $connect_time, $ident, $host, $modes, $ip, $numeric, $realname);
419 foreach(P10_Channel::getChannels() as $channel) {
420 $sorted_users = array('ov' => array(), 'o' => array(), 'v' => array(), '-' => array());
421 $local_users = false;
422 foreach($channel->getUsers() as $user) {
423 if(substr($user->getNumeric(), 0, 2) != $this->server->getNumeric()) continue; //skip users that are not on the local server
424 $privs = $channel->getUserPrivs($user);
426 if(($privs & P10_Channel::USERPRIV_OPED)) $strPrivs .= "o";
427 if(($privs & P10_Channel::USERPRIV_VOICE)) $strPrivs .= "v";
428 if($strPrivs == "") $strPrivs = "-";
430 $sorted_users[$strPrivs][] = $user;
432 if(!$local_users) continue;
434 foreach($sorted_users['-'] as $user) {
435 if($userStr != "") $userStr.=",";
436 $userStr .= $user->getNumeric();
438 foreach($sorted_users['ov'] as $i => $user) {
439 if($userStr != "") $userStr.=",";
440 $userStr .= $user->getNumeric();
441 if($i == 0) $userStr .= ":ov";
443 foreach($sorted_users['o'] as $i => $user) {
444 if($userStr != "") $userStr.=",";
445 $userStr .= $user->getNumeric();
446 if($i == 0) $userStr .= ":o";
448 foreach($sorted_users['v'] as $i => $user) {
449 if($userStr != "") $userStr.=",";
450 $userStr .= $user->getNumeric();
451 if($i == 0) $userStr .= ":v";
454 //TODO: Build ban String
456 $modeString = $channel->getModes()->getModeString();
457 if($modeString != "+") {
458 $burstString .= $modeString;
461 if($burstString != "") $burstString .= " ";
462 $burstString .= $userStr;
464 if($banString != "") {
465 if($burstString != "") $burstString .= " ";
466 $burstString .= ":%".$banString;
468 $this->send("B", $channel->getName(), $channel->getCreateTime(), $burstString);