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 '<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 '</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 '</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 class="text-center">' . ClickZans::getCntBlock($blog->info['zan']) . '</td>';
echo '</tr>';

View File

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

View File

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

View File

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

View File

@ -41,7 +41,7 @@ $friend_links = DB::selectAll([
?>
<tr>
<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>
</tr>
<?php endforeach ?>

View File

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

View File

@ -108,7 +108,7 @@ if (isSuperUser(Auth::user())) {
]);
foreach ($res as $row) {
$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>

View File

@ -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'] . ' 的评论里提到你:<a href="' . $uri . '">点击此处查看</a>';
@ -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'] . ' 的评论里提到你:<a href="' . $uri . '">点击此处查看</a>';
@ -215,7 +215,7 @@ $comments_pag = new Paginator([
</div>
<div id="comment-body-<?= $comment['id'] ?>" class="comtbox flex-grow-1 ms-3">
<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>
<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'] ?>">
<td class="text-center text-primary">#<?= $submission['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['judge_time'] ?></td>
</tr>
@ -1472,7 +1472,7 @@ EOD;
</tr>
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 {

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');
}
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) {
$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 '<div class="card mb-3 table-responsive">';
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';
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 '<tr>';
echo '<td>' . $user['rank'] . '</td>';
echo '<td>' . getUserLink($user['username']) . '</td>';
echo '<td>' . UOJUser::getLink($user['username']) . '</td>';
echo "<td>";
echo $purifier->purify($parsedown->line($user['motto']));
echo "</td>";

View File

@ -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';
}

View File

@ -105,7 +105,7 @@ class HTML {
}
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) {
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]);
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 = '<p>' . $user_link . ' 您好:</p>';

View File

@ -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() {

View File

@ -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 '<tr>';
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>' . $user['ac_num'] . '</td>';
echo '</tr>';

View File

@ -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']) {

View File

@ -20,7 +20,7 @@ $extra_text = $blog->info['is_hidden'] ? '<span class="text-muted">[已隐藏]</
</a>
</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) : ?>
<div class="card mb-4">
<div class="card-body">

View File

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