MessageIndex.php 52 KB

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