Recent.php 54 KB

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