MessageIndex.php 50 KB

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