problem.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. requirePHPLib('form');
  3. requirePHPLib('judger');
  4. if (!validateUInt($_GET['id']) || !($problem = queryProblemBrief($_GET['id']))) {
  5. become404Page();
  6. }
  7. $problem_content = queryProblemContent($problem['id']);
  8. $contest = validateUInt($_GET['contest_id']) ? queryContest($_GET['contest_id']) : null;
  9. if ($contest != null) {
  10. genMoreContestInfo($contest);
  11. $problem_rank = queryContestProblemRank($contest, $problem);
  12. if ($problem_rank == null) {
  13. become404Page();
  14. } else {
  15. $problem_letter = chr(ord('A') + $problem_rank - 1);
  16. }
  17. }
  18. $is_in_contest = false;
  19. $ban_in_contest = false;
  20. if ($contest != null) {
  21. if (!hasContestPermission($myUser, $contest)) {
  22. if ($contest['cur_progress'] == CONTEST_NOT_STARTED) {
  23. become404Page();
  24. } elseif ($contest['cur_progress'] == CONTEST_IN_PROGRESS) {
  25. if ($myUser == null || !hasRegistered($myUser, $contest)) {
  26. becomeMsgPage("<h1>比赛正在进行中</h1><p>很遗憾,您尚未报名。比赛结束后再来看吧~</p>");
  27. } else {
  28. $is_in_contest = true;
  29. DB::update("update contests_registrants set has_participated = 1 where username = '{$myUser['username']}' and contest_id = {$contest['id']}");
  30. }
  31. } else {
  32. $ban_in_contest = !isProblemVisibleToUser($problem, $myUser);
  33. }
  34. }
  35. } else {
  36. if (!isProblemVisibleToUser($problem, $myUser)) {
  37. become404Page();
  38. }
  39. }
  40. $submission_requirement = json_decode($problem['submission_requirement'], true);
  41. $problem_extra_config = getProblemExtraConfig($problem);
  42. $custom_test_requirement = getProblemCustomTestRequirement($problem);
  43. if ($custom_test_requirement && Auth::check()) {
  44. $custom_test_submission = DB::selectFirst("select * from custom_test_submissions where submitter = '".Auth::id()."' and problem_id = {$problem['id']} order by id desc limit 1");
  45. $custom_test_submission_result = json_decode($custom_test_submission['result'], true);
  46. }
  47. if ($custom_test_requirement && $_GET['get'] == 'custom-test-status-details' && Auth::check()) {
  48. if ($custom_test_submission == null) {
  49. echo json_encode(null);
  50. } elseif ($custom_test_submission['status'] != 'Judged') {
  51. echo json_encode(array(
  52. 'judged' => false,
  53. 'html' => getSubmissionStatusDetails($custom_test_submission)
  54. ));
  55. } else {
  56. ob_start();
  57. $styler = new CustomTestSubmissionDetailsStyler();
  58. if (!hasViewPermission($problem_extra_config['view_details_type'], $myUser, $problem, $submission)) {
  59. $styler->fade_all_details = true;
  60. }
  61. echoJudgementDetails($custom_test_submission_result['details'], $styler, 'custom_test_details');
  62. $result = ob_get_contents();
  63. ob_end_clean();
  64. echo json_encode(array(
  65. 'judged' => true,
  66. 'html' => getSubmissionStatusDetails($custom_test_submission),
  67. 'result' => $result
  68. ));
  69. }
  70. die();
  71. }
  72. $can_use_zip_upload = true;
  73. foreach ($submission_requirement as $req) {
  74. if ($req['type'] == 'source code') {
  75. $can_use_zip_upload = false;
  76. }
  77. }
  78. function handleUpload($zip_file_name, $content, $tot_size) {
  79. global $problem, $contest, $myUser, $is_in_contest;
  80. $content['config'][] = array('problem_id', $problem['id']);
  81. if ($is_in_contest && $contest['extra_config']["contest_type"]!='IOI' && !isset($contest['extra_config']["problem_{$problem['id']}"])) {
  82. $content['final_test_config'] = $content['config'];
  83. $content['config'][] = array('test_sample_only', 'on');
  84. }
  85. $esc_content = DB::escape(json_encode($content));
  86. $language = '/';
  87. foreach ($content['config'] as $row) {
  88. if (strEndWith($row[0], '_language')) {
  89. $language = $row[1];
  90. break;
  91. }
  92. }
  93. if ($language != '/') {
  94. Cookie::set('uoj_preferred_language', $language, time() + 60 * 60 * 24 * 365, '/');
  95. }
  96. $esc_language = DB::escape($language);
  97. $result = array();
  98. $result['status'] = "Waiting";
  99. $result_json = json_encode($result);
  100. if ($is_in_contest) {
  101. DB::query("insert into submissions (problem_id, contest_id, submit_time, submitter, content, language, tot_size, status, result, is_hidden) values (${problem['id']}, ${contest['id']}, now(), '${myUser['username']}', '$esc_content', '$esc_language', $tot_size, '${result['status']}', '$result_json', 0)");
  102. } else {
  103. DB::query("insert into submissions (problem_id, submit_time, submitter, content, language, tot_size, status, result, is_hidden) values (${problem['id']}, now(), '${myUser['username']}', '$esc_content', '$esc_language', $tot_size, '${result['status']}', '$result_json', {$problem['is_hidden']})");
  104. }
  105. }
  106. function handleCustomTestUpload($zip_file_name, $content, $tot_size) {
  107. global $problem, $contest, $myUser;
  108. $content['config'][] = array('problem_id', $problem['id']);
  109. $content['config'][] = array('custom_test', 'on');
  110. $esc_content = DB::escape(json_encode($content));
  111. $language = '/';
  112. foreach ($content['config'] as $row) {
  113. if (strEndWith($row[0], '_language')) {
  114. $language = $row[1];
  115. break;
  116. }
  117. }
  118. if ($language != '/') {
  119. Cookie::set('uoj_preferred_language', $language, time() + 60 * 60 * 24 * 365, '/');
  120. }
  121. $esc_language = DB::escape($language);
  122. $result = array();
  123. $result['status'] = "Waiting";
  124. $result_json = json_encode($result);
  125. DB::insert("insert into custom_test_submissions (problem_id, submit_time, submitter, content, status, result) values ({$problem['id']}, now(), '{$myUser['username']}', '$esc_content', '{$result['status']}', '$result_json')");
  126. }
  127. if ($can_use_zip_upload) {
  128. $zip_answer_form = newZipSubmissionForm('zip_answer',
  129. $submission_requirement,
  130. 'uojRandAvaiableSubmissionFileName',
  131. 'handleUpload');
  132. $zip_answer_form->extra_validator = function() {
  133. global $ban_in_contest;
  134. if ($ban_in_contest) {
  135. return '请耐心等待比赛结束后题目对所有人可见了再提交';
  136. }
  137. return '';
  138. };
  139. $zip_answer_form->succ_href = $is_in_contest ? "/contest/{$contest['id']}/submissions" : '/submissions';
  140. $zip_answer_form->runAtServer();
  141. }
  142. $answer_form = newSubmissionForm('answer',
  143. $submission_requirement,
  144. 'uojRandAvaiableSubmissionFileName',
  145. 'handleUpload');
  146. $answer_form->extra_validator = function() {
  147. global $ban_in_contest;
  148. if ($ban_in_contest) {
  149. return '请耐心等待比赛结束后题目对所有人可见了再提交';
  150. }
  151. return '';
  152. };
  153. $answer_form->succ_href = $is_in_contest ? "/contest/{$contest['id']}/submissions" : '/submissions';
  154. $answer_form->runAtServer();
  155. if ($custom_test_requirement) {
  156. $custom_test_form = newSubmissionForm('custom_test',
  157. $custom_test_requirement,
  158. function() {
  159. return uojRandAvaiableFileName('/tmp/');
  160. },
  161. 'handleCustomTestUpload');
  162. $custom_test_form->appendHTML(<<<EOD
  163. <div id="div-custom_test_result"></div>
  164. EOD
  165. );
  166. $custom_test_form->succ_href = 'none';
  167. $custom_test_form->extra_validator = function() {
  168. global $ban_in_contest, $custom_test_submission;
  169. if ($ban_in_contest) {
  170. return '请耐心等待比赛结束后题目对所有人可见了再提交';
  171. }
  172. if ($custom_test_submission && $custom_test_submission['status'] != 'Judged') {
  173. return '上一个测评尚未结束';
  174. }
  175. return '';
  176. };
  177. $custom_test_form->ctrl_enter_submit = true;
  178. $custom_test_form->setAjaxSubmit(<<<EOD
  179. function(response_text) {custom_test_onsubmit(response_text, $('#div-custom_test_result')[0], '{$_SERVER['REQUEST_URI']}?get=custom-test-status-details')}
  180. EOD
  181. );
  182. $custom_test_form->submit_button_config['text'] = UOJLocale::get('problems::run');
  183. $custom_test_form->runAtServer();
  184. }
  185. ?>
  186. <?php
  187. $REQUIRE_LIB['mathjax'] = '';
  188. $REQUIRE_LIB['hljs'] = '';
  189. ?>
  190. <?php echoUOJPageHeader(HTML::stripTags($problem['title']) . ' - ' . UOJLocale::get('problems::problem')) ?>
  191. <?php
  192. $limit = getUOJConf("/var/uoj_data/{$problem['id']}/problem.conf");
  193. $time_limit = $limit['time_limit'];
  194. $memory_limit = $limit['memory_limit'];
  195. ?>
  196. <div class="row d-flex justify-content-center">
  197. <span class="badge badge-secondary mr-1">时间限制:<?=$time_limit!=null?"$time_limit s":"N/A"?></span>
  198. <span class="badge badge-secondary mr-1">空间限制:<?=$memory_limit!=null?"$memory_limit MB":"N/A"?></span>
  199. <span class="badge badge-secondary mr-1">控制组: <?php echo $problem['ctrl'] == '' ? '默认' : $problem['ctrl'] ?></span>
  200. <span class="badge badge-secondary mr-1">压缩包大小: <?php echo printSize(queryProblemSize($problem['id'])) ?></span>
  201. </div>
  202. <div class="float-right">
  203. <?= getClickZanBlock('P', $problem['id'], $problem['zan']) ?>
  204. </div>
  205. <?php if ($contest): ?>
  206. <div class="page-header row">
  207. <h1 class="col-md-3 text-left"><small><?= $contest['name'] ?></small></h1>
  208. <h1 class="col-md-7 text-center"><?= $problem_letter ?>. <?= $problem['title'] ?></h1>
  209. <div class="col-md-2 text-right" id="contest-countdown"></div>
  210. </div>
  211. <a role="button" class="btn btn-info float-right" href="/contest/<?= $contest['id'] ?>/problem/<?= $problem['id'] ?>/statistics"><span class="glyphicon glyphicon-stats"></span> <?= UOJLocale::get('problems::statistics') ?></a>
  212. <?php if ($contest['cur_progress'] <= CONTEST_IN_PROGRESS): ?>
  213. <script type="text/javascript">
  214. checkContestNotice(<?= $contest['id'] ?>, '<?= UOJTime::$time_now_str ?>');
  215. $('#contest-countdown').countdown(<?= $contest['end_time']->getTimestamp() - UOJTime::$time_now->getTimestamp() ?>);
  216. </script>
  217. <?php endif ?>
  218. <?php else: ?>
  219. <h1 class="page-header text-center">#<?= $problem['id']?>. <?= $problem['title'] ?></h1>
  220. <a role="button" class="btn btn-info float-right" href="/problem/<?= $problem['id'] ?>/statistics"><span class="glyphicon glyphicon-stats"></span> <?= UOJLocale::get('problems::statistics') ?></a>
  221. <?php endif ?>
  222. <ul class="nav nav-tabs" role="tablist">
  223. <li class="nav-item"><a class="nav-link active" href="#tab-statement" role="tab" data-toggle="tab"><span class="glyphicon glyphicon-book"></span> <?= UOJLocale::get('problems::statement') ?></a></li>
  224. <li class="nav-item"><a class="nav-link" href="#tab-submit-answer" role="tab" data-toggle="tab"><span class="glyphicon glyphicon-upload"></span> <?= UOJLocale::get('problems::submit') ?></a></li>
  225. <?php if ($custom_test_requirement): ?>
  226. <li class="nav-item"><a class="nav-link" href="#tab-custom-test" role="tab" data-toggle="tab"><span class="glyphicon glyphicon-console"></span> <?= UOJLocale::get('problems::custom test') ?></a></li>
  227. <?php endif ?>
  228. <?php if (hasProblemCtrlPermission($myUser, $problem)): ?>
  229. <li class="nav-item"><a class="nav-link" href="/problem/<?= $problem['id'] ?>/manage/statement" role="tab"><?= UOJLocale::get('problems::manage') ?></a></li>
  230. <?php endif ?>
  231. <?php if ($contest): ?>
  232. <li class="nav-item"><a class="nav-link" href="/contest/<?= $contest['id'] ?>" role="tab"><?= UOJLocale::get('contests::back to the contest') ?></a></li>
  233. <?php endif ?>
  234. </ul>
  235. <div class="tab-content">
  236. <div class="tab-pane active" id="tab-statement">
  237. <article class="top-buffer-md"><?= $problem_content['statement'] ?></article>
  238. </div>
  239. <div class="tab-pane" id="tab-submit-answer">
  240. <div class="top-buffer-sm"></div>
  241. <?php if ($can_use_zip_upload): ?>
  242. <?php $zip_answer_form->printHTML(); ?>
  243. <hr />
  244. <strong><?= UOJLocale::get('problems::or upload files one by one') ?><br /></strong>
  245. <?php endif ?>
  246. <?php $answer_form->printHTML(); ?>
  247. </div>
  248. <?php if ($custom_test_requirement): ?>
  249. <div class="tab-pane" id="tab-custom-test">
  250. <div class="top-buffer-sm"></div>
  251. <?php $custom_test_form->printHTML(); ?>
  252. </div>
  253. <?php endif ?>
  254. </div>
  255. <?php echoUOJPageFooter() ?>