-
Notifications
You must be signed in to change notification settings - Fork 1
/
irclog.php
1231 lines (1044 loc) · 36.4 KB
/
irclog.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// phpcs:disable
// array of first names
define('NAMES', explode("\n", file_get_contents(__DIR__.'/scroller_data/nimilista.txt')));
// name count minus one to ignore empty name at the end of the name list
define('NAME_COUNT', count(NAMES) - 1);
// kate: space-indent true; encoding utf-8; indent-width 4;
$rev = '$Id$';
// Storage driver. currently supported:
// * DB - uses pear DB layer
// * logfile - reads messages from logfile
$storage = 'logfile';
// Default channel. Currently affects only on DB, and an topic change display.
$channel = "#pulina";
//
// logfile storage
//
ini_set( 'date.timezone', 'Europe/Helsinki' );
if ( file_exists( '/var/www/pulina/test.log' ) ) {
$logfile = '/var/www/pulina/test.log';
} else {
$logfile = '/var/www/pulina.fi/public_html/pulina-days/pul-'.strftime('%Y-%m-%d').'.log';
}
// Starting offset in bytes (how many bytes are readed from end of file?)
$startoffsetbytes = 6000;
// Format of logfile. Currently supported:
// * mirc - For those who use m-IRC (or compatible logfile format)
// * irssi - For default irssi logfiles
// * egg - Eggdrop logfile (set quick-logs 1 on eggdrop to see dynamic updates)
$logfileformat = 'irssi';
//
// DB storage
//
// DB dns
$dbdns = "mysql://user:*******@localhost/pulinairc";
// How many rows to show at begining?
$startrows = 150;
// Severside polling
// Serverside polling means that client keeps connection
// to server, and server keeps checking for updates. When
// update is available, server sends it and client generates
// immediatly a new connection. Good thing is, that updates
// are allmost instant. Bad thing is, that for busy sites,
// there is going to be a lot of open, idle connections.
// By setting this to false, client request are served
// immediatly, and client tries to calculate next update
// inteval. This causes a lot of requests to server, but
// connections are only open as minimal time as possible.
$polling = true;
// "throtling".
// If system is near this AVG load, (%80),
// fetching new messages are delayed.
// Set $loadavg to false if throtling is not wanted
// (eg, in windows enviroment, it does not work).
if(is_readable('/proc/loadavg')) {
$loadavg = 10;
$delaytime = 5;
$maxdelay = 10;
}
// Use page compression?
// If true, uses gzip for compressing new messages.
// Can save (a bit) bandwith, but on quiet channel,
// just wastes CPU cycles.
$gzipencode = true;
// Scrolling method.
// As RSL whished, if you don't want to use smooth scrolling,
// set this to false, so windows allways moves to bottom without
// smooth scrolling
$smoothscrolling = (string) "true";
if(isset($_GET['smoothscroll']) && ( $_GET['smoothscroll'] == "true" || $_GET['smoothscroll'] == "false" )) {
$smoothscrolling = (string) $_GET['smoothscroll'];
}
///
/// The code part
///
// Remember start time.
$starttime = time();
// Prevent error displaying, which would screw javascript
//if( $_get['time'] ) error_reporting(0);
// Code from http://www.phpcs.com/codes/COLORISATION-HTML-DES-LOGS-IRC/30393.aspx
function rgb2html($tablo) {
//Le str_pad permet de remplir avec des 0
//parce que sinon rgb2html(Array(0,255,255)) retournerai #0ffff<=manque un 0 !
return "#".str_pad(dechex(($tablo[0]<<16)|($tablo[1]<<8)|$tablo[2]),6,"0",STR_PAD_LEFT);
}
function chooseColor($irc){
switch($irc){
case "0":$color=rgb2html(array(255, 255, 255));break;
case "1":$color=rgb2html(array(0, 0, 0));break;
case "2":$color=rgb2html(array(0, 0, 127));break;
case "3":$color=rgb2html(array(94, 199, 62));break;
case "4":$color=rgb2html(array(255, 0, 0));break;
case "5":$color=rgb2html(array(127, 0, 0));break;
case "6":$color=rgb2html(array(127, 0, 127));break;
case "7":$color=rgb2html(array(255, 127, 0));break;
case "8":$color=rgb2html(array(255, 255, 0));break;
case "9":$color=rgb2html(array(0, 255, 0));break;
case "10":$color=rgb2html(array(63, 127, 127));break;
case "11":$color=rgb2html(array(0, 255, 255));break;
case "12":$color=rgb2html(array(0, 0, 255));break;
case "13":$color=rgb2html(array(255, 0, 255));break;
case "14":$color=rgb2html(array(127, 127, 127));break;
case "15":$color=rgb2html(array(191, 191, 191));break;
default:$color=rgb2html(array(0, 0, 0));break;
}
return $color;
}
function irc2html($texte){
$buffer = "";
$is_bold=false;
$is_under=false;
$is_fg=false;
$is_bg=false;
$is_space=false;
$fg=1;
$bg=0;
for($i=0;$i<strlen($texte);$i++){
$chr = substr($texte,$i,1);
$ord = ord($chr);
switch($ord){
case "10":
if($is_bold) {$buffer.= "</b>";$is_bold=false;}
if($is_under) {$buffer.= "</u>";$is_under=false;}
if($is_fg) {$buffer.= "</span>";$is_fg=false;}
if($is_bg) {$buffer.= "</span>";$is_bg=false;}
$is_space=false;
//$buffer.= "<br>";
break;
case "2":
//->Mettre en gras
if($is_bold) {$buffer.= "</b>";$is_bold=false;}
else {$buffer.= "<b>";$is_bold=true;}
break;
case "3":
//->Mettre en couleur
$fg1="";$fg2="";$bg1="";$bg2="";
$i++;$chr = substr($texte,$i,1);
if(ereg("[0-9]",$chr)){
$fg1=$chr;$i++;
$chr=substr($texte,$i,1);
if(ereg("[0-9]",$chr)){
$fg2=$chr;$i++;$chr=substr($texte,$i,1);
}
if($chr==","){
$i++;$chr = substr($texte,$i,1);
if(ereg("[0-9]",$chr)){
$bg1 = $chr;$i++;
$chr = substr($texte,$i,1);
if(ereg("[0-9]",$chr)){
$bg2=$chr;
}
else{
$i--;
}
}
}
else{
$i--;
}
}
$fg=($fg1.$fg2)+0;
$bg=($bg1.$bg2)+0;
//echo "<b>[C : ".$fg." / ".$bg."]</b>";
if($is_fg){$buffer.= "</span>";$is_fg=false;}
if($fg!=0) {$buffer.= "<span style='color:".chooseColor($fg).";'>";$is_fg=true;}
if($is_bg){$buffer.= "</span>";$is_bg=false;}
if($bg!=0) {$buffer.= "<span style='background-color:".chooseColor($bg).";'>";$is_bg=true;}
break;
case "15":
//->Enlever les couleurs
if($is_fg) {$buffer.= "</span>";$is_fg=false;}
if($is_bg) {$buffer.= "</span>";$is_bg=false;}
if($is_bold) {$buffer.= "</b>";$is_bold=false;}
if($is_under) {$buffer.= "</u>";$is_under=false;}
break;
case "22":
//->Inverser BG et FG
if($is_fg) {$buffer.= "</span>";$is_fg=false;}
if($is_bg) {$buffer.= "</span>";$is_bg=false;}
$temp=$fg;
$fg=$bg;
$bg=$temp;
$buffer.= "<span style='color:".chooseColor($fg).";'>";$is_fg=true;
$buffer.= "<span style='background-color:".chooseColor($bg).";'>";$is_bg=true;
break;
case "31":
//->Souligner
if($is_under) {$buffer.= "</u>";$is_under=false;}
else {$buffer.= "<u>";$is_under=true;}
break;
case "32":
//->Espace
if($is_space) {$buffer.= " ";$is_space=false;}
else {$buffer.=" ";$is_space=true;}
break;
default:
//->Chr normal, afficher
// ei näy ääkköset jos tämä on:
// $buffer.=htmlspecialchars($chr,ENT_QUOTES);
// tällä näkyy:
$buffer.=$chr;
break;
}
}
return $buffer;
}
function htmlline($str) {
if(function_exists("mb_convert_encoding"))
$str = mb_convert_encoding($str, "UTF-8","UTF-8,Windows-1252,ISO-8859-15,ISO-8859-1");
//$str = htmlspecialchars($str);
$str = irc2html($str);
$str = preg_replace( "/([[:alnum:]]+):\/\/([^[:space:]]*)([[:alnum:]#?\/&=])/i", "<a href=\"\\1://\\2\\3\" target=\"_blank\">\\1://\\2\\3</a>", $str);
$str = preg_replace( "/(([a-z0-9_]|\\-|\\.)+@([^[:space:]]*)([[:alnum:]-]))/i", "<a href=\"mailto:\\1%s\" >\\1</a>", $str);
return $str;
}
// Get a first name from the original nickname
function nickToFirstName($nick){
return NAMES[(int)(substr(crc32(md5($nick)), 0, 5)) % NAME_COUNT];
}
function formatNick($nick) {
// Convert the nick
$nick = nickToFirstName($nick);
return htmlentities($nick, ENT_QUOTES);
}
function formatMircTime(&$time, $channel) {
$times = explode(":", $time);
$times = array_slice($times, 0, 3);
while(count($times) < 3) {
array_push($times, 00);
}
// Kesällä pitää olla $times[0]-3 (kesäaika/talviaika)
$time = mktime((int) $times[0], (int) $times[1], (int) $times[2]);
}
function getMessagesDB(&$pos, $channel) {
require_once("DB.php");
global $dbdns, $startrows, $starttime, $polling;
$max_wait = ini_get('max_execution_time')-1;
static $DB;
if(!isset($DB)) {
$DB = DB::Connect($dbdns);
}
if( $pos == null ) {
$sql = '
SELECT
`key`, UNIX_TIMESTAMP(`time`) , `action`, `nick` , `msg`
FROM
`ircmsg`
WHERE
`channel` = '.$DB->quote($channel).'
ORDER BY
`time` DESC, `key` DESC
LIMIT 0 , '.$startrows;
$pos = 0;
} else {
$lsql = '
SELECT
COUNT(*) AS n
FROM `ircmsg`
WHERE
`key` > '.$DB->quote($pos).' AND
`channel` = '.$DB->quote($channel).'
LIMIT 0,1';
$sql = '
SELECT
`key`, UNIX_TIMESTAMP(`time`) , `action`, `nick` , `msg`
FROM
`ircmsg`
WHERE
`key` > '.$DB->quote($pos).' AND
`channel` = '.$DB->quote($channel).'
ORDER BY
`time` DESC, `key` DESC';
// Respawn update checks
while(true) {
// Use quick check for latest DB changes
$res =& $DB->query($lsql);
if(DB::IsError($res)) return array();
list($n) = $res->fetchRow();
$res->free();
if($n > 0) break;
elseif($polling == false) break;
elseif($max_wait < (time()-$starttime)) break; // Is execution time near end?
sleep(1);
}
}
if(isset($n) && $n == 0) return array(); // Bailing out. Possibly out-of-time
$res =& $DB->query($sql);
if(DB::IsError($res)) {
die("Error: DB: ".$res->getMessage());
}
$results = array();
while(list($key, $time, $action, $nick, $msg)=$res->fetchRow()) {
if($key > $pos) $pos = $key;
$results[] = array('time' => $time, 'action' => $action, 'nick' => $nick, 'msg' => $msg);
}
$results = array_reverse($results);
return $results;
}
function _parserMessagesMirc($log) {
preg_match_all('/^\[([^\]]*)] (.*)$/mU', $log, $match, PREG_PATTERN_ORDER);
$match = array_slice($match, 1);
$times =& $match[0];
array_walk($times,'formatMircTime');
$results = array();
foreach($match[1] as $key => $val) {
// Is action?
if(preg_match('/^\*\*\* (.*) (.*)$/U', $val, $tulitikut)) {
$nick =& $tulitikut[1];
$act =& $tulitikut[2];
if(preg_match('/^\(([^\)]*)\) has joined/',$act, $whom)) {
$action = "JOIN";
$msg = $whom[1];
} elseif(preg_match('/^has left .* \(([^\)]*)\)$/U',$act, $whom)) {
// Crappy mirc. Part, left and quit are all logged as left.
$action = "QUIT";
$msg = $whom[1];
} elseif(preg_match('/^is now known as (.*)$/U',$act, $whom)) {
$action = "NICK";
$msg = $whom[1];
} elseif(preg_match('/^sets mode: (.*)$/U',$act, $whom)) {
$action = "MODE";
$msg = $whom[1];
} elseif(preg_match('/^was kicked by .* \(([^\)]*)\)$/U',$act, $whom)) {
$action = "KICK";
$msg = $whom[1];
} else {
continue;
}
} elseif (preg_match('/^<([^>]*)> (.*)$/U', $val, $tulitikut)) {
$action = "PRIVMSG";
$nick = $tulitikut[1];
$msg = $tulitikut[2];
} else {
continue;
}
$results[] = array(
'time' => $times[$key],
'action' => $action,
'nick' => $nick,
'msg' => $msg
);
//$results[count($results)-1]['raw'] = $val;
}
return $results;
}
function _parserMessagesIrssi($log) {
preg_match_all('/^([\d:]*) (.*)$/mU', $log, $match, PREG_PATTERN_ORDER);
$match = array_slice($match, 1);
$times =& $match[0];
array_walk($times,'formatMircTime');
$results = array();
foreach($match[1] as $key => $val) {
// Is action?
if(preg_match('/^-!- (.*) (.*)$/U', $val, $tulitikut)) {
$nick =& $tulitikut[1];
$act =& $tulitikut[2];
if(preg_match('/^\[([^\]]*)\] has joined/U',$act, $whom)) {
$action = "JOIN";
$msg = $whom[1];
} elseif(preg_match('/^\[[^\]]*\] has left .* \[([^\]]*)\]$/U',$act, $whom)) {
$action = "PART";
$msg = $whom[1];
} elseif(preg_match('/^\[[^\]]*\] has quit \[([^\]]*)\]$/U',$act, $whom)) {
$action = "QUIT";
$msg = $whom[1];
} elseif(preg_match('/^is now known as (.*)$/U',$act, $whom)) {
$action = "NICK";
$msg = $whom[1];
} elseif(preg_match('/mode\/.* \[([^\]]*)\] by (.*)$/U',$val, $whom)) {
$action = "MODE";
$msg = $whom[1];
$nick = $whom[2];
} elseif(preg_match('/^was kicked from .* \[([^\]]*)\]/U',$act, $whom)) {
$action = "KICK";
$msg = $whom[1];
} else {
continue;
}
} elseif (preg_match('/^<([^>]*)> (.*)$/U', $val, $tulitikut)) {
$action = "PRIVMSG";
// First character in nicks in irssi logs is mode character
$nick = substr($tulitikut[1],1);
$msg = $tulitikut[2];
} else {
continue;
}
$results[] = array(
'time' => $times[$key],
'action' => $action,
'nick' => $nick,
'msg' => $msg
);
}
return $results;
}
function _parserMessagesEgg($log) {
preg_match_all('/^\[([^\]]*)] (.*)$/mU', $log, $match, PREG_PATTERN_ORDER);
$match = array_slice($match, 1);
$times =& $match[0];
array_walk($times,'formatMircTime');
$results = array();
foreach($match[1] as $key => $val) {
// Is action?
if (preg_match('/^<([^>]*)> (.*)$/U', $val, $tulitikut)) {
$action = "PRIVMSG";
$nick = $tulitikut[1];
$msg = $tulitikut[2];
} elseif (preg_match('%(.*) (.*)$%U', $val, $tulitikut)) {
$nick =& $tulitikut[1];
$act =& $tulitikut[2];
if(preg_match('/^\(([^\)]*)\) joined .*$/U',$act, $whom)) {
$action = "JOIN";
$msg = $whom[1];
} elseif (preg_match('/^\([^\)]*\) left irc: (.*)$/U',$act, $reason)) {
$action = "QUIT";
$msg = $reason[1];
} elseif (preg_match('/^\([^\)]*\) left .*\(([^\)]*)\)/U',$act, $reason)) {
$action = "PART";
$msg = $reason[1];
} elseif (preg_match('/^\([^\)]*\) left .*$/U',$act, $reason)) {
// Without reason
$action = "PART";
$msg = "";
} elseif (preg_match('/^Nick change: (.*) -> (.*)$/U',$val, $whom)) {
$action = "NICK";
$nick = $whom[1];
$msg = $whom[2];
} elseif (preg_match('/^[^:]*: mode change \'([^\']*)\' by ([^!]*)!.*/U',$val, $mode)) {
$action = "MODE";
$msg = $mode[1];
$nick = $mode[2];
} elseif (preg_match('/^kicked from .* by [^:]*: (.*)$/U',$act, $reason)) {
$action = "KICK";
$msg = $reason[1];
} else {
//die(__LINE__.$val);
continue;
}
} else {
//die(__LINE__.$val);
continue;
}
$results[] = array(
'time' => $times[$key],
'action' => $action,
'nick' => $nick,
'msg' => $msg
);
}
return $results;
}
/**
* Read file for logmenu entries.
*/
function getMessagesLogfile(&$pos,$channe=null) {
global $logfile, $logfileformat, $startoffsetbytes, $starttime, $polling;
$max_wait = ini_get('max_execution_time')-1;
if(!file_exists($logfile)) {
die("Error: logfile '{$logfile}' does not exists");
}
if(!$fp = fopen($logfile, "r")) {
die("Error: Error opening logfile '{$logfile}' handler");
}
while(true) {
$totalsize = filesize($logfile);
// Move pointer
if($pos == null) {
if($startoffsetbytes > $totalsize) $startoffsetbytes = $totalsize;
fseek($fp, -$startoffsetbytes, SEEK_END);
$readAmmount=$startoffsetbytes;
} elseif($totalsize < $pos) {
// Possible Log reload. Read From beginning.
$pos = 0;
continue;
} else {
$pos = intval($pos);
if($pos < 0 || $pos > $totalsize) {
// You fuckwad
fseek($fp, -$startoffset, SEEK_END);
$readAmmount=$startoffsetbytes;
} else {
fseek($fp, $pos);
$readAmmount=$totalsize-$pos;
}
}
// Respawn read, if no new lines.
if($readAmmount <= 0) {
if($polling == false) break;
if($max_wait < (time()-$starttime)) break; // Is execution time near end?
// Wait for new event
clearstatcache();
sleep(0.5);
continue;
} else {
// read logfile
$log = fread($fp, $readAmmount);
$pos = ftell($fp);
break;
}
}
fclose($fp);
switch(strtolower($logfileformat)) {
case "mirc" :
$results = _parserMessagesMirc($log);
break;
case "egg" :
$results = _parserMessagesEgg($log);
break;
case "irssi" :
$results = _parserMessagesIrssi($log);
break;
default :
die("Unknown logtype {$logfileformat}");
}
return $results;
}
function getMessages($channel="#pulina",&$time=null) {
global $storage;
if(substr($channel,0,1) != "#") $channel = "#".$channel;
switch($storage) {
case "DB" :
$results = getMessagesDB($time,$channel);
break;
case "logfile" :
$results = getMessagesLogfile($time,$channel);
break;
default :
die("No usable driver");
break;
}
if(function_exists('json_encodeasdf')) {
// Format for json_encode
$times = array();
$act = array();
$nicks = array();
$mesgs = array();
foreach($results as $row) {
$times[] = $row['time'];
$act[] = $row['action'];
$nicks[] = $row['nick'];
switch($row['action']) {
case 'QUIT' :
case 'PART' :
case 'PRIVMSG' :
$mesgs[] = htmlline($row['msg']);
break;
default :
$mesgs[] = htmlentities($row['msg'], ENT_NOQUOTES, 'UTF-8');
break;
}
}
return json_encode(array(
$time,
$times,
$act,
$nicks,
$mesgs
));
} else {
return json_fallback($results, $time);
}
}
function json_fallback($results, $time) {
$times = '';
$actions = '';
$nicks = '';
$mesgs = '';
foreach($results as $row) {
$times .= '"'.$row['time'].'",';
$actions .= '"'.$row['action'].'",';
$nicks .= '"'.formatNick($row['nick']).'",';
switch($row['action']) {
case 'QUIT' :
case 'PART' :
case 'PRIVMSG' :
$mesgs .= '"'.addslashes(htmlline($row['msg'])).'",';
break;
default :
$mesgs .= '"'.htmlentities($row['msg'], ENT_QUOTES, "UTF-8").'",';
break;
}
}
$times = substr($times,0,strlen($times)-1);
$actions = substr($actions,0,strlen($actions)-1);
$nicks = substr($nicks,0,strlen($nicks)-1);
$mesgs = substr($mesgs,0,strlen($mesgs)-1);
return '["'.$time.'",['.$times.'],['.$actions.'],['.$nicks.'],['.$mesgs.']]';
}
/**
* This function calculates load avarage for linux system.
* This data is then used to add litle delay to page load, so
* system won't be totally trashed
* Code partly stolen from phpsysinfo
* http://cvs.sourceforge.net/viewcvs.py/phpsysinfo/phpsysinfo-dev/includes/os/class.Linux.inc.php
*
* @return estimate of load avarage of system total capacity in percents
*/
function loadavg() {
if ($fd = fopen('/proc/loadavg', 'r')) {
$avg = preg_split("/\s/", fgets($fd, 4096),4); // We wan't only the most curren avarage
fclose($fd);
return $avg[0];
} else {
return false;
}
}
/**
* Encode string to gzip-compatible.
* @param $content string content to encode.
* @param $strlen int string length
* @param $crc string crc checksum of string
*/
function gzipencode($content, $strlen=null, $crc=null) {
if($strlen===null) $strlen = strlen($content);
if($crc===null) $crc = crc32($content);
$content = "\x1f\x8b\x08\x00\x00\x00\x00\x00".
substr(gzcompress($content, 3), 0, - 4). // Use mid compression
pack('V', $crc).
pack('V', $strlen);
return $content;
}
if(empty($_GET['channel'])) $_GET['channel'] = $channel;
if(isset($_GET['time'])) {
// It really does not matter if updates aren't instant.
// Use sleep so if server is under load, frequent updates
// won't trash system totally.
if($loadavg) {
$load = loadavg();
$loadprc = ($load/$loadavg);
if($loadprc > 0.80) {
// calculate how log to delay.
$delay = ($loadprc-0.8)*5*$delaytime;
// Don't delay longer than maximal execution time.
if($maxdelay >= ini_get('max_execution_time')) $maxdelay = ini_get('max_execution_time')-(time()-$starttime)-1;
if($delay > $maxdelay) $delay = $maxdelay;
sleep($delay);
}
}
// Save old time
$old_time = $_GET['time'];
// $_GET['time'] is an pointer for getMessages.
$msgs = getMessages($_GET['channel'], $_GET['time']);
// Send time as etag identifier.
header('ETag: '.$_GET['time']);
// Force validation?
if($old_time != $_GET['time']) {
// Konqueror want's to cache, and won't return real deal
// in xmlHttp. Not suitable, not at all...
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: ".gmdate("D, d M Y H:i:s", time())." GMT");
//} elseif($_SERVER['HTTP_IF_NONE_MATCH'] == $_GET['time']) {
// Not modified
// header('HTTP/1.1 304 Not Modified');
// die();
}
if($gzipencode == true && headers_sent() && strstr($_SERVER['HTTP_ACCEPT_ENCODING'], "gzip")) {
$_strlen = strlen($msgs);
$_msgs = gzipencode($msgs,$_strlen);
// If uncompressed is smaller than compressed, send uncompressed one.
if(strlen($_msgs) < $_strlen) {
header('Content-Encoding: gzip');
$msgs = $_msgs;
}
}
die($msgs);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<head>
<title><?= htmlspecialchars($_GET['channel']);?> IrcScroller</title>
<style>
@font-face {
font-family: 'Menlo';
src: url('fonts/menlo-webfont.eot'); /* IE9 Compat Modes */
src: url('fonts/menlo-webfont.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
url('fonts/menlo-webfont.woff2') format('woff2'), /* Super Modern Browsers */
url('fonts/menlo-webfont.woff') format('woff'), /* Pretty Modern Browsers */
url('fonts/menlo-webfont.ttf') format('truetype'), /* Safari, Android, iOS */
url('fonts/menlo-webfont.svg#svgFontName') format('svg'); /* Legacy iOS */
}
@font-face {
font-family: 'SF Mono';
src: url('fonts/sfmono-regular.eot'); /* IE9 Compat Modes */
src: url('fonts/sfmono-regular.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
url('fonts/sfmono-regular.woff2') format('woff2'), /* Super Modern Browsers */
url('fonts/sfmono-regular.woff') format('woff'), /* Pretty Modern Browsers */
url('fonts/sfmono-regular.ttf') format('truetype'), /* Safari, Android, iOS */
url('fonts/sfmono-regular.svg#svgFontName') format('svg'); /* Legacy iOS */
}
@font-face {
font-family: 'SF Mono';
font-weight: 700;
src: url('fonts/sfmono-bold.eot'); /* IE9 Compat Modes */
src: url('fonts/sfmono-bold.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
url('fonts/sfmono-bold.woff2') format('woff2'), /* Super Modern Browsers */
url('fonts/sfmono-bold.woff') format('woff'), /* Pretty Modern Browsers */
url('fonts/sfmono-bold.ttf') format('truetype'), /* Safari, Android, iOS */
url('fonts/sfmono-bold.svg#svgFontName') format('svg'); /* Legacy iOS */
}
@font-face {
font-family: 'SF Mono';
font-weight: 300;
src: url('fonts/sfmono-light.eot'); /* IE9 Compat Modes */
src: url('fonts/sfmono-light.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
url('fonts/sfmono-light.woff2') format('woff2'), /* Super Modern Browsers */
url('fonts/sfmono-light.woff') format('woff'), /* Pretty Modern Browsers */
url('fonts/sfmono-light.ttf') format('truetype'), /* Safari, Android, iOS */
url('fonts/sfmono-light.svg#svgFontName') format('svg'); /* Legacy iOS */
}
body {
font: 14px 'SF Mono', 'Menlo', -apple-system, 'Roboto', 'Rubik', system-ui, Segoe UI, Roboto, Ubuntu, Cantarell, Noto Sans, sans-serif, BlinkMacSystemFont, Helvetica Neue, Arial;
color: rgba(248, 248, 242, 0.77);
background: #0c162d;
}
span.message {
/* width: 95%; */
display:block;
overflow:hidden;
}
/* Timestamp */
/* #6272a4 */
.timestamp {
color: rgba(98, 114, 164, .5) !important;
}
.time-td {
font-weight: 300;
order: 3;
padding-right: 20px;
justify-self: flex-end;
margin-left: auto;
}
.nick-td {
font-weight: 700;
order: 1;
width: 125px;
min-width: 125px;
overflow: hidden;
margin-right: 10px;
}
.msg-td {
font-weight: 300;
order: 2;
display: flex;
flex-wrap: wrap;
}
.msg-td span {
margin-left: 9px;
}
table,
td,
tr {
line-height: 1.6;
}
.work-tr {
display: flex;
justify-content: flex-start;
margin: 2px 0;
padding-right: 10px;
max-width: 100vw;
}
a:link, a:visited, a:active {
text-decoration: none;
color: #3498db;
}
a:hover {
text-decoration: underline;
color: #1abc9c;
}
#container {
width:100%;
height:100%;
position:absolute;
}
#container table {
padding-bottom: 20px; /* Firefox hack */
}
#foo {
background:inherit;
}
@media (max-width: 760px) {
body {
font-size: 13px;
}
}
@media (max-width: 500px) {
body {
font-size: 12px;
}
}
</style>
<script>
var topuri = "https://www.<?= $_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME'] ?>?time=";
var channelparam = "<?= addslashes($_GET['channel']); ?>";
var smoothScroll = <?= $smoothscrolling; ?>;
// For initial table buildup.
var xmlResult = <?= getMessages($_GET['channel'], $_GET['time']);?>;
var xmlHttp = null;
var _sleepTime=_sleepDefTime= 1;
var _appendId = "viestit";
var nickColors = new Array();
// To prevent double-refreshing
var _refreshing = false;
function getXMLHTTPResult() {
if(_refreshing==true) {
return false;
} else if(xmlHttp&&xmlHttp.readyState!=0) {
requesterInit();
getXMLHTTPResult();
} else if(!xmlHttp) {
// Turhaa edes yrittää
_refreshing = true;
} else {
if(_refreshing == false ) {
var openUri=topuri+xmlResult[0]+'&channel='+escape(channelparam);
_refreshing = true;
xmlHttp.open("GET",openUri,true);
xmlHttp.onreadystatechange=parseResult;
xmlHttp.send(null);
return true;
}
}
}
function parseResult() {
if(xmlHttp.readyState==4) {
if(xmlHttp.responseText) {
xmlResult = eval(xmlHttp.responseText);
buildLayout();
}
requesterInit();
_refreshing = false;
setTimeout("getXMLHTTPResult()", getTimer());
}
}
function buildLayout() {
if(xmlResult[1].length < 1) return;
// Current working row
var workTR = null;
for( var f=0; f<xmlResult[1].length; ++f) {
// Array begins with offset 0