Recent.php 54 KB

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