two new tests, added security suite and fixed utf8 tests.
[phpbb.git] / phpBB / viewtopic.php
blob4ad1d05caf55ccdaf4f1f424655c77714924ddbb
1 <?php
2 /**
4 * @package phpBB3
5 * @version $Id$
6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
9 */
11 /**
12 * @ignore
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);
21 // Start session management
22 $user->session_begin();
23 $auth->acl($user->data);
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));
31 $start = request_var('start', 0);
32 $view = request_var('view', '');
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'));
38 $update = request_var('update', false);
40 /**
41 * @todo normalize?
43 $hilit_words = request_var('hilit', '', true);
45 // Do we have a topic or post id?
46 if (!$topic_id && !$post_id)
48 trigger_error('NO_TOPIC');
51 // Find topic id if user requested a newer or older topic
52 if ($view && !$post_id)
54 if (!$forum_id)
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);
63 if (!$forum_id)
65 trigger_error('NO_TOPIC');
69 if ($view == 'unread')
71 // Get topic tracking info
72 $topic_tracking_info = get_complete_topic_tracking($forum_id, $topic_id);
74 $topic_last_read = (isset($topic_tracking_info[$topic_id])) ? $topic_tracking_info[$topic_id] : 0;
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);
86 if (!$row)
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);
96 if (!$row)
98 // Setup user environment so we can process lang string
99 $user->setup('viewtopic');
101 trigger_error('NO_TOPIC');
104 $post_id = $row['post_id'];
105 $topic_id = $row['topic_id'];
107 else if ($view == 'next' || $view == 'previous')
109 $sql_condition = ($view == 'next') ? '>' : '<';
110 $sql_ordering = ($view == 'next') ? 'ASC' : 'DESC';
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);
119 if (!$row)
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');
125 else
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);
138 if (!$row)
140 $user->setup('viewtopic');
141 trigger_error(($view == 'next') ? 'NO_NEWER_TOPICS' : 'NO_OLDER_TOPICS');
143 else
145 $topic_id = $row['topic_id'];
147 // Check for global announcement correctness?
148 if (!$row['forum_id'] && !$forum_id)
150 trigger_error('NO_TOPIC');
152 else if ($row['forum_id'])
154 $forum_id = $row['forum_id'];
160 // Check for global announcement correctness?
161 if ((!isset($row) || !$row['forum_id']) && !$forum_id)
163 trigger_error('NO_TOPIC');
165 else if (isset($row) && $row['forum_id'])
167 $forum_id = $row['forum_id'];
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.*',
177 'FROM' => array(
178 FORUMS_TABLE => 'f',
182 if ($user->data['is_registered'])
184 $sql_array['SELECT'] .= ', tw.notify_status';
185 $sql_array['LEFT_JOIN'] = array();
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'
192 if ($config['allow_bookmarks'])
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'
201 if ($config['load_db_lastread'])
203 $sql_array['SELECT'] .= ', tt.mark_time, ft.mark_time as forum_mark_time';
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'
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'
217 if (!$post_id)
219 $sql_array['WHERE'] = "t.topic_id = $topic_id";
221 else
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';
227 $sql_array['WHERE'] .= ' AND (f.forum_id = t.forum_id';
229 if (!$forum_id)
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 . ')';
235 else
237 $sql_array['WHERE'] .= ' OR (t.topic_type = ' . POST_GLOBAL . "
238 AND f.forum_id = $forum_id)";
241 $sql_array['WHERE'] .= ')';
242 $sql_array['FROM'][TOPICS_TABLE] = 't';
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);
253 if (!$topic_data)
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)
258 redirect(append_sid('viewtopic', "f=$forum_id&amp;t=$topic_id"));
261 trigger_error('NO_TOPIC');
264 // This is for determining where we are (page)
265 if ($post_id)
267 if ($post_id == $topic_data['topic_first_post_id'] || $post_id == $topic_data['topic_last_post_id'])
269 $check_sort = ($post_id == $topic_data['topic_first_post_id']) ? 'd' : 'a';
271 if ($sort_dir == $check_sort)
273 $topic_data['prev_posts'] = ($auth->acl_get('m_approve', $forum_id)) ? $topic_data['topic_replies_real'] : $topic_data['topic_replies'];
275 else
277 $topic_data['prev_posts'] = 0;
280 else
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');
289 $result = $db->sql_query($sql);
290 $row = $db->sql_fetchrow($result);
291 $db->sql_freeresult($result);
293 $topic_data['prev_posts'] = $row['prev_posts'] - 1;
297 $forum_id = (int) $topic_data['forum_id'];
298 $topic_id = (int) $topic_data['topic_id'];
301 $topic_replies = ($auth->acl_get('m_approve', $forum_id)) ? $topic_data['topic_replies_real'] : $topic_data['topic_replies'];
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())
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);
311 $topic_data['topic_type'] = POST_NORMAL;
312 $topic_data['topic_time_limit'] = 0;
315 // Setup look and feel
316 $user->setup('viewtopic', $topic_data['forum_style']);
318 if (!$topic_data['topic_approved'] && !$auth->acl_get('m_approve', $forum_id))
320 trigger_error('NO_TOPIC');
323 // Start auth check
324 if (!$auth->acl_get('f_read', $forum_id))
326 if ($user->data['user_id'] != ANONYMOUS)
328 trigger_error('SORRY_AUTH_READ');
331 login_box('', $user->lang['LOGIN_VIEWFORUM']);
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'])
338 login_forum_box($topic_data);
341 // Redirect to login or to the correct post upon emailed notification links
342 if (isset($_GET['e']))
344 $jump_to = request_var('e', 0);
346 $redirect_url = append_sid('viewtopic', "f=$forum_id&amp;t=$topic_id");
348 if ($user->data['user_id'] == ANONYMOUS)
350 login_box($redirect_url . "&amp;p=$post_id&amp;e=$jump_to", $user->lang['LOGIN_NOTIFY_TOPIC']);
353 if ($jump_to > 0)
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");
360 // What is start equal to?
361 if ($post_id)
363 $start = floor(($topic_data['prev_posts']) / $config['posts_per_page']) * $config['posts_per_page'];
366 // Get topic tracking info
367 if (!isset($topic_tracking_info))
369 $topic_tracking_info = array();
371 // Get topic tracking info
372 if ($config['load_db_lastread'] && $user->data['is_registered'])
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);
378 else if ($config['load_anon_lastread'] || $user->data['is_registered'])
380 $topic_tracking_info = get_complete_topic_tracking($forum_id, $topic_id);
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']);
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');
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);
393 // Obtain correct post count and ordering SQL if user has
394 // requested anything different
395 if ($sort_days)
397 $min_post_time = time() - ($sort_days * 86400);
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);
408 $limit_posts_time = "AND p.post_time >= $min_post_time ";
410 if (isset($_POST['sort']))
412 $start = 0;
415 else
417 $total_posts = $topic_replies + 1;
418 $limit_posts_time = '';
421 // Was a highlight request part of the URI?
422 $highlight_match = $highlight = '';
423 if ($hilit_words)
425 foreach (explode(' ', trim($hilit_words)) as $word)
427 if (trim($word))
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;
435 $highlight = urlencode($hilit_words);
438 // Make sure $start is set to the last page if it exceeds the amount
439 if ($start < 0 || $start > $total_posts)
441 $start = ($start < 0) ? 0 : floor(($total_posts - 1) / $config['posts_per_page']) * $config['posts_per_page'];
444 // General Viewtopic URL for return links
445 $viewtopic_url = append_sid('viewtopic', "f=$forum_id&amp;t=$topic_id&amp;start=$start&amp;$u_sort_param" . (($highlight_match) ? "&amp;hilit=$highlight" : ''));
447 // Are we watching this topic?
448 $s_watching_topic = array(
449 'link' => '',
450 'title' => '',
451 'is_watching' => false,
454 if ($config['email_enable'] && $config['allow_topic_notify'] && $user->data['is_registered'])
456 watch_topic_forum('topic', $s_watching_topic, $user->data['user_id'], $forum_id, $topic_id, $topic_data['notify_status'], $start);
459 // Bookmarks
460 if ($config['allow_bookmarks'] && $user->data['is_registered'] && request_var('bookmark', 0))
462 if (!$topic_data['bookmarked'])
464 $sql = 'INSERT INTO ' . BOOKMARKS_TABLE . ' ' . $db->sql_build_array('INSERT', array(
465 'user_id' => $user->data['user_id'],
466 'topic_id' => $topic_id,
468 $db->sql_query($sql);
470 else
472 $sql = 'DELETE FROM ' . BOOKMARKS_TABLE . "
473 WHERE user_id = {$user->data['user_id']}
474 AND topic_id = $topic_id";
475 $db->sql_query($sql);
478 meta_refresh(3, $viewtopic_url);
480 $message = (($topic_data['bookmarked']) ? $user->lang['BOOKMARK_REMOVED'] : $user->lang['BOOKMARK_ADDED']) . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $viewtopic_url . '">', '</a>');
481 trigger_error($message);
484 // Grab ranks
485 $ranks = cache::obtain_ranks();
487 // Grab icons
488 $icons = cache::obtain_icons();
490 // Grab extensions
491 $extensions = array();
492 if ($topic_data['topic_attachment'])
494 $extensions = cache::obtain_attach_extensions($forum_id);
497 // Forum rules listing
498 $s_forum_rules = '';
499 gen_forum_auth_level('topic', $forum_id, $topic_data['forum_status']);
501 // Quick mod tools
502 $allow_change_type = ($auth->acl_get('m_', $forum_id) || ($user->data['is_registered'] && $user->data['user_id'] == $topic_data['topic_poster'])) ? true : false;
504 $topic_mod = '';
505 $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>') : '';
506 $topic_mod .= ($auth->acl_get('m_delete', $forum_id)) ? '<option value="delete_topic">' . $user->lang['DELETE_TOPIC'] . '</option>' : '';
507 $topic_mod .= ($auth->acl_get('m_move', $forum_id) && $topic_data['topic_status'] != ITEM_MOVED) ? '<option value="move">' . $user->lang['MOVE_TOPIC'] . '</option>' : '';
508 $topic_mod .= ($auth->acl_get('m_split', $forum_id)) ? '<option value="split">' . $user->lang['SPLIT_TOPIC'] . '</option>' : '';
509 $topic_mod .= ($auth->acl_get('m_merge', $forum_id)) ? '<option value="merge">' . $user->lang['MERGE_POSTS'] . '</option>' : '';
510 $topic_mod .= ($auth->acl_get('m_merge', $forum_id)) ? '<option value="merge_topic">' . $user->lang['MERGE_TOPIC'] . '</option>' : '';
511 $topic_mod .= ($auth->acl_get('m_move', $forum_id)) ? '<option value="fork">' . $user->lang['FORK_TOPIC'] . '</option>' : '';
512 $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>' : '';
513 $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>' : '';
514 $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>' : '';
515 $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>' : '';
516 $topic_mod .= ($auth->acl_get('m_', $forum_id)) ? '<option value="topic_logs">' . $user->lang['VIEW_TOPIC_LOGS'] . '</option>' : '';
518 // If we've got a hightlight set pass it on to pagination.
519 $pagination = generate_pagination(append_sid('viewtopic', "f=$forum_id&amp;t=$topic_id&amp;$u_sort_param" . (($highlight_match) ? "&amp;hilit=$highlight" : '')), $total_posts, $config['posts_per_page'], $start);
521 // Navigation links
522 generate_forum_nav($topic_data);
524 // Forum Rules
525 generate_forum_rules($topic_data);
527 // Moderators
528 $forum_moderators = array();
529 get_moderators($forum_moderators, $forum_id);
531 // This is only used for print view so ...
532 $server_path = (!$view) ? PHPBB_ROOT_PATH : generate_board_url() . '/';
534 // Replace naughty words in title
535 $topic_data['topic_title'] = censor_text($topic_data['topic_title']);
537 // Send vars to template
538 $template->assign_vars(array(
539 'FORUM_ID' => $forum_id,
540 'FORUM_NAME' => $topic_data['forum_name'],
541 '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']),
542 'TOPIC_ID' => $topic_id,
543 'TOPIC_TITLE' => $topic_data['topic_title'],
544 'TOPIC_POSTER' => $topic_data['topic_poster'],
546 'TOPIC_AUTHOR_FULL' => get_username_string('full', $topic_data['topic_poster'], $topic_data['topic_first_poster_name'], $topic_data['topic_first_poster_colour']),
547 'TOPIC_AUTHOR_COLOUR' => get_username_string('colour', $topic_data['topic_poster'], $topic_data['topic_first_poster_name'], $topic_data['topic_first_poster_colour']),
548 'TOPIC_AUTHOR' => get_username_string('username', $topic_data['topic_poster'], $topic_data['topic_first_poster_name'], $topic_data['topic_first_poster_colour']),
550 'PAGINATION' => $pagination,
551 'PAGE_NUMBER' => on_page($total_posts, $config['posts_per_page'], $start),
552 'TOTAL_POSTS' => ($total_posts == 1) ? $user->lang['VIEW_TOPIC_POST'] : sprintf($user->lang['VIEW_TOPIC_POSTS'], $total_posts),
553 '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&amp;$u_sort_param", true, $user->session_id) : '',
554 'MODERATORS' => (isset($forum_moderators[$forum_id]) && sizeof($forum_moderators[$forum_id])) ? implode(', ', $forum_moderators[$forum_id]) : '',
556 'POST_IMG' => ($topic_data['forum_status'] == ITEM_LOCKED) ? $user->img('button_topic_locked', 'FORUM_LOCKED') : $user->img('button_topic_new', 'POST_NEW_TOPIC'),
557 'QUOTE_IMG' => $user->img('icon_post_quote', 'REPLY_WITH_QUOTE'),
558 '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'),
559 'EDIT_IMG' => $user->img('icon_post_edit', 'EDIT_POST'),
560 'DELETE_IMG' => $user->img('icon_post_delete', 'DELETE_POST'),
561 'INFO_IMG' => $user->img('icon_post_info', 'VIEW_INFO'),
562 'PROFILE_IMG' => $user->img('icon_user_profile', 'READ_PROFILE'),
563 'SEARCH_IMG' => $user->img('icon_user_search', 'SEARCH_USER_POSTS'),
564 'PM_IMG' => $user->img('icon_contact_pm', 'SEND_PRIVATE_MESSAGE'),
565 'EMAIL_IMG' => $user->img('icon_contact_email', 'SEND_EMAIL'),
566 'WWW_IMG' => $user->img('icon_contact_www', 'VISIT_WEBSITE'),
567 'ICQ_IMG' => $user->img('icon_contact_icq', 'ICQ'),
568 'AIM_IMG' => $user->img('icon_contact_aim', 'AIM'),
569 'MSN_IMG' => $user->img('icon_contact_msnm', 'MSNM'),
570 'YIM_IMG' => $user->img('icon_contact_yahoo', 'YIM'),
571 'JABBER_IMG' => $user->img('icon_contact_jabber', 'JABBER') ,
572 'REPORT_IMG' => $user->img('icon_post_report', 'REPORT_POST'),
573 'REPORTED_IMG' => $user->img('icon_topic_reported', 'POST_REPORTED'),
574 'UNAPPROVED_IMG' => $user->img('icon_topic_unapproved', 'POST_UNAPPROVED'),
575 'WARN_IMG' => $user->img('icon_user_warn', 'WARN_USER'),
577 'S_IS_LOCKED' =>($topic_data['topic_status'] == ITEM_UNLOCKED) ? false : true,
578 'S_SELECT_SORT_DIR' => $s_sort_dir,
579 'S_SELECT_SORT_KEY' => $s_sort_key,
580 'S_SELECT_SORT_DAYS' => $s_limit_days,
581 'S_SINGLE_MODERATOR' => (!empty($forum_moderators[$forum_id]) && sizeof($forum_moderators[$forum_id]) > 1) ? false : true,
582 'S_TOPIC_ACTION' => append_sid('viewtopic', "f=$forum_id&amp;t=$topic_id&amp;start=$start"),
583 'S_TOPIC_MOD' => ($topic_mod != '') ? '<select name="action" id="quick-mod-select">' . $topic_mod . '</select>' : '',
584 '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),
586 'S_VIEWTOPIC' => true,
587 'S_DISPLAY_SEARCHBOX' => ($auth->acl_get('u_search') && $auth->acl_get('f_search', $forum_id) && $config['load_search']) ? true : false,
588 'S_SEARCHBOX_ACTION' => append_sid('search', 't=' . $topic_id),
590 'S_DISPLAY_POST_INFO' => ($topic_data['forum_type'] == FORUM_POST && ($auth->acl_get('f_post', $forum_id) || $user->data['user_id'] == ANONYMOUS)) ? true : false,
591 'S_DISPLAY_REPLY_INFO' => ($topic_data['forum_type'] == FORUM_POST && ($auth->acl_get('f_reply', $forum_id) || $user->data['user_id'] == ANONYMOUS)) ? true : false,
593 'U_TOPIC' => "{$server_path}viewtopic." . PHP_EXT . "?f=$forum_id&amp;t=$topic_id",
594 'U_FORUM' => $server_path,
595 'U_VIEW_TOPIC' => $viewtopic_url,
596 'U_VIEW_FORUM' => append_sid('viewforum', 'f=' . $forum_id),
597 'U_VIEW_OLDER_TOPIC' => append_sid('viewtopic', "f=$forum_id&amp;t=$topic_id&amp;view=previous"),
598 'U_VIEW_NEWER_TOPIC' => append_sid('viewtopic', "f=$forum_id&amp;t=$topic_id&amp;view=next"),
599 'U_PRINT_TOPIC' => ($auth->acl_get('f_print', $forum_id)) ? $viewtopic_url . '&amp;view=print' : '',
600 'U_EMAIL_TOPIC' => ($auth->acl_get('f_email', $forum_id) && $config['email_enable']) ? append_sid('memberlist', "mode=email&amp;t=$topic_id") : '',
602 'U_WATCH_TOPIC' => $s_watching_topic['link'],
603 'L_WATCH_TOPIC' => $s_watching_topic['title'],
604 'S_WATCHING_TOPIC' => $s_watching_topic['is_watching'],
606 'U_BOOKMARK_TOPIC' => ($user->data['is_registered'] && $config['allow_bookmarks']) ? $viewtopic_url . '&amp;bookmark=1' : '',
607 'L_BOOKMARK_TOPIC' => ($user->data['is_registered'] && $config['allow_bookmarks'] && $topic_data['bookmarked']) ? $user->lang['BOOKMARK_TOPIC_REMOVE'] : $user->lang['BOOKMARK_TOPIC'],
609 '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") : '',
610 '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") : '',
611 '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") : '')
614 // Does this topic contain a poll?
615 if (!empty($topic_data['poll_start']))
617 $sql = 'SELECT o.*, p.bbcode_bitfield, p.bbcode_uid
618 FROM ' . POLL_OPTIONS_TABLE . ' o, ' . POSTS_TABLE . " p
619 WHERE o.topic_id = $topic_id
620 AND p.post_id = {$topic_data['topic_first_post_id']}
621 AND p.topic_id = o.topic_id
622 ORDER BY o.poll_option_id";
623 $result = $db->sql_query($sql);
625 $poll_info = array();
626 while ($row = $db->sql_fetchrow($result))
628 $poll_info[] = $row;
630 $db->sql_freeresult($result);
632 $cur_voted_id = array();
633 if ($user->data['is_registered'])
635 $sql = 'SELECT poll_option_id
636 FROM ' . POLL_VOTES_TABLE . '
637 WHERE topic_id = ' . $topic_id . '
638 AND vote_user_id = ' . $user->data['user_id'];
639 $result = $db->sql_query($sql);
641 while ($row = $db->sql_fetchrow($result))
643 $cur_voted_id[] = $row['poll_option_id'];
645 $db->sql_freeresult($result);
647 else
649 // Cookie based guest tracking ... I don't like this but hum ho
650 // it's oft requested. This relies on "nice" users who don't feel
651 // the need to delete cookies to mess with results.
652 if (isset($_COOKIE[$config['cookie_name'] . '_poll_' . $topic_id]))
654 $cur_voted_id = explode(',', $_COOKIE[$config['cookie_name'] . '_poll_' . $topic_id]);
655 $cur_voted_id = array_map('intval', $cur_voted_id);
659 $s_can_vote = (((!sizeof($cur_voted_id) && $auth->acl_get('f_vote', $forum_id)) ||
660 ($auth->acl_get('f_votechg', $forum_id) && $topic_data['poll_vote_change'])) &&
661 (($topic_data['poll_length'] != 0 && $topic_data['poll_start'] + $topic_data['poll_length'] > time()) || $topic_data['poll_length'] == 0) &&
662 $topic_data['topic_status'] != ITEM_LOCKED &&
663 $topic_data['forum_status'] != ITEM_LOCKED) ? true : false;
664 $s_display_results = (!$s_can_vote || ($s_can_vote && sizeof($cur_voted_id)) || $view == 'viewpoll') ? true : false;
666 if ($update && $s_can_vote)
669 if (!sizeof($voted_id) || sizeof($voted_id) > $topic_data['poll_max_options'] || in_array(VOTE_CONVERTED, $cur_voted_id))
671 $redirect_url = append_sid('viewtopic', "f=$forum_id&amp;t=$topic_id&amp;start=$start");
673 meta_refresh(5, $redirect_url);
674 if (!sizeof($voted_id))
676 $message = 'NO_VOTE_OPTION';
678 else if (sizeof($voted_id) > $topic_data['poll_max_options'])
680 $message = 'TOO_MANY_VOTE_OPTIONS';
682 else
684 $message = 'VOTE_CONVERTED';
687 $message = $user->lang[$message] . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $redirect_url . '">', '</a>');
688 trigger_error($message);
691 foreach ($voted_id as $option)
693 if (in_array($option, $cur_voted_id))
695 continue;
698 $sql = 'UPDATE ' . POLL_OPTIONS_TABLE . '
699 SET poll_option_total = poll_option_total + 1
700 WHERE poll_option_id = ' . (int) $option . '
701 AND topic_id = ' . (int) $topic_id;
702 $db->sql_query($sql);
704 if ($user->data['is_registered'])
706 $sql_ary = array(
707 'topic_id' => (int) $topic_id,
708 'poll_option_id' => (int) $option,
709 'vote_user_id' => (int) $user->data['user_id'],
710 'vote_user_ip' => (string) $user->ip,
713 $sql = 'INSERT INTO ' . POLL_VOTES_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
714 $db->sql_query($sql);
718 foreach ($cur_voted_id as $option)
720 if (!in_array($option, $voted_id))
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);
728 if ($user->data['is_registered'])
730 $sql = 'DELETE FROM ' . POLL_VOTES_TABLE . '
731 WHERE topic_id = ' . (int) $topic_id . '
732 AND poll_option_id = ' . (int) $option . '
733 AND vote_user_id = ' . (int) $user->data['user_id'];
734 $db->sql_query($sql);
739 if ($user->data['user_id'] == ANONYMOUS && !$user->data['is_bot'])
741 $user->set_cookie('poll_' . $topic_id, implode(',', $voted_id), time() + 31536000);
744 $sql = 'UPDATE ' . TOPICS_TABLE . '
745 SET poll_last_vote = ' . time() . "
746 WHERE topic_id = $topic_id";
747 //, topic_last_post_time = ' . time() . " -- for bumping topics with new votes, ignore for now
748 $db->sql_query($sql);
750 $redirect_url = append_sid('viewtopic', "f=$forum_id&amp;t=$topic_id&amp;start=$start");
752 meta_refresh(5, $redirect_url);
753 trigger_error($user->lang['VOTE_SUBMITTED'] . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $redirect_url . '">', '</a>'));
756 $poll_total = 0;
757 foreach ($poll_info as $poll_option)
759 $poll_total += $poll_option['poll_option_total'];
762 if ($poll_info[0]['bbcode_bitfield'])
764 $poll_bbcode = new bbcode();
766 else
768 $poll_bbcode = false;
771 for ($i = 0, $size = sizeof($poll_info); $i < $size; $i++)
773 $poll_info[$i]['poll_option_text'] = censor_text($poll_info[$i]['poll_option_text']);
775 if ($poll_bbcode !== false)
777 $poll_bbcode->bbcode_second_pass($poll_info[$i]['poll_option_text'], $poll_info[$i]['bbcode_uid'], $poll_option['bbcode_bitfield']);
780 $poll_info[$i]['poll_option_text'] = bbcode_nl2br($poll_info[$i]['poll_option_text']);
781 $poll_info[$i]['poll_option_text'] = smiley_text($poll_info[$i]['poll_option_text']);
784 $topic_data['poll_title'] = censor_text($topic_data['poll_title']);
786 if ($poll_bbcode !== false)
788 $poll_bbcode->bbcode_second_pass($topic_data['poll_title'], $poll_info[0]['bbcode_uid'], $poll_info[0]['bbcode_bitfield']);
791 $topic_data['poll_title'] = bbcode_nl2br($topic_data['poll_title']);
792 $topic_data['poll_title'] = smiley_text($topic_data['poll_title']);
794 unset($poll_bbcode);
796 foreach ($poll_info as $poll_option)
798 $option_pct = ($poll_total > 0) ? $poll_option['poll_option_total'] / $poll_total : 0;
799 $option_pct_txt = sprintf("%.1d%%", ($option_pct * 100));
801 $template->assign_block_vars('poll_option', array(
802 'POLL_OPTION_ID' => $poll_option['poll_option_id'],
803 'POLL_OPTION_CAPTION' => $poll_option['poll_option_text'],
804 'POLL_OPTION_RESULT' => $poll_option['poll_option_total'],
805 'POLL_OPTION_PERCENT' => $option_pct_txt,
806 'POLL_OPTION_PCT' => round($option_pct * 100),
807 'POLL_OPTION_IMG' => $user->img('poll_center', $option_pct_txt, round($option_pct * 250)),
808 'POLL_OPTION_VOTED' => (in_array($poll_option['poll_option_id'], $cur_voted_id)) ? true : false)
812 $poll_end = $topic_data['poll_length'] + $topic_data['poll_start'];
814 $template->assign_vars(array(
815 'POLL_QUESTION' => $topic_data['poll_title'],
816 'TOTAL_VOTES' => $poll_total,
817 'POLL_LEFT_CAP_IMG' => $user->img('poll_left'),
818 'POLL_RIGHT_CAP_IMG'=> $user->img('poll_right'),
820 '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']),
821 'L_POLL_LENGTH' => ($topic_data['poll_length']) ? sprintf($user->lang[($poll_end > time()) ? 'POLL_RUN_TILL' : 'POLL_ENDED_AT'], $user->format_date($poll_end)) : '',
823 'S_HAS_POLL' => true,
824 'S_CAN_VOTE' => $s_can_vote,
825 'S_DISPLAY_RESULTS' => $s_display_results,
826 'S_IS_MULTI_CHOICE' => ($topic_data['poll_max_options'] > 1) ? true : false,
827 'S_POLL_ACTION' => $viewtopic_url,
829 'U_VIEW_RESULTS' => $viewtopic_url . '&amp;view=viewpoll')
832 unset($poll_end, $poll_info, $voted_id);
835 // If the user is trying to reach the second half of the topic, fetch it starting from the end
836 $store_reverse = false;
837 $sql_limit = $config['posts_per_page'];
839 if ($start > $total_posts / 2)
841 $store_reverse = true;
843 if ($start + $config['posts_per_page'] > $total_posts)
845 $sql_limit = min($config['posts_per_page'], max(1, $total_posts - $start));
848 // Select the sort order
849 $sql_sort_order = $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'ASC' : 'DESC');
850 $sql_start = max(0, $total_posts - $sql_limit - $start);
852 else
854 // Select the sort order
855 $sql_sort_order = $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'DESC' : 'ASC');
856 $sql_start = $start;
859 // Container for user details, only process once
860 $post_list = $user_cache = $id_cache = $attachments = $attach_list = $rowset = $update_count = $post_edit_list = array();
861 $has_attachments = $display_notice = false;
862 $bbcode_bitfield = '';
863 $i = $i_total = 0;
865 // Go ahead and pull all data for this topic
866 $sql = 'SELECT p.post_id
867 FROM ' . POSTS_TABLE . ' p' . (($sort_by_sql[$sort_key][0] == 'u') ? ', ' . USERS_TABLE . ' u': '') . "
868 WHERE p.topic_id = $topic_id
869 " . ((!$auth->acl_get('m_approve', $forum_id)) ? 'AND p.post_approved = 1' : '') . "
870 " . (($sort_by_sql[$sort_key][0] == 'u') ? 'AND u.user_id = p.poster_id': '') . "
871 $limit_posts_time
872 ORDER BY $sql_sort_order";
873 $result = $db->sql_query_limit($sql, $sql_limit, $sql_start);
875 $i = ($store_reverse) ? $sql_limit - 1 : 0;
876 while ($row = $db->sql_fetchrow($result))
878 $post_list[$i] = $row['post_id'];
879 ($store_reverse) ? $i-- : $i++;
881 $db->sql_freeresult($result);
883 if (!sizeof($post_list))
885 if ($sort_days)
887 trigger_error('NO_POSTS_TIME_FRAME');
889 else
891 trigger_error('NO_TOPIC');
895 // Holding maximum post time for marking topic read
896 // We need to grab it because we do reverse ordering sometimes
897 $max_post_time = 0;
899 $sql = $db->sql_build_query('SELECT', array(
900 'SELECT' => 'u.*, z.friend, z.foe, p.*',
902 'FROM' => array(
903 USERS_TABLE => 'u',
904 POSTS_TABLE => 'p',
907 'LEFT_JOIN' => array(
908 array(
909 'FROM' => array(ZEBRA_TABLE => 'z'),
910 'ON' => 'z.user_id = ' . $user->data['user_id'] . ' AND z.zebra_id = p.poster_id'
914 'WHERE' => $db->sql_in_set('p.post_id', $post_list) . '
915 AND u.user_id = p.poster_id'
918 $result = $db->sql_query($sql);
920 $now = getdate(time() + $user->timezone + $user->dst - date('Z'));
922 // Posts are stored in the $rowset array while $attach_list, $user_cache
923 // and the global bbcode_bitfield are built
924 while ($row = $db->sql_fetchrow($result))
926 // Set max_post_time
927 if ($row['post_time'] > $max_post_time)
929 $max_post_time = $row['post_time'];
932 $poster_id = $row['poster_id'];
934 // Does post have an attachment? If so, add it to the list
935 if ($row['post_attachment'] && $config['allow_attachments'])
937 $attach_list[] = $row['post_id'];
939 if ($row['post_approved'])
941 $has_attachments = true;
945 $rowset[$row['post_id']] = array(
946 'hide_post' => ($row['foe'] && ($view != 'show' || $post_id != $row['post_id'])) ? true : false,
948 'post_id' => $row['post_id'],
949 'post_time' => $row['post_time'],
950 'user_id' => $row['user_id'],
951 'username' => $row['username'],
952 'user_colour' => $row['user_colour'],
953 'topic_id' => $row['topic_id'],
954 'forum_id' => $row['forum_id'],
955 'post_subject' => $row['post_subject'],
956 'post_edit_count' => $row['post_edit_count'],
957 'post_edit_time' => $row['post_edit_time'],
958 'post_edit_reason' => $row['post_edit_reason'],
959 'post_edit_user' => $row['post_edit_user'],
961 // Make sure the icon actually exists
962 'icon_id' => (isset($icons[$row['icon_id']]['img'], $icons[$row['icon_id']]['height'], $icons[$row['icon_id']]['width'])) ? $row['icon_id'] : 0,
963 'post_attachment' => $row['post_attachment'],
964 'post_approved' => $row['post_approved'],
965 'post_reported' => $row['post_reported'],
966 'post_username' => $row['post_username'],
967 'post_text' => $row['post_text'],
968 'bbcode_uid' => $row['bbcode_uid'],
969 'bbcode_bitfield' => $row['bbcode_bitfield'],
970 'enable_smilies' => $row['enable_smilies'],
971 'enable_sig' => $row['enable_sig'],
972 'friend' => $row['friend'],
973 'foe' => $row['foe'],
976 // Define the global bbcode bitfield, will be used to load bbcodes
977 $bbcode_bitfield = $bbcode_bitfield | base64_decode($row['bbcode_bitfield']);
979 // Is a signature attached? Are we going to display it?
980 if ($row['enable_sig'] && $config['allow_sig'] && $user->optionget('viewsigs'))
982 $bbcode_bitfield = $bbcode_bitfield | base64_decode($row['user_sig_bbcode_bitfield']);
985 // Cache various user specific data ... so we don't have to recompute
986 // this each time the same user appears on this page
987 if (!isset($user_cache[$poster_id]))
989 if ($poster_id == ANONYMOUS)
991 $user_cache[$poster_id] = array(
992 'joined' => '',
993 'posts' => '',
994 'from' => '',
996 'sig' => '',
997 'sig_bbcode_uid' => '',
998 'sig_bbcode_bitfield' => '',
1000 'online' => false,
1001 'avatar' => '',
1002 'rank_title' => '',
1003 'rank_image' => '',
1004 'rank_image_src' => '',
1005 'sig' => '',
1006 'profile' => '',
1007 'pm' => '',
1008 'email' => '',
1009 'www' => '',
1010 'icq_status_img' => '',
1011 'icq' => '',
1012 'aim' => '',
1013 'msn' => '',
1014 'yim' => '',
1015 'jabber' => '',
1016 'search' => '',
1017 'age' => '',
1019 'username' => $row['username'],
1020 'user_colour' => $row['user_colour'],
1022 'warnings' => 0,
1023 'allow_pm' => 0,
1026 else
1028 $user_sig = '';
1030 // We add the signature to every posters entry because enable_sig is post dependant
1031 if ($row['user_sig'] && $config['allow_sig'] && $user->optionget('viewsigs'))
1033 $user_sig = $row['user_sig'];
1036 $id_cache[] = $poster_id;
1038 $user_cache[$poster_id] = array(
1039 'joined' => $user->format_date($row['user_regdate']),
1040 'posts' => $row['user_posts'],
1041 'warnings' => (isset($row['user_warnings'])) ? $row['user_warnings'] : 0,
1042 'from' => (!empty($row['user_from'])) ? $row['user_from'] : '',
1044 'sig' => $user_sig,
1045 'sig_bbcode_uid' => (!empty($row['user_sig_bbcode_uid'])) ? $row['user_sig_bbcode_uid'] : '',
1046 'sig_bbcode_bitfield' => (!empty($row['user_sig_bbcode_bitfield'])) ? $row['user_sig_bbcode_bitfield'] : '',
1048 'viewonline' => $row['user_allow_viewonline'],
1049 'allow_pm' => $row['user_allow_pm'],
1051 'avatar' => ($user->optionget('viewavatars')) ? get_user_avatar($row['user_avatar'], $row['user_avatar_type'], $row['user_avatar_width'], $row['user_avatar_height']) : '',
1052 'age' => '',
1054 'rank_title' => '',
1055 'rank_image' => '',
1056 'rank_image_src' => '',
1058 'username' => $row['username'],
1059 'user_colour' => $row['user_colour'],
1061 'online' => false,
1062 'profile' => append_sid('memberlist', "mode=viewprofile&amp;u=$poster_id"),
1063 'www' => $row['user_website'],
1064 'aim' => ($row['user_aim'] && $auth->acl_get('u_sendim')) ? append_sid('memberlist', "mode=contact&amp;action=aim&amp;u=$poster_id") : '',
1065 'msn' => ($row['user_msnm'] && $auth->acl_get('u_sendim')) ? append_sid('memberlist', "mode=contact&amp;action=msnm&amp;u=$poster_id") : '',
1066 'yim' => ($row['user_yim']) ? 'http://edit.yahoo.com/config/send_webmesg?.target=' . urlencode($row['user_yim']) . '&amp;.src=pg' : '',
1067 'jabber' => ($row['user_jabber'] && $auth->acl_get('u_sendim')) ? append_sid('memberlist', "mode=contact&amp;action=jabber&amp;u=$poster_id") : '',
1068 'search' => ($auth->acl_get('u_search')) ? append_sid('search', 'search_author=' . urlencode($row['username']) .'&amp;showresults=posts') : '',
1071 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']);
1073 if (!empty($row['user_allow_viewemail']) || $auth->acl_get('a_email'))
1075 $user_cache[$poster_id]['email'] = ($config['board_email_form'] && $config['email_enable']) ? append_sid('memberlist', "mode=email&amp;u=$poster_id") : (($config['board_hide_emails'] && !$auth->acl_get('a_email')) ? '' : 'mailto:' . $row['user_email']);
1077 else
1079 $user_cache[$poster_id]['email'] = '';
1082 if (!empty($row['user_icq']))
1084 $user_cache[$poster_id]['icq'] = 'http://www.icq.com/people/webmsg.php?to=' . $row['user_icq'];
1085 $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="" />';
1087 else
1089 $user_cache[$poster_id]['icq_status_img'] = '';
1090 $user_cache[$poster_id]['icq'] = '';
1093 if ($config['allow_birthdays'] && !empty($row['user_birthday']))
1095 list($bday_day, $bday_month, $bday_year) = array_map('intval', explode('-', $row['user_birthday']));
1097 if ($bday_year)
1099 $diff = $now['mon'] - $bday_month;
1100 if ($diff == 0)
1102 $diff = ($now['mday'] - $bday_day < 0) ? 1 : 0;
1104 else
1106 $diff = ($diff < 0) ? 1 : 0;
1109 $user_cache[$poster_id]['age'] = (int) ($now['year'] - $bday_year - $diff);
1115 $db->sql_freeresult($result);
1117 // Load custom profile fields
1118 if ($config['load_cpf_viewtopic'])
1120 include(PHPBB_ROOT_PATH . 'includes/functions_profile_fields.' . PHP_EXT);
1121 $cp = new custom_profile();
1123 // Grab all profile fields from users in id cache for later use - similar to the poster cache
1124 $profile_fields_cache = $cp->generate_profile_fields_template('grab', $id_cache);
1127 // Generate online information for user
1128 if ($config['load_onlinetrack'] && sizeof($id_cache))
1130 $sql = 'SELECT session_user_id, MAX(session_time) as online_time, MIN(session_viewonline) AS viewonline
1131 FROM ' . SESSIONS_TABLE . '
1132 WHERE ' . $db->sql_in_set('session_user_id', $id_cache) . '
1133 GROUP BY session_user_id';
1134 $result = $db->sql_query($sql);
1136 $update_time = $config['load_online_time'] * 60;
1137 while ($row = $db->sql_fetchrow($result))
1139 $user_cache[$row['session_user_id']]['online'] = (time() - $update_time < $row['online_time'] && (($row['viewonline']) || $auth->acl_get('u_viewonline'))) ? true : false;
1141 $db->sql_freeresult($result);
1143 unset($id_cache);
1145 // Pull attachment data
1146 if (sizeof($attach_list))
1148 if ($auth->acl_get('u_download') && $auth->acl_get('f_download', $forum_id))
1150 $sql = 'SELECT *
1151 FROM ' . ATTACHMENTS_TABLE . '
1152 WHERE ' . $db->sql_in_set('post_msg_id', $attach_list) . '
1153 AND in_message = 0
1154 ORDER BY filetime DESC, post_msg_id ASC';
1155 $result = $db->sql_query($sql);
1157 while ($row = $db->sql_fetchrow($result))
1159 $attachments[$row['post_msg_id']][] = $row;
1161 $db->sql_freeresult($result);
1163 // No attachments exist, but post table thinks they do so go ahead and reset post_attach flags
1164 if (!sizeof($attachments))
1166 $sql = 'UPDATE ' . POSTS_TABLE . '
1167 SET post_attachment = 0
1168 WHERE ' . $db->sql_in_set('post_id', $attach_list);
1169 $db->sql_query($sql);
1171 // We need to update the topic indicator too if the complete topic is now without an attachment
1172 if (sizeof($rowset) != $total_posts)
1174 // Not all posts are displayed so we query the db to find if there's any attachment for this topic
1175 $sql = 'SELECT a.post_msg_id as post_id
1176 FROM ' . ATTACHMENTS_TABLE . ' a, ' . POSTS_TABLE . " p
1177 WHERE p.topic_id = $topic_id
1178 AND p.post_approved = 1
1179 AND p.topic_id = a.topic_id";
1180 $result = $db->sql_query_limit($sql, 1);
1181 $row = $db->sql_fetchrow($result);
1182 $db->sql_freeresult($result);
1184 if (!$row)
1186 $sql = 'UPDATE ' . TOPICS_TABLE . "
1187 SET topic_attachment = 0
1188 WHERE topic_id = $topic_id";
1189 $db->sql_query($sql);
1192 else
1194 $sql = 'UPDATE ' . TOPICS_TABLE . "
1195 SET topic_attachment = 0
1196 WHERE topic_id = $topic_id";
1197 $db->sql_query($sql);
1200 else if ($has_attachments && !$topic_data['topic_attachment'])
1202 // Topic has approved attachments but its flag is wrong
1203 $sql = 'UPDATE ' . TOPICS_TABLE . "
1204 SET topic_attachment = 1
1205 WHERE topic_id = $topic_id";
1206 $db->sql_query($sql);
1208 $topic_data['topic_attachment'] = 1;
1211 else
1213 $display_notice = true;
1217 // Instantiate BBCode if need be
1218 if ($bbcode_bitfield !== '')
1220 $bbcode = new bbcode(base64_encode($bbcode_bitfield));
1223 $i_total = sizeof($rowset) - 1;
1224 $prev_post_id = '';
1226 $template->assign_vars(array(
1227 'S_NUM_POSTS' => sizeof($post_list))
1230 // Output the posts
1231 $first_unread = $post_unread = false;
1232 for ($i = 0, $end = sizeof($post_list); $i < $end; ++$i)
1234 // A non-existing rowset only happens if there was no user present for the entered poster_id
1235 // This could be a broken posts table.
1236 if (!isset($rowset[$post_list[$i]]))
1238 continue;
1241 $row =& $rowset[$post_list[$i]];
1242 $poster_id = $row['user_id'];
1244 // End signature parsing, only if needed
1245 if ($user_cache[$poster_id]['sig'] && $row['enable_sig'] && empty($user_cache[$poster_id]['sig_parsed']))
1247 $user_cache[$poster_id]['sig'] = censor_text($user_cache[$poster_id]['sig']);
1249 if ($user_cache[$poster_id]['sig_bbcode_bitfield'])
1251 $bbcode->bbcode_second_pass($user_cache[$poster_id]['sig'], $user_cache[$poster_id]['sig_bbcode_uid'], $user_cache[$poster_id]['sig_bbcode_bitfield']);
1254 $user_cache[$poster_id]['sig'] = bbcode_nl2br($user_cache[$poster_id]['sig']);
1255 $user_cache[$poster_id]['sig'] = smiley_text($user_cache[$poster_id]['sig']);
1256 $user_cache[$poster_id]['sig_parsed'] = true;
1259 // Parse the message and subject
1260 $message = censor_text($row['post_text']);
1262 // Second parse bbcode here
1263 if ($row['bbcode_bitfield'])
1265 $bbcode->bbcode_second_pass($message, $row['bbcode_uid'], $row['bbcode_bitfield']);
1268 $message = bbcode_nl2br($message);
1269 $message = smiley_text($message);
1271 if (!empty($attachments[$row['post_id']]))
1273 parse_attachments($forum_id, $message, $attachments[$row['post_id']], $update_count);
1276 // Replace naughty words such as farty pants
1277 $row['post_subject'] = censor_text($row['post_subject']);
1279 // Highlight active words (primarily for search)
1280 if ($highlight_match)
1282 $message = preg_replace('#(?!<.*)(?<!\w)(' . $highlight_match . ')(?!\w|[^<>]*(?:</s(?:cript|tyle))?>)#is', '<span class="posthilit">\1</span>', $message);
1283 $row['post_subject'] = preg_replace('#(?!<.*)(?<!\w)(' . $highlight_match . ')(?!\w|[^<>]*(?:</s(?:cript|tyle))?>)#is', '<span class="posthilit">\1</span>', $row['post_subject']);
1286 // Editing information
1287 if (($row['post_edit_count'] && $config['display_last_edited']) || $row['post_edit_reason'])
1289 // Get usernames for all following posts if not already stored
1290 if (!sizeof($post_edit_list) && ($row['post_edit_reason'] || ($row['post_edit_user'] && !isset($user_cache[$row['post_edit_user']]))))
1292 // Remove all post_ids already parsed (we do not have to check them)
1293 $post_storage_list = (!$store_reverse) ? array_slice($post_list, $i) : array_slice(array_reverse($post_list), $i);
1295 $sql = 'SELECT DISTINCT u.user_id, u.username, u.user_colour
1296 FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
1297 WHERE ' . $db->sql_in_set('p.post_id', $post_storage_list) . '
1298 AND p.post_edit_count <> 0
1299 AND p.post_edit_user <> 0
1300 AND p.post_edit_user = u.user_id';
1301 $result2 = $db->sql_query($sql);
1302 while ($user_edit_row = $db->sql_fetchrow($result2))
1304 $post_edit_list[$user_edit_row['user_id']] = $user_edit_row;
1306 $db->sql_freeresult($result2);
1308 unset($post_storage_list);
1311 $l_edit_time_total = ($row['post_edit_count'] == 1) ? $user->lang['EDITED_TIME_TOTAL'] : $user->lang['EDITED_TIMES_TOTAL'];
1313 if ($row['post_edit_reason'])
1315 // User having edited the post also being the post author?
1316 if (!$row['post_edit_user'] || $row['post_edit_user'] == $poster_id)
1318 $display_username = get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']);
1320 else
1322 $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']);
1325 $l_edited_by = sprintf($l_edit_time_total, $display_username, $user->format_date($row['post_edit_time']), $row['post_edit_count']);
1327 else
1329 if ($row['post_edit_user'] && !isset($user_cache[$row['post_edit_user']]))
1331 $user_cache[$row['post_edit_user']] = $post_edit_list[$row['post_edit_user']];
1334 // User having edited the post also being the post author?
1335 if (!$row['post_edit_user'] || $row['post_edit_user'] == $poster_id)
1337 $display_username = get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']);
1339 else
1341 $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']);
1344 $l_edited_by = sprintf($l_edit_time_total, $display_username, $user->format_date($row['post_edit_time']), $row['post_edit_count']);
1347 else
1349 $l_edited_by = '';
1352 // Bump information
1353 if ($topic_data['topic_bumped'] && $row['post_id'] == $topic_data['topic_last_post_id'] && isset($user_cache[$topic_data['topic_bumper']]) )
1355 // It is safe to grab the username from the user cache array, we are at the last
1356 // post and only the topic poster and last poster are allowed to bump.
1357 // Admins and mods are bound to the above rules too...
1358 $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']));
1360 else
1362 $l_bumped_by = '';
1365 $cp_row = array();
1368 if ($config['load_cpf_viewtopic'])
1370 $cp_row = (isset($profile_fields_cache[$poster_id])) ? $cp->generate_profile_fields_template('show', false, $profile_fields_cache[$poster_id]) : array();
1373 $post_unread = (isset($topic_tracking_info[$topic_id]) && $row['post_time'] > $topic_tracking_info[$topic_id]) ? true : false;
1375 $s_first_unread = false;
1376 if (!$first_unread && $post_unread)
1378 $s_first_unread = $first_unread = true;
1382 $postrow = array(
1383 'POST_AUTHOR_FULL' => get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
1384 'POST_AUTHOR_COLOUR' => get_username_string('colour', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
1385 'POST_AUTHOR' => get_username_string('username', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
1386 'U_POST_AUTHOR' => get_username_string('profile', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
1388 'RANK_TITLE' => $user_cache[$poster_id]['rank_title'],
1389 'RANK_IMG' => $user_cache[$poster_id]['rank_image'],
1390 'RANK_IMG_SRC' => $user_cache[$poster_id]['rank_image_src'],
1391 'POSTER_JOINED' => $user_cache[$poster_id]['joined'],
1392 'POSTER_POSTS' => $user_cache[$poster_id]['posts'],
1393 'POSTER_FROM' => $user_cache[$poster_id]['from'],
1394 'POSTER_AVATAR' => $user_cache[$poster_id]['avatar'],
1395 'POSTER_WARNINGS' => $user_cache[$poster_id]['warnings'],
1396 'POSTER_AGE' => $user_cache[$poster_id]['age'],
1398 'POST_DATE' => $user->format_date($row['post_time']),
1399 'POST_SUBJECT' => $row['post_subject'],
1400 'MESSAGE' => $message,
1401 'SIGNATURE' => ($row['enable_sig']) ? $user_cache[$poster_id]['sig'] : '',
1402 'EDITED_MESSAGE' => $l_edited_by,
1403 'EDIT_REASON' => $row['post_edit_reason'],
1404 'BUMPED_MESSAGE' => $l_bumped_by,
1406 'MINI_POST_IMG' => ($post_unread) ? $user->img('icon_post_target_unread', 'NEW_POST') : $user->img('icon_post_target', 'POST'),
1407 'POST_ICON_IMG' => ($topic_data['enable_icons'] && !empty($row['icon_id'])) ? $icons[$row['icon_id']]['img'] : '',
1408 'POST_ICON_IMG_WIDTH' => ($topic_data['enable_icons'] && !empty($row['icon_id'])) ? $icons[$row['icon_id']]['width'] : '',
1409 'POST_ICON_IMG_HEIGHT' => ($topic_data['enable_icons'] && !empty($row['icon_id'])) ? $icons[$row['icon_id']]['height'] : '',
1410 'ICQ_STATUS_IMG' => $user_cache[$poster_id]['icq_status_img'],
1411 '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')),
1412 'S_ONLINE' => ($poster_id == ANONYMOUS || !$config['load_onlinetrack']) ? false : (($user_cache[$poster_id]['online']) ? true : false),
1414 '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('posting', "mode=edit&amp;f=$forum_id&amp;p={$row['post_id']}") : ''),
1415 'U_QUOTE' => ($auth->acl_get('f_reply', $forum_id)) ? append_sid('posting', "mode=quote&amp;f=$forum_id&amp;p={$row['post_id']}") : '',
1416 'U_INFO' => ($auth->acl_get('m_info', $forum_id)) ? append_sid('mcp', "i=main&amp;mode=post_details&amp;f=$forum_id&amp;p=" . $row['post_id'], true, $user->session_id) : '',
1417 '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('posting', "mode=delete&amp;f=$forum_id&amp;p={$row['post_id']}") : ''),
1419 'U_PROFILE' => $user_cache[$poster_id]['profile'],
1420 'U_SEARCH' => $user_cache[$poster_id]['search'],
1421 '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('ucp', 'i=pm&amp;mode=compose&amp;action=quotepost&amp;p=' . $row['post_id']) : '',
1422 'U_EMAIL' => $user_cache[$poster_id]['email'],
1423 'U_WWW' => $user_cache[$poster_id]['www'],
1424 'U_ICQ' => $user_cache[$poster_id]['icq'],
1425 'U_AIM' => $user_cache[$poster_id]['aim'],
1426 'U_MSN' => $user_cache[$poster_id]['msn'],
1427 'U_YIM' => $user_cache[$poster_id]['yim'],
1428 'U_JABBER' => $user_cache[$poster_id]['jabber'],
1430 'U_REPORT' => ($auth->acl_get('f_report', $forum_id)) ? append_sid('report', 'f=' . $forum_id . '&amp;p=' . $row['post_id']) : '',
1431 'U_MCP_REPORT' => ($auth->acl_get('m_report', $forum_id)) ? append_sid('mcp', 'i=reports&amp;mode=report_details&amp;f=' . $forum_id . '&amp;p=' . $row['post_id'], true, $user->session_id) : '',
1432 'U_MCP_APPROVE' => ($auth->acl_get('m_approve', $forum_id)) ? append_sid('mcp', 'i=queue&amp;mode=approve_details&amp;f=' . $forum_id . '&amp;p=' . $row['post_id'], true, $user->session_id) : '',
1433 'U_MINI_POST' => append_sid('viewtopic', 'p=' . $row['post_id']) . (($topic_data['topic_type'] == POST_GLOBAL) ? '&amp;f=' . $forum_id : '') . '#p' . $row['post_id'],
1434 'U_NEXT_POST_ID' => ($i < $i_total && isset($rowset[$post_list[$i + 1]])) ? $rowset[$post_list[$i + 1]]['post_id'] : '',
1435 'U_PREV_POST_ID' => $prev_post_id,
1436 'U_NOTES' => ($auth->acl_getf_global('m_')) ? append_sid('mcp', 'i=notes&amp;mode=user_notes&amp;u=' . $poster_id, true, $user->session_id) : '',
1437 'U_WARN' => ($auth->acl_get('m_warn') && $poster_id != $user->data['user_id'] && $poster_id != ANONYMOUS) ? append_sid('mcp', 'i=warn&amp;mode=warn_post&amp;f=' . $forum_id . '&amp;p=' . $row['post_id'], true, $user->session_id) : '',
1439 'POST_ID' => $row['post_id'],
1440 'POSTER_ID' => $poster_id,
1442 'S_HAS_ATTACHMENTS' => (!empty($attachments[$row['post_id']])) ? true : false,
1443 'S_POST_UNAPPROVED' => ($row['post_approved']) ? false : true,
1444 'S_POST_REPORTED' => ($row['post_reported'] && $auth->acl_get('m_report', $forum_id)) ? true : false,
1445 'S_DISPLAY_NOTICE' => $display_notice && $row['post_attachment'],
1446 'S_FRIEND' => ($row['friend']) ? true : false,
1447 'S_UNREAD_POST' => $post_unread,
1448 'S_FIRST_UNREAD' => $s_first_unread,
1449 'S_CUSTOM_FIELDS' => (isset($cp_row['row']) && sizeof($cp_row['row'])) ? true : false,
1450 'S_TOPIC_POSTER' => ($topic_data['topic_poster'] == $poster_id) ? true : false,
1452 'S_IGNORE_POST' => ($row['hide_post']) ? true : false,
1453 '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>') : '',
1456 if (isset($cp_row['row']) && sizeof($cp_row['row']))
1458 $postrow = array_merge($postrow, $cp_row['row']);
1461 // Dump vars into template
1462 $template->assign_block_vars('postrow', $postrow);
1464 if (!empty($cp_row['blockrow']))
1466 foreach ($cp_row['blockrow'] as $field_data)
1468 $template->assign_block_vars('postrow.custom_fields', $field_data);
1472 // Display not already displayed Attachments for this post, we already parsed them. ;)
1473 if (!empty($attachments[$row['post_id']]))
1475 foreach ($attachments[$row['post_id']] as $attachment)
1477 $template->assign_block_vars('postrow.attachment', array(
1478 'DISPLAY_ATTACHMENT' => $attachment)
1483 $prev_post_id = $row['post_id'];
1485 unset($rowset[$post_list[$i]]);
1486 unset($attachments[$row['post_id']]);
1488 unset($rowset, $user_cache);
1490 // Update topic view and if necessary attachment view counters ... but only if this is the first 'page view'
1491 if (isset($user->data['session_page']) && strpos($user->data['session_page'], '&t=' . $topic_id) === false)
1493 $sql = 'UPDATE ' . TOPICS_TABLE . '
1494 SET topic_views = topic_views + 1, topic_last_view_time = ' . time() . "
1495 WHERE topic_id = $topic_id";
1496 $db->sql_query($sql);
1498 // Update the attachment download counts
1499 if (sizeof($update_count))
1501 $sql = 'UPDATE ' . ATTACHMENTS_TABLE . '
1502 SET download_count = download_count + 1
1503 WHERE ' . $db->sql_in_set('attach_id', array_unique($update_count));
1504 $db->sql_query($sql);
1508 // Only mark topic if it's currently unread. Also make sure we do not set topic tracking back if earlier pages are viewed.
1509 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])
1511 markread('topic', $forum_id, $topic_id, $max_post_time);
1513 // Update forum info
1514 $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);
1516 else
1518 $all_marked_read = true;
1521 // If there are absolutely no more unread posts in this forum and unread posts shown, we can savely show the #unread link
1522 if ($all_marked_read)
1524 if ($post_unread)
1526 $template->assign_vars(array(
1527 'U_VIEW_UNREAD_POST' => '#unread',
1530 else if (isset($topic_tracking_info[$topic_id]) && $topic_data['topic_last_post_time'] > $topic_tracking_info[$topic_id])
1532 $template->assign_vars(array(
1533 'U_VIEW_UNREAD_POST' => append_sid('viewtopic', "f=$forum_id&amp;t=$topic_id&amp;view=unread") . '#unread',
1537 else if (!$all_marked_read)
1539 $last_page = ((floor($start / $config['posts_per_page']) + 1) == max(ceil($total_posts / $config['posts_per_page']), 1)) ? true : false;
1541 // What can happen is that we are at the last displayed page. If so, we also display the #unread link based in $post_unread
1542 if ($last_page && $post_unread)
1544 $template->assign_vars(array(
1545 'U_VIEW_UNREAD_POST' => '#unread',
1548 else if (!$last_page)
1550 $template->assign_vars(array(
1551 'U_VIEW_UNREAD_POST' => append_sid('viewtopic', "f=$forum_id&amp;t=$topic_id&amp;view=unread") . '#unread',
1556 // We overwrite $_REQUEST['f'] if there is no forum specified
1557 // to be able to display the correct online list.
1558 // One downside is that the user currently viewing this topic/post is not taken into account.
1559 if (empty($_REQUEST['f']))
1561 $_REQUEST['f'] = $forum_id;
1564 // Output the page
1565 page_header($user->lang['VIEW_TOPIC'] .' - ' . $topic_data['topic_title']);
1567 $template->set_filenames(array(
1568 'body' => ($view == 'print') ? 'viewtopic_print.html' : 'viewtopic_body.html')
1570 make_jumpbox(append_sid('viewforum'), $forum_id);
1572 page_footer();