Recent.php 59 KB

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