fix for #12255 - do not allow empty cleaned usernames
[phpbb.git] / phpBB / viewtopic.php
blobe65388d35ecec7b6f44cfa918825029ec2245c2f
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.topic_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'] : $topic_data['topic_replies'];
256 else
258 $topic_data['prev_posts'] = 0;
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']) / $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 $word = str_replace('\*', '\w+?', preg_quote($word, '#'));
411 $word = preg_replace('#(^|\s)\\\\w\*\?(\s|$)#', '$1\w+?$2', $word);
412 $highlight_match .= (($highlight_match != '') ? '|' : '') . $word;
416 $highlight = urlencode($hilit_words);
419 // Make sure $start is set to the last page if it exceeds the amount
420 if ($start < 0 || $start > $total_posts)
422 $start = ($start < 0) ? 0 : floor(($total_posts - 1) / $config['posts_per_page']) * $config['posts_per_page'];
425 // General Viewtopic URL for return links
426 $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" : ''));
428 // Are we watching this topic?
429 $s_watching_topic = $s_watching_topic_img = array();
430 $s_watching_topic['link'] = $s_watching_topic['title'] = '';
431 $s_watching_topic['is_watching'] = false;
433 if ($config['email_enable'] && $config['allow_topic_notify'] && $user->data['is_registered'])
435 watch_topic_forum('topic', $s_watching_topic, $s_watching_topic_img, $user->data['user_id'], $forum_id, $topic_id, $topic_data['notify_status'], $start);
438 // Bookmarks
439 if ($config['allow_bookmarks'] && $user->data['is_registered'] && request_var('bookmark', 0))
441 if (!$topic_data['bookmarked'])
443 $sql = 'INSERT INTO ' . BOOKMARKS_TABLE . ' ' . $db->sql_build_array('INSERT', array(
444 'user_id' => $user->data['user_id'],
445 'topic_id' => $topic_id,
447 $db->sql_query($sql);
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);
457 meta_refresh(3, $viewtopic_url);
459 $message = (($topic_data['bookmarked']) ? $user->lang['BOOKMARK_REMOVED'] : $user->lang['BOOKMARK_ADDED']) . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $viewtopic_url . '">', '</a>');
460 trigger_error($message);
463 // Grab ranks
464 $ranks = $cache->obtain_ranks();
466 // Grab icons
467 $icons = $cache->obtain_icons();
469 // Grab extensions
470 $extensions = array();
471 if ($topic_data['topic_attachment'])
473 $extensions = $cache->obtain_attach_extensions($forum_id);
476 // Forum rules listing
477 $s_forum_rules = '';
478 gen_forum_auth_level('topic', $forum_id, $topic_data['forum_status']);
480 // Quick mod tools
481 $allow_change_type = ($auth->acl_get('m_', $forum_id) || ($user->data['is_registered'] && $user->data['user_id'] == $topic_data['topic_poster'])) ? true : false;
483 $topic_mod = '';
484 $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>') : '';
485 $topic_mod .= ($auth->acl_get('m_delete', $forum_id)) ? '<option value="delete_topic">' . $user->lang['DELETE_TOPIC'] . '</option>' : '';
486 $topic_mod .= ($auth->acl_get('m_move', $forum_id) && $topic_data['topic_status'] != ITEM_MOVED) ? '<option value="move">' . $user->lang['MOVE_TOPIC'] . '</option>' : '';
487 $topic_mod .= ($auth->acl_get('m_split', $forum_id)) ? '<option value="split">' . $user->lang['SPLIT_TOPIC'] . '</option>' : '';
488 $topic_mod .= ($auth->acl_get('m_merge', $forum_id)) ? '<option value="merge">' . $user->lang['MERGE_POSTS'] . '</option>' : '';
489 $topic_mod .= ($auth->acl_get('m_merge', $forum_id)) ? '<option value="merge_topic">' . $user->lang['MERGE_TOPIC'] . '</option>' : '';
490 $topic_mod .= ($auth->acl_get('m_move', $forum_id)) ? '<option value="fork">' . $user->lang['FORK_TOPIC'] . '</option>' : '';
491 $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>' : '';
492 $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>' : '';
493 $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>' : '';
494 $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>' : '';
495 $topic_mod .= ($auth->acl_get('m_', $forum_id)) ? '<option value="topic_logs">' . $user->lang['VIEW_TOPIC_LOGS'] . '</option>' : '';
497 // If we've got a hightlight set pass it on to pagination.
498 $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);
500 // Navigation links
501 generate_forum_nav($topic_data);
503 // Forum Rules
504 generate_forum_rules($topic_data);
506 // Moderators
507 $forum_moderators = array();
508 get_moderators($forum_moderators, $forum_id);
510 // This is only used for print view so ...
511 $server_path = (!$view) ? $phpbb_root_path : generate_board_url() . '/';
513 // Replace naughty words in title
514 $topic_data['topic_title'] = censor_text($topic_data['topic_title']);
516 // Send vars to template
517 $template->assign_vars(array(
518 'FORUM_ID' => $forum_id,
519 'FORUM_NAME' => $topic_data['forum_name'],
520 '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']),
521 'TOPIC_ID' => $topic_id,
522 'TOPIC_TITLE' => $topic_data['topic_title'],
523 'TOPIC_POSTER' => $topic_data['topic_poster'],
525 'TOPIC_AUTHOR_FULL' => get_username_string('full', $topic_data['topic_poster'], $topic_data['topic_first_poster_name'], $topic_data['topic_first_poster_colour']),
526 'TOPIC_AUTHOR_COLOUR' => get_username_string('colour', $topic_data['topic_poster'], $topic_data['topic_first_poster_name'], $topic_data['topic_first_poster_colour']),
527 'TOPIC_AUTHOR' => get_username_string('username', $topic_data['topic_poster'], $topic_data['topic_first_poster_name'], $topic_data['topic_first_poster_colour']),
529 'PAGINATION' => $pagination,
530 'PAGE_NUMBER' => on_page($total_posts, $config['posts_per_page'], $start),
531 'TOTAL_POSTS' => ($total_posts == 1) ? $user->lang['VIEW_TOPIC_POST'] : sprintf($user->lang['VIEW_TOPIC_POSTS'], $total_posts),
532 '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) : '',
533 'MODERATORS' => (isset($forum_moderators[$forum_id]) && sizeof($forum_moderators[$forum_id])) ? implode(', ', $forum_moderators[$forum_id]) : '',
535 'POST_IMG' => ($topic_data['forum_status'] == ITEM_LOCKED) ? $user->img('button_topic_locked', 'FORUM_LOCKED') : $user->img('button_topic_new', 'POST_NEW_TOPIC'),
536 'QUOTE_IMG' => $user->img('icon_post_quote', 'REPLY_WITH_QUOTE'),
537 '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'),
538 'EDIT_IMG' => $user->img('icon_post_edit', 'EDIT_POST'),
539 'DELETE_IMG' => $user->img('icon_post_delete', 'DELETE_POST'),
540 'INFO_IMG' => $user->img('icon_post_info', 'VIEW_INFO'),
541 'PROFILE_IMG' => $user->img('icon_user_profile', 'READ_PROFILE'),
542 'SEARCH_IMG' => $user->img('icon_user_search', 'SEARCH_USER_POSTS'),
543 'PM_IMG' => $user->img('icon_contact_pm', 'SEND_PRIVATE_MESSAGE'),
544 'EMAIL_IMG' => $user->img('icon_contact_email', 'SEND_EMAIL'),
545 'WWW_IMG' => $user->img('icon_contact_www', 'VISIT_WEBSITE'),
546 'ICQ_IMG' => $user->img('icon_contact_icq', 'ICQ'),
547 'AIM_IMG' => $user->img('icon_contact_aim', 'AIM'),
548 'MSN_IMG' => $user->img('icon_contact_msnm', 'MSNM'),
549 'YIM_IMG' => $user->img('icon_contact_yahoo', 'YIM'),
550 'JABBER_IMG' => $user->img('icon_contact_jabber', 'JABBER') ,
551 'REPORT_IMG' => $user->img('icon_post_report', 'REPORT_POST'),
552 'REPORTED_IMG' => $user->img('icon_topic_reported', 'POST_REPORTED'),
553 'UNAPPROVED_IMG' => $user->img('icon_topic_unapproved', 'POST_UNAPPROVED'),
554 'WARN_IMG' => $user->img('icon_user_warn', 'WARN_USER'),
556 'S_IS_LOCKED' =>($topic_data['topic_status'] == ITEM_UNLOCKED) ? false : true,
557 'S_SELECT_SORT_DIR' => $s_sort_dir,
558 'S_SELECT_SORT_KEY' => $s_sort_key,
559 'S_SELECT_SORT_DAYS' => $s_limit_days,
560 'S_SINGLE_MODERATOR' => (!empty($forum_moderators[$forum_id]) && sizeof($forum_moderators[$forum_id]) > 1) ? false : true,
561 'S_TOPIC_ACTION' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;start=$start"),
562 'S_TOPIC_MOD' => ($topic_mod != '') ? '<select name="action">' . $topic_mod . '</select>' : '',
563 '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),
565 'S_DISPLAY_SEARCHBOX' => ($auth->acl_get('u_search') && $auth->acl_get('f_search', $forum_id) && $config['load_search']) ? true : false,
566 'S_SEARCHBOX_ACTION' => append_sid("{$phpbb_root_path}search.$phpEx", 't=' . $topic_id),
568 'U_TOPIC' => "{$server_path}viewtopic.$phpEx?f=$forum_id&amp;t=$topic_id",
569 'U_FORUM' => $server_path,
570 'U_VIEW_TOPIC' => $viewtopic_url,
571 'U_VIEW_FORUM' => append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_id),
572 'U_VIEW_OLDER_TOPIC' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;view=previous"),
573 'U_VIEW_NEWER_TOPIC' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;view=next"),
574 'U_PRINT_TOPIC' => ($auth->acl_get('f_print', $forum_id)) ? $viewtopic_url . '&amp;view=print' : '',
575 '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") : '',
577 'U_WATCH_TOPIC' => $s_watching_topic['link'],
578 'L_WATCH_TOPIC' => $s_watching_topic['title'],
579 'S_WATCHING_TOPIC' => $s_watching_topic['is_watching'],
581 'U_BOOKMARK_TOPIC' => ($user->data['is_registered'] && $config['allow_bookmarks']) ? $viewtopic_url . '&amp;bookmark=1' : '',
582 'L_BOOKMARK_TOPIC' => ($user->data['is_registered'] && $config['allow_bookmarks'] && $topic_data['bookmarked']) ? $user->lang['BOOKMARK_TOPIC_REMOVE'] : $user->lang['BOOKMARK_TOPIC'],
584 'U_POST_NEW_TOPIC' => append_sid("{$phpbb_root_path}posting.$phpEx", "mode=post&amp;f=$forum_id"),
585 'U_POST_REPLY_TOPIC' => append_sid("{$phpbb_root_path}posting.$phpEx", "mode=reply&amp;f=$forum_id&amp;t=$topic_id"),
586 '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") : '')
589 // Does this topic contain a poll?
590 if (!empty($topic_data['poll_start']))
592 $sql = 'SELECT o.*, p.bbcode_bitfield, p.bbcode_uid
593 FROM ' . POLL_OPTIONS_TABLE . ' o, ' . POSTS_TABLE . " p
594 WHERE o.topic_id = $topic_id
595 AND p.post_id = {$topic_data['topic_first_post_id']}
596 AND p.topic_id = o.topic_id
597 ORDER BY o.poll_option_id";
598 $result = $db->sql_query($sql);
600 $poll_info = array();
601 while ($row = $db->sql_fetchrow($result))
603 $poll_info[] = $row;
605 $db->sql_freeresult($result);
607 $cur_voted_id = array();
608 if ($user->data['is_registered'])
610 $sql = 'SELECT poll_option_id
611 FROM ' . POLL_VOTES_TABLE . '
612 WHERE topic_id = ' . $topic_id . '
613 AND vote_user_id = ' . $user->data['user_id'];
614 $result = $db->sql_query($sql);
616 while ($row = $db->sql_fetchrow($result))
618 $cur_voted_id[] = $row['poll_option_id'];
620 $db->sql_freeresult($result);
622 else
624 // Cookie based guest tracking ... I don't like this but hum ho
625 // it's oft requested. This relies on "nice" users who don't feel
626 // the need to delete cookies to mess with results.
627 if (isset($_COOKIE[$config['cookie_name'] . '_poll_' . $topic_id]))
629 $cur_voted_id = explode(',', $_COOKIE[$config['cookie_name'] . '_poll_' . $topic_id]);
630 $cur_voted_id = array_map('intval', $cur_voted_id);
634 $s_can_vote = (((!sizeof($cur_voted_id) && $auth->acl_get('f_vote', $forum_id)) ||
635 ($auth->acl_get('f_votechg', $forum_id) && $topic_data['poll_vote_change'])) &&
636 (($topic_data['poll_length'] != 0 && $topic_data['poll_start'] + $topic_data['poll_length'] > time()) || $topic_data['poll_length'] == 0) &&
637 $topic_data['topic_status'] != ITEM_LOCKED &&
638 $topic_data['forum_status'] != ITEM_LOCKED) ? true : false;
639 $s_display_results = (!$s_can_vote || ($s_can_vote && sizeof($cur_voted_id)) || $view == 'viewpoll') ? true : false;
641 if ($update && $s_can_vote)
643 if (!sizeof($voted_id) || sizeof($voted_id) > $topic_data['poll_max_options'])
645 $redirect_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id");
647 meta_refresh(5, $redirect_url);
649 $message = (!sizeof($voted_id)) ? 'NO_VOTE_OPTION' : 'TOO_MANY_VOTE_OPTIONS';
650 $message = $user->lang[$message] . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $redirect_url . '">', '</a>');
651 trigger_error($message);
654 foreach ($voted_id as $option)
656 if (in_array($option, $cur_voted_id))
658 continue;
661 $sql = 'UPDATE ' . POLL_OPTIONS_TABLE . '
662 SET poll_option_total = poll_option_total + 1
663 WHERE poll_option_id = ' . (int) $option . '
664 AND topic_id = ' . (int) $topic_id;
665 $db->sql_query($sql);
667 if ($user->data['is_registered'])
669 $sql_ary = array(
670 'topic_id' => (int) $topic_id,
671 'poll_option_id' => (int) $option,
672 'vote_user_id' => (int) $user->data['user_id'],
673 'vote_user_ip' => (string) $user->ip,
676 $sql = 'INSERT INTO ' . POLL_VOTES_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
677 $db->sql_query($sql);
681 foreach ($cur_voted_id as $option)
683 if (!in_array($option, $voted_id))
685 $sql = 'UPDATE ' . POLL_OPTIONS_TABLE . '
686 SET poll_option_total = poll_option_total - 1
687 WHERE poll_option_id = ' . (int) $option . '
688 AND topic_id = ' . (int) $topic_id;
689 $db->sql_query($sql);
691 if ($user->data['is_registered'])
693 $sql = 'DELETE FROM ' . POLL_VOTES_TABLE . '
694 WHERE topic_id = ' . (int) $topic_id . '
695 AND poll_option_id = ' . (int) $option . '
696 AND vote_user_id = ' . (int) $user->data['user_id'];
697 $db->sql_query($sql);
702 if ($user->data['user_id'] == ANONYMOUS && !$user->data['is_bot'])
704 $user->set_cookie('poll_' . $topic_id, implode(',', $voted_id), time() + 31536000);
707 $sql = 'UPDATE ' . TOPICS_TABLE . '
708 SET poll_last_vote = ' . time() . "
709 WHERE topic_id = $topic_id";
710 //, topic_last_post_time = ' . time() . " -- for bumping topics with new votes, ignore for now
711 $db->sql_query($sql);
713 $redirect_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id");
715 meta_refresh(5, $redirect_url);
716 trigger_error($user->lang['VOTE_SUBMITTED'] . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $redirect_url . '">', '</a>'));
719 $poll_total = 0;
720 foreach ($poll_info as $poll_option)
722 $poll_total += $poll_option['poll_option_total'];
725 if ($poll_info[0]['bbcode_bitfield'])
727 $poll_bbcode = new bbcode();
729 else
731 $poll_bbcode = false;
734 for ($i = 0, $size = sizeof($poll_info); $i < $size; $i++)
736 $poll_info[$i]['poll_option_text'] = censor_text($poll_info[$i]['poll_option_text']);
737 $poll_info[$i]['poll_option_text'] = str_replace("\n", '<br />', $poll_info[$i]['poll_option_text']);
739 if ($poll_bbcode !== false)
741 $poll_bbcode->bbcode_second_pass($poll_info[$i]['poll_option_text'], $poll_info[$i]['bbcode_uid'], $poll_option['bbcode_bitfield']);
744 $poll_info[$i]['poll_option_text'] = smiley_text($poll_info[$i]['poll_option_text']);
747 $topic_data['poll_title'] = censor_text($topic_data['poll_title']);
748 $topic_data['poll_title'] = str_replace("\n", '<br />', $topic_data['poll_title']);
750 if ($poll_bbcode !== false)
752 $poll_bbcode->bbcode_second_pass($topic_data['poll_title'], $poll_info[0]['bbcode_uid'], $poll_info[0]['bbcode_bitfield']);
754 $topic_data['poll_title'] = smiley_text($topic_data['poll_title']);
756 unset($poll_bbcode);
758 foreach ($poll_info as $poll_option)
760 $option_pct = ($poll_total > 0) ? $poll_option['poll_option_total'] / $poll_total : 0;
761 $option_pct_txt = sprintf("%.1d%%", ($option_pct * 100));
763 $template->assign_block_vars('poll_option', array(
764 'POLL_OPTION_ID' => $poll_option['poll_option_id'],
765 'POLL_OPTION_CAPTION' => $poll_option['poll_option_text'],
766 'POLL_OPTION_RESULT' => $poll_option['poll_option_total'],
767 'POLL_OPTION_PERCENT' => $option_pct_txt,
768 'POLL_OPTION_PCT' => round($option_pct * 100),
769 'POLL_OPTION_IMG' => $user->img('poll_center', $option_pct_txt, round($option_pct * 250)),
770 'POLL_OPTION_VOTED' => (in_array($poll_option['poll_option_id'], $cur_voted_id)) ? true : false)
774 $poll_end = $topic_data['poll_length'] + $topic_data['poll_start'];
776 $template->assign_vars(array(
777 'POLL_QUESTION' => $topic_data['poll_title'],
778 'TOTAL_VOTES' => $poll_total,
779 'POLL_LEFT_CAP_IMG' => $user->img('poll_left'),
780 'POLL_RIGHT_CAP_IMG'=> $user->img('poll_right'),
782 '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']),
783 'L_POLL_LENGTH' => ($topic_data['poll_length']) ? sprintf($user->lang[($poll_end > time()) ? 'POLL_RUN_TILL' : 'POLL_ENDED_AT'], $user->format_date($poll_end)) : '',
785 'S_HAS_POLL' => true,
786 'S_CAN_VOTE' => $s_can_vote,
787 'S_DISPLAY_RESULTS' => $s_display_results,
788 'S_IS_MULTI_CHOICE' => ($topic_data['poll_max_options'] > 1) ? true : false,
789 'S_POLL_ACTION' => $viewtopic_url,
791 'U_VIEW_RESULTS' => $viewtopic_url . '&amp;view=viewpoll')
794 unset($poll_end, $poll_info, $voted_id);
797 // If the user is trying to reach the second half of the topic, fetch it starting from the end
798 $store_reverse = false;
799 $sql_limit = $config['posts_per_page'];
801 if ($start > $total_posts / 2)
803 $store_reverse = true;
805 if ($start + $config['posts_per_page'] > $total_posts)
807 $sql_limit = min($config['posts_per_page'], max(1, $total_posts - $start));
810 // Select the sort order
811 $sql_sort_order = $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'ASC' : 'DESC');
812 $sql_start = max(0, $total_posts - $sql_limit - $start);
814 else
816 // Select the sort order
817 $sql_sort_order = $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'DESC' : 'ASC');
818 $sql_start = $start;
821 // Container for user details, only process once
822 $post_list = $user_cache = $id_cache = $attachments = $attach_list = $rowset = $update_count = $post_edit_list = array();
823 $has_attachments = $display_notice = false;
824 $bbcode_bitfield = '';
825 $i = $i_total = 0;
827 // Go ahead and pull all data for this topic
828 $sql = 'SELECT p.post_id
829 FROM ' . POSTS_TABLE . ' p' . (($sort_by_sql[$sort_key][0] == 'u') ? ', ' . USERS_TABLE . ' u': '') . "
830 WHERE p.topic_id = $topic_id
831 " . ((!$auth->acl_get('m_approve', $forum_id)) ? 'AND p.post_approved = 1' : '') . "
832 " . (($sort_by_sql[$sort_key][0] == 'u') ? 'AND u.user_id = p.poster_id': '') . "
833 $limit_posts_time
834 ORDER BY $sql_sort_order";
835 $result = $db->sql_query_limit($sql, $sql_limit, $sql_start);
837 $i = ($store_reverse) ? $sql_limit - 1 : 0;
838 while ($row = $db->sql_fetchrow($result))
840 $post_list[$i] = $row['post_id'];
841 ($store_reverse) ? $i-- : $i++;
843 $db->sql_freeresult($result);
845 if (!sizeof($post_list))
847 if ($sort_days)
849 trigger_error('NO_POSTS_TIME_FRAME');
851 else
853 trigger_error('NO_TOPIC');
857 // Holding maximum post time for marking topic read
858 // We need to grab it because we do reverse ordering sometimes
859 $max_post_time = 0;
861 $sql = $db->sql_build_query('SELECT', array(
862 'SELECT' => 'u.*, z.friend, z.foe, p.*',
864 'FROM' => array(
865 USERS_TABLE => 'u',
866 POSTS_TABLE => 'p',
869 'LEFT_JOIN' => array(
870 array(
871 'FROM' => array(ZEBRA_TABLE => 'z'),
872 'ON' => 'z.user_id = ' . $user->data['user_id'] . ' AND z.zebra_id = p.poster_id'
876 'WHERE' => $db->sql_in_set('p.post_id', $post_list) . '
877 AND u.user_id = p.poster_id'
880 $result = $db->sql_query($sql);
882 $now = getdate(time() + $user->timezone + $user->dst - date('Z'));
884 // Posts are stored in the $rowset array while $attach_list, $user_cache
885 // and the global bbcode_bitfield are built
886 while ($row = $db->sql_fetchrow($result))
888 // Set max_post_time
889 if ($row['post_time'] > $max_post_time)
891 $max_post_time = $row['post_time'];
894 $poster_id = $row['poster_id'];
896 // Does post have an attachment? If so, add it to the list
897 if ($row['post_attachment'] && $config['allow_attachments'])
899 $attach_list[] = $row['post_id'];
901 if ($row['post_approved'])
903 $has_attachments = true;
907 $rowset[$row['post_id']] = array(
908 'hide_post' => ($row['foe'] && ($view != 'show' || $post_id != $row['post_id'])) ? true : false,
910 'post_id' => $row['post_id'],
911 'post_time' => $row['post_time'],
912 'user_id' => $row['user_id'],
913 'username' => $row['username'],
914 'user_colour' => $row['user_colour'],
915 'topic_id' => $row['topic_id'],
916 'forum_id' => $row['forum_id'],
917 'post_subject' => $row['post_subject'],
918 'post_edit_count' => $row['post_edit_count'],
919 'post_edit_time' => $row['post_edit_time'],
920 'post_edit_reason' => $row['post_edit_reason'],
921 'post_edit_user' => $row['post_edit_user'],
923 // Make sure the icon actually exists
924 'icon_id' => (isset($icons[$row['icon_id']]['img'], $icons[$row['icon_id']]['height'], $icons[$row['icon_id']]['width'])) ? $row['icon_id'] : 0,
925 'post_attachment' => $row['post_attachment'],
926 'post_approved' => $row['post_approved'],
927 'post_reported' => $row['post_reported'],
928 'post_username' => $row['post_username'],
929 'post_text' => $row['post_text'],
930 'bbcode_uid' => $row['bbcode_uid'],
931 'bbcode_bitfield' => $row['bbcode_bitfield'],
932 'enable_smilies' => $row['enable_smilies'],
933 'enable_sig' => $row['enable_sig'],
934 'friend' => $row['friend'],
935 'foe' => $row['foe'],
938 // Define the global bbcode bitfield, will be used to load bbcodes
939 $bbcode_bitfield = $bbcode_bitfield | base64_decode($row['bbcode_bitfield']);
941 // Is a signature attached? Are we going to display it?
942 if ($row['enable_sig'] && $config['allow_sig'] && $user->optionget('viewsigs'))
944 $bbcode_bitfield = $bbcode_bitfield | base64_decode($row['user_sig_bbcode_bitfield']);
947 // Cache various user specific data ... so we don't have to recompute
948 // this each time the same user appears on this page
949 if (!isset($user_cache[$poster_id]))
951 if ($poster_id == ANONYMOUS)
953 $user_cache[$poster_id] = array(
954 'joined' => '',
955 'posts' => '',
956 'from' => '',
958 'sig' => '',
959 'sig_bbcode_uid' => '',
960 'sig_bbcode_bitfield' => '',
962 'online' => false,
963 'avatar' => '',
964 'rank_title' => '',
965 'rank_image' => '',
966 'rank_image_src' => '',
967 'sig' => '',
968 'posts' => '',
969 'profile' => '',
970 'pm' => '',
971 'email' => '',
972 'www' => '',
973 'icq_status_img' => '',
974 'icq' => '',
975 'aim' => '',
976 'msn' => '',
977 'yim' => '',
978 'jabber' => '',
979 'search' => '',
980 'age' => '',
982 'username' => $row['username'],
983 'user_colour' => $row['user_colour'],
985 'warnings' => 0,
986 'allow_pm' => 0,
989 else
991 $user_sig = '';
993 // We add the signature to every posters entry because enable_sig is post dependant
994 if ($row['user_sig'] && $config['allow_sig'] && $user->optionget('viewsigs'))
996 $user_sig = $row['user_sig'];
999 $id_cache[] = $poster_id;
1001 $user_cache[$poster_id] = array(
1002 'joined' => $user->format_date($row['user_regdate']),
1003 'posts' => $row['user_posts'],
1004 'warnings' => (isset($row['user_warnings'])) ? $row['user_warnings'] : 0,
1005 'from' => (!empty($row['user_from'])) ? $row['user_from'] : '',
1007 'sig' => $user_sig,
1008 'sig_bbcode_uid' => (!empty($row['user_sig_bbcode_uid'])) ? $row['user_sig_bbcode_uid'] : '',
1009 'sig_bbcode_bitfield' => (!empty($row['user_sig_bbcode_bitfield'])) ? $row['user_sig_bbcode_bitfield'] : '',
1011 'viewonline' => $row['user_allow_viewonline'],
1012 'allow_pm' => $row['user_allow_pm'],
1014 'avatar' => ($user->optionget('viewavatars')) ? get_user_avatar($row['user_avatar'], $row['user_avatar_type'], $row['user_avatar_width'], $row['user_avatar_height']) : '',
1015 'age' => '',
1017 'rank_title' => '',
1018 'rank_image' => '',
1019 'rank_image_src' => '',
1021 'username' => $row['username'],
1022 'user_colour' => $row['user_colour'],
1024 'online' => false,
1025 'profile' => append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=viewprofile&amp;u=$poster_id"),
1026 'www' => $row['user_website'],
1027 '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") : '',
1028 '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") : '',
1029 'yim' => ($row['user_yim']) ? 'http://edit.yahoo.com/config/send_webmesg?.target=' . $row['user_yim'] . '&amp;.src=pg' : '',
1030 '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") : '',
1031 'search' => ($auth->acl_get('u_search')) ? append_sid("{$phpbb_root_path}search.$phpEx", 'search_author=' . urlencode($row['username']) .'&amp;showresults=posts') : '',
1034 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']);
1036 if (!empty($row['user_allow_viewemail']) || $auth->acl_get('a_email'))
1038 $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']);
1040 else
1042 $user_cache[$poster_id]['email'] = '';
1045 if (!empty($row['user_icq']))
1047 $user_cache[$poster_id]['icq'] = 'http://www.icq.com/people/webmsg.php?to=' . $row['user_icq'];
1048 $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="" />';
1050 else
1052 $user_cache[$poster_id]['icq_status_img'] = '';
1053 $user_cache[$poster_id]['icq'] = '';
1056 if (!empty($row['user_birthday']))
1058 list($bday_day, $bday_month, $bday_year) = array_map('intval', explode('-', $row['user_birthday']));
1060 if ($bday_year)
1062 $diff = $now['mon'] - $bday_month;
1063 if ($diff == 0)
1065 $diff = ($now['mday'] - $bday_day < 0) ? 1 : 0;
1067 else
1069 $diff = ($diff < 0) ? 1 : 0;
1072 $user_cache[$poster_id]['age'] = (int) ($now['year'] - $bday_year - $diff);
1078 $db->sql_freeresult($result);
1079 unset($today);
1081 // Load custom profile fields
1082 if ($config['load_cpf_viewtopic'])
1084 include($phpbb_root_path . 'includes/functions_profile_fields.' . $phpEx);
1085 $cp = new custom_profile();
1087 // Grab all profile fields from users in id cache for later use - similar to the poster cache
1088 $profile_fields_cache = $cp->generate_profile_fields_template('grab', $id_cache);
1091 // Generate online information for user
1092 if ($config['load_onlinetrack'] && sizeof($id_cache))
1094 $sql = 'SELECT session_user_id, MAX(session_time) as online_time, MIN(session_viewonline) AS viewonline
1095 FROM ' . SESSIONS_TABLE . '
1096 WHERE ' . $db->sql_in_set('session_user_id', $id_cache) . '
1097 GROUP BY session_user_id';
1098 $result = $db->sql_query($sql);
1100 $update_time = $config['load_online_time'] * 60;
1101 while ($row = $db->sql_fetchrow($result))
1103 $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;
1105 $db->sql_freeresult($result);
1107 unset($id_cache);
1109 // Pull attachment data
1110 if (sizeof($attach_list))
1112 if ($auth->acl_get('u_download') && $auth->acl_get('f_download', $forum_id))
1114 $sql = 'SELECT *
1115 FROM ' . ATTACHMENTS_TABLE . '
1116 WHERE ' . $db->sql_in_set('post_msg_id', $attach_list) . '
1117 AND in_message = 0
1118 ORDER BY filetime DESC, post_msg_id ASC';
1119 $result = $db->sql_query($sql);
1121 while ($row = $db->sql_fetchrow($result))
1123 $attachments[$row['post_msg_id']][] = $row;
1125 $db->sql_freeresult($result);
1127 // No attachments exist, but post table thinks they do so go ahead and reset post_attach flags
1128 if (!sizeof($attachments))
1130 $sql = 'UPDATE ' . POSTS_TABLE . '
1131 SET post_attachment = 0
1132 WHERE ' . $db->sql_in_set('post_id', $attach_list);
1133 $db->sql_query($sql);
1135 // We need to update the topic indicator too if the complete topic is now without an attachment
1136 if (sizeof($rowset) != $total_posts)
1138 // Not all posts are displayed so we query the db to find if there's any attachment for this topic
1139 $sql = 'SELECT a.post_msg_id as post_id
1140 FROM ' . ATTACHMENTS_TABLE . ' a, ' . POSTS_TABLE . " p
1141 WHERE p.topic_id = $topic_id
1142 AND p.post_approved = 1
1143 AND p.topic_id = a.topic_id";
1144 $result = $db->sql_query_limit($sql, 1);
1145 $row = $db->sql_fetchrow($result);
1146 $db->sql_freeresult($result);
1148 if (!$row)
1150 $sql = 'UPDATE ' . TOPICS_TABLE . "
1151 SET topic_attachment = 0
1152 WHERE topic_id = $topic_id";
1153 $db->sql_query($sql);
1156 else
1158 $sql = 'UPDATE ' . TOPICS_TABLE . "
1159 SET topic_attachment = 0
1160 WHERE topic_id = $topic_id";
1161 $db->sql_query($sql);
1164 else if ($has_attachments && !$topic_data['topic_attachment'])
1166 // Topic has approved attachments but its flag is wrong
1167 $sql = 'UPDATE ' . TOPICS_TABLE . "
1168 SET topic_attachment = 1
1169 WHERE topic_id = $topic_id";
1170 $db->sql_query($sql);
1172 $topic_data['topic_attachment'] = 1;
1175 else
1177 $display_notice = true;
1181 // Instantiate BBCode if need be
1182 if ($bbcode_bitfield !== '')
1184 $bbcode = new bbcode(base64_encode($bbcode_bitfield));
1187 $i_total = sizeof($rowset) - 1;
1188 $prev_post_id = '';
1190 $template->assign_vars(array(
1191 'S_NUM_POSTS' => sizeof($post_list))
1194 // Output the posts
1195 $first_unread = $post_unread = false;
1196 for ($i = 0, $end = sizeof($post_list); $i < $end; ++$i)
1198 // A non-existing rowset only happens if there was no user present for the entered poster_id
1199 // This could be a broken posts table.
1200 if (!isset($rowset[$post_list[$i]]))
1202 continue;
1205 $row =& $rowset[$post_list[$i]];
1206 $poster_id = $row['user_id'];
1208 // End signature parsing, only if needed
1209 if ($user_cache[$poster_id]['sig'] && $row['enable_sig'] && empty($user_cache[$poster_id]['sig_parsed']))
1211 $user_cache[$poster_id]['sig'] = censor_text($user_cache[$poster_id]['sig']);
1212 $user_cache[$poster_id]['sig'] = str_replace("\n", '<br />', $user_cache[$poster_id]['sig']);
1214 if ($user_cache[$poster_id]['sig_bbcode_bitfield'])
1216 $bbcode->bbcode_second_pass($user_cache[$poster_id]['sig'], $user_cache[$poster_id]['sig_bbcode_uid'], $user_cache[$poster_id]['sig_bbcode_bitfield']);
1219 $user_cache[$poster_id]['sig'] = smiley_text($user_cache[$poster_id]['sig']);
1220 $user_cache[$poster_id]['sig_parsed'] = true;
1223 // Parse the message and subject
1224 $message = censor_text($row['post_text']);
1226 // Second parse bbcode here
1227 if ($row['bbcode_bitfield'])
1229 $bbcode->bbcode_second_pass($message, $row['bbcode_uid'], $row['bbcode_bitfield']);
1232 $message = str_replace("\n", '<br />', $message);
1234 // Always process smilies after parsing bbcodes
1235 $message = smiley_text($message);
1237 if (!empty($attachments[$row['post_id']]))
1239 parse_attachments($forum_id, $message, $attachments[$row['post_id']], $update_count);
1242 // Highlight active words (primarily for search)
1243 if ($highlight_match)
1245 $message = preg_replace('#(?!<.*)(?<!\w)(' . $highlight_match . ')(?!\w|[^<>]*(?:</s(?:cript|tyle))?>)#is', '<span class="posthilit">\1</span>', $message);
1248 // Replace naughty words such as farty pants
1249 $row['post_subject'] = censor_text($row['post_subject']);
1251 // Editing information
1252 if (($row['post_edit_count'] && $config['display_last_edited']) || $row['post_edit_reason'])
1254 // Get usernames for all following posts if not already stored
1255 if (!sizeof($post_edit_list) && ($row['post_edit_reason'] || ($row['post_edit_user'] && !isset($user_cache[$row['post_edit_user']]))))
1257 // Remove all post_ids already parsed (we do not have to check them)
1258 $post_storage_list = (!$store_reverse) ? array_slice($post_list, $i) : array_slice(array_reverse($post_list), $i);
1260 $sql = 'SELECT DISTINCT u.user_id, u.username, u.user_colour
1261 FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
1262 WHERE ' . $db->sql_in_set('p.post_id', $post_storage_list) . '
1263 AND p.post_edit_count <> 0
1264 AND p.post_edit_user <> 0
1265 AND p.post_edit_user = u.user_id';
1266 $result2 = $db->sql_query($sql);
1267 while ($user_edit_row = $db->sql_fetchrow($result2))
1269 $post_edit_list[$user_edit_row['user_id']] = $user_edit_row;
1271 $db->sql_freeresult($result2);
1273 unset($post_storage_list);
1276 $l_edit_time_total = ($row['post_edit_count'] == 1) ? $user->lang['EDITED_TIME_TOTAL'] : $user->lang['EDITED_TIMES_TOTAL'];
1278 if ($row['post_edit_reason'])
1280 // User having edited the post also being the post author?
1281 if (!$row['post_edit_user'] || $row['post_edit_user'] == $poster_id)
1283 $display_username = get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']);
1285 else
1287 $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']);
1290 $l_edited_by = sprintf($l_edit_time_total, $display_username, $user->format_date($row['post_edit_time']), $row['post_edit_count']);
1292 else
1294 if ($row['post_edit_user'] && !isset($user_cache[$row['post_edit_user']]))
1296 $user_cache[$row['post_edit_user']] = $post_edit_list[$row['post_edit_user']];
1299 // User having edited the post also being the post author?
1300 if (!$row['post_edit_user'] || $row['post_edit_user'] == $poster_id)
1302 $display_username = get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']);
1304 else
1306 $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']);
1309 $l_edited_by = sprintf($l_edit_time_total, $display_username, $user->format_date($row['post_edit_time']), $row['post_edit_count']);
1312 else
1314 $l_edited_by = '';
1317 // Bump information
1318 if ($topic_data['topic_bumped'] && $row['post_id'] == $topic_data['topic_last_post_id'] && isset($user_cache[$topic_data['topic_bumper']]) )
1320 // It is safe to grab the username from the user cache array, we are at the last
1321 // post and only the topic poster and last poster are allowed to bump.
1322 // Admins and mods are bound to the above rules too...
1323 $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']));
1325 else
1327 $l_bumped_by = '';
1330 $cp_row = array();
1333 if ($config['load_cpf_viewtopic'])
1335 $cp_row = (isset($profile_fields_cache[$poster_id])) ? $cp->generate_profile_fields_template('show', false, $profile_fields_cache[$poster_id]) : array();
1338 $post_unread = (isset($topic_tracking_info[$topic_id]) && $row['post_time'] > $topic_tracking_info[$topic_id]) ? true : false;
1340 $s_first_unread = false;
1341 if (!$first_unread && $post_unread)
1343 $s_first_unread = $first_unread = true;
1347 $postrow = array(
1348 'POST_AUTHOR_FULL' => get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
1349 'POST_AUTHOR_COLOUR' => get_username_string('colour', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
1350 'POST_AUTHOR' => get_username_string('username', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
1351 'U_POST_AUTHOR' => get_username_string('profile', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
1353 'RANK_TITLE' => $user_cache[$poster_id]['rank_title'],
1354 'RANK_IMG' => $user_cache[$poster_id]['rank_image'],
1355 'RANK_IMG_SRC' => $user_cache[$poster_id]['rank_image_src'],
1356 'POSTER_JOINED' => $user_cache[$poster_id]['joined'],
1357 'POSTER_POSTS' => $user_cache[$poster_id]['posts'],
1358 'POSTER_FROM' => $user_cache[$poster_id]['from'],
1359 'POSTER_AVATAR' => $user_cache[$poster_id]['avatar'],
1360 'POSTER_WARNINGS' => $user_cache[$poster_id]['warnings'],
1361 'POSTER_AGE' => $user_cache[$poster_id]['age'],
1363 'POST_DATE' => $user->format_date($row['post_time']),
1364 'POST_SUBJECT' => $row['post_subject'],
1365 'MESSAGE' => $message,
1366 'SIGNATURE' => ($row['enable_sig']) ? $user_cache[$poster_id]['sig'] : '',
1367 'EDITED_MESSAGE' => $l_edited_by,
1368 'EDIT_REASON' => $row['post_edit_reason'],
1369 'BUMPED_MESSAGE' => $l_bumped_by,
1371 'MINI_POST_IMG' => ($post_unread) ? $user->img('icon_post_target_unread', 'NEW_POST') : $user->img('icon_post_target', 'POST'),
1372 'POST_ICON_IMG' => ($topic_data['enable_icons'] && !empty($row['icon_id'])) ? $icons[$row['icon_id']]['img'] : '',
1373 'POST_ICON_IMG_WIDTH' => ($topic_data['enable_icons'] && !empty($row['icon_id'])) ? $icons[$row['icon_id']]['width'] : '',
1374 'POST_ICON_IMG_HEIGHT' => ($topic_data['enable_icons'] && !empty($row['icon_id'])) ? $icons[$row['icon_id']]['height'] : '',
1375 'ICQ_STATUS_IMG' => $user_cache[$poster_id]['icq_status_img'],
1376 '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')),
1377 'S_ONLINE' => ($poster_id == ANONYMOUS || !$config['load_onlinetrack']) ? false : (($user_cache[$poster_id]['online']) ? true : false),
1379 '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']}") : ''),
1380 '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']}") : '',
1381 '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) : '',
1382 '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']}") : ''),
1384 'U_PROFILE' => $user_cache[$poster_id]['profile'],
1385 'U_SEARCH' => $user_cache[$poster_id]['search'],
1386 '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']) : '',
1387 'U_EMAIL' => $user_cache[$poster_id]['email'],
1388 'U_WWW' => $user_cache[$poster_id]['www'],
1389 'U_ICQ' => $user_cache[$poster_id]['icq'],
1390 'U_AIM' => $user_cache[$poster_id]['aim'],
1391 'U_MSN' => $user_cache[$poster_id]['msn'],
1392 'U_YIM' => $user_cache[$poster_id]['yim'],
1393 'U_JABBER' => $user_cache[$poster_id]['jabber'],
1395 'U_REPORT' => ($auth->acl_get('f_report', $forum_id)) ? append_sid("{$phpbb_root_path}report.$phpEx", 'f=' . $forum_id . '&amp;p=' . $row['post_id']) : '',
1396 '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) : '',
1397 '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) : '',
1398 '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'],
1399 'U_NEXT_POST_ID' => ($i < $i_total && isset($rowset[$post_list[$i + 1]])) ? $rowset[$post_list[$i + 1]]['post_id'] : '',
1400 'U_PREV_POST_ID' => $prev_post_id,
1401 '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) : '',
1402 '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) : '',
1404 'POST_ID' => $row['post_id'],
1405 'POSTER_ID' => $poster_id,
1407 'S_HAS_ATTACHMENTS' => (!empty($attachments[$row['post_id']])) ? true : false,
1408 'S_POST_UNAPPROVED' => ($row['post_approved']) ? false : true,
1409 'S_POST_REPORTED' => ($row['post_reported'] && $auth->acl_get('m_report', $forum_id)) ? true : false,
1410 'S_DISPLAY_NOTICE' => $display_notice && $row['post_attachment'],
1411 'S_FRIEND' => ($row['friend']) ? true : false,
1412 'S_UNREAD_POST' => $post_unread,
1413 'S_FIRST_UNREAD' => $s_first_unread,
1414 'S_CUSTOM_FIELDS' => (isset($cp_row['row']) && sizeof($cp_row['row'])) ? true : false,
1415 'S_TOPIC_POSTER' => ($topic_data['topic_poster'] == $poster_id) ? true : false,
1417 'S_IGNORE_POST' => ($row['hide_post']) ? true : false,
1418 '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>') : '',
1421 if (isset($cp_row['row']) && sizeof($cp_row['row']))
1423 $postrow = array_merge($postrow, $cp_row['row']);
1426 // Dump vars into template
1427 $template->assign_block_vars('postrow', $postrow);
1429 if (!empty($cp_row['blockrow']))
1431 foreach ($cp_row['blockrow'] as $field_data)
1433 $template->assign_block_vars('postrow.custom_fields', $field_data);
1437 // Display not already displayed Attachments for this post, we already parsed them. ;)
1438 if (!empty($attachments[$row['post_id']]))
1440 foreach ($attachments[$row['post_id']] as $attachment)
1442 $template->assign_block_vars('postrow.attachment', array(
1443 'DISPLAY_ATTACHMENT' => $attachment)
1448 $prev_post_id = $row['post_id'];
1450 unset($rowset[$post_list[$i]]);
1451 unset($attachments[$row['post_id']]);
1453 unset($rowset, $user_cache);
1455 // Update topic view and if necessary attachment view counters ... but only if this is the first 'page view'
1456 if (isset($user->data['session_page']) && strpos($user->data['session_page'], '&t=' . $topic_id) === false)
1458 $sql = 'UPDATE ' . TOPICS_TABLE . '
1459 SET topic_views = topic_views + 1, topic_last_view_time = ' . time() . "
1460 WHERE topic_id = $topic_id";
1461 $db->sql_query($sql);
1463 // Update the attachment download counts
1464 if (sizeof($update_count))
1466 $sql = 'UPDATE ' . ATTACHMENTS_TABLE . '
1467 SET download_count = download_count + 1
1468 WHERE ' . $db->sql_in_set('attach_id', array_unique($update_count));
1469 $db->sql_query($sql);
1473 // Only mark topic if it's currently unread. Also make sure we do not set topic tracking back if earlier pages are viewed.
1474 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])
1476 markread('topic', $forum_id, $topic_id, $max_post_time);
1478 // Update forum info
1479 $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);
1481 else
1483 $all_marked_read = true;
1486 // If there are absolutely no more unread posts in this forum and unread posts shown, we can savely show the #unread link
1487 if ($all_marked_read)
1489 if ($post_unread)
1491 $template->assign_vars(array(
1492 'U_VIEW_UNREAD_POST' => '#unread',
1495 else if (isset($topic_tracking_info[$topic_id]) && $topic_data['topic_last_post_time'] > $topic_tracking_info[$topic_id])
1497 $template->assign_vars(array(
1498 'U_VIEW_UNREAD_POST' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;view=unread") . '#unread',
1502 else if (!$all_marked_read)
1504 $last_page = ((floor($start / $config['posts_per_page']) + 1) == max(ceil($total_posts / $config['posts_per_page']), 1)) ? true : false;
1506 // What can happen is that we are at the last displayed page. If so, we also display the #unread link based in $post_unread
1507 if ($last_page && $post_unread)
1509 $template->assign_vars(array(
1510 'U_VIEW_UNREAD_POST' => '#unread',
1513 else if (!$last_page)
1515 $template->assign_vars(array(
1516 'U_VIEW_UNREAD_POST' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;view=unread") . '#unread',
1521 // We overwrite $_REQUEST['f'] if there is no forum specified
1522 // to be able to display the correct online list.
1523 // One downside is that the user currently viewing this topic/post is not taken into account.
1524 if (empty($_REQUEST['f']))
1526 $_REQUEST['f'] = $forum_id;
1529 // Output the page
1530 page_header($user->lang['VIEW_TOPIC'] .' - ' . $topic_data['topic_title']);
1532 $template->set_filenames(array(
1533 'body' => ($view == 'print') ? 'viewtopic_print.html' : 'viewtopic_body.html')
1535 make_jumpbox(append_sid("{$phpbb_root_path}viewforum.$phpEx"), $forum_id);
1537 page_footer();