From b8e84f43f1a30345afdadfe219fa7b6a3ed003d3 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Mon, 20 Mar 2023 15:03:25 +0800 Subject: [PATCH] feat: fetch problem from luogu --- INSTALLATION.md | 6 + web/app/controllers/problem.php | 279 ++++++++++++++++++ web/app/controllers/problem.php.patch | 17 ++ web/app/controllers/problem_set.php | 224 ++++++++++++++ web/app/controllers/problem_set.php.patch | 57 ++++ .../controllers/problem_statement_manage.php | 78 +++++ .../problem_statement_manage.php.patch | 40 +++ web/app/libs/uoj-luogu-lib.php | 32 +- web/app/models/Curl.php | 2 - 9 files changed, 730 insertions(+), 5 deletions(-) create mode 100644 web/app/controllers/problem.php create mode 100644 web/app/controllers/problem.php.patch create mode 100644 web/app/controllers/problem_set.php create mode 100644 web/app/controllers/problem_set.php.patch create mode 100644 web/app/controllers/problem_statement_manage.php create mode 100644 web/app/controllers/problem_statement_manage.php.patch diff --git a/INSTALLATION.md b/INSTALLATION.md index 8914ec4..5b82355 100644 --- a/INSTALLATION.md +++ b/INSTALLATION.md @@ -1,3 +1,9 @@ # 安装 +## 前期准备 + +请先在服务器上安装 Node.js 18 LTS 和 `php-curl`。 + +## 修改源码 + _TODO_ diff --git a/web/app/controllers/problem.php b/web/app/controllers/problem.php new file mode 100644 index 0000000..a639593 --- /dev/null +++ b/web/app/controllers/problem.php @@ -0,0 +1,279 @@ +比赛正在进行中
很遗憾,您尚未报名。比赛结束后再来看吧~
"); + } else { + $is_in_contest = true; + DB::update("update contests_registrants set has_participated = 1 where username = '{$myUser['username']}' and contest_id = {$contest['id']}"); + } + } else { + $ban_in_contest = !isProblemVisibleToUser($problem, $myUser); + } + } + } else { + if (!isProblemVisibleToUser($problem, $myUser)) { + become404Page(); + } + } + + $submission_requirement = json_decode($problem['submission_requirement'], true); + $problem_extra_config = getProblemExtraConfig($problem); + $custom_test_requirement = getProblemCustomTestRequirement($problem); + + if ($custom_test_requirement && Auth::check()) { + $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"); + $custom_test_submission_result = json_decode($custom_test_submission['result'], true); + } + if ($custom_test_requirement && $_GET['get'] == 'custom-test-status-details' && Auth::check()) { + if ($custom_test_submission == null) { + echo json_encode(null); + } elseif ($custom_test_submission['status'] != 'Judged') { + echo json_encode(array( + 'judged' => false, + 'html' => getSubmissionStatusDetails($custom_test_submission) + )); + } else { + ob_start(); + $styler = new CustomTestSubmissionDetailsStyler(); + if (!hasViewPermission($problem_extra_config['view_details_type'], $myUser, $problem, $submission)) { + $styler->fade_all_details = true; + } + echoJudgementDetails($custom_test_submission_result['details'], $styler, 'custom_test_details'); + $result = ob_get_contents(); + ob_end_clean(); + echo json_encode(array( + 'judged' => true, + 'html' => getSubmissionStatusDetails($custom_test_submission), + 'result' => $result + )); + } + die(); + } + + $can_use_zip_upload = true; + foreach ($submission_requirement as $req) { + if ($req['type'] == 'source code') { + $can_use_zip_upload = false; + } + } + + function handleUpload($zip_file_name, $content, $tot_size) { + global $problem, $contest, $myUser, $is_in_contest; + + $content['config'][] = array('problem_id', $problem['id']); + if ($is_in_contest && $contest['extra_config']["contest_type"]!='IOI' && !isset($contest['extra_config']["problem_{$problem['id']}"])) { + $content['final_test_config'] = $content['config']; + $content['config'][] = array('test_sample_only', 'on'); + } + $esc_content = DB::escape(json_encode($content)); + + $language = '/'; + foreach ($content['config'] as $row) { + if (strEndWith($row[0], '_language')) { + $language = $row[1]; + break; + } + } + if ($language != '/') { + Cookie::set('uoj_preferred_language', $language, time() + 60 * 60 * 24 * 365, '/'); + } + $esc_language = DB::escape($language); + + $result = array(); + $result['status'] = "Waiting"; + $result_json = json_encode($result); + + if ($is_in_contest) { + 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)"); + } else { + 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']})"); + } + } + function handleCustomTestUpload($zip_file_name, $content, $tot_size) { + global $problem, $contest, $myUser; + + $content['config'][] = array('problem_id', $problem['id']); + $content['config'][] = array('custom_test', 'on'); + $esc_content = DB::escape(json_encode($content)); + + $language = '/'; + foreach ($content['config'] as $row) { + if (strEndWith($row[0], '_language')) { + $language = $row[1]; + break; + } + } + if ($language != '/') { + Cookie::set('uoj_preferred_language', $language, time() + 60 * 60 * 24 * 365, '/'); + } + $esc_language = DB::escape($language); + + $result = array(); + $result['status'] = "Waiting"; + $result_json = json_encode($result); + + 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')"); + } + + if ($can_use_zip_upload) { + $zip_answer_form = newZipSubmissionForm('zip_answer', + $submission_requirement, + 'uojRandAvaiableSubmissionFileName', + 'handleUpload'); + $zip_answer_form->extra_validator = function() { + global $ban_in_contest; + if ($ban_in_contest) { + return '请耐心等待比赛结束后题目对所有人可见了再提交'; + } + return ''; + }; + $zip_answer_form->succ_href = $is_in_contest ? "/contest/{$contest['id']}/submissions" : '/submissions'; + $zip_answer_form->runAtServer(); + } + + $answer_form = newSubmissionForm('answer', + $submission_requirement, + 'uojRandAvaiableSubmissionFileName', + 'handleUpload'); + $answer_form->extra_validator = function() { + global $ban_in_contest; + if ($ban_in_contest) { + return '请耐心等待比赛结束后题目对所有人可见了再提交'; + } + return ''; + }; + $answer_form->succ_href = $is_in_contest ? "/contest/{$contest['id']}/submissions" : '/submissions'; + $answer_form->runAtServer(); + + if ($custom_test_requirement) { + $custom_test_form = newSubmissionForm('custom_test', + $custom_test_requirement, + function() { + return uojRandAvaiableFileName('/tmp/'); + }, + 'handleCustomTestUpload'); + $custom_test_form->appendHTML(<<' . $e->getMessage() . ''); + } + }; + // $new_luogu_problem_form->submit_button_config['align'] = 'right'; + $new_luogu_problem_form->submit_button_config['class_str'] = 'btn btn-primary'; + $new_luogu_problem_form->submit_button_config['text'] = UOJLocale::get('problems::add new'); + $new_luogu_problem_form->submit_button_config['confirm'] = '新增来自洛谷的题目'; + $new_luogu_problem_form->runAtServer(); + } + + function echoProblem($problem) { + global $myUser; + if (isProblemVisibleToUser($problem, $myUser)) { + echo '
'.UOJLocale::get('none').' |
' . $e->getMessage() . ''); ++ } ++ }; ++ // $new_luogu_problem_form->submit_button_config['align'] = 'right'; ++ $new_luogu_problem_form->submit_button_config['class_str'] = 'btn btn-primary'; ++ $new_luogu_problem_form->submit_button_config['text'] = UOJLocale::get('problems::add new'); ++ $new_luogu_problem_form->submit_button_config['confirm'] = '新增来自洛谷的题目'; ++ $new_luogu_problem_form->runAtServer(); + } + + function echoProblem($problem) { +@@ -186,7 +207,16 @@ + echo ''; + + if (isSuperUser($myUser)) { ++ echo ''; + $new_problem_form->printHTML(); ++ ++ echo ''; ++ echo '
' . $e->getMessage() . ''); + } + }; + $refetch_problem_form->runAtServer(); + } +?> + +
' . $e->getMessage() . ''); ++ } ++ }; ++ $refetch_problem_form->runAtServer(); ++ } + ?> + +