PageRenderTime 63ms CodeModel.GetById 14ms 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
  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[$row['icon_id']]['img'], $icons[$row['icon_id']]['height'], $icons[$row['icon_id']]['width'])) ? $row['icon_id'] : 0,
  1195. 'post_attachment' => $row['post_attachment'],
  1196. 'post_visibility' => $row['post_visibility'],
  1197. 'post_reported' => $row['post_reported'],
  1198. 'post_username' => $row['post_username'],
  1199. 'post_text' => $row['post_text'],
  1200. 'bbcode_uid' => $row['bbcode_uid'],
  1201. 'bbcode_bitfield' => $row['bbcode_bitfield'],
  1202. 'enable_smilies' => $row['enable_smilies'],
  1203. 'enable_sig' => $row['enable_sig'],
  1204. 'friend' => $row['friend'],
  1205. 'foe' => $row['foe'],
  1206. );
  1207. /**
  1208. * Modify the post rowset containing data to be displayed with posts
  1209. *
  1210. * @event core.viewtopic_post_rowset_data
  1211. * @var array rowset_data Array with the rowset data for this post
  1212. * @var array row Array with original user and post data
  1213. * @since 3.1.0-a1
  1214. */
  1215. $vars = array('rowset_data', 'row');
  1216. extract($phpbb_dispatcher->trigger_event('core.viewtopic_post_rowset_data', compact($vars)));
  1217. $rowset[$row['post_id']] = $rowset_data;
  1218. // Cache various user specific data ... so we don't have to recompute
  1219. // this each time the same user appears on this page
  1220. if (!isset($user_cache[$poster_id]))
  1221. {
  1222. if ($poster_id == ANONYMOUS)
  1223. {
  1224. $user_cache_data = array(
  1225. 'user_type' => USER_IGNORE,
  1226. 'joined' => '',
  1227. 'posts' => '',
  1228. 'sig' => '',
  1229. 'sig_bbcode_uid' => '',
  1230. 'sig_bbcode_bitfield' => '',
  1231. 'online' => false,
  1232. 'avatar' => ($user->optionget('viewavatars')) ? phpbb_get_user_avatar($row) : '',
  1233. 'rank_title' => '',
  1234. 'rank_image' => '',
  1235. 'rank_image_src' => '',
  1236. 'pm' => '',
  1237. 'email' => '',
  1238. 'jabber' => '',
  1239. 'search' => '',
  1240. 'age' => '',
  1241. 'username' => $row['username'],
  1242. 'user_colour' => $row['user_colour'],
  1243. 'contact_user' => '',
  1244. 'warnings' => 0,
  1245. 'allow_pm' => 0,
  1246. );
  1247. /**
  1248. * Modify the guest user's data displayed with the posts
  1249. *
  1250. * @event core.viewtopic_cache_guest_data
  1251. * @var array user_cache_data Array with the user's data
  1252. * @var int poster_id Poster's user id
  1253. * @var array row Array with original user and post data
  1254. * @since 3.1.0-a1
  1255. */
  1256. $vars = array('user_cache_data', 'poster_id', 'row');
  1257. extract($phpbb_dispatcher->trigger_event('core.viewtopic_cache_guest_data', compact($vars)));
  1258. $user_cache[$poster_id] = $user_cache_data;
  1259. $user_rank_data = phpbb_get_user_rank($row, false);
  1260. $user_cache[$poster_id]['rank_title'] = $user_rank_data['title'];
  1261. $user_cache[$poster_id]['rank_image'] = $user_rank_data['img'];
  1262. $user_cache[$poster_id]['rank_image_src'] = $user_rank_data['img_src'];
  1263. }
  1264. else
  1265. {
  1266. $user_sig = '';
  1267. // We add the signature to every posters entry because enable_sig is post dependent
  1268. if ($row['user_sig'] && $config['allow_sig'] && $user->optionget('viewsigs'))
  1269. {
  1270. $user_sig = $row['user_sig'];
  1271. }
  1272. $id_cache[] = $poster_id;
  1273. $user_cache_data = array(
  1274. 'user_type' => $row['user_type'],
  1275. 'user_inactive_reason' => $row['user_inactive_reason'],
  1276. 'joined' => $user->format_date($row['user_regdate']),
  1277. 'posts' => $row['user_posts'],
  1278. 'warnings' => (isset($row['user_warnings'])) ? $row['user_warnings'] : 0,
  1279. 'sig' => $user_sig,
  1280. 'sig_bbcode_uid' => (!empty($row['user_sig_bbcode_uid'])) ? $row['user_sig_bbcode_uid'] : '',
  1281. 'sig_bbcode_bitfield' => (!empty($row['user_sig_bbcode_bitfield'])) ? $row['user_sig_bbcode_bitfield'] : '',
  1282. 'viewonline' => $row['user_allow_viewonline'],
  1283. 'allow_pm' => $row['user_allow_pm'],
  1284. 'avatar' => ($user->optionget('viewavatars')) ? phpbb_get_user_avatar($row) : '',
  1285. 'age' => '',
  1286. 'rank_title' => '',
  1287. 'rank_image' => '',
  1288. 'rank_image_src' => '',
  1289. 'username' => $row['username'],
  1290. 'user_colour' => $row['user_colour'],
  1291. 'contact_user' => $user->lang('CONTACT_USER', get_username_string('username', $poster_id, $row['username'], $row['user_colour'], $row['username'])),
  1292. 'online' => false,
  1293. 'jabber' => ($config['jab_enable'] && $row['user_jabber'] && $auth->acl_get('u_sendim')) ? append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=contact&amp;action=jabber&amp;u=$poster_id") : '',
  1294. 'search' => ($config['load_search'] && $auth->acl_get('u_search')) ? append_sid("{$phpbb_root_path}search.$phpEx", "author_id=$poster_id&amp;sr=posts") : '',
  1295. 'author_full' => get_username_string('full', $poster_id, $row['username'], $row['user_colour']),
  1296. 'author_colour' => get_username_string('colour', $poster_id, $row['username'], $row['user_colour']),
  1297. 'author_username' => get_username_string('username', $poster_id, $row['username'], $row['user_colour']),
  1298. 'author_profile' => get_username_string('profile', $poster_id, $row['username'], $row['user_colour']),
  1299. );
  1300. /**
  1301. * Modify the users' data displayed with their posts
  1302. *
  1303. * @event core.viewtopic_cache_user_data
  1304. * @var array user_cache_data Array with the user's data
  1305. * @var int poster_id Poster's user id
  1306. * @var array row Array with original user and post data
  1307. * @since 3.1.0-a1
  1308. */
  1309. $vars = array('user_cache_data', 'poster_id', 'row');
  1310. extract($phpbb_dispatcher->trigger_event('core.viewtopic_cache_user_data', compact($vars)));
  1311. $user_cache[$poster_id] = $user_cache_data;
  1312. $user_rank_data = phpbb_get_user_rank($row, $row['user_posts']);
  1313. $user_cache[$poster_id]['rank_title'] = $user_rank_data['title'];
  1314. $user_cache[$poster_id]['rank_image'] = $user_rank_data['img'];
  1315. $user_cache[$poster_id]['rank_image_src'] = $user_rank_data['img_src'];
  1316. if ((!empty($row['user_allow_viewemail']) && $auth->acl_get('u_sendemail')) || $auth->acl_get('a_email'))
  1317. {
  1318. $user_cache[$poster_id]['email'] = ($config['board_email_form'] && $config['email_enable']) ? append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=email&amp;u=$poster_id") : (($config['board_hide_emails'] && !$auth->acl_get('a_email')) ? '' : 'mailto:' . $row['user_email']);
  1319. }
  1320. else
  1321. {
  1322. $user_cache[$poster_id]['email'] = '';
  1323. }
  1324. if ($config['allow_birthdays'] && !empty($row['user_birthday']))
  1325. {
  1326. list($bday_day, $bday_month, $bday_year) = array_map('intval', explode('-', $row['user_birthday']));
  1327. if ($bday_year)
  1328. {
  1329. $diff = $now['mon'] - $bday_month;
  1330. if ($diff == 0)
  1331. {
  1332. $diff = ($now['mday'] - $bday_day < 0) ? 1 : 0;
  1333. }
  1334. else
  1335. {
  1336. $diff = ($diff < 0) ? 1 : 0;
  1337. }
  1338. $user_cache[$poster_id]['age'] = (int) ($now['year'] - $bday_year - $diff);
  1339. }
  1340. }
  1341. }
  1342. }
  1343. }
  1344. $db->sql_freeresult($result);
  1345. // Load custom profile fields
  1346. if ($config['load_cpf_viewtopic'])
  1347. {
  1348. /* @var $cp \phpbb\profilefields\manager */
  1349. $cp = $phpbb_container->get('profilefields.manager');
  1350. // Grab all profile fields from users in id cache for later use - similar to the poster cache
  1351. $profile_fields_tmp = $cp->grab_profile_fields_data($id_cache);
  1352. // filter out fields not to be displayed on viewtopic. Yes, it's a hack, but this shouldn't break any MODs.
  1353. $profile_fields_cache = array();
  1354. foreach ($profile_fields_tmp as $profile_user_id => $profile_fields)
  1355. {
  1356. $profile_fields_cache[$profile_user_id] = array();
  1357. foreach ($profile_fields as $used_ident => $profile_field)
  1358. {
  1359. if ($profile_field['data']['field_show_on_vt'])
  1360. {
  1361. $profile_fields_cache[$profile_user_id][$used_ident] = $profile_field;
  1362. }
  1363. }
  1364. }
  1365. unset($profile_fields_tmp);
  1366. }
  1367. // Generate online information for user
  1368. if ($config['load_onlinetrack'] && count($id_cache))
  1369. {
  1370. $sql = 'SELECT session_user_id, MAX(session_time) as online_time, MIN(session_viewonline) AS viewonline
  1371. FROM ' . SESSIONS_TABLE . '
  1372. WHERE ' . $db->sql_in_set('session_user_id', $id_cache) . '
  1373. GROUP BY session_user_id';
  1374. $result = $db->sql_query($sql);
  1375. $update_time = $config['load_online_time'] * 60;
  1376. while ($row = $db->sql_fetchrow($result))
  1377. {
  1378. $user_cache[$row['session_user_id']]['online'] = (time() - $update_time < $row['online_time'] && (($row['viewonline']) || $auth->acl_get('u_viewonline'))) ? true : false;
  1379. }
  1380. $db->sql_freeresult($result);
  1381. }
  1382. unset($id_cache);
  1383. // Pull attachment data
  1384. if (count($attach_list))
  1385. {
  1386. if ($auth->acl_get('u_download') && $auth->acl_get('f_download', $forum_id))
  1387. {
  1388. $sql = 'SELECT *
  1389. FROM ' . ATTACHMENTS_TABLE . '
  1390. WHERE ' . $db->sql_in_set('post_msg_id', $attach_list) . '
  1391. AND in_message = 0
  1392. ORDER BY attach_id DESC, post_msg_id ASC';
  1393. $result = $db->sql_query($sql);
  1394. while ($row = $db->sql_fetchrow($result))
  1395. {
  1396. $attachments[$row['post_msg_id']][] = $row;
  1397. }
  1398. $db->sql_freeresult($result);
  1399. // No attachments exist, but post table thinks they do so go ahead and reset post_attach flags
  1400. if (!count($attachments))
  1401. {
  1402. $sql = 'UPDATE ' . POSTS_TABLE . '
  1403. SET post_attachment = 0
  1404. WHERE ' . $db->sql_in_set('post_id', $attach_list);
  1405. $db->sql_query($sql);
  1406. // We need to update the topic indicator too if the complete topic is now without an attachment
  1407. if (count($rowset) != $total_posts)
  1408. {
  1409. // Not all posts are displayed so we query the db to find if there's any attachment for this topic
  1410. $sql = 'SELECT a.post_msg_id as post_id
  1411. FROM ' . ATTACHMENTS_TABLE . ' a, ' . POSTS_TABLE . " p
  1412. WHERE p.topic_id = $topic_id
  1413. AND p.post_visibility = " . ITEM_APPROVED . '
  1414. AND p.topic_id = a.topic_id';
  1415. $result = $db->sql_query_limit($sql, 1);
  1416. $row = $db->sql_fetchrow($result);
  1417. $db->sql_freeresult($result);
  1418. if (!$row)
  1419. {
  1420. $sql = 'UPDATE ' . TOPICS_TABLE . "
  1421. SET topic_attachment = 0
  1422. WHERE topic_id = $topic_id";
  1423. $db->sql_query($sql);
  1424. }
  1425. }
  1426. else
  1427. {
  1428. $sql = 'UPDATE ' . TOPICS_TABLE . "
  1429. SET topic_attachment = 0
  1430. WHERE topic_id = $topic_id";
  1431. $db->sql_query($sql);
  1432. }
  1433. }
  1434. else if ($has_approved_attachments && !$topic_data['topic_attachment'])
  1435. {
  1436. // Topic has approved attachments but its flag is wrong
  1437. $sql = 'UPDATE ' . TOPICS_TABLE . "
  1438. SET topic_attachment = 1
  1439. WHERE topic_id = $topic_id";
  1440. $db->sql_query($sql);
  1441. $topic_data['topic_attachment'] = 1;
  1442. }
  1443. else if ($has_unapproved_attachments && !$topic_data['topic_attachment'])
  1444. {
  1445. // Topic has only unapproved attachments but we have the right to see and download them
  1446. $topic_data['topic_attachment'] = 1;
  1447. }
  1448. }
  1449. else
  1450. {
  1451. $display_notice = true;
  1452. }
  1453. }
  1454. if ($config['enable_accurate_pm_button'])
  1455. {
  1456. // Get the list of users who can receive private messages
  1457. $can_receive_pm_list = $auth->acl_get_list(array_keys($user_cache), 'u_readpm');
  1458. $can_receive_pm_list = (empty($can_receive_pm_list) || !isset($can_receive_pm_list[0]['u_readpm'])) ? array() : $can_receive_pm_list[0]['u_readpm'];
  1459. // Get the list of permanently banned users
  1460. $permanently_banned_users = phpbb_get_banned_user_ids(array_keys($user_cache), false);
  1461. }
  1462. else
  1463. {
  1464. $can_receive_pm_list = array_keys($user_cache);
  1465. $permanently_banned_users = [];
  1466. }
  1467. $i_total = count($rowset) - 1;
  1468. $prev_post_id = '';
  1469. $template->assign_vars(array(
  1470. 'S_HAS_ATTACHMENTS' => $topic_data['topic_attachment'],
  1471. 'S_NUM_POSTS' => count($post_list))
  1472. );
  1473. /**
  1474. * Event to modify the post, poster and attachment data before assigning the posts
  1475. *
  1476. * @event core.viewtopic_modify_post_data
  1477. * @var int forum_id Forum ID
  1478. * @var int topic_id Topic ID
  1479. * @var array topic_data Array with topic data
  1480. * @var array post_list Array with post_ids we are going to display
  1481. * @var array rowset Array with post_id => post data
  1482. * @var array user_cache Array with prepared user data
  1483. * @var int start Pagination information
  1484. * @var int sort_days Display posts of previous x days
  1485. * @var string sort_key Key the posts are sorted by
  1486. * @var string sort_dir Direction the posts are sorted by
  1487. * @var bool display_notice Shall we display a notice instead of attachments
  1488. * @var bool has_approved_attachments Does the topic have approved attachments
  1489. * @var array attachments List of attachments post_id => array of attachments
  1490. * @var array permanently_banned_users List of permanently banned users
  1491. * @var array can_receive_pm_list Array with posters that can receive pms
  1492. * @since 3.1.0-RC3
  1493. */
  1494. $vars = array(
  1495. 'forum_id',
  1496. 'topic_id',
  1497. 'topic_data',
  1498. 'post_list',
  1499. 'rowset',
  1500. 'user_cache',
  1501. 'sort_days',
  1502. 'sort_key',
  1503. 'sort_dir',
  1504. 'start',
  1505. 'permanently_banned_users',
  1506. 'can_receive_pm_list',
  1507. 'display_notice',
  1508. 'has_approved_attachments',
  1509. 'attachments',
  1510. );
  1511. extract($phpbb_dispatcher->trigger_event('core.viewtopic_modify_post_data', compact($vars)));
  1512. // Output the posts
  1513. $first_unread = $post_unread = false;
  1514. for ($i = 0, $end = count($post_list); $i < $end; ++$i)
  1515. {
  1516. // A non-existing rowset only happens if there was no user present for the entered poster_id
  1517. // This could be a broken posts table.
  1518. if (!isset($rowset[$post_list[$i]]))
  1519. {
  1520. continue;
  1521. }
  1522. $row = $rowset[$post_list[$i]];
  1523. $poster_id = $row['user_id'];
  1524. // End signature parsing, only if needed
  1525. if ($user_cache[$poster_id]['sig'] && $row['enable_sig'] && empty($user_cache[$poster_id]['sig_parsed']))
  1526. {
  1527. $parse_flags = ($user_cache[$poster_id]['sig_bbcode_bitfield'] ? OPTION_FLAG_BBCODE : 0) | OPTION_FLAG_SMILIES;
  1528. $user_cache[$poster_id]['sig'] = generate_text_for_display($user_cache[$poster_id]['sig'], $user_cache[$poster_id]['sig_bbcode_uid'], $user_cache[$poster_id]['sig_bbcode_bitfield'], $parse_flags, true);
  1529. $user_cache[$poster_id]['sig_parsed'] = true;
  1530. }
  1531. // Parse the message and subject
  1532. $parse_flags = ($row['bbcode_bitfield'] ? OPTION_FLAG_BBCODE : 0) | OPTION_FLAG_SMILIES;
  1533. $message = generate_text_for_display($row['post_text'], $row['bbcode_uid'], $row['bbcode_bitfield'], $parse_flags, true);
  1534. if (!empty($attachments[$row['post_id']]))
  1535. {
  1536. parse_attachments($forum_id, $message, $attachments[$row['post_id']], $update_count);
  1537. }
  1538. // Replace naughty words such as farty pants
  1539. $row['post_subject'] = censor_text($row['post_subject']);
  1540. // Highlight active words (primarily for search)
  1541. if ($highlight_match)
  1542. {
  1543. $message = preg_replace('#(?!<.*)(?<!\w)(' . $highlight_match . ')(?!\w|[^<>]*(?:</s(?:cript|tyle))?>)#is', '<span class="posthilit">\1</span>', $message);
  1544. $row['post_subject'] = preg_replace('#(?!<.*)(?<!\w)(' . $highlight_match . ')(?!\w|[^<>]*(?:</s(?:cript|tyle))?>)#is', '<span class="posthilit">\1</span>', $row['post_subject']);
  1545. }
  1546. // Editing information
  1547. if (($row['post_edit_count'] && $config['display_last_edited']) || $row['post_edit_reason'])
  1548. {
  1549. // Get usernames for all following posts if not already stored
  1550. if (!count($post_edit_list) && ($row['post_edit_reason'] || ($row['post_edit_user'] && !isset($user_cache[$row['post_edit_user']]))))
  1551. {
  1552. // Remove all post_ids already parsed (we do not have to check them)
  1553. $post_storage_list = (!$store_reverse) ? array_slice($post_list, $i) : array_slice(array_reverse($post_list), $i);
  1554. $sql = 'SELECT DISTINCT u.user_id, u.username, u.user_colour
  1555. FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
  1556. WHERE ' . $db->sql_in_set('p.post_id', $post_storage_list) . '
  1557. AND p.post_edit_count <> 0
  1558. AND p.post_edit_user <> 0
  1559. AND p.post_edit_user = u.user_id';
  1560. $result2 = $db->sql_query($sql);
  1561. while ($user_edit_row = $db->sql_fetchrow($result2))
  1562. {
  1563. $post_edit_list[$user_edit_row['user_id']] = $user_edit_row;
  1564. }
  1565. $db->sql_freeresult($result2);
  1566. unset($post_storage_list);
  1567. }
  1568. if ($row['post_edit_reason'])
  1569. {
  1570. // User having edited the post also being the post author?
  1571. if (!$row['post_edit_user'] || $row['post_edit_user'] == $poster_id)
  1572. {
  1573. $display_username = get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']);
  1574. }
  1575. else
  1576. {
  1577. $display_username = get_username_string('full', $row['post_edit_user'], $post_edit_list[$row['post_edit_user']]['username'], $post_edit_list[$row['post_edit_user']]['user_colour']);
  1578. }
  1579. $l_edited_by = $user->lang('EDITED_TIMES_TOTAL', (int) $row['post_edit_count'], $display_username, $user->format_date($row['post_edit_time'], false, true));
  1580. }
  1581. else
  1582. {
  1583. if ($row['post_edit_user'] && !isset($user_cache[$row['post_edit_user']]))
  1584. {
  1585. $user_cache[$row['post_edit_user']] = $post_edit_list[$row['post_edit_user']];
  1586. }
  1587. // User having edited the post also being the post author?
  1588. if (!$row['post_edit_user'] || $row['post_edit_user'] == $poster_id)
  1589. {
  1590. $display_username = get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']);
  1591. }
  1592. else
  1593. {
  1594. $display_username = get_username_string('full', $row['post_edit_user'], $user_cache[$row['post_edit_user']]['username'], $user_cache[$row['post_edit_user']]['user_colour']);
  1595. }
  1596. $l_edited_by = $user->lang('EDITED_TIMES_TOTAL', (int) $row['post_edit_count'], $display_username, $user->format_date($row['post_edit_time'], false, true));
  1597. }
  1598. }
  1599. else
  1600. {
  1601. $l_edited_by = '';
  1602. }
  1603. // Deleting information
  1604. if ($row['post_visibility'] == ITEM_DELETED && $row['post_delete_user'])
  1605. {
  1606. // Get usernames for all following posts if not already stored
  1607. if (!count($post_delete_list) && ($row['post_delete_reason'] || ($row['post_delete_user'] && !isset($user_cache[$row['post_delete_user']]))))
  1608. {
  1609. // Remove all post_ids already parsed (we do not have to check them)
  1610. $post_storage_list = (!$store_reverse) ? array_slice($post_list, $i) : array_slice(array_reverse($post_list), $i);
  1611. $sql = 'SELECT DISTINCT u.user_id, u.username, u.user_colour
  1612. FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
  1613. WHERE ' . $db->sql_in_set('p.post_id', $post_storage_list) . '
  1614. AND p.post_delete_user <> 0
  1615. AND p.post_delete_user = u.user_id';
  1616. $result2 = $db->sql_query($sql);
  1617. while ($user_delete_row = $db->sql_fetchrow($result2))
  1618. {
  1619. $post_delete_list[$user_delete_row['user_id']] = $user_delete_row;
  1620. }
  1621. $db->sql_freeresult($result2);
  1622. unset($post_storage_list);
  1623. }
  1624. if ($row['post_delete_user'] && !isset($user_cache[$row['post_delete_user']]))
  1625. {
  1626. $user_cache[$row['post_delete_user']] = $post_delete_list[$row['post_delete_user']];
  1627. }
  1628. $display_postername = get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']);
  1629. // User having deleted the post also being the post author?
  1630. if (!$row['post_delete_user'] || $row['post_delete_user'] == $poster_id)
  1631. {
  1632. $display_username = $display_postername;
  1633. }
  1634. else
  1635. {
  1636. $display_username = get_username_string('full', $row['post_delete_user'], $user_cache[$row['post_delete_user']]['username'], $user_cache[$row['post_delete_user']]['user_colour']);
  1637. }
  1638. if ($row['post_delete_reason'])
  1639. {
  1640. $l_deleted_message = $user->lang('POST_DELETED_BY_REASON', $display_postername, $display_username, $user->format_date($row['post_delete_time'], false, true), $row['post_delete_reason']);
  1641. }
  1642. else
  1643. {
  1644. $l_deleted_message = $user->lang('POST_DELETED_BY', $display_postername, $display_username, $user->format_date($row['post_delete_time'], false, true));
  1645. }
  1646. $l_deleted_by = $user->lang('DELETED_INFORMATION', $display_username, $user->format_date($row['post_delete_time'], false, true));
  1647. }
  1648. else
  1649. {
  1650. $l_deleted_by = $l_deleted_message = '';
  1651. }
  1652. // Bump information
  1653. if ($topic_data['topic_bumped'] && $row['post_id'] == $topic_data['topic_last_post_id'] && isset($user_cache[$topic_data['topic_bumper']]) )
  1654. {
  1655. // It is safe to grab the username from the user cache array, we are at the last
  1656. // post and only the topic poster and last poster are allowed to bump.
  1657. // Admins and mods are bound to the above rules too...
  1658. $l_bumped_by = sprintf($user->lang['BUMPED_BY'], $user_cache[$topic_data['topic_bumper']]['username'], $user->format_date($topic_data['topic_last_post_time'], false, true));
  1659. }
  1660. else
  1661. {
  1662. $l_bumped_by = '';
  1663. }
  1664. $cp_row = array();
  1665. //
  1666. if ($config['load_cpf_viewtopic'])
  1667. {
  1668. $cp_row = (isset($profile_fields_cache[$poster_id])) ? $cp->generate_profile_fields_template_data($profile_fields_cache[$poster_id]) : array();
  1669. }
  1670. $post_unread = (isset($topic_tracking_info[$topic_id]) && $row['post_time'] > $topic_tracking_info[$topic_id]) ? true : false;
  1671. $s_first_unread = false;
  1672. if (!$first_unread && $post_unread)
  1673. {
  1674. $s_first_unread = $first_unread = true;
  1675. }
  1676. $force_edit_allowed = $force_delete_allowed = $force_softdelete_allowed = false;
  1677. $s_cannot_edit = !$auth->acl_get('f_edit', $forum_id) || $user->data['user_id'] != $poster_id;
  1678. $s_cannot_edit_time = $config['edit_time'] && $row['post_time'] <= time() - ($config['edit_time'] * 60);
  1679. $s_cannot_edit_locked = ($topic_data['topic_status'] == ITEM_LOCKED && !$auth->acl_get('m_lock', $forum_id)) || $row['post_edit_locked'];
  1680. $s_cannot_delete = $user->data['user_id'] != $poster_id || (
  1681. !$auth->acl_get('f_delete', $forum_id) &&
  1682. (!$auth->acl_get('f_softdelete', $forum_id) || $row['post_visibility'] == ITEM_DELETED)
  1683. );
  1684. $s_cannot_delete_lastpost = $topic_data['topic_last_post_id'] != $row['post_id'];
  1685. $s_cannot_delete_time = $config['delete_time'] && $row['post_time'] <= time() - ($config['delete_time'] * 60);
  1686. // we do not want to allow removal of the last post if a moderator locked it!
  1687. $s_cannot_delete_locked = $topic_data['topic_status'] == ITEM_LOCKED || $row['post_edit_locked'];
  1688. /**
  1689. * This event allows you to modify the conditions for the "can edit post" and "can delete post" checks
  1690. *
  1691. * @event core.viewtopic_modify_post_action_conditions
  1692. * @var array row Array with post data
  1693. * @var array topic_data Array with topic data
  1694. * @var bool force_edit_allowed Allow the user to edit the post (all permissions and conditions are ignored)
  1695. * @var bool s_cannot_edit User can not edit the post because it's not his
  1696. * @var bool s_cannot_edit_locked User can not edit the post because it's locked
  1697. * @var bool s_cannot_edit_time User can not edit the post because edit_time has passed
  1698. * @var bool force_delete_allowed Allow the user to delete the post (all permissions and conditions are ignored)
  1699. * @var bool s_cannot_delete User can not delete the post because it's not his
  1700. * @var bool s_cannot_delete_lastpost User can not delete the post because it's not the last post of the topic
  1701. * @var bool s_cannot_delete_locked User can not delete the post because it's locked
  1702. * @var bool s_cannot_delete_time User can not delete the post because edit_time has passed
  1703. * @var bool force_softdelete_allowed Allow the user to Ñ‹oftdelete the post (all permissions and conditions are ignored)
  1704. * @since 3.1.0-b4
  1705. * @changed 3.1.11-RC1 Added force_softdelete_allowed var
  1706. */
  1707. $vars = array(
  1708. 'row',
  1709. 'topic_data',
  1710. 'force_edit_allowed',
  1711. 's_cannot_edit',
  1712. 's_cannot_edit_locked',
  1713. 's_cannot_edit_time',
  1714. 'force_delete_allowed',
  1715. 's_cannot_delete',
  1716. 's_cannot_delete_lastpost',
  1717. 's_cannot_delete_locked',
  1718. 's_cannot_delete_time',
  1719. 'force_softdelete_allowed',
  1720. );
  1721. extract($phpbb_dispatcher->trigger_event('core.viewtopic_modify_post_action_conditions', compact($vars)));
  1722. $edit_allowed = $force_edit_allowed || ($user->data['is_registered'] && ($auth->acl_get('m_edit', $forum_id) || (
  1723. !$s_cannot_edit &&
  1724. !$s_cannot_edit_time &&
  1725. !$s_cannot_edit_locked
  1726. )));
  1727. $quote_allowed = $auth->acl_get('m_edit', $forum_id) || ($topic_data['topic_status'] != ITEM_LOCKED &&
  1728. ($user->data['user_id'] == ANONYMOUS || $auth->acl_get('f_reply', $forum_id))
  1729. );
  1730. // Only display the quote button if the post is quotable. Posts not approved are not quotable.
  1731. $quote_allowed = ($quote_allowed && $row['post_visibility'] == ITEM_APPROVED) ? true : false;
  1732. $delete_allowed = $force_delete_allowed || ($user->data['is_registered'] && (
  1733. ($auth->acl_get('m_delete', $forum_id) || ($auth->acl_get('m_softdelete', $forum_id) && $row['post_visibility'] != ITEM_DELETED)) ||
  1734. (!$s_cannot_delete && !$s_cannot_delete_lastpost && !$s_cannot_delete_time && !$s_cannot_delete_locked)
  1735. ));
  1736. $softdelete_allowed = $force_softdelete_allowed || (($auth->acl_get('m_softdelete', $forum_id) ||
  1737. ($auth->acl_get('f_softdelete', $forum_id) && $user->data['user_id'] == $poster_id)) && ($row['post_visibility'] != ITEM_DELETED));
  1738. $permanent_delete_allowed = $force_delete_allowed || ($auth->acl_get('m_delete', $forum_id) ||
  1739. ($auth->acl_get('f_delete', $forum_id) && $user->data['user_id'] == $poster_id));
  1740. // Can this user receive a Private Message?
  1741. $can_receive_pm = (
  1742. // They must be a "normal" user
  1743. $user_cache[$poster_id]['user_type'] != USER_IGNORE &&
  1744. // They must not be deactivated by the administrator
  1745. ($user_cache[$poster_id]['user_type'] != USER_INACTIVE || $user_cache[$poster_id]['user_inactive_reason'] != INACTIVE_MANUAL) &&
  1746. // They must be able to read PMs
  1747. in_array($poster_id, $can_receive_pm_list) &&
  1748. // They must not be permanently banned
  1749. !in_array($poster_id, $permanently_banned_users) &&
  1750. // They must allow users to contact via PM
  1751. (($auth->acl_gets('a_', 'm_') || $auth->acl_getf_global('m_')) || $user_cache[$poster_id]['allow_pm'])
  1752. );
  1753. $u_pm = '';
  1754. if ($config['allow_privmsg'] && $auth->acl_get('u_sendpm') && $can_receive_pm)
  1755. {
  1756. $u_pm = append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&amp;mode=compose&amp;action=quotepost&amp;p=' . $row['post_id']);
  1757. }
  1758. //
  1759. $post_row = array(
  1760. 'POST_AUTHOR_FULL' => ($poster_id != ANONYMOUS) ? $user_cache[$poster_id]['author_full'] : get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
  1761. 'POST_AUTHOR_COLOUR' => ($poster_id != ANONYMOUS) ? $user_cache[$poster_id]['author_colour'] : get_username_string('colour', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
  1762. 'POST_AUTHOR' => ($poster_id != ANONYMOUS) ? $user_cache[$poster_id]['author_username'] : get_username_string('username', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
  1763. 'U_POST_AUTHOR' => ($poster_id != ANONYMOUS) ? $user_cache[$poster_id]['author_profile'] : get_username_string('profile', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
  1764. 'RANK_TITLE' => $user_cache[$poster_id]['rank_title'],
  1765. 'RANK_IMG' => $user_cache[$poster_id]['rank_image'],
  1766. 'RANK_IMG_SRC' => $user_cache[$poster_id]['rank_image_src'],
  1767. 'POSTER_JOINED' => $user_cache[$poster_id]['joined'],
  1768. 'POSTER_POSTS' => $user_cache[$poster_id]['posts'],
  1769. 'POSTER_AVATAR' => $user_cache[$poster_id]['avatar'],
  1770. 'POSTER_WARNINGS' => $auth->acl_get('m_warn') ? $user_cache[$poster_id]['warnings'] : '',
  1771. 'POSTER_AGE' => $user_cache[$poster_id]['age'],
  1772. 'CONTACT_USER' => $user_cache[$poster_id]['contact_user'],
  1773. 'POST_DATE' => $user->format_date($row['post_time'], false, ($view == 'print') ? true : false),
  1774. 'POST_DATE_RFC3339' => gmdate(DATE_RFC3339, $row['post_time']),
  1775. 'POST_SUBJECT' => $row['post_subject'],
  1776. 'MESSAGE' => $message,
  1777. 'SIGNATURE' => ($row['enable_sig']) ? $user_cache[$poster_id]['sig'] : '',
  1778. 'EDITED_MESSAGE' => $l_edited_by,
  1779. 'EDIT_REASON' => $row['post_edit_reason'],
  1780. 'DELETED_MESSAGE' => $l_deleted_by,
  1781. 'DELETE_REASON' => $row['post_delete_reason'],
  1782. 'BUMPED_MESSAGE' => $l_bumped_by,
  1783. 'MINI_POST_IMG' => ($post_unread) ? $user->img('icon_post_target_unread', 'UNREAD_POST') : $user->img('icon_post_target', 'POST'),
  1784. 'POST_ICON_IMG' => ($topic_data['enable_icons'] && !empty($row['icon_id'])) ? $icons[$row['icon_id']]['img'] : '',
  1785. 'POST_ICON_IMG_WIDTH' => ($topic_data['enable_icons'] && !empty($row['icon_id'])) ? $icons[$row['icon_id']]['width'] : '',
  1786. 'POST_ICON_IMG_HEIGHT' => ($topic_data['enable_icons'] && !empty($row['icon_id'])) ? $icons[$row['icon_id']]['height'] : '',
  1787. 'POST_ICON_IMG_ALT' => ($topic_data['enable_icons'] && !empty($row['icon_id'])) ? $icons[$row['icon_id']]['alt'] : '',
  1788. 'ONLINE_IMG' => ($poster_id == ANONYMOUS || !$config['load_onlinetrack']) ? '' : (($user_cache[$poster_id]['online']) ? $user->img('icon_user_online', 'ONLINE') : $user->img('icon_user_offline', 'OFFLINE')),
  1789. 'S_ONLINE' => ($poster_id == ANONYMOUS || !$config['load_onlinetrack']) ? false : (($user_cache[$poster_id]['online']) ? true : false),
  1790. 'U_EDIT' => ($edit_allowed) ? append_sid("{$phpbb_root_path}posting.$phpEx", "mode=edit&amp;f=$forum_id&amp;p={$row['post_id']}") : '',
  1791. 'U_QUOTE' => ($quote_allowed) ? append_sid("{$phpbb_root_path}posting.$phpEx", "mode=quote&amp;f=$forum_id&amp;p={$row['post_id']}") : '',
  1792. 'U_INFO' => ($auth->acl_get('m_info', $forum_id)) ? append_sid("{$phpbb_root_path}mcp.$phpEx", "i=main&amp;mode=post_details&amp;f=$forum_id&amp;p=" . $row['post_id'], true, $user->session_id) : '',
  1793. 'U_DELETE' => ($delete_allowed) ? append_sid("{$phpbb_root_path}posting.$phpEx", 'mode=' . (($softdelete_allowed) ? 'soft_delete' : 'delete') . "&amp;f=$forum_id&amp;p={$row['post_id']}") : '',
  1794. 'U_SEARCH' => $user_cache[$poster_id]['search'],
  1795. 'U_PM' => $u_pm,
  1796. 'U_EMAIL' => $user_cache[$poster_id]['email'],
  1797. 'U_JABBER' => $user_cache[$poster_id]['jabber'],
  1798. 'U_APPROVE_ACTION' => append_sid("{$phpbb_root_path}mcp.$phpEx", "i=queue&amp;p={$row['post_id']}&amp;f=$forum_id&amp;redirect=" . urlencode(str_replace('&amp;', '&', $viewtopic_url . '&amp;p=' . $row['post_id'] . '#p' . $row['post_id']))),
  1799. 'U_REPORT' => ($auth->acl_get('f_report', $forum_id)) ? $phpbb_container->get('controller.helper')->route('phpbb_report_post_controller', array('id' => $row['post_id'])) : '',
  1800. 'U_MCP_REPORT' => ($auth->acl_get('m_report', $forum_id)) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=reports&amp;mode=report_details&amp;f=' . $forum_id . '&amp;p=' . $row['post_id'], true, $user->session_id) : '',
  1801. 'U_MCP_APPROVE' => ($auth->acl_get('m_approve', $forum_id)) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=queue&amp;mode=approve_details&amp;f=' . $forum_id . '&amp;p=' . $row['post_id'], true, $user->session_id) : '',
  1802. 'U_MCP_RESTORE' => ($auth->acl_get('m_approve', $forum_id)) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=queue&amp;mode=' . (($topic_data['topic_visibility'] != ITEM_DELETED) ? 'deleted_posts' : 'deleted_topics') . '&amp;f=' . $forum_id . '&amp;p=' . $row['post_id'], true, $user->session_id) : '',
  1803. 'U_MINI_POST' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'p=' . $row['post_id']) . '#p' . $row['post_id'],
  1804. 'U_NEXT_POST_ID' => ($i < $i_total && isset($rowset[$post_list[$i + 1]])) ? $rowset[$post_list[$i + 1]]['post_id'] : '',
  1805. 'U_PREV_POST_ID' => $prev_post_id,
  1806. 'U_NOTES' => ($auth->acl_getf_global('m_')) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=notes&amp;mode=user_notes&amp;u=' . $poster_id, true, $user->session_id) : '',
  1807. 'U_WARN' => ($auth->acl_get('m_warn') && $poster_id != $user->data['user_id'] && $poster_id != ANONYMOUS) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=warn&amp;mode=warn_post&amp;f=' . $forum_id . '&amp;p=' . $row['post_id'], true, $user->session_id) : '',
  1808. 'POST_ID' => $row['post_id'],
  1809. 'POST_NUMBER' => $i + $start + 1,
  1810. 'POSTER_ID' => $poster_id,
  1811. 'MINI_POST' => ($post_unread) ? $user->lang['UNREAD_POST'] : $user->lang['POST'],
  1812. 'S_HAS_ATTACHMENTS' => (!empty($attachments[$row['post_id']])) ? true : false,
  1813. 'S_MULTIPLE_ATTACHMENTS' => !empty($attachments[$row['post_id']]) && count($attachments[$row['post_id']]) > 1,
  1814. 'S_POST_UNAPPROVED' => ($row['post_visibility'] == ITEM_UNAPPROVED || $row['post_visibility'] == ITEM_REAPPROVE) ? true : false,
  1815. 'S_CAN_APPROVE' => $auth->acl_get('m_approve', $forum_id),
  1816. 'S_POST_DELETED' => ($row['post_visibility'] == ITEM_DELETED) ? true : false,
  1817. 'L_POST_DELETED_MESSAGE' => $l_deleted_message,
  1818. 'S_POST_REPORTED' => ($row['post_reported'] && $auth->acl_get('m_report', $forum_id)) ? true : false,
  1819. 'S_DISPLAY_NOTICE' => $display_notice && $row['post_attachment'],
  1820. 'S_FRIEND' => ($row['friend']) ? true : false,
  1821. 'S_UNREAD_POST' => $post_unread,
  1822. 'S_FIRST_UNREAD' => $s_first_unread,
  1823. 'S_CUSTOM_FIELDS' => (isset($cp_row['row']) && count($cp_row['row'])) ? true : false,
  1824. 'S_TOPIC_POSTER' => ($topic_data['topic_poster'] == $poster_id) ? true : false,
  1825. 'S_FIRST_POST' => ($topic_data['topic_first_post_id'] == $row['post_id']) ? true : false,
  1826. 'S_IGNORE_POST' => ($row['foe']) ? true : false,
  1827. 'L_IGNORE_POST' => ($row['foe']) ? sprintf($user->lang['POST_BY_FOE'], get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username'])) : '',
  1828. 'S_POST_HIDDEN' => $row['hide_post'],
  1829. 'L_POST_DISPLAY' => ($row['hide_post']) ? $user->lang('POST_DISPLAY', '<a class="display_post" data-post-id="' . $row['post_id'] . '" href="' . $viewtopic_url . "&amp;p={$row['post_id']}&amp;view=show#p{$row['post_id']}" . '">', '</a>') : '',
  1830. 'S_DELETE_PERMANENT' => $permanent_delete_allowed,
  1831. );
  1832. $user_poster_data = $user_cache[$poster_id];
  1833. $current_row_number = $i;
  1834. /**
  1835. * Modify the posts template block
  1836. *
  1837. * @event core.viewtopic_modify_post_row
  1838. * @var int start Start item of this page
  1839. * @var int current_row_number Number of the post on this page
  1840. * @var int end Number of posts on this page
  1841. * @var int total_posts Total posts count
  1842. * @var int poster_id Post author id
  1843. * @var array row Array with original post and user data
  1844. * @var array cp_row Custom profile field data of the poster
  1845. * @var array attachments List of attachments
  1846. * @var array user_poster_data Poster's data from user cache
  1847. * @var array post_row Template block array of the post
  1848. * @var array topic_data Array with topic data
  1849. * @var array user_cache Array with cached user data
  1850. * @var array post_edit_list Array with post edited list
  1851. * @since 3.1.0-a1
  1852. * @changed 3.1.0-a3 Added vars start, current_row_number, end, attachments
  1853. * @changed 3.1.0-b3 Added topic_data array, total_posts
  1854. * @changed 3.1.0-RC3 Added poster_id
  1855. * @changed 3.2.2-RC1 Added user_cache and post_edit_list
  1856. */
  1857. $vars = array(
  1858. 'start',
  1859. 'current_row_number',
  1860. 'end',
  1861. 'total_posts',
  1862. 'poster_id',
  1863. 'row',
  1864. 'cp_row',
  1865. 'attachments',
  1866. 'user_poster_data',
  1867. 'post_row',
  1868. 'topic_data',
  1869. 'user_cache',
  1870. 'post_edit_list',
  1871. );
  1872. extract($phpbb_dispatcher->trigger_event('core.viewtopic_modify_post_row', compact($vars)));
  1873. $i = $current_row_number;
  1874. if (isset($cp_row['row']) && count($cp_row['row']))
  1875. {
  1876. $post_row = array_merge($post_row, $cp_row['row']);
  1877. }
  1878. // Dump vars into template
  1879. $template->assign_block_vars('postrow', $post_row);
  1880. $contact_fields = array(
  1881. array(
  1882. 'ID' => 'pm',
  1883. 'NAME' => $user->lang['SEND_PRIVATE_MESSAGE'],
  1884. 'U_CONTACT' => $post_row['U_PM'],
  1885. ),
  1886. array(
  1887. 'ID' => 'email',
  1888. 'NAME' => $user->lang['SEND_EMAIL'],
  1889. 'U_CONTACT' => $user_cache[$poster_id]['email'],
  1890. ),
  1891. array(
  1892. 'ID' => 'jabber',
  1893. 'NAME' => $user->lang['JABBER'],
  1894. 'U_CONTACT' => $user_cache[$poster_id]['jabber'],
  1895. ),
  1896. );
  1897. foreach ($contact_fields as $field)
  1898. {
  1899. if ($field['U_CONTACT'])
  1900. {
  1901. $template->assign_block_vars('postrow.contact', $field);
  1902. }
  1903. }
  1904. if (!empty($cp_row['blockrow']))
  1905. {
  1906. foreach ($cp_row['blockrow'] as $field_data)
  1907. {
  1908. $template->assign_block_vars('postrow.custom_fields', $field_data);
  1909. if ($field_data['S_PROFILE_CONTACT'])
  1910. {
  1911. $template->assign_block_vars('postrow.contact', array(
  1912. 'ID' => $field_data['PROFILE_FIELD_IDENT'],
  1913. 'NAME' => $field_data['PROFILE_FIELD_NAME'],
  1914. 'U_CONTACT' => $field_data['PROFILE_FIELD_CONTACT'],
  1915. ));
  1916. }
  1917. }
  1918. }
  1919. // Display not already displayed Attachments for this post, we already parsed them. ;)
  1920. if (!empty($attachments[$row['post_id']]))
  1921. {
  1922. foreach ($attachments[$row['post_id']] as $attachment)
  1923. {
  1924. $template->assign_block_vars('postrow.attachment', array(
  1925. 'DISPLAY_ATTACHMENT' => $attachment)
  1926. );
  1927. }
  1928. }
  1929. $current_row_number = $i;
  1930. /**
  1931. * Event after the post data has been assigned to the template
  1932. *
  1933. * @event core.viewtopic_post_row_after
  1934. * @var int start Start item of this page
  1935. * @var int current_row_number Number of the post on this page
  1936. * @var int end Number of posts on this page
  1937. * @var int total_posts Total posts count
  1938. * @var array row Array with original post and user data
  1939. * @var array cp_row Custom profile field data of the poster
  1940. * @var array attachments List of attachments
  1941. * @var array user_poster_data Poster's data from user cache
  1942. * @var array post_row Template block array of the post
  1943. * @var array topic_data Array with topic data
  1944. * @since 3.1.0-a3
  1945. * @changed 3.1.0-b3 Added topic_data array, total_posts
  1946. */
  1947. $vars = array(
  1948. 'start',
  1949. 'current_row_number',
  1950. 'end',
  1951. 'total_posts',
  1952. 'row',
  1953. 'cp_row',
  1954. 'attachments',
  1955. 'user_poster_data',
  1956. 'post_row',
  1957. 'topic_data',
  1958. );
  1959. extract($phpbb_dispatcher->trigger_event('core.viewtopic_post_row_after', compact($vars)));
  1960. $i = $current_row_number;
  1961. $prev_post_id = $row['post_id'];
  1962. unset($rowset[$post_list[$i]]);
  1963. unset($attachments[$row['post_id']]);
  1964. }
  1965. unset($rowset, $user_cache);
  1966. // Update topic view and if necessary attachment view counters ... but only for humans and if this is the first 'page view'
  1967. if (isset($user->data['session_page']) && !$user->data['is_bot'] && (strpos($user->data['session_page'], '&t=' . $topic_id) === false || isset($user->data['session_created'])))
  1968. {
  1969. $sql = 'UPDATE ' . TOPICS_TABLE . '
  1970. SET topic_views = topic_views + 1, topic_last_view_time = ' . time() . "
  1971. WHERE topic_id = $topic_id";
  1972. $db->sql_query($sql);
  1973. // Update the attachment download counts
  1974. if (count($update_count))
  1975. {
  1976. $sql = 'UPDATE ' . ATTACHMENTS_TABLE . '
  1977. SET download_count = download_count + 1
  1978. WHERE ' . $db->sql_in_set('attach_id', array_unique($update_count));
  1979. $db->sql_query($sql);
  1980. }
  1981. }
  1982. // Only mark topic if it's currently unread. Also make sure we do not set topic tracking back if earlier pages are viewed.
  1983. if (isset($topic_tracking_info[$topic_id]) && $topic_data['topic_last_post_time'] > $topic_tracking_info[$topic_id] && $max_post_time > $topic_tracking_info[$topic_id])
  1984. {
  1985. markread('topic', $forum_id, $topic_id, $max_post_time);
  1986. // Update forum info
  1987. $all_marked_read = update_forum_tracking_info($forum_id, $topic_data['forum_last_post_time'], (isset($topic_data['forum_mark_time'])) ? $topic_data['forum_mark_time'] : false, false);
  1988. }
  1989. else
  1990. {
  1991. $all_marked_read = true;
  1992. }
  1993. // If there are absolutely no more unread posts in this forum
  1994. // and unread posts shown, we can safely show the #unread link
  1995. if ($all_marked_read)
  1996. {
  1997. if ($post_unread)
  1998. {
  1999. $template->assign_vars(array(
  2000. 'U_VIEW_UNREAD_POST' => '#unread',
  2001. ));
  2002. }
  2003. else if (isset($topic_tracking_info[$topic_id]) && $topic_data['topic_last_post_time'] > $topic_tracking_info[$topic_id])
  2004. {
  2005. $template->assign_vars(array(
  2006. 'U_VIEW_UNREAD_POST' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;view=unread") . '#unread',
  2007. ));
  2008. }
  2009. }
  2010. else if (!$all_marked_read)
  2011. {
  2012. $last_page = ((floor($start / $config['posts_per_page']) + 1) == max(ceil($total_posts / $config['posts_per_page']), 1)) ? true : false;
  2013. // What can happen is that we are at the last displayed page. If so, we also display the #unread link based in $post_unread
  2014. if ($last_page && $post_unread)
  2015. {
  2016. $template->assign_vars(array(
  2017. 'U_VIEW_UNREAD_POST' => '#unread',
  2018. ));
  2019. }
  2020. else if (!$last_page)
  2021. {
  2022. $template->assign_vars(array(
  2023. 'U_VIEW_UNREAD_POST' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;view=unread") . '#unread',
  2024. ));
  2025. }
  2026. }
  2027. // let's set up quick_reply
  2028. $s_quick_reply = false;
  2029. if ($user->data['is_registered'] && $config['allow_quick_reply'] && ($topic_data['forum_flags'] & FORUM_FLAG_QUICK_REPLY) && $auth->acl_get('f_reply', $forum_id))
  2030. {
  2031. // Quick reply enabled forum
  2032. $s_quick_reply = (($topic_data['forum_status'] == ITEM_UNLOCKED && $topic_data['topic_status'] == ITEM_UNLOCKED) || $auth->acl_get('m_edit', $forum_id)) ? true : false;
  2033. }
  2034. if ($s_can_vote || $s_quick_reply)
  2035. {
  2036. add_form_key('posting');
  2037. if ($s_quick_reply)
  2038. {
  2039. $s_attach_sig = $config['allow_sig'] && $user->optionget('attachsig') && $auth->acl_get('f_sigs', $forum_id) && $auth->acl_get('u_sig');
  2040. $s_smilies = $config['allow_smilies'] && $user->optionget('smilies') && $auth->acl_get('f_smilies', $forum_id);
  2041. $s_bbcode = $config['allow_bbcode'] && $user->optionget('bbcode') && $auth->acl_get('f_bbcode', $forum_id);
  2042. $s_notify = $config['allow_topic_notify'] && ($user->data['user_notify'] || $s_watching_topic['is_watching']);
  2043. $qr_hidden_fields = array(
  2044. 'topic_cur_post_id' => (int) $topic_data['topic_last_post_id'],
  2045. 'topic_id' => (int) $topic_data['topic_id'],
  2046. 'forum_id' => (int) $forum_id,
  2047. );
  2048. // Originally we use checkboxes and check with isset(), so we only provide them if they would be checked
  2049. (!$s_bbcode) ? $qr_hidden_fields['disable_bbcode'] = 1 : true;
  2050. (!$s_smilies) ? $qr_hidden_fields['disable_smilies'] = 1 : true;
  2051. (!$config['allow_post_links']) ? $qr_hidden_fields['disable_magic_url'] = 1 : true;
  2052. ($s_attach_sig) ? $qr_hidden_fields['attach_sig'] = 1 : true;
  2053. ($s_notify) ? $qr_hidden_fields['notify'] = 1 : true;
  2054. ($topic_data['topic_status'] == ITEM_LOCKED) ? $qr_hidden_fields['lock_topic'] = 1 : true;
  2055. $tpl_ary = [
  2056. 'S_QUICK_REPLY' => true,
  2057. 'U_QR_ACTION' => append_sid("{$phpbb_root_path}posting.$phpEx", "mode=reply&amp;f=$forum_id&amp;t=$topic_id"),
  2058. 'QR_HIDDEN_FIELDS' => build_hidden_fields($qr_hidden_fields),
  2059. 'SUBJECT' => 'Re: ' . censor_text($topic_data['topic_title']),
  2060. ];
  2061. /**
  2062. * Event after the quick-reply has been setup
  2063. *
  2064. * @event core.viewtopic_modify_quick_reply_template_vars
  2065. * @var array tpl_ary Array with template data
  2066. * @var array topic_data Array with topic data
  2067. * @since 3.2.9-RC1
  2068. */
  2069. $vars = ['tpl_ary', 'topic_data'];
  2070. extract($phpbb_dispatcher->trigger_event('core.viewtopic_modify_quick_reply_template_vars', compact($vars)));
  2071. $template->assign_vars($tpl_ary);
  2072. }
  2073. }
  2074. // now I have the urge to wash my hands :(
  2075. // We overwrite $_REQUEST['f'] if there is no forum specified
  2076. // to be able to display the correct online list.
  2077. // One downside is that the user currently viewing this topic/post is not taken into account.
  2078. if (!$request->variable('f', 0))
  2079. {
  2080. $request->overwrite('f', $forum_id);
  2081. }
  2082. // We need to do the same with the topic_id. See #53025.
  2083. if (!$request->variable('t', 0) && !empty($topic_id))
  2084. {
  2085. $request->overwrite('t', $topic_id);
  2086. }
  2087. $page_title = $topic_data['topic_title'] . ($start ? ' - ' . sprintf($user->lang['PAGE_TITLE_NUMBER'], $pagination->get_on_page($config['posts_per_page'], $start)) : '');
  2088. /**
  2089. * You can use this event to modify the page title of the viewtopic page
  2090. *
  2091. * @event core.viewtopic_modify_page_title
  2092. * @var string page_title Title of the viewtopic page
  2093. * @var array topic_data Array with topic data
  2094. * @var int forum_id Forum ID of the topic
  2095. * @var int start Start offset used to calculate the page
  2096. * @var array post_list Array with post_ids we are going to display
  2097. * @since 3.1.0-a1
  2098. * @changed 3.1.0-RC4 Added post_list var
  2099. */
  2100. $vars = array('page_title', 'topic_data', 'forum_id', 'start', 'post_list');
  2101. extract($phpbb_dispatcher->trigger_event('core.viewtopic_modify_page_title', compact($vars)));
  2102. // Output the page
  2103. page_header($page_title, true, $forum_id);
  2104. $template->set_filenames(array(
  2105. 'body' => ($view == 'print') ? 'viewtopic_print.html' : 'viewtopic_body.html')
  2106. );
  2107. make_jumpbox(append_sid("{$phpbb_root_path}viewforum.$phpEx"), $forum_id);
  2108. page_footer();