Register
phpBB.com Wiki · Home Projects Help

root / trunk / phpBB / viewtopic.php

1
<?php
2
/**
3
*
4
* @package phpBB3
5
* @version $Id: viewtopic.php 9064 2008-11-13 13:04:54Z toonarmy $
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
if (!defined('PHPBB_ROOT_PATH')) define('PHPBB_ROOT_PATH', './');
16
if (!defined('PHP_EXT')) define('PHP_EXT', substr(strrchr(__FILE__, '.'), 1));
17
include(PHPBB_ROOT_PATH . 'common.' . PHP_EXT);
18
include(PHPBB_ROOT_PATH . 'includes/functions_display.' . PHP_EXT);
19
include(PHPBB_ROOT_PATH . 'includes/bbcode.' . PHP_EXT);
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
$default_sort_days	= (!empty($user->data['user_post_show_days'])) ? $user->data['user_post_show_days'] : 0;
35
$default_sort_key	= (!empty($user->data['user_post_sortby_type'])) ? $user->data['user_post_sortby_type'] : 't';
36
$default_sort_dir	= (!empty($user->data['user_post_sortby_dir'])) ? $user->data['user_post_sortby_dir'] : 'a';
37
38
$sort_days	= request_var('st', $default_sort_days);
39
$sort_key	= request_var('sk', $default_sort_key);
40
$sort_dir	= request_var('sd', $default_sort_dir);
41
42
$update		= request_var('update', false);
43
44
/**
45
* @todo normalize?
46
*/
47
$hilit_words	= request_var('hilit', '', true);
48
49
// Do we have a topic or post id?
50
if (!$topic_id && !$post_id)
51
{
52
	trigger_error('NO_TOPIC');
53
}
54
55
// Find topic id if user requested a newer or older topic
56
if ($view && !$post_id)
57
{
58
	if (!$forum_id)
59
	{
60
		$sql = 'SELECT forum_id
61
			FROM ' . TOPICS_TABLE . "
62
			WHERE topic_id = $topic_id";
63
		$result = $db->sql_query($sql);
64
		$forum_id = (int) $db->sql_fetchfield('forum_id');
65
		$db->sql_freeresult($result);
66
67
		if (!$forum_id)
68
		{
69
			trigger_error('NO_TOPIC');
70
		}
71
	}
72
73
	if ($view == 'unread')
74
	{
75
		// Get topic tracking info
76
		$topic_tracking_info = get_complete_topic_tracking($forum_id, $topic_id);
77
78
		$topic_last_read = (isset($topic_tracking_info[$topic_id])) ? $topic_tracking_info[$topic_id] : 0;
79
80
		$sql = 'SELECT post_id, topic_id, forum_id
81
			FROM ' . POSTS_TABLE . "
82
			WHERE topic_id = $topic_id
83
				" . (($auth->acl_get('m_approve', $forum_id)) ? '' : 'AND post_approved = 1') . "
84
				AND post_time > $topic_last_read
85
			ORDER BY post_time ASC";
86
		$result = $db->sql_query_limit($sql, 1);
87
		$row = $db->sql_fetchrow($result);
88
		$db->sql_freeresult($result);
89
90
		if (!$row)
91
		{
92
			$sql = 'SELECT topic_last_post_id as post_id, topic_id, forum_id
93
				FROM ' . TOPICS_TABLE . '
94
				WHERE topic_id = ' . $topic_id;
95
			$result = $db->sql_query($sql);
96
			$row = $db->sql_fetchrow($result);
97
			$db->sql_freeresult($result);
98
		}
99
100
		if (!$row)
101
		{
102
			// Setup user environment so we can process lang string
103
			$user->setup('viewtopic');
104
105
			trigger_error('NO_TOPIC');
106
		}
107
108
		$post_id = $row['post_id'];
109
		$topic_id = $row['topic_id'];
110
	}
111
	else if ($view == 'next' || $view == 'previous')
112
	{
113
		$sql_condition = ($view == 'next') ? '>' : '<';
114
		$sql_ordering = ($view == 'next') ? 'ASC' : 'DESC';
115
116
		$sql = 'SELECT forum_id, topic_last_post_time
117
			FROM ' . TOPICS_TABLE . '
118
			WHERE topic_id = ' . $topic_id;
119
		$result = $db->sql_query($sql);
120
		$row = $db->sql_fetchrow($result);
121
		$db->sql_freeresult($result);
122
123
		if (!$row)
124
		{
125
			$user->setup('viewtopic');
126
			// OK, the topic doesn't exist. This error message is not helpful, but technically correct.
127
			trigger_error(($view == 'next') ? 'NO_NEWER_TOPICS' : 'NO_OLDER_TOPICS');
128
		}
129
		else
130
		{
131
			$sql = 'SELECT topic_id, forum_id
132
				FROM ' . TOPICS_TABLE . '
133
				WHERE forum_id = ' . $row['forum_id'] . "
134
					AND topic_moved_id = 0
135
					AND topic_last_post_time $sql_condition {$row['topic_last_post_time']}
136
					" . (($auth->acl_get('m_approve', $row['forum_id'])) ? '' : 'AND topic_approved = 1') . "
137
				ORDER BY topic_last_post_time $sql_ordering";
138
			$result = $db->sql_query_limit($sql, 1);
139
			$row = $db->sql_fetchrow($result);
140
			$db->sql_freeresult($result);
141
142
			if (!$row)
143
			{
144
				$user->setup('viewtopic');
145
				trigger_error(($view == 'next') ? 'NO_NEWER_TOPICS' : 'NO_OLDER_TOPICS');
146
			}
147
			else
148
			{
149
				$topic_id = $row['topic_id'];
150
151
				// Check for global announcement correctness?
152
				if (!$row['forum_id'] && !$forum_id)
153
				{
154
					trigger_error('NO_TOPIC');
155
				}
156
				else if ($row['forum_id'])
157
				{
158
					$forum_id = $row['forum_id'];
159
				}
160
			}
161
		}
162
	}
163
164
	// Check for global announcement correctness?
165
	if ((!isset($row) || !$row['forum_id']) && !$forum_id)
166
	{
167
		trigger_error('NO_TOPIC');
168
	}
169
	else if (isset($row) && $row['forum_id'])
170
	{
171
		$forum_id = $row['forum_id'];
172
	}
173
}
174
175
// This rather complex gaggle of code handles querying for topics but
176
// also allows for direct linking to a post (and the calculation of which
177
// page the post is on and the correct display of viewtopic)
178
$sql_array = array(
179
	'SELECT'	=> 't.*, f.*',
180
181
	'FROM'		=> array(FORUMS_TABLE => 'f'),
182
);
183
184
// The FROM-Order is quite important here, else t.* columns can not be correctly bound.
185
if ($post_id)
186
{
187
	$sql_array['FROM'][POSTS_TABLE] = 'p';
188
}
189
190
// Topics table need to be the last in the chain
191
$sql_array['FROM'][TOPICS_TABLE] = 't';
192
193
if ($user->data['is_registered'])
194
{
195
	$sql_array['SELECT'] .= ', tw.notify_status';
196
	$sql_array['LEFT_JOIN'] = array();
197
198
	$sql_array['LEFT_JOIN'][] = array(
199
		'FROM'	=> array(TOPICS_WATCH_TABLE => 'tw'),
200
		'ON'	=> 'tw.user_id = ' . $user->data['user_id'] . ' AND t.topic_id = tw.topic_id'
201
	);
202
203
	if ($config['allow_bookmarks'])
204
	{
205
		$sql_array['SELECT'] .= ', bm.topic_id as bookmarked';
206
		$sql_array['LEFT_JOIN'][] = array(
207
			'FROM'	=> array(BOOKMARKS_TABLE => 'bm'),
208
			'ON'	=> 'bm.user_id = ' . $user->data['user_id'] . ' AND t.topic_id = bm.topic_id'
209
		);
210
	}
211
212
	if ($config['load_db_lastread'])
213
	{
214
		$sql_array['SELECT'] .= ', tt.mark_time, ft.mark_time as forum_mark_time';
215
216
		$sql_array['LEFT_JOIN'][] = array(
217
			'FROM'	=> array(TOPICS_TRACK_TABLE => 'tt'),
218
			'ON'	=> 'tt.user_id = ' . $user->data['user_id'] . ' AND t.topic_id = tt.topic_id'
219
		);
220
221
		$sql_array['LEFT_JOIN'][] = array(
222
			'FROM'	=> array(FORUMS_TRACK_TABLE => 'ft'),
223
			'ON'	=> 'ft.user_id = ' . $user->data['user_id'] . ' AND t.forum_id = ft.forum_id'
224
		);
225
	}
226
}
227
228
if (!$post_id)
229
{
230
	$sql_array['WHERE'] = "t.topic_id = $topic_id";
231
}
232
else
233
{
234
	$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' : '');
235
}
236
237
$sql_array['WHERE'] .= ' AND (f.forum_id = t.forum_id';
238
239
if (!$forum_id)
240
{
241
	// If it is a global announcement make sure to set the forum id to a postable forum
242
	$sql_array['WHERE'] .= ' OR (t.topic_type = ' . POST_GLOBAL . '
243
		AND f.forum_type = ' . FORUM_POST . ')';
244
}
245
else
246
{
247
	$sql_array['WHERE'] .= ' OR (t.topic_type = ' . POST_GLOBAL . "
248
		AND f.forum_id = $forum_id)";
249
}
250
251
$sql_array['WHERE'] .= ')';
252
253
// Join to forum table on topic forum_id unless topic forum_id is zero
254
// whereupon we join on the forum_id passed as a parameter ... this
255
// is done so navigation, forum name, etc. remain consistent with where
256
// user clicked to view a global topic
257
$sql = $db->sql_build_query('SELECT', $sql_array);
258
$result = $db->sql_query($sql);
259
$topic_data = $db->sql_fetchrow($result);
260
$db->sql_freeresult($result);
261
262
if (!$topic_data)
263
{
264
	// If post_id was submitted, we try at least to display the topic as a last resort...
265
	if ($post_id && $forum_id && $topic_id)
266
	{
267
		redirect(append_sid('viewtopic', "f=$forum_id&amp;t=$topic_id"));
268
	}
269
270
	trigger_error('NO_TOPIC');
271
}
272
273
// This is for determining where we are (page)
274
if ($post_id)
275
{
276
	if ($post_id == $topic_data['topic_first_post_id'] || $post_id == $topic_data['topic_last_post_id'])
277
	{
278
		$check_sort = ($post_id == $topic_data['topic_first_post_id']) ? 'd' : 'a';
279
280
		if ($sort_dir == $check_sort)
281
		{
282
			$topic_data['prev_posts'] = ($auth->acl_get('m_approve', $forum_id)) ? $topic_data['topic_replies_real'] : $topic_data['topic_replies'];
283
		}
284
		else
285
		{
286
			$topic_data['prev_posts'] = 0;
287
		}
288
	}
289
	else
290
	{
291
		$sql = 'SELECT COUNT(p1.post_id) AS prev_posts
292
			FROM ' . POSTS_TABLE . ' p1, ' . POSTS_TABLE . " p2
293
			WHERE p1.topic_id = {$topic_data['topic_id']}
294
				AND p2.post_id = {$post_id}
295
				" . ((!$auth->acl_get('m_approve', $forum_id)) ? 'AND p1.post_approved = 1' : '') . '
296
				AND ' . (($sort_dir == 'd') ? 'p1.post_time >= p2.post_time' : 'p1.post_time <= p2.post_time');
297
298
		$result = $db->sql_query($sql);
299
		$row = $db->sql_fetchrow($result);
300
		$db->sql_freeresult($result);
301
302
		$topic_data['prev_posts'] = $row['prev_posts'] - 1;
303
	}
304
}
305
306
$forum_id = (int) $topic_data['forum_id'];
307
$topic_id = (int) $topic_data['topic_id'];
308
309
//
310
$topic_replies = ($auth->acl_get('m_approve', $forum_id)) ? $topic_data['topic_replies_real'] : $topic_data['topic_replies'];
311
312
// Check sticky/announcement time limit
313
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())
314
{
315
	$sql = 'UPDATE ' . TOPICS_TABLE . '
316
		SET topic_type = ' . POST_NORMAL . ', topic_time_limit = 0
317
		WHERE topic_id = ' . $topic_id;
318
	$db->sql_query($sql);
319
320
	$topic_data['topic_type'] = POST_NORMAL;
321
	$topic_data['topic_time_limit'] = 0;
322
}
323
324
// Setup look and feel
325
$user->setup('viewtopic', $topic_data['forum_style']);
326
327
if (!$topic_data['topic_approved'] && !$auth->acl_get('m_approve', $forum_id))
328
{
329
	trigger_error('NO_TOPIC');
330
}
331
332
// Start auth check
333
if (!$auth->acl_get('f_read', $forum_id))
334
{
335
	if ($user->data['user_id'] != ANONYMOUS)
336
	{
337
		trigger_error('SORRY_AUTH_READ');
338
	}
339
340
	login_box('', $user->lang['LOGIN_VIEWFORUM']);
341
}
342
343
// Forum is passworded ... check whether access has been granted to this
344
// user this session, if not show login box
345
if ($topic_data['forum_password'])
346
{
347
	login_forum_box($topic_data);
348
}
349
350
// Redirect to login or to the correct post upon emailed notification links
351
if (isset($_GET['e']))
352
{
353
	$jump_to = request_var('e', 0);
354
355
	$redirect_url = append_sid('viewtopic', "f=$forum_id&amp;t=$topic_id");
356
357
	if ($user->data['user_id'] == ANONYMOUS)
358
	{
359
		login_box($redirect_url . "&amp;p=$post_id&amp;e=$jump_to", $user->lang['LOGIN_NOTIFY_TOPIC']);
360
	}
361
362
	if ($jump_to > 0)
363
	{
364
		// We direct the already logged in user to the correct post...
365
		redirect($redirect_url . ((!$post_id) ? "&amp;p=$jump_to" : "&amp;p=$post_id") . "#p$jump_to");
366
	}
367
}
368
369
// What is start equal to?
370
if ($post_id)
371
{
372
	$start = floor(($topic_data['prev_posts']) / $config['posts_per_page']) * $config['posts_per_page'];
373
}
374
375
// Get topic tracking info
376
if (!isset($topic_tracking_info))
377
{
378
	$topic_tracking_info = array();
379
380
	// Get topic tracking info
381
	if ($config['load_db_lastread'] && $user->data['is_registered'])
382
	{
383
		$tmp_topic_data = array($topic_id => $topic_data);
384
		$topic_tracking_info = get_topic_tracking($forum_id, $topic_id, $tmp_topic_data, array($forum_id => $topic_data['forum_mark_time']));
385
		unset($tmp_topic_data);
386
	}
387
	else if ($config['load_anon_lastread'] || $user->data['is_registered'])
388
	{
389
		$topic_tracking_info = get_complete_topic_tracking($forum_id, $topic_id);
390
	}
391
}
392
393
// Post ordering options
394
$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']);
395
396
$sort_by_text = array('a' => $user->lang['AUTHOR'], 't' => $user->lang['POST_TIME'], 's' => $user->lang['SUBJECT']);
397
$sort_by_sql = array('a' => 'u.username_clean', 't' => 'p.post_time', 's' => 'p.post_subject');
398
399
$s_limit_days = $s_sort_key = $s_sort_dir = $u_sort_param = '';
400
401
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);
402
403
// Obtain correct post count and ordering SQL if user has
404
// requested anything different
405
if ($sort_days)
406
{
407
	$min_post_time = time() - ($sort_days * 86400);
408
409
	$sql = 'SELECT COUNT(post_id) AS num_posts
410
		FROM ' . POSTS_TABLE . "
411
		WHERE topic_id = $topic_id
412
			AND post_time >= $min_post_time
413
		" . (($auth->acl_get('m_approve', $forum_id)) ? '' : 'AND post_approved = 1');
414
	$result = $db->sql_query($sql);
415
	$total_posts = (int) $db->sql_fetchfield('num_posts');
416
	$db->sql_freeresult($result);
417
418
	$limit_posts_time = "AND p.post_time >= $min_post_time ";
419
420
	if (isset($_POST['sort']))
421
	{
422
		$start = 0;
423
	}
424
}
425
else
426
{
427
	$total_posts = $topic_replies + 1;
428
	$limit_posts_time = '';
429
}
430
431
// Was a highlight request part of the URI?
432
$highlight_match = $highlight = '';
433
if ($hilit_words)
434
{
435
	foreach (explode(' ', trim($hilit_words)) as $word)
436
	{
437
		if (trim($word))
438
		{
439
			$word = str_replace('\*', '\w+?', preg_quote($word, '#'));
440
			$word = preg_replace('#(^|\s)\\\\w\*\?(\s|$)#', '$1\w+?$2', $word);
441
			$highlight_match .= (($highlight_match != '') ? '|' : '') . $word;
442
		}
443
	}
444
445
	$highlight = urlencode($hilit_words);
446
}
447
448
// Make sure $start is set to the last page if it exceeds the amount
449
if ($start < 0 || $start >= $total_posts)
450
{
451
	$start = ($start < 0) ? 0 : floor(($total_posts - 1) / $config['posts_per_page']) * $config['posts_per_page'];
452
}
453
454
// General Viewtopic URL for return links
455
$viewtopic_url = append_sid('viewtopic', "f=$forum_id&amp;t=$topic_id&amp;start=$start" . ((strlen($u_sort_param)) ? "&amp;$u_sort_param" : '') . (($highlight_match) ? "&amp;hilit=$highlight" : ''));
456
457
// Are we watching this topic?
458
$s_watching_topic = array(
459
	'link'			=> '',
460
	'title'			=> '',
461
	'is_watching'	=> false,
462
);
463
464
if (($config['email_enable'] || $config['jab_enable']) && $config['allow_topic_notify'] && $user->data['is_registered'])
465
{
466
	watch_topic_forum('topic', $s_watching_topic, $user->data['user_id'], $forum_id, $topic_id, $topic_data['notify_status'], $start);
467
468
	// Reset forum notification if forum notify is set
469
	if ($config['allow_forum_notify'] && $auth->acl_get('f_subscribe', $forum_id))
470
	{
471
		$s_watching_forum = $s_watching_topic;
472
		watch_topic_forum('forum', $s_watching_forum, $user->data['user_id'], $forum_id, 0);
473
	}
474
}
475
476
// Bookmarks
477
if ($config['allow_bookmarks'] && $user->data['is_registered'] && request_var('bookmark', 0))
478
{
479
	if (check_link_hash(request_var('hash', ''),"topic_$topic_id"))
480
	{
481
		if (!$topic_data['bookmarked'])
482
		{
483
			$sql = 'INSERT INTO ' . BOOKMARKS_TABLE . ' ' . $db->sql_build_array('INSERT', array(
484
				'user_id'	=> $user->data['user_id'],
485
				'topic_id'	=> $topic_id,
486
			));
487
			$db->sql_query($sql);
488
		}
489
		else
490
		{
491
			$sql = 'DELETE FROM ' . BOOKMARKS_TABLE . "
492
				WHERE user_id = {$user->data['user_id']}
493
					AND topic_id = $topic_id";
494
			$db->sql_query($sql);
495
		}
496
		$message = (($topic_data['bookmarked']) ? $user->lang['BOOKMARK_REMOVED'] : $user->lang['BOOKMARK_ADDED']) . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $viewtopic_url . '">', '</a>');
497
	}
498
	else
499
	{
500
		$message = $user->lang['BOOKMARK_ERR'] . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $viewtopic_url . '">', '</a>');
501
	}
502
	meta_refresh(3, $viewtopic_url);
503
504
	trigger_error($message);
505
}
506
507
// Grab ranks
508
$ranks = cache::obtain_ranks();
509
510
// Grab icons
511
$icons = cache::obtain_icons();
512
513
// Grab extensions
514
$extensions = array();
515
if ($topic_data['topic_attachment'])
516
{
517
	$extensions = cache::obtain_attach_extensions($forum_id);
518
}
519
520
// Forum rules listing
521
$s_forum_rules = '';
522
gen_forum_auth_level('topic', $forum_id, $topic_data['forum_status']);
523
524
// Quick mod tools
525
$allow_change_type = ($auth->acl_get('m_', $forum_id) || ($user->data['is_registered'] && $user->data['user_id'] == $topic_data['topic_poster'])) ? true : false;
526
527
$topic_mod = '';
528
$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>') : '';
529
$topic_mod .= ($auth->acl_get('m_delete', $forum_id)) ? '<option value="delete_topic">' . $user->lang['DELETE_TOPIC'] . '</option>' : '';
530
$topic_mod .= ($auth->acl_get('m_move', $forum_id) && $topic_data['topic_status'] != ITEM_MOVED) ? '<option value="move">' . $user->lang['MOVE_TOPIC'] . '</option>' : '';
531
$topic_mod .= ($auth->acl_get('m_split', $forum_id)) ? '<option value="split">' . $user->lang['SPLIT_TOPIC'] . '</option>' : '';
532
$topic_mod .= ($auth->acl_get('m_merge', $forum_id)) ? '<option value="merge">' . $user->lang['MERGE_POSTS'] . '</option>' : '';
533
$topic_mod .= ($auth->acl_get('m_merge', $forum_id)) ? '<option value="merge_topic">' . $user->lang['MERGE_TOPIC'] . '</option>' : '';
534
$topic_mod .= ($auth->acl_get('m_move', $forum_id)) ? '<option value="fork">' . $user->lang['FORK_TOPIC'] . '</option>' : '';
535
$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>' : '';
536
$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>' : '';
537
$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>' : '';
538
$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>' : '';
539
$topic_mod .= ($auth->acl_get('m_', $forum_id)) ? '<option value="topic_logs">' . $user->lang['VIEW_TOPIC_LOGS'] . '</option>' : '';
540
541
// If we've got a hightlight set pass it on to pagination.
542
$pagination = generate_pagination(append_sid('viewtopic', "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);
543
544
// Navigation links
545
generate_forum_nav($topic_data);
546
547
// Forum Rules
548
generate_forum_rules($topic_data);
549
550
// Moderators
551
$forum_moderators = array();
552
get_moderators($forum_moderators, $forum_id);
553
554
// This is only used for print view so ...
555
$server_path = (!$view) ? PHPBB_ROOT_PATH : generate_board_url() . '/';
556
557
// Replace naughty words in title
558
$topic_data['topic_title'] = censor_text($topic_data['topic_title']);
559
560
// Send vars to template
561
$template->assign_vars(array(
562
	'FORUM_ID' 		=> $forum_id,
563
	'FORUM_NAME' 	=> $topic_data['forum_name'],
564
	'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']),
565
	'TOPIC_ID' 		=> $topic_id,
566
	'TOPIC_TITLE' 	=> $topic_data['topic_title'],
567
	'TOPIC_POSTER'	=> $topic_data['topic_poster'],
568
569
	'TOPIC_AUTHOR_FULL'		=> get_username_string('full', $topic_data['topic_poster'], $topic_data['topic_first_poster_name'], $topic_data['topic_first_poster_colour']),
570
	'TOPIC_AUTHOR_COLOUR'	=> get_username_string('colour', $topic_data['topic_poster'], $topic_data['topic_first_poster_name'], $topic_data['topic_first_poster_colour']),
571
	'TOPIC_AUTHOR'			=> get_username_string('username', $topic_data['topic_poster'], $topic_data['topic_first_poster_name'], $topic_data['topic_first_poster_colour']),
572
573
	'PAGINATION' 	=> $pagination,
574
	'PAGE_NUMBER' 	=> on_page($total_posts, $config['posts_per_page'], $start),
575
	'TOTAL_POSTS'	=> ($total_posts == 1) ? $user->lang['VIEW_TOPIC_POST'] : sprintf($user->lang['VIEW_TOPIC_POSTS'], $total_posts),
576
	'U_MCP' 		=> ($auth->acl_get('m_', $forum_id)) ? append_sid('mcp', "i=main&amp;mode=topic_view&amp;f=$forum_id&amp;t=$topic_id&amp;start=$start" . ((strlen($u_sort_param)) ? "&amp;$u_sort_param" : ''), true, $user->session_id) : '',
577
578
	'MODERATORS'	=> (isset($forum_moderators[$forum_id]) && sizeof($forum_moderators[$forum_id])) ? implode(', ', $forum_moderators[$forum_id]) : '',
579
580
	'POST_IMG' 			=> ($topic_data['forum_status'] == ITEM_LOCKED) ? $user->img('button_topic_locked', 'FORUM_LOCKED') : $user->img('button_topic_new', 'POST_NEW_TOPIC'),
581
	'QUOTE_IMG' 		=> $user->img('icon_post_quote', 'REPLY_WITH_QUOTE'),
582
	'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'),
583
	'EDIT_IMG' 			=> $user->img('icon_post_edit', 'EDIT_POST'),
584
	'DELETE_IMG' 		=> $user->img('icon_post_delete', 'DELETE_POST'),
585
	'INFO_IMG' 			=> $user->img('icon_post_info', 'VIEW_INFO'),
586
	'PROFILE_IMG'		=> $user->img('icon_user_profile', 'READ_PROFILE'),
587
	'SEARCH_IMG' 		=> $user->img('icon_user_search', 'SEARCH_USER_POSTS'),
588
	'PM_IMG' 			=> $user->img('icon_contact_pm', 'SEND_PRIVATE_MESSAGE'),
589
	'EMAIL_IMG' 		=> $user->img('icon_contact_email', 'SEND_EMAIL'),
590
	'WWW_IMG' 			=> $user->img('icon_contact_www', 'VISIT_WEBSITE'),
591
	'ICQ_IMG' 			=> $user->img('icon_contact_icq', 'ICQ'),
592
	'AIM_IMG' 			=> $user->img('icon_contact_aim', 'AIM'),
593
	'MSN_IMG' 			=> $user->img('icon_contact_msnm', 'MSNM'),
594
	'YIM_IMG' 			=> $user->img('icon_contact_yahoo', 'YIM'),
595
	'JABBER_IMG'		=> $user->img('icon_contact_jabber', 'JABBER') ,
596
	'REPORT_IMG'		=> $user->img('icon_post_report', 'REPORT_POST'),
597
	'REPORTED_IMG'		=> $user->img('icon_topic_reported', 'POST_REPORTED'),
598
	'UNAPPROVED_IMG'	=> $user->img('icon_topic_unapproved', 'POST_UNAPPROVED'),
599
	'WARN_IMG'			=> $user->img('icon_user_warn', 'WARN_USER'),
600
601
	'S_IS_LOCKED'			=>($topic_data['topic_status'] == ITEM_UNLOCKED) ? false : true,
602
	'S_SELECT_SORT_DIR' 	=> $s_sort_dir,
603
	'S_SELECT_SORT_KEY' 	=> $s_sort_key,
604
	'S_SELECT_SORT_DAYS' 	=> $s_limit_days,
605
	'S_SINGLE_MODERATOR'	=> (!empty($forum_moderators[$forum_id]) && sizeof($forum_moderators[$forum_id]) > 1) ? false : true,
606
	'S_TOPIC_ACTION' 		=> append_sid('viewtopic', "f=$forum_id&amp;t=$topic_id&amp;start=$start"),
607
	'S_TOPIC_MOD' 			=> ($topic_mod != '') ? '<select name="action" id="quick-mod-select">' . $topic_mod . '</select>' : '',
608
	'S_MOD_ACTION' 			=> append_sid('mcp', "f=$forum_id&amp;t=$topic_id&amp;quickmod=1&amp;redirect=" . urlencode(str_replace('&amp;', '&', $viewtopic_url)), true, $user->session_id),
609
610
	'S_VIEWTOPIC'			=> true,
611
	'S_DISPLAY_SEARCHBOX'	=> ($auth->acl_get('u_search') && $auth->acl_get('f_search', $forum_id) && $config['load_search']) ? true : false,
612
	'S_SEARCHBOX_ACTION'	=> append_sid('search', 't=' . $topic_id),
613
614
	'S_DISPLAY_POST_INFO'	=> ($topic_data['forum_type'] == FORUM_POST && ($auth->acl_get('f_post', $forum_id) || $user->data['user_id'] == ANONYMOUS)) ? true : false,
615
	'S_DISPLAY_REPLY_INFO'	=> ($topic_data['forum_type'] == FORUM_POST && ($auth->acl_get('f_reply', $forum_id) || $user->data['user_id'] == ANONYMOUS)) ? true : false,
616
617
	'U_TOPIC'				=> "{$server_path}viewtopic." . PHP_EXT . "?f=$forum_id&amp;t=$topic_id",
618
	'U_FORUM'				=> $server_path,
619
	'U_VIEW_TOPIC' 			=> $viewtopic_url,
620
	'U_VIEW_FORUM' 			=> append_sid('viewforum', 'f=' . $forum_id),
621
	'U_VIEW_OLDER_TOPIC'	=> append_sid('viewtopic', "f=$forum_id&amp;t=$topic_id&amp;view=previous"),
622
	'U_VIEW_NEWER_TOPIC'	=> append_sid('viewtopic', "f=$forum_id&amp;t=$topic_id&amp;view=next"),
623
	'U_PRINT_TOPIC'			=> ($auth->acl_get('f_print', $forum_id)) ? $viewtopic_url . '&amp;view=print' : '',
624
	'U_EMAIL_TOPIC'			=> ($auth->acl_get('f_email', $forum_id) && $config['email_enable']) ? append_sid('memberlist', "mode=email&amp;t=$topic_id") : '',
625
626
	'U_WATCH_TOPIC' 		=> $s_watching_topic['link'],
627
	'L_WATCH_TOPIC' 		=> $s_watching_topic['title'],
628
	'S_WATCHING_TOPIC'		=> $s_watching_topic['is_watching'],
629
630
	'U_BOOKMARK_TOPIC'		=> ($user->data['is_registered'] && $config['allow_bookmarks']) ? $viewtopic_url . '&amp;bookmark=1&amp;hash=' . generate_link_hash("topic_$topic_id") : '',
631
	'L_BOOKMARK_TOPIC'		=> ($user->data['is_registered'] && $config['allow_bookmarks'] && $topic_data['bookmarked']) ? $user->lang['BOOKMARK_TOPIC_REMOVE'] : $user->lang['BOOKMARK_TOPIC'],
632
633
	'U_POST_NEW_TOPIC' 		=> ($auth->acl_get('f_post', $forum_id) || $user->data['user_id'] == ANONYMOUS) ? append_sid('posting', "mode=post&amp;f=$forum_id") : '',
634
	'U_POST_REPLY_TOPIC' 	=> ($auth->acl_get('f_reply', $forum_id) || $user->data['user_id'] == ANONYMOUS) ? append_sid('posting', "mode=reply&amp;f=$forum_id&amp;t=$topic_id") : '',
635
	'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('posting', "mode=bump&amp;f=$forum_id&amp;t=$topic_id&amp;hash=" . generate_link_hash("topic_$topic_id")) : '')
636
);
637
638
// Does this topic contain a poll?
639
if (!empty($topic_data['poll_start']))
640
{
641
	$sql = 'SELECT o.*, p.bbcode_bitfield, p.bbcode_uid
642
		FROM ' . POLL_OPTIONS_TABLE . ' o, ' . POSTS_TABLE . " p
643
		WHERE o.topic_id = $topic_id
644
			AND p.post_id = {$topic_data['topic_first_post_id']}
645
			AND p.topic_id = o.topic_id
646
		ORDER BY o.poll_option_id";
647
	$result = $db->sql_query($sql);
648
649
	$poll_info = array();
650
	while ($row = $db->sql_fetchrow($result))
651
	{
652
		$poll_info[] = $row;
653
	}
654
	$db->sql_freeresult($result);
655
656
	$cur_voted_id = array();
657
	if ($user->data['is_registered'])
658
	{
659
		$sql = 'SELECT poll_option_id
660
			FROM ' . POLL_VOTES_TABLE . '
661
			WHERE topic_id = ' . $topic_id . '
662
				AND vote_user_id = ' . $user->data['user_id'];
663
		$result = $db->sql_query($sql);
664
665
		while ($row = $db->sql_fetchrow($result))
666
		{
667
			$cur_voted_id[] = $row['poll_option_id'];
668
		}
669
		$db->sql_freeresult($result);
670
	}
671
	else
672
	{
673
		// Cookie based guest tracking ... I don't like this but hum ho
674
		// it's oft requested. This relies on "nice" users who don't feel
675
		// the need to delete cookies to mess with results.
676
		if (isset($_COOKIE[$config['cookie_name'] . '_poll_' . $topic_id]))
677
		{
678
			$cur_voted_id = explode(',', $_COOKIE[$config['cookie_name'] . '_poll_' . $topic_id]);
679
			$cur_voted_id = array_map('intval', $cur_voted_id);
680
		}
681
	}
682
683
	$s_can_vote = (((!sizeof($cur_voted_id) && $auth->acl_get('f_vote', $forum_id)) ||
684
		($auth->acl_get('f_votechg', $forum_id) && $topic_data['poll_vote_change'])) &&
685
		(($topic_data['poll_length'] != 0 && $topic_data['poll_start'] + $topic_data['poll_length'] > time()) || $topic_data['poll_length'] == 0) &&
686
		$topic_data['topic_status'] != ITEM_LOCKED &&
687
		$topic_data['forum_status'] != ITEM_LOCKED) ? true : false;
688
	$s_display_results = (!$s_can_vote || ($s_can_vote && sizeof($cur_voted_id)) || $view == 'viewpoll') ? true : false;
689
690
	if ($update && $s_can_vote)
691
	{
692
693
		if (!sizeof($voted_id) || sizeof($voted_id) > $topic_data['poll_max_options'] || in_array(VOTE_CONVERTED, $cur_voted_id))
694
		{
695
			$redirect_url = append_sid('viewtopic', "f=$forum_id&amp;t=$topic_id&amp;start=$start");
696
697
			meta_refresh(5, $redirect_url);
698
			if (!sizeof($voted_id))
699
			{
700
				$message = 'NO_VOTE_OPTION';
701
			}
702
			else if (sizeof($voted_id) > $topic_data['poll_max_options'])
703
			{
704
				$message = 'TOO_MANY_VOTE_OPTIONS';
705
			}
706
			else
707
			{
708
				$message = 'VOTE_CONVERTED';
709
			}
710
711
			$message = $user->lang[$message] . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $redirect_url . '">', '</a>');
712
			trigger_error($message);
713
		}
714
715
		foreach ($voted_id as $option)
716
		{
717
			if (in_array($option, $cur_voted_id))
718
			{
719
				continue;
720
			}
721
722
			$sql = 'UPDATE ' . POLL_OPTIONS_TABLE . '
723
				SET poll_option_total = poll_option_total + 1
724
				WHERE poll_option_id = ' . (int) $option . '
725
					AND topic_id = ' . (int) $topic_id;
726
			$db->sql_query($sql);
727
728
			if ($user->data['is_registered'])
729
			{
730
				$sql_ary = array(
731
					'topic_id'			=> (int) $topic_id,
732
					'poll_option_id'	=> (int) $option,
733
					'vote_user_id'		=> (int) $user->data['user_id'],
734
					'vote_user_ip'		=> (string) $user->ip,
735
				);
736
737
				$sql = 'INSERT INTO ' . POLL_VOTES_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
738
				$db->sql_query($sql);
739
			}
740
		}
741
742
		foreach ($cur_voted_id as $option)
743
		{
744
			if (!in_array($option, $voted_id))
745
			{
746
				$sql = 'UPDATE ' . POLL_OPTIONS_TABLE . '
747
					SET poll_option_total = poll_option_total - 1
748
					WHERE poll_option_id = ' . (int) $option . '
749
						AND topic_id = ' . (int) $topic_id;
750
				$db->sql_query($sql);
751
752
				if ($user->data['is_registered'])
753
				{
754
					$sql = 'DELETE FROM ' . POLL_VOTES_TABLE . '
755
						WHERE topic_id = ' . (int) $topic_id . '
756
							AND poll_option_id = ' . (int) $option . '
757
							AND vote_user_id = ' . (int) $user->data['user_id'];
758
					$db->sql_query($sql);
759
				}
760
			}
761
		}
762
763
		if ($user->data['user_id'] == ANONYMOUS && !$user->data['is_bot'])
764
		{
765
			$user->set_cookie('poll_' . $topic_id, implode(',', $voted_id), time() + 31536000);
766
		}
767
768
		$sql = 'UPDATE ' . TOPICS_TABLE . '
769
			SET poll_last_vote = ' . time() . "
770
			WHERE topic_id = $topic_id";
771
		//, topic_last_post_time = ' . time() . " -- for bumping topics with new votes, ignore for now
772
		$db->sql_query($sql);
773
774
		$redirect_url = append_sid('viewtopic', "f=$forum_id&amp;t=$topic_id&amp;start=$start");
775
776
		meta_refresh(5, $redirect_url);
777
		trigger_error($user->lang['VOTE_SUBMITTED'] . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $redirect_url . '">', '</a>'));
778
	}
779
780
	$poll_total = 0;
781
	foreach ($poll_info as $poll_option)
782
	{
783
		$poll_total += $poll_option['poll_option_total'];
784
	}
785
786
	if ($poll_info[0]['bbcode_bitfield'])
787
	{
788
		$poll_bbcode = new bbcode();
789
	}
790
	else
791
	{
792
		$poll_bbcode = false;
793
	}
794
795
	for ($i = 0, $size = sizeof($poll_info); $i < $size; $i++)
796
	{
797
		$poll_info[$i]['poll_option_text'] = censor_text($poll_info[$i]['poll_option_text']);
798
799
		if ($poll_bbcode !== false)
800
		{
801
			$poll_bbcode->bbcode_second_pass($poll_info[$i]['poll_option_text'], $poll_info[$i]['bbcode_uid'], $poll_option['bbcode_bitfield']);
802
		}
803
804
		$poll_info[$i]['poll_option_text'] = bbcode_nl2br($poll_info[$i]['poll_option_text']);
805
		$poll_info[$i]['poll_option_text'] = smiley_text($poll_info[$i]['poll_option_text']);
806
	}
807
808
	$topic_data['poll_title'] = censor_text($topic_data['poll_title']);
809
810
	if ($poll_bbcode !== false)
811
	{
812
		$poll_bbcode->bbcode_second_pass($topic_data['poll_title'], $poll_info[0]['bbcode_uid'], $poll_info[0]['bbcode_bitfield']);
813
	}
814
815
	$topic_data['poll_title'] = bbcode_nl2br($topic_data['poll_title']);
816
	$topic_data['poll_title'] = smiley_text($topic_data['poll_title']);
817
818
	unset($poll_bbcode);
819
820
	foreach ($poll_info as $poll_option)
821
	{
822
		$option_pct = ($poll_total > 0) ? $poll_option['poll_option_total'] / $poll_total : 0;
823
		$option_pct_txt = sprintf("%.1d%%", round($option_pct * 100));
824
825
		$template->assign_block_vars('poll_option', array(
826
			'POLL_OPTION_ID' 		=> $poll_option['poll_option_id'],
827
			'POLL_OPTION_CAPTION' 	=> $poll_option['poll_option_text'],
828
			'POLL_OPTION_RESULT' 	=> $poll_option['poll_option_total'],
829
			'POLL_OPTION_PERCENT' 	=> $option_pct_txt,
830
			'POLL_OPTION_PCT'		=> round($option_pct * 100),
831
			'POLL_OPTION_IMG' 		=> $user->img('poll_center', $option_pct_txt, '', round($option_pct * 250)),
832
			'POLL_OPTION_VOTED'		=> (in_array($poll_option['poll_option_id'], $cur_voted_id)) ? true : false)
833
		);
834
	}
835
836
	$poll_end = $topic_data['poll_length'] + $topic_data['poll_start'];
837
838
	$template->assign_vars(array(
839
		'POLL_QUESTION'		=> $topic_data['poll_title'],
840
		'TOTAL_VOTES' 		=> $poll_total,
841
		'POLL_LEFT_CAP_IMG'	=> $user->img('poll_left'),
842
		'POLL_RIGHT_CAP_IMG'=> $user->img('poll_right'),
843
844
		'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']),
845
		'L_POLL_LENGTH'		=> ($topic_data['poll_length']) ? sprintf($user->lang[($poll_end > time()) ? 'POLL_RUN_TILL' : 'POLL_ENDED_AT'], $user->format_date($poll_end)) : '',
846
847
		'S_HAS_POLL'		=> true,