PageRenderTime 72ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/sources/controllers/Recent.controller.php

https://github.com/Arantor/Elkarte
PHP | 1372 lines | 1150 code | 128 blank | 94 comment | 175 complexity | 7ae5bc280858685cc56237f3cd9fb32c MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0

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

  1. <?php
  2. /**
  3. * @name ElkArte Forum
  4. * @copyright ElkArte Forum contributors
  5. * @license BSD http://opensource.org/licenses/BSD-3-Clause
  6. *
  7. * This software is a derived product, based on:
  8. *
  9. * Simple Machines Forum (SMF)
  10. * copyright: 2011 Simple Machines (http://www.simplemachines.org)
  11. * license: BSD, See included LICENSE.TXT for terms and conditions.
  12. *
  13. * @version 1.0 Alpha
  14. *
  15. * Find and retrieve information about recently posted topics, messages, and the like.
  16. *
  17. */
  18. if (!defined('ELKARTE'))
  19. die('No access...');
  20. /**
  21. * Get the latest post made on the system
  22. *
  23. * - respects approved, recycled, and board permissions
  24. * - @todo is this even used anywhere?
  25. *
  26. * @return array
  27. */
  28. function getLastPost()
  29. {
  30. global $user_info, $scripturl, $modSettings, $smcFunc;
  31. // Find it by the board - better to order by board than sort the entire messages table.
  32. $request = $smcFunc['db_query']('substring', '
  33. SELECT ml.poster_time, ml.subject, ml.id_topic, ml.poster_name, SUBSTRING(ml.body, 1, 385) AS body,
  34. ml.smileys_enabled
  35. FROM {db_prefix}boards AS b
  36. INNER JOIN {db_prefix}messages AS ml ON (ml.id_msg = b.id_last_msg)
  37. WHERE {query_wanna_see_board}' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
  38. AND b.id_board != {int:recycle_board}' : '') . '
  39. AND ml.approved = {int:is_approved}
  40. ORDER BY b.id_msg_updated DESC
  41. LIMIT 1',
  42. array(
  43. 'recycle_board' => $modSettings['recycle_board'],
  44. 'is_approved' => 1,
  45. )
  46. );
  47. if ($smcFunc['db_num_rows']($request) == 0)
  48. return array();
  49. $row = $smcFunc['db_fetch_assoc']($request);
  50. $smcFunc['db_free_result']($request);
  51. // Censor the subject and post...
  52. censorText($row['subject']);
  53. censorText($row['body']);
  54. $row['body'] = strip_tags(strtr(parse_bbc($row['body'], $row['smileys_enabled']), array('<br />' => '&#10;')));
  55. if ($smcFunc['strlen']($row['body']) > 128)
  56. $row['body'] = $smcFunc['substr']($row['body'], 0, 128) . '...';
  57. // Send the data.
  58. return array(
  59. 'topic' => $row['id_topic'],
  60. 'subject' => $row['subject'],
  61. 'short_subject' => shorten_subject($row['subject'], 24),
  62. 'preview' => $row['body'],
  63. 'time' => timeformat($row['poster_time']),
  64. 'timestamp' => forum_time(true, $row['poster_time']),
  65. 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.new;topicseen#new',
  66. 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.new;topicseen#new">' . $row['subject'] . '</a>'
  67. );
  68. }
  69. /**
  70. * Find the ten most recent posts.
  71. * Accessed by action=recent.
  72. */
  73. function action_recent()
  74. {
  75. global $txt, $scripturl, $user_info, $context, $modSettings, $board, $smcFunc;
  76. loadTemplate('Recent');
  77. $context['page_title'] = $txt['recent_posts'];
  78. if (isset($_REQUEST['start']) && $_REQUEST['start'] > 95)
  79. $_REQUEST['start'] = 95;
  80. $query_parameters = array();
  81. if (!empty($_REQUEST['c']) && empty($board))
  82. {
  83. $_REQUEST['c'] = explode(',', $_REQUEST['c']);
  84. foreach ($_REQUEST['c'] as $i => $c)
  85. $_REQUEST['c'][$i] = (int) $c;
  86. if (count($_REQUEST['c']) == 1)
  87. {
  88. $request = $smcFunc['db_query']('', '
  89. SELECT name
  90. FROM {db_prefix}categories
  91. WHERE id_cat = {int:id_cat}
  92. LIMIT 1',
  93. array(
  94. 'id_cat' => $_REQUEST['c'][0],
  95. )
  96. );
  97. list ($name) = $smcFunc['db_fetch_row']($request);
  98. $smcFunc['db_free_result']($request);
  99. if (empty($name))
  100. fatal_lang_error('no_access', false);
  101. $context['linktree'][] = array(
  102. 'url' => $scripturl . '#c' . (int) $_REQUEST['c'],
  103. 'name' => $name
  104. );
  105. }
  106. $request = $smcFunc['db_query']('', '
  107. SELECT b.id_board, b.num_posts
  108. FROM {db_prefix}boards AS b
  109. WHERE b.id_cat IN ({array_int:category_list})
  110. AND {query_see_board}',
  111. array(
  112. 'category_list' => $_REQUEST['c'],
  113. )
  114. );
  115. $total_cat_posts = 0;
  116. $boards = array();
  117. while ($row = $smcFunc['db_fetch_assoc']($request))
  118. {
  119. $boards[] = $row['id_board'];
  120. $total_cat_posts += $row['num_posts'];
  121. }
  122. $smcFunc['db_free_result']($request);
  123. if (empty($boards))
  124. fatal_lang_error('error_no_boards_selected');
  125. $query_this_board = 'b.id_board IN ({array_int:boards})';
  126. $query_parameters['boards'] = $boards;
  127. // If this category has a significant number of posts in it...
  128. if ($total_cat_posts > 100 && $total_cat_posts > $modSettings['totalMessages'] / 15)
  129. {
  130. $query_this_board .= '
  131. AND m.id_msg >= {int:max_id_msg}';
  132. $query_parameters['max_id_msg'] = max(0, $modSettings['maxMsgID'] - 400 - $_REQUEST['start'] * 7);
  133. }
  134. $context['page_index'] = constructPageIndex($scripturl . '?action=recent;c=' . implode(',', $_REQUEST['c']), $_REQUEST['start'], min(100, $total_cat_posts), 10, false);
  135. }
  136. elseif (!empty($_REQUEST['boards']))
  137. {
  138. $_REQUEST['boards'] = explode(',', $_REQUEST['boards']);
  139. foreach ($_REQUEST['boards'] as $i => $b)
  140. $_REQUEST['boards'][$i] = (int) $b;
  141. $request = $smcFunc['db_query']('', '
  142. SELECT b.id_board, b.num_posts
  143. FROM {db_prefix}boards AS b
  144. WHERE b.id_board IN ({array_int:board_list})
  145. AND {query_see_board}
  146. LIMIT {int:limit}',
  147. array(
  148. 'board_list' => $_REQUEST['boards'],
  149. 'limit' => count($_REQUEST['boards']),
  150. )
  151. );
  152. $total_posts = 0;
  153. $boards = array();
  154. while ($row = $smcFunc['db_fetch_assoc']($request))
  155. {
  156. $boards[] = $row['id_board'];
  157. $total_posts += $row['num_posts'];
  158. }
  159. $smcFunc['db_free_result']($request);
  160. if (empty($boards))
  161. fatal_lang_error('error_no_boards_selected');
  162. $query_this_board = 'b.id_board IN ({array_int:boards})';
  163. $query_parameters['boards'] = $boards;
  164. // If these boards have a significant number of posts in them...
  165. if ($total_posts > 100 && $total_posts > $modSettings['totalMessages'] / 12)
  166. {
  167. $query_this_board .= '
  168. AND m.id_msg >= {int:max_id_msg}';
  169. $query_parameters['max_id_msg'] = max(0, $modSettings['maxMsgID'] - 500 - $_REQUEST['start'] * 9);
  170. }
  171. $context['page_index'] = constructPageIndex($scripturl . '?action=recent;boards=' . implode(',', $_REQUEST['boards']), $_REQUEST['start'], min(100, $total_posts), 10, false);
  172. }
  173. elseif (!empty($board))
  174. {
  175. $request = $smcFunc['db_query']('', '
  176. SELECT num_posts
  177. FROM {db_prefix}boards
  178. WHERE id_board = {int:current_board}
  179. LIMIT 1',
  180. array(
  181. 'current_board' => $board,
  182. )
  183. );
  184. list ($total_posts) = $smcFunc['db_fetch_row']($request);
  185. $smcFunc['db_free_result']($request);
  186. $query_this_board = 'b.id_board = {int:board}';
  187. $query_parameters['board'] = $board;
  188. // If this board has a significant number of posts in it...
  189. if ($total_posts > 80 && $total_posts > $modSettings['totalMessages'] / 10)
  190. {
  191. $query_this_board .= '
  192. AND m.id_msg >= {int:max_id_msg}';
  193. $query_parameters['max_id_msg'] = max(0, $modSettings['maxMsgID'] - 600 - $_REQUEST['start'] * 10);
  194. }
  195. $context['page_index'] = constructPageIndex($scripturl . '?action=recent;board=' . $board . '.%1$d', $_REQUEST['start'], min(100, $total_posts), 10, true);
  196. }
  197. else
  198. {
  199. $query_this_board = '{query_wanna_see_board}' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
  200. AND b.id_board != {int:recycle_board}' : ''). '
  201. AND m.id_msg >= {int:max_id_msg}';
  202. $query_parameters['max_id_msg'] = max(0, $modSettings['maxMsgID'] - 100 - $_REQUEST['start'] * 6);
  203. $query_parameters['recycle_board'] = $modSettings['recycle_board'];
  204. // @todo This isn't accurate because we ignore the recycle bin.
  205. $context['page_index'] = constructPageIndex($scripturl . '?action=recent', $_REQUEST['start'], min(100, $modSettings['totalMessages']), 10, false);
  206. }
  207. $context['linktree'][] = array(
  208. 'url' => $scripturl . '?action=recent' . (empty($board) ? (empty($_REQUEST['c']) ? '' : ';c=' . (int) $_REQUEST['c']) : ';board=' . $board . '.0'),
  209. 'name' => $context['page_title']
  210. );
  211. $key = 'recent-' . $user_info['id'] . '-' . md5(serialize(array_diff_key($query_parameters, array('max_id_msg' => 0)))) . '-' . (int) $_REQUEST['start'];
  212. if (empty($modSettings['cache_enable']) || ($messages = cache_get_data($key, 120)) == null)
  213. {
  214. $done = false;
  215. while (!$done)
  216. {
  217. // Find the 10 most recent messages they can *view*.
  218. // @todo SLOW This query is really slow still, probably?
  219. $request = $smcFunc['db_query']('', '
  220. SELECT m.id_msg
  221. FROM {db_prefix}messages AS m
  222. INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)
  223. WHERE ' . $query_this_board . '
  224. AND m.approved = {int:is_approved}
  225. ORDER BY m.id_msg DESC
  226. LIMIT {int:offset}, {int:limit}',
  227. array_merge($query_parameters, array(
  228. 'is_approved' => 1,
  229. 'offset' => $_REQUEST['start'],
  230. 'limit' => 10,
  231. ))
  232. );
  233. // If we don't have 10 results, try again with an unoptimized version covering all rows, and cache the result.
  234. if (isset($query_parameters['max_id_msg']) && $smcFunc['db_num_rows']($request) < 10)
  235. {
  236. $smcFunc['db_free_result']($request);
  237. $query_this_board = str_replace('AND m.id_msg >= {int:max_id_msg}', '', $query_this_board);
  238. $cache_results = true;
  239. unset($query_parameters['max_id_msg']);
  240. }
  241. else
  242. $done = true;
  243. }
  244. $messages = array();
  245. while ($row = $smcFunc['db_fetch_assoc']($request))
  246. $messages[] = $row['id_msg'];
  247. $smcFunc['db_free_result']($request);
  248. if (!empty($cache_results))
  249. cache_put_data($key, $messages, 120);
  250. }
  251. // Nothing here... Or at least, nothing you can see...
  252. if (empty($messages))
  253. {
  254. $context['posts'] = array();
  255. return;
  256. }
  257. // Get all the most recent posts.
  258. $request = $smcFunc['db_query']('', '
  259. SELECT
  260. m.id_msg, m.subject, m.smileys_enabled, m.poster_time, m.body, m.id_topic, t.id_board, b.id_cat,
  261. b.name AS bname, c.name AS cname, t.num_replies, m.id_member, m2.id_member AS id_first_member,
  262. IFNULL(mem2.real_name, m2.poster_name) AS first_poster_name, t.id_first_msg,
  263. IFNULL(mem.real_name, m.poster_name) AS poster_name, t.id_last_msg
  264. FROM {db_prefix}messages AS m
  265. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
  266. INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
  267. INNER JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat)
  268. INNER JOIN {db_prefix}messages AS m2 ON (m2.id_msg = t.id_first_msg)
  269. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
  270. LEFT JOIN {db_prefix}members AS mem2 ON (mem2.id_member = m2.id_member)
  271. WHERE m.id_msg IN ({array_int:message_list})
  272. ORDER BY m.id_msg DESC
  273. LIMIT ' . count($messages),
  274. array(
  275. 'message_list' => $messages,
  276. )
  277. );
  278. $counter = $_REQUEST['start'] + 1;
  279. $context['posts'] = array();
  280. $board_ids = array('own' => array(), 'any' => array());
  281. while ($row = $smcFunc['db_fetch_assoc']($request))
  282. {
  283. // Censor everything.
  284. censorText($row['body']);
  285. censorText($row['subject']);
  286. // BBC-atize the message.
  287. $row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']);
  288. // And build the array.
  289. $context['posts'][$row['id_msg']] = array(
  290. 'id' => $row['id_msg'],
  291. 'counter' => $counter++,
  292. 'alternate' => $counter % 2,
  293. 'category' => array(
  294. 'id' => $row['id_cat'],
  295. 'name' => $row['cname'],
  296. 'href' => $scripturl . '#c' . $row['id_cat'],
  297. 'link' => '<a href="' . $scripturl . '#c' . $row['id_cat'] . '">' . $row['cname'] . '</a>'
  298. ),
  299. 'board' => array(
  300. 'id' => $row['id_board'],
  301. 'name' => $row['bname'],
  302. 'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
  303. 'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['bname'] . '</a>'
  304. ),
  305. 'topic' => $row['id_topic'],
  306. 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'],
  307. 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'] . '" rel="nofollow">' . $row['subject'] . '</a>',
  308. 'start' => $row['num_replies'],
  309. 'subject' => $row['subject'],
  310. 'time' => timeformat($row['poster_time']),
  311. 'timestamp' => forum_time(true, $row['poster_time']),
  312. 'first_poster' => array(
  313. 'id' => $row['id_first_member'],
  314. 'name' => $row['first_poster_name'],
  315. 'href' => empty($row['id_first_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_first_member'],
  316. 'link' => empty($row['id_first_member']) ? $row['first_poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_first_member'] . '">' . $row['first_poster_name'] . '</a>'
  317. ),
  318. 'poster' => array(
  319. 'id' => $row['id_member'],
  320. 'name' => $row['poster_name'],
  321. 'href' => empty($row['id_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member'],
  322. 'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>'
  323. ),
  324. 'message' => $row['body'],
  325. 'can_reply' => false,
  326. 'can_mark_notify' => false,
  327. 'can_delete' => false,
  328. 'delete_possible' => ($row['id_first_msg'] != $row['id_msg'] || $row['id_last_msg'] == $row['id_msg']) && (empty($modSettings['edit_disable_time']) || $row['poster_time'] + $modSettings['edit_disable_time'] * 60 >= time()),
  329. );
  330. if ($user_info['id'] == $row['id_first_member'])
  331. $board_ids['own'][$row['id_board']][] = $row['id_msg'];
  332. $board_ids['any'][$row['id_board']][] = $row['id_msg'];
  333. }
  334. $smcFunc['db_free_result']($request);
  335. // There might be - and are - different permissions between any and own.
  336. $permissions = array(
  337. 'own' => array(
  338. 'post_reply_own' => 'can_reply',
  339. 'delete_own' => 'can_delete',
  340. ),
  341. 'any' => array(
  342. 'post_reply_any' => 'can_reply',
  343. 'mark_any_notify' => 'can_mark_notify',
  344. 'delete_any' => 'can_delete',
  345. )
  346. );
  347. // Now go through all the permissions, looking for boards they can do it on.
  348. foreach ($permissions as $type => $list)
  349. {
  350. foreach ($list as $permission => $allowed)
  351. {
  352. // They can do it on these boards...
  353. $boards = boardsAllowedTo($permission);
  354. // If 0 is the only thing in the array, they can do it everywhere!
  355. if (!empty($boards) && $boards[0] == 0)
  356. $boards = array_keys($board_ids[$type]);
  357. // Go through the boards, and look for posts they can do this on.
  358. foreach ($boards as $board_id)
  359. {
  360. // Hmm, they have permission, but there are no topics from that board on this page.
  361. if (!isset($board_ids[$type][$board_id]))
  362. continue;
  363. // Okay, looks like they can do it for these posts.
  364. foreach ($board_ids[$type][$board_id] as $counter)
  365. if ($type == 'any' || $context['posts'][$counter]['poster']['id'] == $user_info['id'])
  366. $context['posts'][$counter][$allowed] = true;
  367. }
  368. }
  369. }
  370. $quote_enabled = empty($modSettings['disabledBBC']) || !in_array('quote', explode(',', $modSettings['disabledBBC']));
  371. foreach ($context['posts'] as $counter => $dummy)
  372. {
  373. // Some posts - the first posts - can't just be deleted.
  374. $context['posts'][$counter]['can_delete'] &= $context['posts'][$counter]['delete_possible'];
  375. // And some cannot be quoted...
  376. $context['posts'][$counter]['can_quote'] = $context['posts'][$counter]['can_reply'] && $quote_enabled;
  377. }
  378. }
  379. /**
  380. * Find unread topics and replies.
  381. * Accessed by action=unread and action=unreadreplies
  382. */
  383. function action_unread()
  384. {
  385. global $board, $txt, $scripturl;
  386. global $user_info, $context, $settings, $modSettings, $smcFunc, $options;
  387. // Guests can't have unread things, we don't know anything about them.
  388. is_not_guest();
  389. // Prefetching + lots of MySQL work = bad mojo.
  390. if (isset($_SERVER['HTTP_X_MOZ']) && $_SERVER['HTTP_X_MOZ'] == 'prefetch')
  391. {
  392. ob_end_clean();
  393. header('HTTP/1.1 403 Forbidden');
  394. die;
  395. }
  396. $context['showCheckboxes'] = !empty($options['display_quick_mod']) && $options['display_quick_mod'] == 1 && $settings['show_mark_read'];
  397. $context['showing_all_topics'] = isset($_GET['all']);
  398. $context['start'] = (int) $_REQUEST['start'];
  399. $context['topics_per_page'] = empty($modSettings['disableCustomPerPage']) && !empty($options['topics_per_page']) ? $options['topics_per_page'] : $modSettings['defaultMaxTopics'];
  400. if ($_REQUEST['action'] == 'unread')
  401. $context['page_title'] = $context['showing_all_topics'] ? $txt['unread_topics_all'] : $txt['unread_topics_visit'];
  402. else
  403. $context['page_title'] = $txt['unread_replies'];
  404. if ($context['showing_all_topics'] && !empty($context['load_average']) && !empty($modSettings['loadavg_allunread']) && $context['load_average'] >= $modSettings['loadavg_allunread'])
  405. fatal_lang_error('loadavg_allunread_disabled', false);
  406. elseif ($_REQUEST['action'] != 'unread' && !empty($context['load_average']) && !empty($modSettings['loadavg_unreadreplies']) && $context['load_average'] >= $modSettings['loadavg_unreadreplies'])
  407. fatal_lang_error('loadavg_unreadreplies_disabled', false);
  408. elseif (!$context['showing_all_topics'] && $_REQUEST['action'] == 'unread' && !empty($context['load_average']) && !empty($modSettings['loadavg_unread']) && $context['load_average'] >= $modSettings['loadavg_unread'])
  409. fatal_lang_error('loadavg_unread_disabled', false);
  410. // Parameters for the main query.
  411. $query_parameters = array();
  412. // Are we specifying any specific board?
  413. if (isset($_REQUEST['children']) && (!empty($board) || !empty($_REQUEST['boards'])))
  414. {
  415. $boards = array();
  416. if (!empty($_REQUEST['boards']))
  417. {
  418. $_REQUEST['boards'] = explode(',', $_REQUEST['boards']);
  419. foreach ($_REQUEST['boards'] as $b)
  420. $boards[] = (int) $b;
  421. }
  422. if (!empty($board))
  423. $boards[] = (int) $board;
  424. // The easiest thing is to just get all the boards they can see, but since we've specified the top of tree we ignore some of them
  425. $request = $smcFunc['db_query']('', '
  426. SELECT b.id_board, b.id_parent
  427. FROM {db_prefix}boards AS b
  428. WHERE {query_wanna_see_board}
  429. AND b.child_level > {int:no_child}
  430. AND b.id_board NOT IN ({array_int:boards})
  431. ORDER BY child_level ASC
  432. ',
  433. array(
  434. 'no_child' => 0,
  435. 'boards' => $boards,
  436. )
  437. );
  438. while ($row = $smcFunc['db_fetch_assoc']($request))
  439. if (in_array($row['id_parent'], $boards))
  440. $boards[] = $row['id_board'];
  441. $smcFunc['db_free_result']($request);
  442. if (empty($boards))
  443. fatal_lang_error('error_no_boards_selected');
  444. $query_this_board = 'id_board IN ({array_int:boards})';
  445. $query_parameters['boards'] = $boards;
  446. $context['querystring_board_limits'] = ';boards=' . implode(',', $boards) . ';start=%d';
  447. }
  448. elseif (!empty($board))
  449. {
  450. $query_this_board = 'id_board = {int:board}';
  451. $query_parameters['board'] = $board;
  452. $context['querystring_board_limits'] = ';board=' . $board . '.%1$d';
  453. }
  454. elseif (!empty($_REQUEST['boards']))
  455. {
  456. $_REQUEST['boards'] = explode(',', $_REQUEST['boards']);
  457. foreach ($_REQUEST['boards'] as $i => $b)
  458. $_REQUEST['boards'][$i] = (int) $b;
  459. $request = $smcFunc['db_query']('', '
  460. SELECT b.id_board
  461. FROM {db_prefix}boards AS b
  462. WHERE {query_see_board}
  463. AND b.id_board IN ({array_int:board_list})',
  464. array(
  465. 'board_list' => $_REQUEST['boards'],
  466. )
  467. );
  468. $boards = array();
  469. while ($row = $smcFunc['db_fetch_assoc']($request))
  470. $boards[] = $row['id_board'];
  471. $smcFunc['db_free_result']($request);
  472. if (empty($boards))
  473. fatal_lang_error('error_no_boards_selected');
  474. $query_this_board = 'id_board IN ({array_int:boards})';
  475. $query_parameters['boards'] = $boards;
  476. $context['querystring_board_limits'] = ';boards=' . implode(',', $boards) . ';start=%1$d';
  477. }
  478. elseif (!empty($_REQUEST['c']))
  479. {
  480. $_REQUEST['c'] = explode(',', $_REQUEST['c']);
  481. foreach ($_REQUEST['c'] as $i => $c)
  482. $_REQUEST['c'][$i] = (int) $c;
  483. $see_board = isset($_REQUEST['action']) && $_REQUEST['action'] == 'unreadreplies' ? 'query_see_board' : 'query_wanna_see_board';
  484. $request = $smcFunc['db_query']('', '
  485. SELECT b.id_board
  486. FROM {db_prefix}boards AS b
  487. WHERE ' . $user_info[$see_board] . '
  488. AND b.id_cat IN ({array_int:id_cat})',
  489. array(
  490. 'id_cat' => $_REQUEST['c'],
  491. )
  492. );
  493. $boards = array();
  494. while ($row = $smcFunc['db_fetch_assoc']($request))
  495. $boards[] = $row['id_board'];
  496. $smcFunc['db_free_result']($request);
  497. if (empty($boards))
  498. fatal_lang_error('error_no_boards_selected');
  499. $query_this_board = 'id_board IN ({array_int:boards})';
  500. $query_parameters['boards'] = $boards;
  501. $context['querystring_board_limits'] = ';c=' . implode(',', $_REQUEST['c']) . ';start=%1$d';
  502. }
  503. else
  504. {
  505. $see_board = isset($_REQUEST['action']) && $_REQUEST['action'] == 'unreadreplies' ? 'query_see_board' : 'query_wanna_see_board';
  506. // Don't bother to show deleted posts!
  507. $request = $smcFunc['db_query']('', '
  508. SELECT b.id_board
  509. FROM {db_prefix}boards AS b
  510. WHERE ' . $user_info[$see_board] . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
  511. AND b.id_board != {int:recycle_board}' : ''),
  512. array(
  513. 'recycle_board' => (int) $modSettings['recycle_board'],
  514. )
  515. );
  516. $boards = array();
  517. while ($row = $smcFunc['db_fetch_assoc']($request))
  518. $boards[] = $row['id_board'];
  519. $smcFunc['db_free_result']($request);
  520. if (empty($boards))
  521. fatal_lang_error('error_no_boards_selected');
  522. $query_this_board = 'id_board IN ({array_int:boards})';
  523. $query_parameters['boards'] = $boards;
  524. $context['querystring_board_limits'] = ';start=%1$d';
  525. $context['no_board_limits'] = true;
  526. }
  527. $sort_methods = array(
  528. 'subject' => 'ms.subject',
  529. 'starter' => 'IFNULL(mems.real_name, ms.poster_name)',
  530. 'replies' => 't.num_replies',
  531. 'views' => 't.num_views',
  532. 'first_post' => 't.id_topic',
  533. 'last_post' => 't.id_last_msg'
  534. );
  535. // The default is the most logical: newest first.
  536. if (!isset($_REQUEST['sort']) || !isset($sort_methods[$_REQUEST['sort']]))
  537. {
  538. $context['sort_by'] = 'last_post';
  539. $_REQUEST['sort'] = 't.id_last_msg';
  540. $ascending = isset($_REQUEST['asc']);
  541. $context['querystring_sort_limits'] = $ascending ? ';asc' : '';
  542. }
  543. // But, for other methods the default sort is ascending.
  544. else
  545. {
  546. $context['sort_by'] = $_REQUEST['sort'];
  547. $_REQUEST['sort'] = $sort_methods[$_REQUEST['sort']];
  548. $ascending = !isset($_REQUEST['desc']);
  549. $context['querystring_sort_limits'] = ';sort=' . $context['sort_by'] . ($ascending ? '' : ';desc');
  550. }
  551. $context['sort_direction'] = $ascending ? 'up' : 'down';
  552. if (!empty($_REQUEST['c']) && is_array($_REQUEST['c']) && count($_REQUEST['c']) == 1)
  553. {
  554. $request = $smcFunc['db_query']('', '
  555. SELECT name
  556. FROM {db_prefix}categories
  557. WHERE id_cat = {int:id_cat}
  558. LIMIT 1',
  559. array(
  560. 'id_cat' => (int) $_REQUEST['c'][0],
  561. )
  562. );
  563. list ($name) = $smcFunc['db_fetch_row']($request);
  564. $smcFunc['db_free_result']($request);
  565. $context['linktree'][] = array(
  566. 'url' => $scripturl . '#c' . (int) $_REQUEST['c'][0],
  567. 'name' => $name
  568. );
  569. }
  570. $context['linktree'][] = array(
  571. 'url' => $scripturl . '?action=' . $_REQUEST['action'] . sprintf($context['querystring_board_limits'], 0) . $context['querystring_sort_limits'],
  572. 'name' => $_REQUEST['action'] == 'unread' ? $txt['unread_topics_visit'] : $txt['unread_replies']
  573. );
  574. if ($context['showing_all_topics'])
  575. $context['linktree'][] = array(
  576. 'url' => $scripturl . '?action=' . $_REQUEST['action'] . ';all' . sprintf($context['querystring_board_limits'], 0) . $context['querystring_sort_limits'],
  577. 'name' => $txt['unread_topics_all']
  578. );
  579. else
  580. $txt['unread_topics_visit_none'] = strtr($txt['unread_topics_visit_none'], array('?action=unread;all' => '?action=unread;all' . sprintf($context['querystring_board_limits'], 0) . $context['querystring_sort_limits']));
  581. loadTemplate('Recent');
  582. $context['sub_template'] = $_REQUEST['action'] == 'unread' ? 'unread' : 'replies';
  583. // Setup the default topic icons... for checking they exist and the like ;)
  584. $stable_icons = array('xx', 'thumbup', 'thumbdown', 'exclamation', 'question', 'lamp', 'smiley', 'angry', 'cheesy', 'grin', 'sad', 'wink', 'poll', 'moved', 'recycled', 'wireless', 'clip');
  585. $context['icon_sources'] = array();
  586. foreach ($stable_icons as $icon)
  587. $context['icon_sources'][$icon] = 'images_url';
  588. $is_topics = $_REQUEST['action'] == 'unread';
  589. // This part is the same for each query.
  590. $select_clause = '
  591. ms.subject AS first_subject, ms.poster_time AS first_poster_time, ms.id_topic, t.id_board, b.name AS bname,
  592. t.num_replies, t.num_views, ms.id_member AS id_first_member, ml.id_member AS id_last_member,
  593. ml.poster_time AS last_poster_time, IFNULL(mems.real_name, ms.poster_name) AS first_poster_name,
  594. IFNULL(meml.real_name, ml.poster_name) AS last_poster_name, ml.subject AS last_subject,
  595. ml.icon AS last_icon, ms.icon AS first_icon, t.id_poll, t.is_sticky, t.locked, ml.modified_time AS last_modified_time,
  596. IFNULL(lt.id_msg, IFNULL(lmr.id_msg, -1)) + 1 AS new_from, SUBSTRING(ml.body, 1, 385) AS last_body,
  597. SUBSTRING(ms.body, 1, 385) AS first_body, ml.smileys_enabled AS last_smileys, ms.smileys_enabled AS first_smileys, t.id_first_msg, t.id_last_msg';
  598. if ($context['showing_all_topics'])
  599. {
  600. if (!empty($board))
  601. {
  602. $request = $smcFunc['db_query']('', '
  603. SELECT MIN(id_msg)
  604. FROM {db_prefix}log_mark_read
  605. WHERE id_member = {int:current_member}
  606. AND id_board = {int:current_board}',
  607. array(
  608. 'current_board' => $board,
  609. 'current_member' => $user_info['id'],
  610. )
  611. );
  612. list ($earliest_msg) = $smcFunc['db_fetch_row']($request);
  613. $smcFunc['db_free_result']($request);
  614. }
  615. else
  616. {
  617. $request = $smcFunc['db_query']('', '
  618. SELECT MIN(lmr.id_msg)
  619. FROM {db_prefix}boards AS b
  620. LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = b.id_board AND lmr.id_member = {int:current_member})
  621. WHERE {query_see_board}',
  622. array(
  623. 'current_member' => $user_info['id'],
  624. )
  625. );
  626. list ($earliest_msg) = $smcFunc['db_fetch_row']($request);
  627. $smcFunc['db_free_result']($request);
  628. }
  629. // This is needed in case of topics marked unread.
  630. if (empty($earliest_msg))
  631. $earliest_msg = 0;
  632. else
  633. {
  634. // Using caching, when possible, to ignore the below slow query.
  635. if (isset($_SESSION['cached_log_time']) && $_SESSION['cached_log_time'][0] + 45 > time())
  636. $earliest_msg2 = $_SESSION['cached_log_time'][1];
  637. else
  638. {
  639. // This query is pretty slow, but it's needed to ensure nothing crucial is ignored.
  640. $request = $smcFunc['db_query']('', '
  641. SELECT MIN(id_msg)
  642. FROM {db_prefix}log_topics
  643. WHERE id_member = {int:current_member}',
  644. array(
  645. 'current_member' => $user_info['id'],
  646. )
  647. );
  648. list ($earliest_msg2) = $smcFunc['db_fetch_row']($request);
  649. $smcFunc['db_free_result']($request);
  650. // In theory this could be zero, if the first ever post is unread, so fudge it ;)
  651. if ($earliest_msg2 == 0)
  652. $earliest_msg2 = -1;
  653. $_SESSION['cached_log_time'] = array(time(), $earliest_msg2);
  654. }
  655. $earliest_msg = min($earliest_msg2, $earliest_msg);
  656. }
  657. }
  658. // @todo Add modified_time in for log_time check?
  659. if ($modSettings['totalMessages'] > 100000 && $context['showing_all_topics'])
  660. {
  661. $smcFunc['db_query']('', '
  662. DROP TABLE IF EXISTS {db_prefix}log_topics_unread',
  663. array(
  664. )
  665. );
  666. // Let's copy things out of the log_topics table, to reduce searching.
  667. $have_temp_table = $smcFunc['db_query']('', '
  668. CREATE TEMPORARY TABLE {db_prefix}log_topics_unread (
  669. PRIMARY KEY (id_topic)
  670. )
  671. SELECT lt.id_topic, lt.id_msg, lt.disregarded
  672. FROM {db_prefix}topics AS t
  673. INNER JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic)
  674. WHERE lt.id_member = {int:current_member}
  675. AND t.' . $query_this_board . (empty($earliest_msg) ? '' : '
  676. AND t.id_last_msg > {int:earliest_msg}') . ($modSettings['postmod_active'] ? '
  677. AND t.approved = {int:is_approved}' : '') . ($modSettings['enable_disregard'] ? '
  678. AND lt.disregarded != 1' : ''),
  679. array_merge($query_parameters, array(
  680. 'current_member' => $user_info['id'],
  681. 'earliest_msg' => !empty($earliest_msg) ? $earliest_msg : 0,
  682. 'is_approved' => 1,
  683. 'db_error_skip' => true,
  684. ))
  685. ) !== false;
  686. }
  687. else
  688. $have_temp_table = false;
  689. if ($context['showing_all_topics'] && $have_temp_table)
  690. {
  691. $request = $smcFunc['db_query']('', '
  692. SELECT COUNT(*), MIN(t.id_last_msg)
  693. FROM {db_prefix}topics AS t
  694. LEFT JOIN {db_prefix}log_topics_unread AS lt ON (lt.id_topic = t.id_topic)
  695. LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})
  696. WHERE t.' . $query_this_board . (!empty($earliest_msg) ? '
  697. AND t.id_last_msg > {int:earliest_msg}' : '') . '
  698. AND IFNULL(lt.id_msg, IFNULL(lmr.id_msg, 0)) < t.id_last_msg' .
  699. ($modSettings['postmod_active'] ? ' AND t.approved = {int:is_approved}' : '') .
  700. ($modSettings['enable_disregard'] ? ' AND IFNULL(lt.disregarded, 0) != 1' : ''),
  701. array_merge($query_parameters, array(
  702. 'current_member' => $user_info['id'],
  703. 'earliest_msg' => !empty($earliest_msg) ? $earliest_msg : 0,
  704. 'is_approved' => 1,
  705. ))
  706. );
  707. list ($num_topics, $min_message) = $smcFunc['db_fetch_row']($request);
  708. $smcFunc['db_free_result']($request);
  709. // Make sure the starting place makes sense and construct the page index.
  710. $context['page_index'] = constructPageIndex($scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . $context['querystring_board_limits'] . $context['querystring_sort_limits'], $_REQUEST['start'], $num_topics, $context['topics_per_page'], true);
  711. $context['current_page'] = (int) $_REQUEST['start'] / $context['topics_per_page'];
  712. $context['links'] = array(
  713. 'first' => $_REQUEST['start'] >= $context['topics_per_page'] ? $scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . sprintf($context['querystring_board_limits'], 0) . $context['querystring_sort_limits'] : '',
  714. 'prev' => $_REQUEST['start'] >= $context['topics_per_page'] ? $scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . sprintf($context['querystring_board_limits'], $_REQUEST['start'] - $context['topics_per_page']) . $context['querystring_sort_limits'] : '',
  715. 'next' => $_REQUEST['start'] + $context['topics_per_page'] < $num_topics ? $scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . sprintf($context['querystring_board_limits'], $_REQUEST['start'] + $context['topics_per_page']) . $context['querystring_sort_limits'] : '',
  716. 'last' => $_REQUEST['start'] + $context['topics_per_page'] < $num_topics ? $scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . sprintf($context['querystring_board_limits'], floor(($num_topics - 1) / $context['topics_per_page']) * $context['topics_per_page']) . $context['querystring_sort_limits'] : '',
  717. 'up' => $scripturl,
  718. );
  719. $context['page_info'] = array(
  720. 'current_page' => $_REQUEST['start'] / $context['topics_per_page'] + 1,
  721. 'num_pages' => floor(($num_topics - 1) / $context['topics_per_page']) + 1
  722. );
  723. if ($num_topics == 0)
  724. {
  725. // Mark the boards as read if there are no unread topics!
  726. require_once(SUBSDIR . '/Boards.subs.php');
  727. markBoardsRead(empty($boards) ? $board : $boards);
  728. $context['topics'] = array();
  729. if ($context['querystring_board_limits'] == ';start=%1$d')
  730. $context['querystring_board_limits'] = '';
  731. else
  732. $context['querystring_board_limits'] = sprintf($context['querystring_board_limits'], $_REQUEST['start']);
  733. return;
  734. }
  735. else
  736. $min_message = (int) $min_message;
  737. $request = $smcFunc['db_query']('substring', '
  738. SELECT ' . $select_clause . '
  739. FROM {db_prefix}messages AS ms
  740. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = ms.id_topic AND t.id_first_msg = ms.id_msg)
  741. INNER JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)
  742. LEFT JOIN {db_prefix}boards AS b ON (b.id_board = ms.id_board)
  743. LEFT JOIN {db_prefix}members AS mems ON (mems.id_member = ms.id_member)
  744. LEFT JOIN {db_prefix}members AS meml ON (meml.id_member = ml.id_member)
  745. LEFT JOIN {db_prefix}log_topics_unread AS lt ON (lt.id_topic = t.id_topic)
  746. LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})
  747. WHERE b.' . $query_this_board . '
  748. AND t.id_last_msg >= {int:min_message}
  749. AND IFNULL(lt.id_msg, IFNULL(lmr.id_msg, 0)) < t.id_last_msg' .
  750. ($modSettings['postmod_active'] ? ' AND ms.approved = {int:is_approved}' : '') .
  751. ($modSettings['enable_disregard'] ? ' AND IFNULL(lt.disregarded, 0) != 1' : '') . '
  752. ORDER BY {raw:sort}
  753. LIMIT {int:offset}, {int:limit}',
  754. array_merge($query_parameters, array(
  755. 'current_member' => $user_info['id'],
  756. 'min_message' => $min_message,
  757. 'is_approved' => 1,
  758. 'sort' => $_REQUEST['sort'] . ($ascending ? '' : ' DESC'),
  759. 'offset' => $_REQUEST['start'],
  760. 'limit' => $context['topics_per_page'],
  761. ))
  762. );
  763. }
  764. elseif ($is_topics)
  765. {
  766. $request = $smcFunc['db_query']('', '
  767. SELECT COUNT(*), MIN(t.id_last_msg)
  768. FROM {db_prefix}topics AS t' . (!empty($have_temp_table) ? '
  769. LEFT JOIN {db_prefix}log_topics_unread AS lt ON (lt.id_topic = t.id_topic)' : '
  770. LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})') . '
  771. LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})
  772. WHERE t.' . $query_this_board . ($context['showing_all_topics'] && !empty($earliest_msg) ? '
  773. AND t.id_last_msg > {int:earliest_msg}' : (!$context['showing_all_topics'] && empty($_SESSION['first_login']) ? '
  774. AND t.id_last_msg > {int:id_msg_last_visit}' : '')) . '
  775. AND IFNULL(lt.id_msg, IFNULL(lmr.id_msg, 0)) < t.id_last_msg' .
  776. ($modSettings['postmod_active'] ? ' AND t.approved = {int:is_approved}' : '') .
  777. ($modSettings['enable_disregard'] ? ' AND IFNULL(lt.disregarded, 0) != 1' : ''),
  778. array_merge($query_parameters, array(
  779. 'current_member' => $user_info['id'],
  780. 'earliest_msg' => !empty($earliest_msg) ? $earliest_msg : 0,
  781. 'id_msg_last_visit' => $_SESSION['id_msg_last_visit'],
  782. 'is_approved' => 1,
  783. ))
  784. );
  785. list ($num_topics, $min_message) = $smcFunc['db_fetch_row']($request);
  786. $smcFunc['db_free_result']($request);
  787. // Make sure the starting place makes sense and construct the page index.
  788. $context['page_index'] = constructPageIndex($scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . $context['querystring_board_limits'] . $context['querystring_sort_limits'], $_REQUEST['start'], $num_topics, $context['topics_per_page'], true);
  789. $context['current_page'] = (int) $_REQUEST['start'] / $context['topics_per_page'];
  790. $context['links'] = array(
  791. 'first' => $_REQUEST['start'] >= $context['topics_per_page'] ? $scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . sprintf($context['querystring_board_limits'], 0) . $context['querystring_sort_limits'] : '',
  792. 'prev' => $_REQUEST['start'] >= $context['topics_per_page'] ? $scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . sprintf($context['querystring_board_limits'], $_REQUEST['start'] - $context['topics_per_page']) . $context['querystring_sort_limits'] : '',
  793. 'next' => $_REQUEST['start'] + $context['topics_per_page'] < $num_topics ? $scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . sprintf($context['querystring_board_limits'], $_REQUEST['start'] + $context['topics_per_page']) . $context['querystring_sort_limits'] : '',
  794. 'last' => $_REQUEST['start'] + $context['topics_per_page'] < $num_topics ? $scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . sprintf($context['querystring_board_limits'], floor(($num_topics - 1) / $context['topics_per_page']) * $context['topics_per_page']) . $context['querystring_sort_limits'] : '',
  795. 'up' => $scripturl,
  796. );
  797. $context['page_info'] = array(
  798. 'current_page' => $_REQUEST['start'] / $context['topics_per_page'] + 1,
  799. 'num_pages' => floor(($num_topics - 1) / $context['topics_per_page']) + 1
  800. );
  801. if ($num_topics == 0)
  802. {
  803. // Is this an all topics query?
  804. if ($context['showing_all_topics'])
  805. {
  806. // Since there are no unread topics, mark the boards as read!
  807. require_once(SUBSDIR . '/Boards.subs.php');
  808. markBoardsRead(empty($boards) ? $board : $boards);
  809. }
  810. $context['topics'] = array();
  811. if ($context['querystring_board_limits'] == ';start=%d')
  812. $context['querystring_board_limits'] = '';
  813. else
  814. $context['querystring_board_limits'] = sprintf($context['querystring_board_limits'], $_REQUEST['start']);
  815. return;
  816. }
  817. else
  818. $min_message = (int) $min_message;
  819. $request = $smcFunc['db_query']('substring', '
  820. SELECT ' . $select_clause . '
  821. FROM {db_prefix}messages AS ms
  822. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = ms.id_topic AND t.id_first_msg = ms.id_msg)
  823. INNER JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)
  824. LEFT JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
  825. LEFT JOIN {db_prefix}members AS mems ON (mems.id_member = ms.id_member)
  826. LEFT JOIN {db_prefix}members AS meml ON (meml.id_member = ml.id_member)' . (!empty($have_temp_table) ? '
  827. LEFT JOIN {db_prefix}log_topics_unread AS lt ON (lt.id_topic = t.id_topic)' : '
  828. LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})') . '
  829. LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})
  830. WHERE t.' . $query_this_board . '
  831. AND t.id_last_msg >= {int:min_message}
  832. AND IFNULL(lt.id_msg, IFNULL(lmr.id_msg, 0)) < ml.id_msg' .
  833. ($modSettings['postmod_active'] ? ' AND ms.approved = {int:is_approved}' : '') .
  834. ($modSettings['enable_disregard'] ? ' AND IFNULL(lt.disregarded, 0) != 1' : '') . '
  835. ORDER BY {raw:order}
  836. LIMIT {int:offset}, {int:limit}',
  837. array_merge($query_parameters, array(
  838. 'current_member' => $user_info['id'],
  839. 'min_message' => $min_message,
  840. 'is_approved' => 1,
  841. 'order' => $_REQUEST['sort'] . ($ascending ? '' : ' DESC'),
  842. 'offset' => $_REQUEST['start'],
  843. 'limit' => $context['topics_per_page'],
  844. ))
  845. );
  846. }
  847. else
  848. {
  849. if ($modSettings['totalMessages'] > 100000)
  850. {
  851. $smcFunc['db_query']('', '
  852. DROP TABLE IF EXISTS {db_prefix}topics_posted_in',
  853. array(
  854. )
  855. );
  856. $smcFunc['db_query']('', '
  857. DROP TABLE IF EXISTS {db_prefix}log_topics_posted_in',
  858. array(
  859. )
  860. );
  861. $sortKey_joins = array(
  862. 'ms.subject' => '
  863. INNER JOIN {db_prefix}messages AS ms ON (ms.id_msg = t.id_first_msg)',
  864. 'IFNULL(mems.real_name, ms.poster_name)' => '
  865. INNER JOIN {db_prefix}messages AS ms ON (ms.id_msg = t.id_first_msg)
  866. LEFT JOIN {db_prefix}members AS mems ON (mems.id_member = ms.id_member)',
  867. );
  868. // The main benefit of this temporary table is not that it's faster; it's that it avoids locks later.
  869. $have_temp_table = $smcFunc['db_query']('', '
  870. CREATE TEMPORARY TABLE {db_prefix}topics_posted_in (
  871. id_topic mediumint(8) unsigned NOT NULL default {string:string_zero},
  872. id_board smallint(5) unsigned NOT NULL default {string:string_zero},
  873. id_last_msg int(10) unsigned NOT NULL default {string:string_zero},
  874. id_msg int(10) unsigned NOT NULL default {string:string_zero},
  875. PRIMARY KEY (id_topic)
  876. )
  877. SELECT t.id_topic, t.id_board, t.id_last_msg, IFNULL(lmr.id_msg, 0) AS id_msg' . (!in_array($_REQUEST['sort'], array('t.id_last_msg', 't.id_topic')) ? ', ' . $_REQUEST['sort'] . ' AS sort_key' : '') . '
  878. FROM {db_prefix}messages AS m
  879. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
  880. LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})' . (isset($sortKey_joins[$_REQUEST['sort']]) ? $sortKey_joins[$_REQUEST['sort']] : '') . '
  881. WHERE m.id_member = {int:current_member}' . (!empty($board) ? '
  882. AND t.id_board = {int:current_board}' : '') .
  883. ($modSettings['postmod_active'] ? ' AND t.approved = {int:is_approved}' : '') .
  884. ($modSettings['enable_disregard'] ? ' AND IFNULL(lt.disregarded, 0) != 1' : '') . '
  885. GROUP BY m.id_topic',
  886. array(
  887. 'current_board' => $board,
  888. 'current_member' => $user_info['id'],
  889. 'is_approved' => 1,
  890. 'string_zero' => '0',
  891. 'db_error_skip' => true,
  892. )
  893. ) !== false;
  894. // If that worked, create a sample of the log_topics table too.
  895. if ($have_temp_table)
  896. $have_temp_table = $smcFunc['db_query']('', '
  897. CREATE TEMPORARY TABLE {db_prefix}log_topics_posted_in (
  898. PRIMARY KEY (id_topic)
  899. )
  900. SELECT lt.id_topic, lt.id_msg
  901. FROM {db_prefix}log_topics AS lt
  902. INNER JOIN {db_prefix}topics_posted_in AS pi ON (pi.id_topic = lt.id_topic)
  903. WHERE lt.id_member = {int:current_member}',
  904. array(
  905. 'current_member' => $user_info['id'],
  906. 'db_error_skip' => true,
  907. )
  908. ) !== false;
  909. }
  910. if (!empty($have_temp_table))
  911. {
  912. $request = $smcFunc['db_query']('', '
  913. SELECT COUNT(*)
  914. FROM {db_prefix}topics_posted_in AS pi
  915. LEFT JOIN {db_prefix}log_topics_posted_in AS lt ON (lt.id_topic = pi.id_topic)
  916. WHERE pi.' . $query_this_board . '
  917. AND IFNULL(lt.id_msg, pi.id_msg) < pi.id_last_msg',
  918. array_merge($query_parameters, array(
  919. ))
  920. );
  921. list ($num_topics) = $smcFunc['db_fetch_row']($request);
  922. $smcFunc['db_free_result']($request);
  923. }
  924. else
  925. {
  926. $request = $smcFunc['db_query']('unread_fetch_topic_count', '
  927. SELECT COUNT(DISTINCT t.id_topic), MIN(t.id_last_msg)
  928. FROM {db_prefix}topics AS t
  929. INNER JOIN {db_prefix}messages AS m ON (m.id_topic = t.id_topic)
  930. LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})
  931. LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})
  932. WHERE t.' . $query_this_board . '
  933. AND m.id_member = {int:current_member}
  934. AND IFNULL(lt.id_msg, IFNULL(lmr.id_msg, 0)) < t.id_last_msg' .
  935. ($modSettings['postmod_active'] ? ' AND t.approved = {int:is_approved}' : '') .
  936. ($modSettings['enable_disregard'] ? ' AND IFNULL(lt.disregarded, 0) != 1' : ''),
  937. array_merge($query_parameters, array(
  938. 'current_member' => $user_info['id'],
  939. 'is_approved' => 1,
  940. ))
  941. );
  942. list ($num_topics, $min_message) = $smcFunc['db_fetch_row']($request);
  943. $smcFunc['db_free_result']($request);
  944. }
  945. // Make sure the starting place makes sense and construct the page index.
  946. $context['page_index'] = constructPageIndex($scripturl . '?action=' . $_REQUEST['action'] . $context['querystring_board_limits'] . $context['querystring_sort_limits'], $_REQUEST['start'], $num_topics, $context['topics_per_page'], true);
  947. $context['current_page'] = (int) $_REQUEST['start'] / $context['topics_per_page'];
  948. $context['links'] = array(
  949. 'first' => $_REQUEST['start'] >= $context['topics_per_page'] ? $scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . sprintf($context['querystring_board_limits'], 0) . $context['querystring_sort_limits'] : '',
  950. 'prev' => $_REQUEST['start'] >= $context['topics_per_page'] ? $scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . sprintf($context['querystring_board_limits'], $_REQUEST['start'] - $context['topics_per_page']) . $context['querystring_sort_limits'] : '',
  951. 'next' => $_REQUEST['start'] + $context['topics_per_page'] < $num_topics ? $scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . sprintf($context['querystring_board_limits'], $_REQUEST['start'] + $context['topics_per_page']) . $context['querystring_sort_limits'] : '',
  952. 'last' => $_REQUEST['start'] + $context['topics_per_page'] < $num_topics ? $scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . sprintf($context['querystring_board_limits'], floor(($num_topics - 1) / $context['topics_per_page']) * $context['topics_per_page']) . $context['querystring_sort_limits'] : '',
  953. 'up' => $scripturl,
  954. );
  955. $context['page_info'] = array(
  956. 'current_page' => $_REQUEST['start'] / $context['topics_per_page'] + 1,
  957. 'num_pages' => floor(($num_topics - 1) / $context['topics_per_page']) + 1
  958. );
  959. if ($num_topics == 0)
  960. {
  961. $context['topics'] = array();
  962. if ($context['querystring_board_limits'] == ';start=%d')
  963. $context['querystring_board_limits'] = '';
  964. else
  965. $context['querystring_board_limits'] = sprintf($context['querystring_board_limits'], $_REQUEST['start']);
  966. return;
  967. }
  968. if (!empty($have_temp_table))
  969. $request = $smcFunc['db_query']('', '
  970. SELECT t.id_topic
  971. FROM {db_prefix}topics_posted_in AS t
  972. LEFT JOIN {db_prefix}log_topics_posted_in AS lt ON (lt.id_topic = t.id_topic)
  973. WHERE t.' . $query_this_board . '
  974. AND IFNULL(lt.id_msg, t.id_msg) < t.id_last_msg
  975. ORDER BY {raw:order}
  976. LIMIT {int:offset}, {int:limit}',
  977. array_merge($query_parameters, array(
  978. 'order' => (in_array($_REQUEST['sort'], array('t.id_last_msg', 't.id_topic')) ? $_REQUEST['sort'] : 't.sort_key') . ($ascending ? '' : ' DESC'),
  979. 'offset' => $_REQUEST['start'],
  980. 'limit' => $context['topics_per_page'],
  981. ))
  982. );
  983. else
  984. $request = $smcFunc['db_query']('unread_replies', '
  985. SELECT DISTINCT t.id_topic
  986. FROM {db_prefix}topics AS t
  987. INNER JOIN {db_prefix}messages AS m ON (m.id_topic = t.id_topic AND m.id_member = {int:current_member})' . (strpos($_REQUEST['sort'], 'ms.') === false ? '' : '
  988. INNER JOIN {db_prefix}messages AS ms ON (ms.id_msg = t.id_first_msg)') . (strpos($_REQUEST['sort'], 'mems.') === false ? '' : '
  989. LEFT JOIN {db_prefix}members AS mems ON (mems.id_member = ms.id_member)') . '
  990. LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})
  991. LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})
  992. WHERE t.' . $query_this_board . '
  993. AND t.id_last_msg >= {int:min_message}
  994. AND (IFNULL(lt.id_msg, IFNULL(lmr.id_msg, 0))) < t.id_last_msg' .
  995. ($modSettings['postmod_active'] ? ' AND t.approved = {int:is_approved}' : '') .
  996. ($modSettings['enable_disregard'] ? ' AND IFNULL(lt.disregarded, 0) != 1' : '') . '
  997. ORDER BY {raw:order}
  998. LIMIT {int:offset}, {int:limit}',
  999. array_merge($query_parameters, array(
  1000. 'current_member' => $user_info['id'],
  1001. 'min_message' => (int) $min_message,
  1002. 'is_approved' => 1,
  1003. 'order' => $_REQUEST['sort'] . ($ascending ? '' : ' DESC'),
  1004. 'offset' => $_REQUEST['start'],
  1005. 'limit' => $context['topics_per_page'],
  1006. 'sort' => $_REQUEST['sort'],
  1007. ))
  1008. );
  1009. $topics = array();
  1010. while ($row = $smcFunc['db_fetch_assoc']($request))
  1011. $topics[] = $row['id_topic'];
  1012. $smcFunc['db_free_result']($request);
  1013. // Sanity... where have you gone?
  1014. if (empty($topics))
  1015. {
  1016. $context['topics'] = array();
  1017. if ($context['querystring_board_limits'] == ';start=%d')
  1018. $context['querystring_board_limits'] = '';
  1019. else
  1020. $context['querystring_board_limits'] = sprintf($context['querystring_board_limits'], $_REQUEST['start']);
  1021. return;
  1022. }
  1023. $request = $smcFunc['db_query']('substring', '
  1024. SELECT ' . $select_clause . '
  1025. FROM {db_prefix}topics AS t
  1026. INNER JOIN {db_prefix}messages AS ms ON (ms.id_topic = t.id_topic AND ms.id_msg = t.id_first_msg)
  1027. INNER JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)
  1028. INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
  1029. LEFT JOIN {db_prefix}members AS mems ON (mems.id_member = ms.id_member)
  1030. LEFT JOIN {db_prefix}members AS meml ON (meml.id_member = ml.id_member)
  1031. LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})
  1032. LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})
  1033. WHERE t.id_topic IN ({array_int:topic_list})
  1034. ORDER BY ' . $_REQUEST['sort'] . ($ascending ? '' : ' DESC') . '
  1035. LIMIT ' . count($topics),
  1036. array(
  1037. 'current_member' => $user_info['id'],
  1038. 'topic_list' => $topics,
  1039. )
  1040. );
  1041. }
  1042. $context['topics'] = array();
  1043. $topic_ids = array();
  1044. while ($row = $smcFunc['db_fetch_assoc']($request))
  1045. {
  1046. if ($row['id_poll'] > 0 && $modSettings['pollMode'] == '0')
  1047. continue;
  1048. $topic_ids[] = $row['id_topic'];
  1049. if (!empty($settings['message_index_preview']))
  1050. {
  1051. // Limit them to 128 characters - do this FIRST because it's a lot of wasted censoring otherwise.
  1052. $row['first_body'] = strip_tags(strtr(parse_bbc($row['first_body'], $row['first_smileys'], $row['id_first_msg']), array('<br />' => '&#10;')));
  1053. if ($smcFunc['strlen']($row['first_body']) > 128)
  1054. $row['first_body'] = $smcFunc['substr']($row['first_body'], 0, 128) . '...';
  1055. $row['last_body'] = strip_tags(strtr(parse_bbc($row['last_body'], $row['last_smileys'], $row['id_last_msg']), array('<br />' => '&#10;')));
  1056. if ($smcFunc['strlen']($row['last_body']) > 128)
  1057. $row['last_body'] = $smcFunc['substr']($row['last_body'], 0, 128) . '...';
  1058. // Censor the subject and message preview.
  1059. censorText($row['first_subject']);
  1060. censorText($row['first_body']);
  1061. // Don't censor them twice!
  1062. if ($row['id_first_msg'] == $row['id_last_msg'])
  1063. {
  1064. $row['last_subject'] = $row['first_subject'];
  1065. $row['last_body'] = $row['first_body'];
  1066. }
  1067. else
  1068. {
  1069. censorText($row['last_subject']);
  1070. censorText($row['last_body']);
  1071. }
  1072. }
  1073. else
  1074. {
  1075. $row['first_body'] = '';
  1076. $row['last_body'] = '';
  1077. censorText($row['first_subject']);
  1078. if ($row['id_first_msg'] == $row['id_last_msg'])
  1079. $row['last_subject'] = $row['first_subject'];
  1080. else
  1081. censorText($row['last_subject']);
  1082. }
  1083. // Decide how many pages the topic should have.
  1084. $topic_length = $row['num_replies'] + 1;
  1085. $messages_per_page = empty($modSettings['disableCustomPerPage']) && !empty($options['messages_per_page']) ? $options['messages_per_page'] : $modSettings['defaultMaxMessages…

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