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

/phpBB/viewtopic.php

http://github.com/phpbb/phpbb3
PHP | 2427 lines | 1669 code | 319 blank | 439 comment | 438 complexity | 6e4a49f9f5e8297d065940f5d92a74e8 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. * This file is part of the phpBB Forum Software package.
  5. *
  6. * @copyright (c) phpBB Limited <https://www.phpbb.com>
  7. * @license GNU General Public License, version 2 (GPL-2.0)
  8. *
  9. * For full copyright and license information, please see
  10. * the docs/CREDITS.txt file.
  11. *
  12. */
  13. /**
  14. * @ignore
  15. */
  16. define('IN_PHPBB', true);
  17. $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
  18. $phpEx = substr(strrchr(__FILE__, '.'), 1);
  19. include($phpbb_root_path . 'common.' . $phpEx);
  20. include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
  21. include($phpbb_root_path . 'includes/bbcode.' . $phpEx);
  22. include($phpbb_root_path . 'includes/functions_user.' . $phpEx);
  23. // Start session management
  24. $user->session_begin();
  25. $auth->acl($user->data);
  26. // Initial var setup
  27. $forum_id = $request->variable('f', 0);
  28. $topic_id = $request->variable('t', 0);
  29. $post_id = $request->variable('p', 0);
  30. $voted_id = $request->variable('vote_id', array('' => 0));
  31. $voted_id = (count($voted_id) > 1) ? array_unique($voted_id) : $voted_id;
  32. $start = $request->variable('start', 0);
  33. $view = $request->variable('view', '');
  34. $default_sort_days = (!empty($user->data['user_post_show_days'])) ? $user->data['user_post_show_days'] : 0;
  35. $default_sort_key = (!empty($user->data['user_post_sortby_type'])) ? $user->data['user_post_sortby_type'] : 't';
  36. $default_sort_dir = (!empty($user->data['user_post_sortby_dir'])) ? $user->data['user_post_sortby_dir'] : 'a';
  37. $sort_days = $request->variable('st', $default_sort_days);
  38. $sort_key = $request->variable('sk', $default_sort_key);
  39. $sort_dir = $request->variable('sd', $default_sort_dir);
  40. $update = $request->variable('update', false);
  41. /* @var $pagination \phpbb\pagination */
  42. $pagination = $phpbb_container->get('pagination');
  43. $s_can_vote = false;
  44. /**
  45. * @todo normalize?
  46. */
  47. $hilit_words = $request->variable('hilit', '', true);
  48. // Do we have a topic or post id?
  49. if (!$topic_id && !$post_id)
  50. {
  51. trigger_error('NO_TOPIC');
  52. }
  53. /* @var $phpbb_content_visibility \phpbb\content_visibility */
  54. $phpbb_content_visibility = $phpbb_container->get('content.visibility');
  55. // Find topic id if user requested a newer or older topic
  56. if ($view && !$post_id)
  57. {
  58. if (!$forum_id)
  59. {
  60. $sql = 'SELECT forum_id
  61. FROM ' . TOPICS_TABLE . "
  62. WHERE topic_id = $topic_id";
  63. $result = $db->sql_query($sql);
  64. $forum_id = (int) $db->sql_fetchfield('forum_id');
  65. $db->sql_freeresult($result);
  66. if (!$forum_id)
  67. {
  68. trigger_error('NO_TOPIC');
  69. }
  70. }
  71. if ($view == 'unread')
  72. {
  73. // Get topic tracking info
  74. $topic_tracking_info = get_complete_topic_tracking($forum_id, $topic_id);
  75. $topic_last_read = (isset($topic_tracking_info[$topic_id])) ? $topic_tracking_info[$topic_id] : 0;
  76. $sql = 'SELECT post_id, topic_id, forum_id
  77. FROM ' . POSTS_TABLE . "
  78. WHERE topic_id = $topic_id
  79. AND " . $phpbb_content_visibility->get_visibility_sql('post', $forum_id) . "
  80. AND post_time > $topic_last_read
  81. AND forum_id = $forum_id
  82. ORDER BY post_time ASC, post_id ASC";
  83. $result = $db->sql_query_limit($sql, 1);
  84. $row = $db->sql_fetchrow($result);
  85. $db->sql_freeresult($result);
  86. if (!$row)
  87. {
  88. $sql = 'SELECT topic_last_post_id as post_id, topic_id, forum_id
  89. FROM ' . TOPICS_TABLE . '
  90. WHERE topic_id = ' . $topic_id;
  91. $result = $db->sql_query($sql);
  92. $row = $db->sql_fetchrow($result);
  93. $db->sql_freeresult($result);
  94. }
  95. if (!$row)
  96. {
  97. // Setup user environment so we can process lang string
  98. $user->setup('viewtopic');
  99. trigger_error('NO_TOPIC');
  100. }
  101. $post_id = $row['post_id'];
  102. $topic_id = $row['topic_id'];
  103. }
  104. else if ($view == 'next' || $view == 'previous')
  105. {
  106. $sql_condition = ($view == 'next') ? '>' : '<';
  107. $sql_ordering = ($view == 'next') ? 'ASC' : 'DESC';
  108. $sql = 'SELECT forum_id, topic_last_post_time
  109. FROM ' . TOPICS_TABLE . '
  110. WHERE topic_id = ' . $topic_id;
  111. $result = $db->sql_query($sql);
  112. $row = $db->sql_fetchrow($result);
  113. $db->sql_freeresult($result);
  114. if (!$row)
  115. {
  116. $user->setup('viewtopic');
  117. // OK, the topic doesn't exist. This error message is not helpful, but technically correct.
  118. trigger_error(($view == 'next') ? 'NO_NEWER_TOPICS' : 'NO_OLDER_TOPICS');
  119. }
  120. else
  121. {
  122. $sql = 'SELECT topic_id, forum_id
  123. FROM ' . TOPICS_TABLE . '
  124. WHERE forum_id = ' . $row['forum_id'] . "
  125. AND topic_moved_id = 0
  126. AND topic_last_post_time $sql_condition {$row['topic_last_post_time']}
  127. AND " . $phpbb_content_visibility->get_visibility_sql('topic', $row['forum_id']) . "
  128. ORDER BY topic_last_post_time $sql_ordering, topic_last_post_id $sql_ordering";
  129. $result = $db->sql_query_limit($sql, 1);
  130. $row = $db->sql_fetchrow($result);
  131. $db->sql_freeresult($result);
  132. if (!$row)
  133. {
  134. $sql = 'SELECT forum_style
  135. FROM ' . FORUMS_TABLE . "
  136. WHERE forum_id = $forum_id";
  137. $result = $db->sql_query($sql);
  138. $forum_style = (int) $db->sql_fetchfield('forum_style');
  139. $db->sql_freeresult($result);
  140. $user->setup('viewtopic', $forum_style);
  141. trigger_error(($view == 'next') ? 'NO_NEWER_TOPICS' : 'NO_OLDER_TOPICS');
  142. }
  143. else
  144. {
  145. $topic_id = $row['topic_id'];
  146. $forum_id = $row['forum_id'];
  147. }
  148. }
  149. }
  150. if (isset($row) && $row['forum_id'])
  151. {
  152. $forum_id = $row['forum_id'];
  153. }
  154. }
  155. // This rather complex gaggle of code handles querying for topics but
  156. // also allows for direct linking to a post (and the calculation of which
  157. // page the post is on and the correct display of viewtopic)
  158. $sql_array = array(
  159. 'SELECT' => 't.*, f.*',
  160. 'FROM' => array(FORUMS_TABLE => 'f'),
  161. );
  162. // The FROM-Order is quite important here, else t.* columns can not be correctly bound.
  163. if ($post_id)
  164. {
  165. $sql_array['SELECT'] .= ', p.post_visibility, p.post_time, p.post_id';
  166. $sql_array['FROM'][POSTS_TABLE] = 'p';
  167. }
  168. // Topics table need to be the last in the chain
  169. $sql_array['FROM'][TOPICS_TABLE] = 't';
  170. if ($user->data['is_registered'])
  171. {
  172. $sql_array['SELECT'] .= ', tw.notify_status';
  173. $sql_array['LEFT_JOIN'] = array();
  174. $sql_array['LEFT_JOIN'][] = array(
  175. 'FROM' => array(TOPICS_WATCH_TABLE => 'tw'),
  176. 'ON' => 'tw.user_id = ' . $user->data['user_id'] . ' AND t.topic_id = tw.topic_id'
  177. );
  178. if ($config['allow_bookmarks'])
  179. {
  180. $sql_array['SELECT'] .= ', bm.topic_id as bookmarked';
  181. $sql_array['LEFT_JOIN'][] = array(
  182. 'FROM' => array(BOOKMARKS_TABLE => 'bm'),
  183. 'ON' => 'bm.user_id = ' . $user->data['user_id'] . ' AND t.topic_id = bm.topic_id'
  184. );
  185. }
  186. if ($config['load_db_lastread'])
  187. {
  188. $sql_array['SELECT'] .= ', tt.mark_time, ft.mark_time as forum_mark_time';
  189. $sql_array['LEFT_JOIN'][] = array(
  190. 'FROM' => array(TOPICS_TRACK_TABLE => 'tt'),
  191. 'ON' => 'tt.user_id = ' . $user->data['user_id'] . ' AND t.topic_id = tt.topic_id'
  192. );
  193. $sql_array['LEFT_JOIN'][] = array(
  194. 'FROM' => array(FORUMS_TRACK_TABLE => 'ft'),
  195. 'ON' => 'ft.user_id = ' . $user->data['user_id'] . ' AND t.forum_id = ft.forum_id'
  196. );
  197. }
  198. }
  199. if (!$post_id)
  200. {
  201. $sql_array['WHERE'] = "t.topic_id = $topic_id";
  202. }
  203. else
  204. {
  205. $sql_array['WHERE'] = "p.post_id = $post_id AND t.topic_id = p.topic_id";
  206. }
  207. $sql_array['WHERE'] .= ' AND f.forum_id = t.forum_id';
  208. $sql = $db->sql_build_query('SELECT', $sql_array);
  209. $result = $db->sql_query($sql);
  210. $topic_data = $db->sql_fetchrow($result);
  211. $db->sql_freeresult($result);
  212. // link to unapproved post or incorrect link
  213. if (!$topic_data)
  214. {
  215. // If post_id was submitted, we try at least to display the topic as a last resort...
  216. if ($post_id && $topic_id)
  217. {
  218. redirect(append_sid("{$phpbb_root_path}viewtopic.$phpEx", "t=$topic_id" . (($forum_id) ? "&amp;f=$forum_id" : '')));
  219. }
  220. trigger_error('NO_TOPIC');
  221. }
  222. $forum_id = (int) $topic_data['forum_id'];
  223. /**
  224. * Modify the forum ID to handle the correct display of viewtopic if needed
  225. *
  226. * @event core.viewtopic_modify_forum_id
  227. * @var string forum_id forum ID
  228. * @var array topic_data array of topic's data
  229. * @since 3.2.5-RC1
  230. */
  231. $vars = array(
  232. 'forum_id',
  233. 'topic_data',
  234. );
  235. extract($phpbb_dispatcher->trigger_event('core.viewtopic_modify_forum_id', compact($vars)));
  236. // If the request is missing the f parameter, the forum id in the user session data is 0 at the moment.
  237. // Let's fix that now so that the user can't hide from the forum's Who Is Online list.
  238. $user->page['forum'] = $forum_id;
  239. // Now we know the forum_id and can check the permissions
  240. if (!$phpbb_content_visibility->is_visible('topic', $forum_id, $topic_data))
  241. {
  242. trigger_error('NO_TOPIC');
  243. }
  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_visibility'] == ITEM_UNAPPROVED || $topic_data['post_visibility'] == ITEM_REAPPROVE) && !$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'] = $phpbb_content_visibility->get_count('topic_posts', $topic_data, $forum_id) - 1;
  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. AND " . $phpbb_content_visibility->get_visibility_sql('post', $forum_id, 'p.');
  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. $topic_replies = $phpbb_content_visibility->get_count('topic_posts', $topic_data, $forum_id) - 1;
  291. // Check sticky/announcement/global time limit
  292. if (($topic_data['topic_type'] != POST_NORMAL) && $topic_data['topic_time_limit'] && ($topic_data['topic_time'] + $topic_data['topic_time_limit']) < time())
  293. {
  294. $sql = 'UPDATE ' . TOPICS_TABLE . '
  295. SET topic_type = ' . POST_NORMAL . ', topic_time_limit = 0
  296. WHERE topic_id = ' . $topic_id;
  297. $db->sql_query($sql);
  298. $topic_data['topic_type'] = POST_NORMAL;
  299. $topic_data['topic_time_limit'] = 0;
  300. }
  301. // Setup look and feel
  302. $user->setup('viewtopic', $topic_data['forum_style']);
  303. if ($view == 'print' && !$auth->acl_get('f_print', $forum_id))
  304. {
  305. send_status_line(403, 'Forbidden');
  306. trigger_error('NO_AUTH_PRINT_TOPIC');
  307. }
  308. $overrides_f_read_check = false;
  309. $overrides_forum_password_check = false;
  310. $topic_tracking_info = isset($topic_tracking_info) ? $topic_tracking_info : null;
  311. /**
  312. * Event to apply extra permissions and to override original phpBB's f_read permission and forum password check
  313. * on viewtopic access
  314. *
  315. * @event core.viewtopic_before_f_read_check
  316. * @var int forum_id The forum id from where the topic belongs
  317. * @var int topic_id The id of the topic the user tries to access
  318. * @var int post_id The id of the post the user tries to start viewing at.
  319. * It may be 0 for none given.
  320. * @var array topic_data All the information from the topic and forum tables for this topic
  321. * It includes posts information if post_id is not 0
  322. * @var bool overrides_f_read_check Set true to remove f_read check afterwards
  323. * @var bool overrides_forum_password_check Set true to remove forum_password check afterwards
  324. * @var array topic_tracking_info Information upon calling get_topic_tracking()
  325. * Set it to NULL to allow auto-filling later.
  326. * Set it to an array to override original data.
  327. * @since 3.1.3-RC1
  328. */
  329. $vars = array(
  330. 'forum_id',
  331. 'topic_id',
  332. 'post_id',
  333. 'topic_data',
  334. 'overrides_f_read_check',
  335. 'overrides_forum_password_check',
  336. 'topic_tracking_info',
  337. );
  338. extract($phpbb_dispatcher->trigger_event('core.viewtopic_before_f_read_check', compact($vars)));
  339. // Start auth check
  340. if (!$overrides_f_read_check && !$auth->acl_get('f_read', $forum_id))
  341. {
  342. if ($user->data['user_id'] != ANONYMOUS)
  343. {
  344. send_status_line(403, 'Forbidden');
  345. trigger_error('SORRY_AUTH_READ');
  346. }
  347. login_box('', $user->lang['LOGIN_VIEWFORUM']);
  348. }
  349. // Forum is passworded ... check whether access has been granted to this
  350. // user this session, if not show login box
  351. if (!$overrides_forum_password_check && $topic_data['forum_password'])
  352. {
  353. login_forum_box($topic_data);
  354. }
  355. // Redirect to login upon emailed notification links if user is not logged in.
  356. if (isset($_GET['e']) && $user->data['user_id'] == ANONYMOUS)
  357. {
  358. login_box(build_url('e') . '#unread', $user->lang['LOGIN_NOTIFY_TOPIC']);
  359. }
  360. // What is start equal to?
  361. if ($post_id)
  362. {
  363. $start = floor(($topic_data['prev_posts']) / $config['posts_per_page']) * $config['posts_per_page'];
  364. }
  365. // Get topic tracking info
  366. if (!isset($topic_tracking_info))
  367. {
  368. $topic_tracking_info = array();
  369. // Get topic tracking info
  370. if ($config['load_db_lastread'] && $user->data['is_registered'])
  371. {
  372. $tmp_topic_data = array($topic_id => $topic_data);
  373. $topic_tracking_info = get_topic_tracking($forum_id, $topic_id, $tmp_topic_data, array($forum_id => $topic_data['forum_mark_time']));
  374. unset($tmp_topic_data);
  375. }
  376. else if ($config['load_anon_lastread'] || $user->data['is_registered'])
  377. {
  378. $topic_tracking_info = get_complete_topic_tracking($forum_id, $topic_id);
  379. }
  380. }
  381. // Post ordering options
  382. $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']);
  383. $sort_by_text = array('a' => $user->lang['AUTHOR'], 't' => $user->lang['POST_TIME'], 's' => $user->lang['SUBJECT']);
  384. $sort_by_sql = array('a' => array('u.username_clean', 'p.post_id'), 't' => array('p.post_time', 'p.post_id'), 's' => array('p.post_subject', 'p.post_id'));
  385. $join_user_sql = array('a' => true, 't' => false, 's' => false);
  386. $s_limit_days = $s_sort_key = $s_sort_dir = $u_sort_param = '';
  387. /**
  388. * Event to add new sorting options
  389. *
  390. * @event core.viewtopic_gen_sort_selects_before
  391. * @var array limit_days Limit results by time
  392. * @var array sort_by_text Language strings for sorting options
  393. * @var array sort_by_sql SQL conditions for sorting options
  394. * @var array join_user_sql SQL joins required for sorting options
  395. * @var int sort_days User selected sort days
  396. * @var string sort_key User selected sort key
  397. * @var string sort_dir User selected sort direction
  398. * @var string s_limit_days Initial value of limit days selectbox
  399. * @var string s_sort_key Initial value of sort key selectbox
  400. * @var string s_sort_dir Initial value of sort direction selectbox
  401. * @var string u_sort_param Initial value of sorting form action
  402. * @since 3.2.8-RC1
  403. */
  404. $vars = array(
  405. 'limit_days',
  406. 'sort_by_text',
  407. 'sort_by_sql',
  408. 'join_user_sql',
  409. 'sort_days',
  410. 'sort_key',
  411. 'sort_dir',
  412. 's_limit_days',
  413. 's_sort_key',
  414. 's_sort_dir',
  415. 'u_sort_param',
  416. );
  417. extract($phpbb_dispatcher->trigger_event('core.viewtopic_gen_sort_selects_before', compact($vars)));
  418. 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);
  419. // Obtain correct post count and ordering SQL if user has
  420. // requested anything different
  421. if ($sort_days)
  422. {
  423. $min_post_time = time() - ($sort_days * 86400);
  424. $sql = 'SELECT COUNT(post_id) AS num_posts
  425. FROM ' . POSTS_TABLE . "
  426. WHERE topic_id = $topic_id
  427. AND post_time >= $min_post_time
  428. AND " . $phpbb_content_visibility->get_visibility_sql('post', $forum_id);
  429. $result = $db->sql_query($sql);
  430. $total_posts = (int) $db->sql_fetchfield('num_posts');
  431. $db->sql_freeresult($result);
  432. $limit_posts_time = "AND p.post_time >= $min_post_time ";
  433. if (isset($_POST['sort']))
  434. {
  435. $start = 0;
  436. }
  437. }
  438. else
  439. {
  440. $total_posts = $topic_replies + 1;
  441. $limit_posts_time = '';
  442. }
  443. // Was a highlight request part of the URI?
  444. $highlight_match = $highlight = '';
  445. if ($hilit_words)
  446. {
  447. $highlight_match = phpbb_clean_search_string($hilit_words);
  448. $highlight = urlencode($highlight_match);
  449. $highlight_match = str_replace('\*', '\w+?', preg_quote($highlight_match, '#'));
  450. $highlight_match = preg_replace('#(?<=^|\s)\\\\w\*\?(?=\s|$)#', '\w+?', $highlight_match);
  451. $highlight_match = str_replace(' ', '|', $highlight_match);
  452. }
  453. // Make sure $start is set to the last page if it exceeds the amount
  454. $start = $pagination->validate_start($start, $config['posts_per_page'], $total_posts);
  455. // General Viewtopic URL for return links
  456. $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" : ''));
  457. // Are we watching this topic?
  458. $s_watching_topic = array(
  459. 'link' => '',
  460. 'link_toggle' => '',
  461. 'title' => '',
  462. 'title_toggle' => '',
  463. 'is_watching' => false,
  464. );
  465. if ($config['allow_topic_notify'])
  466. {
  467. $notify_status = (isset($topic_data['notify_status'])) ? $topic_data['notify_status'] : null;
  468. watch_topic_forum('topic', $s_watching_topic, $user->data['user_id'], $forum_id, $topic_id, $notify_status, $start, $topic_data['topic_title']);
  469. // Reset forum notification if forum notify is set
  470. if ($config['allow_forum_notify'] && $auth->acl_get('f_subscribe', $forum_id))
  471. {
  472. $s_watching_forum = $s_watching_topic;
  473. watch_topic_forum('forum', $s_watching_forum, $user->data['user_id'], $forum_id, 0);
  474. }
  475. }
  476. /**
  477. * Event to modify highlight.
  478. *
  479. * @event core.viewtopic_highlight_modify
  480. * @var string highlight String to be highlighted
  481. * @var string highlight_match Highlight string to be used in preg_replace
  482. * @var array topic_data Topic data
  483. * @var int start Pagination start
  484. * @var int total_posts Number of posts
  485. * @var string viewtopic_url Current viewtopic URL
  486. * @since 3.1.11-RC1
  487. */
  488. $vars = array(
  489. 'highlight',
  490. 'highlight_match',
  491. 'topic_data',
  492. 'start',
  493. 'total_posts',
  494. 'viewtopic_url',
  495. );
  496. extract($phpbb_dispatcher->trigger_event('core.viewtopic_highlight_modify', compact($vars)));
  497. // Bookmarks
  498. if ($config['allow_bookmarks'] && $user->data['is_registered'] && $request->variable('bookmark', 0))
  499. {
  500. if (check_link_hash($request->variable('hash', ''), "topic_$topic_id"))
  501. {
  502. if (!$topic_data['bookmarked'])
  503. {
  504. $sql = 'INSERT INTO ' . BOOKMARKS_TABLE . ' ' . $db->sql_build_array('INSERT', array(
  505. 'user_id' => $user->data['user_id'],
  506. 'topic_id' => $topic_id,
  507. ));
  508. $db->sql_query($sql);
  509. }
  510. else
  511. {
  512. $sql = 'DELETE FROM ' . BOOKMARKS_TABLE . "
  513. WHERE user_id = {$user->data['user_id']}
  514. AND topic_id = $topic_id";
  515. $db->sql_query($sql);
  516. }
  517. $message = (($topic_data['bookmarked']) ? $user->lang['BOOKMARK_REMOVED'] : $user->lang['BOOKMARK_ADDED']);
  518. if (!$request->is_ajax())
  519. {
  520. $message .= '<br /><br />' . $user->lang('RETURN_TOPIC', '<a href="' . $viewtopic_url . '">', '</a>');
  521. }
  522. }
  523. else
  524. {
  525. $message = $user->lang['BOOKMARK_ERR'];
  526. if (!$request->is_ajax())
  527. {
  528. $message .= '<br /><br />' . $user->lang('RETURN_TOPIC', '<a href="' . $viewtopic_url . '">', '</a>');
  529. }
  530. }
  531. meta_refresh(3, $viewtopic_url);
  532. trigger_error($message);
  533. }
  534. // Grab ranks
  535. $ranks = $cache->obtain_ranks();
  536. // Grab icons
  537. $icons = $cache->obtain_icons();
  538. // Grab extensions
  539. $extensions = array();
  540. if ($topic_data['topic_attachment'])
  541. {
  542. $extensions = $cache->obtain_attach_extensions($forum_id);
  543. }
  544. // Forum rules listing
  545. $s_forum_rules = '';
  546. gen_forum_auth_level('topic', $forum_id, $topic_data['forum_status']);
  547. // Quick mod tools
  548. $allow_change_type = ($auth->acl_get('m_', $forum_id) || ($user->data['is_registered'] && $user->data['user_id'] == $topic_data['topic_poster'])) ? true : false;
  549. $s_quickmod_action = append_sid(
  550. "{$phpbb_root_path}mcp.$phpEx",
  551. array(
  552. 'f' => $forum_id,
  553. 't' => $topic_id,
  554. 'start' => $start,
  555. 'quickmod' => 1,
  556. 'redirect' => urlencode(str_replace('&amp;', '&', $viewtopic_url)),
  557. ),
  558. true,
  559. $user->session_id
  560. );
  561. $quickmod_array = array(
  562. // 'key' => array('LANG_KEY', $userHasPermissions),
  563. 'lock' => array('LOCK_TOPIC', ($topic_data['topic_status'] == ITEM_UNLOCKED) && ($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']))),
  564. 'unlock' => array('UNLOCK_TOPIC', ($topic_data['topic_status'] != ITEM_UNLOCKED) && ($auth->acl_get('m_lock', $forum_id))),
  565. 'delete_topic' => array('DELETE_TOPIC', ($auth->acl_get('m_delete', $forum_id) || (($topic_data['topic_visibility'] != ITEM_DELETED) && $auth->acl_get('m_softdelete', $forum_id)))),
  566. 'restore_topic' => array('RESTORE_TOPIC', (($topic_data['topic_visibility'] == ITEM_DELETED) && $auth->acl_get('m_approve', $forum_id))),
  567. 'move' => array('MOVE_TOPIC', $auth->acl_get('m_move', $forum_id) && $topic_data['topic_status'] != ITEM_MOVED),
  568. 'split' => array('SPLIT_TOPIC', $auth->acl_get('m_split', $forum_id)),
  569. 'merge' => array('MERGE_POSTS', $auth->acl_get('m_merge', $forum_id)),
  570. 'merge_topic' => array('MERGE_TOPIC', $auth->acl_get('m_merge', $forum_id)),
  571. 'fork' => array('FORK_TOPIC', $auth->acl_get('m_move', $forum_id)),
  572. 'make_normal' => array('MAKE_NORMAL', ($allow_change_type && $auth->acl_gets('f_sticky', 'f_announce', 'f_announce_global', $forum_id) && $topic_data['topic_type'] != POST_NORMAL)),
  573. 'make_sticky' => array('MAKE_STICKY', ($allow_change_type && $auth->acl_get('f_sticky', $forum_id) && $topic_data['topic_type'] != POST_STICKY)),
  574. 'make_announce' => array('MAKE_ANNOUNCE', ($allow_change_type && $auth->acl_get('f_announce', $forum_id) && $topic_data['topic_type'] != POST_ANNOUNCE)),
  575. 'make_global' => array('MAKE_GLOBAL', ($allow_change_type && $auth->acl_get('f_announce_global', $forum_id) && $topic_data['topic_type'] != POST_GLOBAL)),
  576. 'topic_logs' => array('VIEW_TOPIC_LOGS', $auth->acl_get('m_', $forum_id)),
  577. );
  578. /**
  579. * Event to modify data in the quickmod_array before it gets sent to the
  580. * phpbb_add_quickmod_option function.
  581. *
  582. * @event core.viewtopic_add_quickmod_option_before
  583. * @var int forum_id Forum ID
  584. * @var int post_id Post ID
  585. * @var array quickmod_array Array with quick moderation options data
  586. * @var array topic_data Array with topic data
  587. * @var int topic_id Topic ID
  588. * @var array topic_tracking_info Array with topic tracking data
  589. * @var string viewtopic_url URL to the topic page
  590. * @var bool allow_change_type Topic change permissions check
  591. * @since 3.1.9-RC1
  592. */
  593. $vars = array(
  594. 'forum_id',
  595. 'post_id',
  596. 'quickmod_array',
  597. 'topic_data',
  598. 'topic_id',
  599. 'topic_tracking_info',
  600. 'viewtopic_url',
  601. 'allow_change_type',
  602. );
  603. extract($phpbb_dispatcher->trigger_event('core.viewtopic_add_quickmod_option_before', compact($vars)));
  604. foreach ($quickmod_array as $option => $qm_ary)
  605. {
  606. if (!empty($qm_ary[1]))
  607. {
  608. phpbb_add_quickmod_option($s_quickmod_action, $option, $qm_ary[0]);
  609. }
  610. }
  611. // Navigation links
  612. generate_forum_nav($topic_data);
  613. // Forum Rules
  614. generate_forum_rules($topic_data);
  615. // Moderators
  616. $forum_moderators = array();
  617. if ($config['load_moderators'])
  618. {
  619. get_moderators($forum_moderators, $forum_id);
  620. }
  621. // This is only used for print view so ...
  622. $server_path = (!$view) ? $phpbb_root_path : generate_board_url() . '/';
  623. // Replace naughty words in title
  624. $topic_data['topic_title'] = censor_text($topic_data['topic_title']);
  625. $s_search_hidden_fields = array(
  626. 't' => $topic_id,
  627. 'sf' => 'msgonly',
  628. );
  629. if ($_SID)
  630. {
  631. $s_search_hidden_fields['sid'] = $_SID;
  632. }
  633. if (!empty($_EXTRA_URL))
  634. {
  635. foreach ($_EXTRA_URL as $url_param)
  636. {
  637. $url_param = explode('=', $url_param, 2);
  638. $s_search_hidden_fields[$url_param[0]] = $url_param[1];
  639. }
  640. }
  641. // If we've got a hightlight set pass it on to pagination.
  642. $base_url = 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" : ''));
  643. /**
  644. * Event to modify data before template variables are being assigned
  645. *
  646. * @event core.viewtopic_assign_template_vars_before
  647. * @var string base_url URL to be passed to generate pagination
  648. * @var int forum_id Forum ID
  649. * @var int post_id Post ID
  650. * @var array quickmod_array Array with quick moderation options data
  651. * @var int start Pagination information
  652. * @var array topic_data Array with topic data
  653. * @var int topic_id Topic ID
  654. * @var array topic_tracking_info Array with topic tracking data
  655. * @var int total_posts Topic total posts count
  656. * @var string viewtopic_url URL to the topic page
  657. * @since 3.1.0-RC4
  658. * @changed 3.1.2-RC1 Added viewtopic_url
  659. */
  660. $vars = array(
  661. 'base_url',
  662. 'forum_id',
  663. 'post_id',
  664. 'quickmod_array',
  665. 'start',
  666. 'topic_data',
  667. 'topic_id',
  668. 'topic_tracking_info',
  669. 'total_posts',
  670. 'viewtopic_url',
  671. );
  672. extract($phpbb_dispatcher->trigger_event('core.viewtopic_assign_template_vars_before', compact($vars)));
  673. $pagination->generate_template_pagination($base_url, 'pagination', 'start', $total_posts, $config['posts_per_page'], $start);
  674. // Send vars to template
  675. $template->assign_vars(array(
  676. 'FORUM_ID' => $forum_id,
  677. 'FORUM_NAME' => $topic_data['forum_name'],
  678. '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']),
  679. 'TOPIC_ID' => $topic_id,
  680. 'TOPIC_TITLE' => $topic_data['topic_title'],
  681. 'TOPIC_POSTER' => $topic_data['topic_poster'],
  682. 'TOPIC_AUTHOR_FULL' => get_username_string('full', $topic_data['topic_poster'], $topic_data['topic_first_poster_name'], $topic_data['topic_first_poster_colour']),
  683. 'TOPIC_AUTHOR_COLOUR' => get_username_string('colour', $topic_data['topic_poster'], $topic_data['topic_first_poster_name'], $topic_data['topic_first_poster_colour']),
  684. 'TOPIC_AUTHOR' => get_username_string('username', $topic_data['topic_poster'], $topic_data['topic_first_poster_name'], $topic_data['topic_first_poster_colour']),
  685. 'TOTAL_POSTS' => $user->lang('VIEW_TOPIC_POSTS', (int) $total_posts),
  686. '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) : '',
  687. 'MODERATORS' => (isset($forum_moderators[$forum_id]) && count($forum_moderators[$forum_id])) ? implode($user->lang['COMMA_SEPARATOR'], $forum_moderators[$forum_id]) : '',
  688. 'POST_IMG' => ($topic_data['forum_status'] == ITEM_LOCKED) ? $user->img('button_topic_locked', 'FORUM_LOCKED') : $user->img('button_topic_new', 'POST_NEW_TOPIC'),
  689. 'QUOTE_IMG' => $user->img('icon_post_quote', 'REPLY_WITH_QUOTE'),
  690. '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'),
  691. 'EDIT_IMG' => $user->img('icon_post_edit', 'EDIT_POST'),
  692. 'DELETE_IMG' => $user->img('icon_post_delete', 'DELETE_POST'),
  693. 'DELETED_IMG' => $user->img('icon_topic_deleted', 'POST_DELETED_RESTORE'),
  694. 'INFO_IMG' => $user->img('icon_post_info', 'VIEW_INFO'),
  695. 'PROFILE_IMG' => $user->img('icon_user_profile', 'READ_PROFILE'),
  696. 'SEARCH_IMG' => $user->img('icon_user_search', 'SEARCH_USER_POSTS'),
  697. 'PM_IMG' => $user->img('icon_contact_pm', 'SEND_PRIVATE_MESSAGE'),
  698. 'EMAIL_IMG' => $user->img('icon_contact_email', 'SEND_EMAIL'),
  699. 'JABBER_IMG' => $user->img('icon_contact_jabber', 'JABBER') ,
  700. 'REPORT_IMG' => $user->img('icon_post_report', 'REPORT_POST'),
  701. 'REPORTED_IMG' => $user->img('icon_topic_reported', 'POST_REPORTED'),
  702. 'UNAPPROVED_IMG' => $user->img('icon_topic_unapproved', 'POST_UNAPPROVED'),
  703. 'WARN_IMG' => $user->img('icon_user_warn', 'WARN_USER'),
  704. 'S_IS_LOCKED' => ($topic_data['topic_status'] == ITEM_UNLOCKED && $topic_data['forum_status'] == ITEM_UNLOCKED) ? false : true,
  705. 'S_SELECT_SORT_DIR' => $s_sort_dir,
  706. 'S_SELECT_SORT_KEY' => $s_sort_key,
  707. 'S_SELECT_SORT_DAYS' => $s_limit_days,
  708. 'S_SINGLE_MODERATOR' => (!empty($forum_moderators[$forum_id]) && count($forum_moderators[$forum_id]) > 1) ? false : true,
  709. 'S_TOPIC_ACTION' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id" . (($start == 0) ? '' : "&amp;start=$start")),
  710. 'S_MOD_ACTION' => $s_quickmod_action,
  711. 'L_RETURN_TO_FORUM' => $user->lang('RETURN_TO', $topic_data['forum_name']),
  712. 'S_VIEWTOPIC' => true,
  713. 'S_UNREAD_VIEW' => $view == 'unread',
  714. 'S_DISPLAY_SEARCHBOX' => ($auth->acl_get('u_search') && $auth->acl_get('f_search', $forum_id) && $config['load_search']) ? true : false,
  715. 'S_SEARCHBOX_ACTION' => append_sid("{$phpbb_root_path}search.$phpEx"),
  716. 'S_SEARCH_LOCAL_HIDDEN_FIELDS' => build_hidden_fields($s_search_hidden_fields),
  717. 'S_DISPLAY_POST_INFO' => ($topic_data['forum_type'] == FORUM_POST && ($auth->acl_get('f_post', $forum_id) || $user->data['user_id'] == ANONYMOUS)) ? true : false,
  718. 'S_DISPLAY_REPLY_INFO' => ($topic_data['forum_type'] == FORUM_POST && ($auth->acl_get('f_reply', $forum_id) || $user->data['user_id'] == ANONYMOUS)) ? true : false,
  719. 'S_ENABLE_FEEDS_TOPIC' => ($config['feed_topic'] && !phpbb_optionget(FORUM_OPTION_FEED_EXCLUDE, $topic_data['forum_options'])) ? true : false,
  720. 'U_TOPIC' => "{$server_path}viewtopic.$phpEx?f=$forum_id&amp;t=$topic_id",
  721. 'U_FORUM' => $server_path,
  722. 'U_VIEW_TOPIC' => 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" : '')),
  723. 'U_CANONICAL' => generate_board_url() . '/' . append_sid("viewtopic.$phpEx", "t=$topic_id" . (($start) ? "&amp;start=$start" : ''), true, ''),
  724. 'U_VIEW_FORUM' => append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_id),
  725. 'U_VIEW_OLDER_TOPIC' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;view=previous"),
  726. 'U_VIEW_NEWER_TOPIC' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;view=next"),
  727. 'U_PRINT_TOPIC' => ($auth->acl_get('f_print', $forum_id)) ? $viewtopic_url . '&amp;view=print' : '',
  728. '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") : '',
  729. 'U_WATCH_TOPIC' => $s_watching_topic['link'],
  730. 'U_WATCH_TOPIC_TOGGLE' => $s_watching_topic['link_toggle'],
  731. 'S_WATCH_TOPIC_TITLE' => $s_watching_topic['title'],
  732. 'S_WATCH_TOPIC_TOGGLE' => $s_watching_topic['title_toggle'],
  733. 'S_WATCHING_TOPIC' => $s_watching_topic['is_watching'],
  734. 'U_BOOKMARK_TOPIC' => ($user->data['is_registered'] && $config['allow_bookmarks']) ? $viewtopic_url . '&amp;bookmark=1&amp;hash=' . generate_link_hash("topic_$topic_id") : '',
  735. 'S_BOOKMARK_TOPIC' => ($user->data['is_registered'] && $config['allow_bookmarks'] && $topic_data['bookmarked']) ? $user->lang['BOOKMARK_TOPIC_REMOVE'] : $user->lang['BOOKMARK_TOPIC'],
  736. 'S_BOOKMARK_TOGGLE' => (!$user->data['is_registered'] || !$config['allow_bookmarks'] || !$topic_data['bookmarked']) ? $user->lang['BOOKMARK_TOPIC_REMOVE'] : $user->lang['BOOKMARK_TOPIC'],
  737. 'S_BOOKMARKED_TOPIC' => ($user->data['is_registered'] && $config['allow_bookmarks'] && $topic_data['bookmarked']) ? true : false,
  738. '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") : '',
  739. '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") : '',
  740. '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")) : '')
  741. );
  742. // Does this topic contain a poll?
  743. if (!empty($topic_data['poll_start']))
  744. {
  745. $sql = 'SELECT o.*, p.bbcode_bitfield, p.bbcode_uid
  746. FROM ' . POLL_OPTIONS_TABLE . ' o, ' . POSTS_TABLE . " p
  747. WHERE o.topic_id = $topic_id
  748. AND p.post_id = {$topic_data['topic_first_post_id']}
  749. AND p.topic_id = o.topic_id
  750. ORDER BY o.poll_option_id";
  751. $result = $db->sql_query($sql);
  752. $poll_info = $vote_counts = array();
  753. while ($row = $db->sql_fetchrow($result))
  754. {
  755. $poll_info[] = $row;
  756. $option_id = (int) $row['poll_option_id'];
  757. $vote_counts[$option_id] = (int) $row['poll_option_total'];
  758. }
  759. $db->sql_freeresult($result);
  760. $cur_voted_id = array();
  761. if ($user->data['is_registered'])
  762. {
  763. $sql = 'SELECT poll_option_id
  764. FROM ' . POLL_VOTES_TABLE . '
  765. WHERE topic_id = ' . $topic_id . '
  766. AND vote_user_id = ' . $user->data['user_id'];
  767. $result = $db->sql_query($sql);
  768. while ($row = $db->sql_fetchrow($result))
  769. {
  770. $cur_voted_id[] = $row['poll_option_id'];
  771. }
  772. $db->sql_freeresult($result);
  773. }
  774. else
  775. {
  776. // Cookie based guest tracking ... I don't like this but hum ho
  777. // it's oft requested. This relies on "nice" users who don't feel
  778. // the need to delete cookies to mess with results.
  779. if ($request->is_set($config['cookie_name'] . '_poll_' . $topic_id, \phpbb\request\request_interface::COOKIE))
  780. {
  781. $cur_voted_id = explode(',', $request->variable($config['cookie_name'] . '_poll_' . $topic_id, '', true, \phpbb\request\request_interface::COOKIE));
  782. $cur_voted_id = array_map('intval', $cur_voted_id);
  783. }
  784. }
  785. // Can not vote at all if no vote permission
  786. $s_can_vote = ($auth->acl_get('f_vote', $forum_id) &&
  787. (($topic_data['poll_length'] != 0 && $topic_data['poll_start'] + $topic_data['poll_length'] > time()) || $topic_data['poll_length'] == 0) &&
  788. $topic_data['topic_status'] != ITEM_LOCKED &&
  789. $topic_data['forum_status'] != ITEM_LOCKED &&
  790. (!count($cur_voted_id) ||
  791. ($auth->acl_get('f_votechg', $forum_id) && $topic_data['poll_vote_change']))) ? true : false;
  792. $s_display_results = (!$s_can_vote || ($s_can_vote && count($cur_voted_id)) || $view == 'viewpoll') ? true : false;
  793. /**
  794. * Event to manipulate the poll data
  795. *
  796. * @event core.viewtopic_modify_poll_data
  797. * @var array cur_voted_id Array with options' IDs current user has voted for
  798. * @var int forum_id The topic's forum id
  799. * @var array poll_info Array with the poll information
  800. * @var bool s_can_vote Flag indicating if a user can vote
  801. * @var bool s_display_results Flag indicating if results or poll options should be displayed
  802. * @var int topic_id The id of the topic the user tries to access
  803. * @var array topic_data All the information from the topic and forum tables for this topic
  804. * @var string viewtopic_url URL to the topic page
  805. * @var array vote_counts Array with the vote counts for every poll option
  806. * @var array voted_id Array with updated options' IDs current user is voting for
  807. * @since 3.1.5-RC1
  808. */
  809. $vars = array(
  810. 'cur_voted_id',
  811. 'forum_id',
  812. 'poll_info',
  813. 's_can_vote',
  814. 's_display_results',
  815. 'topic_id',
  816. 'topic_data',
  817. 'viewtopic_url',
  818. 'vote_counts',
  819. 'voted_id',
  820. );
  821. extract($phpbb_dispatcher->trigger_event('core.viewtopic_modify_poll_data', compact($vars)));
  822. if ($update && $s_can_vote)
  823. {
  824. if (!count($voted_id) || count($voted_id) > $topic_data['poll_max_options'] || in_array(VOTE_CONVERTED, $cur_voted_id) || !check_form_key('posting'))
  825. {
  826. $redirect_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id" . (($start == 0) ? '' : "&amp;start=$start"));
  827. meta_refresh(5, $redirect_url);
  828. if (!count($voted_id))
  829. {
  830. $message = 'NO_VOTE_OPTION';
  831. }
  832. else if (count($voted_id) > $topic_data['poll_max_options'])
  833. {
  834. $message = 'TOO_MANY_VOTE_OPTIONS';
  835. }
  836. else if (in_array(VOTE_CONVERTED, $cur_voted_id))
  837. {
  838. $message = 'VOTE_CONVERTED';
  839. }
  840. else
  841. {
  842. $message = 'FORM_INVALID';
  843. }
  844. $message = $user->lang[$message] . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $redirect_url . '">', '</a>');
  845. trigger_error($message);
  846. }
  847. foreach ($voted_id as $option)
  848. {
  849. if (in_array($option, $cur_voted_id))
  850. {
  851. continue;
  852. }
  853. $sql = 'UPDATE ' . POLL_OPTIONS_TABLE . '
  854. SET poll_option_total = poll_option_total + 1
  855. WHERE poll_option_id = ' . (int) $option . '
  856. AND topic_id = ' . (int) $topic_id;
  857. $db->sql_query($sql);
  858. $vote_counts[$option]++;
  859. if ($user->data['is_registered'])
  860. {
  861. $sql_ary = array(
  862. 'topic_id' => (int) $topic_id,
  863. 'poll_option_id' => (int) $option,
  864. 'vote_user_id' => (int) $user->data['user_id'],
  865. 'vote_user_ip' => (string) $user->ip,
  866. );
  867. $sql = 'INSERT INTO ' . POLL_VOTES_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
  868. $db->sql_query($sql);
  869. }
  870. }
  871. foreach ($cur_voted_id as $option)
  872. {
  873. if (!in_array($option, $voted_id))
  874. {
  875. $sql = 'UPDATE ' . POLL_OPTIONS_TABLE . '
  876. SET poll_option_total = poll_option_total - 1
  877. WHERE poll_option_id = ' . (int) $option . '
  878. AND topic_id = ' . (int) $topic_id;
  879. $db->sql_query($sql);
  880. $vote_counts[$option]--;
  881. if ($user->data['is_registered'])
  882. {
  883. $sql = 'DELETE FROM ' . POLL_VOTES_TABLE . '
  884. WHERE topic_id = ' . (int) $topic_id . '
  885. AND poll_option_id = ' . (int) $option . '
  886. AND vote_user_id = ' . (int) $user->data['user_id'];
  887. $db->sql_query($sql);
  888. }
  889. }
  890. }
  891. if ($user->data['user_id'] == ANONYMOUS && !$user->data['is_bot'])
  892. {
  893. $user->set_cookie('poll_' . $topic_id, implode(',', $voted_id), time() + 31536000);
  894. }
  895. $sql = 'UPDATE ' . TOPICS_TABLE . '
  896. SET poll_last_vote = ' . time() . "
  897. WHERE topic_id = $topic_id";
  898. //, topic_last_post_time = ' . time() . " -- for bumping topics with new votes, ignore for now
  899. $db->sql_query($sql);
  900. $redirect_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id" . (($start == 0) ? '' : "&amp;start=$start"));
  901. $message = $user->lang['VOTE_SUBMITTED'] . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $redirect_url . '">', '</a>');
  902. if ($request->is_ajax())
  903. {
  904. // Filter out invalid options
  905. $valid_user_votes = array_intersect(array_keys($vote_counts), $voted_id);
  906. $data = array(
  907. 'NO_VOTES' => $user->lang['NO_VOTES'],
  908. 'success' => true,
  909. 'user_votes' => array_flip($valid_user_votes),
  910. 'vote_counts' => $vote_counts,
  911. 'total_votes' => array_sum($vote_counts),
  912. 'can_vote' => !count($valid_user_votes) || ($auth->acl_get('f_votechg', $forum_id) && $topic_data['poll_vote_change']),
  913. );
  914. /**
  915. * Event to manipulate the poll data sent by AJAX response
  916. *
  917. * @event core.viewtopic_modify_poll_ajax_data
  918. * @var array data JSON response data
  919. * @var array valid_user_votes Valid user votes
  920. * @var array vote_counts Vote counts
  921. * @var int forum_id Forum ID
  922. * @var array topic_data Topic data
  923. * @var array poll_info Array with the poll information
  924. * @since 3.2.4-RC1
  925. */
  926. $vars = array(
  927. 'data',
  928. 'valid_user_votes',
  929. 'vote_counts',
  930. 'forum_id',
  931. 'topic_data',
  932. 'poll_info',
  933. );
  934. extract($phpbb_dispatcher->trigger_event('core.viewtopic_modify_poll_ajax_data', compact($vars)));
  935. $json_response = new \phpbb\json_response();
  936. $json_response->send($data);
  937. }
  938. meta_refresh(5, $redirect_url);
  939. trigger_error($message);
  940. }
  941. $poll_total = 0;
  942. $poll_most = 0;
  943. foreach ($poll_info as $poll_option)
  944. {
  945. $poll_total += $poll_option['poll_option_total'];
  946. $poll_most = ($poll_option['poll_option_total'] >= $poll_most) ? $poll_option['poll_option_total'] : $poll_most;
  947. }
  948. $parse_flags = ($poll_info[0]['bbcode_bitfield'] ? OPTION_FLAG_BBCODE : 0) | OPTION_FLAG_SMILIES;
  949. for ($i = 0, $size = count($poll_info); $i < $size; $i++)
  950. {
  951. $poll_info[$i]['poll_option_text'] = generate_text_for_display($poll_info[$i]['poll_option_text'], $poll_info[$i]['bbcode_uid'], $poll_option['bbcode_bitfield'], $parse_flags, true);
  952. }
  953. $topic_data['poll_title'] = generate_text_for_display($topic_data['poll_title'], $poll_info[0]['bbcode_uid'], $poll_info[0]['bbcode_bitfield'], $parse_flags, true);
  954. $poll_template_data = $poll_options_template_data = array();
  955. foreach ($poll_info as $poll_option)
  956. {
  957. $option_pct = ($poll_total > 0) ? $poll_option['poll_option_total'] / $poll_total : 0;
  958. $option_pct_txt = sprintf("%.1d%%", round($option_pct * 100));
  959. $option_pct_rel = ($poll_most > 0) ? $poll_option['poll_option_total'] / $poll_most : 0;
  960. $option_pct_rel_txt = sprintf("%.1d%%", round($option_pct_rel * 100));
  961. $option_most_votes = ($poll_option['poll_option_total'] > 0 && $poll_option['poll_option_total'] == $poll_most) ? true : false;
  962. $poll_options_template_data[] = array(
  963. 'POLL_OPTION_ID' => $poll_option['poll_option_id'],
  964. 'POLL_OPTION_CAPTION' => $poll_option['poll_option_text'],
  965. 'POLL_OPTION_RESULT' => $poll_option['poll_option_total'],
  966. 'POLL_OPTION_PERCENT' => $option_pct_txt,
  967. 'POLL_OPTION_PERCENT_REL' => $option_pct_rel_txt,
  968. 'POLL_OPTION_PCT' => round($option_pct * 100),
  969. 'POLL_OPTION_WIDTH' => round($option_pct * 250),
  970. 'POLL_OPTION_VOTED' => (in_array($poll_option['poll_option_id'], $cur_voted_id)) ? true : false,
  971. 'POLL_OPTION_MOST_VOTES' => $option_most_votes,
  972. );
  973. }
  974. $poll_end = $topic_data['poll_length'] + $topic_data['poll_start'];
  975. $poll_template_data = array(
  976. 'POLL_QUESTION' => $topic_data['poll_title'],
  977. 'TOTAL_VOTES' => $poll_total,
  978. 'POLL_LEFT_CAP_IMG' => $user->img('poll_left'),
  979. 'POLL_RIGHT_CAP_IMG'=> $user->img('poll_right'),
  980. 'L_MAX_VOTES' => $user->lang('MAX_OPTIONS_SELECT', (int) $topic_data['poll_max_options']),
  981. 'L_POLL_LENGTH' => ($topic_data['poll_length']) ? sprintf($user->lang[($poll_end > time()) ? 'POLL_RUN_TILL' : 'POLL_ENDED_AT'], $user->format_date($poll_end)) : '',
  982. 'S_HAS_POLL' => true,
  983. 'S_CAN_VOTE' => $s_can_vote,
  984. 'S_DISPLAY_RESULTS' => $s_display_results,
  985. 'S_IS_MULTI_CHOICE' => ($topic_data['poll_max_options'] > 1) ? true : false,
  986. 'S_POLL_ACTION' => $viewtopic_url,
  987. 'U_VIEW_RESULTS' => $viewtopic_url . '&amp;view=viewpoll',
  988. );
  989. /**
  990. * Event to add/modify poll template data
  991. *
  992. * @event core.viewtopic_modify_poll_template_data
  993. * @var array cur_voted_id Array with options' IDs current user has voted for
  994. * @var int poll_end The poll end time
  995. * @var array poll_info Array with the poll information
  996. * @var array poll_options_template_data Array with the poll options template data
  997. * @var array poll_template_data Array with the common poll template data
  998. * @var int poll_total Total poll votes count
  999. * @var int poll_most Mostly voted option votes count
  1000. * @var array topic_data All the information from the topic and forum tables for this topic
  1001. * @var string viewtopic_url URL to the topic page
  1002. * @var array vote_counts Array with the vote counts for every poll option
  1003. * @var array voted_id Array with updated options' IDs current user is voting for
  1004. * @since 3.1.5-RC1
  1005. */
  1006. $vars = array(
  1007. 'cur_voted_id',
  1008. 'poll_end',
  1009. 'poll_info',
  1010. 'poll_options_template_data',
  1011. 'poll_template_data',
  1012. 'poll_total',
  1013. 'poll_most',
  1014. 'topic_data',
  1015. 'viewtopic_url',
  1016. 'vote_counts',
  1017. 'voted_id',
  1018. );
  1019. extract($phpbb_dispatcher->trigger_event('core.viewtopic_modify_poll_template_data', compact($vars)));
  1020. $template->assign_block_vars_array('poll_option', $poll_options_template_data);
  1021. $template->assign_vars($poll_template_data);
  1022. unset($poll_end, $poll_info, $poll_options_template_data, $poll_template_data, $voted_id);
  1023. }
  1024. // If the user is trying to reach the second half of the topic, fetch it starting from the end
  1025. $store_reverse = false;
  1026. $sql_limit = $config['posts_per_page'];
  1027. $sql_sort_order = $direction = '';
  1028. if ($start > $total_posts / 2)
  1029. {
  1030. $store_reverse = true;
  1031. // Select the sort order
  1032. $direction = (($sort_dir == 'd') ? 'ASC' : 'DESC');
  1033. $sql_limit = $pagination->reverse_limit($start, $sql_limit, $total_posts);
  1034. $sql_start = $pagination->reverse_start($start, $sql_limit, $total_posts);
  1035. }
  1036. else
  1037. {
  1038. // Select the sort order
  1039. $direction = (($sort_dir == 'd') ? 'DESC' : 'ASC');
  1040. $sql_start = $start;
  1041. }
  1042. if (is_array($sort_by_sql[$sort_key]))
  1043. {
  1044. $sql_sort_order = implode(' ' . $direction . ', ', $sort_by_sql[$sort_key]) . ' ' . $direction;
  1045. }
  1046. else
  1047. {
  1048. $sql_sort_order = $sort_by_sql[$sort_key] . ' ' . $direction;
  1049. }
  1050. // Container for user details, only process once
  1051. $post_list = $user_cache = $id_cache = $attachments = $attach_list = $rowset = $update_count = $post_edit_list = $post_delete_list = array();
  1052. $has_unapproved_attachments = $has_approved_attachments = $display_notice = false;
  1053. $i = $i_total = 0;
  1054. // Go ahead and pull all data for this topic
  1055. $sql = 'SELECT p.post_id
  1056. FROM ' . POSTS_TABLE . ' p' . (($join_user_sql[$sort_key]) ? ', ' . USERS_TABLE . ' u': '') . "
  1057. WHERE p.topic_id = $topic_id
  1058. AND " . $phpbb_content_visibility->get_visibility_sql('post', $forum_id, 'p.') . "
  1059. " . (($join_user_sql[$sort_key]) ? 'AND u.user_id = p.poster_id': '') . "
  1060. $limit_posts_time
  1061. ORDER BY $sql_sort_order";
  1062. /**
  1063. * Event to modify the SQL query that gets post_list
  1064. *
  1065. * @event core.viewtopic_modify_post_list_sql
  1066. * @var string sql The SQL query to generate the post_list
  1067. * @var int sql_limit The number of posts the query fetches
  1068. * @var int sql_start The index the query starts to fetch from
  1069. * @var string sort_key Key the posts are sorted by
  1070. * @var string sort_days Display posts of previous x days
  1071. * @var int forum_id Forum ID
  1072. * @since 3.2.4-RC1
  1073. */
  1074. $vars = array(
  1075. 'sql',
  1076. 'sql_limit',
  1077. 'sql_start',
  1078. 'sort_key',
  1079. 'sort_days',
  1080. 'forum_id',
  1081. );
  1082. extract($phpbb_dispatcher->trigger_event('core.viewtopic_modify_post_list_sql', compact($vars)));
  1083. $result = $db->sql_query_limit($sql, $sql_limit, $sql_start);
  1084. $i = ($store_reverse) ? $sql_limit - 1 : 0;
  1085. while ($row = $db->sql_fetchrow($result))
  1086. {
  1087. $post_list[$i] = (int) $row['post_id'];
  1088. ($store_reverse) ? $i-- : $i++;
  1089. }
  1090. $db->sql_freeresult($result);
  1091. if (!count($post_list))
  1092. {
  1093. if ($sort_days)
  1094. {
  1095. trigger_error('NO_POSTS_TIME_FRAME');
  1096. }
  1097. else
  1098. {
  1099. trigger_error('NO_TOPIC');
  1100. }
  1101. }
  1102. // Holding maximum post time for marking topic read
  1103. // We need to grab it because we do reverse ordering sometimes
  1104. $max_post_time = 0;
  1105. $sql_ary = array(
  1106. 'SELECT' => 'u.*, z.friend, z.foe, p.*',
  1107. 'FROM' => array(
  1108. USERS_TABLE => 'u',
  1109. POSTS_TABLE => 'p',
  1110. ),
  1111. 'LEFT_JOIN' => array(
  1112. array(
  1113. 'FROM' => array(ZEBRA_TABLE => 'z'),
  1114. 'ON' => 'z.user_id = ' . $user->data['user_id'] . ' AND z.zebra_id = p.poster_id',
  1115. ),
  1116. ),
  1117. 'WHERE' => $db->sql_in_set('p.post_id', $post_list) . '
  1118. AND u.user_id = p.poster_id',
  1119. );
  1120. /**
  1121. * Event to modify the SQL query before the post and poster data is retrieved
  1122. *
  1123. * @event core.viewtopic_get_post_data
  1124. * @var int forum_id Forum ID
  1125. * @var int topic_id Topic ID
  1126. * @var array topic_data Array with topic data
  1127. * @var array post_list Array with post_ids we are going to retrieve
  1128. * @var int sort_days Display posts of previous x days
  1129. * @var string sort_key Key the posts are sorted by
  1130. * @var string sort_dir Direction the posts are sorted by
  1131. * @var int start Pagination information
  1132. * @var array sql_ary The SQL array to get the data of posts and posters
  1133. * @since 3.1.0-a1
  1134. * @changed 3.1.0-a2 Added vars forum_id, topic_id, topic_data, post_list, sort_days, sort_key, sort_dir, start
  1135. */
  1136. $vars = array(
  1137. 'forum_id',
  1138. 'topic_id',
  1139. 'topic_data',
  1140. 'post_list',
  1141. 'sort_days',
  1142. 'sort_key',
  1143. 'sort_dir',
  1144. 'start',
  1145. 'sql_ary',
  1146. );
  1147. extract($phpbb_dispatcher->trigger_event('core.viewtopic_get_post_data', compact($vars)));
  1148. $sql = $db->sql_build_query('SELECT', $sql_ary);
  1149. $result = $db->sql_query($sql);
  1150. $now = $user->create_datetime();
  1151. $now = phpbb_gmgetdate($now->getTimestamp() + $now->getOffset());
  1152. // Posts are stored in the $rowset array while $attach_list, $user_cache
  1153. // and the global bbcode_bitfield are built
  1154. while ($row = $db->sql_fetchrow($result))
  1155. {
  1156. // Set max_post_time
  1157. if ($row['post_time'] > $max_post_time)
  1158. {
  1159. $max_post_time = $row['post_time'];
  1160. }
  1161. $poster_id = (int) $row['poster_id'];
  1162. // Does post have an attachment? If so, add it to the list
  1163. if ($row['post_attachment'] && $config['allow_attachments'])
  1164. {
  1165. $attach_list[] = (int) $row['post_id'];
  1166. if ($row['post_visibility'] == ITEM_UNAPPROVED || $row['post_visibility'] == ITEM_REAPPROVE)
  1167. {
  1168. $has_unapproved_attachments = true;
  1169. }
  1170. else if ($row['post_visibility'] == ITEM_APPROVED)
  1171. {
  1172. $has_approved_attachments = true;
  1173. }
  1174. }
  1175. $rowset_data = array(
  1176. 'hide_post' => (($row['foe'] || $row['post_visibility'] == ITEM_DELETED) && ($view != 'show' || $post_id != $row['post_id'])) ? true : false,
  1177. 'post_id' => $row['post_id'],
  1178. 'post_time' => $row['post_time'],
  1179. 'user_id' => $row['user_id'],
  1180. 'username' => $row['username'],
  1181. 'user_colour' => $row['user_colour'],
  1182. 'topic_id' => $row['topic_id'],
  1183. 'forum_id' => $row['forum_id'],
  1184. 'post_subject' => $row['post_subject'],
  1185. 'post_edit_count' => $row['post_edit_count'],
  1186. 'post_edit_time' => $row['post_edit_time'],
  1187. 'post_edit_reason' => $row['post_edit_reason'],
  1188. 'post_edit_user' => $row['post_edit_user'],
  1189. 'post_edit_locked' => $row['post_edit_locked'],
  1190. 'post_delete_time' => $row['post_delete_time'],
  1191. 'post_delete_reason'=> $row['post_delete_reason'],
  1192. 'post_delete_user' => $row['post_delete_user'],
  1193. // Make sure the icon actually exists
  1194. 'icon_id' => (isset($icons[$ro

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