MessageIndex.php 46 KB

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