From 1c94b98efc7c1a1e3e19ca61e2039ebd6b2aebaf Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sat, 12 Nov 2022 07:11:49 +0800 Subject: [PATCH] refactor(web): use `UOJUser::getLink()` --- web/app/controllers/announcements.php | 2 +- web/app/controllers/blogs.php | 2 +- web/app/controllers/contest_confirmation.php | 2 +- web/app/controllers/contest_manage.php | 46 ++++++--- web/app/controllers/contest_members.php | 2 +- web/app/controllers/index.php | 2 +- web/app/controllers/list_manage.php | 51 +++++----- .../controllers/problem_managers_manage.php | 2 +- web/app/controllers/subdomain/blog/blog.php | 6 +- web/app/controllers/super_manage.php | 4 +- web/app/libs/uoj-html-lib.php | 98 +++---------------- web/app/libs/uoj-utility-lib.php | 22 ----- web/app/models/HTML.php | 2 +- web/app/models/UOJContest.php | 2 +- web/app/models/UOJProblem.php | 2 +- web/app/models/UOJRanklist.php | 4 +- web/app/models/UOJSubmissionLikeTrait.php | 2 +- web/app/views/blog-preview.php | 2 +- web/app/views/contest-question-table.php | 2 +- 19 files changed, 88 insertions(+), 167 deletions(-) diff --git a/web/app/controllers/announcements.php b/web/app/controllers/announcements.php index 3b836f3..ecd2935 100644 --- a/web/app/controllers/announcements.php +++ b/web/app/controllers/announcements.php @@ -32,7 +32,7 @@ EOD, echo ''; echo '' . $blog->getLink(['show_level' => true, 'show_new_tag' => true]) . ''; - echo '' . getUserLink($blog->info['poster']) . ''; + echo '' . UOJUser::getLink($blog->info['poster']) . ''; echo '' . $blog->info['post_time'] . ''; echo ''; }, diff --git a/web/app/controllers/blogs.php b/web/app/controllers/blogs.php index d409d89..d410520 100644 --- a/web/app/controllers/blogs.php +++ b/web/app/controllers/blogs.php @@ -57,7 +57,7 @@ Auth::check() || redirectToLogin(); echo ' ', UOJLocale::get('hidden'), ' '; } echo ''; - echo '' . getUserLink($blog->info['poster']) . ''; + echo '' . UOJUser::getLink($blog->info['poster']) . ''; echo '' . $blog->info['post_time'] . ''; echo '' . ClickZans::getCntBlock($blog->info['zan']) . ''; echo ''; diff --git a/web/app/controllers/contest_confirmation.php b/web/app/controllers/contest_confirmation.php index 742c291..388e190 100644 --- a/web/app/controllers/contest_confirmation.php +++ b/web/app/controllers/contest_confirmation.php @@ -40,7 +40,7 @@ $confirm_form->runAtServer(); 参赛选手 - + 开始时间 diff --git a/web/app/controllers/contest_manage.php b/web/app/controllers/contest_manage.php index d31f084..4ede4b2 100644 --- a/web/app/controllers/contest_manage.php +++ b/web/app/controllers/contest_manage.php @@ -687,22 +687,38 @@ EOD, "contest_id = {$contest['id']}", 'ORDER BY username', << - 用户名 - 操作 - - EOD, + + 用户名 + 操作 + + EOD, function ($row) { - echo ''; - echo '', getUserLink($row['username']), ''; - echo ''; - echo '
'; - echo ''; - echo ''; - echo ''; - echo '
'; - echo ''; - echo ''; + $user = UOJUser::query($row['username']); + + echo HTML::tag_begin('tr'); + echo HTML::tag('td', [], UOJUser::getLink($user)); + echo HTML::tag('td', [], [ + HTML::tag('form', [ + 'method' => 'POST', + 'target' => '_self', + 'class' => 'd-inline-block', + 'onsubmit' => "return confirm('你确定要将 {$user['username']} 从比赛管理员列表中移除吗?');", + ], [ + HTML::hiddenToken(), + HTML::empty_tag('input', [ + 'type' => 'hidden', + 'name' => 'username', + 'value' => $user['username'], + ]), + HTML::tag('button', [ + 'type' => 'submit', + 'class' => 'btn btn-link text-danger text-decoration-none p-0', + 'name' => 'submit-remove_manager', + 'value' => 'remove_manager', + ], '移除'), + ]), + ]); + echo HTML::tag_end('tr'); }, [ 'echo_full' => true, diff --git a/web/app/controllers/contest_members.php b/web/app/controllers/contest_members.php index b813fb9..2fada0c 100644 --- a/web/app/controllers/contest_members.php +++ b/web/app/controllers/contest_members.php @@ -197,7 +197,7 @@ echoLongTable( echo ''; echo '' . $num . ''; - echo '' . getUserLink($user['username']) . ''; + echo '' . UOJUser::getLink($user) . ''; if ($show_ip) { echo '' . $user['remote_addr'] . ''; echo '' . $user['http_x_forwarded_for'] . ''; diff --git a/web/app/controllers/index.php b/web/app/controllers/index.php index 65b4e50..10d91f6 100644 --- a/web/app/controllers/index.php +++ b/web/app/controllers/index.php @@ -41,7 +41,7 @@ $friend_links = DB::selectAll([ ?> getLink(['show_new_tag' => true]) ?> - by info['poster']) ?> + by info['poster']) ?> info['post_time'] ?> diff --git a/web/app/controllers/list_manage.php b/web/app/controllers/list_manage.php index 88e400b..5e14415 100644 --- a/web/app/controllers/list_manage.php +++ b/web/app/controllers/list_manage.php @@ -301,21 +301,10 @@ EOD);
UOJList::info('id'), - "lists_problems.problem_id" => DB::raw("problems.id") - ] - ], - "1", - "order by id asc", + ['problem_id'], + "lists_problems", + ["list_id" => UOJList::info('id')], + "order by problem_id asc", << ID @@ -324,23 +313,31 @@ EOD); EOD, function ($row) { - echo HTML::tag_begin('tr'); + $problem = UOJProblem::query($row['problem_id']); - echo HTML::tag('td', ['class' => 'text-center'], $row['id']); + echo HTML::tag_begin('tr'); + echo HTML::tag('td', ['class' => 'text-center'], $problem->info['id']); echo HTML::tag_begin('td'); - echo getProblemLink($row); - if ($row['is_hidden']) { + echo $problem->getLink(); + if ($problem->info['is_hidden']) { echo ' ', UOJLocale::get('hidden'), ' '; } echo HTML::tag_end('td'); - echo HTML::tag_begin('td'); - echo '
'; - echo ''; - echo ''; - echo ''; - echo '
'; - echo HTML::tag_end('td'); - + echo HTML::tag('td', [], HTML::tag('form', [ + 'target' => '_self', + 'method' => 'POST', + 'class' => 'd-inline-block', + 'onsubmit' => "return confirm('你确定要将 {$problem->info['id']} 从题单中移除吗?');", + ], [ + HTML::hiddenToken(), + HTML::empty_tag('input', ['type' => 'hidden', 'name' => 'problem_id', 'value' => $problem->info['id']]), + html::tag('button', [ + 'type' => 'submit', + 'class' => 'btn btn-link text-danger text-decoration-none p-0', + 'name' => 'submit-remove_problem', + 'value' => 'remove_problem', + ], '移除'), + ])); echo HTML::tag_end('tr'); }, [ diff --git a/web/app/controllers/problem_managers_manage.php b/web/app/controllers/problem_managers_manage.php index 6620d2b..2f1798b 100644 --- a/web/app/controllers/problem_managers_manage.php +++ b/web/app/controllers/problem_managers_manage.php @@ -108,7 +108,7 @@ if (isSuperUser(Auth::user())) { ]); foreach ($res as $row) { $row_id++; - echo '', '', $row_id, '', '', getUserLink($row['username']), '', ''; + echo '', '', $row_id, '', '', UOJUser::getLink($row['username']), '', ''; } ?> diff --git a/web/app/controllers/subdomain/blog/blog.php b/web/app/controllers/subdomain/blog/blog.php index 7c44f43..88c3ffe 100644 --- a/web/app/controllers/subdomain/blog/blog.php +++ b/web/app/controllers/subdomain/blog/blog.php @@ -62,7 +62,7 @@ $comment_form->handle = function () { $page = floor($rank / 20) + 1; $uri = getLongTablePageUri($page) . '#' . "comment-{$comment_id}"; - $user_link = getUserLink($myUser['username']); + $user_link = UOJUser::getLink(Auth::user()); foreach ($referrers as $referrer) { $content = $user_link . ' 在博客 ' . $blog['title'] . ' 的评论里提到你:点击此处查看'; @@ -142,7 +142,7 @@ $reply_form->handle = function (&$vdata) { $page = floor($rank / 20) + 1; $uri = getLongTablePageUri($page) . '#' . "comment-{$reply_id}"; - $user_link = getUserLink($myUser['username']); + $user_link = UOJUser::getLink(Auth::user()); foreach ($referrers as $referrer) { $content = $user_link . ' 在博客 ' . $blog['title'] . ' 的评论里提到你:点击此处查看'; @@ -215,7 +215,7 @@ $comments_pag = new Paginator([
-
+
diff --git a/web/app/controllers/super_manage.php b/web/app/controllers/super_manage.php index c9cf14a..092e5d5 100644 --- a/web/app/controllers/super_manage.php +++ b/web/app/controllers/super_manage.php @@ -1387,7 +1387,7 @@ EOD, # # - + @@ -1472,7 +1472,7 @@ EOD; EOD, function ($row) { - $user_link = getUserLink($row['uploader']); + $user_link = UOJUser::getLink($row['uploader']); if ($row['size'] < 1024 * 512) { $size = strval(round($row['size'] * 1.0 / 1024, 1)) . ' KB'; } else { diff --git a/web/app/libs/uoj-html-lib.php b/web/app/libs/uoj-html-lib.php index 81d0c32..1eb63f3 100644 --- a/web/app/libs/uoj-html-lib.php +++ b/web/app/libs/uoj-html-lib.php @@ -98,63 +98,6 @@ function become403Page($message = '访问被拒绝,您可能需要适当的权 becomeMsgPage('
403

' . $message . '

', '403'); } -function getUserLink($username) { - if (validateUsername($username) && ($user = UOJUser::query($username)) && $user['usergroup'] != 'B') { - $realname = $user['realname']; - - if ($realname == "") { - return '' . $username . ''; - } else { - return '' . $username . ''; - } - } else { - $esc_username = HTML::escape($username); - return '' . $esc_username . ''; - } -} - -function getUserName($username, $realname = null) { - if ($realname == null) { - if (validateUsername($username) && ($user = UOJUser::query($username))) { - $realname = $user['realname']; - } - } - - if ($realname == "") { - return "$username"; - } else { - return "$username ($realname)"; - } -} - - -function getProblemLink($problem, $problem_title = '!title_only') { - global $REQUIRE_LIB; - - if ($problem_title == '!title_only') { - $problem_title = $problem['title']; - } elseif ($problem_title == '!id_and_title') { - $problem_title = "#${problem['id']}. ${problem['title']}"; - } - $result = '' . $problem_title . ''; - - return $result; -} -function getContestProblemLink($problem, $contest_id, $problem_title = '!title_only') { - if ($problem_title == '!title_only') { - $problem_title = $problem['title']; - } elseif ($problem_title == '!id_and_title') { - $problem_title = "#{$problem['id']}. {$problem['title']}"; - } - $result = '' . $problem_title . ''; - - return $result; -} - function getLongTablePageRawUri($page) { $path = strtok(UOJContext::requestURI(), '?'); $query_string = strtok('?'); @@ -286,7 +229,6 @@ function echoSubmission($submission, $config, $viewer) { $usubm->echoStatusTableRow($config, $viewer); } - function echoSubmissionsListOnlyOne($submission, $config, $user) { echo '
'; echo ''; @@ -381,27 +323,15 @@ function echoSubmissionsList($cond, $tail, $config, $user) { $table_name = isset($config['table_name']) ? $config['table_name'] : 'submissions'; - if (!isProblemManager($user)) { - if ($user != null) { - $permission_cond = DB::lor([ - "submissions.is_hidden" => false, - "submissions.submitter" => $user['username'], - DB::land([ - "submissions.is_hidden" => true, - DB::lor([ - "submissions.problem_id in (select problem_id from problems_permissions where username = '{$user['username']}')", - "submissions.problem_id in (select id from problems where uploader = '{$user['username']}')", - ]), - ]), - ]); - } else { - $permission_cond = ["submissions.is_hidden" => "false"]; - } - if ($cond !== '1') { - $cond = DB::land($cond, $permission_cond); - } else { - $cond = $permission_cond; - } + $cond = $cond === '1' ? [] : [DB::conds($cond)]; + $cond[] = UOJSubmission::sqlForUserCanView($user, $config['problem']); + if ($config['problem']) { + $cond[] = ['submissions.problem_id', '=', $config['problem']->info['id']]; + } + if (count($cond) == 1) { + $cond = $cond[0]; + } else { + $cond = DB::land($cond); } $table_config = isset($config['table_config']) ? $config['table_config'] : null; @@ -909,10 +839,10 @@ function echoHackDetails($hack_details, $name) { } function echoHack($hack, $config, $viewer) { - $uhack = new UOJHack($hack); - $uhack->setProblem(); - $uhack->setSubmission(); - $uhack->echoStatusTableRow($config, $viewer); + $uhack = new UOJHack($hack); + $uhack->setProblem(); + $uhack->setSubmission(); + $uhack->echoStatusTableRow($config, $viewer); } function echoHackListOnlyOne($hack, $config, $user) { @@ -1106,7 +1036,7 @@ function echoRanklist($config = []) { echo ''; echo ''; - echo ''; + echo ''; echo ""; diff --git a/web/app/libs/uoj-utility-lib.php b/web/app/libs/uoj-utility-lib.php index eb4073f..2aa54c4 100644 --- a/web/app/libs/uoj-utility-lib.php +++ b/web/app/libs/uoj-utility-lib.php @@ -171,28 +171,6 @@ function hasUserType($user, $type) { return in_array($type, $usertype); } -function isProblemUploader($user) { - if ($user == null) { - return false; - } - return hasUserType($user, 'problem_uploader'); -} -function isProblemManager($user) { - if ($user == null) { - return false; - } - if (isSuperUser($user)) { - return true; - } - return hasUserType($user, 'problem_manager'); -} -function isContestJudger($user) { - if ($user == null) { - return false; - } - return hasUserType($user, 'contest_judger'); -} - function isSuperUser($user) { return $user != null && $user['usergroup'] == 'S'; } diff --git a/web/app/models/HTML.php b/web/app/models/HTML.php index d8b03dc..1327db0 100644 --- a/web/app/models/HTML.php +++ b/web/app/models/HTML.php @@ -105,7 +105,7 @@ class HTML { } public static function hiddenToken() { - return ''; + return HTML::empty_tag('input', ['type' => 'hidden', 'name' => '_token', 'value' => crsf_token()]); } public static function div_vinput($name, $type, $label_text, $default_value) { return '
' diff --git a/web/app/models/UOJContest.php b/web/app/models/UOJContest.php index a452aaa..ab3186e 100644 --- a/web/app/models/UOJContest.php +++ b/web/app/models/UOJContest.php @@ -135,7 +135,7 @@ class UOJContest { calcStandings($contest, $data, $score, $standings, ['update_contests_submissions' => true]); for ($i = 0; $i < count($standings); $i++) { - $user_link = getUserLink($standings[$i][2][0]); + $user_link = UOJUser::getLink($standings[$i][2][0]); $tail = $standings[$i][0] == $total_score ? ',请继续保持。' : ',请继续努力!'; $content = '

' . $user_link . ' 您好:

'; diff --git a/web/app/models/UOJProblem.php b/web/app/models/UOJProblem.php index e827250..36574b7 100644 --- a/web/app/models/UOJProblem.php +++ b/web/app/models/UOJProblem.php @@ -95,7 +95,7 @@ class UOJProblem { } public function getUploaderLink() { - return getUserLink($this->info['uploader'] ?: "root"); + return UOJUser::getLink($this->info['uploader'] ?: "root"); } public function findInContests() { diff --git a/web/app/models/UOJRanklist.php b/web/app/models/UOJRanklist.php index a626bd4..2aadd7a 100644 --- a/web/app/models/UOJRanklist.php +++ b/web/app/models/UOJRanklist.php @@ -43,7 +43,7 @@ class UOJRanklist { $user['rank'] = $rank; $userpro = HTML::url('/user/' . $user['username']); - $userlink = getUserLink($user['username']); + $userlink = UOJUser::getLink($user['username']); $asrc = HTML::avatar_addr($user, 100); $esc_motto = $purifier->purify($parsedown->line($user['motto'])); $solved_text = UOJLocale::get('solved'); @@ -160,7 +160,7 @@ class UOJRanklist { echo '
'; echo ''; - echo ''; + echo ''; echo ''; echo ''; echo ''; diff --git a/web/app/models/UOJSubmissionLikeTrait.php b/web/app/models/UOJSubmissionLikeTrait.php index 0254469..75390ee 100644 --- a/web/app/models/UOJSubmissionLikeTrait.php +++ b/web/app/models/UOJSubmissionLikeTrait.php @@ -233,7 +233,7 @@ trait UOJSubmissionLikeTrait { case 'submitter': case 'owner': case 'hacker': - echo getUserLink($this->info[$name]); + echo UOJUser::getLink($this->info[$name]); break; case 'used_time': if ($cfg['show_actual_score']) { diff --git a/web/app/views/blog-preview.php b/web/app/views/blog-preview.php index 13316bc..d94f3ae 100644 --- a/web/app/views/blog-preview.php +++ b/web/app/views/blog-preview.php @@ -20,7 +20,7 @@ $extra_text = $blog->info['is_hidden'] ? '[已隐藏] -
info['post_time'] ?> By info['poster']) ?> (博客 ID: info['id'] ?>)
+
info['post_time'] ?> By info['poster']) ?> (博客 ID: info['id'] ?>)
diff --git a/web/app/views/contest-question-table.php b/web/app/views/contest-question-table.php index 926ab3e..c6a5d1f 100644 --- a/web/app/views/contest-question-table.php +++ b/web/app/views/contest-question-table.php @@ -26,7 +26,7 @@ if (!isset($can_reply)) {
get() as $question): ?> - +
' . $user['rank'] . '' . getUserLink($user['username']) . '' . UOJUser::getLink($user['username']) . '"; echo $purifier->purify($parsedown->line($user['motto'])); echo "
' . $user['rank'] . '' . getUserLink($user['username']) . '' . UOJUser::getLink($user['username']) . '' . $purifier->purify($parsedown->line($user['motto'])) . '' . $user['ac_num'] . '