blog.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. requirePHPLib('form');
  3. if (!isset($_GET['id']) || !validateUInt($_GET['id']) || !($blog = queryBlog($_GET['id'])) || !UOJContext::isHis($blog)) {
  4. become404Page();
  5. }
  6. if ($blog['is_hidden'] && !UOJContext::hasBlogPermission()) {
  7. become403Page();
  8. }
  9. $comment_form = new UOJForm('comment');
  10. $comment_form->addVTextArea('comment', '内容', '',
  11. function($comment) {
  12. global $myUser;
  13. if ($myUser == null) {
  14. return '请先登录';
  15. }
  16. if (!$comment) {
  17. return '评论不能为空';
  18. }
  19. if (strlen($comment) > 1000) {
  20. return '不能超过1000个字节';
  21. }
  22. return '';
  23. },
  24. null
  25. );
  26. $comment_form->handle = function() {
  27. global $myUser, $blog, $comment_form;
  28. $comment = HTML::escape($_POST['comment']);
  29. list($comment, $referrers) = uojHandleAtSign($comment, "/post/{$blog['id']}");
  30. $esc_comment = DB::escape($comment);
  31. DB::insert("insert into blogs_comments (poster, blog_id, content, reply_id, post_time, zan) values ('{$myUser['username']}', '{$blog['id']}', '$esc_comment', 0, now(), 0)");
  32. $comment_id = DB::insert_id();
  33. $rank = DB::selectCount("select count(*) from blogs_comments where blog_id = {$blog['id']} and reply_id = 0 and id < {$comment_id}");
  34. $page = floor($rank / 20) + 1;
  35. $uri = getLongTablePageUri($page) . '#' . "comment-{$comment_id}";
  36. foreach ($referrers as $referrer) {
  37. $content = '有人在博客 ' . $blog['title'] . ' 的评论里提到你:<a href="' . $uri . '">点击此处查看</a>';
  38. sendSystemMsg($referrer, '有人提到你', $content);
  39. }
  40. if ($blog['poster'] !== $myUser['username']) {
  41. $content = '有人回复了您的博客 ' . $blog['title'] . ' :<a href="' . $uri . '">点击此处查看</a>';
  42. sendSystemMsg($blog['poster'], '博客新回复通知', $content);
  43. }
  44. $comment_form->succ_href = getLongTablePageRawUri($page);
  45. };
  46. $comment_form->ctrl_enter_submit = true;
  47. $comment_form->runAtServer();
  48. $reply_form = new UOJForm('reply');
  49. $reply_form->addHidden('reply_id', '0',
  50. function($reply_id, &$vdata) {
  51. global $blog;
  52. if (!validateUInt($reply_id) || $reply_id == 0) {
  53. return '您要回复的对象不存在';
  54. }
  55. $comment = queryBlogComment($reply_id);
  56. if (!$comment || $comment['blog_id'] != $blog['id']) {
  57. return '您要回复的对象不存在';
  58. }
  59. $vdata['parent'] = $comment;
  60. return '';
  61. },
  62. null
  63. );
  64. $reply_form->addVTextArea('reply_comment', '内容', '',
  65. function($comment) {
  66. global $myUser;
  67. if ($myUser == null) {
  68. return '请先登录';
  69. }
  70. if (!$comment) {
  71. return '评论不能为空';
  72. }
  73. if (strlen($comment) > 140) {
  74. return '不能超过140个字节';
  75. }
  76. return '';
  77. },
  78. null
  79. );
  80. $reply_form->handle = function(&$vdata) {
  81. global $myUser, $blog, $reply_form;
  82. $comment = HTML::escape($_POST['reply_comment']);
  83. list($comment, $referrers) = uojHandleAtSign($comment, "/post/{$blog['id']}");
  84. $reply_id = $_POST['reply_id'];
  85. $esc_comment = DB::escape($comment);
  86. DB::insert("insert into blogs_comments (poster, blog_id, content, reply_id, post_time, zan) values ('{$myUser['username']}', '{$blog['id']}', '$esc_comment', $reply_id, now(), 0)");
  87. $comment_id = DB::insert_id();
  88. $rank = DB::selectCount("select count(*) from blogs_comments where blog_id = {$blog['id']} and reply_id = 0 and id < {$reply_id}");
  89. $page = floor($rank / 20) + 1;
  90. $uri = getLongTablePageUri($page) . '#' . "comment-{$reply_id}";
  91. foreach ($referrers as $referrer) {
  92. $content = '有人在博客 ' . $blog['title'] . ' 的评论里提到你:<a href="' . $uri . '">点击此处查看</a>';
  93. sendSystemMsg($referrer, '有人提到你', $content);
  94. }
  95. $parent = $vdata['parent'];
  96. $notified = array();
  97. if ($parent['poster'] !== $myUser['username']) {
  98. $notified[] = $parent['poster'];
  99. $content = '有人回复了您在博客 ' . $blog['title'] . ' 下的评论 :<a href="' . $uri . '">点击此处查看</a>';
  100. sendSystemMsg($parent['poster'], '评论新回复通知', $content);
  101. }
  102. if ($blog['poster'] !== $myUser['username'] && !in_array($blog['poster'], $notified)) {
  103. $notified[] = $blog['poster'];
  104. $content = '有人回复了您的博客 ' . $blog['title'] . ' :<a href="' . $uri . '">点击此处查看</a>';
  105. sendSystemMsg($blog['poster'], '博客新回复通知', $content);
  106. }
  107. $reply_form->succ_href = getLongTablePageRawUri($page);
  108. };
  109. $reply_form->ctrl_enter_submit = true;
  110. $reply_form->runAtServer();
  111. $comments_pag = new Paginator(array(
  112. 'col_names' => array('*'),
  113. 'table_name' => 'blogs_comments',
  114. 'cond' => 'blog_id = ' . $blog['id'] . ' and reply_id = 0',
  115. 'tail' => 'order by id asc',
  116. 'page_len' => 20
  117. ));
  118. ?>
  119. <?php
  120. $REQUIRE_LIB['mathjax'] = '';
  121. $REQUIRE_LIB['hljs'] = '';
  122. ?>
  123. <?php echoUOJPageHeader(HTML::stripTags($blog['title']) . ' - 博客') ?>
  124. <?php echoBlog($blog, array('show_title_only' => isset($_GET['page']) && $_GET['page'] != 1)) ?>
  125. <h2>评论 <span class="glyphicon glyphicon-comment"></span></h2>
  126. <div class="list-group">
  127. <?php if ($comments_pag->isEmpty()): ?>
  128. <div class="list-group-item text-muted">暂无评论</div>
  129. <?php else: ?>
  130. <?php foreach ($comments_pag->get() as $comment):
  131. $poster = queryUser($comment['poster']);
  132. $esc_email = HTML::escape($poster['email']);
  133. $asrc = HTML::avatar_addr($poster, 80);
  134. $replies = DB::selectAll("select id, poster, content, post_time from blogs_comments where reply_id = {$comment['id']} order by id");
  135. foreach ($replies as $idx => $reply) {
  136. $replies[$idx]['poster_rating'] = queryUser($reply['poster'])['rating'];
  137. }
  138. $replies_json = json_encode($replies);
  139. ?>
  140. <div id="comment-<?= $comment['id'] ?>" class="list-group-item">
  141. <div class="media">
  142. <div class="media-left comtposterbox mr-3">
  143. <a href="<?= HTML::url('/user/profile/'.$poster['username']) ?>" class="d-none d-sm-block">
  144. <img class="media-object img-rounded" src="<?= $asrc ?>" alt="avatar" />
  145. </a>
  146. </div>
  147. <div id="comment-body-<?= $comment['id'] ?>" class="media-body comtbox">
  148. <div class="row">
  149. <div class="col-sm-6"><?= getUserLink($poster['username']) ?></div>
  150. <div class="col-sm-6 text-right"><?= getClickZanBlock('BC', $comment['id'], $comment['zan']) ?></div>
  151. </div>
  152. <div class="comtbox1"><?= $comment['content'] ?></div>
  153. <ul class="text-right list-inline bot-buffer-no"><li><small class="text-muted"><?= $comment['post_time'] ?></small></li><li><a id="reply-to-<?= $comment['id'] ?>" href="#">回复</a></li></ul>
  154. <?php if ($replies): ?>
  155. <div id="replies-<?= $comment['id'] ?>" class="comtbox5"></div>
  156. <?php endif ?>
  157. <script type="text/javascript">showCommentReplies('<?= $comment['id'] ?>', <?= $replies_json ?>);</script>
  158. </div>
  159. </div>
  160. </div>
  161. <?php endforeach ?>
  162. <?php endif ?>
  163. </div>
  164. <?= $comments_pag->pagination() ?>
  165. <h3 class="mt-4">发表评论</h3>
  166. <p>可以用@mike来提到mike这个用户,mike会被高亮显示。如果你真的想打“@”这个字符,请用“@@”。</p>
  167. <?php $comment_form->printHTML() ?>
  168. <div id="div-form-reply" style="display:none">
  169. <?php $reply_form->printHTML() ?>
  170. </div>
  171. <?php echoUOJPageFooter() ?>