MessageIndex.php 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251
  1. <?php
  2. /**
  3. * This file is what shows the listing of topics in a board.
  4. * It's just one or two functions, but don't under estimate it ;).
  5. *
  6. * Simple Machines Forum (SMF)
  7. *
  8. * @package SMF
  9. * @author Simple Machines http://www.simplemachines.org
  10. * @copyright 2014 Simple Machines and individual contributors
  11. * @license http://www.simplemachines.org/about/smf/license.php BSD
  12. *
  13. * @version 2.1 Alpha 1
  14. */
  15. if (!defined('SMF'))
  16. die('No direct access...');
  17. /**
  18. * Show the list of topics in this board, along with any child boards.
  19. */
  20. function MessageIndex()
  21. {
  22. global $txt, $scripturl, $board, $modSettings, $context;
  23. global $options, $settings, $board_info, $user_info, $smcFunc, $sourcedir;
  24. // If this is a redirection board head off.
  25. if ($board_info['redirect'])
  26. {
  27. $smcFunc['db_query']('', '
  28. UPDATE {db_prefix}boards
  29. SET num_posts = num_posts + 1
  30. WHERE id_board = {int:current_board}',
  31. array(
  32. 'current_board' => $board,
  33. )
  34. );
  35. redirectexit($board_info['redirect']);
  36. }
  37. if (WIRELESS)
  38. $context['sub_template'] = WIRELESS_PROTOCOL . '_messageindex';
  39. else
  40. loadTemplate('MessageIndex');
  41. if (!$user_info['is_guest'])
  42. {
  43. // We can't know they read it if we allow prefetches.
  44. // But we'll actually mark it read later after we've done everything else.
  45. if (isset($_SERVER['HTTP_X_MOZ']) && $_SERVER['HTTP_X_MOZ'] == 'prefetch')
  46. {
  47. ob_end_clean();
  48. header('HTTP/1.1 403 Prefetch Forbidden');
  49. die;
  50. }
  51. }
  52. $context['name'] = $board_info['name'];
  53. $context['description'] = $board_info['description'];
  54. if (!empty($board_info['description']))
  55. $context['meta_description'] = strip_tags($board_info['description']);
  56. // How many topics do we have in total?
  57. $board_info['total_topics'] = allowedTo('approve_posts') ? $board_info['num_topics'] + $board_info['unapproved_topics'] : $board_info['num_topics'] + $board_info['unapproved_user_topics'];
  58. // View all the topics, or just a few?
  59. $context['topics_per_page'] = empty($modSettings['disableCustomPerPage']) && !empty($options['topics_per_page']) && !WIRELESS ? $options['topics_per_page'] : $modSettings['defaultMaxTopics'];
  60. $context['messages_per_page'] = empty($modSettings['disableCustomPerPage']) && !empty($options['messages_per_page']) && !WIRELESS ? $options['messages_per_page'] : $modSettings['defaultMaxMessages'];
  61. $maxindex = isset($_REQUEST['all']) && !empty($modSettings['enableAllMessages']) ? $board_info['total_topics'] : $context['topics_per_page'];
  62. // Right, let's only index normal stuff!
  63. if (count($_GET) > 1)
  64. {
  65. $session_name = session_name();
  66. foreach ($_GET as $k => $v)
  67. {
  68. if (!in_array($k, array('board', 'start', $session_name)))
  69. $context['robot_no_index'] = true;
  70. }
  71. }
  72. if (!empty($_REQUEST['start']) && (!is_numeric($_REQUEST['start']) || $_REQUEST['start'] % $context['messages_per_page'] != 0))
  73. $context['robot_no_index'] = true;
  74. // If we can view unapproved messages and there are some build up a list.
  75. if (allowedTo('approve_posts') && ($board_info['unapproved_topics'] || $board_info['unapproved_posts']))
  76. {
  77. $untopics = $board_info['unapproved_topics'] ? '<a href="' . $scripturl . '?action=moderate;area=postmod;sa=topics;brd=' . $board . '">' . $board_info['unapproved_topics'] . '</a>' : 0;
  78. $unposts = $board_info['unapproved_posts'] ? '<a href="' . $scripturl . '?action=moderate;area=postmod;sa=posts;brd=' . $board . '">' . ($board_info['unapproved_posts'] - $board_info['unapproved_topics']) . '</a>' : 0;
  79. $context['unapproved_posts_message'] = sprintf($txt['there_are_unapproved_topics'], $untopics, $unposts, $scripturl . '?action=moderate;area=postmod;sa=' . ($board_info['unapproved_topics'] ? 'topics' : 'posts') . ';brd=' . $board);
  80. }
  81. // We only know these.
  82. if (isset($_REQUEST['sort']) && !in_array($_REQUEST['sort'], array('subject', 'starter', 'last_poster', 'replies', 'views', 'first_post', 'last_post')))
  83. $_REQUEST['sort'] = 'last_post';
  84. // Make sure the starting place makes sense and construct the page index.
  85. if (isset($_REQUEST['sort']))
  86. $context['page_index'] = constructPageIndex($scripturl . '?board=' . $board . '.%1$d;sort=' . $_REQUEST['sort'] . (isset($_REQUEST['desc']) ? ';desc' : ''), $_REQUEST['start'], $board_info['total_topics'], $maxindex, true);
  87. else
  88. $context['page_index'] = constructPageIndex($scripturl . '?board=' . $board . '.%1$d', $_REQUEST['start'], $board_info['total_topics'], $maxindex, true);
  89. $context['start'] = &$_REQUEST['start'];
  90. // Set a canonical URL for this page.
  91. $context['canonical_url'] = $scripturl . '?board=' . $board . '.' . $context['start'];
  92. $can_show_all = !empty($modSettings['enableAllMessages']) && $maxindex > $modSettings['enableAllMessages'];
  93. if (WIRELESS || !($can_show_all && isset($_REQUEST['all'])))
  94. {
  95. $context['links'] = array(
  96. 'first' => $_REQUEST['start'] >= $context['topics_per_page'] ? $scripturl . '?board=' . $board . '.0' : '',
  97. 'prev' => $_REQUEST['start'] >= $context['topics_per_page'] ? $scripturl . '?board=' . $board . '.' . ($_REQUEST['start'] - $context['topics_per_page']) : '',
  98. 'next' => $_REQUEST['start'] + $context['topics_per_page'] < $board_info['total_topics'] ? $scripturl . '?board=' . $board . '.' . ($_REQUEST['start'] + $context['topics_per_page']) : '',
  99. 'last' => $_REQUEST['start'] + $context['topics_per_page'] < $board_info['total_topics'] ? $scripturl . '?board=' . $board . '.' . (floor(($board_info['total_topics'] - 1) / $context['topics_per_page']) * $context['topics_per_page']) : '',
  100. 'up' => $board_info['parent'] == 0 ? $scripturl . '?' : $scripturl . '?board=' . $board_info['parent'] . '.0'
  101. );
  102. }
  103. $context['page_info'] = array(
  104. 'current_page' => $_REQUEST['start'] / $context['topics_per_page'] + 1,
  105. 'num_pages' => floor(($board_info['total_topics'] - 1) / $context['topics_per_page']) + 1
  106. );
  107. if (isset($_REQUEST['all']) && $can_show_all)
  108. {
  109. $maxindex = $modSettings['enableAllMessages'];
  110. $_REQUEST['start'] = 0;
  111. }
  112. // Build a list of the board's moderators.
  113. $context['moderators'] = &$board_info['moderators'];
  114. $context['moderator_groups'] = &$board_info['moderator_groups'];
  115. $context['link_moderators'] = array();
  116. if (!empty($board_info['moderators']))
  117. {
  118. foreach ($board_info['moderators'] as $mod)
  119. $context['link_moderators'][] ='<a href="' . $scripturl . '?action=profile;u=' . $mod['id'] . '" title="' . $txt['board_moderator'] . '">' . $mod['name'] . '</a>';
  120. }
  121. if (!empty($board_info['moderator_groups']))
  122. {
  123. // By default just tack the moderator groups onto the end of the members
  124. foreach ($board_info['moderator_groups'] as $mod_group)
  125. $context['link_moderators'][] = '<a href="' . $scripturl . '?action=groups;sa=members;group=' . $mod_group['id'] . '" title="' . $txt['board_moderator'] . '">' . $mod_group['name'] . '</a>';
  126. }
  127. // Now we tack the info onto the end of the linktree
  128. if (!empty($context['link_moderators']))
  129. {
  130. $context['linktree'][count($context['linktree']) - 1]['extra_after'] = '<span class="board_moderators">(' . (count($context['link_moderators']) == 1 ? $txt['moderator'] : $txt['moderators']) . ': ' . implode(', ', $context['link_moderators']) . ')</span>';
  131. }
  132. // 'Print' the header and board info.
  133. $context['page_title'] = strip_tags($board_info['name']);
  134. // Set the variables up for the template.
  135. $context['can_mark_notify'] = !$user_info['is_guest'];
  136. $context['can_post_new'] = allowedTo('post_new') || ($modSettings['postmod_active'] && allowedTo('post_unapproved_topics'));
  137. $context['can_post_poll'] = $modSettings['pollMode'] == '1' && allowedTo('poll_post') && $context['can_post_new'];
  138. $context['can_moderate_forum'] = allowedTo('moderate_forum');
  139. $context['can_approve_posts'] = allowedTo('approve_posts');
  140. require_once($sourcedir . '/Subs-BoardIndex.php');
  141. $boardIndexOptions = array(
  142. 'include_categories' => false,
  143. 'base_level' => $board_info['child_level'] + 1,
  144. 'parent_id' => $board_info['id'],
  145. 'set_latest_post' => false,
  146. 'countChildPosts' => !empty($modSettings['countChildPosts']),
  147. );
  148. $context['boards'] = getBoardIndex($boardIndexOptions);
  149. // Nosey, nosey - who's viewing this topic?
  150. if (!empty($settings['display_who_viewing']))
  151. {
  152. $context['view_members'] = array();
  153. $context['view_members_list'] = array();
  154. $context['view_num_hidden'] = 0;
  155. $request = $smcFunc['db_query']('', '
  156. SELECT
  157. lo.id_member, lo.log_time, mem.real_name, mem.member_name, mem.show_online,
  158. mg.online_color, mg.id_group, mg.group_name
  159. FROM {db_prefix}log_online AS lo
  160. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = lo.id_member)
  161. LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = CASE WHEN mem.id_group = {int:reg_member_group} THEN mem.id_post_group ELSE mem.id_group END)
  162. WHERE INSTR(lo.url, {string:in_url_string}) > 0 OR lo.session = {string:session}',
  163. array(
  164. 'reg_member_group' => 0,
  165. 'in_url_string' => 's:5:"board";i:' . $board . ';',
  166. 'session' => $user_info['is_guest'] ? 'ip' . $user_info['ip'] : session_id(),
  167. )
  168. );
  169. while ($row = $smcFunc['db_fetch_assoc']($request))
  170. {
  171. if (empty($row['id_member']))
  172. continue;
  173. if (!empty($row['online_color']))
  174. $link = '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '" style="color: ' . $row['online_color'] . ';">' . $row['real_name'] . '</a>';
  175. else
  176. $link = '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>';
  177. $is_buddy = in_array($row['id_member'], $user_info['buddies']);
  178. if ($is_buddy)
  179. $link = '<strong>' . $link . '</strong>';
  180. if (!empty($row['show_online']) || allowedTo('moderate_forum'))
  181. $context['view_members_list'][$row['log_time'] . $row['member_name']] = empty($row['show_online']) ? '<em>' . $link . '</em>' : $link;
  182. // @todo why are we filling this array of data that are just counted (twice) and discarded? ???
  183. $context['view_members'][$row['log_time'] . $row['member_name']] = array(
  184. 'id' => $row['id_member'],
  185. 'username' => $row['member_name'],
  186. 'name' => $row['real_name'],
  187. 'group' => $row['id_group'],
  188. 'href' => $scripturl . '?action=profile;u=' . $row['id_member'],
  189. 'link' => $link,
  190. 'is_buddy' => $is_buddy,
  191. 'hidden' => empty($row['show_online']),
  192. );
  193. if (empty($row['show_online']))
  194. $context['view_num_hidden']++;
  195. }
  196. $context['view_num_guests'] = $smcFunc['db_num_rows']($request) - count($context['view_members']);
  197. $smcFunc['db_free_result']($request);
  198. // Put them in "last clicked" order.
  199. krsort($context['view_members_list']);
  200. krsort($context['view_members']);
  201. }
  202. // Default sort methods.
  203. $sort_methods = array(
  204. 'subject' => 'mf.subject',
  205. 'starter' => 'IFNULL(memf.real_name, mf.poster_name)',
  206. 'last_poster' => 'IFNULL(meml.real_name, ml.poster_name)',
  207. 'replies' => 't.num_replies',
  208. 'views' => 't.num_views',
  209. 'first_post' => 't.id_topic',
  210. 'last_post' => 't.id_last_msg'
  211. );
  212. // They didn't pick one, default to by last post descending.
  213. if (!isset($_REQUEST['sort']) || !isset($sort_methods[$_REQUEST['sort']]))
  214. {
  215. $context['sort_by'] = 'last_post';
  216. $_REQUEST['sort'] = 'id_last_msg';
  217. $ascending = isset($_REQUEST['asc']);
  218. }
  219. // Otherwise default to ascending.
  220. else
  221. {
  222. $context['sort_by'] = $_REQUEST['sort'];
  223. $_REQUEST['sort'] = $sort_methods[$_REQUEST['sort']];
  224. $ascending = !isset($_REQUEST['desc']);
  225. }
  226. $context['sort_direction'] = $ascending ? 'up' : 'down';
  227. $txt['starter'] = $txt['started_by'];
  228. foreach ($sort_methods as $key => $val)
  229. $context['topics_headers'][$key] = '<a href="' . $scripturl . '?board=' . $context['current_board'] . '.' . $context['start'] . ';sort=' . $key . ($context['sort_by'] == $key && $context['sort_direction'] == 'up' ? ';desc' : '') . '">' . $txt[$key] . ($context['sort_by'] == $key ? '<span class="sort sort_' . $context['sort_direction'] . '"></span>' : '') . '</a>';
  230. // Calculate the fastest way to get the topics.
  231. $start = (int) $_REQUEST['start'];
  232. if ($start > ($board_info['total_topics'] - 1) / 2)
  233. {
  234. $ascending = !$ascending;
  235. $fake_ascending = true;
  236. $maxindex = $board_info['total_topics'] < $start + $maxindex + 1 ? $board_info['total_topics'] - $start : $maxindex;
  237. $start = $board_info['total_topics'] < $start + $maxindex + 1 ? 0 : $board_info['total_topics'] - $start - $maxindex;
  238. }
  239. else
  240. $fake_ascending = false;
  241. // Setup the default topic icons...
  242. $stable_icons = array('xx', 'thumbup', 'thumbdown', 'exclamation', 'question', 'lamp', 'smiley', 'angry', 'cheesy', 'grin', 'sad', 'wink', 'poll', 'moved', 'recycled', 'wireless', 'clip');
  243. $context['icon_sources'] = array();
  244. foreach ($stable_icons as $icon)
  245. $context['icon_sources'][$icon] = 'images_url';
  246. $topic_ids = array();
  247. $context['topics'] = array();
  248. // Sequential pages are often not optimized, so we add an additional query.
  249. $pre_query = $start > 0;
  250. if ($pre_query && $maxindex > 0)
  251. {
  252. $request = $smcFunc['db_query']('', '
  253. SELECT t.id_topic
  254. FROM {db_prefix}topics AS t' . ($context['sort_by'] === 'last_poster' ? '
  255. INNER JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)' : (in_array($context['sort_by'], array('starter', 'subject')) ? '
  256. INNER JOIN {db_prefix}messages AS mf ON (mf.id_msg = t.id_first_msg)' : '')) . ($context['sort_by'] === 'starter' ? '
  257. LEFT JOIN {db_prefix}members AS memf ON (memf.id_member = mf.id_member)' : '') . ($context['sort_by'] === 'last_poster' ? '
  258. LEFT JOIN {db_prefix}members AS meml ON (meml.id_member = ml.id_member)' : '') . '
  259. WHERE t.id_board = {int:current_board}' . (!$modSettings['postmod_active'] || $context['can_approve_posts'] ? '' : '
  260. AND (t.approved = {int:is_approved}' . ($user_info['is_guest'] ? '' : ' OR t.id_member_started = {int:current_member}') . ')') . '
  261. ORDER BY is_sticky' . ($fake_ascending ? '' : ' DESC') . ', ' . $_REQUEST['sort'] . ($ascending ? '' : ' DESC') . '
  262. LIMIT {int:start}, {int:maxindex}',
  263. array(
  264. 'current_board' => $board,
  265. 'current_member' => $user_info['id'],
  266. 'is_approved' => 1,
  267. 'id_member_guest' => 0,
  268. 'start' => $start,
  269. 'maxindex' => $maxindex,
  270. )
  271. );
  272. $topic_ids = array();
  273. while ($row = $smcFunc['db_fetch_assoc']($request))
  274. $topic_ids[] = $row['id_topic'];
  275. }
  276. // Grab the appropriate topic information...
  277. if (!$pre_query || !empty($topic_ids))
  278. {
  279. // For search engine effectiveness we'll link guests differently.
  280. $context['pageindex_multiplier'] = empty($modSettings['disableCustomPerPage']) && !empty($options['messages_per_page']) && !WIRELESS ? $options['messages_per_page'] : $modSettings['defaultMaxMessages'];
  281. $result = $smcFunc['db_query']('substring', '
  282. SELECT
  283. t.id_topic, t.num_replies, t.locked, t.num_views, t.is_sticky, t.id_poll, t.id_previous_board,
  284. ' . ($user_info['is_guest'] ? '0' : 'IFNULL(lt.id_msg, IFNULL(lmr.id_msg, -1)) + 1') . ' AS new_from,
  285. t.id_last_msg, t.approved, t.unapproved_posts, ml.poster_time AS last_poster_time, t.id_redirect_topic,
  286. ml.id_msg_modified, ml.subject AS last_subject, ml.icon AS last_icon,
  287. ml.poster_name AS last_member_name, ml.id_member AS last_id_member, ' . (!empty($settings['avatars_on_indexes']) ? 'meml.avatar,' : '') . '
  288. IFNULL(meml.real_name, ml.poster_name) AS last_display_name, t.id_first_msg,
  289. mf.poster_time AS first_poster_time, mf.subject AS first_subject, mf.icon AS first_icon,
  290. mf.poster_name AS first_member_name, mf.id_member AS first_id_member,
  291. IFNULL(memf.real_name, mf.poster_name) AS first_display_name, ' . (!empty($modSettings['preview_characters']) ? '
  292. SUBSTRING(ml.body, 1, ' . ($modSettings['preview_characters'] + 256) . ') AS last_body,
  293. SUBSTRING(mf.body, 1, ' . ($modSettings['preview_characters'] + 256) . ') AS first_body,' : '') . 'ml.smileys_enabled AS last_smileys, mf.smileys_enabled AS first_smileys' . (!empty($settings['avatars_on_indexes']) ? ',
  294. IFNULL(a.id_attach, 0) AS id_attach, a.filename, a.attachment_type' : '') . '
  295. FROM {db_prefix}topics AS t
  296. INNER JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)
  297. INNER JOIN {db_prefix}messages AS mf ON (mf.id_msg = t.id_first_msg)
  298. LEFT JOIN {db_prefix}members AS meml ON (meml.id_member = ml.id_member)
  299. LEFT JOIN {db_prefix}members AS memf ON (memf.id_member = mf.id_member)' . ($user_info['is_guest'] ? '' : '
  300. LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})
  301. LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = {int:current_board} AND lmr.id_member = {int:current_member})') . (!empty($settings['avatars_on_indexes']) ? '
  302. LEFT JOIN {db_prefix}attachments AS a ON (a.id_member = ml.id_member)' : '') . '
  303. WHERE ' . ($pre_query ? 't.id_topic IN ({array_int:topic_list})' : 't.id_board = {int:current_board}') . (!$modSettings['postmod_active'] || $context['can_approve_posts'] ? '' : '
  304. AND (t.approved = {int:is_approved}' . ($user_info['is_guest'] ? '' : ' OR t.id_member_started = {int:current_member}') . ')') . '
  305. ORDER BY ' . ($pre_query ? 'FIND_IN_SET(t.id_topic, {string:find_set_topics})' : 'is_sticky' . ($fake_ascending ? '' : ' DESC') . ', ' . $_REQUEST['sort'] . ($ascending ? '' : ' DESC')) . '
  306. LIMIT ' . ($pre_query ? '' : '{int:start}, ') . '{int:maxindex}',
  307. array(
  308. 'current_board' => $board,
  309. 'current_member' => $user_info['id'],
  310. 'topic_list' => $topic_ids,
  311. 'is_approved' => 1,
  312. 'find_set_topics' => implode(',', $topic_ids),
  313. 'start' => $start,
  314. 'maxindex' => $maxindex,
  315. )
  316. );
  317. // Begin 'printing' the message index for current board.
  318. while ($row = $smcFunc['db_fetch_assoc']($result))
  319. {
  320. if ($row['id_poll'] > 0 && $modSettings['pollMode'] == '0')
  321. continue;
  322. if (!$pre_query)
  323. $topic_ids[] = $row['id_topic'];
  324. // Does the theme support message previews?
  325. if (!empty($settings['message_index_preview']) && !empty($modSettings['preview_characters']))
  326. {
  327. // Limit them to $modSettings['preview_characters'] characters
  328. $row['first_body'] = strip_tags(strtr(parse_bbc($row['first_body'], $row['first_smileys'], $row['id_first_msg']), array('<br>' => '&#10;')));
  329. if ($smcFunc['strlen']($row['first_body']) > $modSettings['preview_characters'])
  330. $row['first_body'] = $smcFunc['substr']($row['first_body'], 0, $modSettings['preview_characters']) . '...';
  331. $row['last_body'] = strip_tags(strtr(parse_bbc($row['last_body'], $row['last_smileys'], $row['id_last_msg']), array('<br>' => '&#10;')));
  332. if ($smcFunc['strlen']($row['last_body']) > $modSettings['preview_characters'])
  333. $row['last_body'] = $smcFunc['substr']($row['last_body'], 0, $modSettings['preview_characters']) . '...';
  334. // Censor the subject and message preview.
  335. censorText($row['first_subject']);
  336. censorText($row['first_body']);
  337. // Don't censor them twice!
  338. if ($row['id_first_msg'] == $row['id_last_msg'])
  339. {
  340. $row['last_subject'] = $row['first_subject'];
  341. $row['last_body'] = $row['first_body'];
  342. }
  343. else
  344. {
  345. censorText($row['last_subject']);
  346. censorText($row['last_body']);
  347. }
  348. }
  349. else
  350. {
  351. $row['first_body'] = '';
  352. $row['last_body'] = '';
  353. censorText($row['first_subject']);
  354. if ($row['id_first_msg'] == $row['id_last_msg'])
  355. $row['last_subject'] = $row['first_subject'];
  356. else
  357. censorText($row['last_subject']);
  358. }
  359. // Decide how many pages the topic should have.
  360. if ($row['num_replies'] + 1 > $context['messages_per_page'])
  361. {
  362. $pages = '&#171; ';
  363. // We can't pass start by reference.
  364. $start = -1;
  365. $pages .= constructPageIndex($scripturl . '?topic=' . $row['id_topic'] . '.%1$d', $start, $row['num_replies'] + 1, $context['messages_per_page'], true, false);
  366. // If we can use all, show all.
  367. if (!empty($modSettings['enableAllMessages']) && $row['num_replies'] + 1 < $modSettings['enableAllMessages'])
  368. $pages .= ' &nbsp;<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0;all">' . $txt['all'] . '</a>';
  369. $pages .= ' &#187;';
  370. }
  371. else
  372. $pages = '';
  373. // We need to check the topic icons exist...
  374. if (!empty($modSettings['messageIconChecks_enable']))
  375. {
  376. if (!isset($context['icon_sources'][$row['first_icon']]))
  377. $context['icon_sources'][$row['first_icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['first_icon'] . '.png') ? 'images_url' : 'default_images_url';
  378. if (!isset($context['icon_sources'][$row['last_icon']]))
  379. $context['icon_sources'][$row['last_icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['last_icon'] . '.png') ? 'images_url' : 'default_images_url';
  380. }
  381. else
  382. {
  383. if (!isset($context['icon_sources'][$row['first_icon']]))
  384. $context['icon_sources'][$row['first_icon']] = 'images_url';
  385. if (!isset($context['icon_sources'][$row['last_icon']]))
  386. $context['icon_sources'][$row['last_icon']] = 'images_url';
  387. }
  388. if (!empty($board_info['recycle']))
  389. $row['first_icon'] = 'recycled';
  390. // 'Print' the topic info.
  391. $context['topics'][$row['id_topic']] = array(
  392. 'id' => $row['id_topic'],
  393. 'first_post' => array(
  394. 'id' => $row['id_first_msg'],
  395. 'member' => array(
  396. 'username' => $row['first_member_name'],
  397. 'name' => $row['first_display_name'],
  398. 'id' => $row['first_id_member'],
  399. 'href' => !empty($row['first_id_member']) ? $scripturl . '?action=profile;u=' . $row['first_id_member'] : '',
  400. 'link' => !empty($row['first_id_member']) ? '<a href="' . $scripturl . '?action=profile;u=' . $row['first_id_member'] . '" title="' . $txt['profile_of'] . ' ' . $row['first_display_name'] . '" class="preview">' . $row['first_display_name'] . '</a>' : $row['first_display_name']
  401. ),
  402. 'time' => timeformat($row['first_poster_time']),
  403. 'timestamp' => forum_time(true, $row['first_poster_time']),
  404. 'subject' => $row['first_subject'],
  405. 'preview' => $row['first_body'],
  406. 'icon' => $row['first_icon'],
  407. 'icon_url' => $settings[$context['icon_sources'][$row['first_icon']]] . '/post/' . $row['first_icon'] . '.png',
  408. 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
  409. 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['first_subject'] . '</a>'
  410. ),
  411. 'last_post' => array(
  412. 'id' => $row['id_last_msg'],
  413. 'member' => array(
  414. 'username' => $row['last_member_name'],
  415. 'name' => $row['last_display_name'],
  416. 'id' => $row['last_id_member'],
  417. 'href' => !empty($row['last_id_member']) ? $scripturl . '?action=profile;u=' . $row['last_id_member'] : '',
  418. 'link' => !empty($row['last_id_member']) ? '<a href="' . $scripturl . '?action=profile;u=' . $row['last_id_member'] . '">' . $row['last_display_name'] . '</a>' : $row['last_display_name']
  419. ),
  420. 'time' => timeformat($row['last_poster_time']),
  421. 'timestamp' => forum_time(true, $row['last_poster_time']),
  422. 'subject' => $row['last_subject'],
  423. 'preview' => $row['last_body'],
  424. 'icon' => $row['last_icon'],
  425. 'icon_url' => $settings[$context['icon_sources'][$row['last_icon']]] . '/post/' . $row['last_icon'] . '.png',
  426. 'href' => $scripturl . '?topic=' . $row['id_topic'] . ($user_info['is_guest'] ? ('.' . (!empty($options['view_newest_first']) ? 0 : ((int) (($row['num_replies']) / $context['pageindex_multiplier'])) * $context['pageindex_multiplier']) . '#msg' . $row['id_last_msg']) : (($row['num_replies'] == 0 ? '.0' : '.msg' . $row['id_last_msg']) . '#new')),
  427. 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . ($user_info['is_guest'] ? ('.' . (!empty($options['view_newest_first']) ? 0 : ((int) (($row['num_replies']) / $context['pageindex_multiplier'])) * $context['pageindex_multiplier']) . '#msg' . $row['id_last_msg']) : (($row['num_replies'] == 0 ? '.0' : '.msg' . $row['id_last_msg']) . '#new')) . '" ' . ($row['num_replies'] == 0 ? '' : 'rel="nofollow"') . '>' . $row['last_subject'] . '</a>'
  428. ),
  429. 'is_sticky' => !empty($row['is_sticky']),
  430. 'is_locked' => !empty($row['locked']),
  431. 'is_redirect' => !empty($row['id_redirect_topic']),
  432. 'is_poll' => $modSettings['pollMode'] == '1' && $row['id_poll'] > 0,
  433. 'is_posted_in' => false,
  434. 'is_watched' => false,
  435. 'icon' => $row['first_icon'],
  436. 'icon_url' => $settings[$context['icon_sources'][$row['first_icon']]] . '/post/' . $row['first_icon'] . '.png',
  437. 'subject' => $row['first_subject'],
  438. 'new' => $row['new_from'] <= $row['id_msg_modified'],
  439. 'new_from' => $row['new_from'],
  440. 'newtime' => $row['new_from'],
  441. 'new_href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['new_from'] . '#new',
  442. 'pages' => $pages,
  443. 'replies' => comma_format($row['num_replies']),
  444. 'views' => comma_format($row['num_views']),
  445. 'approved' => $row['approved'],
  446. 'unapproved_posts' => $row['unapproved_posts'],
  447. );
  448. if (!empty($settings['avatars_on_indexes']))
  449. $context['topics'][$row['id_topic']]['last_post']['member']['avatar'] = array(
  450. 'name' => $row['avatar'],
  451. 'image' => $row['avatar'] == '' ? ($row['id_attach'] > 0 ? '<img class="avatar" src="' . (empty($row['attachment_type']) ? $scripturl . '?action=dlattach;attach=' . $row['id_attach'] . ';type=avatar' : $modSettings['custom_avatar_url'] . '/' . $row['filename']) . '" alt="">' : '') : (stristr($row['avatar'], 'http://') || stristr($row['avatar'], 'https://') ? '<img class="avatar" src="' . $row['avatar'] . '" alt="">' : '<img class="avatar" src="' . $modSettings['avatar_url'] . '/' . $smcFunc['htmlspecialchars']($row['avatar']) . '" alt="">'),
  452. 'href' => $row['avatar'] == '' ? ($row['id_attach'] > 0 ? (empty($row['attachment_type']) ? $scripturl . '?action=dlattach;attach=' . $row['id_attach'] . ';type=avatar' : $modSettings['custom_avatar_url'] . '/' . $row['filename']) : '') : (stristr($row['avatar'], 'http://') || stristr($row['avatar'], 'https://') ? $row['avatar'] : $modSettings['avatar_url'] . '/' . $row['avatar']),
  453. 'url' => $row['avatar'] == '' ? '' : (stristr($row['avatar'], 'http://') || stristr($row['avatar'], 'https://') ? $row['avatar'] : $modSettings['avatar_url'] . '/' . $row['avatar'])
  454. );
  455. }
  456. $smcFunc['db_free_result']($result);
  457. // Fix the sequence of topics if they were retrieved in the wrong order. (for speed reasons...)
  458. if ($fake_ascending)
  459. $context['topics'] = array_reverse($context['topics'], true);
  460. if (!empty($modSettings['enableParticipation']) && !$user_info['is_guest'] && !empty($topic_ids))
  461. {
  462. $result = $smcFunc['db_query']('', '
  463. SELECT id_topic
  464. FROM {db_prefix}messages
  465. WHERE id_topic IN ({array_int:topic_list})
  466. AND id_member = {int:current_member}
  467. GROUP BY id_topic
  468. LIMIT ' . count($topic_ids),
  469. array(
  470. 'current_member' => $user_info['id'],
  471. 'topic_list' => $topic_ids,
  472. )
  473. );
  474. while ($row = $smcFunc['db_fetch_assoc']($result))
  475. $context['topics'][$row['id_topic']]['is_posted_in'] = true;
  476. $smcFunc['db_free_result']($result);
  477. }
  478. }
  479. $context['jump_to'] = array(
  480. 'label' => addslashes(un_htmlspecialchars($txt['jump_to'])),
  481. 'board_name' => $smcFunc['htmlspecialchars'](strtr(strip_tags($board_info['name']), array('&amp;' => '&'))),
  482. 'child_level' => $board_info['child_level'],
  483. );
  484. // Is Quick Moderation active/needed?
  485. if (!empty($options['display_quick_mod']) && !empty($context['topics']))
  486. {
  487. $context['can_markread'] = $context['user']['is_logged'];
  488. $context['can_lock'] = allowedTo('lock_any');
  489. $context['can_sticky'] = allowedTo('make_sticky');
  490. $context['can_move'] = allowedTo('move_any');
  491. $context['can_remove'] = allowedTo('remove_any');
  492. $context['can_merge'] = allowedTo('merge_any');
  493. // Ignore approving own topics as it's unlikely to come up...
  494. $context['can_approve'] = $modSettings['postmod_active'] && allowedTo('approve_posts') && !empty($board_info['unapproved_topics']);
  495. // Can we restore topics?
  496. $context['can_restore'] = allowedTo('move_any') && !empty($board_info['recycle']);
  497. if ($user_info['is_admin'] || $modSettings['topic_move_any'])
  498. $context['can_move_any'] = true;
  499. else
  500. {
  501. // We'll use this in a minute
  502. $boards_allowed = boardsAllowedTo('post_new');
  503. // How many boards can you do this on besides this one?
  504. $context['can_move_any'] = count($boards_allowed) > 1;
  505. }
  506. // Set permissions for all the topics.
  507. foreach ($context['topics'] as $t => $topic)
  508. {
  509. $started = $topic['first_post']['member']['id'] == $user_info['id'];
  510. $context['topics'][$t]['quick_mod'] = array(
  511. 'lock' => allowedTo('lock_any') || ($started && allowedTo('lock_own')),
  512. 'sticky' => allowedTo('make_sticky'),
  513. 'move' => (allowedTo('move_any') || ($started && allowedTo('move_own')) && $context['can_move_any']),
  514. 'modify' => allowedTo('modify_any') || ($started && allowedTo('modify_own')),
  515. 'remove' => allowedTo('remove_any') || ($started && allowedTo('remove_own')),
  516. 'approve' => $context['can_approve'] && $topic['unapproved_posts']
  517. );
  518. $context['can_lock'] |= ($started && allowedTo('lock_own'));
  519. $context['can_move'] |= ($started && allowedTo('move_own') && $context['can_move_any']);
  520. $context['can_remove'] |= ($started && allowedTo('remove_own'));
  521. }
  522. // Can we use quick moderation checkboxes?
  523. if ($options['display_quick_mod'] == 1)
  524. $context['can_quick_mod'] = $context['user']['is_logged'] || $context['can_approve'] || $context['can_remove'] || $context['can_lock'] || $context['can_sticky'] || $context['can_move'] || $context['can_merge'] || $context['can_restore'];
  525. // Or the icons?
  526. else
  527. $context['can_quick_mod'] = $context['can_remove'] || $context['can_lock'] || $context['can_sticky'] || $context['can_move'];
  528. }
  529. if (!empty($context['can_quick_mod']) && $options['display_quick_mod'] == 1)
  530. {
  531. $context['qmod_actions'] = array('approve', 'remove', 'lock', 'sticky', 'move', 'merge', 'restore', 'markread');
  532. call_integration_hook('integrate_quick_mod_actions');
  533. }
  534. // Mark current and parent boards as seen.
  535. if (!$user_info['is_guest'])
  536. {
  537. $smcFunc['db_insert']('replace',
  538. '{db_prefix}log_boards',
  539. array('id_msg' => 'int', 'id_member' => 'int', 'id_board' => 'int'),
  540. array($modSettings['maxMsgID'], $user_info['id'], $board),
  541. array('id_member', 'id_board')
  542. );
  543. if (!empty($board_info['parent_boards']))
  544. {
  545. $smcFunc['db_query']('', '
  546. UPDATE {db_prefix}log_boards
  547. SET id_msg = {int:id_msg}
  548. WHERE id_member = {int:current_member}
  549. AND id_board IN ({array_int:board_list})',
  550. array(
  551. 'current_member' => $user_info['id'],
  552. 'board_list' => array_keys($board_info['parent_boards']),
  553. 'id_msg' => $modSettings['maxMsgID'],
  554. )
  555. );
  556. // We've seen all these boards now!
  557. foreach ($board_info['parent_boards'] as $k => $dummy)
  558. if (isset($_SESSION['topicseen_cache'][$k]))
  559. unset($_SESSION['topicseen_cache'][$k]);
  560. }
  561. if (isset($_SESSION['topicseen_cache'][$board]))
  562. unset($_SESSION['topicseen_cache'][$board]);
  563. $request = $smcFunc['db_query']('', '
  564. SELECT id_topic, id_board, sent
  565. FROM {db_prefix}log_notify
  566. WHERE id_member = {int:current_member}
  567. AND (' . (!empty($context['topics']) ? 'id_topic IN ({array_int:topics}) OR ' : '') . 'id_board = {int:current_board})',
  568. array(
  569. 'current_board' => $board,
  570. 'topics' => !empty($context['topics']) ? array_keys($context['topics']) : array(),
  571. 'current_member' => $user_info['id'],
  572. )
  573. );
  574. $context['is_marked_notify'] = false; // this is for the *board* only
  575. $notify = array();
  576. while ($row = $smcFunc['db_fetch_assoc']($request))
  577. {
  578. if (!empty($row['id_board']))
  579. {
  580. $context['is_marked_notify'] = true;
  581. $board_sent = $row['sent'];
  582. }
  583. if (!empty($row['id_topic']))
  584. $context['topics'][$row['id_topic']]['is_watched'] = true;
  585. }
  586. $smcFunc['db_free_result']($request);
  587. if ($context['is_marked_notify'] && !empty($board_sent))
  588. {
  589. $smcFunc['db_query']('', '
  590. UPDATE {db_prefix}log_notify
  591. SET sent = {int:is_sent}
  592. WHERE id_member = {int:current_member}
  593. AND id_board = {int:current_board}',
  594. array(
  595. 'current_board' => $board,
  596. 'current_member' => $user_info['id'],
  597. 'is_sent' => 0,
  598. )
  599. );
  600. }
  601. }
  602. else
  603. $context['is_marked_notify'] = false;
  604. // If there are children, but no topics and no ability to post topics...
  605. $context['no_topic_listing'] = !empty($context['boards']) && empty($context['topics']) && !$context['can_post_new'];
  606. // Build the message index button array.
  607. $context['normal_buttons'] = array(
  608. 'new_topic' => array('test' => 'can_post_new', 'text' => 'new_topic', 'image' => 'new_topic.png', 'lang' => true, 'url' => $scripturl . '?action=post;board=' . $context['current_board'] . '.0', 'active' => true),
  609. 'post_poll' => array('test' => 'can_post_poll', 'text' => 'new_poll', 'image' => 'new_poll.png', 'lang' => true, 'url' => $scripturl . '?action=post;board=' . $context['current_board'] . '.0;poll'),
  610. 'notify' => array('test' => 'can_mark_notify', 'text' => $context['is_marked_notify'] ? 'unwatch_board' : 'watch_board', 'image' => ($context['is_marked_notify'] ? 'un' : ''). 'notify.png', 'lang' => true, 'custom' => 'onclick="return confirm(\'' . ($context['is_marked_notify'] ? $txt['notification_disable_board'] : $txt['notification_enable_board']) . '\');"', 'url' => $scripturl . '?action=notifyboard;sa=' . ($context['is_marked_notify'] ? 'off' : 'on') . ';board=' . $context['current_board'] . '.' . $context['start'] . ';' . $context['session_var'] . '=' . $context['session_id']),
  611. 'markread' => array('text' => 'mark_read_short', 'image' => 'markread.png', 'lang' => true, 'custom' => 'onclick="return confirm(\'' . $txt['are_sure_mark_read'] . '\');"', 'url' => $scripturl . '?action=markasread;sa=board;board=' . $context['current_board'] . '.0;' . $context['session_var'] . '=' . $context['session_id']),
  612. );
  613. // Allow adding new buttons easily.
  614. // Note: $context['normal_buttons'] is added for backward compatibility with 2.0, but is deprecated and should not be used
  615. call_integration_hook('integrate_messageindex_buttons', array(&$context['normal_buttons']));
  616. }
  617. /**
  618. * Allows for moderation from the message index.
  619. * @todo refactor this...
  620. */
  621. function QuickModeration()
  622. {
  623. global $sourcedir, $board, $user_info, $modSettings, $smcFunc, $context;
  624. // Check the session = get or post.
  625. checkSession('request');
  626. // Lets go straight to the restore area.
  627. if (isset($_REQUEST['qaction']) && $_REQUEST['qaction'] == 'restore' && !empty($_REQUEST['topics']))
  628. redirectexit('action=restoretopic;topics=' . implode(',', $_REQUEST['topics']) . ';' . $context['session_var'] . '=' . $context['session_id']);
  629. if (isset($_SESSION['topicseen_cache']))
  630. $_SESSION['topicseen_cache'] = array();
  631. // This is going to be needed to send off the notifications and for updateLastMessages().
  632. require_once($sourcedir . '/Subs-Post.php');
  633. // Remember the last board they moved things to.
  634. if (isset($_REQUEST['move_to']))
  635. $_SESSION['move_to_topic'] = $_REQUEST['move_to'];
  636. // Only a few possible actions.
  637. $possibleActions = array();
  638. if (!empty($board))
  639. {
  640. $boards_can = array(
  641. 'make_sticky' => allowedTo('make_sticky') ? array($board) : array(),
  642. 'move_any' => allowedTo('move_any') ? array($board) : array(),
  643. 'move_own' => allowedTo('move_own') ? array($board) : array(),
  644. 'remove_any' => allowedTo('remove_any') ? array($board) : array(),
  645. 'remove_own' => allowedTo('remove_own') ? array($board) : array(),
  646. 'lock_any' => allowedTo('lock_any') ? array($board) : array(),
  647. 'lock_own' => allowedTo('lock_own') ? array($board) : array(),
  648. 'merge_any' => allowedTo('merge_any') ? array($board) : array(),
  649. 'approve_posts' => allowedTo('approve_posts') ? array($board) : array(),
  650. );
  651. $redirect_url = 'board=' . $board . '.' . $_REQUEST['start'];
  652. }
  653. else
  654. {
  655. /**
  656. * @todo Ugly. There's no getting around this, is there?
  657. * @todo Maybe just do this on the actions people want to use?
  658. */
  659. $boards_can = boardsAllowedTo(array('make_sticky', 'move_any', 'move_own', 'remove_any', 'remove_own', 'lock_any', 'lock_own', 'merge_any', 'approve_posts'), true, false);
  660. $redirect_url = isset($_POST['redirect_url']) ? $_POST['redirect_url'] : (isset($_SESSION['old_url']) ? $_SESSION['old_url'] : '');
  661. }
  662. // Are we enforcing the "no moving topics to boards where you can't post new ones" rule?
  663. if (!$user_info['is_admin'] && !$modSettings['topic_move_any'])
  664. {
  665. // Don't count this board, if it's specified
  666. if (!empty($board))
  667. {
  668. $boards_can['post_new'] = array_diff(boardsAllowedTo('post_new'), array($board));
  669. }
  670. else
  671. {
  672. $boards_can['post_new'] = boardsAllowedTo('post_new');
  673. }
  674. if (empty($boards_can['post_new']))
  675. {
  676. $boards_can['move_any'] = $boards_can['move_own'] = array();
  677. }
  678. }
  679. if (!$user_info['is_guest'])
  680. $possibleActions[] = 'markread';
  681. if (!empty($boards_can['make_sticky']))
  682. $possibleActions[] = 'sticky';
  683. if (!empty($boards_can['move_any']) || !empty($boards_can['move_own']))
  684. $possibleActions[] = 'move';
  685. if (!empty($boards_can['remove_any']) || !empty($boards_can['remove_own']))
  686. $possibleActions[] = 'remove';
  687. if (!empty($boards_can['lock_any']) || !empty($boards_can['lock_own']))
  688. $possibleActions[] = 'lock';
  689. if (!empty($boards_can['merge_any']))
  690. $possibleActions[] = 'merge';
  691. if (!empty($boards_can['approve_posts']))
  692. $possibleActions[] = 'approve';
  693. // Two methods: $_REQUEST['actions'] (id_topic => action), and $_REQUEST['topics'] and $_REQUEST['qaction'].
  694. // (if action is 'move', $_REQUEST['move_to'] or $_REQUEST['move_tos'][$topic] is used.)
  695. if (!empty($_REQUEST['topics']))
  696. {
  697. // If the action isn't valid, just quit now.
  698. if (empty($_REQUEST['qaction']) || !in_array($_REQUEST['qaction'], $possibleActions))
  699. redirectexit($redirect_url);
  700. // Merge requires all topics as one parameter and can be done at once.
  701. if ($_REQUEST['qaction'] == 'merge')
  702. {
  703. // Merge requires at least two topics.
  704. if (empty($_REQUEST['topics']) || count($_REQUEST['topics']) < 2)
  705. redirectexit($redirect_url);
  706. require_once($sourcedir . '/SplitTopics.php');
  707. return MergeExecute($_REQUEST['topics']);
  708. }
  709. // Just convert to the other method, to make it easier.
  710. foreach ($_REQUEST['topics'] as $topic)
  711. $_REQUEST['actions'][(int) $topic] = $_REQUEST['qaction'];
  712. }
  713. // Weird... how'd you get here?
  714. if (empty($_REQUEST['actions']))
  715. redirectexit($redirect_url);
  716. // Validate each action.
  717. $temp = array();
  718. foreach ($_REQUEST['actions'] as $topic => $action)
  719. {
  720. if (in_array($action, $possibleActions))
  721. $temp[(int) $topic] = $action;
  722. }
  723. $_REQUEST['actions'] = $temp;
  724. if (!empty($_REQUEST['actions']))
  725. {
  726. // Find all topics...
  727. $request = $smcFunc['db_query']('', '
  728. SELECT id_topic, id_member_started, id_board, locked, approved, unapproved_posts
  729. FROM {db_prefix}topics
  730. WHERE id_topic IN ({array_int:action_topic_ids})
  731. LIMIT ' . count($_REQUEST['actions']),
  732. array(
  733. 'action_topic_ids' => array_keys($_REQUEST['actions']),
  734. )
  735. );
  736. while ($row = $smcFunc['db_fetch_assoc']($request))
  737. {
  738. if (!empty($board))
  739. {
  740. if ($row['id_board'] != $board || ($modSettings['postmod_active'] && !$row['approved'] && !allowedTo('approve_posts')))
  741. unset($_REQUEST['actions'][$row['id_topic']]);
  742. }
  743. else
  744. {
  745. // Don't allow them to act on unapproved posts they can't see...
  746. if ($modSettings['postmod_active'] && !$row['approved'] && !in_array(0, $boards_can['approve_posts']) && !in_array($row['id_board'], $boards_can['approve_posts']))
  747. unset($_REQUEST['actions'][$row['id_topic']]);
  748. // Goodness, this is fun. We need to validate the action.
  749. elseif ($_REQUEST['actions'][$row['id_topic']] == 'sticky' && !in_array(0, $boards_can['make_sticky']) && !in_array($row['id_board'], $boards_can['make_sticky']))
  750. unset($_REQUEST['actions'][$row['id_topic']]);
  751. elseif ($_REQUEST['actions'][$row['id_topic']] == 'move' && !in_array(0, $boards_can['move_any']) && !in_array($row['id_board'], $boards_can['move_any']) && ($row['id_member_started'] != $user_info['id'] || (!in_array(0, $boards_can['move_own']) && !in_array($row['id_board'], $boards_can['move_own']))))
  752. unset($_REQUEST['actions'][$row['id_topic']]);
  753. elseif ($_REQUEST['actions'][$row['id_topic']] == 'remove' && !in_array(0, $boards_can['remove_any']) && !in_array($row['id_board'], $boards_can['remove_any']) && ($row['id_member_started'] != $user_info['id'] || (!in_array(0, $boards_can['remove_own']) && !in_array($row['id_board'], $boards_can['remove_own']))))
  754. unset($_REQUEST['actions'][$row['id_topic']]);
  755. // @todo $locked is not set, what are you trying to do? (taking the change it is supposed to be $row['locked'])
  756. elseif ($_REQUEST['actions'][$row['id_topic']] == 'lock' && !in_array(0, $boards_can['lock_any']) && !in_array($row['id_board'], $boards_can['lock_any']) && ($row['id_member_started'] != $user_info['id'] || $row['locked'] == 1 || (!in_array(0, $boards_can['lock_own']) && !in_array($row['id_board'], $boards_can['lock_own']))))
  757. unset($_REQUEST['actions'][$row['id_topic']]);
  758. // If the topic is approved then you need permission to approve the posts within.
  759. elseif ($_REQUEST['actions'][$row['id_topic']] == 'approve' && (!$row['unapproved_posts'] || (!in_array(0, $boards_can['approve_posts']) && !in_array($row['id_board'], $boards_can['approve_posts']))))
  760. unset($_REQUEST['actions'][$row['id_topic']]);
  761. }
  762. }
  763. $smcFunc['db_free_result']($request);
  764. }
  765. $stickyCache = array();
  766. $moveCache = array(0 => array(), 1 => array());
  767. $removeCache = array();
  768. $lockCache = array();
  769. $markCache = array();
  770. $approveCache = array();
  771. // Separate the actions.
  772. foreach ($_REQUEST['actions'] as $topic => $action)
  773. {
  774. $topic = (int) $topic;
  775. if ($action == 'markread')
  776. $markCache[] = $topic;
  777. elseif ($action == 'sticky')
  778. $stickyCache[] = $topic;
  779. elseif ($action == 'move')
  780. {
  781. require_once($sourcedir . '/MoveTopic.php');
  782. moveTopicConcurrence();
  783. // $moveCache[0] is the topic, $moveCache[1] is the board to move to.
  784. $moveCache[1][$topic] = (int) (isset($_REQUEST['move_tos'][$topic]) ? $_REQUEST['move_tos'][$topic] : $_REQUEST['move_to']);
  785. if (empty($moveCache[1][$topic]))
  786. continue;
  787. $moveCache[0][] = $topic;
  788. }
  789. elseif ($action == 'remove')
  790. $removeCache[] = $topic;
  791. elseif ($action == 'lock')
  792. $lockCache[] = $topic;
  793. elseif ($action == 'approve')
  794. $approveCache[] = $topic;
  795. }
  796. if (empty($board))
  797. $affectedBoards = array();
  798. else
  799. $affectedBoards = array($board => array(0, 0));
  800. // Do all the stickies...
  801. if (!empty($stickyCache))
  802. {
  803. $smcFunc['db_query']('', '
  804. UPDATE {db_prefix}topics
  805. SET is_sticky = CASE WHEN is_sticky = {int:is_sticky} THEN 0 ELSE 1 END
  806. WHERE id_topic IN ({array_int:sticky_topic_ids})',
  807. array(
  808. 'sticky_topic_ids' => $stickyCache,
  809. 'is_sticky' => 1,
  810. )
  811. );
  812. // Get the board IDs and Sticky status
  813. $request = $smcFunc['db_query']('', '
  814. SELECT id_topic, id_board, is_sticky
  815. FROM {db_prefix}topics
  816. WHERE id_topic IN ({array_int:sticky_topic_ids})
  817. LIMIT ' . count($stickyCache),
  818. array(
  819. 'sticky_topic_ids' => $stickyCache,
  820. )
  821. );
  822. $stickyCacheBoards = array();
  823. $stickyCacheStatus = array();
  824. while ($row = $smcFunc['db_fetch_assoc']($request))
  825. {
  826. $stickyCacheBoards[$row['id_topic']] = $row['id_board'];
  827. $stickyCacheStatus[$row['id_topic']] = empty($row['is_sticky']);
  828. }
  829. $smcFunc['db_free_result']($request);
  830. }
  831. // Move sucka! (this is, by the by, probably the most complicated part....)
  832. if (!empty($moveCache[0]))
  833. {
  834. // I know - I just KNOW you're trying to beat the system. Too bad for you... we CHECK :P.
  835. $request = $smcFunc['db_query']('', '
  836. SELECT t.id_topic, t.id_board, b.count_posts
  837. FROM {db_prefix}topics AS t
  838. LEFT JOIN {db_prefix}boards AS b ON (t.id_board = b.id_board)
  839. WHERE t.id_topic IN ({array_int:move_topic_ids})' . (!empty($board) && !allowedTo('move_any') ? '
  840. AND t.id_member_started = {int:current_member}' : '') . '
  841. LIMIT ' . count($moveCache[0]),
  842. array(
  843. 'current_member' => $user_info['id'],
  844. 'move_topic_ids' => $moveCache[0],
  845. )
  846. );
  847. $moveTos = array();
  848. $moveCache2 = array();
  849. $countPosts = array();
  850. while ($row = $smcFunc['db_fetch_assoc']($request))
  851. {
  852. $to = $moveCache[1][$row['id_topic']];
  853. if (empty($to))
  854. continue;
  855. // Does this topic's board count the posts or not?
  856. $countPosts[$row['id_topic']] = empty($row['count_posts']);
  857. if (!isset($moveTos[$to]))
  858. $moveTos[$to] = array();
  859. $moveTos[$to][] = $row['id_topic'];
  860. // For reporting...
  861. $moveCache2[] = array($row['id_topic'], $row['id_board'], $to);
  862. }
  863. $smcFunc['db_free_result']($request);
  864. $moveCache = $moveCache2;
  865. require_once($sourcedir . '/MoveTopic.php');
  866. // Do the actual moves...
  867. foreach ($moveTos as $to => $topics)
  868. moveTopics($topics, $to);
  869. // Does the post counts need to be updated?
  870. if (!empty($moveTos))
  871. {
  872. $topicRecounts = array();
  873. $request = $smcFunc['db_query']('', '
  874. SELECT id_board, count_posts
  875. FROM {db_prefix}boards
  876. WHERE id_board IN ({array_int:move_boards})',
  877. array(
  878. 'move_boards' => array_keys($moveTos),
  879. )
  880. );
  881. while ($row = $smcFunc['db_fetch_assoc']($request))
  882. {
  883. $cp = empty($row['count_posts']);
  884. // Go through all the topics that are being moved to this board.
  885. foreach ($moveTos[$row['id_board']] as $topic)
  886. {
  887. // If both boards have the same value for post counting then no adjustment needs to be made.
  888. if ($countPosts[$topic] != $cp)
  889. {
  890. // If the board being moved to does count the posts then the other one doesn't so add to their post count.
  891. $topicRecounts[$topic] = $cp ? '+' : '-';
  892. }
  893. }
  894. }
  895. $smcFunc['db_free_result']($request);
  896. if (!empty($topicRecounts))
  897. {
  898. $members = array();
  899. // Get all the members who have posted in the moved topics.
  900. $request = $smcFunc['db_query']('', '
  901. SELECT id_member, id_topic
  902. FROM {db_prefix}messages
  903. WHERE id_topic IN ({array_int:moved_topic_ids})',
  904. array(
  905. 'moved_topic_ids' => array_keys($topicRecounts),
  906. )
  907. );
  908. while ($row = $smcFunc['db_fetch_assoc']($request))
  909. {
  910. if (!isset($members[$row['id_member']]))
  911. $members[$row['id_member']] = 0;
  912. if ($topicRecounts[$row['id_topic']] === '+')
  913. $members[$row['id_member']] += 1;
  914. else
  915. $members[$row['id_member']] -= 1;
  916. }
  917. $smcFunc['db_free_result']($request);
  918. // And now update them member's post counts
  919. foreach ($members as $id_member => $post_adj)
  920. updateMemberData($id_member, array('posts' => 'posts + ' . $post_adj));
  921. }
  922. }
  923. }
  924. // Now delete the topics...
  925. if (!empty($removeCache))
  926. {
  927. // They can only delete their own topics. (we wouldn't be here if they couldn't do that..)
  928. $result = $smcFunc['db_query']('', '
  929. SELECT id_topic, id_board
  930. FROM {db_prefix}topics
  931. WHERE id_topic IN ({array_int:removed_topic_ids})' . (!empty($board) && !allowedTo('remove_any') ? '
  932. AND id_member_started = {int:current_member}' : '') . '
  933. LIMIT ' . count($removeCache),
  934. array(
  935. 'current_member' => $user_info['id'],
  936. 'removed_topic_ids' => $removeCache,
  937. )
  938. );
  939. $removeCache = array();
  940. $removeCacheBoards = array();
  941. while ($row = $smcFunc['db_fetch_assoc']($result))
  942. {
  943. $removeCache[] = $row['id_topic'];
  944. $removeCacheBoards[$row['id_topic']] = $row['id_board'];
  945. }
  946. $smcFunc['db_free_result']($result);
  947. // Maybe *none* were their own topics.
  948. if (!empty($removeCache))
  949. {
  950. // Gotta send the notifications *first*!
  951. foreach ($removeCache as $topic)
  952. {
  953. // Only log the topic ID if it's not in the recycle board.
  954. logAction('remove', array((empty($modSettings['recycle_enable']) || $modSettings['recycle_board'] != $removeCacheBoards[$topic] ? 'topic' : 'old_topic_id') => $topic, 'board' => $removeCacheBoards[$topic]));
  955. sendNotifications($topic, 'remove');
  956. }
  957. require_once($sourcedir . '/RemoveTopic.php');
  958. removeTopics($removeCache);
  959. }
  960. }
  961. // Approve the topics...
  962. if (!empty($approveCache))
  963. {
  964. // We need unapproved topic ids and their authors!
  965. $request = $smcFunc['db_query']('', '
  966. SELECT id_topic, id_member_started
  967. FROM {db_prefix}topics
  968. WHERE id_topic IN ({array_int:approve_topic_ids})
  969. AND approved = {int:not_approved}
  970. LIMIT ' . count($approveCache),
  971. array(
  972. 'approve_topic_ids' => $approveCache,
  973. 'not_approved' => 0,
  974. )
  975. );
  976. $approveCache = array();
  977. $approveCacheMembers = array();
  978. while ($row = $smcFunc['db_fetch_assoc']($request))
  979. {
  980. $approveCache[] = $row['id_topic'];
  981. $approveCacheMembers[$row['id_topic']] = $row['id_member_started'];
  982. }
  983. $smcFunc['db_free_result']($request);
  984. // Any topics to approve?
  985. if (!empty($approveCache))
  986. {
  987. // Handle the approval part...
  988. approveTopics($approveCache);
  989. // Time for some logging!
  990. foreach ($approveCache as $topic)
  991. logAction('approve_topic', array('topic' => $topic, 'member' => $approveCacheMembers[$topic]));
  992. }
  993. }
  994. // And (almost) lastly, lock the topics...
  995. if (!empty($lockCache))
  996. {
  997. $lockStatus = array();
  998. // Gotta make sure they CAN lock/unlock these topics...
  999. if (!empty($board) && !allowedTo('lock_any'))
  1000. {
  1001. // Make sure they started the topic AND it isn't already locked by someone with higher priv's.
  1002. $result = $smcFunc['db_query']('', '
  1003. SELECT id_topic, locked, id_board
  1004. FROM {db_prefix}topics
  1005. WHERE id_topic IN ({array_int:locked_topic_ids})
  1006. AND id_member_started = {int:current_member}
  1007. AND locked IN (2, 0)
  1008. LIMIT ' . count($lockCache),
  1009. array(
  1010. 'current_member' => $user_info['id'],
  1011. 'locked_topic_ids' => $lockCache,
  1012. )
  1013. );
  1014. $lockCache = array();
  1015. $lockCacheBoards = array();
  1016. while ($row = $smcFunc['db_fetch_assoc']($result))
  1017. {
  1018. $lockCache[] = $row['id_topic'];
  1019. $lockCacheBoards[$row['id_topic']] = $row['id_board'];
  1020. $lockStatus[$row['id_topic']] = empty($row['locked']);
  1021. }
  1022. $smcFunc['db_free_result']($result);
  1023. }
  1024. else
  1025. {
  1026. $result = $smcFunc['db_query']('', '
  1027. SELECT id_topic, locked, id_board
  1028. FROM {db_prefix}topics
  1029. WHERE id_topic IN ({array_int:locked_topic_ids})
  1030. LIMIT ' . count($lockCache),
  1031. array(
  1032. 'locked_topic_ids' => $lockCache,
  1033. )
  1034. );
  1035. $lockCacheBoards = array();
  1036. while ($row = $smcFunc['db_fetch_assoc']($result))
  1037. {
  1038. $lockStatus[$row['id_topic']] = empty($row['locked']);
  1039. $lockCacheBoards[$row['id_topic']] = $row['id_board'];
  1040. }
  1041. $smcFunc['db_free_result']($result);
  1042. }
  1043. // It could just be that *none* were their own topics...
  1044. if (!empty($lockCache))
  1045. {
  1046. // Alternate the locked value.
  1047. $smcFunc['db_query']('', '
  1048. UPDATE {db_prefix}topics
  1049. SET locked = CASE WHEN locked = {int:is_locked} THEN ' . (allowedTo('lock_any') ? '1' : '2') . ' ELSE 0 END
  1050. WHERE id_topic IN ({array_int:locked_topic_ids})',
  1051. array(
  1052. 'locked_topic_ids' => $lockCache,
  1053. 'is_locked' => 0,
  1054. )
  1055. );
  1056. }
  1057. }
  1058. if (!empty($markCache))
  1059. {
  1060. $smcFunc['db_query']('', '
  1061. SELECT id_topic, unwatched
  1062. FROM {db_prefix}log_topics
  1063. WHERE id_topic IN ({array_int:selected_topics})
  1064. AND id_member = {int:current_user}',
  1065. array(
  1066. 'selected_topics' => $markCache,
  1067. 'current_user' => $user_info['id'],
  1068. )
  1069. );
  1070. $logged_topics = array();
  1071. while ($row = $smcFunc['db_fetch_assoc']($request))
  1072. $logged_topics[$row['id_topic']] = $row['unwatched'];
  1073. $smcFunc['db_free_result']($request);
  1074. $markArray = array();
  1075. foreach ($markCache as $topic)
  1076. $markArray[] = array($modSettings['maxMsgID'], $user_info['id'], $topic, (isset($logged_topics[$topic]) ? $logged_topics[$topic] : 0));
  1077. $smcFunc['db_insert']('replace',
  1078. '{db_prefix}log_topics',
  1079. array('id_msg' => 'int', 'id_member' => 'int', 'id_topic' => 'int', 'unwatched' => 'int'),
  1080. $markArray,
  1081. array('id_member', 'id_topic')
  1082. );
  1083. }
  1084. foreach ($moveCache as $topic)
  1085. {
  1086. // Didn't actually move anything!
  1087. if (!isset($topic[0]))
  1088. break;
  1089. logAction('move', array('topic' => $topic[0], 'board_from' => $topic[1], 'board_to' => $topic[2]));
  1090. sendNotifications($topic[0], 'move');
  1091. }
  1092. foreach ($lockCache as $topic)
  1093. {
  1094. logAction($lockStatus[$topic] ? 'lock' : 'unlock', array('topic' => $topic, 'board' => $lockCacheBoards[$topic]));
  1095. sendNotifications($topic, $lockStatus[$topic] ? 'lock' : 'unlock');
  1096. }
  1097. foreach ($stickyCache as $topic)
  1098. {
  1099. logAction($stickyCacheStatus[$topic] ? 'unsticky' : 'sticky', array('topic' => $topic, 'board' => $stickyCacheBoards[$topic]));
  1100. sendNotifications($topic, 'sticky');
  1101. }
  1102. updateStats('topic');
  1103. updateStats('message');
  1104. updateSettings(array(
  1105. 'calendar_updated' => time(),
  1106. ));
  1107. if (!empty($affectedBoards))
  1108. updateLastMessages(array_keys($affectedBoards));
  1109. redirectexit($redirect_url);
  1110. }
  1111. ?>