Function check_badges($color, $uid, $limit = 300, $_DEBUG = true)


 
Thread Tools Search this Thread
Top Forums Web Development Function check_badges($color, $uid, $limit = 300, $_DEBUG = true)
# 1  
Old 01-08-2019
Function check_badges($color, $uid, $limit = 300, $_DEBUG = true)

Here is the first draft PHP function to check badges:


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;
}

These 2 Users Gave Thanks to Neo For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

3 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Internal software error in the tostring function for files greater than 300 MB

Hello, When I run my awk script with input files greater than 300 MB I always get this error: awk: Internal software error in the tostring function on TS1101?05044400?.0085498227?0?.0011041461?.0034752266?.00397045?0?0?0?0?0?0?11/02/10?09/23/10???10?no??0??no?sct_det3_10_20110516_143936.txt ... (0 Replies)
Discussion started by: script_op2a
0 Replies

2. Shell Programming and Scripting

AWK limit for (length) function?

I am trying to use the following code: awk '{s=$0;if(length(s) < 750){getline; s=s " " $0}printf("%s\n",s)}' filename but an error shows that 'awk' is too long. Is there a limit to the awk length function? and what could be an alternate solution for long fixed width records? The code... (3 Replies)
Discussion started by: CKT_newbie88
3 Replies

3. Programming

how to write a wrapper c code to return uid using getuid() function

And how to use setuid() ? thanks (4 Replies)
Discussion started by: pwd
4 Replies
Login or Register to Ask a Question