viewforum.php
changeset 0 ceef6d64c21d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/viewforum.php	Wed Mar 07 16:41:10 2012 +0400
     1.3 @@ -0,0 +1,731 @@
     1.4 +<?php
     1.5 +/**
     1.6 +*
     1.7 +* @package phpBB3
     1.8 +* @version $Id$
     1.9 +* @copyright (c) 2005 phpBB Group
    1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
    1.11 +*
    1.12 +*/
    1.13 +
    1.14 +/**
    1.15 +* @ignore
    1.16 +*/
    1.17 +define('IN_PHPBB', true);
    1.18 +$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
    1.19 +$phpEx = substr(strrchr(__FILE__, '.'), 1);
    1.20 +include($phpbb_root_path . 'common.' . $phpEx);
    1.21 +include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
    1.22 +
    1.23 +// Start session
    1.24 +$user->session_begin();
    1.25 +$auth->acl($user->data);
    1.26 +
    1.27 +// Start initial var setup
    1.28 +$forum_id	= request_var('f', 0);
    1.29 +$mark_read	= request_var('mark', '');
    1.30 +$start		= request_var('start', 0);
    1.31 +
    1.32 +$default_sort_days	= (!empty($user->data['user_topic_show_days'])) ? $user->data['user_topic_show_days'] : 0;
    1.33 +$default_sort_key	= (!empty($user->data['user_topic_sortby_type'])) ? $user->data['user_topic_sortby_type'] : 't';
    1.34 +$default_sort_dir	= (!empty($user->data['user_topic_sortby_dir'])) ? $user->data['user_topic_sortby_dir'] : 'd';
    1.35 +
    1.36 +$sort_days	= request_var('st', $default_sort_days);
    1.37 +$sort_key	= request_var('sk', $default_sort_key);
    1.38 +$sort_dir	= request_var('sd', $default_sort_dir);
    1.39 +
    1.40 +// Check if the user has actually sent a forum ID with his/her request
    1.41 +// If not give them a nice error page.
    1.42 +if (!$forum_id)
    1.43 +{
    1.44 +	trigger_error('NO_FORUM');
    1.45 +}
    1.46 +
    1.47 +$sql_from = FORUMS_TABLE . ' f';
    1.48 +$lastread_select = '';
    1.49 +
    1.50 +// Grab appropriate forum data
    1.51 +if ($config['load_db_lastread'] && $user->data['is_registered'])
    1.52 +{
    1.53 +	$sql_from .= ' LEFT JOIN ' . FORUMS_TRACK_TABLE . ' ft ON (ft.user_id = ' . $user->data['user_id'] . '
    1.54 +		AND ft.forum_id = f.forum_id)';
    1.55 +	$lastread_select .= ', ft.mark_time';
    1.56 +}
    1.57 +
    1.58 +if ($user->data['is_registered'])
    1.59 +{
    1.60 +	$sql_from .= ' LEFT JOIN ' . FORUMS_WATCH_TABLE . ' fw ON (fw.forum_id = f.forum_id AND fw.user_id = ' . $user->data['user_id'] . ')';
    1.61 +	$lastread_select .= ', fw.notify_status';
    1.62 +}
    1.63 +
    1.64 +$sql = "SELECT f.* $lastread_select
    1.65 +	FROM $sql_from
    1.66 +	WHERE f.forum_id = $forum_id";
    1.67 +$result = $db->sql_query($sql);
    1.68 +$forum_data = $db->sql_fetchrow($result);
    1.69 +$db->sql_freeresult($result);
    1.70 +
    1.71 +if (!$forum_data)
    1.72 +{
    1.73 +	trigger_error('NO_FORUM');
    1.74 +}
    1.75 +
    1.76 +
    1.77 +// Configure style, language, etc.
    1.78 +$user->setup('viewforum', $forum_data['forum_style']);
    1.79 +
    1.80 +// Redirect to login upon emailed notification links
    1.81 +if (isset($_GET['e']) && !$user->data['is_registered'])
    1.82 +{
    1.83 +	login_box('', $user->lang['LOGIN_NOTIFY_FORUM']);
    1.84 +}
    1.85 +
    1.86 +// Permissions check
    1.87 +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)))
    1.88 +{
    1.89 +	if ($user->data['user_id'] != ANONYMOUS)
    1.90 +	{
    1.91 +		trigger_error('SORRY_AUTH_READ');
    1.92 +	}
    1.93 +
    1.94 +	login_box('', $user->lang['LOGIN_VIEWFORUM']);
    1.95 +}
    1.96 +
    1.97 +// Forum is passworded ... check whether access has been granted to this
    1.98 +// user this session, if not show login box
    1.99 +if ($forum_data['forum_password'])
   1.100 +{
   1.101 +	login_forum_box($forum_data);
   1.102 +}
   1.103 +
   1.104 +// Is this forum a link? ... User got here either because the
   1.105 +// number of clicks is being tracked or they guessed the id
   1.106 +if ($forum_data['forum_type'] == FORUM_LINK && $forum_data['forum_link'])
   1.107 +{
   1.108 +	// Does it have click tracking enabled?
   1.109 +	if ($forum_data['forum_flags'] & FORUM_FLAG_LINK_TRACK)
   1.110 +	{
   1.111 +		$sql = 'UPDATE ' . FORUMS_TABLE . '
   1.112 +			SET forum_posts = forum_posts + 1
   1.113 +			WHERE forum_id = ' . $forum_id;
   1.114 +		$db->sql_query($sql);
   1.115 +	}
   1.116 +
   1.117 +	// We redirect to the url. The third parameter indicates that external redirects are allowed.
   1.118 +	redirect($forum_data['forum_link'], false, true);
   1.119 +	return;
   1.120 +}
   1.121 +
   1.122 +// Build navigation links
   1.123 +generate_forum_nav($forum_data);
   1.124 +
   1.125 +// Forum Rules
   1.126 +if ($auth->acl_get('f_read', $forum_id))
   1.127 +{
   1.128 +	generate_forum_rules($forum_data);
   1.129 +}
   1.130 +
   1.131 +// Do we have subforums?
   1.132 +$active_forum_ary = $moderators = array();
   1.133 +
   1.134 +if ($forum_data['left_id'] != $forum_data['right_id'] - 1)
   1.135 +{
   1.136 +	list($active_forum_ary, $moderators) = display_forums($forum_data, $config['load_moderators'], $config['load_moderators']);
   1.137 +}
   1.138 +else
   1.139 +{
   1.140 +	$template->assign_var('S_HAS_SUBFORUM', false);
   1.141 +	if ($config['load_moderators'])
   1.142 +	{
   1.143 +		get_moderators($moderators, $forum_id);
   1.144 +	}
   1.145 +}
   1.146 +
   1.147 +// Dump out the page header and load viewforum template
   1.148 +page_header($user->lang['VIEW_FORUM'] . ' - ' . $forum_data['forum_name'], true, $forum_id);
   1.149 +
   1.150 +$template->set_filenames(array(
   1.151 +	'body' => 'viewforum_body.html')
   1.152 +);
   1.153 +
   1.154 +make_jumpbox(append_sid("{$phpbb_root_path}viewforum.$phpEx"), $forum_id);
   1.155 +
   1.156 +$template->assign_vars(array(
   1.157 +	'U_VIEW_FORUM'			=> append_sid("{$phpbb_root_path}viewforum.$phpEx", "f=$forum_id" . (($start == 0) ? '' : "&amp;start=$start")),
   1.158 +));
   1.159 +
   1.160 +// Not postable forum or showing active topics?
   1.161 +if (!($forum_data['forum_type'] == FORUM_POST || (($forum_data['forum_flags'] & FORUM_FLAG_ACTIVE_TOPICS) && $forum_data['forum_type'] == FORUM_CAT)))
   1.162 +{
   1.163 +	page_footer();
   1.164 +}
   1.165 +
   1.166 +// Ok, if someone has only list-access, we only display the forum list.
   1.167 +// We also make this circumstance available to the template in case we want to display a notice. ;)
   1.168 +if (!$auth->acl_get('f_read', $forum_id))
   1.169 +{
   1.170 +	$template->assign_vars(array(
   1.171 +		'S_NO_READ_ACCESS'		=> true,
   1.172 +	));
   1.173 +
   1.174 +	page_footer();
   1.175 +}
   1.176 +
   1.177 +// Handle marking posts
   1.178 +if ($mark_read == 'topics')
   1.179 +{
   1.180 +	$token = request_var('hash', '');
   1.181 +	if (check_link_hash($token, 'global'))
   1.182 +	{
   1.183 +		// Add 0 to forums array to mark global announcements correctly
   1.184 +		markread('topics', array($forum_id, 0));
   1.185 +	}
   1.186 +	$redirect_url = append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_id);
   1.187 +	meta_refresh(3, $redirect_url);
   1.188 +
   1.189 +	trigger_error($user->lang['TOPICS_MARKED'] . '<br /><br />' . sprintf($user->lang['RETURN_FORUM'], '<a href="' . $redirect_url . '">', '</a>'));
   1.190 +}
   1.191 +
   1.192 +// Is a forum specific topic count required?
   1.193 +if ($forum_data['forum_topics_per_page'])
   1.194 +{
   1.195 +	$config['topics_per_page'] = $forum_data['forum_topics_per_page'];
   1.196 +}
   1.197 +
   1.198 +// Do the forum Prune thang - cron type job ...
   1.199 +if ($forum_data['prune_next'] < time() && $forum_data['enable_prune'])
   1.200 +{
   1.201 +	$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" />');
   1.202 +}
   1.203 +
   1.204 +// Forum rules and subscription info
   1.205 +$s_watching_forum = array(
   1.206 +	'link'			=> '',
   1.207 +	'title'			=> '',
   1.208 +	'is_watching'	=> false,
   1.209 +);
   1.210 +
   1.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))
   1.212 +{
   1.213 +	$notify_status = (isset($forum_data['notify_status'])) ? $forum_data['notify_status'] : NULL;
   1.214 +	watch_topic_forum('forum', $s_watching_forum, $user->data['user_id'], $forum_id, 0, $notify_status, $start, $forum_data['forum_name']);
   1.215 +}
   1.216 +
   1.217 +$s_forum_rules = '';
   1.218 +gen_forum_auth_level('forum', $forum_id, $forum_data['forum_status']);
   1.219 +
   1.220 +// Topic ordering options
   1.221 +$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']);
   1.222 +
   1.223 +$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']);
   1.224 +$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');
   1.225 +
   1.226 +$s_limit_days = $s_sort_key = $s_sort_dir = $u_sort_param = '';
   1.227 +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);
   1.228 +
   1.229 +// Limit topics to certain time frame, obtain correct topic count
   1.230 +// global announcements must not be counted, normal announcements have to
   1.231 +// be counted, as forum_topics(_real) includes them
   1.232 +if ($sort_days)
   1.233 +{
   1.234 +	$min_post_time = time() - ($sort_days * 86400);
   1.235 +
   1.236 +	$sql = 'SELECT COUNT(topic_id) AS num_topics
   1.237 +		FROM ' . TOPICS_TABLE . "
   1.238 +		WHERE forum_id = $forum_id
   1.239 +			AND ((topic_type <> " . POST_GLOBAL . " AND topic_last_post_time >= $min_post_time)
   1.240 +				OR topic_type = " . POST_ANNOUNCE . ")
   1.241 +		" . (($auth->acl_get('m_approve', $forum_id)) ? '' : 'AND topic_approved = 1');
   1.242 +	$result = $db->sql_query($sql);
   1.243 +	$topics_count = (int) $db->sql_fetchfield('num_topics');
   1.244 +	$db->sql_freeresult($result);
   1.245 +
   1.246 +	if (isset($_POST['sort']))
   1.247 +	{
   1.248 +		$start = 0;
   1.249 +	}
   1.250 +	$sql_limit_time = "AND t.topic_last_post_time >= $min_post_time";
   1.251 +
   1.252 +	// Make sure we have information about day selection ready
   1.253 +	$template->assign_var('S_SORT_DAYS', true);
   1.254 +}
   1.255 +else
   1.256 +{
   1.257 +	$topics_count = ($auth->acl_get('m_approve', $forum_id)) ? $forum_data['forum_topics_real'] : $forum_data['forum_topics'];
   1.258 +	$sql_limit_time = '';
   1.259 +}
   1.260 +
   1.261 +// Make sure $start is set to the last page if it exceeds the amount
   1.262 +if ($start < 0 || $start > $topics_count)
   1.263 +{
   1.264 +	$start = ($start < 0) ? 0 : floor(($topics_count - 1) / $config['topics_per_page']) * $config['topics_per_page'];
   1.265 +}
   1.266 +
   1.267 +// Basic pagewide vars
   1.268 +$post_alt = ($forum_data['forum_status'] == ITEM_LOCKED) ? $user->lang['FORUM_LOCKED'] : $user->lang['POST_NEW_TOPIC'];
   1.269 +
   1.270 +// Display active topics?
   1.271 +$s_display_active = ($forum_data['forum_type'] == FORUM_CAT && ($forum_data['forum_flags'] & FORUM_FLAG_ACTIVE_TOPICS)) ? true : false;
   1.272 +
   1.273 +$s_search_hidden_fields = array('fid' => array($forum_id));
   1.274 +if ($_SID)
   1.275 +{
   1.276 +	$s_search_hidden_fields['sid'] = $_SID;
   1.277 +}
   1.278 +
   1.279 +if (!empty($_EXTRA_URL))
   1.280 +{
   1.281 +	foreach ($_EXTRA_URL as $url_param)
   1.282 +	{
   1.283 +		$url_param = explode('=', $url_param, 2);
   1.284 +		$s_hidden_fields[$url_param[0]] = $url_param[1];
   1.285 +	}
   1.286 +}
   1.287 +
   1.288 +$template->assign_vars(array(
   1.289 +	'MODERATORS'	=> (!empty($moderators[$forum_id])) ? implode(', ', $moderators[$forum_id]) : '',
   1.290 +
   1.291 +	'POST_IMG'					=> ($forum_data['forum_status'] == ITEM_LOCKED) ? $user->img('button_topic_locked', $post_alt) : $user->img('button_topic_new', $post_alt),
   1.292 +	'NEWEST_POST_IMG'			=> $user->img('icon_topic_newest', 'VIEW_NEWEST_POST'),
   1.293 +	'LAST_POST_IMG'				=> $user->img('icon_topic_latest', 'VIEW_LATEST_POST'),
   1.294 +	'FOLDER_IMG'				=> $user->img('topic_read', 'NO_UNREAD_POSTS'),
   1.295 +	'FOLDER_UNREAD_IMG'			=> $user->img('topic_unread', 'UNREAD_POSTS'),
   1.296 +	'FOLDER_HOT_IMG'			=> $user->img('topic_read_hot', 'NO_UNREAD_POSTS_HOT'),
   1.297 +	'FOLDER_HOT_UNREAD_IMG'		=> $user->img('topic_unread_hot', 'UNREAD_POSTS_HOT'),
   1.298 +	'FOLDER_LOCKED_IMG'			=> $user->img('topic_read_locked', 'NO_UNREAD_POSTS_LOCKED'),
   1.299 +	'FOLDER_LOCKED_UNREAD_IMG'	=> $user->img('topic_unread_locked', 'UNREAD_POSTS_LOCKED'),
   1.300 +	'FOLDER_STICKY_IMG'			=> $user->img('sticky_read', 'POST_STICKY'),
   1.301 +	'FOLDER_STICKY_UNREAD_IMG'	=> $user->img('sticky_unread', 'POST_STICKY'),
   1.302 +	'FOLDER_ANNOUNCE_IMG'		=> $user->img('announce_read', 'POST_ANNOUNCEMENT'),
   1.303 +	'FOLDER_ANNOUNCE_UNREAD_IMG'=> $user->img('announce_unread', 'POST_ANNOUNCEMENT'),
   1.304 +	'FOLDER_MOVED_IMG'			=> $user->img('topic_moved', 'TOPIC_MOVED'),
   1.305 +	'REPORTED_IMG'				=> $user->img('icon_topic_reported', 'TOPIC_REPORTED'),
   1.306 +	'UNAPPROVED_IMG'			=> $user->img('icon_topic_unapproved', 'TOPIC_UNAPPROVED'),
   1.307 +	'GOTO_PAGE_IMG'				=> $user->img('icon_post_target', 'GOTO_PAGE'),
   1.308 +
   1.309 +	'L_NO_TOPICS' 			=> ($forum_data['forum_status'] == ITEM_LOCKED) ? $user->lang['POST_FORUM_LOCKED'] : $user->lang['NO_TOPICS'],
   1.310 +
   1.311 +	'S_DISPLAY_POST_INFO'	=> ($forum_data['forum_type'] == FORUM_POST && ($auth->acl_get('f_post', $forum_id) || $user->data['user_id'] == ANONYMOUS)) ? true : false,
   1.312 +
   1.313 +	'S_IS_POSTABLE'			=> ($forum_data['forum_type'] == FORUM_POST) ? true : false,
   1.314 +	'S_USER_CAN_POST'		=> ($auth->acl_get('f_post', $forum_id)) ? true : false,
   1.315 +	'S_DISPLAY_ACTIVE'		=> $s_display_active,
   1.316 +	'S_SELECT_SORT_DIR'		=> $s_sort_dir,
   1.317 +	'S_SELECT_SORT_KEY'		=> $s_sort_key,
   1.318 +	'S_SELECT_SORT_DAYS'	=> $s_limit_days,
   1.319 +	'S_TOPIC_ICONS'			=> ($s_display_active && sizeof($active_forum_ary)) ? max($active_forum_ary['enable_icons']) : (($forum_data['enable_icons']) ? true : false),
   1.320 +	'S_WATCH_FORUM_LINK'	=> $s_watching_forum['link'],
   1.321 +	'S_WATCH_FORUM_TITLE'	=> $s_watching_forum['title'],
   1.322 +	'S_WATCHING_FORUM'		=> $s_watching_forum['is_watching'],
   1.323 +	'S_FORUM_ACTION'		=> append_sid("{$phpbb_root_path}viewforum.$phpEx", "f=$forum_id" . (($start == 0) ? '' : "&amp;start=$start")),
   1.324 +	'S_DISPLAY_SEARCHBOX'	=> ($auth->acl_get('u_search') && $auth->acl_get('f_search', $forum_id) && $config['load_search']) ? true : false,
   1.325 +	'S_SEARCHBOX_ACTION'	=> append_sid("{$phpbb_root_path}search.$phpEx"),
   1.326 +	'S_SEARCH_LOCAL_HIDDEN_FIELDS'	=> build_hidden_fields($s_search_hidden_fields),
   1.327 +	'S_SINGLE_MODERATOR'	=> (!empty($moderators[$forum_id]) && sizeof($moderators[$forum_id]) > 1) ? false : true,
   1.328 +	'S_IS_LOCKED'			=> ($forum_data['forum_status'] == ITEM_LOCKED) ? true : false,
   1.329 +	'S_VIEWFORUM'			=> true,
   1.330 +
   1.331 +	'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) : '',
   1.332 +	'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) : '',
   1.333 +	'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")),
   1.334 +	'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") : '',
   1.335 +));
   1.336 +
   1.337 +// Grab icons
   1.338 +$icons = $cache->obtain_icons();
   1.339 +
   1.340 +// Grab all topic data
   1.341 +$rowset = $announcement_list = $topic_list = $global_announce_list = array();
   1.342 +
   1.343 +$sql_array = array(
   1.344 +	'SELECT'	=> 't.*',
   1.345 +	'FROM'		=> array(
   1.346 +		TOPICS_TABLE		=> 't'
   1.347 +	),
   1.348 +	'LEFT_JOIN'	=> array(),
   1.349 +);
   1.350 +
   1.351 +$sql_approved = ($auth->acl_get('m_approve', $forum_id)) ? '' : 'AND t.topic_approved = 1';
   1.352 +
   1.353 +if ($user->data['is_registered'])
   1.354 +{
   1.355 +	if ($config['load_db_track'])
   1.356 +	{
   1.357 +		$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']);
   1.358 +		$sql_array['SELECT'] .= ', tp.topic_posted';
   1.359 +	}
   1.360 +
   1.361 +	if ($config['load_db_lastread'])
   1.362 +	{
   1.363 +		$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']);
   1.364 +		$sql_array['SELECT'] .= ', tt.mark_time';
   1.365 +
   1.366 +		if ($s_display_active && sizeof($active_forum_ary))
   1.367 +		{
   1.368 +			$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']);
   1.369 +			$sql_array['SELECT'] .= ', ft.mark_time AS forum_mark_time';
   1.370 +		}
   1.371 +	}
   1.372 +}
   1.373 +
   1.374 +if ($forum_data['forum_type'] == FORUM_POST)
   1.375 +{
   1.376 +	// Obtain announcements ... removed sort ordering, sort by time in all cases
   1.377 +	$sql = $db->sql_build_query('SELECT', array(
   1.378 +		'SELECT'	=> $sql_array['SELECT'],
   1.379 +		'FROM'		=> $sql_array['FROM'],
   1.380 +		'LEFT_JOIN'	=> $sql_array['LEFT_JOIN'],
   1.381 +
   1.382 +		'WHERE'		=> 't.forum_id IN (' . $forum_id . ', 0)
   1.383 +			AND t.topic_type IN (' . POST_ANNOUNCE . ', ' . POST_GLOBAL . ')',
   1.384 +
   1.385 +		'ORDER_BY'	=> 't.topic_time DESC',
   1.386 +	));
   1.387 +	$result = $db->sql_query($sql);
   1.388 +
   1.389 +	while ($row = $db->sql_fetchrow($result))
   1.390 +	{
   1.391 +		if (!$row['topic_approved'] && !$auth->acl_get('m_approve', $row['forum_id']))
   1.392 +		{
   1.393 +			// Do not display announcements that are waiting for approval.
   1.394 +			continue;
   1.395 +		}
   1.396 +
   1.397 +		$rowset[$row['topic_id']] = $row;
   1.398 +		$announcement_list[] = $row['topic_id'];
   1.399 +
   1.400 +		if ($row['topic_type'] == POST_GLOBAL)
   1.401 +		{
   1.402 +			$global_announce_list[$row['topic_id']] = true;
   1.403 +		}
   1.404 +		else
   1.405 +		{
   1.406 +			$topics_count--;
   1.407 +		}
   1.408 +	}
   1.409 +	$db->sql_freeresult($result);
   1.410 +}
   1.411 +
   1.412 +// If the user is trying to reach late pages, start searching from the end
   1.413 +$store_reverse = false;
   1.414 +$sql_limit = $config['topics_per_page'];
   1.415 +if ($start > $topics_count / 2)
   1.416 +{
   1.417 +	$store_reverse = true;
   1.418 +
   1.419 +	if ($start + $config['topics_per_page'] > $topics_count)
   1.420 +	{
   1.421 +		$sql_limit = min($config['topics_per_page'], max(1, $topics_count - $start));
   1.422 +	}
   1.423 +
   1.424 +	// Select the sort order
   1.425 +	$sql_sort_order = $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'ASC' : 'DESC');
   1.426 +	$sql_start = max(0, $topics_count - $sql_limit - $start);
   1.427 +}
   1.428 +else
   1.429 +{
   1.430 +	// Select the sort order
   1.431 +	$sql_sort_order = $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'DESC' : 'ASC');
   1.432 +	$sql_start = $start;
   1.433 +}
   1.434 +
   1.435 +if ($forum_data['forum_type'] == FORUM_POST || !sizeof($active_forum_ary))
   1.436 +{
   1.437 +	$sql_where = 't.forum_id = ' . $forum_id;
   1.438 +}
   1.439 +else if (empty($active_forum_ary['exclude_forum_id']))
   1.440 +{
   1.441 +	$sql_where = $db->sql_in_set('t.forum_id', $active_forum_ary['forum_id']);
   1.442 +}
   1.443 +else
   1.444 +{
   1.445 +	$get_forum_ids = array_diff($active_forum_ary['forum_id'], $active_forum_ary['exclude_forum_id']);
   1.446 +	$sql_where = (sizeof($get_forum_ids)) ? $db->sql_in_set('t.forum_id', $get_forum_ids) : 't.forum_id = ' . $forum_id;
   1.447 +}
   1.448 +
   1.449 +// Grab just the sorted topic ids
   1.450 +$sql = 'SELECT t.topic_id
   1.451 +	FROM ' . TOPICS_TABLE . " t
   1.452 +	WHERE $sql_where
   1.453 +		AND t.topic_type IN (" . POST_NORMAL . ', ' . POST_STICKY . ")
   1.454 +		$sql_approved
   1.455 +		$sql_limit_time
   1.456 +	ORDER BY t.topic_type " . ((!$store_reverse) ? 'DESC' : 'ASC') . ', ' . $sql_sort_order;
   1.457 +$result = $db->sql_query_limit($sql, $sql_limit, $sql_start);
   1.458 +
   1.459 +while ($row = $db->sql_fetchrow($result))
   1.460 +{
   1.461 +	$topic_list[] = (int) $row['topic_id'];
   1.462 +}
   1.463 +$db->sql_freeresult($result);
   1.464 +
   1.465 +// For storing shadow topics
   1.466 +$shadow_topic_list = array();
   1.467 +
   1.468 +if (sizeof($topic_list))
   1.469 +{
   1.470 +	// SQL array for obtaining topics/stickies
   1.471 +	$sql_array = array(
   1.472 +		'SELECT'		=> $sql_array['SELECT'],
   1.473 +		'FROM'			=> $sql_array['FROM'],
   1.474 +		'LEFT_JOIN'		=> $sql_array['LEFT_JOIN'],
   1.475 +
   1.476 +		'WHERE'			=> $db->sql_in_set('t.topic_id', $topic_list),
   1.477 +	);
   1.478 +
   1.479 +	// If store_reverse, then first obtain topics, then stickies, else the other way around...
   1.480 +	// Funnily enough you typically save one query if going from the last page to the middle (store_reverse) because
   1.481 +	// the number of stickies are not known
   1.482 +	$sql = $db->sql_build_query('SELECT', $sql_array);
   1.483 +	$result = $db->sql_query($sql);
   1.484 +
   1.485 +	while ($row = $db->sql_fetchrow($result))
   1.486 +	{
   1.487 +		if ($row['topic_status'] == ITEM_MOVED)
   1.488 +		{
   1.489 +			$shadow_topic_list[$row['topic_moved_id']] = $row['topic_id'];
   1.490 +		}
   1.491 +
   1.492 +		$rowset[$row['topic_id']] = $row;
   1.493 +	}
   1.494 +	$db->sql_freeresult($result);
   1.495 +}
   1.496 +
   1.497 +// If we have some shadow topics, update the rowset to reflect their topic information
   1.498 +if (sizeof($shadow_topic_list))
   1.499 +{
   1.500 +	$sql = 'SELECT *
   1.501 +		FROM ' . TOPICS_TABLE . '
   1.502 +		WHERE ' . $db->sql_in_set('topic_id', array_keys($shadow_topic_list));
   1.503 +	$result = $db->sql_query($sql);
   1.504 +
   1.505 +	while ($row = $db->sql_fetchrow($result))
   1.506 +	{
   1.507 +		$orig_topic_id = $shadow_topic_list[$row['topic_id']];
   1.508 +
   1.509 +		// If the shadow topic is already listed within the rowset (happens for active topics for example), then do not include it...
   1.510 +		if (isset($rowset[$row['topic_id']]))
   1.511 +		{
   1.512 +			// We need to remove any trace regarding this topic. :)
   1.513 +			unset($rowset[$orig_topic_id]);
   1.514 +			unset($topic_list[array_search($orig_topic_id, $topic_list)]);
   1.515 +			$topics_count--;
   1.516 +
   1.517 +			continue;
   1.518 +		}
   1.519 +
   1.520 +		// Do not include those topics the user has no permission to access
   1.521 +		if (!$auth->acl_get('f_read', $row['forum_id']))
   1.522 +		{
   1.523 +			// We need to remove any trace regarding this topic. :)
   1.524 +			unset($rowset[$orig_topic_id]);
   1.525 +			unset($topic_list[array_search($orig_topic_id, $topic_list)]);
   1.526 +			$topics_count--;
   1.527 +
   1.528 +			continue;
   1.529 +		}
   1.530 +
   1.531 +		// We want to retain some values
   1.532 +		$row = array_merge($row, array(
   1.533 +			'topic_moved_id'	=> $rowset[$orig_topic_id]['topic_moved_id'],
   1.534 +			'topic_status'		=> $rowset[$orig_topic_id]['topic_status'],
   1.535 +			'topic_type'		=> $rowset[$orig_topic_id]['topic_type'],
   1.536 +			'topic_title'		=> $rowset[$orig_topic_id]['topic_title'],
   1.537 +		));
   1.538 +
   1.539 +		// Shadow topics are never reported
   1.540 +		$row['topic_reported'] = 0;
   1.541 +
   1.542 +		$rowset[$orig_topic_id] = $row;
   1.543 +	}
   1.544 +	$db->sql_freeresult($result);
   1.545 +}
   1.546 +unset($shadow_topic_list);
   1.547 +
   1.548 +// Ok, adjust topics count for active topics list
   1.549 +if ($s_display_active)
   1.550 +{
   1.551 +	$topics_count = 1;
   1.552 +}
   1.553 +
   1.554 +// 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
   1.555 +$total_topic_count = $topics_count + sizeof($announcement_list) - sizeof($global_announce_list);
   1.556 +
   1.557 +$template->assign_vars(array(
   1.558 +	'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),
   1.559 +	'PAGE_NUMBER'	=> on_page($topics_count, $config['topics_per_page'], $start),
   1.560 +	'TOTAL_TOPICS'	=> ($s_display_active) ? false : (($total_topic_count == 1) ? $user->lang['VIEW_FORUM_TOPIC'] : sprintf($user->lang['VIEW_FORUM_TOPICS'], $total_topic_count)))
   1.561 +);
   1.562 +
   1.563 +$topic_list = ($store_reverse) ? array_merge($announcement_list, array_reverse($topic_list)) : array_merge($announcement_list, $topic_list);
   1.564 +$topic_tracking_info = $tracking_topics = array();
   1.565 +
   1.566 +// Okay, lets dump out the page ...
   1.567 +if (sizeof($topic_list))
   1.568 +{
   1.569 +	$mark_forum_read = true;
   1.570 +	$mark_time_forum = 0;
   1.571 +
   1.572 +	// Active topics?
   1.573 +	if ($s_display_active && sizeof($active_forum_ary))
   1.574 +	{
   1.575 +		// Generate topic forum list...
   1.576 +		$topic_forum_list = array();
   1.577 +		foreach ($rowset as $t_id => $row)
   1.578 +		{
   1.579 +			$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;
   1.580 +			$topic_forum_list[$row['forum_id']]['topics'][] = $t_id;
   1.581 +		}
   1.582 +
   1.583 +		if ($config['load_db_lastread'] && $user->data['is_registered'])
   1.584 +		{
   1.585 +			foreach ($topic_forum_list as $f_id => $topic_row)
   1.586 +			{
   1.587 +				$topic_tracking_info += get_topic_tracking($f_id, $topic_row['topics'], $rowset, array($f_id => $topic_row['forum_mark_time']), false);
   1.588 +			}
   1.589 +		}
   1.590 +		else if ($config['load_anon_lastread'] || $user->data['is_registered'])
   1.591 +		{
   1.592 +			foreach ($topic_forum_list as $f_id => $topic_row)
   1.593 +			{
   1.594 +				$topic_tracking_info += get_complete_topic_tracking($f_id, $topic_row['topics'], false);
   1.595 +			}
   1.596 +		}
   1.597 +
   1.598 +		unset($topic_forum_list);
   1.599 +	}
   1.600 +	else
   1.601 +	{
   1.602 +		if ($config['load_db_lastread'] && $user->data['is_registered'])
   1.603 +		{
   1.604 +			$topic_tracking_info = get_topic_tracking($forum_id, $topic_list, $rowset, array($forum_id => $forum_data['mark_time']), $global_announce_list);
   1.605 +			$mark_time_forum = (!empty($forum_data['mark_time'])) ? $forum_data['mark_time'] : $user->data['user_lastmark'];
   1.606 +		}
   1.607 +		else if ($config['load_anon_lastread'] || $user->data['is_registered'])
   1.608 +		{
   1.609 +			$topic_tracking_info = get_complete_topic_tracking($forum_id, $topic_list, $global_announce_list);
   1.610 +
   1.611 +			if (!$user->data['is_registered'])
   1.612 +			{
   1.613 +				$user->data['user_lastmark'] = (isset($tracking_topics['l'])) ? (int) (base_convert($tracking_topics['l'], 36, 10) + $config['board_startdate']) : 0;
   1.614 +			}
   1.615 +			$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'];
   1.616 +		}
   1.617 +	}
   1.618 +
   1.619 +	$s_type_switch = 0;
   1.620 +	foreach ($topic_list as $topic_id)
   1.621 +	{
   1.622 +		$row = &$rowset[$topic_id];
   1.623 +
   1.624 +		$topic_forum_id = ($row['forum_id']) ? (int) $row['forum_id'] : $forum_id;
   1.625 +
   1.626 +		// This will allow the style designer to output a different header
   1.627 +		// or even separate the list of announcements from sticky and normal topics
   1.628 +		$s_type_switch_test = ($row['topic_type'] == POST_ANNOUNCE || $row['topic_type'] == POST_GLOBAL) ? 1 : 0;
   1.629 +
   1.630 +		// Replies
   1.631 +		$replies = ($auth->acl_get('m_approve', $topic_forum_id)) ? $row['topic_replies_real'] : $row['topic_replies'];
   1.632 +
   1.633 +		if ($row['topic_status'] == ITEM_MOVED)
   1.634 +		{
   1.635 +			$topic_id = $row['topic_moved_id'];
   1.636 +			$unread_topic = false;
   1.637 +		}
   1.638 +		else
   1.639 +		{
   1.640 +			$unread_topic = (isset($topic_tracking_info[$topic_id]) && $row['topic_last_post_time'] > $topic_tracking_info[$topic_id]) ? true : false;
   1.641 +		}
   1.642 +
   1.643 +		// Get folder img, topic status/type related information
   1.644 +		$folder_img = $folder_alt = $topic_type = '';
   1.645 +		topic_status($row, $replies, $unread_topic, $folder_img, $folder_alt, $topic_type);
   1.646 +
   1.647 +		// Generate all the URIs ...
   1.648 +		$view_topic_url_params = 'f=' . $topic_forum_id . '&amp;t=' . $topic_id;
   1.649 +		$view_topic_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", $view_topic_url_params);
   1.650 +
   1.651 +		$topic_unapproved = (!$row['topic_approved'] && $auth->acl_get('m_approve', $topic_forum_id)) ? true : false;
   1.652 +		$posts_unapproved = ($row['topic_approved'] && $row['topic_replies'] < $row['topic_replies_real'] && $auth->acl_get('m_approve', $topic_forum_id)) ? true : false;
   1.653 +		$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) : '';
   1.654 +
   1.655 +		// Send vars to template
   1.656 +		$template->assign_block_vars('topicrow', array(
   1.657 +			'FORUM_ID'					=> $topic_forum_id,
   1.658 +			'TOPIC_ID'					=> $topic_id,
   1.659 +			'TOPIC_AUTHOR'				=> get_username_string('username', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
   1.660 +			'TOPIC_AUTHOR_COLOUR'		=> get_username_string('colour', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
   1.661 +			'TOPIC_AUTHOR_FULL'			=> get_username_string('full', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
   1.662 +			'FIRST_POST_TIME'			=> $user->format_date($row['topic_time']),
   1.663 +			'LAST_POST_SUBJECT'			=> censor_text($row['topic_last_post_subject']),
   1.664 +			'LAST_POST_TIME'			=> $user->format_date($row['topic_last_post_time']),
   1.665 +			'LAST_VIEW_TIME'			=> $user->format_date($row['topic_last_view_time']),
   1.666 +			'LAST_POST_AUTHOR'			=> get_username_string('username', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
   1.667 +			'LAST_POST_AUTHOR_COLOUR'	=> get_username_string('colour', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
   1.668 +			'LAST_POST_AUTHOR_FULL'		=> get_username_string('full', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
   1.669 +
   1.670 +			'PAGINATION'		=> topic_generate_pagination($replies, $view_topic_url),
   1.671 +			'REPLIES'			=> $replies,
   1.672 +			'VIEWS'				=> $row['topic_views'],
   1.673 +			'TOPIC_TITLE'		=> censor_text($row['topic_title']),
   1.674 +			'TOPIC_TYPE'		=> $topic_type,
   1.675 +
   1.676 +			'TOPIC_FOLDER_IMG'		=> $user->img($folder_img, $folder_alt),
   1.677 +			'TOPIC_FOLDER_IMG_SRC'	=> $user->img($folder_img, $folder_alt, false, '', 'src'),
   1.678 +			'TOPIC_FOLDER_IMG_ALT'	=> $user->lang[$folder_alt],
   1.679 +			'TOPIC_FOLDER_IMG_WIDTH'=> $user->img($folder_img, '', false, '', 'width'),
   1.680 +			'TOPIC_FOLDER_IMG_HEIGHT'	=> $user->img($folder_img, '', false, '', 'height'),
   1.681 +
   1.682 +			'TOPIC_ICON_IMG'		=> (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['img'] : '',
   1.683 +			'TOPIC_ICON_IMG_WIDTH'	=> (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['width'] : '',
   1.684 +			'TOPIC_ICON_IMG_HEIGHT'	=> (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['height'] : '',
   1.685 +			'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']) : '',
   1.686 +			'UNAPPROVED_IMG'		=> ($topic_unapproved || $posts_unapproved) ? $user->img('icon_topic_unapproved', ($topic_unapproved) ? 'TOPIC_UNAPPROVED' : 'POSTS_UNAPPROVED') : '',
   1.687 +
   1.688 +			'S_TOPIC_TYPE'			=> $row['topic_type'],
   1.689 +			'S_USER_POSTED'			=> (isset($row['topic_posted']) && $row['topic_posted']) ? true : false,
   1.690 +			'S_UNREAD_TOPIC'		=> $unread_topic,
   1.691 +			'S_TOPIC_REPORTED'		=> (!empty($row['topic_reported']) && $auth->acl_get('m_report', $topic_forum_id)) ? true : false,
   1.692 +			'S_TOPIC_UNAPPROVED'	=> $topic_unapproved,
   1.693 +			'S_POSTS_UNAPPROVED'	=> $posts_unapproved,
   1.694 +			'S_HAS_POLL'			=> ($row['poll_start']) ? true : false,
   1.695 +			'S_POST_ANNOUNCE'		=> ($row['topic_type'] == POST_ANNOUNCE) ? true : false,
   1.696 +			'S_POST_GLOBAL'			=> ($row['topic_type'] == POST_GLOBAL) ? true : false,
   1.697 +			'S_POST_STICKY'			=> ($row['topic_type'] == POST_STICKY) ? true : false,
   1.698 +			'S_TOPIC_LOCKED'		=> ($row['topic_status'] == ITEM_LOCKED) ? true : false,
   1.699 +			'S_TOPIC_MOVED'			=> ($row['topic_status'] == ITEM_MOVED) ? true : false,
   1.700 +
   1.701 +			'U_NEWEST_POST'			=> append_sid("{$phpbb_root_path}viewtopic.$phpEx", $view_topic_url_params . '&amp;view=unread') . '#unread',
   1.702 +			'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'],
   1.703 +			'U_LAST_POST_AUTHOR'	=> get_username_string('profile', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),
   1.704 +			'U_TOPIC_AUTHOR'		=> get_username_string('profile', $row['topic_poster'], $row['topic_first_poster_name'], $row['topic_first_poster_colour']),
   1.705 +			'U_VIEW_TOPIC'			=> $view_topic_url,
   1.706 +			'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),
   1.707 +			'U_MCP_QUEUE'			=> $u_mcp_queue,
   1.708 +
   1.709 +			'S_TOPIC_TYPE_SWITCH'	=> ($s_type_switch == $s_type_switch_test) ? -1 : $s_type_switch_test)
   1.710 +		);
   1.711 +
   1.712 +		$s_type_switch = ($row['topic_type'] == POST_ANNOUNCE || $row['topic_type'] == POST_GLOBAL) ? 1 : 0;
   1.713 +
   1.714 +		if ($unread_topic)
   1.715 +		{
   1.716 +			$mark_forum_read = false;
   1.717 +		}
   1.718 +
   1.719 +		unset($rowset[$topic_id]);
   1.720 +	}
   1.721 +}
   1.722 +
   1.723 +// This is rather a fudge but it's the best I can think of without requiring information
   1.724 +// on all topics (as we do in 2.0.x). It looks for unread or new topics, if it doesn't find
   1.725 +// any it updates the forum last read cookie. This requires that the user visit the forum
   1.726 +// after reading a topic
   1.727 +if ($forum_data['forum_type'] == FORUM_POST && sizeof($topic_list) && $mark_forum_read)
   1.728 +{
   1.729 +	update_forum_tracking_info($forum_id, $forum_data['forum_last_post_time'], false, $mark_time_forum);
   1.730 +}
   1.731 +
   1.732 +page_footer();
   1.733 +
   1.734 +?>
   1.735 \ No newline at end of file