function action_commitdata()
{
    global $currentmessage, $mysql_database;
    if ($_SESSION["status"] != "Admin") {
        $_SESSION["message"] = $currentmessage;
        $_SESSION["message"][] = "Access Denied : You need to be an Administrator to perform that operation.";
        echo "<script>window.location='?display=faq';</script>";
        return;
    }
    if (!isset($_POST["recordname"]) || empty($_POST["recordname"])) {
        $_SESSION["message"][] = "Data Commitment Error : Insufficient Data";
        return;
    }
    $tablename = "backup_" . time();
    $temp = mysql_query("SHOW TABLES LIKE '{$tablename}';");
    if (is_resource($temp) && mysql_num_rows($temp) > 0) {
        $_SESSION["message"][] = "Data Commitment Error : A table with that name already exists.";
        return;
    }
    action_adminwork();
    $problems = mysql_query("SELECT * FROM problems WHERE status='Active'");
    if (!is_resource($problems)) {
        $_SESSION["message"][] = "Data Commitment Error : Failure 001.";
        return;
    }
    $nprob = mysql_num_rows($problems);
    $teams = mysql_query("SELECT * FROM teams WHERE status='Normal'");
    if (!is_resource($teams)) {
        $_SESSION["message"][] = "Data Commitment Error : Failure 002.";
        return;
    }
    $columns = "info,id,name,score";
    for ($i = 1; $i <= $nprob; $i++) {
        for ($j = 1; $j <= 6; $j++) {
            $columns .= ",data_" . $i . "_" . $j;
        }
    }
    // Create Table
    $query = "CREATE TABLE {$tablename} (bid int not null primary key auto_increment,info tinytext,id tinytext,name tinytext,score tinytext";
    for ($i = 1; $i <= max(1, $nprob); $i++) {
        for ($j = 1; $j <= 6; $j++) {
            $query .= ", data_" . $i . "_" . $j . " longtext";
        }
    }
    $query .= ");";
    mysql_query($query);
    // Insert System Index
    $_POST["recordname"] = eregi_replace("\"", "'", $_POST["recordname"]);
    mysql_query("INSERT INTO {$tablename} (info,id,name,score) VALUES ('system-fields','Timestamp','Commit Name','Status');");
    mysql_query("INSERT INTO {$tablename} (info,id,name,score) VALUES ('system','" . time() . "','" . addslashes($_POST["recordname"]) . "','Active');");
    // Insert Problem Index
    mysql_query("INSERT INTO {$tablename} (info,id,name,score,data_1_1,data_1_2,data_1_3,data_1_4,data_1_5,data_1_6) VALUES ('problem-fields','Problem ID','Problem Name','Problem Score','Problem Statement','Problem Input','Problem Output','Problem Time Limit','Problem Type','Problem Statistics');");
    // Insert Problems
    $pids = array();
    while ($problem = mysql_fetch_array($problems)) {
        $stat1 = mysql_query("SELECT count( DISTINCT tid ) FROM runs WHERE result='AC' AND pid=" . $problem["pid"]);
        if (!is_resource($stat1) || !mysql_num_rows($stat1)) {
            echo mysql_error();
        }
        //continue;
        $stat1 = mysql_fetch_array($stat1);
        $stat2 = mysql_query("SELECT count( DISTINCT tid ) FROM runs WHERE pid=" . $problem["pid"]);
        if (!is_resource($stat2) || !mysql_num_rows($stat2)) {
            echo mysql_error();
        }
        //continue;
        $stat2 = mysql_fetch_array($stat2);
        $statistics = $stat1[0] . "/" . $stat2[0];
        $pids[] = $problem["pid"];
        mysql_query("INSERT INTO {$tablename} (info,id,name,score,data_1_1,data_1_2,data_1_3,data_1_4,data_1_5,data_1_6) VALUES ('problem','" . addslashes($problem["pid"]) . "','" . addslashes($problem["name"]) . "','" . addslashes($problem["score"]) . "','" . addslashes($problem["statement"]) . "','" . addslashes($problem["input"]) . "','" . addslashes($problem["output"]) . "','" . addslashes($problem["timelimit"]) . "','" . addslashes($problem["type"]) . "','{$statistics}');");
    }
    // Insert Team Index
    $query = "INSERT INTO {$tablename} ({$columns}) VALUES ('team-fields','Team ID','Team Name','Team Score'";
    foreach ($pids as $pid) {
        $query .= ",'Problem-{$pid} Attempts','Problem-{$pid} Language','Problem-{$pid} Code','Problem-{$pid} Result','Problem-{$pid} Time','Problem-{$pid} Error'";
    }
    $query .= ");";
    mysql_query($query);
    // Insert Teams
    while ($team = mysql_fetch_array($teams)) {
        $query = "INSERT INTO {$tablename} ({$columns}) VALUES ('team','" . addslashes($team["tid"]) . "','" . addslashes($team["teamname"]) . "','" . addslashes($team["score"]) . ":" . addslashes($team["penalty"]) . "'";
        foreach ($pids as $pid) {
            $ac3 = mysql_query("SELECT * FROM runs WHERE tid='{$team['tid']}' AND pid='{$pid}' AND result='AC' ORDER BY rid DESC LIMIT 0,1");
            if (is_resource($ac3) && mysql_num_rows($ac3) == 1) {
                $ac3 = mysql_fetch_array($ac3);
            } else {
                $ac3 = NULL;
            }
            if ($ac3 != NULL) {
                $ac2 = mysql_query("SELECT * FROM runs WHERE tid='{$team['tid']}' AND pid='{$pid}' AND result='AC' AND access!='deleted' ORDER BY rid ASC  LIMIT 0,1");
                if (is_resource($ac2) && mysql_num_rows($ac2) == 1) {
                    $ac2 = mysql_fetch_array($ac2);
                } else {
                    $ac2 = NULL;
                }
                $ac1 = mysql_query("SELECT count(*) FROM runs WHERE tid='{$team['tid']}' AND pid='{$pid}' AND result!='AC' AND access!='deleted' AND rid<=" . $ac2["rid"]) + 1;
                if (is_resource($ac1) && mysql_num_rows($ac1) == 1) {
                    $ac1 = mysql_fetch_array($ac1);
                } else {
                    $ac1 = NULL;
                }
                $query .= ",'{$ac1['0']}','{$ac2['language']}','{$ac2['code']}','{$ac2['result']}','{$ac2['time']}','{$ac2['error']}'";
            } else {
                $ac1 = mysql_query("SELECT count(*) FROM runs WHERE tid='{$team['tid']}' AND pid='{$pid}' AND result!='AC' AND access!='deleted'");
                if (is_resource($ac1) && mysql_num_rows($ac1) == 1) {
                    $ac1 = mysql_fetch_array($ac1);
                } else {
                    $ac1 = NULL;
                }
                $ac0 = mysql_query("SELECT * FROM runs WHERE tid='{$team['tid']}' AND pid='{$pid}' AND access!='deleted' ORDER BY rid DESC LIMIT 0,1");
                if (is_resource($ac0) && mysql_num_rows($ac0) == 1) {
                    $ac0 = mysql_fetch_array($ac0);
                } else {
                    $ac0 = NULL;
                }
                if ($ac0 == NULL) {
                    $query .= ",'0','','','','',''";
                } else {
                    $query .= ",'{$ac1['0']}','{$ac0['language']}','{$ac0['code']}','{$ac0['result']}','{$ac0['time']}','{$ac0['error']}'";
                }
            }
        }
        // for each problem
        $query .= ");";
        mysql_query($query);
    }
    // for each team
    $_SESSION["message"][] = "Data Commitment Successful";
    action_scoreboard();
}
function mysql_initiate()
{
    global $mysql_hostname, $mysql_username, $mysql_password, $mysql_database, $admin, $ajaxlogout, $sessionid, $admin_teamname, $admin_password;
    $link = mysql_connect($mysql_hostname, $mysql_username, $mysql_password);
    if (!$link) {
        $_SESSION["message"][] = "SQL Error : Could Not Establish Connection.";
        return;
    }
    if (!mysql_select_db($mysql_database)) {
        mysql_query("CREATE DATABASE " . $mysql_database);
        if (!mysql_select_db($mysql_database)) {
            $_SESSION["message"][] = "SQL Error : Could Not Select Database.";
            return;
        }
    }
    $data = mysql_list_tables($mysql_database);
    $table = array();
    if (is_resource($data)) {
        while ($temp = mysql_fetch_row($data)) {
            $table[] = $temp[0];
        }
    }
    if (!in_array("teams", $table)) {
        mysql_query("CREATE TABLE teams (tid int not null primary key auto_increment,teamname tinytext,teamname2 tinytext,pass tinytext,status tinytext,score int,penalty bigint,name1 tinytext,roll1 tinytext,branch1 tinytext,email1 tinytext,phone1 tinytext,name2 tinytext,roll2 tinytext,branch2 tinytext,email2 tinytext,phone2 tinytext,name3 tinytext,roll3 tinytext,branch3 tinytext,email3 tinytext,phone3 tinytext,platform text,ip text,session tinytext,gid int not null)");
    }
    if (!in_array("problems", $table)) {
        mysql_query("CREATE TABLE problems (pid int not null primary key auto_increment,code tinytext,name tinytext,type tinytext,status tinytext,pgroup tinytext,statement longtext,image blob,imgext tinytext,input longtext,output longtext,timelimit int,score int,languages tinytext,options tinytext)");
    }
    if (!in_array("runs", $table)) {
        mysql_query("CREATE TABLE runs (rid int not null primary key auto_increment,pid int,tid int,language tinytext,name tinytext,code longtext,time tinytext,result tinytext,error text,access tinytext,submittime int,output longtext)");
    }
    if (!in_array("admin", $table)) {
        mysql_query("CREATE TABLE admin (variable tinytext,value longtext)");
    }
    if (!in_array("logs", $table)) {
        mysql_query("CREATE TABLE logs (time int not null primary key,ip tinytext,tid int,request tinytext)");
    }
    if (!in_array("clar", $table)) {
        mysql_query("CREATE TABLE clar (time int not null primary key,tid int,pid int,query text,reply text,access tinytext,createtime int)");
    }
    if (!in_array("groups", $table)) {
        mysql_query("CREATE TABLE groups (gid int not null primary key auto_increment, groupname tinytext, statusx int)");
    }
    // If empty tables
    $temp = mysql_query("SELECT * FROM teams");
    if (is_resource($temp) && mysql_num_rows($temp) == 0) {
        mysql_query("INSERT INTO teams (teamname,pass,status,score,name1,roll1,branch1,email1,phone1) VALUES ('" . $admin_teamname . "','" . _md5($admin_password) . "','Admin',0,'Kaustubh Karkare','','','*****@*****.**','')");
        mysql_query("INSERT INTO teams (teamname,pass,status,score,name1,roll1,branch1,email1,phone1) VALUES ('ACM','" . _md5($admin_password) . "','Admin',0,'ACM Team','','','','')");
        ###
    }
    $temp = mysql_query("SELECT * FROM problems");
    if (is_resource($temp) && mysql_num_rows($temp) == 0) {
        mysql_query("INSERT INTO problems (pid,code,name,type,status,pgroup,statement,input,output,timelimit,score,languages) VALUES (1,'TEST','Squares','Ad-Hoc','Active','#00 Test','" . addslashes(file_get('data/example/problem.txt')) . "','" . addslashes(file_get('data/example/input.txt')) . "','" . addslashes(file_get('data/example/output.txt')) . "',1,0,'Brain,C,C++,C#,Java,JavaScript,Pascal,Perl,PHP,Python,Ruby,Text')");
    }
    $temp = mysql_query("SELECT * FROM runs");
    if (is_resource($temp) && mysql_num_rows($temp) == 0) {
        mysql_query("INSERT INTO runs (rid,pid,tid,language,name,code,time,result,access) VALUES (1,1,1,'C','code','" . addslashes(file_get('data/example/code.c')) . "',NULL,NULL,'public')");
        mysql_query("INSERT INTO runs (rid,pid,tid,language,name,code,time,result,access) VALUES (2,1,1,'C++','code','" . addslashes(file_get('data/example/code.cpp')) . "',NULL,NULL,'public')");
        mysql_query("INSERT INTO runs (rid,pid,tid,language,name,code,time,result,access) VALUES (3,1,1,'C#','code','" . addslashes(file_get('data/example/code.cs')) . "',NULL,NULL,'public')");
        mysql_query("INSERT INTO runs (rid,pid,tid,language,name,code,time,result,access) VALUES (4,1,1,'Java','code','" . addslashes(file_get('data/example/code.java')) . "',NULL,NULL,'public')");
        mysql_query("INSERT INTO runs (rid,pid,tid,language,name,code,time,result,access) VALUES (5,1,1,'JavaScript','code','" . addslashes(file_get('data/example/code.js')) . "',NULL,NULL,'public')");
        mysql_query("INSERT INTO runs (rid,pid,tid,language,name,code,time,result,access) VALUES (6,1,1,'Pascal','code','" . addslashes(file_get('data/example/code.pas')) . "',NULL,NULL,'public')");
        mysql_query("INSERT INTO runs (rid,pid,tid,language,name,code,time,result,access) VALUES (7,1,1,'Perl','code','" . addslashes(file_get('data/example/code.pl')) . "',NULL,NULL,'public')");
        mysql_query("INSERT INTO runs (rid,pid,tid,language,name,code,time,result,access) VALUES (8,1,1,'PHP','code','" . addslashes(file_get('data/example/code.php')) . "',NULL,NULL,'public')");
        mysql_query("INSERT INTO runs (rid,pid,tid,language,name,code,time,result,access) VALUES (9,1,1,'Python','code','" . addslashes(file_get('data/example/code.py')) . "',NULL,NULL,'public')");
        mysql_query("INSERT INTO runs (rid,pid,tid,language,name,code,time,result,access) VALUES (10,1,1,'Ruby','code','" . addslashes(file_get('data/example/code.rb')) . "',NULL,NULL,'public')");
    }
    $temp = mysql_query("SELECT * FROM admin");
    if (is_resource($temp) && mysql_num_rows($temp) == 0) {
        mysql_query("INSERT INTO admin VALUES ('mode','Passive');");
        mysql_query("INSERT INTO admin VALUES ('lastjudge','0');");
        mysql_query("INSERT INTO admin VALUES ('ajaxrr','0');");
        mysql_query("INSERT INTO admin VALUES ('mode','Passive');");
        mysql_query("INSERT INTO admin VALUES ('penalty','20');");
        mysql_query("INSERT INTO admin VALUES ('mysublist','5');");
        mysql_query("INSERT INTO admin VALUES ('allsublist','10');");
        mysql_query("INSERT INTO admin VALUES ('ranklist','10');");
        mysql_query("INSERT INTO admin VALUES ('clarpublic','2');");
        mysql_query("INSERT INTO admin VALUES ('clarprivate','2');");
        mysql_query("INSERT INTO admin VALUES ('regautoauth','1');");
        mysql_query("INSERT INTO admin VALUES ('multilogin','0');");
        mysql_query("INSERT INTO admin VALUES ('clarpage','10');");
        mysql_query("INSERT INTO admin VALUES ('substatpage','25');");
        mysql_query("INSERT INTO admin VALUES ('probpage','25');");
        mysql_query("INSERT INTO admin VALUES ('teampage','25');");
        mysql_query("INSERT INTO admin VALUES ('rankpage','25');");
        mysql_query("INSERT INTO admin VALUES ('logpage','100');");
        mysql_query("INSERT INTO admin VALUES ('notice','Announcements\nWelcome to the Aurora Online Judge.');");
    }
    // Other Inits
    $data = mysql_query("SELECT * FROM admin");
    if (is_resource($data)) {
        while ($temp = mysql_fetch_array($data)) {
            if (!in_array($temp["variable"], array("scoreboard"))) {
                $admin[$temp["variable"]] = $temp["value"];
            }
        }
    }
    if ($admin["mode"] == "Active" && time() >= $admin["endtime"]) {
        $admin["mode"] = "Disabled";
    }
    if ($admin["mode"] == "Lockdown" && $_SESSION["tid"] != 0 && $_SESSION["status"] != "Admin") {
        $_SESSION["message"][] = "Access Denied : You have been logged out as the contest has been locked down. Please try again again.";
        action_logout();
        $ajaxlogout = 1;
    }
    if (!$admin["multilogin"] && $_SESSION["tid"] && $_SESSION["status"] != "Admin") {
        $sess = mysql_query("SELECT session FROM teams WHERE tid=" . $_SESSION["tid"]);
        $sess = mysql_fetch_array($sess);
        $sess = $sess["session"];
        if ($sess != $sessionid) {
            $_SESSION["message"][] = "Multiple Login Not Allowed.";
            action_logout();
            $ajaxlogout = 1;
        }
    }
    if (1 || !isset($admin["adminwork"]) || $admin["adminwork"] < time()) {
        action_adminwork();
        $admin["adminwork"] = time() + 10;
    }
    return 0;
    // Success
}