Recent.php 58 KB

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