MessageIndex.php 48 KB

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