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

Tags
badging system, debug, forum development, function, limit, php, uid, web programming

 
Thread Tools Search this Thread
# 1  
Old 1 Week Ago
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:
RavinderSingh13 (1 Week Ago) rbatte1 (1 Week Ago)

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
[Solved] Identify Color and send email with same color Kingcobra Shell Programming and Scripting 10 03-26-2014 02:33 PM
How to change the background color in the init 3 mode(not line color) Dexobox UNIX for Dummies Questions & Answers 2 07-12-2012 10:15 AM
Internal software error in the tostring function for files greater than 300 MB script_op2a Shell Programming and Scripting 0 05-22-2011 07:23 PM
AWK limit for (length) function? CKT_newbie88 Shell Programming and Scripting 3 05-03-2009 11:57 PM
how to write a wrapper c code to return uid using getuid() function pwd Programming 4 05-13-2008 11:01 AM