-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathnew_remote_problem.php
More file actions
170 lines (139 loc) · 4.88 KB
/
Copy pathnew_remote_problem.php
File metadata and controls
170 lines (139 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
requirePHPLib('form');
requirePHPLib('data');
Auth::check() || redirectToLogin();
UOJProblem::userCanCreateProblem(Auth::user()) || UOJResponse::page403();
$new_remote_problem_form = new UOJForm('new_remote_problem');
$new_remote_problem_form->addSelect('remote_online_judge', [
'label' => '远程 OJ',
'options' => array_map(fn ($provider) => $provider['name'], UOJRemoteProblem::$providers),
]);
$new_remote_problem_form->addInput('remote_problem_id', [
'div_class' => 'mt-3',
'label' => '远程 OJ 上的题目 ID',
'validator_php' => function ($id, &$vdata) {
$remote_oj = $_POST['remote_online_judge'];
if ($remote_oj === 'codeforces') {
$id = trim(strtoupper($id));
if (!validateCodeforcesProblemId($id)) {
return '不合法的题目 ID';
}
$vdata['remote_problem_id'] = $id;
return '';
} else if ($remote_oj === 'atcoder') {
$id = trim(strtolower($id));
if (!validateString($id)) {
return '不合法的题目 ID';
}
$vdata['remote_problem_id'] = $id;
return '';
} else if ($remote_oj === 'uoj') {
if (!validateUInt($id)) {
return '不合法的题目 ID';
}
$vdata['remote_problem_id'] = $id;
return '';
} else if ($remote_oj === 'loj') {
if (!validateUInt($id)) {
return '不合法的题目 ID';
}
$vdata['remote_problem_id'] = $id;
return '';
} else if ($remote_oj === 'luogu') {
$id = trim(strtoupper($id));
if (!validateLuoguProblemId($id)) {
return '不合法的题目 ID';
}
$vdata['remote_problem_id'] = $id;
return '';
}
return '不合法的远程 OJ 类型';
},
]);
$new_remote_problem_form->handle = function (&$vdata) {
$remote_online_judge = $_POST['remote_online_judge'];
$remote_problem_id = $vdata['remote_problem_id'];
$remote_provider = UOJRemoteProblem::$providers[$remote_online_judge];
try {
$data = UOJRemoteProblem::getProblemBasicInfo($remote_online_judge, $remote_problem_id);
} catch (Exception $e) {
$data = null;
UOJLog::error($e->getMessage());
}
if ($data === null) {
UOJResponse::page500('题目抓取失败,可能是题目不存在或者没有题面!如果题目没有问题,请稍后再试。<a href="">返回</a>');
}
$submission_requirement = UOJRemoteProblem::getSubmissionRequirements($remote_online_judge);
$enc_submission_requirement = json_encode($submission_requirement);
$extra_config = [
'remote_online_judge' => $remote_online_judge,
'remote_problem_id' => $remote_problem_id,
'time_limit' => $data['time_limit'],
'memory_limit' => $data['memory_limit'],
];
$enc_extra_config = json_encode($extra_config, JSON_FORCE_OBJECT);
DB::insert([
"insert into problems",
"(title, uploader, is_hidden, submission_requirement, extra_config, difficulty, type)",
"values", DB::tuple([$data['title'], Auth::id(), 1, $enc_submission_requirement, $enc_extra_config, $data['difficulty'] ?: -1, "remote"])
]);
$id = DB::insert_id();
dataNewProblem($id);
if ($data['type'] == 'pdf') {
file_put_contents(UOJContext::storagePath(), "/problem_resources/$id/statement.pdf", $data['pdf_data']);
$data['statement'] = "<div data-pdf data-src=\"/problem/$id/resources/statement.pdf\"></div>\n" . $data['statement'];
}
DB::insert([
"insert into problems_contents",
"(id, remote_content, statement, statement_md)",
"values",
DB::tuple([$id, HTML::purifier(['a' => ['target' => 'Enum#_blank']])->purify($data['statement']), '', ''])
]);
DB::insert([
"insert into problems_tags",
"(problem_id, tag)",
"values",
DB::tuple([$id, $remote_provider['name']]),
]);
UOJRemoteProblem::downloadImagesInRemoteContent(strval($id));
redirectTo("/problem/{$id}");
die();
};
$new_remote_problem_form->runAtServer();
?>
<?php echoUOJPageHeader('导入远程题库') ?>
<h1>导入远程题库</h1>
<div class="row">
<div class="col-md-9">
<div class="card">
<div class="card-body">
<div class="row">
<div class="col-md-6">
<?php $new_remote_problem_form->printHTML() ?>
</div>
<div class="col-md-6 mt-3 mt-md-0">
<h4>使用帮助</h4>
<ul>
<li>
<p>目前支持导入以下题库的题目作为远端评测题:</p>
<ul class="mb-3">
<li><a href="https://codeforces.com/problemset">Codeforces</a></li>
<li><a href="https://codeforces.com/gyms">Codeforces::Gym</a>(题号前加 <code>GYM</code>)</li>
<li><a href="https://atcoder.jp/contests/archive">AtCoder</a></li>
<li><a href="https://uoj.ac/problems">UniversalOJ</a></li>
<li><a href="https://loj.ac/p">LibreOJ</a></li>
<li><a href="https://www.luogu.com.cn/problem/list">洛谷</a></li>
</ul>
</li>
<li>在导入题目前请先搜索题库中是否已经存在相应题目,避免重复添加。</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="col-md-3">
<?php uojIncludeView('sidebar') ?>
</div>
</div>
<?php echoUOJPageFooter() ?>