Recent.php 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  1. <?php
  2. /**
  3. * Find and retrieve information about recently posted topics, messages, and the like.
  4. *
  5. * Simple Machines Forum (SMF)
  6. *
  7. * @package SMF
  8. * @author Simple Machines http://www.simplemachines.org
  9. * @copyright 2014 Simple Machines and individual contributors
  10. * @license http://www.simplemachines.org/about/smf/license.php BSD
  11. *
  12. * @version 2.1 Alpha 1
  13. */
  14. if (!defined('SMF'))
  15. die('No direct access...');
  16. /**
  17. * Get the latest post made on the system
  18. *
  19. * - respects approved, recycled, and board permissions
  20. * - @todo is this even used anywhere?
  21. *
  22. * @return array
  23. */
  24. function getLastPost()
  25. {
  26. global $scripturl, $modSettings, $smcFunc;
  27. // Find it by the board - better to order by board than sort the entire messages table.
  28. $request = $smcFunc['db_query']('substring', '
  29. SELECT ml.poster_time, ml.subject, ml.id_topic, ml.poster_name, SUBSTRING(ml.body, 1, 385) AS body,
  30. ml.smileys_enabled
  31. FROM {db_prefix}boards AS b
  32. INNER JOIN {db_prefix}messages AS ml ON (ml.id_msg = b.id_last_msg)
  33. WHERE {query_wanna_see_board}' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
  34. AND b.id_board != {int:recycle_board}' : '') . '
  35. AND ml.approved = {int:is_approved}
  36. ORDER BY b.id_msg_updated DESC
  37. LIMIT 1',
  38. array(
  39. 'recycle_board' => $modSettings['recycle_board'],
  40. 'is_approved' => 1,
  41. )
  42. );
  43. if ($smcFunc['db_num_rows']($request) == 0)
  44. return array();
  45. $row = $smcFunc['db_fetch_assoc']($request);
  46. $smcFunc['db_free_result']($request);
  47. // Censor the subject and post...
  48. censorText($row['subject']);
  49. censorText($row['body']);
  50. $row['body'] = strip_tags(strtr(parse_bbc($row['body'], $row['smileys_enabled']), array('<br />' => '&#10;')));
  51. if ($smcFunc['strlen']($row['body']) > 128)
  52. $row['body'] = $smcFunc['substr']($row['body'], 0, 128) . '...';
  53. // Send the data.
  54. return array(
  55. 'topic' => $row['id_topic'],
  56. 'subject' => $row['subject'],
  57. 'short_subject' => shorten_subject($row['subject'], 24),
  58. 'preview' => $row['body'],
  59. 'time' => timeformat($row['poster_time']),
  60. 'timestamp' => forum_time(true, $row['poster_time']),
  61. 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.new;topicseen#new',
  62. 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.new;topicseen#new">' . $row['subject'] . '</a>'
  63. );
  64. }
  65. /**
  66. * Find the ten most recent posts.
  67. */
  68. function RecentPosts()
  69. {
  70. global $txt, $scripturl, $user_info, $context, $modSettings, $board, $smcFunc;
  71. loadTemplate('Recent');
  72. $context['page_title'] = $txt['recent_posts'];
  73. $context['sub_template'] = 'recent';
  74. if (isset($_REQUEST['start']) && $_REQUEST['start'] > 95)
  75. $_REQUEST['start'] = 95;
  76. $query_parameters = array();
  77. if (!empty($_REQUEST['c']) && empty($board))
  78. {
  79. $_REQUEST['c'] = explode(',', $_REQUEST['c']);
  80. foreach ($_REQUEST['c'] as $i => $c)
  81. $_REQUEST['c'][$i] = (int) $c;
  82. if (count($_REQUEST['c']) == 1)
  83. {
  84. $request = $smcFunc['db_query']('', '
  85. SELECT name
  86. FROM {db_prefix}categories
  87. WHERE id_cat = {int:id_cat}
  88. LIMIT 1',
  89. array(
  90. 'id_cat' => $_REQUEST['c'][0],
  91. )
  92. );
  93. list ($name) = $smcFunc['db_fetch_row']($request);
  94. $smcFunc['db_free_result']($request);
  95. if (empty($name))
  96. fatal_lang_error('no_access', false);
  97. $context['linktree'][] = array(
  98. 'url' => $scripturl . '#c' . (int) $_REQUEST['c'],
  99. 'name' => $name
  100. );
  101. }
  102. $request = $smcFunc['db_query']('', '
  103. SELECT b.id_board, b.num_posts
  104. FROM {db_prefix}boards AS b
  105. WHERE b.id_cat IN ({array_int:category_list})
  106. AND {query_see_board}',
  107. array(
  108. 'category_list' => $_REQUEST['c'],
  109. )
  110. );
  111. $total_cat_posts = 0;
  112. $boards = array();
  113. while ($row = $smcFunc['db_fetch_assoc']($request))
  114. {
  115. $boards[] = $row['id_board'];
  116. $total_cat_posts += $row['num_posts'];
  117. }
  118. $smcFunc['db_free_result']($request);
  119. if (empty($boards))
  120. fatal_lang_error('error_no_boards_selected');
  121. $query_this_board = 'b.id_board IN ({array_int:boards})';
  122. $query_parameters['boards'] = $boards;
  123. // If this category has a significant number of posts in it...
  124. if ($total_cat_posts > 100 && $total_cat_posts > $modSettings['totalMessages'] / 15)
  125. {
  126. $query_this_board .= '
  127. AND m.id_msg >= {int:max_id_msg}';
  128. $query_parameters['max_id_msg'] = max(0, $modSettings['maxMsgID'] - 400 - $_REQUEST['start'] * 7);
  129. }
  130. $context['page_index'] = constructPageIndex($scripturl . '?action=recent;c=' . implode(',', $_REQUEST['c']), $_REQUEST['start'], min(100, $total_cat_posts), 10, false);
  131. }
  132. elseif (!empty($_REQUEST['boards']))
  133. {
  134. $_REQUEST['boards'] = explode(',', $_REQUEST['boards']);
  135. foreach ($_REQUEST['boards'] as $i => $b)
  136. $_REQUEST['boards'][$i] = (int) $b;
  137. $request = $smcFunc['db_query']('', '
  138. SELECT b.id_board, b.num_posts
  139. FROM {db_prefix}boards AS b
  140. WHERE b.id_board IN ({array_int:board_list})
  141. AND {query_see_board}
  142. LIMIT {int:limit}',
  143. array(
  144. 'board_list' => $_REQUEST['boards'],
  145. 'limit' => count($_REQUEST['boards']),
  146. )
  147. );
  148. $total_posts = 0;
  149. $boards = array();
  150. while ($row = $smcFunc['db_fetch_assoc']($request))
  151. {
  152. $boards[] = $row['id_board'];
  153. $total_posts += $row['num_posts'];
  154. }
  155. $smcFunc['db_free_result']($request);
  156. if (empty($boards))
  157. fatal_lang_error('error_no_boards_selected');
  158. $query_this_board = 'b.id_board IN ({array_int:boards})';
  159. $query_parameters['boards'] = $boards;
  160. // If these boards have a significant number of posts in them...
  161. if ($total_posts > 100 && $total_posts > $modSettings['totalMessages'] / 12)
  162. {
  163. $query_this_board .= '
  164. AND m.id_msg >= {int:max_id_msg}';
  165. $query_parameters['max_id_msg'] = max(0, $modSettings['maxMsgID'] - 500 - $_REQUEST['start'] * 9);
  166. }
  167. $context['page_index'] = constructPageIndex($scripturl . '?action=recent;boards=' . implode(',', $_REQUEST['boards']), $_REQUEST['start'], min(100, $total_posts), 10, false);
  168. }
  169. elseif (!empty($board))
  170. {
  171. $request = $smcFunc['db_query']('', '
  172. SELECT num_posts
  173. FROM {db_prefix}boards
  174. WHERE id_board = {int:current_board}
  175. LIMIT 1',
  176. array(
  177. 'current_board' => $board,
  178. )
  179. );
  180. list ($total_posts) = $smcFunc['db_fetch_row']($request);
  181. $smcFunc['db_free_result']($request);
  182. $query_this_board = 'b.id_board = {int:board}';
  183. $query_parameters['board'] = $board;
  184. // If this board has a significant number of posts in it...
  185. if ($total_posts > 80 && $total_posts > $modSettings['totalMessages'] / 10)
  186. {
  187. $query_this_board .= '
  188. AND m.id_msg >= {int:max_id_msg}';
  189. $query_parameters['max_id_msg'] = max(0, $modSettings['maxMsgID'] - 600 - $_REQUEST['start'] * 10);
  190. }
  191. $context['page_index'] = constructPageIndex($scripturl . '?action=recent;board=' . $board . '.%1$d', $_REQUEST['start'], min(100, $total_posts), 10, true);
  192. }
  193. else
  194. {
  195. $query_this_board = '{query_wanna_see_board}' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
  196. AND b.id_board != {int:recycle_board}' : ''). '
  197. AND m.id_msg >= {int:max_id_msg}';
  198. $query_parameters['max_id_msg'] = max(0, $modSettings['maxMsgID'] - 100 - $_REQUEST['start'] * 6);
  199. $query_parameters['recycle_board'] = $modSettings['recycle_board'];
  200. // @todo This isn't accurate because we ignore the recycle bin.
  201. $context['page_index'] = constructPageIndex($scripturl . '?action=recent', $_REQUEST['start'], min(100, $modSettings['totalMessages']), 10, false);
  202. }
  203. $context['linktree'][] = array(
  204. 'url' => $scripturl . '?action=recent' . (empty($board) ? (empty($_REQUEST['c']) ? '' : ';c=' . (int) $_REQUEST['c']) : ';board=' . $board . '.0'),
  205. 'name' => $context['page_title']
  206. );
  207. $key = 'recent-' . $user_info['id'] . '-' . md5(serialize(array_diff_key($query_parameters, array('max_id_msg' => 0)))) . '-' . (int) $_REQUEST['start'];
  208. if (empty($modSettings['cache_enable']) || ($messages = cache_get_data($key, 120)) == null)
  209. {
  210. $done = false;
  211. while (!$done)
  212. {
  213. // Find the 10 most recent messages they can *view*.
  214. // @todo SLOW This query is really slow still, probably?
  215. $request = $smcFunc['db_query']('', '
  216. SELECT m.id_msg
  217. FROM {db_prefix}messages AS m
  218. INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)
  219. WHERE ' . $query_this_board . '
  220. AND m.approved = {int:is_approved}
  221. ORDER BY m.id_msg DESC
  222. LIMIT {int:offset}, {int:limit}',
  223. array_merge($query_parameters, array(
  224. 'is_approved' => 1,
  225. 'offset' => $_REQUEST['start'],
  226. 'limit' => 10,
  227. ))
  228. );
  229. // If we don't have 10 results, try again with an unoptimized version covering all rows, and cache the result.
  230. if (isset($query_parameters['max_id_msg']) && $smcFunc['db_num_rows']($request) < 10)
  231. {
  232. $smcFunc['db_free_result']($request);
  233. $query_this_board = str_replace('AND m.id_msg >= {int:max_id_msg}', '', $query_this_board);
  234. $cache_results = true;
  235. unset($query_parameters['max_id_msg']);
  236. }
  237. else
  238. $done = true;
  239. }
  240. $messages = array();
  241. while ($row = $smcFunc['db_fetch_assoc']($request))
  242. $messages[] = $row['id_msg'];
  243. $smcFunc['db_free_result']($request);
  244. if (!empty($cache_results))
  245. cache_put_data($key, $messages, 120);
  246. }
  247. // Nothing here... Or at least, nothing you can see...
  248. if (empty($messages))
  249. {
  250. $context['posts'] = array();
  251. return;
  252. }
  253. // Get all the most recent posts.
  254. $request = $smcFunc['db_query']('', '
  255. SELECT
  256. m.id_msg, m.subject, m.smileys_enabled, m.poster_time, m.body, m.id_topic, t.id_board, b.id_cat,
  257. b.name AS bname, c.name AS cname, t.num_replies, m.id_member, m2.id_member AS id_first_member,
  258. IFNULL(mem2.real_name, m2.poster_name) AS first_poster_name, t.id_first_msg,
  259. IFNULL(mem.real_name, m.poster_name) AS poster_name, t.id_last_msg
  260. FROM {db_prefix}messages AS m
  261. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
  262. INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
  263. INNER JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat)
  264. INNER JOIN {db_prefix}messages AS m2 ON (m2.id_msg = t.id_first_msg)
  265. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
  266. LEFT JOIN {db_prefix}members AS mem2 ON (mem2.id_member = m2.id_member)
  267. WHERE m.id_msg IN ({array_int:message_list})
  268. ORDER BY m.id_msg DESC
  269. LIMIT ' . count($messages),
  270. array(
  271. 'message_list' => $messages,
  272. )
  273. );
  274. $counter = $_REQUEST['start'] + 1;
  275. $context['posts'] = array();
  276. $board_ids = array('own' => array(), 'any' => array());
  277. while ($row = $smcFunc['db_fetch_assoc']($request))
  278. {
  279. // Censor everything.
  280. censorText($row['body']);
  281. censorText($row['subject']);
  282. // BBC-atize the message.
  283. $row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']);
  284. // And build the array.
  285. $context['posts'][$row['id_msg']] = array(
  286. 'id' => $row['id_msg'],
  287. 'counter' => $counter++,
  288. 'alternate' => $counter % 2,
  289. 'category' => array(
  290. 'id' => $row['id_cat'],
  291. 'name' => $row['cname'],
  292. 'href' => $scripturl . '#c' . $row['id_cat'],
  293. 'link' => '<a href="' . $scripturl . '#c' . $row['id_cat'] . '">' . $row['cname'] . '</a>'
  294. ),
  295. 'board' => array(
  296. 'id' => $row['id_board'],
  297. 'name' => $row['bname'],
  298. 'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
  299. 'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['bname'] . '</a>'
  300. ),
  301. 'topic' => $row['id_topic'],
  302. 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'],
  303. 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'] . '" rel="nofollow">' . $row['subject'] . '</a>',
  304. 'start' => $row['num_replies'],
  305. 'subject' => $row['subject'],
  306. 'time' => timeformat($row['poster_time']),
  307. 'timestamp' => forum_time(true, $row['poster_time']),
  308. 'first_poster' => array(
  309. 'id' => $row['id_first_member'],
  310. 'name' => $row['first_poster_name'],
  311. 'href' => empty($row['id_first_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_first_member'],
  312. '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>'
  313. ),
  314. 'poster' => array(
  315. 'id' => $row['id_member'],
  316. 'name' => $row['poster_name'],
  317. 'href' => empty($row['id_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member'],
  318. 'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>'
  319. ),
  320. 'message' => $row['body'],
  321. 'can_reply' => false,
  322. 'can_mark_notify' => !$context['user']['is_guest'],
  323. 'can_delete' => false,
  324. '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()),
  325. );
  326. if ($user_info['id'] == $row['id_first_member'])
  327. $board_ids['own'][$row['id_board']][] = $row['id_msg'];
  328. $board_ids['any'][$row['id_board']][] = $row['id_msg'];
  329. }
  330. $smcFunc['db_free_result']($request);
  331. // There might be - and are - different permissions between any and own.
  332. $permissions = array(
  333. 'own' => array(
  334. 'post_reply_own' => 'can_reply',
  335. 'delete_own' => 'can_delete',
  336. ),
  337. 'any' => array(
  338. 'post_reply_any' => 'can_reply',
  339. 'delete_any' => 'can_delete',
  340. )
  341. );
  342. // Now go through all the permissions, looking for boards they can do it on.
  343. foreach ($permissions as $type => $list)
  344. {
  345. foreach ($list as $permission => $allowed)
  346. {
  347. // They can do it on these boards...
  348. $boards = boardsAllowedTo($permission);
  349. // If 0 is the only thing in the array, they can do it everywhere!
  350. if (!empty($boards) && $boards[0] == 0)
  351. $boards = array_keys($board_ids[$type]);
  352. // Go through the boards, and look for posts they can do this on.
  353. foreach ($boards as $board_id)
  354. {
  355. // Hmm, they have permission, but there are no topics from that board on this page.
  356. if (!isset($board_ids[$type][$board_id]))
  357. continue;
  358. // Okay, looks like they can do it for these posts.
  359. foreach ($board_ids[$type][$board_id] as $counter)
  360. if ($type == 'any' || $context['posts'][$counter]['poster']['id'] == $user_info['id'])
  361. $context['posts'][$counter][$allowed] = true;
  362. }
  363. }
  364. }
  365. $quote_enabled = empty($modSettings['disabledBBC']) || !in_array('quote', explode(',', $modSettings['disabledBBC']));
  366. foreach ($context['posts'] as $counter => $dummy)
  367. {
  368. // Some posts - the first posts - can't just be deleted.
  369. $context['posts'][$counter]['can_delete'] &= $context['posts'][$counter]['delete_possible'];
  370. // And some cannot be quoted...
  371. $context['posts'][$counter]['can_quote'] = $context['posts'][$counter]['can_reply'] && $quote_enabled;
  372. }
  373. }
  374. /**
  375. * Find unread topics and replies.
  376. */
  377. function UnreadTopics()
  378. {
  379. global $board, $txt, $scripturl, $sourcedir;
  380. global $user_info, $context, $settings, $modSettings, $smcFunc, $options;
  381. // Guests can't have unread things, we don't know anything about them.
  382. is_not_guest();
  383. // Prefetching + lots of MySQL work = bad mojo.
  384. if (isset($_SERVER['HTTP_X_MOZ']) && $_SERVER['HTTP_X_MOZ'] == 'prefetch')
  385. {
  386. ob_end_clean();
  387. header('HTTP/1.1 403 Forbidden');
  388. die;
  389. }
  390. $context['showCheckboxes'] = !empty($options['display_quick_mod']) && $options['display_quick_mod'] == 1 && $settings['show_mark_read'];
  391. $context['showing_all_topics'] = isset($_GET['all']);
  392. $context['start'] = (int) $_REQUEST['start'];
  393. $context['topics_per_page'] = empty($modSettings['disableCustomPerPage']) && !empty($options['topics_per_page']) && !WIRELESS ? $options['topics_per_page'] : $modSettings['defaultMaxTopics'];
  394. if ($_REQUEST['action'] == 'unread')
  395. $context['page_title'] = $context['showing_all_topics'] ? $txt['unread_topics_all'] : $txt['unread_topics_visit'];
  396. else
  397. $context['page_title'] = $txt['unread_replies'];
  398. if ($context['showing_all_topics'] && !empty($context['load_average']) && !empty($modSettings['loadavg_allunread']) && $context['load_average'] >= $modSettings['loadavg_allunread'])
  399. fatal_lang_error('loadavg_allunread_disabled', false);
  400. elseif ($_REQUEST['action'] != 'unread' && !empty($context['load_average']) && !empty($modSettings['loadavg_unreadreplies']) && $context['load_average'] >= $modSettings['loadavg_unreadreplies'])
  401. fatal_lang_error('loadavg_unreadreplies_disabled', false);
  402. elseif (!$context['showing_all_topics'] && $_REQUEST['action'] == 'unread' && !empty($context['load_average']) && !empty($modSettings['loadavg_unread']) && $context['load_average'] >= $modSettings['loadavg_unread'])
  403. fatal_lang_error('loadavg_unread_disabled', false);
  404. // Parameters for the main query.
  405. $query_parameters = array();
  406. // Are we specifying any specific board?
  407. if (isset($_REQUEST['children']) && (!empty($board) || !empty($_REQUEST['boards'])))
  408. {
  409. $boards = array();
  410. if (!empty($_REQUEST['boards']))
  411. {
  412. $_REQUEST['boards'] = explode(',', $_REQUEST['boards']);
  413. foreach ($_REQUEST['boards'] as $b)
  414. $boards[] = (int) $b;
  415. }
  416. if (!empty($board))
  417. $boards[] = (int) $board;
  418. // 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
  419. $request = $smcFunc['db_query']('', '
  420. SELECT b.id_board, b.id_parent
  421. FROM {db_prefix}boards AS b
  422. WHERE {query_wanna_see_board}
  423. AND b.child_level > {int:no_child}
  424. AND b.id_board NOT IN ({array_int:boards})
  425. ORDER BY child_level ASC
  426. ',
  427. array(
  428. 'no_child' => 0,
  429. 'boards' => $boards,
  430. )
  431. );
  432. while ($row = $smcFunc['db_fetch_assoc']($request))
  433. if (in_array($row['id_parent'], $boards))
  434. $boards[] = $row['id_board'];
  435. $smcFunc['db_free_result']($request);
  436. if (empty($boards))
  437. fatal_lang_error('error_no_boards_selected');
  438. $query_this_board = 'id_board IN ({array_int:boards})';
  439. $query_parameters['boards'] = $boards;
  440. $context['querystring_board_limits'] = ';boards=' . implode(',', $boards) . ';start=%d';
  441. }
  442. elseif (!empty($board))
  443. {
  444. $query_this_board = 'id_board = {int:board}';
  445. $query_parameters['board'] = $board;
  446. $context['querystring_board_limits'] = ';board=' . $board . '.%1$d';
  447. }
  448. elseif (!empty($_REQUEST['boards']))
  449. {
  450. $_REQUEST['boards'] = explode(',', $_REQUEST['boards']);
  451. foreach ($_REQUEST['boards'] as $i => $b)
  452. $_REQUEST['boards'][$i] = (int) $b;
  453. $request = $smcFunc['db_query']('', '
  454. SELECT b.id_board
  455. FROM {db_prefix}boards AS b
  456. WHERE {query_see_board}
  457. AND b.id_board IN ({array_int:board_list})',
  458. array(
  459. 'board_list' => $_REQUEST['boards'],
  460. )
  461. );
  462. $boards = array();
  463. while ($row = $smcFunc['db_fetch_assoc']($request))
  464. $boards[] = $row['id_board'];
  465. $smcFunc['db_free_result']($request);
  466. if (empty($boards))
  467. fatal_lang_error('error_no_boards_selected');
  468. $query_this_board = 'id_board IN ({array_int:boards})';
  469. $query_parameters['boards'] = $boards;
  470. $context['querystring_board_limits'] = ';boards=' . implode(',', $boards) . ';start=%1$d';
  471. }
  472. elseif (!empty($_REQUEST['c']))
  473. {
  474. $_REQUEST['c'] = explode(',', $_REQUEST['c']);
  475. foreach ($_REQUEST['c'] as $i => $c)
  476. $_REQUEST['c'][$i] = (int) $c;
  477. $see_board = isset($_REQUEST['action']) && $_REQUEST['action'] == 'unreadreplies' ? 'query_see_board' : 'query_wanna_see_board';
  478. $request = $smcFunc['db_query']('', '
  479. SELECT b.id_board
  480. FROM {db_prefix}boards AS b
  481. WHERE ' . $user_info[$see_board] . '
  482. AND b.id_cat IN ({array_int:id_cat})',
  483. array(
  484. 'id_cat' => $_REQUEST['c'],
  485. )
  486. );
  487. $boards = array();
  488. while ($row = $smcFunc['db_fetch_assoc']($request))
  489. $boards[] = $row['id_board'];
  490. $smcFunc['db_free_result']($request);
  491. if (empty($boards))
  492. fatal_lang_error('error_no_boards_selected');
  493. $query_this_board = 'id_board IN ({array_int:boards})';
  494. $query_parameters['boards'] = $boards;
  495. $context['querystring_board_limits'] = ';c=' . implode(',', $_REQUEST['c']) . ';start=%1$d';
  496. }
  497. else
  498. {
  499. $see_board = isset($_REQUEST['action']) && $_REQUEST['action'] == 'unreadreplies' ? 'query_see_board' : 'query_wanna_see_board';
  500. // Don't bother to show deleted posts!
  501. $request = $smcFunc['db_query']('', '
  502. SELECT b.id_board
  503. FROM {db_prefix}boards AS b
  504. WHERE ' . $user_info[$see_board] . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
  505. AND b.id_board != {int:recycle_board}' : ''),
  506. array(
  507. 'recycle_board' => (int) $modSettings['recycle_board'],
  508. )
  509. );
  510. $boards = array();
  511. while ($row = $smcFunc['db_fetch_assoc']($request))
  512. $boards[] = $row['id_board'];
  513. $smcFunc['db_free_result']($request);
  514. if (empty($boards))
  515. fatal_lang_error('error_no_boards_available', false);
  516. $query_this_board = 'id_board IN ({array_int:boards})';
  517. $query_parameters['boards'] = $boards;
  518. $context['querystring_board_limits'] = ';start=%1$d';
  519. $context['no_board_limits'] = true;
  520. }
  521. $sort_methods = array(
  522. 'subject' => 'ms.subject',
  523. 'starter' => 'IFNULL(mems.real_name, ms.poster_name)',
  524. 'replies' => 't.num_replies',
  525. 'views' => 't.num_views',
  526. 'first_post' => 't.id_topic',
  527. 'last_post' => 't.id_last_msg'
  528. );
  529. // The default is the most logical: newest first.
  530. if (!isset($_REQUEST['sort']) || !isset($sort_methods[$_REQUEST['sort']]))
  531. {
  532. $context['sort_by'] = 'last_post';
  533. $_REQUEST['sort'] = 't.id_last_msg';
  534. $ascending = isset($_REQUEST['asc']);
  535. $context['querystring_sort_limits'] = $ascending ? ';asc' : '';
  536. }
  537. // But, for other methods the default sort is ascending.
  538. else
  539. {
  540. $context['sort_by'] = $_REQUEST['sort'];
  541. $_REQUEST['sort'] = $sort_methods[$_REQUEST['sort']];
  542. $ascending = !isset($_REQUEST['desc']);
  543. $context['querystring_sort_limits'] = ';sort=' . $context['sort_by'] . ($ascending ? '' : ';desc');
  544. }
  545. $context['sort_direction'] = $ascending ? 'up' : 'down';
  546. if (!empty($_REQUEST['c']) && is_array($_REQUEST['c']) && count($_REQUEST['c']) == 1)
  547. {
  548. $request = $smcFunc['db_query']('', '
  549. SELECT name
  550. FROM {db_prefix}categories
  551. WHERE id_cat = {int:id_cat}
  552. LIMIT 1',
  553. array(
  554. 'id_cat' => (int) $_REQUEST['c'][0],
  555. )
  556. );
  557. list ($name) = $smcFunc['db_fetch_row']($request);
  558. $smcFunc['db_free_result']($request);
  559. $context['linktree'][] = array(
  560. 'url' => $scripturl . '#c' . (int) $_REQUEST['c'][0],
  561. 'name' => $name
  562. );
  563. }
  564. $context['linktree'][] = array(
  565. 'url' => $scripturl . '?action=' . $_REQUEST['action'] . sprintf($context['querystring_board_limits'], 0) . $context['querystring_sort_limits'],
  566. 'name' => $_REQUEST['action'] == 'unread' ? $txt['unread_topics_visit'] : $txt['unread_replies']
  567. );
  568. if ($context['showing_all_topics'])
  569. $context['linktree'][] = array(
  570. 'url' => $scripturl . '?action=' . $_REQUEST['action'] . ';all' . sprintf($context['querystring_board_limits'], 0) . $context['querystring_sort_limits'],
  571. 'name' => $txt['unread_topics_all']
  572. );
  573. else
  574. $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']));
  575. if (WIRELESS)
  576. $context['sub_template'] = WIRELESS_PROTOCOL . '_recent';
  577. else
  578. {
  579. loadTemplate('Recent');
  580. loadTemplate('MessageIndex');
  581. $context['sub_template'] = $_REQUEST['action'] == 'unread' ? 'unread' : 'replies';
  582. }
  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
  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_unwatch'] ? '
  678. AND lt.unwatched != 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' . ($modSettings['postmod_active'] ? '
  699. AND t.approved = {int:is_approved}' : '') . ($modSettings['enable_unwatch'] ? '
  700. AND lt.unwatched != 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($sourcedir . '/Subs-Boards.php');
  727. markBoardsRead(empty($boards) ? $board : $boards);
  728. $context['topics'] = array();
  729. $context['no_topic_listing'] = true;
  730. if ($context['querystring_board_limits'] == ';start=%1$d')
  731. $context['querystring_board_limits'] = '';
  732. else
  733. $context['querystring_board_limits'] = sprintf($context['querystring_board_limits'], $_REQUEST['start']);
  734. return;
  735. }
  736. else
  737. $min_message = (int) $min_message;
  738. $request = $smcFunc['db_query']('substring', '
  739. SELECT ' . $select_clause . '
  740. FROM {db_prefix}messages AS ms
  741. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = ms.id_topic AND t.id_first_msg = ms.id_msg)
  742. INNER JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)
  743. LEFT JOIN {db_prefix}boards AS b ON (b.id_board = ms.id_board)
  744. LEFT JOIN {db_prefix}members AS mems ON (mems.id_member = ms.id_member)
  745. LEFT JOIN {db_prefix}members AS meml ON (meml.id_member = ml.id_member)
  746. LEFT JOIN {db_prefix}log_topics_unread AS lt ON (lt.id_topic = t.id_topic)
  747. LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})
  748. WHERE b.' . $query_this_board . '
  749. AND t.id_last_msg >= {int:min_message}
  750. AND IFNULL(lt.id_msg, IFNULL(lmr.id_msg, 0)) < t.id_last_msg' . ($modSettings['postmod_active'] ? '
  751. AND ms.approved = {int:is_approved}' : '') . ($modSettings['enable_unwatch'] ? '
  752. AND lt.unwatched != 1' : '') . '
  753. ORDER BY {raw:sort}
  754. LIMIT {int:offset}, {int:limit}',
  755. array_merge($query_parameters, array(
  756. 'current_member' => $user_info['id'],
  757. 'min_message' => $min_message,
  758. 'is_approved' => 1,
  759. 'sort' => $_REQUEST['sort'] . ($ascending ? '' : ' DESC'),
  760. 'offset' => $_REQUEST['start'],
  761. 'limit' => $context['topics_per_page'],
  762. ))
  763. );
  764. }
  765. elseif ($is_topics)
  766. {
  767. $request = $smcFunc['db_query']('', '
  768. SELECT COUNT(*), MIN(t.id_last_msg)
  769. FROM {db_prefix}topics AS t' . (!empty($have_temp_table) ? '
  770. LEFT JOIN {db_prefix}log_topics_unread AS lt ON (lt.id_topic = t.id_topic)' : '
  771. LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})') . '
  772. LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})
  773. WHERE t.' . $query_this_board . ($context['showing_all_topics'] && !empty($earliest_msg) ? '
  774. AND t.id_last_msg > {int:earliest_msg}' : (!$context['showing_all_topics'] && empty($_SESSION['first_login']) ? '
  775. AND t.id_last_msg > {int:id_msg_last_visit}' : '')) . '
  776. AND IFNULL(lt.id_msg, IFNULL(lmr.id_msg, 0)) < t.id_last_msg' . ($modSettings['postmod_active'] ? '
  777. AND t.approved = {int:is_approved}' : '') . ($modSettings['enable_unwatch'] ? '
  778. AND lt.unwatched != 1' : ''),
  779. array_merge($query_parameters, array(
  780. 'current_member' => $user_info['id'],
  781. 'earliest_msg' => !empty($earliest_msg) ? $earliest_msg : 0,
  782. 'id_msg_last_visit' => $_SESSION['id_msg_last_visit'],
  783. 'is_approved' => 1,
  784. ))
  785. );
  786. list ($num_topics, $min_message) = $smcFunc['db_fetch_row']($request);
  787. $smcFunc['db_free_result']($request);
  788. // Make sure the starting place makes sense and construct the page index.
  789. $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);
  790. $context['current_page'] = (int) $_REQUEST['start'] / $context['topics_per_page'];
  791. $context['links'] = array(
  792. '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'] : '',
  793. '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'] : '',
  794. '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'] : '',
  795. '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'] : '',
  796. 'up' => $scripturl,
  797. );
  798. $context['page_info'] = array(
  799. 'current_page' => $_REQUEST['start'] / $context['topics_per_page'] + 1,
  800. 'num_pages' => floor(($num_topics - 1) / $context['topics_per_page']) + 1
  801. );
  802. if ($num_topics == 0)
  803. {
  804. // Is this an all topics query?
  805. if ($context['showing_all_topics'])
  806. {
  807. // Since there are no unread topics, mark the boards as read!
  808. require_once($sourcedir . '/Subs-Boards.php');
  809. markBoardsRead(empty($boards) ? $board : $boards);
  810. }
  811. $context['topics'] = array();
  812. $context['no_topic_listing'] = true;
  813. if ($context['querystring_board_limits'] == ';start=%d')
  814. $context['querystring_board_limits'] = '';
  815. else
  816. $context['querystring_board_limits'] = sprintf($context['querystring_board_limits'], $_REQUEST['start']);
  817. return;
  818. }
  819. else
  820. $min_message = (int) $min_message;
  821. $request = $smcFunc['db_query']('substring', '
  822. SELECT ' . $select_clause . '
  823. FROM {db_prefix}messages AS ms
  824. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = ms.id_topic AND t.id_first_msg = ms.id_msg)
  825. INNER JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)
  826. LEFT JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
  827. LEFT JOIN {db_prefix}members AS mems ON (mems.id_member = ms.id_member)
  828. LEFT JOIN {db_prefix}members AS meml ON (meml.id_member = ml.id_member)' . (!empty($have_temp_table) ? '
  829. LEFT JOIN {db_prefix}log_topics_unread AS lt ON (lt.id_topic = t.id_topic)' : '
  830. LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})') . '
  831. LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})
  832. WHERE t.' . $query_this_board . '
  833. AND t.id_last_msg >= {int:min_message}
  834. AND IFNULL(lt.id_msg, IFNULL(lmr.id_msg, 0)) < ml.id_msg' . ($modSettings['postmod_active'] ? '
  835. AND ms.approved = {int:is_approved}' : '') . ($modSettings['enable_unwatch'] ? '
  836. AND lt.unwatched != 1' : '') . '
  837. ORDER BY {raw:order}
  838. LIMIT {int:offset}, {int:limit}',
  839. array_merge($query_parameters, array(
  840. 'current_member' => $user_info['id'],
  841. 'min_message' => $min_message,
  842. 'is_approved' => 1,
  843. 'order' => $_REQUEST['sort'] . ($ascending ? '' : ' DESC'),
  844. 'offset' => $_REQUEST['start'],
  845. 'limit' => $context['topics_per_page'],
  846. ))
  847. );
  848. }
  849. else
  850. {
  851. if ($modSettings['totalMessages'] > 100000)
  852. {
  853. $smcFunc['db_query']('', '
  854. DROP TABLE IF EXISTS {db_prefix}topics_posted_in',
  855. array(
  856. )
  857. );
  858. $smcFunc['db_query']('', '
  859. DROP TABLE IF EXISTS {db_prefix}log_topics_posted_in',
  860. array(
  861. )
  862. );
  863. $sortKey_joins = array(
  864. 'ms.subject' => '
  865. INNER JOIN {db_prefix}messages AS ms ON (ms.id_msg = t.id_first_msg)',
  866. 'IFNULL(mems.real_name, ms.poster_name)' => '
  867. INNER JOIN {db_prefix}messages AS ms ON (ms.id_msg = t.id_first_msg)
  868. LEFT JOIN {db_prefix}members AS mems ON (mems.id_member = ms.id_member)',
  869. );
  870. // The main benefit of this temporary table is not that it's faster; it's that it avoids locks later.
  871. $have_temp_table = $smcFunc['db_query']('', '
  872. CREATE TEMPORARY TABLE {db_prefix}topics_posted_in (
  873. id_topic mediumint(8) unsigned NOT NULL default {string:string_zero},
  874. id_board smallint(5) unsigned NOT NULL default {string:string_zero},
  875. id_last_msg int(10) unsigned NOT NULL default {string:string_zero},
  876. id_msg int(10) unsigned NOT NULL default {string:string_zero},
  877. PRIMARY KEY (id_topic)
  878. )
  879. 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' : '') . '
  880. FROM {db_prefix}messages AS m
  881. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
  882. 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']] : '') . '
  883. WHERE m.id_member = {int:current_member}' . (!empty($board) ? '
  884. AND t.id_board = {int:current_board}' : '') . ($modSettings['postmod_active'] ? '
  885. AND t.approved = {int:is_approved}' : '') . ($modSettings['enable_unwatch'] ? '
  886. AND lt.unwatched != 1' : '') . '
  887. GROUP BY m.id_topic',
  888. array(
  889. 'current_board' => $board,
  890. 'current_member' => $user_info['id'],
  891. 'is_approved' => 1,
  892. 'string_zero' => '0',
  893. 'db_error_skip' => true,
  894. )
  895. ) !== false;
  896. // If that worked, create a sample of the log_topics table too.
  897. if ($have_temp_table)
  898. $have_temp_table = $smcFunc['db_query']('', '
  899. CREATE TEMPORARY TABLE {db_prefix}log_topics_posted_in (
  900. PRIMARY KEY (id_topic)
  901. )
  902. SELECT lt.id_topic, lt.id_msg
  903. FROM {db_prefix}log_topics AS lt
  904. INNER JOIN {db_prefix}topics_posted_in AS pi ON (pi.id_topic = lt.id_topic)
  905. WHERE lt.id_member = {int:current_member}',
  906. array(
  907. 'current_member' => $user_info['id'],
  908. 'db_error_skip' => true,
  909. )
  910. ) !== false;
  911. }
  912. if (!empty($have_temp_table))
  913. {
  914. $request = $smcFunc['db_query']('', '
  915. SELECT COUNT(*)
  916. FROM {db_prefix}topics_posted_in AS pi
  917. LEFT JOIN {db_prefix}log_topics_posted_in AS lt ON (lt.id_topic = pi.id_topic)
  918. WHERE pi.' . $query_this_board . '
  919. AND IFNULL(lt.id_msg, pi.id_msg) < pi.id_last_msg',
  920. array_merge($query_parameters, array(
  921. ))
  922. );
  923. list ($num_topics) = $smcFunc['db_fetch_row']($request);
  924. $smcFunc['db_free_result']($request);
  925. }
  926. else
  927. {
  928. $request = $smcFunc['db_query']('unread_fetch_topic_count', '
  929. SELECT COUNT(DISTINCT t.id_topic), MIN(t.id_last_msg)
  930. FROM {db_prefix}topics AS t
  931. INNER JOIN {db_prefix}messages AS m ON (m.id_topic = t.id_topic)
  932. LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})
  933. LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})
  934. WHERE t.' . $query_this_board . '
  935. AND m.id_member = {int:current_member}
  936. AND IFNULL(lt.id_msg, IFNULL(lmr.id_msg, 0)) < t.id_last_msg' . ($modSettings['postmod_active'] ? '
  937. AND t.approved = {int:is_approved}' : '') . ($modSettings['enable_unwatch'] ? '
  938. AND lt.unwatched != 1' : ''),
  939. array_merge($query_parameters, array(
  940. 'current_member' => $user_info['id'],
  941. 'is_approved' => 1,
  942. ))
  943. );
  944. list ($num_topics, $min_message) = $smcFunc['db_fetch_row']($request);
  945. $smcFunc['db_free_result']($request);
  946. }
  947. // Make sure the starting place makes sense and construct the page index.
  948. $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);
  949. $context['current_page'] = (int) $_REQUEST['start'] / $context['topics_per_page'];
  950. $context['links'] = array(
  951. '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'] : '',
  952. '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'] : '',
  953. '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'] : '',
  954. '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'] : '',
  955. 'up' => $scripturl,
  956. );
  957. $context['page_info'] = array(
  958. 'current_page' => $_REQUEST['start'] / $context['topics_per_page'] + 1,
  959. 'num_pages' => floor(($num_topics - 1) / $context['topics_per_page']) + 1
  960. );
  961. if ($num_topics == 0)
  962. {
  963. $context['topics'] = array();
  964. $context['no_topic_listing'] = true;
  965. if ($context['querystring_board_limits'] == ';start=%d')
  966. $context['querystring_board_limits'] = '';
  967. else
  968. $context['querystring_board_limits'] = sprintf($context['querystring_board_limits'], $_REQUEST['start']);
  969. return;
  970. }
  971. if (!empty($have_temp_table))
  972. $request = $smcFunc['db_query']('', '
  973. SELECT t.id_topic
  974. FROM {db_prefix}topics_posted_in AS t
  975. LEFT JOIN {db_prefix}log_topics_posted_in AS lt ON (lt.id_topic = t.id_topic)
  976. WHERE t.' . $query_this_board . '
  977. AND IFNULL(lt.id_msg, t.id_msg) < t.id_last_msg
  978. ORDER BY {raw:order}
  979. LIMIT {int:offset}, {int:limit}',
  980. array_merge($query_parameters, array(
  981. 'order' => (in_array($_REQUEST['sort'], array('t.id_last_msg', 't.id_topic')) ? $_REQUEST['sort'] : 't.sort_key') . ($ascending ? '' : ' DESC'),
  982. 'offset' => $_REQUEST['start'],
  983. 'limit' => $context['topics_per_page'],
  984. ))
  985. );
  986. else
  987. $request = $smcFunc['db_query']('unread_replies', '
  988. SELECT DISTINCT t.id_topic
  989. FROM {db_prefix}topics AS t
  990. 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 ? '' : '
  991. INNER JOIN {db_prefix}messages AS ms ON (ms.id_msg = t.id_first_msg)') . (strpos($_REQUEST['sort'], 'mems.') === false ? '' : '
  992. LEFT JOIN {db_prefix}members AS mems ON (mems.id_member = ms.id_member)') . '
  993. LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})
  994. LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})
  995. WHERE t.' . $query_this_board . '
  996. AND t.id_last_msg >= {int:min_message}
  997. AND (IFNULL(lt.id_msg, IFNULL(lmr.id_msg, 0))) < t.id_last_msg
  998. AND t.approved = {int:is_approved}' . ($modSettings['enable_unwatch'] ? '
  999. AND lt.unwatched != 1' : '') . '
  1000. ORDER BY {raw:order}
  1001. LIMIT {int:offset}, {int:limit}',
  1002. array_merge($query_parameters, array(
  1003. 'current_member' => $user_info['id'],
  1004. 'min_message' => (int) $min_message,
  1005. 'is_approved' => 1,
  1006. 'order' => $_REQUEST['sort'] . ($ascending ? '' : ' DESC'),
  1007. 'offset' => $_REQUEST['start'],
  1008. 'limit' => $context['topics_per_page'],
  1009. 'sort' => $_REQUEST['sort'],
  1010. ))
  1011. );
  1012. $topics = array();
  1013. while ($row = $smcFunc['db_fetch_assoc']($request))
  1014. $topics[] = $row['id_topic'];
  1015. $smcFunc['db_free_result']($request);
  1016. // Sanity... where have you gone?
  1017. if (empty($topics))
  1018. {
  1019. $context['topics'] = array();
  1020. $context['no_topic_listing'] = true;
  1021. if ($context['querystring_board_limits'] == ';start=%d')
  1022. $context['querystring_board_limits'] = '';
  1023. else
  1024. $context['querystring_board_limits'] = sprintf($context['querystring_board_limits'], $_REQUEST['start']);
  1025. return;
  1026. }
  1027. $request = $smcFunc['db_query']('substring', '
  1028. SELECT ' . $select_clause . '
  1029. FROM {db_prefix}topics AS t
  1030. INNER JOIN {db_prefix}messages AS ms ON (ms.id_topic = t.id_topic AND ms.id_msg = t.id_first_msg)
  1031. INNER JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)
  1032. INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
  1033. LEFT JOIN {db_prefix}members AS mems ON (mems.id_member = ms.id_member)
  1034. LEFT JOIN {db_prefix}members AS meml ON (meml.id_member = ml.id_member)
  1035. LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})
  1036. LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})
  1037. WHERE t.id_topic IN ({array_int:topic_list})
  1038. ORDER BY ' . $_REQUEST['sort'] . ($ascending ? '' : ' DESC') . '
  1039. LIMIT ' . count($topics),
  1040. array(
  1041. 'current_member' => $user_info['id'],
  1042. 'topic_list' => $topics,
  1043. )
  1044. );
  1045. }
  1046. $context['topics'] = array();
  1047. $topic_ids = array();
  1048. while ($row = $smcFunc['db_fetch_assoc']($request))
  1049. {
  1050. if ($row['id_poll'] > 0 && $modSettings['pollMode'] == '0')
  1051. continue;
  1052. $topic_ids[] = $row['id_topic'];
  1053. if (!empty($settings['message_index_preview']))
  1054. {
  1055. // Limit them to 128 characters - do this FIRST because it's a lot of wasted censoring otherwise.
  1056. $row['first_body'] = strip_tags(strtr(parse_bbc($row['first_body'], $row['first_smileys'], $row['id_first_msg']), array('<br />' => '&#10;')));
  1057. if ($smcFunc['strlen']($row['first_body']) > 128)
  1058. $row['first_body'] = $smcFunc['substr']($row['first_body'], 0, 128) . '...';
  1059. $row['last_body'] = strip_tags(strtr(parse_bbc($row['last_body'], $row['last_smileys'], $row['id_last_msg']), array('<br />' => '&#10;')));
  1060. if ($smcFunc['strlen']($row['last_body']) > 128)
  1061. $row['last_body'] = $smcFunc['substr']($row['last_body'], 0, 128) . '...';
  1062. // Censor the subject and message preview.
  1063. censorText($row['first_subject']);
  1064. censorText($row['first_body']);
  1065. // Don't censor them twice!
  1066. if ($row['id_first_msg'] == $row['id_last_msg'])
  1067. {
  1068. $row['last_subject'] = $row['first_subject'];
  1069. $row['last_body'] = $row['first_body'];
  1070. }
  1071. else
  1072. {
  1073. censorText($row['last_subject']);
  1074. censorText($row['last_body']);
  1075. }
  1076. }
  1077. else
  1078. {
  1079. $row['first_body'] = '';
  1080. $row['last_body'] = '';
  1081. censorText($row['first_subject']);
  1082. if ($row['id_first_msg'] == $row['id_last_msg'])
  1083. $row['last_subject'] = $row['first_subject'];
  1084. else
  1085. censorText($row['last_subject']);
  1086. }
  1087. // Decide how many pages the topic should have.
  1088. $topic_length = $row['num_replies'] + 1;
  1089. $messages_per_page = empty($modSettings['disableCustomPerPage']) && !empty($options['messages_per_page']) && !WIRELESS ? $options['messages_per_page'] : $modSettings['defaultMaxMessages'];
  1090. if ($topic_length > $messages_per_page)
  1091. {
  1092. $tmppages = array();
  1093. $tmpa = 1;
  1094. for ($tmpb = 0; $tmpb < $topic_length; $tmpb += $messages_per_page)
  1095. {
  1096. $tmppages[] = '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.' . $tmpb . ';topicseen">' . $tmpa . '</a>';
  1097. $tmpa++;
  1098. }
  1099. // Show links to all the pages?
  1100. if (count($tmppages) <= 5)
  1101. $pages = '&#171; ' . implode(' ', $tmppages);
  1102. // Or skip a few?
  1103. else
  1104. $pages = '&#171; ' . $tmppages[0] . ' ' . $tmppages[1] . ' ... ' . $tmppages[count($tmppages) - 2] . ' ' . $tmppages[count($tmppages) - 1];
  1105. if (!empty($modSettings['enableAllMessages']) && $topic_length < $modSettings['enableAllMessages'])
  1106. $pages .= ' &nbsp;<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0;all">' . $txt['all'] . '</a>';
  1107. $pages .= ' &#187;';
  1108. }
  1109. else
  1110. $pages = '';
  1111. // We need to check the topic icons exist... you can never be too sure!
  1112. if (!empty($modSettings['messageIconChecks_enable']))
  1113. {
  1114. // First icon first... as you'd expect.
  1115. if (!isset($context['icon_sources'][$row['first_icon']]))
  1116. $context['icon_sources'][$row['first_icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['first_icon'] . '.png') ? 'images_url' : 'default_images_url';
  1117. // Last icon... last... duh.
  1118. if (!isset($context['icon_sources'][$row['last_icon']]))
  1119. $context['icon_sources'][$row['last_icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['last_icon'] . '.png') ? 'images_url' : 'default_images_url';
  1120. }
  1121. // And build the array.
  1122. $context['topics'][$row['id_topic']] = array(
  1123. 'id' => $row['id_topic'],
  1124. 'first_post' => array(
  1125. 'id' => $row['id_first_msg'],
  1126. 'member' => array(
  1127. 'name' => $row['first_poster_name'],
  1128. 'id' => $row['id_first_member'],
  1129. 'href' => $scripturl . '?action=profile;u=' . $row['id_first_member'],
  1130. 'link' => !empty($row['id_first_member']) ? '<a class="preview" href="' . $scripturl . '?action=profile;u=' . $row['id_first_member'] . '" title="' . $txt['profile_of'] . ' ' . $row['first_poster_name'] . '">' . $row['first_poster_name'] . '</a>' : $row['first_poster_name']
  1131. ),
  1132. 'time' => timeformat($row['first_poster_time']),
  1133. 'timestamp' => forum_time(true, $row['first_poster_time']),
  1134. 'subject' => $row['first_subject'],
  1135. 'preview' => $row['first_body'],
  1136. 'icon' => $row['first_icon'],
  1137. 'icon_url' => $settings[$context['icon_sources'][$row['first_icon']]] . '/post/' . $row['first_icon'] . '.png',
  1138. 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.0;topicseen',
  1139. 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0;topicseen">' . $row['first_subject'] . '</a>'
  1140. ),
  1141. 'last_post' => array(
  1142. 'id' => $row['id_last_msg'],
  1143. 'member' => array(
  1144. 'name' => $row['last_poster_name'],
  1145. 'id' => $row['id_last_member'],
  1146. 'href' => $scripturl . '?action=profile;u=' . $row['id_last_member'],
  1147. 'link' => !empty($row['id_last_member']) ? '<a href="' . $scripturl . '?action=profile;u=' . $row['id_last_member'] . '">' . $row['last_poster_name'] . '</a>' : $row['last_poster_name']
  1148. ),
  1149. 'time' => timeformat($row['last_poster_time']),
  1150. 'timestamp' => forum_time(true, $row['last_poster_time']),
  1151. 'subject' => $row['last_subject'],
  1152. 'preview' => $row['last_body'],
  1153. 'icon' => $row['last_icon'],
  1154. 'icon_url' => $settings[$context['icon_sources'][$row['last_icon']]] . '/post/' . $row['last_icon'] . '.png',
  1155. 'href' => $scripturl . '?topic=' . $row['id_topic'] . ($row['num_replies'] == 0 ? '.0' : '.msg' . $row['id_last_msg']) . ';topicseen#msg' . $row['id_last_msg'],
  1156. 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . ($row['num_replies'] == 0 ? '.0' : '.msg' . $row['id_last_msg']) . ';topicseen#msg' . $row['id_last_msg'] . '" rel="nofollow">' . $row['last_subject'] . '</a>'
  1157. ),
  1158. 'new_from' => $row['new_from'],
  1159. 'new_href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['new_from'] . ';topicseen#new',
  1160. 'href' => $scripturl . '?topic=' . $row['id_topic'] . ($row['num_replies'] == 0 ? '.0' : '.msg' . $row['new_from']) . ';topicseen' . ($row['num_replies'] == 0 ? '' : 'new'),
  1161. 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . ($row['num_replies'] == 0 ? '.0' : '.msg' . $row['new_from']) . ';topicseen#msg' . $row['new_from'] . '" rel="nofollow">' . $row['first_subject'] . '</a>',
  1162. 'is_sticky' => !empty($row['is_sticky']),
  1163. 'is_locked' => !empty($row['locked']),
  1164. 'is_poll' => $modSettings['pollMode'] == '1' && $row['id_poll'] > 0,
  1165. 'is_posted_in' => false,
  1166. 'icon' => $row['first_icon'],
  1167. 'icon_url' => $settings[$context['icon_sources'][$row['first_icon']]] . '/post/' . $row['first_icon'] . '.png',
  1168. 'subject' => $row['first_subject'],
  1169. 'pages' => $pages,
  1170. 'replies' => comma_format($row['num_replies']),
  1171. 'views' => comma_format($row['num_views']),
  1172. 'board' => array(
  1173. 'id' => $row['id_board'],
  1174. 'name' => $row['bname'],
  1175. 'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
  1176. 'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['bname'] . '</a>'
  1177. )
  1178. );
  1179. $context['topics'][$row['id_topic']]['first_post']['started_by'] = sprintf($txt['topic_started_by'], $context['topics'][$row['id_topic']]['first_post']['member']['link'], $context['topics'][$row['id_topic']]['board']['link']);
  1180. determineTopicClass($context['topics'][$row['id_topic']]);
  1181. }
  1182. $smcFunc['db_free_result']($request);
  1183. if ($is_topics && !empty($modSettings['enableParticipation']) && !empty($topic_ids))
  1184. {
  1185. $result = $smcFunc['db_query']('', '
  1186. SELECT id_topic
  1187. FROM {db_prefix}messages
  1188. WHERE id_topic IN ({array_int:topic_list})
  1189. AND id_member = {int:current_member}
  1190. GROUP BY id_topic
  1191. LIMIT {int:limit}',
  1192. array(
  1193. 'current_member' => $user_info['id'],
  1194. 'topic_list' => $topic_ids,
  1195. 'limit' => count($topic_ids),
  1196. )
  1197. );
  1198. while ($row = $smcFunc['db_fetch_assoc']($result))
  1199. {
  1200. if (empty($context['topics'][$row['id_topic']]['is_posted_in']))
  1201. {
  1202. $context['topics'][$row['id_topic']]['is_posted_in'] = true;
  1203. $context['topics'][$row['id_topic']]['class'] = 'my_' . $context['topics'][$row['id_topic']]['class'];
  1204. }
  1205. }
  1206. $smcFunc['db_free_result']($result);
  1207. }
  1208. $context['querystring_board_limits'] = sprintf($context['querystring_board_limits'], $_REQUEST['start']);
  1209. $context['topics_to_mark'] = implode('-', $topic_ids);
  1210. if ($settings['show_mark_read'])
  1211. {
  1212. // Build the recent button array.
  1213. if ($is_topics)
  1214. {
  1215. $context['recent_buttons'] = array(
  1216. 'markread' => array('text' => !empty($context['no_board_limits']) ? 'mark_as_read' : 'mark_read_short', 'image' => 'markread.png', 'lang' => true, 'custom' => 'onclick="return confirm(\'' . $txt['are_sure_mark_read'] . '\');"', 'url' => $scripturl . '?action=markasread;sa=' . (!empty($context['no_board_limits']) ? 'all' : 'board' . $context['querystring_board_limits']) . ';' . $context['session_var'] . '=' . $context['session_id']),
  1217. );
  1218. if ($context['showCheckboxes'])
  1219. $context['recent_buttons']['markselectread'] = array(
  1220. 'text' => 'quick_mod_markread',
  1221. 'image' => 'markselectedread.png',
  1222. 'lang' => true,
  1223. 'url' => 'javascript:document.quickModForm.submit();',
  1224. );
  1225. if (!empty($context['topics']) && !$context['showing_all_topics'])
  1226. $context['recent_buttons']['readall'] = array('text' => 'unread_topics_all', 'image' => 'markreadall.png', 'lang' => true, 'url' => $scripturl . '?action=unread;all' . $context['querystring_board_limits'], 'active' => true);
  1227. }
  1228. elseif (!$is_topics && isset($context['topics_to_mark']))
  1229. {
  1230. $context['recent_buttons'] = array(
  1231. 'markread' => array('text' => 'mark_as_read', 'image' => 'markread.png', 'lang' => true, 'custom' => 'onclick="return confirm(\'' . $txt['are_sure_mark_read'] . '\');"', 'url' => $scripturl . '?action=markasread;sa=unreadreplies;topics=' . $context['topics_to_mark'] . ';' . $context['session_var'] . '=' . $context['session_id']),
  1232. );
  1233. if ($context['showCheckboxes'])
  1234. $context['recent_buttons']['markselectread'] = array(
  1235. 'text' => 'quick_mod_markread',
  1236. 'image' => 'markselectedread.png',
  1237. 'lang' => true,
  1238. 'url' => 'javascript:document.quickModForm.submit();',
  1239. );
  1240. }
  1241. // Allow mods to add additional buttons here
  1242. call_integration_hook('integrate_recent_buttons');
  1243. }
  1244. $context['no_topic_listing'] = empty($context['topics']);
  1245. // Allow helpdesks and bug trackers and what not to add their own unread data (just add a template_layer to show custom stuff in the template!)
  1246. call_integration_hook('integrate_unread_list');
  1247. }
  1248. ?>