S2OJ/web/app/controllers/subdomain/blog/blog.php

368 lines
12 KiB
PHP
Raw Normal View History

2016-07-18 16:39:37 +00:00
<?php
2022-11-06 02:26:21 +00:00
requireLib('mathjax');
requireLib('hljs');
requirePHPLib('form');
2022-03-17 04:00:03 +00:00
2022-11-11 23:10:34 +00:00
Auth::check() || redirectToLogin();
2022-11-06 02:26:21 +00:00
UOJBlog::init(UOJRequest::get('id')) || UOJResponse::page404();
UOJBlog::cur()->belongsToUserBlog() || UOJResponse::page404();
UOJBlog::cur()->userCanView(Auth::user()) || UOJResponse::page403();
$blog = UOJBlog::info();
2023-02-24 14:10:29 +00:00
$purifier = HTML::purifier();
$parsedown = HTML::parsedown([
'username_with_color' => true,
]);
2022-11-06 02:26:21 +00:00
function getCommentContentToDisplay($comment) {
2023-02-24 14:10:29 +00:00
global $purifier, $parsedown;
$rendered = $purifier->purify($parsedown->text($comment['content']));
if ($comment['is_hidden']) {
$esc_hide_reason = $comment['reason_to_hide'];
$res = <<<EOD
<div class="alert alert-warning d-flex align-items-center my-0" role="alert">
<div class="flex-shrink-0 me-3">
<i class="fs-4 bi bi-exclamation-triangle-fill"></i>
</div>
<div>
<div class="fw-bold mb-2">该评论被隐藏</div>
<div class="small">{$esc_hide_reason}</div>
</div>
</div>
EOD;
if (UOJUserBlog::userHasManagePermission(Auth::user())) {
$res .= <<<EOD
<div class="mt-2">{$rendered}</div>
EOD;
}
return $res;
2022-03-17 04:00:03 +00:00
}
2023-02-24 14:10:29 +00:00
return $rendered;
2022-11-06 02:26:21 +00:00
}
2023-02-01 12:39:24 +00:00
$comment_form = new UOJForm('comment');
2023-03-14 09:02:44 +00:00
$comment_form->addMarkdownEditor('comment', [
2023-02-01 12:39:24 +00:00
'label' => '内容',
2023-02-24 14:10:29 +00:00
'help' => '评论支持 Markdown 语法。可以用 <code>@mike</code> 来提到 <code>mike</code> 这个用户,<code>mike</code> 会被高亮显示。如果你真的想打 <code>@</code> 这个字符,请用 <code>@@</code>。',
2023-02-01 12:39:24 +00:00
'validator_php' => function ($comment) {
2022-11-21 12:14:47 +00:00
if (!Auth::check()) {
2022-11-06 02:26:21 +00:00
return '请先登录';
}
if (!$comment) {
return '评论不能为空';
}
2023-02-28 12:00:52 +00:00
if (strlen($comment) > 2000) {
return '不能超过 2000 个字节';
2022-11-06 02:26:21 +00:00
}
return '';
},
2023-02-01 12:39:24 +00:00
]);
2023-03-14 09:02:44 +00:00
$comment_form->config['ctrl_enter_submit'] = true;
2022-11-06 02:26:21 +00:00
$comment_form->handle = function () {
2023-02-01 12:39:24 +00:00
global $blog, $comment_form;
2023-02-28 12:13:59 +00:00
$comment = $_POST['comment'];
2022-11-06 02:26:21 +00:00
2023-02-28 12:13:59 +00:00
list(, $referrers) = uojHandleAtSign($comment, "/post/{$blog['id']}");
2022-11-06 02:26:21 +00:00
DB::insert([
"insert into blogs_comments",
"(poster, blog_id, content, reply_id, post_time)",
"values", DB::tuple([Auth::id(), $blog['id'], $comment, 0, DB::now()])
]);
$comment_id = DB::insert_id();
$rank = DB::selectCount([
"select count(*) from blogs_comments",
"where", [
"blog_id" => $blog['id'],
"reply_id" => 0,
["id", "<", $comment_id]
]
]);
$page = floor($rank / 20) + 1;
2022-10-08 06:10:14 +00:00
2022-11-06 02:26:21 +00:00
$uri = getLongTablePageUri($page) . '#' . "comment-{$comment_id}";
2023-02-13 10:46:06 +00:00
$user_link = UOJUser::getLink(Auth::user(), ['color' => false]);
2022-11-06 02:26:21 +00:00
foreach ($referrers as $referrer) {
$content = $user_link . ' 在博客 ' . $blog['title'] . ' 的评论里提到你:<a href="' . $uri . '">点击此处查看</a>';
sendSystemMsg($referrer, '有人提到你', $content);
2016-07-18 16:39:37 +00:00
}
2022-11-06 02:26:21 +00:00
if ($blog['poster'] !== Auth::id()) {
$content = $user_link . ' 回复了您的博客 ' . $blog['title'] . ' <a href="' . $uri . '">点击此处查看</a>';
sendSystemMsg($blog['poster'], '博客新回复通知', $content);
2016-07-18 16:39:37 +00:00
}
2022-09-29 14:02:56 +00:00
2022-11-06 02:26:21 +00:00
UOJBlog::cur()->updateActiveTime();
2023-02-28 12:13:59 +00:00
$comment_form->succ_href = getLongTablePageRawUri($page) . "#comment-{$comment_id}";
2022-11-06 02:26:21 +00:00
};
2023-02-01 12:39:24 +00:00
$comment_form->config['ctrl_enter_submit'] = true;
2022-11-06 02:26:21 +00:00
$comment_form->runAtServer();
2023-02-01 12:39:24 +00:00
$reply_form = new UOJForm('reply');
2022-11-06 02:26:21 +00:00
$reply_form->addHidden(
'reply_id',
'0',
function ($reply_id, &$vdata) {
global $blog;
if (!validateUInt($reply_id) || $reply_id == 0) {
return '您要回复的对象不存在';
}
2022-11-06 02:26:21 +00:00
$comment = UOJBlogComment::query($reply_id);
2023-02-01 12:04:48 +00:00
if (!$comment || $comment->info['blog_id'] != $blog['id']) {
2022-11-06 02:26:21 +00:00
return '您要回复的对象不存在';
2016-07-18 16:39:37 +00:00
}
2022-11-06 02:26:21 +00:00
$vdata['parent'] = $comment;
return '';
},
null
);
2023-03-14 09:02:44 +00:00
$reply_form->addMarkdownEditor('reply_comment', [
2023-02-01 12:39:24 +00:00
'label' => '内容',
'validator_php' => function ($comment) {
2022-11-06 02:26:21 +00:00
if (!Auth::check()) {
return '请先登录';
2016-07-18 16:39:37 +00:00
}
2022-11-06 02:26:21 +00:00
if (!$comment) {
return '评论不能为空';
2016-07-18 16:39:37 +00:00
}
2023-02-28 12:00:52 +00:00
if (strlen($comment) > 1000) {
return '不能超过 1000 个字节';
2016-07-18 16:39:37 +00:00
}
2022-11-06 02:26:21 +00:00
return '';
},
2023-02-01 12:39:24 +00:00
]);
2022-11-06 02:26:21 +00:00
$reply_form->handle = function (&$vdata) {
2023-02-01 12:39:24 +00:00
global $blog, $reply_form;
2023-02-28 12:13:59 +00:00
$comment = $_POST['reply_comment'];
2022-11-06 02:26:21 +00:00
2023-02-28 12:13:59 +00:00
list(, $referrers) = uojHandleAtSign($comment, "/post/{$blog['id']}");
2022-11-06 02:26:21 +00:00
$reply_id = $_POST['reply_id'];
DB::insert([
"insert into blogs_comments",
"(poster, blog_id, content, reply_id, post_time)",
"values", DB::tuple([Auth::id(), $blog['id'], $comment, $reply_id, DB::now()])
]);
$comment_id = DB::insert_id();
$rank = DB::selectCount([
"select count(*) from blogs_comments",
"where", [
"blog_id" => $blog['id'],
"reply_id" => 0,
["id", "<", $reply_id]
]
]);
$page = floor($rank / 20) + 1;
$uri = getLongTablePageUri($page) . '#' . "comment-{$reply_id}";
2023-02-13 10:46:06 +00:00
$user_link = UOJUser::getLink(Auth::user(), ['color' => false]);
2022-11-06 02:26:21 +00:00
foreach ($referrers as $referrer) {
$content = $user_link . ' 在博客 ' . $blog['title'] . ' 的评论里提到你:<a href="' . $uri . '">点击此处查看</a>';
sendSystemMsg($referrer, '有人提到你', $content);
}
$parent = $vdata['parent'];
$notified = [];
if ($parent->info['poster'] !== Auth::id()) {
$notified[] = $parent->info['poster'];
$content = $user_link . ' 回复了您在博客 ' . $blog['title'] . ' 下的评论 <a href="' . $uri . '">点击此处查看</a>';
sendSystemMsg($parent->info['poster'], '评论新回复通知', $content);
}
if ($blog['poster'] !== Auth::id() && !in_array($blog['poster'], $notified)) {
$notified[] = $blog['poster'];
2023-02-13 12:29:32 +00:00
$content = $user_link . ' 回复了您的博客 ' . $blog['title'] . ' <a href="' . $uri . '">点击此处查看</a>';
2022-11-06 02:26:21 +00:00
sendSystemMsg($blog['poster'], '博客新回复通知', $content);
}
UOJBlog::cur()->updateActiveTime();
2023-02-28 12:13:59 +00:00
$reply_form->succ_href = getLongTablePageRawUri($page) . "#comment-{$comment_id}";
2022-11-06 02:26:21 +00:00
};
2023-02-01 12:39:24 +00:00
$reply_form->config['ctrl_enter_submit'] = true;
2022-11-06 02:26:21 +00:00
$reply_form->runAtServer();
2023-02-07 10:38:24 +00:00
if (UOJUserBlog::userHasManagePermission(Auth::user())) {
$hide_form = new UOJForm('hide');
$hide_form->addHidden('comment_hide_id', '', 'validateCommentId', null);
$hide_form->addSelect('comment_hide_type', [
'label' => '隐藏理由',
'options' => UOJBlogComment::HIDE_REASONS,
'default_value' => 'spam',
]);
$hide_form->addInput('comment_hide_reason', [
'div_class' => 'mt-3',
2023-02-23 10:28:20 +00:00
'label' => '自定义隐藏理由(当上方隐藏理由为自定义时有效)',
2023-02-07 10:38:24 +00:00
'default_value' => '该评论由于违反社区规定,已被管理员隐藏',
'validator_php' => 'validateString',
]);
$hide_form->handle = function (&$vdata) {
$comment = $vdata['comment_hide_id'];
2023-02-07 10:38:24 +00:00
if ($_POST['comment_hide_type'] == 'unhide') {
$reason = '';
} else if ($_POST['comment_hide_type'] == 'other') {
$reason = $_POST['comment_hide_reason'];
} else {
$reason = '该评论由于' . UOJBlogComment::HIDE_REASONS[$_POST['comment_hide_type']] . ',已被管理员隐藏';
}
$comment->hide($reason);
if ($_POST['comment_hide_type'] != 'unhide') {
sendSystemMsg(
$comment->info['poster'],
'评论隐藏通知',
2023-02-28 12:27:18 +00:00
"您为博客 " . UOJBlog::cur()->getLink() . " 回复的评论 “" . substr(HTML::stripTags($comment->info['content']), 0, 30) . "……” 已被管理员隐藏,隐藏原因为 “{$reason}”。"
);
}
2023-02-07 10:38:24 +00:00
};
$hide_form->runAtServer();
}
2022-11-06 02:26:21 +00:00
$comments_pag = new Paginator([
'col_names' => ['*'],
'table_name' => 'blogs_comments',
'cond' => 'blog_id = ' . $blog['id'] . ' and reply_id = 0',
'tail' => 'order by id asc',
'page_len' => 20
]);
?>
2023-02-07 10:38:24 +00:00
2016-07-18 16:39:37 +00:00
<?php echoUOJPageHeader(HTML::stripTags($blog['title']) . ' - 博客') ?>
2023-02-07 10:38:24 +00:00
<script>
var user_can_hide_comment = <?= json_encode(isset($hide_form)) ?>;
</script>
2022-11-06 02:26:21 +00:00
<?php UOJBlog::cur()->echoView(['show_title_only' => isset($_GET['page']) && $_GET['page'] != 1]) ?>
2023-02-07 10:38:24 +00:00
<?php if (isset($hide_form)) : ?>
<div class="modal fade" id="HideCommentModal" tabindex="-1" aria-labelledby="HideCommentModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title fs-5" id="HideCommentModalLabel">隐藏评论</h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="mb-3">
<div class="mb-2">原评论ID: <span id="span-comment_hide_id"></span></div>
<blockquote id="HideCommentModalOriginalComment" class="border-start border-3 ps-3 text-muted"></blockquote>
</div>
<hr>
<?php $hide_form->printHTML(); ?>
</div>
</div>
</div>
</div>
<?php endif ?>
2022-09-29 14:02:56 +00:00
<h2>
评论
<i class="bi bi-chat-fill"></i>
</h2>
2022-11-06 02:26:21 +00:00
2016-07-18 16:39:37 +00:00
<div class="list-group">
2022-11-06 02:26:21 +00:00
<?php if ($comments_pag->isEmpty()) : ?>
<div class="list-group-item text-muted">暂无评论</div>
<?php else : ?>
<?php foreach ($comments_pag->get() as $comment) :
$poster = UOJUser::query($comment['poster']);
$esc_email = HTML::escape($poster['email']);
$asrc = HTML::avatar_addr($poster, 80);
$replies = DB::selectAll([
2023-02-24 14:10:29 +00:00
"select id, poster, content, post_time, is_hidden, reason_to_hide, zan from blogs_comments",
2022-11-06 02:26:21 +00:00
"where", ["reply_id" => $comment['id']],
"order by id"
]);
foreach ($replies as $idx => $reply) {
2023-01-14 10:24:36 +00:00
$reply_user = UOJUser::query($reply['poster']);
2023-02-24 14:10:29 +00:00
$replies[$idx]['poster_avatar'] = HTML::avatar_addr($reply_user, 80);
2023-01-14 10:24:36 +00:00
$replies[$idx]['poster_realname'] = $reply_user['realname'];
$replies[$idx]['poster_username_color'] = UOJUser::getUserColor($reply_user);
2022-11-06 02:26:21 +00:00
$replies[$idx]['content'] = getCommentContentToDisplay($reply);
2023-02-24 14:10:29 +00:00
$replies[$idx]['click_zan_block'] = ClickZans::getBlock('BC', $reply['id'], $reply['zan']);
2022-11-06 02:26:21 +00:00
}
$replies_json = json_encode($replies);
2022-09-18 04:58:35 +00:00
?>
2022-11-06 02:26:21 +00:00
<div id="comment-<?= $comment['id'] ?>" class="list-group-item">
<div class="d-flex">
2023-02-24 14:10:29 +00:00
<div class="d-none d-sm-block mr-3 flex-shrink-0">
<a href="<?= HTML::url('/user/' . $poster['username']) ?>">
2023-02-07 11:36:48 +00:00
<img class="rounded uoj-user-avatar" src="<?= $asrc ?>" alt="Avatar of <?= $poster['username'] ?>" width="64" height="64" />
2022-09-29 14:02:56 +00:00
</a>
2022-11-06 02:26:21 +00:00
</div>
2023-02-01 13:30:36 +00:00
<div id="comment-body-<?= $comment['id'] ?>" class="flex-grow-1 ms-3">
<div class="row justify-content-between flex-wrap">
<div class="col-auto">
<?= UOJUser::getLink($poster['username']) ?>
</div>
<div class="col-auto">
<?= ClickZans::getBlock('BC', $comment['id'], $comment['zan']) ?>
</div>
2022-11-06 02:26:21 +00:00
</div>
2023-02-24 14:10:29 +00:00
<div class="comment-content markdown-body my-2" id="comment-content-<?= $comment['id'] ?>"><?= getCommentContentToDisplay($comment) ?></div>
2022-11-06 02:26:21 +00:00
<ul class="list-inline mb-0 text-end">
2023-02-01 13:30:36 +00:00
<li class="list-inline-item small text-muted">
<?= $comment['post_time'] ?>
2022-11-06 02:26:21 +00:00
</li>
2023-02-07 10:38:24 +00:00
<?php if (isset($hide_form)) : ?>
<li class="list-inline-item">
<a href="#" class="text-warning-emphasis text-decoration-none p-0 uoj-blog-hide-comment-btn" data-comment-id="<?= $comment['id'] ?>">
隐藏
</a>
</li>
<?php endif ?>
2023-02-01 13:30:36 +00:00
<li class="list-inline-item">
2023-02-07 10:38:24 +00:00
<a id="reply-to-<?= $comment['id'] ?>" href="#">
2022-11-06 02:26:21 +00:00
回复
</a>
</li>
</ul>
<?php if ($replies) : ?>
2023-02-24 14:10:29 +00:00
<div id="replies-<?= $comment['id'] ?>" class="rounded bg-secondary-subtle mt-2 border"></div>
2022-11-06 02:26:21 +00:00
<?php endif ?>
<script>
showCommentReplies('<?= $comment['id'] ?>', <?= $replies_json ?>);
</script>
2022-11-06 02:26:21 +00:00
</div>
</div>
2016-07-18 16:39:37 +00:00
</div>
2022-11-06 02:26:21 +00:00
<?php endforeach ?>
<?php endif ?>
2016-07-18 16:39:37 +00:00
</div>
<?= $comments_pag->pagination() ?>
<h3 class="mt-4">发表评论</h3>
2016-07-18 16:39:37 +00:00
<?php $comment_form->printHTML() ?>
<div id="div-form-reply" style="display:none">
<?php $reply_form->printHTML() ?>
</div>
2023-02-07 10:38:24 +00:00
<script>
$('.uoj-blog-hide-comment-btn').each(function() {
2023-02-23 10:28:20 +00:00
$(this).click(function(event) {
2023-02-07 10:38:24 +00:00
var comment_id = $(this).data('comment-id');
2023-02-24 14:10:29 +00:00
2023-02-23 10:28:20 +00:00
event.preventDefault();
2023-02-07 10:38:24 +00:00
toggleModalHideComment(comment_id, $('#comment-content-' + comment_id).html());
});
})
</script>
2016-07-18 16:39:37 +00:00
<?php echoUOJPageFooter() ?>