PageRenderTime 27ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/ext/comment/theme.php

https://gitlab.com/dali99/shimmie2-Material-Theme
PHP | 323 lines | 254 code | 31 blank | 38 comment | 15 complexity | e3690553f1ccaa18347e74ec99dd3de9 MD5 | raw file
  1. <?php
  2. class CommentListTheme extends Themelet {
  3. var $comments_shown = 0;
  4. var $show_anon_id = false;
  5. var $anon_id = 1;
  6. var $anon_cid = 0;
  7. var $anon_map = array();
  8. var $ct = null;
  9. private function get_anon_colour($ip) {
  10. if(is_null($this->ct)) {
  11. $this->ct = hsl_rainbow();
  12. }
  13. if(!array_key_exists($ip, $this->anon_map)) {
  14. $this->anon_map[$ip] = $this->ct[$this->anon_cid++ % count($this->ct)];
  15. }
  16. return $this->anon_map[$ip];
  17. }
  18. /**
  19. * Display a page with a list of images, and for each image, the image's comments.
  20. *
  21. * @param array $images
  22. * @param int $page_number
  23. * @param int $total_pages
  24. * @param bool $can_post
  25. */
  26. public function display_comment_list($images, $page_number, $total_pages, $can_post) {
  27. global $config, $page, $user;
  28. // aaaaaaargh php
  29. assert(is_array($images));
  30. assert(is_numeric($page_number));
  31. assert(is_numeric($total_pages));
  32. assert(is_bool($can_post));
  33. // parts for the whole page
  34. $prev = $page_number - 1;
  35. $next = $page_number + 1;
  36. $h_prev = ($page_number <= 1) ? "Prev" :
  37. '<a href="'.make_link('comment/list/'.$prev).'">Prev</a>';
  38. $h_index = "<a href='".make_link("post/list")."'>Index</a>";
  39. $h_next = ($page_number >= $total_pages) ? "Next" :
  40. '<a href="'.make_link('comment/list/'.$next).'">Next</a>';
  41. $nav = $h_prev.' | '.$h_index.' | '.$h_next;
  42. $page->set_title("Comments");
  43. $page->set_heading("Comments");
  44. $page->add_block(new Block("Navigation", $nav, "left"));
  45. $this->display_paginator($page, "comment/list", null, $page_number, $total_pages);
  46. // parts for each image
  47. $position = 10;
  48. $comment_limit = $config->get_int("comment_list_count", 10);
  49. $comment_captcha = $config->get_bool('comment_captcha');
  50. foreach($images as $pair) {
  51. $image = $pair[0];
  52. $comments = $pair[1];
  53. $thumb_html = $this->build_thumb_html($image);
  54. $comment_html = "";
  55. $comment_count = count($comments);
  56. if($comment_limit > 0 && $comment_count > $comment_limit) {
  57. $comment_html .= "<p>showing $comment_limit of $comment_count comments</p>";
  58. $comments = array_slice($comments, -$comment_limit);
  59. $this->show_anon_id = false;
  60. }
  61. else {
  62. $this->show_anon_id = true;
  63. }
  64. $this->anon_id = 1;
  65. foreach($comments as $comment) {
  66. $comment_html .= $this->comment_to_html($comment);
  67. }
  68. if(!$user->is_anonymous()) {
  69. if($can_post) {
  70. $comment_html .= $this->build_postbox($image->id);
  71. }
  72. } else {
  73. if ($can_post) {
  74. if(!$comment_captcha) {
  75. $comment_html .= $this->build_postbox($image->id);
  76. }
  77. else {
  78. $link = make_link("post/view/".$image->id);
  79. $comment_html .= "<a href='$link'>Add Comment</a>";
  80. }
  81. }
  82. }
  83. $html = '
  84. <table class="comment_list_table"><tr>
  85. <td width="220">'.$thumb_html.'</td>
  86. <td>'.$comment_html.'</td>
  87. </tr></table>
  88. ';
  89. $page->add_block(new Block( $image->id.': '.$image->get_tag_list(), $html, "main", $position++, "comment-list-list"));
  90. }
  91. }
  92. public function display_admin_block() {
  93. global $page;
  94. $html = '
  95. Delete comments by IP.
  96. <br><br>'.make_form(make_link("comment/bulk_delete"), 'POST')."
  97. <table class='form'>
  98. <tr><th>IP&nbsp;Address</th> <td><input type='text' name='ip' size='15'></td></tr>
  99. <tr><td colspan='2'><input type='submit' value='Delete'></td></tr>
  100. </table>
  101. </form>
  102. ";
  103. $page->add_block(new Block("Mass Comment Delete", $html));
  104. }
  105. /**
  106. * Add some comments to the page, probably in a sidebar.
  107. *
  108. * @param \Comment[] $comments An array of Comment objects to be shown
  109. */
  110. public function display_recent_comments($comments) {
  111. global $page;
  112. $this->show_anon_id = false;
  113. $html = "";
  114. foreach($comments as $comment) {
  115. $html .= $this->comment_to_html($comment, true);
  116. }
  117. $html .= "<a class='more' href='".make_link("comment/list")."'>Full List</a>";
  118. $page->add_block(new Block("Comments", $html, "left", 50, "comment-list-recent"));
  119. }
  120. /**
  121. * Show comments for an image.
  122. *
  123. * @param Image $image
  124. * @param \Comment[] $comments
  125. * @param bool $postbox
  126. */
  127. public function display_image_comments(Image $image, $comments, $postbox) {
  128. global $page;
  129. $this->show_anon_id = true;
  130. $html = "";
  131. foreach($comments as $comment) {
  132. $html .= $this->comment_to_html($comment);
  133. }
  134. if($postbox) {
  135. $html .= $this->build_postbox($image->id);
  136. }
  137. $page->add_block(new Block("Comments", $html, "main", 30, "comment-list-image"));
  138. }
  139. /**
  140. * Show comments made by a user.
  141. *
  142. * @param \Comment[] $comments
  143. * @param \User $user
  144. */
  145. public function display_recent_user_comments($comments, User $user) {
  146. global $page;
  147. $html = "";
  148. foreach($comments as $comment) {
  149. $html .= $this->comment_to_html($comment, true);
  150. }
  151. if(empty($html)) {
  152. $html = '<p>No comments by this user.</p>';
  153. }
  154. else {
  155. $html .= "<p><a href='".make_link("comment/beta-search/{$user->name}/1")."'>More</a></p>";
  156. }
  157. $page->add_block(new Block("Comments", $html, "left", 70, "comment-list-user"));
  158. }
  159. /**
  160. * @param \Comment[] $comments
  161. * @param int $page_number
  162. * @param int $total_pages
  163. * @param \User $user
  164. */
  165. public function display_all_user_comments($comments, $page_number, $total_pages, User $user) {
  166. global $page;
  167. assert(is_numeric($page_number));
  168. assert(is_numeric($total_pages));
  169. $html = "";
  170. foreach($comments as $comment) {
  171. $html .= $this->comment_to_html($comment, true);
  172. }
  173. if(empty($html)) {
  174. $html = '<p>No comments by this user.</p>';
  175. }
  176. $page->add_block(new Block("Comments", $html, "main", 70, "comment-list-user"));
  177. $prev = $page_number - 1;
  178. $next = $page_number + 1;
  179. //$search_terms = array('I','have','no','idea','what','this','does!');
  180. //$u_tags = url_escape(implode(" ", $search_terms));
  181. //$query = empty($u_tags) ? "" : '/'.$u_tags;
  182. $h_prev = ($page_number <= 1) ? "Prev" : "<a href='$prev'>Prev</a>";
  183. $h_index = "<a href='".make_link("post/list")."'>Index</a>";
  184. $h_next = ($page_number >= $total_pages) ? "Next" : "<a href='$next'>Next</a>";
  185. $page->set_title(html_escape($user->name)."'s comments");
  186. $page->add_block(new Block("Navigation", $h_prev.' | '.$h_index.' | '.$h_next, "left", 0));
  187. $this->display_paginator($page, "comment/beta-search/{$user->name}", null, $page_number, $total_pages);
  188. }
  189. /**
  190. * @param \Comment $comment
  191. * @param bool $trim
  192. * @return string
  193. */
  194. protected function comment_to_html(Comment $comment, $trim=false) {
  195. global $config, $user;
  196. $tfe = new TextFormattingEvent($comment->comment);
  197. send_event($tfe);
  198. $i_uid = int_escape($comment->owner_id);
  199. $h_name = html_escape($comment->owner_name);
  200. $h_timestamp = autodate($comment->posted);
  201. $h_comment = ($trim ? truncate($tfe->stripped, 50) : $tfe->formatted);
  202. $i_comment_id = int_escape($comment->comment_id);
  203. $i_image_id = int_escape($comment->image_id);
  204. if($i_uid == $config->get_int("anon_id")) {
  205. $anoncode = "";
  206. $anoncode2 = "";
  207. if($this->show_anon_id) {
  208. $anoncode = '<sup>'.$this->anon_id.'</sup>';
  209. if(!array_key_exists($comment->poster_ip, $this->anon_map)) {
  210. $this->anon_map[$comment->poster_ip] = $this->anon_id;
  211. }
  212. #if($user->can("view_ip")) {
  213. #$style = " style='color: ".$this->get_anon_colour($comment->poster_ip).";'";
  214. if($user->can("view_ip") || $config->get_bool("comment_samefags_public", false)) {
  215. if($this->anon_map[$comment->poster_ip] != $this->anon_id) {
  216. $anoncode2 = '<sup>('.$this->anon_map[$comment->poster_ip].')</sup>';
  217. }
  218. }
  219. }
  220. $h_userlink = "<span class='username'>" . $h_name . $anoncode . $anoncode2 . "</span>";
  221. $this->anon_id++;
  222. }
  223. else {
  224. $h_userlink = '<a class="username" href="'.make_link('user/'.$h_name).'">'.$h_name.'</a>';
  225. }
  226. $stripped_nonl = str_replace("\n", "\\n", substr($tfe->stripped, 0, 50));
  227. $stripped_nonl = str_replace("\r", "\\r", $stripped_nonl);
  228. $hb = ($comment->owner_class == "hellbanned" ? "hb" : "");
  229. if($trim) {
  230. $html = "
  231. <div class=\"comment $hb\">
  232. $h_userlink: $h_comment
  233. <a href=\"".make_link("post/view/$i_image_id#c$i_comment_id")."\">&gt;&gt;&gt;</a>
  234. </div>
  235. ";
  236. }
  237. else {
  238. $h_avatar = "";
  239. if(!empty($comment->owner_email)) {
  240. $hash = md5(strtolower($comment->owner_email));
  241. $cb = date("Y-m-d");
  242. $h_avatar = "<img src=\"http://www.gravatar.com/avatar/$hash.jpg?cacheBreak=$cb\"><br>";
  243. }
  244. $h_reply = " - <a href='javascript: replyTo($i_image_id, $i_comment_id, \"$h_name\")'>Reply</a>";
  245. $h_ip = $user->can("view_ip") ? "<br>".show_ip($comment->poster_ip, "Comment posted {$comment->posted}") : "";
  246. $h_del = $user->can("delete_comment") ?
  247. ' - <a onclick="return confirm(\'Delete comment by '.$h_name.':\\n'.$stripped_nonl.'\');" '.
  248. 'href="'.make_link('comment/delete/'.$i_comment_id.'/'.$i_image_id).'">Del</a>' : '';
  249. $html = "
  250. <div class=\"comment $hb\" id=\"c$i_comment_id\">
  251. <div class=\"info\">
  252. $h_avatar
  253. $h_timestamp$h_reply$h_ip$h_del
  254. </div>
  255. $h_userlink: $h_comment
  256. </div>
  257. ";
  258. }
  259. return $html;
  260. }
  261. /**
  262. * @param int $image_id
  263. * @return string
  264. */
  265. protected function build_postbox(/*int*/ $image_id) {
  266. global $config;
  267. $i_image_id = int_escape($image_id);
  268. $hash = CommentList::get_hash();
  269. $h_captcha = $config->get_bool("comment_captcha") ? captcha_get_html() : "";
  270. return '
  271. <div class="comment comment_add">
  272. '.make_form(make_link("comment/add")).'
  273. <input type="hidden" name="image_id" value="'.$i_image_id.'" />
  274. <input type="hidden" name="hash" value="'.$hash.'" />
  275. <textarea id="comment_on_'.$i_image_id.'" name="comment" rows="5" cols="50"></textarea>
  276. '.$h_captcha.'
  277. <br><input type="submit" value="Post Comment" />
  278. </form>
  279. </div>
  280. ';
  281. }
  282. }