~azzar1/unity/add-show-desktop-key

443 by dcoles
Added Forum application along with unmodifed version of phpBB3 "Olympus" 3.0.0
1
<?php
2
/**
3
*
4
* @package phpBB3
5
* @version $Id: viewtopic.php,v 1.513 2007/11/06 00:05:53 kellanved Exp $
6
* @copyright (c) 2005 phpBB Group
7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
8
*
9
*/
10
11
/**
12
* @ignore
13
*/
14
define('IN_PHPBB', true);
15
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
16
$phpEx = substr(strrchr(__FILE__, '.'), 1);
17
include($phpbb_root_path . 'common.' . $phpEx);
18
include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
19
include($phpbb_root_path . 'includes/bbcode.' . $phpEx);
20
21
// Start session management
22
$user->session_begin();
23
$auth->acl($user->data);
24
25
// Initial var setup
26
$forum_id	= request_var('f', 0);
27
$topic_id	= request_var('t', 0);
28
$post_id	= request_var('p', 0);
29
$voted_id	= request_var('vote_id', array('' => 0));
30
31
$start		= request_var('start', 0);
32
$view		= request_var('view', '');
33
34
$sort_days	= request_var('st', ((!empty($user->data['user_post_show_days'])) ? $user->data['user_post_show_days'] : 0));
35
$sort_key	= request_var('sk', ((!empty($user->data['user_post_sortby_type'])) ? $user->data['user_post_sortby_type'] : 't'));
36
$sort_dir	= request_var('sd', ((!empty($user->data['user_post_sortby_dir'])) ? $user->data['user_post_sortby_dir'] : 'a'));
37
38
$update		= request_var('update', false);
39
40
/**
41
* @todo normalize?
42
*/
43
$hilit_words	= request_var('hilit', '', true);
44
45
// Do we have a topic or post id?
46
if (!$topic_id && !$post_id)
47
{
48
	trigger_error('NO_TOPIC');
49
}
50
51
// Find topic id if user requested a newer or older topic
52
if ($view && !$post_id)
53
{
54
	if (!$forum_id)
55
	{
56
		$sql = 'SELECT forum_id
57
			FROM ' . TOPICS_TABLE . "
58
			WHERE topic_id = $topic_id";
59
		$result = $db->sql_query($sql);
60
		$forum_id = (int) $db->sql_fetchfield('forum_id');
61
		$db->sql_freeresult($result);
62
63
		if (!$forum_id)
64
		{
65
			trigger_error('NO_TOPIC');
66
		}
67
	}
68
69
	if ($view == 'unread')
70
	{
71
		// Get topic tracking info
72
		$topic_tracking_info = get_complete_topic_tracking($forum_id, $topic_id);
73
74
		$topic_last_read = (isset($topic_tracking_info[$topic_id])) ? $topic_tracking_info[$topic_id] : 0;
75
76
		$sql = 'SELECT post_id, topic_id, forum_id
77
			FROM ' . POSTS_TABLE . "
78
			WHERE topic_id = $topic_id
79
				" . (($auth->acl_get('m_approve', $forum_id)) ? '' : 'AND post_approved = 1') . "
80
				AND post_time > $topic_last_read
81
			ORDER BY post_time ASC";
82
		$result = $db->sql_query_limit($sql, 1);
83
		$row = $db->sql_fetchrow($result);
84
		$db->sql_freeresult($result);
85
86
		if (!$row)
87
		{
88
			$sql = 'SELECT topic_last_post_id as post_id, topic_id, forum_id
89
				FROM ' . TOPICS_TABLE . '
90
				WHERE topic_id = ' . $topic_id;
91
			$result = $db->sql_query($sql);
92
			$row = $db->sql_fetchrow($result);
93
			$db->sql_freeresult($result);
94
		}
95
96
		if (!$row)
97
		{
98
			// Setup user environment so we can process lang string
99
			$user->setup('viewtopic');
100
101
			trigger_error('NO_TOPIC');
102
		}
103
104
		$post_id = $row['post_id'];
105
		$topic_id = $row['topic_id'];
106
	}
107
	else if ($view == 'next' || $view == 'previous')
108
	{
109
		$sql_condition = ($view == 'next') ? '>' : '<';
110
		$sql_ordering = ($view == 'next') ? 'ASC' : 'DESC';
111
112
		$sql = 'SELECT forum_id, topic_last_post_time
113
			FROM ' . TOPICS_TABLE . '
114
			WHERE topic_id = ' . $topic_id;
115
		$result = $db->sql_query($sql);
116
		$row = $db->sql_fetchrow($result);
117
		$db->sql_freeresult($result);
118
119
		if (!$row)
120
		{
121
			$user->setup('viewtopic');
122
			// OK, the topic doesn't exist. This error message is not helpful, but technically correct.
123
			trigger_error(($view == 'next') ? 'NO_NEWER_TOPICS' : 'NO_OLDER_TOPICS');
124
		}
125
		else
126
		{
127
			$sql = 'SELECT topic_id, forum_id
128
				FROM ' . TOPICS_TABLE . '
129
				WHERE forum_id = ' . $row['forum_id'] . "
130
					AND topic_moved_id = 0
131
					AND topic_last_post_time $sql_condition {$row['topic_last_post_time']}
132
					" . (($auth->acl_get('m_approve', $row['forum_id'])) ? '' : 'AND topic_approved = 1') . "
133
				ORDER BY topic_last_post_time $sql_ordering";
134
			$result = $db->sql_query_limit($sql, 1);
135
			$row = $db->sql_fetchrow($result);
136
			$db->sql_freeresult($result);
137
138
			if (!$row)
139
			{
140
				$user->setup('viewtopic');
141
				trigger_error(($view == 'next') ? 'NO_NEWER_TOPICS' : 'NO_OLDER_TOPICS');
142
			}
143
			else
144
			{
145
				$topic_id = $row['topic_id'];
146
147
				// Check for global announcement correctness?
148
				if (!$row['forum_id'] && !$forum_id)
149
				{
150
					trigger_error('NO_TOPIC');
151
				}
152
				else if ($row['forum_id'])
153
				{
154
					$forum_id = $row['forum_id'];
155
				}
156
			}
157
		}
158
	}
159
160
	// Check for global announcement correctness?
161
	if ((!isset($row) || !$row['forum_id']) && !$forum_id)
162
	{
163
		trigger_error('NO_TOPIC');
164
	}
165
	else if (isset($row) && $row['forum_id'])
166
	{
167
		$forum_id = $row['forum_id'];
168
	}
169
}
170
171
// This rather complex gaggle of code handles querying for topics but
172
// also allows for direct linking to a post (and the calculation of which
173
// page the post is on and the correct display of viewtopic)
174
$sql_array = array(
175
	'SELECT'	=> 't.*, f.*',
176
177
	'FROM'		=> array(
178
		FORUMS_TABLE	=> 'f',
179
	)
180
);
181
182
if ($user->data['is_registered'])
183
{
184
	$sql_array['SELECT'] .= ', tw.notify_status';
185
	$sql_array['LEFT_JOIN'] = array();
186
187
	$sql_array['LEFT_JOIN'][] = array(
188
		'FROM'	=> array(TOPICS_WATCH_TABLE => 'tw'),
189
		'ON'	=> 'tw.user_id = ' . $user->data['user_id'] . ' AND t.topic_id = tw.topic_id'
190
	);
191
192
	if ($config['allow_bookmarks'])
193
	{
194
		$sql_array['SELECT'] .= ', bm.topic_id as bookmarked';
195
		$sql_array['LEFT_JOIN'][] = array(
196
			'FROM'	=> array(BOOKMARKS_TABLE => 'bm'),
197
			'ON'	=> 'bm.user_id = ' . $user->data['user_id'] . ' AND t.topic_id = bm.topic_id'
198
		);
199
	}
200
201
	if ($config['load_db_lastread'])
202
	{
203
		$sql_array['SELECT'] .= ', tt.mark_time, ft.mark_time as forum_mark_time';
204
205
		$sql_array['LEFT_JOIN'][] = array(
206
			'FROM'	=> array(TOPICS_TRACK_TABLE => 'tt'),
207
			'ON'	=> 'tt.user_id = ' . $user->data['user_id'] . ' AND t.topic_id = tt.topic_id'
208
		);
209
210
		$sql_array['LEFT_JOIN'][] = array(
211
			'FROM'	=> array(FORUMS_TRACK_TABLE => 'ft'),
212
			'ON'	=> 'ft.user_id = ' . $user->data['user_id'] . ' AND t.forum_id = ft.forum_id'
213
		);
214
	}
215
}
216
217
if (!$post_id)
218
{
219
	$sql_array['WHERE'] = "t.topic_id = $topic_id";
220
}
221
else
222
{
223
	$sql_array['WHERE'] = "p.post_id = $post_id AND t.topic_id = p.topic_id" . ((!$auth->acl_get('m_approve', $forum_id)) ? ' AND p.post_approved = 1' : '');
224
	$sql_array['FROM'][POSTS_TABLE] = 'p';
225
}
226
227
$sql_array['WHERE'] .= ' AND (f.forum_id = t.forum_id';
228
229
if (!$forum_id)
230
{
231
	// If it is a global announcement make sure to set the forum id to a postable forum
232
	$sql_array['WHERE'] .= ' OR (t.topic_type = ' . POST_GLOBAL . '
233
		AND f.forum_type = ' . FORUM_POST . ')';
234
}
235
else
236
{
237
	$sql_array['WHERE'] .= ' OR (t.topic_type = ' . POST_GLOBAL . "
238
		AND f.forum_id = $forum_id)";
239
}
240
241
$sql_array['WHERE'] .= ')';
242
$sql_array['FROM'][TOPICS_TABLE] = 't';
243
244
// Join to forum table on topic forum_id unless topic forum_id is zero
245
// whereupon we join on the forum_id passed as a parameter ... this
246
// is done so navigation, forum name, etc. remain consistent with where
247
// user clicked to view a global topic
248
$sql = $db->sql_build_query('SELECT', $sql_array);
249
$result = $db->sql_query($sql);
250
$topic_data = $db->sql_fetchrow($result);
251
$db->sql_freeresult($result);
252
253
if (!$topic_data)
254
{
255
	// If post_id was submitted, we try at least to display the topic as a last resort...
256
	if ($post_id && $forum_id && $topic_id)
257
	{
258
		redirect(append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id"));
259
	}
260
261
	trigger_error('NO_TOPIC');
262
}
263
264
// This is for determining where we are (page)
265
if ($post_id)
266
{
267
	if ($post_id == $topic_data['topic_first_post_id'] || $post_id == $topic_data['topic_last_post_id'])
268
	{
269
		$check_sort = ($post_id == $topic_data['topic_first_post_id']) ? 'd' : 'a';
270
271
		if ($sort_dir == $check_sort)
272
		{
273
			$topic_data['prev_posts'] = ($auth->acl_get('m_approve', $forum_id)) ? $topic_data['topic_replies_real'] : $topic_data['topic_replies'];
274
		}
275
		else
276
		{
277
			$topic_data['prev_posts'] = 0;
278
		}
279
	}
280
	else
281
	{
282
		$sql = 'SELECT COUNT(p1.post_id) AS prev_posts
283
			FROM ' . POSTS_TABLE . ' p1, ' . POSTS_TABLE . " p2
284
			WHERE p1.topic_id = {$topic_data['topic_id']}
285
				AND p2.post_id = {$post_id}
286
				" . ((!$auth->acl_get('m_approve', $forum_id)) ? 'AND p1.post_approved = 1' : '') . '
287
				AND ' . (($sort_dir == 'd') ? 'p1.post_time >= p2.post_time' : 'p1.post_time <= p2.post_time');
288
289
		$result = $db->sql_query($sql);
290
		$row = $db->sql_fetchrow($result);
291
		$db->sql_freeresult($result);
292
293
		$topic_data['prev_posts'] = $row['prev_posts'] - 1;
294
	}
295
}
296
297
$forum_id = (int) $topic_data['forum_id'];
298
$topic_id = (int) $topic_data['topic_id'];
299
300
//
301
$topic_replies = ($auth->acl_get('m_approve', $forum_id)) ? $topic_data['topic_replies_real'] : $topic_data['topic_replies'];
302
303
// Check sticky/announcement time limit
304
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())
305
{
306
	$sql = 'UPDATE ' . TOPICS_TABLE . '
307
		SET topic_type = ' . POST_NORMAL . ', topic_time_limit = 0
308
		WHERE topic_id = ' . $topic_id;
309
	$db->sql_query($sql);
310
311
	$topic_data['topic_type'] = POST_NORMAL;
312
	$topic_data['topic_time_limit'] = 0;
313
}
314
315
// Setup look and feel
316
$user->setup('viewtopic', $topic_data['forum_style']);
317
318
if (!$topic_data['topic_approved'] && !$auth->acl_get('m_approve', $forum_id))
319
{
320
	trigger_error('NO_TOPIC');
321
}
322
323
// Start auth check
324
if (!$auth->acl_get('f_read', $forum_id))
325
{
326
	if ($user->data['user_id'] != ANONYMOUS)
327
	{
328
		trigger_error('SORRY_AUTH_READ');
329
	}
330
331
	login_box('', $user->lang['LOGIN_VIEWFORUM']);
332
}
333
334
// Forum is passworded ... check whether access has been granted to this
335
// user this session, if not show login box
336
if ($topic_data['forum_password'])
337
{
338
	login_forum_box($topic_data);
339
}
340
341
// Redirect to login or to the correct post upon emailed notification links
342
if (isset($_GET['e']))
343
{
344
	$jump_to = request_var('e', 0);
345
346
	$redirect_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id");
347
348
	if ($user->data['user_id'] == ANONYMOUS)
349
	{
350
		login_box($redirect_url . "&amp;p=$post_id&amp;e=$jump_to", $user->lang['LOGIN_NOTIFY_TOPIC']);
351
	}
352
353
	if ($jump_to > 0)
354
	{
355
		// We direct the already logged in user to the correct post...
356
		redirect($redirect_url . ((!$post_id) ? "&amp;p=$jump_to" : "&amp;p=$post_id") . "#p$jump_to");
357
	}
358
}
359
360
// What is start equal to?
361
if ($post_id)
362
{
363
	$start = floor(($topic_data['prev_posts']) / $config['posts_per_page']) * $config['posts_per_page'];
364
}
365
366
// Get topic tracking info
367
if (!isset($topic_tracking_info))
368
{
369
	$topic_tracking_info = array();
370
371
	// Get topic tracking info
372
	if ($config['load_db_lastread'] && $user->data['is_registered'])
373
	{
374
		$tmp_topic_data = array($topic_id => $topic_data);
375
		$topic_tracking_info = get_topic_tracking($forum_id, $topic_id, $tmp_topic_data, array($forum_id => $topic_data['forum_mark_time']));
376
		unset($tmp_topic_data);
377
	}
378
	else if ($config['load_anon_lastread'] || $user->data['is_registered'])
379
	{
380
		$topic_tracking_info = get_complete_topic_tracking($forum_id, $topic_id);
381
	}
382
}
383
384
// Post ordering options
385
$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']);
386
387
$sort_by_text = array('a' => $user->lang['AUTHOR'], 't' => $user->lang['POST_TIME'], 's' => $user->lang['SUBJECT']);
388
$sort_by_sql = array('a' => 'u.username_clean', 't' => 'p.post_time', 's' => 'p.post_subject');
389
390
$s_limit_days = $s_sort_key = $s_sort_dir = $u_sort_param = '';
391
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);
392
393
// Obtain correct post count and ordering SQL if user has
394
// requested anything different
395
if ($sort_days)
396
{
397
	$min_post_time = time() - ($sort_days * 86400);
398
399
	$sql = 'SELECT COUNT(post_id) AS num_posts
400
		FROM ' . POSTS_TABLE . "
401
		WHERE topic_id = $topic_id
402
			AND post_time >= $min_post_time
403
		" . (($auth->acl_get('m_approve', $forum_id)) ? '' : 'AND post_approved = 1');
404
	$result = $db->sql_query($sql);
405
	$total_posts = (int) $db->sql_fetchfield('num_posts');
406
	$db->sql_freeresult($result);
407
408
	$limit_posts_time = "AND p.post_time >= $min_post_time ";
409
410
	if (isset($_POST['sort']))
411
	{
412
		$start = 0;
413
	}
414
}
415
else
416
{
417
	$total_posts = $topic_replies + 1;
418
	$limit_posts_time = '';
419
}
420
421
// Was a highlight request part of the URI?
422
$highlight_match = $highlight = '';
423
if ($hilit_words)
424
{
425
	foreach (explode(' ', trim($hilit_words)) as $word)
426
	{
427
		if (trim($word))
428
		{
429
			$word = str_replace('\*', '\w+?', preg_quote($word, '#'));
430
			$word = preg_replace('#(^|\s)\\\\w\*\?(\s|$)#', '$1\w+?$2', $word);
431
			$highlight_match .= (($highlight_match != '') ? '|' : '') . $word;
432
		}
433
	}
434
435
	$highlight = urlencode($hilit_words);
436
}
437
438
// Make sure $start is set to the last page if it exceeds the amount
439
if ($start < 0 || $start > $total_posts)
440
{
441
	$start = ($start < 0) ? 0 : floor(($total_posts - 1) / $config['posts_per_page']) * $config['posts_per_page'];
442
}
443
444
// General Viewtopic URL for return links
445
$viewtopic_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;start=$start&amp;$u_sort_param" . (($highlight_match) ? "&amp;hilit=$highlight" : ''));
446
447
// Are we watching this topic?
448
$s_watching_topic = $s_watching_topic_img = array();
449
$s_watching_topic['link'] = $s_watching_topic['title'] = '';
450
$s_watching_topic['is_watching'] = false;
451
452
if ($config['email_enable'] && $config['allow_topic_notify'] && $user->data['is_registered'])
453
{
454
	watch_topic_forum('topic', $s_watching_topic, $s_watching_topic_img, $user->data['user_id'], $forum_id, $topic_id, $topic_data['notify_status'], $start);
455
}
456
457
// Bookmarks
458
if ($config['allow_bookmarks'] && $user->data['is_registered'] && request_var('bookmark', 0))
459
{
460
	if (!$topic_data['bookmarked'])
461
	{
462
		$sql = 'INSERT INTO ' . BOOKMARKS_TABLE . ' ' . $db->sql_build_array('INSERT', array(
463
			'user_id'	=> $user->data['user_id'],
464
			'topic_id'	=> $topic_id,
465
		));
466
		$db->sql_query($sql);
467
	}
468
	else
469
	{
470
		$sql = 'DELETE FROM ' . BOOKMARKS_TABLE . "
471
			WHERE user_id = {$user->data['user_id']}
472
				AND topic_id = $topic_id";
473
		$db->sql_query($sql);
474
	}
475
476
	meta_refresh(3, $viewtopic_url);
477
478
	$message = (($topic_data['bookmarked']) ? $user->lang['BOOKMARK_REMOVED'] : $user->lang['BOOKMARK_ADDED']) . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $viewtopic_url . '">', '</a>');
479
	trigger_error($message);
480
}
481
482
// Grab ranks
483
$ranks = $cache->obtain_ranks();
484
485
// Grab icons
486
$icons = $cache->obtain_icons();
487
488
// Grab extensions
489
$extensions = array();
490
if ($topic_data['topic_attachment'])
491
{
492
	$extensions = $cache->obtain_attach_extensions($forum_id);
493
}
494
495
// Forum rules listing
496
$s_forum_rules = '';
497
gen_forum_auth_level('topic', $forum_id, $topic_data['forum_status']);
498
499
// Quick mod tools
500
$allow_change_type = ($auth->acl_get('m_', $forum_id) || ($user->data['is_registered'] && $user->data['user_id'] == $topic_data['topic_poster'])) ? true : false;
501
502
$topic_mod = '';
503
$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>') : '';
504
$topic_mod .= ($auth->acl_get('m_delete', $forum_id)) ? '<option value="delete_topic">' . $user->lang['DELETE_TOPIC'] . '</option>' : '';
505
$topic_mod .= ($auth->acl_get('m_move', $forum_id) && $topic_data['topic_status'] != ITEM_MOVED) ? '<option value="move">' . $user->lang['MOVE_TOPIC'] . '</option>' : '';
506
$topic_mod .= ($auth->acl_get('m_split', $forum_id)) ? '<option value="split">' . $user->lang['SPLIT_TOPIC'] . '</option>' : '';
507
$topic_mod .= ($auth->acl_get('m_merge', $forum_id)) ? '<option value="merge">' . $user->lang['MERGE_POSTS'] . '</option>' : '';
508
$topic_mod .= ($auth->acl_get('m_merge', $forum_id)) ? '<option value="merge_topic">' . $user->lang['MERGE_TOPIC'] . '</option>' : '';
509
$topic_mod .= ($auth->acl_get('m_move', $forum_id)) ? '<option value="fork">' . $user->lang['FORK_TOPIC'] . '</option>' : '';
510
$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>' : '';
511
$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>' : '';
512
$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>' : '';
513
$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>' : '';
514
$topic_mod .= ($auth->acl_get('m_', $forum_id)) ? '<option value="topic_logs">' . $user->lang['VIEW_TOPIC_LOGS'] . '</option>' : '';
515
516
// If we've got a hightlight set pass it on to pagination.
517
$pagination = generate_pagination(append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;$u_sort_param" . (($highlight_match) ? "&amp;hilit=$highlight" : '')), $total_posts, $config['posts_per_page'], $start);
518
519
// Navigation links
520
generate_forum_nav($topic_data);
521
522
// Forum Rules
523
generate_forum_rules($topic_data);
524
525
// Moderators
526
$forum_moderators = array();
527
get_moderators($forum_moderators, $forum_id);
528
529
// This is only used for print view so ...
530
$server_path = (!$view) ? $phpbb_root_path : generate_board_url() . '/';
531
532
// Replace naughty words in title
533
$topic_data['topic_title'] = censor_text($topic_data['topic_title']);
534
535
// Send vars to template
536
$template->assign_vars(array(
537
	'FORUM_ID' 		=> $forum_id,
538
	'FORUM_NAME' 	=> $topic_data['forum_name'],
539
	'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']),
540
	'TOPIC_ID' 		=> $topic_id,
541
	'TOPIC_TITLE' 	=> $topic_data['topic_title'],
542
	'TOPIC_POSTER'	=> $topic_data['topic_poster'],
543
544
	'TOPIC_AUTHOR_FULL'		=> get_username_string('full', $topic_data['topic_poster'], $topic_data['topic_first_poster_name'], $topic_data['topic_first_poster_colour']),
545
	'TOPIC_AUTHOR_COLOUR'	=> get_username_string('colour', $topic_data['topic_poster'], $topic_data['topic_first_poster_name'], $topic_data['topic_first_poster_colour']),
546
	'TOPIC_AUTHOR'			=> get_username_string('username', $topic_data['topic_poster'], $topic_data['topic_first_poster_name'], $topic_data['topic_first_poster_colour']),
547
548
	'PAGINATION' 	=> $pagination,
549
	'PAGE_NUMBER' 	=> on_page($total_posts, $config['posts_per_page'], $start),
550
	'TOTAL_POSTS'	=> ($total_posts == 1) ? $user->lang['VIEW_TOPIC_POST'] : sprintf($user->lang['VIEW_TOPIC_POSTS'], $total_posts),
551
	'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&amp;start=$start&amp;$u_sort_param", true, $user->session_id) : '',
552
	'MODERATORS'	=> (isset($forum_moderators[$forum_id]) && sizeof($forum_moderators[$forum_id])) ? implode(', ', $forum_moderators[$forum_id]) : '',
553
554
	'POST_IMG' 			=> ($topic_data['forum_status'] == ITEM_LOCKED) ? $user->img('button_topic_locked', 'FORUM_LOCKED') : $user->img('button_topic_new', 'POST_NEW_TOPIC'),
555
	'QUOTE_IMG' 		=> $user->img('icon_post_quote', 'REPLY_WITH_QUOTE'),
556
	'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'),
557
	'EDIT_IMG' 			=> $user->img('icon_post_edit', 'EDIT_POST'),
558
	'DELETE_IMG' 		=> $user->img('icon_post_delete', 'DELETE_POST'),
559
	'INFO_IMG' 			=> $user->img('icon_post_info', 'VIEW_INFO'),
560
	'PROFILE_IMG'		=> $user->img('icon_user_profile', 'READ_PROFILE'),
561
	'SEARCH_IMG' 		=> $user->img('icon_user_search', 'SEARCH_USER_POSTS'),
562
	'PM_IMG' 			=> $user->img('icon_contact_pm', 'SEND_PRIVATE_MESSAGE'),
563
	'EMAIL_IMG' 		=> $user->img('icon_contact_email', 'SEND_EMAIL'),
564
	'WWW_IMG' 			=> $user->img('icon_contact_www', 'VISIT_WEBSITE'),
565
	'ICQ_IMG' 			=> $user->img('icon_contact_icq', 'ICQ'),
566
	'AIM_IMG' 			=> $user->img('icon_contact_aim', 'AIM'),
567
	'MSN_IMG' 			=> $user->img('icon_contact_msnm', 'MSNM'),
568
	'YIM_IMG' 			=> $user->img('icon_contact_yahoo', 'YIM'),
569
	'JABBER_IMG'		=> $user->img('icon_contact_jabber', 'JABBER') ,
570
	'REPORT_IMG'		=> $user->img('icon_post_report', 'REPORT_POST'),
571
	'REPORTED_IMG'		=> $user->img('icon_topic_reported', 'POST_REPORTED'),
572
	'UNAPPROVED_IMG'	=> $user->img('icon_topic_unapproved', 'POST_UNAPPROVED'),
573
	'WARN_IMG'			=> $user->img('icon_user_warn', 'WARN_USER'),
574
575
	'S_IS_LOCKED'			=>($topic_data['topic_status'] == ITEM_UNLOCKED) ? false : true,
576
	'S_SELECT_SORT_DIR' 	=> $s_sort_dir,
577
	'S_SELECT_SORT_KEY' 	=> $s_sort_key,
578
	'S_SELECT_SORT_DAYS' 	=> $s_limit_days,
579
	'S_SINGLE_MODERATOR'	=> (!empty($forum_moderators[$forum_id]) && sizeof($forum_moderators[$forum_id]) > 1) ? false : true,
580
	'S_TOPIC_ACTION' 		=> append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;start=$start"),
581
	'S_TOPIC_MOD' 			=> ($topic_mod != '') ? '<select name="action">' . $topic_mod . '</select>' : '',
582
	'S_MOD_ACTION' 			=> append_sid("{$phpbb_root_path}mcp.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;quickmod=1&amp;redirect=" . urlencode(str_replace('&amp;', '&', $viewtopic_url)), true, $user->session_id),
583
584
	'S_VIEWTOPIC'			=> true,
585
	'S_DISPLAY_SEARCHBOX'	=> ($auth->acl_get('u_search') && $auth->acl_get('f_search', $forum_id) && $config['load_search']) ? true : false,
586
	'S_SEARCHBOX_ACTION'	=> append_sid("{$phpbb_root_path}search.$phpEx", 't=' . $topic_id),
587
588
	'S_DISPLAY_POST_INFO'	=> ($topic_data['forum_type'] == FORUM_POST && ($auth->acl_get('f_post', $forum_id) || $user->data['user_id'] == ANONYMOUS)) ? true : false,
589
	'S_DISPLAY_REPLY_INFO'	=> ($topic_data['forum_type'] == FORUM_POST && ($auth->acl_get('f_reply', $forum_id) || $user->data['user_id'] == ANONYMOUS)) ? true : false,
590
591
	'U_TOPIC'				=> "{$server_path}viewtopic.$phpEx?f=$forum_id&amp;t=$topic_id",
592
	'U_FORUM'				=> $server_path,
593
	'U_VIEW_TOPIC' 			=> $viewtopic_url,
594
	'U_VIEW_FORUM' 			=> append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_id),
595
	'U_VIEW_OLDER_TOPIC'	=> append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;view=previous"),
596
	'U_VIEW_NEWER_TOPIC'	=> append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;view=next"),
597
	'U_PRINT_TOPIC'			=> ($auth->acl_get('f_print', $forum_id)) ? $viewtopic_url . '&amp;view=print' : '',
598
	'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") : '',
599
600
	'U_WATCH_TOPIC' 		=> $s_watching_topic['link'],
601
	'L_WATCH_TOPIC' 		=> $s_watching_topic['title'],
602
	'S_WATCHING_TOPIC'		=> $s_watching_topic['is_watching'],
603
604
	'U_BOOKMARK_TOPIC'		=> ($user->data['is_registered'] && $config['allow_bookmarks']) ? $viewtopic_url . '&amp;bookmark=1' : '',
605
	'L_BOOKMARK_TOPIC'		=> ($user->data['is_registered'] && $config['allow_bookmarks'] && $topic_data['bookmarked']) ? $user->lang['BOOKMARK_TOPIC_REMOVE'] : $user->lang['BOOKMARK_TOPIC'],
606
607
	'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") : '',
608
	'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") : '',
609
	'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") : '')
610
);
611
612
// Does this topic contain a poll?
613
if (!empty($topic_data['poll_start']))
614
{
615
	$sql = 'SELECT o.*, p.bbcode_bitfield, p.bbcode_uid
616
		FROM ' . POLL_OPTIONS_TABLE . ' o, ' . POSTS_TABLE . " p
617
		WHERE o.topic_id = $topic_id
618
			AND p.post_id = {$topic_data['topic_first_post_id']}
619
			AND p.topic_id = o.topic_id
620
		ORDER BY o.poll_option_id";
621
	$result = $db->sql_query($sql);
622
623
	$poll_info = array();
624
	while ($row = $db->sql_fetchrow($result))
625
	{
626
		$poll_info[] = $row;
627
	}
628
	$db->sql_freeresult($result);
629
630
	$cur_voted_id = array();
631
	if ($user->data['is_registered'])
632
	{
633
		$sql = 'SELECT poll_option_id
634
			FROM ' . POLL_VOTES_TABLE . '
635
			WHERE topic_id = ' . $topic_id . '
636
				AND vote_user_id = ' . $user->data['user_id'];
637
		$result = $db->sql_query($sql);
638
639
		while ($row = $db->sql_fetchrow($result))
640
		{
641
			$cur_voted_id[] = $row['poll_option_id'];
642
		}
643
		$db->sql_freeresult($result);
644
	}
645
	else
646
	{
647
		// Cookie based guest tracking ... I don't like this but hum ho
648
		// it's oft requested. This relies on "nice" users who don't feel
649
		// the need to delete cookies to mess with results.
650
		if (isset($_COOKIE[$config['cookie_name'] . '_poll_' . $topic_id]))
651
		{
652
			$cur_voted_id = explode(',', $_COOKIE[$config['cookie_name'] . '_poll_' . $topic_id]);
653
			$cur_voted_id = array_map('intval', $cur_voted_id);
654
		}
655
	}
656
657
	$s_can_vote = (((!sizeof($cur_voted_id) && $auth->acl_get('f_vote', $forum_id)) ||
658
		($auth->acl_get('f_votechg', $forum_id) && $topic_data['poll_vote_change'])) &&
659
		(($topic_data['poll_length'] != 0 && $topic_data['poll_start'] + $topic_data['poll_length'] > time()) || $topic_data['poll_length'] == 0) &&
660
		$topic_data['topic_status'] != ITEM_LOCKED &&
661
		$topic_data['forum_status'] != ITEM_LOCKED) ? true : false;
662
	$s_display_results = (!$s_can_vote || ($s_can_vote && sizeof($cur_voted_id)) || $view == 'viewpoll') ? true : false;
663
664
	if ($update && $s_can_vote)
665
	{
666
		
667
		if (!sizeof($voted_id) || sizeof($voted_id) > $topic_data['poll_max_options'] || in_array(VOTE_CONVERTED, $cur_voted_id))
668
		{
669
			$redirect_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;start=$start");
670
671
			meta_refresh(5, $redirect_url);
672
			if (!sizeof($voted_id))
673
			{
674
				$message = 'NO_VOTE_OPTION';
675
			}
676
			else if (sizeof($voted_id) > $topic_data['poll_max_options'])
677
			{
678
				$message = 'TOO_MANY_VOTE_OPTIONS';
679
			}
680
			else
681
			{
682
				$message = 'VOTE_CONVERTED';
683
			}
684
 
685
			$message = $user->lang[$message] . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $redirect_url . '">', '</a>');
686
			trigger_error($message);
687
		}
688
689
		foreach ($voted_id as $option)
690
		{
691
			if (in_array($option, $cur_voted_id))
692
			{
693
				continue;
694
			}
695
696
			$sql = 'UPDATE ' . POLL_OPTIONS_TABLE . '
697
				SET poll_option_total = poll_option_total + 1
698
				WHERE poll_option_id = ' . (int) $option . '
699
					AND topic_id = ' . (int) $topic_id;
700
			$db->sql_query($sql);
701
702
			if ($user->data['is_registered'])
703
			{
704
				$sql_ary = array(
705
					'topic_id'			=> (int) $topic_id,
706
					'poll_option_id'	=> (int) $option,
707
					'vote_user_id'		=> (int) $user->data['user_id'],
708
					'vote_user_ip'		=> (string) $user->ip,
709
				);
710
711
				$sql = 'INSERT INTO ' . POLL_VOTES_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
712
				$db->sql_query($sql);
713
			}
714
		}
715
716
		foreach ($cur_voted_id as $option)
717
		{
718
			if (!in_array($option, $voted_id))
719
			{
720
				$sql = 'UPDATE ' . POLL_OPTIONS_TABLE . '
721
					SET poll_option_total = poll_option_total - 1
722
					WHERE poll_option_id = ' . (int) $option . '
723
						AND topic_id = ' . (int) $topic_id;
724
				$db->sql_query($sql);
725
726
				if ($user->data['is_registered'])
727
				{
728
					$sql = 'DELETE FROM ' . POLL_VOTES_TABLE . '
729
						WHERE topic_id = ' . (int) $topic_id . '
730
							AND poll_option_id = ' . (int) $option . '
731
							AND vote_user_id = ' . (int) $user->data['user_id'];
732
					$db->sql_query($sql);
733
				}
734
			}
735
		}
736
737
		if ($user->data['user_id'] == ANONYMOUS && !$user->data['is_bot'])
738
		{
739
			$user->set_cookie('poll_' . $topic_id, implode(',', $voted_id), time() + 31536000);
740
		}
741
742
		$sql = 'UPDATE ' . TOPICS_TABLE . '
743
			SET poll_last_vote = ' . time() . "
744
			WHERE topic_id = $topic_id";
745
		//, topic_last_post_time = ' . time() . " -- for bumping topics with new votes, ignore for now
746
		$db->sql_query($sql);
747
748
		$redirect_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;start=$start");
749
750
		meta_refresh(5, $redirect_url);
751
		trigger_error($user->lang['VOTE_SUBMITTED'] . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $redirect_url . '">', '</a>'));
752
	}
753
754
	$poll_total = 0;
755
	foreach ($poll_info as $poll_option)
756
	{
757
		$poll_total += $poll_option['poll_option_total'];
758
	}
759
760
	if ($poll_info[0]['bbcode_bitfield'])
761
	{
762
		$poll_bbcode = new bbcode();
763
	}
764
	else
765
	{
766
		$poll_bbcode = false;
767
	}
768
769
	for ($i = 0, $size = sizeof($poll_info); $i < $size; $i++)
770
	{
771
		$poll_info[$i]['poll_option_text'] = censor_text($poll_info[$i]['poll_option_text']);
772
773
		if ($poll_bbcode !== false)
774
		{
775
			$poll_bbcode->bbcode_second_pass($poll_info[$i]['poll_option_text'], $poll_info[$i]['bbcode_uid'], $poll_option['bbcode_bitfield']);
776
		}
777
778
		$poll_info[$i]['poll_option_text'] = bbcode_nl2br($poll_info[$i]['poll_option_text']);
779
		$poll_info[$i]['poll_option_text'] = smiley_text($poll_info[$i]['poll_option_text']);
780
	}
781
782
	$topic_data['poll_title'] = censor_text($topic_data['poll_title']);
783
784
	if ($poll_bbcode !== false)
785
	{
786
		$poll_bbcode->bbcode_second_pass($topic_data['poll_title'], $poll_info[0]['bbcode_uid'], $poll_info[0]['bbcode_bitfield']);
787
	}
788
789
	$topic_data['poll_title'] = bbcode_nl2br($topic_data['poll_title']);
790
	$topic_data['poll_title'] = smiley_text($topic_data['poll_title']);
791
792
	unset($poll_bbcode);
793
794
	foreach ($poll_info as $poll_option)
795
	{
796
		$option_pct = ($poll_total > 0) ? $poll_option['poll_option_total'] / $poll_total : 0;
797
		$option_pct_txt = sprintf("%.1d%%", ($option_pct * 100));
798
799
		$template->assign_block_vars('poll_option', array(
800
			'POLL_OPTION_ID' 		=> $poll_option['poll_option_id'],
801
			'POLL_OPTION_CAPTION' 	=> $poll_option['poll_option_text'],
802
			'POLL_OPTION_RESULT' 	=> $poll_option['poll_option_total'],
803
			'POLL_OPTION_PERCENT' 	=> $option_pct_txt,
804
			'POLL_OPTION_PCT'		=> round($option_pct * 100),
805
			'POLL_OPTION_IMG' 		=> $user->img('poll_center', $option_pct_txt, round($option_pct * 250)),
806
			'POLL_OPTION_VOTED'		=> (in_array($poll_option['poll_option_id'], $cur_voted_id)) ? true : false)
807
		);
808
	}
809
810
	$poll_end = $topic_data['poll_length'] + $topic_data['poll_start'];
811
812
	$template->assign_vars(array(
813
		'POLL_QUESTION'		=> $topic_data['poll_title'],
814
		'TOTAL_VOTES' 		=> $poll_total,
815
		'POLL_LEFT_CAP_IMG'	=> $user->img('poll_left'),
816
		'POLL_RIGHT_CAP_IMG'=> $user->img('poll_right'),
817
818
		'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']),
819
		'L_POLL_LENGTH'		=> ($topic_data['poll_length']) ? sprintf($user->lang[($poll_end > time()) ? 'POLL_RUN_TILL' : 'POLL_ENDED_AT'], $user->format_date($poll_end)) : '',
820
821
		'S_HAS_POLL'		=> true,
822
		'S_CAN_VOTE'		=> $s_can_vote,
823
		'S_DISPLAY_RESULTS'	=> $s_display_results,
824
		'S_IS_MULTI_CHOICE'	=> ($topic_data['poll_max_options'] > 1) ? true : false,
825
		'S_POLL_ACTION'		=> $viewtopic_url,
826
827
		'U_VIEW_RESULTS'	=> $viewtopic_url . '&amp;view=viewpoll')
828
	);
829
830
	unset($poll_end, $poll_info, $voted_id);
831
}
832
833
// If the user is trying to reach the second half of the topic, fetch it starting from the end
834
$store_reverse = false;
835
$sql_limit = $config['posts_per_page'];
836
837
if ($start > $total_posts / 2)
838
{
839
	$store_reverse = true;
840
841
	if ($start + $config['posts_per_page'] > $total_posts)
842
	{
843
		$sql_limit = min($config['posts_per_page'], max(1, $total_posts - $start));
844
	}
845
846
	// Select the sort order
847
	$sql_sort_order = $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'ASC' : 'DESC');
848
	$sql_start = max(0, $total_posts - $sql_limit - $start);
849
}
850
else
851
{
852
	// Select the sort order
853
	$sql_sort_order = $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'DESC' : 'ASC');
854
	$sql_start = $start;
855
}
856
857
// Container for user details, only process once
858
$post_list = $user_cache = $id_cache = $attachments = $attach_list = $rowset = $update_count = $post_edit_list = array();
859
$has_attachments = $display_notice = false;
860
$bbcode_bitfield = '';
861
$i = $i_total = 0;
862
863
// Go ahead and pull all data for this topic
864
$sql = 'SELECT p.post_id
865
	FROM ' . POSTS_TABLE . ' p' . (($sort_by_sql[$sort_key][0] == 'u') ? ', ' . USERS_TABLE . ' u': '') . "
866
	WHERE p.topic_id = $topic_id
867
		" . ((!$auth->acl_get('m_approve', $forum_id)) ? 'AND p.post_approved = 1' : '') . "
868
		" . (($sort_by_sql[$sort_key][0] == 'u') ? 'AND u.user_id = p.poster_id': '') . "
869
		$limit_posts_time
870
	ORDER BY $sql_sort_order";
871
$result = $db->sql_query_limit($sql, $sql_limit, $sql_start);
872
873
$i = ($store_reverse) ? $sql_limit - 1 : 0;
874
while ($row = $db->sql_fetchrow($result))
875
{
876
	$post_list[$i] = $row['post_id'];
877
	($store_reverse) ? $i-- : $i++;
878
}
879
$db->sql_freeresult($result);
880
881
if (!sizeof($post_list))
882
{
883
	if ($sort_days)
884
	{
885
		trigger_error('NO_POSTS_TIME_FRAME');
886
	}
887
	else
888
	{
889
		trigger_error('NO_TOPIC');
890
	}
891
}
892
893
// Holding maximum post time for marking topic read
894
// We need to grab it because we do reverse ordering sometimes
895
$max_post_time = 0;
896
897
$sql = $db->sql_build_query('SELECT', array(
898
	'SELECT'	=> 'u.*, z.friend, z.foe, p.*',
899
900
	'FROM'		=> array(
901
		USERS_TABLE		=> 'u',
902
		POSTS_TABLE		=> 'p',
903
	),
904
905
	'LEFT_JOIN'	=> array(
906
		array(
907
			'FROM'	=> array(ZEBRA_TABLE => 'z'),
908
			'ON'	=> 'z.user_id = ' . $user->data['user_id'] . ' AND z.zebra_id = p.poster_id'
909
		)
910
	),
911
912
	'WHERE'		=> $db->sql_in_set('p.post_id', $post_list) . '
913
		AND u.user_id = p.poster_id'
914
));
915
916
$result = $db->sql_query($sql);
917
918
$now = getdate(time() + $user->timezone + $user->dst - date('Z'));
919
920
// Posts are stored in the $rowset array while $attach_list, $user_cache
921
// and the global bbcode_bitfield are built
922
while ($row = $db->sql_fetchrow($result))
923
{
924
	// Set max_post_time
925
	if ($row['post_time'] > $max_post_time)
926
	{
927
		$max_post_time = $row['post_time'];
928
	}
929
930
	$poster_id = $row['poster_id'];
931
932
	// Does post have an attachment? If so, add it to the list
933
	if ($row['post_attachment'] && $config['allow_attachments'])
934
	{
935
		$attach_list[] = $row['post_id'];
936
937
		if ($row['post_approved'])
938
		{
939
			$has_attachments = true;
940
		}
941
	}
942
943
	$rowset[$row['post_id']] = array(
944
		'hide_post'			=> ($row['foe'] && ($view != 'show' || $post_id != $row['post_id'])) ? true : false,
945
946
		'post_id'			=> $row['post_id'],
947
		'post_time'			=> $row['post_time'],
948
		'user_id'			=> $row['user_id'],
949
		'username'			=> $row['username'],
950
		'user_colour'		=> $row['user_colour'],
951
		'topic_id'			=> $row['topic_id'],
952
		'forum_id'			=> $row['forum_id'],
953
		'post_subject'		=> $row['post_subject'],
954
		'post_edit_count'	=> $row['post_edit_count'],
955
		'post_edit_time'	=> $row['post_edit_time'],
956
		'post_edit_reason'	=> $row['post_edit_reason'],
957
		'post_edit_user'	=> $row['post_edit_user'],
958
959
		// Make sure the icon actually exists
960
		'icon_id'			=> (isset($icons[$row['icon_id']]['img'], $icons[$row['icon_id']]['height'], $icons[$row['icon_id']]['width'])) ? $row['icon_id'] : 0,
961
		'post_attachment'	=> $row['post_attachment'],
962
		'post_approved'		=> $row['post_approved'],
963
		'post_reported'		=> $row['post_reported'],
964
		'post_username'		=> $row['post_username'],
965
		'post_text'			=> $row['post_text'],
966
		'bbcode_uid'		=> $row['bbcode_uid'],
967
		'bbcode_bitfield'	=> $row['bbcode_bitfield'],
968
		'enable_smilies'	=> $row['enable_smilies'],
969
		'enable_sig'		=> $row['enable_sig'],
970
		'friend'			=> $row['friend'],
971
		'foe'				=> $row['foe'],
972
	);
973
974
	// Define the global bbcode bitfield, will be used to load bbcodes
975
	$bbcode_bitfield = $bbcode_bitfield | base64_decode($row['bbcode_bitfield']);
976
977
	// Is a signature attached? Are we going to display it?
978
	if ($row['enable_sig'] && $config['allow_sig'] && $user->optionget('viewsigs'))
979
	{
980
		$bbcode_bitfield = $bbcode_bitfield | base64_decode($row['user_sig_bbcode_bitfield']);
981
	}
982
983
	// Cache various user specific data ... so we don't have to recompute
984
	// this each time the same user appears on this page
985
	if (!isset($user_cache[$poster_id]))
986
	{
987
		if ($poster_id == ANONYMOUS)
988
		{
989
			$user_cache[$poster_id] = array(
990
				'joined'		=> '',
991
				'posts'			=> '',
992
				'from'			=> '',
993
994
				'sig'					=> '',
995
				'sig_bbcode_uid'		=> '',
996
				'sig_bbcode_bitfield'	=> '',
997
998
				'online'			=> false,
999
				'avatar'			=> '',
1000
				'rank_title'		=> '',
1001
				'rank_image'		=> '',
1002
				'rank_image_src'	=> '',
1003
				'sig'				=> '',
1004
				'posts'				=> '',
1005
				'profile'			=> '',
1006
				'pm'				=> '',
1007
				'email'				=> '',
1008
				'www'				=> '',
1009
				'icq_status_img'	=> '',
1010
				'icq'				=> '',
1011
				'aim'				=> '',
1012
				'msn'				=> '',
1013
				'yim'				=> '',
1014
				'jabber'			=> '',
1015
				'search'			=> '',
1016
				'age'				=> '',
1017
1018
				'username'			=> $row['username'],
1019
				'user_colour'		=> $row['user_colour'],
1020
1021
				'warnings'			=> 0,
1022
				'allow_pm'			=> 0,
1023
			);
1024
		}
1025
		else
1026
		{
1027
			$user_sig = '';
1028
1029
			// We add the signature to every posters entry because enable_sig is post dependant
1030
			if ($row['user_sig'] && $config['allow_sig'] && $user->optionget('viewsigs'))
1031
			{
1032
				$user_sig = $row['user_sig'];
1033
			}
1034
1035
			$id_cache[] = $poster_id;
1036
1037
			$user_cache[$poster_id] = array(
1038
				'joined'		=> $user->format_date($row['user_regdate']),
1039
				'posts'			=> $row['user_posts'],
1040
				'warnings'		=> (isset($row['user_warnings'])) ? $row['user_warnings'] : 0,
1041
				'from'			=> (!empty($row['user_from'])) ? $row['user_from'] : '',
1042
1043
				'sig'					=> $user_sig,
1044
				'sig_bbcode_uid'		=> (!empty($row['user_sig_bbcode_uid'])) ? $row['user_sig_bbcode_uid'] : '',
1045
				'sig_bbcode_bitfield'	=> (!empty($row['user_sig_bbcode_bitfield'])) ? $row['user_sig_bbcode_bitfield'] : '',
1046
1047
				'viewonline'	=> $row['user_allow_viewonline'],
1048
				'allow_pm'		=> $row['user_allow_pm'],
1049
1050
				'avatar'		=> ($user->optionget('viewavatars')) ? get_user_avatar($row['user_avatar'], $row['user_avatar_type'], $row['user_avatar_width'], $row['user_avatar_height']) : '',
1051
				'age'			=> '',
1052
1053
				'rank_title'		=> '',
1054
				'rank_image'		=> '',
1055
				'rank_image_src'	=> '',
1056
1057
				'username'			=> $row['username'],
1058
				'user_colour'		=> $row['user_colour'],
1059
1060
				'online'		=> false,
1061
				'profile'		=> append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=viewprofile&amp;u=$poster_id"),
1062
				'www'			=> $row['user_website'],
1063
				'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") : '',
1064
				'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") : '',
1065
				'yim'			=> ($row['user_yim']) ? 'http://edit.yahoo.com/config/send_webmesg?.target=' . urlencode($row['user_yim']) . '&amp;.src=pg' : '',
1066
				'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") : '',
1067
				'search'		=> ($auth->acl_get('u_search')) ? append_sid("{$phpbb_root_path}search.$phpEx", 'search_author=' . urlencode($row['username']) .'&amp;showresults=posts') : '',
1068
			);
1069
1070
			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']);
1071
1072
			if (!empty($row['user_allow_viewemail']) || $auth->acl_get('a_email'))
1073
			{
1074
				$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']);
1075
			}
1076
			else
1077
			{
1078
				$user_cache[$poster_id]['email'] = '';
1079
			}
1080
1081
			if (!empty($row['user_icq']))
1082
			{
1083
				$user_cache[$poster_id]['icq'] = 'http://www.icq.com/people/webmsg.php?to=' . $row['user_icq'];
1084
				$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="" />';
1085
			}
1086
			else
1087
			{
1088
				$user_cache[$poster_id]['icq_status_img'] = '';
1089
				$user_cache[$poster_id]['icq'] = '';
1090
			}
1091
1092
			if ($config['allow_birthdays'] && !empty($row['user_birthday']))
1093
			{
1094
				list($bday_day, $bday_month, $bday_year) = array_map('intval', explode('-', $row['user_birthday']));
1095
1096
				if ($bday_year)
1097
				{
1098
					$diff = $now['mon'] - $bday_month;
1099
					if ($diff == 0)
1100
					{
1101
						$diff = ($now['mday'] - $bday_day < 0) ? 1 : 0;
1102
					}
1103
					else
1104
					{
1105
						$diff = ($diff < 0) ? 1 : 0;
1106
					}
1107
1108
					$user_cache[$poster_id]['age'] = (int) ($now['year'] - $bday_year - $diff);
1109
				}
1110
			}
1111
		}
1112
	}
1113
}
1114
$db->sql_freeresult($result);
1115
1116
// Load custom profile fields
1117
if ($config['load_cpf_viewtopic'])
1118
{
1119
	include($phpbb_root_path . 'includes/functions_profile_fields.' . $phpEx);
1120
	$cp = new custom_profile();
1121
1122
	// Grab all profile fields from users in id cache for later use - similar to the poster cache
1123
	$profile_fields_cache = $cp->generate_profile_fields_template('grab', $id_cache);
1124
}
1125
1126
// Generate online information for user
1127
if ($config['load_onlinetrack'] && sizeof($id_cache))
1128
{
1129
	$sql = 'SELECT session_user_id, MAX(session_time) as online_time, MIN(session_viewonline) AS viewonline
1130
		FROM ' . SESSIONS_TABLE . '
1131
		WHERE ' . $db->sql_in_set('session_user_id', $id_cache) . '
1132
		GROUP BY session_user_id';
1133
	$result = $db->sql_query($sql);
1134
1135
	$update_time = $config['load_online_time'] * 60;
1136
	while ($row = $db->sql_fetchrow($result))
1137
	{
1138
		$user_cache[$row['session_user_id']]['online'] = (time() - $update_time < $row['online_time'] && (($row['viewonline']) || $auth->acl_get('u_viewonline'))) ? true : false;
1139
	}
1140
	$db->sql_freeresult($result);
1141
}
1142
unset($id_cache);
1143
1144
// Pull attachment data
1145
if (sizeof($attach_list))
1146
{
1147
	if ($auth->acl_get('u_download') && $auth->acl_get('f_download', $forum_id))
1148
	{
1149
		$sql = 'SELECT *
1150
			FROM ' . ATTACHMENTS_TABLE . '
1151
			WHERE ' . $db->sql_in_set('post_msg_id', $attach_list) . '
1152
				AND in_message = 0
1153
			ORDER BY filetime DESC, post_msg_id ASC';
1154
		$result = $db->sql_query($sql);
1155
1156
		while ($row = $db->sql_fetchrow($result))
1157
		{
1158
			$attachments[$row['post_msg_id']][] = $row;
1159
		}
1160
		$db->sql_freeresult($result);
1161
1162
		// No attachments exist, but post table thinks they do so go ahead and reset post_attach flags
1163
		if (!sizeof($attachments))
1164
		{
1165
			$sql = 'UPDATE ' . POSTS_TABLE . '
1166
				SET post_attachment = 0
1167
				WHERE ' . $db->sql_in_set('post_id', $attach_list);
1168
			$db->sql_query($sql);
1169
1170
			// We need to update the topic indicator too if the complete topic is now without an attachment
1171
			if (sizeof($rowset) != $total_posts)
1172
			{
1173
				// Not all posts are displayed so we query the db to find if there's any attachment for this topic
1174
				$sql = 'SELECT a.post_msg_id as post_id
1175
					FROM ' . ATTACHMENTS_TABLE . ' a, ' . POSTS_TABLE . " p
1176
					WHERE p.topic_id = $topic_id
1177
						AND p.post_approved = 1
1178
						AND p.topic_id = a.topic_id";
1179
				$result = $db->sql_query_limit($sql, 1);
1180
				$row = $db->sql_fetchrow($result);
1181
				$db->sql_freeresult($result);
1182
1183
				if (!$row)
1184
				{
1185
					$sql = 'UPDATE ' . TOPICS_TABLE . "
1186
						SET topic_attachment = 0
1187
						WHERE topic_id = $topic_id";
1188
					$db->sql_query($sql);
1189
				}
1190
			}
1191
			else
1192
			{
1193
				$sql = 'UPDATE ' . TOPICS_TABLE . "
1194
					SET topic_attachment = 0
1195
					WHERE topic_id = $topic_id";
1196
				$db->sql_query($sql);
1197
			}
1198
		}
1199
		else if ($has_attachments && !$topic_data['topic_attachment'])
1200
		{
1201
			// Topic has approved attachments but its flag is wrong
1202
			$sql = 'UPDATE ' . TOPICS_TABLE . "
1203
				SET topic_attachment = 1
1204
				WHERE topic_id = $topic_id";
1205
			$db->sql_query($sql);
1206
1207
			$topic_data['topic_attachment'] = 1;
1208
		}
1209
	}
1210
	else
1211
	{
1212
		$display_notice = true;
1213
	}
1214
}
1215
1216
// Instantiate BBCode if need be
1217
if ($bbcode_bitfield !== '')
1218
{
1219
	$bbcode = new bbcode(base64_encode($bbcode_bitfield));
1220
}
1221
1222
$i_total = sizeof($rowset) - 1;
1223
$prev_post_id = '';
1224
1225
$template->assign_vars(array(
1226
	'S_NUM_POSTS' => sizeof($post_list))
1227
);
1228
1229
// Output the posts
1230
$first_unread = $post_unread = false;
1231
for ($i = 0, $end = sizeof($post_list); $i < $end; ++$i)
1232
{
1233
	// A non-existing rowset only happens if there was no user present for the entered poster_id
1234
	// This could be a broken posts table.
1235
	if (!isset($rowset[$post_list[$i]]))
1236
	{
1237
		continue;
1238
	}
1239
1240
	$row =& $rowset[$post_list[$i]];
1241
	$poster_id = $row['user_id'];
1242
1243
	// End signature parsing, only if needed
1244
	if ($user_cache[$poster_id]['sig'] && $row['enable_sig'] && empty($user_cache[$poster_id]['sig_parsed']))
1245
	{
1246
		$user_cache[$poster_id]['sig'] = censor_text($user_cache[$poster_id]['sig']);
1247
1248
		if ($user_cache[$poster_id]['sig_bbcode_bitfield'])
1249
		{
1250
			$bbcode->bbcode_second_pass($user_cache[$poster_id]['sig'], $user_cache[$poster_id]['sig_bbcode_uid'], $user_cache[$poster_id]['sig_bbcode_bitfield']);
1251
		}
1252
1253
		$user_cache[$poster_id]['sig'] = bbcode_nl2br($user_cache[$poster_id]['sig']);
1254
		$user_cache[$poster_id]['sig'] = smiley_text($user_cache[$poster_id]['sig']);
1255
		$user_cache[$poster_id]['sig_parsed'] = true;
1256
	}
1257
1258
	// Parse the message and subject
1259
	$message = censor_text($row['post_text']);
1260
1261
	// Second parse bbcode here
1262
	if ($row['bbcode_bitfield'])
1263
	{
1264
		$bbcode->bbcode_second_pass($message, $row['bbcode_uid'], $row['bbcode_bitfield']);
1265
	}
1266
1267
	$message = bbcode_nl2br($message);
1268
	$message = smiley_text($message);
1269
1270
	if (!empty($attachments[$row['post_id']]))
1271
	{
1272
		parse_attachments($forum_id, $message, $attachments[$row['post_id']], $update_count);
1273
	}
1274
1275
	// Replace naughty words such as farty pants
1276
	$row['post_subject'] = censor_text($row['post_subject']);
1277
1278
	// Highlight active words (primarily for search)
1279
	if ($highlight_match)
1280
	{
1281
		$message = preg_replace('#(?!<.*)(?<!\w)(' . $highlight_match . ')(?!\w|[^<>]*(?:</s(?:cript|tyle))?>)#is', '<span class="posthilit">\1</span>', $message);
1282
		$row['post_subject'] = preg_replace('#(?!<.*)(?<!\w)(' . $highlight_match . ')(?!\w|[^<>]*(?:</s(?:cript|tyle))?>)#is', '<span class="posthilit">\1</span>', $row['post_subject']);
1283
	}
1284
1285
	// Editing information
1286
	if (($row['post_edit_count'] && $config['display_last_edited']) || $row['post_edit_reason'])
1287
	{
1288
		// Get usernames for all following posts if not already stored
1289
		if (!sizeof($post_edit_list) && ($row['post_edit_reason'] || ($row['post_edit_user'] && !isset($user_cache[$row['post_edit_user']]))))
1290
		{
1291
			// Remove all post_ids already parsed (we do not have to check them)
1292
			$post_storage_list = (!$store_reverse) ? array_slice($post_list, $i) : array_slice(array_reverse($post_list), $i);
1293
1294
			$sql = 'SELECT DISTINCT u.user_id, u.username, u.user_colour
1295
				FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
1296
				WHERE ' . $db->sql_in_set('p.post_id', $post_storage_list) . '
1297
					AND p.post_edit_count <> 0
1298
					AND p.post_edit_user <> 0
1299
					AND p.post_edit_user = u.user_id';
1300
			$result2 = $db->sql_query($sql);
1301
			while ($user_edit_row = $db->sql_fetchrow($result2))
1302
			{
1303
				$post_edit_list[$user_edit_row['user_id']] = $user_edit_row;
1304
			}
1305
			$db->sql_freeresult($result2);
1306
1307
			unset($post_storage_list);
1308
		}
1309
1310
		$l_edit_time_total = ($row['post_edit_count'] == 1) ? $user->lang['EDITED_TIME_TOTAL'] : $user->lang['EDITED_TIMES_TOTAL'];
1311
1312
		if ($row['post_edit_reason'])
1313
		{
1314
			// User having edited the post also being the post author?
1315
			if (!$row['post_edit_user'] || $row['post_edit_user'] == $poster_id)
1316
			{
1317
				$display_username = get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']);
1318
			}
1319
			else
1320
			{
1321
				$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']);
1322
			}
1323
1324
			$l_edited_by = sprintf($l_edit_time_total, $display_username, $user->format_date($row['post_edit_time']), $row['post_edit_count']);
1325
		}
1326
		else
1327
		{
1328
			if ($row['post_edit_user'] && !isset($user_cache[$row['post_edit_user']]))
1329
			{
1330
				$user_cache[$row['post_edit_user']] = $post_edit_list[$row['post_edit_user']];
1331
			}
1332
1333
			// User having edited the post also being the post author?
1334
			if (!$row['post_edit_user'] || $row['post_edit_user'] == $poster_id)
1335
			{
1336
				$display_username = get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']);
1337
			}
1338
			else
1339
			{
1340
				$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']);
1341
			}
1342
1343
			$l_edited_by = sprintf($l_edit_time_total, $display_username, $user->format_date($row['post_edit_time']), $row['post_edit_count']);
1344
		}
1345
	}
1346
	else
1347
	{
1348
		$l_edited_by = '';
1349
	}
1350
1351
	// Bump information
1352
	if ($topic_data['topic_bumped'] && $row['post_id'] == $topic_data['topic_last_post_id'] && isset($user_cache[$topic_data['topic_bumper']]) )
1353
	{
1354
		// It is safe to grab the username from the user cache array, we are at the last
1355
		// post and only the topic poster and last poster are allowed to bump.
1356
		// Admins and mods are bound to the above rules too...
1357
		$l_bumped_by = '<br /><br />' . sprintf($user->lang['BUMPED_BY'], $user_cache[$topic_data['topic_bumper']]['username'], $user->format_date($topic_data['topic_last_post_time']));
1358
	}
1359
	else
1360
	{
1361
		$l_bumped_by = '';
1362
	}
1363
1364
	$cp_row = array();
1365
1366
	//
1367
	if ($config['load_cpf_viewtopic'])
1368
	{
1369
		$cp_row = (isset($profile_fields_cache[$poster_id])) ? $cp->generate_profile_fields_template('show', false, $profile_fields_cache[$poster_id]) : array();
1370
	}
1371
1372
	$post_unread = (isset($topic_tracking_info[$topic_id]) && $row['post_time'] > $topic_tracking_info[$topic_id]) ? true : false;
1373
1374
	$s_first_unread = false;
1375
	if (!$first_unread && $post_unread)
1376
	{
1377
		$s_first_unread = $first_unread = true;
1378
	}
1379
1380
	//
1381
	$postrow = array(
1382
		'POST_AUTHOR_FULL'		=> get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
1383
		'POST_AUTHOR_COLOUR'	=> get_username_string('colour', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
1384
		'POST_AUTHOR'			=> get_username_string('username', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
1385
		'U_POST_AUTHOR'			=> get_username_string('profile', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
1386
1387
		'RANK_TITLE'		=> $user_cache[$poster_id]['rank_title'],
1388
		'RANK_IMG'			=> $user_cache[$poster_id]['rank_image'],
1389
		'RANK_IMG_SRC'		=> $user_cache[$poster_id]['rank_image_src'],
1390
		'POSTER_JOINED'		=> $user_cache[$poster_id]['joined'],
1391
		'POSTER_POSTS'		=> $user_cache[$poster_id]['posts'],
1392
		'POSTER_FROM'		=> $user_cache[$poster_id]['from'],
1393
		'POSTER_AVATAR'		=> $user_cache[$poster_id]['avatar'],
1394
		'POSTER_WARNINGS'	=> $user_cache[$poster_id]['warnings'],
1395
		'POSTER_AGE'		=> $user_cache[$poster_id]['age'],
1396
1397
		'POST_DATE'			=> $user->format_date($row['post_time']),
1398
		'POST_SUBJECT'		=> $row['post_subject'],
1399
		'MESSAGE'			=> $message,
1400
		'SIGNATURE'			=> ($row['enable_sig']) ? $user_cache[$poster_id]['sig'] : '',
1401
		'EDITED_MESSAGE'	=> $l_edited_by,
1402
		'EDIT_REASON'		=> $row['post_edit_reason'],
1403
		'BUMPED_MESSAGE'	=> $l_bumped_by,
1404
1405
		'MINI_POST_IMG'			=> ($post_unread) ? $user->img('icon_post_target_unread', 'NEW_POST') : $user->img('icon_post_target', 'POST'),
1406
		'POST_ICON_IMG'			=> ($topic_data['enable_icons'] && !empty($row['icon_id'])) ? $icons[$row['icon_id']]['img'] : '',
1407
		'POST_ICON_IMG_WIDTH'	=> ($topic_data['enable_icons'] && !empty($row['icon_id'])) ? $icons[$row['icon_id']]['width'] : '',
1408
		'POST_ICON_IMG_HEIGHT'	=> ($topic_data['enable_icons'] && !empty($row['icon_id'])) ? $icons[$row['icon_id']]['height'] : '',
1409
		'ICQ_STATUS_IMG'		=> $user_cache[$poster_id]['icq_status_img'],
1410
		'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')),
1411
		'S_ONLINE'				=> ($poster_id == ANONYMOUS || !$config['load_onlinetrack']) ? false : (($user_cache[$poster_id]['online']) ? true : false),
1412
1413
		'U_EDIT'			=> (!$user->data['is_registered']) ? '' : ((($user->data['user_id'] == $poster_id && $auth->acl_get('f_edit', $forum_id) && ($row['post_time'] > time() - ($config['edit_time'] * 60) || !$config['edit_time'])) || $auth->acl_get('m_edit', $forum_id)) ? append_sid("{$phpbb_root_path}posting.$phpEx", "mode=edit&amp;f=$forum_id&amp;p={$row['post_id']}") : ''),
1414
		'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']}") : '',
1415
		'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) : '',
1416
		'U_DELETE'			=> (!$user->data['is_registered']) ? '' : ((($user->data['user_id'] == $poster_id && $auth->acl_get('f_delete', $forum_id) && $topic_data['topic_last_post_id'] == $row['post_id'] && ($row['post_time'] > time() - ($config['edit_time'] * 60) || !$config['edit_time'])) || $auth->acl_get('m_delete', $forum_id)) ? append_sid("{$phpbb_root_path}posting.$phpEx", "mode=delete&amp;f=$forum_id&amp;p={$row['post_id']}") : ''),
1417
1418
		'U_PROFILE'		=> $user_cache[$poster_id]['profile'],
1419
		'U_SEARCH'		=> $user_cache[$poster_id]['search'],
1420
		'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']) : '',
1421
		'U_EMAIL'		=> $user_cache[$poster_id]['email'],
1422
		'U_WWW'			=> $user_cache[$poster_id]['www'],
1423
		'U_ICQ'			=> $user_cache[$poster_id]['icq'],
1424
		'U_AIM'			=> $user_cache[$poster_id]['aim'],
1425
		'U_MSN'			=> $user_cache[$poster_id]['msn'],
1426
		'U_YIM'			=> $user_cache[$poster_id]['yim'],
1427
		'U_JABBER'		=> $user_cache[$poster_id]['jabber'],
1428
1429
		'U_REPORT'			=> ($auth->acl_get('f_report', $forum_id)) ? append_sid("{$phpbb_root_path}report.$phpEx", 'f=' . $forum_id . '&amp;p=' . $row['post_id']) : '',
1430
		'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) : '',
1431
		'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) : '',
1432
		'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'],
1433
		'U_NEXT_POST_ID'	=> ($i < $i_total && isset($rowset[$post_list[$i + 1]])) ? $rowset[$post_list[$i + 1]]['post_id'] : '',
1434
		'U_PREV_POST_ID'	=> $prev_post_id,
1435
		'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) : '',
1436
		'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) : '',
1437
1438
		'POST_ID'			=> $row['post_id'],
1439
		'POSTER_ID'			=> $poster_id,
1440
1441
		'S_HAS_ATTACHMENTS'	=> (!empty($attachments[$row['post_id']])) ? true : false,
1442
		'S_POST_UNAPPROVED'	=> ($row['post_approved']) ? false : true,
1443
		'S_POST_REPORTED'	=> ($row['post_reported'] && $auth->acl_get('m_report', $forum_id)) ? true : false,
1444
		'S_DISPLAY_NOTICE'	=> $display_notice && $row['post_attachment'],
1445
		'S_FRIEND'			=> ($row['friend']) ? true : false,
1446
		'S_UNREAD_POST'		=> $post_unread,
1447
		'S_FIRST_UNREAD'	=> $s_first_unread,
1448
		'S_CUSTOM_FIELDS'	=> (isset($cp_row['row']) && sizeof($cp_row['row'])) ? true : false,
1449
		'S_TOPIC_POSTER'	=> ($topic_data['topic_poster'] == $poster_id) ? true : false,
1450
1451
		'S_IGNORE_POST'		=> ($row['hide_post']) ? true : false,
1452
		'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>') : '',
1453
	);
1454
1455
	if (isset($cp_row['row']) && sizeof($cp_row['row']))
1456
	{
1457
		$postrow = array_merge($postrow, $cp_row['row']);
1458
	}
1459
1460
	// Dump vars into template
1461
	$template->assign_block_vars('postrow', $postrow);
1462
1463
	if (!empty($cp_row['blockrow']))
1464
	{
1465
		foreach ($cp_row['blockrow'] as $field_data)
1466
		{
1467
			$template->assign_block_vars('postrow.custom_fields', $field_data);
1468
		}
1469
	}
1470
1471
	// Display not already displayed Attachments for this post, we already parsed them. ;)
1472
	if (!empty($attachments[$row['post_id']]))
1473
	{
1474
		foreach ($attachments[$row['post_id']] as $attachment)
1475
		{
1476
			$template->assign_block_vars('postrow.attachment', array(
1477
				'DISPLAY_ATTACHMENT'	=> $attachment)
1478
			);
1479
		}
1480
	}
1481
1482
	$prev_post_id = $row['post_id'];
1483
1484
	unset($rowset[$post_list[$i]]);
1485
	unset($attachments[$row['post_id']]);
1486
}
1487
unset($rowset, $user_cache);
1488
1489
// Update topic view and if necessary attachment view counters ... but only if this is the first 'page view'
1490
if (isset($user->data['session_page']) && strpos($user->data['session_page'], '&t=' . $topic_id) === false)
1491
{
1492
	$sql = 'UPDATE ' . TOPICS_TABLE . '
1493
		SET topic_views = topic_views + 1, topic_last_view_time = ' . time() . "
1494
		WHERE topic_id = $topic_id";
1495
	$db->sql_query($sql);
1496
1497
	// Update the attachment download counts
1498
	if (sizeof($update_count))
1499
	{
1500
		$sql = 'UPDATE ' . ATTACHMENTS_TABLE . '
1501
			SET download_count = download_count + 1
1502
			WHERE ' . $db->sql_in_set('attach_id', array_unique($update_count));
1503
		$db->sql_query($sql);
1504
	}
1505
}
1506
1507
// Only mark topic if it's currently unread. Also make sure we do not set topic tracking back if earlier pages are viewed.
1508
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])
1509
{
1510
	markread('topic', $forum_id, $topic_id, $max_post_time);
1511
1512
	// Update forum info
1513
	$all_marked_read = update_forum_tracking_info($forum_id, $topic_data['forum_last_post_time'], (isset($topic_data['forum_mark_time'])) ? $topic_data['forum_mark_time'] : false, false);
1514
}
1515
else
1516
{
1517
	$all_marked_read = true;
1518
}
1519
1520
// If there are absolutely no more unread posts in this forum and unread posts shown, we can savely show the #unread link
1521
if ($all_marked_read)
1522
{
1523
	if ($post_unread)
1524
	{
1525
		$template->assign_vars(array(
1526
			'U_VIEW_UNREAD_POST'	=> '#unread',
1527
		));
1528
	}
1529
	else if (isset($topic_tracking_info[$topic_id]) && $topic_data['topic_last_post_time'] > $topic_tracking_info[$topic_id])
1530
	{
1531
		$template->assign_vars(array(
1532
			'U_VIEW_UNREAD_POST'	=> append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;view=unread") . '#unread',
1533
		));
1534
	}
1535
}
1536
else if (!$all_marked_read)
1537
{
1538
	$last_page = ((floor($start / $config['posts_per_page']) + 1) == max(ceil($total_posts / $config['posts_per_page']), 1)) ? true : false;
1539
1540
	// What can happen is that we are at the last displayed page. If so, we also display the #unread link based in $post_unread
1541
	if ($last_page && $post_unread)
1542
	{
1543
		$template->assign_vars(array(
1544
			'U_VIEW_UNREAD_POST'	=> '#unread',
1545
		));
1546
	}
1547
	else if (!$last_page)
1548
	{
1549
		$template->assign_vars(array(
1550
			'U_VIEW_UNREAD_POST'	=> append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;view=unread") . '#unread',
1551
		));
1552
	}
1553
}
1554
1555
// We overwrite $_REQUEST['f'] if there is no forum specified
1556
// to be able to display the correct online list.
1557
// One downside is that the user currently viewing this topic/post is not taken into account.
1558
if (empty($_REQUEST['f']))
1559
{
1560
	$_REQUEST['f'] = $forum_id;
1561
}
1562
1563
// Output the page
1564
page_header($user->lang['VIEW_TOPIC'] .' - ' . $topic_data['topic_title']);
1565
1566
$template->set_filenames(array(
1567
	'body' => ($view == 'print') ? 'viewtopic_print.html' : 'viewtopic_body.html')
1568
);
1569
make_jumpbox(append_sid("{$phpbb_root_path}viewforum.$phpEx"), $forum_id);
1570
1571
page_footer();
1572
1573
?>