PageRenderTime 55ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Sources/Display.php

https://github.com/smf-portal/SMF2.1
PHP | 1831 lines | 1336 code | 219 blank | 276 comment | 406 complexity | 90765a7785ab0d0e699fce00d946bafd MD5 | raw file

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

  1. <?php
  2. /**
  3. * This is perhaps the most important and probably most accessed file in all
  4. * of SMF. This file controls topic, message, and attachment display.
  5. *
  6. * Simple Machines Forum (SMF)
  7. *
  8. * @package SMF
  9. * @author Simple Machines http://www.simplemachines.org
  10. * @copyright 2012 Simple Machines
  11. * @license http://www.simplemachines.org/about/smf/license.php BSD
  12. *
  13. * @version 2.1 Alpha 1
  14. */
  15. if (!defined('SMF'))
  16. die('Hacking attempt...');
  17. /**
  18. * The central part of the board - topic display.
  19. * This function loads the posts in a topic up so they can be displayed.
  20. * It supports wireless, using wap/wap2/imode and the Wireless templates.
  21. * It uses the main sub template of the Display template.
  22. * It requires a topic, and can go to the previous or next topic from it.
  23. * It jumps to the correct post depending on a number/time/IS_MSG passed.
  24. * It depends on the messages_per_page, defaultMaxMessages and enableAllMessages settings.
  25. * It is accessed by ?topic=id_topic.START.
  26. */
  27. function Display()
  28. {
  29. global $scripturl, $txt, $modSettings, $context, $settings;
  30. global $options, $sourcedir, $user_info, $board_info, $topic, $board;
  31. global $attachments, $messages_request, $topicinfo, $language, $smcFunc;
  32. // What are you gonna display if these are empty?!
  33. if (empty($topic))
  34. fatal_lang_error('no_board', false);
  35. // Load the proper template and/or sub template.
  36. if (WIRELESS)
  37. $context['sub_template'] = WIRELESS_PROTOCOL . '_display';
  38. else
  39. loadTemplate('Display');
  40. // Not only does a prefetch make things slower for the server, but it makes it impossible to know if they read it.
  41. if (isset($_SERVER['HTTP_X_MOZ']) && $_SERVER['HTTP_X_MOZ'] == 'prefetch')
  42. {
  43. ob_end_clean();
  44. header('HTTP/1.1 403 Prefetch Forbidden');
  45. die;
  46. }
  47. // How much are we sticking on each page?
  48. $context['messages_per_page'] = empty($modSettings['disableCustomPerPage']) && !empty($options['messages_per_page']) && !WIRELESS ? $options['messages_per_page'] : $modSettings['defaultMaxMessages'];
  49. // Let's do some work on what to search index.
  50. if (count($_GET) > 2)
  51. foreach ($_GET as $k => $v)
  52. {
  53. if (!in_array($k, array('topic', 'board', 'start', session_name())))
  54. $context['robot_no_index'] = true;
  55. }
  56. if (!empty($_REQUEST['start']) && (!is_numeric($_REQUEST['start']) || $_REQUEST['start'] % $context['messages_per_page'] != 0))
  57. $context['robot_no_index'] = true;
  58. // Find the previous or next topic. Make a fuss if there are no more.
  59. if (isset($_REQUEST['prev_next']) && ($_REQUEST['prev_next'] == 'prev' || $_REQUEST['prev_next'] == 'next'))
  60. {
  61. // No use in calculating the next topic if there's only one.
  62. if ($board_info['num_topics'] > 1)
  63. {
  64. // Just prepare some variables that are used in the query.
  65. $gt_lt = $_REQUEST['prev_next'] == 'prev' ? '>' : '<';
  66. $order = $_REQUEST['prev_next'] == 'prev' ? '' : ' DESC';
  67. $request = $smcFunc['db_query']('', '
  68. SELECT t2.id_topic
  69. FROM {db_prefix}topics AS t
  70. INNER JOIN {db_prefix}topics AS t2 ON (' . (empty($modSettings['enableStickyTopics']) ? '
  71. t2.id_last_msg ' . $gt_lt . ' t.id_last_msg' : '
  72. (t2.id_last_msg ' . $gt_lt . ' t.id_last_msg AND t2.is_sticky ' . $gt_lt . '= t.is_sticky) OR t2.is_sticky ' . $gt_lt . ' t.is_sticky') . ')
  73. WHERE t.id_topic = {int:current_topic}
  74. AND t2.id_board = {int:current_board}' . (!$modSettings['postmod_active'] || allowedTo('approve_posts') ? '' : '
  75. AND (t2.approved = {int:is_approved} OR (t2.id_member_started != {int:id_member_started} AND t2.id_member_started = {int:current_member}))') . '
  76. ORDER BY' . (empty($modSettings['enableStickyTopics']) ? '' : ' t2.is_sticky' . $order . ',') . ' t2.id_last_msg' . $order . '
  77. LIMIT 1',
  78. array(
  79. 'current_board' => $board,
  80. 'current_member' => $user_info['id'],
  81. 'current_topic' => $topic,
  82. 'is_approved' => 1,
  83. 'id_member_started' => 0,
  84. )
  85. );
  86. // No more left.
  87. if ($smcFunc['db_num_rows']($request) == 0)
  88. {
  89. $smcFunc['db_free_result']($request);
  90. // Roll over - if we're going prev, get the last - otherwise the first.
  91. $request = $smcFunc['db_query']('', '
  92. SELECT id_topic
  93. FROM {db_prefix}topics
  94. WHERE id_board = {int:current_board}' . (!$modSettings['postmod_active'] || allowedTo('approve_posts') ? '' : '
  95. AND (approved = {int:is_approved} OR (id_member_started != {int:id_member_started} AND id_member_started = {int:current_member}))') . '
  96. ORDER BY' . (empty($modSettings['enableStickyTopics']) ? '' : ' is_sticky' . $order . ',') . ' id_last_msg' . $order . '
  97. LIMIT 1',
  98. array(
  99. 'current_board' => $board,
  100. 'current_member' => $user_info['id'],
  101. 'is_approved' => 1,
  102. 'id_member_started' => 0,
  103. )
  104. );
  105. }
  106. // Now you can be sure $topic is the id_topic to view.
  107. list ($topic) = $smcFunc['db_fetch_row']($request);
  108. $smcFunc['db_free_result']($request);
  109. $context['current_topic'] = $topic;
  110. }
  111. // Go to the newest message on this topic.
  112. $_REQUEST['start'] = 'new';
  113. }
  114. // Add 1 to the number of views of this topic (except for robots).
  115. if (!$user_info['possibly_robot'] && (empty($_SESSION['last_read_topic']) || $_SESSION['last_read_topic'] != $topic))
  116. {
  117. $smcFunc['db_query']('', '
  118. UPDATE {db_prefix}topics
  119. SET num_views = num_views + 1
  120. WHERE id_topic = {int:current_topic}',
  121. array(
  122. 'current_topic' => $topic,
  123. )
  124. );
  125. $_SESSION['last_read_topic'] = $topic;
  126. }
  127. $topic_parameters = array(
  128. 'current_member' => $user_info['id'],
  129. 'current_topic' => $topic,
  130. 'current_board' => $board,
  131. );
  132. $topic_selects = array();
  133. $topic_tables = array();
  134. call_integration_hook('integrate_display_topic', array($topic_selects, $topic_tables, $topic_parameters));
  135. // @todo Why isn't this cached?
  136. // @todo if we get id_board in this query and cache it, we can save a query on posting
  137. // Get all the important topic info.
  138. $request = $smcFunc['db_query']('', '
  139. SELECT
  140. t.num_replies, t.num_views, t.locked, ms.subject, t.is_sticky, t.id_poll,
  141. t.id_member_started, t.id_first_msg, t.id_last_msg, t.approved, t.unapproved_posts, t.id_redirect_topic,
  142. ' . ($user_info['is_guest'] ? 't.id_last_msg + 1' : 'IFNULL(lt.id_msg, IFNULL(lmr.id_msg, -1)) + 1') . ' AS new_from
  143. ' . (!empty($modSettings['recycle_board']) && $modSettings['recycle_board'] == $board ? ', id_previous_board, id_previous_topic' : '') . '
  144. ' . (!empty($topic_selects) ? implode(',', $topic_selects) : '') . '
  145. FROM {db_prefix}topics AS t
  146. INNER JOIN {db_prefix}messages AS ms ON (ms.id_msg = t.id_first_msg)' . ($user_info['is_guest'] ? '' : '
  147. LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = {int:current_topic} AND lt.id_member = {int:current_member})
  148. LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = {int:current_board} AND lmr.id_member = {int:current_member})') . '
  149. ' . (!empty($topic_tables) ? implode("\n\t", $topic_tables) : '') . '
  150. WHERE t.id_topic = {int:current_topic}
  151. LIMIT 1',
  152. $topic_parameters
  153. );
  154. if ($smcFunc['db_num_rows']($request) == 0)
  155. fatal_lang_error('not_a_topic', false);
  156. $topicinfo = $smcFunc['db_fetch_assoc']($request);
  157. $smcFunc['db_free_result']($request);
  158. // Is this a moved topic that we are redirecting to?
  159. if (!empty($topicinfo['id_redirect_topic']))
  160. redirectexit('topic=' . $topicinfo['id_redirect_topic'] . '.0');
  161. $context['real_num_replies'] = $context['num_replies'] = $topicinfo['num_replies'];
  162. $context['topic_first_message'] = $topicinfo['id_first_msg'];
  163. $context['topic_last_message'] = $topicinfo['id_last_msg'];
  164. // Add up unapproved replies to get real number of replies...
  165. if ($modSettings['postmod_active'] && allowedTo('approve_posts'))
  166. $context['real_num_replies'] += $topicinfo['unapproved_posts'] - ($topicinfo['approved'] ? 0 : 1);
  167. // If this topic has unapproved posts, we need to work out how many posts the user can see, for page indexing.
  168. if ($modSettings['postmod_active'] && $topicinfo['unapproved_posts'] && !$user_info['is_guest'] && !allowedTo('approve_posts'))
  169. {
  170. $request = $smcFunc['db_query']('', '
  171. SELECT COUNT(id_member) AS my_unapproved_posts
  172. FROM {db_prefix}messages
  173. WHERE id_topic = {int:current_topic}
  174. AND id_member = {int:current_member}
  175. AND approved = 0',
  176. array(
  177. 'current_topic' => $topic,
  178. 'current_member' => $user_info['id'],
  179. )
  180. );
  181. list ($myUnapprovedPosts) = $smcFunc['db_fetch_row']($request);
  182. $smcFunc['db_free_result']($request);
  183. $context['total_visible_posts'] = $context['num_replies'] + $myUnapprovedPosts + ($topicinfo['approved'] ? 1 : 0);
  184. }
  185. elseif ($user_info['is_guest'])
  186. $context['total_visible_posts'] = $context['num_replies'] + ($topicinfo['approved'] ? 1 : 0);
  187. else
  188. $context['total_visible_posts'] = $context['num_replies'] + $topicinfo['unapproved_posts'] + ($topicinfo['approved'] ? 1 : 0);
  189. // When was the last time this topic was replied to? Should we warn them about it?
  190. $request = $smcFunc['db_query']('', '
  191. SELECT poster_time
  192. FROM {db_prefix}messages
  193. WHERE id_msg = {int:id_last_msg}
  194. LIMIT 1',
  195. array(
  196. 'id_last_msg' => $topicinfo['id_last_msg'],
  197. )
  198. );
  199. list ($lastPostTime) = $smcFunc['db_fetch_row']($request);
  200. $smcFunc['db_free_result']($request);
  201. $context['oldTopicError'] = !empty($modSettings['oldTopicDays']) && $lastPostTime + $modSettings['oldTopicDays'] * 86400 < time() && empty($topicinfo['is_sticky']);
  202. // The start isn't a number; it's information about what to do, where to go.
  203. if (!is_numeric($_REQUEST['start']))
  204. {
  205. // Redirect to the page and post with new messages, originally by Omar Bazavilvazo.
  206. if ($_REQUEST['start'] == 'new')
  207. {
  208. // Guests automatically go to the last post.
  209. if ($user_info['is_guest'])
  210. {
  211. $context['start_from'] = $context['total_visible_posts'] - 1;
  212. $_REQUEST['start'] = empty($options['view_newest_first']) ? $context['start_from'] : 0;
  213. }
  214. else
  215. {
  216. // Find the earliest unread message in the topic. (the use of topics here is just for both tables.)
  217. $request = $smcFunc['db_query']('', '
  218. SELECT IFNULL(lt.id_msg, IFNULL(lmr.id_msg, -1)) + 1 AS new_from
  219. FROM {db_prefix}topics AS t
  220. LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = {int:current_topic} AND lt.id_member = {int:current_member})
  221. LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = {int:current_board} AND lmr.id_member = {int:current_member})
  222. WHERE t.id_topic = {int:current_topic}
  223. LIMIT 1',
  224. array(
  225. 'current_board' => $board,
  226. 'current_member' => $user_info['id'],
  227. 'current_topic' => $topic,
  228. )
  229. );
  230. list ($new_from) = $smcFunc['db_fetch_row']($request);
  231. $smcFunc['db_free_result']($request);
  232. // Fall through to the next if statement.
  233. $_REQUEST['start'] = 'msg' . $new_from;
  234. }
  235. }
  236. // Start from a certain time index, not a message.
  237. if (substr($_REQUEST['start'], 0, 4) == 'from')
  238. {
  239. $timestamp = (int) substr($_REQUEST['start'], 4);
  240. if ($timestamp === 0)
  241. $_REQUEST['start'] = 0;
  242. else
  243. {
  244. // Find the number of messages posted before said time...
  245. $request = $smcFunc['db_query']('', '
  246. SELECT COUNT(*)
  247. FROM {db_prefix}messages
  248. WHERE poster_time < {int:timestamp}
  249. AND id_topic = {int:current_topic}' . ($modSettings['postmod_active'] && $topicinfo['unapproved_posts'] && !allowedTo('approve_posts') ? '
  250. AND (approved = {int:is_approved}' . ($user_info['is_guest'] ? '' : ' OR id_member = {int:current_member}') . ')' : ''),
  251. array(
  252. 'current_topic' => $topic,
  253. 'current_member' => $user_info['id'],
  254. 'is_approved' => 1,
  255. 'timestamp' => $timestamp,
  256. )
  257. );
  258. list ($context['start_from']) = $smcFunc['db_fetch_row']($request);
  259. $smcFunc['db_free_result']($request);
  260. // Handle view_newest_first options, and get the correct start value.
  261. $_REQUEST['start'] = empty($options['view_newest_first']) ? $context['start_from'] : $context['total_visible_posts'] - $context['start_from'] - 1;
  262. }
  263. }
  264. // Link to a message...
  265. elseif (substr($_REQUEST['start'], 0, 3) == 'msg')
  266. {
  267. $virtual_msg = (int) substr($_REQUEST['start'], 3);
  268. if (!$topicinfo['unapproved_posts'] && $virtual_msg >= $topicinfo['id_last_msg'])
  269. $context['start_from'] = $context['total_visible_posts'] - 1;
  270. elseif (!$topicinfo['unapproved_posts'] && $virtual_msg <= $topicinfo['id_first_msg'])
  271. $context['start_from'] = 0;
  272. else
  273. {
  274. // Find the start value for that message......
  275. $request = $smcFunc['db_query']('', '
  276. SELECT COUNT(*)
  277. FROM {db_prefix}messages
  278. WHERE id_msg < {int:virtual_msg}
  279. AND id_topic = {int:current_topic}' . ($modSettings['postmod_active'] && $topicinfo['unapproved_posts'] && !allowedTo('approve_posts') ? '
  280. AND (approved = {int:is_approved}' . ($user_info['is_guest'] ? '' : ' OR id_member = {int:current_member}') . ')' : ''),
  281. array(
  282. 'current_member' => $user_info['id'],
  283. 'current_topic' => $topic,
  284. 'virtual_msg' => $virtual_msg,
  285. 'is_approved' => 1,
  286. 'no_member' => 0,
  287. )
  288. );
  289. list ($context['start_from']) = $smcFunc['db_fetch_row']($request);
  290. $smcFunc['db_free_result']($request);
  291. }
  292. // We need to reverse the start as well in this case.
  293. $_REQUEST['start'] = empty($options['view_newest_first']) ? $context['start_from'] : $context['total_visible_posts'] - $context['start_from'] - 1;
  294. }
  295. }
  296. // Create a previous next string if the selected theme has it as a selected option.
  297. $context['previous_next'] = $modSettings['enablePreviousNext'] ? '<a href="' . $scripturl . '?topic=' . $topic . '.0;prev_next=prev#new">' . $txt['previous_next_back'] . '</a> - <a href="' . $scripturl . '?topic=' . $topic . '.0;prev_next=next#new">' . $txt['previous_next_forward'] . '</a>' : '';
  298. // Check if spellchecking is both enabled and actually working. (for quick reply.)
  299. $context['show_spellchecking'] = !empty($modSettings['enableSpellChecking']) && function_exists('pspell_new');
  300. // Do we need to show the visual verification image?
  301. $context['require_verification'] = !$user_info['is_mod'] && !$user_info['is_admin'] && !empty($modSettings['posts_require_captcha']) && ($user_info['posts'] < $modSettings['posts_require_captcha'] || ($user_info['is_guest'] && $modSettings['posts_require_captcha'] == -1));
  302. if ($context['require_verification'])
  303. {
  304. require_once($sourcedir . '/Subs-Editor.php');
  305. $verificationOptions = array(
  306. 'id' => 'post',
  307. );
  308. $context['require_verification'] = create_control_verification($verificationOptions);
  309. $context['visual_verification_id'] = $verificationOptions['id'];
  310. }
  311. // Are we showing signatures - or disabled fields?
  312. $context['signature_enabled'] = substr($modSettings['signature_settings'], 0, 1) == 1;
  313. $context['disabled_fields'] = isset($modSettings['disabled_profile_fields']) ? array_flip(explode(',', $modSettings['disabled_profile_fields'])) : array();
  314. // Censor the title...
  315. censorText($topicinfo['subject']);
  316. $context['page_title'] = $topicinfo['subject'];
  317. // Is this topic sticky, or can it even be?
  318. $topicinfo['is_sticky'] = empty($modSettings['enableStickyTopics']) ? '0' : $topicinfo['is_sticky'];
  319. // Default this topic to not marked for notifications... of course...
  320. $context['is_marked_notify'] = false;
  321. // Did we report a post to a moderator just now?
  322. $context['report_sent'] = isset($_GET['reportsent']);
  323. // Let's get nosey, who is viewing this topic?
  324. if (!empty($settings['display_who_viewing']))
  325. {
  326. // Start out with no one at all viewing it.
  327. $context['view_members'] = array();
  328. $context['view_members_list'] = array();
  329. $context['view_num_hidden'] = 0;
  330. // Search for members who have this topic set in their GET data.
  331. $request = $smcFunc['db_query']('', '
  332. SELECT
  333. lo.id_member, lo.log_time, mem.real_name, mem.member_name, mem.show_online,
  334. mg.online_color, mg.id_group, mg.group_name
  335. FROM {db_prefix}log_online AS lo
  336. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = lo.id_member)
  337. LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = CASE WHEN mem.id_group = {int:reg_id_group} THEN mem.id_post_group ELSE mem.id_group END)
  338. WHERE INSTR(lo.url, {string:in_url_string}) > 0 OR lo.session = {string:session}',
  339. array(
  340. 'reg_id_group' => 0,
  341. 'in_url_string' => 's:5:"topic";i:' . $topic . ';',
  342. 'session' => $user_info['is_guest'] ? 'ip' . $user_info['ip'] : session_id(),
  343. )
  344. );
  345. while ($row = $smcFunc['db_fetch_assoc']($request))
  346. {
  347. if (empty($row['id_member']))
  348. continue;
  349. if (!empty($row['online_color']))
  350. $link = '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '" style="color: ' . $row['online_color'] . ';">' . $row['real_name'] . '</a>';
  351. else
  352. $link = '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>';
  353. $is_buddy = in_array($row['id_member'], $user_info['buddies']);
  354. if ($is_buddy)
  355. $link = '<strong>' . $link . '</strong>';
  356. // Add them both to the list and to the more detailed list.
  357. if (!empty($row['show_online']) || allowedTo('moderate_forum'))
  358. $context['view_members_list'][$row['log_time'] . $row['member_name']] = empty($row['show_online']) ? '<em>' . $link . '</em>' : $link;
  359. $context['view_members'][$row['log_time'] . $row['member_name']] = array(
  360. 'id' => $row['id_member'],
  361. 'username' => $row['member_name'],
  362. 'name' => $row['real_name'],
  363. 'group' => $row['id_group'],
  364. 'href' => $scripturl . '?action=profile;u=' . $row['id_member'],
  365. 'link' => $link,
  366. 'is_buddy' => $is_buddy,
  367. 'hidden' => empty($row['show_online']),
  368. );
  369. if (empty($row['show_online']))
  370. $context['view_num_hidden']++;
  371. }
  372. // The number of guests is equal to the rows minus the ones we actually used ;).
  373. $context['view_num_guests'] = $smcFunc['db_num_rows']($request) - count($context['view_members']);
  374. $smcFunc['db_free_result']($request);
  375. // Sort the list.
  376. krsort($context['view_members']);
  377. krsort($context['view_members_list']);
  378. }
  379. // If all is set, but not allowed... just unset it.
  380. $can_show_all = !empty($modSettings['enableAllMessages']) && $context['total_visible_posts'] > $context['messages_per_page'] && $context['total_visible_posts'] < $modSettings['enableAllMessages'];
  381. if (isset($_REQUEST['all']) && !$can_show_all)
  382. unset($_REQUEST['all']);
  383. // Otherwise, it must be allowed... so pretend start was -1.
  384. elseif (isset($_REQUEST['all']))
  385. $_REQUEST['start'] = -1;
  386. // Construct the page index, allowing for the .START method...
  387. $context['page_index'] = constructPageIndex($scripturl . '?topic=' . $topic . '.%1$d', $_REQUEST['start'], $context['total_visible_posts'], $context['messages_per_page'], true);
  388. $context['start'] = $_REQUEST['start'];
  389. // This is information about which page is current, and which page we're on - in case you don't like the constructed page index. (again, wireles..)
  390. $context['page_info'] = array(
  391. 'current_page' => $_REQUEST['start'] / $context['messages_per_page'] + 1,
  392. 'num_pages' => floor(($context['total_visible_posts'] - 1) / $context['messages_per_page']) + 1,
  393. );
  394. // Figure out all the link to the next/prev/first/last/etc. for wireless mainly.
  395. $context['links'] = array(
  396. 'first' => $_REQUEST['start'] >= $context['messages_per_page'] ? $scripturl . '?topic=' . $topic . '.0' : '',
  397. 'prev' => $_REQUEST['start'] >= $context['messages_per_page'] ? $scripturl . '?topic=' . $topic . '.' . ($_REQUEST['start'] - $context['messages_per_page']) : '',
  398. 'next' => $_REQUEST['start'] + $context['messages_per_page'] < $context['total_visible_posts'] ? $scripturl . '?topic=' . $topic. '.' . ($_REQUEST['start'] + $context['messages_per_page']) : '',
  399. 'last' => $_REQUEST['start'] + $context['messages_per_page'] < $context['total_visible_posts'] ? $scripturl . '?topic=' . $topic. '.' . (floor($context['total_visible_posts'] / $context['messages_per_page']) * $context['messages_per_page']) : '',
  400. 'up' => $scripturl . '?board=' . $board . '.0'
  401. );
  402. // If they are viewing all the posts, show all the posts, otherwise limit the number.
  403. if ($can_show_all)
  404. {
  405. if (isset($_REQUEST['all']))
  406. {
  407. // No limit! (actually, there is a limit, but...)
  408. $context['messages_per_page'] = -1;
  409. $context['page_index'] .= empty($modSettings['compactTopicPagesEnable']) ? '<strong>' . $txt['all'] . '</strong> ' : '[<strong>' . $txt['all'] . '</strong>] ';
  410. // Set start back to 0...
  411. $_REQUEST['start'] = 0;
  412. }
  413. // They aren't using it, but the *option* is there, at least.
  414. else
  415. $context['page_index'] .= '&nbsp;<a href="' . $scripturl . '?topic=' . $topic . '.0;all">' . $txt['all'] . '</a> ';
  416. }
  417. // Build the link tree.
  418. $context['linktree'][] = array(
  419. 'url' => $scripturl . '?topic=' . $topic . '.0',
  420. 'name' => $topicinfo['subject'],
  421. );
  422. // Build a list of this board's moderators.
  423. $context['moderators'] = &$board_info['moderators'];
  424. $context['link_moderators'] = array();
  425. if (!empty($board_info['moderators']))
  426. {
  427. // Add a link for each moderator...
  428. foreach ($board_info['moderators'] as $mod)
  429. $context['link_moderators'][] = '<a href="' . $scripturl . '?action=profile;u=' . $mod['id'] . '" title="' . $txt['board_moderator'] . '">' . $mod['name'] . '</a>';
  430. // And show it after the board's name.
  431. $context['linktree'][count($context['linktree']) - 2]['extra_after'] = '<span class="board_moderators"> (' . (count($context['link_moderators']) == 1 ? $txt['moderator'] : $txt['moderators']) . ': ' . implode(', ', $context['link_moderators']) . ')</span>';
  432. }
  433. // Information about the current topic...
  434. $context['is_locked'] = $topicinfo['locked'];
  435. $context['is_sticky'] = $topicinfo['is_sticky'];
  436. $context['is_very_hot'] = $topicinfo['num_replies'] >= $modSettings['hotTopicVeryPosts'];
  437. $context['is_hot'] = $topicinfo['num_replies'] >= $modSettings['hotTopicPosts'];
  438. $context['is_approved'] = $topicinfo['approved'];
  439. // @todo Tricks? We don't want to show the poll icon in the topic class here, so pretend it's not one.
  440. $context['is_poll'] = false;
  441. determineTopicClass($context);
  442. $context['is_poll'] = $topicinfo['id_poll'] > 0 && $modSettings['pollMode'] == '1' && allowedTo('poll_view');
  443. // Did this user start the topic or not?
  444. $context['user']['started'] = $user_info['id'] == $topicinfo['id_member_started'] && !$user_info['is_guest'];
  445. $context['topic_starter_id'] = $topicinfo['id_member_started'];
  446. // Set the topic's information for the template.
  447. $context['subject'] = $topicinfo['subject'];
  448. $context['num_views'] = $topicinfo['num_views'];
  449. $context['num_views_text'] = $context['num_views'] == 1 ? $txt['read_one_time'] : sprintf($txt['read_many_times'], $context['num_views']);
  450. $context['mark_unread_time'] = !empty($virtual_msg) ? $virtual_msg : $topicinfo['new_from'];
  451. // Set a canonical URL for this page.
  452. $context['canonical_url'] = $scripturl . '?topic=' . $topic . '.' . $context['start'];
  453. // For quick reply we need a response prefix in the default forum language.
  454. if (!isset($context['response_prefix']) && !($context['response_prefix'] = cache_get_data('response_prefix', 600)))
  455. {
  456. if ($language === $user_info['language'])
  457. $context['response_prefix'] = $txt['response_prefix'];
  458. else
  459. {
  460. loadLanguage('index', $language, false);
  461. $context['response_prefix'] = $txt['response_prefix'];
  462. loadLanguage('index');
  463. }
  464. cache_put_data('response_prefix', $context['response_prefix'], 600);
  465. }
  466. // If we want to show event information in the topic, prepare the data.
  467. if (allowedTo('calendar_view') && !empty($modSettings['cal_showInTopic']) && !empty($modSettings['cal_enabled']))
  468. {
  469. // First, try create a better time format, ignoring the "time" elements.
  470. if (preg_match('~%[AaBbCcDdeGghjmuYy](?:[^%]*%[AaBbCcDdeGghjmuYy])*~', $user_info['time_format'], $matches) == 0 || empty($matches[0]))
  471. $date_string = $user_info['time_format'];
  472. else
  473. $date_string = $matches[0];
  474. // Any calendar information for this topic?
  475. $request = $smcFunc['db_query']('', '
  476. SELECT cal.id_event, cal.start_date, cal.end_date, cal.title, cal.id_member, mem.real_name
  477. FROM {db_prefix}calendar AS cal
  478. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = cal.id_member)
  479. WHERE cal.id_topic = {int:current_topic}
  480. ORDER BY start_date',
  481. array(
  482. 'current_topic' => $topic,
  483. )
  484. );
  485. $context['linked_calendar_events'] = array();
  486. while ($row = $smcFunc['db_fetch_assoc']($request))
  487. {
  488. // Prepare the dates for being formatted.
  489. $start_date = sscanf($row['start_date'], '%04d-%02d-%02d');
  490. $start_date = mktime(12, 0, 0, $start_date[1], $start_date[2], $start_date[0]);
  491. $end_date = sscanf($row['end_date'], '%04d-%02d-%02d');
  492. $end_date = mktime(12, 0, 0, $end_date[1], $end_date[2], $end_date[0]);
  493. $context['linked_calendar_events'][] = array(
  494. 'id' => $row['id_event'],
  495. 'title' => $row['title'],
  496. 'can_edit' => allowedTo('calendar_edit_any') || ($row['id_member'] == $user_info['id'] && allowedTo('calendar_edit_own')),
  497. 'modify_href' => $scripturl . '?action=post;msg=' . $topicinfo['id_first_msg'] . ';topic=' . $topic . '.0;calendar;eventid=' . $row['id_event'] . ';' . $context['session_var'] . '=' . $context['session_id'],
  498. 'can_export' => allowedTo('calendar_edit_any') || ($row['id_member'] == $user_info['id'] && allowedTo('calendar_edit_own')),
  499. 'export_href' => $scripturl . '?action=calendar;sa=ical;eventid=' . $row['id_event'] . ';' . $context['session_var'] . '=' . $context['session_id'],
  500. 'start_date' => timeformat($start_date, $date_string, 'none'),
  501. 'start_timestamp' => $start_date,
  502. 'end_date' => timeformat($end_date, $date_string, 'none'),
  503. 'end_timestamp' => $end_date,
  504. 'is_last' => false
  505. );
  506. }
  507. $smcFunc['db_free_result']($request);
  508. if (!empty($context['linked_calendar_events']))
  509. $context['linked_calendar_events'][count($context['linked_calendar_events']) - 1]['is_last'] = true;
  510. }
  511. // Create the poll info if it exists.
  512. if ($context['is_poll'])
  513. {
  514. // Get the question and if it's locked.
  515. $request = $smcFunc['db_query']('', '
  516. SELECT
  517. p.question, p.voting_locked, p.hide_results, p.expire_time, p.max_votes, p.change_vote,
  518. p.guest_vote, p.id_member, IFNULL(mem.real_name, p.poster_name) AS poster_name, p.num_guest_voters, p.reset_poll
  519. FROM {db_prefix}polls AS p
  520. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = p.id_member)
  521. WHERE p.id_poll = {int:id_poll}
  522. LIMIT 1',
  523. array(
  524. 'id_poll' => $topicinfo['id_poll'],
  525. )
  526. );
  527. $pollinfo = $smcFunc['db_fetch_assoc']($request);
  528. $smcFunc['db_free_result']($request);
  529. $request = $smcFunc['db_query']('', '
  530. SELECT COUNT(DISTINCT id_member) AS total
  531. FROM {db_prefix}log_polls
  532. WHERE id_poll = {int:id_poll}
  533. AND id_member != {int:not_guest}',
  534. array(
  535. 'id_poll' => $topicinfo['id_poll'],
  536. 'not_guest' => 0,
  537. )
  538. );
  539. list ($pollinfo['total']) = $smcFunc['db_fetch_row']($request);
  540. $smcFunc['db_free_result']($request);
  541. // Total voters needs to include guest voters
  542. $pollinfo['total'] += $pollinfo['num_guest_voters'];
  543. // Get all the options, and calculate the total votes.
  544. $request = $smcFunc['db_query']('', '
  545. SELECT pc.id_choice, pc.label, pc.votes, IFNULL(lp.id_choice, -1) AS voted_this
  546. FROM {db_prefix}poll_choices AS pc
  547. LEFT JOIN {db_prefix}log_polls AS lp ON (lp.id_choice = pc.id_choice AND lp.id_poll = {int:id_poll} AND lp.id_member = {int:current_member} AND lp.id_member != {int:not_guest})
  548. WHERE pc.id_poll = {int:id_poll}',
  549. array(
  550. 'current_member' => $user_info['id'],
  551. 'id_poll' => $topicinfo['id_poll'],
  552. 'not_guest' => 0,
  553. )
  554. );
  555. $pollOptions = array();
  556. $realtotal = 0;
  557. $pollinfo['has_voted'] = false;
  558. while ($row = $smcFunc['db_fetch_assoc']($request))
  559. {
  560. censorText($row['label']);
  561. $pollOptions[$row['id_choice']] = $row;
  562. $realtotal += $row['votes'];
  563. $pollinfo['has_voted'] |= $row['voted_this'] != -1;
  564. }
  565. $smcFunc['db_free_result']($request);
  566. // If this is a guest we need to do our best to work out if they have voted, and what they voted for.
  567. if ($user_info['is_guest'] && $pollinfo['guest_vote'] && allowedTo('poll_vote'))
  568. {
  569. if (!empty($_COOKIE['guest_poll_vote']) && preg_match('~^[0-9,;]+$~', $_COOKIE['guest_poll_vote']) && strpos($_COOKIE['guest_poll_vote'], ';' . $topicinfo['id_poll'] . ',') !== false)
  570. {
  571. // ;id,timestamp,[vote,vote...]; etc
  572. $guestinfo = explode(';', $_COOKIE['guest_poll_vote']);
  573. // Find the poll we're after.
  574. foreach ($guestinfo as $i => $guestvoted)
  575. {
  576. $guestvoted = explode(',', $guestvoted);
  577. if ($guestvoted[0] == $topicinfo['id_poll'])
  578. break;
  579. }
  580. // Has the poll been reset since guest voted?
  581. if ($pollinfo['reset_poll'] > $guestvoted[1])
  582. {
  583. // Remove the poll info from the cookie to allow guest to vote again
  584. unset($guestinfo[$i]);
  585. if (!empty($guestinfo))
  586. $_COOKIE['guest_poll_vote'] = ';' . implode(';', $guestinfo);
  587. else
  588. unset($_COOKIE['guest_poll_vote']);
  589. }
  590. else
  591. {
  592. // What did they vote for?
  593. unset($guestvoted[0], $guestvoted[1]);
  594. foreach ($pollOptions as $choice => $details)
  595. {
  596. $pollOptions[$choice]['voted_this'] = in_array($choice, $guestvoted) ? 1 : -1;
  597. $pollinfo['has_voted'] |= $pollOptions[$choice]['voted_this'] != -1;
  598. }
  599. unset($choice, $details, $guestvoted);
  600. }
  601. unset($guestinfo, $guestvoted, $i);
  602. }
  603. }
  604. // Set up the basic poll information.
  605. $context['poll'] = array(
  606. 'id' => $topicinfo['id_poll'],
  607. 'image' => 'normal_' . (empty($pollinfo['voting_locked']) ? 'poll' : 'locked_poll'),
  608. 'question' => parse_bbc($pollinfo['question']),
  609. 'total_votes' => $pollinfo['total'],
  610. 'change_vote' => !empty($pollinfo['change_vote']),
  611. 'is_locked' => !empty($pollinfo['voting_locked']),
  612. 'options' => array(),
  613. 'lock' => allowedTo('poll_lock_any') || ($context['user']['started'] && allowedTo('poll_lock_own')),
  614. 'edit' => allowedTo('poll_edit_any') || ($context['user']['started'] && allowedTo('poll_edit_own')),
  615. 'allowed_warning' => $pollinfo['max_votes'] > 1 ? sprintf($txt['poll_options6'], min(count($pollOptions), $pollinfo['max_votes'])) : '',
  616. 'is_expired' => !empty($pollinfo['expire_time']) && $pollinfo['expire_time'] < time(),
  617. 'expire_time' => !empty($pollinfo['expire_time']) ? timeformat($pollinfo['expire_time']) : 0,
  618. 'has_voted' => !empty($pollinfo['has_voted']),
  619. 'starter' => array(
  620. 'id' => $pollinfo['id_member'],
  621. 'name' => $row['poster_name'],
  622. 'href' => $pollinfo['id_member'] == 0 ? '' : $scripturl . '?action=profile;u=' . $pollinfo['id_member'],
  623. 'link' => $pollinfo['id_member'] == 0 ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $pollinfo['id_member'] . '">' . $row['poster_name'] . '</a>'
  624. )
  625. );
  626. // Make the lock and edit permissions defined above more directly accessible.
  627. $context['allow_lock_poll'] = $context['poll']['lock'];
  628. $context['allow_edit_poll'] = $context['poll']['edit'];
  629. // You're allowed to vote if:
  630. // 1. the poll did not expire, and
  631. // 2. you're either not a guest OR guest voting is enabled... and
  632. // 3. you're not trying to view the results, and
  633. // 4. the poll is not locked, and
  634. // 5. you have the proper permissions, and
  635. // 6. you haven't already voted before.
  636. $context['allow_vote'] = !$context['poll']['is_expired'] && (!$user_info['is_guest'] || ($pollinfo['guest_vote'] && allowedTo('poll_vote'))) && empty($pollinfo['voting_locked']) && allowedTo('poll_vote') && !$context['poll']['has_voted'];
  637. // You're allowed to view the results if:
  638. // 1. you're just a super-nice-guy, or
  639. // 2. anyone can see them (hide_results == 0), or
  640. // 3. you can see them after you voted (hide_results == 1), or
  641. // 4. you've waited long enough for the poll to expire. (whether hide_results is 1 or 2.)
  642. $context['allow_poll_view'] = allowedTo('moderate_board') || $pollinfo['hide_results'] == 0 || ($pollinfo['hide_results'] == 1 && $context['poll']['has_voted']) || $context['poll']['is_expired'];
  643. $context['poll']['show_results'] = $context['allow_poll_view'] && (isset($_REQUEST['viewresults']) || isset($_REQUEST['viewResults']));
  644. $context['show_view_results_button'] = $context['allow_vote'] && (!$context['allow_poll_view'] || !$context['poll']['show_results'] || !$context['poll']['has_voted']);
  645. // You're allowed to change your vote if:
  646. // 1. the poll did not expire, and
  647. // 2. you're not a guest... and
  648. // 3. the poll is not locked, and
  649. // 4. you have the proper permissions, and
  650. // 5. you have already voted, and
  651. // 6. the poll creator has said you can!
  652. $context['allow_change_vote'] = !$context['poll']['is_expired'] && !$user_info['is_guest'] && empty($pollinfo['voting_locked']) && allowedTo('poll_vote') && $context['poll']['has_voted'] && $context['poll']['change_vote'];
  653. // You're allowed to return to voting options if:
  654. // 1. you are (still) allowed to vote.
  655. // 2. you are currently seeing the results.
  656. $context['allow_return_vote'] = $context['allow_vote'] && $context['poll']['show_results'];
  657. // Calculate the percentages and bar lengths...
  658. $divisor = $realtotal == 0 ? 1 : $realtotal;
  659. // Determine if a decimal point is needed in order for the options to add to 100%.
  660. $precision = $realtotal == 100 ? 0 : 1;
  661. // Now look through each option, and...
  662. foreach ($pollOptions as $i => $option)
  663. {
  664. // First calculate the percentage, and then the width of the bar...
  665. $bar = round(($option['votes'] * 100) / $divisor, $precision);
  666. $barWide = $bar == 0 ? 1 : floor(($bar * 8) / 3);
  667. // Now add it to the poll's contextual theme data.
  668. $context['poll']['options'][$i] = array(
  669. 'id' => 'options-' . $i,
  670. 'percent' => $bar,
  671. 'votes' => $option['votes'],
  672. 'voted_this' => $option['voted_this'] != -1,
  673. 'bar' => '<span style="white-space: nowrap;"><img src="' . $settings['images_url'] . '/poll_' . ($context['right_to_left'] ? 'right' : 'left') . '.png" alt="" /><img src="' . $settings['images_url'] . '/poll_middle.png" width="' . $barWide . '" height="12" alt="-" /><img src="' . $settings['images_url'] . '/poll_' . ($context['right_to_left'] ? 'left' : 'right') . '.png" alt="" /></span>',
  674. // Note: IE < 8 requires us to set a width on the container, too.
  675. 'bar_ndt' => $bar > 0 ? '<div class="bar" style="width: ' . ($bar * 3.5 + 4) . 'px;"><div style="width: ' . $bar * 3.5 . 'px;"></div></div>' : '',
  676. 'bar_width' => $barWide,
  677. 'option' => parse_bbc($option['label']),
  678. 'vote_button' => '<input type="' . ($pollinfo['max_votes'] > 1 ? 'checkbox' : 'radio') . '" name="options[]" id="options-' . $i . '" value="' . $i . '" class="input_' . ($pollinfo['max_votes'] > 1 ? 'check' : 'radio') . '" />'
  679. );
  680. }
  681. // Build the poll moderation button array.
  682. $context['poll_buttons'] = array(
  683. 'vote' => array('test' => 'allow_return_vote', 'text' => 'poll_return_vote', 'image' => 'poll_options.png', 'lang' => true, 'url' => $scripturl . '?topic=' . $context['current_topic'] . '.' . $context['start']),
  684. 'results' => array('test' => 'show_view_results_button', 'text' => 'poll_results', 'image' => 'poll_results.png', 'lang' => true, 'url' => $scripturl . '?topic=' . $context['current_topic'] . '.' . $context['start'] . ';viewresults'),
  685. 'change_vote' => array('test' => 'allow_change_vote', 'text' => 'poll_change_vote', 'image' => 'poll_change_vote.png', 'lang' => true, 'url' => $scripturl . '?action=vote;topic=' . $context['current_topic'] . '.' . $context['start'] . ';poll=' . $context['poll']['id'] . ';' . $context['session_var'] . '=' . $context['session_id']),
  686. 'lock' => array('test' => 'allow_lock_poll', 'text' => (!$context['poll']['is_locked'] ? 'poll_lock' : 'poll_unlock'), 'image' => 'poll_lock.png', 'lang' => true, 'url' => $scripturl . '?action=lockvoting;topic=' . $context['current_topic'] . '.' . $context['start'] . ';' . $context['session_var'] . '=' . $context['session_id']),
  687. 'edit' => array('test' => 'allow_edit_poll', 'text' => 'poll_edit', 'image' => 'poll_edit.png', 'lang' => true, 'url' => $scripturl . '?action=editpoll;topic=' . $context['current_topic'] . '.' . $context['start']),
  688. 'remove_poll' => array('test' => 'can_remove_poll', 'text' => 'poll_remove', 'image' => 'admin_remove_poll.png', 'lang' => true, 'custom' => 'onclick="return confirm(\'' . $txt['poll_remove_warn'] . '\');"', 'url' => $scripturl . '?action=removepoll;topic=' . $context['current_topic'] . '.' . $context['start'] . ';' . $context['session_var'] . '=' . $context['session_id']),
  689. );
  690. // Allow mods to add additional buttons here
  691. call_integration_hook('integrate_poll_buttons');
  692. }
  693. // Calculate the fastest way to get the messages!
  694. $ascending = empty($options['view_newest_first']);
  695. $start = $_REQUEST['start'];
  696. $limit = $context['messages_per_page'];
  697. $firstIndex = 0;
  698. if ($start >= $context['total_visible_posts'] / 2 && $context['messages_per_page'] != -1)
  699. {
  700. $ascending = !$ascending;
  701. $limit = $context['total_visible_posts'] <= $start + $limit ? $context['total_visible_posts'] - $start : $limit;
  702. $start = $context['total_visible_posts'] <= $start + $limit ? 0 : $context['total_visible_posts'] - $start - $limit;
  703. $firstIndex = $limit - 1;
  704. }
  705. // Get each post and poster in this topic.
  706. $request = $smcFunc['db_query']('display_get_post_poster', '
  707. SELECT id_msg, id_member, approved
  708. FROM {db_prefix}messages
  709. WHERE id_topic = {int:current_topic}' . (!$modSettings['postmod_active'] || allowedTo('approve_posts') ? '' : (!empty($modSettings['db_mysql_group_by_fix']) ? '' : '
  710. GROUP BY id_msg') . '
  711. HAVING (approved = {int:is_approved}' . ($user_info['is_guest'] ? '' : ' OR id_member = {int:current_member}') . ')') . '
  712. ORDER BY id_msg ' . ($ascending ? '' : 'DESC') . ($context['messages_per_page'] == -1 ? '' : '
  713. LIMIT ' . $start . ', ' . $limit),
  714. array(
  715. 'current_member' => $user_info['id'],
  716. 'current_topic' => $topic,
  717. 'is_approved' => 1,
  718. 'blank_id_member' => 0,
  719. )
  720. );
  721. $messages = array();
  722. $all_posters = array();
  723. while ($row = $smcFunc['db_fetch_assoc']($request))
  724. {
  725. if (!empty($row['id_member']))
  726. $all_posters[$row['id_msg']] = $row['id_member'];
  727. $messages[] = $row['id_msg'];
  728. }
  729. $smcFunc['db_free_result']($request);
  730. $posters = array_unique($all_posters);
  731. call_integration_hook('integrate_display_message_list', array($messages, $posters));
  732. // Guests can't mark topics read or for notifications, just can't sorry.
  733. if (!$user_info['is_guest'] && !empty($messages))
  734. {
  735. $mark_at_msg = max($messages);
  736. if ($mark_at_msg >= $topicinfo['id_last_msg'])
  737. $mark_at_msg = $modSettings['maxMsgID'];
  738. if ($mark_at_msg >= $topicinfo['new_from'])
  739. {
  740. $smcFunc['db_insert']($topicinfo['new_from'] == 0 ? 'ignore' : 'replace',
  741. '{db_prefix}log_topics',
  742. array(
  743. 'id_member' => 'int', 'id_topic' => 'int', 'id_msg' => 'int',
  744. ),
  745. array(
  746. $user_info['id'], $topic, $mark_at_msg,
  747. ),
  748. array('id_member', 'id_topic')
  749. );
  750. }
  751. // Check for notifications on this topic OR board.
  752. $request = $smcFunc['db_query']('', '
  753. SELECT sent, id_topic
  754. FROM {db_prefix}log_notify
  755. WHERE (id_topic = {int:current_topic} OR id_board = {int:current_board})
  756. AND id_member = {int:current_member}
  757. LIMIT 2',
  758. array(
  759. 'current_board' => $board,
  760. 'current_member' => $user_info['id'],
  761. 'current_topic' => $topic,
  762. )
  763. );
  764. $do_once = true;
  765. while ($row = $smcFunc['db_fetch_assoc']($request))
  766. {
  767. // Find if this topic is marked for notification...
  768. if (!empty($row['id_topic']))
  769. $context['is_marked_notify'] = true;
  770. // Only do this once, but mark the notifications as "not sent yet" for next time.
  771. if (!empty($row['sent']) && $do_once)
  772. {
  773. $smcFunc['db_query']('', '
  774. UPDATE {db_prefix}log_notify
  775. SET sent = {int:is_not_sent}
  776. WHERE (id_topic = {int:current_topic} OR id_board = {int:current_board})
  777. AND id_member = {int:current_member}',
  778. array(
  779. 'current_board' => $board,
  780. 'current_member' => $user_info['id'],
  781. 'current_topic' => $topic,
  782. 'is_not_sent' => 0,
  783. )
  784. );
  785. $do_once = false;
  786. }
  787. }
  788. // Have we recently cached the number of new topics in this board, and it's still a lot?
  789. if (isset($_REQUEST['topicseen']) && isset($_SESSION['topicseen_cache'][$board]) && $_SESSION['topicseen_cache'][$board] > 5)
  790. $_SESSION['topicseen_cache'][$board]--;
  791. // Mark board as seen if this is the only new topic.
  792. elseif (isset($_REQUEST['topicseen']))
  793. {
  794. // Use the mark read tables... and the last visit to figure out if this should be read or not.
  795. $request = $smcFunc['db_query']('', '
  796. SELECT COUNT(*)
  797. FROM {db_prefix}topics AS t
  798. LEFT JOIN {db_prefix}log_boards AS lb ON (lb.id_board = {int:current_board} AND lb.id_member = {int:current_member})
  799. LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})
  800. WHERE t.id_board = {int:current_board}
  801. AND t.id_last_msg > IFNULL(lb.id_msg, 0)
  802. AND t.id_last_msg > IFNULL(lt.id_msg, 0)' . (empty($_SESSION['id_msg_last_visit']) ? '' : '
  803. AND t.id_last_msg > {int:id_msg_last_visit}'),
  804. array(
  805. 'current_board' => $board,
  806. 'current_member' => $user_info['id'],
  807. 'id_msg_last_visit' => (int) $_SESSION['id_msg_last_visit'],
  808. )
  809. );
  810. list ($numNewTopics) = $smcFunc['db_fetch_row']($request);
  811. $smcFunc['db_free_result']($request);
  812. // If there're no real new topics in this board, mark the board as seen.
  813. if (empty($numNewTopics))
  814. $_REQUEST['boardseen'] = true;
  815. else
  816. $_SESSION['topicseen_cache'][$board] = $numNewTopics;
  817. }
  818. // Probably one less topic - maybe not, but even if we decrease this too fast it will only make us look more often.
  819. elseif (isset($_SESSION['topicseen_cache'][$board]))
  820. $_SESSION['topicseen_cache'][$board]--;
  821. // Mark board as seen if we came using last post link from BoardIndex. (or other places...)
  822. if (isset($_REQUEST['boardseen']))
  823. {
  824. $smcFunc['db_insert']('replace',
  825. '{db_prefix}log_boards',
  826. array('id_msg' => 'int', 'id_member' => 'int', 'id_board' => 'int'),
  827. array($modSettings['maxMsgID'], $user_info['id'], $board),
  828. array('id_member', 'id_board')
  829. );
  830. }
  831. }
  832. $attachments = array();
  833. // If there _are_ messages here... (probably an error otherwise :!)
  834. if (!empty($messages))
  835. {
  836. // Fetch attachments.
  837. if (!empty($modSettings['attachmentEnable']) && allowedTo('view_attachments'))
  838. {
  839. $request = $smcFunc['db_query']('', '
  840. SELECT
  841. a.id_attach, a.id_folder, a.id_msg, a.filename, a.file_hash, IFNULL(a.size, 0) AS filesize, a.downloads, a.approved,
  842. a.width, a.height' . (empty($modSettings['attachmentShowImages']) || empty($modSettings['attachmentThumbnails']) ? '' : ',
  843. IFNULL(thumb.id_attach, 0) AS id_thumb, thumb.width AS thumb_width, thumb.height AS thumb_height') . '
  844. FROM {db_prefix}attachments AS a' . (empty($modSettings['attachmentShowImages']) || empty($modSettings['attachmentThumbnails']) ? '' : '
  845. LEFT JOIN {db_prefix}attachments AS thumb ON (thumb.id_attach = a.id_thumb)') . '
  846. WHERE a.id_msg IN ({array_int:message_list})
  847. AND a.attachment_type = {int:attachment_type}',
  848. array(
  849. 'message_list' => $messages,
  850. 'attachment_type' => 0,
  851. 'is_approved' => 1,
  852. )
  853. );
  854. $temp = array();
  855. while ($row = $smcFunc['db_fetch_assoc']($request))
  856. {
  857. if (!$row['approved'] && $modSettings['postmod_active'] && !allowedTo('approve_posts') && (!isset($all_posters[$row['id_msg']]) || $all_posters[$row['id_msg']] != $user_info['id']))
  858. continue;
  859. $temp[$row['id_attach']] = $row;
  860. if (!isset($attachments[$row['id_msg']]))
  861. $attachments[$row['id_msg']] = array();
  862. }
  863. $smcFunc['db_free_result']($request);
  864. // This is better than sorting it with the query...
  865. ksort($temp);
  866. foreach ($temp as $row)
  867. $attachments[$row['id_msg']][] = $row;
  868. }
  869. $msg_parameters = array(
  870. 'message_list' => $messages,
  871. 'new_from' => $topicinfo['new_from'],
  872. );
  873. $msg_selects = array();
  874. $msg_tables = array();
  875. call_integration_hook('integrate_query_message', array($msg_selects, $msg_tables, $msg_parameters));
  876. // What? It's not like it *couldn't* be only guests in this topic...
  877. if (!empty($posters))
  878. loadMemberData($posters);
  879. $messages_request = $smcFunc['db_query']('', '
  880. SELECT
  881. id_msg, icon, subject, poster_time, poster_ip, id_member, modified_time, modified_name, body,
  882. smileys_enabled, poster_name, poster_email, approved,
  883. id_msg_modified < {int:new_from} AS is_read
  884. ' . (!empty($msg_selects) ? implode(',', $msg_selects) : '') . '
  885. FROM {db_prefix}messages
  886. ' . (!empty($msg_tables) ? implode("\n\t", $msg_tables) : '') . '
  887. WHERE id_msg IN ({array_int:message_list})
  888. ORDER BY id_msg' . (empty($options['view_newest_first']) ? '' : ' DESC'),
  889. $msg_parameters
  890. );
  891. // Go to the last message if the given time is beyond the time of the last message.
  892. if (isset($context['start_from']) && $context['start_from'] >= $topicinfo['num_replies'])
  893. $context['start_from'] = $topicinfo['num_replies'];
  894. // Since the anchor information is needed on the top of the page we load these variables beforehand.
  895. $context['first_message'] = isset($messages[$firstIndex]) ? $messages[$firstIndex] : $messages[0];
  896. if (empty($options['view_newest_first']))
  897. $context['first_new_message'] = isset($context['start_from']) && $_REQUEST['start'] == $context['start_from'];
  898. else
  899. $context['first_new_message'] = isset($context['start_from']) && $_REQUEST['start'] == $topicinfo['num_replies'] - $context['start_from'];
  900. }
  901. else
  902. {
  903. $messages_request = false;
  904. $context['first_message'] = 0;
  905. $context['first_new_message'] = false;
  906. }
  907. $context['jump_to'] = array(
  908. 'label' => addslashes(un_htmlspecialchars($txt['jump_to'])),
  909. 'board_name' => htmlspecialchars(strtr(strip_tags($board_info['name']), array('&amp;' => '&'))),
  910. 'child_level' => $board_info['child_level'],
  911. );
  912. // Set the callback. (do you REALIZE how much memory all the messages would take?!?)
  913. // This will be called from the template.
  914. $context['get_message'] = 'prepareDisplayContext';
  915. // Now set all the wonderful, wonderful permissions... like moderation ones...
  916. $common_permissions = array(
  917. 'can_approve' => 'approve_posts',
  918. 'can_ban' => 'manage_bans',
  919. 'can_sticky' => 'make_sticky',
  920. 'can_merge' => 'merge_any',
  921. 'can_split' => 'split_any',
  922. 'calendar_post' => 'calendar_post',
  923. 'can_mark_notify' => 'mark_any_notify',
  924. 'can_send_topic' => 'send_topic',
  925. 'can_send_pm' => 'pm_send',
  926. 'can_send_email' => 'send_email_to_members',
  927. 'can_report_moderator' => 'report_any',
  928. 'can_moderate_forum' => 'moderate_forum',
  929. 'can_issue_warning' => 'issue_warning',
  930. 'can_restore_topic' => 'move_any',
  931. 'can_restore_msg' => 'move_any',
  932. );
  933. foreach ($common_permissions as $contextual => $perm)
  934. $context[$contextual] = allowedTo($perm);
  935. // Permissions with _any/_own versions. $context[YYY] => ZZZ_any/_own.
  936. $anyown_permissions = array(
  937. 'can_move' => 'move',
  938. 'can_lock' => 'lock',
  939. 'can_delete' => 'remove',
  940. 'can_add_poll' => 'poll_add',
  941. 'can_remove_poll' => 'poll_remove',
  942. 'can_reply' => 'post_reply',
  943. 'can_reply_unapproved' => 'post_unapproved_replies',
  944. );
  945. foreach ($anyown_permissions as $contextual => $perm)
  946. $context[$contextual] = allowedTo($perm . '_any') || ($context['user']['started'] && allowedTo($perm . '_own'));
  947. // Cleanup all the permissions with extra stuff...
  948. $context['can_mark_notify'] &= !$context['user']['is_guest'];
  949. $context['can_sticky'] &= !empty($modSettings['enableStickyTopics']);
  950. $context['calendar_post'] &= !empty($modSettings['cal_enabled']);
  951. $context['can_add_poll'] &= $modSettings['pollMode'] == '1' && $topicinfo['id_poll'] <= 0;
  952. $context['can_remove_poll'] &= $modSettings['pollMode'] == '1' && $topicinfo['id_poll'] > 0;
  953. $context['can_reply'] &= empty($topicinfo['locked']) || allowedTo('moderate_board');
  954. $context['can_reply_unapproved'] &= $modSettings['postmod_active'] && (empty($topicinfo['locked']) || allowedTo('moderate_board'));
  955. $context['can_issue_warning'] &= in_array('w', $context['admin_features']) && $modSettings['warning_settings'][0] == 1;
  956. // Handle approval flags...
  957. $context['can_reply_approved'] = $context['can_reply'];
  958. $context['can_reply'] |= $context['can_reply_unapproved'];
  959. $context['can_quote'] = $context['can_reply'] && (empty($modSettings['disabledBBC']) || !in_array('quote', explode(',', $modSettings['disabledBBC'])));
  960. $context['can_mark_unread'] = !$user_info['is_guest'] && $settings['show_mark_read'];
  961. $context['can_send_topic'] = (!$modSettings['postmod_active'] || $topicinfo['approved']) && allowedTo('send_topic');
  962. $context['can_print'] = empty($modSettings['disable_print_topic']);
  963. // Start this off for quick moderation - it will be or'd for each post.
  964. $context['can_remove_post'] = allowedTo('delete_any') || (allowedTo('delete_replies') && $context['user']['started']);
  965. // Can restore topic? That's if the topic is in the recycle board and has a previous restore state.
  966. $context['can_restore_topic'] &= !empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] == $board && !empty($topicinfo['id_previous_board']);
  967. $context['can_restore_msg'] &= !empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] == $board && !empty($topicinfo['id_previous_topic']);
  968. // Check if the draft functions are enabled and that they have permission to use them (for quick reply.)
  969. $context['drafts_save'] = !empty($modSettings['drafts_enabled']) && !empty($modSettings['drafts_post_enabled']) && allowedTo('post_draft') && $context['can_reply'];
  970. $context['drafts_autosave'] = !empty($context['drafts_save']) && !empty($modSettings['drafts_autosave_enabled']) && allowedTo('post_autosave_draft');
  971. if (!empty($context['drafts_save']))
  972. loadLangu

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