/hypervm/httpdocs/live/main.php
PHP | 576 lines | 469 code | 60 blank | 47 comment | 82 complexity | 55cba5e4b753c76679bef8cc4c389fcd MD5 | raw file
1<?php
2 if (preg_match("/Mozilla\/\d.+Compatible; MSIE/i", $_SERVER['HTTP_USER_AGENT']) && !preg_match("/Opera/i", $_SERVER['HTTP_USER_AGENT'])) {
3 header('Expires: 0');
4 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
5 header('Pragma: public');
6 } else {
7 header('Expires: 0');
8 header('Pragma: no-cache');
9 }
10
11include("common.php");
12include("ircfunc.php");
13
14$gl_never_responded = 1;
15$gl_client_started = 0;
16$livetrfp = null;
17if ($g_login) {
18 $livetrfp = lfopen("__path_program_etc/livetranscript.txt", "a");
19}
20/*
21$channel = $_GET['channel'];
22echo "$channel <br>";
23if (!$channel) {
24 $channel = $channels[0];
25}
26*/
27
28
29if ($redirect)
30 $channel = "{$chanbase}-command";
31else
32 $channel = $chanbase;
33
34
35$licid = $_GET['licid'];
36
37$user = $_GET['username'];
38$id = $_GET['id'];
39
40$base_nick = $_GET['username'];
41//$nick = "hello";
42//$serv_addr = "127.0.0.1";
43//$serv_port = 6667;
44
45echo <<<EOF
46<html>
47<head>
48<title>PHP IRC Chat output page</title>
49$css
50</head>
51<body bgcolor="$chan_bg" text="$chan_fg">
52
53EOF;
54flush();
55$smily_code = array("/:-?\)/",
56 "/;b/",
57 "/:-?P/i",
58 "/;-?\)/i",
59 "/:-?\(/",
60 "/:-?D/i",
61 "/:-?\|/i",
62 "/\(y\)/i",
63 "/:-?S/i");
64$smily_repl = array("<img src='pics/sm-smile.gif' width=15 height=15>",
65 "<img src='pics/sm-bsmile.gif' width=15 height=15>",
66 "<img src='pics/sm-psmile.gif' width=15 height=15>",
67 "<img src='pics/sm-wink.gif' width=15 height=15>",
68 "<img src='pics/sm-sad1.gif' width=15 height=15>",
69 "<img src='pics/sm-dsmile.gif' width=15 height=15>",
70 "<img src='pics/sm-amazed.gif' width=15 height=15>",
71 "<img src='pics/sm-thumbsup.gif' width=15 height=15>",
72 "<img src='pics/sm-sad2.gif' width=15 height=15>");
73
74@set_time_limit(3600); // ten minutes execution time, when user says or does something, this is reset with 10 min.
75/* ** ** set_time_limit() doesn't seem to have any effect whatsoever... ** ** */
76
77register_shutdown_function("einde");
78
79echo <<<EOF
80<script language="JavaScript"><!--; parent.scrl(1); //--></script>
81EOF;
82
83
84$socket = irc_open($serv_addr, $serv_port, $errno, $errstr);
85
86if ($socket < 0) { echo "failed... $errno: $errstr"; return; }
87else { echo "Connecting to Lxlabs Live. Please Wait....<br> "; }
88
89
90$max_per_personnel = $_REQUEST['max_per_personnel'];
91$max_number_personnel = $_REQUEST['max_number_personnel'];
92$per_personnel = 1;
93$number_personnel = 1;
94if ($redirect) {
95 $nick = "{$base_nick}_{$number_personnel}-{$per_personnel}";
96} else {
97 $nick = $base_nick;
98}
99
100echo "<br>";
101echo "<script language='JavaScript'>\n<!--;\n parent.scrl(1);\n //-->\n</script>";
102flush(); //output this;
103
104$out = "";
105$login_flag = 0;
106$loggedin = false;
107$nickcount = 0;
108$inchan = false;
109$nicktry = 0;
110$signontime = time();
111function einde() {
112 global $link, $user, $id;
113 global $socket,$signontime;
114 if ($socket) {
115 irc_write($socket, "QUIT :lxlabs \r\n");
116 irc_close($socket);
117 }
118 sqlite_query($link, "DELETE FROM phpchat WHERE username = '$user' AND id = '$id'");
119 $signofftime = time();
120 $onlinetime = $signofftime-$signontime;
121
122 $d1 = (floor($onlinetime/3600) < 10) ? "0".floor($onlinetime/3600) : floor($onlinetime/3600);
123 $rest = $onlinetime%3600;
124 $d1 .= (floor($rest/60) < 10) ? ":0".floor($rest/60) : ":".floor($rest/60);;
125 $rest = $rest%60;
126 $d1 .= ($rest < 10) ? ":0".$rest : ":".$rest;
127 echo "Signed on at: " . date("H:i:s d-m-Y", $signontime) . ", Signed off at: " . date("H:i:s d-m-Y", $signofftime) . "<br>";
128 echo "Online time: $d1 ($onlinetime seconds)";
129}
130
131
132function retn_color($fg, $bg) {
133 global $ircColors;
134 if ($bg != -1) {
135 return "<font style='color: ".$ircColors[$fg]."; background-color: ".$ircColors[$bg].";'>";
136 } else {
137 return "<font style='color: ".$ircColors[$fg].";'>";
138 }
139}
140function smile_repl($string) {
141 return $string;
142 global $smily_code, $smily_repl, $ircColors, $page_bg, $page_fg;
143 $string = preg_replace("/\003(\d+),(\d+)/e", "retn_color($1,$2)", $string);
144 $string = preg_replace("/\003(\d+)/e", "retn_color($1, -1)", $string);
145 $string = preg_replace("/\003/", "<font style='color: $page_fg; background-color: $page_bg;'>", $string);
146 for ($a = 0; $a < substr_count($string, "<font"); $a++) {
147 $string .= "</font>";
148 }
149 return preg_replace($smily_code, $smily_repl, $string);
150}
151
152$link = lsqlite_open($g_db_file);
153
154$fully_logged_in = 0;
155$timetoping = 10; // every tenth loop ask a ping reply
156$gl_timer = 0;
157$gl_nonavailable_printed = false;
158while($socket > 0) {
159
160 $read = null;
161 $read[0] = $socket;
162 $writea = null;
163 $excpta = null;
164 /*
165 foreach((array) $client as $c) {
166 $read[] = $c['sock'];
167}
168*/
169 //dprint("Before: ");
170 //dprintr($read);
171 // Set up a blocking call to stream_select()
172 $ready = stream_select($read, $writea, $excpta, 1);
173
174 //dprint("After: $ready");
175 //dprintr($read);
176
177 // This means that sock - which is our main master socket - is ready for reading, which in turn signifies that a NEW connection has arrived. The other members of the read array
178 $full = null;
179 if ($ready) {
180 if (in_array($socket, $read)) {
181 $full = irc_read($socket, 4096);
182 }
183 }
184 $full = rtrim($full);
185
186 $timeout = 30;
187
188 if ($gl_never_responded && $gl_client_started) {
189 $gl_timer++;
190 if ($gl_timer == $timeout) {
191 irc_write($socket, "PRIVMSG $channel :lxclmsg2 No response for 50 sec\r\n");
192 //print($gl_timer);
193 }
194 if ($gl_timer % (2 * $timeout) == 0) {
195 //print($gl_timer);
196 irc_write($socket, "PRIVMSG $channel :lxclmsg3 No response for $gl_timer/20 sec\r\n");
197 }
198 if ($gl_timer % (6 * $timeout) == 0) {
199 //print($gl_timer);
200 irc_write($socket, "PRIVMSG $channel :lxclmsg3 No response for $gl_timer/20 sec\r\n");
201 if (!$gl_nonavailable_printed) {
202 print("It appears there's no one in the channel. Please leave your message here, or please contact lxhelp@lxlabs.com. We apologize for the inconvenience <br> \n");
203 $gl_nonavailable_printed = true;
204 } else {
205 print("...");
206 }
207 }
208 }
209
210 $list = explode("\n", $full);
211 foreach($list as $out) {
212 if (strlen($out) > 1) {
213 //print($out . "<br> ");
214 if (preg_match("/PING (.+)/", $out, $matches)) {
215 irc_write($socket, "PONG $matches[1]\r\n");
216 //echo "ping-pong<br>";
217
218 } elseif (preg_match("/:([^\s]+) NOTICE ([^\s]+) :(.+)/", $out, $matches)) {
219 if (preg_match("/$nick/i", $matches[3])) {
220 $matches[1] = "<b>$matches[1]</b>";
221 }
222 $src = $matches[1];
223 $text = smile_repl(htmlspecialchars($matches[3]));
224 if (preg_match("/([^!]+)!.+/", $src, $matches)) {
225 $src = $matches[1];
226 }
227 //echo "<font color='$ircColors[5]'>-$src- $text</font><br>";
228
229 } elseif (preg_match("/:[^ ]* (\d+) ([^\s]+) (.+)/i", $out, $matches)) {
230
231 if ($matches[1] == "006" || $matches[1] == "001") {
232 if ($redirect) {
233 $cmdchan = "{$chanbase}_{$number_personnel}-command" ;
234 $realchan = $chanbase . "_" . $number_personnel . "-" . $per_personnel ;
235 irc_write($socket, "JOIN #$cmdchan\r\n");
236 irc_write($socket, "PRIVMSG #$cmdchan :lxcommand channel #$realchan\r\n");
237 irc_write($socket, "PART #$cmdchan\r\n");
238 //print($realchan);
239 irc_write($socket, "JOIN #$realchan\r\n");
240 } else {
241 irc_write($socket, "JOIN #$chanbase\r\n");
242 }
243 //print("gotcha<br> \n");
244 }
245 if ($matches[1] == "PONG") {
246 }
247
248 if ($matches[1] == "376") {
249 /*end of motd*/ $loggedin = true;
250 }
251
252 elseif($matches[1] == "433") {
253 //print($out);
254 if ($redirect) {
255 if ($number_personnel == $max_number_personnel) {
256 $number_personnel = 1;
257 if ($per_personnel == $max_per_personnel) {
258 $per_personnel = 1;
259 $number_personnel = 1;
260 print("All Personnel currently busy. Will try after 30 seconds.... <br> \n");
261 flush();
262 sleep(30);
263 } else {
264 $per_personnel++;
265 }
266 } else {
267 $number_personnel++;
268 }
269 $nick = "{$base_nick}_{$number_personnel}-{$per_personnel}";
270 } else {
271
272 $nickcount++;
273 $nick = $base_nick . "$nickcount";
274 }
275 //echo "<font color='$ircColors[7]'>Nick already in use, changing to: $nick</font><br>\n";
276 irc_write($socket, "NICK :$nick\r\n");
277 }
278
279
280 elseif ($matches[1] == "422") {
281 /* no motd, but logged in */ $loggedin = true;
282 }
283 elseif ($matches[1] == "353") { //names
284 if (preg_match("/= (\#[^\s]+) :(.+)/", $matches[3], $match)) {
285 $namen = $match[2];
286 //$namen = str_replace("@", "", $match[2]);
287 //$namen = str_replace("%", "", $namen);
288 //$namen = str_replace("+", "", $namen);
289 $names = preg_split("/\s+/", $namen);
290 natcasesort($names);
291 if (!isset($nicklist["$match[1]"])) {
292 $nicklist["$match[1]"] = "";
293 }
294 foreach($names as $name) {
295 //echo "$name, ";
296 $name = str_replace("@", "", $name);
297 if ($redirect) {
298 if ($name == $nick) {
299 $name = "Myself";
300 }
301 }
302 $nicklist["$match[1]"] .= "$name:";
303 }
304
305
306 }
307
308 } elseif ($matches[1] == "366") { // endofnames
309 if (preg_match("/(#[^\s]+)/", $matches[3], $match)) {
310
311 if (isset($nicklist[$match[1]])) {
312 $namelist = $nicklist[$match[1]];
313 echo "\n<script language='JavaScript'>\n<!--;\n\nparent.nixreload(':$namelist');\n\n//-->\n</script>\n\n";
314 }
315 $nicklist[$match[1]] = "";
316 $_tnlist = explode(":", $namelist);
317
318 echo "</font>";
319 if (!$fully_logged_in) {
320
321 if ($licid !== 'nobody') {
322 echo "Hello $licid, ";
323 }
324
325 echo "please type your message to start the conference <br> \n";
326 $fully_logged_in = 1;
327 }
328 flush();
329 }
330 } elseif ($matches[1] == "332") {
331 if (preg_match("/(#[^\s]+) :(.+)/", $matches[3], $match)) {
332 //echo "<font color='$ircColors[7]'>--- Topic for $match[1] is: $match[2]</font><br>";
333 }
334 } elseif (($matches[1] == "372" || $matches[1] == "375") && $hide_motd) {
335 // do nothing, the motd doesn't have to be displayed...
336 } elseif ($matches[1] == "317") {
337 // whois idle time and signon time
338 if (preg_match("/([^\s]+)\s+(\d+)\s+(\d+)\s+:.+/", $matches[3], $match)) {
339 $d1 = (floor($match[2]/3600) < 10) ? "0".floor($match[2]/3600) : floor($match[2]/3600);
340 $rest = $match[2]%3600;
341 $d1 .= (floor($rest/60) < 10) ? ":0".floor($rest/60) : ":".floor($rest/60);;
342 $rest = $rest%60;
343 $d1 .= ($rest < 10) ? ":0".$rest : ":".$rest;
344 $d2 = date("Y-m-d H:i:s", $match[3]);
345 //echo "<font color='$ircColors[12]'>-$serv_name- $match[1] idle: $d1, signon: $d2</font><br>";
346 }
347 } else {
348 //echo "<font color='$ircColors[12]'>-$serv_name- $matches[3]</font><br>";
349 }
350
351 } elseif (preg_match("/Closing Link(.*)/i", $out, $matches)) {
352 //echo "<font color='$ircColors[4]'>Disconnected$matches[1]...</font><br>\n";
353 irc_close($socket);
354 sleep(60);
355 $socket = irc_open($serv_addr, $serv_port, $errno, $errstr);
356
357 if ($socket < 0) { echo "failed... $errno: $errstr"; return; }
358 else { echo "Connecting Again....<br> "; }
359
360 $login_flag = 0;
361 $loggedin = false;
362 $inchan = false;
363 $nicktry = 0;
364 flush();
365 continue;
366
367 } elseif (preg_match("/:([^!]+)![^\s]+ PRIVMSG ([^\s]+) :(.+)/", $out, $matches)) {
368 if (preg_match("/$nick/i", $matches[3])) { $matches[1] = "<b>$matches[1]</b>"; }
369 $matches[3] = smile_repl(htmlspecialchars($matches[3]));
370
371 if (preg_match("/\001(\w+)(.*)/i", $matches[3], $match)) { // CTCP's
372 if ($match[1] == "VERSION") {
373 irc_write($socket, "NOTICE $matches[1] :\001VERSION \r\n");
374 } elseif ($match[1] == "PING") {
375 irc_write($socket, "NOTICE $matches[1] :\001PING$match[2]\r\n");
376 } elseif ($match[1] == "CLIENTINFO") {
377 irc_write($socket, "NOTICE $matches[1] :\001CLIENTINFO ip: {$_SERVER['REMOTE_ADDR']} ; {$HTTP_SERVER_VARS['REMOTE_HOST']}\001\r\n");
378 irc_write($socket, "NOTICE $matches[1] :\001CLIENTINFO useragent: {$_SERVER['HTTP_USER_AGENT']}\001\r\n");
379 } elseif ($match[1] == "ACTION") {
380 $matches[3] = substr($matches[3],7);
381 echo "<font color='$ircColors[6]'>* $matches[1] $matches[3]</font><br>";
382 } elseif ($match[1] == "DCC") {
383 preg_match("/^[\W]*(\w+)\s+([^\s]+)\s+\d+\s+\d+[\s\d]*/", "$match[2]", $blaat);
384 //echo "<font color='$ircColors[5]'>-- Ignored DCC from $matches[1] ($blaat[1] $blaat[2])</font><br>";
385 //irc_write($socket, "NOTICE $matches[1] :Sorry, but my client (PHPWebchat) doesn't support DCC transfers.\r\n");
386 } else {
387 echo "CTCP: $matches[3]<br>";
388 }
389 } elseif (!preg_match("/^#.+/", $matches[2])) {
390 echo "<<font color='$ircColors[7]'>$matches[1]->$nick</font>> $matches[3]<br>";
391 } else {
392
393 $msg = preg_replace("/\s+/", " ", $matches[3]);
394 $msglist = explode(" ", $msg);
395 if ($msglist[0] == "lxbuzz")
396 continue;
397
398 $msg = "";
399 if (isset($msglist[0]) && $msglist[0] == "lxquit") {
400 einde();
401 exit(0);
402 }
403 foreach($msglist as $m) {
404 $msg .= " " . $m;
405 }
406 if ($matches[1] == $nick) {
407 print("<font color=blue><$matches[1]> </font> ");
408 } else {
409 print("<font color=black><$matches[1]> </font> ");
410 }
411 print(" $msg<br>");
412 if ($livetrfp) {
413 fwrite($livetrfp, "$matches[1]: $msg\n");
414 }
415
416 $gl_never_responded = 0;
417
418 }
419
420 } elseif (preg_match("/:([^!]+)![^\s]+ NICK :(.+)/", $out, $matches)) {
421 if ($nick == $matches[1]) {
422 echo "<font color='$ircColors[3]'>-=- You are now known as $matches[2]</font><br>";
423 } else {
424 echo "<font color='$ircColors[3]'>-=- $matches[1] is now known as $matches[2]</font><br>";
425 }
426 irc_write($socket, "NAMES $matches[2]\r\n");
427
428 } elseif (preg_match("/:([^!]+)![^\s]+ JOIN :(.+)/", $out, $matches)) {
429 //echo "<font color='$ircColors[3]'>--> $matches[1] has joined the channel</font><br>";
430 $channel = $matches[2];
431 $res = sqlite_query($link, "delete from channel where user = '$user' and id = '$id';");
432 $res = sqlite_query($link, "insert into channel (user, id, channel) values ('$user', '$id', '$channel');");
433 $inchan = true;
434 /*
435 if ($matches[1] != $nick) {
436 irc_write($socket, "NAMES $matches[2]\r\n");
437 }
438 */
439 irc_write($socket, "NAMES $matches[2]\r\n");
440
441 } elseif (preg_match("/:([^!]+)![^\s]+ PART (.+)/", $out, $matches)) {
442 if (preg_match("/(#[^\s]+) :(.+)/", $matches[2], $match)) {
443 //echo "<font color='$ircColors[3]'><-- $matches[1] has left $match[1] ($match[2])</font><br>";
444 } else {
445 //echo "<font color='$ircColors[3]'><-- $matches[1] has left $matches[2]</font><br>";
446 }
447 irc_write($socket, "NAMES $matches[2]\r\n");
448
449 } elseif (preg_match("/:([^!]+)![^\s]+ QUIT :(.*)/", $out, $matches)) {
450 //echo "<font color='$ircColors[3]'><-- $matches[1] has left</font><br>";
451 irc_write($socket, "NAMES #$channel\r\n");
452
453 } elseif (preg_match("/:([^!]+)![^\s]+ TOPIC (#[^\s]+) :(.*)/", $out, $matches)) {
454 //echo "<font color='$ircColors[7]'>--- $matches[1] changed the topic for $matches[2] to: $matches[3]</font><br>";
455
456 } elseif (preg_match("/:([^!]+)![^\s]+ MODE (#[^\s]+) ([^\s]+) (.+)/", $out, $matches)) {
457 //echo "<font color='$ircColors[7]'>--- $matches[1] sets mode $matches[3] on $matches[4]</font><br>";
458 irc_write($socket, "NAMES $matches[2]\r\n");
459
460 } elseif (preg_match("/:([^\s]+) MODE ([^\s]+) :(.+)/", $out, $matches)) {
461 //echo "<font color='$ircColors[7]'>--- $matches[1] sets mode $matches[3] on $matches[2]</font><br>";
462
463 } else {
464 //echo "$out<br>";
465 }
466 echo "\n";
467
468 }
469 }
470 /**********************************************************************************************/
471
472
473
474
475
476 $result = sqlite_query("SELECT * FROM phpchat WHERE username = '$user' AND id = '$id' ORDER BY tijd", $link);
477 $a = 0;
478 while ($rij = sqlite_fetch_array($result, SQLITE_ASSOC)) {
479 $a++;
480 $var = "DELETE FROM phpchat WHERE username = '$user' AND id = '$id' AND tijd = '{$rij['tijd']}'";
481 sqlite_query($link, $var);
482 if (preg_match("/(PRIVMSG) ([^\s]+) :(.+)/i", $rij['commando'], $match) || preg_match("/(NOTICE) ([^\s]+) :(.+)/i", $rij['commando'], $match)) {
483 $match[3] = smile_repl($match[3]);
484 if ($match[1] == "PRIVMSG") {
485 if (preg_match("/\001ACTION ([^\001]+)\001/i", $match[3], $mat)) {
486 echo "<font color='$ircColors[6]'>* $nick $mat[1]</font><br>";
487 } elseif (preg_match("/\001(.+)\001/", $match[3], $mat)) { // CTCPs
488 echo "<font color='$ircColors[5]'>CTCP $match[2] $mat[1]</font><br>";
489
490 } elseif (preg_match("/^#.+/", $match[2])) { // The main client conversation
491 if ($redirect) {
492 echo "<font color=blue><Myself> $match[3] </font> <br>";
493 if ($livetrfp) {
494 fwrite($livetrfp, "Myself: $match[3]\n");
495 }
496 } else {
497 echo "<font color=blue><$nick> $match[3] </font> <br>";
498 }
499 if (!$gl_client_started) {
500 $gl_client_started = 1;
501 irc_write($socket, "PRIVMSG $channel :lxclmsg1 Client $licid has started talking \r\n");
502 print("Connecting to the chat personnel... Please wait <br> \n");
503 }
504 } else {
505 echo "<<font color='$ircColors[7]'>$nick->$match[2]</font>> $match[3]<br>";
506 }
507 } elseif ($match[1] == "NOTICE") {
508 echo "<font color='$ircColors[5]'>>$match[2]< $match[3]</font><br>";
509 } else {
510 echo "<font color=blue><Myself> $match[2] </font> <br>";
511 }
512 }
513 if (preg_match("/^NICK (.+)/i", $rij['commando'], $match)) {
514 $nick = $match[1];
515 irc_write($socket, "NAMES $channel\r\n");
516 flush();
517 }
518 irc_write($socket, "{$rij['commando']}\r\n");
519 if (preg_match("/^QUIT.*/", $rij['commando'])) {
520 echo "<font color='$ircColors[4]'>Disconnected...</font><br>";
521 break 2;
522 break 2;
523 }
524 echo "\n";
525 }
526 if ($a > 0) { @set_time_limit(3600); /* ten minutes extra to say sth */ }
527 /**********************************************************************************************/
528 if ($login_flag == 0) {
529 $name = $_SERVER['REMOTE_ADDR'];
530 irc_write($socket, "USER phpchat {$_SERVER['REMOTE_ADDR']} $name :$name \r\nNICK :".$nick."\r\n");
531 $login_flag = 1;
532 }
533 if (connection_aborted()) {
534 echo "<font color='$ircColors[4]'>Disconnected...</font><br>";
535 break 2;
536 }
537 echo "<!-- -->"; // keep connection alive
538
539 $timetoping--;
540 if ($timetoping < 0) {
541 $timetoping = 10;
542 irc_write($socket, "PING LAG".time() . "\r\n");
543 }
544 flush_server_buffer();
545 flush(); //output all...
546
547// sleep(1); // ony sleep() works on windoze apache :/
548}
549einde();
550
551function getGreeter($nicklist, $nick)
552{
553 foreach($nicklist as $n) {
554 $n = trim($n);
555 if (!$n) {
556 continue;
557 }
558 if ($n === $nick) {
559 continue;
560 }
561 if ($n === 'Myself') {
562 continue;
563 }
564 if (cse($n, "|aw")) {
565 continue;
566 }
567 return "Connected...<br><br> <> Type your message to start the conference. <br>";
568 }
569
570 return "Connected...<br><br> <Leave Message> I am not available now. Please leave a message. <br>";
571
572
573}
574?>
575</body>
576</html>