MessageIndex.php 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168
  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, 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, SUBSTRING(ml.body, 1, 385) AS last_body,
  327. SUBSTRING(mf.body, 1, 385) AS first_body, ml.smileys_enabled AS last_smileys, mf.smileys_enabled AS first_smileys
  328. FROM {db_prefix}topics AS t
  329. INNER JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)
  330. INNER JOIN {db_prefix}messages AS mf ON (mf.id_msg = t.id_first_msg)
  331. LEFT JOIN {db_prefix}members AS meml ON (meml.id_member = ml.id_member)
  332. LEFT JOIN {db_prefix}members AS memf ON (memf.id_member = mf.id_member)' . ($user_info['is_guest'] ? '' : '
  333. LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})
  334. LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = {int:current_board} AND lmr.id_member = {int:current_member})'). '
  335. WHERE ' . ($pre_query ? 't.id_topic IN ({array_int:topic_list})' : 't.id_board = {int:current_board}') . (!$modSettings['postmod_active'] || $context['can_approve_posts'] ? '' : '
  336. AND (t.approved = {int:is_approved}' . ($user_info['is_guest'] ? '' : ' OR t.id_member_started = {int:current_member}') . ')') . '
  337. 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')) . '
  338. LIMIT ' . ($pre_query ? '' : '{int:start}, ') . '{int:maxindex}',
  339. array(
  340. 'current_board' => $board,
  341. 'current_member' => $user_info['id'],
  342. 'topic_list' => $topic_ids,
  343. 'is_approved' => 1,
  344. 'find_set_topics' => implode(',', $topic_ids),
  345. 'start' => $start,
  346. 'maxindex' => $maxindex,
  347. )
  348. );
  349. // Begin 'printing' the message index for current board.
  350. while ($row = $smcFunc['db_fetch_assoc']($result))
  351. {
  352. if ($row['id_poll'] > 0 && $modSettings['pollMode'] == '0')
  353. continue;
  354. if (!$pre_query)
  355. $topic_ids[] = $row['id_topic'];
  356. if (!empty($settings['message_index_preview']))
  357. {
  358. // Limit them to 128 characters - do this FIRST because it's a lot of wasted censoring otherwise.
  359. $row['first_body'] = strip_tags(strtr(parse_bbc($row['first_body'], $row['first_smileys'], $row['id_first_msg']), array('<br />' => '&#10;')));
  360. if ($smcFunc['strlen']($row['first_body']) > 128)
  361. $row['first_body'] = $smcFunc['substr']($row['first_body'], 0, 128) . '...';
  362. $row['last_body'] = strip_tags(strtr(parse_bbc($row['last_body'], $row['last_smileys'], $row['id_last_msg']), array('<br />' => '&#10;')));
  363. if ($smcFunc['strlen']($row['last_body']) > 128)
  364. $row['last_body'] = $smcFunc['substr']($row['last_body'], 0, 128) . '...';
  365. // Censor the subject and message preview.
  366. censorText($row['first_subject']);
  367. censorText($row['first_body']);
  368. // Don't censor them twice!
  369. if ($row['id_first_msg'] == $row['id_last_msg'])
  370. {
  371. $row['last_subject'] = $row['first_subject'];
  372. $row['last_body'] = $row['first_body'];
  373. }
  374. else
  375. {
  376. censorText($row['last_subject']);
  377. censorText($row['last_body']);
  378. }
  379. }
  380. else
  381. {
  382. $row['first_body'] = '';
  383. $row['last_body'] = '';
  384. censorText($row['first_subject']);
  385. if ($row['id_first_msg'] == $row['id_last_msg'])
  386. $row['last_subject'] = $row['first_subject'];
  387. else
  388. censorText($row['last_subject']);
  389. }
  390. // Decide how many pages the topic should have.
  391. if ($row['num_replies'] + 1 > $context['messages_per_page'])
  392. {
  393. $pages = '&#171; ';
  394. // We can't pass start by reference.
  395. $start = -1;
  396. $pages .= constructPageIndex($scripturl . '?topic=' . $row['id_topic'] . '.%1$d', $start, $row['num_replies'] + 1, $context['messages_per_page'], true, false);
  397. // If we can use all, show all.
  398. if (!empty($modSettings['enableAllMessages']) && $row['num_replies'] + 1 < $modSettings['enableAllMessages'])
  399. $pages .= ' &nbsp;<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0;all">' . $txt['all'] . '</a>';
  400. $pages .= ' &#187;';
  401. }
  402. else
  403. $pages = '';
  404. // We need to check the topic icons exist...
  405. if (!empty($modSettings['messageIconChecks_enable']))
  406. {
  407. if (!isset($context['icon_sources'][$row['first_icon']]))
  408. $context['icon_sources'][$row['first_icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['first_icon'] . '.png') ? 'images_url' : 'default_images_url';
  409. if (!isset($context['icon_sources'][$row['last_icon']]))
  410. $context['icon_sources'][$row['last_icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['last_icon'] . '.png') ? 'images_url' : 'default_images_url';
  411. }
  412. else
  413. {
  414. if (!isset($context['icon_sources'][$row['first_icon']]))
  415. $context['icon_sources'][$row['first_icon']] = 'images_url';
  416. if (!isset($context['icon_sources'][$row['last_icon']]))
  417. $context['icon_sources'][$row['last_icon']] = 'images_url';
  418. }
  419. // 'Print' the topic info.
  420. $context['topics'][$row['id_topic']] = array(
  421. 'id' => $row['id_topic'],
  422. 'first_post' => array(
  423. 'id' => $row['id_first_msg'],
  424. 'member' => array(
  425. 'username' => $row['first_member_name'],
  426. 'name' => $row['first_display_name'],
  427. 'id' => $row['first_id_member'],
  428. 'href' => !empty($row['first_id_member']) ? $scripturl . '?action=profile;u=' . $row['first_id_member'] : '',
  429. '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']
  430. ),
  431. 'time' => timeformat($row['first_poster_time']),
  432. 'timestamp' => forum_time(true, $row['first_poster_time']),
  433. 'subject' => $row['first_subject'],
  434. 'preview' => $row['first_body'],
  435. 'icon' => $row['first_icon'],
  436. 'icon_url' => $settings[$context['icon_sources'][$row['first_icon']]] . '/post/' . $row['first_icon'] . '.png',
  437. 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
  438. 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['first_subject'] . '</a>'
  439. ),
  440. 'last_post' => array(
  441. 'id' => $row['id_last_msg'],
  442. 'member' => array(
  443. 'username' => $row['last_member_name'],
  444. 'name' => $row['last_display_name'],
  445. 'id' => $row['last_id_member'],
  446. 'href' => !empty($row['last_id_member']) ? $scripturl . '?action=profile;u=' . $row['last_id_member'] : '',
  447. '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']
  448. ),
  449. 'time' => timeformat($row['last_poster_time']),
  450. 'timestamp' => forum_time(true, $row['last_poster_time']),
  451. 'subject' => $row['last_subject'],
  452. 'preview' => $row['last_body'],
  453. 'icon' => $row['last_icon'],
  454. 'icon_url' => $settings[$context['icon_sources'][$row['last_icon']]] . '/post/' . $row['last_icon'] . '.png',
  455. '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')),
  456. '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>'
  457. ),
  458. 'is_sticky' => !empty($modSettings['enableStickyTopics']) && !empty($row['is_sticky']),
  459. 'is_locked' => !empty($row['locked']),
  460. 'is_poll' => $modSettings['pollMode'] == '1' && $row['id_poll'] > 0,
  461. 'is_hot' => $row['num_replies'] >= $modSettings['hotTopicPosts'],
  462. 'is_very_hot' => $row['num_replies'] >= $modSettings['hotTopicVeryPosts'],
  463. 'is_posted_in' => false,
  464. 'icon' => $row['first_icon'],
  465. 'icon_url' => $settings[$context['icon_sources'][$row['first_icon']]] . '/post/' . $row['first_icon'] . '.png',
  466. 'subject' => $row['first_subject'],
  467. 'new' => $row['new_from'] <= $row['id_msg_modified'],
  468. 'new_from' => $row['new_from'],
  469. 'newtime' => $row['new_from'],
  470. 'new_href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['new_from'] . '#new',
  471. 'pages' => $pages,
  472. 'replies' => comma_format($row['num_replies']),
  473. 'views' => comma_format($row['num_views']),
  474. 'approved' => $row['approved'],
  475. 'unapproved_posts' => $row['unapproved_posts'],
  476. );
  477. determineTopicClass($context['topics'][$row['id_topic']]);
  478. }
  479. $smcFunc['db_free_result']($result);
  480. // Fix the sequence of topics if they were retrieved in the wrong order. (for speed reasons...)
  481. if ($fake_ascending)
  482. $context['topics'] = array_reverse($context['topics'], true);
  483. if (!empty($modSettings['enableParticipation']) && !$user_info['is_guest'] && !empty($topic_ids))
  484. {
  485. $result = $smcFunc['db_query']('', '
  486. SELECT id_topic
  487. FROM {db_prefix}messages
  488. WHERE id_topic IN ({array_int:topic_list})
  489. AND id_member = {int:current_member}
  490. GROUP BY id_topic
  491. LIMIT ' . count($topic_ids),
  492. array(
  493. 'current_member' => $user_info['id'],
  494. 'topic_list' => $topic_ids,
  495. )
  496. );
  497. while ($row = $smcFunc['db_fetch_assoc']($result))
  498. {
  499. $context['topics'][$row['id_topic']]['is_posted_in'] = true;
  500. $context['topics'][$row['id_topic']]['class'] = 'my_' . $context['topics'][$row['id_topic']]['class'];
  501. }
  502. $smcFunc['db_free_result']($result);
  503. }
  504. }
  505. $context['jump_to'] = array(
  506. 'label' => addslashes(un_htmlspecialchars($txt['jump_to'])),
  507. 'board_name' => htmlspecialchars(strtr(strip_tags($board_info['name']), array('&amp;' => '&'))),
  508. 'child_level' => $board_info['child_level'],
  509. );
  510. // Is Quick Moderation active/needed?
  511. if (!empty($options['display_quick_mod']) && !empty($context['topics']))
  512. {
  513. $context['can_lock'] = allowedTo('lock_any');
  514. $context['can_sticky'] = allowedTo('make_sticky') && !empty($modSettings['enableStickyTopics']);
  515. $context['can_move'] = allowedTo('move_any');
  516. $context['can_remove'] = allowedTo('remove_any');
  517. $context['can_merge'] = allowedTo('merge_any');
  518. // Ignore approving own topics as it's unlikely to come up...
  519. $context['can_approve'] = $modSettings['postmod_active'] && allowedTo('approve_posts') && !empty($board_info['unapproved_topics']);
  520. // Can we restore topics?
  521. $context['can_restore'] = allowedTo('move_any') && !empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] == $board;
  522. // Set permissions for all the topics.
  523. foreach ($context['topics'] as $t => $topic)
  524. {
  525. $started = $topic['first_post']['member']['id'] == $user_info['id'];
  526. $context['topics'][$t]['quick_mod'] = array(
  527. 'lock' => allowedTo('lock_any') || ($started && allowedTo('lock_own')),
  528. 'sticky' => allowedTo('make_sticky') && !empty($modSettings['enableStickyTopics']),
  529. 'move' => allowedTo('move_any') || ($started && allowedTo('move_own')),
  530. 'modify' => allowedTo('modify_any') || ($started && allowedTo('modify_own')),
  531. 'remove' => allowedTo('remove_any') || ($started && allowedTo('remove_own')),
  532. 'approve' => $context['can_approve'] && $topic['unapproved_posts']
  533. );
  534. $context['can_lock'] |= ($started && allowedTo('lock_own'));
  535. $context['can_move'] |= ($started && allowedTo('move_own'));
  536. $context['can_remove'] |= ($started && allowedTo('remove_own'));
  537. }
  538. // Find the boards/cateogories they can move their topic to.
  539. if ($options['display_quick_mod'] == 1 && $context['can_move'] && !empty($context['topics']))
  540. {
  541. require_once($sourcedir . '/Subs-MessageIndex.php');
  542. $boardListOptions = array(
  543. 'excluded_boards' => array($board),
  544. 'not_redirection' => true,
  545. 'use_permissions' => true,
  546. 'selected_board' => empty($_SESSION['move_to_topic']) ? null : $_SESSION['move_to_topic'],
  547. );
  548. $context['move_to_boards'] = getBoardList($boardListOptions);
  549. // Make the boards safe for display.
  550. foreach ($context['move_to_boards'] as $id_cat => $cat)
  551. {
  552. $context['move_to_boards'][$id_cat]['name'] = strip_tags($cat['name']);
  553. foreach ($cat['boards'] as $id_board => $aboard)
  554. $context['move_to_boards'][$id_cat]['boards'][$id_board]['name'] = strip_tags($aboard['name']);
  555. }
  556. // With no other boards to see, it's useless to move.
  557. if (empty($context['move_to_boards']))
  558. $context['can_move'] = false;
  559. }
  560. // Can we use quick moderation checkboxes?
  561. if ($options['display_quick_mod'] == 1)
  562. $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'];
  563. // Or the icons?
  564. else
  565. $context['can_quick_mod'] = $context['can_remove'] || $context['can_lock'] || $context['can_sticky'] || $context['can_move'];
  566. }
  567. // If there are children, but no topics and no ability to post topics...
  568. $context['no_topic_listing'] = !empty($context['boards']) && empty($context['topics']) && !$context['can_post_new'];
  569. }
  570. /**
  571. * Allows for moderation from the message index.
  572. * @todo refactor this...
  573. */
  574. function QuickModeration()
  575. {
  576. global $sourcedir, $board, $user_info, $modSettings, $smcFunc, $context;
  577. // Check the session = get or post.
  578. checkSession('request');
  579. // Lets go straight to the restore area.
  580. if (isset($_REQUEST['qaction']) && $_REQUEST['qaction'] == 'restore' && !empty($_REQUEST['topics']))
  581. redirectexit('action=restoretopic;topics=' . implode(',', $_REQUEST['topics']) . ';' . $context['session_var'] . '=' . $context['session_id']);
  582. if (isset($_SESSION['topicseen_cache']))
  583. $_SESSION['topicseen_cache'] = array();
  584. // This is going to be needed to send off the notifications and for updateLastMessages().
  585. require_once($sourcedir . '/Subs-Post.php');
  586. // Remember the last board they moved things to.
  587. if (isset($_REQUEST['move_to']))
  588. $_SESSION['move_to_topic'] = $_REQUEST['move_to'];
  589. // Only a few possible actions.
  590. $possibleActions = array();
  591. if (!empty($board))
  592. {
  593. $boards_can = array(
  594. 'make_sticky' => allowedTo('make_sticky') ? array($board) : array(),
  595. 'move_any' => allowedTo('move_any') ? array($board) : array(),
  596. 'move_own' => allowedTo('move_own') ? array($board) : array(),
  597. 'remove_any' => allowedTo('remove_any') ? array($board) : array(),
  598. 'remove_own' => allowedTo('remove_own') ? array($board) : array(),
  599. 'lock_any' => allowedTo('lock_any') ? array($board) : array(),
  600. 'lock_own' => allowedTo('lock_own') ? array($board) : array(),
  601. 'merge_any' => allowedTo('merge_any') ? array($board) : array(),
  602. 'approve_posts' => allowedTo('approve_posts') ? array($board) : array(),
  603. );
  604. $redirect_url = 'board=' . $board . '.' . $_REQUEST['start'];
  605. }
  606. else
  607. {
  608. /**
  609. * @todo Ugly. There's no getting around this, is there?
  610. * @todo Maybe just do this on the actions people want to use?
  611. */
  612. $boards_can = array(
  613. 'make_sticky' => boardsAllowedTo('make_sticky'),
  614. 'move_any' => boardsAllowedTo('move_any'),
  615. 'move_own' => boardsAllowedTo('move_own'),
  616. 'remove_any' => boardsAllowedTo('remove_any'),
  617. 'remove_own' => boardsAllowedTo('remove_own'),
  618. 'lock_any' => boardsAllowedTo('lock_any'),
  619. 'lock_own' => boardsAllowedTo('lock_own'),
  620. 'merge_any' => boardsAllowedTo('merge_any'),
  621. 'approve_posts' => boardsAllowedTo('approve_posts'),
  622. );
  623. $redirect_url = isset($_POST['redirect_url']) ? $_POST['redirect_url'] : (isset($_SESSION['old_url']) ? $_SESSION['old_url'] : '');
  624. }
  625. if (!$user_info['is_guest'])
  626. $possibleActions[] = 'markread';
  627. if (!empty($boards_can['make_sticky']) && !empty($modSettings['enableStickyTopics']))
  628. $possibleActions[] = 'sticky';
  629. if (!empty($boards_can['move_any']) || !empty($boards_can['move_own']))
  630. $possibleActions[] = 'move';
  631. if (!empty($boards_can['remove_any']) || !empty($boards_can['remove_own']))
  632. $possibleActions[] = 'remove';
  633. if (!empty($boards_can['lock_any']) || !empty($boards_can['lock_own']))
  634. $possibleActions[] = 'lock';
  635. if (!empty($boards_can['merge_any']))
  636. $possibleActions[] = 'merge';
  637. if (!empty($boards_can['approve_posts']))
  638. $possibleActions[] = 'approve';
  639. // Two methods: $_REQUEST['actions'] (id_topic => action), and $_REQUEST['topics'] and $_REQUEST['qaction'].
  640. // (if action is 'move', $_REQUEST['move_to'] or $_REQUEST['move_tos'][$topic] is used.)
  641. if (!empty($_REQUEST['topics']))
  642. {
  643. // If the action isn't valid, just quit now.
  644. if (empty($_REQUEST['qaction']) || !in_array($_REQUEST['qaction'], $possibleActions))
  645. redirectexit($redirect_url);
  646. // Merge requires all topics as one parameter and can be done at once.
  647. if ($_REQUEST['qaction'] == 'merge')
  648. {
  649. // Merge requires at least two topics.
  650. if (empty($_REQUEST['topics']) || count($_REQUEST['topics']) < 2)
  651. redirectexit($redirect_url);
  652. require_once($sourcedir . '/SplitTopics.php');
  653. return MergeExecute($_REQUEST['topics']);
  654. }
  655. // Just convert to the other method, to make it easier.
  656. foreach ($_REQUEST['topics'] as $topic)
  657. $_REQUEST['actions'][(int) $topic] = $_REQUEST['qaction'];
  658. }
  659. // Weird... how'd you get here?
  660. if (empty($_REQUEST['actions']))
  661. redirectexit($redirect_url);
  662. // Validate each action.
  663. $temp = array();
  664. foreach ($_REQUEST['actions'] as $topic => $action)
  665. {
  666. if (in_array($action, $possibleActions))
  667. $temp[(int) $topic] = $action;
  668. }
  669. $_REQUEST['actions'] = $temp;
  670. if (!empty($_REQUEST['actions']))
  671. {
  672. // Find all topics...
  673. $request = $smcFunc['db_query']('', '
  674. SELECT id_topic, id_member_started, id_board, locked, approved, unapproved_posts
  675. FROM {db_prefix}topics
  676. WHERE id_topic IN ({array_int:action_topic_ids})
  677. LIMIT ' . count($_REQUEST['actions']),
  678. array(
  679. 'action_topic_ids' => array_keys($_REQUEST['actions']),
  680. )
  681. );
  682. while ($row = $smcFunc['db_fetch_assoc']($request))
  683. {
  684. if (!empty($board))
  685. {
  686. if ($row['id_board'] != $board || ($modSettings['postmod_active'] && !$row['approved'] && !allowedTo('approve_posts')))
  687. unset($_REQUEST['actions'][$row['id_topic']]);
  688. }
  689. else
  690. {
  691. // Don't allow them to act on unapproved posts they can't see...
  692. if ($modSettings['postmod_active'] && !$row['approved'] && !in_array(0, $boards_can['approve_posts']) && !in_array($row['id_board'], $boards_can['approve_posts']))
  693. unset($_REQUEST['actions'][$row['id_topic']]);
  694. // Goodness, this is fun. We need to validate the action.
  695. elseif ($_REQUEST['actions'][$row['id_topic']] == 'sticky' && !in_array(0, $boards_can['make_sticky']) && !in_array($row['id_board'], $boards_can['make_sticky']))
  696. unset($_REQUEST['actions'][$row['id_topic']]);
  697. 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']))))
  698. unset($_REQUEST['actions'][$row['id_topic']]);
  699. 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']))))
  700. unset($_REQUEST['actions'][$row['id_topic']]);
  701. // @todo $locked is not set, what are you trying to do? (taking the change it is supposed to be $row['locked'])
  702. 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']))))
  703. unset($_REQUEST['actions'][$row['id_topic']]);
  704. // If the topic is approved then you need permission to approve the posts within.
  705. 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']))))
  706. unset($_REQUEST['actions'][$row['id_topic']]);
  707. }
  708. }
  709. $smcFunc['db_free_result']($request);
  710. }
  711. $stickyCache = array();
  712. $moveCache = array(0 => array(), 1 => array());
  713. $removeCache = array();
  714. $lockCache = array();
  715. $markCache = array();
  716. $approveCache = array();
  717. // Separate the actions.
  718. foreach ($_REQUEST['actions'] as $topic => $action)
  719. {
  720. $topic = (int) $topic;
  721. if ($action == 'markread')
  722. $markCache[] = $topic;
  723. elseif ($action == 'sticky')
  724. $stickyCache[] = $topic;
  725. elseif ($action == 'move')
  726. {
  727. // $moveCache[0] is the topic, $moveCache[1] is the board to move to.
  728. $moveCache[1][$topic] = (int) (isset($_REQUEST['move_tos'][$topic]) ? $_REQUEST['move_tos'][$topic] : $_REQUEST['move_to']);
  729. if (empty($moveCache[1][$topic]))
  730. continue;
  731. $moveCache[0][] = $topic;
  732. }
  733. elseif ($action == 'remove')
  734. $removeCache[] = $topic;
  735. elseif ($action == 'lock')
  736. $lockCache[] = $topic;
  737. elseif ($action == 'approve')
  738. $approveCache[] = $topic;
  739. }
  740. if (empty($board))
  741. $affectedBoards = array();
  742. else
  743. $affectedBoards = array($board => array(0, 0));
  744. // Do all the stickies...
  745. if (!empty($stickyCache))
  746. {
  747. $smcFunc['db_query']('', '
  748. UPDATE {db_prefix}topics
  749. SET is_sticky = CASE WHEN is_sticky = {int:is_sticky} THEN 0 ELSE 1 END
  750. WHERE id_topic IN ({array_int:sticky_topic_ids})',
  751. array(
  752. 'sticky_topic_ids' => $stickyCache,
  753. 'is_sticky' => 1,
  754. )
  755. );
  756. // Get the board IDs and Sticky status
  757. $request = $smcFunc['db_query']('', '
  758. SELECT id_topic, id_board, is_sticky
  759. FROM {db_prefix}topics
  760. WHERE id_topic IN ({array_int:sticky_topic_ids})
  761. LIMIT ' . count($stickyCache),
  762. array(
  763. 'sticky_topic_ids' => $stickyCache,
  764. )
  765. );
  766. $stickyCacheBoards = array();
  767. $stickyCacheStatus = array();
  768. while ($row = $smcFunc['db_fetch_assoc']($request))
  769. {
  770. $stickyCacheBoards[$row['id_topic']] = $row['id_board'];
  771. $stickyCacheStatus[$row['id_topic']] = empty($row['is_sticky']);
  772. }
  773. $smcFunc['db_free_result']($request);
  774. }
  775. // Move sucka! (this is, by the by, probably the most complicated part....)
  776. if (!empty($moveCache[0]))
  777. {
  778. // I know - I just KNOW you're trying to beat the system. Too bad for you... we CHECK :P.
  779. $request = $smcFunc['db_query']('', '
  780. SELECT t.id_topic, t.id_board, b.count_posts
  781. FROM {db_prefix}topics AS t
  782. LEFT JOIN {db_prefix}boards AS b ON (t.id_board = b.id_board)
  783. WHERE t.id_topic IN ({array_int:move_topic_ids})' . (!empty($board) && !allowedTo('move_any') ? '
  784. AND t.id_member_started = {int:current_member}' : '') . '
  785. LIMIT ' . count($moveCache[0]),
  786. array(
  787. 'current_member' => $user_info['id'],
  788. 'move_topic_ids' => $moveCache[0],
  789. )
  790. );
  791. $moveTos = array();
  792. $moveCache2 = array();
  793. $countPosts = array();
  794. while ($row = $smcFunc['db_fetch_assoc']($request))
  795. {
  796. $to = $moveCache[1][$row['id_topic']];
  797. if (empty($to))
  798. continue;
  799. // Does this topic's board count the posts or not?
  800. $countPosts[$row['id_topic']] = empty($row['count_posts']);
  801. if (!isset($moveTos[$to]))
  802. $moveTos[$to] = array();
  803. $moveTos[$to][] = $row['id_topic'];
  804. // For reporting...
  805. $moveCache2[] = array($row['id_topic'], $row['id_board'], $to);
  806. }
  807. $smcFunc['db_free_result']($request);
  808. $moveCache = $moveCache2;
  809. require_once($sourcedir . '/MoveTopic.php');
  810. // Do the actual moves...
  811. foreach ($moveTos as $to => $topics)
  812. moveTopics($topics, $to);
  813. // Does the post counts need to be updated?
  814. if (!empty($moveTos))
  815. {
  816. $topicRecounts = array();
  817. $request = $smcFunc['db_query']('', '
  818. SELECT id_board, count_posts
  819. FROM {db_prefix}boards
  820. WHERE id_board IN ({array_int:move_boards})',
  821. array(
  822. 'move_boards' => array_keys($moveTos),
  823. )
  824. );
  825. while ($row = $smcFunc['db_fetch_assoc']($request))
  826. {
  827. $cp = empty($row['count_posts']);
  828. // Go through all the topics that are being moved to this board.
  829. foreach ($moveTos[$row['id_board']] as $topic)
  830. {
  831. // If both boards have the same value for post counting then no adjustment needs to be made.
  832. if ($countPosts[$topic] != $cp)
  833. {
  834. // If the board being moved to does count the posts then the other one doesn't so add to their post count.
  835. $topicRecounts[$topic] = $cp ? '+' : '-';
  836. }
  837. }
  838. }
  839. $smcFunc['db_free_result']($request);
  840. if (!empty($topicRecounts))
  841. {
  842. $members = array();
  843. // Get all the members who have posted in the moved topics.
  844. $request = $smcFunc['db_query']('', '
  845. SELECT id_member, id_topic
  846. FROM {db_prefix}messages
  847. WHERE id_topic IN ({array_int:moved_topic_ids})',
  848. array(
  849. 'moved_topic_ids' => array_keys($topicRecounts),
  850. )
  851. );
  852. while ($row = $smcFunc['db_fetch_assoc']($request))
  853. {
  854. if (!isset($members[$row['id_member']]))
  855. $members[$row['id_member']] = 0;
  856. if ($topicRecounts[$row['id_topic']] === '+')
  857. $members[$row['id_member']] += 1;
  858. else
  859. $members[$row['id_member']] -= 1;
  860. }
  861. $smcFunc['db_free_result']($request);
  862. // And now update them member's post counts
  863. foreach ($members as $id_member => $post_adj)
  864. updateMemberData($id_member, array('posts' => 'posts + ' . $post_adj));
  865. }
  866. }
  867. }
  868. // Now delete the topics...
  869. if (!empty($removeCache))
  870. {
  871. // They can only delete their own topics. (we wouldn't be here if they couldn't do that..)
  872. $result = $smcFunc['db_query']('', '
  873. SELECT id_topic, id_board
  874. FROM {db_prefix}topics
  875. WHERE id_topic IN ({array_int:removed_topic_ids})' . (!empty($board) && !allowedTo('remove_any') ? '
  876. AND id_member_started = {int:current_member}' : '') . '
  877. LIMIT ' . count($removeCache),
  878. array(
  879. 'current_member' => $user_info['id'],
  880. 'removed_topic_ids' => $removeCache,
  881. )
  882. );
  883. $removeCache = array();
  884. $removeCacheBoards = array();
  885. while ($row = $smcFunc['db_fetch_assoc']($result))
  886. {
  887. $removeCache[] = $row['id_topic'];
  888. $removeCacheBoards[$row['id_topic']] = $row['id_board'];
  889. }
  890. $smcFunc['db_free_result']($result);
  891. // Maybe *none* were their own topics.
  892. if (!empty($removeCache))
  893. {
  894. // Gotta send the notifications *first*!
  895. foreach ($removeCache as $topic)
  896. {
  897. // Only log the topic ID if it's not in the recycle board.
  898. logAction('remove', array((empty($modSettings['recycle_enable']) || $modSettings['recycle_board'] != $removeCacheBoards[$topic] ? 'topic' : 'old_topic_id') => $topic, 'board' => $removeCacheBoards[$topic]));
  899. sendNotifications($topic, 'remove');
  900. }
  901. require_once($sourcedir . '/RemoveTopic.php');
  902. removeTopics($removeCache);
  903. }
  904. }
  905. // Approve the topics...
  906. if (!empty($approveCache))
  907. {
  908. // We need unapproved topic ids and their authors!
  909. $request = $smcFunc['db_query']('', '
  910. SELECT id_topic, id_member_started
  911. FROM {db_prefix}topics
  912. WHERE id_topic IN ({array_int:approve_topic_ids})
  913. AND approved = {int:not_approved}
  914. LIMIT ' . count($approveCache),
  915. array(
  916. 'approve_topic_ids' => $approveCache,
  917. 'not_approved' => 0,
  918. )
  919. );
  920. $approveCache = array();
  921. $approveCacheMembers = array();
  922. while ($row = $smcFunc['db_fetch_assoc']($request))
  923. {
  924. $approveCache[] = $row['id_topic'];
  925. $approveCacheMembers[$row['id_topic']] = $row['id_member_started'];
  926. }
  927. $smcFunc['db_free_result']($request);
  928. // Any topics to approve?
  929. if (!empty($approveCache))
  930. {
  931. // Handle the approval part...
  932. approveTopics($approveCache);
  933. // Time for some logging!
  934. foreach ($approveCache as $topic)
  935. logAction('approve_topic', array('topic' => $topic, 'member' => $approveCacheMembers[$topic]));
  936. }
  937. }
  938. // And (almost) lastly, lock the topics...
  939. if (!empty($lockCache))
  940. {
  941. $lockStatus = array();
  942. // Gotta make sure they CAN lock/unlock these topics...
  943. if (!empty($board) && !allowedTo('lock_any'))
  944. {
  945. // Make sure they started the topic AND it isn't already locked by someone with higher priv's.
  946. $result = $smcFunc['db_query']('', '
  947. SELECT id_topic, locked, id_board
  948. FROM {db_prefix}topics
  949. WHERE id_topic IN ({array_int:locked_topic_ids})
  950. AND id_member_started = {int:current_member}
  951. AND locked IN (2, 0)
  952. LIMIT ' . count($lockCache),
  953. array(
  954. 'current_member' => $user_info['id'],
  955. 'locked_topic_ids' => $lockCache,
  956. )
  957. );
  958. $lockCache = array();
  959. $lockCacheBoards = array();
  960. while ($row = $smcFunc['db_fetch_assoc']($result))
  961. {
  962. $lockCache[] = $row['id_topic'];
  963. $lockCacheBoards[$row['id_topic']] = $row['id_board'];
  964. $lockStatus[$row['id_topic']] = empty($row['locked']);
  965. }
  966. $smcFunc['db_free_result']($result);
  967. }
  968. else
  969. {
  970. $result = $smcFunc['db_query']('', '
  971. SELECT id_topic, locked, id_board
  972. FROM {db_prefix}topics
  973. WHERE id_topic IN ({array_int:locked_topic_ids})
  974. LIMIT ' . count($lockCache),
  975. array(
  976. 'locked_topic_ids' => $lockCache,
  977. )
  978. );
  979. $lockCacheBoards = array();
  980. while ($row = $smcFunc['db_fetch_assoc']($result))
  981. {
  982. $lockStatus[$row['id_topic']] = empty($row['locked']);
  983. $lockCacheBoards[$row['id_topic']] = $row['id_board'];
  984. }
  985. $smcFunc['db_free_result']($result);
  986. }
  987. // It could just be that *none* were their own topics...
  988. if (!empty($lockCache))
  989. {
  990. // Alternate the locked value.
  991. $smcFunc['db_query']('', '
  992. UPDATE {db_prefix}topics
  993. SET locked = CASE WHEN locked = {int:is_locked} THEN ' . (allowedTo('lock_any') ? '1' : '2') . ' ELSE 0 END
  994. WHERE id_topic IN ({array_int:locked_topic_ids})',
  995. array(
  996. 'locked_topic_ids' => $lockCache,
  997. 'is_locked' => 0,
  998. )
  999. );
  1000. }
  1001. }
  1002. if (!empty($markCache))
  1003. {
  1004. $markArray = array();
  1005. foreach ($markCache as $topic)
  1006. $markArray[] = array($modSettings['maxMsgID'], $user_info['id'], $topic);
  1007. $smcFunc['db_insert']('replace',
  1008. '{db_prefix}log_topics',
  1009. array('id_msg' => 'int', 'id_member' => 'int', 'id_topic' => 'int'),
  1010. $markArray,
  1011. array('id_member', 'id_topic')
  1012. );
  1013. }
  1014. foreach ($moveCache as $topic)
  1015. {
  1016. // Didn't actually move anything!
  1017. if (!isset($topic[0]))
  1018. break;
  1019. logAction('move', array('topic' => $topic[0], 'board_from' => $topic[1], 'board_to' => $topic[2]));
  1020. sendNotifications($topic[0], 'move');
  1021. }
  1022. foreach ($lockCache as $topic)
  1023. {
  1024. logAction($lockStatus[$topic] ? 'lock' : 'unlock', array('topic' => $topic, 'board' => $lockCacheBoards[$topic]));
  1025. sendNotifications($topic, $lockStatus[$topic] ? 'lock' : 'unlock');
  1026. }
  1027. foreach ($stickyCache as $topic)
  1028. {
  1029. logAction($stickyCacheStatus[$topic] ? 'unsticky' : 'sticky', array('topic' => $topic, 'board' => $stickyCacheBoards[$topic]));
  1030. sendNotifications($topic, 'sticky');
  1031. }
  1032. updateStats('topic');
  1033. updateStats('message');
  1034. updateSettings(array(
  1035. 'calendar_updated' => time(),
  1036. ));
  1037. if (!empty($affectedBoards))
  1038. updateLastMessages(array_keys($affectedBoards));
  1039. redirectexit($redirect_url);
  1040. }
  1041. ?>