PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/nacridan/forum/viewforum.php

https://gitlab.com/nacridan/Nacridan
PHP | 309 lines | 238 code | 49 blank | 22 comment | 75 complexity | 10b8e6edcce1f6a085368db4e7127bc7 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright (C) 2008-2012 FluxBB
  4. * based on code by Rickard Andersson copyright (C) 2002-2008 PunBB
  5. * License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
  6. */
  7. define('PUN_ROOT', dirname(__FILE__).'/');
  8. require PUN_ROOT.'include/common.php';
  9. if ($pun_user['g_read_board'] == '0')
  10. message($lang_common['No view'], false, '403 Forbidden');
  11. $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
  12. if ($id < 1)
  13. message($lang_common['Bad request'], false, '404 Not Found');
  14. // Load the viewforum.php language file
  15. require PUN_ROOT.'lang/'.$pun_user['language'].'/forum.php';
  16. // Fetch some info about the forum
  17. if (!$pun_user['is_guest'])
  18. $result = $db->query('SELECT f.forum_name, f.redirect_url, f.moderators, f.num_topics, f.sort_by, fp.post_topics, s.user_id AS is_subscribed FROM '.$db->prefix.'forums AS f LEFT JOIN '.$db->prefix.'forum_subscriptions AS s ON (f.id=s.forum_id AND s.user_id='.$pun_user['id'].') LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$pun_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND f.id='.$id) or error('Unable to fetch forum info', __FILE__, __LINE__, $db->error());
  19. else
  20. $result = $db->query('SELECT f.forum_name, f.redirect_url, f.moderators, f.num_topics, f.sort_by, fp.post_topics, 0 AS is_subscribed FROM '.$db->prefix.'forums AS f LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$pun_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND f.id='.$id) or error('Unable to fetch forum info', __FILE__, __LINE__, $db->error());
  21. if (!$db->num_rows($result))
  22. message($lang_common['Bad request'], false, '404 Not Found');
  23. $cur_forum = $db->fetch_assoc($result);
  24. // Is this a redirect forum? In that case, redirect!
  25. if ($cur_forum['redirect_url'] != '')
  26. {
  27. header('Location: '.$cur_forum['redirect_url']);
  28. exit;
  29. }
  30. // Sort out who the moderators are and if we are currently a moderator (or an admin)
  31. $mods_array = ($cur_forum['moderators'] != '') ? unserialize($cur_forum['moderators']) : array();
  32. $is_admmod = ($pun_user['g_id'] == PUN_ADMIN || ($pun_user['g_moderator'] == '1' && array_key_exists($pun_user['username'], $mods_array))) ? true : false;
  33. switch ($cur_forum['sort_by'])
  34. {
  35. case 0:
  36. $sort_by = 'last_post DESC';
  37. break;
  38. case 1:
  39. $sort_by = 'posted DESC';
  40. break;
  41. case 2:
  42. $sort_by = 'subject ASC';
  43. break;
  44. default:
  45. $sort_by = 'last_post DESC';
  46. break;
  47. }
  48. // Can we or can we not post new topics?
  49. if (($cur_forum['post_topics'] == '' && $pun_user['g_post_topics'] == '1') || $cur_forum['post_topics'] == '1' || $is_admmod)
  50. $post_link = "\t\t\t".'<p class="postlink conr"><a href="post.php?fid='.$id.'">'.$lang_forum['Post topic'].'</a></p>'."\n";
  51. else
  52. $post_link = '';
  53. // Get topic/forum tracking data
  54. if (!$pun_user['is_guest'])
  55. $tracked_topics = get_tracked_topics();
  56. // Determine the topic offset (based on $_GET['p'])
  57. $num_pages = ceil($cur_forum['num_topics'] / $pun_user['disp_topics']);
  58. $p = (!isset($_GET['p']) || $_GET['p'] <= 1 || $_GET['p'] > $num_pages) ? 1 : intval($_GET['p']);
  59. $start_from = $pun_user['disp_topics'] * ($p - 1);
  60. // Generate paging links
  61. $paging_links = '<span class="pages-label">'.$lang_common['Pages'].' </span>'.paginate($num_pages, $p, 'viewforum.php?id='.$id);
  62. // Add relationship meta tags
  63. $page_head = array();
  64. $page_head['canonical'] = '<link rel="canonical" href="viewforum.php?id='.$id.($p == 1 ? '' : '&amp;p='.$p).'" title="'.sprintf($lang_common['Page'], $p).'" />';
  65. if ($num_pages > 1)
  66. {
  67. if ($p > 1)
  68. $page_head['prev'] = '<link rel="prev" href="viewforum.php?id='.$id.($p == 2 ? '' : '&amp;p='.($p - 1)).'" title="'.sprintf($lang_common['Page'], $p - 1).'" />';
  69. if ($p < $num_pages)
  70. $page_head['next'] = '<link rel="next" href="viewforum.php?id='.$id.'&amp;p='.($p + 1).'" title="'.sprintf($lang_common['Page'], $p + 1).'" />';
  71. }
  72. if ($pun_config['o_feed_type'] == '1')
  73. $page_head['feed'] = '<link rel="alternate" type="application/rss+xml" href="extern.php?action=feed&amp;fid='.$id.'&amp;type=rss" title="'.$lang_common['RSS forum feed'].'" />';
  74. else if ($pun_config['o_feed_type'] == '2')
  75. $page_head['feed'] = '<link rel="alternate" type="application/atom+xml" href="extern.php?action=feed&amp;fid='.$id.'&amp;type=atom" title="'.$lang_common['Atom forum feed'].'" />';
  76. $forum_actions = array();
  77. if (!$pun_user['is_guest'])
  78. {
  79. if ($pun_config['o_forum_subscriptions'] == '1')
  80. {
  81. if ($cur_forum['is_subscribed'])
  82. $forum_actions[] = '<span>'.$lang_forum['Is subscribed'].' - </span><a href="misc.php?action=unsubscribe&amp;fid='.$id.'">'.$lang_forum['Unsubscribe'].'</a>';
  83. else
  84. $forum_actions[] = '<a href="misc.php?action=subscribe&amp;fid='.$id.'">'.$lang_forum['Subscribe'].'</a>';
  85. }
  86. $forum_actions[] = '<a href="misc.php?action=markforumread&amp;fid='.$id.'">'.$lang_common['Mark forum read'].'</a>';
  87. }
  88. $page_title = array(pun_htmlspecialchars($pun_config['o_board_title']), pun_htmlspecialchars($cur_forum['forum_name']));
  89. define('PUN_ALLOW_INDEX', 1);
  90. define('PUN_ACTIVE_PAGE', 'index');
  91. require PUN_ROOT.'header.php';
  92. ?>
  93. <div class="linkst">
  94. <div class="inbox crumbsplus">
  95. <ul class="crumbs">
  96. <li><a href="index.php"><?php echo $lang_common['Index'] ?></a></li>
  97. <li><span>»&#160;</span><strong><a href="viewforum.php?id=<?php echo $id ?>"><?php echo pun_htmlspecialchars($cur_forum['forum_name']) ?></a></strong></li>
  98. </ul>
  99. <div class="pagepost">
  100. <p class="pagelink conl"><?php echo $paging_links ?></p>
  101. <?php echo $post_link ?>
  102. </div>
  103. <div class="clearer"></div>
  104. </div>
  105. </div>
  106. <div id="vf" class="blocktable">
  107. <h2><span><?php echo pun_htmlspecialchars($cur_forum['forum_name']) ?></span></h2>
  108. <div class="box">
  109. <div class="inbox">
  110. <table>
  111. <thead>
  112. <tr>
  113. <th class="tcl" scope="col"><?php echo $lang_common['Topic'] ?></th>
  114. <th class="tc2" scope="col"><?php echo $lang_common['Replies'] ?></th>
  115. <?php if ($pun_config['o_topic_views'] == '1'): ?> <th class="tc3" scope="col"><?php echo $lang_forum['Views'] ?></th>
  116. <?php endif; ?> <th class="tcr" scope="col"><?php echo $lang_common['Last post'] ?></th>
  117. </tr>
  118. </thead>
  119. <tbody>
  120. <?php
  121. // Retrieve a list of topic IDs, LIMIT is (really) expensive so we only fetch the IDs here then later fetch the remaining data
  122. $result = $db->query('SELECT id FROM '.$db->prefix.'topics WHERE forum_id='.$id.' ORDER BY sticky DESC, '.$sort_by.', id DESC LIMIT '.$start_from.', '.$pun_user['disp_topics']) or error('Unable to fetch topic IDs', __FILE__, __LINE__, $db->error());
  123. // If there are topics in this forum
  124. if ($db->num_rows($result))
  125. {
  126. $topic_ids = array();
  127. for ($i = 0; $cur_topic_id = $db->result($result, $i); $i++)
  128. $topic_ids[] = $cur_topic_id;
  129. // Fetch list of topics to display on this page
  130. if ($pun_user['is_guest'] || $pun_config['o_show_dot'] == '0')
  131. {
  132. // Without "the dot"
  133. $sql = 'SELECT id, poster, subject, posted, last_post, last_post_id, last_poster, num_views, num_replies, closed, sticky, moved_to FROM '.$db->prefix.'topics WHERE id IN('.implode(',', $topic_ids).') ORDER BY sticky DESC, '.$sort_by.', id DESC';
  134. }
  135. else
  136. {
  137. // With "the dot"
  138. $sql = 'SELECT p.poster_id AS has_posted, t.id, t.subject, t.poster, t.posted, t.last_post, t.last_post_id, t.last_poster, t.num_views, t.num_replies, t.closed, t.sticky, t.moved_to FROM '.$db->prefix.'topics AS t LEFT JOIN '.$db->prefix.'posts AS p ON t.id=p.topic_id AND p.poster_id='.$pun_user['id'].' WHERE t.id IN('.implode(',', $topic_ids).') GROUP BY t.id'.($db_type == 'pgsql' ? ', t.subject, t.poster, t.posted, t.last_post, t.last_post_id, t.last_poster, t.num_views, t.num_replies, t.closed, t.sticky, t.moved_to, p.poster_id' : '').' ORDER BY t.sticky DESC, t.'.$sort_by.', t.id DESC';
  139. }
  140. $result = $db->query($sql) or error('Unable to fetch topic list', __FILE__, __LINE__, $db->error());
  141. $topic_count = 0;
  142. while ($cur_topic = $db->fetch_assoc($result))
  143. {
  144. ++$topic_count;
  145. $status_text = array();
  146. $item_status = ($topic_count % 2 == 0) ? 'roweven' : 'rowodd';
  147. $icon_type = 'icon';
  148. if (is_null($cur_topic['moved_to']))
  149. $last_post = '<a href="viewtopic.php?pid='.$cur_topic['last_post_id'].'#p'.$cur_topic['last_post_id'].'">'.format_time($cur_topic['last_post']).'</a> <span class="byuser">'.$lang_common['by'].' '.pun_htmlspecialchars($cur_topic['last_poster']).'</span>';
  150. else
  151. $last_post = '- - -';
  152. if ($pun_config['o_censoring'] == '1')
  153. $cur_topic['subject'] = censor_words($cur_topic['subject']);
  154. if ($cur_topic['sticky'] == '1')
  155. {
  156. $item_status .= ' isticky';
  157. $status_text[] = '<span class="stickytext">'.$lang_forum['Sticky'].'</span>';
  158. }
  159. if ($cur_topic['moved_to'] != 0)
  160. {
  161. $subject = '<a href="viewtopic.php?id='.$cur_topic['moved_to'].'">'.pun_htmlspecialchars($cur_topic['subject']).'</a> <span class="byuser">'.$lang_common['by'].' '.pun_htmlspecialchars($cur_topic['poster']).'</span>';
  162. $status_text[] = '<span class="movedtext">'.$lang_forum['Moved'].'</span>';
  163. $item_status .= ' imoved';
  164. }
  165. else if ($cur_topic['closed'] == '0')
  166. $subject = '<a href="viewtopic.php?id='.$cur_topic['id'].'">'.pun_htmlspecialchars($cur_topic['subject']).'</a> <span class="byuser">'.$lang_common['by'].' '.pun_htmlspecialchars($cur_topic['poster']).'</span>';
  167. else
  168. {
  169. $subject = '<a href="viewtopic.php?id='.$cur_topic['id'].'">'.pun_htmlspecialchars($cur_topic['subject']).'</a> <span class="byuser">'.$lang_common['by'].' '.pun_htmlspecialchars($cur_topic['poster']).'</span>';
  170. $status_text[] = '<span class="closedtext">'.$lang_forum['Closed'].'</span>';
  171. $item_status .= ' iclosed';
  172. }
  173. if (!$pun_user['is_guest'] && $cur_topic['last_post'] > $pun_user['last_visit'] && (!isset($tracked_topics['topics'][$cur_topic['id']]) || $tracked_topics['topics'][$cur_topic['id']] < $cur_topic['last_post']) && (!isset($tracked_topics['forums'][$id]) || $tracked_topics['forums'][$id] < $cur_topic['last_post']) && is_null($cur_topic['moved_to']))
  174. {
  175. $item_status .= ' inew';
  176. $icon_type = 'icon icon-new';
  177. $subject = '<strong>'.$subject.'</strong>';
  178. $subject_new_posts = '<span class="newtext">[ <a href="viewtopic.php?id='.$cur_topic['id'].'&amp;action=new" title="'.$lang_common['New posts info'].'">'.$lang_common['New posts'].'</a> ]</span>';
  179. }
  180. else
  181. $subject_new_posts = null;
  182. // Insert the status text before the subject
  183. $subject = implode(' ', $status_text).' '.$subject;
  184. // Should we display the dot or not? :)
  185. if (!$pun_user['is_guest'] && $pun_config['o_show_dot'] == '1')
  186. {
  187. if ($cur_topic['has_posted'] == $pun_user['id'])
  188. {
  189. $subject = '<strong class="ipost">·&#160;</strong>'.$subject;
  190. $item_status .= ' iposted';
  191. }
  192. }
  193. $num_pages_topic = ceil(($cur_topic['num_replies'] + 1) / $pun_user['disp_posts']);
  194. if ($num_pages_topic > 1)
  195. $subject_multipage = '<span class="pagestext">[ '.paginate($num_pages_topic, -1, 'viewtopic.php?id='.$cur_topic['id']).' ]</span>';
  196. else
  197. $subject_multipage = null;
  198. // Should we show the "New posts" and/or the multipage links?
  199. if (!empty($subject_new_posts) || !empty($subject_multipage))
  200. {
  201. $subject .= !empty($subject_new_posts) ? ' '.$subject_new_posts : '';
  202. $subject .= !empty($subject_multipage) ? ' '.$subject_multipage : '';
  203. }
  204. ?>
  205. <tr class="<?php echo $item_status ?>">
  206. <td class="tcl">
  207. <div class="<?php echo $icon_type ?>"><div class="nosize"><?php echo forum_number_format($topic_count + $start_from) ?></div></div>
  208. <div class="tclcon">
  209. <div>
  210. <?php echo $subject."\n" ?>
  211. </div>
  212. </div>
  213. </td>
  214. <td class="tc2"><?php echo (is_null($cur_topic['moved_to'])) ? forum_number_format($cur_topic['num_replies']) : '-' ?></td>
  215. <?php if ($pun_config['o_topic_views'] == '1'): ?> <td class="tc3"><?php echo (is_null($cur_topic['moved_to'])) ? forum_number_format($cur_topic['num_views']) : '-' ?></td>
  216. <?php endif; ?> <td class="tcr"><?php echo $last_post ?></td>
  217. </tr>
  218. <?php
  219. }
  220. }
  221. else
  222. {
  223. $colspan = ($pun_config['o_topic_views'] == '1') ? 4 : 3;
  224. ?>
  225. <tr class="rowodd inone">
  226. <td class="tcl" colspan="<?php echo $colspan ?>">
  227. <div class="icon inone"><div class="nosize"><!-- --></div></div>
  228. <div class="tclcon">
  229. <div>
  230. <strong><?php echo $lang_forum['Empty forum'] ?></strong>
  231. </div>
  232. </div>
  233. </td>
  234. </tr>
  235. <?php
  236. }
  237. ?>
  238. </tbody>
  239. </table>
  240. </div>
  241. </div>
  242. </div>
  243. <div class="linksb">
  244. <div class="inbox crumbsplus">
  245. <div class="pagepost">
  246. <p class="pagelink conl"><?php echo $paging_links ?></p>
  247. <?php echo $post_link ?>
  248. </div>
  249. <ul class="crumbs">
  250. <li><a href="index.php"><?php echo $lang_common['Index'] ?></a></li>
  251. <li><span>»&#160;</span><strong><a href="viewforum.php?id=<?php echo $id ?>"><?php echo pun_htmlspecialchars($cur_forum['forum_name']) ?></a></strong></li>
  252. </ul>
  253. <?php echo (!empty($forum_actions) ? "\t\t".'<p class="subscribelink clearb">'.implode(' - ', $forum_actions).'</p>'."\n" : '') ?>
  254. <div class="clearer"></div>
  255. </div>
  256. </div>
  257. <?php
  258. $forum_id = $id;
  259. $footer_style = 'viewforum';
  260. require PUN_ROOT.'footer.php';