submit.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. requirePHPLib('judger');
  3. requirePHPLib('data');
  4. if (!authenticateJudger()) {
  5. become404Page();
  6. }
  7. function submissionJudged() {
  8. $submission = DB::selectFirst("select submitter, status, content, result, problem_id from submissions where id = {$_POST['id']}");
  9. if ($submission == null) {
  10. return;
  11. }
  12. if ($submission['status'] != 'Judging' && $submission['status'] != 'Judged, Judging') {
  13. return;
  14. }
  15. $content = json_decode($submission['content'], true);
  16. if (isset($content['first_test_config'])) {
  17. $result = json_decode($submission['result'], true);
  18. $result['final_result'] = json_decode($_POST['result'], true);
  19. $result['final_result']['details'] = uojTextEncode($result['final_result']['details']);
  20. $esc_result = DB::escape(json_encode($result, JSON_UNESCAPED_UNICODE));
  21. $content['final_test_config'] = $content['config'];
  22. $content['config'] = $content['first_test_config'];
  23. unset($content['first_test_config']);
  24. $esc_content = DB::escape(json_encode($content));
  25. DB::update("update submissions set status = 'Judged', result = '$esc_result', content = '$esc_content' where id = {$_POST['id']}");
  26. } else {
  27. $result = json_decode($_POST['result'], true);
  28. $result['details'] = uojTextEncode($result['details']);
  29. $esc_result = DB::escape(json_encode($result, JSON_UNESCAPED_UNICODE));
  30. if (isset($result["error"])) {
  31. DB::update("update submissions set status = '{$result['status']}', result_error = '{$result['error']}', result = '$esc_result', score = null, used_time = null, used_memory = null where id = {$_POST['id']}");
  32. } else {
  33. DB::update("update submissions set status = '{$result['status']}', result_error = null, result = '$esc_result', score = {$result['score']}, used_time = {$result['time']}, used_memory = {$result['memory']} where id = {$_POST['id']}");
  34. }
  35. if (isset($content['final_test_config'])) {
  36. $content['first_test_config'] = $content['config'];
  37. $content['config'] = $content['final_test_config'];
  38. unset($content['final_test_config']);
  39. $esc_content = DB::escape(json_encode($content));
  40. DB::update("update submissions set status = 'Judged, Waiting', content = '$esc_content' where id = ${_POST['id']}");
  41. }
  42. }
  43. DB::update("update submissions set status_details = '' where id = {$_POST['id']}");
  44. updateBestACSubmissions($submission['submitter'], $submission['problem_id']);
  45. }
  46. function customTestSubmissionJudged() {
  47. $submission = DB::selectFirst("select submitter, status, content, result, problem_id from custom_test_submissions where id = {$_POST['id']}");
  48. if ($submission == null) {
  49. return;
  50. }
  51. if ($submission['status'] != 'Judging') {
  52. return;
  53. }
  54. $content = json_decode($submission['content'], true);
  55. $result = json_decode($_POST['result'], true);
  56. $result['details'] = uojTextEncode($result['details']);
  57. $esc_result = DB::escape(json_encode($result, JSON_UNESCAPED_UNICODE));
  58. if (isset($result["error"])) {
  59. DB::update("update custom_test_submissions set status = '{$result['status']}', result = '$esc_result' where id = {$_POST['id']}");
  60. } else {
  61. DB::update("update custom_test_submissions set status = '{$result['status']}', result = '$esc_result' where id = {$_POST['id']}");
  62. }
  63. DB::update("update custom_test_submissions set status_details = '' where id = {$_POST['id']}");
  64. }
  65. function hackJudged() {
  66. $result = json_decode($_POST['result'], true);
  67. $esc_details = DB::escape(uojTextEncode($result['details']));
  68. $ok = DB::update("update hacks set success = {$result['score']}, details = '$esc_details' where id = {$_POST['id']}");
  69. if ($ok) {
  70. list($hack_input) = DB::fetch(DB::query("select input from hacks where id = {$_POST['id']}"), MYSQLI_NUM);
  71. unlink(UOJContext::storagePath().$hack_input);
  72. if ($result['score']) {
  73. list($problem_id) = DB::selectFirst("select problem_id from hacks where id = ${_POST['id']}", MYSQLI_NUM);
  74. if (validateUploadedFile('hack_input') && validateUploadedFile('std_output')) {
  75. dataAddExtraTest(queryProblemBrief($problem_id), $_FILES["hack_input"]["tmp_name"], $_FILES["std_output"]["tmp_name"]);
  76. } else {
  77. error_log("hack successfully but received no data. id: ${_POST['id']}");
  78. }
  79. }
  80. }
  81. }
  82. if (isset($_POST['submit'])) {
  83. if (!validateUInt($_POST['id'])) {
  84. die("Wow! hacker! T_T....");
  85. }
  86. if (isset($_POST['is_hack'])) {
  87. hackJudged();
  88. } elseif (isset($_POST['is_custom_test'])) {
  89. customTestSubmissionJudged();
  90. } else {
  91. submissionJudged();
  92. }
  93. }
  94. if (isset($_POST['update-status'])) {
  95. if (!validateUInt($_POST['id'])) {
  96. die("Wow! hacker! T_T....");
  97. }
  98. $esc_status_details = DB::escape($_POST['status']);
  99. if (isset($_POST['is_custom_test'])) {
  100. DB::update("update custom_test_submissions set status_details = '$esc_status_details' where id = {$_POST['id']}");
  101. } else {
  102. DB::update("update submissions set status_details = '$esc_status_details' where id = {$_POST['id']}");
  103. }
  104. die();
  105. }
  106. $submission = null;
  107. $hack = null;
  108. function querySubmissionToJudge($status, $set_q) {
  109. global $submission;
  110. $submission = DB::selectFirst("select id, problem_id, content from submissions where status = '$status' order by id limit 1");
  111. if ($submission) {
  112. DB::update("update submissions set $set_q where id = {$submission['id']} and status = '$status'");
  113. if (DB::affected_rows() != 1) {
  114. $submission = null;
  115. }
  116. }
  117. }
  118. function queryCustomTestSubmissionToJudge() {
  119. global $submission;
  120. $submission = DB::selectFirst("select id, problem_id, content from custom_test_submissions where judge_time is null order by id limit 1");
  121. if ($submission) {
  122. DB::update("update custom_test_submissions set judge_time = now(), status = 'Judging' where id = {$submission['id']} and judge_time is null");
  123. if (DB::affected_rows() != 1) {
  124. $submission = null;
  125. }
  126. }
  127. if ($submission) {
  128. $submission['is_custom_test'] = '';
  129. }
  130. }
  131. function queryHackToJudge() {
  132. global $hack;
  133. $hack = DB::selectFirst("select id, submission_id, input, input_type from hacks where judge_time is null order by id limit 1");
  134. if ($hack) {
  135. DB::update("update hacks set judge_time = now() where id = {$hack['id']} and judge_time is null");
  136. if (DB::affected_rows() != 1) {
  137. $hack = null;
  138. }
  139. }
  140. }
  141. function findSubmissionToJudge() {
  142. global $submission, $hack;
  143. querySubmissionToJudge('Waiting', "judge_time = now(), status = 'Judging'");
  144. if ($submission) {
  145. return true;
  146. }
  147. queryCustomTestSubmissionToJudge();
  148. if ($submission) {
  149. return true;
  150. }
  151. querySubmissionToJudge('Waiting Rejudge', "judge_time = now(), status = 'Judging'");
  152. if ($submission) {
  153. return true;
  154. }
  155. querySubmissionToJudge('Judged, Waiting', "status = 'Judged, Judging'");
  156. if ($submission) {
  157. return true;
  158. }
  159. queryHackToJudge();
  160. if ($hack) {
  161. $submission = DB::selectFirst("select id, problem_id, content from submissions where id = {$hack['submission_id']} and score = 100");
  162. if (!$submission) {
  163. $details = "<error>the score gained by the hacked submission is not 100.\n</error>";
  164. $esc_details = DB::escape(uojTextEncode($details));
  165. DB::update("update hacks set success = 0, details = '$esc_details' where id = {$hack['id']}");
  166. return false;
  167. }
  168. return true;
  169. }
  170. return false;
  171. }
  172. if (isset($_POST['fetch_new']) && !$_POST['fetch_new']) {
  173. die("Nothing to judge");
  174. }
  175. if (!findSubmissionToJudge()) {
  176. die("Nothing to judge");
  177. }
  178. $submission['id'] = (int)$submission['id'];
  179. $submission['problem_id'] = (int)$submission['problem_id'];
  180. $submission['problem_mtime'] = filemtime("/var/uoj_data/{$submission['problem_id']}");
  181. $submission['content'] = json_decode($submission['content']);
  182. if ($hack) {
  183. $submission['is_hack'] = "";
  184. $submission['hack']['id'] = (int)$hack['id'];
  185. $submission['hack']['input'] = $hack['input'];
  186. $submission['hack']['input_type'] = $hack['input_type'];
  187. }
  188. echo json_encode($submission);
  189. ?>