uoj-judger-lib.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. global $uojSupportedLanguages, $uojMainJudgerWorkPath;
  3. $uojSupportedLanguages = array('C', 'C++14', 'C++17', 'C++17ubsan', 'Python3');
  4. $uojMainJudgerWorkPath = "/opt/uoj/judger/uoj_judger";
  5. function authenticateJudger() {
  6. if (!is_string($_POST['judger_name']) || !is_string($_POST['password'])) {
  7. return false;
  8. }
  9. $esc_judger_name = DB::escape($_POST['judger_name']);
  10. $judger = DB::selectFirst("select password from judger_info where judger_name = '$esc_judger_name'");
  11. if ($judger == null) {
  12. return false;
  13. }
  14. return $judger['password'] == $_POST['password'];
  15. }
  16. function judgerCodeStr($code) {
  17. switch ($code) {
  18. case 0:
  19. return "Accepted";
  20. case 1:
  21. return "Wrong Answer";
  22. case 2:
  23. return "Runtime Error";
  24. case 3:
  25. return "Memory Limit Exceeded";
  26. case 4:
  27. return "Time Limit Exceeded";
  28. case 5:
  29. return "Output Limit Exceeded";
  30. case 6:
  31. return "Dangerous Syscalls";
  32. case 7:
  33. return "Judgement Failed";
  34. default:
  35. return "No Comment";
  36. }
  37. }
  38. class StrictFileReader {
  39. private $f;
  40. private $buf = '', $off = 0;
  41. public function __construct($file_name) {
  42. $this->f = fopen($file_name, 'r');
  43. }
  44. public function failed() {
  45. return $this->f === false;
  46. }
  47. public function readChar() {
  48. if (isset($this->buf[$this->off])) {
  49. return $this->buf[$this->off++];
  50. }
  51. return fgetc($this->f);
  52. }
  53. public function unreadChar($c) {
  54. $this->buf .= $c;
  55. if ($this->off > 1000) {
  56. $this->buf = substr($this->buf, $this->off);
  57. $this->off = 0;
  58. }
  59. }
  60. public function readString() {
  61. $str = '';
  62. while (true) {
  63. $c = $this->readChar();
  64. if ($c === false) {
  65. break;
  66. } elseif ($c === " " || $c === "\n" || $c === "\r") {
  67. $this->unreadChar($c);
  68. break;
  69. } else {
  70. $str .= $c;
  71. }
  72. }
  73. return $str;
  74. }
  75. public function ignoreWhite() {
  76. while (true) {
  77. $c = $this->readChar();
  78. if ($c === false) {
  79. break;
  80. } elseif ($c === " " || $c === "\n" || $c === "\r") {
  81. continue;
  82. } else {
  83. $this->unreadChar($c);
  84. break;
  85. }
  86. }
  87. }
  88. public function eof() {
  89. return feof($this->f);
  90. }
  91. public function close() {
  92. fclose($this->f);
  93. }
  94. }
  95. function getUOJConf($file_name) {
  96. $reader = new StrictFileReader($file_name);
  97. if ($reader->failed()) {
  98. return -1;
  99. }
  100. $conf = array();
  101. while (!$reader->eof()) {
  102. $reader->ignoreWhite();
  103. $key = $reader->readString();
  104. if ($key === '') {
  105. break;
  106. }
  107. $reader->ignoreWhite();
  108. $val = $reader->readString();
  109. if ($val === '') {
  110. break;
  111. }
  112. if (isset($conf[$key])) {
  113. return -2;
  114. }
  115. $conf[$key] = $val;
  116. }
  117. $reader->close();
  118. return $conf;
  119. }
  120. function putUOJConf($file_name, $conf) {
  121. $f = fopen($file_name, 'w');
  122. foreach ($conf as $key => $val) {
  123. fwrite($f, "$key $val\n");
  124. }
  125. fclose($f);
  126. }
  127. function getUOJConfVal($conf, $key, $default_val) {
  128. if (isset($conf[$key])) {
  129. return $conf[$key];
  130. } else {
  131. return $default_val;
  132. }
  133. }
  134. function getUOJProblemInputFileName($problem_conf, $num) {
  135. return getUOJConfVal($problem_conf, 'input_pre', 'input') . $num . '.' . getUOJConfVal($problem_conf, 'input_suf', 'txt');
  136. }
  137. function getUOJProblemOutputFileName($problem_conf, $num) {
  138. return getUOJConfVal($problem_conf, 'output_pre', 'output') . $num . '.' . getUOJConfVal($problem_conf, 'output_suf', 'txt');
  139. }
  140. function getUOJProblemExtraInputFileName($problem_conf, $num) {
  141. return 'ex_' . getUOJConfVal($problem_conf, 'input_pre', 'input') . $num . '.' . getUOJConfVal($problem_conf, 'input_suf', 'txt');
  142. }
  143. function getUOJProblemExtraOutputFileName($problem_conf, $num) {
  144. return 'ex_' . getUOJConfVal($problem_conf, 'output_pre', 'output') . $num . '.' . getUOJConfVal($problem_conf, 'output_suf', 'txt');
  145. }
  146. function rejudgeProblem($problem) {
  147. DB::query("update submissions set judge_time = NULL , result = '' , score = NULL , status = 'Waiting Rejudge' where problem_id = ${problem['id']}");
  148. }
  149. function rejudgeProblemAC($problem) {
  150. DB::query("update submissions set judge_time = NULL , result = '' , score = NULL , status = 'Waiting Rejudge' where problem_id = ${problem['id']} and score = 100");
  151. }
  152. function rejudgeProblemGe97($problem) {
  153. DB::query("update submissions set judge_time = NULL , result = '' , score = NULL , status = 'Waiting Rejudge' where problem_id = ${problem['id']} and score >= 97");
  154. }
  155. function rejudgeSubmission($submission) {
  156. DB::query("update submissions set judge_time = NULL , result = '' , score = NULL , status = 'Waiting Rejudge' where id = ${submission['id']}");
  157. }
  158. function updateBestACSubmissions($username, $problem_id) {
  159. $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");
  160. $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");
  161. DB::delete("delete from best_ac_submissions where submitter = '$username' and problem_id = $problem_id");
  162. if ($best) {
  163. 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']})");
  164. }
  165. $cnt = DB::selectCount("select count(*) from best_ac_submissions where submitter='$username'");
  166. DB::update("update user_info set ac_num = $cnt where username='$username'");
  167. 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");
  168. }
  169. ?>