MessageIndex.php 52 KB

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