viewtopic.php
changeset 0 ceef6d64c21d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/viewtopic.php	Wed Mar 07 16:41:10 2012 +0400
     1.3 @@ -0,0 +1,1784 @@
     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 +include($phpbb_root_path . 'includes/bbcode.' . $phpEx);
    1.23 +
    1.24 +// Start session management
    1.25 +$user->session_begin();
    1.26 +$auth->acl($user->data);
    1.27 +
    1.28 +// Initial var setup
    1.29 +$forum_id	= request_var('f', 0);
    1.30 +$topic_id	= request_var('t', 0);
    1.31 +$post_id	= request_var('p', 0);
    1.32 +$voted_id	= request_var('vote_id', array('' => 0));
    1.33 +
    1.34 +$voted_id = (sizeof($voted_id) > 1) ? array_unique($voted_id) : $voted_id;
    1.35 +
    1.36 +
    1.37 +$start		= request_var('start', 0);
    1.38 +$view		= request_var('view', '');
    1.39 +
    1.40 +$default_sort_days	= (!empty($user->data['user_post_show_days'])) ? $user->data['user_post_show_days'] : 0;
    1.41 +$default_sort_key	= (!empty($user->data['user_post_sortby_type'])) ? $user->data['user_post_sortby_type'] : 't';
    1.42 +$default_sort_dir	= (!empty($user->data['user_post_sortby_dir'])) ? $user->data['user_post_sortby_dir'] : 'a';
    1.43 +
    1.44 +$sort_days	= request_var('st', $default_sort_days);
    1.45 +$sort_key	= request_var('sk', $default_sort_key);
    1.46 +$sort_dir	= request_var('sd', $default_sort_dir);
    1.47 +
    1.48 +$update		= request_var('update', false);
    1.49 +
    1.50 +$s_can_vote = false;
    1.51 +/**
    1.52 +* @todo normalize?
    1.53 +*/
    1.54 +$hilit_words	= request_var('hilit', '', true);
    1.55 +
    1.56 +// Do we have a topic or post id?
    1.57 +if (!$topic_id && !$post_id)
    1.58 +{
    1.59 +	trigger_error('NO_TOPIC');
    1.60 +}
    1.61 +
    1.62 +// Find topic id if user requested a newer or older topic
    1.63 +if ($view && !$post_id)
    1.64 +{
    1.65 +	if (!$forum_id)
    1.66 +	{
    1.67 +		$sql = 'SELECT forum_id
    1.68 +			FROM ' . TOPICS_TABLE . "
    1.69 +			WHERE topic_id = $topic_id";
    1.70 +		$result = $db->sql_query($sql);
    1.71 +		$forum_id = (int) $db->sql_fetchfield('forum_id');
    1.72 +		$db->sql_freeresult($result);
    1.73 +
    1.74 +		if (!$forum_id)
    1.75 +		{
    1.76 +			trigger_error('NO_TOPIC');
    1.77 +		}
    1.78 +	}
    1.79 +
    1.80 +	if ($view == 'unread')
    1.81 +	{
    1.82 +		// Get topic tracking info
    1.83 +		$topic_tracking_info = get_complete_topic_tracking($forum_id, $topic_id);
    1.84 +
    1.85 +		$topic_last_read = (isset($topic_tracking_info[$topic_id])) ? $topic_tracking_info[$topic_id] : 0;
    1.86 +
    1.87 +		$sql = 'SELECT post_id, topic_id, forum_id
    1.88 +			FROM ' . POSTS_TABLE . "
    1.89 +			WHERE topic_id = $topic_id
    1.90 +				" . (($auth->acl_get('m_approve', $forum_id)) ? '' : 'AND post_approved = 1') . "
    1.91 +				AND post_time > $topic_last_read
    1.92 +				AND forum_id = $forum_id
    1.93 +			ORDER BY post_time ASC";
    1.94 +		$result = $db->sql_query_limit($sql, 1);
    1.95 +		$row = $db->sql_fetchrow($result);
    1.96 +		$db->sql_freeresult($result);
    1.97 +
    1.98 +		if (!$row)
    1.99 +		{
   1.100 +			$sql = 'SELECT topic_last_post_id as post_id, topic_id, forum_id
   1.101 +				FROM ' . TOPICS_TABLE . '
   1.102 +				WHERE topic_id = ' . $topic_id;
   1.103 +			$result = $db->sql_query($sql);
   1.104 +			$row = $db->sql_fetchrow($result);
   1.105 +			$db->sql_freeresult($result);
   1.106 +		}
   1.107 +
   1.108 +		if (!$row)
   1.109 +		{
   1.110 +			// Setup user environment so we can process lang string
   1.111 +			$user->setup('viewtopic');
   1.112 +
   1.113 +			trigger_error('NO_TOPIC');
   1.114 +		}
   1.115 +
   1.116 +		$post_id = $row['post_id'];
   1.117 +		$topic_id = $row['topic_id'];
   1.118 +	}
   1.119 +	else if ($view == 'next' || $view == 'previous')
   1.120 +	{
   1.121 +		$sql_condition = ($view == 'next') ? '>' : '<';
   1.122 +		$sql_ordering = ($view == 'next') ? 'ASC' : 'DESC';
   1.123 +
   1.124 +		$sql = 'SELECT forum_id, topic_last_post_time
   1.125 +			FROM ' . TOPICS_TABLE . '
   1.126 +			WHERE topic_id = ' . $topic_id;
   1.127 +		$result = $db->sql_query($sql);
   1.128 +		$row = $db->sql_fetchrow($result);
   1.129 +		$db->sql_freeresult($result);
   1.130 +
   1.131 +		if (!$row)
   1.132 +		{
   1.133 +			$user->setup('viewtopic');
   1.134 +			// OK, the topic doesn't exist. This error message is not helpful, but technically correct.
   1.135 +			trigger_error(($view == 'next') ? 'NO_NEWER_TOPICS' : 'NO_OLDER_TOPICS');
   1.136 +		}
   1.137 +		else
   1.138 +		{
   1.139 +			$sql = 'SELECT topic_id, forum_id
   1.140 +				FROM ' . TOPICS_TABLE . '
   1.141 +				WHERE forum_id = ' . $row['forum_id'] . "
   1.142 +					AND topic_moved_id = 0
   1.143 +					AND topic_last_post_time $sql_condition {$row['topic_last_post_time']}
   1.144 +					" . (($auth->acl_get('m_approve', $row['forum_id'])) ? '' : 'AND topic_approved = 1') . "
   1.145 +				ORDER BY topic_last_post_time $sql_ordering";
   1.146 +			$result = $db->sql_query_limit($sql, 1);
   1.147 +			$row = $db->sql_fetchrow($result);
   1.148 +			$db->sql_freeresult($result);
   1.149 +
   1.150 +			if (!$row)
   1.151 +			{
   1.152 +				$user->setup('viewtopic');
   1.153 +				trigger_error(($view == 'next') ? 'NO_NEWER_TOPICS' : 'NO_OLDER_TOPICS');
   1.154 +			}
   1.155 +			else
   1.156 +			{
   1.157 +				$topic_id = $row['topic_id'];
   1.158 +
   1.159 +				// Check for global announcement correctness?
   1.160 +				if (!$row['forum_id'] && !$forum_id)
   1.161 +				{
   1.162 +					trigger_error('NO_TOPIC');
   1.163 +				}
   1.164 +				else if ($row['forum_id'])
   1.165 +				{
   1.166 +					$forum_id = $row['forum_id'];
   1.167 +				}
   1.168 +			}
   1.169 +		}
   1.170 +	}
   1.171 +
   1.172 +	// Check for global announcement correctness?
   1.173 +	if ((!isset($row) || !$row['forum_id']) && !$forum_id)
   1.174 +	{
   1.175 +		trigger_error('NO_TOPIC');
   1.176 +	}
   1.177 +	else if (isset($row) && $row['forum_id'])
   1.178 +	{
   1.179 +		$forum_id = $row['forum_id'];
   1.180 +	}
   1.181 +}
   1.182 +
   1.183 +// This rather complex gaggle of code handles querying for topics but
   1.184 +// also allows for direct linking to a post (and the calculation of which
   1.185 +// page the post is on and the correct display of viewtopic)
   1.186 +$sql_array = array(
   1.187 +	'SELECT'	=> 't.*, f.*',
   1.188 +
   1.189 +	'FROM'		=> array(FORUMS_TABLE => 'f'),
   1.190 +);
   1.191 +
   1.192 +// Firebird handles two columns of the same name a little differently, this
   1.193 +// addresses that by forcing the forum_id to come from the forums table.
   1.194 +if ($db->sql_layer === 'firebird')
   1.195 +{
   1.196 +	$sql_array['SELECT'] = 'f.forum_id AS forum_id, ' . $sql_array['SELECT'];
   1.197 +}
   1.198 +
   1.199 +// The FROM-Order is quite important here, else t.* columns can not be correctly bound.
   1.200 +if ($post_id)
   1.201 +{
   1.202 +	$sql_array['SELECT'] .= ', p.post_approved, p.post_time, p.post_id';
   1.203 +	$sql_array['FROM'][POSTS_TABLE] = 'p';
   1.204 +}
   1.205 +
   1.206 +// Topics table need to be the last in the chain
   1.207 +$sql_array['FROM'][TOPICS_TABLE] = 't';
   1.208 +
   1.209 +if ($user->data['is_registered'])
   1.210 +{
   1.211 +	$sql_array['SELECT'] .= ', tw.notify_status';
   1.212 +	$sql_array['LEFT_JOIN'] = array();
   1.213 +
   1.214 +	$sql_array['LEFT_JOIN'][] = array(
   1.215 +		'FROM'	=> array(TOPICS_WATCH_TABLE => 'tw'),
   1.216 +		'ON'	=> 'tw.user_id = ' . $user->data['user_id'] . ' AND t.topic_id = tw.topic_id'
   1.217 +	);
   1.218 +
   1.219 +	if ($config['allow_bookmarks'])
   1.220 +	{
   1.221 +		$sql_array['SELECT'] .= ', bm.topic_id as bookmarked';
   1.222 +		$sql_array['LEFT_JOIN'][] = array(
   1.223 +			'FROM'	=> array(BOOKMARKS_TABLE => 'bm'),
   1.224 +			'ON'	=> 'bm.user_id = ' . $user->data['user_id'] . ' AND t.topic_id = bm.topic_id'
   1.225 +		);
   1.226 +	}
   1.227 +
   1.228 +	if ($config['load_db_lastread'])
   1.229 +	{
   1.230 +		$sql_array['SELECT'] .= ', tt.mark_time, ft.mark_time as forum_mark_time';
   1.231 +
   1.232 +		$sql_array['LEFT_JOIN'][] = array(
   1.233 +			'FROM'	=> array(TOPICS_TRACK_TABLE => 'tt'),
   1.234 +			'ON'	=> 'tt.user_id = ' . $user->data['user_id'] . ' AND t.topic_id = tt.topic_id'
   1.235 +		);
   1.236 +
   1.237 +		$sql_array['LEFT_JOIN'][] = array(
   1.238 +			'FROM'	=> array(FORUMS_TRACK_TABLE => 'ft'),
   1.239 +			'ON'	=> 'ft.user_id = ' . $user->data['user_id'] . ' AND t.forum_id = ft.forum_id'
   1.240 +		);
   1.241 +	}
   1.242 +}
   1.243 +
   1.244 +if (!$post_id)
   1.245 +{
   1.246 +	$sql_array['WHERE'] = "t.topic_id = $topic_id";
   1.247 +}
   1.248 +else
   1.249 +{
   1.250 +	$sql_array['WHERE'] = "p.post_id = $post_id AND t.topic_id = p.topic_id";
   1.251 +}
   1.252 +
   1.253 +$sql_array['WHERE'] .= ' AND (f.forum_id = t.forum_id';
   1.254 +
   1.255 +if (!$forum_id)
   1.256 +{
   1.257 +	// If it is a global announcement make sure to set the forum id to a postable forum
   1.258 +	$sql_array['WHERE'] .= ' OR (t.topic_type = ' . POST_GLOBAL . '
   1.259 +		AND f.forum_type = ' . FORUM_POST . ')';
   1.260 +}
   1.261 +else
   1.262 +{
   1.263 +	$sql_array['WHERE'] .= ' OR (t.topic_type = ' . POST_GLOBAL . "
   1.264 +		AND f.forum_id = $forum_id)";
   1.265 +}
   1.266 +
   1.267 +$sql_array['WHERE'] .= ')';
   1.268 +
   1.269 +// Join to forum table on topic forum_id unless topic forum_id is zero
   1.270 +// whereupon we join on the forum_id passed as a parameter ... this
   1.271 +// is done so navigation, forum name, etc. remain consistent with where
   1.272 +// user clicked to view a global topic
   1.273 +$sql = $db->sql_build_query('SELECT', $sql_array);
   1.274 +$result = $db->sql_query($sql);
   1.275 +$topic_data = $db->sql_fetchrow($result);
   1.276 +$db->sql_freeresult($result);
   1.277 +
   1.278 +// link to unapproved post or incorrect link
   1.279 +if (!$topic_data)
   1.280 +{
   1.281 +	// If post_id was submitted, we try at least to display the topic as a last resort...
   1.282 +	if ($post_id && $topic_id)
   1.283 +	{
   1.284 +		redirect(append_sid("{$phpbb_root_path}viewtopic.$phpEx", "t=$topic_id" . (($forum_id) ? "&amp;f=$forum_id" : '')));
   1.285 +	}
   1.286 +
   1.287 +	trigger_error('NO_TOPIC');
   1.288 +}
   1.289 +
   1.290 +$forum_id = (int) $topic_data['forum_id'];
   1.291 +// This is for determining where we are (page)
   1.292 +if ($post_id)
   1.293 +{
   1.294 +	// are we where we are supposed to be?
   1.295 +	if (!$topic_data['post_approved'] && !$auth->acl_get('m_approve', $topic_data['forum_id']))
   1.296 +	{
   1.297 +		// If post_id was submitted, we try at least to display the topic as a last resort...
   1.298 +		if ($topic_id)
   1.299 +		{
   1.300 +			redirect(append_sid("{$phpbb_root_path}viewtopic.$phpEx", "t=$topic_id" . (($forum_id) ? "&amp;f=$forum_id" : '')));
   1.301 +		}
   1.302 +
   1.303 +		trigger_error('NO_TOPIC');
   1.304 +	}
   1.305 +	if ($post_id == $topic_data['topic_first_post_id'] || $post_id == $topic_data['topic_last_post_id'])
   1.306 +	{
   1.307 +		$check_sort = ($post_id == $topic_data['topic_first_post_id']) ? 'd' : 'a';
   1.308 +
   1.309 +		if ($sort_dir == $check_sort)
   1.310 +		{
   1.311 +			$topic_data['prev_posts'] = ($auth->acl_get('m_approve', $forum_id)) ? $topic_data['topic_replies_real'] : $topic_data['topic_replies'];
   1.312 +		}
   1.313 +		else
   1.314 +		{
   1.315 +			$topic_data['prev_posts'] = 0;
   1.316 +		}
   1.317 +	}
   1.318 +	else
   1.319 +	{
   1.320 +		$sql = 'SELECT COUNT(p.post_id) AS prev_posts
   1.321 +			FROM ' . POSTS_TABLE . " p
   1.322 +			WHERE p.topic_id = {$topic_data['topic_id']}
   1.323 +				" . ((!$auth->acl_get('m_approve', $forum_id)) ? 'AND p.post_approved = 1' : '');
   1.324 +
   1.325 +		if ($sort_dir == 'd')
   1.326 +		{
   1.327 +			$sql .= " AND (p.post_time > {$topic_data['post_time']} OR (p.post_time = {$topic_data['post_time']} AND p.post_id >= {$topic_data['post_id']}))";
   1.328 +		}
   1.329 +		else
   1.330 +		{
   1.331 +			$sql .= " AND (p.post_time < {$topic_data['post_time']} OR (p.post_time = {$topic_data['post_time']} AND p.post_id <= {$topic_data['post_id']}))";
   1.332 +		}
   1.333 +
   1.334 +		$result = $db->sql_query($sql);
   1.335 +		$row = $db->sql_fetchrow($result);
   1.336 +		$db->sql_freeresult($result);
   1.337 +
   1.338 +		$topic_data['prev_posts'] = $row['prev_posts'] - 1;
   1.339 +	}
   1.340 +}
   1.341 +
   1.342 +$topic_id = (int) $topic_data['topic_id'];
   1.343 +//
   1.344 +$topic_replies = ($auth->acl_get('m_approve', $forum_id)) ? $topic_data['topic_replies_real'] : $topic_data['topic_replies'];
   1.345 +
   1.346 +// Check sticky/announcement time limit
   1.347 +if (($topic_data['topic_type'] == POST_STICKY || $topic_data['topic_type'] == POST_ANNOUNCE) && $topic_data['topic_time_limit'] && ($topic_data['topic_time'] + $topic_data['topic_time_limit']) < time())
   1.348 +{
   1.349 +	$sql = 'UPDATE ' . TOPICS_TABLE . '
   1.350 +		SET topic_type = ' . POST_NORMAL . ', topic_time_limit = 0
   1.351 +		WHERE topic_id = ' . $topic_id;
   1.352 +	$db->sql_query($sql);
   1.353 +
   1.354 +	$topic_data['topic_type'] = POST_NORMAL;
   1.355 +	$topic_data['topic_time_limit'] = 0;
   1.356 +}
   1.357 +
   1.358 +// Setup look and feel
   1.359 +$user->setup('viewtopic', $topic_data['forum_style']);
   1.360 +
   1.361 +if (!$topic_data['topic_approved'] && !$auth->acl_get('m_approve', $forum_id))
   1.362 +{
   1.363 +	trigger_error('NO_TOPIC');
   1.364 +}
   1.365 +
   1.366 +// Start auth check
   1.367 +if (!$auth->acl_get('f_read', $forum_id))
   1.368 +{
   1.369 +	if ($user->data['user_id'] != ANONYMOUS)
   1.370 +	{
   1.371 +		trigger_error('SORRY_AUTH_READ');
   1.372 +	}
   1.373 +
   1.374 +	login_box('', $user->lang['LOGIN_VIEWFORUM']);
   1.375 +}
   1.376 +
   1.377 +// Forum is passworded ... check whether access has been granted to this
   1.378 +// user this session, if not show login box
   1.379 +if ($topic_data['forum_password'])
   1.380 +{
   1.381 +	login_forum_box($topic_data);
   1.382 +}
   1.383 +
   1.384 +// Redirect to login or to the correct post upon emailed notification links
   1.385 +if (isset($_GET['e']))
   1.386 +{
   1.387 +	$jump_to = request_var('e', 0);
   1.388 +
   1.389 +	$redirect_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id");
   1.390 +
   1.391 +	if ($user->data['user_id'] == ANONYMOUS)
   1.392 +	{
   1.393 +		login_box($redirect_url . "&amp;p=$post_id&amp;e=$jump_to", $user->lang['LOGIN_NOTIFY_TOPIC']);
   1.394 +	}
   1.395 +
   1.396 +	if ($jump_to > 0)
   1.397 +	{
   1.398 +		// We direct the already logged in user to the correct post...
   1.399 +		redirect($redirect_url . ((!$post_id) ? "&amp;p=$jump_to" : "&amp;p=$post_id") . "#p$jump_to");
   1.400 +	}
   1.401 +}
   1.402 +
   1.403 +// What is start equal to?
   1.404 +if ($post_id)
   1.405 +{
   1.406 +	$start = floor(($topic_data['prev_posts']) / $config['posts_per_page']) * $config['posts_per_page'];
   1.407 +}
   1.408 +
   1.409 +// Get topic tracking info
   1.410 +if (!isset($topic_tracking_info))
   1.411 +{
   1.412 +	$topic_tracking_info = array();
   1.413 +
   1.414 +	// Get topic tracking info
   1.415 +	if ($config['load_db_lastread'] && $user->data['is_registered'])
   1.416 +	{
   1.417 +		$tmp_topic_data = array($topic_id => $topic_data);
   1.418 +		$topic_tracking_info = get_topic_tracking($forum_id, $topic_id, $tmp_topic_data, array($forum_id => $topic_data['forum_mark_time']));
   1.419 +		unset($tmp_topic_data);
   1.420 +	}
   1.421 +	else if ($config['load_anon_lastread'] || $user->data['is_registered'])
   1.422 +	{
   1.423 +		$topic_tracking_info = get_complete_topic_tracking($forum_id, $topic_id);
   1.424 +	}
   1.425 +}
   1.426 +
   1.427 +// Post ordering options
   1.428 +$limit_days = array(0 => $user->lang['ALL_POSTS'], 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.429 +
   1.430 +$sort_by_text = array('a' => $user->lang['AUTHOR'], 't' => $user->lang['POST_TIME'], 's' => $user->lang['SUBJECT']);
   1.431 +$sort_by_sql = array('a' => array('u.username_clean', 'p.post_id'), 't' => 'p.post_time', 's' => array('p.post_subject', 'p.post_id'));
   1.432 +$join_user_sql = array('a' => true, 't' => false, 's' => false);
   1.433 +
   1.434 +$s_limit_days = $s_sort_key = $s_sort_dir = $u_sort_param = '';
   1.435 +
   1.436 +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.437 +
   1.438 +// Obtain correct post count and ordering SQL if user has
   1.439 +// requested anything different
   1.440 +if ($sort_days)
   1.441 +{
   1.442 +	$min_post_time = time() - ($sort_days * 86400);
   1.443 +
   1.444 +	$sql = 'SELECT COUNT(post_id) AS num_posts
   1.445 +		FROM ' . POSTS_TABLE . "
   1.446 +		WHERE topic_id = $topic_id
   1.447 +			AND post_time >= $min_post_time
   1.448 +		" . (($auth->acl_get('m_approve', $forum_id)) ? '' : 'AND post_approved = 1');
   1.449 +	$result = $db->sql_query($sql);
   1.450 +	$total_posts = (int) $db->sql_fetchfield('num_posts');
   1.451 +	$db->sql_freeresult($result);
   1.452 +
   1.453 +	$limit_posts_time = "AND p.post_time >= $min_post_time ";
   1.454 +
   1.455 +	if (isset($_POST['sort']))
   1.456 +	{
   1.457 +		$start = 0;
   1.458 +	}
   1.459 +}
   1.460 +else
   1.461 +{
   1.462 +	$total_posts = $topic_replies + 1;
   1.463 +	$limit_posts_time = '';
   1.464 +}
   1.465 +
   1.466 +// Was a highlight request part of the URI?
   1.467 +$highlight_match = $highlight = '';
   1.468 +if ($hilit_words)
   1.469 +{
   1.470 +	foreach (explode(' ', trim($hilit_words)) as $word)
   1.471 +	{
   1.472 +		if (trim($word))
   1.473 +		{
   1.474 +			$word = str_replace('\*', '\w+?', preg_quote($word, '#'));
   1.475 +			$word = preg_replace('#(^|\s)\\\\w\*\?(\s|$)#', '$1\w+?$2', $word);
   1.476 +			$highlight_match .= (($highlight_match != '') ? '|' : '') . $word;
   1.477 +		}
   1.478 +	}
   1.479 +
   1.480 +	$highlight = urlencode($hilit_words);
   1.481 +}
   1.482 +
   1.483 +// Make sure $start is set to the last page if it exceeds the amount
   1.484 +if ($start < 0 || $start >= $total_posts)
   1.485 +{
   1.486 +	$start = ($start < 0) ? 0 : floor(($total_posts - 1) / $config['posts_per_page']) * $config['posts_per_page'];
   1.487 +}
   1.488 +
   1.489 +// General Viewtopic URL for return links
   1.490 +$viewtopic_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id" . (($start == 0) ? '' : "&amp;start=$start") . ((strlen($u_sort_param)) ? "&amp;$u_sort_param" : '') . (($highlight_match) ? "&amp;hilit=$highlight" : ''));
   1.491 +
   1.492 +// Are we watching this topic?
   1.493 +$s_watching_topic = array(
   1.494 +	'link'			=> '',
   1.495 +	'title'			=> '',
   1.496 +	'is_watching'	=> false,
   1.497 +);
   1.498 +
   1.499 +if (($config['email_enable'] || $config['jab_enable']) && $config['allow_topic_notify'])
   1.500 +{
   1.501 +	$notify_status = (isset($topic_data['notify_status'])) ? $topic_data['notify_status'] : null;
   1.502 +	watch_topic_forum('topic', $s_watching_topic, $user->data['user_id'], $forum_id, $topic_id, $notify_status, $start, $topic_data['topic_title']);
   1.503 +
   1.504 +	// Reset forum notification if forum notify is set
   1.505 +	if ($config['allow_forum_notify'] && $auth->acl_get('f_subscribe', $forum_id))
   1.506 +	{
   1.507 +		$s_watching_forum = $s_watching_topic;
   1.508 +		watch_topic_forum('forum', $s_watching_forum, $user->data['user_id'], $forum_id, 0);
   1.509 +	}
   1.510 +}
   1.511 +
   1.512 +// Bookmarks
   1.513 +if ($config['allow_bookmarks'] && $user->data['is_registered'] && request_var('bookmark', 0))
   1.514 +{
   1.515 +	if (check_link_hash(request_var('hash', ''), "topic_$topic_id"))
   1.516 +	{
   1.517 +		if (!$topic_data['bookmarked'])
   1.518 +		{
   1.519 +			$sql = 'INSERT INTO ' . BOOKMARKS_TABLE . ' ' . $db->sql_build_array('INSERT', array(
   1.520 +				'user_id'	=> $user->data['user_id'],
   1.521 +				'topic_id'	=> $topic_id,
   1.522 +			));
   1.523 +			$db->sql_query($sql);
   1.524 +		}
   1.525 +		else
   1.526 +		{
   1.527 +			$sql = 'DELETE FROM ' . BOOKMARKS_TABLE . "
   1.528 +				WHERE user_id = {$user->data['user_id']}
   1.529 +					AND topic_id = $topic_id";
   1.530 +			$db->sql_query($sql);
   1.531 +		}
   1.532 +		$message = (($topic_data['bookmarked']) ? $user->lang['BOOKMARK_REMOVED'] : $user->lang['BOOKMARK_ADDED']) . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $viewtopic_url . '">', '</a>');
   1.533 +	}
   1.534 +	else
   1.535 +	{
   1.536 +		$message = $user->lang['BOOKMARK_ERR'] . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $viewtopic_url . '">', '</a>');
   1.537 +	}
   1.538 +	meta_refresh(3, $viewtopic_url);
   1.539 +
   1.540 +	trigger_error($message);
   1.541 +}
   1.542 +
   1.543 +// Grab ranks
   1.544 +$ranks = $cache->obtain_ranks();
   1.545 +
   1.546 +// Grab icons
   1.547 +$icons = $cache->obtain_icons();
   1.548 +
   1.549 +// Grab extensions
   1.550 +$extensions = array();
   1.551 +if ($topic_data['topic_attachment'])
   1.552 +{
   1.553 +	$extensions = $cache->obtain_attach_extensions($forum_id);
   1.554 +}
   1.555 +
   1.556 +// Forum rules listing
   1.557 +$s_forum_rules = '';
   1.558 +gen_forum_auth_level('topic', $forum_id, $topic_data['forum_status']);
   1.559 +
   1.560 +// Quick mod tools
   1.561 +$allow_change_type = ($auth->acl_get('m_', $forum_id) || ($user->data['is_registered'] && $user->data['user_id'] == $topic_data['topic_poster'])) ? true : false;
   1.562 +
   1.563 +$topic_mod = '';
   1.564 +$topic_mod .= ($auth->acl_get('m_lock', $forum_id) || ($auth->acl_get('f_user_lock', $forum_id) && $user->data['is_registered'] && $user->data['user_id'] == $topic_data['topic_poster'] && $topic_data['topic_status'] == ITEM_UNLOCKED)) ? (($topic_data['topic_status'] == ITEM_UNLOCKED) ? '<option value="lock">' . $user->lang['LOCK_TOPIC'] . '</option>' : '<option value="unlock">' . $user->lang['UNLOCK_TOPIC'] . '</option>') : '';
   1.565 +$topic_mod .= ($auth->acl_get('m_delete', $forum_id)) ? '<option value="delete_topic">' . $user->lang['DELETE_TOPIC'] . '</option>' : '';
   1.566 +$topic_mod .= ($auth->acl_get('m_move', $forum_id) && $topic_data['topic_status'] != ITEM_MOVED) ? '<option value="move">' . $user->lang['MOVE_TOPIC'] . '</option>' : '';
   1.567 +$topic_mod .= ($auth->acl_get('m_split', $forum_id)) ? '<option value="split">' . $user->lang['SPLIT_TOPIC'] . '</option>' : '';
   1.568 +$topic_mod .= ($auth->acl_get('m_merge', $forum_id)) ? '<option value="merge">' . $user->lang['MERGE_POSTS'] . '</option>' : '';
   1.569 +$topic_mod .= ($auth->acl_get('m_merge', $forum_id)) ? '<option value="merge_topic">' . $user->lang['MERGE_TOPIC'] . '</option>' : '';
   1.570 +$topic_mod .= ($auth->acl_get('m_move', $forum_id)) ? '<option value="fork">' . $user->lang['FORK_TOPIC'] . '</option>' : '';
   1.571 +$topic_mod .= ($allow_change_type && $auth->acl_gets('f_sticky', 'f_announce', $forum_id) && $topic_data['topic_type'] != POST_NORMAL) ? '<option value="make_normal">' . $user->lang['MAKE_NORMAL'] . '</option>' : '';
   1.572 +$topic_mod .= ($allow_change_type && $auth->acl_get('f_sticky', $forum_id) && $topic_data['topic_type'] != POST_STICKY) ? '<option value="make_sticky">' . $user->lang['MAKE_STICKY'] . '</option>' : '';
   1.573 +$topic_mod .= ($allow_change_type && $auth->acl_get('f_announce', $forum_id) && $topic_data['topic_type'] != POST_ANNOUNCE) ? '<option value="make_announce">' . $user->lang['MAKE_ANNOUNCE'] . '</option>' : '';
   1.574 +$topic_mod .= ($allow_change_type && $auth->acl_get('f_announce', $forum_id) && $topic_data['topic_type'] != POST_GLOBAL) ? '<option value="make_global">' . $user->lang['MAKE_GLOBAL'] . '</option>' : '';
   1.575 +$topic_mod .= ($auth->acl_get('m_', $forum_id)) ? '<option value="topic_logs">' . $user->lang['VIEW_TOPIC_LOGS'] . '</option>' : '';
   1.576 +
   1.577 +// If we've got a hightlight set pass it on to pagination.
   1.578 +$pagination = generate_pagination(append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id" . ((strlen($u_sort_param)) ? "&amp;$u_sort_param" : '') . (($highlight_match) ? "&amp;hilit=$highlight" : '')), $total_posts, $config['posts_per_page'], $start);
   1.579 +
   1.580 +// Navigation links
   1.581 +generate_forum_nav($topic_data);
   1.582 +
   1.583 +// Forum Rules
   1.584 +generate_forum_rules($topic_data);
   1.585 +
   1.586 +// Moderators
   1.587 +$forum_moderators = array();
   1.588 +if ($config['load_moderators'])
   1.589 +{
   1.590 +	get_moderators($forum_moderators, $forum_id);
   1.591 +}
   1.592 +
   1.593 +// This is only used for print view so ...
   1.594 +$server_path = (!$view) ? $phpbb_root_path : generate_board_url() . '/';
   1.595 +
   1.596 +// Replace naughty words in title
   1.597 +$topic_data['topic_title'] = censor_text($topic_data['topic_title']);
   1.598 +
   1.599 +$s_search_hidden_fields = array(
   1.600 +	't' => $topic_id,
   1.601 +	'sf' => 'msgonly',
   1.602 +);
   1.603 +if ($_SID)
   1.604 +{
   1.605 +	$s_search_hidden_fields['sid'] = $_SID;
   1.606 +}
   1.607 +
   1.608 +if (!empty($_EXTRA_URL))
   1.609 +{
   1.610 +	foreach ($_EXTRA_URL as $url_param)
   1.611 +	{
   1.612 +		$url_param = explode('=', $url_param, 2);
   1.613 +		$s_hidden_fields[$url_param[0]] = $url_param[1];
   1.614 +	}
   1.615 +}
   1.616 +
   1.617 +// Send vars to template
   1.618 +$template->assign_vars(array(
   1.619 +	'FORUM_ID' 		=> $forum_id,
   1.620 +	'FORUM_NAME' 	=> $topic_data['forum_name'],
   1.621 +	'FORUM_DESC'	=> generate_text_for_display($topic_data['forum_desc'], $topic_data['forum_desc_uid'], $topic_data['forum_desc_bitfield'], $topic_data['forum_desc_options']),
   1.622 +	'TOPIC_ID' 		=> $topic_id,
   1.623 +	'TOPIC_TITLE' 	=> $topic_data['topic_title'],
   1.624 +	'TOPIC_POSTER'	=> $topic_data['topic_poster'],
   1.625 +
   1.626 +	'TOPIC_AUTHOR_FULL'		=> get_username_string('full', $topic_data['topic_poster'], $topic_data['topic_first_poster_name'], $topic_data['topic_first_poster_colour']),
   1.627 +	'TOPIC_AUTHOR_COLOUR'	=> get_username_string('colour', $topic_data['topic_poster'], $topic_data['topic_first_poster_name'], $topic_data['topic_first_poster_colour']),
   1.628 +	'TOPIC_AUTHOR'			=> get_username_string('username', $topic_data['topic_poster'], $topic_data['topic_first_poster_name'], $topic_data['topic_first_poster_colour']),
   1.629 +
   1.630 +	'PAGINATION' 	=> $pagination,
   1.631 +	'PAGE_NUMBER' 	=> on_page($total_posts, $config['posts_per_page'], $start),
   1.632 +	'TOTAL_POSTS'	=> ($total_posts == 1) ? $user->lang['VIEW_TOPIC_POST'] : sprintf($user->lang['VIEW_TOPIC_POSTS'], $total_posts),
   1.633 +	'U_MCP' 		=> ($auth->acl_get('m_', $forum_id)) ? append_sid("{$phpbb_root_path}mcp.$phpEx", "i=main&amp;mode=topic_view&amp;f=$forum_id&amp;t=$topic_id" . (($start == 0) ? '' : "&amp;start=$start") . ((strlen($u_sort_param)) ? "&amp;$u_sort_param" : ''), true, $user->session_id) : '',
   1.634 +	'MODERATORS'	=> (isset($forum_moderators[$forum_id]) && sizeof($forum_moderators[$forum_id])) ? implode(', ', $forum_moderators[$forum_id]) : '',
   1.635 +
   1.636 +	'POST_IMG' 			=> ($topic_data['forum_status'] == ITEM_LOCKED) ? $user->img('button_topic_locked', 'FORUM_LOCKED') : $user->img('button_topic_new', 'POST_NEW_TOPIC'),
   1.637 +	'QUOTE_IMG' 		=> $user->img('icon_post_quote', 'REPLY_WITH_QUOTE'),
   1.638 +	'REPLY_IMG'			=> ($topic_data['forum_status'] == ITEM_LOCKED || $topic_data['topic_status'] == ITEM_LOCKED) ? $user->img('button_topic_locked', 'TOPIC_LOCKED') : $user->img('button_topic_reply', 'REPLY_TO_TOPIC'),
   1.639 +	'EDIT_IMG' 			=> $user->img('icon_post_edit', 'EDIT_POST'),
   1.640 +	'DELETE_IMG' 		=> $user->img('icon_post_delete', 'DELETE_POST'),
   1.641 +	'INFO_IMG' 			=> $user->img('icon_post_info', 'VIEW_INFO'),
   1.642 +	'PROFILE_IMG'		=> $user->img('icon_user_profile', 'READ_PROFILE'),
   1.643 +	'SEARCH_IMG' 		=> $user->img('icon_user_search', 'SEARCH_USER_POSTS'),
   1.644 +	'PM_IMG' 			=> $user->img('icon_contact_pm', 'SEND_PRIVATE_MESSAGE'),
   1.645 +	'EMAIL_IMG' 		=> $user->img('icon_contact_email', 'SEND_EMAIL'),
   1.646 +	'WWW_IMG' 			=> $user->img('icon_contact_www', 'VISIT_WEBSITE'),
   1.647 +	'ICQ_IMG' 			=> $user->img('icon_contact_icq', 'ICQ'),
   1.648 +	'AIM_IMG' 			=> $user->img('icon_contact_aim', 'AIM'),
   1.649 +	'MSN_IMG' 			=> $user->img('icon_contact_msnm', 'MSNM'),
   1.650 +	'YIM_IMG' 			=> $user->img('icon_contact_yahoo', 'YIM'),
   1.651 +	'JABBER_IMG'		=> $user->img('icon_contact_jabber', 'JABBER') ,
   1.652 +	'REPORT_IMG'		=> $user->img('icon_post_report', 'REPORT_POST'),
   1.653 +	'REPORTED_IMG'		=> $user->img('icon_topic_reported', 'POST_REPORTED'),
   1.654 +	'UNAPPROVED_IMG'	=> $user->img('icon_topic_unapproved', 'POST_UNAPPROVED'),
   1.655 +	'WARN_IMG'			=> $user->img('icon_user_warn', 'WARN_USER'),
   1.656 +
   1.657 +	'S_IS_LOCKED'			=> ($topic_data['topic_status'] == ITEM_UNLOCKED && $topic_data['forum_status'] == ITEM_UNLOCKED) ? false : true,
   1.658 +	'S_SELECT_SORT_DIR' 	=> $s_sort_dir,
   1.659 +	'S_SELECT_SORT_KEY' 	=> $s_sort_key,
   1.660 +	'S_SELECT_SORT_DAYS' 	=> $s_limit_days,
   1.661 +	'S_SINGLE_MODERATOR'	=> (!empty($forum_moderators[$forum_id]) && sizeof($forum_moderators[$forum_id]) > 1) ? false : true,
   1.662 +	'S_TOPIC_ACTION' 		=> append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id" . (($start == 0) ? '' : "&amp;start=$start")),
   1.663 +	'S_TOPIC_MOD' 			=> ($topic_mod != '') ? '<select name="action" id="quick-mod-select">' . $topic_mod . '</select>' : '',
   1.664 +	'S_MOD_ACTION' 			=> append_sid("{$phpbb_root_path}mcp.$phpEx", "f=$forum_id&amp;t=$topic_id" . (($start == 0) ? '' : "&amp;start=$start") . "&amp;quickmod=1&amp;redirect=" . urlencode(str_replace('&amp;', '&', $viewtopic_url)), true, $user->session_id),
   1.665 +
   1.666 +	'S_VIEWTOPIC'			=> true,
   1.667 +	'S_DISPLAY_SEARCHBOX'	=> ($auth->acl_get('u_search') && $auth->acl_get('f_search', $forum_id) && $config['load_search']) ? true : false,
   1.668 +	'S_SEARCHBOX_ACTION'	=> append_sid("{$phpbb_root_path}search.$phpEx"),
   1.669 +	'S_SEARCH_LOCAL_HIDDEN_FIELDS'	=> build_hidden_fields($s_search_hidden_fields),
   1.670 +
   1.671 +	'S_DISPLAY_POST_INFO'	=> ($topic_data['forum_type'] == FORUM_POST && ($auth->acl_get('f_post', $forum_id) || $user->data['user_id'] == ANONYMOUS)) ? true : false,
   1.672 +	'S_DISPLAY_REPLY_INFO'	=> ($topic_data['forum_type'] == FORUM_POST && ($auth->acl_get('f_reply', $forum_id) || $user->data['user_id'] == ANONYMOUS)) ? true : false,
   1.673 +	'S_ENABLE_FEEDS_TOPIC'	=> ($config['feed_topic'] && !phpbb_optionget(FORUM_OPTION_FEED_EXCLUDE, $topic_data['forum_options'])) ? true : false,
   1.674 +
   1.675 +	'U_TOPIC'				=> "{$server_path}viewtopic.$phpEx?f=$forum_id&amp;t=$topic_id",
   1.676 +	'U_FORUM'				=> $server_path,
   1.677 +	'U_VIEW_TOPIC' 			=> $viewtopic_url,
   1.678 +	'U_VIEW_FORUM' 			=> append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_id),
   1.679 +	'U_VIEW_OLDER_TOPIC'	=> append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;view=previous"),
   1.680 +	'U_VIEW_NEWER_TOPIC'	=> append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;view=next"),
   1.681 +	'U_PRINT_TOPIC'			=> ($auth->acl_get('f_print', $forum_id)) ? $viewtopic_url . '&amp;view=print' : '',
   1.682 +	'U_EMAIL_TOPIC'			=> ($auth->acl_get('f_email', $forum_id) && $config['email_enable']) ? append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=email&amp;t=$topic_id") : '',
   1.683 +
   1.684 +	'U_WATCH_TOPIC' 		=> $s_watching_topic['link'],
   1.685 +	'L_WATCH_TOPIC' 		=> $s_watching_topic['title'],
   1.686 +	'S_WATCHING_TOPIC'		=> $s_watching_topic['is_watching'],
   1.687 +
   1.688 +	'U_BOOKMARK_TOPIC'		=> ($user->data['is_registered'] && $config['allow_bookmarks']) ? $viewtopic_url . '&amp;bookmark=1&amp;hash=' . generate_link_hash("topic_$topic_id") : '',
   1.689 +	'L_BOOKMARK_TOPIC'		=> ($user->data['is_registered'] && $config['allow_bookmarks'] && $topic_data['bookmarked']) ? $user->lang['BOOKMARK_TOPIC_REMOVE'] : $user->lang['BOOKMARK_TOPIC'],
   1.690 +
   1.691 +	'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.692 +	'U_POST_REPLY_TOPIC' 	=> ($auth->acl_get('f_reply', $forum_id) || $user->data['user_id'] == ANONYMOUS) ? append_sid("{$phpbb_root_path}posting.$phpEx", "mode=reply&amp;f=$forum_id&amp;t=$topic_id") : '',
   1.693 +	'U_BUMP_TOPIC'			=> (bump_topic_allowed($forum_id, $topic_data['topic_bumped'], $topic_data['topic_last_post_time'], $topic_data['topic_poster'], $topic_data['topic_last_poster_id'])) ? append_sid("{$phpbb_root_path}posting.$phpEx", "mode=bump&amp;f=$forum_id&amp;t=$topic_id&amp;hash=" . generate_link_hash("topic_$topic_id")) : '')
   1.694 +);
   1.695 +
   1.696 +// Does this topic contain a poll?
   1.697 +if (!empty($topic_data['poll_start']))
   1.698 +{
   1.699 +	$sql = 'SELECT o.*, p.bbcode_bitfield, p.bbcode_uid
   1.700 +		FROM ' . POLL_OPTIONS_TABLE . ' o, ' . POSTS_TABLE . " p
   1.701 +		WHERE o.topic_id = $topic_id
   1.702 +			AND p.post_id = {$topic_data['topic_first_post_id']}
   1.703 +			AND p.topic_id = o.topic_id
   1.704 +		ORDER BY o.poll_option_id";
   1.705 +	$result = $db->sql_query($sql);
   1.706 +
   1.707 +	$poll_info = array();
   1.708 +	while ($row = $db->sql_fetchrow($result))
   1.709 +	{
   1.710 +		$poll_info[] = $row;
   1.711 +	}
   1.712 +	$db->sql_freeresult($result);
   1.713 +
   1.714 +	$cur_voted_id = array();
   1.715 +	if ($user->data['is_registered'])
   1.716 +	{
   1.717 +		$sql = 'SELECT poll_option_id
   1.718 +			FROM ' . POLL_VOTES_TABLE . '
   1.719 +			WHERE topic_id = ' . $topic_id . '
   1.720 +				AND vote_user_id = ' . $user->data['user_id'];
   1.721 +		$result = $db->sql_query($sql);
   1.722 +
   1.723 +		while ($row = $db->sql_fetchrow($result))
   1.724 +		{
   1.725 +			$cur_voted_id[] = $row['poll_option_id'];
   1.726 +		}
   1.727 +		$db->sql_freeresult($result);
   1.728 +	}
   1.729 +	else
   1.730 +	{
   1.731 +		// Cookie based guest tracking ... I don't like this but hum ho
   1.732 +		// it's oft requested. This relies on "nice" users who don't feel
   1.733 +		// the need to delete cookies to mess with results.
   1.734 +		if (isset($_COOKIE[$config['cookie_name'] . '_poll_' . $topic_id]))
   1.735 +		{
   1.736 +			$cur_voted_id = explode(',', $_COOKIE[$config['cookie_name'] . '_poll_' . $topic_id]);
   1.737 +			$cur_voted_id = array_map('intval', $cur_voted_id);
   1.738 +		}
   1.739 +	}
   1.740 +
   1.741 +	// Can not vote at all if no vote permission
   1.742 +	$s_can_vote = ($auth->acl_get('f_vote', $forum_id) &&
   1.743 +		(($topic_data['poll_length'] != 0 && $topic_data['poll_start'] + $topic_data['poll_length'] > time()) || $topic_data['poll_length'] == 0) &&
   1.744 +		$topic_data['topic_status'] != ITEM_LOCKED &&
   1.745 +		$topic_data['forum_status'] != ITEM_LOCKED &&
   1.746 +		(!sizeof($cur_voted_id) ||
   1.747 +		($auth->acl_get('f_votechg', $forum_id) && $topic_data['poll_vote_change']))) ? true : false;
   1.748 +	$s_display_results = (!$s_can_vote || ($s_can_vote && sizeof($cur_voted_id)) || $view == 'viewpoll') ? true : false;
   1.749 +
   1.750 +	if ($update && $s_can_vote)
   1.751 +	{
   1.752 +
   1.753 +		if (!sizeof($voted_id) || sizeof($voted_id) > $topic_data['poll_max_options'] || in_array(VOTE_CONVERTED, $cur_voted_id) || !check_form_key('posting'))
   1.754 +		{
   1.755 +			$redirect_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id" . (($start == 0) ? '' : "&amp;start=$start"));
   1.756 +
   1.757 +			meta_refresh(5, $redirect_url);
   1.758 +			if (!sizeof($voted_id))
   1.759 +			{
   1.760 +				$message = 'NO_VOTE_OPTION';
   1.761 +			}
   1.762 +			else if (sizeof($voted_id) > $topic_data['poll_max_options'])
   1.763 +			{
   1.764 +				$message = 'TOO_MANY_VOTE_OPTIONS';
   1.765 +			}
   1.766 +			else if (in_array(VOTE_CONVERTED, $cur_voted_id))
   1.767 +			{
   1.768 +				$message = 'VOTE_CONVERTED';
   1.769 +			}
   1.770 +			else
   1.771 +			{
   1.772 +				$message = 'FORM_INVALID';
   1.773 +			}
   1.774 +
   1.775 +			$message = $user->lang[$message] . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $redirect_url . '">', '</a>');
   1.776 +			trigger_error($message);
   1.777 +		}
   1.778 +
   1.779 +		foreach ($voted_id as $option)
   1.780 +		{
   1.781 +			if (in_array($option, $cur_voted_id))
   1.782 +			{
   1.783 +				continue;
   1.784 +			}
   1.785 +
   1.786 +			$sql = 'UPDATE ' . POLL_OPTIONS_TABLE . '
   1.787 +				SET poll_option_total = poll_option_total + 1
   1.788 +				WHERE poll_option_id = ' . (int) $option . '
   1.789 +					AND topic_id = ' . (int) $topic_id;
   1.790 +			$db->sql_query($sql);
   1.791 +
   1.792 +			if ($user->data['is_registered'])
   1.793 +			{
   1.794 +				$sql_ary = array(
   1.795 +					'topic_id'			=> (int) $topic_id,
   1.796 +					'poll_option_id'	=> (int) $option,
   1.797 +					'vote_user_id'		=> (int) $user->data['user_id'],
   1.798 +					'vote_user_ip'		=> (string) $user->ip,
   1.799 +				);
   1.800 +
   1.801 +				$sql = 'INSERT INTO ' . POLL_VOTES_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
   1.802 +				$db->sql_query($sql);
   1.803 +			}
   1.804 +		}
   1.805 +
   1.806 +		foreach ($cur_voted_id as $option)
   1.807 +		{
   1.808 +			if (!in_array($option, $voted_id))
   1.809 +			{
   1.810 +				$sql = 'UPDATE ' . POLL_OPTIONS_TABLE . '
   1.811 +					SET poll_option_total = poll_option_total - 1
   1.812 +					WHERE poll_option_id = ' . (int) $option . '
   1.813 +						AND topic_id = ' . (int) $topic_id;
   1.814 +				$db->sql_query($sql);
   1.815 +
   1.816 +				if ($user->data['is_registered'])
   1.817 +				{
   1.818 +					$sql = 'DELETE FROM ' . POLL_VOTES_TABLE . '
   1.819 +						WHERE topic_id = ' . (int) $topic_id . '
   1.820 +							AND poll_option_id = ' . (int) $option . '
   1.821 +							AND vote_user_id = ' . (int) $user->data['user_id'];
   1.822 +					$db->sql_query($sql);
   1.823 +				}
   1.824 +			}
   1.825 +		}
   1.826 +
   1.827 +		if ($user->data['user_id'] == ANONYMOUS && !$user->data['is_bot'])
   1.828 +		{
   1.829 +			$user->set_cookie('poll_' . $topic_id, implode(',', $voted_id), time() + 31536000);
   1.830 +		}
   1.831 +
   1.832 +		$sql = 'UPDATE ' . TOPICS_TABLE . '
   1.833 +			SET poll_last_vote = ' . time() . "
   1.834 +			WHERE topic_id = $topic_id";
   1.835 +		//, topic_last_post_time = ' . time() . " -- for bumping topics with new votes, ignore for now
   1.836 +		$db->sql_query($sql);
   1.837 +
   1.838 +		$redirect_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id" . (($start == 0) ? '' : "&amp;start=$start"));
   1.839 +
   1.840 +		meta_refresh(5, $redirect_url);
   1.841 +		trigger_error($user->lang['VOTE_SUBMITTED'] . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $redirect_url . '">', '</a>'));
   1.842 +	}
   1.843 +
   1.844 +	$poll_total = 0;
   1.845 +	foreach ($poll_info as $poll_option)
   1.846 +	{
   1.847 +		$poll_total += $poll_option['poll_option_total'];
   1.848 +	}
   1.849 +
   1.850 +	if ($poll_info[0]['bbcode_bitfield'])
   1.851 +	{
   1.852 +		$poll_bbcode = new bbcode();
   1.853 +	}
   1.854 +	else
   1.855 +	{
   1.856 +		$poll_bbcode = false;
   1.857 +	}
   1.858 +
   1.859 +	for ($i = 0, $size = sizeof($poll_info); $i < $size; $i++)
   1.860 +	{
   1.861 +		$poll_info[$i]['poll_option_text'] = censor_text($poll_info[$i]['poll_option_text']);
   1.862 +
   1.863 +		if ($poll_bbcode !== false)
   1.864 +		{
   1.865 +			$poll_bbcode->bbcode_second_pass($poll_info[$i]['poll_option_text'], $poll_info[$i]['bbcode_uid'], $poll_option['bbcode_bitfield']);
   1.866 +		}
   1.867 +
   1.868 +		$poll_info[$i]['poll_option_text'] = bbcode_nl2br($poll_info[$i]['poll_option_text']);
   1.869 +		$poll_info[$i]['poll_option_text'] = smiley_text($poll_info[$i]['poll_option_text']);
   1.870 +	}
   1.871 +
   1.872 +	$topic_data['poll_title'] = censor_text($topic_data['poll_title']);
   1.873 +
   1.874 +	if ($poll_bbcode !== false)
   1.875 +	{
   1.876 +		$poll_bbcode->bbcode_second_pass($topic_data['poll_title'], $poll_info[0]['bbcode_uid'], $poll_info[0]['bbcode_bitfield']);
   1.877 +	}
   1.878 +
   1.879 +	$topic_data['poll_title'] = bbcode_nl2br($topic_data['poll_title']);
   1.880 +	$topic_data['poll_title'] = smiley_text($topic_data['poll_title']);
   1.881 +
   1.882 +	unset($poll_bbcode);
   1.883 +
   1.884 +	foreach ($poll_info as $poll_option)
   1.885 +	{
   1.886 +		$option_pct = ($poll_total > 0) ? $poll_option['poll_option_total'] / $poll_total : 0;
   1.887 +		$option_pct_txt = sprintf("%.1d%%", round($option_pct * 100));
   1.888 +
   1.889 +		$template->assign_block_vars('poll_option', array(
   1.890 +			'POLL_OPTION_ID' 		=> $poll_option['poll_option_id'],
   1.891 +			'POLL_OPTION_CAPTION' 	=> $poll_option['poll_option_text'],
   1.892 +			'POLL_OPTION_RESULT' 	=> $poll_option['poll_option_total'],
   1.893 +			'POLL_OPTION_PERCENT' 	=> $option_pct_txt,
   1.894 +			'POLL_OPTION_PCT'		=> round($option_pct * 100),
   1.895 +			'POLL_OPTION_IMG' 		=> $user->img('poll_center', $option_pct_txt, round($option_pct * 250)),
   1.896 +			'POLL_OPTION_VOTED'		=> (in_array($poll_option['poll_option_id'], $cur_voted_id)) ? true : false)
   1.897 +		);
   1.898 +	}
   1.899 +
   1.900 +	$poll_end = $topic_data['poll_length'] + $topic_data['poll_start'];
   1.901 +
   1.902 +	$template->assign_vars(array(
   1.903 +		'POLL_QUESTION'		=> $topic_data['poll_title'],
   1.904 +		'TOTAL_VOTES' 		=> $poll_total,
   1.905 +		'POLL_LEFT_CAP_IMG'	=> $user->img('poll_left'),
   1.906 +		'POLL_RIGHT_CAP_IMG'=> $user->img('poll_right'),
   1.907 +
   1.908 +		'L_MAX_VOTES'		=> ($topic_data['poll_max_options'] == 1) ? $user->lang['MAX_OPTION_SELECT'] : sprintf($user->lang['MAX_OPTIONS_SELECT'], $topic_data['poll_max_options']),
   1.909 +		'L_POLL_LENGTH'		=> ($topic_data['poll_length']) ? sprintf($user->lang[($poll_end > time()) ? 'POLL_RUN_TILL' : 'POLL_ENDED_AT'], $user->format_date($poll_end)) : '',
   1.910 +
   1.911 +		'S_HAS_POLL'		=> true,
   1.912 +		'S_CAN_VOTE'		=> $s_can_vote,
   1.913 +		'S_DISPLAY_RESULTS'	=> $s_display_results,
   1.914 +		'S_IS_MULTI_CHOICE'	=> ($topic_data['poll_max_options'] > 1) ? true : false,
   1.915 +		'S_POLL_ACTION'		=> $viewtopic_url,
   1.916 +
   1.917 +		'U_VIEW_RESULTS'	=> $viewtopic_url . '&amp;view=viewpoll')
   1.918 +	);
   1.919 +
   1.920 +	unset($poll_end, $poll_info, $voted_id);
   1.921 +}
   1.922 +
   1.923 +// If the user is trying to reach the second half of the topic, fetch it starting from the end
   1.924 +$store_reverse = false;
   1.925 +$sql_limit = $config['posts_per_page'];
   1.926 +$sql_sort_order = $direction = '';
   1.927 +
   1.928 +if ($start > $total_posts / 2)
   1.929 +{
   1.930 +	$store_reverse = true;
   1.931 +
   1.932 +	if ($start + $config['posts_per_page'] > $total_posts)
   1.933 +	{
   1.934 +		$sql_limit = min($config['posts_per_page'], max(1, $total_posts - $start));
   1.935 +	}
   1.936 +
   1.937 +	// Select the sort order
   1.938 +	$direction = (($sort_dir == 'd') ? 'ASC' : 'DESC');
   1.939 +	$sql_start = max(0, $total_posts - $sql_limit - $start);
   1.940 +}
   1.941 +else
   1.942 +{
   1.943 +	// Select the sort order
   1.944 +	$direction = (($sort_dir == 'd') ? 'DESC' : 'ASC');
   1.945 +	$sql_start = $start;
   1.946 +}
   1.947 +
   1.948 +if (is_array($sort_by_sql[$sort_key]))
   1.949 +{
   1.950 +	$sql_sort_order = implode(' ' . $direction . ', ', $sort_by_sql[$sort_key]) . ' ' . $direction;
   1.951 +}
   1.952 +else
   1.953 +{
   1.954 +	$sql_sort_order = $sort_by_sql[$sort_key] . ' ' . $direction;
   1.955 +}
   1.956 +
   1.957 +// Container for user details, only process once
   1.958 +$post_list = $user_cache = $id_cache = $attachments = $attach_list = $rowset = $update_count = $post_edit_list = array();
   1.959 +$has_attachments = $display_notice = false;
   1.960 +$bbcode_bitfield = '';
   1.961 +$i = $i_total = 0;
   1.962 +
   1.963 +// Go ahead and pull all data for this topic
   1.964 +$sql = 'SELECT p.post_id
   1.965 +	FROM ' . POSTS_TABLE . ' p' . (($join_user_sql[$sort_key]) ? ', ' . USERS_TABLE . ' u': '') . "
   1.966 +	WHERE p.topic_id = $topic_id
   1.967 +		" . ((!$auth->acl_get('m_approve', $forum_id)) ? 'AND p.post_approved = 1' : '') . "
   1.968 +		" . (($join_user_sql[$sort_key]) ? 'AND u.user_id = p.poster_id': '') . "
   1.969 +		$limit_posts_time
   1.970 +	ORDER BY $sql_sort_order";
   1.971 +$result = $db->sql_query_limit($sql, $sql_limit, $sql_start);
   1.972 +
   1.973 +$i = ($store_reverse) ? $sql_limit - 1 : 0;
   1.974 +while ($row = $db->sql_fetchrow($result))
   1.975 +{
   1.976 +	$post_list[$i] = (int) $row['post_id'];
   1.977 +	($store_reverse) ? $i-- : $i++;
   1.978 +}
   1.979 +$db->sql_freeresult($result);
   1.980 +
   1.981 +if (!sizeof($post_list))
   1.982 +{
   1.983 +	if ($sort_days)
   1.984 +	{
   1.985 +		trigger_error('NO_POSTS_TIME_FRAME');
   1.986 +	}
   1.987 +	else
   1.988 +	{
   1.989 +		trigger_error('NO_TOPIC');
   1.990 +	}
   1.991 +}
   1.992 +
   1.993 +// Holding maximum post time for marking topic read
   1.994 +// We need to grab it because we do reverse ordering sometimes
   1.995 +$max_post_time = 0;
   1.996 +
   1.997 +$sql = $db->sql_build_query('SELECT', array(
   1.998 +	'SELECT'	=> 'u.*, z.friend, z.foe, p.*',
   1.999 +
  1.1000 +	'FROM'		=> array(
  1.1001 +		USERS_TABLE		=> 'u',
  1.1002 +		POSTS_TABLE		=> 'p',
  1.1003 +	),
  1.1004 +
  1.1005 +	'LEFT_JOIN'	=> array(
  1.1006 +		array(
  1.1007 +			'FROM'	=> array(ZEBRA_TABLE => 'z'),
  1.1008 +			'ON'	=> 'z.user_id = ' . $user->data['user_id'] . ' AND z.zebra_id = p.poster_id'
  1.1009 +		)
  1.1010 +	),
  1.1011 +
  1.1012 +	'WHERE'		=> $db->sql_in_set('p.post_id', $post_list) . '
  1.1013 +		AND u.user_id = p.poster_id'
  1.1014 +));
  1.1015 +
  1.1016 +$result = $db->sql_query($sql);
  1.1017 +
  1.1018 +$now = phpbb_gmgetdate(time() + $user->timezone + $user->dst);
  1.1019 +
  1.1020 +// Posts are stored in the $rowset array while $attach_list, $user_cache
  1.1021 +// and the global bbcode_bitfield are built
  1.1022 +while ($row = $db->sql_fetchrow($result))
  1.1023 +{
  1.1024 +	// Set max_post_time
  1.1025 +	if ($row['post_time'] > $max_post_time)
  1.1026 +	{
  1.1027 +		$max_post_time = $row['post_time'];
  1.1028 +	}
  1.1029 +
  1.1030 +	$poster_id = (int) $row['poster_id'];
  1.1031 +
  1.1032 +	// Does post have an attachment? If so, add it to the list
  1.1033 +	if ($row['post_attachment'] && $config['allow_attachments'])
  1.1034 +	{
  1.1035 +		$attach_list[] = (int) $row['post_id'];
  1.1036 +
  1.1037 +		if ($row['post_approved'])
  1.1038 +		{
  1.1039 +			$has_attachments = true;
  1.1040 +		}
  1.1041 +	}
  1.1042 +
  1.1043 +	$rowset[$row['post_id']] = array(
  1.1044 +		'hide_post'			=> ($row['foe'] && ($view != 'show' || $post_id != $row['post_id'])) ? true : false,
  1.1045 +
  1.1046 +		'post_id'			=> $row['post_id'],
  1.1047 +		'post_time'			=> $row['post_time'],
  1.1048 +		'user_id'			=> $row['user_id'],
  1.1049 +		'username'			=> $row['username'],
  1.1050 +		'user_colour'		=> $row['user_colour'],
  1.1051 +		'topic_id'			=> $row['topic_id'],
  1.1052 +		'forum_id'			=> $row['forum_id'],
  1.1053 +		'post_subject'		=> $row['post_subject'],
  1.1054 +		'post_edit_count'	=> $row['post_edit_count'],
  1.1055 +		'post_edit_time'	=> $row['post_edit_time'],
  1.1056 +		'post_edit_reason'	=> $row['post_edit_reason'],
  1.1057 +		'post_edit_user'	=> $row['post_edit_user'],
  1.1058 +		'post_edit_locked'	=> $row['post_edit_locked'],
  1.1059 +
  1.1060 +		// Make sure the icon actually exists
  1.1061 +		'icon_id'			=> (isset($icons[$row['icon_id']]['img'], $icons[$row['icon_id']]['height'], $icons[$row['icon_id']]['width'])) ? $row['icon_id'] : 0,
  1.1062 +		'post_attachment'	=> $row['post_attachment'],
  1.1063 +		'post_approved'		=> $row['post_approved'],
  1.1064 +		'post_reported'		=> $row['post_reported'],
  1.1065 +		'post_username'		=> $row['post_username'],
  1.1066 +		'post_text'			=> $row['post_text'],
  1.1067 +		'bbcode_uid'		=> $row['bbcode_uid'],
  1.1068 +		'bbcode_bitfield'	=> $row['bbcode_bitfield'],
  1.1069 +		'enable_smilies'	=> $row['enable_smilies'],
  1.1070 +		'enable_sig'		=> $row['enable_sig'],
  1.1071 +		'friend'			=> $row['friend'],
  1.1072 +		'foe'				=> $row['foe'],
  1.1073 +	);
  1.1074 +
  1.1075 +	// Define the global bbcode bitfield, will be used to load bbcodes
  1.1076 +	$bbcode_bitfield = $bbcode_bitfield | base64_decode($row['bbcode_bitfield']);
  1.1077 +
  1.1078 +	// Is a signature attached? Are we going to display it?
  1.1079 +	if ($row['enable_sig'] && $config['allow_sig'] && $user->optionget('viewsigs'))
  1.1080 +	{
  1.1081 +		$bbcode_bitfield = $bbcode_bitfield | base64_decode($row['user_sig_bbcode_bitfield']);
  1.1082 +	}
  1.1083 +
  1.1084 +	// Cache various user specific data ... so we don't have to recompute
  1.1085 +	// this each time the same user appears on this page
  1.1086 +	if (!isset($user_cache[$poster_id]))
  1.1087 +	{
  1.1088 +		if ($poster_id == ANONYMOUS)
  1.1089 +		{
  1.1090 +			$user_cache[$poster_id] = array(
  1.1091 +				'joined'		=> '',
  1.1092 +				'posts'			=> '',
  1.1093 +				'from'			=> '',
  1.1094 +
  1.1095 +				'sig'					=> '',
  1.1096 +				'sig_bbcode_uid'		=> '',
  1.1097 +				'sig_bbcode_bitfield'	=> '',
  1.1098 +
  1.1099 +				'online'			=> false,
  1.1100 +				'avatar'			=> ($user->optionget('viewavatars')) ? get_user_avatar($row['user_avatar'], $row['user_avatar_type'], $row['user_avatar_width'], $row['user_avatar_height']) : '',
  1.1101 +				'rank_title'		=> '',
  1.1102 +				'rank_image'		=> '',
  1.1103 +				'rank_image_src'	=> '',
  1.1104 +				'sig'				=> '',
  1.1105 +				'profile'			=> '',
  1.1106 +				'pm'				=> '',
  1.1107 +				'email'				=> '',
  1.1108 +				'www'				=> '',
  1.1109 +				'icq_status_img'	=> '',
  1.1110 +				'icq'				=> '',
  1.1111 +				'aim'				=> '',
  1.1112 +				'msn'				=> '',
  1.1113 +				'yim'				=> '',
  1.1114 +				'jabber'			=> '',
  1.1115 +				'search'			=> '',
  1.1116 +				'age'				=> '',
  1.1117 +
  1.1118 +				'username'			=> $row['username'],
  1.1119 +				'user_colour'		=> $row['user_colour'],
  1.1120 +
  1.1121 +				'warnings'			=> 0,
  1.1122 +				'allow_pm'			=> 0,
  1.1123 +			);
  1.1124 +
  1.1125 +			get_user_rank($row['user_rank'], false, $user_cache[$poster_id]['rank_title'], $user_cache[$poster_id]['rank_image'], $user_cache[$poster_id]['rank_image_src']);
  1.1126 +		}
  1.1127 +		else
  1.1128 +		{
  1.1129 +			$user_sig = '';
  1.1130 +
  1.1131 +			// We add the signature to every posters entry because enable_sig is post dependant
  1.1132 +			if ($row['user_sig'] && $config['allow_sig'] && $user->optionget('viewsigs'))
  1.1133 +			{
  1.1134 +				$user_sig = $row['user_sig'];
  1.1135 +			}
  1.1136 +
  1.1137 +			$id_cache[] = $poster_id;
  1.1138 +
  1.1139 +			$user_cache[$poster_id] = array(
  1.1140 +				'joined'		=> $user->format_date($row['user_regdate']),
  1.1141 +				'posts'			=> $row['user_posts'],
  1.1142 +				'warnings'		=> (isset($row['user_warnings'])) ? $row['user_warnings'] : 0,
  1.1143 +				'from'			=> (!empty($row['user_from'])) ? $row['user_from'] : '',
  1.1144 +
  1.1145 +				'sig'					=> $user_sig,
  1.1146 +				'sig_bbcode_uid'		=> (!empty($row['user_sig_bbcode_uid'])) ? $row['user_sig_bbcode_uid'] : '',
  1.1147 +				'sig_bbcode_bitfield'	=> (!empty($row['user_sig_bbcode_bitfield'])) ? $row['user_sig_bbcode_bitfield'] : '',
  1.1148 +
  1.1149 +				'viewonline'	=> $row['user_allow_viewonline'],
  1.1150 +				'allow_pm'		=> $row['user_allow_pm'],
  1.1151 +
  1.1152 +				'avatar'		=> ($user->optionget('viewavatars')) ? get_user_avatar($row['user_avatar'], $row['user_avatar_type'], $row['user_avatar_width'], $row['user_avatar_height']) : '',
  1.1153 +				'age'			=> '',
  1.1154 +
  1.1155 +				'rank_title'		=> '',
  1.1156 +				'rank_image'		=> '',
  1.1157 +				'rank_image_src'	=> '',
  1.1158 +
  1.1159 +				'username'			=> $row['username'],
  1.1160 +				'user_colour'		=> $row['user_colour'],
  1.1161 +
  1.1162 +				'online'		=> false,
  1.1163 +				'profile'		=> append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=viewprofile&amp;u=$poster_id"),
  1.1164 +				'www'			=> $row['user_website'],
  1.1165 +				'aim'			=> ($row['user_aim'] && $auth->acl_get('u_sendim')) ? append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=contact&amp;action=aim&amp;u=$poster_id") : '',
  1.1166 +				'msn'			=> ($row['user_msnm'] && $auth->acl_get('u_sendim')) ? append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=contact&amp;action=msnm&amp;u=$poster_id") : '',
  1.1167 +				'yim'			=> ($row['user_yim']) ? 'http://edit.yahoo.com/config/send_webmesg?.target=' . urlencode($row['user_yim']) . '&amp;.src=pg' : '',
  1.1168 +				'jabber'		=> ($row['user_jabber'] && $auth->acl_get('u_sendim')) ? append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=contact&amp;action=jabber&amp;u=$poster_id") : '',
  1.1169 +				'search'		=> ($auth->acl_get('u_search')) ? append_sid("{$phpbb_root_path}search.$phpEx", "author_id=$poster_id&amp;sr=posts") : '',
  1.1170 +
  1.1171 +				'author_full'		=> get_username_string('full', $poster_id, $row['username'], $row['user_colour']),
  1.1172 +				'author_colour'		=> get_username_string('colour', $poster_id, $row['username'], $row['user_colour']),
  1.1173 +				'author_username'	=> get_username_string('username', $poster_id, $row['username'], $row['user_colour']),
  1.1174 +				'author_profile'	=> get_username_string('profile', $poster_id, $row['username'], $row['user_colour']),
  1.1175 +			);
  1.1176 +
  1.1177 +			get_user_rank($row['user_rank'], $row['user_posts'], $user_cache[$poster_id]['rank_title'], $user_cache[$poster_id]['rank_image'], $user_cache[$poster_id]['rank_image_src']);
  1.1178 +
  1.1179 +			if ((!empty($row['user_allow_viewemail']) && $auth->acl_get('u_sendemail')) || $auth->acl_get('a_email'))
  1.1180 +			{
  1.1181 +				$user_cache[$poster_id]['email'] = ($config['board_email_form'] && $config['email_enable']) ? append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=email&amp;u=$poster_id") : (($config['board_hide_emails'] && !$auth->acl_get('a_email')) ? '' : 'mailto:' . $row['user_email']);
  1.1182 +			}
  1.1183 +			else
  1.1184 +			{
  1.1185 +				$user_cache[$poster_id]['email'] = '';
  1.1186 +			}
  1.1187 +
  1.1188 +			if (!empty($row['user_icq']))
  1.1189 +			{
  1.1190 +				$user_cache[$poster_id]['icq'] = 'http://www.icq.com/people/' . urlencode($row['user_icq']) . '/';
  1.1191 +				$user_cache[$poster_id]['icq_status_img'] = '<img src="http://web.icq.com/whitepages/online?icq=' . $row['user_icq'] . '&amp;img=5" width="18" height="18" alt="" />';
  1.1192 +			}
  1.1193 +			else
  1.1194 +			{
  1.1195 +				$user_cache[$poster_id]['icq_status_img'] = '';
  1.1196 +				$user_cache[$poster_id]['icq'] = '';
  1.1197 +			}
  1.1198 +
  1.1199 +			if ($config['allow_birthdays'] && !empty($row['user_birthday']))
  1.1200 +			{
  1.1201 +				list($bday_day, $bday_month, $bday_year) = array_map('intval', explode('-', $row['user_birthday']));
  1.1202 +
  1.1203 +				if ($bday_year)
  1.1204 +				{
  1.1205 +					$diff = $now['mon'] - $bday_month;
  1.1206 +					if ($diff == 0)
  1.1207 +					{
  1.1208 +						$diff = ($now['mday'] - $bday_day < 0) ? 1 : 0;
  1.1209 +					}
  1.1210 +					else
  1.1211 +					{
  1.1212 +						$diff = ($diff < 0) ? 1 : 0;
  1.1213 +					}
  1.1214 +
  1.1215 +					$user_cache[$poster_id]['age'] = (int) ($now['year'] - $bday_year - $diff);
  1.1216 +				}
  1.1217 +			}
  1.1218 +		}
  1.1219 +	}
  1.1220 +}
  1.1221 +$db->sql_freeresult($result);
  1.1222 +
  1.1223 +// Load custom profile fields
  1.1224 +if ($config['load_cpf_viewtopic'])
  1.1225 +{
  1.1226 +	if (!class_exists('custom_profile'))
  1.1227 +	{
  1.1228 +		include($phpbb_root_path . 'includes/functions_profile_fields.' . $phpEx);
  1.1229 +	}
  1.1230 +	$cp = new custom_profile();
  1.1231 +
  1.1232 +	// Grab all profile fields from users in id cache for later use - similar to the poster cache
  1.1233 +	$profile_fields_tmp = $cp->generate_profile_fields_template('grab', $id_cache);
  1.1234 +
  1.1235 +	// filter out fields not to be displayed on viewtopic. Yes, it's a hack, but this shouldn't break any MODs.
  1.1236 +	$profile_fields_cache = array();
  1.1237 +	foreach ($profile_fields_tmp as $profile_user_id => $profile_fields)
  1.1238 +	{
  1.1239 +		$profile_fields_cache[$profile_user_id] = array();
  1.1240 +		foreach ($profile_fields as $used_ident => $profile_field)
  1.1241 +		{
  1.1242 +			if ($profile_field['data']['field_show_on_vt'])
  1.1243 +			{
  1.1244 +				$profile_fields_cache[$profile_user_id][$used_ident] = $profile_field;
  1.1245 +			}
  1.1246 +		}
  1.1247 +	}
  1.1248 +	unset($profile_fields_tmp);
  1.1249 +}
  1.1250 +
  1.1251 +// Generate online information for user
  1.1252 +if ($config['load_onlinetrack'] && sizeof($id_cache))
  1.1253 +{
  1.1254 +	$sql = 'SELECT session_user_id, MAX(session_time) as online_time, MIN(session_viewonline) AS viewonline
  1.1255 +		FROM ' . SESSIONS_TABLE . '
  1.1256 +		WHERE ' . $db->sql_in_set('session_user_id', $id_cache) . '
  1.1257 +		GROUP BY session_user_id';
  1.1258 +	$result = $db->sql_query($sql);
  1.1259 +
  1.1260 +	$update_time = $config['load_online_time'] * 60;
  1.1261 +	while ($row = $db->sql_fetchrow($result))
  1.1262 +	{
  1.1263 +		$user_cache[$row['session_user_id']]['online'] = (time() - $update_time < $row['online_time'] && (($row['viewonline']) || $auth->acl_get('u_viewonline'))) ? true : false;
  1.1264 +	}
  1.1265 +	$db->sql_freeresult($result);
  1.1266 +}
  1.1267 +unset($id_cache);
  1.1268 +
  1.1269 +// Pull attachment data
  1.1270 +if (sizeof($attach_list))
  1.1271 +{
  1.1272 +	if ($auth->acl_get('u_download') && $auth->acl_get('f_download', $forum_id))
  1.1273 +	{
  1.1274 +		$sql = 'SELECT *
  1.1275 +			FROM ' . ATTACHMENTS_TABLE . '
  1.1276 +			WHERE ' . $db->sql_in_set('post_msg_id', $attach_list) . '
  1.1277 +				AND in_message = 0
  1.1278 +			ORDER BY filetime DESC, post_msg_id ASC';
  1.1279 +		$result = $db->sql_query($sql);
  1.1280 +
  1.1281 +		while ($row = $db->sql_fetchrow($result))
  1.1282 +		{
  1.1283 +			$attachments[$row['post_msg_id']][] = $row;
  1.1284 +		}
  1.1285 +		$db->sql_freeresult($result);
  1.1286 +
  1.1287 +		// No attachments exist, but post table thinks they do so go ahead and reset post_attach flags
  1.1288 +		if (!sizeof($attachments))
  1.1289 +		{
  1.1290 +			$sql = 'UPDATE ' . POSTS_TABLE . '
  1.1291 +				SET post_attachment = 0
  1.1292 +				WHERE ' . $db->sql_in_set('post_id', $attach_list);
  1.1293 +			$db->sql_query($sql);
  1.1294 +
  1.1295 +			// We need to update the topic indicator too if the complete topic is now without an attachment
  1.1296 +			if (sizeof($rowset) != $total_posts)
  1.1297 +			{
  1.1298 +				// Not all posts are displayed so we query the db to find if there's any attachment for this topic
  1.1299 +				$sql = 'SELECT a.post_msg_id as post_id
  1.1300 +					FROM ' . ATTACHMENTS_TABLE . ' a, ' . POSTS_TABLE . " p
  1.1301 +					WHERE p.topic_id = $topic_id
  1.1302 +						AND p.post_approved = 1
  1.1303 +						AND p.topic_id = a.topic_id";
  1.1304 +				$result = $db->sql_query_limit($sql, 1);
  1.1305 +				$row = $db->sql_fetchrow($result);
  1.1306 +				$db->sql_freeresult($result);
  1.1307 +
  1.1308 +				if (!$row)
  1.1309 +				{
  1.1310 +					$sql = 'UPDATE ' . TOPICS_TABLE . "
  1.1311 +						SET topic_attachment = 0
  1.1312 +						WHERE topic_id = $topic_id";
  1.1313 +					$db->sql_query($sql);
  1.1314 +				}
  1.1315 +			}
  1.1316 +			else
  1.1317 +			{
  1.1318 +				$sql = 'UPDATE ' . TOPICS_TABLE . "
  1.1319 +					SET topic_attachment = 0
  1.1320 +					WHERE topic_id = $topic_id";
  1.1321 +				$db->sql_query($sql);
  1.1322 +			}
  1.1323 +		}
  1.1324 +		else if ($has_attachments && !$topic_data['topic_attachment'])
  1.1325 +		{
  1.1326 +			// Topic has approved attachments but its flag is wrong
  1.1327 +			$sql = 'UPDATE ' . TOPICS_TABLE . "
  1.1328 +				SET topic_attachment = 1
  1.1329 +				WHERE topic_id = $topic_id";
  1.1330 +			$db->sql_query($sql);
  1.1331 +
  1.1332 +			$topic_data['topic_attachment'] = 1;
  1.1333 +		}
  1.1334 +	}
  1.1335 +	else
  1.1336 +	{
  1.1337 +		$display_notice = true;
  1.1338 +	}
  1.1339 +}
  1.1340 +
  1.1341 +// Instantiate BBCode if need be
  1.1342 +if ($bbcode_bitfield !== '')
  1.1343 +{
  1.1344 +	$bbcode = new bbcode(base64_encode($bbcode_bitfield));
  1.1345 +}
  1.1346 +
  1.1347 +$i_total = sizeof($rowset) - 1;
  1.1348 +$prev_post_id = '';
  1.1349 +
  1.1350 +$template->assign_vars(array(
  1.1351 +	'S_NUM_POSTS' => sizeof($post_list))
  1.1352 +);
  1.1353 +
  1.1354 +// Output the posts
  1.1355 +$first_unread = $post_unread = false;
  1.1356 +for ($i = 0, $end = sizeof($post_list); $i < $end; ++$i)
  1.1357 +{
  1.1358 +	// A non-existing rowset only happens if there was no user present for the entered poster_id
  1.1359 +	// This could be a broken posts table.
  1.1360 +	if (!isset($rowset[$post_list[$i]]))
  1.1361 +	{
  1.1362 +		continue;
  1.1363 +	}
  1.1364 +
  1.1365 +	$row =& $rowset[$post_list[$i]];
  1.1366 +	$poster_id = $row['user_id'];
  1.1367 +
  1.1368 +	// End signature parsing, only if needed
  1.1369 +	if ($user_cache[$poster_id]['sig'] && $row['enable_sig'] && empty($user_cache[$poster_id]['sig_parsed']))
  1.1370 +	{
  1.1371 +		$user_cache[$poster_id]['sig'] = censor_text($user_cache[$poster_id]['sig']);
  1.1372 +
  1.1373 +		if ($user_cache[$poster_id]['sig_bbcode_bitfield'])
  1.1374 +		{
  1.1375 +			$bbcode->bbcode_second_pass($user_cache[$poster_id]['sig'], $user_cache[$poster_id]['sig_bbcode_uid'], $user_cache[$poster_id]['sig_bbcode_bitfield']);
  1.1376 +		}
  1.1377 +
  1.1378 +		$user_cache[$poster_id]['sig'] = bbcode_nl2br($user_cache[$poster_id]['sig']);
  1.1379 +		$user_cache[$poster_id]['sig'] = smiley_text($user_cache[$poster_id]['sig']);
  1.1380 +		$user_cache[$poster_id]['sig_parsed'] = true;
  1.1381 +	}
  1.1382 +
  1.1383 +	// Parse the message and subject
  1.1384 +	$message = censor_text($row['post_text']);
  1.1385 +
  1.1386 +	// Second parse bbcode here
  1.1387 +	if ($row['bbcode_bitfield'])
  1.1388 +	{
  1.1389 +		$bbcode->bbcode_second_pass($message, $row['bbcode_uid'], $row['bbcode_bitfield']);
  1.1390 +	}
  1.1391 +
  1.1392 +	$message = bbcode_nl2br($message);
  1.1393 +	$message = smiley_text($message);
  1.1394 +
  1.1395 +	if (!empty($attachments[$row['post_id']]))
  1.1396 +	{
  1.1397 +		parse_attachments($forum_id, $message, $attachments[$row['post_id']], $update_count);
  1.1398 +	}
  1.1399 +
  1.1400 +	// Replace naughty words such as farty pants
  1.1401 +	$row['post_subject'] = censor_text($row['post_subject']);
  1.1402 +
  1.1403 +	// Highlight active words (primarily for search)
  1.1404 +	if ($highlight_match)
  1.1405 +	{
  1.1406 +		$message = preg_replace('#(?!<.*)(?<!\w)(' . $highlight_match . ')(?!\w|[^<>]*(?:</s(?:cript|tyle))?>)#is', '<span class="posthilit">\1</span>', $message);
  1.1407 +		$row['post_subject'] = preg_replace('#(?!<.*)(?<!\w)(' . $highlight_match . ')(?!\w|[^<>]*(?:</s(?:cript|tyle))?>)#is', '<span class="posthilit">\1</span>', $row['post_subject']);
  1.1408 +	}
  1.1409 +
  1.1410 +	// Editing information
  1.1411 +	if (($row['post_edit_count'] && $config['display_last_edited']) || $row['post_edit_reason'])
  1.1412 +	{
  1.1413 +		// Get usernames for all following posts if not already stored
  1.1414 +		if (!sizeof($post_edit_list) && ($row['post_edit_reason'] || ($row['post_edit_user'] && !isset($user_cache[$row['post_edit_user']]))))
  1.1415 +		{
  1.1416 +			// Remove all post_ids already parsed (we do not have to check them)
  1.1417 +			$post_storage_list = (!$store_reverse) ? array_slice($post_list, $i) : array_slice(array_reverse($post_list), $i);
  1.1418 +
  1.1419 +			$sql = 'SELECT DISTINCT u.user_id, u.username, u.user_colour
  1.1420 +				FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
  1.1421 +				WHERE ' . $db->sql_in_set('p.post_id', $post_storage_list) . '
  1.1422 +					AND p.post_edit_count <> 0
  1.1423 +					AND p.post_edit_user <> 0
  1.1424 +					AND p.post_edit_user = u.user_id';
  1.1425 +			$result2 = $db->sql_query($sql);
  1.1426 +			while ($user_edit_row = $db->sql_fetchrow($result2))
  1.1427 +			{
  1.1428 +				$post_edit_list[$user_edit_row['user_id']] = $user_edit_row;
  1.1429 +			}
  1.1430 +			$db->sql_freeresult($result2);
  1.1431 +
  1.1432 +			unset($post_storage_list);
  1.1433 +		}
  1.1434 +
  1.1435 +		$l_edit_time_total = ($row['post_edit_count'] == 1) ? $user->lang['EDITED_TIME_TOTAL'] : $user->lang['EDITED_TIMES_TOTAL'];
  1.1436 +
  1.1437 +		if ($row['post_edit_reason'])
  1.1438 +		{
  1.1439 +			// User having edited the post also being the post author?
  1.1440 +			if (!$row['post_edit_user'] || $row['post_edit_user'] == $poster_id)
  1.1441 +			{
  1.1442 +				$display_username = get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']);
  1.1443 +			}
  1.1444 +			else
  1.1445 +			{
  1.1446 +				$display_username = get_username_string('full', $row['post_edit_user'], $post_edit_list[$row['post_edit_user']]['username'], $post_edit_list[$row['post_edit_user']]['user_colour']);
  1.1447 +			}
  1.1448 +
  1.1449 +			$l_edited_by = sprintf($l_edit_time_total, $display_username, $user->format_date($row['post_edit_time'], false, true), $row['post_edit_count']);
  1.1450 +		}
  1.1451 +		else
  1.1452 +		{
  1.1453 +			if ($row['post_edit_user'] && !isset($user_cache[$row['post_edit_user']]))
  1.1454 +			{
  1.1455 +				$user_cache[$row['post_edit_user']] = $post_edit_list[$row['post_edit_user']];
  1.1456 +			}
  1.1457 +
  1.1458 +			// User having edited the post also being the post author?
  1.1459 +			if (!$row['post_edit_user'] || $row['post_edit_user'] == $poster_id)
  1.1460 +			{
  1.1461 +				$display_username = get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']);
  1.1462 +			}
  1.1463 +			else
  1.1464 +			{
  1.1465 +				$display_username = get_username_string('full', $row['post_edit_user'], $user_cache[$row['post_edit_user']]['username'], $user_cache[$row['post_edit_user']]['user_colour']);
  1.1466 +			}
  1.1467 +
  1.1468 +			$l_edited_by = sprintf($l_edit_time_total, $display_username, $user->format_date($row['post_edit_time'], false, true), $row['post_edit_count']);
  1.1469 +		}
  1.1470 +	}
  1.1471 +	else
  1.1472 +	{
  1.1473 +		$l_edited_by = '';
  1.1474 +	}
  1.1475 +
  1.1476 +	// Bump information
  1.1477 +	if ($topic_data['topic_bumped'] && $row['post_id'] == $topic_data['topic_last_post_id'] && isset($user_cache[$topic_data['topic_bumper']]) )
  1.1478 +	{
  1.1479 +		// It is safe to grab the username from the user cache array, we are at the last
  1.1480 +		// post and only the topic poster and last poster are allowed to bump.
  1.1481 +		// Admins and mods are bound to the above rules too...
  1.1482 +		$l_bumped_by = sprintf($user->lang['BUMPED_BY'], $user_cache[$topic_data['topic_bumper']]['username'], $user->format_date($topic_data['topic_last_post_time'], false, true));
  1.1483 +	}
  1.1484 +	else
  1.1485 +	{
  1.1486 +		$l_bumped_by = '';
  1.1487 +	}
  1.1488 +
  1.1489 +	$cp_row = array();
  1.1490 +
  1.1491 +	//
  1.1492 +	if ($config['load_cpf_viewtopic'])
  1.1493 +	{
  1.1494 +		$cp_row = (isset($profile_fields_cache[$poster_id])) ? $cp->generate_profile_fields_template('show', false, $profile_fields_cache[$poster_id]) : array();
  1.1495 +	}
  1.1496 +
  1.1497 +	$post_unread = (isset($topic_tracking_info[$topic_id]) && $row['post_time'] > $topic_tracking_info[$topic_id]) ? true : false;
  1.1498 +
  1.1499 +	$s_first_unread = false;
  1.1500 +	if (!$first_unread && $post_unread)
  1.1501 +	{
  1.1502 +		$s_first_unread = $first_unread = true;
  1.1503 +	}
  1.1504 +
  1.1505 +	$edit_allowed = ($user->data['is_registered'] && ($auth->acl_get('m_edit', $forum_id) || (
  1.1506 +		$user->data['user_id'] == $poster_id &&
  1.1507 +		$auth->acl_get('f_edit', $forum_id) &&
  1.1508 +		!$row['post_edit_locked'] &&
  1.1509 +		($row['post_time'] > time() - ($config['edit_time'] * 60) || !$config['edit_time'])
  1.1510 +	)));
  1.1511 +
  1.1512 +	$delete_allowed = ($user->data['is_registered'] && ($auth->acl_get('m_delete', $forum_id) || (
  1.1513 +		$user->data['user_id'] == $poster_id &&
  1.1514 +		$auth->acl_get('f_delete', $forum_id) &&
  1.1515 +		$topic_data['topic_last_post_id'] == $row['post_id'] &&
  1.1516 +		($row['post_time'] > time() - ($config['delete_time'] * 60) || !$config['delete_time']) &&
  1.1517 +		// we do not want to allow removal of the last post if a moderator locked it!
  1.1518 +		!$row['post_edit_locked']
  1.1519 +	)));
  1.1520 +
  1.1521 +	//
  1.1522 +	$postrow = array(
  1.1523 +		'POST_AUTHOR_FULL'		=> ($poster_id != ANONYMOUS) ? $user_cache[$poster_id]['author_full'] : get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
  1.1524 +		'POST_AUTHOR_COLOUR'	=> ($poster_id != ANONYMOUS) ? $user_cache[$poster_id]['author_colour'] : get_username_string('colour', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
  1.1525 +		'POST_AUTHOR'			=> ($poster_id != ANONYMOUS) ? $user_cache[$poster_id]['author_username'] : get_username_string('username', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
  1.1526 +		'U_POST_AUTHOR'			=> ($poster_id != ANONYMOUS) ? $user_cache[$poster_id]['author_profile'] : get_username_string('profile', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
  1.1527 +
  1.1528 +		'RANK_TITLE'		=> $user_cache[$poster_id]['rank_title'],
  1.1529 +		'RANK_IMG'			=> $user_cache[$poster_id]['rank_image'],
  1.1530 +		'RANK_IMG_SRC'		=> $user_cache[$poster_id]['rank_image_src'],
  1.1531 +		'POSTER_JOINED'		=> $user_cache[$poster_id]['joined'],
  1.1532 +		'POSTER_POSTS'		=> $user_cache[$poster_id]['posts'],
  1.1533 +		'POSTER_FROM'		=> $user_cache[$poster_id]['from'],
  1.1534 +		'POSTER_AVATAR'		=> $user_cache[$poster_id]['avatar'],
  1.1535 +		'POSTER_WARNINGS'	=> $user_cache[$poster_id]['warnings'],
  1.1536 +		'POSTER_AGE'		=> $user_cache[$poster_id]['age'],
  1.1537 +
  1.1538 +		'POST_DATE'			=> $user->format_date($row['post_time'], false, ($view == 'print') ? true : false),
  1.1539 +		'POST_SUBJECT'		=> $row['post_subject'],
  1.1540 +		'MESSAGE'			=> $message,
  1.1541 +		'SIGNATURE'			=> ($row['enable_sig']) ? $user_cache[$poster_id]['sig'] : '',
  1.1542 +		'EDITED_MESSAGE'	=> $l_edited_by,
  1.1543 +		'EDIT_REASON'		=> $row['post_edit_reason'],
  1.1544 +		'BUMPED_MESSAGE'	=> $l_bumped_by,
  1.1545 +
  1.1546 +		'MINI_POST_IMG'			=> ($post_unread) ? $user->img('icon_post_target_unread', 'UNREAD_POST') : $user->img('icon_post_target', 'POST'),
  1.1547 +		'POST_ICON_IMG'			=> ($topic_data['enable_icons'] && !empty($row['icon_id'])) ? $icons[$row['icon_id']]['img'] : '',
  1.1548 +		'POST_ICON_IMG_WIDTH'	=> ($topic_data['enable_icons'] && !empty($row['icon_id'])) ? $icons[$row['icon_id']]['width'] : '',
  1.1549 +		'POST_ICON_IMG_HEIGHT'	=> ($topic_data['enable_icons'] && !empty($row['icon_id'])) ? $icons[$row['icon_id']]['height'] : '',
  1.1550 +		'ICQ_STATUS_IMG'		=> $user_cache[$poster_id]['icq_status_img'],
  1.1551 +		'ONLINE_IMG'			=> ($poster_id == ANONYMOUS || !$config['load_onlinetrack']) ? '' : (($user_cache[$poster_id]['online']) ? $user->img('icon_user_online', 'ONLINE') : $user->img('icon_user_offline', 'OFFLINE')),
  1.1552 +		'S_ONLINE'				=> ($poster_id == ANONYMOUS || !$config['load_onlinetrack']) ? false : (($user_cache[$poster_id]['online']) ? true : false),
  1.1553 +
  1.1554 +		'U_EDIT'			=> ($edit_allowed) ? append_sid("{$phpbb_root_path}posting.$phpEx", "mode=edit&amp;f=$forum_id&amp;p={$row['post_id']}") : '',
  1.1555 +		'U_QUOTE'			=> ($auth->acl_get('f_reply', $forum_id)) ? append_sid("{$phpbb_root_path}posting.$phpEx", "mode=quote&amp;f=$forum_id&amp;p={$row['post_id']}") : '',
  1.1556 +		'U_INFO'			=> ($auth->acl_get('m_info', $forum_id)) ? append_sid("{$phpbb_root_path}mcp.$phpEx", "i=main&amp;mode=post_details&amp;f=$forum_id&amp;p=" . $row['post_id'], true, $user->session_id) : '',
  1.1557 +		'U_DELETE'			=> ($delete_allowed) ? append_sid("{$phpbb_root_path}posting.$phpEx", "mode=delete&amp;f=$forum_id&amp;p={$row['post_id']}") : '',
  1.1558 +
  1.1559 +		'U_PROFILE'		=> $user_cache[$poster_id]['profile'],
  1.1560 +		'U_SEARCH'		=> $user_cache[$poster_id]['search'],
  1.1561 +		'U_PM'			=> ($poster_id != ANONYMOUS && $config['allow_privmsg'] && $auth->acl_get('u_sendpm') && ($user_cache[$poster_id]['allow_pm'] || $auth->acl_gets('a_', 'm_') || $auth->acl_getf_global('m_'))) ? append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&amp;mode=compose&amp;action=quotepost&amp;p=' . $row['post_id']) : '',
  1.1562 +		'U_EMAIL'		=> $user_cache[$poster_id]['email'],
  1.1563 +		'U_WWW'			=> $user_cache[$poster_id]['www'],
  1.1564 +		'U_ICQ'			=> $user_cache[$poster_id]['icq'],
  1.1565 +		'U_AIM'			=> $user_cache[$poster_id]['aim'],
  1.1566 +		'U_MSN'			=> $user_cache[$poster_id]['msn'],
  1.1567 +		'U_YIM'			=> $user_cache[$poster_id]['yim'],
  1.1568 +		'U_JABBER'		=> $user_cache[$poster_id]['jabber'],
  1.1569 +
  1.1570 +		'U_REPORT'			=> ($auth->acl_get('f_report', $forum_id)) ? append_sid("{$phpbb_root_path}report.$phpEx", 'f=' . $forum_id . '&amp;p=' . $row['post_id']) : '',
  1.1571 +		'U_MCP_REPORT'		=> ($auth->acl_get('m_report', $forum_id)) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=reports&amp;mode=report_details&amp;f=' . $forum_id . '&amp;p=' . $row['post_id'], true, $user->session_id) : '',
  1.1572 +		'U_MCP_APPROVE'		=> ($auth->acl_get('m_approve', $forum_id)) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=queue&amp;mode=approve_details&amp;f=' . $forum_id . '&amp;p=' . $row['post_id'], true, $user->session_id) : '',
  1.1573 +		'U_MINI_POST'		=> append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'p=' . $row['post_id']) . (($topic_data['topic_type'] == POST_GLOBAL) ? '&amp;f=' . $forum_id : '') . '#p' . $row['post_id'],
  1.1574 +		'U_NEXT_POST_ID'	=> ($i < $i_total && isset($rowset[$post_list[$i + 1]])) ? $rowset[$post_list[$i + 1]]['post_id'] : '',
  1.1575 +		'U_PREV_POST_ID'	=> $prev_post_id,
  1.1576 +		'U_NOTES'			=> ($auth->acl_getf_global('m_')) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=notes&amp;mode=user_notes&amp;u=' . $poster_id, true, $user->session_id) : '',
  1.1577 +		'U_WARN'			=> ($auth->acl_get('m_warn') && $poster_id != $user->data['user_id'] && $poster_id != ANONYMOUS) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=warn&amp;mode=warn_post&amp;f=' . $forum_id . '&amp;p=' . $row['post_id'], true, $user->session_id) : '',
  1.1578 +
  1.1579 +		'POST_ID'			=> $row['post_id'],
  1.1580 +		'POST_NUMBER'		=> $i + $start + 1,
  1.1581 +		'POSTER_ID'			=> $poster_id,
  1.1582 +
  1.1583 +		'S_HAS_ATTACHMENTS'	=> (!empty($attachments[$row['post_id']])) ? true : false,
  1.1584 +		'S_POST_UNAPPROVED'	=> ($row['post_approved']) ? false : true,
  1.1585 +		'S_POST_REPORTED'	=> ($row['post_reported'] && $auth->acl_get('m_report', $forum_id)) ? true : false,
  1.1586 +		'S_DISPLAY_NOTICE'	=> $display_notice && $row['post_attachment'],
  1.1587 +		'S_FRIEND'			=> ($row['friend']) ? true : false,
  1.1588 +		'S_UNREAD_POST'		=> $post_unread,
  1.1589 +		'S_FIRST_UNREAD'	=> $s_first_unread,
  1.1590 +		'S_CUSTOM_FIELDS'	=> (isset($cp_row['row']) && sizeof($cp_row['row'])) ? true : false,
  1.1591 +		'S_TOPIC_POSTER'	=> ($topic_data['topic_poster'] == $poster_id) ? true : false,
  1.1592 +
  1.1593 +		'S_IGNORE_POST'		=> ($row['hide_post']) ? true : false,
  1.1594 +		'L_IGNORE_POST'		=> ($row['hide_post']) ? sprintf($user->lang['POST_BY_FOE'], get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']), '<a href="' . $viewtopic_url . "&amp;p={$row['post_id']}&amp;view=show#p{$row['post_id']}" . '">', '</a>') : '',
  1.1595 +	);
  1.1596 +
  1.1597 +	if (isset($cp_row['row']) && sizeof($cp_row['row']))
  1.1598 +	{
  1.1599 +		$postrow = array_merge($postrow, $cp_row['row']);
  1.1600 +	}
  1.1601 +
  1.1602 +	// Dump vars into template
  1.1603 +	$template->assign_block_vars('postrow', $postrow);
  1.1604 +
  1.1605 +	if (!empty($cp_row['blockrow']))
  1.1606 +	{
  1.1607 +		foreach ($cp_row['blockrow'] as $field_data)
  1.1608 +		{
  1.1609 +			$template->assign_block_vars('postrow.custom_fields', $field_data);
  1.1610 +		}
  1.1611 +	}
  1.1612 +
  1.1613 +	// Display not already displayed Attachments for this post, we already parsed them. ;)
  1.1614 +	if (!empty($attachments[$row['post_id']]))
  1.1615 +	{
  1.1616 +		foreach ($attachments[$row['post_id']] as $attachment)
  1.1617 +		{
  1.1618 +			$template->assign_block_vars('postrow.attachment', array(
  1.1619 +				'DISPLAY_ATTACHMENT'	=> $attachment)
  1.1620 +			);
  1.1621 +		}
  1.1622 +	}
  1.1623 +
  1.1624 +	$prev_post_id = $row['post_id'];
  1.1625 +
  1.1626 +	unset($rowset[$post_list[$i]]);
  1.1627 +	unset($attachments[$row['post_id']]);
  1.1628 +}
  1.1629 +unset($rowset, $user_cache);
  1.1630 +
  1.1631 +// Update topic view and if necessary attachment view counters ... but only for humans and if this is the first 'page view'
  1.1632 +if (isset($user->data['session_page']) && !$user->data['is_bot'] && (strpos($user->data['session_page'], '&t=' . $topic_id) === false || isset($user->data['session_created'])))
  1.1633 +{
  1.1634 +	$sql = 'UPDATE ' . TOPICS_TABLE . '
  1.1635 +		SET topic_views = topic_views + 1, topic_last_view_time = ' . time() . "
  1.1636 +		WHERE topic_id = $topic_id";
  1.1637 +	$db->sql_query($sql);
  1.1638 +
  1.1639 +	// Update the attachment download counts
  1.1640 +	if (sizeof($update_count))
  1.1641 +	{
  1.1642 +		$sql = 'UPDATE ' . ATTACHMENTS_TABLE . '
  1.1643 +			SET download_count = download_count + 1
  1.1644 +			WHERE ' . $db->sql_in_set('attach_id', array_unique($update_count));
  1.1645 +		$db->sql_query($sql);
  1.1646 +	}
  1.1647 +}
  1.1648 +
  1.1649 +// Get last post time for all global announcements
  1.1650 +// to keep proper forums tracking
  1.1651 +if ($topic_data['topic_type'] == POST_GLOBAL)
  1.1652 +{
  1.1653 +	$sql = 'SELECT topic_last_post_time as forum_last_post_time
  1.1654 +		FROM ' . TOPICS_TABLE . '
  1.1655 +		WHERE forum_id = 0
  1.1656 +		ORDER BY topic_last_post_time DESC';
  1.1657 +	$result = $db->sql_query_limit($sql, 1);
  1.1658 +	$topic_data['forum_last_post_time'] = (int) $db->sql_fetchfield('forum_last_post_time');
  1.1659 +	$db->sql_freeresult($result);
  1.1660 +
  1.1661 +	$sql = 'SELECT mark_time as forum_mark_time
  1.1662 +		FROM ' . FORUMS_TRACK_TABLE . '
  1.1663 +		WHERE forum_id = 0
  1.1664 +			AND user_id = ' . $user->data['user_id'];
  1.1665 +	$result = $db->sql_query($sql);
  1.1666 +	$topic_data['forum_mark_time'] = (int) $db->sql_fetchfield('forum_mark_time');
  1.1667 +	$db->sql_freeresult($result);
  1.1668 +}
  1.1669 +
  1.1670 +// Only mark topic if it's currently unread. Also make sure we do not set topic tracking back if earlier pages are viewed.
  1.1671 +if (isset($topic_tracking_info[$topic_id]) && $topic_data['topic_last_post_time'] > $topic_tracking_info[$topic_id] && $max_post_time > $topic_tracking_info[$topic_id])
  1.1672 +{
  1.1673 +	markread('topic', (($topic_data['topic_type'] == POST_GLOBAL) ? 0 : $forum_id), $topic_id, $max_post_time);
  1.1674 +
  1.1675 +	// Update forum info
  1.1676 +	$all_marked_read = update_forum_tracking_info((($topic_data['topic_type'] == POST_GLOBAL) ? 0 : $forum_id), $topic_data['forum_last_post_time'], (isset($topic_data['forum_mark_time'])) ? $topic_data['forum_mark_time'] : false, false);
  1.1677 +}
  1.1678 +else
  1.1679 +{
  1.1680 +	$all_marked_read = true;
  1.1681 +}
  1.1682 +
  1.1683 +// If there are absolutely no more unread posts in this forum and unread posts shown, we can savely show the #unread link
  1.1684 +if ($all_marked_read)
  1.1685 +{
  1.1686 +	if ($post_unread)
  1.1687 +	{
  1.1688 +		$template->assign_vars(array(
  1.1689 +			'U_VIEW_UNREAD_POST'	=> '#unread',
  1.1690 +		));
  1.1691 +	}
  1.1692 +	else if (isset($topic_tracking_info[$topic_id]) && $topic_data['topic_last_post_time'] > $topic_tracking_info[$topic_id])
  1.1693 +	{
  1.1694 +		$template->assign_vars(array(
  1.1695 +			'U_VIEW_UNREAD_POST'	=> append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;view=unread") . '#unread',
  1.1696 +		));
  1.1697 +	}
  1.1698 +}
  1.1699 +else if (!$all_marked_read)
  1.1700 +{
  1.1701 +	$last_page = ((floor($start / $config['posts_per_page']) + 1) == max(ceil($total_posts / $config['posts_per_page']), 1)) ? true : false;
  1.1702 +
  1.1703 +	// What can happen is that we are at the last displayed page. If so, we also display the #unread link based in $post_unread
  1.1704 +	if ($last_page && $post_unread)
  1.1705 +	{
  1.1706 +		$template->assign_vars(array(
  1.1707 +			'U_VIEW_UNREAD_POST'	=> '#unread',
  1.1708 +		));
  1.1709 +	}
  1.1710 +	else if (!$last_page)
  1.1711 +	{
  1.1712 +		$template->assign_vars(array(
  1.1713 +			'U_VIEW_UNREAD_POST'	=> append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;view=unread") . '#unread',
  1.1714 +		));
  1.1715 +	}
  1.1716 +}
  1.1717 +
  1.1718 +// let's set up quick_reply
  1.1719 +$s_quick_reply = false;
  1.1720 +if ($user->data['is_registered'] && $config['allow_quick_reply'] && ($topic_data['forum_flags'] & FORUM_FLAG_QUICK_REPLY) && $auth->acl_get('f_reply', $forum_id))
  1.1721 +{
  1.1722 +	// Quick reply enabled forum
  1.1723 +	$s_quick_reply = (($topic_data['forum_status'] == ITEM_UNLOCKED && $topic_data['topic_status'] == ITEM_UNLOCKED) || $auth->acl_get('m_edit', $forum_id)) ? true : false;
  1.1724 +}
  1.1725 +
  1.1726 +if ($s_can_vote || $s_quick_reply)
  1.1727 +{
  1.1728 +	add_form_key('posting');
  1.1729 +
  1.1730 +	if ($s_quick_reply)
  1.1731 +	{
  1.1732 +		$s_attach_sig	= $config['allow_sig'] && $user->optionget('attachsig') && $auth->acl_get('f_sigs', $forum_id) && $auth->acl_get('u_sig');
  1.1733 +		$s_smilies		= $config['allow_smilies'] && $user->optionget('smilies') && $auth->acl_get('f_smilies', $forum_id);
  1.1734 +		$s_bbcode		= $config['allow_bbcode'] && $user->optionget('bbcode') && $auth->acl_get('f_bbcode', $forum_id);
  1.1735 +		$s_notify		= $config['allow_topic_notify'] && ($user->data['user_notify'] || $s_watching_topic['is_watching']);
  1.1736 +
  1.1737 +		$qr_hidden_fields = array(
  1.1738 +			'topic_cur_post_id'		=> (int) $topic_data['topic_last_post_id'],
  1.1739 +			'lastclick'				=> (int) time(),
  1.1740 +			'topic_id'				=> (int) $topic_data['topic_id'],
  1.1741 +			'forum_id'				=> (int) $forum_id,
  1.1742 +		);
  1.1743 +
  1.1744 +		// Originally we use checkboxes and check with isset(), so we only provide them if they would be checked
  1.1745 +		(!$s_bbcode)					? $qr_hidden_fields['disable_bbcode'] = 1		: true;
  1.1746 +		(!$s_smilies)					? $qr_hidden_fields['disable_smilies'] = 1		: true;
  1.1747 +		(!$config['allow_post_links'])	? $qr_hidden_fields['disable_magic_url'] = 1	: true;
  1.1748 +		($s_attach_sig)					? $qr_hidden_fields['attach_sig'] = 1			: true;
  1.1749 +		($s_notify)						? $qr_hidden_fields['notify'] = 1				: true;
  1.1750 +		($topic_data['topic_status'] == ITEM_LOCKED) ? $qr_hidden_fields['lock_topic'] = 1 : true;
  1.1751 +
  1.1752 +		$template->assign_vars(array(
  1.1753 +			'S_QUICK_REPLY'			=> true,
  1.1754 +			'U_QR_ACTION'			=> append_sid("{$phpbb_root_path}posting.$phpEx", "mode=reply&amp;f=$forum_id&amp;t=$topic_id"),
  1.1755 +			'QR_HIDDEN_FIELDS'		=> build_hidden_fields($qr_hidden_fields),
  1.1756 +			'SUBJECT'				=> 'Re: ' . censor_text($topic_data['topic_title']),
  1.1757 +		));
  1.1758 +	}
  1.1759 +}
  1.1760 +// now I have the urge to wash my hands :(
  1.1761 +
  1.1762 +
  1.1763 +// We overwrite $_REQUEST['f'] if there is no forum specified
  1.1764 +// to be able to display the correct online list.
  1.1765 +// One downside is that the user currently viewing this topic/post is not taken into account.
  1.1766 +if (empty($_REQUEST['f']))
  1.1767 +{
  1.1768 +	$_REQUEST['f'] = $forum_id;
  1.1769 +}
  1.1770 +
  1.1771 +// We need to do the same with the topic_id. See #53025.
  1.1772 +if (empty($_REQUEST['t']) && !empty($topic_id))
  1.1773 +{
  1.1774 +	$_REQUEST['t'] = $topic_id;
  1.1775 +}
  1.1776 +
  1.1777 +// Output the page
  1.1778 +page_header($user->lang['VIEW_TOPIC'] . ' - ' . $topic_data['topic_title'], true, $forum_id);
  1.1779 +
  1.1780 +$template->set_filenames(array(
  1.1781 +	'body' => ($view == 'print') ? 'viewtopic_print.html' : 'viewtopic_body.html')
  1.1782 +);
  1.1783 +make_jumpbox(append_sid("{$phpbb_root_path}viewforum.$phpEx"), $forum_id);
  1.1784 +
  1.1785 +page_footer();
  1.1786 +
  1.1787 +?>
  1.1788 \ No newline at end of file