Recent.php 59 KB

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