refactor(web): use UOJUser::getLink()

This commit is contained in:
Baoshuo Ren 2022-11-12 07:11:49 +08:00
parent aeba27d701
commit 1c94b98efc
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
19 changed files with 88 additions and 167 deletions

View File

@ -32,7 +32,7 @@ EOD,
echo '<tr>'; echo '<tr>';
echo '<td>' . $blog->getLink(['show_level' => true, 'show_new_tag' => true]) . '</td>'; echo '<td>' . $blog->getLink(['show_level' => true, 'show_new_tag' => true]) . '</td>';
echo '<td>' . getUserLink($blog->info['poster']) . '</td>'; echo '<td>' . UOJUser::getLink($blog->info['poster']) . '</td>';
echo '<td>' . $blog->info['post_time'] . '</td>'; echo '<td>' . $blog->info['post_time'] . '</td>';
echo '</tr>'; echo '</tr>';
}, },

View File

@ -57,7 +57,7 @@ Auth::check() || redirectToLogin();
echo ' <span class="badge text-bg-danger"><i class="bi bi-eye-slash-fill"></i> ', UOJLocale::get('hidden'), '</span> '; echo ' <span class="badge text-bg-danger"><i class="bi bi-eye-slash-fill"></i> ', UOJLocale::get('hidden'), '</span> ';
} }
echo '</td>'; echo '</td>';
echo '<td>' . getUserLink($blog->info['poster']) . '</td>'; echo '<td>' . UOJUser::getLink($blog->info['poster']) . '</td>';
echo '<td>' . $blog->info['post_time'] . '</td>'; echo '<td>' . $blog->info['post_time'] . '</td>';
echo '<td class="text-center">' . ClickZans::getCntBlock($blog->info['zan']) . '</td>'; echo '<td class="text-center">' . ClickZans::getCntBlock($blog->info['zan']) . '</td>';
echo '</tr>'; echo '</tr>';

View File

@ -40,7 +40,7 @@ $confirm_form->runAtServer();
</tr> </tr>
<tr> <tr>
<td class="text-center">参赛选手</td> <td class="text-center">参赛选手</td>
<td><?= getUserLink(Auth::id()) ?></td> <td><?= UOJUser::getLink(Auth::user()) ?></td>
</tr> </tr>
<tr> <tr>
<td class="text-center">开始时间</td> <td class="text-center">开始时间</td>

View File

@ -687,22 +687,38 @@ EOD,
"contest_id = {$contest['id']}", "contest_id = {$contest['id']}",
'ORDER BY username', 'ORDER BY username',
<<<EOD <<<EOD
<tr> <tr>
<th>用户名</th> <th>用户名</th>
<th style="width:6em">操作</th> <th style="width:6em">操作</th>
</tr> </tr>
EOD, EOD,
function ($row) { function ($row) {
echo '<tr>'; $user = UOJUser::query($row['username']);
echo '<td>', getUserLink($row['username']), '</td>';
echo '<td>'; echo HTML::tag_begin('tr');
echo '<form method="POST" target="_self" class="d-inline-block" onsubmit=\'return confirm("你确定要将 ', $row['username'], ' 从比赛管理员列表中移除吗?")\'>'; echo HTML::tag('td', [], UOJUser::getLink($user));
echo '<input type="hidden" name="_token" value="', crsf_token(), '">'; echo HTML::tag('td', [], [
echo '<input type="hidden" name="username" value="', $row['username'], '">'; HTML::tag('form', [
echo '<button type="submit" class="btn btn-link text-danger text-decoration-none p-0" name="submit-remove_manager" value="remove_manager">移除</button>'; 'method' => 'POST',
echo '</form>'; 'target' => '_self',
echo '</td>'; 'class' => 'd-inline-block',
echo '</tr>'; '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, 'echo_full' => true,

View File

@ -197,7 +197,7 @@ echoLongTable(
echo '<tr>'; echo '<tr>';
echo '<td>' . $num . '</td>'; echo '<td>' . $num . '</td>';
echo '<td>' . getUserLink($user['username']) . '</td>'; echo '<td>' . UOJUser::getLink($user) . '</td>';
if ($show_ip) { if ($show_ip) {
echo '<td>' . $user['remote_addr'] . '</td>'; echo '<td>' . $user['remote_addr'] . '</td>';
echo '<td>' . $user['http_x_forwarded_for'] . '</td>'; echo '<td>' . $user['http_x_forwarded_for'] . '</td>';

View File

@ -41,7 +41,7 @@ $friend_links = DB::selectAll([
?> ?>
<tr> <tr>
<td><?= $blog->getLink(['show_new_tag' => true]) ?></td> <td><?= $blog->getLink(['show_new_tag' => true]) ?></td>
<td>by <?= getUserLink($blog->info['poster']) ?></td> <td>by <?= UOJUser::getLink($blog->info['poster']) ?></td>
<td><small><?= $blog->info['post_time'] ?></small></td> <td><small><?= $blog->info['post_time'] ?></small></td>
</tr> </tr>
<?php endforeach ?> <?php endforeach ?>

View File

@ -301,21 +301,10 @@ EOD);
<div class="tab-pane active" id="problems"> <div class="tab-pane active" id="problems">
<?php <?php
echoLongTable( echoLongTable(
[ ['problem_id'],
'problems.id as id', "lists_problems",
'problems.title as title', ["list_id" => UOJList::info('id')],
'problems.is_hidden as is_hidden', "order by problem_id asc",
],
[
"problems",
"inner join lists_problems",
"on", [
"lists_problems.list_id" => UOJList::info('id'),
"lists_problems.problem_id" => DB::raw("problems.id")
]
],
"1",
"order by id asc",
<<<EOD <<<EOD
<tr> <tr>
<th class="text-center" style="width:5em">ID</th> <th class="text-center" style="width:5em">ID</th>
@ -324,23 +313,31 @@ EOD);
</tr> </tr>
EOD, EOD,
function ($row) { 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 HTML::tag_begin('td');
echo getProblemLink($row); echo $problem->getLink();
if ($row['is_hidden']) { if ($problem->info['is_hidden']) {
echo ' <span class="badge text-bg-danger"><i class="bi bi-eye-slash-fill"></i> ', UOJLocale::get('hidden'), '</span> '; echo ' <span class="badge text-bg-danger"><i class="bi bi-eye-slash-fill"></i> ', UOJLocale::get('hidden'), '</span> ';
} }
echo HTML::tag_end('td'); echo HTML::tag_end('td');
echo HTML::tag_begin('td'); echo HTML::tag('td', [], HTML::tag('form', [
echo '<form target="_self" method="POST" class="d-inline-block" onsubmit=\'return confirm("你确定要将题目 #', $row['id'], ' 从题单中移除吗?")\'>'; 'target' => '_self',
echo '<input type="hidden" name="_token" value="', crsf_token(), '">'; 'method' => 'POST',
echo '<input type="hidden" name="problem_id" value="', $row['id'], '">'; 'class' => 'd-inline-block',
echo '<button class="btn btn-link text-danger text-decoration-none p-0" name="submit-remove_problem" value="remove_problem">移除</button>'; 'onsubmit' => "return confirm('你确定要将 {$problem->info['id']} 从题单中移除吗?');",
echo '</form>'; ], [
echo HTML::tag_end('td'); 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'); echo HTML::tag_end('tr');
}, },
[ [

View File

@ -108,7 +108,7 @@ if (isSuperUser(Auth::user())) {
]); ]);
foreach ($res as $row) { foreach ($res as $row) {
$row_id++; $row_id++;
echo '<tr>', '<td>', $row_id, '</td>', '<td>', getUserLink($row['username']), '</td>', '</tr>'; echo '<tr>', '<td>', $row_id, '</td>', '<td>', UOJUser::getLink($row['username']), '</td>', '</tr>';
} }
?> ?>
</tbody> </tbody>

View File

@ -62,7 +62,7 @@ $comment_form->handle = function () {
$page = floor($rank / 20) + 1; $page = floor($rank / 20) + 1;
$uri = getLongTablePageUri($page) . '#' . "comment-{$comment_id}"; $uri = getLongTablePageUri($page) . '#' . "comment-{$comment_id}";
$user_link = getUserLink($myUser['username']); $user_link = UOJUser::getLink(Auth::user());
foreach ($referrers as $referrer) { foreach ($referrers as $referrer) {
$content = $user_link . ' 在博客 ' . $blog['title'] . ' 的评论里提到你:<a href="' . $uri . '">点击此处查看</a>'; $content = $user_link . ' 在博客 ' . $blog['title'] . ' 的评论里提到你:<a href="' . $uri . '">点击此处查看</a>';
@ -142,7 +142,7 @@ $reply_form->handle = function (&$vdata) {
$page = floor($rank / 20) + 1; $page = floor($rank / 20) + 1;
$uri = getLongTablePageUri($page) . '#' . "comment-{$reply_id}"; $uri = getLongTablePageUri($page) . '#' . "comment-{$reply_id}";
$user_link = getUserLink($myUser['username']); $user_link = UOJUser::getLink(Auth::user());
foreach ($referrers as $referrer) { foreach ($referrers as $referrer) {
$content = $user_link . ' 在博客 ' . $blog['title'] . ' 的评论里提到你:<a href="' . $uri . '">点击此处查看</a>'; $content = $user_link . ' 在博客 ' . $blog['title'] . ' 的评论里提到你:<a href="' . $uri . '">点击此处查看</a>';
@ -215,7 +215,7 @@ $comments_pag = new Paginator([
</div> </div>
<div id="comment-body-<?= $comment['id'] ?>" class="comtbox flex-grow-1 ms-3"> <div id="comment-body-<?= $comment['id'] ?>" class="comtbox flex-grow-1 ms-3">
<div class="row"> <div class="row">
<div class="col-sm-6"><?= getUserLink($poster['username']) ?></div> <div class="col-sm-6"><?= UOJUser::getLink($poster['username']) ?></div>
<div class="col-sm-6 text-end"><?= ClickZans::getBlock('BC', $comment['id'], $comment['zan']) ?></div> <div class="col-sm-6 text-end"><?= ClickZans::getBlock('BC', $comment['id'], $comment['zan']) ?></div>
</div> </div>
<div class="comtbox1"><?= $comment['content'] ?></div> <div class="comtbox1"><?= $comment['content'] ?></div>

View File

@ -1387,7 +1387,7 @@ EOD,
<tr style="cursor: pointer" data-bs-toggle="collapse" data-bs-target="#custom_test__<?= $submission['id'] ?>"> <tr style="cursor: pointer" data-bs-toggle="collapse" data-bs-target="#custom_test__<?= $submission['id'] ?>">
<td class="text-center text-primary">#<?= $submission['id'] ?></td> <td class="text-center text-primary">#<?= $submission['id'] ?></td>
<td class="text-center">#<?= $submission['problem_id'] ?></td> <td class="text-center">#<?= $submission['problem_id'] ?></td>
<td><?= getUserLink($submission['submitter']) ?></td> <td><?= UOJUser::getLink($submission['submitter']) ?></td>
<td><?= $submission['submit_time'] ?></td> <td><?= $submission['submit_time'] ?></td>
<td><?= $submission['judge_time'] ?></td> <td><?= $submission['judge_time'] ?></td>
</tr> </tr>
@ -1472,7 +1472,7 @@ EOD;
</tr> </tr>
EOD, EOD,
function ($row) { function ($row) {
$user_link = getUserLink($row['uploader']); $user_link = UOJUser::getLink($row['uploader']);
if ($row['size'] < 1024 * 512) { if ($row['size'] < 1024 * 512) {
$size = strval(round($row['size'] * 1.0 / 1024, 1)) . ' KB'; $size = strval(round($row['size'] * 1.0 / 1024, 1)) . ' KB';
} else { } else {

View File

@ -98,63 +98,6 @@ function become403Page($message = '访问被拒绝,您可能需要适当的权
becomeMsgPage('<div class="text-center"><div style="font-size:150px">403</div><p>' . $message . '</p></div>', '403'); becomeMsgPage('<div class="text-center"><div style="font-size:150px">403</div><p>' . $message . '</p></div>', '403');
} }
function getUserLink($username) {
if (validateUsername($username) && ($user = UOJUser::query($username)) && $user['usergroup'] != 'B') {
$realname = $user['realname'];
if ($realname == "") {
return '<span class="uoj-username">' . $username . '</span>';
} else {
return '<span class="uoj-username" data-realname="' . HTML::escape($realname) . '">' . $username . '</span>';
}
} else {
$esc_username = HTML::escape($username);
return '<span>' . $esc_username . '</span>';
}
}
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 = '<a ';
if (isset($REQUIRE_LIB['bootstrap5'])) {
$result .= ' class="text-decoration-none" ';
}
$result .= ' href="/problem/' . $problem['id'] . '">' . $problem_title . '</a>';
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 = '<a class="text-decoration-none" href="/contest/' . $contest_id . '/problem/' . $problem['id'] . '">' . $problem_title . '</a>';
return $result;
}
function getLongTablePageRawUri($page) { function getLongTablePageRawUri($page) {
$path = strtok(UOJContext::requestURI(), '?'); $path = strtok(UOJContext::requestURI(), '?');
$query_string = strtok('?'); $query_string = strtok('?');
@ -286,7 +229,6 @@ function echoSubmission($submission, $config, $viewer) {
$usubm->echoStatusTableRow($config, $viewer); $usubm->echoStatusTableRow($config, $viewer);
} }
function echoSubmissionsListOnlyOne($submission, $config, $user) { function echoSubmissionsListOnlyOne($submission, $config, $user) {
echo '<div class="card mb-3 table-responsive">'; echo '<div class="card mb-3 table-responsive">';
echo '<table class="table text-center uoj-table mb-0">'; echo '<table class="table text-center uoj-table mb-0">';
@ -381,27 +323,15 @@ function echoSubmissionsList($cond, $tail, $config, $user) {
$table_name = isset($config['table_name']) ? $config['table_name'] : 'submissions'; $table_name = isset($config['table_name']) ? $config['table_name'] : 'submissions';
if (!isProblemManager($user)) { $cond = $cond === '1' ? [] : [DB::conds($cond)];
if ($user != null) { $cond[] = UOJSubmission::sqlForUserCanView($user, $config['problem']);
$permission_cond = DB::lor([ if ($config['problem']) {
"submissions.is_hidden" => false, $cond[] = ['submissions.problem_id', '=', $config['problem']->info['id']];
"submissions.submitter" => $user['username'], }
DB::land([ if (count($cond) == 1) {
"submissions.is_hidden" => true, $cond = $cond[0];
DB::lor([ } else {
"submissions.problem_id in (select problem_id from problems_permissions where username = '{$user['username']}')", $cond = DB::land($cond);
"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;
}
} }
$table_config = isset($config['table_config']) ? $config['table_config'] : null; $table_config = isset($config['table_config']) ? $config['table_config'] : null;
@ -909,10 +839,10 @@ function echoHackDetails($hack_details, $name) {
} }
function echoHack($hack, $config, $viewer) { function echoHack($hack, $config, $viewer) {
$uhack = new UOJHack($hack); $uhack = new UOJHack($hack);
$uhack->setProblem(); $uhack->setProblem();
$uhack->setSubmission(); $uhack->setSubmission();
$uhack->echoStatusTableRow($config, $viewer); $uhack->echoStatusTableRow($config, $viewer);
} }
function echoHackListOnlyOne($hack, $config, $user) { function echoHackListOnlyOne($hack, $config, $user) {
@ -1106,7 +1036,7 @@ function echoRanklist($config = []) {
echo '<tr>'; echo '<tr>';
echo '<td>' . $user['rank'] . '</td>'; echo '<td>' . $user['rank'] . '</td>';
echo '<td>' . getUserLink($user['username']) . '</td>'; echo '<td>' . UOJUser::getLink($user['username']) . '</td>';
echo "<td>"; echo "<td>";
echo $purifier->purify($parsedown->line($user['motto'])); echo $purifier->purify($parsedown->line($user['motto']));
echo "</td>"; echo "</td>";

View File

@ -171,28 +171,6 @@ function hasUserType($user, $type) {
return in_array($type, $usertype); 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) { function isSuperUser($user) {
return $user != null && $user['usergroup'] == 'S'; return $user != null && $user['usergroup'] == 'S';
} }

View File

@ -105,7 +105,7 @@ class HTML {
} }
public static function hiddenToken() { public static function hiddenToken() {
return '<input type="hidden" name="_token" value="' . crsf_token() . '" />'; return HTML::empty_tag('input', ['type' => 'hidden', 'name' => '_token', 'value' => crsf_token()]);
} }
public static function div_vinput($name, $type, $label_text, $default_value) { public static function div_vinput($name, $type, $label_text, $default_value) {
return '<div id="' . "div-$name" . '" class="mb-3">' return '<div id="' . "div-$name" . '" class="mb-3">'

View File

@ -135,7 +135,7 @@ class UOJContest {
calcStandings($contest, $data, $score, $standings, ['update_contests_submissions' => true]); calcStandings($contest, $data, $score, $standings, ['update_contests_submissions' => true]);
for ($i = 0; $i < count($standings); $i++) { 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 ? ',请继续保持。' : ',请继续努力!'; $tail = $standings[$i][0] == $total_score ? ',请继续保持。' : ',请继续努力!';
$content = '<p>' . $user_link . ' 您好:</p>'; $content = '<p>' . $user_link . ' 您好:</p>';

View File

@ -95,7 +95,7 @@ class UOJProblem {
} }
public function getUploaderLink() { public function getUploaderLink() {
return getUserLink($this->info['uploader'] ?: "root"); return UOJUser::getLink($this->info['uploader'] ?: "root");
} }
public function findInContests() { public function findInContests() {

View File

@ -43,7 +43,7 @@ class UOJRanklist {
$user['rank'] = $rank; $user['rank'] = $rank;
$userpro = HTML::url('/user/' . $user['username']); $userpro = HTML::url('/user/' . $user['username']);
$userlink = getUserLink($user['username']); $userlink = UOJUser::getLink($user['username']);
$asrc = HTML::avatar_addr($user, 100); $asrc = HTML::avatar_addr($user, 100);
$esc_motto = $purifier->purify($parsedown->line($user['motto'])); $esc_motto = $purifier->purify($parsedown->line($user['motto']));
$solved_text = UOJLocale::get('solved'); $solved_text = UOJLocale::get('solved');
@ -160,7 +160,7 @@ class UOJRanklist {
echo '<tr>'; echo '<tr>';
echo '<td>' . $user['rank'] . '</td>'; echo '<td>' . $user['rank'] . '</td>';
echo '<td>' . getUserLink($user['username']) . '</td>'; echo '<td>' . UOJUser::getLink($user['username']) . '</td>';
echo '<td>' . $purifier->purify($parsedown->line($user['motto'])) . '</td>'; echo '<td>' . $purifier->purify($parsedown->line($user['motto'])) . '</td>';
echo '<td>' . $user['ac_num'] . '</td>'; echo '<td>' . $user['ac_num'] . '</td>';
echo '</tr>'; echo '</tr>';

View File

@ -233,7 +233,7 @@ trait UOJSubmissionLikeTrait {
case 'submitter': case 'submitter':
case 'owner': case 'owner':
case 'hacker': case 'hacker':
echo getUserLink($this->info[$name]); echo UOJUser::getLink($this->info[$name]);
break; break;
case 'used_time': case 'used_time':
if ($cfg['show_actual_score']) { if ($cfg['show_actual_score']) {

View File

@ -20,7 +20,7 @@ $extra_text = $blog->info['is_hidden'] ? '<span class="text-muted">[已隐藏]</
</a> </a>
</h1> </h1>
<div><?= $blog->info['post_time'] ?> <strong>By</strong> <?= getUserLink($blog->info['poster']) ?> (<strong>博客 ID: </strong> <?= $blog->info['id'] ?>)</div> <div><?= $blog->info['post_time'] ?> <strong>By</strong> <?= UOJUser::getLink($blog->info['poster']) ?> (<strong>博客 ID: </strong> <?= $blog->info['id'] ?>)</div>
<?php if (!$show_title_only) : ?> <?php if (!$show_title_only) : ?>
<div class="card mb-4"> <div class="card mb-4">
<div class="card-body"> <div class="card-body">

View File

@ -26,7 +26,7 @@ if (!isset($can_reply)) {
<tr><td colspan="233"><?= UOJLocale::get('none') ?></td></tr> <tr><td colspan="233"><?= UOJLocale::get('none') ?></td></tr>
<?php else: foreach ($pag->get() as $question): ?> <?php else: foreach ($pag->get() as $question): ?>
<tr> <tr>
<td><?= getUserLink($question['username']) ?></td> <td><?= UOJUser::getLink($question['username']) ?></td>
<td class="small"><?= $question['post_time'] ?></td> <td class="small"><?= $question['post_time'] ?></td>
<td style="text-align: left" class="question" data-qid="<?=$question['id']?>"> <td style="text-align: left" class="question" data-qid="<?=$question['id']?>">
<div class="question-content uoj-readmore"><?= HTML::escape($question['question']) ?></div> <div class="question-content uoj-readmore"><?= HTML::escape($question['question']) ?></div>