PageRenderTime 57ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/forum/viewtopic.php

https://bitbucket.org/itoxable/chiron-gaming
PHP | 1768 lines | 1361 code | 263 blank | 144 comment | 381 complexity | caed7dc75fe4876ff0a6526407a5ef03 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. session_start();
  3. /**
  4. *
  5. * @package phpBB3
  6. * @version $Id$
  7. * @copyright (c) 2005 phpBB Group
  8. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  9. *
  10. */
  11. /**
  12. * @ignore
  13. */
  14. define('IN_PHPBB', true);
  15. $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? 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);
  20. // Start session management
  21. $user->session_begin();
  22. $auth->acl($user->data);
  23. $_SESSION['PHPBB_USER'] = $user;
  24. // Initial var setup
  25. $forum_id = request_var('f', 0);
  26. $topic_id = request_var('t', 0);
  27. $post_id = request_var('p', 0);
  28. $voted_id = request_var('vote_id', array('' => 0));
  29. $voted_id = (sizeof($voted_id) > 1) ? array_unique($voted_id) : $voted_id;
  30. $start = request_var('start', 0);
  31. $view = request_var('view', '');
  32. $default_sort_days = (!empty($user->data['user_post_show_days'])) ? $user->data['user_post_show_days'] : 0;
  33. $default_sort_key = (!empty($user->data['user_post_sortby_type'])) ? $user->data['user_post_sortby_type'] : 't';
  34. $default_sort_dir = (!empty($user->data['user_post_sortby_dir'])) ? $user->data['user_post_sortby_dir'] : 'a';
  35. $sort_days = request_var('st', $default_sort_days);
  36. $sort_key = request_var('sk', $default_sort_key);
  37. $sort_dir = request_var('sd', $default_sort_dir);
  38. $update = request_var('update', false);
  39. $s_can_vote = false;
  40. /**
  41. * @todo normalize?
  42. */
  43. $hilit_words = request_var('hilit', '', true);
  44. // Do we have a topic or post id?
  45. if (!$topic_id && !$post_id)
  46. {
  47. trigger_error('NO_TOPIC');
  48. }
  49. // Find topic id if user requested a newer or older topic
  50. if ($view && !$post_id)
  51. {
  52. if (!$forum_id)
  53. {
  54. $sql = 'SELECT forum_id
  55. FROM ' . TOPICS_TABLE . "
  56. WHERE topic_id = $topic_id";
  57. $result = $db->sql_query($sql);
  58. $forum_id = (int) $db->sql_fetchfield('forum_id');
  59. $db->sql_freeresult($result);
  60. if (!$forum_id)
  61. {
  62. trigger_error('NO_TOPIC');
  63. }
  64. }
  65. if ($view == 'unread')
  66. {
  67. // Get topic tracking info
  68. $topic_tracking_info = get_complete_topic_tracking($forum_id, $topic_id);
  69. $topic_last_read = (isset($topic_tracking_info[$topic_id])) ? $topic_tracking_info[$topic_id] : 0;
  70. $sql = 'SELECT post_id, topic_id, forum_id
  71. FROM ' . POSTS_TABLE . "
  72. WHERE topic_id = $topic_id
  73. " . (($auth->acl_get('m_approve', $forum_id)) ? '' : 'AND post_approved = 1') . "
  74. AND post_time > $topic_last_read
  75. AND forum_id = $forum_id
  76. ORDER BY post_time ASC";
  77. $result = $db->sql_query_limit($sql, 1);
  78. $row = $db->sql_fetchrow($result);
  79. $db->sql_freeresult($result);
  80. if (!$row)
  81. {
  82. $sql = 'SELECT topic_last_post_id as post_id, topic_id, forum_id
  83. FROM ' . TOPICS_TABLE . '
  84. WHERE topic_id = ' . $topic_id;
  85. $result = $db->sql_query($sql);
  86. $row = $db->sql_fetchrow($result);
  87. $db->sql_freeresult($result);
  88. }
  89. if (!$row)
  90. {
  91. // Setup user environment so we can process lang string
  92. $user->setup('viewtopic');
  93. trigger_error('NO_TOPIC');
  94. }
  95. $post_id = $row['post_id'];
  96. $topic_id = $row['topic_id'];
  97. }
  98. else if ($view == 'next' || $view == 'previous')
  99. {
  100. $sql_condition = ($view == 'next') ? '>' : '<';
  101. $sql_ordering = ($view == 'next') ? 'ASC' : 'DESC';
  102. $sql = 'SELECT forum_id, topic_last_post_time
  103. FROM ' . TOPICS_TABLE . '
  104. WHERE topic_id = ' . $topic_id;
  105. $result = $db->sql_query($sql);
  106. $row = $db->sql_fetchrow($result);
  107. $db->sql_freeresult($result);
  108. if (!$row)
  109. {
  110. $user->setup('viewtopic');
  111. // OK, the topic doesn't exist. This error message is not helpful, but technically correct.
  112. trigger_error(($view == 'next') ? 'NO_NEWER_TOPICS' : 'NO_OLDER_TOPICS');
  113. }
  114. else
  115. {
  116. $sql = 'SELECT topic_id, forum_id
  117. FROM ' . TOPICS_TABLE . '
  118. WHERE forum_id = ' . $row['forum_id'] . "
  119. AND topic_moved_id = 0
  120. AND topic_last_post_time $sql_condition {$row['topic_last_post_time']}
  121. " . (($auth->acl_get('m_approve', $row['forum_id'])) ? '' : 'AND topic_approved = 1') . "
  122. ORDER BY topic_last_post_time $sql_ordering";
  123. $result = $db->sql_query_limit($sql, 1);
  124. $row = $db->sql_fetchrow($result);
  125. $db->sql_freeresult($result);
  126. if (!$row)
  127. {
  128. $user->setup('viewtopic');
  129. trigger_error(($view == 'next') ? 'NO_NEWER_TOPICS' : 'NO_OLDER_TOPICS');
  130. }
  131. else
  132. {
  133. $topic_id = $row['topic_id'];
  134. // Check for global announcement correctness?
  135. if (!$row['forum_id'] && !$forum_id)
  136. {
  137. trigger_error('NO_TOPIC');
  138. }
  139. else if ($row['forum_id'])
  140. {
  141. $forum_id = $row['forum_id'];
  142. }
  143. }
  144. }
  145. }
  146. // Check for global announcement correctness?
  147. if ((!isset($row) || !$row['forum_id']) && !$forum_id)
  148. {
  149. trigger_error('NO_TOPIC');
  150. }
  151. else if (isset($row) && $row['forum_id'])
  152. {
  153. $forum_id = $row['forum_id'];
  154. }
  155. }
  156. // This rather complex gaggle of code handles querying for topics but
  157. // also allows for direct linking to a post (and the calculation of which
  158. // page the post is on and the correct display of viewtopic)
  159. $sql_array = array(
  160. 'SELECT' => 't.*, f.*',
  161. 'FROM' => array(FORUMS_TABLE => 'f'),
  162. );
  163. // Firebird handles two columns of the same name a little differently, this
  164. // addresses that by forcing the forum_id to come from the forums table.
  165. if ($db->sql_layer === 'firebird')
  166. {
  167. $sql_array['SELECT'] = 'f.forum_id AS forum_id, ' . $sql_array['SELECT'];
  168. }
  169. // The FROM-Order is quite important here, else t.* columns can not be correctly bound.
  170. if ($post_id)
  171. {
  172. $sql_array['SELECT'] .= ', p.post_approved';
  173. $sql_array['FROM'][POSTS_TABLE] = 'p';
  174. }
  175. // Topics table need to be the last in the chain
  176. $sql_array['FROM'][TOPICS_TABLE] = 't';
  177. if ($user->data['is_registered'])
  178. {
  179. $sql_array['SELECT'] .= ', tw.notify_status';
  180. $sql_array['LEFT_JOIN'] = array();
  181. $sql_array['LEFT_JOIN'][] = array(
  182. 'FROM' => array(TOPICS_WATCH_TABLE => 'tw'),
  183. 'ON' => 'tw.user_id = ' . $user->data['user_id'] . ' AND t.topic_id = tw.topic_id'
  184. );
  185. if ($config['allow_bookmarks'])
  186. {
  187. $sql_array['SELECT'] .= ', bm.topic_id as bookmarked';
  188. $sql_array['LEFT_JOIN'][] = array(
  189. 'FROM' => array(BOOKMARKS_TABLE => 'bm'),
  190. 'ON' => 'bm.user_id = ' . $user->data['user_id'] . ' AND t.topic_id = bm.topic_id'
  191. );
  192. }
  193. if ($config['load_db_lastread'])
  194. {
  195. $sql_array['SELECT'] .= ', tt.mark_time, ft.mark_time as forum_mark_time';
  196. $sql_array['LEFT_JOIN'][] = array(
  197. 'FROM' => array(TOPICS_TRACK_TABLE => 'tt'),
  198. 'ON' => 'tt.user_id = ' . $user->data['user_id'] . ' AND t.topic_id = tt.topic_id'
  199. );
  200. $sql_array['LEFT_JOIN'][] = array(
  201. 'FROM' => array(FORUMS_TRACK_TABLE => 'ft'),
  202. 'ON' => 'ft.user_id = ' . $user->data['user_id'] . ' AND t.forum_id = ft.forum_id'
  203. );
  204. }
  205. }
  206. if (!$post_id)
  207. {
  208. $sql_array['WHERE'] = "t.topic_id = $topic_id";
  209. }
  210. else
  211. {
  212. $sql_array['WHERE'] = "p.post_id = $post_id AND t.topic_id = p.topic_id";
  213. }
  214. $sql_array['WHERE'] .= ' AND (f.forum_id = t.forum_id';
  215. if (!$forum_id)
  216. {
  217. // If it is a global announcement make sure to set the forum id to a postable forum
  218. $sql_array['WHERE'] .= ' OR (t.topic_type = ' . POST_GLOBAL . '
  219. AND f.forum_type = ' . FORUM_POST . ')';
  220. }
  221. else
  222. {
  223. $sql_array['WHERE'] .= ' OR (t.topic_type = ' . POST_GLOBAL . "
  224. AND f.forum_id = $forum_id)";
  225. }
  226. $sql_array['WHERE'] .= ')';
  227. // Join to forum table on topic forum_id unless topic forum_id is zero
  228. // whereupon we join on the forum_id passed as a parameter ... this
  229. // is done so navigation, forum name, etc. remain consistent with where
  230. // user clicked to view a global topic
  231. $sql = $db->sql_build_query('SELECT', $sql_array);
  232. $result = $db->sql_query($sql);
  233. $topic_data = $db->sql_fetchrow($result);
  234. $db->sql_freeresult($result);
  235. // link to unapproved post or incorrect link
  236. if (!$topic_data)
  237. {
  238. // If post_id was submitted, we try at least to display the topic as a last resort...
  239. if ($post_id && $topic_id)
  240. {
  241. redirect(append_sid("{$phpbb_root_path}viewtopic.$phpEx", "t=$topic_id" . (($forum_id) ? "&amp;f=$forum_id" : '')));
  242. }
  243. trigger_error('NO_TOPIC');
  244. }
  245. $forum_id = (int) $topic_data['forum_id'];
  246. // This is for determining where we are (page)
  247. if ($post_id)
  248. {
  249. // are we where we are supposed to be?
  250. if (!$topic_data['post_approved'] && !$auth->acl_get('m_approve', $topic_data['forum_id']))
  251. {
  252. // If post_id was submitted, we try at least to display the topic as a last resort...
  253. if ($topic_id)
  254. {
  255. redirect(append_sid("{$phpbb_root_path}viewtopic.$phpEx", "t=$topic_id" . (($forum_id) ? "&amp;f=$forum_id" : '')));
  256. }
  257. trigger_error('NO_TOPIC');
  258. }
  259. if ($post_id == $topic_data['topic_first_post_id'] || $post_id == $topic_data['topic_last_post_id'])
  260. {
  261. $check_sort = ($post_id == $topic_data['topic_first_post_id']) ? 'd' : 'a';
  262. if ($sort_dir == $check_sort)
  263. {
  264. $topic_data['prev_posts'] = ($auth->acl_get('m_approve', $forum_id)) ? $topic_data['topic_replies_real'] : $topic_data['topic_replies'];
  265. }
  266. else
  267. {
  268. $topic_data['prev_posts'] = 0;
  269. }
  270. }
  271. else
  272. {
  273. $sql = 'SELECT COUNT(p1.post_id) AS prev_posts
  274. FROM ' . POSTS_TABLE . ' p1, ' . POSTS_TABLE . " p2
  275. WHERE p1.topic_id = {$topic_data['topic_id']}
  276. AND p2.post_id = {$post_id}
  277. " . ((!$auth->acl_get('m_approve', $forum_id)) ? 'AND p1.post_approved = 1' : '') . '
  278. AND ' . (($sort_dir == 'd') ? 'p1.post_time >= p2.post_time' : 'p1.post_time <= p2.post_time');
  279. $result = $db->sql_query($sql);
  280. $row = $db->sql_fetchrow($result);
  281. $db->sql_freeresult($result);
  282. $topic_data['prev_posts'] = $row['prev_posts'] - 1;
  283. }
  284. }
  285. $topic_id = (int) $topic_data['topic_id'];
  286. //
  287. $topic_replies = ($auth->acl_get('m_approve', $forum_id)) ? $topic_data['topic_replies_real'] : $topic_data['topic_replies'];
  288. // Check sticky/announcement time limit
  289. 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())
  290. {
  291. $sql = 'UPDATE ' . TOPICS_TABLE . '
  292. SET topic_type = ' . POST_NORMAL . ', topic_time_limit = 0
  293. WHERE topic_id = ' . $topic_id;
  294. $db->sql_query($sql);
  295. $topic_data['topic_type'] = POST_NORMAL;
  296. $topic_data['topic_time_limit'] = 0;
  297. }
  298. // Setup look and feel
  299. $user->setup('viewtopic', $topic_data['forum_style']);
  300. if (!$topic_data['topic_approved'] && !$auth->acl_get('m_approve', $forum_id))
  301. {
  302. trigger_error('NO_TOPIC');
  303. }
  304. // Start auth check
  305. if (!$auth->acl_get('f_read', $forum_id))
  306. {
  307. if ($user->data['user_id'] != ANONYMOUS)
  308. {
  309. trigger_error('SORRY_AUTH_READ');
  310. }
  311. login_box('', $user->lang['LOGIN_VIEWFORUM']);
  312. }
  313. // Forum is passworded ... check whether access has been granted to this
  314. // user this session, if not show login box
  315. if ($topic_data['forum_password'])
  316. {
  317. login_forum_box($topic_data);
  318. }
  319. // Redirect to login or to the correct post upon emailed notification links
  320. if (isset($_GET['e']))
  321. {
  322. $jump_to = request_var('e', 0);
  323. $redirect_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id");
  324. if ($user->data['user_id'] == ANONYMOUS)
  325. {
  326. login_box($redirect_url . "&amp;p=$post_id&amp;e=$jump_to", $user->lang['LOGIN_NOTIFY_TOPIC']);
  327. }
  328. if ($jump_to > 0)
  329. {
  330. // We direct the already logged in user to the correct post...
  331. redirect($redirect_url . ((!$post_id) ? "&amp;p=$jump_to" : "&amp;p=$post_id") . "#p$jump_to");
  332. }
  333. }
  334. // What is start equal to?
  335. if ($post_id)
  336. {
  337. $start = floor(($topic_data['prev_posts']) / $config['posts_per_page']) * $config['posts_per_page'];
  338. }
  339. // Get topic tracking info
  340. if (!isset($topic_tracking_info))
  341. {
  342. $topic_tracking_info = array();
  343. // Get topic tracking info
  344. if ($config['load_db_lastread'] && $user->data['is_registered'])
  345. {
  346. $tmp_topic_data = array($topic_id => $topic_data);
  347. $topic_tracking_info = get_topic_tracking($forum_id, $topic_id, $tmp_topic_data, array($forum_id => $topic_data['forum_mark_time']));
  348. unset($tmp_topic_data);
  349. }
  350. else if ($config['load_anon_lastread'] || $user->data['is_registered'])
  351. {
  352. $topic_tracking_info = get_complete_topic_tracking($forum_id, $topic_id);
  353. }
  354. }
  355. // Post ordering options
  356. $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']);
  357. $sort_by_text = array('a' => $user->lang['AUTHOR'], 't' => $user->lang['POST_TIME'], 's' => $user->lang['SUBJECT']);
  358. $sort_by_sql = array('a' => array('u.username_clean', 'p.post_id'), 't' => 'p.post_time', 's' => array('p.post_subject', 'p.post_id'));
  359. $join_user_sql = array('a' => true, 't' => false, 's' => false);
  360. $s_limit_days = $s_sort_key = $s_sort_dir = $u_sort_param = '';
  361. 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);
  362. // Obtain correct post count and ordering SQL if user has
  363. // requested anything different
  364. if ($sort_days)
  365. {
  366. $min_post_time = time() - ($sort_days * 86400);
  367. $sql = 'SELECT COUNT(post_id) AS num_posts
  368. FROM ' . POSTS_TABLE . "
  369. WHERE topic_id = $topic_id
  370. AND post_time >= $min_post_time
  371. " . (($auth->acl_get('m_approve', $forum_id)) ? '' : 'AND post_approved = 1');
  372. $result = $db->sql_query($sql);
  373. $total_posts = (int) $db->sql_fetchfield('num_posts');
  374. $db->sql_freeresult($result);
  375. $limit_posts_time = "AND p.post_time >= $min_post_time ";
  376. if (isset($_POST['sort']))
  377. {
  378. $start = 0;
  379. }
  380. }
  381. else
  382. {
  383. $total_posts = $topic_replies + 1;
  384. $limit_posts_time = '';
  385. }
  386. // Was a highlight request part of the URI?
  387. $highlight_match = $highlight = '';
  388. if ($hilit_words)
  389. {
  390. foreach (explode(' ', trim($hilit_words)) as $word)
  391. {
  392. if (trim($word))
  393. {
  394. $word = str_replace('\*', '\w+?', preg_quote($word, '#'));
  395. $word = preg_replace('#(^|\s)\\\\w\*\?(\s|$)#', '$1\w+?$2', $word);
  396. $highlight_match .= (($highlight_match != '') ? '|' : '') . $word;
  397. }
  398. }
  399. $highlight = urlencode($hilit_words);
  400. }
  401. // Make sure $start is set to the last page if it exceeds the amount
  402. if ($start < 0 || $start >= $total_posts)
  403. {
  404. $start = ($start < 0) ? 0 : floor(($total_posts - 1) / $config['posts_per_page']) * $config['posts_per_page'];
  405. }
  406. // General Viewtopic URL for return links
  407. $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" : ''));
  408. // Are we watching this topic?
  409. $s_watching_topic = array(
  410. 'link' => '',
  411. 'title' => '',
  412. 'is_watching' => false,
  413. );
  414. if (($config['email_enable'] || $config['jab_enable']) && $config['allow_topic_notify'] && $user->data['is_registered'])
  415. {
  416. watch_topic_forum('topic', $s_watching_topic, $user->data['user_id'], $forum_id, $topic_id, $topic_data['notify_status'], $start);
  417. // Reset forum notification if forum notify is set
  418. if ($config['allow_forum_notify'] && $auth->acl_get('f_subscribe', $forum_id))
  419. {
  420. $s_watching_forum = $s_watching_topic;
  421. watch_topic_forum('forum', $s_watching_forum, $user->data['user_id'], $forum_id, 0);
  422. }
  423. }
  424. // Bookmarks
  425. if ($config['allow_bookmarks'] && $user->data['is_registered'] && request_var('bookmark', 0))
  426. {
  427. if (check_link_hash(request_var('hash', ''), "topic_$topic_id"))
  428. {
  429. if (!$topic_data['bookmarked'])
  430. {
  431. $sql = 'INSERT INTO ' . BOOKMARKS_TABLE . ' ' . $db->sql_build_array('INSERT', array(
  432. 'user_id' => $user->data['user_id'],
  433. 'topic_id' => $topic_id,
  434. ));
  435. $db->sql_query($sql);
  436. }
  437. else
  438. {
  439. $sql = 'DELETE FROM ' . BOOKMARKS_TABLE . "
  440. WHERE user_id = {$user->data['user_id']}
  441. AND topic_id = $topic_id";
  442. $db->sql_query($sql);
  443. }
  444. $message = (($topic_data['bookmarked']) ? $user->lang['BOOKMARK_REMOVED'] : $user->lang['BOOKMARK_ADDED']) . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $viewtopic_url . '">', '</a>');
  445. }
  446. else
  447. {
  448. $message = $user->lang['BOOKMARK_ERR'] . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $viewtopic_url . '">', '</a>');
  449. }
  450. meta_refresh(3, $viewtopic_url);
  451. trigger_error($message);
  452. }
  453. // Grab ranks
  454. $ranks = $cache->obtain_ranks();
  455. // Grab icons
  456. $icons = $cache->obtain_icons();
  457. // Grab extensions
  458. $extensions = array();
  459. if ($topic_data['topic_attachment'])
  460. {
  461. $extensions = $cache->obtain_attach_extensions($forum_id);
  462. }
  463. // Forum rules listing
  464. $s_forum_rules = '';
  465. gen_forum_auth_level('topic', $forum_id, $topic_data['forum_status']);
  466. // Quick mod tools
  467. $allow_change_type = ($auth->acl_get('m_', $forum_id) || ($user->data['is_registered'] && $user->data['user_id'] == $topic_data['topic_poster'])) ? true : false;
  468. $topic_mod = '';
  469. $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>') : '';
  470. $topic_mod .= ($auth->acl_get('m_delete', $forum_id)) ? '<option value="delete_topic">' . $user->lang['DELETE_TOPIC'] . '</option>' : '';
  471. $topic_mod .= ($auth->acl_get('m_move', $forum_id) && $topic_data['topic_status'] != ITEM_MOVED) ? '<option value="move">' . $user->lang['MOVE_TOPIC'] . '</option>' : '';
  472. $topic_mod .= ($auth->acl_get('m_split', $forum_id)) ? '<option value="split">' . $user->lang['SPLIT_TOPIC'] . '</option>' : '';
  473. $topic_mod .= ($auth->acl_get('m_merge', $forum_id)) ? '<option value="merge">' . $user->lang['MERGE_POSTS'] . '</option>' : '';
  474. $topic_mod .= ($auth->acl_get('m_merge', $forum_id)) ? '<option value="merge_topic">' . $user->lang['MERGE_TOPIC'] . '</option>' : '';
  475. $topic_mod .= ($auth->acl_get('m_move', $forum_id)) ? '<option value="fork">' . $user->lang['FORK_TOPIC'] . '</option>' : '';
  476. $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>' : '';
  477. $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>' : '';
  478. $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>' : '';
  479. $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>' : '';
  480. $topic_mod .= ($auth->acl_get('m_', $forum_id)) ? '<option value="topic_logs">' . $user->lang['VIEW_TOPIC_LOGS'] . '</option>' : '';
  481. // If we've got a hightlight set pass it on to pagination.
  482. $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);
  483. // Navigation links
  484. generate_forum_nav($topic_data);
  485. // Forum Rules
  486. generate_forum_rules($topic_data);
  487. // Moderators
  488. $forum_moderators = array();
  489. if ($config['load_moderators'])
  490. {
  491. get_moderators($forum_moderators, $forum_id);
  492. }
  493. // This is only used for print view so ...
  494. $server_path = (!$view) ? $phpbb_root_path : generate_board_url() . '/';
  495. // Replace naughty words in title
  496. $topic_data['topic_title'] = censor_text($topic_data['topic_title']);
  497. $s_search_hidden_fields = array(
  498. 't' => $topic_id,
  499. 'sf' => 'msgonly',
  500. );
  501. if ($_SID)
  502. {
  503. $s_search_hidden_fields['sid'] = $_SID;
  504. }
  505. // Send vars to template
  506. $template->assign_vars(array(
  507. 'FORUM_ID' => $forum_id,
  508. 'FORUM_NAME' => $topic_data['forum_name'],
  509. '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']),
  510. 'TOPIC_ID' => $topic_id,
  511. 'TOPIC_TITLE' => $topic_data['topic_title'],
  512. 'TOPIC_POSTER' => $topic_data['topic_poster'],
  513. 'TOPIC_AUTHOR_FULL' => get_username_string('full', $topic_data['topic_poster'], $topic_data['topic_first_poster_name'], $topic_data['topic_first_poster_colour']),
  514. 'TOPIC_AUTHOR_COLOUR' => get_username_string('colour', $topic_data['topic_poster'], $topic_data['topic_first_poster_name'], $topic_data['topic_first_poster_colour']),
  515. 'TOPIC_AUTHOR' => get_username_string('username', $topic_data['topic_poster'], $topic_data['topic_first_poster_name'], $topic_data['topic_first_poster_colour']),
  516. 'PAGINATION' => $pagination,
  517. 'PAGE_NUMBER' => on_page($total_posts, $config['posts_per_page'], $start),
  518. 'TOTAL_POSTS' => ($total_posts == 1) ? $user->lang['VIEW_TOPIC_POST'] : sprintf($user->lang['VIEW_TOPIC_POSTS'], $total_posts),
  519. '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) : '',
  520. 'MODERATORS' => (isset($forum_moderators[$forum_id]) && sizeof($forum_moderators[$forum_id])) ? implode(', ', $forum_moderators[$forum_id]) : '',
  521. 'POST_IMG' => ($topic_data['forum_status'] == ITEM_LOCKED) ? $user->img('button_topic_locked', 'FORUM_LOCKED') : $user->img('button_topic_new', 'POST_NEW_TOPIC'),
  522. 'QUOTE_IMG' => $user->img('icon_post_quote', 'REPLY_WITH_QUOTE'),
  523. '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'),
  524. 'EDIT_IMG' => $user->img('icon_post_edit', 'EDIT_POST'),
  525. 'DELETE_IMG' => $user->img('icon_post_delete', 'DELETE_POST'),
  526. 'INFO_IMG' => $user->img('icon_post_info', 'VIEW_INFO'),
  527. 'PROFILE_IMG' => $user->img('icon_user_profile', 'READ_PROFILE'),
  528. 'SEARCH_IMG' => $user->img('icon_user_search', 'SEARCH_USER_POSTS'),
  529. 'PM_IMG' => $user->img('icon_contact_pm', 'SEND_PRIVATE_MESSAGE'),
  530. 'EMAIL_IMG' => $user->img('icon_contact_email', 'SEND_EMAIL'),
  531. 'WWW_IMG' => $user->img('icon_contact_www', 'VISIT_WEBSITE'),
  532. 'ICQ_IMG' => $user->img('icon_contact_icq', 'ICQ'),
  533. 'AIM_IMG' => $user->img('icon_contact_aim', 'AIM'),
  534. 'MSN_IMG' => $user->img('icon_contact_msnm', 'MSNM'),
  535. 'YIM_IMG' => $user->img('icon_contact_yahoo', 'YIM'),
  536. 'JABBER_IMG' => $user->img('icon_contact_jabber', 'JABBER') ,
  537. 'REPORT_IMG' => $user->img('icon_post_report', 'REPORT_POST'),
  538. 'REPORTED_IMG' => $user->img('icon_topic_reported', 'POST_REPORTED'),
  539. 'UNAPPROVED_IMG' => $user->img('icon_topic_unapproved', 'POST_UNAPPROVED'),
  540. 'WARN_IMG' => $user->img('icon_user_warn', 'WARN_USER'),
  541. 'S_IS_LOCKED' => ($topic_data['topic_status'] == ITEM_UNLOCKED && $topic_data['forum_status'] == ITEM_UNLOCKED) ? false : true,
  542. 'S_SELECT_SORT_DIR' => $s_sort_dir,
  543. 'S_SELECT_SORT_KEY' => $s_sort_key,
  544. 'S_SELECT_SORT_DAYS' => $s_limit_days,
  545. 'S_SINGLE_MODERATOR' => (!empty($forum_moderators[$forum_id]) && sizeof($forum_moderators[$forum_id]) > 1) ? false : true,
  546. 'S_TOPIC_ACTION' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id" . (($start == 0) ? '' : "&amp;start=$start")),
  547. 'S_TOPIC_MOD' => ($topic_mod != '') ? '<select name="action" id="quick-mod-select">' . $topic_mod . '</select>' : '',
  548. '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),
  549. 'S_VIEWTOPIC' => true,
  550. 'S_DISPLAY_SEARCHBOX' => ($auth->acl_get('u_search') && $auth->acl_get('f_search', $forum_id) && $config['load_search']) ? true : false,
  551. 'S_SEARCHBOX_ACTION' => append_sid("{$phpbb_root_path}search.$phpEx"),
  552. 'S_SEARCH_LOCAL_HIDDEN_FIELDS' => build_hidden_fields($s_search_hidden_fields),
  553. 'S_DISPLAY_POST_INFO' => ($topic_data['forum_type'] == FORUM_POST && ($auth->acl_get('f_post', $forum_id) || $user->data['user_id'] == ANONYMOUS)) ? true : false,
  554. 'S_DISPLAY_REPLY_INFO' => ($topic_data['forum_type'] == FORUM_POST && ($auth->acl_get('f_reply', $forum_id) || $user->data['user_id'] == ANONYMOUS)) ? true : false,
  555. 'S_ENABLE_FEEDS_TOPIC' => ($config['feed_topic'] && !phpbb_optionget(FORUM_OPTION_FEED_EXCLUDE, $topic_data['forum_options'])) ? true : false,
  556. 'U_TOPIC' => "{$server_path}viewtopic.$phpEx?f=$forum_id&amp;t=$topic_id",
  557. 'U_FORUM' => $server_path,
  558. 'U_VIEW_TOPIC' => $viewtopic_url,
  559. 'U_VIEW_FORUM' => append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_id),
  560. 'U_VIEW_OLDER_TOPIC' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;view=previous"),
  561. 'U_VIEW_NEWER_TOPIC' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;view=next"),
  562. 'U_PRINT_TOPIC' => ($auth->acl_get('f_print', $forum_id)) ? $viewtopic_url . '&amp;view=print' : '',
  563. '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") : '',
  564. 'U_WATCH_TOPIC' => $s_watching_topic['link'],
  565. 'L_WATCH_TOPIC' => $s_watching_topic['title'],
  566. 'S_WATCHING_TOPIC' => $s_watching_topic['is_watching'],
  567. 'U_BOOKMARK_TOPIC' => ($user->data['is_registered'] && $config['allow_bookmarks']) ? $viewtopic_url . '&amp;bookmark=1&amp;hash=' . generate_link_hash("topic_$topic_id") : '',
  568. 'L_BOOKMARK_TOPIC' => ($user->data['is_registered'] && $config['allow_bookmarks'] && $topic_data['bookmarked']) ? $user->lang['BOOKMARK_TOPIC_REMOVE'] : $user->lang['BOOKMARK_TOPIC'],
  569. '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") : '',
  570. '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") : '',
  571. '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")) : '')
  572. );
  573. // Does this topic contain a poll?
  574. if (!empty($topic_data['poll_start']))
  575. {
  576. $sql = 'SELECT o.*, p.bbcode_bitfield, p.bbcode_uid
  577. FROM ' . POLL_OPTIONS_TABLE . ' o, ' . POSTS_TABLE . " p
  578. WHERE o.topic_id = $topic_id
  579. AND p.post_id = {$topic_data['topic_first_post_id']}
  580. AND p.topic_id = o.topic_id
  581. ORDER BY o.poll_option_id";
  582. $result = $db->sql_query($sql);
  583. $poll_info = array();
  584. while ($row = $db->sql_fetchrow($result))
  585. {
  586. $poll_info[] = $row;
  587. }
  588. $db->sql_freeresult($result);
  589. $cur_voted_id = array();
  590. if ($user->data['is_registered'])
  591. {
  592. $sql = 'SELECT poll_option_id
  593. FROM ' . POLL_VOTES_TABLE . '
  594. WHERE topic_id = ' . $topic_id . '
  595. AND vote_user_id = ' . $user->data['user_id'];
  596. $result = $db->sql_query($sql);
  597. while ($row = $db->sql_fetchrow($result))
  598. {
  599. $cur_voted_id[] = $row['poll_option_id'];
  600. }
  601. $db->sql_freeresult($result);
  602. }
  603. else
  604. {
  605. // Cookie based guest tracking ... I don't like this but hum ho
  606. // it's oft requested. This relies on "nice" users who don't feel
  607. // the need to delete cookies to mess with results.
  608. if (isset($_COOKIE[$config['cookie_name'] . '_poll_' . $topic_id]))
  609. {
  610. $cur_voted_id = explode(',', $_COOKIE[$config['cookie_name'] . '_poll_' . $topic_id]);
  611. $cur_voted_id = array_map('intval', $cur_voted_id);
  612. }
  613. }
  614. // Can not vote at all if no vote permission
  615. $s_can_vote = ($auth->acl_get('f_vote', $forum_id) &&
  616. (($topic_data['poll_length'] != 0 && $topic_data['poll_start'] + $topic_data['poll_length'] > time()) || $topic_data['poll_length'] == 0) &&
  617. $topic_data['topic_status'] != ITEM_LOCKED &&
  618. $topic_data['forum_status'] != ITEM_LOCKED &&
  619. (!sizeof($cur_voted_id) ||
  620. ($auth->acl_get('f_votechg', $forum_id) && $topic_data['poll_vote_change']))) ? true : false;
  621. $s_display_results = (!$s_can_vote || ($s_can_vote && sizeof($cur_voted_id)) || $view == 'viewpoll') ? true : false;
  622. if ($update && $s_can_vote)
  623. {
  624. if (!sizeof($voted_id) || sizeof($voted_id) > $topic_data['poll_max_options'] || in_array(VOTE_CONVERTED, $cur_voted_id) || !check_form_key('posting'))
  625. {
  626. $redirect_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id" . (($start == 0) ? '' : "&amp;start=$start"));
  627. meta_refresh(5, $redirect_url);
  628. if (!sizeof($voted_id))
  629. {
  630. $message = 'NO_VOTE_OPTION';
  631. }
  632. else if (sizeof($voted_id) > $topic_data['poll_max_options'])
  633. {
  634. $message = 'TOO_MANY_VOTE_OPTIONS';
  635. }
  636. else if (in_array(VOTE_CONVERTED, $cur_voted_id))
  637. {
  638. $message = 'VOTE_CONVERTED';
  639. }
  640. else
  641. {
  642. $message = 'FORM_INVALID';
  643. }
  644. $message = $user->lang[$message] . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $redirect_url . '">', '</a>');
  645. trigger_error($message);
  646. }
  647. foreach ($voted_id as $option)
  648. {
  649. if (in_array($option, $cur_voted_id))
  650. {
  651. continue;
  652. }
  653. $sql = 'UPDATE ' . POLL_OPTIONS_TABLE . '
  654. SET poll_option_total = poll_option_total + 1
  655. WHERE poll_option_id = ' . (int) $option . '
  656. AND topic_id = ' . (int) $topic_id;
  657. $db->sql_query($sql);
  658. if ($user->data['is_registered'])
  659. {
  660. $sql_ary = array(
  661. 'topic_id' => (int) $topic_id,
  662. 'poll_option_id' => (int) $option,
  663. 'vote_user_id' => (int) $user->data['user_id'],
  664. 'vote_user_ip' => (string) $user->ip,
  665. );
  666. $sql = 'INSERT INTO ' . POLL_VOTES_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
  667. $db->sql_query($sql);
  668. }
  669. }
  670. foreach ($cur_voted_id as $option)
  671. {
  672. if (!in_array($option, $voted_id))
  673. {
  674. $sql = 'UPDATE ' . POLL_OPTIONS_TABLE . '
  675. SET poll_option_total = poll_option_total - 1
  676. WHERE poll_option_id = ' . (int) $option . '
  677. AND topic_id = ' . (int) $topic_id;
  678. $db->sql_query($sql);
  679. if ($user->data['is_registered'])
  680. {
  681. $sql = 'DELETE FROM ' . POLL_VOTES_TABLE . '
  682. WHERE topic_id = ' . (int) $topic_id . '
  683. AND poll_option_id = ' . (int) $option . '
  684. AND vote_user_id = ' . (int) $user->data['user_id'];
  685. $db->sql_query($sql);
  686. }
  687. }
  688. }
  689. if ($user->data['user_id'] == ANONYMOUS && !$user->data['is_bot'])
  690. {
  691. $user->set_cookie('poll_' . $topic_id, implode(',', $voted_id), time() + 31536000);
  692. }
  693. $sql = 'UPDATE ' . TOPICS_TABLE . '
  694. SET poll_last_vote = ' . time() . "
  695. WHERE topic_id = $topic_id";
  696. //, topic_last_post_time = ' . time() . " -- for bumping topics with new votes, ignore for now
  697. $db->sql_query($sql);
  698. $redirect_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id" . (($start == 0) ? '' : "&amp;start=$start"));
  699. meta_refresh(5, $redirect_url);
  700. trigger_error($user->lang['VOTE_SUBMITTED'] . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $redirect_url . '">', '</a>'));
  701. }
  702. $poll_total = 0;
  703. foreach ($poll_info as $poll_option)
  704. {
  705. $poll_total += $poll_option['poll_option_total'];
  706. }
  707. if ($poll_info[0]['bbcode_bitfield'])
  708. {
  709. $poll_bbcode = new bbcode();
  710. }
  711. else
  712. {
  713. $poll_bbcode = false;
  714. }
  715. for ($i = 0, $size = sizeof($poll_info); $i < $size; $i++)
  716. {
  717. $poll_info[$i]['poll_option_text'] = censor_text($poll_info[$i]['poll_option_text']);
  718. if ($poll_bbcode !== false)
  719. {
  720. $poll_bbcode->bbcode_second_pass($poll_info[$i]['poll_option_text'], $poll_info[$i]['bbcode_uid'], $poll_option['bbcode_bitfield']);
  721. }
  722. $poll_info[$i]['poll_option_text'] = bbcode_nl2br($poll_info[$i]['poll_option_text']);
  723. $poll_info[$i]['poll_option_text'] = smiley_text($poll_info[$i]['poll_option_text']);
  724. }
  725. $topic_data['poll_title'] = censor_text($topic_data['poll_title']);
  726. if ($poll_bbcode !== false)
  727. {
  728. $poll_bbcode->bbcode_second_pass($topic_data['poll_title'], $poll_info[0]['bbcode_uid'], $poll_info[0]['bbcode_bitfield']);
  729. }
  730. $topic_data['poll_title'] = bbcode_nl2br($topic_data['poll_title']);
  731. $topic_data['poll_title'] = smiley_text($topic_data['poll_title']);
  732. unset($poll_bbcode);
  733. foreach ($poll_info as $poll_option)
  734. {
  735. $option_pct = ($poll_total > 0) ? $poll_option['poll_option_total'] / $poll_total : 0;
  736. $option_pct_txt = sprintf("%.1d%%", round($option_pct * 100));
  737. $template->assign_block_vars('poll_option', array(
  738. 'POLL_OPTION_ID' => $poll_option['poll_option_id'],
  739. 'POLL_OPTION_CAPTION' => $poll_option['poll_option_text'],
  740. 'POLL_OPTION_RESULT' => $poll_option['poll_option_total'],
  741. 'POLL_OPTION_PERCENT' => $option_pct_txt,
  742. 'POLL_OPTION_PCT' => round($option_pct * 100),
  743. 'POLL_OPTION_IMG' => $user->img('poll_center', $option_pct_txt, round($option_pct * 250)),
  744. 'POLL_OPTION_VOTED' => (in_array($poll_option['poll_option_id'], $cur_voted_id)) ? true : false)
  745. );
  746. }
  747. $poll_end = $topic_data['poll_length'] + $topic_data['poll_start'];
  748. $template->assign_vars(array(
  749. 'POLL_QUESTION' => $topic_data['poll_title'],
  750. 'TOTAL_VOTES' => $poll_total,
  751. 'POLL_LEFT_CAP_IMG' => $user->img('poll_left'),
  752. 'POLL_RIGHT_CAP_IMG'=> $user->img('poll_right'),
  753. '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']),
  754. 'L_POLL_LENGTH' => ($topic_data['poll_length']) ? sprintf($user->lang[($poll_end > time()) ? 'POLL_RUN_TILL' : 'POLL_ENDED_AT'], $user->format_date($poll_end)) : '',
  755. 'S_HAS_POLL' => true,
  756. 'S_CAN_VOTE' => $s_can_vote,
  757. 'S_DISPLAY_RESULTS' => $s_display_results,
  758. 'S_IS_MULTI_CHOICE' => ($topic_data['poll_max_options'] > 1) ? true : false,
  759. 'S_POLL_ACTION' => $viewtopic_url,
  760. 'U_VIEW_RESULTS' => $viewtopic_url . '&amp;view=viewpoll')
  761. );
  762. unset($poll_end, $poll_info, $voted_id);
  763. }
  764. // If the user is trying to reach the second half of the topic, fetch it starting from the end
  765. $store_reverse = false;
  766. $sql_limit = $config['posts_per_page'];
  767. $sql_sort_order = $direction = '';
  768. if ($start > $total_posts / 2)
  769. {
  770. $store_reverse = true;
  771. if ($start + $config['posts_per_page'] > $total_posts)
  772. {
  773. $sql_limit = min($config['posts_per_page'], max(1, $total_posts - $start));
  774. }
  775. // Select the sort order
  776. $direction = (($sort_dir == 'd') ? 'ASC' : 'DESC');
  777. $sql_start = max(0, $total_posts - $sql_limit - $start);
  778. }
  779. else
  780. {
  781. // Select the sort order
  782. $direction = (($sort_dir == 'd') ? 'DESC' : 'ASC');
  783. $sql_start = $start;
  784. }
  785. if (is_array($sort_by_sql[$sort_key]))
  786. {
  787. $sql_sort_order = implode(' ' . $direction . ', ', $sort_by_sql[$sort_key]) . ' ' . $direction;
  788. }
  789. else
  790. {
  791. $sql_sort_order = $sort_by_sql[$sort_key] . ' ' . $direction;
  792. }
  793. // Container for user details, only process once
  794. $post_list = $user_cache = $id_cache = $attachments = $attach_list = $rowset = $update_count = $post_edit_list = array();
  795. $has_attachments = $display_notice = false;
  796. $bbcode_bitfield = '';
  797. $i = $i_total = 0;
  798. // Go ahead and pull all data for this topic
  799. $sql = 'SELECT p.post_id
  800. FROM ' . POSTS_TABLE . ' p' . (($join_user_sql[$sort_key]) ? ', ' . USERS_TABLE . ' u': '') . "
  801. WHERE p.topic_id = $topic_id
  802. " . ((!$auth->acl_get('m_approve', $forum_id)) ? 'AND p.post_approved = 1' : '') . "
  803. " . (($join_user_sql[$sort_key]) ? 'AND u.user_id = p.poster_id': '') . "
  804. $limit_posts_time
  805. ORDER BY $sql_sort_order";
  806. $result = $db->sql_query_limit($sql, $sql_limit, $sql_start);
  807. $i = ($store_reverse) ? $sql_limit - 1 : 0;
  808. while ($row = $db->sql_fetchrow($result))
  809. {
  810. $post_list[$i] = (int) $row['post_id'];
  811. ($store_reverse) ? $i-- : $i++;
  812. }
  813. $db->sql_freeresult($result);
  814. if (!sizeof($post_list))
  815. {
  816. if ($sort_days)
  817. {
  818. trigger_error('NO_POSTS_TIME_FRAME');
  819. }
  820. else
  821. {
  822. trigger_error('NO_TOPIC');
  823. }
  824. }
  825. // Holding maximum post time for marking topic read
  826. // We need to grab it because we do reverse ordering sometimes
  827. $max_post_time = 0;
  828. $sql = $db->sql_build_query('SELECT', array(
  829. 'SELECT' => 'u.*, z.friend, z.foe, p.*',
  830. 'FROM' => array(
  831. USERS_TABLE => 'u',
  832. POSTS_TABLE => 'p',
  833. ),
  834. 'LEFT_JOIN' => array(
  835. array(
  836. 'FROM' => array(ZEBRA_TABLE => 'z'),
  837. 'ON' => 'z.user_id = ' . $user->data['user_id'] . ' AND z.zebra_id = p.poster_id'
  838. )
  839. ),
  840. 'WHERE' => $db->sql_in_set('p.post_id', $post_list) . '
  841. AND u.user_id = p.poster_id'
  842. ));
  843. $result = $db->sql_query($sql);
  844. $now = getdate(time() + $user->timezone + $user->dst - date('Z'));
  845. // Posts are stored in the $rowset array while $attach_list, $user_cache
  846. // and the global bbcode_bitfield are built
  847. while ($row = $db->sql_fetchrow($result))
  848. {
  849. // Set max_post_time
  850. if ($row['post_time'] > $max_post_time)
  851. {
  852. $max_post_time = $row['post_time'];
  853. }
  854. $poster_id = (int) $row['poster_id'];
  855. // Does post have an attachment? If so, add it to the list
  856. if ($row['post_attachment'] && $config['allow_attachments'])
  857. {
  858. $attach_list[] = (int) $row['post_id'];
  859. if ($row['post_approved'])
  860. {
  861. $has_attachments = true;
  862. }
  863. }
  864. $rowset[$row['post_id']] = array(
  865. 'hide_post' => ($row['foe'] && ($view != 'show' || $post_id != $row['post_id'])) ? true : false,
  866. 'post_id' => $row['post_id'],
  867. 'post_time' => $row['post_time'],
  868. 'user_id' => $row['user_id'],
  869. 'username' => $row['username'],
  870. 'user_colour' => $row['user_colour'],
  871. 'topic_id' => $row['topic_id'],
  872. 'forum_id' => $row['forum_id'],
  873. 'post_subject' => $row['post_subject'],
  874. 'post_edit_count' => $row['post_edit_count'],
  875. 'post_edit_time' => $row['post_edit_time'],
  876. 'post_edit_reason' => $row['post_edit_reason'],
  877. 'post_edit_user' => $row['post_edit_user'],
  878. 'post_edit_locked' => $row['post_edit_locked'],
  879. // Make sure the icon actually exists
  880. 'icon_id' => (isset($icons[$row['icon_id']]['img'], $icons[$row['icon_id']]['height'], $icons[$row['icon_id']]['width'])) ? $row['icon_id'] : 0,
  881. 'post_attachment' => $row['post_attachment'],
  882. 'post_approved' => $row['post_approved'],
  883. 'post_reported' => $row['post_reported'],
  884. 'post_username' => $row['post_username'],
  885. 'post_text' => $row['post_text'],
  886. 'bbcode_uid' => $row['bbcode_uid'],
  887. 'bbcode_bitfield' => $row['bbcode_bitfield'],
  888. 'enable_smilies' => $row['enable_smilies'],
  889. 'enable_sig' => $row['enable_sig'],
  890. 'friend' => $row['friend'],
  891. 'foe' => $row['foe'],
  892. );
  893. // Define the global bbcode bitfield, will be used to load bbcodes
  894. $bbcode_bitfield = $bbcode_bitfield | base64_decode($row['bbcode_bitfield']);
  895. // Is a signature attached? Are we going to display it?
  896. if ($row['enable_sig'] && $config['allow_sig'] && $user->optionget('viewsigs'))
  897. {
  898. $bbcode_bitfield = $bbcode_bitfield | base64_decode($row['user_sig_bbcode_bitfield']);
  899. }
  900. // Cache various user specific data ... so we don't have to recompute
  901. // this each time the same user appears on this page
  902. if (!isset($user_cache[$poster_id]))
  903. {
  904. if ($poster_id == ANONYMOUS)
  905. {
  906. $user_cache[$poster_id] = array(
  907. 'joined' => '',
  908. 'posts' => '',
  909. 'from' => '',
  910. 'sig' => '',
  911. 'sig_bbcode_uid' => '',
  912. 'sig_bbcode_bitfield' => '',
  913. 'online' => false,
  914. 'avatar' => ($user->optionget('viewavatars')) ? get_user_avatar($row['user_avatar'], $row['user_avatar_type'], $row['user_avatar_width'], $row['user_avatar_height']) : '',
  915. 'rank_title' => '',
  916. 'rank_image' => '',
  917. 'rank_image_src' => '',
  918. 'sig' => '',
  919. 'profile' => '',
  920. 'pm' => '',
  921. 'email' => '',
  922. 'www' => '',
  923. 'icq_status_img' => '',
  924. 'icq' => '',
  925. 'aim' => '',
  926. 'msn' => '',
  927. 'yim' => '',
  928. 'jabber' => '',
  929. 'search' => '',
  930. 'age' => '',
  931. 'username' => $row['username'],
  932. 'user_colour' => $row['user_colour'],
  933. 'warnings' => 0,
  934. 'allow_pm' => 0,
  935. );
  936. 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']);
  937. }
  938. else
  939. {
  940. $user_sig = '';
  941. // We add the signature to every posters entry because enable_sig is post dependant
  942. if ($row['user_sig'] && $config['allow_sig'] && $user->optionget('viewsigs'))
  943. {
  944. $user_sig = $row['user_sig'];
  945. }
  946. $id_cache[] = $poster_id;
  947. $user_cache[$poster_id] = array(
  948. 'joined' => $user->format_date($row['user_regdate']),
  949. 'posts' => $row['user_posts'],
  950. 'warnings' => (isset($row['user_warnings'])) ? $row['user_warnings'] : 0,
  951. 'from' => (!empty($row['user_from'])) ? $row['user_from'] : '',
  952. 'sig' => $user_sig,
  953. 'sig_bbcode_uid' => (!empty($row['user_sig_bbcode_uid'])) ? $row['user_sig_bbcode_uid'] : '',
  954. 'sig_bbcode_bitfield' => (!empty($row['user_sig_bbcode_bitfield'])) ? $row['user_sig_bbcode_bitfield'] : '',
  955. 'viewonline' => $row['user_allow_viewonline'],
  956. 'allow_pm' => $row['user_allow_pm'],
  957. 'avatar' => ($user->optionget('viewavatars')) ? get_user_avatar($row['user_avatar'], $row['user_avatar_type'], $row['user_avatar_width'], $row['user_avatar_height']) : '',
  958. 'age' => '',
  959. 'rank_title' => '',
  960. 'rank_image' => '',
  961. 'rank_image_src' => '',
  962. 'username' => $row['username'],
  963. 'user_colour' => $row['user_colour'],
  964. 'online' => false,
  965. 'profile' => append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=viewprofile&amp;u=$poster_id"),
  966. 'www' => $row['user_website'],
  967. '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") : '',
  968. '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") : '',
  969. 'yim' => ($row['user_yim']) ? 'http://edit.yahoo.com/config/send_webmesg?.target=' . urlencode($row['user_yim']) . '&amp;.src=pg' : '',
  970. '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") : '',
  971. 'search' => ($auth->acl_get('u_search')) ? append_sid("{$phpbb_root_path}search.$phpEx", "author_id=$poster_id&amp;sr=posts") : '',
  972. 'author_full' => get_username_string('full', $poster_id, $row['username'], $row['user_colour']),
  973. 'author_colour' => get_username_string('colour', $poster_id, $row['username'], $row['user_colour']),
  974. 'author_username' => get_username_string('username', $poster_id, $row['username'], $row['user_colour']),
  975. 'author_profile' => get_username_string('profile', $poster_id, $row['username'], $row['user_colour']),
  976. );
  977. 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']);
  978. if ((!empty($row['user_allow_viewemail']) && $auth->acl_get('u_sendemail')) || $auth->acl_get('a_email'))
  979. {
  980. $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']);
  981. }
  982. else
  983. {
  984. $user_cache[$poster_id]['email'] = '';
  985. }
  986. if (!empty($row['user_icq']))
  987. {
  988. $user_cache[$poster_id]['icq'] = 'http://www.icq.com/people/' . urlencode($row['user_icq']) . '/';
  989. $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="" />';
  990. }
  991. else
  992. {
  993. $user_cache[$poster_id]['icq_status_img'] = '';
  994. $user_cache[$poster_id]['icq'] = '';
  995. }
  996. if ($config['allow_birthdays'] && !empty($row['user_birthday']))
  997. {
  998. list($bday_day, $bday_month, $bday_year) = array_map('intval', explode('-', $row['user_birthday']));
  999. if ($bday_year)
  1000. {
  1001. $diff = $now['mon'] - $bday_month;
  1002. if ($diff == 0)
  1003. {
  1004. $diff = ($now['mday'] - $bday_day < 0) ? 1 : 0;
  1005. }
  1006. else
  1007. {
  1008. $diff = ($diff < 0) ? 1 : 0;
  1009. }
  1010. $user_cache[$poster_id]['age'] = (int) ($now['year'] - $bday_year - $diff);
  1011. }
  1012. }
  1013. }
  1014. }
  1015. }
  1016. $db->sql_freeresult($result);
  1017. // Load custom profile fields
  1018. if ($config['load_cpf_viewtopic'])
  1019. {
  1020. if (!class_exists('custom_profile'))
  1021. {
  1022. include($phpbb_root_path . 'includes/functions_profile_fields.' . $phpEx);
  1023. }
  1024. $cp = new custom_profile();
  1025. // Grab all profile fields from users in id cache for later use - similar to the poster cache
  1026. $profile_fields_tmp = $cp->generate_profile_fields_template('grab', $id_cache);
  1027. // filter out fields not to be displayed on viewtopic. Yes, it's a hack, but this shouldn't break any MODs.
  1028. $profile_fields_cache = array();
  1029. foreach ($profile_fields_tmp as $profile_user_id => $profile_fields)
  1030. {
  1031. $profile_fields_cache[$profile_user_id] = array();
  1032. foreach ($profile_fields as $used_ident => $profile_field)
  1033. {
  1034. if ($profile_field['data']['field_show_on_vt'])
  1035. {
  1036. $profile_fields_cache[$profile_user_id][$used_ident] = $profile_field;
  1037. }
  1038. }
  1039. }
  1040. unset($profile_fields_tmp);
  1041. }
  1042. // Generate online information for user
  1043. if ($config['load_onlinetrack'] && sizeof($id_cache))
  1044. {
  1045. $sql = 'SELECT session_user_id, MAX(session_time) as online_time, MIN(session_viewonline) AS viewonline
  1046. FROM ' . SESSIONS_TABLE . '
  1047. WHERE ' . $db->sql_in_set('session_user_id', $id_cache) . '
  1048. GROUP BY session_user_id';
  1049. $result = $db->sql_query($sql);
  1050. $update_time = $config['load_online_time'] * 60;
  1051. while ($row = $db->sql_fetchrow($result))
  1052. {
  1053. $user_cache[$row['session_user_id']]['online'] = (time() - $update_time < $row['online_time'] && (($row['viewonline']) || $auth->acl_get('u_viewonline'))) ? true : false;
  1054. }
  1055. $db->sql_freeresult($result);
  1056. }
  1057. unset($id_cache);
  1058. // Pull attachment data
  1059. if (sizeof($attach_list))
  1060. {
  1061. if ($auth->acl_get('u_download') && $auth->acl_get('f_download', $forum_id))
  1062. {
  1063. $sql = 'SELECT *
  1064. FROM ' . ATTACHMENTS_TABLE . '
  1065. WHERE ' . $db->sql_in_set('post_msg_id', $attach_list) . '
  1066. AND in_message = 0
  1067. ORDER BY filetime DESC, post_msg_id ASC';
  1068. $result = $db->sql_query($sql);
  1069. while ($row = $db->sql_fetchrow($result))
  1070. {
  1071. $attachments[$row['post_msg_id']][] = $row;
  1072. }
  1073. $db->sql_freeresult($result);
  1074. // No attachments exist, but post table thinks they do so go ahead and reset post_attach flags
  1075. if (!sizeof($attachments))
  1076. {
  1077. $sql = 'UPDATE ' . POSTS_TABLE . '
  1078. SET post_attachment = 0
  1079. WHERE ' . $db->sql_in_set('post_id', $attach_list);
  1080. $db->sql_query($sql);
  1081. // We need to update the topic indicator too if the complete topic is now without an attachment
  1082. if (sizeof($rowset) != $total_posts)
  1083. {
  1084. // Not all posts are displayed so we query the db to find if there's any attachment for this topic
  1085. $sql = 'SELECT a.post_msg_id as post_id
  1086. FROM ' . ATTACHMENTS_TABLE . ' a, ' . POSTS_TABLE . " p
  1087. WHERE p.topic_id = $topic_id
  1088. AND p.post_approved = 1
  1089. AND p.topic_id = a.topic_id";
  1090. $result = $db->sql_query_limit($sql, 1);
  1091. $row = $db->sql_fetchrow($result);
  1092. $db->sql_freeresult($result);
  1093. if (!$row)
  1094. {
  1095. $sql = 'UPDATE ' . TOPICS_TABLE . "
  1096. SET topic_attachment = 0
  1097. WHERE topic_id = $topic_id";
  1098. $db->sql_query($sql);
  1099. }
  1100. }
  1101. else
  1102. {
  1103. $sql = 'UPDATE ' . TOPICS_TABLE . "
  1104. SET topic_attachment = 0
  1105. WHERE topic_id = $topic_id";
  1106. $db->sql_query($sql);
  1107. }
  1108. }
  1109. else if ($has_attachments && !$topic_data['topic_attachment'])
  1110. {
  1111. // Topic has approved attachments but its flag is wrong
  1112. $sql = 'UPDATE ' . TOPICS_TABLE . "
  1113. SET topic_attachment = 1
  1114. WHERE topic_id = $topic_id";
  1115. $db->sql_query($sql);
  1116. $topic_data['topic_attachment'] = 1;
  1117. }
  1118. }
  1119. else
  1120. {
  1121. $display_notice = true;
  1122. }
  1123. }
  1124. // Instantiate BBCode if need be
  1125. if ($bbcode_bitfield !== '')
  1126. {
  1127. $bbcode = new bbcode(base64_encode($bbcode_bitfield));
  1128. }
  1129. $i_total = sizeof($rowset) - 1;
  1130. $prev_post_id = '';
  1131. $template->assign_vars(array(
  1132. 'S_NUM_POSTS' => sizeof($post_list))
  1133. );
  1134. // Output the posts
  1135. $first_unread = $post_unread = false;
  1136. for ($i = 0, $end = sizeof($post_list); $i < $end; ++$i)
  1137. {
  1138. // A non-existing rowset only happens if there was no user present for the entered poster_id
  1139. // This could be a broken posts table.
  1140. if (!isset($rowset[$post_list[$i]]))
  1141. {
  1142. continue;
  1143. }
  1144. $row =& $rowset[$post_list[$i]];
  1145. $poster_id = $row['user_id'];
  1146. // End signature parsing, only if needed
  1147. if ($user_cache[$poster_id]['sig'] && $row['enable_sig'] && empty($user_cache[$poster_id]['sig_parsed']))
  1148. {
  1149. $user_cache[$poster_id]['sig'] = censor_text($user_cache[$poster_id]['sig']);
  1150. if ($user_cache[$poster_id]['sig_bbcode_bitfield'])
  1151. {
  1152. $bbcode->bbcode_second_pass($user_cache[$poster_id]['sig'], $user_cache[$poster_id]['sig_bbcode_uid'], $user_cache[$poster_id]['sig_bbcode_bitfield']);
  1153. }
  1154. $user_cache[$poster_id]['sig'] = bbcode_nl2br($user_cache[$poster_id]['sig']);
  1155. $user_cache[$poster_id]['sig'] = smiley_text($user_cache[$poster_id]['sig']);
  1156. $user_cache[$poster_id]['sig_parsed'] = true;
  1157. }
  1158. // Parse the message and subject
  1159. $message = censor_text($row['post_text']);
  1160. // Second parse bbcode here
  1161. if ($row['bbcode_bitfield'])
  1162. {
  1163. $bbcode->bbcode_second_pass($message, $row['bbcode_uid'], $row['bbcode_bitfield']);
  1164. }
  1165. $message = bbcode_nl2br($message);
  1166. $message = smiley_text($message);
  1167. if (!empty($attachments[$row['post_id']]))
  1168. {
  1169. parse_attachments($forum_id, $message, $attachments[$row['post_id']], $update_count);
  1170. }
  1171. // Replace naughty words such as farty pan…

Large files files are truncated, but you can click here to view the full file