PageRenderTime 60ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/phpBB/viewforum.php

https://github.com/Jipem/phpbb
PHP | 857 lines | 603 code | 140 blank | 114 comment | 157 complexity | 3fe6849f29bc8bf2f4add156becb88c6 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * This file is part of the phpBB Forum Software package.
  5. *
  6. * @copyright (c) phpBB Limited <https://www.phpbb.com>
  7. * @license GNU General Public License, version 2 (GPL-2.0)
  8. *
  9. * For full copyright and license information, please see
  10. * the docs/CREDITS.txt file.
  11. *
  12. */
  13. /**
  14. * @ignore
  15. */
  16. define('IN_PHPBB', true);
  17. $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
  18. $phpEx = substr(strrchr(__FILE__, '.'), 1);
  19. include($phpbb_root_path . 'common.' . $phpEx);
  20. include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
  21. // Start session
  22. $user->session_begin();
  23. $auth->acl($user->data);
  24. // Start initial var setup
  25. $forum_id = request_var('f', 0);
  26. $mark_read = request_var('mark', '');
  27. $start = request_var('start', 0);
  28. $default_sort_days = (!empty($user->data['user_topic_show_days'])) ? $user->data['user_topic_show_days'] : 0;
  29. $default_sort_key = (!empty($user->data['user_topic_sortby_type'])) ? $user->data['user_topic_sortby_type'] : 't';
  30. $default_sort_dir = (!empty($user->data['user_topic_sortby_dir'])) ? $user->data['user_topic_sortby_dir'] : 'd';
  31. $sort_days = request_var('st', $default_sort_days);
  32. $sort_key = request_var('sk', $default_sort_key);
  33. $sort_dir = request_var('sd', $default_sort_dir);
  34. $pagination = $phpbb_container->get('pagination');
  35. // Check if the user has actually sent a forum ID with his/her request
  36. // If not give them a nice error page.
  37. if (!$forum_id)
  38. {
  39. trigger_error('NO_FORUM');
  40. }
  41. $sql_from = FORUMS_TABLE . ' f';
  42. $lastread_select = '';
  43. // Grab appropriate forum data
  44. if ($config['load_db_lastread'] && $user->data['is_registered'])
  45. {
  46. $sql_from .= ' LEFT JOIN ' . FORUMS_TRACK_TABLE . ' ft ON (ft.user_id = ' . $user->data['user_id'] . '
  47. AND ft.forum_id = f.forum_id)';
  48. $lastread_select .= ', ft.mark_time';
  49. }
  50. if ($user->data['is_registered'])
  51. {
  52. $sql_from .= ' LEFT JOIN ' . FORUMS_WATCH_TABLE . ' fw ON (fw.forum_id = f.forum_id AND fw.user_id = ' . $user->data['user_id'] . ')';
  53. $lastread_select .= ', fw.notify_status';
  54. }
  55. $sql = "SELECT f.* $lastread_select
  56. FROM $sql_from
  57. WHERE f.forum_id = $forum_id";
  58. $result = $db->sql_query($sql);
  59. $forum_data = $db->sql_fetchrow($result);
  60. $db->sql_freeresult($result);
  61. if (!$forum_data)
  62. {
  63. trigger_error('NO_FORUM');
  64. }
  65. // Configure style, language, etc.
  66. $user->setup('viewforum', $forum_data['forum_style']);
  67. // Redirect to login upon emailed notification links
  68. if (isset($_GET['e']) && !$user->data['is_registered'])
  69. {
  70. login_box('', $user->lang['LOGIN_NOTIFY_FORUM']);
  71. }
  72. // Permissions check
  73. if (!$auth->acl_gets('f_list', 'f_read', $forum_id) || ($forum_data['forum_type'] == FORUM_LINK && $forum_data['forum_link'] && !$auth->acl_get('f_read', $forum_id)))
  74. {
  75. if ($user->data['user_id'] != ANONYMOUS)
  76. {
  77. trigger_error('SORRY_AUTH_READ');
  78. }
  79. login_box('', $user->lang['LOGIN_VIEWFORUM']);
  80. }
  81. // Forum is passworded ... check whether access has been granted to this
  82. // user this session, if not show login box
  83. if ($forum_data['forum_password'])
  84. {
  85. login_forum_box($forum_data);
  86. }
  87. // Is this forum a link? ... User got here either because the
  88. // number of clicks is being tracked or they guessed the id
  89. if ($forum_data['forum_type'] == FORUM_LINK && $forum_data['forum_link'])
  90. {
  91. // Does it have click tracking enabled?
  92. if ($forum_data['forum_flags'] & FORUM_FLAG_LINK_TRACK)
  93. {
  94. $sql = 'UPDATE ' . FORUMS_TABLE . '
  95. SET forum_posts_approved = forum_posts_approved + 1
  96. WHERE forum_id = ' . $forum_id;
  97. $db->sql_query($sql);
  98. }
  99. // We redirect to the url. The third parameter indicates that external redirects are allowed.
  100. redirect($forum_data['forum_link'], false, true);
  101. return;
  102. }
  103. // Build navigation links
  104. generate_forum_nav($forum_data);
  105. // Forum Rules
  106. if ($auth->acl_get('f_read', $forum_id))
  107. {
  108. generate_forum_rules($forum_data);
  109. }
  110. // Do we have subforums?
  111. $active_forum_ary = $moderators = array();
  112. if ($forum_data['left_id'] != $forum_data['right_id'] - 1)
  113. {
  114. list($active_forum_ary, $moderators) = display_forums($forum_data, $config['load_moderators'], $config['load_moderators']);
  115. }
  116. else
  117. {
  118. $template->assign_var('S_HAS_SUBFORUM', false);
  119. if ($config['load_moderators'])
  120. {
  121. get_moderators($moderators, $forum_id);
  122. }
  123. }
  124. $phpbb_content_visibility = $phpbb_container->get('content.visibility');
  125. // Dump out the page header and load viewforum template
  126. $topics_count = $phpbb_content_visibility->get_count('forum_topics', $forum_data, $forum_id);
  127. $start = $pagination->validate_start($start, $config['topics_per_page'], $topics_count);
  128. page_header($forum_data['forum_name'] . ($start ? ' - ' . $user->lang('PAGE_TITLE_NUMBER', $pagination->get_on_page($config['topics_per_page'], $start)) : ''), true, $forum_id);
  129. $template->set_filenames(array(
  130. 'body' => 'viewforum_body.html')
  131. );
  132. make_jumpbox(append_sid("{$phpbb_root_path}viewforum.$phpEx"), $forum_id);
  133. $template->assign_vars(array(
  134. 'U_VIEW_FORUM' => append_sid("{$phpbb_root_path}viewforum.$phpEx", "f=$forum_id" . (($start == 0) ? '' : "&amp;start=$start")),
  135. ));
  136. // Not postable forum or showing active topics?
  137. if (!($forum_data['forum_type'] == FORUM_POST || (($forum_data['forum_flags'] & FORUM_FLAG_ACTIVE_TOPICS) && $forum_data['forum_type'] == FORUM_CAT)))
  138. {
  139. page_footer();
  140. }
  141. // Ok, if someone has only list-access, we only display the forum list.
  142. // We also make this circumstance available to the template in case we want to display a notice. ;)
  143. if (!$auth->acl_get('f_read', $forum_id))
  144. {
  145. $template->assign_vars(array(
  146. 'S_NO_READ_ACCESS' => true,
  147. ));
  148. page_footer();
  149. }
  150. // Handle marking posts
  151. if ($mark_read == 'topics')
  152. {
  153. $token = request_var('hash', '');
  154. if (check_link_hash($token, 'global'))
  155. {
  156. markread('topics', array($forum_id), false, request_var('mark_time', 0));
  157. }
  158. $redirect_url = append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_id);
  159. meta_refresh(3, $redirect_url);
  160. if ($request->is_ajax())
  161. {
  162. // Tell the ajax script what language vars and URL need to be replaced
  163. $data = array(
  164. 'NO_UNREAD_POSTS' => $user->lang['NO_UNREAD_POSTS'],
  165. 'UNREAD_POSTS' => $user->lang['UNREAD_POSTS'],
  166. 'U_MARK_TOPICS' => ($user->data['is_registered'] || $config['load_anon_lastread']) ? append_sid("{$phpbb_root_path}viewforum.$phpEx", 'hash=' . generate_link_hash('global') . "&f=$forum_id&mark=topics&mark_time=" . time()) : '',
  167. 'MESSAGE_TITLE' => $user->lang['INFORMATION'],
  168. 'MESSAGE_TEXT' => $user->lang['TOPICS_MARKED']
  169. );
  170. $json_response = new \phpbb\json_response();
  171. $json_response->send($data);
  172. }
  173. trigger_error($user->lang['TOPICS_MARKED'] . '<br /><br />' . sprintf($user->lang['RETURN_FORUM'], '<a href="' . $redirect_url . '">', '</a>'));
  174. }
  175. // Is a forum specific topic count required?
  176. if ($forum_data['forum_topics_per_page'])
  177. {
  178. $config['topics_per_page'] = $forum_data['forum_topics_per_page'];
  179. }
  180. // Do the forum Prune thang - cron type job ...
  181. if (!$config['use_system_cron'])
  182. {
  183. $cron = $phpbb_container->get('cron.manager');
  184. $task = $cron->find_task('cron.task.core.prune_forum');
  185. $task->set_forum_data($forum_data);
  186. if ($task->is_ready())
  187. {
  188. $url = $task->get_url();
  189. $template->assign_var('RUN_CRON_TASK', '<img src="' . $url . '" width="1" height="1" alt="cron" />');
  190. }
  191. else
  192. {
  193. // See if we should prune the shadow topics instead
  194. $task = $cron->find_task('cron.task.core.prune_shadow_topics');
  195. $task->set_forum_data($forum_data);
  196. if ($task->is_ready())
  197. {
  198. $url = $task->get_url();
  199. $template->assign_var('RUN_CRON_TASK', '<img src="' . $url . '" width="1" height="1" alt="cron" />');
  200. }
  201. }
  202. }
  203. // Forum rules and subscription info
  204. $s_watching_forum = array(
  205. 'link' => '',
  206. 'link_toggle' => '',
  207. 'title' => '',
  208. 'title_toggle' => '',
  209. 'is_watching' => false,
  210. );
  211. if (($config['email_enable'] || $config['jab_enable']) && $config['allow_forum_notify'] && $forum_data['forum_type'] == FORUM_POST && ($auth->acl_get('f_subscribe', $forum_id) || $user->data['user_id'] == ANONYMOUS))
  212. {
  213. $notify_status = (isset($forum_data['notify_status'])) ? $forum_data['notify_status'] : NULL;
  214. watch_topic_forum('forum', $s_watching_forum, $user->data['user_id'], $forum_id, 0, $notify_status, $start, $forum_data['forum_name']);
  215. }
  216. $s_forum_rules = '';
  217. gen_forum_auth_level('forum', $forum_id, $forum_data['forum_status']);
  218. // Topic ordering options
  219. $limit_days = array(0 => $user->lang['ALL_TOPICS'], 1 => $user->lang['1_DAY'], 7 => $user->lang['7_DAYS'], 14 => $user->lang['2_WEEKS'], 30 => $user->lang['1_MONTH'], 90 => $user->lang['3_MONTHS'], 180 => $user->lang['6_MONTHS'], 365 => $user->lang['1_YEAR']);
  220. $sort_by_text = array('a' => $user->lang['AUTHOR'], 't' => $user->lang['POST_TIME'], 'r' => $user->lang['REPLIES'], 's' => $user->lang['SUBJECT'], 'v' => $user->lang['VIEWS']);
  221. $sort_by_sql = array('a' => 't.topic_first_poster_name', 't' => 't.topic_last_post_time', 'r' => (($auth->acl_get('m_approve', $forum_id)) ? 't.topic_posts_approved + t.topic_posts_unapproved + t.topic_posts_softdeleted' : 't.topic_posts_approved'), 's' => 't.topic_title', 'v' => 't.topic_views');
  222. $s_limit_days = $s_sort_key = $s_sort_dir = $u_sort_param = '';
  223. gen_sort_selects($limit_days, $sort_by_text, $sort_days, $sort_key, $sort_dir, $s_limit_days, $s_sort_key, $s_sort_dir, $u_sort_param, $default_sort_days, $default_sort_key, $default_sort_dir);
  224. // Limit topics to certain time frame, obtain correct topic count
  225. if ($sort_days)
  226. {
  227. $min_post_time = time() - ($sort_days * 86400);
  228. $sql = 'SELECT COUNT(topic_id) AS num_topics
  229. FROM ' . TOPICS_TABLE . "
  230. WHERE forum_id = $forum_id
  231. AND (topic_last_post_time >= $min_post_time
  232. OR topic_type = " . POST_ANNOUNCE . '
  233. OR topic_type = ' . POST_GLOBAL . ')
  234. AND ' . $phpbb_content_visibility->get_visibility_sql('topic', $forum_id);
  235. $result = $db->sql_query($sql);
  236. $topics_count = (int) $db->sql_fetchfield('num_topics');
  237. $db->sql_freeresult($result);
  238. if (isset($_POST['sort']))
  239. {
  240. $start = 0;
  241. }
  242. $sql_limit_time = "AND t.topic_last_post_time >= $min_post_time";
  243. // Make sure we have information about day selection ready
  244. $template->assign_var('S_SORT_DAYS', true);
  245. }
  246. else
  247. {
  248. $sql_limit_time = '';
  249. }
  250. // Basic pagewide vars
  251. $post_alt = ($forum_data['forum_status'] == ITEM_LOCKED) ? $user->lang['FORUM_LOCKED'] : $user->lang['POST_NEW_TOPIC'];
  252. // Display active topics?
  253. $s_display_active = ($forum_data['forum_type'] == FORUM_CAT && ($forum_data['forum_flags'] & FORUM_FLAG_ACTIVE_TOPICS)) ? true : false;
  254. $s_search_hidden_fields = array('fid' => array($forum_id));
  255. if ($_SID)
  256. {
  257. $s_search_hidden_fields['sid'] = $_SID;
  258. }
  259. if (!empty($_EXTRA_URL))
  260. {
  261. foreach ($_EXTRA_URL as $url_param)
  262. {
  263. $url_param = explode('=', $url_param, 2);
  264. $s_search_hidden_fields[$url_param[0]] = $url_param[1];
  265. }
  266. }
  267. $template->assign_vars(array(
  268. 'MODERATORS' => (!empty($moderators[$forum_id])) ? implode($user->lang['COMMA_SEPARATOR'], $moderators[$forum_id]) : '',
  269. 'POST_IMG' => ($forum_data['forum_status'] == ITEM_LOCKED) ? $user->img('button_topic_locked', $post_alt) : $user->img('button_topic_new', $post_alt),
  270. 'NEWEST_POST_IMG' => $user->img('icon_topic_newest', 'VIEW_NEWEST_POST'),
  271. 'LAST_POST_IMG' => $user->img('icon_topic_latest', 'VIEW_LATEST_POST'),
  272. 'FOLDER_IMG' => $user->img('topic_read', 'NO_UNREAD_POSTS'),
  273. 'FOLDER_UNREAD_IMG' => $user->img('topic_unread', 'UNREAD_POSTS'),
  274. 'FOLDER_HOT_IMG' => $user->img('topic_read_hot', 'NO_UNREAD_POSTS_HOT'),
  275. 'FOLDER_HOT_UNREAD_IMG' => $user->img('topic_unread_hot', 'UNREAD_POSTS_HOT'),
  276. 'FOLDER_LOCKED_IMG' => $user->img('topic_read_locked', 'NO_UNREAD_POSTS_LOCKED'),
  277. 'FOLDER_LOCKED_UNREAD_IMG' => $user->img('topic_unread_locked', 'UNREAD_POSTS_LOCKED'),
  278. 'FOLDER_STICKY_IMG' => $user->img('sticky_read', 'POST_STICKY'),
  279. 'FOLDER_STICKY_UNREAD_IMG' => $user->img('sticky_unread', 'POST_STICKY'),
  280. 'FOLDER_ANNOUNCE_IMG' => $user->img('announce_read', 'POST_ANNOUNCEMENT'),
  281. 'FOLDER_ANNOUNCE_UNREAD_IMG'=> $user->img('announce_unread', 'POST_ANNOUNCEMENT'),
  282. 'FOLDER_MOVED_IMG' => $user->img('topic_moved', 'TOPIC_MOVED'),
  283. 'REPORTED_IMG' => $user->img('icon_topic_reported', 'TOPIC_REPORTED'),
  284. 'UNAPPROVED_IMG' => $user->img('icon_topic_unapproved', 'TOPIC_UNAPPROVED'),
  285. 'DELETED_IMG' => $user->img('icon_topic_deleted', 'TOPIC_DELETED'),
  286. 'POLL_IMG' => $user->img('icon_topic_poll', 'TOPIC_POLL'),
  287. 'GOTO_PAGE_IMG' => $user->img('icon_post_target', 'GOTO_PAGE'),
  288. 'L_NO_TOPICS' => ($forum_data['forum_status'] == ITEM_LOCKED) ? $user->lang['POST_FORUM_LOCKED'] : $user->lang['NO_TOPICS'],
  289. 'S_DISPLAY_POST_INFO' => ($forum_data['forum_type'] == FORUM_POST && ($auth->acl_get('f_post', $forum_id) || $user->data['user_id'] == ANONYMOUS)) ? true : false,
  290. 'S_IS_POSTABLE' => ($forum_data['forum_type'] == FORUM_POST) ? true : false,
  291. 'S_USER_CAN_POST' => ($auth->acl_get('f_post', $forum_id)) ? true : false,
  292. 'S_DISPLAY_ACTIVE' => $s_display_active,
  293. 'S_SELECT_SORT_DIR' => $s_sort_dir,
  294. 'S_SELECT_SORT_KEY' => $s_sort_key,
  295. 'S_SELECT_SORT_DAYS' => $s_limit_days,
  296. 'S_TOPIC_ICONS' => ($s_display_active && sizeof($active_forum_ary)) ? max($active_forum_ary['enable_icons']) : (($forum_data['enable_icons']) ? true : false),
  297. 'U_WATCH_FORUM_LINK' => $s_watching_forum['link'],
  298. 'U_WATCH_FORUM_TOGGLE' => $s_watching_forum['link_toggle'],
  299. 'S_WATCH_FORUM_TITLE' => $s_watching_forum['title'],
  300. 'S_WATCH_FORUM_TOGGLE' => $s_watching_forum['title_toggle'],
  301. 'S_WATCHING_FORUM' => $s_watching_forum['is_watching'],
  302. 'S_FORUM_ACTION' => append_sid("{$phpbb_root_path}viewforum.$phpEx", "f=$forum_id" . (($start == 0) ? '' : "&amp;start=$start")),
  303. 'S_DISPLAY_SEARCHBOX' => ($auth->acl_get('u_search') && $auth->acl_get('f_search', $forum_id) && $config['load_search']) ? true : false,
  304. 'S_SEARCHBOX_ACTION' => append_sid("{$phpbb_root_path}search.$phpEx"),
  305. 'S_SEARCH_LOCAL_HIDDEN_FIELDS' => build_hidden_fields($s_search_hidden_fields),
  306. 'S_SINGLE_MODERATOR' => (!empty($moderators[$forum_id]) && sizeof($moderators[$forum_id]) > 1) ? false : true,
  307. 'S_IS_LOCKED' => ($forum_data['forum_status'] == ITEM_LOCKED) ? true : false,
  308. 'S_VIEWFORUM' => true,
  309. 'U_MCP' => ($auth->acl_get('m_', $forum_id)) ? append_sid("{$phpbb_root_path}mcp.$phpEx", "f=$forum_id&amp;i=main&amp;mode=forum_view", true, $user->session_id) : '',
  310. 'U_POST_NEW_TOPIC' => ($auth->acl_get('f_post', $forum_id) || $user->data['user_id'] == ANONYMOUS) ? append_sid("{$phpbb_root_path}posting.$phpEx", 'mode=post&amp;f=' . $forum_id) : '',
  311. 'U_VIEW_FORUM' => append_sid("{$phpbb_root_path}viewforum.$phpEx", "f=$forum_id" . ((strlen($u_sort_param)) ? "&amp;$u_sort_param" : '') . (($start == 0) ? '' : "&amp;start=$start")),
  312. 'U_CANONICAL' => generate_board_url() . '/' . append_sid("viewforum.$phpEx", "f=$forum_id" . ((strlen($u_sort_param)) ? "&amp;$u_sort_param" : '') . (($start) ? "&amp;start=$start" : ''), true, ''),
  313. 'U_MARK_TOPICS' => ($user->data['is_registered'] || $config['load_anon_lastread']) ? append_sid("{$phpbb_root_path}viewforum.$phpEx", 'hash=' . generate_link_hash('global') . "&amp;f=$forum_id&amp;mark=topics&amp;mark_time=" . time()) : '',
  314. ));
  315. // Grab icons
  316. $icons = $cache->obtain_icons();
  317. // Grab all topic data
  318. $rowset = $announcement_list = $topic_list = $global_announce_forums = array();
  319. $sql_array = array(
  320. 'SELECT' => 't.*',
  321. 'FROM' => array(
  322. TOPICS_TABLE => 't'
  323. ),
  324. 'LEFT_JOIN' => array(),
  325. );
  326. /**
  327. * Event to modify the SQL query before the topic data is retrieved
  328. *
  329. * @event core.viewforum_get_topic_data
  330. * @var array sql_array The SQL array to get the data of all topics
  331. * @since 3.1.0-a1
  332. */
  333. $vars = array('sql_array');
  334. extract($phpbb_dispatcher->trigger_event('core.viewforum_get_topic_data', compact($vars)));
  335. $sql_approved = ' AND ' . $phpbb_content_visibility->get_visibility_sql('topic', $forum_id, 't.');
  336. if ($user->data['is_registered'])
  337. {
  338. if ($config['load_db_track'])
  339. {
  340. $sql_array['LEFT_JOIN'][] = array('FROM' => array(TOPICS_POSTED_TABLE => 'tp'), 'ON' => 'tp.topic_id = t.topic_id AND tp.user_id = ' . $user->data['user_id']);
  341. $sql_array['SELECT'] .= ', tp.topic_posted';
  342. }
  343. if ($config['load_db_lastread'])
  344. {
  345. $sql_array['LEFT_JOIN'][] = array('FROM' => array(TOPICS_TRACK_TABLE => 'tt'), 'ON' => 'tt.topic_id = t.topic_id AND tt.user_id = ' . $user->data['user_id']);
  346. $sql_array['SELECT'] .= ', tt.mark_time';
  347. if ($s_display_active && sizeof($active_forum_ary))
  348. {
  349. $sql_array['LEFT_JOIN'][] = array('FROM' => array(FORUMS_TRACK_TABLE => 'ft'), 'ON' => 'ft.forum_id = t.forum_id AND ft.user_id = ' . $user->data['user_id']);
  350. $sql_array['SELECT'] .= ', ft.mark_time AS forum_mark_time';
  351. }
  352. }
  353. }
  354. if ($forum_data['forum_type'] == FORUM_POST)
  355. {
  356. // Get global announcement forums
  357. $g_forum_ary = $auth->acl_getf('f_read', true);
  358. $g_forum_ary = array_unique(array_keys($g_forum_ary));
  359. $sql_anounce_array['LEFT_JOIN'] = $sql_array['LEFT_JOIN'];
  360. $sql_anounce_array['LEFT_JOIN'][] = array('FROM' => array(FORUMS_TABLE => 'f'), 'ON' => 'f.forum_id = t.forum_id');
  361. $sql_anounce_array['SELECT'] = $sql_array['SELECT'] . ', f.forum_name';
  362. // Obtain announcements ... removed sort ordering, sort by time in all cases
  363. $sql_ary = array(
  364. 'SELECT' => $sql_anounce_array['SELECT'],
  365. 'FROM' => $sql_array['FROM'],
  366. 'LEFT_JOIN' => $sql_anounce_array['LEFT_JOIN'],
  367. 'WHERE' => '(t.forum_id = ' . $forum_id . '
  368. AND t.topic_type = ' . POST_ANNOUNCE . ') OR
  369. (' . $db->sql_in_set('t.forum_id', $g_forum_ary) . '
  370. AND t.topic_type = ' . POST_GLOBAL . ')',
  371. 'ORDER_BY' => 't.topic_time DESC',
  372. );
  373. $sql = $db->sql_build_query('SELECT', $sql_ary);
  374. $result = $db->sql_query($sql);
  375. while ($row = $db->sql_fetchrow($result))
  376. {
  377. if ($row['topic_visibility'] != ITEM_APPROVED && !$auth->acl_get('m_approve', $row['forum_id']))
  378. {
  379. // Do not display announcements that are waiting for approval or soft deleted.
  380. continue;
  381. }
  382. $rowset[$row['topic_id']] = $row;
  383. $announcement_list[] = $row['topic_id'];
  384. if ($forum_id != $row['forum_id'])
  385. {
  386. $topics_count++;
  387. $global_announce_forums[] = $row['forum_id'];
  388. }
  389. }
  390. $db->sql_freeresult($result);
  391. }
  392. $forum_tracking_info = array();
  393. if ($user->data['is_registered'])
  394. {
  395. $forum_tracking_info[$forum_id] = $forum_data['mark_time'];
  396. if (!empty($global_announce_forums) && $config['load_db_lastread'])
  397. {
  398. $sql = 'SELECT forum_id, mark_time
  399. FROM ' . FORUMS_TRACK_TABLE . '
  400. WHERE ' . $db->sql_in_set('forum_id', $global_announce_forums) . '
  401. AND user_id = ' . $user->data['user_id'];
  402. $result = $db->sql_query($sql);
  403. while ($row = $db->sql_fetchrow($result))
  404. {
  405. $forum_tracking_info[$row['forum_id']] = $row['mark_time'];
  406. }
  407. $db->sql_freeresult($result);
  408. }
  409. }
  410. // If the user is trying to reach late pages, start searching from the end
  411. $store_reverse = false;
  412. $sql_limit = $config['topics_per_page'];
  413. if ($start > $topics_count / 2)
  414. {
  415. $store_reverse = true;
  416. // Select the sort order
  417. $sql_sort_order = $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'ASC' : 'DESC');
  418. $sql_limit = $pagination->reverse_limit($start, $sql_limit, $topics_count);
  419. $sql_start = $pagination->reverse_start($start, $sql_limit, $topics_count);
  420. }
  421. else
  422. {
  423. // Select the sort order
  424. $sql_sort_order = $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'DESC' : 'ASC');
  425. $sql_start = $start;
  426. }
  427. if ($forum_data['forum_type'] == FORUM_POST || !sizeof($active_forum_ary))
  428. {
  429. $sql_where = 't.forum_id = ' . $forum_id;
  430. }
  431. else if (empty($active_forum_ary['exclude_forum_id']))
  432. {
  433. $sql_where = $db->sql_in_set('t.forum_id', $active_forum_ary['forum_id']);
  434. }
  435. else
  436. {
  437. $get_forum_ids = array_diff($active_forum_ary['forum_id'], $active_forum_ary['exclude_forum_id']);
  438. $sql_where = (sizeof($get_forum_ids)) ? $db->sql_in_set('t.forum_id', $get_forum_ids) : 't.forum_id = ' . $forum_id;
  439. }
  440. // Grab just the sorted topic ids
  441. $sql = 'SELECT t.topic_id
  442. FROM ' . TOPICS_TABLE . " t
  443. WHERE $sql_where
  444. AND t.topic_type IN (" . POST_NORMAL . ', ' . POST_STICKY . ")
  445. $sql_approved
  446. $sql_limit_time
  447. ORDER BY t.topic_type " . ((!$store_reverse) ? 'DESC' : 'ASC') . ', ' . $sql_sort_order;
  448. $result = $db->sql_query_limit($sql, $sql_limit, $sql_start);
  449. while ($row = $db->sql_fetchrow($result))
  450. {
  451. $topic_list[] = (int) $row['topic_id'];
  452. }
  453. $db->sql_freeresult($result);
  454. // For storing shadow topics
  455. $shadow_topic_list = array();
  456. if (sizeof($topic_list))
  457. {
  458. // SQL array for obtaining topics/stickies
  459. $sql_array = array(
  460. 'SELECT' => $sql_array['SELECT'],
  461. 'FROM' => $sql_array['FROM'],
  462. 'LEFT_JOIN' => $sql_array['LEFT_JOIN'],
  463. 'WHERE' => $db->sql_in_set('t.topic_id', $topic_list),
  464. );
  465. // If store_reverse, then first obtain topics, then stickies, else the other way around...
  466. // Funnily enough you typically save one query if going from the last page to the middle (store_reverse) because
  467. // the number of stickies are not known
  468. $sql = $db->sql_build_query('SELECT', $sql_array);
  469. $result = $db->sql_query($sql);
  470. while ($row = $db->sql_fetchrow($result))
  471. {
  472. if ($row['topic_status'] == ITEM_MOVED)
  473. {
  474. $shadow_topic_list[$row['topic_moved_id']] = $row['topic_id'];
  475. }
  476. $rowset[$row['topic_id']] = $row;
  477. }
  478. $db->sql_freeresult($result);
  479. }
  480. // If we have some shadow topics, update the rowset to reflect their topic information
  481. if (sizeof($shadow_topic_list))
  482. {
  483. // SQL array for obtaining shadow topics
  484. $sql_array = array(
  485. 'SELECT' => 't.*',
  486. 'FROM' => array(
  487. TOPICS_TABLE => 't'
  488. ),
  489. 'WHERE' => $db->sql_in_set('t.topic_id', array_keys($shadow_topic_list)),
  490. );
  491. /**
  492. * Event to modify the SQL query before the shadowtopic data is retrieved
  493. *
  494. * @event core.viewforum_get_shadowtopic_data
  495. * @var array sql_array SQL array to get the data of any shadowtopics
  496. * @since 3.1.0-a1
  497. */
  498. $vars = array('sql_array');
  499. extract($phpbb_dispatcher->trigger_event('core.viewforum_get_shadowtopic_data', compact($vars)));
  500. $sql = $db->sql_build_query('SELECT', $sql_array);
  501. $result = $db->sql_query($sql);
  502. while ($row = $db->sql_fetchrow($result))
  503. {
  504. $orig_topic_id = $shadow_topic_list[$row['topic_id']];
  505. // If the shadow topic is already listed within the rowset (happens for active topics for example), then do not include it...
  506. if (isset($rowset[$row['topic_id']]))
  507. {
  508. // We need to remove any trace regarding this topic. :)
  509. unset($rowset[$orig_topic_id]);
  510. unset($topic_list[array_search($orig_topic_id, $topic_list)]);
  511. $topics_count--;
  512. continue;
  513. }
  514. // Do not include those topics the user has no permission to access
  515. if (!$auth->acl_get('f_read', $row['forum_id']))
  516. {
  517. // We need to remove any trace regarding this topic. :)
  518. unset($rowset[$orig_topic_id]);
  519. unset($topic_list[array_search($orig_topic_id, $topic_list)]);
  520. $topics_count--;
  521. continue;
  522. }
  523. // We want to retain some values
  524. $row = array_merge($row, array(
  525. 'topic_moved_id' => $rowset[$orig_topic_id]['topic_moved_id'],
  526. 'topic_status' => $rowset[$orig_topic_id]['topic_status'],
  527. 'topic_type' => $rowset[$orig_topic_id]['topic_type'],
  528. 'topic_title' => $rowset[$orig_topic_id]['topic_title'],
  529. ));
  530. // Shadow topics are never reported
  531. $row['topic_reported'] = 0;
  532. $rowset[$orig_topic_id] = $row;
  533. }
  534. $db->sql_freeresult($result);
  535. }
  536. unset($shadow_topic_list);
  537. // Ok, adjust topics count for active topics list
  538. if ($s_display_active)
  539. {
  540. $topics_count = 1;
  541. }
  542. // We need to remove the global announcements from the forums total topic count,
  543. // otherwise the number is different from the one on the forum list
  544. $total_topic_count = $topics_count - sizeof($global_announce_forums);
  545. $base_url = append_sid("{$phpbb_root_path}viewforum.$phpEx", "f=$forum_id" . ((strlen($u_sort_param)) ? "&amp;$u_sort_param" : ''));
  546. $pagination->generate_template_pagination($base_url, 'pagination', 'start', $topics_count, $config['topics_per_page'], $start);
  547. $template->assign_vars(array(
  548. 'TOTAL_TOPICS' => ($s_display_active) ? false : $user->lang('VIEW_FORUM_TOPICS', (int) $total_topic_count),
  549. ));
  550. $topic_list = ($store_reverse) ? array_merge($announcement_list, array_reverse($topic_list)) : array_merge($announcement_list, $topic_list);
  551. $topic_tracking_info = $tracking_topics = array();
  552. /**
  553. * Modify topics data before we display the viewforum page
  554. *
  555. * @event core.viewforum_modify_topics_data
  556. * @var array topic_list Array with current viewforum page topic ids
  557. * @var array rowset Array with topics data (in topic_id => topic_data format)
  558. * @var int total_topic_count Forum's total topic count
  559. * @since 3.1.0-b3
  560. */
  561. $vars = array('topic_list', 'rowset', 'total_topic_count');
  562. extract($phpbb_dispatcher->trigger_event('core.viewforum_modify_topics_data', compact($vars)));
  563. // Okay, lets dump out the page ...
  564. if (sizeof($topic_list))
  565. {
  566. $mark_forum_read = true;
  567. $mark_time_forum = 0;
  568. // Generate topic forum list...
  569. $topic_forum_list = array();
  570. foreach ($rowset as $t_id => $row)
  571. {
  572. if (isset($forum_tracking_info[$row['forum_id']]))
  573. {
  574. $row['forum_mark_time'] = $forum_tracking_info[$row['forum_id']];
  575. }
  576. $topic_forum_list[$row['forum_id']]['forum_mark_time'] = ($config['load_db_lastread'] && $user->data['is_registered'] && isset($row['forum_mark_time'])) ? $row['forum_mark_time'] : 0;
  577. $topic_forum_list[$row['forum_id']]['topics'][] = (int) $t_id;
  578. }
  579. if ($config['load_db_lastread'] && $user->data['is_registered'])
  580. {
  581. foreach ($topic_forum_list as $f_id => $topic_row)
  582. {
  583. $topic_tracking_info += get_topic_tracking($f_id, $topic_row['topics'], $rowset, array($f_id => $topic_row['forum_mark_time']));
  584. }
  585. }
  586. else if ($config['load_anon_lastread'] || $user->data['is_registered'])
  587. {
  588. foreach ($topic_forum_list as $f_id => $topic_row)
  589. {
  590. $topic_tracking_info += get_complete_topic_tracking($f_id, $topic_row['topics']);
  591. }
  592. }
  593. unset($topic_forum_list);
  594. if (!$s_display_active)
  595. {
  596. if ($config['load_db_lastread'] && $user->data['is_registered'])
  597. {
  598. $mark_time_forum = (!empty($forum_data['mark_time'])) ? $forum_data['mark_time'] : $user->data['user_lastmark'];
  599. }
  600. else if ($config['load_anon_lastread'] || $user->data['is_registered'])
  601. {
  602. if (!$user->data['is_registered'])
  603. {
  604. $user->data['user_lastmark'] = (isset($tracking_topics['l'])) ? (int) (base_convert($tracking_topics['l'], 36, 10) + $config['board_startdate']) : 0;
  605. }
  606. $mark_time_forum = (isset($tracking_topics['f'][$forum_id])) ? (int) (base_convert($tracking_topics['f'][$forum_id], 36, 10) + $config['board_startdate']) : $user->data['user_lastmark'];
  607. }
  608. }
  609. $s_type_switch = 0;
  610. foreach ($topic_list as $topic_id)
  611. {
  612. $row = &$rowset[$topic_id];
  613. $topic_forum_id = ($row['forum_id']) ? (int) $row['forum_id'] : $forum_id;
  614. // This will allow the style designer to output a different header
  615. // or even separate the list of announcements from sticky and normal topics
  616. $s_type_switch_test = ($row['topic_type'] == POST_ANNOUNCE || $row['topic_type'] == POST_GLOBAL) ? 1 : 0;
  617. // Replies
  618. $replies = $phpbb_content_visibility->get_count('topic_posts', $row, $topic_forum_id) - 1;
  619. if ($row['topic_status'] == ITEM_MOVED)
  620. {
  621. $topic_id = $row['topic_moved_id'];
  622. $unread_topic = false;
  623. }
  624. else
  625. {
  626. $unread_topic = (isset($topic_tracking_info[$topic_id]) && $row['topic_last_post_time'] > $topic_tracking_info[$topic_id]) ? true : false;
  627. }
  628. // Get folder img, topic status/type related information
  629. $folder_img = $folder_alt = $topic_type = '';
  630. topic_status($row, $replies, $unread_topic, $folder_img, $folder_alt, $topic_type);
  631. // Generate all the URIs ...
  632. $view_topic_url_params = 'f=' . $row['forum_id'] . '&amp;t=' . $topic_id;
  633. $view_topic_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", $view_topic_url_params);
  634. $topic_unapproved = (($row['topic_visibility'] == ITEM_UNAPPROVED || $row['topic_visibility'] == ITEM_REAPPROVE) && $auth->acl_get('m_approve', $row['forum_id']));
  635. $posts_unapproved = ($row['topic_visibility'] == ITEM_APPROVED && $row['topic_posts_unapproved'] && $auth->acl_get('m_approve', $row['forum_id']));
  636. $topic_deleted = $row['topic_visibility'] == ITEM_DELETED;
  637. $u_mcp_queue = ($topic_unapproved || $posts_unapproved) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=queue&amp;mode=' . (($topic_unapproved) ? 'approve_details' : 'unapproved_posts') . "&amp;t=$topic_id", true, $user->session_id) : '';
  638. $u_mcp_queue = (!$u_mcp_queue && $topic_deleted) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=queue&amp;mode=deleted_topics&amp;t=' . $topic_id, true, $user->session_id) : $u_mcp_queue;
  639. // Send vars to template
  640. $topic_row = array(
  641. 'FORUM_ID' => $row['forum_id'],
  642. 'TOPIC_ID' => $topic_id,
  643. 'TOPIC_AUTHOR' => get_username_string('username', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
  644. 'TOPIC_AUTHOR_COLOUR' => get_username_string('colour', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
  645. 'TOPIC_AUTHOR_FULL' => get_username_string('full', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
  646. 'FIRST_POST_TIME' => $user->format_date($row['topic_time']),
  647. 'LAST_POST_SUBJECT' => censor_text($row['topic_last_post_subject']),
  648. 'LAST_POST_TIME' => $user->format_date($row['topic_last_post_time']),
  649. 'LAST_VIEW_TIME' => $user->format_date($row['topic_last_view_time']),
  650. 'LAST_POST_AUTHOR' => get_username_string('username', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
  651. 'LAST_POST_AUTHOR_COLOUR' => get_username_string('colour', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
  652. 'LAST_POST_AUTHOR_FULL' => get_username_string('full', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
  653. 'REPLIES' => $replies,
  654. 'VIEWS' => $row['topic_views'],
  655. 'TOPIC_TITLE' => censor_text($row['topic_title']),
  656. 'TOPIC_TYPE' => $topic_type,
  657. 'FORUM_NAME' => (isset($row['forum_name'])) ? $row['forum_name'] : $forum_data['forum_name'],
  658. 'TOPIC_IMG_STYLE' => $folder_img,
  659. 'TOPIC_FOLDER_IMG' => $user->img($folder_img, $folder_alt),
  660. 'TOPIC_FOLDER_IMG_ALT' => $user->lang[$folder_alt],
  661. 'TOPIC_ICON_IMG' => (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['img'] : '',
  662. 'TOPIC_ICON_IMG_WIDTH' => (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['width'] : '',
  663. 'TOPIC_ICON_IMG_HEIGHT' => (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['height'] : '',
  664. 'ATTACH_ICON_IMG' => ($auth->acl_get('u_download') && $auth->acl_get('f_download', $row['forum_id']) && $row['topic_attachment']) ? $user->img('icon_topic_attach', $user->lang['TOTAL_ATTACHMENTS']) : '',
  665. 'UNAPPROVED_IMG' => ($topic_unapproved || $posts_unapproved) ? $user->img('icon_topic_unapproved', ($topic_unapproved) ? 'TOPIC_UNAPPROVED' : 'POSTS_UNAPPROVED') : '',
  666. 'S_TOPIC_TYPE' => $row['topic_type'],
  667. 'S_USER_POSTED' => (isset($row['topic_posted']) && $row['topic_posted']) ? true : false,
  668. 'S_UNREAD_TOPIC' => $unread_topic,
  669. 'S_TOPIC_REPORTED' => (!empty($row['topic_reported']) && $auth->acl_get('m_report', $row['forum_id'])) ? true : false,
  670. 'S_TOPIC_UNAPPROVED' => $topic_unapproved,
  671. 'S_POSTS_UNAPPROVED' => $posts_unapproved,
  672. 'S_TOPIC_DELETED' => $topic_deleted,
  673. 'S_HAS_POLL' => ($row['poll_start']) ? true : false,
  674. 'S_POST_ANNOUNCE' => ($row['topic_type'] == POST_ANNOUNCE) ? true : false,
  675. 'S_POST_GLOBAL' => ($row['topic_type'] == POST_GLOBAL) ? true : false,
  676. 'S_POST_STICKY' => ($row['topic_type'] == POST_STICKY) ? true : false,
  677. 'S_TOPIC_LOCKED' => ($row['topic_status'] == ITEM_LOCKED) ? true : false,
  678. 'S_TOPIC_MOVED' => ($row['topic_status'] == ITEM_MOVED) ? true : false,
  679. 'U_NEWEST_POST' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", $view_topic_url_params . '&amp;view=unread') . '#unread',
  680. 'U_LAST_POST' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", $view_topic_url_params . '&amp;p=' . $row['topic_last_post_id']) . '#p' . $row['topic_last_post_id'],
  681. 'U_LAST_POST_AUTHOR' => get_username_string('profile', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
  682. 'U_TOPIC_AUTHOR' => get_username_string('profile', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
  683. 'U_VIEW_TOPIC' => $view_topic_url,
  684. 'U_VIEW_FORUM' => append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $row['forum_id']),
  685. 'U_MCP_REPORT' => append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=reports&amp;mode=reports&amp;f=' . $row['forum_id'] . '&amp;t=' . $topic_id, true, $user->session_id),
  686. 'U_MCP_QUEUE' => $u_mcp_queue,
  687. 'S_TOPIC_TYPE_SWITCH' => ($s_type_switch == $s_type_switch_test) ? -1 : $s_type_switch_test,
  688. );
  689. /**
  690. * Modify the topic data before it is assigned to the template
  691. *
  692. * @event core.viewforum_modify_topicrow
  693. * @var array row Array with topic data
  694. * @var array topic_row Template array with topic data
  695. * @since 3.1.0-a1
  696. */
  697. $vars = array('row', 'topic_row');
  698. extract($phpbb_dispatcher->trigger_event('core.viewforum_modify_topicrow', compact($vars)));
  699. $template->assign_block_vars('topicrow', $topic_row);
  700. $pagination->generate_template_pagination($view_topic_url, 'topicrow.pagination', 'start', $replies + 1, $config['posts_per_page'], 1, true, true);
  701. $s_type_switch = ($row['topic_type'] == POST_ANNOUNCE || $row['topic_type'] == POST_GLOBAL) ? 1 : 0;
  702. if ($unread_topic)
  703. {
  704. $mark_forum_read = false;
  705. }
  706. unset($rowset[$topic_id]);
  707. }
  708. }
  709. // This is rather a fudge but it's the best I can think of without requiring information
  710. // on all topics (as we do in 2.0.x). It looks for unread or new topics, if it doesn't find
  711. // any it updates the forum last read cookie. This requires that the user visit the forum
  712. // after reading a topic
  713. if ($forum_data['forum_type'] == FORUM_POST && sizeof($topic_list) && $mark_forum_read)
  714. {
  715. update_forum_tracking_info($forum_id, $forum_data['forum_last_post_time'], false, $mark_time_forum);
  716. }
  717. page_footer();