Example #1
0
function DBNewProblem($contestnumber, $param, $c = null)
{
    if (isset($param["action"]) && $param["action"] == "delete") {
        return DBDeleteProblem($contestnumber, $param);
    }
    $ac = array('number', 'name');
    $type['number'] = 1;
    $type['updatetime'] = 1;
    $ac1 = array('colorname', 'fake', 'color', 'updatetime', 'fullname', 'basename', 'inputfilename', 'inputfilepath');
    $colorname = '';
    $color = '';
    $fake = 'f';
    foreach ($ac as $key) {
        if (!isset($param[$key])) {
            MSGError("DBNewProblem param error: {$key} is not set");
            return false;
        }
        if (isset($type[$key]) && !is_numeric($param[$key])) {
            MSGError("DBNewProblem param error: {$key} is not numeric");
            return false;
        }
        ${$key} = sanitizeText($param[$key]);
    }
    $basename = '';
    $inputfilename = '';
    $inputfilepath = '';
    $fullname = '';
    $updatetime = -1;
    foreach ($ac1 as $key) {
        if (isset($param[$key])) {
            if (isset($type[$key]) && !is_numeric($param[$key])) {
                MSGError("DBNewProblem param error: {$key} is not numeric");
                return false;
            }
            ${$key} = sanitizeText($param[$key]);
        }
    }
    $t = time();
    if ($updatetime <= 0) {
        $updatetime = $t;
    }
    $inputhash = '';
    $sql2 = "select * from problemtable where contestnumber={$contestnumber} and problemnumber={$number} for update";
    // "select * from problemtable where contestnumber=$contestnumber and problemnumber=$number " .
    // "and probleminputfilename='$inputfilename'";
    $cw = false;
    if ($c == null) {
        $cw = true;
        $c = DBConnect();
        DBExec($c, "begin work", "DBNewProblem(transaction)");
    }
    $r = DBExec($c, $sql2, "DBNewProblem(get problem for update)");
    $n = DBnlines($r);
    $ret = 1;
    $oldfullname = '';
    $deservesupdatetime = false;
    if ($n == 0) {
        DBExec($c, "insert into problemtable (contestnumber, problemnumber, problemname, problemcolor) values " . "({$contestnumber}, {$number}, '{$name}','-1')", "DBNewProblem(insert problem)");
        $deservesupdatetime = true;
        $s = "created";
    } else {
        $lr = DBRow($r, 0);
        $t = $lr['updatetime'];
        $oldfullname = $lr['problemfullname'];
        $s = "updated";
        $inputhash = $lr['probleminputfilehash'];
    }
    if ($s == "created" || $updatetime > $t) {
        if (substr($inputfilepath, 0, 7) != "base64:") {
            if ($inputfilepath != "") {
                $hash = myshorthash(file_get_contents($inputfilepath));
                if ($hash != $inputhash) {
                    $oldoid = '';
                    if (isset($lr)) {
                        $oldoid = $lr['probleminputfile'];
                    }
                    if (($oid1 = DB_lo_import($c, $inputfilepath)) === false) {
                        DBExec($c, "rollback work", "DBNewProblem(rollback-input)");
                        LOGError("Unable to create a large object for file {$inputfilename}.");
                        MSGError("problem importing file to database. See log for details!");
                        exit;
                    }
                    if ($oldoid != '') {
                        DB_lo_unlink($c, $oldoid);
                    }
                    $inputhash = DBcrc($contestnumber, $oid1, $c);
                } else {
                    $oid1 = $lr['probleminputfile'];
                }
            }
        } else {
            $inputfilepath = base64_decode(substr($inputfilepath, 7));
            $hash = myshorthash($inputfilepath);
            if ($hash != $inputhash) {
                $oldoid = '';
                if (isset($lr)) {
                    $oldoid = $lr['probleminputfile'];
                }
                if (($oid1 = DB_lo_import_text($c, $inputfilepath)) == null) {
                    DBExec($c, "rollback work", "DBNewProblem(rollback-i-import)");
                    LOGError("Unable to import the large object for file {$inputfilename}.");
                    MSGError("problem importing file to database. See log for details!");
                    exit;
                }
                if ($oldoid != '') {
                    DB_lo_unlink($c, $oldoid);
                }
                $inputhash = DBcrc($contestnumber, $oid1, $c);
            } else {
                $oid1 = $lr['probleminputfile'];
            }
        }
        if ($name != "") {
            DBExec($c, "update problemtable set problemname='{$name}' where contestnumber={$contestnumber} " . "and problemnumber={$number}", "DBNewProblem(update name)");
        }
        if ($fullname != "" || strpos($oldfullname, '(DEL)') !== false) {
            $deservesupdatetime = true;
            DBExec($c, "update problemtable set problemfullname='{$fullname}' where contestnumber={$contestnumber} " . "and problemnumber={$number}", "DBNewProblem(update fullname)");
        }
        if ($basename != "") {
            $deservesupdatetime = true;
            DBExec($c, "update problemtable set problembasefilename='{$basename}' where contestnumber={$contestnumber} " . "and problemnumber={$number}", "DBNewProblem(update basename)");
        }
        if ($colorname != "") {
            DBExec($c, "update problemtable set problemcolorname='{$colorname}' where contestnumber={$contestnumber} " . "and problemnumber={$number}", "DBNewProblem(update colorname)");
        }
        if ($color != "") {
            DBExec($c, "update problemtable set problemcolor='{$color}' where contestnumber={$contestnumber} " . "and problemnumber={$number}", "DBNewProblem(update color)");
        }
        if ($inputfilename != "") {
            $deservesupdatetime = true;
            DBExec($c, "update problemtable set probleminputfilename='{$inputfilename}' where " . "contestnumber={$contestnumber} and problemnumber={$number} ", "DBNewProblem(update inputfilename)");
        }
        if ($inputfilepath != "") {
            $deservesupdatetime = true;
            DBExec($c, "update problemtable set probleminputfile={$oid1},probleminputfilehash='{$inputhash}' where contestnumber={$contestnumber} and " . "problemnumber={$number} ", "DBNewProblem(update inputfile)");
        }
        if ($fake == "t") {
            $deservesupdatetime = true;
            DBExec($c, "update problemtable set fake='{$fake}' where contestnumber={$contestnumber} and " . "problemnumber={$number}", "DBNewProblem(update fake)");
        }
        if ($deservesupdatetime) {
            $ds = DIRECTORY_SEPARATOR;
            if ($ds == "") {
                $ds = "/";
            }
            @unlink($_SESSION["locr"] . $ds . "private" . $ds . "problemtmp" . $ds . "contest" . $contestnumber . "-problem" . $number . '.name');
            DBExec($c, "update problemtable set updatetime=" . $updatetime . " where contestnumber={$contestnumber} and problemnumber={$number}", "DBNewProblem(time)");
        }
        if ($cw) {
            DBExec($c, "commit work", "DBNewProblem(commit)");
        }
        LOGLevel("Problem {$number} (inputfile={$inputfilename}) {$s} (user="******"usertable"]["usernumber"] . ",site=" . $_SESSION["usertable"]["usersitenumber"] . ",contest={$contestnumber})", 2);
        $ret = 2;
    } else {
        if ($cw) {
            DBExec($c, "commit work", "DBNewProblem(commit)");
        }
    }
    return $ret;
}
Example #2
0
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
////////////////////////////////////////////////////////////////////////////////
// Last modified 31/aug/2012 by cassio@ime.usp.br
if ($_POST["confirmation"] != "confirm") {
    unset($_POST['noflush']);
}
require 'header.php';
if (($ct = DBContestInfo($_SESSION["usertable"]["contestnumber"])) == null) {
    ForceLoad("../index.php");
}
if (isset($_GET["delete"]) && is_numeric($_GET["delete"]) && isset($_GET["input"])) {
    $param = array();
    $param['number'] = $_GET["delete"];
    $param['inputfilename'] = $_GET["input"];
    if (!DBDeleteProblem($_SESSION["usertable"]["contestnumber"], $param)) {
        MSGError('Error deleting problem');
        LogError('Error deleting problem');
    }
    ForceLoad("problem.php");
}
if (isset($_POST['Submit5']) && $_POST['Submit5'] == 'Build') {
    ForceLoad("buildproblem.php");
}
if (isset($_POST['Submit5']) && $_POST['Submit5'] == 'Send') {
    if (isset($_POST['basename']) && isset($_POST['fullname']) && isset($_POST['timelimit']) && $_POST["confirmation"] == "confirm") {
        if ($_FILES["probleminput"]["name"] != "") {
            $type = myhtmlspecialchars($_FILES["probleminput"]["type"]);
            $size = myhtmlspecialchars($_FILES["probleminput"]["size"]);
            $name = myhtmlspecialchars($_FILES["probleminput"]["name"]);
            $temp = myhtmlspecialchars($_FILES["probleminput"]["tmp_name"]);