f = fopen($file_name, 'r'); } public function failed() { return $this->f === false; } public function readChar() { if (isset($this->buf[$this->off])) { return $this->buf[$this->off++]; } return fgetc($this->f); } public function unreadChar($c) { $this->buf .= $c; if ($this->off > 1000) { $this->buf = substr($this->buf, $this->off); $this->off = 0; } } public function readString() { $str = ''; while (true) { $c = $this->readChar(); if ($c === false) { break; } elseif ($c === " " || $c === "\n" || $c === "\r") { $this->unreadChar($c); break; } else { $str .= $c; } } return $str; } public function ignoreWhite() { while (true) { $c = $this->readChar(); if ($c === false) { break; } elseif ($c === " " || $c === "\n" || $c === "\r") { continue; } else { $this->unreadChar($c); break; } } } public function eof() { return feof($this->f); } public function close() { fclose($this->f); } } function getUOJConf($file_name) { $reader = new StrictFileReader($file_name); if ($reader->failed()) { return -1; } $conf = array(); while (!$reader->eof()) { $reader->ignoreWhite(); $key = $reader->readString(); if ($key === '') { break; } $reader->ignoreWhite(); $val = $reader->readString(); if ($val === '') { break; } if (isset($conf[$key])) { return -2; } $conf[$key] = $val; } $reader->close(); return $conf; } function putUOJConf($file_name, $conf) { $f = fopen($file_name, 'w'); foreach ($conf as $key => $val) { fwrite($f, "$key $val\n"); } fclose($f); } function getUOJConfVal($conf, $key, $default_val) { if (isset($conf[$key])) { return $conf[$key]; } else { return $default_val; } } // problem.conf can also arrive inside a zip that the problem's controller // uploads, so *every* value in it is attacker-controlled: the field checks in // problem_data_manage.php are not a security boundary, because the zip upload // path never goes through them. input_pre / input_suf / output_pre / // output_suf used to be concatenated straight into a filesystem path, so // // input_pre x/../../../../../opt/uoj/web/shell. // input_suf php // // made dataAddExtraTest() move an uploaded hack input to // /opt/uoj/web/shell.1.php -- an arbitrary-content arbitrary-path write into // the document root, i.e. remote code execution as the web user. // // Only a bare filename component is ever legitimate here. Anything else // (separators, "..", absolute paths, ...) falls back to the default instead of // escaping the problem's own data directory. Same idea as the whitelist that // already guards use_builtin_checker in uoj-data-lib.php. function getUOJConfFileNamePart($conf, $key, $default_val) { $val = getUOJConfVal($conf, $key, $default_val); if (!is_string($val) || !preg_match('/^[A-Za-z0-9_.-]*$/', $val) || strpos($val, '..') !== false) { error_log("uoj: ignoring unsafe problem.conf value for '$key'"); return $default_val; } return $val; } function getUOJProblemInputFileName($problem_conf, $num) { return getUOJConfFileNamePart($problem_conf, 'input_pre', 'input') . $num . '.' . getUOJConfFileNamePart($problem_conf, 'input_suf', 'txt'); } function getUOJProblemOutputFileName($problem_conf, $num) { return getUOJConfFileNamePart($problem_conf, 'output_pre', 'output') . $num . '.' . getUOJConfFileNamePart($problem_conf, 'output_suf', 'txt'); } function getUOJProblemExtraInputFileName($problem_conf, $num) { return 'ex_' . getUOJConfFileNamePart($problem_conf, 'input_pre', 'input') . $num . '.' . getUOJConfFileNamePart($problem_conf, 'input_suf', 'txt'); } function getUOJProblemExtraOutputFileName($problem_conf, $num) { return 'ex_' . getUOJConfFileNamePart($problem_conf, 'output_pre', 'output') . $num . '.' . getUOJConfFileNamePart($problem_conf, 'output_suf', 'txt'); } function rejudgeProblem($problem) { DB::query("update submissions set judge_time = NULL , result = '' , score = NULL , status = 'Waiting Rejudge' where problem_id = ${problem['id']}"); } function rejudgeProblemAC($problem) { DB::query("update submissions set judge_time = NULL , result = '' , score = NULL , status = 'Waiting Rejudge' where problem_id = ${problem['id']} and score = 100"); } function rejudgeProblemGe97($problem) { DB::query("update submissions set judge_time = NULL , result = '' , score = NULL , status = 'Waiting Rejudge' where problem_id = ${problem['id']} and score >= 97"); } function rejudgeSubmission($submission) { DB::query("update submissions set judge_time = NULL , result = '' , score = NULL , status = 'Waiting Rejudge' where id = ${submission['id']}"); } function updateBestACSubmissions($username, $problem_id) { $best = DB::selectFirst("select id, used_time, used_memory, tot_size from submissions where submitter = '$username' and problem_id = $problem_id and score = 100 order by used_time, used_memory, tot_size asc limit 1"); $shortest = DB::selectFirst("select id, used_time, used_memory, tot_size from submissions where submitter = '$username' and problem_id = $problem_id and score = 100 order by tot_size, used_time, used_memory asc limit 1"); DB::delete("delete from best_ac_submissions where submitter = '$username' and problem_id = $problem_id"); if ($best) { DB::insert("insert into best_ac_submissions (problem_id, submitter, submission_id, used_time, used_memory, tot_size, shortest_id, shortest_used_time, shortest_used_memory, shortest_tot_size) values ($problem_id, '$username', ${best['id']}, ${best['used_time']}, ${best['used_memory']}, ${best['tot_size']}, ${shortest['id']}, ${shortest['used_time']}, ${shortest['used_memory']}, ${shortest['tot_size']})"); } $cnt = DB::selectCount("select count(*) from best_ac_submissions where submitter='$username'"); DB::update("update user_info set ac_num = $cnt where username='$username'"); DB::update("update problems set ac_num = (select count(*) from submissions where problem_id = problems.id and score = 100), submit_num = (select count(*) from submissions where problem_id = problems.id) where id = $problem_id"); } ?>