PageRenderTime 69ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/forum/viewtopic.php

https://github.com/GreyTeardrop/socionicasys-forum
PHP | 2079 lines | 1562 code | 285 blank | 232 comment | 452 complexity | fbdeaf1f81801c86754227bde276e73c MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-3.0, MPL-2.0-no-copyleft-exception

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

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