forked from goldenice/GMOT-Whatpulse-Parser
/
bbcodegenerator.php
569 lines (437 loc) · 18.8 KB
/
bbcodegenerator.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
<?php
/* GMOT BB-code Whatpulse stats parser
* Parses stats from the database into BBCode
*
* Source, contributors, changelog and issues:
* - https://github.com/goldenice/GMOT-Whatpulse-Parser
*/
# display as plain text
header('Content-Type: text/plain');
# Defines
define('ROOT', dirname(__FILE__));
define('ENDL', "\r\n");
define('SECONDS_PER_DAY', 86400);
# Load configuration
require_once('config.php');
require_once('functions.php');
# get script hash (for mirror check)
if (isset($_GET['hash'])) {
die(sha1Newline(file_get_contents(__FILE__)));
}
# PHP and content settings
$starttime = microtime(true);
if (DEVMODE || isset($_GET['devmode'])) {
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
set_time_limit(60);
} else {
// don't let non-devs use PHP < 5.4
if (version_compare(PHP_VERSION, '5.4.0', '<')) {
die('You need at least PHP 5.4 to run this script.' . ENDL . ENDL . 'Time to upgrade! :)');
}
error_reporting(0);
set_time_limit(10);
}
# Automatic Class Loader
function autoClassLoader($class) {
$path = ROOT . '/classes/'.$class.'.class.php';
if (file_exists($path)) {
include($path);
} else {
die('class ' . $class . ' @ ' . $path . ' not found');
}
}
spl_autoload_register('autoClassLoader');
# Database Connection
$db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME, DB_PORT);
if ($db->connect_errno) {
die('MySQL verbinding mislukt: ' . $db->connect_error);
}
# Script Settings
$teamtag = '[GMOT]'; // important for removing the team tag from the username.
$sourceUrl = 'https://raw.githubusercontent.com/goldenice/GMOT-Whatpulse-Parser/master/bbcodegenerator.php';
$scriptUrls = array(
'https://rpi.ricklubbers.nl/sandbox/gmotwpstats/new/bbcodegenerator.php',
'http://private.woutervdb.com/php/gmotwpstats/bbcodegenerator.php',
'http://martinbeentjes.nl/dev/gmot/gmotwpstats/bbcodegenerator.php',
'https://mirkwood.bjarno.be/~bjarno/GMOT-Whatpulse-Parser/bbcodegenerator.php'
);
// $basedir = 'http://rpi.ricklubbers.nl/sandbox/gmotwpstats/new';
$rank_up_png = 'http://is.gd/6aftPs';
# --------------------------------------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------------------------------------
# Let's start by gathering information!
#
# 1. gather some global stats
# 2. select all (per-user) data we need to build the scoreboard
# 3. calculate totals and rank offsets
# 4. determine which users have joined/left/returned
# --------------------------------------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------------------------------------
// warning when developer mode is enabled
if (DEVMODE || isset($_GET['devmode'])) {
echo '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!' . ENDL;
echo '!! DEVELOPER MODE IS ENABLED !!' . ENDL;
echo '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!' . ENDL;
echo '!! OUTPUT MAY CONTAIN DEVELOPER DEBUG INFORMATION !!' . ENDL;
echo '!! PHP WARNINGS OR INCORRECT INFORMATION !!' . ENDL;
echo '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!' . ENDL . ENDL;
}
// allow output of previous days to be re-generated:
$goback = 0;
if (isset($_GET['goback'])) {
if (intval($_GET['goback']) > 0) {
$goback = intval($_GET['goback']);
}
}
// stat timestamps (from - till)
$sql = '
SELECT
timestamp
FROM
`3_global`
ORDER BY
`timestamp` DESC
LIMIT 3
OFFSET ' . intval($goback) . ';';
$result = $db->query($sql);
$statsDateTill = $result->fetch_row()[0];
$statsDateFrom = $result->fetch_row()[0];
$statsDateYesterday = $result->fetch_row()[0];
// userdata
$sql = '
SELECT
users.username,
users.status,
today.userid,
today.rank,
IFNULL(
yesterday.rank,
today.rank
) AS `oldrank`,
today.keys,
today.clicks,
today.upload,
today.download,
today.uptime,
today.upload + today.download AS `bandwidth`,
today.keys - yesterday.keys AS `keysDiff`,
today.clicks - yesterday.clicks AS `clicksDiff`,
today.upload - yesterday.upload AS `uploadDiff`,
today.download - yesterday.download AS `downloadDiff`,
today.uptime - yesterday.uptime AS `uptimeDiff`,
today.download - yesterday.download +
today.upload - yesterday.upload AS `bandwidthDiff`,
yesterday.lastpulse
FROM
3_users AS users
LEFT JOIN 3_updates AS today
ON today.userid = users.id
AND today.seqnum = (SELECT MAX(seqnum) FROM 3_updates) - ' . intval($goback) . '
LEFT JOIN 3_updates AS yesterday
ON yesterday.userid = users.id
AND yesterday.seqnum = (SELECT MAX(seqnum) FROM 3_updates) - ' . (intval($goback) + 1) . '
WHERE
users.status != "ex-member"
GROUP BY
users.username
ORDER BY
IFNULL(today.rank, yesterday.rank) ASC;';
$result = $db->query($sql);
$users = array();
$rankDelta = 0;
while ($userData = $result->fetch_assoc()) {
// Get the amount of days since the last pulse that is not the pulse of today (yesterday.lastpulse)
$userData['saverdays'] = max(0, ceil( ($statsDateFrom - $userData['lastpulse']) / SECONDS_PER_DAY ));
// Someone's considdered a saver if yesterday.lastpulse was not yesterday.
$userData['saver'] = ($userData['lastpulse'] < $statsDateYesterday);
if ($userData['status'] == 'just-joined' || $userData['status'] == 'returned') {
// if the user is inserted in the scoreboard (returning or new member), set his old rank to the current
// rank and increase the rank offset for each following users.
$users[] = new User($userData, $rankDelta);
$rankDelta += 1;
} else if ($userData['status'] == 'just-left') {
// if the user has been removed from the scoreboard, the rank offset is decreased for the following users.
$users[] = new User($userData, $rankDelta);
$rankDelta -= 1;
} else {
// normal user: nothing special.
$users[] = new User($userData, $rankDelta);
}
}
// user that just left have a negative diff (because they had NULL today and a value yesterday)
// so the math should be correct.. (right?)
// while we're iterating through the users, also record events (users joining, leaving, etc.)
$events = array();
$statkeys = array('keys', 'clicks', 'upload', 'download', 'uptime', 'bandwidth');
foreach ($statkeys as $key) {
$totals[$key] = 0;
$totals[$key . 'Diff'] = 0;
$highest[$key . 'Diff'] = 0;
$highest[$key . 'SavedDiff'] = 0;
}
$totals['savers'] = 0;
$totals['pulsers'] = 0;
$totals['active'] = 0;
foreach ($users as $user) {
foreach ($statkeys as $key) {
$totals[$key] += $user->getRawData($key);
$totals[$key . 'Diff'] += $user->getRawData($key . 'Diff');
if (!$user->isSaver()) {
$highest[$key . 'Diff'] = max($highest[$key . 'Diff'], $user->getRawData($key . 'Diff'));
} else {
$highest[$key . 'SavedDiff'] = max($highest[$key . 'SavedDiff'], $user->getRawData($key . 'Diff'));
}
}
switch($user->getRawData('status')) {
case 'just-joined':
$events[] = new Event('Welkom %s! :D', $user);
break;
case 'returned':
$events[] = new Event('Welkom terug %s! :D', $user);
break;
case 'just-left':
$events[] = new Event('%s heeft besloten ons te verlaten :(', $user);
break;
}
// count active users, pulsers and savers
if ($user->isActive()) {
$totals['active'] += 1;
if ($user->hasPulsed()) {
$totals['pulsers'] += 1;
if ($user->isSaver()) {
$totals['savers'] += 1;
}
}
}
}
// ignore maximum saved diff when it's surpassed by the maximum unsaved diff
foreach ($statkeys as $key) {
if ($highest[$key . 'Diff'] > $highest[$key . 'SavedDiff']) {
$highest[$key . 'SavedDiff'] = 0;
}
}
// now we're going to check whether or not the mirrors are up to date
$ownVersion = filemtime(__FILE__);
$mirrors = array();
$mirrorValidate = true;
$sourceStr = readExternalFile($sourceUrl);
$sourceHash = sha1Newline($sourceStr);
// if somehow github is down; don't bother validating
if ($sourceStr === false) {
$mirrorValidate = false;
}
foreach($scriptUrls as $url) {
$mirrors[] = new Mirror($url, $sourceHash);
}
shuffle($mirrors); // Because all mirrors are created equal.
# --------------------------------------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------------------------------------
# We now have all the information we need :)
#
# 5. Determine which stats to use in the third column
# 6. Display message heading
# 7. Display scoreboard (with milestones)
# 8. Display user events
# 9. Display totals
# 10. Display links
# --------------------------------------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------------------------------------
// choose which stat to use in the third column.
if (date('z', $statsDateTill) % 2 == 0) {
$thirdStat = 'uptime';
$thirdStatHeading = 'Uptime';
} else {
$thirdStat = 'bandwidth';
$thirdStatHeading = 'Bandwidth';
}
// Milestones
$milestones = array(
array('name' => 'Kryptonite', 'keyvalue' => 100000000),
array('name' => 'Platina', 'keyvalue' => 75000000),
array('name' => 'Diamant', 'keyvalue' => 50000000),
array('name' => 'Titanium', 'keyvalue' => 25000000),
array('name' => 'Goud', 'keyvalue' => 10000000),
array('name' => 'Zilver', 'keyvalue' => 7500000),
array('name' => 'Brons', 'keyvalue' => 5000000),
array('name' => 'Metaal', 'keyvalue' => 2500000),
array('name' => 'Steen', 'keyvalue' => 1000000),
array('name' => 'Klei', 'keyvalue' => 500000),
array('name' => 'Kool', 'keyvalue' => 250000),
array('name' => 'Hout', 'keyvalue' => 100000),
array('name' => 'N00b', 'keyvalue' => 10)
);
// find first milestone
$milestoneIndex = 0;
$milestonePrint = true;
if (count($users) > 0) {
while ($milestones[$milestoneIndex]['keyvalue'] > $users[0]->getRawData('keys') && $milestoneIndex < count($milestones) - 1) {
$milestoneIndex += 1;
}
} else {
$milestoneIndex = count($milestones) - 1;
$milestonePrint = false;
}
// message heading
echo '[b][size=14pt]Statistieken gegenereerd op ' . Format::DateTime($statsDateTill) . '[/size] ';
echo '(' . date("H:i:s", $statsDateTill) . ')' . ENDL;
echo 'Geteld vanaf ' . Format::DateTime($statsDateFrom) . ' ' . date('H:i:s', $statsDateFrom) . ENDL . ENDL;
// table heading
echo '[table][tr][td][b]#[/td][td][b]Gebruikersnaam[/td]';
echo '[td][b]Keys[/td][td][/td][td][b]Kliks[/td][td][/td]';
echo '[td][b]' . $thirdStatHeading . '[/td][td][/td][/tr]' . ENDL;
// table rows
foreach ($users as $user) {
// do not display users that have left.
if (!$user->isActive() || $user->getRawData('keys') < 10) { continue; }
// determine if we need to print another milestone
if ($milestoneIndex < count($milestones) - 1) {
while ($milestones[$milestoneIndex]['keyvalue'] > $user->getRawData('keys') && $milestoneIndex < count($milestones) - 1) {
$milestoneIndex += 1;
$milestonePrint = true;
}
}
// print a milestone if we have to
if ($milestonePrint) {
echo '[tr][td][/td][td][b]' . $milestones[$milestoneIndex]['name'] . '[/td]';
echo '[td][right][b]' . Format::Number($milestones[$milestoneIndex]['keyvalue']) . '[/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]' . ENDL;
$milestonePrint = false;
}
// Start user row
// 1st column: ranking
echo '[tr][td]';
$rank = $user->getRawData('rank');
$rankDiff = $user->getRankDiff();
// red or green text when rank has changed
if ($rankDiff < 0) {
echo '[green][abbr=+' . Format::Number(-$rankDiff) . ']';
} elseif ($rankDiff > 0) {
echo '[red][abbr=-' . Format::Number($rankDiff) . ']';
}
echo $rank . '[/td]';
// 2nd column: username (and rank up icon)
echo '[td]';
// show rank up symbol if keys - keysDiff is smaller than the next milestone.
if ($milestoneIndex < count($milestones) - 1) {
if ($user->getRawData('keys') - $user->getRawData('keysDiff') < $milestones[$milestoneIndex]['keyvalue']) {
echo '[img]' . $rank_up_png . '[/img] ';
}
}
// show the username in green if the user has returned (re-joined after leaving)
if ($user->getRawData('status') == 'returned') {
echo '[abbr=The prodigal son has returned!][green]';
}
echo $user->getUsername() . '[/td]';
// 3rd column: keys
echo '[td][right]' . Format::StatNumber($user->getRawData('keys')) . '[/td][td]';
// keysDiff value
$keysDiff = $user->getRawData('keysDiff');
if ($keysDiff > 0) {
$saverdays = $user->getRawData('saverdays');
$prefix = '';
// cannot use StatNumber inside [abbr] tag
if ($saverdays > 1) {
$prefix .= '[abbr=Verdeeld over ' . Format::Number($saverdays) . ' dagen, ';
$prefix .= 'gemiddeld ' . Format::Number($keysDiff / $saverdays) . ' keys per dag]';
}
if (!$user->isSaver() && $keysDiff == $highest['keysDiff']) {
$prefix .= ' [blue]';
} else if ($user->isSaver() && $keysDiff == $highest['keysSavedDiff']) {
$prefix .= ' [color=purple]';
} else {
$prefix .= ' [green]';
}
echo $prefix . '+' . Format::StatNumber($keysDiff);
}
echo '[/td]';
// 4th column: clicks
echo '[td][right]' . Format::StatNumber($user->getRawData('clicks')) . '[/td][td]';
// clicksDiff value
$clicksDiff = $user->getRawData('clicksDiff');
if ($clicksDiff > 0) {
if (!$user->isSaver() && $clicksDiff == $highest['clicksDiff']) {
$prefix = ' [blue]';
} else if ($user->isSaver() && $clicksDiff == $highest['clicksSavedDiff']) {
$prefix = ' [color=purple]';
} else {
$prefix = ' [green]';
}
echo $prefix . '+' . Format::StatNumber($clicksDiff);
}
echo '[/td]';
// 5th column: third stat
echo '[td]';
if ($thirdStat == 'uptime') {
echo Format::Uptime($user->getRawData('uptime')) . '[/td][td]';
$uptimeDiff = $user->getRawData('uptimeDiff');
if ($uptimeDiff > 0) {
if (!$user->isSaver() && $uptimeDiff == $highest['uptimeDiff']) {
$prefix = ' [blue]';
} else if ($user->isSaver() && $uptimeDiff == $highest['uptimeSavedDiff']) {
$prefix = ' [color=purple]';
} else {
$prefix = ' [green]';
}
echo $prefix . '+' . Format::Uptime($uptimeDiff);
}
} elseif ($thirdStat == 'bandwidth') {
echo Format::Bandwidth($user->getRawData('bandwidth')) . '[/td][td]';
$bandwidthDiff = $user->getRawData('bandwidthDiff');
if ($bandwidthDiff > 0) {
if (!$user->isSaver() && $bandwidthDiff == $highest['bandwidthDiff']) {
$prefix = ' [blue]';
} else if ($user->isSaver() && $bandwidthDiff == $highest['bandwidthSavedDiff']) {
$prefix = ' [color=purple]';
} else {
$prefix = ' [green]';
}
echo $prefix . '+' . Format::Bandwidth($bandwidthDiff);
}
}
echo '[/td][/tr]' . ENDL;
}
echo '[/table]' . ENDL . ENDL;
// end of table
// display events
foreach ($events as $event) {
echo $event->getString() . ENDL;
}
if (count($events) > 0) {
echo ENDL;
}
// display totals
echo '[b]Totalen[/b]' . ENDL;
echo '[table]';
echo '[tr][td]Keys [/td]';
echo '[td]' . Format::StatNumber($totals['keys']) . '[tt] [/tt][/td]';
echo '[td]' . (($totals['keysDiff'] > 0)?'[green]+':'[red]-') . Format::StatNumber($totals['keysDiff']) . '[/td][/tr]' . ENDL;
echo '[tr][td]Kliks [/td]';
echo '[td]' . Format::StatNumber($totals['clicks']) . '[/td]';
echo '[td]' . (($totals['clicksDiff'] > 0)?'[green]+':'[red]-') . Format::StatNumber($totals['clicksDiff']) . '[/td][/tr]' . ENDL;
echo '[tr][td]Uptime [/td]';
echo '[td]' . Format::Uptime($totals['uptime']) . '[/td]';
echo '[td]' . (($totals['uptimeDiff'] > 0)?'[green]+':'[red]-') . Format::Uptime($totals['uptimeDiff']) . '[/td][/tr]' . ENDL;
echo '[tr][td]Download [/td]';
echo '[td]' . Format::Bandwidth($totals['download']) . '[/td]';
echo '[td]' . (($totals['downloadDiff'] > 0)?'[green]+':'[red]-') . Format::Bandwidth($totals['downloadDiff']) . '[/td][/tr]' . ENDL;
echo '[tr][td]Upload [/td]';
echo '[td]' . Format::Bandwidth($totals['upload']) . '[/td]';
echo '[td]' . (($totals['uploadDiff'] > 0)?'[green]+':'[red]-') . Format::Bandwidth($totals['uploadDiff']) . '[/td][/tr]' . ENDL;
if ($totals['pulsers'] > 0) {
echo '[tr][td]Pulsers[/td][td]' . $totals['pulsers'] . '[/td]';
echo '[td][abbr=Percentage van alle leden]' . round(($totals['pulsers'] / $totals['active']) * 100, 2).'%[/td][/tr]' . ENDL;
echo '[tr][td]Spaarders[tt] [/tt][/td][td]' . $totals['savers'] . '[/td]';
echo '[td][abbr=Percentage van de pulsers]' . round(($totals['savers'] / $totals['pulsers']) * 100, 2).'%[/td][/tr]' . ENDL;
}
echo '[/table]' . ENDL . ENDL;
echo Mirror::getMirrorsBBcode($mirrors);
echo '[/sup] ([url=https://github.com/goldenice/GMOT-Whatpulse-Parser]Broncode[/url])' . ENDL . ENDL;
if (DEVMODE || isset($_GET['devmode']) || isset($_GET['gentime'])) {
echo 'Generated in ' . ((microtime(true) - $starttime) * 1000) . ' milliseconds.';
echo 'On ' . date('r') . '.';
}