-
Notifications
You must be signed in to change notification settings - Fork 13
/
sss.php
377 lines (310 loc) · 10.2 KB
/
sss.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
<?php declare(strict_types=1);
/**
* Copyright (c) 2009-2024, Jos de Ruijter <[email protected]>
*/
/**
* Override php.ini directives.
*/
ini_set('display_errors', 'stdout');
ini_set('error_reporting', '-1');
ini_set('pcre.jit', '0');
/**
* Check for PHP version requirement.
*/
preg_match('/^8\.[234]\./', PHP_VERSION) or exit('>> php version 8.[234] required to run this program <<'."\n");
/**
* Check if all required extensions are loaded.
*/
foreach (['sqlite3', 'mbstring'] as $module) {
extension_loaded($module) or exit('>> php\'s '.$module.' extension isn\'t loaded <<'."\n");
}
/**
* Autoloader. This code handles on the fly inclusion of classes and traits at
* time of instantiation.
*/
spl_autoload_register(function (string $class): void {
if (str_starts_with($class, 'parser_')) {
require __DIR__.'/parsers/'.substr($class, 7).'.php';
} else {
require __DIR__.'/'.$class.'.php';
}
});
/**
* Read options from the command line. Show a hint on invalid input.
*/
$options = getopt('c:e:i:m:o:qv');
ksort($options);
preg_match('/^c?(e|i|i?o|m)[qv]?$/', implode('', array_keys($options))) or exit('usage: php sss.php [-q | -v] [-c config] [-i <logfile or directory>] [-o html]'."\n");
/**
* Main class.
*/
class sss
{
use common;
private bool $need_maintenance = false;
private bool $with_zlib = false;
private string $database = '';
private string $parser = '';
private string $timezone = '';
public function __construct(array $options)
{
/**
* Explicitly set the locale to C (POSIX) for all categories so there hopefully
* won't be any unexpected results between platforms.
*/
setlocale(LC_ALL, 'C');
/**
* Use UTC until config specified timezone is set.
*/
date_default_timezone_set('UTC');
/**
* Set the character encoding used by all mbstring functions.
*/
mb_internal_encoding('UTF-8');
/**
* Set output verbosity if applicable.
*/
if (isset($options['q'])) {
out::set_verbosity(0);
} elseif (isset($options['v'])) {
out::set_verbosity(2);
}
/**
* Read either the user provided config file or the default one.
*/
$settings = $this->read_config($options['c'] ?? __DIR__.'/sss.conf');
/**
* Set the proper timezone.
*/
date_default_timezone_set($this->timezone) or out::put('critical', 'invalid timezone: \''.$this->timezone.'\'');
out::put('debug', 'timezone set to: \''.$this->timezone.'\'');
/**
* Check if the zlib extension is loaded.
*/
if (extension_loaded('zlib')) {
$this->with_zlib = true;
} else {
out::put('notice', 'php\'s zlib extension isn\'t loaded, skipping any gzipped logs');
}
/**
* Open the database connection and (clear and) store config settings.
*/
db::set_database($this->database);
db::connect();
db::query_exec('DELETE FROM settings');
foreach ($settings as $setting => $value) {
db::query_exec('INSERT INTO settings (var, value) VALUES (\''.$setting.'\', \''.preg_replace('/\'/', '\'\'', $value).'\')');
}
/**
* Init done, move to main.
*/
$this->main($options);
/**
* Synchronize and finalize.
*/
db::disconnect();
out::put('notice', 'kthxbye');
}
private function create_html(string $file): void
{
if (($fp = fopen($file, 'wb')) === false) {
out::put('notice', 'failed to open file: \''.$file.'\', cannot create stats page');
return;
}
out::put('notice', 'creating stats page');
$html = new html();
fwrite($fp, $html->get_contents());
fclose($fp);
}
private function export_nicks(string $file): void
{
if (($total = db::query_single_col('SELECT COUNT(*) FROM uid_details')) === 0) {
out::put('notice', 'database is empty, nothing to export');
return;
}
out::put('notice', 'exporting nicks');
$results = db::query('SELECT status, csnick, (SELECT GROUP_CONCAT(csnick) FROM uid_details WHERE ruid = t1.ruid AND status = 2) AS aliases FROM uid_details AS t1 WHERE status IN (1,3,4) ORDER BY csnick ASC');
$contents = '';
while ($result = $results->fetchArray(SQLITE3_ASSOC)) {
$contents .= $result['status'].','.$result['csnick'].(!is_null($result['aliases']) ? ','.$result['aliases'] : '')."\n";
}
if (!is_null($unlinked = db::query_single_col('SELECT GROUP_CONCAT(csnick) FROM uid_details WHERE status = 0'))) {
$contents .= '*,'.$unlinked."\n";
}
if (($fp = fopen($file, 'wb')) === false) {
out::put('critical', 'failed to open file: \''.$file.'\'');
}
fwrite($fp, $contents);
fclose($fp);
out::put('debug', $total.' nick'.($total !== 1 ? 's' : '').' exported');
}
private function import_nicks(string $file): void
{
out::put('notice', 'importing nicks');
if (($rp = realpath($file)) === false) {
out::put('critical', 'no such file: \''.$file.'\'');
}
if (($fp = fopen($rp, 'rb')) === false) {
out::put('critical', 'failed to open file: \''.$rp.'\'');
}
/**
* Set all nicks to their default state before updating them according to
* imported data.
*/
db::query_exec('UPDATE uid_details SET ruid = uid, status = 0 WHERE ruid != uid OR status != 0');
while (($line = fgets($fp)) !== false) {
/**
* Skip lines we can't work with. This check is very loose and can only save so
* many from shooting themselves in the foot.
*/
if (!preg_match('/^(?<status>[134]),(?<registered_nick>[^,*\']+)(,(?<aliases>[^,*\']+(,[^,*\']+)*)?)?$/n', preg_replace('/\s+/', '', $line), $matches, PREG_UNMATCHED_AS_NULL)) {
continue;
}
/**
* Skip updates for nonexistent registered nicks.
*/
if (db::query_single_col('SELECT EXISTS (SELECT 1 FROM uid_details WHERE csnick = \''.$matches['registered_nick'].'\')') === 0) {
continue;
}
db::query_exec('UPDATE uid_details SET status = '.$matches['status'].' WHERE csnick = \''.$matches['registered_nick'].'\'');
if (!is_null($matches['aliases'])) {
db::query_exec('UPDATE uid_details SET status = 2, ruid = (SELECT uid FROM uid_details WHERE csnick = \''.$matches['registered_nick'].'\') WHERE csnick IN (\''.preg_replace('/,/', '\',\'', $matches['aliases']).'\')');
}
}
fclose($fp);
}
/**
* Take action based on given command line arguments.
*/
private function main(array $options): void
{
if (isset($options['e'])) {
$this->export_nicks($options['e']);
}
if (isset($options['m'])) {
$this->import_nicks($options['m']);
$this->need_maintenance = true;
}
if (isset($options['i'])) {
$this->parse_log($options['i']);
}
if ($this->need_maintenance) {
$maintenance = new maintenance();
}
if (isset($options['o'])) {
$this->create_html($options['o']);
}
}
private function parse_log(string $filedir): void
{
if (($rp = realpath($filedir)) === false) {
out::put('critical', 'no such file or directory: \''.$filedir.'\'');
}
$files = [];
if (is_dir($rp)) {
if (($dh = opendir($rp)) === false) {
out::put('critical', 'failed to open directory: \''.$rp.'\'');
}
while (($entry = readdir($dh)) !== false) {
$entry = realpath($rp.'/'.$entry);
if (!is_dir($entry)) {
$files[] = $entry;
}
}
closedir($dh);
} else {
$files[] = $rp;
}
foreach ($files as $file) {
/**
* Each filename must contain a date formatted like "Ymd" or "Y-m-d".
*/
if (preg_match('/(?<!\d)(?<year>\d{4})-?(?<month>\d{2})-?(?<day>\d{2})(?!\d)/', $file, $matches)) {
if (str_ends_with($file, '.gz') && !$this->with_zlib) {
continue;
}
$logfiles[$matches['year'].'-'.$matches['month'].'-'.$matches['day']] = $file;
}
}
if (!isset($logfiles)) {
out::put('critical', 'no logfiles found having a date in their name (e.g. #chatroom.'.date('Ymd').'.log)');
}
/**
* Sort the files on the date found in the filename.
*/
ksort($logfiles);
foreach ($logfiles as $date => $logfile) {
/**
* Skip logs that have already been processed. We only support parsing logs in
* chronological order.
*/
if (!is_null($date_last_log_parsed = db::query_single_col('SELECT MAX(date) FROM parse_history')) && $date < $date_last_log_parsed) {
continue;
}
$parser = new $this->parser($date);
/**
* Get the parse history and set the line number on which to start parsing the
* log. This would be 1 for a fresh log and +1 for a log with a parse history.
*/
if (!is_null($linenum_start = db::query_single_col('SELECT lines_parsed FROM parse_history WHERE date = \''.$date.'\''))) {
++$linenum_start;
} else {
$linenum_start = 1;
}
out::put('notice', 'parsing logfile: \''.$logfile.'\' from line '.$linenum_start);
$parser->parse_log($logfile, $linenum_start, str_ends_with($logfile, '.gz'));
/**
* Update the parse history when there are actual (non-empty) lines parsed.
*/
if ($parser->get_int('linenum_last_nonempty') >= $linenum_start) {
db::query_exec('INSERT INTO parse_history (date, lines_parsed) VALUES (\''.$date.'\', '.$parser->get_int('linenum_last_nonempty').') ON CONFLICT (date) DO UPDATE SET lines_parsed = excluded.lines_parsed');
/**
* Store data in the database. We will need maintenance if any data is stored.
*/
if ($parser->store_data()) {
$this->need_maintenance = true;
}
}
}
}
/**
* Read settings from the config file.
*/
private function read_config(string $file): array
{
if (($rp = realpath($file)) === false) {
out::put('critical', 'no such file: \''.$file.'\'');
}
if (($fp = fopen($rp, 'rb')) === false) {
out::put('critical', 'failed to open file: \''.$rp.'\'');
}
$settings_missing = $settings_required = ['database', 'parser', 'timezone'];
while (($line = fgets($fp)) !== false) {
if (!preg_match('/^\s*(?<setting>\w+)\s*=\s*"(?<value>[^"]+)"/', $line, $matches)) {
continue;
}
$setting = $matches['setting'];
$value = $matches['value'];
$settings[$setting] = $value;
/**
* Apply and keep track of required settings.
*/
if (in_array($setting, $settings_required)) {
$settings_missing = array_diff($settings_missing, [$setting]);
$this->$setting = $value;
}
}
fclose($fp);
/**
* Check if all required settings were present.
*/
if (!empty($settings_missing)) {
out::put('critical', 'missing required setting'.(count($settings_missing) !== 1 ? 's' : '').': \''.implode('\', \'', $settings_missing).'\'');
}
return $settings;
}
}
/**
* Launch superseriousstats!
*/
$sss = new sss($options);