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