function action_adminwork()
{
    global $admin, $fullresult;
    // Update Team Scores
    $data = mysql_query("SELECT pid,score FROM problems WHERE status='Active'");
    $score = array();
    if (is_resource($data)) {
        while ($temp = mysql_fetch_array($data)) {
            $score[$temp["pid"]] = $temp["score"];
        }
    }
    $data = mysql_query("SELECT * FROM teams");
    if (is_resource($data)) {
        while ($temp = mysql_fetch_array($data)) {
            $tid = $temp["tid"];
            $solvedn = 0;
            $score = 0;
            $penalty = 0;
            $prob = mysql_query("SELECT distinct(runs.pid) as pid,problems.score FROM runs,problems WHERE runs.tid='{$tid}' and runs.result='AC' and runs.pid=problems.pid and problems.status='Active' and runs.access!='deleted' ");
            if (is_resource($prob)) {
                $solvedn = mysql_num_rows($prob);
                while ($temp = mysql_fetch_array($prob)) {
                    $pid = $temp["pid"];
                    $score += $temp["score"];
                    $sub = mysql_query("SELECT rid,submittime FROM runs WHERE result='AC' and tid={$tid} and pid={$pid} and access!='deleted' ORDER BY rid ASC LIMIT 0,1");
                    if (is_resource($sub) && ($t = mysql_fetch_array($sub))) {
                        $penalty += $t["submittime"];
                        $sub = mysql_query("SELECT count(*) as incorrect FROM runs WHERE result!='AC' and access!='deleted' and rid<" . $t["rid"] . " and tid={$tid} and pid={$pid}");
                        if (is_resource($sub) && ($t = mysql_fetch_array($sub))) {
                            $penalty += $t["incorrect"] * (isset($admin["penalty"]) ? $admin["penalty"] * 60 : 0);
                        }
                    }
                }
            }
            if ($penalty == 0) {
                $penalty = 2000000000;
            }
            // just to put someone with at least one AC submission (of 0 points) above others without it
            mysql_query("UPDATE teams SET score={$score},penalty='{$penalty}' WHERE tid={$tid}");
        }
    }
    $json = "<h3><a href='?display=rankings'>Current Rankings</a></h3>";
    $json .= "<table><th>Rank</th><th>Team</th><th>Solved</th><th>Score</th></tr>";
    if (isset($admin["ranklist"]) && $admin["ranklist"] >= 0) {
        $limit = $admin["ranklist"];
    } else {
        $limit = 10;
    }
    $data = mysql_query("SELECT * FROM teams WHERE status='Normal' ORDER BY score DESC, penalty ASC LIMIT 0,{$limit}");
    if (is_resource($data)) {
        for ($rank = 1; $temp = mysql_fetch_array($data); $rank++) {
            $solvedn = mysql_query("SELECT count(distinct(runs.pid)) as n FROM runs,problems WHERE runs.tid='{$temp['tid']}' and runs.result='AC' and runs.pid=problems.pid and problems.status='Active'");
            if (is_resource($solvedn) && mysql_num_rows($solvedn) == 1) {
                $solvedn = mysql_fetch_array($solvedn);
                $solvedn = $solvedn["n"];
            } else {
                $solvedn = 0;
            }
            $json .= "<!--{$temp['tid']}--><tr><td>{$rank}</td><td><a href='?display=submissions&tid={$temp['tid']}'>{$temp['teamname']}</td><td>{$solvedn}</td><td>{$temp['score']}</td></tr><!--{$temp['tid']}-->";
        }
    }
    $json .= "</table>";
    $admin["cache-rankings"] = $json;
    $json = "<h3><a href='?display=submissions'>Latest Submissions</a></h3>";
    $json .= "<table><th title='Run ID'>RID</th><th>Team</th><th>Problem</th><th>Result</th></tr>";
    if (isset($admin["allsublist"]) && $admin["allsublist"] >= 0) {
        $limit = $admin["allsublist"];
    } else {
        $limit = 10;
    }
    $data = mysql_query("SELECT runs.result as result,runs.rid as rid,runs.tid as tid,runs.pid as pid,teams.teamname as teamname,problems.name as probname,problems.code as probcode FROM runs,problems,teams WHERE runs.access!='deleted' AND runs.pid = problems.pid AND runs.tid = teams.tid AND runs.access!='deleted' AND teams.status='Normal' AND problems.status='Active' ORDER BY rid DESC LIMIT 0," . $limit);
    if (is_resource($data)) {
        while ($temp = mysql_fetch_array($data)) {
            $result = $temp["result"];
            if (isset($fullresult[$result])) {
                $result = $fullresult[$result];
            }
            //$json.="<tr class='$temp[result]'><td>$temp[rid]</td><td><a href='?display=submissions&tid=$temp[tid]'>$teamname</td><td title=\"$probname\"><a href='?display=problem&pid=$temp[pid]'>$probcode</td><td title='$result'>$temp[result]</td></tr>";
            $json .= "<tr class='{$temp['result']}'><td>{$temp['rid']}</td><td><a href='?display=submissions&tid={$temp['tid']}'>" . substr($temp["teamname"], 0, 100) . (strlen($temp["teamname"]) > 100 ? "..." : "") . "</td><td title=\"{$temp['probname']}\"><a href='?display=problem&pid={$temp['pid']}'>{$temp['probcode']}</td><td title='{$result}'>{$temp['result']}</td></tr>";
        }
    }
    $json .= "</table>";
    $admin["cache-allsubmit"] = $json;
    $json = "";
    if (($g = mysql_getdata("SELECT distinct pgroup FROM problems WHERE status='Active' ORDER BY pgroup")) != NULL) {
        $t = array();
        foreach ($g as $gn) {
            $t[] = $gn["pgroup"];
        }
        $g = $t;
        unset($t);
        $json .= "<h3><a href='?display=problem'>Problems Index</a></h3><div class='problist'><table>";
        if (in_array("", $g)) {
            unset($g[array_search("", $g)]);
            $g[] = "";
        }
        foreach ($g as $gn) {
            $json .= "<tr><th colspan=2>" . eregi_replace("^#[0-9]+ ", "", $gn == "" ? "Unclassified" : $gn) . "</th></tr><tr class='AC'><td><i>Problem Name</i></td><td><i>Points</i></td></tr>";
            $data = mysql_query("SELECT * FROM problems WHERE status='Active' AND pgroup='{$gn}'");
            if (is_resource($data)) {
                while ($temp = mysql_fetch_array($data)) {
                    $json .= "<tr><td><a href='?display=problem&pid={$temp['pid']}' ";
                    if ($admin["mode"] == "Active" && $_SESSION["status"] != "Admin") {
                    } else {
                        $json .= " title=\"" . stripslashes($temp["type"]) . "\"";
                    }
                    $json .= ">" . stripslashes($temp["name"]) . "</a></td><td>{$temp['score']}</td></tr>";
                }
            }
            //$json.= "</table><br><table>";
        }
        $json .= "</table></div>";
    }
    $admin["cache-problems"] = $json;
    action_clarcache();
}
function action_updateclar()
{
    global $invalidchars;
    if ($_SESSION["status"] != "Admin" && $_SESSION["tid"] != 0) {
        // special condition for normal users to delete their clarifications
        if (!isset($_POST["field"]) || empty($_POST["field"])) {
            $_SESSION["message"][] = "Clarification Update Error : Insufficient Data.";
            return;
        }
        action_clarcache();
        if ($_POST["field"] == "Status") {
            if (!isset($_POST["time"]) || empty($_POST["time"]) || !isset($_POST["value"]) || empty($_POST["value"])) {
                $_SESSION["message"][] = "Clarification Update Error : Insufficient Data.";
                return;
            }
            if (!in_array($_POST["value"], array("Delete"))) {
                $_SESSION["message"][] = "Clarification Update Error : Invalid Data.";
                return;
            }
            mysql_query("UPDATE clar SET access='Delete',time=" . time() . " WHERE tid=" . $_SESSION["tid"] . " AND reply='' AND time=" . $_POST["time"]);
            $_SESSION["message"][] = "Clarification Deletion Successful.";
        }
        action_clarcache();
        return;
    }
    if (!isset($_POST["field"]) || empty($_POST["field"])) {
        $_SESSION["message"][] = "Clarification Update Error : Insufficient Data.";
        return;
    }
    if ($_SESSION["status"] != "Admin") {
        $_SESSION["message"][] = "Clarification Update Error : You need to be an Administrator to perform this action.";
        return;
    }
    if ($_POST["field"] == "Reply") {
        if (!isset($_POST["time"]) || empty($_POST["time"])) {
            $_SESSION["message"][] = "Clarification Update Error : Insufficient Data.";
            return;
        }
        if (eregi($invalidchars, $_POST["value"])) {
            $_SESSION["message"][] = "Clarification Update Error : Invalid characters in Reply.";
            return;
        }
        if (!empty($_POST["value"])) {
            mysql_query("UPDATE clar SET time=" . time() . ",reply='" . addslashes(filter($_POST["value"])) . "' WHERE time=" . $_POST["time"]);
        } else {
            mysql_query("UPDATE clar SET reply='' WHERE time=" . $_POST["time"]);
        }
    }
    if ($_POST["field"] == "Status") {
        if (!isset($_POST["time"]) || empty($_POST["time"]) || !isset($_POST["value"]) || empty($_POST["value"])) {
            $_SESSION["message"][] = "Clarification Update Error : Insufficient Data.";
            return;
        }
        if (!in_array($_POST["value"], array("Public", "Private", "Delete"))) {
            $_SESSION["message"][] = "Clarification Update Error : Invalid Data.";
            return;
        }
        mysql_query("UPDATE clar SET access='" . $_POST["value"] . "',time=" . time() . " WHERE time=" . $_POST["time"]);
    }
    if ($_POST["field"] == "Clear") {
        mysql_query("UPDATE clar SET access='Delete'");
    }
    action_clarcache();
}