PageRenderTime 63ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/viewtopic.php

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