Recent.php 57 KB

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