PageRenderTime 68ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/viewtopic.php

https://bitbucket.org/jablonski/yebood
PHP | 1784 lines | 1374 code | 266 blank | 144 comment | 382 complexity | e19ab5193bba61a9c3aa029f1420244e MD5 | raw file
Possible License(s): AGPL-1.0

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

  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. // Send vars to template
  519. $template->assign_vars(array(
  520. 'FORUM_ID' => $forum_id,
  521. 'FORUM_NAME' => $topic_data['forum_name'],
  522. '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']),
  523. 'TOPIC_ID' => $topic_id,
  524. 'TOPIC_TITLE' => $topic_data['topic_title'],
  525. 'TOPIC_POSTER' => $topic_data['topic_poster'],
  526. 'TOPIC_AUTHOR_FULL' => get_username_string('full', $topic_data['topic_poster'], $topic_data['topic_first_poster_name'], $topic_data['topic_first_poster_colour']),
  527. 'TOPIC_AUTHOR_COLOUR' => get_username_string('colour', $topic_data['topic_poster'], $topic_data['topic_first_poster_name'], $topic_data['topic_first_poster_colour']),
  528. 'TOPIC_AUTHOR' => get_username_string('username', $topic_data['topic_poster'], $topic_data['topic_first_poster_name'], $topic_data['topic_first_poster_colour']),
  529. 'PAGINATION' => $pagination,
  530. 'PAGE_NUMBER' => on_page($total_posts, $config['posts_per_page'], $start),
  531. 'TOTAL_POSTS' => ($total_posts == 1) ? $user->lang['VIEW_TOPIC_POST'] : sprintf($user->lang['VIEW_TOPIC_POSTS'], $total_posts),
  532. 'U_MCP' => ($auth->acl_get('m_', $forum_id)) ? append_sid("{$phpbb_root_path}mcp.$phpEx", "i=main&amp;mode=topic_view&amp;f=$forum_id&amp;t=$topic_id" . (($start == 0) ? '' : "&amp;start=$start") . ((strlen($u_sort_param)) ? "&amp;$u_sort_param" : ''), true, $user->session_id) : '',
  533. 'MODERATORS' => (isset($forum_moderators[$forum_id]) && sizeof($forum_moderators[$forum_id])) ? implode(', ', $forum_moderators[$forum_id]) : '',
  534. 'POST_IMG' => ($topic_data['forum_status'] == ITEM_LOCKED) ? $user->img('button_topic_locked', 'FORUM_LOCKED') : $user->img('button_topic_new', 'POST_NEW_TOPIC'),
  535. 'QUOTE_IMG' => $user->img('icon_post_quote', 'REPLY_WITH_QUOTE'),
  536. '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'),
  537. 'EDIT_IMG' => $user->img('icon_post_edit', 'EDIT_POST'),
  538. 'DELETE_IMG' => $user->img('icon_post_delete', 'DELETE_POST'),
  539. 'INFO_IMG' => $user->img('icon_post_info', 'VIEW_INFO'),
  540. 'PROFILE_IMG' => $user->img('icon_user_profile', 'READ_PROFILE'),
  541. 'SEARCH_IMG' => $user->img('icon_user_search', 'SEARCH_USER_POSTS'),
  542. 'PM_IMG' => $user->img('icon_contact_pm', 'SEND_PRIVATE_MESSAGE'),
  543. 'EMAIL_IMG' => $user->img('icon_contact_email', 'SEND_EMAIL'),
  544. 'WWW_IMG' => $user->img('icon_contact_www', 'VISIT_WEBSITE'),
  545. 'ICQ_IMG' => $user->img('icon_contact_icq', 'ICQ'),
  546. 'AIM_IMG' => $user->img('icon_contact_aim', 'AIM'),
  547. 'MSN_IMG' => $user->img('icon_contact_msnm', 'MSNM'),
  548. 'YIM_IMG' => $user->img('icon_contact_yahoo', 'YIM'),
  549. 'JABBER_IMG' => $user->img('icon_contact_jabber', 'JABBER') ,
  550. 'REPORT_IMG' => $user->img('icon_post_report', 'REPORT_POST'),
  551. 'REPORTED_IMG' => $user->img('icon_topic_reported', 'POST_REPORTED'),
  552. 'UNAPPROVED_IMG' => $user->img('icon_topic_unapproved', 'POST_UNAPPROVED'),
  553. 'WARN_IMG' => $user->img('icon_user_warn', 'WARN_USER'),
  554. 'S_IS_LOCKED' => ($topic_data['topic_status'] == ITEM_UNLOCKED && $topic_data['forum_status'] == ITEM_UNLOCKED) ? false : true,
  555. 'S_SELECT_SORT_DIR' => $s_sort_dir,
  556. 'S_SELECT_SORT_KEY' => $s_sort_key,
  557. 'S_SELECT_SORT_DAYS' => $s_limit_days,
  558. 'S_SINGLE_MODERATOR' => (!empty($forum_moderators[$forum_id]) && sizeof($forum_moderators[$forum_id]) > 1) ? false : true,
  559. 'S_TOPIC_ACTION' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id" . (($start == 0) ? '' : "&amp;start=$start")),
  560. 'S_TOPIC_MOD' => ($topic_mod != '') ? '<select name="action" id="quick-mod-select">' . $topic_mod . '</select>' : '',
  561. '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),
  562. 'S_VIEWTOPIC' => true,
  563. 'S_DISPLAY_SEARCHBOX' => ($auth->acl_get('u_search') && $auth->acl_get('f_search', $forum_id) && $config['load_search']) ? true : false,
  564. 'S_SEARCHBOX_ACTION' => append_sid("{$phpbb_root_path}search.$phpEx"),
  565. 'S_SEARCH_LOCAL_HIDDEN_FIELDS' => build_hidden_fields($s_search_hidden_fields),
  566. 'S_DISPLAY_POST_INFO' => ($topic_data['forum_type'] == FORUM_POST && ($auth->acl_get('f_post', $forum_id) || $user->data['user_id'] == ANONYMOUS)) ? true : false,
  567. 'S_DISPLAY_REPLY_INFO' => ($topic_data['forum_type'] == FORUM_POST && ($auth->acl_get('f_reply', $forum_id) || $user->data['user_id'] == ANONYMOUS)) ? true : false,
  568. 'S_ENABLE_FEEDS_TOPIC' => ($config['feed_topic'] && !phpbb_optionget(FORUM_OPTION_FEED_EXCLUDE, $topic_data['forum_options'])) ? true : false,
  569. 'U_TOPIC' => "{$server_path}viewtopic.$phpEx?f=$forum_id&amp;t=$topic_id",
  570. 'U_FORUM' => $server_path,
  571. 'U_VIEW_TOPIC' => $viewtopic_url,
  572. 'U_VIEW_FORUM' => append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_id),
  573. 'U_VIEW_OLDER_TOPIC' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;view=previous"),
  574. 'U_VIEW_NEWER_TOPIC' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;view=next"),
  575. 'U_PRINT_TOPIC' => ($auth->acl_get('f_print', $forum_id)) ? $viewtopic_url . '&amp;view=print' : '',
  576. 'U_EMAIL_TOPIC' => ($auth->acl_get('f_email', $forum_id) && $config['email_enable']) ? append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=email&amp;t=$topic_id") : '',
  577. 'U_WATCH_TOPIC' => $s_watching_topic['link'],
  578. 'L_WATCH_TOPIC' => $s_watching_topic['title'],
  579. 'S_WATCHING_TOPIC' => $s_watching_topic['is_watching'],
  580. 'U_BOOKMARK_TOPIC' => ($user->data['is_registered'] && $config['allow_bookmarks']) ? $viewtopic_url . '&amp;bookmark=1&amp;hash=' . generate_link_hash("topic_$topic_id") : '',
  581. 'L_BOOKMARK_TOPIC' => ($user->data['is_registered'] && $config['allow_bookmarks'] && $topic_data['bookmarked']) ? $user->lang['BOOKMARK_TOPIC_REMOVE'] : $user->lang['BOOKMARK_TOPIC'],
  582. '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") : '',
  583. '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") : '',
  584. '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")) : '')
  585. );
  586. // Does this topic contain a poll?
  587. if (!empty($topic_data['poll_start']))
  588. {
  589. $sql = 'SELECT o.*, p.bbcode_bitfield, p.bbcode_uid
  590. FROM ' . POLL_OPTIONS_TABLE . ' o, ' . POSTS_TABLE . " p
  591. WHERE o.topic_id = $topic_id
  592. AND p.post_id = {$topic_data['topic_first_post_id']}
  593. AND p.topic_id = o.topic_id
  594. ORDER BY o.poll_option_id";
  595. $result = $db->sql_query($sql);
  596. $poll_info = array();
  597. while ($row = $db->sql_fetchrow($result))
  598. {
  599. $poll_info[] = $row;
  600. }
  601. $db->sql_freeresult($result);
  602. $cur_voted_id = array();
  603. if ($user->data['is_registered'])
  604. {
  605. $sql = 'SELECT poll_option_id
  606. FROM ' . POLL_VOTES_TABLE . '
  607. WHERE topic_id = ' . $topic_id . '
  608. AND vote_user_id = ' . $user->data['user_id'];
  609. $result = $db->sql_query($sql);
  610. while ($row = $db->sql_fetchrow($result))
  611. {
  612. $cur_voted_id[] = $row['poll_option_id'];
  613. }
  614. $db->sql_freeresult($result);
  615. }
  616. else
  617. {
  618. // Cookie based guest tracking ... I don't like this but hum ho
  619. // it's oft requested. This relies on "nice" users who don't feel
  620. // the need to delete cookies to mess with results.
  621. if (isset($_COOKIE[$config['cookie_name'] . '_poll_' . $topic_id]))
  622. {
  623. $cur_voted_id = explode(',', $_COOKIE[$config['cookie_name'] . '_poll_' . $topic_id]);
  624. $cur_voted_id = array_map('intval', $cur_voted_id);
  625. }
  626. }
  627. // Can not vote at all if no vote permission
  628. $s_can_vote = ($auth->acl_get('f_vote', $forum_id) &&
  629. (($topic_data['poll_length'] != 0 && $topic_data['poll_start'] + $topic_data['poll_length'] > time()) || $topic_data['poll_length'] == 0) &&
  630. $topic_data['topic_status'] != ITEM_LOCKED &&
  631. $topic_data['forum_status'] != ITEM_LOCKED &&
  632. (!sizeof($cur_voted_id) ||
  633. ($auth->acl_get('f_votechg', $forum_id) && $topic_data['poll_vote_change']))) ? true : false;
  634. $s_display_results = (!$s_can_vote || ($s_can_vote && sizeof($cur_voted_id)) || $view == 'viewpoll') ? true : false;
  635. if ($update && $s_can_vote)
  636. {
  637. if (!sizeof($voted_id) || sizeof($voted_id) > $topic_data['poll_max_options'] || in_array(VOTE_CONVERTED, $cur_voted_id) || !check_form_key('posting'))
  638. {
  639. $redirect_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id" . (($start == 0) ? '' : "&amp;start=$start"));
  640. meta_refresh(5, $redirect_url);
  641. if (!sizeof($voted_id))
  642. {
  643. $message = 'NO_VOTE_OPTION';
  644. }
  645. else if (sizeof($voted_id) > $topic_data['poll_max_options'])
  646. {
  647. $message = 'TOO_MANY_VOTE_OPTIONS';
  648. }
  649. else if (in_array(VOTE_CONVERTED, $cur_voted_id))
  650. {
  651. $message = 'VOTE_CONVERTED';
  652. }
  653. else
  654. {
  655. $message = 'FORM_INVALID';
  656. }
  657. $message = $user->lang[$message] . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $redirect_url . '">', '</a>');
  658. trigger_error($message);
  659. }
  660. foreach ($voted_id as $option)
  661. {
  662. if (in_array($option, $cur_voted_id))
  663. {
  664. continue;
  665. }
  666. $sql = 'UPDATE ' . POLL_OPTIONS_TABLE . '
  667. SET poll_option_total = poll_option_total + 1
  668. WHERE poll_option_id = ' . (int) $option . '
  669. AND topic_id = ' . (int) $topic_id;
  670. $db->sql_query($sql);
  671. if ($user->data['is_registered'])
  672. {
  673. $sql_ary = array(
  674. 'topic_id' => (int) $topic_id,
  675. 'poll_option_id' => (int) $option,
  676. 'vote_user_id' => (int) $user->data['user_id'],
  677. 'vote_user_ip' => (string) $user->ip,
  678. );
  679. $sql = 'INSERT INTO ' . POLL_VOTES_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
  680. $db->sql_query($sql);
  681. }
  682. }
  683. foreach ($cur_voted_id as $option)
  684. {
  685. if (!in_array($option, $voted_id))
  686. {
  687. $sql = 'UPDATE ' . POLL_OPTIONS_TABLE . '
  688. SET poll_option_total = poll_option_total - 1
  689. WHERE poll_option_id = ' . (int) $option . '
  690. AND topic_id = ' . (int) $topic_id;
  691. $db->sql_query($sql);
  692. if ($user->data['is_registered'])
  693. {
  694. $sql = 'DELETE FROM ' . POLL_VOTES_TABLE . '
  695. WHERE topic_id = ' . (int) $topic_id . '
  696. AND poll_option_id = ' . (int) $option . '
  697. AND vote_user_id = ' . (int) $user->data['user_id'];
  698. $db->sql_query($sql);
  699. }
  700. }
  701. }
  702. if ($user->data['user_id'] == ANONYMOUS && !$user->data['is_bot'])
  703. {
  704. $user->set_cookie('poll_' . $topic_id, implode(',', $voted_id), time() + 31536000);
  705. }
  706. $sql = 'UPDATE ' . TOPICS_TABLE . '
  707. SET poll_last_vote = ' . time() . "
  708. WHERE topic_id = $topic_id";
  709. //, topic_last_post_time = ' . time() . " -- for bumping topics with new votes, ignore for now
  710. $db->sql_query($sql);
  711. $redirect_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id" . (($start == 0) ? '' : "&amp;start=$start"));
  712. meta_refresh(5, $redirect_url);
  713. trigger_error($user->lang['VOTE_SUBMITTED'] . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $redirect_url . '">', '</a>'));
  714. }
  715. $poll_total = 0;
  716. foreach ($poll_info as $poll_option)
  717. {
  718. $poll_total += $poll_option['poll_option_total'];
  719. }
  720. if ($poll_info[0]['bbcode_bitfield'])
  721. {
  722. $poll_bbcode = new bbcode();
  723. }
  724. else
  725. {
  726. $poll_bbcode = false;
  727. }
  728. for ($i = 0, $size = sizeof($poll_info); $i < $size; $i++)
  729. {
  730. $poll_info[$i]['poll_option_text'] = censor_text($poll_info[$i]['poll_option_text']);
  731. if ($poll_bbcode !== false)
  732. {
  733. $poll_bbcode->bbcode_second_pass($poll_info[$i]['poll_option_text'], $poll_info[$i]['bbcode_uid'], $poll_option['bbcode_bitfield']);
  734. }
  735. $poll_info[$i]['poll_option_text'] = bbcode_nl2br($poll_info[$i]['poll_option_text']);
  736. $poll_info[$i]['poll_option_text'] = smiley_text($poll_info[$i]['poll_option_text']);
  737. }
  738. $topic_data['poll_title'] = censor_text($topic_data['poll_title']);
  739. if ($poll_bbcode !== false)
  740. {
  741. $poll_bbcode->bbcode_second_pass($topic_data['poll_title'], $poll_info[0]['bbcode_uid'], $poll_info[0]['bbcode_bitfield']);
  742. }
  743. $topic_data['poll_title'] = bbcode_nl2br($topic_data['poll_title']);
  744. $topic_data['poll_title'] = smiley_text($topic_data['poll_title']);
  745. unset($poll_bbcode);
  746. foreach ($poll_info as $poll_option)
  747. {
  748. $option_pct = ($poll_total > 0) ? $poll_option['poll_option_total'] / $poll_total : 0;
  749. $option_pct_txt = sprintf("%.1d%%", round($option_pct * 100));
  750. $template->assign_block_vars('poll_option', array(
  751. 'POLL_OPTION_ID' => $poll_option['poll_option_id'],
  752. 'POLL_OPTION_CAPTION' => $poll_option['poll_option_text'],
  753. 'POLL_OPTION_RESULT' => $poll_option['poll_option_total'],
  754. 'POLL_OPTION_PERCENT' => $option_pct_txt,
  755. 'POLL_OPTION_PCT' => round($option_pct * 100),
  756. 'POLL_OPTION_IMG' => $user->img('poll_center', $option_pct_txt, round($option_pct * 250)),
  757. 'POLL_OPTION_VOTED' => (in_array($poll_option['poll_option_id'], $cur_voted_id)) ? true : false)
  758. );
  759. }
  760. $poll_end = $topic_data['poll_length'] + $topic_data['poll_start'];
  761. $template->assign_vars(array(
  762. 'POLL_QUESTION' => $topic_data['poll_title'],
  763. 'TOTAL_VOTES' => $poll_total,
  764. 'POLL_LEFT_CAP_IMG' => $user->img('poll_left'),
  765. 'POLL_RIGHT_CAP_IMG'=> $user->img('poll_right'),
  766. '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']),
  767. 'L_POLL_LENGTH' => ($topic_data['poll_length']) ? sprintf($user->lang[($poll_end > time()) ? 'POLL_RUN_TILL' : 'POLL_ENDED_AT'], $user->format_date($poll_end)) : '',
  768. 'S_HAS_POLL' => true,
  769. 'S_CAN_VOTE' => $s_can_vote,
  770. 'S_DISPLAY_RESULTS' => $s_display_results,
  771. 'S_IS_MULTI_CHOICE' => ($topic_data['poll_max_options'] > 1) ? true : false,
  772. 'S_POLL_ACTION' => $viewtopic_url,
  773. 'U_VIEW_RESULTS' => $viewtopic_url . '&amp;view=viewpoll')
  774. );
  775. unset($poll_end, $poll_info, $voted_id);
  776. }
  777. // If the user is trying to reach the second half of the topic, fetch it starting from the end
  778. $store_reverse = false;
  779. $sql_limit = $config['posts_per_page'];
  780. $sql_sort_order = $direction = '';
  781. if ($start > $total_posts / 2)
  782. {
  783. $store_reverse = true;
  784. if ($start + $config['posts_per_page'] > $total_posts)
  785. {
  786. $sql_limit = min($config['posts_per_page'], max(1, $total_posts - $start));
  787. }
  788. // Select the sort order
  789. $direction = (($sort_dir == 'd') ? 'ASC' : 'DESC');
  790. $sql_start = max(0, $total_posts - $sql_limit - $start);
  791. }
  792. else
  793. {
  794. // Select the sort order
  795. $direction = (($sort_dir == 'd') ? 'DESC' : 'ASC');
  796. $sql_start = $start;
  797. }
  798. if (is_array($sort_by_sql[$sort_key]))
  799. {
  800. $sql_sort_order = implode(' ' . $direction . ', ', $sort_by_sql[$sort_key]) . ' ' . $direction;
  801. }
  802. else
  803. {
  804. $sql_sort_order = $sort_by_sql[$sort_key] . ' ' . $direction;
  805. }
  806. // Container for user details, only process once
  807. $post_list = $user_cache = $id_cache = $attachments = $attach_list = $rowset = $update_count = $post_edit_list = array();
  808. $has_attachments = $display_notice = false;
  809. $bbcode_bitfield = '';
  810. $i = $i_total = 0;
  811. // Go ahead and pull all data for this topic
  812. $sql = 'SELECT p.post_id
  813. FROM ' . POSTS_TABLE . ' p' . (($join_user_sql[$sort_key]) ? ', ' . USERS_TABLE . ' u': '') . "
  814. WHERE p.topic_id = $topic_id
  815. " . ((!$auth->acl_get('m_approve', $forum_id)) ? 'AND p.post_approved = 1' : '') . "
  816. " . (($join_user_sql[$sort_key]) ? 'AND u.user_id = p.poster_id': '') . "
  817. $limit_posts_time
  818. ORDER BY $sql_sort_order";
  819. $result = $db->sql_query_limit($sql, $sql_limit, $sql_start);
  820. $i = ($store_reverse) ? $sql_limit - 1 : 0;
  821. while ($row = $db->sql_fetchrow($result))
  822. {
  823. $post_list[$i] = (int) $row['post_id'];
  824. ($store_reverse) ? $i-- : $i++;
  825. }
  826. $db->sql_freeresult($result);
  827. if (!sizeof($post_list))
  828. {
  829. if ($sort_days)
  830. {
  831. trigger_error('NO_POSTS_TIME_FRAME');
  832. }
  833. else
  834. {
  835. trigger_error('NO_TOPIC');
  836. }
  837. }
  838. // Holding maximum post time for marking topic read
  839. // We need to grab it because we do reverse ordering sometimes
  840. $max_post_time = 0;
  841. $sql = $db->sql_build_query('SELECT', array(
  842. 'SELECT' => 'u.*, z.friend, z.foe, p.*',
  843. 'FROM' => array(
  844. USERS_TABLE => 'u',
  845. POSTS_TABLE => 'p',
  846. ),
  847. 'LEFT_JOIN' => array(
  848. array(
  849. 'FROM' => array(ZEBRA_TABLE => 'z'),
  850. 'ON' => 'z.user_id = ' . $user->data['user_id'] . ' AND z.zebra_id = p.poster_id'
  851. )
  852. ),
  853. 'WHERE' => $db->sql_in_set('p.post_id', $post_list) . '
  854. AND u.user_id = p.poster_id'
  855. ));
  856. $result = $db->sql_query($sql);
  857. $now = phpbb_gmgetdate(time() + $user->timezone + $user->dst);
  858. // Posts are stored in the $rowset array while $attach_list, $user_cache
  859. // and the global bbcode_bitfield are built
  860. while ($row = $db->sql_fetchrow($result))
  861. {
  862. // Set max_post_time
  863. if ($row['post_time'] > $max_post_time)
  864. {
  865. $max_post_time = $row['post_time'];
  866. }
  867. $poster_id = (int) $row['poster_id'];
  868. // Does post have an attachment? If so, add it to the list
  869. if ($row['post_attachment'] && $config['allow_attachments'])
  870. {
  871. $attach_list[] = (int) $row['post_id'];
  872. if ($row['post_approved'])
  873. {
  874. $has_attachments = true;
  875. }
  876. }
  877. $rowset[$row['post_id']] = array(
  878. 'hide_post' => ($row['foe'] && ($view != 'show' || $post_id != $row['post_id'])) ? true : false,
  879. 'post_id' => $row['post_id'],
  880. 'post_time' => $row['post_time'],
  881. 'user_id' => $row['user_id'],
  882. 'username' => $row['username'],
  883. 'user_colour' => $row['user_colour'],
  884. 'topic_id' => $row['topic_id'],
  885. 'forum_id' => $row['forum_id'],
  886. 'post_subject' => $row['post_subject'],
  887. 'post_edit_count' => $row['post_edit_count'],
  888. 'post_edit_time' => $row['post_edit_time'],
  889. 'post_edit_reason' => $row['post_edit_reason'],
  890. 'post_edit_user' => $row['post_edit_user'],
  891. 'post_edit_locked' => $row['post_edit_locked'],
  892. // Make sure the icon actually exists
  893. 'icon_id' => (isset($icons[$row['icon_id']]['img'], $icons[$row['icon_id']]['height'], $icons[$row['icon_id']]['width'])) ? $row['icon_id'] : 0,
  894. 'post_attachment' => $row['post_attachment'],
  895. 'post_approved' => $row['post_approved'],
  896. 'post_reported' => $row['post_reported'],
  897. 'post_username' => $row['post_username'],
  898. 'post_text' => $row['post_text'],
  899. 'bbcode_uid' => $row['bbcode_uid'],
  900. 'bbcode_bitfield' => $row['bbcode_bitfield'],
  901. 'enable_smilies' => $row['enable_smilies'],
  902. 'enable_sig' => $row['enable_sig'],
  903. 'friend' => $row['friend'],
  904. 'foe' => $row['foe'],
  905. );
  906. // Define the global bbcode bitfield, will be used to load bbcodes
  907. $bbcode_bitfield = $bbcode_bitfield | base64_decode($row['bbcode_bitfield']);
  908. // Is a signature attached? Are we going to display it?
  909. if ($row['enable_sig'] && $config['allow_sig'] && $user->optionget('viewsigs'))
  910. {
  911. $bbcode_bitfield = $bbcode_bitfield | base64_decode($row['user_sig_bbcode_bitfield']);
  912. }
  913. // Cache various user specific data ... so we don't have to recompute
  914. // this each time the same user appears on this page
  915. if (!isset($user_cache[$poster_id]))
  916. {
  917. if ($poster_id == ANONYMOUS)
  918. {
  919. $user_cache[$poster_id] = array(
  920. 'joined' => '',
  921. 'posts' => '',
  922. 'from' => '',
  923. 'sig' => '',
  924. 'sig_bbcode_uid' => '',
  925. 'sig_bbcode_bitfield' => '',
  926. 'online' => false,
  927. 'avatar' => ($user->optionget('viewavatars')) ? get_user_avatar($row['user_avatar'], $row['user_avatar_type'], $row['user_avatar_width'], $row['user_avatar_height']) : '',
  928. 'rank_title' => '',
  929. 'rank_image' => '',
  930. 'rank_image_src' => '',
  931. 'sig' => '',
  932. 'profile' => '',
  933. 'pm' => '',
  934. 'email' => '',
  935. 'www' => '',
  936. 'icq_status_img' => '',
  937. 'icq' => '',
  938. 'aim' => '',
  939. 'msn' => '',
  940. 'yim' => '',
  941. 'jabber' => '',
  942. 'search' => '',
  943. 'age' => '',
  944. 'username' => $row['username'],
  945. 'user_colour' => $row['user_colour'],
  946. 'warnings' => 0,
  947. 'allow_pm' => 0,
  948. );
  949. 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']);
  950. }
  951. else
  952. {
  953. $user_sig = '';
  954. // We add the signature to every posters entry because enable_sig is post dependant
  955. if ($row['user_sig'] && $config['allow_sig'] && $user->optionget('viewsigs'))
  956. {
  957. $user_sig = $row['user_sig'];
  958. }
  959. $id_cache[] = $poster_id;
  960. $user_cache[$poster_id] = array(
  961. 'joined' => $user->format_date($row['user_regdate']),
  962. 'posts' => $row['user_posts'],
  963. 'warnings' => (isset($row['user_warnings'])) ? $row['user_warnings'] : 0,
  964. 'from' => (!empty($row['user_from'])) ? $row['user_from'] : '',
  965. 'sig' => $user_sig,
  966. 'sig_bbcode_uid' => (!empty($row['user_sig_bbcode_uid'])) ? $row['user_sig_bbcode_uid'] : '',
  967. 'sig_bbcode_bitfield' => (!empty($row['user_sig_bbcode_bitfield'])) ? $row['user_sig_bbcode_bitfield'] : '',
  968. 'viewonline' => $row['user_allow_viewonline'],
  969. 'allow_pm' => $row['user_allow_pm'],
  970. 'avatar' => ($user->optionget('viewavatars')) ? get_user_avatar($row['user_avatar'], $row['user_avatar_type'], $row['user_avatar_width'], $row['user_avatar_height']) : '',
  971. 'age' => '',
  972. 'rank_title' => '',
  973. 'rank_image' => '',
  974. 'rank_image_src' => '',
  975. 'username' => $row['username'],
  976. 'user_colour' => $row['user_colour'],
  977. 'online' => false,
  978. 'profile' => append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=viewprofile&amp;u=$poster_id"),
  979. 'www' => $row['user_website'],
  980. '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") : '',
  981. '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") : '',
  982. 'yim' => ($row['user_yim']) ? 'http://edit.yahoo.com/config/send_webmesg?.target=' . urlencode($row['user_yim']) . '&amp;.src=pg' : '',
  983. '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") : '',
  984. 'search' => ($auth->acl_get('u_search')) ? append_sid("{$phpbb_root_path}search.$phpEx", "author_id=$poster_id&amp;sr=posts") : '',
  985. 'author_full' => get_username_string('full', $poster_id, $row['username'], $row['user_colour']),
  986. 'author_colour' => get_username_string('colour', $poster_id, $row['username'], $row['user_colour']),
  987. 'author_username' => get_username_string('username', $poster_id, $row['username'], $row['user_colour']),
  988. 'author_profile' => get_username_string('profile', $poster_id, $row['username'], $row['user_colour']),
  989. );
  990. 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']);
  991. if ((!empty($row['user_allow_viewemail']) && $auth->acl_get('u_sendemail')) || $auth->acl_get('a_email'))
  992. {
  993. $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']);
  994. }
  995. else
  996. {
  997. $user_cache[$poster_id]['email'] = '';
  998. }
  999. if (!empty($row['user_icq']))
  1000. {
  1001. $user_cache[$poster_id]['icq'] = 'http://www.icq.com/people/' . urlencode($row['user_icq']) . '/';
  1002. $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="" />';
  1003. }
  1004. else
  1005. {
  1006. $user_cache[$poster_id]['icq_status_img'] = '';
  1007. $user_cache[$poster_id]['icq'] = '';
  1008. }
  1009. if ($config['allow_birthdays'] && !empty($row['user_birthday']))
  1010. {
  1011. list($bday_day, $bday_month, $bday_year) = array_map('intval', explode('-', $row['user_birthday']));
  1012. if ($bday_year)
  1013. {
  1014. $diff = $now['mon'] - $bday_month;
  1015. if ($diff == 0)
  1016. {
  1017. $diff = ($now['mday'] - $bday_day < 0) ? 1 : 0;
  1018. }
  1019. else
  1020. {
  1021. $diff = ($diff < 0) ? 1 : 0;
  1022. }
  1023. $user_cache[$poster_id]['age'] = (int) ($now['year'] - $bday_year - $diff);
  1024. }
  1025. }
  1026. }
  1027. }
  1028. }
  1029. $db->sql_freeresult($result);
  1030. // Load custom profile fields
  1031. if ($config['load_cpf_viewtopic'])
  1032. {
  1033. if (!class_exists('custom_profile'))
  1034. {
  1035. include($phpbb_root_path . 'includes/functions_profile_fields.' . $phpEx);
  1036. }
  1037. $cp = new custom_profile();
  1038. // Grab all profile fields from users in id cache for later use - similar to the poster cache
  1039. $profile_fields_tmp = $cp->generate_profile_fields_template('grab', $id_cache);
  1040. // filter out fields not to be displayed on viewtopic. Yes, it's a hack, but this shouldn't break any MODs.
  1041. $profile_fields_cache = array();
  1042. foreach ($profile_fields_tmp as $profile_user_id => $profile_fields)
  1043. {
  1044. $profile_fields_cache[$profile_user_id] = array();
  1045. foreach ($profile_fields as $used_ident => $profile_field)
  1046. {
  1047. if ($profile_field['data']['field_show_on_vt'])
  1048. {
  1049. $profile_fields_cache[$profile_user_id][$used_ident] = $profile_field;
  1050. }
  1051. }
  1052. }
  1053. unset($profile_fields_tmp);
  1054. }
  1055. // Generate online information for user
  1056. if ($config['load_onlinetrack'] && sizeof($id_cache))
  1057. {
  1058. $sql = 'SELECT session_user_id, MAX(session_time) as online_time, MIN(session_viewonline) AS viewonline
  1059. FROM ' . SESSIONS_TABLE . '
  1060. WHERE ' . $db->sql_in_set('session_user_id', $id_cache) . '
  1061. GROUP BY session_user_id';
  1062. $result = $db->sql_query($sql);
  1063. $update_time = $config['load_online_time'] * 60;
  1064. while ($row = $db->sql_fetchrow($result))
  1065. {
  1066. $user_cache[$row['session_user_id']]['online'] = (time() - $update_time < $row['online_time'] && (($row['viewonline']) || $auth->acl_get('u_viewonline'))) ? true : false;
  1067. }
  1068. $db->sql_freeresult($result);
  1069. }
  1070. unset($id_cache);
  1071. // Pull attachment data
  1072. if (sizeof($attach_list))
  1073. {
  1074. if ($auth->acl_get('u_download') && $auth->acl_get('f_download', $forum_id))
  1075. {
  1076. $sql = 'SELECT *
  1077. FROM ' . ATTACHMENTS_TABLE . '
  1078. WHERE ' . $db->sql_in_set('post_msg_id', $attach_list) . '
  1079. AND in_message = 0
  1080. ORDER BY filetime DESC, post_msg_id ASC';
  1081. $result = $db->sql_query($sql);
  1082. while ($row = $db->sql_fetchrow($result))
  1083. {
  1084. $attachments[$row['post_msg_id']][] = $row;
  1085. }
  1086. $db->sql_freeresult($result);
  1087. // No attachments exist, but post table thinks they do so go ahead and reset post_attach flags
  1088. if (!sizeof($attachments))
  1089. {
  1090. $sql = 'UPDATE ' . POSTS_TABLE . '
  1091. SET post_attachment = 0
  1092. WHERE ' . $db->sql_in_set('post_id', $attach_list);
  1093. $db->sql_query($sql);
  1094. // We need to update the topic indicator too if the complete topic is now without an attachment
  1095. if (sizeof($rowset) != $total_posts)
  1096. {
  1097. // Not all posts are displayed so we query the db to find if there's any attachment for this topic
  1098. $sql = 'SELECT a.post_msg_id as post_id
  1099. FROM ' . ATTACHMENTS_TABLE . ' a, ' . POSTS_TABLE . " p
  1100. WHERE p.topic_id = $topic_id
  1101. AND p.post_approved = 1
  1102. AND p.topic_id = a.topic_id";
  1103. $result = $db->sql_query_limit($sql, 1);
  1104. $row = $db->sql_fetchrow($result);
  1105. $db->sql_freeresult($result);
  1106. if (!$row)
  1107. {
  1108. $sql = 'UPDATE ' . TOPICS_TABLE . "
  1109. SET topic_attachment = 0
  1110. WHERE topic_id = $topic_id";
  1111. $db->sql_query($sql);
  1112. }
  1113. }
  1114. else
  1115. {
  1116. $sql = 'UPDATE ' . TOPICS_TABLE . "
  1117. SET topic_attachment = 0
  1118. WHERE topic_id = $topic_id";
  1119. $db->sql_query($sql);
  1120. }
  1121. }
  1122. else if ($has_attachments && !$topic_data['topic_attachment'])
  1123. {
  1124. // Topic has approved attachments but its flag is wrong
  1125. $sql = 'UPDATE ' . TOPICS_TABLE . "
  1126. SET topic_attachment = 1
  1127. WHERE topic_id = $topic_id";
  1128. $db->sql_query($sql);
  1129. $topic_data['topic_attachment'] = 1;
  1130. }
  1131. }
  1132. else
  1133. {
  1134. $display_notice = true;
  1135. }
  1136. }
  1137. // Instantiate BBCode if need be
  1138. if ($bbcode_bitfield !== '')
  1139. {
  1140. $bbcode = new bbcode(base64_encode($bbcode_bitfield));
  1141. }
  1142. $i_total = sizeof($rowset) - 1;
  1143. $prev_post_id = '';
  1144. $template->assign_vars(array(
  1145. 'S_NUM_POSTS' => sizeof($post_list))
  1146. );
  1147. // Output the posts
  1148. $first_unread = $post_unread = false;
  1149. for ($i = 0, $end = sizeof($post_list); $i < $end; ++$i)
  1150. {
  1151. // A non-existing rowset only happens if there was no user present for the entered poster_id
  1152. // This could be a broken posts table.
  1153. if (!isset($rowset[$post_list[$i]]))
  1154. {
  1155. continue;
  1156. }
  1157. $row =& $rowset[$post_list[$i]];
  1158. $poster_id = $row['user_id'];
  1159. // End signature parsing, only if needed
  1160. if ($user_cache[$poster_id]['sig'] && $row['enable_sig'] && empty($user_cache[$poster_id]['sig_parsed']))
  1161. {
  1162. $user_cache[$poster_id]['sig'] = censor_text($user_cache[$poster_id]['sig']);
  1163. if ($user_cache[$poster_id]['sig_bbcode_bitfield'])
  1164. {
  1165. $bbcode->bbcode_second_pass($user_cache[$poster_id]['sig'], $user_cache[$poster_id]['sig_bbcode_uid'], $user_cache[$poster_id]['sig_bbcode_bitfield']);
  1166. }
  1167. $user_cache[$poster_id]['sig'] = bbcode_nl2br($user_cache[$poster_id]['sig']);
  1168. $user_cache[$poster_id]['sig'] = smiley_text($user_cache[$poster_id]['sig']);
  1169. $user_cache[$poster_id]['sig_parsed'] = true;
  1170. }
  1171. // Parse the message and subject
  1172. $message = censor_text($row['post_text']);

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