MessageIndex.php 52 KB

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