- * wildcard in a search query should cause \w+ in highlighting, not \w* [Bug #10031]
[phpbb.git] / phpBB / viewtopic.php
blob29e828243515d101c9255169546de9d120f93f32
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 $phpbb_root_path = './';
16 $phpEx = substr(strrchr(__FILE__, '.'), 1);
17 include($phpbb_root_path . 'common.' . $phpEx);
18 include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
19 include($phpbb_root_path . 'includes/bbcode.' . $phpEx);
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 $hilit_words = request_var('hilit', '', true);
42 // Do we have a topic or post id?
43 if (!$topic_id && !$post_id)
45 trigger_error('NO_TOPIC');
48 // Find topic id if user requested a newer or older topic
49 if ($view && !$post_id)
51 if (!$forum_id)
53 $sql = 'SELECT forum_id
54 FROM ' . TOPICS_TABLE . "
55 WHERE topic_id = $topic_id";
56 $result = $db->sql_query($sql);
57 $forum_id = (int) $db->sql_fetchfield('forum_id');
58 $db->sql_freeresult($result);
60 if (!$forum_id)
62 trigger_error('NO_TOPIC');
66 if ($view == 'unread')
68 // Get topic tracking info
69 $topic_tracking_info = get_complete_topic_tracking($forum_id, $topic_id);
71 $topic_last_read = (isset($topic_tracking_info[$topic_id])) ? $topic_tracking_info[$topic_id] : 0;
73 $sql = 'SELECT post_id, topic_id, forum_id
74 FROM ' . POSTS_TABLE . "
75 WHERE topic_id = $topic_id
76 " . (($auth->acl_get('m_approve', $forum_id)) ? '' : 'AND post_approved = 1') . "
77 AND post_time > $topic_last_read
78 ORDER BY post_time ASC";
79 $result = $db->sql_query_limit($sql, 1);
80 $row = $db->sql_fetchrow($result);
81 $db->sql_freeresult($result);
83 if (!$row)
85 $sql = 'SELECT topic_last_post_id as post_id, topic_id, forum_id
86 FROM ' . TOPICS_TABLE . '
87 WHERE topic_id = ' . $topic_id;
88 $result = $db->sql_query($sql);
89 $row = $db->sql_fetchrow($result);
90 $db->sql_freeresult($result);
93 if (!$row)
95 // Setup user environment so we can process lang string
96 $user->setup('viewtopic');
98 trigger_error('NO_TOPIC');
101 $post_id = $row['post_id'];
102 $topic_id = $row['topic_id'];
104 else if ($view == 'next' || $view == 'previous')
106 $sql_condition = ($view == 'next') ? '>' : '<';
107 $sql_ordering = ($view == 'next') ? 'ASC' : 'DESC';
109 $sql = 'SELECT t.topic_id, t.forum_id
110 FROM ' . TOPICS_TABLE . ' t
111 LEFT JOIN ' . TOPICS_TABLE . " t2 ON (t2.topic_id = $topic_id AND t.forum_id = t2.forum_id)
112 WHERE t.topic_last_post_time $sql_condition t2.topic_last_post_time
113 " . (($auth->acl_get('m_approve', $forum_id)) ? '' : 'AND t.topic_approved = 1') . "
114 AND t.topic_moved_id = 0
115 ORDER BY t.topic_last_post_time $sql_ordering";
116 $result = $db->sql_query_limit($sql, 1);
117 $row = $db->sql_fetchrow($result);
118 $db->sql_freeresult($result);
120 if (!$row)
122 $user->setup('viewtopic');
123 trigger_error(($view == 'next') ? 'NO_NEWER_TOPICS' : 'NO_OLDER_TOPICS');
125 else
127 $topic_id = $row['topic_id'];
129 // Check for global announcement correctness?
130 if (!$row['forum_id'] && !$forum_id)
132 trigger_error('NO_TOPIC');
134 else if ($row['forum_id'])
136 $forum_id = $row['forum_id'];
141 // Check for global announcement correctness?
142 if ((!isset($row) || !$row['forum_id']) && !$forum_id)
144 trigger_error('NO_TOPIC');
146 else if (isset($row) && $row['forum_id'])
148 $forum_id = $row['forum_id'];
152 // This rather complex gaggle of code handles querying for topics but
153 // also allows for direct linking to a post (and the calculation of which
154 // page the post is on and the correct display of viewtopic)
155 $sql_array = array(
156 'SELECT' => 't.*, f.*',
158 'FROM' => array(
159 FORUMS_TABLE => 'f',
163 if ($user->data['is_registered'])
165 $sql_array['SELECT'] .= ', tw.notify_status';
166 $sql_array['LEFT_JOIN'] = array();
168 $sql_array['LEFT_JOIN'][] = array(
169 'FROM' => array(TOPICS_WATCH_TABLE => 'tw'),
170 'ON' => 'tw.user_id = ' . $user->data['user_id'] . ' AND t.topic_id = tw.topic_id'
173 if ($config['allow_bookmarks'])
175 $sql_array['SELECT'] .= ', bm.order_id as bookmarked';
176 $sql_array['LEFT_JOIN'][] = array(
177 'FROM' => array(BOOKMARKS_TABLE => 'bm'),
178 'ON' => 'bm.user_id = ' . $user->data['user_id'] . ' AND t.topic_id = bm.topic_id'
182 if ($config['load_db_lastread'])
184 $sql_array['SELECT'] .= ', tt.mark_time, ft.mark_time as forum_mark_time';
186 $sql_array['LEFT_JOIN'][] = array(
187 'FROM' => array(TOPICS_TRACK_TABLE => 'tt'),
188 'ON' => 'tt.user_id = ' . $user->data['user_id'] . ' AND t.topic_id = tt.topic_id'
191 $sql_array['LEFT_JOIN'][] = array(
192 'FROM' => array(FORUMS_TRACK_TABLE => 'ft'),
193 'ON' => 'ft.user_id = ' . $user->data['user_id'] . ' AND t.forum_id = ft.forum_id'
198 if (!$post_id)
200 $sql_array['WHERE'] = "t.topic_id = $topic_id";
202 else
204 $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' : '');
205 $sql_array['FROM'][POSTS_TABLE] = 'p';
208 $sql_array['WHERE'] .= ' AND (f.forum_id = t.forum_id';
210 if (!$forum_id)
212 // If it is a global announcement make sure to set the forum id to a postable forum
213 $sql_array['WHERE'] .= ' OR (t.topic_type = ' . POST_GLOBAL . '
214 AND f.forum_type = ' . FORUM_POST . ')';
216 else
218 $sql_array['WHERE'] .= ' OR (t.topic_type = ' . POST_GLOBAL . "
219 AND f.forum_id = $forum_id)";
222 $sql_array['WHERE'] .= ')';
223 $sql_array['FROM'][TOPICS_TABLE] = 't';
225 // Join to forum table on topic forum_id unless topic forum_id is zero
226 // whereupon we join on the forum_id passed as a parameter ... this
227 // is done so navigation, forum name, etc. remain consistent with where
228 // user clicked to view a global topic
229 $sql = $db->sql_build_query('SELECT', $sql_array);
230 $result = $db->sql_query($sql);
231 $topic_data = $db->sql_fetchrow($result);
232 $db->sql_freeresult($result);
234 if (!$topic_data)
236 // If post_id was submitted, we try at least to display the topic as a last resort...
237 if ($post_id && $forum_id && $topic_id)
239 redirect(append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id"));
242 trigger_error('NO_TOPIC');
245 // This is for determining where we are (page)
246 if ($post_id)
248 if ($post_id == $topic_data['topic_first_post_id'] || $post_id == $topic_data['topic_last_post_id'])
250 $check_sort = ($post_id == $topic_data['topic_first_post_id']) ? 'd' : 'a';
252 if ($sort_dir == $check_sort)
254 $topic_data['prev_posts'] = ($auth->acl_get('m_approve', $forum_id)) ? $topic_data['topic_replies_real'] + 1 : $topic_data['topic_replies'] + 1;
256 else
258 $topic_data['prev_posts'] = 1;
261 else
263 $sql = 'SELECT COUNT(p1.post_id) AS prev_posts
264 FROM ' . POSTS_TABLE . ' p1, ' . POSTS_TABLE . " p2
265 WHERE p1.topic_id = {$topic_data['topic_id']}
266 AND p2.post_id = {$post_id}
267 " . ((!$auth->acl_get('m_approve', $forum_id)) ? 'AND p1.post_approved = 1' : '') . '
268 AND ' . (($sort_dir == 'd') ? 'p1.post_time >= p2.post_time' : 'p1.post_time <= p2.post_time');
270 $result = $db->sql_query($sql);
271 $row = $db->sql_fetchrow($result);
272 $db->sql_freeresult($result);
274 $topic_data['prev_posts'] = $row['prev_posts'] + 1;
278 $forum_id = (int) $topic_data['forum_id'];
279 $topic_id = (int) $topic_data['topic_id'];
282 $topic_replies = ($auth->acl_get('m_approve', $forum_id)) ? $topic_data['topic_replies_real'] : $topic_data['topic_replies'];
284 // Check sticky/announcement time limit
285 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())
287 $sql = 'UPDATE ' . TOPICS_TABLE . '
288 SET topic_type = ' . POST_NORMAL . ', topic_time_limit = 0
289 WHERE topic_id = ' . $topic_id;
290 $db->sql_query($sql);
292 $topic_data['topic_type'] = POST_NORMAL;
293 $topic_data['topic_time_limit'] = 0;
296 // Setup look and feel
297 $user->setup('viewtopic', $topic_data['forum_style']);
299 if (!$topic_data['topic_approved'] && !$auth->acl_get('m_approve', $forum_id))
301 trigger_error('NO_TOPIC');
304 // Start auth check
305 if (!$auth->acl_get('f_read', $forum_id))
307 if ($user->data['user_id'] != ANONYMOUS)
309 trigger_error('SORRY_AUTH_READ');
312 login_box('', $user->lang['LOGIN_VIEWFORUM']);
315 // Forum is passworded ... check whether access has been granted to this
316 // user this session, if not show login box
317 if ($topic_data['forum_password'])
319 login_forum_box($topic_data);
322 // Redirect to login or to the correct post upon emailed notification links
323 if (isset($_GET['e']))
325 $jump_to = request_var('e', 0);
327 $redirect_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id");
329 if ($user->data['user_id'] == ANONYMOUS)
331 login_box($redirect_url . "&amp;p=$post_id&amp;e=$jump_to", $user->lang['LOGIN_NOTIFY_TOPIC']);
334 if ($jump_to > 0)
336 // We direct the already logged in user to the correct post...
337 redirect($redirect_url . ((!$post_id) ? "&amp;p=$jump_to" : "&amp;p=$post_id") . "#p$jump_to");
341 // What is start equal to?
342 if ($post_id)
344 $start = floor(($topic_data['prev_posts'] - 1) / $config['posts_per_page']) * $config['posts_per_page'];
347 // Get topic tracking info
348 if (!isset($topic_tracking_info))
350 $topic_tracking_info = array();
352 // Get topic tracking info
353 if ($config['load_db_lastread'] && $user->data['is_registered'])
355 $tmp_topic_data = array($topic_id => $topic_data);
356 $topic_tracking_info = get_topic_tracking($forum_id, $topic_id, $tmp_topic_data, array($forum_id => $topic_data['forum_mark_time']));
357 unset($tmp_topic_data);
359 else if ($config['load_anon_lastread'] || $user->data['is_registered'])
361 $topic_tracking_info = get_complete_topic_tracking($forum_id, $topic_id);
365 // Post ordering options
366 $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']);
368 $sort_by_text = array('a' => $user->lang['AUTHOR'], 't' => $user->lang['POST_TIME'], 's' => $user->lang['SUBJECT']);
369 $sort_by_sql = array('a' => 'u.username_clean', 't' => 'p.post_time', 's' => 'p.post_subject');
371 $s_limit_days = $s_sort_key = $s_sort_dir = $u_sort_param = '';
372 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);
374 // Obtain correct post count and ordering SQL if user has
375 // requested anything different
376 if ($sort_days)
378 $min_post_time = time() - ($sort_days * 86400);
380 $sql = 'SELECT COUNT(post_id) AS num_posts
381 FROM ' . POSTS_TABLE . "
382 WHERE topic_id = $topic_id
383 AND post_time >= $min_post_time
384 " . (($auth->acl_get('m_approve', $forum_id)) ? '' : 'AND post_approved = 1');
385 $result = $db->sql_query($sql);
386 $total_posts = (int) $db->sql_fetchfield('num_posts');
387 $db->sql_freeresult($result);
389 $limit_posts_time = "AND p.post_time >= $min_post_time ";
391 if (isset($_POST['sort']))
393 $start = 0;
396 else
398 $total_posts = $topic_replies + 1;
399 $limit_posts_time = '';
402 // Was a highlight request part of the URI?
403 $highlight_match = $highlight = '';
404 if ($hilit_words)
406 foreach (explode(' ', trim($hilit_words)) as $word)
408 if (trim($word))
410 $highlight_match .= (($highlight_match != '') ? '|' : '') . str_replace('\*', '\w+?', preg_quote($word, '#'));
414 $highlight = urlencode($hilit_words);
417 // Make sure $start is set to the last page if it exceeds the amount
418 if ($start < 0 || $start > $total_posts)
420 $start = ($start < 0) ? 0 : floor(($total_posts - 1) / $config['posts_per_page']) * $config['posts_per_page'];
423 // General Viewtopic URL for return links
424 $viewtopic_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;start=$start&amp;$u_sort_param" . (($highlight_match) ? "&amp;hilit=$highlight" : ''));
426 // Are we watching this topic?
427 $s_watching_topic = $s_watching_topic_img = array();
428 $s_watching_topic['link'] = $s_watching_topic['title'] = '';
429 if ($config['email_enable'] && $config['allow_topic_notify'] && $user->data['is_registered'])
431 watch_topic_forum('topic', $s_watching_topic, $s_watching_topic_img, $user->data['user_id'], $forum_id, $topic_id, $topic_data['notify_status'], $start);
434 // Bookmarks
435 if ($config['allow_bookmarks'] && $user->data['is_registered'] && request_var('bookmark', 0))
437 if (!$topic_data['bookmarked'])
439 $sql = 'INSERT INTO ' . BOOKMARKS_TABLE . ' ' . $db->sql_build_array('INSERT', array(
440 'user_id' => $user->data['user_id'],
441 'topic_id' => $topic_id,
442 'order_id' => 0)
444 $db->sql_query($sql);
446 $where_sql = '';
447 $sign = '+';
449 else
451 $sql = 'DELETE FROM ' . BOOKMARKS_TABLE . "
452 WHERE user_id = {$user->data['user_id']}
453 AND topic_id = $topic_id";
454 $db->sql_query($sql);
456 // Works because of current order_id selected as bookmark value (please do not change because of simplicity)
457 $where_sql = " AND order_id > {$topic_data['bookmarked']}";
458 $sign = '-';
461 // Re-Sort Bookmarks
462 $sql = 'UPDATE ' . BOOKMARKS_TABLE . "
463 SET order_id = order_id $sign 1
464 WHERE user_id = {$user->data['user_id']}
465 $where_sql";
466 $db->sql_query($sql);
468 meta_refresh(3, $viewtopic_url);
470 $message = (($topic_data['bookmarked']) ? $user->lang['BOOKMARK_REMOVED'] : $user->lang['BOOKMARK_ADDED']) . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $viewtopic_url . '">', '</a>');
471 trigger_error($message);
474 // Grab ranks
475 $ranks = $cache->obtain_ranks();
477 // Grab icons
478 $icons = $cache->obtain_icons();
480 // Grab extensions
481 $extensions = array();
482 if ($topic_data['topic_attachment'])
484 $extensions = $cache->obtain_attach_extensions($forum_id);
487 // Forum rules listing
488 $s_forum_rules = '';
489 gen_forum_auth_level('topic', $forum_id, $topic_data['forum_status']);
491 // Quick mod tools
492 $allow_change_type = ($auth->acl_get('m_', $forum_id) || ($user->data['is_registered'] && $user->data['user_id'] == $topic_data['topic_poster'])) ? true : false;
494 $topic_mod = '';
495 $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>') : '';
496 $topic_mod .= ($auth->acl_get('m_delete', $forum_id)) ? '<option value="delete_topic">' . $user->lang['DELETE_TOPIC'] . '</option>' : '';
497 $topic_mod .= ($auth->acl_get('m_move', $forum_id) && $topic_data['topic_status'] != ITEM_MOVED) ? '<option value="move">' . $user->lang['MOVE_TOPIC'] . '</option>' : '';
498 $topic_mod .= ($auth->acl_get('m_split', $forum_id)) ? '<option value="split">' . $user->lang['SPLIT_TOPIC'] . '</option>' : '';
499 $topic_mod .= ($auth->acl_get('m_merge', $forum_id)) ? '<option value="merge">' . $user->lang['MERGE_POSTS'] . '</option>' : '';
500 $topic_mod .= ($auth->acl_get('m_merge', $forum_id)) ? '<option value="merge_topic">' . $user->lang['MERGE_TOPIC'] . '</option>' : '';
501 $topic_mod .= ($auth->acl_get('m_move', $forum_id)) ? '<option value="fork">' . $user->lang['FORK_TOPIC'] . '</option>' : '';
502 $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>' : '';
503 $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>' : '';
504 $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>' : '';
505 $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>' : '';
506 $topic_mod .= ($auth->acl_get('m_', $forum_id)) ? '<option value="topic_logs">' . $user->lang['VIEW_TOPIC_LOGS'] . '</option>' : '';
508 // If we've got a hightlight set pass it on to pagination.
509 $pagination = generate_pagination(append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;$u_sort_param" . (($highlight_match) ? "&amp;hilit=$highlight" : '')), $total_posts, $config['posts_per_page'], $start);
511 // Navigation links
512 generate_forum_nav($topic_data);
514 // Forum Rules
515 generate_forum_rules($topic_data);
517 // Moderators
518 $forum_moderators = array();
519 get_moderators($forum_moderators, $forum_id);
521 // This is only used for print view so ...
522 $server_path = (!$view) ? $phpbb_root_path : generate_board_url() . '/';
524 // Replace naughty words in title
525 $topic_data['topic_title'] = censor_text($topic_data['topic_title']);
527 // Send vars to template
528 $template->assign_vars(array(
529 'FORUM_ID' => $forum_id,
530 'FORUM_NAME' => $topic_data['forum_name'],
531 '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']),
532 'TOPIC_ID' => $topic_id,
533 'TOPIC_TITLE' => $topic_data['topic_title'],
534 'TOPIC_POSTER' => $topic_data['topic_poster'],
536 'TOPIC_AUTHOR_FULL' => get_username_string('full', $topic_data['topic_poster'], $topic_data['topic_first_poster_name'], $topic_data['topic_first_poster_colour']),
537 'TOPIC_AUTHOR_COLOUR' => get_username_string('colour', $topic_data['topic_poster'], $topic_data['topic_first_poster_name'], $topic_data['topic_first_poster_colour']),
538 'TOPIC_AUTHOR' => get_username_string('username', $topic_data['topic_poster'], $topic_data['topic_first_poster_name'], $topic_data['topic_first_poster_colour']),
540 'PAGINATION' => $pagination,
541 'PAGE_NUMBER' => on_page($total_posts, $config['posts_per_page'], $start),
542 'TOTAL_POSTS' => ($total_posts == 1) ? $user->lang['VIEW_TOPIC_POST'] : sprintf($user->lang['VIEW_TOPIC_POSTS'], $total_posts),
543 'U_MCP' => ($auth->acl_get('m_', $forum_id)) ? append_sid("{$phpbb_root_path}mcp.$phpEx", "i=main&amp;mode=topic_view&amp;f=$forum_id&amp;t=$topic_id&amp;start=$start&amp;$u_sort_param", true, $user->session_id) : '',
544 'MODERATORS' => (isset($forum_moderators[$forum_id]) && sizeof($forum_moderators[$forum_id])) ? implode(', ', $forum_moderators[$forum_id]) : '',
546 'POST_IMG' => ($topic_data['forum_status'] == ITEM_LOCKED) ? $user->img('button_topic_locked', 'FORUM_LOCKED') : $user->img('button_topic_new', 'POST_NEW_TOPIC'),
547 'QUOTE_IMG' => $user->img('icon_post_quote', 'REPLY_WITH_QUOTE'),
548 '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'),
549 'EDIT_IMG' => $user->img('icon_post_edit', 'EDIT_POST'),
550 'DELETE_IMG' => $user->img('icon_post_delete', 'DELETE_POST'),
551 'INFO_IMG' => $user->img('icon_post_info', 'VIEW_INFO'),
552 'PROFILE_IMG' => $user->img('icon_user_profile', 'READ_PROFILE'),
553 'SEARCH_IMG' => $user->img('icon_user_search', 'SEARCH_USER_POSTS'),
554 'PM_IMG' => $user->img('icon_contact_pm', 'SEND_PRIVATE_MESSAGE'),
555 'EMAIL_IMG' => $user->img('icon_contact_email', 'SEND_EMAIL'),
556 'WWW_IMG' => $user->img('icon_contact_www', 'VISIT_WEBSITE'),
557 'ICQ_IMG' => $user->img('icon_contact_icq', 'ICQ'),
558 'AIM_IMG' => $user->img('icon_contact_aim', 'AIM'),
559 'MSN_IMG' => $user->img('icon_contact_msnm', 'MSNM'),
560 'YIM_IMG' => $user->img('icon_contact_yahoo', 'YIM'),
561 'JABBER_IMG' => $user->img('icon_contact_jabber', 'JABBER') ,
562 'REPORT_IMG' => $user->img('icon_post_report', 'REPORT_POST'),
563 'REPORTED_IMG' => $user->img('icon_topic_reported', 'POST_REPORTED'),
564 'UNAPPROVED_IMG' => $user->img('icon_topic_unapproved', 'POST_UNAPPROVED'),
565 'WARN_IMG' => $user->img('icon_user_warn', 'WARN_USER'),
567 'S_IS_LOCKED' =>($topic_data['topic_status'] == ITEM_UNLOCKED) ? false : true,
568 'S_SELECT_SORT_DIR' => $s_sort_dir,
569 'S_SELECT_SORT_KEY' => $s_sort_key,
570 'S_SELECT_SORT_DAYS' => $s_limit_days,
571 'S_SINGLE_MODERATOR' => (!empty($forum_moderators[$forum_id]) && sizeof($forum_moderators[$forum_id]) > 1) ? false : true,
572 'S_TOPIC_ACTION' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;start=$start"),
573 'S_TOPIC_MOD' => ($topic_mod != '') ? '<select name="action">' . $topic_mod . '</select>' : '',
574 'S_MOD_ACTION' => append_sid("{$phpbb_root_path}mcp.$phpEx", "t=$topic_id&amp;f=$forum_id&amp;quickmod=1&amp;redirect=" . urlencode(str_replace('&amp;', '&', $viewtopic_url)), true, $user->session_id),
576 'S_DISPLAY_SEARCHBOX' => ($auth->acl_get('u_search') && $auth->acl_get('f_search', $forum_id) && $config['load_search']) ? true : false,
577 'S_SEARCHBOX_ACTION' => append_sid("{$phpbb_root_path}search.$phpEx", 't=' . $topic_id),
579 'U_TOPIC' => "{$server_path}viewtopic.$phpEx?f=$forum_id&amp;t=$topic_id",
580 'U_FORUM' => $server_path,
581 'U_VIEW_TOPIC' => $viewtopic_url,
582 'U_VIEW_FORUM' => append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_id),
583 'U_VIEW_OLDER_TOPIC' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;view=previous"),
584 'U_VIEW_NEWER_TOPIC' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;view=next"),
585 'U_PRINT_TOPIC' => ($auth->acl_get('f_print', $forum_id)) ? $viewtopic_url . '&amp;view=print' : '',
586 'U_EMAIL_TOPIC' => ($auth->acl_get('f_email', $forum_id) && $config['email_enable']) ? append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=email&amp;t=$topic_id") : '',
588 'U_WATCH_TOPIC' => $s_watching_topic['link'],
589 'L_WATCH_TOPIC' => $s_watching_topic['title'],
591 'U_BOOKMARK_TOPIC' => ($user->data['is_registered'] && $config['allow_bookmarks']) ? $viewtopic_url . '&amp;bookmark=1' : '',
592 'L_BOOKMARK_TOPIC' => ($user->data['is_registered'] && $config['allow_bookmarks'] && $topic_data['bookmarked']) ? $user->lang['BOOKMARK_TOPIC_REMOVE'] : $user->lang['BOOKMARK_TOPIC'],
594 'U_POST_NEW_TOPIC' => append_sid("{$phpbb_root_path}posting.$phpEx", "mode=post&amp;f=$forum_id"),
595 'U_POST_REPLY_TOPIC' => append_sid("{$phpbb_root_path}posting.$phpEx", "mode=reply&amp;f=$forum_id&amp;t=$topic_id"),
596 'U_BUMP_TOPIC' => (bump_topic_allowed($forum_id, $topic_data['topic_bumped'], $topic_data['topic_last_post_time'], $topic_data['topic_poster'], $topic_data['topic_last_poster_id'])) ? append_sid("{$phpbb_root_path}posting.$phpEx", "mode=bump&amp;f=$forum_id&amp;t=$topic_id") : '')
599 // Does this topic contain a poll?
600 if (!empty($topic_data['poll_start']))
602 $sql = 'SELECT o.*, p.bbcode_bitfield, p.bbcode_uid
603 FROM ' . POLL_OPTIONS_TABLE . ' o, ' . POSTS_TABLE . " p
604 WHERE o.topic_id = $topic_id
605 AND p.post_id = {$topic_data['topic_first_post_id']}
606 AND p.topic_id = o.topic_id
607 ORDER BY o.poll_option_id";
608 $result = $db->sql_query($sql);
610 $poll_info = array();
611 while ($row = $db->sql_fetchrow($result))
613 $poll_info[] = $row;
615 $db->sql_freeresult($result);
617 $cur_voted_id = array();
618 if ($user->data['is_registered'])
620 $sql = 'SELECT poll_option_id
621 FROM ' . POLL_VOTES_TABLE . '
622 WHERE topic_id = ' . $topic_id . '
623 AND vote_user_id = ' . $user->data['user_id'];
624 $result = $db->sql_query($sql);
626 while ($row = $db->sql_fetchrow($result))
628 $cur_voted_id[] = $row['poll_option_id'];
630 $db->sql_freeresult($result);
632 else
634 // Cookie based guest tracking ... I don't like this but hum ho
635 // it's oft requested. This relies on "nice" users who don't feel
636 // the need to delete cookies to mess with results.
637 if (isset($_COOKIE[$config['cookie_name'] . '_poll_' . $topic_id]))
639 $cur_voted_id = explode(',', $_COOKIE[$config['cookie_name'] . '_poll_' . $topic_id]);
640 $cur_voted_id = array_map('intval', $cur_voted_id);
644 $s_can_vote = (((!sizeof($cur_voted_id) && $auth->acl_get('f_vote', $forum_id)) ||
645 ($auth->acl_get('f_votechg', $forum_id) && $topic_data['poll_vote_change'])) &&
646 (($topic_data['poll_length'] != 0 && $topic_data['poll_start'] + $topic_data['poll_length'] > time()) || $topic_data['poll_length'] == 0) &&
647 $topic_data['topic_status'] != ITEM_LOCKED &&
648 $topic_data['forum_status'] != ITEM_LOCKED) ? true : false;
649 $s_display_results = (!$s_can_vote || ($s_can_vote && sizeof($cur_voted_id)) || $view == 'viewpoll') ? true : false;
651 if ($update && $s_can_vote)
653 if (!sizeof($voted_id) || sizeof($voted_id) > $topic_data['poll_max_options'])
655 $redirect_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id");
657 meta_refresh(5, $redirect_url);
659 $message = (!sizeof($voted_id)) ? 'NO_VOTE_OPTION' : 'TOO_MANY_VOTE_OPTIONS';
660 $message = $user->lang[$message] . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $redirect_url . '">', '</a>');
661 trigger_error($message);
664 foreach ($voted_id as $option)
666 if (in_array($option, $cur_voted_id))
668 continue;
671 $sql = 'UPDATE ' . POLL_OPTIONS_TABLE . '
672 SET poll_option_total = poll_option_total + 1
673 WHERE poll_option_id = ' . (int) $option . '
674 AND topic_id = ' . (int) $topic_id;
675 $db->sql_query($sql);
677 if ($user->data['is_registered'])
679 $sql_ary = array(
680 'topic_id' => (int) $topic_id,
681 'poll_option_id' => (int) $option,
682 'vote_user_id' => (int) $user->data['user_id'],
683 'vote_user_ip' => (string) $user->ip,
686 $sql = 'INSERT INTO ' . POLL_VOTES_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
687 $db->sql_query($sql);
691 foreach ($cur_voted_id as $option)
693 if (!in_array($option, $voted_id))
695 $sql = 'UPDATE ' . POLL_OPTIONS_TABLE . '
696 SET poll_option_total = poll_option_total - 1
697 WHERE poll_option_id = ' . (int) $option . '
698 AND topic_id = ' . (int) $topic_id;
699 $db->sql_query($sql);
701 if ($user->data['is_registered'])
703 $sql = 'DELETE FROM ' . POLL_VOTES_TABLE . '
704 WHERE topic_id = ' . (int) $topic_id . '
705 AND poll_option_id = ' . (int) $option . '
706 AND vote_user_id = ' . (int) $user->data['user_id'];
707 $db->sql_query($sql);
712 if ($user->data['user_id'] == ANONYMOUS && !$user->data['is_bot'])
714 $user->set_cookie('poll_' . $topic_id, implode(',', $voted_id), time() + 31536000);
717 $sql = 'UPDATE ' . TOPICS_TABLE . '
718 SET poll_last_vote = ' . time() . "
719 WHERE topic_id = $topic_id";
720 //, topic_last_post_time = ' . time() . " -- for bumping topics with new votes, ignore for now
721 $db->sql_query($sql);
723 $redirect_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id");
725 meta_refresh(5, $redirect_url);
726 trigger_error($user->lang['VOTE_SUBMITTED'] . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $redirect_url . '">', '</a>'));
729 $poll_total = 0;
730 foreach ($poll_info as $poll_option)
732 $poll_total += $poll_option['poll_option_total'];
735 if ($poll_info[0]['bbcode_bitfield'])
737 $poll_bbcode = new bbcode();
739 else
741 $poll_bbcode = false;
744 for ($i = 0, $size = sizeof($poll_info); $i < $size; $i++)
746 $poll_info[$i]['poll_option_text'] = censor_text($poll_info[$i]['poll_option_text']);
747 $poll_info[$i]['poll_option_text'] = str_replace("\n", '<br />', $poll_info[$i]['poll_option_text']);
749 if ($poll_bbcode !== false)
751 $poll_bbcode->bbcode_second_pass($poll_info[$i]['poll_option_text'], $poll_info[$i]['bbcode_uid'], $poll_option['bbcode_bitfield']);
754 $poll_info[$i]['poll_option_text'] = smiley_text($poll_info[$i]['poll_option_text']);
757 $topic_data['poll_title'] = censor_text($topic_data['poll_title']);
758 $topic_data['poll_title'] = str_replace("\n", '<br />', $topic_data['poll_title']);
760 if ($poll_bbcode !== false)
762 $poll_bbcode->bbcode_second_pass($topic_data['poll_title'], $poll_info[0]['bbcode_uid'], $poll_info[0]['bbcode_bitfield']);
764 $topic_data['poll_title'] = smiley_text($topic_data['poll_title']);
766 unset($poll_bbcode);
768 foreach ($poll_info as $poll_option)
770 $option_pct = ($poll_total > 0) ? $poll_option['poll_option_total'] / $poll_total : 0;
771 $option_pct_txt = sprintf("%.1d%%", ($option_pct * 100));
773 $template->assign_block_vars('poll_option', array(
774 'POLL_OPTION_ID' => $poll_option['poll_option_id'],
775 'POLL_OPTION_CAPTION' => $poll_option['poll_option_text'],
776 'POLL_OPTION_RESULT' => $poll_option['poll_option_total'],
777 'POLL_OPTION_PERCENT' => $option_pct_txt,
778 'POLL_OPTION_PCT' => round($option_pct * 100),
779 'POLL_OPTION_IMG' => $user->img('poll_center', $option_pct_txt, round($option_pct * 250)),
780 'POLL_OPTION_VOTED' => (in_array($poll_option['poll_option_id'], $cur_voted_id)) ? true : false)
784 $poll_end = $topic_data['poll_length'] + $topic_data['poll_start'];
786 $template->assign_vars(array(
787 'POLL_QUESTION' => $topic_data['poll_title'],
788 'TOTAL_VOTES' => $poll_total,
789 'POLL_LEFT_CAP_IMG' => $user->img('poll_left'),
790 'POLL_RIGHT_CAP_IMG'=> $user->img('poll_right'),
792 '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']),
793 'L_POLL_LENGTH' => ($topic_data['poll_length']) ? sprintf($user->lang[($poll_end > time()) ? 'POLL_RUN_TILL' : 'POLL_ENDED_AT'], $user->format_date($poll_end)) : '',
795 'S_HAS_POLL' => true,
796 'S_CAN_VOTE' => $s_can_vote,
797 'S_DISPLAY_RESULTS' => $s_display_results,
798 'S_IS_MULTI_CHOICE' => ($topic_data['poll_max_options'] > 1) ? true : false,
799 'S_POLL_ACTION' => $viewtopic_url,
801 'U_VIEW_RESULTS' => $viewtopic_url . '&amp;view=viewpoll')
804 unset($poll_end, $poll_info, $voted_id);
807 // If the user is trying to reach the second half of the topic, fetch it starting from the end
808 $store_reverse = false;
809 $sql_limit = $config['posts_per_page'];
811 if ($start > $total_posts / 2)
813 $store_reverse = true;
815 if ($start + $config['posts_per_page'] > $total_posts)
817 $sql_limit = min($config['posts_per_page'], max(1, $total_posts - $start));
820 // Select the sort order
821 $sql_sort_order = $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'ASC' : 'DESC');
822 $sql_start = max(0, $total_posts - $sql_limit - $start);
824 else
826 // Select the sort order
827 $sql_sort_order = $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'DESC' : 'ASC');
828 $sql_start = $start;
831 // Container for user details, only process once
832 $post_list = $user_cache = $id_cache = $attachments = $attach_list = $rowset = $update_count = $post_edit_list = array();
833 $has_attachments = $display_notice = false;
834 $bbcode_bitfield = '';
835 $i = $i_total = 0;
837 // Go ahead and pull all data for this topic
838 $sql = 'SELECT p.post_id
839 FROM ' . POSTS_TABLE . ' p' . (($sort_by_sql[$sort_key][0] == 'u') ? ', ' . USERS_TABLE . ' u': '') . "
840 WHERE p.topic_id = $topic_id
841 " . ((!$auth->acl_get('m_approve', $forum_id)) ? 'AND p.post_approved = 1' : '') . "
842 " . (($sort_by_sql[$sort_key][0] == 'u') ? 'AND u.user_id = p.poster_id': '') . "
843 $limit_posts_time
844 ORDER BY $sql_sort_order";
845 $result = $db->sql_query_limit($sql, $sql_limit, $sql_start);
847 $i = ($store_reverse) ? $sql_limit - 1 : 0;
848 while ($row = $db->sql_fetchrow($result))
850 $post_list[$i] = $row['post_id'];
851 ($store_reverse) ? $i-- : $i++;
853 $db->sql_freeresult($result);
855 if (!sizeof($post_list))
857 if ($sort_days)
859 trigger_error('NO_POSTS_TIME_FRAME');
861 else
863 trigger_error('NO_TOPIC');
867 // Holding maximum post time for marking topic read
868 // We need to grab it because we do reverse ordering sometimes
869 $max_post_time = 0;
871 $sql = $db->sql_build_query('SELECT', array(
872 'SELECT' => 'u.*, z.friend, z.foe, p.*',
874 'FROM' => array(
875 USERS_TABLE => 'u',
876 POSTS_TABLE => 'p',
879 'LEFT_JOIN' => array(
880 array(
881 'FROM' => array(ZEBRA_TABLE => 'z'),
882 'ON' => 'z.user_id = ' . $user->data['user_id'] . ' AND z.zebra_id = p.poster_id'
886 'WHERE' => $db->sql_in_set('p.post_id', $post_list) . '
887 AND u.user_id = p.poster_id'
890 $result = $db->sql_query($sql);
892 $now = getdate(time() + $user->timezone + $user->dst - date('Z'));
894 // Posts are stored in the $rowset array while $attach_list, $user_cache
895 // and the global bbcode_bitfield are built
896 while ($row = $db->sql_fetchrow($result))
898 // Set max_post_time
899 if ($row['post_time'] > $max_post_time)
901 $max_post_time = $row['post_time'];
904 $poster_id = $row['poster_id'];
906 // Does post have an attachment? If so, add it to the list
907 if ($row['post_attachment'] && $config['allow_attachments'])
909 $attach_list[] = $row['post_id'];
911 if ($row['post_approved'])
913 $has_attachments = true;
917 $rowset[$row['post_id']] = array(
918 'hide_post' => ($row['foe'] && ($view != 'show' || $post_id != $row['post_id'])) ? true : false,
920 'post_id' => $row['post_id'],
921 'post_time' => $row['post_time'],
922 'user_id' => $row['user_id'],
923 'username' => $row['username'],
924 'user_colour' => $row['user_colour'],
925 'topic_id' => $row['topic_id'],
926 'forum_id' => $row['forum_id'],
927 'post_subject' => $row['post_subject'],
928 'post_edit_count' => $row['post_edit_count'],
929 'post_edit_time' => $row['post_edit_time'],
930 'post_edit_reason' => $row['post_edit_reason'],
931 'post_edit_user' => $row['post_edit_user'],
933 // Make sure the icon actually exists
934 'icon_id' => (isset($icons[$row['icon_id']]['img'], $icons[$row['icon_id']]['height'], $icons[$row['icon_id']]['width'])) ? $row['icon_id'] : 0,
935 'post_attachment' => $row['post_attachment'],
936 'post_approved' => $row['post_approved'],
937 'post_reported' => $row['post_reported'],
938 'post_username' => $row['post_username'],
939 'post_text' => $row['post_text'],
940 'bbcode_uid' => $row['bbcode_uid'],
941 'bbcode_bitfield' => $row['bbcode_bitfield'],
942 'enable_smilies' => $row['enable_smilies'],
943 'enable_sig' => $row['enable_sig'],
944 'friend' => $row['friend'],
945 'foe' => $row['foe'],
948 // Define the global bbcode bitfield, will be used to load bbcodes
949 $bbcode_bitfield = $bbcode_bitfield | base64_decode($row['bbcode_bitfield']);
951 // Is a signature attached? Are we going to display it?
952 if ($row['enable_sig'] && $config['allow_sig'] && $user->optionget('viewsigs'))
954 $bbcode_bitfield = $bbcode_bitfield | base64_decode($row['user_sig_bbcode_bitfield']);
957 // Cache various user specific data ... so we don't have to recompute
958 // this each time the same user appears on this page
959 if (!isset($user_cache[$poster_id]))
961 if ($poster_id == ANONYMOUS)
963 $user_cache[$poster_id] = array(
964 'joined' => '',
965 'posts' => '',
966 'from' => '',
968 'sig' => '',
969 'sig_bbcode_uid' => '',
970 'sig_bbcode_bitfield' => '',
972 'online' => false,
973 'avatar' => '',
974 'rank_title' => '',
975 'rank_image' => '',
976 'rank_image_src' => '',
977 'sig' => '',
978 'posts' => '',
979 'profile' => '',
980 'pm' => '',
981 'email' => '',
982 'www' => '',
983 'icq_status_img' => '',
984 'icq' => '',
985 'aim' => '',
986 'msn' => '',
987 'yim' => '',
988 'jabber' => '',
989 'search' => '',
990 'age' => '',
992 'username' => $row['username'],
993 'user_colour' => $row['user_colour'],
995 'warnings' => 0,
996 'allow_pm' => 0,
999 else
1001 $user_sig = '';
1003 // We add the signature to every posters entry because enable_sig is post dependant
1004 if ($row['user_sig'] && $config['allow_sig'] && $user->optionget('viewsigs'))
1006 $user_sig = $row['user_sig'];
1009 $id_cache[] = $poster_id;
1011 $user_cache[$poster_id] = array(
1012 'joined' => $user->format_date($row['user_regdate']),
1013 'posts' => $row['user_posts'],
1014 'warnings' => (isset($row['user_warnings'])) ? $row['user_warnings'] : 0,
1015 'from' => (!empty($row['user_from'])) ? $row['user_from'] : '',
1017 'sig' => $user_sig,
1018 'sig_bbcode_uid' => (!empty($row['user_sig_bbcode_uid'])) ? $row['user_sig_bbcode_uid'] : '',
1019 'sig_bbcode_bitfield' => (!empty($row['user_sig_bbcode_bitfield'])) ? $row['user_sig_bbcode_bitfield'] : '',
1021 'viewonline' => $row['user_allow_viewonline'],
1022 'allow_pm' => $row['user_allow_pm'],
1024 'avatar' => ($user->optionget('viewavatars')) ? get_user_avatar($row['user_avatar'], $row['user_avatar_type'], $row['user_avatar_width'], $row['user_avatar_height']) : '',
1025 'age' => '',
1027 'rank_title' => '',
1028 'rank_image' => '',
1029 'rank_image_src' => '',
1031 'username' => $row['username'],
1032 'user_colour' => $row['user_colour'],
1034 'online' => false,
1035 'profile' => append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=viewprofile&amp;u=$poster_id"),
1036 'www' => $row['user_website'],
1037 'aim' => ($row['user_aim'] && $auth->acl_get('u_sendim')) ? append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=contact&amp;action=aim&amp;u=$poster_id") : '',
1038 'msn' => ($row['user_msnm'] && $auth->acl_get('u_sendim')) ? append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=contact&amp;action=msnm&amp;u=$poster_id") : '',
1039 'yim' => ($row['user_yim']) ? 'http://edit.yahoo.com/config/send_webmesg?.target=' . $row['user_yim'] . '&amp;.src=pg' : '',
1040 'jabber' => ($row['user_jabber'] && $auth->acl_get('u_sendim')) ? append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=contact&amp;action=jabber&amp;u=$poster_id") : '',
1041 'search' => ($auth->acl_get('u_search')) ? append_sid("{$phpbb_root_path}search.$phpEx", 'search_author=' . urlencode($row['username']) .'&amp;showresults=posts') : '',
1044 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']);
1046 if (!empty($row['user_allow_viewemail']) || $auth->acl_get('a_email'))
1048 $user_cache[$poster_id]['email'] = ($config['board_email_form'] && $config['email_enable']) ? append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=email&amp;u=$poster_id") : (($config['board_hide_emails'] && !$auth->acl_get('a_email')) ? '' : 'mailto:' . $row['user_email']);
1050 else
1052 $user_cache[$poster_id]['email'] = '';
1055 if (!empty($row['user_icq']))
1057 $user_cache[$poster_id]['icq'] = 'http://www.icq.com/people/webmsg.php?to=' . $row['user_icq'];
1058 $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="" />';
1060 else
1062 $user_cache[$poster_id]['icq_status_img'] = '';
1063 $user_cache[$poster_id]['icq'] = '';
1066 if (!empty($row['user_birthday']))
1068 list($bday_day, $bday_month, $bday_year) = array_map('intval', explode('-', $row['user_birthday']));
1070 if ($bday_year)
1072 $diff = $now['mon'] - $bday_month;
1073 if ($diff == 0)
1075 $diff = ($now['mday'] - $bday_day < 0) ? 1 : 0;
1077 else
1079 $diff = ($diff < 0) ? 1 : 0;
1082 $user_cache[$poster_id]['age'] = (int) ($now['year'] - $bday_year - $diff);
1088 $db->sql_freeresult($result);
1089 unset($today);
1091 // Load custom profile fields
1092 if ($config['load_cpf_viewtopic'])
1094 include($phpbb_root_path . 'includes/functions_profile_fields.' . $phpEx);
1095 $cp = new custom_profile();
1097 // Grab all profile fields from users in id cache for later use - similar to the poster cache
1098 $profile_fields_cache = $cp->generate_profile_fields_template('grab', $id_cache);
1101 // Generate online information for user
1102 if ($config['load_onlinetrack'] && sizeof($id_cache))
1104 $sql = 'SELECT session_user_id, MAX(session_time) as online_time, MIN(session_viewonline) AS viewonline
1105 FROM ' . SESSIONS_TABLE . '
1106 WHERE ' . $db->sql_in_set('session_user_id', $id_cache) . '
1107 GROUP BY session_user_id';
1108 $result = $db->sql_query($sql);
1110 $update_time = $config['load_online_time'] * 60;
1111 while ($row = $db->sql_fetchrow($result))
1113 $user_cache[$row['session_user_id']]['online'] = (time() - $update_time < $row['online_time'] && (($row['viewonline'] && $user_cache[$row['session_user_id']]['viewonline']) || $auth->acl_get('u_viewonline'))) ? true : false;
1115 $db->sql_freeresult($result);
1117 unset($id_cache);
1119 // Pull attachment data
1120 if (sizeof($attach_list))
1122 if ($auth->acl_get('u_download') && $auth->acl_get('f_download', $forum_id))
1124 $sql = 'SELECT *
1125 FROM ' . ATTACHMENTS_TABLE . '
1126 WHERE ' . $db->sql_in_set('post_msg_id', $attach_list) . '
1127 AND in_message = 0
1128 ORDER BY filetime DESC, post_msg_id ASC';
1129 $result = $db->sql_query($sql);
1131 while ($row = $db->sql_fetchrow($result))
1133 $attachments[$row['post_msg_id']][] = $row;
1135 $db->sql_freeresult($result);
1137 // No attachments exist, but post table thinks they do so go ahead and reset post_attach flags
1138 if (!sizeof($attachments))
1140 $sql = 'UPDATE ' . POSTS_TABLE . '
1141 SET post_attachment = 0
1142 WHERE ' . $db->sql_in_set('post_id', $attach_list);
1143 $db->sql_query($sql);
1145 // We need to update the topic indicator too if the complete topic is now without an attachment
1146 if (sizeof($rowset) != $total_posts)
1148 // Not all posts are displayed so we query the db to find if there's any attachment for this topic
1149 $sql = 'SELECT a.post_msg_id as post_id
1150 FROM ' . ATTACHMENTS_TABLE . ' a, ' . POSTS_TABLE . " p
1151 WHERE p.topic_id = $topic_id
1152 AND p.post_approved = 1
1153 AND p.topic_id = a.topic_id";
1154 $result = $db->sql_query_limit($sql, 1);
1155 $row = $db->sql_fetchrow($result);
1156 $db->sql_freeresult($result);
1158 if (!$row)
1160 $sql = 'UPDATE ' . TOPICS_TABLE . "
1161 SET topic_attachment = 0
1162 WHERE topic_id = $topic_id";
1163 $db->sql_query($sql);
1166 else
1168 $sql = 'UPDATE ' . TOPICS_TABLE . "
1169 SET topic_attachment = 0
1170 WHERE topic_id = $topic_id";
1171 $db->sql_query($sql);
1174 else if ($has_attachments && !$topic_data['topic_attachment'])
1176 // Topic has approved attachments but its flag is wrong
1177 $sql = 'UPDATE ' . TOPICS_TABLE . "
1178 SET topic_attachment = 1
1179 WHERE topic_id = $topic_id";
1180 $db->sql_query($sql);
1182 $topic_data['topic_attachment'] = 1;
1185 else
1187 $display_notice = true;
1191 // Instantiate BBCode if need be
1192 if ($bbcode_bitfield !== '')
1194 $bbcode = new bbcode(base64_encode($bbcode_bitfield));
1197 $i_total = sizeof($rowset) - 1;
1198 $prev_post_id = '';
1200 $template->assign_vars(array(
1201 'S_NUM_POSTS' => sizeof($post_list))
1204 // Output the posts
1205 $first_unread = $post_unread = false;
1206 for ($i = 0, $end = sizeof($post_list); $i < $end; ++$i)
1208 // A non-existing rowset only happens if there was no user present for the entered poster_id
1209 // This could be a broken posts table.
1210 if (!isset($rowset[$post_list[$i]]))
1212 continue;
1215 $row =& $rowset[$post_list[$i]];
1216 $poster_id = $row['user_id'];
1218 // End signature parsing, only if needed
1219 if ($user_cache[$poster_id]['sig'] && $row['enable_sig'] && empty($user_cache[$poster_id]['sig_parsed']))
1221 $user_cache[$poster_id]['sig'] = censor_text($user_cache[$poster_id]['sig']);
1222 $user_cache[$poster_id]['sig'] = str_replace("\n", '<br />', $user_cache[$poster_id]['sig']);
1224 if ($user_cache[$poster_id]['sig_bbcode_bitfield'])
1226 $bbcode->bbcode_second_pass($user_cache[$poster_id]['sig'], $user_cache[$poster_id]['sig_bbcode_uid'], $user_cache[$poster_id]['sig_bbcode_bitfield']);
1229 $user_cache[$poster_id]['sig'] = smiley_text($user_cache[$poster_id]['sig']);
1230 $user_cache[$poster_id]['sig_parsed'] = true;
1233 // Parse the message and subject
1234 $message = censor_text($row['post_text']);
1236 // Second parse bbcode here
1237 if ($row['bbcode_bitfield'])
1239 $bbcode->bbcode_second_pass($message, $row['bbcode_uid'], $row['bbcode_bitfield']);
1242 $message = str_replace("\n", '<br />', $message);
1244 // Always process smilies after parsing bbcodes
1245 $message = smiley_text($message);
1247 if (!empty($attachments[$row['post_id']]))
1249 parse_attachments($forum_id, $message, $attachments[$row['post_id']], $update_count);
1252 // Highlight active words (primarily for search)
1253 if ($highlight_match)
1255 $message = preg_replace('#(?!<.*)(?<!\w)(' . $highlight_match . ')(?!\w|[^<>]*(?:</s(?:cript|tyle))?>)#is', '<span class="posthilit">\1</span>', $message);
1258 // Replace naughty words such as farty pants
1259 $row['post_subject'] = censor_text($row['post_subject']);
1261 // Editing information
1262 if (($row['post_edit_count'] && $config['display_last_edited']) || $row['post_edit_reason'])
1264 // Get usernames for all following posts if not already stored
1265 if (!sizeof($post_edit_list) && ($row['post_edit_reason'] || ($row['post_edit_user'] && !isset($user_cache[$row['post_edit_user']]))))
1267 // Remove all post_ids already parsed (we do not have to check them)
1268 $post_storage_list = (!$store_reverse) ? array_slice($post_list, $i) : array_slice(array_reverse($post_list), $i);
1270 $sql = 'SELECT DISTINCT u.user_id, u.username, u.user_colour
1271 FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
1272 WHERE ' . $db->sql_in_set('p.post_id', $post_storage_list) . '
1273 AND p.post_edit_count <> 0
1274 AND p.post_edit_user <> 0
1275 AND p.post_edit_user = u.user_id';
1276 $result2 = $db->sql_query($sql);
1277 while ($user_edit_row = $db->sql_fetchrow($result2))
1279 $post_edit_list[$user_edit_row['user_id']] = $user_edit_row;
1281 $db->sql_freeresult($result2);
1283 unset($post_storage_list);
1286 $l_edit_time_total = ($row['post_edit_count'] == 1) ? $user->lang['EDITED_TIME_TOTAL'] : $user->lang['EDITED_TIMES_TOTAL'];
1288 if ($row['post_edit_reason'])
1290 // User having edited the post also being the post author?
1291 if (!$row['post_edit_user'] || $row['post_edit_user'] == $poster_id)
1293 $display_username = get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']);
1295 else
1297 $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']);
1300 $l_edited_by = sprintf($l_edit_time_total, $display_username, $user->format_date($row['post_edit_time']), $row['post_edit_count']);
1302 else
1304 if ($row['post_edit_user'] && !isset($user_cache[$row['post_edit_user']]))
1306 $user_cache[$row['post_edit_user']] = $post_edit_list[$row['post_edit_user']];
1309 // User having edited the post also being the post author?
1310 if (!$row['post_edit_user'] || $row['post_edit_user'] == $poster_id)
1312 $display_username = get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']);
1314 else
1316 $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']);
1319 $l_edited_by = sprintf($l_edit_time_total, $display_username, $user->format_date($row['post_edit_time']), $row['post_edit_count']);
1322 else
1324 $l_edited_by = '';
1327 // Bump information
1328 if ($topic_data['topic_bumped'] && $row['post_id'] == $topic_data['topic_last_post_id'] && isset($user_cache[$topic_data['topic_bumper']]) )
1330 // It is safe to grab the username from the user cache array, we are at the last
1331 // post and only the topic poster and last poster are allowed to bump.
1332 // Admins and mods are bound to the above rules too...
1333 $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']));
1335 else
1337 $l_bumped_by = '';
1340 $cp_row = array();
1343 if ($config['load_cpf_viewtopic'])
1345 $cp_row = (isset($profile_fields_cache[$poster_id])) ? $cp->generate_profile_fields_template('show', false, $profile_fields_cache[$poster_id]) : array();
1348 $post_unread = (isset($topic_tracking_info[$topic_id]) && $row['post_time'] > $topic_tracking_info[$topic_id]) ? true : false;
1350 $s_first_unread = false;
1351 if (!$first_unread && $post_unread)
1353 $s_first_unread = $first_unread = true;
1357 $postrow = array(
1358 'POST_AUTHOR_FULL' => get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
1359 'POST_AUTHOR_COLOUR' => get_username_string('colour', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
1360 'POST_AUTHOR' => get_username_string('username', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
1361 'U_POST_AUTHOR' => get_username_string('profile', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
1363 'RANK_TITLE' => $user_cache[$poster_id]['rank_title'],
1364 'RANK_IMG' => $user_cache[$poster_id]['rank_image'],
1365 'RANK_IMG_SRC' => $user_cache[$poster_id]['rank_image_src'],
1366 'POSTER_JOINED' => $user_cache[$poster_id]['joined'],
1367 'POSTER_POSTS' => $user_cache[$poster_id]['posts'],
1368 'POSTER_FROM' => $user_cache[$poster_id]['from'],
1369 'POSTER_AVATAR' => $user_cache[$poster_id]['avatar'],
1370 'POSTER_WARNINGS' => $user_cache[$poster_id]['warnings'],
1371 'POSTER_AGE' => $user_cache[$poster_id]['age'],
1373 'POST_DATE' => $user->format_date($row['post_time']),
1374 'POST_SUBJECT' => $row['post_subject'],
1375 'MESSAGE' => $message,
1376 'SIGNATURE' => ($row['enable_sig']) ? $user_cache[$poster_id]['sig'] : '',
1377 'EDITED_MESSAGE' => $l_edited_by,
1378 'EDIT_REASON' => $row['post_edit_reason'],
1379 'BUMPED_MESSAGE' => $l_bumped_by,
1381 'MINI_POST_IMG' => ($post_unread) ? $user->img('icon_post_target_unread', 'NEW_POST') : $user->img('icon_post_target', 'POST'),
1382 'POST_ICON_IMG' => ($topic_data['enable_icons'] && !empty($row['icon_id'])) ? $icons[$row['icon_id']]['img'] : '',
1383 'POST_ICON_IMG_WIDTH' => ($topic_data['enable_icons'] && !empty($row['icon_id'])) ? $icons[$row['icon_id']]['width'] : '',
1384 'POST_ICON_IMG_HEIGHT' => ($topic_data['enable_icons'] && !empty($row['icon_id'])) ? $icons[$row['icon_id']]['height'] : '',
1385 'ICQ_STATUS_IMG' => $user_cache[$poster_id]['icq_status_img'],
1386 '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')),
1387 'S_ONLINE' => ($poster_id == ANONYMOUS || !$config['load_onlinetrack']) ? false : (($user_cache[$poster_id]['online']) ? true : false),
1389 'U_EDIT' => (!$user->data['is_registered']) ? '' : ((($user->data['user_id'] == $poster_id && $auth->acl_get('f_edit', $forum_id) && ($row['post_time'] > time() - ($config['edit_time'] * 60) || !$config['edit_time'])) || $auth->acl_get('m_edit', $forum_id)) ? append_sid("{$phpbb_root_path}posting.$phpEx", "mode=edit&amp;f=$forum_id&amp;p={$row['post_id']}") : ''),
1390 'U_QUOTE' => ($auth->acl_get('f_reply', $forum_id)) ? append_sid("{$phpbb_root_path}posting.$phpEx", "mode=quote&amp;f=$forum_id&amp;p={$row['post_id']}") : '',
1391 'U_INFO' => ($auth->acl_get('m_info', $forum_id)) ? append_sid("{$phpbb_root_path}mcp.$phpEx", "i=main&amp;mode=post_details&amp;f=$forum_id&amp;p=" . $row['post_id'], true, $user->session_id) : '',
1392 'U_DELETE' => (!$user->data['is_registered']) ? '' : ((($user->data['user_id'] == $poster_id && $auth->acl_get('f_delete', $forum_id) && $topic_data['topic_last_post_id'] == $row['post_id'] && ($row['post_time'] > time() - ($config['edit_time'] * 60) || !$config['edit_time'])) || $auth->acl_get('m_delete', $forum_id)) ? append_sid("{$phpbb_root_path}posting.$phpEx", "mode=delete&amp;f=$forum_id&amp;p={$row['post_id']}") : ''),
1394 'U_PROFILE' => $user_cache[$poster_id]['profile'],
1395 'U_SEARCH' => $user_cache[$poster_id]['search'],
1396 'U_PM' => ($poster_id != ANONYMOUS && $config['allow_privmsg'] && $auth->acl_get('u_sendpm') && ($user_cache[$poster_id]['allow_pm'] || $auth->acl_gets('a_', 'm_') || $auth->acl_getf_global('m_'))) ? append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&amp;mode=compose&amp;action=quotepost&amp;p=' . $row['post_id']) : '',
1397 'U_EMAIL' => $user_cache[$poster_id]['email'],
1398 'U_WWW' => $user_cache[$poster_id]['www'],
1399 'U_ICQ' => $user_cache[$poster_id]['icq'],
1400 'U_AIM' => $user_cache[$poster_id]['aim'],
1401 'U_MSN' => $user_cache[$poster_id]['msn'],
1402 'U_YIM' => $user_cache[$poster_id]['yim'],
1403 'U_JABBER' => $user_cache[$poster_id]['jabber'],
1405 'U_REPORT' => ($auth->acl_get('f_report', $forum_id)) ? append_sid("{$phpbb_root_path}report.$phpEx", 'f=' . $forum_id . '&amp;p=' . $row['post_id']) : '',
1406 'U_MCP_REPORT' => ($auth->acl_get('m_report', $forum_id)) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=reports&amp;mode=report_details&amp;f=' . $forum_id . '&amp;p=' . $row['post_id'], true, $user->session_id) : '',
1407 'U_MCP_APPROVE' => ($auth->acl_get('m_approve', $forum_id)) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=queue&amp;mode=approve_details&amp;f=' . $forum_id . '&amp;p=' . $row['post_id'], true, $user->session_id) : '',
1408 'U_MINI_POST' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'p=' . $row['post_id']) . (($topic_data['topic_type'] == POST_GLOBAL) ? '&amp;f=' . $forum_id : '') . '#p' . $row['post_id'],
1409 'U_NEXT_POST_ID' => ($i < $i_total && isset($rowset[$post_list[$i + 1]])) ? $rowset[$post_list[$i + 1]]['post_id'] : '',
1410 'U_PREV_POST_ID' => $prev_post_id,
1411 'U_NOTES' => ($auth->acl_getf_global('m_')) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=notes&amp;mode=user_notes&amp;u=' . $poster_id, true, $user->session_id) : '',
1412 'U_WARN' => ($auth->acl_get('m_warn') && $poster_id != $user->data['user_id'] && $poster_id != ANONYMOUS) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=warn&amp;mode=warn_post&amp;f=' . $forum_id . '&amp;p=' . $row['post_id'], true, $user->session_id) : '',
1414 'POST_ID' => $row['post_id'],
1415 'POSTER_ID' => $poster_id,
1417 'S_HAS_ATTACHMENTS' => (!empty($attachments[$row['post_id']])) ? true : false,
1418 'S_POST_UNAPPROVED' => ($row['post_approved']) ? false : true,
1419 'S_POST_REPORTED' => ($row['post_reported'] && $auth->acl_get('m_report', $forum_id)) ? true : false,
1420 'S_DISPLAY_NOTICE' => $display_notice && $row['post_attachment'],
1421 'S_FRIEND' => ($row['friend']) ? true : false,
1422 'S_UNREAD_POST' => $post_unread,
1423 'S_FIRST_UNREAD' => $s_first_unread,
1424 'S_CUSTOM_FIELDS' => (isset($cp_row['row']) && sizeof($cp_row['row'])) ? true : false,
1426 'S_IGNORE_POST' => ($row['hide_post']) ? true : false,
1427 '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>') : '',
1430 if (isset($cp_row['row']) && sizeof($cp_row['row']))
1432 $postrow = array_merge($postrow, $cp_row['row']);
1435 // Dump vars into template
1436 $template->assign_block_vars('postrow', $postrow);
1438 if (!empty($cp_row['blockrow']))
1440 foreach ($cp_row['blockrow'] as $field_data)
1442 $template->assign_block_vars('postrow.custom_fields', $field_data);
1446 // Display not already displayed Attachments for this post, we already parsed them. ;)
1447 if (!empty($attachments[$row['post_id']]))
1449 foreach ($attachments[$row['post_id']] as $attachment)
1451 $template->assign_block_vars('postrow.attachment', array(
1452 'DISPLAY_ATTACHMENT' => $attachment)
1457 $prev_post_id = $row['post_id'];
1459 unset($rowset[$post_list[$i]]);
1460 unset($attachments[$row['post_id']]);
1462 unset($rowset, $user_cache);
1464 // Update topic view and if necessary attachment view counters ... but only if this is the first 'page view'
1465 if (isset($user->data['session_page']) && strpos($user->data['session_page'], '&t=' . $topic_id) === false)
1467 $sql = 'UPDATE ' . TOPICS_TABLE . '
1468 SET topic_views = topic_views + 1, topic_last_view_time = ' . time() . "
1469 WHERE topic_id = $topic_id";
1470 $db->sql_query($sql);
1472 // Update the attachment download counts
1473 if (sizeof($update_count))
1475 $sql = 'UPDATE ' . ATTACHMENTS_TABLE . '
1476 SET download_count = download_count + 1
1477 WHERE ' . $db->sql_in_set('attach_id', array_unique($update_count));
1478 $db->sql_query($sql);
1482 // Only mark topic if it's currently unread. Also make sure we do not set topic tracking back if earlier pages are viewed.
1483 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])
1485 markread('topic', $forum_id, $topic_id, $max_post_time);
1487 // Update forum info
1488 $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);
1490 else
1492 $all_marked_read = true;
1495 // If there are absolutely no more unread posts in this forum and unread posts shown, we can savely show the #unread link
1496 if ($all_marked_read)
1498 if ($post_unread)
1500 $template->assign_vars(array(
1501 'U_VIEW_UNREAD_POST' => '#unread',
1504 else if (isset($topic_tracking_info[$topic_id]) && $topic_data['topic_last_post_time'] > $topic_tracking_info[$topic_id])
1506 $template->assign_vars(array(
1507 'U_VIEW_UNREAD_POST' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;view=unread") . '#unread',
1511 else if (!$all_marked_read)
1513 $last_page = ((floor($start / $config['posts_per_page']) + 1) == max(ceil($total_posts / $config['posts_per_page']), 1)) ? true : false;
1515 // What can happen is that we are at the last displayed page. If so, we also display the #unread link based in $post_unread
1516 if ($last_page && $post_unread)
1518 $template->assign_vars(array(
1519 'U_VIEW_UNREAD_POST' => '#unread',
1522 else if (!$last_page)
1524 $template->assign_vars(array(
1525 'U_VIEW_UNREAD_POST' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;view=unread") . '#unread',
1530 // We overwrite $_REQUEST['f'] if there is no forum specified
1531 // to be able to display the correct online list.
1532 // One downside is that the user currently viewing this topic/post is not taken into account.
1533 if (empty($_REQUEST['f']))
1535 $_REQUEST['f'] = $forum_id;
1538 // Output the page
1539 page_header($user->lang['VIEW_TOPIC'] .' - ' . $topic_data['topic_title']);
1541 $template->set_filenames(array(
1542 'body' => ($view == 'print') ? 'viewtopic_print.html' : 'viewtopic_body.html')
1544 make_jumpbox(append_sid("{$phpbb_root_path}viewforum.$phpEx"), $forum_id);
1546 page_footer();