-
Notifications
You must be signed in to change notification settings - Fork 40
/
public_log_parser.php
779 lines (681 loc) · 24.9 KB
/
public_log_parser.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
<?php
//This has very little documentation, and is a hobbled togeather mess where speed to code and preformance of the code were placed before code readability or maintainability. Only works on linux. Requires gzip and gunzip shell commands as well as the php extentions.
// edit the server array below to your server(s)
// symlink server-gamedata/servername to the static folder of tgs3 (gamedata folder for tgs2), and symlink parsed-logs to a folder accessable by the webserver
// you also need the runtime condenser in the folder rc, with the binary named rc
// it creates .gz files, you can abuse http-gzip-static and a few rewrite rules in nginx to make nginx serve them up as http-gzip compressed text files.
$servers = array('sybil', 'basil');
if (php_sapi_name() != "cli")
exit;
$stop = TRUE;
$overwrite = FALSE;
$configonly = FALSE;
$options = getopt("doc", array("diff", "overwrite", "configonly"));
if (isset($options["diff"]) || isset($options["d"]))
$stop = FALSE;
if (isset($options["overwrite"]) || isset($options["o"]))
$overwrite = TRUE;
if (isset($options["configonly"]) || isset($options["c"]))
$configonly = TRUE;
/*
for ($argv as $argn=>$arg)
if ($argn > 0)
switch ($arg) {
case "-d":
case "--diff":
$stop = FALSE;
break;
case "-o":
case "--overwrite":
$overwrite = TRUE;
break;
}
if ($argc >= 2 && $argv[1] === "-diff")
$stop = FALSE;
*/
proc_nice(19);
function getfoldersinfolder($folder) {
$results = scandir($folder);
$folders = array();
foreach ($results as $result) {
if (!is_dir($folder . '/' . $result))
continue;
if ($result === '.' or $result === '..')
continue;
$folders[] = $folder . '/' . $result;
}
return $folders;
}
function getfilesinfolder($folder) {
$results = scandir($folder);
$files = array();
foreach ($results as $result) {
if (!is_file($folder . '/' . $result))
continue;
if ($result === '.' or $result === '..')
continue;
$files[] = $folder . '/' . $result;
}
return $files;
}
function getroundsbyserver($server) {
$years = getfoldersinfolder('server-gamedata/'.$server.'/data/logs');
$logfiles = array();
foreach ($years as $year) {
if (!is_numeric(basename($year)))
continue;
$months = getfoldersinfolder($year);
foreach ($months as $month) {
if (!is_numeric(basename($month)))
continue;
$days = getfoldersinfolder($month);
foreach ($days as $day) {
$rounds = getfoldersinfolder($day);
foreach ($rounds as $round) {
$roundid = (int) substr(basename($round), 6);
//echo "$round => $roundid\n";
if ($roundid < 2)
continue;
$logfiles[] = $round;
}
}
}
}
return $logfiles;
}
function compressfile($file, $target = null) {
if ($target) {
echo "compressing file:$file to $target\n";
exec('gzip -kcf9 "'.$file.'" > "'.$target.'.gz"');
} else {
echo "compressing file:$file\n";
exec('gzip -f9 "'.$file.'"');
}
usleep(100000);
}
function condense_runtimes($infile, $outfile) {
echo "Condensing runtime: $infile -> $outfile\n";
chdir('rc');
exec('gunzip -kc ../"'.$infile.'" | ./rc -s | gzip -c9 > ../"'.$outfile.'"');
chdir('..');
usleep(100000);
}
function parseruntime($runtime, $newpath, $monthruntimes, $dayruntimes) {
echo "parsing runtime: $runtime -> $newpath/runtime.log\n";
$file = fopen($runtime, 'rb');
if ($file === false)
return;
$newfile = gzopen($newpath.'/runtime.txt.gz', 'wb9');
if ($newfile === false) {
fclose($file);
return;
}
//parse each line
while (($line = fgets($file)) !== false) {
//Remove ips
$line = preg_replace('/(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?|[0-9])/', '-censored-', $line);
//Remove byond printed strings
$line = preg_replace('/.*Cannot read \".*/', '-censored (string output)', $line);
//write it to the public locations
gzwrite($newfile, $line);
gzwrite($monthruntimes, $line);
gzwrite($dayruntimes, $line);
}
fclose($file);
gzclose($newfile);
condense_runtimes($newpath.'/runtime.txt.gz', $newpath.'/runtime.condensed.txt.gz');
//compressfile($newfilename.'-condensed.txt');
echo "Parse finished on runtime: $runtime\n";
usleep(100000);
}
function updateconfig($server) {
$serverfiles = array('config/maps.txt', 'config/server_overrides.txt', 'data/paintings.json');
$serverfolders = array('data/minimaps', 'data/npc_saves', 'data/engravings');
$diffedfolders = array('data/Diagnostics/Resources');
$filteredfiles = array('config.txt', 'game_options.txt', 'mrp_shared_overrides.txt');
$sharedfiles = array('admins.txt', 'admin_nicknames.json', 'admin_nicknames.txt', 'admin_ranks.txt', 'awaymissionconfig.txt', 'dynamic.json', 'lavaruinblacklist.txt', 'motd.txt', 'mrp_motd.txt', 'policy.json', 'resources.txt', 'sillytips.txt', 'spaceruinblacklist.txt', 'tips.txt', 'unbuyableshuttles.txt', 'server_side_modifications.dm' => 'server_side_modifications.dm');
if (!file_exists('parsed-logs/'.$server))
mkdir('parsed-logs/'.$server, 0775, true);
foreach ($serverfolders as $serverfolder) {
if (!file_exists('server-gamedata/'.$server.'/'.$serverfolder))
continue;
foreach (getfilesinfolder('server-gamedata/'.$server.'/'.$serverfolder) as $file) {
$target = 'parsed-logs/'.$server.'/'.$serverfolder.'/'.basename($file);
if (!file_exists(dirname($target)))
mkdir(dirname($target), 0775, true);
//@copy($file, $target);
@compressfile($file, $target);
}
}
foreach ($diffedfolders as $difffolder) {
if (!file_exists('server-gamedata/'.$server.'/'.$difffolder))
continue;
$stop = false;
foreach (array_reverse(getfilesinfolder('server-gamedata/'.$server.'/'.$difffolder)) as $file) {
if (!ctype_digit(substr(basename($file), 0, 1)))
continue;
$target = 'parsed-logs/'.$server.'/'.$difffolder.'/'.basename($file);
if (!file_exists(dirname($target)))
mkdir(dirname($target), 0775, true);
echo "$file -> $target\n";
if (file_exists($target.'.gz'))
$stop = true;
//@copy($file, $target);
@compressfile($file, $target);
if ($stop)
break;
}
}
foreach ($sharedfiles as $source => $target) {
if (is_numeric($source)) {
$source = $target;
$target = 'config/'.$target;
}
if (!file_exists('server-gamedata/shared/'.$source))
continue;
$target = 'parsed-logs/'.$server.'/'.$target;
if (!file_exists(dirname($target)))
mkdir(dirname($target), 0775, true);
compressfile('server-gamedata/shared/'.$source, $target);
//compressfile('parsed-logs/'.$server.'/config/'.$serverfile);
}
foreach ($serverfiles as $serverfile) {
if (!file_exists('server-gamedata/'.$server.'/'.$serverfile))
continue;
$target = 'parsed-logs/'.$server.'/'.$serverfile;
if (!file_exists(dirname($target)))
mkdir(dirname($target), 0775, true);
compressfile('server-gamedata/'.$server.'/'.$serverfile, $target);
//compressfile('parsed-logs/'.$server.'/config/'.$serverfile);
}
foreach ($filteredfiles as $serverfile) {
filterconfig($server, $serverfile);
}
//Paintings
$painting_categories = getfoldersinfolder('server-gamedata/'.$server.'/data/paintings');
foreach ($painting_categories as $category) {
$target_dir = 'parsed-logs/'.$server.'/data/paintings/'.basename($category);
if (!file_exists($target_dir))
mkdir($target_dir, 0775, true);
foreach (getfilesinfolder($category) as $file) {
$target = $target_dir.'/'.basename($file);
if (!file_exists($target.'.gz'))
@compressfile($file, $target);
}
}
}
function filterconfig($server, $configfile) {
echo "filtering $configfile\n";
$filteredconfigs = array('COMMS_KEY', 'MEDAL_HUB_PASSWORD', 'DISCORD_TOKEN');
$file = fopen('server-gamedata/shared/'.$configfile, 'rb');
if ($file === false)
return;
$newfile = gzopen('parsed-logs/'.$server.'/config/'.$configfile.'.gz', 'wb9');
if ($newfile === false) {
fclose($file);
return;
}
while (($line = fgets($file)) !== false) {
foreach ($filteredconfigs as $filter)
if (strstr(strtoupper($line), strtoupper($filter)) !== false)
$line = '#'.$filter.' -FILTERED-'."\n";
gzwrite($newfile, $line);
}
echo "done filtering $configfile\n";
usleep(100000);
}
function fillzips($file, $basename, $monthzip, $dayzip, $roundzip, $day, $round) {
return;
$handle = gzopen($file, 'r');
$monthzip[$day.'/'.$round.'/'.$basename] = $handle;
$monthzip[$day.'/'.$round.'/'.$basename]->compress(Phar::BZ2);
gzrewind($handle);
$dayzip[$round.'/'.$basename] = $handle;
$dayzip[$round.'/'.$basename]->compress(Phar::BZ2);
gzrewind($handle);
$roundzip[$basename] = $handle;
$roundzip[$basename]->compress(Phar::BZ2);
gzclose($handle);
usleep(100000);
}
function fill_round_zip($file, $basename, &$roundzip, $day, $round) {
if (!($roundzip instanceof PharData)) {
$roundzip = new PharData($roundzip, Phar::CURRENT_AS_FILEINFO | Phar::KEY_AS_FILENAME, null, Phar::ZIP);
$roundzip->startBuffering();
}
$handle = gzopen($file, 'r');
$roundzip[$basename] = $handle;
$roundzip[$basename]->compress(Phar::BZ2);
gzclose($handle);
usleep(100000);
}
echo "Starting up\n";
$servers = array('sybil', 'basil', 'terry', 'event-hall', 'manuel', 'event-hall-us');
foreach ($servers as $server) {
echo "Loading $server\n";
updateconfig($server);
copy_pictures($server);
if ($configonly)
continue;
//parseruntimes($server);
//continue;
echo "Parsing logs files\n";
$basenewpath = 'parsed-logs/'.$server.'/data/logs';
$first = true;
$last = false;
$years = array_reverse(getfoldersinfolder('server-gamedata/'.$server.'/data/logs'));
$dozips = false;
foreach ($years as $year) {
$baseyear = basename($year);
if (!is_numeric($baseyear))
continue;
$months = array_reverse(getfoldersinfolder($year));
foreach ($months as $month) {
$basemonth = basename($month);
$wrotemonth = false;
if (!is_numeric($basemonth))
continue;
if (!file_exists("$basenewpath/$baseyear/$basemonth"))
mkdir("$basenewpath/$baseyear/$basemonth", 0775, true);
if ($dozips) {
$monthzip = new PharData("$basenewpath/$baseyear/month.$baseyear-$basemonth.zip", Phar::CURRENT_AS_FILEINFO | Phar::KEY_AS_FILENAME, null, Phar::ZIP);
$monthzip->startBuffering();
}
if ($stop)
$monthruntimes = gzopen("$basenewpath/$baseyear/$basemonth/$baseyear-$basemonth.runtime.txt.gz", "ab9");
$days = array_reverse(getfoldersinfolder($month));
foreach ($days as $day) {
$baseday = basename($day);
$wroteday = false;
if (!file_exists("$basenewpath/$baseyear/$basemonth/$baseday"))
mkdir("$basenewpath/$baseyear/$basemonth/$baseday", 0775, true);
if ($dozips) {
$dayzip = new PharData("$basenewpath/$baseyear/$basemonth/day.$baseyear-$basemonth-$baseday.zip", Phar::CURRENT_AS_FILEINFO | Phar::KEY_AS_FILENAME, null, Phar::ZIP);
$dayzip->startBuffering();
}
if ($stop)
$dayruntimes = gzopen("$basenewpath/$baseyear/$basemonth/$baseday/$baseyear-$basemonth-$baseday.runtime.txt.gz", "ab9");
$rounds = array_reverse(getfoldersinfolder($day));
foreach ($rounds as $round) {
$baseround = basename($round);
$roundid = (int) substr($baseround, 6);
//echo "$round => $roundid\n";
if ($roundid < 2)
continue;
if ($first) {
$first = false;
continue;
}
$roundA = explode('/', $round);
$roundA[0] = 'parsed-logs';
$newpath = implode('/', $roundA);
if (!file_exists($newpath)) {
mkdir($newpath,0775,true);
} else if ($stop) {
if ($dozips) {
$dayzip->stopBuffering();
$monthzip->stopBuffering();
}
gzclose($monthruntimes);
condense_runtimes("$basenewpath/$baseyear/$basemonth/$baseyear-$basemonth.runtime.txt.gz", "$basenewpath/$baseyear/$basemonth/$baseyear-$basemonth.runtime.condensed.txt.gz");
gzclose($dayruntimes);
condense_runtimes("$basenewpath/$baseyear/$basemonth/$baseday/$baseyear-$basemonth-$baseday.runtime.txt.gz", "$basenewpath/$baseyear/$basemonth/$baseday/$baseyear-$basemonth-$baseday.runtime.condensed.txt.gz");
break 4;
}
$roundzip = $newpath.'.zip';
$logfiles = getfilesinfolder($round);
foreach ($logfiles as $logfile) {
$basename = basename($logfile);
switch ($basename) {
case 'game.log':
$basefilename = basename($basename, '.log');
$fullnewpath = $newpath.'/'.$basefilename.'.txt';
if (!$overwrite && file_exists($fullnewpath.'.gz'))
continue;
parselog($logfile, $newpath, TRUE, TRUE);
if ($dozips)
fillzips($fullnewpath.'.gz', $basefilename.'.txt', $monthzip, $dayzip, $roundzip, $baseday, $baseround);
else
fill_round_zip($fullnewpath.'.gz', $basefilename.'.txt', $roundzip, $baseday, $baseround);
$fullnewpath = $newpath.'/'.$basefilename.'.html';
if ($dozips)
fillzips($fullnewpath.'.gz', $basefilename.'.html', $monthzip, $dayzip, $roundzip, $baseday, $baseround);
else
fill_round_zip($fullnewpath.'.gz', $basefilename.'.html', $roundzip, $baseday, $baseround);
break;
case 'runtime.log':
$basefilename = basename($basename, '.log');
$fullnewpath = $newpath.'/'.$basefilename.'.txt';
if (!$overwrite && file_exists($fullnewpath.'.gz'))
continue;
parseruntime($logfile, $newpath, $monthruntimes, $dayruntimes);
if ($dozips)
fillzips($fullnewpath.'.gz', $basefilename.'.txt', $monthzip, $dayzip, $roundzip, $baseday, $baseround);
else
fill_round_zip($fullnewpath.'.gz', $basefilename.'.txt', $roundzip, $baseday, $baseround);
$fullnewpath = $newpath.'/'.$basefilename.'.condensed.txt';
if ($dozips)
fillzips($fullnewpath.'.gz', $basefilename.'.condensed.txt', $monthzip, $dayzip, $roundzip, $baseday, $baseround);
else
fill_round_zip($fullnewpath.'.gz', $basefilename.'.condensed.txt', $roundzip, $baseday, $baseround);
break;
case 'sql.log':
$basefilename = basename($basename, '.log');
$fullnewpath = $newpath.'/'.$basefilename.'.txt';
if (!$overwrite && file_exists($fullnewpath.'.gz'))
continue;
parselog($logfile, $newpath, FALSE, FALSE);
if ($dozips)
fillzips($fullnewpath.'.gz', $basefilename.'.txt', $monthzip, $dayzip, $roundzip, $baseday, $baseround);
else
fill_round_zip($fullnewpath.'.gz', $basefilename.'.txt', $roundzip, $baseday, $baseround);
break;
case 'attack.log':
case 'dynamic.log':
case 'qdel.log':
case 'initialize.log':
case 'pda.log':
case 'telecomms.log':
case 'overlay.log':
case 'manifest.log':
case 'job_debug.log':
case 'virus.log':
case 'econ.log':
case 'signals.log':
case 'shuttle.log':
case 'mecha.log':
case 'asset.log':
case 'map_errors.log':
case 'cloning.log':
case 'uplink.log':
case 'paper.log':
case 'harddel.log':
case 'speech_indicators.log':
$basefilename = basename($basename, '.log');
$fullnewpath = $newpath.'/'.$basefilename.'.txt';
if (!$overwrite && file_exists($fullnewpath.'.gz'))
continue;
compressfile($logfile, $fullnewpath);
if ($dozips)
fillzips($fullnewpath.'.gz', $basefilename.'.txt', $monthzip, $dayzip, $roundzip, $baseday, $baseround);
else
fill_round_zip($fullnewpath.'.gz', $basefilename.'.txt', $roundzip, $baseday, $baseround);
break;
case 'kudzu.html':
case 'wires.html':
case 'atmos.html':
case 'cargo.html':
case 'deaths.html':
case 'gravity.html':
case 'records.html':
case 'singulo.html':
case 'experimentor.html':
case 'supermatter.html':
case 'engine.html':
case 'botany.html':
case 'presents.html':
case 'telesci.html':
case 'research.html':
case 'radiation.html':
case 'portals.html':
case 'hallucinations.html':
case 'hypertorus.html':
case 'circuit.html':
case 'nanites.html':
case 'newscaster.json':
case 'dynamic.json':
case 'round_end_data.json':
case 'round_end_data.html':
case 'profiler.json':
case 'sendmaps.json':
case 'id_card_changes.html':
case 'target_zone_switch.json':
case 'silo.json':
case ((substr($basename, 0, 5) == 'perf-') ? $basename : !$basename):
$fullnewpath = $newpath.'/'.$basename;
if (!$overwrite && file_exists($fullnewpath.'.gz'))
continue;
compressfile($logfile, $fullnewpath);
if ($dozips)
fillzips($fullnewpath.'.gz', $basename, $monthzip, $dayzip, $roundzip, $baseday, $baseround);
else
fill_round_zip($fullnewpath.'.gz', $basename, $roundzip, $baseday, $baseround);
break;
case 'config_error.log':
case 'hrefs.html':
case 'hrefs.log':
break;
default:
echo "!!(default) $logfile\n";
break;
}
}
$logfolders = getfoldersinfolder($round);
foreach ($logfolders as $logfolder) {
$basename = basename($logfolder);
switch ($basename) {
case 'photos':
if (!file_exists($newpath.'/photos'))
mkdir($newpath.'/photos',0775,true);
foreach (getfilesinfolder($logfolder) as $picturefile) {
$basename = basename($picturefile);
$fullnewpath = $newpath.'/photos/'.$basename;
copy($picturefile, $fullnewpath);
if ($dozips)
fillzips($fullnewpath, 'photos/'.$basename, $monthzip, $dayzip, $roundzip, $baseday, $baseround);
else
fill_round_zip($fullnewpath, 'photos/'.$basename, $roundzip, $baseday, $baseround);
}
break;
default:
echo "(folder default) $logfile => $newpath/$basename\n";
break;
}
}
if ($roundzip instanceof PharData)
$roundzip->stopBuffering();
}
if ($dozips)
$dayzip->stopBuffering();
if ($stop) {
gzclose($dayruntimes);
condense_runtimes("$basenewpath/$baseyear/$basemonth/$baseday/$baseyear-$basemonth-$baseday.runtime.txt.gz", "$basenewpath/$baseyear/$basemonth/$baseday/$baseyear-$basemonth-$baseday.runtime.condensed.txt.gz");
}
}
if ($dozips)
$monthzip->stopBuffering();
if ($stop) {
gzclose($monthruntimes);
condense_runtimes("$basenewpath/$baseyear/$basemonth/$baseyear-$basemonth.runtime.txt.gz", "$basenewpath/$baseyear/$basemonth/$baseyear-$basemonth.runtime.condensed.txt.gz");
}
}
}
}
function parselog($logfile, $newpath, $lineparse, $htmlify) {
echo "Parsing logfile: $logfile\n";
$filename = basename($logfile);
echo "Parsing logfile as Game Log: $logfile\n";
$file = fopen($logfile, "rb");
if ($file === false)
return;
$tofile = gzopen($newpath.'/'.basename($filename, '.log').'.txt.gz', "wb9");
if ($tofile === false) {
@fclose($file);
return;
}
if (!is_resource($file) || !is_resource($tofile)) {
@fclose($file);
@fclose($tofile);
return;
}
$tofilehtml = NULL;
if ($htmlify) {
$tofilehtml = gzopen($newpath.'/game.html.gz', "wb9");
if ($tofilehtml === false || !is_resource($tofilehtml)) {
@fclose($file);
@fclose($tofile);
return;
}
gzwrite($tofilehtml, '<html><head><title>Log file: '.$newpath.' - /tg/station 13</title><link rel="stylesheet" type="text/css" href="/logfilestyle.css"></head><body>');
}
while (($line = fgets($file)) !== false) {
$rawline = trim($line, "\n\r");
$parsedline = $rawline;
if ($lineparse)
$parsedline = parseline($rawline, htmlspecialchars($line));
$html = "";
if ($htmlify)
$html = preg_replace('/(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?|[0-9])/', '<span class="censored">-censored(ip)-</span>', $parsedline[1]);
$line = preg_replace('/(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?|[0-9])/', '-censored(ip)-', $parsedline[0]); //remove ips
gzwrite($tofile, $line."\r\n");
if ($htmlify)
gzwrite($tofilehtml, $html);
}
if ($htmlify) {
gzwrite($tofilehtml, '</body></html>');
gzclose($tofilehtml);
}
fclose($file);
gzclose($tofile);
usleep(100000);
}
function parseline ($line, $html) {
$line = trim($line);
if (!$line)
return array('-censored(misc)-','<p class="censored">-censored(misc)-</p>');
if ($line[0] != '[')
return array('-censored(misc)-','<p class="censored">-censored(misc)-</p>');
$words = explode(' ', $line);
$htmlwords = explode(' ', $html);
$logtype = explode(']',$words[0]);
if (count($logtype) < 2) {
$logtype = $words[2];
$words[0] .= ' '.$words[1].' '.$words[2];
$htmlwords[0] .= ' '.$htmlwords[1].' '.$htmlwords[2];
array_splice($words, 1, 2);
//echo "$words[0]|||$words[1]|||$words[2]|||$logtype\n";
} else {
$logtype = $logtype[1];
}
switch ($logtype) {
case 'ACCESS:':
if ($words[1] == 'Login:') {
$words[count($words)-4] = '-censored(ip/cid)-';
$htmlwords[count($htmlwords)-4] = '<span class="censored">-censored(ip/cid)-</span>';
}
if ($words[1] == 'Failed')
return censor('invalid connection data');
break;
case 'ADMIN:':
if ($words[1] == 'HELP:')
return censor('asay/apm/ahelp/notes/etc');
if ($words[1] == 'PM:')
return censor('asay/apm/ahelp/notes/etc');
if ($words[1] == 'ASAY:')
return censor('asay/apm/ahelp/notes/etc');
if (preg_match('/ADMIN: .*\\/\\(.*\\) : /', $line))
return censor('asay/apm/ahelp/notes/etc');
if (preg_match('/ADMIN: .*\\/\\(.*\\) added note /', $line))
return censor('asay/apm/ahelp/notes/etc');
if (preg_match('/ADMIN: .*\\/\\(.*\\) removed a note /', $line))
return censor('asay/apm/ahelp/notes/etc');
if (preg_match('/ADMIN: .*\\/\\(.*\\) has added /', $line))
return censor('asay/apm/ahelp/notes/etc');
if (preg_match('/ADMIN: .*\\/\\(.*\\) has edited /', $line))
return censor('asay/apm/ahelp/notes/etc');
if (preg_match('/ADMIN: [^:]*\\/\\(.*\\) ".*"/', $line))
return censor('asay/apm/ahelp');
/*if (preg_match('/ADMIN: .*\\/\\(.*\\) has created a /', $line))
return censor('asay/apm/ahelp/notes/etc');
if (preg_match('/ADMIN: .*\\/\\(.*\\) has removed a /', $line))
return censor('asay/apm/ahelp/notes/etc');
if (preg_match('/ADMIN: .*\\/\\(.*\\) has deleted a /', $line))
return censor('asay/apm/ahelp/notes/etc');*/ //old messages about message/note/memo adding.
if ($words[1] == '<a')
return censor('asay/apm/ahelp/notes/etc');
break;
case 'ADMINPRIVATE:':
return censor('private logtype');
break;
case 'ASAY:':
return censor('asay/apm/ahelp/notes/etc');
break;
case 'SQL:':
return censor('sql logs');
break;
case 'SAY:':
case 'WHISPER:':
case 'OOC:':
foreach ($htmlwords as $i=>$word) {
if ($word == ':') {
$htmlwords[1] = '<b>'.$htmlwords[1];
$htmlwords[$i-1] = $htmlwords[$i-1].'</b>';
break;
}
}
break;
default:
break;
}
return array(implode(' ',$words), '<p class="'.rtrim(explode(']',$words[0])[1],':').'">'.implode(' ',$htmlwords).'</p>');
}
function censor($reason) {
return array('-censored('.$reason.')-','<p class="censored">-censored('.$reason.')-</p>');
}
function copy_pictures($server) {
global $stop, $overwrite;
echo "Parsing picture log files\n";
$basenewpath = 'parsed-logs/'.$server.'/data/picture_logs';
$first = true;
$last = false;
$years = array_reverse(getfoldersinfolder('server-gamedata/'.$server.'/data/picture_logs'));
$dozips = false;
foreach ($years as $year) {
$baseyear = basename($year);
if (!is_numeric($baseyear))
continue;
$months = array_reverse(getfoldersinfolder($year));
foreach ($months as $month) {
$basemonth = basename($month);
if (!is_numeric($basemonth))
continue;
if (!file_exists("$basenewpath/$baseyear/$basemonth"))
mkdir("$basenewpath/$baseyear/$basemonth", 0775, true);
$days = array_reverse(getfoldersinfolder($month));
foreach ($days as $day) {
$baseday = basename($day);
if (!file_exists("$basenewpath/$baseyear/$basemonth/$baseday"))
mkdir("$basenewpath/$baseyear/$basemonth/$baseday", 0775, true);
$rounds = array_reverse(getfoldersinfolder($day));
foreach ($rounds as $round) {
$baseround = basename($round);
$roundid = (int) substr($baseround, 6);
//echo "$round => $roundid\n";
if ($roundid < 2)
continue;
if ($first) {
$first = false;
continue;
}
$roundA = explode('/', $round);
$roundA[0] = 'parsed-logs';
$newpath = implode('/', $roundA);
if (!file_exists($newpath)) {
mkdir($newpath,0775,true);
} else if ($stop) {
break 4;
}
$pictures = getfilesinfolder($round);
foreach ($pictures as $picture) {
$basename = basename($picture);
$fullnewpath = $newpath.'/'.$basename;
if (!$overwrite && file_exists($fullnewpath))
continue;
copy($picture, $newpath.'/'.$basename);
}
}
}
}
}
}
?>