MessageIndex.php 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  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. // 'Print' the topic info.
  448. $context['topics'][$row['id_topic']] = array(
  449. 'id' => $row['id_topic'],
  450. 'first_post' => array(
  451. 'id' => $row['id_first_msg'],
  452. 'member' => array(
  453. 'username' => $row['first_member_name'],
  454. 'name' => $row['first_display_name'],
  455. 'id' => $row['first_id_member'],
  456. 'href' => !empty($row['first_id_member']) ? $scripturl . '?action=profile;u=' . $row['first_id_member'] : '',
  457. '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']
  458. ),
  459. 'time' => timeformat($row['first_poster_time']),
  460. 'timestamp' => forum_time(true, $row['first_poster_time']),
  461. 'subject' => $row['first_subject'],
  462. 'preview' => $row['first_body'],
  463. 'icon' => $row['first_icon'],
  464. 'icon_url' => $settings[$context['icon_sources'][$row['first_icon']]] . '/post/' . $row['first_icon'] . '.png',
  465. 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
  466. 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['first_subject'] . '</a>'
  467. ),
  468. 'last_post' => array(
  469. 'id' => $row['id_last_msg'],
  470. 'member' => array(
  471. 'username' => $row['last_member_name'],
  472. 'name' => $row['last_display_name'],
  473. 'id' => $row['last_id_member'],
  474. 'href' => !empty($row['last_id_member']) ? $scripturl . '?action=profile;u=' . $row['last_id_member'] : '',
  475. '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']
  476. ),
  477. 'time' => timeformat($row['last_poster_time']),
  478. 'timestamp' => forum_time(true, $row['last_poster_time']),
  479. 'subject' => $row['last_subject'],
  480. 'preview' => $row['last_body'],
  481. 'icon' => $row['last_icon'],
  482. 'icon_url' => $settings[$context['icon_sources'][$row['last_icon']]] . '/post/' . $row['last_icon'] . '.png',
  483. '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')),
  484. '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>'
  485. ),
  486. 'is_sticky' => !empty($row['is_sticky']),
  487. 'is_locked' => !empty($row['locked']),
  488. 'is_redirect' => !empty($row['id_redirect_topic']),
  489. 'is_poll' => $modSettings['pollMode'] == '1' && $row['id_poll'] > 0,
  490. 'is_posted_in' => false,
  491. 'icon' => $row['first_icon'],
  492. 'icon_url' => $settings[$context['icon_sources'][$row['first_icon']]] . '/post/' . $row['first_icon'] . '.png',
  493. 'subject' => $row['first_subject'],
  494. 'new' => $row['new_from'] <= $row['id_msg_modified'],
  495. 'new_from' => $row['new_from'],
  496. 'newtime' => $row['new_from'],
  497. 'new_href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['new_from'] . '#new',
  498. 'pages' => $pages,
  499. 'replies' => comma_format($row['num_replies']),
  500. 'views' => comma_format($row['num_views']),
  501. 'approved' => $row['approved'],
  502. 'unapproved_posts' => $row['unapproved_posts'],
  503. );
  504. if (!empty($settings['avatars_on_indexes']))
  505. $context['topics'][$row['id_topic']]['last_post']['member']['avatar'] = array(
  506. 'name' => $row['avatar'],
  507. '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'] . '" alt="" />' : '<img class="avatar" src="' . $modSettings['avatar_url'] . '/' . $smcFunc['htmlspecialchars']($row['avatar']) . '" alt="" />'),
  508. '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']),
  509. 'url' => $row['avatar'] == '' ? '' : (stristr($row['avatar'], 'http://') || stristr($row['avatar'], 'https://') ? $row['avatar'] : $modSettings['avatar_url'] . '/' . $row['avatar'])
  510. );
  511. }
  512. $smcFunc['db_free_result']($result);
  513. // Fix the sequence of topics if they were retrieved in the wrong order. (for speed reasons...)
  514. if ($fake_ascending)
  515. $context['topics'] = array_reverse($context['topics'], true);
  516. if (!empty($modSettings['enableParticipation']) && !$user_info['is_guest'] && !empty($topic_ids))
  517. {
  518. $result = $smcFunc['db_query']('', '
  519. SELECT id_topic
  520. FROM {db_prefix}messages
  521. WHERE id_topic IN ({array_int:topic_list})
  522. AND id_member = {int:current_member}
  523. GROUP BY id_topic
  524. LIMIT ' . count($topic_ids),
  525. array(
  526. 'current_member' => $user_info['id'],
  527. 'topic_list' => $topic_ids,
  528. )
  529. );
  530. while ($row = $smcFunc['db_fetch_assoc']($result))
  531. $context['topics'][$row['id_topic']]['is_posted_in'] = true;
  532. $smcFunc['db_free_result']($result);
  533. }
  534. }
  535. $context['jump_to'] = array(
  536. 'label' => addslashes(un_htmlspecialchars($txt['jump_to'])),
  537. 'board_name' => $smcFunc['htmlspecialchars'](strtr(strip_tags($board_info['name']), array('&amp;' => '&'))),
  538. 'child_level' => $board_info['child_level'],
  539. );
  540. // Is Quick Moderation active/needed?
  541. if (!empty($options['display_quick_mod']) && !empty($context['topics']))
  542. {
  543. $context['can_markread'] = $context['user']['is_logged'];
  544. $context['can_lock'] = allowedTo('lock_any');
  545. $context['can_sticky'] = allowedTo('make_sticky');
  546. $context['can_move'] = allowedTo('move_any');
  547. $context['can_remove'] = allowedTo('remove_any');
  548. $context['can_merge'] = allowedTo('merge_any');
  549. // Ignore approving own topics as it's unlikely to come up...
  550. $context['can_approve'] = $modSettings['postmod_active'] && allowedTo('approve_posts') && !empty($board_info['unapproved_topics']);
  551. // Can we restore topics?
  552. $context['can_restore'] = allowedTo('move_any') && !empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] == $board;
  553. if ($user_info['is_admin'] || $modSettings['topic_move_any'])
  554. $context['can_move_any'] = true;
  555. else
  556. {
  557. // We'll use this in a minute
  558. $boards_allowed = boardsAllowedTo('post_new');
  559. // How many boards can you do this on besides this one?
  560. $context['can_move_any'] = count($boards_allowed) > 1;
  561. }
  562. // Set permissions for all the topics.
  563. foreach ($context['topics'] as $t => $topic)
  564. {
  565. $started = $topic['first_post']['member']['id'] == $user_info['id'];
  566. $context['topics'][$t]['quick_mod'] = array(
  567. 'lock' => allowedTo('lock_any') || ($started && allowedTo('lock_own')),
  568. 'sticky' => allowedTo('make_sticky'),
  569. 'move' => (allowedTo('move_any') || ($started && allowedTo('move_own')) && $context['can_move_any']),
  570. 'modify' => allowedTo('modify_any') || ($started && allowedTo('modify_own')),
  571. 'remove' => allowedTo('remove_any') || ($started && allowedTo('remove_own')),
  572. 'approve' => $context['can_approve'] && $topic['unapproved_posts']
  573. );
  574. $context['can_lock'] |= ($started && allowedTo('lock_own'));
  575. $context['can_move'] |= ($started && allowedTo('move_own') && $context['can_move_any']);
  576. $context['can_remove'] |= ($started && allowedTo('remove_own'));
  577. }
  578. // Can we use quick moderation checkboxes?
  579. if ($options['display_quick_mod'] == 1)
  580. $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'];
  581. // Or the icons?
  582. else
  583. $context['can_quick_mod'] = $context['can_remove'] || $context['can_lock'] || $context['can_sticky'] || $context['can_move'];
  584. }
  585. if (!empty($context['can_quick_mod']) && $options['display_quick_mod'] == 1)
  586. {
  587. $context['qmod_actions'] = array('approve', 'remove', 'lock', 'sticky', 'move', 'merge', 'restore', 'markread');
  588. call_integration_hook('integrate_quick_mod_actions');
  589. }
  590. // If there are children, but no topics and no ability to post topics...
  591. $context['no_topic_listing'] = !empty($context['boards']) && empty($context['topics']) && !$context['can_post_new'];
  592. // Build the message index button array.
  593. $context['normal_buttons'] = array(
  594. '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),
  595. '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'),
  596. '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']),
  597. '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']),
  598. );
  599. // Allow adding new buttons easily.
  600. // Note: $context['normal_buttons'] is added for backward compatibility with 2.0, but is deprecated and should not be used
  601. call_integration_hook('integrate_messageindex_buttons', array(&$context['normal_buttons']));
  602. }
  603. /**
  604. * Allows for moderation from the message index.
  605. * @todo refactor this...
  606. */
  607. function QuickModeration()
  608. {
  609. global $sourcedir, $board, $user_info, $modSettings, $smcFunc, $context;
  610. // Check the session = get or post.
  611. checkSession('request');
  612. // Lets go straight to the restore area.
  613. if (isset($_REQUEST['qaction']) && $_REQUEST['qaction'] == 'restore' && !empty($_REQUEST['topics']))
  614. redirectexit('action=restoretopic;topics=' . implode(',', $_REQUEST['topics']) . ';' . $context['session_var'] . '=' . $context['session_id']);
  615. if (isset($_SESSION['topicseen_cache']))
  616. $_SESSION['topicseen_cache'] = array();
  617. // This is going to be needed to send off the notifications and for updateLastMessages().
  618. require_once($sourcedir . '/Subs-Post.php');
  619. // Remember the last board they moved things to.
  620. if (isset($_REQUEST['move_to']))
  621. $_SESSION['move_to_topic'] = $_REQUEST['move_to'];
  622. // Only a few possible actions.
  623. $possibleActions = array();
  624. if (!empty($board))
  625. {
  626. $boards_can = array(
  627. 'make_sticky' => allowedTo('make_sticky') ? array($board) : array(),
  628. 'move_any' => allowedTo('move_any') ? array($board) : array(),
  629. 'move_own' => allowedTo('move_own') ? array($board) : array(),
  630. 'remove_any' => allowedTo('remove_any') ? array($board) : array(),
  631. 'remove_own' => allowedTo('remove_own') ? array($board) : array(),
  632. 'lock_any' => allowedTo('lock_any') ? array($board) : array(),
  633. 'lock_own' => allowedTo('lock_own') ? array($board) : array(),
  634. 'merge_any' => allowedTo('merge_any') ? array($board) : array(),
  635. 'approve_posts' => allowedTo('approve_posts') ? array($board) : array(),
  636. );
  637. $redirect_url = 'board=' . $board . '.' . $_REQUEST['start'];
  638. }
  639. else
  640. {
  641. /**
  642. * @todo Ugly. There's no getting around this, is there?
  643. * @todo Maybe just do this on the actions people want to use?
  644. */
  645. $boards_can = boardsAllowedTo(array('make_sticky', 'move_any', 'move_own', 'remove_any', 'remove_own', 'lock_any', 'lock_own', 'merge_any', 'approve_posts'), true, false);
  646. $redirect_url = isset($_POST['redirect_url']) ? $_POST['redirect_url'] : (isset($_SESSION['old_url']) ? $_SESSION['old_url'] : '');
  647. }
  648. // Are we enforcing the "no moving topics to boards where you can't post new ones" rule?
  649. if (!$user_info['is_admin'] && !$modSettings['topic_move_any'])
  650. {
  651. // Don't count this board, if it's specified
  652. if (!empty($board))
  653. {
  654. $boards_can['post_new'] = array_diff(boardsAllowedTo('post_new'), array($board));
  655. }
  656. else
  657. {
  658. $boards_can['post_new'] = boardsAllowedTo('post_new');
  659. }
  660. if (empty($boards_can['post_new']))
  661. {
  662. $boards_can['move_any'] = $boards_can['move_own'] = array();
  663. }
  664. }
  665. if (!$user_info['is_guest'])
  666. $possibleActions[] = 'markread';
  667. if (!empty($boards_can['make_sticky']))
  668. $possibleActions[] = 'sticky';
  669. if (!empty($boards_can['move_any']) || !empty($boards_can['move_own']))
  670. $possibleActions[] = 'move';
  671. if (!empty($boards_can['remove_any']) || !empty($boards_can['remove_own']))
  672. $possibleActions[] = 'remove';
  673. if (!empty($boards_can['lock_any']) || !empty($boards_can['lock_own']))
  674. $possibleActions[] = 'lock';
  675. if (!empty($boards_can['merge_any']))
  676. $possibleActions[] = 'merge';
  677. if (!empty($boards_can['approve_posts']))
  678. $possibleActions[] = 'approve';
  679. // Two methods: $_REQUEST['actions'] (id_topic => action), and $_REQUEST['topics'] and $_REQUEST['qaction'].
  680. // (if action is 'move', $_REQUEST['move_to'] or $_REQUEST['move_tos'][$topic] is used.)
  681. if (!empty($_REQUEST['topics']))
  682. {
  683. // If the action isn't valid, just quit now.
  684. if (empty($_REQUEST['qaction']) || !in_array($_REQUEST['qaction'], $possibleActions))
  685. redirectexit($redirect_url);
  686. // Merge requires all topics as one parameter and can be done at once.
  687. if ($_REQUEST['qaction'] == 'merge')
  688. {
  689. // Merge requires at least two topics.
  690. if (empty($_REQUEST['topics']) || count($_REQUEST['topics']) < 2)
  691. redirectexit($redirect_url);
  692. require_once($sourcedir . '/SplitTopics.php');
  693. return MergeExecute($_REQUEST['topics']);
  694. }
  695. // Just convert to the other method, to make it easier.
  696. foreach ($_REQUEST['topics'] as $topic)
  697. $_REQUEST['actions'][(int) $topic] = $_REQUEST['qaction'];
  698. }
  699. // Weird... how'd you get here?
  700. if (empty($_REQUEST['actions']))
  701. redirectexit($redirect_url);
  702. // Validate each action.
  703. $temp = array();
  704. foreach ($_REQUEST['actions'] as $topic => $action)
  705. {
  706. if (in_array($action, $possibleActions))
  707. $temp[(int) $topic] = $action;
  708. }
  709. $_REQUEST['actions'] = $temp;
  710. if (!empty($_REQUEST['actions']))
  711. {
  712. // Find all topics...
  713. $request = $smcFunc['db_query']('', '
  714. SELECT id_topic, id_member_started, id_board, locked, approved, unapproved_posts
  715. FROM {db_prefix}topics
  716. WHERE id_topic IN ({array_int:action_topic_ids})
  717. LIMIT ' . count($_REQUEST['actions']),
  718. array(
  719. 'action_topic_ids' => array_keys($_REQUEST['actions']),
  720. )
  721. );
  722. while ($row = $smcFunc['db_fetch_assoc']($request))
  723. {
  724. if (!empty($board))
  725. {
  726. if ($row['id_board'] != $board || ($modSettings['postmod_active'] && !$row['approved'] && !allowedTo('approve_posts')))
  727. unset($_REQUEST['actions'][$row['id_topic']]);
  728. }
  729. else
  730. {
  731. // Don't allow them to act on unapproved posts they can't see...
  732. if ($modSettings['postmod_active'] && !$row['approved'] && !in_array(0, $boards_can['approve_posts']) && !in_array($row['id_board'], $boards_can['approve_posts']))
  733. unset($_REQUEST['actions'][$row['id_topic']]);
  734. // Goodness, this is fun. We need to validate the action.
  735. elseif ($_REQUEST['actions'][$row['id_topic']] == 'sticky' && !in_array(0, $boards_can['make_sticky']) && !in_array($row['id_board'], $boards_can['make_sticky']))
  736. unset($_REQUEST['actions'][$row['id_topic']]);
  737. 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']))))
  738. unset($_REQUEST['actions'][$row['id_topic']]);
  739. 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']))))
  740. unset($_REQUEST['actions'][$row['id_topic']]);
  741. // @todo $locked is not set, what are you trying to do? (taking the change it is supposed to be $row['locked'])
  742. 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']))))
  743. unset($_REQUEST['actions'][$row['id_topic']]);
  744. // If the topic is approved then you need permission to approve the posts within.
  745. 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']))))
  746. unset($_REQUEST['actions'][$row['id_topic']]);
  747. }
  748. }
  749. $smcFunc['db_free_result']($request);
  750. }
  751. $stickyCache = array();
  752. $moveCache = array(0 => array(), 1 => array());
  753. $removeCache = array();
  754. $lockCache = array();
  755. $markCache = array();
  756. $approveCache = array();
  757. // Separate the actions.
  758. foreach ($_REQUEST['actions'] as $topic => $action)
  759. {
  760. $topic = (int) $topic;
  761. if ($action == 'markread')
  762. $markCache[] = $topic;
  763. elseif ($action == 'sticky')
  764. $stickyCache[] = $topic;
  765. elseif ($action == 'move')
  766. {
  767. require_once($sourcedir . '/MoveTopic.php');
  768. moveTopicConcurrence();
  769. // $moveCache[0] is the topic, $moveCache[1] is the board to move to.
  770. $moveCache[1][$topic] = (int) (isset($_REQUEST['move_tos'][$topic]) ? $_REQUEST['move_tos'][$topic] : $_REQUEST['move_to']);
  771. if (empty($moveCache[1][$topic]))
  772. continue;
  773. $moveCache[0][] = $topic;
  774. }
  775. elseif ($action == 'remove')
  776. $removeCache[] = $topic;
  777. elseif ($action == 'lock')
  778. $lockCache[] = $topic;
  779. elseif ($action == 'approve')
  780. $approveCache[] = $topic;
  781. }
  782. if (empty($board))
  783. $affectedBoards = array();
  784. else
  785. $affectedBoards = array($board => array(0, 0));
  786. // Do all the stickies...
  787. if (!empty($stickyCache))
  788. {
  789. $smcFunc['db_query']('', '
  790. UPDATE {db_prefix}topics
  791. SET is_sticky = CASE WHEN is_sticky = {int:is_sticky} THEN 0 ELSE 1 END
  792. WHERE id_topic IN ({array_int:sticky_topic_ids})',
  793. array(
  794. 'sticky_topic_ids' => $stickyCache,
  795. 'is_sticky' => 1,
  796. )
  797. );
  798. // Get the board IDs and Sticky status
  799. $request = $smcFunc['db_query']('', '
  800. SELECT id_topic, id_board, is_sticky
  801. FROM {db_prefix}topics
  802. WHERE id_topic IN ({array_int:sticky_topic_ids})
  803. LIMIT ' . count($stickyCache),
  804. array(
  805. 'sticky_topic_ids' => $stickyCache,
  806. )
  807. );
  808. $stickyCacheBoards = array();
  809. $stickyCacheStatus = array();
  810. while ($row = $smcFunc['db_fetch_assoc']($request))
  811. {
  812. $stickyCacheBoards[$row['id_topic']] = $row['id_board'];
  813. $stickyCacheStatus[$row['id_topic']] = empty($row['is_sticky']);
  814. }
  815. $smcFunc['db_free_result']($request);
  816. }
  817. // Move sucka! (this is, by the by, probably the most complicated part....)
  818. if (!empty($moveCache[0]))
  819. {
  820. // I know - I just KNOW you're trying to beat the system. Too bad for you... we CHECK :P.
  821. $request = $smcFunc['db_query']('', '
  822. SELECT t.id_topic, t.id_board, b.count_posts
  823. FROM {db_prefix}topics AS t
  824. LEFT JOIN {db_prefix}boards AS b ON (t.id_board = b.id_board)
  825. WHERE t.id_topic IN ({array_int:move_topic_ids})' . (!empty($board) && !allowedTo('move_any') ? '
  826. AND t.id_member_started = {int:current_member}' : '') . '
  827. LIMIT ' . count($moveCache[0]),
  828. array(
  829. 'current_member' => $user_info['id'],
  830. 'move_topic_ids' => $moveCache[0],
  831. )
  832. );
  833. $moveTos = array();
  834. $moveCache2 = array();
  835. $countPosts = array();
  836. while ($row = $smcFunc['db_fetch_assoc']($request))
  837. {
  838. $to = $moveCache[1][$row['id_topic']];
  839. if (empty($to))
  840. continue;
  841. // Does this topic's board count the posts or not?
  842. $countPosts[$row['id_topic']] = empty($row['count_posts']);
  843. if (!isset($moveTos[$to]))
  844. $moveTos[$to] = array();
  845. $moveTos[$to][] = $row['id_topic'];
  846. // For reporting...
  847. $moveCache2[] = array($row['id_topic'], $row['id_board'], $to);
  848. }
  849. $smcFunc['db_free_result']($request);
  850. $moveCache = $moveCache2;
  851. require_once($sourcedir . '/MoveTopic.php');
  852. // Do the actual moves...
  853. foreach ($moveTos as $to => $topics)
  854. moveTopics($topics, $to);
  855. // Does the post counts need to be updated?
  856. if (!empty($moveTos))
  857. {
  858. $topicRecounts = array();
  859. $request = $smcFunc['db_query']('', '
  860. SELECT id_board, count_posts
  861. FROM {db_prefix}boards
  862. WHERE id_board IN ({array_int:move_boards})',
  863. array(
  864. 'move_boards' => array_keys($moveTos),
  865. )
  866. );
  867. while ($row = $smcFunc['db_fetch_assoc']($request))
  868. {
  869. $cp = empty($row['count_posts']);
  870. // Go through all the topics that are being moved to this board.
  871. foreach ($moveTos[$row['id_board']] as $topic)
  872. {
  873. // If both boards have the same value for post counting then no adjustment needs to be made.
  874. if ($countPosts[$topic] != $cp)
  875. {
  876. // If the board being moved to does count the posts then the other one doesn't so add to their post count.
  877. $topicRecounts[$topic] = $cp ? '+' : '-';
  878. }
  879. }
  880. }
  881. $smcFunc['db_free_result']($request);
  882. if (!empty($topicRecounts))
  883. {
  884. $members = array();
  885. // Get all the members who have posted in the moved topics.
  886. $request = $smcFunc['db_query']('', '
  887. SELECT id_member, id_topic
  888. FROM {db_prefix}messages
  889. WHERE id_topic IN ({array_int:moved_topic_ids})',
  890. array(
  891. 'moved_topic_ids' => array_keys($topicRecounts),
  892. )
  893. );
  894. while ($row = $smcFunc['db_fetch_assoc']($request))
  895. {
  896. if (!isset($members[$row['id_member']]))
  897. $members[$row['id_member']] = 0;
  898. if ($topicRecounts[$row['id_topic']] === '+')
  899. $members[$row['id_member']] += 1;
  900. else
  901. $members[$row['id_member']] -= 1;
  902. }
  903. $smcFunc['db_free_result']($request);
  904. // And now update them member's post counts
  905. foreach ($members as $id_member => $post_adj)
  906. updateMemberData($id_member, array('posts' => 'posts + ' . $post_adj));
  907. }
  908. }
  909. }
  910. // Now delete the topics...
  911. if (!empty($removeCache))
  912. {
  913. // They can only delete their own topics. (we wouldn't be here if they couldn't do that..)
  914. $result = $smcFunc['db_query']('', '
  915. SELECT id_topic, id_board
  916. FROM {db_prefix}topics
  917. WHERE id_topic IN ({array_int:removed_topic_ids})' . (!empty($board) && !allowedTo('remove_any') ? '
  918. AND id_member_started = {int:current_member}' : '') . '
  919. LIMIT ' . count($removeCache),
  920. array(
  921. 'current_member' => $user_info['id'],
  922. 'removed_topic_ids' => $removeCache,
  923. )
  924. );
  925. $removeCache = array();
  926. $removeCacheBoards = array();
  927. while ($row = $smcFunc['db_fetch_assoc']($result))
  928. {
  929. $removeCache[] = $row['id_topic'];
  930. $removeCacheBoards[$row['id_topic']] = $row['id_board'];
  931. }
  932. $smcFunc['db_free_result']($result);
  933. // Maybe *none* were their own topics.
  934. if (!empty($removeCache))
  935. {
  936. // Gotta send the notifications *first*!
  937. foreach ($removeCache as $topic)
  938. {
  939. // Only log the topic ID if it's not in the recycle board.
  940. logAction('remove', array((empty($modSettings['recycle_enable']) || $modSettings['recycle_board'] != $removeCacheBoards[$topic] ? 'topic' : 'old_topic_id') => $topic, 'board' => $removeCacheBoards[$topic]));
  941. sendNotifications($topic, 'remove');
  942. }
  943. require_once($sourcedir . '/RemoveTopic.php');
  944. removeTopics($removeCache);
  945. }
  946. }
  947. // Approve the topics...
  948. if (!empty($approveCache))
  949. {
  950. // We need unapproved topic ids and their authors!
  951. $request = $smcFunc['db_query']('', '
  952. SELECT id_topic, id_member_started
  953. FROM {db_prefix}topics
  954. WHERE id_topic IN ({array_int:approve_topic_ids})
  955. AND approved = {int:not_approved}
  956. LIMIT ' . count($approveCache),
  957. array(
  958. 'approve_topic_ids' => $approveCache,
  959. 'not_approved' => 0,
  960. )
  961. );
  962. $approveCache = array();
  963. $approveCacheMembers = array();
  964. while ($row = $smcFunc['db_fetch_assoc']($request))
  965. {
  966. $approveCache[] = $row['id_topic'];
  967. $approveCacheMembers[$row['id_topic']] = $row['id_member_started'];
  968. }
  969. $smcFunc['db_free_result']($request);
  970. // Any topics to approve?
  971. if (!empty($approveCache))
  972. {
  973. // Handle the approval part...
  974. approveTopics($approveCache);
  975. // Time for some logging!
  976. foreach ($approveCache as $topic)
  977. logAction('approve_topic', array('topic' => $topic, 'member' => $approveCacheMembers[$topic]));
  978. }
  979. }
  980. // And (almost) lastly, lock the topics...
  981. if (!empty($lockCache))
  982. {
  983. $lockStatus = array();
  984. // Gotta make sure they CAN lock/unlock these topics...
  985. if (!empty($board) && !allowedTo('lock_any'))
  986. {
  987. // Make sure they started the topic AND it isn't already locked by someone with higher priv's.
  988. $result = $smcFunc['db_query']('', '
  989. SELECT id_topic, locked, id_board
  990. FROM {db_prefix}topics
  991. WHERE id_topic IN ({array_int:locked_topic_ids})
  992. AND id_member_started = {int:current_member}
  993. AND locked IN (2, 0)
  994. LIMIT ' . count($lockCache),
  995. array(
  996. 'current_member' => $user_info['id'],
  997. 'locked_topic_ids' => $lockCache,
  998. )
  999. );
  1000. $lockCache = array();
  1001. $lockCacheBoards = array();
  1002. while ($row = $smcFunc['db_fetch_assoc']($result))
  1003. {
  1004. $lockCache[] = $row['id_topic'];
  1005. $lockCacheBoards[$row['id_topic']] = $row['id_board'];
  1006. $lockStatus[$row['id_topic']] = empty($row['locked']);
  1007. }
  1008. $smcFunc['db_free_result']($result);
  1009. }
  1010. else
  1011. {
  1012. $result = $smcFunc['db_query']('', '
  1013. SELECT id_topic, locked, id_board
  1014. FROM {db_prefix}topics
  1015. WHERE id_topic IN ({array_int:locked_topic_ids})
  1016. LIMIT ' . count($lockCache),
  1017. array(
  1018. 'locked_topic_ids' => $lockCache,
  1019. )
  1020. );
  1021. $lockCacheBoards = array();
  1022. while ($row = $smcFunc['db_fetch_assoc']($result))
  1023. {
  1024. $lockStatus[$row['id_topic']] = empty($row['locked']);
  1025. $lockCacheBoards[$row['id_topic']] = $row['id_board'];
  1026. }
  1027. $smcFunc['db_free_result']($result);
  1028. }
  1029. // It could just be that *none* were their own topics...
  1030. if (!empty($lockCache))
  1031. {
  1032. // Alternate the locked value.
  1033. $smcFunc['db_query']('', '
  1034. UPDATE {db_prefix}topics
  1035. SET locked = CASE WHEN locked = {int:is_locked} THEN ' . (allowedTo('lock_any') ? '1' : '2') . ' ELSE 0 END
  1036. WHERE id_topic IN ({array_int:locked_topic_ids})',
  1037. array(
  1038. 'locked_topic_ids' => $lockCache,
  1039. 'is_locked' => 0,
  1040. )
  1041. );
  1042. }
  1043. }
  1044. if (!empty($markCache))
  1045. {
  1046. $smcFunc['db_query']('', '
  1047. SELECT id_topic, unwatched
  1048. FROM {db_prefix}log_topics
  1049. WHERE id_topic IN ({array_int:selected_topics})
  1050. AND id_member = {int:current_user}',
  1051. array(
  1052. 'selected_topics' => $markCache,
  1053. 'current_user' => $user_info['id'],
  1054. )
  1055. );
  1056. $logged_topics = array();
  1057. while ($row = $smcFunc['db_fetch_assoc']($request))
  1058. $logged_topics[$row['id_topic']] = $row['unwatched'];
  1059. $smcFunc['db_free_result']($request);
  1060. $markArray = array();
  1061. foreach ($markCache as $topic)
  1062. $markArray[] = array($modSettings['maxMsgID'], $user_info['id'], $topic, (isset($logged_topics[$topic]) ? $logged_topics[$topic] : 0));
  1063. $smcFunc['db_insert']('replace',
  1064. '{db_prefix}log_topics',
  1065. array('id_msg' => 'int', 'id_member' => 'int', 'id_topic' => 'int', 'unwatched' => 'int'),
  1066. $markArray,
  1067. array('id_member', 'id_topic')
  1068. );
  1069. }
  1070. foreach ($moveCache as $topic)
  1071. {
  1072. // Didn't actually move anything!
  1073. if (!isset($topic[0]))
  1074. break;
  1075. logAction('move', array('topic' => $topic[0], 'board_from' => $topic[1], 'board_to' => $topic[2]));
  1076. sendNotifications($topic[0], 'move');
  1077. }
  1078. foreach ($lockCache as $topic)
  1079. {
  1080. logAction($lockStatus[$topic] ? 'lock' : 'unlock', array('topic' => $topic, 'board' => $lockCacheBoards[$topic]));
  1081. sendNotifications($topic, $lockStatus[$topic] ? 'lock' : 'unlock');
  1082. }
  1083. foreach ($stickyCache as $topic)
  1084. {
  1085. logAction($stickyCacheStatus[$topic] ? 'unsticky' : 'sticky', array('topic' => $topic, 'board' => $stickyCacheBoards[$topic]));
  1086. sendNotifications($topic, 'sticky');
  1087. }
  1088. updateStats('topic');
  1089. updateStats('message');
  1090. updateSettings(array(
  1091. 'calendar_updated' => time(),
  1092. ));
  1093. if (!empty($affectedBoards))
  1094. updateLastMessages(array_keys($affectedBoards));
  1095. redirectexit($redirect_url);
  1096. }
  1097. ?>