Code:
<?php
function check_badges($color, $uid, $limit = 300, $_DEBUG = true)
{
/*
* check_badges() version 0.1 by Neo 9 Jan 2019
* $_COOKIE['badgeshash'] is not used in this server-side code
* but may be used in the browser. Mostly for debugging.
* $color is an array of badge colors
*/
global $vbulletin;
$now = time();
$st = 0;
$output = null;
if (isset($_COOKIE['badgeshashtime'])) {
$diff = $now - $_COOKIE['badgeshashtime'];
} else {
setcookie("badgeshashtime", $now);
$diff = $now;
}
if ($diff > $limit) {
$st = 1;
$badges_serialized = json_encode($color);
$hash = hash('ripemd160', $badges_serialized);
$getbadges = "SELECT badges AS badges FROM user where userid =" . $uid;
$userbadges = $vbulletin->db->query_first($getbadges);
$badgesDB = json_decode($userbadges['badges'], true);
$badgeDiff = array_diff_assoc($badgesDB, $color);
$output = get_diff_string($badgeDiff);
$countDiff = sizeof($badgeDiff);
if ($_DEBUG) {
$diffB = print_r($badgeDiff, true);
$badgeDiffR = array_diff_assoc($color, $bDiff);
$diffBR = print_r($badgeDiffR, true);
}
if (isset($_COOKIE['badgeshash'])) {
$cookiehash = $_COOKIE['badgeshash'];
} else {
setcookie("badgeshash", $hash);
setcookie("badgeshashtime", $now);
}
$gethash = "SELECT badgeshash AS hash FROM user where userid =" . $uid;
$userhash = $vbulletin->db->query_first($gethash);
if ($hash != $userhash['hash']) {
if ($countDiff > 0 and $_DEBUG) {
error_log(date(DATE_RFC822) . " User " .
$modaluser['username'] . ' Hash ' . $hash .
' Cookie ' . $cookiehash . ' St: 1 Diff(' . $countDiff . ') ' .
$diffB . ' Diff String: ' . $output .
"\n", 3, '/var/log/apache2/debug/neo_global_uim_hash_diff.log');
}
$updatehash = "UPDATE user SET badgeshash ='" . $hash . "' WHERE userid =" . $uid;
$status = $vbulletin->db->query_write($updatehash);
$updatebadges = "UPDATE user SET badges ='" . $badges_serialized . "' WHERE userid =" . $uid;
$status = $vbulletin->db->query_write($updatebadges);
setcookie("badgestatechange", $st);
setcookie("badgeshash", $hash);
setcookie("badgeshashtime", $now);
}
if ($_DEBUG) {
error_log(date(DATE_RFC822) . " GLOBAL Badges for: " .
$modaluser['username'] . ' Hash ' . $hash . ' Cookie ' .
$cookiehash . ' State ' . $st .
"\n", 3, '/var/log/apache2/debug/neo_global_uim_hash.log');
}
}
return $output;
}
function get_diff_string($badgesDiffArray)
{
$string = '';
$size = sizeof($badgesDiffArray);
$i = 0;
$badges_desc = array(
"fauser" => "Post Count",
"faaward" => "First Post",
"fajediorder" => "100 Posts",
"faphoenix" => "500 Posts",
"fajedi" => "1,000 Posts",
"faatom" => "10,000 Posts",
"fascale" => "Moderator",
"facrab" => "Order of the Crab",
"fawizard" => "Order of the Wizards's Hat",
"facrow" => "Order of the Raven",
"fahippo" => "Order of the Hippo",
"faspider" => "Order of the Spider",
"fadragon" => "Order of the Dragon",
"facrow" => "Order of the Raven",
"fabuilding" => "Administrator",
"fabishop" => "Forum Advisor",
"facity" => "Web Dev Ops",
"faflask" => "Formulator",
"fathumbsup" => "Thanks Received",
"fatrophy" => "100 Thanks Received",
"fachalkboardteacher" => "1,000 Thanks Received",
"fashieldalt" => "2,000 Thanks Received",
"fastroopwafel" => "3,000 Thanks Received",
"fafirstorderalt" => "4,000 Thanks Received",
"faidbadge" => "Member Profile",
"faaddressbook" => "Contact Methods",
"fabtc" => "Bits",
"fauniversity" => "Bits Banker",
"fausergraduate" => "Activity",
"farocket" => "Thanks This Past Month",
"facomment" => "Post This Past Month",
"faterminal" => "Discussions Started This Past Month",
"fabolt" => "Posts Per Day",
"faknight" => "Discussions Started",
"fadownload" => "Attachments Uploaded",
"fatags" => "Tags Added",
"faeye" => "Discussion Started Max Views",
"fainfraction" => "Infraction Points",
"fathanks_given" => "Thanks Given",
"fapatreon" => "Patreon Sponsor",
"fajoin_time" => "Time A Member",
"fahistory" => "Weeks Active In A Row",
);
foreach ($badgesDiffArray as $key => $color) {
if ($i++ < $size -1) {
$string .= $badges_desc[$key] . ', ';
} else {
$string .= $badges_desc[$key];
}
}
return $string;
}