MessageIndex.php 51 KB

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