Recent.php 57 KB

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