示例#1
0
文件: fbkp.php 项目: sbaldrich/boca
function DBBkpDelete($number, $site, $contest, $user, $adm = '')
{
    $c = DBConnect();
    DBExec($c, "begin work", "DBBkpDelete(transaction)");
    $sql = "select r.bkpdata as oid from bkptable as r where r.contestnumber={$contest} and " . "r.sitenumber={$site} and r.bkpnumber={$number} and r.usernumber={$user}";
    $r = DBExec($c, $sql . " for update", "DBBkpDelete(get bkp for update)");
    $n = DBnlines($r);
    if ($n != 1) {
        DBExec($c, "rollback work", "DBBkpDelete(rollback)");
        LogLevel("Unable to delete a bkp. " . "(bkp={$number}, site={$site}, contest={$contest}, user={$user})", 1);
        return false;
    }
    $temp = DBRow($r, 0);
    if (DB_lo_unlink($c, $temp['oid']) === false) {
        DBExec($c, "rollback work", "DBBkpDelete(rollback-import)");
        LOGError("Unable to delete a large object (user={$user}, contest={$contest}, site={$site}, number={$number}).");
        MSGError("problem deleting bkp from database. Contact an admin.");
        exit;
    }
    if (strlen($adm) > 0) {
        $str = "deleted by " . $adm;
    } else {
        $str = "deleted";
    }
    DBExec($c, "update bkptable set bkpstatus='{$str}', updatetime=" . time() . " where contestnumber={$contest} and bkpnumber={$number} and sitenumber={$site}", "DBBkpDelete(update bkp)");
    DBExec($c, "commit work", "DBBkpDelete(commit)");
    LOGLevel("Bkp deleted (bkp={$number}, site={$site}, contest={$contest}, user={$user}).", 3);
    return true;
}
示例#2
0
文件: frun.php 项目: sbaldrich/boca
function DBNewRun($param, $c = null)
{
    if (isset($param['contestnumber']) && !isset($param['contest'])) {
        $param['contest'] = $param['contestnumber'];
    }
    if (isset($param['sitenumber']) && !isset($param['site'])) {
        $param['site'] = $param['sitenumber'];
    }
    if (isset($param['usernumber']) && !isset($param['user'])) {
        $param['user'] = $param['usernumber'];
    }
    if (isset($param['number']) && !isset($param['runnumber'])) {
        $param['runnumber'] = $param['number'];
    }
    if (isset($param['runlangnumber']) && !isset($param['lang'])) {
        $param['lang'] = $param['runlangnumber'];
    }
    if (isset($param['runproblem']) && !isset($param['problem'])) {
        $param['problem'] = $param['runproblem'];
    }
    $ac = array('contest', 'site', 'user', 'problem', 'lang', 'filename', 'filepath');
    $ac1 = array('runnumber', 'rundate', 'rundatediff', 'rundatediffans', 'runanswer', 'runstatus', 'runjudge', 'runjudgesite', 'runjudge1', 'runjudgesite1', 'runanswer1', 'runjudge2', 'runjudgesite2', 'runanswer2', 'autoip', 'autobegindate', 'autoenddate', 'autoanswer', 'autostdout', 'autostderr', 'updatetime');
    $type['contest'] = 1;
    $type['autobegindate'] = 1;
    $type['autoenddate'] = 1;
    $type['problem'] = 1;
    $type['updatetime'] = 1;
    $type['site'] = 1;
    $type['user'] = 1;
    $type['runnumber'] = 1;
    $type['rundatediffans'] = 1;
    $type['rundatediff'] = 1;
    $type['rundate'] = 1;
    $type['runanswer'] = 1;
    $type['runjudge'] = 1;
    $type['runjudgesite'] = 1;
    $type['runjudge1'] = 1;
    $type['runjudgesite1'] = 1;
    $type['runanswer1'] = 1;
    $type['runjudge2'] = 1;
    $type['runjudgesite2'] = 1;
    $type['runanswer2'] = 1;
    foreach ($ac as $key) {
        if (!isset($param[$key]) || $param[$key] == "") {
            MSGError("DBNewRun param error: {$key} not found");
            return false;
        }
        if (isset($type[$key]) && !is_numeric($param[$key])) {
            MSGError("DBNewRun param error: {$key} is not numeric");
            return false;
        }
        ${$key} = sanitizeText($param[$key]);
    }
    $t = time();
    $autoip = '';
    $autobegindate = 'NULL';
    $autoenddate = 'NULL';
    $autoanswer = '';
    $autostdout = '';
    $autostderr = '';
    $runjudge = 'NULL';
    $runjudgesite = 'NULL';
    $runjudge1 = 'NULL';
    $runjudgesite1 = 'NULL';
    $runanswer1 = 0;
    $runjudge2 = 'NULL';
    $runjudgesite2 = 'NULL';
    $runanswer2 = 0;
    $runnumber = -1;
    $updatetime = -1;
    $rundatediff = -1;
    $rundate = $t;
    $runanswer = 0;
    $rundatediffans = 999999999;
    $runstatus = 'openrun';
    foreach ($ac1 as $key) {
        if (isset($param[$key])) {
            ${$key} = sanitizeText($param[$key]);
            if (isset($type[$key]) && !is_numeric($param[$key])) {
                MSGError("DBNewRun param error: {$key} is not numeric");
                return false;
            }
        }
    }
    if ($updatetime < 0) {
        $updatetime = $t;
    }
    $cw = false;
    if ($c == null) {
        $cw = true;
        $c = DBConnect();
        DBExec($c, "begin work", "DBNewRun(transaction)");
    }
    $insert = true;
    $oid1 = '';
    $oid2 = '';
    $oldold1 = '';
    $oldold2 = '';
    $sql = "select sitenextrun as nextrun from " . "sitetable where sitenumber={$site} and contestnumber={$contest} for update";
    $r = DBExec($c, $sql, "DBNewRun(get site for update)");
    if (DBnlines($r) != 1) {
        DBExec($c, "rollback work", "DBNewRun(rollback-site)");
        LOGError("Unable to find a unique site/contest in the database. SQL=(" . $sql . ")");
        MSGError("Unable to find a unique site/contest in the database.");
        return false;
    }
    $a = DBRow($r, 0);
    $n = $a["nextrun"] + 1;
    if ($runnumber > 0) {
        $sql = "select * from runtable as t where t.contestnumber={$contest} and " . "t.runsitenumber={$site} and t.runnumber={$runnumber}";
        $r = DBExec($c, $sql . " for update", "DBNewRun(get run for update)");
        $n = DBnlines($r);
        if ($n > 0) {
            $insert = false;
            $lr = DBRow($r, 0);
            $t = $lr['updatetime'];
            if (isset($lr['autostdout'])) {
                $oid1 = $lr['autostdout'];
            }
            if (isset($lr['autostderr'])) {
                $oid2 = $lr['autostderr'];
            }
        }
        $n = $runnumber;
    } else {
        $runnumber = $n;
    }
    if ($rundatediff < 0) {
        $b = DBSiteInfo($contest, $site, $c);
        $dif = $b["currenttime"];
        $rundatediff = $dif;
        if ($dif < 0) {
            if (!isset($param['allowneg'])) {
                DBExec($c, "rollback work", "DBNewRun(rollback-started)");
                LOGError("Tried to submit a run but the contest is not started. SQL=(" . $sql . ")");
                MSGError("The contest is not started yet!");
                return 0;
            }
        }
        if (!$b["siterunning"]) {
            DBExec($c, "rollback work", "DBNewRun(rollback-over)");
            LOGError("Tried to submit a run but the contest is over. SQL=(" . $sql . ")");
            MSGError("The contest is over!");
            return 0;
        }
    } else {
        $dif = $rundatediff;
    }
    if ($updatetime > $t || $insert) {
        DBExec($c, "update sitetable set sitenextrun={$runnumber}, updatetime=" . $t . " where sitenumber={$site} and contestnumber={$contest} and sitenextrun<{$runnumber}", "DBNewRun(update site)");
        //	LOGError($autostdout);
        if (substr($autostdout, 0, 7) == "base64:") {
            $autostdout = base64_decode(substr($autostdout, 7));
            $oldoid1 = $oid1;
            if (($oid1 = DB_lo_import_text($c, $autostdout)) == null) {
                DBExec($c, "rollback work", "DBNewRun(rollback-import stdout)");
                LOGError("Unable to create a large object for file stdout (run={$runnumber},site={$site},contest={$contest}).");
                MSGError("problem importing stdout to database. Contact an admin now!");
                return false;
            }
        } else {
            if ($autostdout != '') {
                DBExec($c, "rollback work", "DBNewRun(rollback-import stderr)");
                LOGError("Unable to create a large object for file stdout that is not BASE64 (run={$runnumber},site={$site},contest={$contest}).");
                MSGError("problem importing stdout (not BASE64) to database. Contact an admin now!");
                return false;
            }
            $oid1 = 'NULL';
        }
        if (substr($autostderr, 0, 7) == "base64:") {
            //		LOGError($autostderr);
            $autostderr = base64_decode(substr($autostderr, 7));
            $oldoid2 = $oid2;
            if (($oid2 = DB_lo_import_text($c, $autostderr)) == null) {
                DBExec($c, "rollback work", "DBNewRun(rollback-import stderr)");
                LOGError("Unable to create a large object for file stderr (run={$runnumber},site={$site},contest={$contest}).");
                MSGError("problem importing stderr to database. Contact an admin now!");
                return false;
            }
        } else {
            if ($autostderr != '') {
                DBExec($c, "rollback work", "DBNewRun(rollback-import stderr)");
                LOGError("Unable to create a large object for file stderr that is not BASE64 (run={$runnumber},site={$site},contest={$contest}).");
                MSGError("problem importing stderr (not BASE64) to database. Contact an admin now!");
                return false;
            }
            $oid2 = 'NULL';
        }
    }
    $ret = 1;
    if ($insert) {
        if (substr($filepath, 0, 7) != "base64:") {
            if (($oid = DB_lo_import($c, $filepath)) === false) {
                DBExec($c, "rollback work", "DBNewRun(rollback-import)");
                LOGError("DBNewRun: Unable to create a large object for file {$filepath}.");
                MSGError("problem importing file {$filepath} to database. Contact an admin now!");
                return false;
            }
        } else {
            $filepath = base64_decode(substr($filepath, 7));
            if (($oid = DB_lo_import_text($c, $filepath)) == null) {
                DBExec($c, "rollback work", "DBNewRun(rollback-import)");
                LOGError("DBNewRun: Unable to create a large object for file.");
                MSGError("problem importing file to database. Contact an admin now!");
                return false;
            }
        }
        DBExec($c, "INSERT INTO runtable (contestnumber, runsitenumber, runnumber, usernumber, rundate, " . "rundatediff, rundatediffans, runproblem, runfilename, rundata, runanswer, runstatus, runlangnumber, " . "runjudge, runjudgesite, runanswer1, runjudge1, runjudgesite1, runanswer2, runjudge2, runjudgesite2, " . "autoip, autobegindate, autoenddate, autoanswer, autostdout, autostderr, updatetime) " . "VALUES ({$contest}, {$site}, {$n}, {$user}, {$rundate}, {$rundatediff}, {$rundatediffans}, {$problem}, '{$filename}', {$oid}, {$runanswer}, " . "'{$runstatus}', {$lang}, {$runjudge}, {$runjudgesite}, {$runanswer1}, {$runjudge1}, {$runjudgesite1}, {$runanswer2}, {$runjudge2}, " . "{$runjudgesite2}, '{$autoip}', {$autobegindate}, {$autoenddate}, '{$autoanswer}', {$oid1}, {$oid2}, {$updatetime})", "DBNewRun(insert run)");
        if ($cw) {
            DBExec($c, "commit work", "DBNewRun(commit)");
            LOGLevel("User {$user} submitted a run (#{$n}) on site #{$site} " . "(problem={$problem},filename={$filename},lang={$lang},contest={$contest},date={$t},datedif={$dif},oid={$oid}).", 2);
        }
        $ret = 2;
    } else {
        if ($updatetime > $t) {
            $ret = 2;
            DBExec($c, "update runtable set rundate={$rundate}, rundatediff={$rundatediff}, " . "rundatediffans={$rundatediffans}, runanswer={$runanswer}, runanswer1={$runanswer1}, runanswer2={$runanswer2}, runstatus='{$runstatus}', " . "runjudge1={$runjudge1}, runjudgesite1={$runjudgesite1}, runjudge2={$runjudge2}, runjudgesite2={$runjudgesite2}, " . "runjudge={$runjudge}, runjudgesite={$runjudgesite}, updatetime={$updatetime}, " . "autoip='{$autoip}', autobegindate={$autobegindate}, autoenddate={$autoenddate}, autoanswer='{$autoanswer}', " . "autostdout={$oid1}, autostderr={$oid2} " . "where runnumber={$runnumber} and contestnumber={$contest} and runsitenumber={$site}", "DBNewRun(update run)");
            if (is_numeric($oldoid1)) {
                DB_lo_unlink($c, $oldoid1);
            }
            if (is_numeric($oldoid2)) {
                DB_lo_unlink($c, $oldoid2);
            }
        }
        if ($cw) {
            DBExec($c, "commit work", "DBNewRun(commit-update)");
        }
    }
    return $ret;
    /* // isso gera problemas de portabilidade e de seguranca (quando outros usuarios tambem tem shell
       // no servidor e podem construir paginas web. Eles podem usar essas paginas para acessar esses arquivos,
       // pois os mesmos ficaram com dono apache/www-data/etc)
       umask(0077);
       @mkdir("/tmp/boca");
       if (!move_uploaded_file ($filepath,
       "/tmp/boca/contest${contest}.site${site}.run${n}.user${user}.problem${problem}.time${t}.${filename}"))
       LOGLevel("Run not saved as file (run=$n,site=$site,contest=$contest", 1);
    */
}
示例#3
0
function DBSiteDeleteAllBkps($contest, $site, $user, $usersite, $c = null)
{
    $cw = false;
    if ($c == null) {
        $cw = true;
        $c = DBConnect();
        DBExec($c, "begin work");
    }
    DBExec($c, "lock table bkptable");
    $r = DBExec($c, "select bkpdata from bkptable where contestnumber={$contest} and sitenumber={$site} and bkpstatus='active'");
    $n = DBnlines($r);
    for ($i = 0; $i < $n; $i++) {
        $a = DBRow($r, $i);
        DB_lo_unlink($c, $a["bkpdata"]);
    }
    DBExec($c, "delete from bkptable where contestnumber={$contest} and sitenumber={$site}");
    if ($cw) {
        DBExec($c, "commit work");
        LOGLevel("All Bkps deleted (site={$site}, contest={$contest}, user={$user}(site={$usersite})).", 3);
    }
    return true;
}
示例#4
0
function GetExternalData($contest)
{
    $c = DBConnect();
    if ($c == null) {
        return;
    }
    $r = DBExec($c, "select * from contesttable where contestnumber={$contest}");
    if (DBnLines($r) == 0) {
        echo "Unable to find the contest {$contest} in the database.\n";
        exit;
    }
    $ct = DBRow($r, 0);
    $localsite = $ct["contestlocalsite"];
    $mainsite = $ct["contestmainsite"];
    if ($mainsite != $localsite) {
        $r = DBExec($c, "select * from sitetable where sitenumber=" . $ct["contestmainsite"] . " and contestnumber={$contest}");
        if (DBnLines($r) == 0) {
            echo "Unable to find the main site in the database (site={$site}, contest={$contest}).\n";
            exit;
        }
        $st = DBRow($r, 0);
        if (($t = DBExtConnect($c, $contest, $st["sitenumber"])) != null) {
            DBExec($c, "begin work");
            // sincronizando contest
            $r = DBExec($t, "select * from contesttable where contestnumber={$contest}");
            $n = DBnLines($r);
            $atual = DBRow($r, 0);
            if ($atual["updatetime"] == "") {
                $atual["updatetime"] = time();
            }
            DBExec($c, "update contesttable set " . "contestduration=" . $atual["contestduration"] . "," . "conteststartdate=" . $atual["conteststartdate"] . "," . "contestmaxfilesize=" . $atual["contestmaxfilesize"] . "," . "contestactive='" . $atual["contestactive"] . "'," . "contestname='" . escape_string($atual["contestname"]) . "'," . "contestlastmileanswer=" . $atual["contestlastmileanswer"] . "," . "contestlastmilescore=" . $atual["contestlastmilescore"] . "," . "contestpenalty=" . $atual["contestpenalty"] . "," . "updatetime=" . $atual["updatetime"] . " where contestnumber={$contest} and updatetime<" . $atual["updatetime"]);
            // sincronizando answers
            $r = DBExec($t, "select * from answertable where contestnumber={$contest}");
            $rr = DBExec($c, "select * from answertable where contestnumber={$contest} for update");
            $n = DBnLines($r);
            $nn = DBnLines($rr);
            echo "answers: external(site={$mainsite}, reading={$mainsite})={$n}, local={$nn}\n";
            for ($k = 0; $k < $nn; $k++) {
                $needed[$k] = false;
            }
            for ($j = 0; $j < $n; $j++) {
                $la = DBRow($r, $j);
                for ($k = 0; $k < $nn; $k++) {
                    $aqui = DBRow($rr, $k);
                    if ($la["answernumber"] == $aqui["answernumber"] && $la["contestnumber"] == $aqui["contestnumber"]) {
                        $needed[$k] = true;
                        if ($la["updatetime"] > $aqui["updatetime"]) {
                            $situacao[$j] = "atualizar";
                        } else {
                            $situacao[$j] = "ok";
                        }
                        break;
                    }
                }
                if ($k >= $nn) {
                    $situacao[$j] = "inserir";
                }
            }
            $del = 0;
            $upd = 0;
            $ins = 0;
            for ($k = 0; $k < $nn; $k++) {
                $atual = DBRow($rr, $k);
                if (!$needed[$k]) {
                    DBExec($c, "delete from answertable where contestnumber={$contest} and answernumber=" . $atual["answernumber"]);
                    $del++;
                }
            }
            for ($j = 0; $j < $n; $j++) {
                $atual = DBRow($r, $j);
                if ($atual["updatetime"] == "") {
                    $atual["updatetime"] = time();
                }
                if ($situacao[$j] == "atualizar") {
                    $upd++;
                    DBExec($c, "update answertable set " . "runanswer='" . $atual["runanswer"] . "'," . "yes='" . $atual["yes"] . "'," . "fake='" . $atual["fake"] . "'," . "updatetime=" . $atual["updatetime"] . " where contestnumber={$contest} and answernumber=" . $atual["answernumber"] . " and updatetime<" . $atual["updatetime"]);
                } else {
                    if ($situacao[$j] == "inserir") {
                        $ins++;
                        DBExec($c, "insert into answertable (contestnumber, answernumber, runanswer, yes, fake, updatetime) values (" . $contest . "," . $atual["answernumber"] . ",'" . $atual["runanswer"] . "','" . $atual["yes"] . "'," . "'" . $atual["fake"] . "'," . $atual["updatetime"] . ")");
                    }
                }
            }
            echo "   deletions={$del}, updates={$upd}, insertions={$ins}\n\n";
            // sincronizando languages
            $r = DBExec($t, "select * from langtable where contestnumber={$contest}");
            $rr = DBExec($c, "select * from langtable where contestnumber={$contest} for update");
            $n = DBnLines($r);
            $nn = DBnLines($rr);
            echo "languages: external(site={$mainsite}, reading={$mainsite})={$n}, local={$nn}\n";
            for ($k = 0; $k < $nn; $k++) {
                $needed[$k] = false;
            }
            for ($j = 0; $j < $n; $j++) {
                $la = DBRow($r, $j);
                for ($k = 0; $k < $nn; $k++) {
                    $aqui = DBRow($rr, $k);
                    if ($la["langnumber"] == $aqui["langnumber"] && $la["contestnumber"] == $aqui["contestnumber"]) {
                        $needed[$k] = true;
                        if ($la["updatetime"] > $aqui["updatetime"]) {
                            $situacao[$j] = "atualizar";
                        } else {
                            $situacao[$j] = "ok";
                        }
                        break;
                    }
                }
                if ($k >= $nn) {
                    $situacao[$j] = "inserir";
                }
            }
            $del = 0;
            $upd = 0;
            $ins = 0;
            for ($k = 0; $k < $nn; $k++) {
                $atual = DBRow($rr, $k);
                if (!$needed[$k]) {
                    DBExec($c, "delete from langtable where contestnumber={$contest} and langnumber=" . $atual["langnumber"]);
                    $del++;
                }
            }
            for ($j = 0; $j < $n; $j++) {
                $atual = DBRow($r, $j);
                if ($atual["updatetime"] == "") {
                    $atual["updatetime"] = time();
                }
                if ($situacao[$j] == "atualizar") {
                    $upd++;
                    DBExec($c, "update langtable set " . "langname='" . escape_string($atual["langname"]) . "'," . "updatetime=" . $atual["updatetime"] . " where contestnumber={$contest} and langnumber=" . $atual["langnumber"] . " and updatetime<" . $atual["updatetime"]);
                } else {
                    if ($situacao[$j] == "inserir") {
                        $ins++;
                        DBExec($c, "insert into langtable (contestnumber, langnumber, langname, updatetime) values ({$contest}," . $atual["langnumber"] . ",'" . escape_string($atual["langname"]) . "'," . $atual["updatetime"] . ")");
                    }
                }
            }
            echo "   deletions={$del}, updates={$upd}, insertions={$ins}\n\n";
            // sincronizando problems
            $r = DBExec($t, "select * from problemtable where contestnumber={$contest}");
            $rr = DBExec($c, "select * from problemtable where contestnumber={$contest} for update");
            $n = DBnLines($r);
            $nn = DBnLines($rr);
            echo "problems: external(site={$mainsite}, reading={$mainsite})={$n}, local={$nn}\n";
            for ($k = 0; $k < $nn; $k++) {
                $needed[$k] = false;
            }
            for ($j = 0; $j < $n; $j++) {
                $la = DBRow($r, $j);
                for ($k = 0; $k < $nn; $k++) {
                    $aqui = DBRow($rr, $k);
                    if ($la["problemnumber"] == $aqui["problemnumber"] && $la["contestnumber"] == $aqui["contestnumber"]) {
                        $needed[$k] = true;
                        if ($la["updatetime"] > $aqui["updatetime"]) {
                            $situacao[$j] = "atualizar";
                        } else {
                            $situacao[$j] = "ok";
                        }
                        break;
                    }
                }
                if ($k >= $nn) {
                    $situacao[$j] = "inserir";
                }
            }
            $del = 0;
            $upd = 0;
            $ins = 0;
            for ($k = 0; $k < $nn; $k++) {
                $atual = DBRow($rr, $k);
                if (!$needed[$k]) {
                    DBExec($c, "delete from problemtable where contestnumber={$contest} and problemnumber=" . $atual["problemnumber"]);
                    $del++;
                }
            }
            for ($j = 0; $j < $n; $j++) {
                $atual = DBRow($r, $j);
                if ($atual["updatetime"] == "") {
                    $atual["updatetime"] = time();
                }
                if ($situacao[$j] == "inserir") {
                    $ins++;
                    $upd--;
                    DBExec($c, "insert into problemtable (contestnumber, problemnumber, problemname, updatetime) values ({$contest}," . $atual["problemnumber"] . ",'" . escape_string($atual["problemname"]) . "', 0)");
                }
                if ($situacao[$j] == "atualizar" || $situacao[$j] == "inserir") {
                    $upd++;
                    $p = DBRow($r2, 0);
                    if ($p["probleminputfile"] > 0) {
                        DB_lo_unlink($c, $p["probleminputfile"]);
                    }
                    if ($p["problemsolfile"] > 0) {
                        DB_lo_unlink($c, $p["problemsolfile"]);
                    }
                    DBExec($t, "begin work");
                    if ($atual["probleminputfile"] == "") {
                        $inputfile = "null";
                    } else {
                        $in = DB_lo_open($t, $atual["probleminputfile"], "r");
                        if (!$in) {
                            $inputfile = "null";
                        } else {
                            $inputfile = DB_lo_create($c);
                            $out = DB_lo_open($c, $inputfile, "w");
                            while (($buf = DB_lo_read($in, 100000)) != false) {
                                DB_lo_write($out, $buf);
                            }
                            DB_lo_close($out);
                            DB_lo_close($in);
                        }
                    }
                    if ($atual["problemsolfile"] == "") {
                        $solfile = "null";
                    } else {
                        $in = DB_lo_open($t, $atual["problemsolfile"], "r");
                        if (!$in) {
                            $solfile = "null";
                        } else {
                            $solfile = DB_lo_create($c);
                            $out = DB_lo_open($c, $solfile, "w");
                            while (($buf = DB_lo_read($in, 100000)) != false) {
                                DB_lo_write($out, $buf);
                            }
                            DB_lo_close($out);
                            DB_lo_close($in);
                        }
                    }
                    DBExec($t, "commit work");
                    DBExec($c, "update problemtable set " . "problemname='" . escape_string($atual["problemname"]) . "'," . "problemfullname='" . escape_string($atual["problemfullname"]) . "'," . "problembasefilename='" . escape_string($atual["problembasefilename"]) . "'," . "probleminputfilename='" . escape_string($atual["probleminputfilename"]) . "'," . "problemsolfilename='" . escape_string($atual["problemsolfilename"]) . "'," . "fake='" . $atual["fake"] . "'," . "probleminputfile=" . $inputfile . "," . "problemsolfile=" . $solfile . "," . "updatetime=" . $atual["updatetime"] . " where contestnumber={$contest} and problemnumber=" . $atual["problemnumber"] . " and updatetime<" . $atual["updatetime"]);
                }
            }
            echo "   deletions={$del}, updates={$upd}, insertions={$ins}\n\n";
            DBExec($c, "commit work");
        }
    }
    $r = DBExec($c, "select * from sitetable where contestnumber={$contest}");
    $n = DBnlines($r);
    if ($n == 0) {
        echo "Unable to find sites in the database.\n";
        exit;
    }
    $st = array();
    for ($i = 0; $i < $n; $i++) {
        $st[$i] = DBRow($r, $i);
    }
    for ($i = 0; $i < count($st); $i++) {
        $site = $st[$i]["sitenumber"];
        if ($site == $ct["contestlocalsite"]) {
            continue;
        }
        if (($t = DBExtConnect($c, $contest, $site)) == null) {
            continue;
        }
        DBExec($c, "begin work");
        // sincronizando site
        $r = DBExec($t, "select * from sitetable where contestnumber={$contest} and sitenumber={$site}");
        $rr = DBExec($c, "select * from sitetable where contestnumber={$contest} and sitenumber={$site} for update");
        $n = DBnLines($r);
        $nn = DBnLines($rr);
        $atual = DBRow($r, 0);
        $p = DBRow($rr, 0);
        //		if ($p["updatetime"]<$atual["updatetime"]) {
        if ($atual["sitestartdate"] == "") {
            $atual["sitestartdate"] = "null";
        }
        if ($atual["sitelastmilescore"] == "") {
            $atual["sitelastmilescore"] = "null";
        }
        if ($atual["sitelastmileanswer"] == "") {
            $atual["sitelastmileanswer"] = "null";
        }
        if ($atual["siteenddate"] == "") {
            $atual["siteenddate"] = "null";
        }
        if ($atual["siteendeddate"] == "") {
            $atual["siteendeddate"] = "null";
        }
        if ($atual["siteautoend"] != "t") {
            $atual["siteautoend"] = "f";
        }
        if ($atual["updatetime"] == "") {
            $atual["updatetime"] = time();
        }
        DBExec($c, "update sitetable set " . "sitename='" . escape_string($atual["sitename"]) . "'," . "siteactive='" . escape_string($atual["siteactive"]) . "'," . "sitepermitlogins='" . escape_string($atual["sitepermitlogins"]) . "'," . "sitestartdate=" . $atual["sitestartdate"] . "," . "sitelastmilescore=" . $atual["sitelastmilescore"] . "," . "sitelastmileanswer=" . $atual["sitelastmileanswer"] . "," . "siteenddate=" . $atual["siteenddate"] . "," . "siteendeddate=" . $atual["siteendeddate"] . "," . "siteautoend='" . escape_string($atual["siteautoend"]) . "'," . "sitejudging='" . escape_string($atual["sitejudging"]) . "'," . "siteglobalscore='" . escape_string($atual["siteglobalscore"]) . "'," . "sitescorelevel=" . $atual["sitescorelevel"] . "," . "sitenextuser="******"sitenextuser"] . "," . "sitenextclar=" . $atual["sitenextclar"] . "," . "sitenextrun=" . $atual["sitenextrun"] . "," . "updatetime=" . $atual["updatetime"] . " where contestnumber={$contest} and sitenumber=" . $atual["sitenumber"]);
        //				." and updatetime<".$atual["updatetime"]);
        //		}
        // sincronizando users
        $r = DBExec($t, "select * from usertable where contestnumber={$contest} and usersitenumber={$site}");
        $rr = DBExec($c, "select * from usertable where contestnumber={$contest} and usersitenumber={$site}");
        $n = DBnLines($r);
        $nn = DBnLines($rr);
        echo "users: external(site={$site}, reading={$site})={$n}, local={$nn}\n";
        for ($k = 0; $k < $nn; $k++) {
            $needed[$k] = false;
        }
        for ($j = 0; $j < $n; $j++) {
            $la = DBRow($r, $j);
            for ($k = 0; $k < $nn; $k++) {
                $aqui = DBRow($rr, $k);
                if ($la["usernumber"] == $aqui["usernumber"] && $la["contestnumber"] == $aqui["contestnumber"] && $la["usersitenumber"] == $aqui["usersitenumber"]) {
                    $needed[$k] = true;
                    if ($la["updatetime"] > $aqui["updatetime"]) {
                        $situacao[$j] = "atualizar";
                    } else {
                        $situacao[$j] = "ok";
                    }
                    break;
                }
            }
            if ($k >= $nn) {
                $situacao[$j] = "inserir";
            }
        }
        $del = 0;
        for ($k = 0; $k < $nn; $k++) {
            $atual = DBRow($rr, $k);
            if (!$needed[$k]) {
                DBExec($c, "delete from usertable where contestnumber={$contest} and usersitenumber={$site} and usernumber=" . $atual["usernumber"]);
                $del++;
            }
        }
        $upd = 0;
        $ins = 0;
        for ($j = 0; $j < $n; $j++) {
            $atual = DBRow($r, $j);
            if ($atual["userlastlogin"] == "") {
                $atual["userlastlogin"] = "******";
            }
            if ($atual["userlastlogout"] == "") {
                $atual["userlastlogout"] = "null";
            }
            if ($atual["updatetime"] == "") {
                $atual["updatetime"] = time();
            }
            if ($situacao[$j] == "atualizar") {
                DBExec($c, "update usertable set " . "username='******'," . "userfullname='" . escape_string($atual["userfullname"]) . "'," . "userdesc='" . escape_string($atual["userdesc"]) . "'," . "usertype='" . escape_string($atual["usertype"]) . "'," . "userenabled='" . $atual["userenabled"] . "'," . "usermultilogin='******'," . "userpassword='******'," . "userip='" . $atual["userip"] . "'," . "userlastlogin="******"userlastlogin"] . "," . "userlastlogout=" . $atual["userlastlogout"] . "," . "usersession='" . $atual["usersession"] . "'," . "userpermitip='" . $atual["userpermitip"] . "'," . "updatetime=" . $atual["updatetime"] . " where contestnumber={$contest} and usersitenumber=" . $atual["usersitenumber"] . " and usernumber=" . $atual["usernumber"] . " and updatetime<" . $atual["updatetime"]);
                $upd++;
            } else {
                if ($situacao[$j] == "inserir") {
                    DBExec($c, "insert into usertable (username, userfullname, userdesc, usertype, userenabled, usermultilogin, " . "userpassword, userip, userlastlogin, userlastlogout, usersession, userpermitip, updatetime, " . "contestnumber, usersitenumber, usernumber) values (" . "'" . escape_string($atual["username"]) . "'," . "'" . escape_string($atual["userfullname"]) . "'," . "'" . escape_string($atual["userdesc"]) . "'," . "'" . $atual["usertype"] . "'," . "'" . $atual["userenabled"] . "'," . "'" . $atual["usermultilogin"] . "'," . "'" . $atual["userpassword"] . "'," . "'" . $atual["userip"] . "'," . $atual["userlastlogin"] . "," . $atual["userlastlogout"] . "," . "'" . $atual["usersession"] . "'," . "'" . $atual["userpermitip"] . "'," . $atual["updatetime"] . ",{$contest}," . $atual["usersitenumber"] . "," . $atual["usernumber"] . ")");
                    $ins++;
                }
            }
        }
        echo "   deletions={$del}, updates={$upd}, insertions={$ins}\n\n";
        // sincronizando clars
        $r = DBExec($t, "select * from clartable where contestnumber={$contest} and clarsitenumber={$site}");
        $rr = DBExec($c, "select * from clartable where contestnumber={$contest} and clarsitenumber={$site}");
        $n = DBnLines($r);
        $nn = DBnLines($rr);
        echo "clars: external(site={$site}, reading={$site})={$n}, local={$nn}\n";
        for ($k = 0; $k < $nn; $k++) {
            $needed[$k] = false;
        }
        for ($j = 0; $j < $n; $j++) {
            $la = DBRow($r, $j);
            for ($k = 0; $k < $nn; $k++) {
                $aqui = DBRow($rr, $k);
                if ($la["clarnumber"] == $aqui["clarnumber"] && $la["contestnumber"] == $aqui["contestnumber"] && $la["clarsitenumber"] == $aqui["clarsitenumber"]) {
                    $needed[$k] = true;
                    if ($la["updatetime"] > $aqui["updatetime"]) {
                        $situacao[$j] = "atualizar";
                    } else {
                        $situacao[$j] = "ok";
                    }
                    break;
                }
            }
            if ($k >= $nn) {
                $situacao[$j] = "inserir";
            }
        }
        $del = 0;
        for ($k = 0; $k < $nn; $k++) {
            $atual = DBRow($rr, $k);
            if (!$needed[$k]) {
                DBExec($c, "delete from clartable where contestnumber={$contest} and clarsitenumber={$site} and clarnumber=" . $atual["clarnumber"]);
                $del++;
            }
        }
        $upd = 0;
        $ins = 0;
        for ($j = 0; $j < $n; $j++) {
            $atual = DBRow($r, $j);
            if ($atual["claranswer"] == "") {
                $atual["claranswer"] = "null";
            } else {
                $atual["claranswer"] = "'" . escape_string($atual["claranswer"]) . "'";
            }
            if ($atual["clarjudge"] == "") {
                $atual["clarjudge"] = "null";
            }
            if ($atual["clarjudgesite"] == "") {
                $atual["clarjudgesite"] = "null";
            }
            if ($atual["updatetime"] == "") {
                $atual["updatetime"] = time();
            }
            if ($situacao[$j] == "atualizar") {
                DBExec($c, "update clartable set " . "usernumber=" . $atual["usernumber"] . "," . "clardate=" . $atual["clardate"] . "," . "clardatediff=" . $atual["clardatediff"] . "," . "clardatediffans=" . $atual["clardatediffans"] . "," . "clarproblem=" . $atual["clarproblem"] . "," . "clardata='" . escape_string($atual["clardata"]) . "'," . "claranswer=" . $atual["claranswer"] . "," . "clarstatus='" . escape_string($atual["clarstatus"]) . "'," . "clarjudge=" . $atual["clarjudge"] . "," . "clarjudgesite=" . $atual["clarjudgesite"] . "," . "updatetime=" . $atual["updatetime"] . " where contestnumber={$contest} and clarsitenumber=" . $atual["clarsitenumber"] . " and clarnumber=" . $atual["clarnumber"] . " and updatetime<" . $atual["updatetime"]);
                $upd++;
            } else {
                if ($situacao[$j] == "inserir") {
                    DBExec($c, "insert into clartable (contestnumber, clarsitenumber, clarnumber, usernumber, clardate," . "clardatediff, clardatediffans, clarproblem, clardata, claranswer, clarstatus, clarjudge, clarjudgesite, updatetime) " . "values (" . $contest . "," . $atual["clarsitenumber"] . "," . $atual["clarnumber"] . "," . $atual["usernumber"] . "," . $atual["clardate"] . "," . $atual["clardatediff"] . "," . $atual["clardatediffans"] . "," . $atual["clarproblem"] . "," . "'" . escape_string($atual["clardata"]) . "'," . $atual["claranswer"] . "," . "'" . escape_string($atual["clarstatus"]) . "'," . $atual["clarjudge"] . "," . $atual["clarjudgesite"] . "," . $atual["updatetime"] . ")");
                    $ins++;
                }
            }
        }
        echo "   deletions={$del}, updates={$upd}, insertions={$ins}\n\n";
        // sincronizando clars
        $r = DBExec($t, "select * from clartable where contestnumber={$contest} and clarsitenumber={$localsite}");
        $rr = DBExec($c, "select * from clartable where contestnumber={$contest} and clarsitenumber={$localsite}");
        $n = DBnLines($r);
        $nn = DBnLines($rr);
        echo "clars: external(site={$site}, reading={$localsite})={$n}, local={$nn}\n";
        for ($k = 0; $k < $nn; $k++) {
            $needed[$k] = false;
        }
        for ($j = 0; $j < $n; $j++) {
            $la = DBRow($r, $j);
            for ($k = 0; $k < $nn; $k++) {
                $aqui = DBRow($rr, $k);
                if ($la["clarnumber"] == $aqui["clarnumber"] && $la["contestnumber"] == $aqui["contestnumber"] && $la["clarsitenumber"] == $aqui["clarsitenumber"]) {
                    $needed[$k] = true;
                    if ($la["updatetime"] > $aqui["updatetime"]) {
                        $situacao[$j] = "atualizar";
                    } else {
                        $situacao[$j] = "ok";
                    }
                    break;
                }
            }
            if ($k >= $nn) {
                $situacao[$j] = "inserir";
            }
        }
        $upd = 0;
        $ins = 0;
        for ($j = 0; $j < $n; $j++) {
            $atual = DBRow($r, $j);
            if ($atual["claranswer"] == "") {
                $atual["claranswer"] = "null";
            } else {
                $atual["claranswer"] = "'" . escape_string($atual["claranswer"]) . "'";
            }
            if ($atual["clarjudge"] == "") {
                $atual["clarjudge"] = "null";
            }
            if ($atual["clarjudgesite"] == "") {
                $atual["clarjudgesite"] = "null";
            }
            if ($atual["updatetime"] == "") {
                $atual["updatetime"] = time();
            }
            if ($situacao[$j] == "atualizar") {
                DBExec($c, "update clartable set " . "usernumber=" . $atual["usernumber"] . "," . "clardate=" . $atual["clardate"] . "," . "clardatediff=" . $atual["clardatediff"] . "," . "clardatediffans=" . $atual["clardatediffans"] . "," . "clarproblem=" . $atual["clarproblem"] . "," . "clardata='" . escape_string($atual["clardata"]) . "'," . "claranswer=" . $atual["claranswer"] . "," . "clarstatus='" . escape_string($atual["clarstatus"]) . "'," . "clarjudge=" . $atual["clarjudge"] . "," . "clarjudgesite=" . $atual["clarjudgesite"] . "," . "updatetime=" . $atual["updatetime"] . " where contestnumber={$contest} and clarsitenumber=" . $atual["clarsitenumber"] . " and clarnumber=" . $atual["clarnumber"] . " and updatetime<" . $atual["updatetime"]);
                $upd++;
            } else {
                if ($situacao[$j] == "inserir") {
                    $ins++;
                    echo "Clar inserts shouldn't exist (clarnumber=" . $atual["clarnumber"] . ")...\n";
                }
            }
        }
        echo "   updates={$upd}, insertions={$ins}\n\n";
        // sincronizando runs
        $r = DBExec($t, "select * from runtable where contestnumber={$contest} and runsitenumber={$site}");
        $rr = DBExec($c, "select * from runtable where contestnumber={$contest} and runsitenumber={$site}");
        $n = DBnLines($r);
        $nn = DBnLines($rr);
        echo "runs: external(site={$site}, reading={$site})={$n}, local={$nn}\n";
        for ($k = 0; $k < $nn; $k++) {
            $needed[$k] = false;
        }
        for ($j = 0; $j < $n; $j++) {
            $la = DBRow($r, $j);
            for ($k = 0; $k < $nn; $k++) {
                $aqui = DBRow($rr, $k);
                if ($la["runnumber"] == $aqui["runnumber"] && $la["contestnumber"] == $aqui["contestnumber"] && $la["runsitenumber"] == $aqui["runsitenumber"]) {
                    $needed[$k] = true;
                    if ($la["updatetime"] > $aqui["updatetime"]) {
                        $situacao[$j] = "atualizar";
                    } else {
                        $situacao[$j] = "ok";
                    }
                    break;
                }
            }
            if ($k >= $nn) {
                $situacao[$j] = "inserir";
            }
        }
        $del = 0;
        for ($k = 0; $k < $nn; $k++) {
            $atual = DBRow($rr, $k);
            if (!$needed[$k]) {
                $del++;
                DBExec($c, "delete from runtable where contestnumber={$contest} and runsitenumber={$site} and runnumber=" . $atual["runnumber"]);
            }
        }
        $upd = 0;
        $ins = 0;
        for ($j = 0; $j < $n; $j++) {
            $atual = DBRow($r, $j);
            /*
            			$r2 = DBExec ($c,"select * from runtable where contestnumber=$contest and runsitenumber=".$atual["runsitenumber"].
                                                    " and runnumber=".$atual["runnumber"]." for update");
            			if (DBnLines($r2)>0) {
            				$p = DBRow($r2, 0);
            				DB_lo_unlink($c, $p["rundata"]);
            			}
            */
            if ($atual["runjudge"] == "") {
                $atual["runjudge"] = "null";
            }
            if ($atual["runjudgesite"] == "") {
                $atual["runjudgesite"] = "null";
            }
            if ($atual["updatetime"] == "") {
                $atual["updatetime"] = time();
            }
            if ($situacao[$j] == "atualizar") {
                $upd++;
                DBExec($c, "update runtable set " . "usernumber=" . $atual["usernumber"] . "," . "rundate=" . $atual["rundate"] . "," . "rundatediff=" . $atual["rundatediff"] . "," . "rundatediffans=" . $atual["rundatediffans"] . "," . "runproblem=" . $atual["runproblem"] . "," . "runlangnumber=" . $atual["runlangnumber"] . "," . "runanswer=" . $atual["runanswer"] . "," . "runstatus='" . escape_string($atual["runstatus"]) . "'," . "runjudge=" . $atual["runjudge"] . "," . "runjudgesite=" . $atual["runjudgesite"] . "," . "runfilename='" . escape_string($atual["runfilename"]) . "'," . "updatetime=" . $atual["updatetime"] . " where contestnumber={$contest} and runsitenumber=" . $atual["runsitenumber"] . " and runnumber=" . $atual["runnumber"] . " and updatetime<" . $atual["updatetime"]);
            } else {
                if ($situacao[$j] == "inserir") {
                    $ins++;
                    DBExec($t, "begin work");
                    $in = DB_lo_open($t, $atual["rundata"], "r");
                    if (!$in) {
                        $o = "0";
                    } else {
                        $o = DB_lo_create($c);
                        $out = DB_lo_open($c, $o, "w");
                        while (($buf = DB_lo_read($in, 1000)) != false) {
                            DB_lo_write($out, $buf);
                        }
                        DB_lo_close($out);
                        DB_lo_close($in);
                        $in = DB_lo_open($t, $atual["rundata"], "r");
                        if (!$in) {
                            $o = "0";
                            LOGLevel("Run not saved as file (run=" . $atual["runnumber"] . ",site=" . $atual["runsitenumber"] . ",contest={$contest}", 1);
                        } else {
                            $sitess = $atual["runsitenumber"];
                            $nss = $atual["runnumber"];
                            $user = $atual["usernumber"];
                            $problem = $atual["runproblem"];
                            $filename = escape_string($atual["runfilename"]);
                            $ttimet = $atual["rundate"];
                            $fp = fopen("/tmp/boca/contest{$contest}.site{$sitess}.run{$nss}.user{$user}." . "problem{$problem}.time{$ttimet}.{$filename}", "w");
                            if ($fp) {
                                while (($buf = DB_lo_read($in, 1000)) != false) {
                                    fwrite($fp, $buf);
                                }
                                fclose($fp);
                                $fp = fopen("/tmp/check/contest{$contest}.site{$sitess}.run{$nss}.user{$user}." . "problem{$problem}.time{$ttimet}.{$filename}.check", "w");
                                if ($fp) {
                                    fwrite($fp, "1");
                                    fclose($fp);
                                } else {
                                    LOGLevel("Run not saved as check file (run=" . $atual["runnumber"] . ",site=" . $atual["runsitenumber"] . ",contest={$contest}", 1);
                                }
                            } else {
                                LOGLevel("Run not saved as file (run=" . $atual["runnumber"] . ",site=" . $atual["runsitenumber"] . ",contest={$contest}", 1);
                            }
                            DB_lo_close($out);
                        }
                    }
                    DBExec($c, "insert into runtable (contestnumber, runsitenumber, runnumber, usernumber, rundate," . "rundatediff, rundatediffans, runproblem, runfilename, rundata, runanswer, runstatus, runjudge," . "runjudgesite, runlangnumber, updatetime) " . "values (" . $contest . "," . $atual["runsitenumber"] . "," . $atual["runnumber"] . "," . $atual["usernumber"] . "," . $atual["rundate"] . "," . $atual["rundatediff"] . "," . $atual["rundatediffans"] . "," . $atual["runproblem"] . "," . "'" . escape_string($atual["runfilename"]) . "',{$o}," . $atual["runanswer"] . "," . "'" . escape_string($atual["runstatus"]) . "'," . $atual["runjudge"] . "," . $atual["runjudgesite"] . "," . $atual["runlangnumber"] . "," . $atual["updatetime"] . ")");
                    DBExec($t, "commit work");
                }
            }
        }
        echo "   deletions={$del}, updates={$upd}, insertions={$ins}\n\n";
        // sincronizando runs
        $r = DBExec($t, "select * from runtable where contestnumber={$contest} and runsitenumber={$localsite}");
        $rr = DBExec($c, "select * from runtable where contestnumber={$contest} and runsitenumber={$localsite}");
        $n = DBnLines($r);
        $nn = DBnLines($rr);
        echo "runs: external(site={$site}, reading={$localsite})={$n}, local={$nn}\n";
        for ($k = 0; $k < $nn; $k++) {
            $needed[$k] = false;
        }
        for ($j = 0; $j < $n; $j++) {
            $la = DBRow($r, $j);
            for ($k = 0; $k < $nn; $k++) {
                $aqui = DBRow($rr, $k);
                if ($la["runnumber"] == $aqui["runnumber"] && $la["contestnumber"] == $aqui["contestnumber"] && $la["runsitenumber"] == $aqui["runsitenumber"]) {
                    $needed[$k] = true;
                    if ($la["updatetime"] > $aqui["updatetime"]) {
                        $situacao[$j] = "atualizar";
                        if ($la["runanswer"] != "") {
                            $rrr = DBExec($c, "select * from answertable where answernumber=" . $la["runanswer"] . " and contestnumber=" . $la["contestnumber"]);
                            $ans = DBnLines($rrr) > 0 ? DBRow($rrr, 0) : null;
                            if ($ans == null) {
                                echo "Problem with the answer table. Unable to send balloon because the answer was " . "not found (run=" . $la["runnumber"] . ", site=" . $la["runsite"] . ", contest=" . $la["contestnumber"] . ", answer=" . $la["runanswer"] . ").";
                                $yesla = 'x';
                            } else {
                                $yesla = $ans["yes"];
                            }
                        } else {
                            $yesla = 'f';
                        }
                        if ($aqui["runanswer"] != "") {
                            $rrr = DBExec($c, "select * from answertable where answernumber=" . $aqui["runanswer"] . " and contestnumber=" . $aqui["contestnumber"]);
                            $ans = DBnLines($rrr) > 0 ? DBRow($rrr, 0) : null;
                            if ($ans == null) {
                                echo "Problem with the answer table. Unable to send balloon because the answer was " . "not found (run=" . $aqui["runnumber"] . ", site=" . $aqui["runsite"] . ", contest=" . $aqui["contestnumber"] . ", answer=" . $aqui["runanswer"] . ").";
                                $yesaqui = 'x';
                            } else {
                                $yesaqui = $ans["yes"];
                            }
                        } else {
                            $yesaqui = 'f';
                        }
                        if ($yesla == 't' && $yesaqui == 'f') {
                            $rrr = DBExec($c, "select * from sitetable where contestnumber=" . $aqui["contestnumber"] . " and sitenumber={$localsite}");
                            if (DBnLines($rrr) <= 0) {
                                echo "Site info not found (contest={$aqui["contestnumber"]}, site={$localsite}).";
                            } else {
                                $b = DBRow($rrr, 0);
                                $ti = $b["sitestartdate"];
                                $tempo = time();
                                $ta = $tempo - $ti;
                                $tf = $b["sitelastmileanswer"];
                                //						                if ($ta < $tf) {
                                $rrr = DBExec($c, "select * from usertable where contestnumber=" . $aqui["contestnumber"] . " and usersitenumber={$localsite} and usernumber=" . $aqui["usernumber"]);
                                if (DBnLines($rrr) <= 0) {
                                    echo "User info not found (contest={$aqui["contestnumber"]}, " . "site={$localsite}, user={$aqui["runusernumber"]}).";
                                } else {
                                    $u = DBRow($rrr, 0);
                                    $rrr = DBExec($c, "select * from problemtable where contestnumber=" . $aqui["contestnumber"] . " and problemnumber=" . $aqui["runproblem"]);
                                    if (DBnLines($rrr) <= 0) {
                                        echo "Problem info not found (contest={$aqui["contestnumber"]}, " . "problem={$aqui["runproblem"]}).";
                                    } else {
                                        $p = DBRow($rrr, 0);
                                        mail("balloon@mainserver", "YES: team=" . $u["username"] . ", problem=" . $p["problemname"], "User " . $u["username"] . " should receive a balloon for " . $p["problemfullname"] . "\n");
                                    }
                                }
                                //								}
                            }
                        } else {
                            if ($yesla == 'f' && $yesaqui == 't') {
                                $rrr = DBExec($c, "select * from usertable where contestnumber=" . $aqui["contestnumber"] . " and usersitenumber={$localsite} and usernumber=" . $aqui["usernumber"]);
                                if (DBnLines($rrr) <= 0) {
                                    echo "User info not found (contest={$aqui["contestnumber"]}, " . "site={$localsite}, user={$aqui["runusernumber"]}).";
                                } else {
                                    $u = DBRow($rrr, 0);
                                    $rrr = DBExec($c, "select * from problemtable where contestnumber=" . $aqui["contestnumber"] . " and problemnumber=" . $aqui["runproblem"]);
                                    if (DBnLines($rrr) <= 0) {
                                        echo "Problem info not found (contest={$aqui["contestnumber"]}, " . "problem={$aqui["runproblem"]}).";
                                    } else {
                                        $p = DBRow($rrr, 0);
                                        mail("balloon@mainserver", "NO: team=" . $u["username"] . ", problem=" . $p["problemname"], "Remove the balloon from user " . $u["username"] . " for " . $p["problemfullname"] . "\n");
                                    }
                                }
                            }
                        }
                    } else {
                        $situacao[$j] = "ok";
                    }
                    break;
                }
            }
            if ($k >= $nn) {
                $situacao[$j] = "inserir";
            }
        }
        $upd = 0;
        $ins = 0;
        for ($j = 0; $j < $n; $j++) {
            $atual = DBRow($r, $j);
            if ($atual["runjudge"] == "") {
                $atual["runjudge"] = "null";
            }
            if ($atual["runjudgesite"] == "") {
                $atual["runjudgesite"] = "null";
            }
            if ($atual["updatetime"] == "") {
                $atual["updatetime"] = time();
            }
            if ($situacao[$j] == "atualizar") {
                $upd++;
                DBExec($c, "update runtable set " . "usernumber=" . $atual["usernumber"] . "," . "rundate=" . $atual["rundate"] . "," . "rundatediff=" . $atual["rundatediff"] . "," . "rundatediffans=" . $atual["rundatediffans"] . "," . "runproblem=" . $atual["runproblem"] . "," . "runlangnumber=" . $atual["runlangnumber"] . "," . "runanswer=" . $atual["runanswer"] . "," . "runstatus='" . escape_string($atual["runstatus"]) . "'," . "runjudge=" . $atual["runjudge"] . "," . "runjudgesite=" . $atual["runjudgesite"] . "," . "runfilename='" . escape_string($atual["runfilename"]) . "'," . "updatetime=" . $atual["updatetime"] . " where contestnumber={$contest} and runsitenumber=" . $atual["runsitenumber"] . " and runnumber=" . $atual["runnumber"] . " and updatetime<" . $atual["updatetime"]);
            } else {
                if ($situacao[$j] == "inserir") {
                    $ins++;
                    echo "Run Inserts shouldn't exist (run=" . $atual["runnumber"] . ")...";
                }
            }
        }
        echo "   updates={$upd}, insertions={$ins}\n\n";
        DBExec($c, "commit work");
        DBClose($t);
    }
    DBClose($c);
}
示例#5
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;
}