MessageIndex.php 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  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 2011 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'] = ' (' . (count($context['link_moderators']) == 1 ? $txt['moderator'] : $txt['moderators']) . ': ' . implode(', ', $context['link_moderators']) . ')';
  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. $context['view_members'][$row['log_time'] . $row['member_name']] = array(
  222. 'id' => $row['id_member'],
  223. 'username' => $row['member_name'],
  224. 'name' => $row['real_name'],
  225. 'group' => $row['id_group'],
  226. 'href' => $scripturl . '?action=profile;u=' . $row['id_member'],
  227. 'link' => $link,
  228. 'is_buddy' => $is_buddy,
  229. 'hidden' => empty($row['show_online']),
  230. );
  231. if (empty($row['show_online']))
  232. $context['view_num_hidden']++;
  233. }
  234. $context['view_num_guests'] = $smcFunc['db_num_rows']($request) - count($context['view_members']);
  235. $smcFunc['db_free_result']($request);
  236. // Put them in "last clicked" order.
  237. krsort($context['view_members_list']);
  238. krsort($context['view_members']);
  239. }
  240. // Default sort methods.
  241. $sort_methods = array(
  242. 'subject' => 'mf.subject',
  243. 'starter' => 'IFNULL(memf.real_name, mf.poster_name)',
  244. 'last_poster' => 'IFNULL(meml.real_name, ml.poster_name)',
  245. 'replies' => 't.num_replies',
  246. 'views' => 't.num_views',
  247. 'first_post' => 't.id_topic',
  248. 'last_post' => 't.id_last_msg'
  249. );
  250. // They didn't pick one, default to by last post descending.
  251. if (!isset($_REQUEST['sort']) || !isset($sort_methods[$_REQUEST['sort']]))
  252. {
  253. $context['sort_by'] = 'last_post';
  254. $_REQUEST['sort'] = 'id_last_msg';
  255. $ascending = isset($_REQUEST['asc']);
  256. }
  257. // Otherwise default to ascending.
  258. else
  259. {
  260. $context['sort_by'] = $_REQUEST['sort'];
  261. $_REQUEST['sort'] = $sort_methods[$_REQUEST['sort']];
  262. $ascending = !isset($_REQUEST['desc']);
  263. }
  264. $context['sort_direction'] = $ascending ? 'up' : 'down';
  265. // Calculate the fastest way to get the topics.
  266. $start = (int) $_REQUEST['start'];
  267. if ($start > ($board_info['total_topics'] - 1) / 2)
  268. {
  269. $ascending = !$ascending;
  270. $fake_ascending = true;
  271. $maxindex = $board_info['total_topics'] < $start + $maxindex + 1 ? $board_info['total_topics'] - $start : $maxindex;
  272. $start = $board_info['total_topics'] < $start + $maxindex + 1 ? 0 : $board_info['total_topics'] - $start - $maxindex;
  273. }
  274. else
  275. $fake_ascending = false;
  276. // Setup the default topic icons...
  277. $stable_icons = array('xx', 'thumbup', 'thumbdown', 'exclamation', 'question', 'lamp', 'smiley', 'angry', 'cheesy', 'grin', 'sad', 'wink', 'poll', 'moved', 'recycled', 'wireless', 'clip');
  278. $context['icon_sources'] = array();
  279. foreach ($stable_icons as $icon)
  280. $context['icon_sources'][$icon] = 'images_url';
  281. $topic_ids = array();
  282. $context['topics'] = array();
  283. // Sequential pages are often not optimized, so we add an additional query.
  284. $pre_query = $start > 0;
  285. if ($pre_query && $maxindex > 0)
  286. {
  287. $request = $smcFunc['db_query']('', '
  288. SELECT t.id_topic
  289. FROM {db_prefix}topics AS t' . ($context['sort_by'] === 'last_poster' ? '
  290. INNER JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)' : (in_array($context['sort_by'], array('starter', 'subject')) ? '
  291. INNER JOIN {db_prefix}messages AS mf ON (mf.id_msg = t.id_first_msg)' : '')) . ($context['sort_by'] === 'starter' ? '
  292. LEFT JOIN {db_prefix}members AS memf ON (memf.id_member = mf.id_member)' : '') . ($context['sort_by'] === 'last_poster' ? '
  293. LEFT JOIN {db_prefix}members AS meml ON (meml.id_member = ml.id_member)' : '') . '
  294. WHERE t.id_board = {int:current_board}' . (!$modSettings['postmod_active'] || $context['can_approve_posts'] ? '' : '
  295. AND (t.approved = {int:is_approved}' . ($user_info['is_guest'] ? '' : ' OR t.id_member_started = {int:current_member}') . ')') . '
  296. ORDER BY ' . (!empty($modSettings['enableStickyTopics']) ? 'is_sticky' . ($fake_ascending ? '' : ' DESC') . ', ' : '') . $_REQUEST['sort'] . ($ascending ? '' : ' DESC') . '
  297. LIMIT {int:start}, {int:maxindex}',
  298. array(
  299. 'current_board' => $board,
  300. 'current_member' => $user_info['id'],
  301. 'is_approved' => 1,
  302. 'id_member_guest' => 0,
  303. 'start' => $start,
  304. 'maxindex' => $maxindex,
  305. )
  306. );
  307. $topic_ids = array();
  308. while ($row = $smcFunc['db_fetch_assoc']($request))
  309. $topic_ids[] = $row['id_topic'];
  310. }
  311. // Grab the appropriate topic information...
  312. if (!$pre_query || !empty($topic_ids))
  313. {
  314. // For search engine effectiveness we'll link guests differently.
  315. $context['pageindex_multiplier'] = empty($modSettings['disableCustomPerPage']) && !empty($options['messages_per_page']) && !WIRELESS ? $options['messages_per_page'] : $modSettings['defaultMaxMessages'];
  316. $result = $smcFunc['db_query']('substring', '
  317. SELECT
  318. t.id_topic, t.num_replies, t.locked, t.num_views, t.is_sticky, t.id_poll, t.id_previous_board,
  319. ' . ($user_info['is_guest'] ? '0' : 'IFNULL(lt.id_msg, IFNULL(lmr.id_msg, -1)) + 1') . ' AS new_from,
  320. t.id_last_msg, t.approved, t.unapproved_posts, t.id_redirect_topic, ml.poster_time AS last_poster_time,
  321. ml.id_msg_modified, ml.subject AS last_subject, ml.icon AS last_icon,
  322. ml.poster_name AS last_member_name, ml.id_member AS last_id_member,
  323. IFNULL(meml.real_name, ml.poster_name) AS last_display_name, t.id_first_msg,
  324. mf.poster_time AS first_poster_time, mf.subject AS first_subject, mf.icon AS first_icon,
  325. mf.poster_name AS first_member_name, mf.id_member AS first_id_member,
  326. IFNULL(memf.real_name, mf.poster_name) AS first_display_name, ' . (!empty($modSettings['preview_characters']) ? '
  327. SUBSTRING(ml.body, 1, ' . ($modSettings['preview_characters'] + 256) . ') AS last_body,
  328. SUBSTRING(mf.body, 1, ' . ($modSettings['preview_characters'] + 256) . ') AS first_body,' : '') . 'ml.smileys_enabled AS last_smileys, mf.smileys_enabled AS first_smileys
  329. FROM {db_prefix}topics AS t
  330. INNER JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)
  331. INNER JOIN {db_prefix}messages AS mf ON (mf.id_msg = t.id_first_msg)
  332. LEFT JOIN {db_prefix}members AS meml ON (meml.id_member = ml.id_member)
  333. LEFT JOIN {db_prefix}members AS memf ON (memf.id_member = mf.id_member)' . ($user_info['is_guest'] ? '' : '
  334. LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})
  335. LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = {int:current_board} AND lmr.id_member = {int:current_member})'). '
  336. WHERE ' . ($pre_query ? 't.id_topic IN ({array_int:topic_list})' : 't.id_board = {int:current_board}') . (!$modSettings['postmod_active'] || $context['can_approve_posts'] ? '' : '
  337. AND (t.approved = {int:is_approved}' . ($user_info['is_guest'] ? '' : ' OR t.id_member_started = {int:current_member}') . ')') . '
  338. 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')) . '
  339. LIMIT ' . ($pre_query ? '' : '{int:start}, ') . '{int:maxindex}',
  340. array(
  341. 'current_board' => $board,
  342. 'current_member' => $user_info['id'],
  343. 'topic_list' => $topic_ids,
  344. 'is_approved' => 1,
  345. 'find_set_topics' => implode(',', $topic_ids),
  346. 'start' => $start,
  347. 'maxindex' => $maxindex,
  348. )
  349. );
  350. // Begin 'printing' the message index for current board.
  351. while ($row = $smcFunc['db_fetch_assoc']($result))
  352. {
  353. if ($row['id_poll'] > 0 && $modSettings['pollMode'] == '0')
  354. continue;
  355. if (!$pre_query)
  356. $topic_ids[] = $row['id_topic'];
  357. // Does the theme support message previews?
  358. if (!empty($settings['message_index_preview']) && !empty($modSettings['preview_characters']))
  359. {
  360. // Limit them to $modSettings['preview_characters'] characters
  361. $row['first_body'] = strip_tags(strtr(parse_bbc($row['first_body'], $row['first_smileys'], $row['id_first_msg']), array('<br />' => '&#10;')));
  362. if ($smcFunc['strlen']($row['first_body']) > $modSettings['preview_characters'])
  363. $row['first_body'] = $smcFunc['substr']($row['first_body'], 0, $modSettings['preview_characters']) . '...';
  364. $row['last_body'] = strip_tags(strtr(parse_bbc($row['last_body'], $row['last_smileys'], $row['id_last_msg']), array('<br />' => '&#10;')));
  365. if ($smcFunc['strlen']($row['last_body']) > $modSettings['preview_characters'])
  366. $row['last_body'] = $smcFunc['substr']($row['last_body'], 0, $modSettings['preview_characters']) . '...';
  367. // Censor the subject and message preview.
  368. censorText($row['first_subject']);
  369. censorText($row['first_body']);
  370. // Don't censor them twice!
  371. if ($row['id_first_msg'] == $row['id_last_msg'])
  372. {
  373. $row['last_subject'] = $row['first_subject'];
  374. $row['last_body'] = $row['first_body'];
  375. }
  376. else
  377. {
  378. censorText($row['last_subject']);
  379. censorText($row['last_body']);
  380. }
  381. }
  382. else
  383. {
  384. $row['first_body'] = '';
  385. $row['last_body'] = '';
  386. censorText($row['first_subject']);
  387. if ($row['id_first_msg'] == $row['id_last_msg'])
  388. $row['last_subject'] = $row['first_subject'];
  389. else
  390. censorText($row['last_subject']);
  391. }
  392. // Decide how many pages the topic should have.
  393. if ($row['num_replies'] + 1 > $context['messages_per_page'])
  394. {
  395. $pages = '&#171; ';
  396. // We can't pass start by reference.
  397. $start = -1;
  398. $pages .= constructPageIndex($scripturl . '?topic=' . $row['id_topic'] . '.%1$d', $start, $row['num_replies'] + 1, $context['messages_per_page'], true, false);
  399. // If we can use all, show all.
  400. if (!empty($modSettings['enableAllMessages']) && $row['num_replies'] + 1 < $modSettings['enableAllMessages'])
  401. $pages .= ' &nbsp;<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0;all">' . $txt['all'] . '</a>';
  402. $pages .= ' &#187;';
  403. }
  404. else
  405. $pages = '';
  406. // We need to check the topic icons exist...
  407. if (!empty($modSettings['messageIconChecks_enable']))
  408. {
  409. if (!isset($context['icon_sources'][$row['first_icon']]))
  410. $context['icon_sources'][$row['first_icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['first_icon'] . '.png') ? 'images_url' : 'default_images_url';
  411. if (!isset($context['icon_sources'][$row['last_icon']]))
  412. $context['icon_sources'][$row['last_icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['last_icon'] . '.png') ? 'images_url' : 'default_images_url';
  413. }
  414. else
  415. {
  416. if (!isset($context['icon_sources'][$row['first_icon']]))
  417. $context['icon_sources'][$row['first_icon']] = 'images_url';
  418. if (!isset($context['icon_sources'][$row['last_icon']]))
  419. $context['icon_sources'][$row['last_icon']] = 'images_url';
  420. }
  421. // 'Print' the topic info.
  422. $context['topics'][$row['id_topic']] = array(
  423. 'id' => $row['id_topic'],
  424. 'first_post' => array(
  425. 'id' => $row['id_first_msg'],
  426. 'member' => array(
  427. 'username' => $row['first_member_name'],
  428. 'name' => $row['first_display_name'],
  429. 'id' => $row['first_id_member'],
  430. 'href' => !empty($row['first_id_member']) ? $scripturl . '?action=profile;u=' . $row['first_id_member'] : '',
  431. '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']
  432. ),
  433. 'time' => timeformat($row['first_poster_time']),
  434. 'timestamp' => forum_time(true, $row['first_poster_time']),
  435. 'subject' => $row['first_subject'],
  436. 'preview' => $row['first_body'],
  437. 'icon' => $row['first_icon'],
  438. 'icon_url' => $settings[$context['icon_sources'][$row['first_icon']]] . '/post/' . $row['first_icon'] . '.png',
  439. 'href' => $scripturl . '?topic=' . (empty($row['id_redirect_topic']) ? $row['id_topic'] : $row['id_redirect_topic']) . '.0',
  440. 'link' => '<a href="' . $scripturl . '?topic=' . (empty($row['id_redirect_topic']) ? $row['id_topic'] : $row['id_redirect_topic']). '.0">' . $row['first_subject'] . '</a>'
  441. ),
  442. 'last_post' => array(
  443. 'id' => $row['id_last_msg'],
  444. 'member' => array(
  445. 'username' => $row['last_member_name'],
  446. 'name' => $row['last_display_name'],
  447. 'id' => $row['last_id_member'],
  448. 'href' => !empty($row['last_id_member']) ? $scripturl . '?action=profile;u=' . $row['last_id_member'] : '',
  449. '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']
  450. ),
  451. 'time' => timeformat($row['last_poster_time']),
  452. 'timestamp' => forum_time(true, $row['last_poster_time']),
  453. 'subject' => $row['last_subject'],
  454. 'preview' => $row['last_body'],
  455. 'icon' => $row['last_icon'],
  456. 'icon_url' => $settings[$context['icon_sources'][$row['last_icon']]] . '/post/' . $row['last_icon'] . '.png',
  457. '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')),
  458. '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>'
  459. ),
  460. 'is_sticky' => !empty($modSettings['enableStickyTopics']) && !empty($row['is_sticky']),
  461. 'is_locked' => !empty($row['locked']),
  462. 'is_poll' => $modSettings['pollMode'] == '1' && $row['id_poll'] > 0,
  463. 'is_hot' => $row['num_replies'] >= $modSettings['hotTopicPosts'],
  464. 'is_very_hot' => $row['num_replies'] >= $modSettings['hotTopicVeryPosts'],
  465. 'is_posted_in' => false,
  466. 'icon' => $row['first_icon'],
  467. 'icon_url' => $settings[$context['icon_sources'][$row['first_icon']]] . '/post/' . $row['first_icon'] . '.png',
  468. 'subject' => $row['first_subject'],
  469. 'new' => $row['new_from'] <= $row['id_msg_modified'],
  470. 'new_from' => $row['new_from'],
  471. 'newtime' => $row['new_from'],
  472. 'new_href' => $scripturl . '?topic=' . (empty($row['id_redirect_topic']) ? $row['id_topic'] : $row['id_redirect_topic']) . '.msg' . $row['new_from'] . '#new',
  473. 'pages' => $pages,
  474. 'replies' => comma_format($row['num_replies']),
  475. 'views' => comma_format($row['num_views']),
  476. 'approved' => $row['approved'],
  477. 'unapproved_posts' => $row['unapproved_posts'],
  478. );
  479. determineTopicClass($context['topics'][$row['id_topic']]);
  480. }
  481. $smcFunc['db_free_result']($result);
  482. // Fix the sequence of topics if they were retrieved in the wrong order. (for speed reasons...)
  483. if ($fake_ascending)
  484. $context['topics'] = array_reverse($context['topics'], true);
  485. if (!empty($modSettings['enableParticipation']) && !$user_info['is_guest'] && !empty($topic_ids))
  486. {
  487. $result = $smcFunc['db_query']('', '
  488. SELECT id_topic
  489. FROM {db_prefix}messages
  490. WHERE id_topic IN ({array_int:topic_list})
  491. AND id_member = {int:current_member}
  492. GROUP BY id_topic
  493. LIMIT ' . count($topic_ids),
  494. array(
  495. 'current_member' => $user_info['id'],
  496. 'topic_list' => $topic_ids,
  497. )
  498. );
  499. while ($row = $smcFunc['db_fetch_assoc']($result))
  500. {
  501. $context['topics'][$row['id_topic']]['is_posted_in'] = true;
  502. $context['topics'][$row['id_topic']]['class'] = 'my_' . $context['topics'][$row['id_topic']]['class'];
  503. }
  504. $smcFunc['db_free_result']($result);
  505. }
  506. }
  507. $context['jump_to'] = array(
  508. 'label' => addslashes(un_htmlspecialchars($txt['jump_to'])),
  509. 'board_name' => htmlspecialchars(strtr(strip_tags($board_info['name']), array('&amp;' => '&'))),
  510. 'child_level' => $board_info['child_level'],
  511. );
  512. // Is Quick Moderation active/needed?
  513. if (!empty($options['display_quick_mod']) && !empty($context['topics']))
  514. {
  515. $context['can_lock'] = allowedTo('lock_any');
  516. $context['can_sticky'] = allowedTo('make_sticky') && !empty($modSettings['enableStickyTopics']);
  517. $context['can_move'] = allowedTo('move_any');
  518. $context['can_remove'] = allowedTo('remove_any');
  519. $context['can_merge'] = allowedTo('merge_any');
  520. // Ignore approving own topics as it's unlikely to come up...
  521. $context['can_approve'] = $modSettings['postmod_active'] && allowedTo('approve_posts') && !empty($board_info['unapproved_topics']);
  522. // Can we restore topics?
  523. $context['can_restore'] = allowedTo('move_any') && !empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] == $board;
  524. // Set permissions for all the topics.
  525. foreach ($context['topics'] as $t => $topic)
  526. {
  527. $started = $topic['first_post']['member']['id'] == $user_info['id'];
  528. $context['topics'][$t]['quick_mod'] = array(
  529. 'lock' => allowedTo('lock_any') || ($started && allowedTo('lock_own')),
  530. 'sticky' => allowedTo('make_sticky') && !empty($modSettings['enableStickyTopics']),
  531. 'move' => allowedTo('move_any') || ($started && allowedTo('move_own')),
  532. 'modify' => allowedTo('modify_any') || ($started && allowedTo('modify_own')),
  533. 'remove' => allowedTo('remove_any') || ($started && allowedTo('remove_own')),
  534. 'approve' => $context['can_approve'] && $topic['unapproved_posts']
  535. );
  536. $context['can_lock'] |= ($started && allowedTo('lock_own'));
  537. $context['can_move'] |= ($started && allowedTo('move_own'));
  538. $context['can_remove'] |= ($started && allowedTo('remove_own'));
  539. }
  540. // Find the boards/cateogories they can move their topic to.
  541. if ($options['display_quick_mod'] == 1 && $context['can_move'] && !empty($context['topics']))
  542. {
  543. require_once($sourcedir . '/Subs-MessageIndex.php');
  544. $boardListOptions = array(
  545. 'excluded_boards' => array($board),
  546. 'not_redirection' => true,
  547. 'use_permissions' => true,
  548. 'selected_board' => empty($_SESSION['move_to_topic']) ? null : $_SESSION['move_to_topic'],
  549. );
  550. $context['move_to_boards'] = getBoardList($boardListOptions);
  551. // Make the boards safe for display.
  552. foreach ($context['move_to_boards'] as $id_cat => $cat)
  553. {
  554. $context['move_to_boards'][$id_cat]['name'] = strip_tags($cat['name']);
  555. foreach ($cat['boards'] as $id_board => $aboard)
  556. $context['move_to_boards'][$id_cat]['boards'][$id_board]['name'] = strip_tags($aboard['name']);
  557. }
  558. // With no other boards to see, it's useless to move.
  559. if (empty($context['move_to_boards']))
  560. $context['can_move'] = false;
  561. }
  562. // Can we use quick moderation checkboxes?
  563. if ($options['display_quick_mod'] == 1)
  564. $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'];
  565. // Or the icons?
  566. else
  567. $context['can_quick_mod'] = $context['can_remove'] || $context['can_lock'] || $context['can_sticky'] || $context['can_move'];
  568. }
  569. // If there are children, but no topics and no ability to post topics...
  570. $context['no_topic_listing'] = !empty($context['boards']) && empty($context['topics']) && !$context['can_post_new'];
  571. // Build the message index button array.
  572. $context['normal_buttons'] = array(
  573. '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),
  574. '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'),
  575. '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']),
  576. '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']),
  577. );
  578. // Allow adding new buttons easily.
  579. call_integration_hook('integrate_messageindex_buttons');
  580. }
  581. /**
  582. * Allows for moderation from the message index.
  583. * @todo refactor this...
  584. */
  585. function QuickModeration()
  586. {
  587. global $sourcedir, $board, $user_info, $modSettings, $smcFunc, $context;
  588. // Check the session = get or post.
  589. checkSession('request');
  590. // Lets go straight to the restore area.
  591. if (isset($_REQUEST['qaction']) && $_REQUEST['qaction'] == 'restore' && !empty($_REQUEST['topics']))
  592. redirectexit('action=restoretopic;topics=' . implode(',', $_REQUEST['topics']) . ';' . $context['session_var'] . '=' . $context['session_id']);
  593. if (isset($_SESSION['topicseen_cache']))
  594. $_SESSION['topicseen_cache'] = array();
  595. // This is going to be needed to send off the notifications and for updateLastMessages().
  596. require_once($sourcedir . '/Subs-Post.php');
  597. // Remember the last board they moved things to.
  598. if (isset($_REQUEST['move_to']))
  599. $_SESSION['move_to_topic'] = $_REQUEST['move_to'];
  600. // Only a few possible actions.
  601. $possibleActions = array();
  602. if (!empty($board))
  603. {
  604. $boards_can = array(
  605. 'make_sticky' => allowedTo('make_sticky') ? array($board) : array(),
  606. 'move_any' => allowedTo('move_any') ? array($board) : array(),
  607. 'move_own' => allowedTo('move_own') ? array($board) : array(),
  608. 'remove_any' => allowedTo('remove_any') ? array($board) : array(),
  609. 'remove_own' => allowedTo('remove_own') ? array($board) : array(),
  610. 'lock_any' => allowedTo('lock_any') ? array($board) : array(),
  611. 'lock_own' => allowedTo('lock_own') ? array($board) : array(),
  612. 'merge_any' => allowedTo('merge_any') ? array($board) : array(),
  613. 'approve_posts' => allowedTo('approve_posts') ? array($board) : array(),
  614. );
  615. $redirect_url = 'board=' . $board . '.' . $_REQUEST['start'];
  616. }
  617. else
  618. {
  619. /**
  620. * @todo Ugly. There's no getting around this, is there?
  621. * @todo Maybe just do this on the actions people want to use?
  622. */
  623. $boards_can = boardsAllowedTo(array('make_sticky', 'move_any', 'move_own', 'remove_any', 'remove_own', 'lock_any', 'lock_own', 'merge_any', 'approve_posts'));
  624. $redirect_url = isset($_POST['redirect_url']) ? $_POST['redirect_url'] : (isset($_SESSION['old_url']) ? $_SESSION['old_url'] : '');
  625. }
  626. if (!$user_info['is_guest'])
  627. $possibleActions[] = 'markread';
  628. if (!empty($boards_can['make_sticky']) && !empty($modSettings['enableStickyTopics']))
  629. $possibleActions[] = 'sticky';
  630. if (!empty($boards_can['move_any']) || !empty($boards_can['move_own']))
  631. $possibleActions[] = 'move';
  632. if (!empty($boards_can['remove_any']) || !empty($boards_can['remove_own']))
  633. $possibleActions[] = 'remove';
  634. if (!empty($boards_can['lock_any']) || !empty($boards_can['lock_own']))
  635. $possibleActions[] = 'lock';
  636. if (!empty($boards_can['merge_any']))
  637. $possibleActions[] = 'merge';
  638. if (!empty($boards_can['approve_posts']))
  639. $possibleActions[] = 'approve';
  640. // Two methods: $_REQUEST['actions'] (id_topic => action), and $_REQUEST['topics'] and $_REQUEST['qaction'].
  641. // (if action is 'move', $_REQUEST['move_to'] or $_REQUEST['move_tos'][$topic] is used.)
  642. if (!empty($_REQUEST['topics']))
  643. {
  644. // If the action isn't valid, just quit now.
  645. if (empty($_REQUEST['qaction']) || !in_array($_REQUEST['qaction'], $possibleActions))
  646. redirectexit($redirect_url);
  647. // Merge requires all topics as one parameter and can be done at once.
  648. if ($_REQUEST['qaction'] == 'merge')
  649. {
  650. // Merge requires at least two topics.
  651. if (empty($_REQUEST['topics']) || count($_REQUEST['topics']) < 2)
  652. redirectexit($redirect_url);
  653. require_once($sourcedir . '/SplitTopics.php');
  654. return MergeExecute($_REQUEST['topics']);
  655. }
  656. // Just convert to the other method, to make it easier.
  657. foreach ($_REQUEST['topics'] as $topic)
  658. $_REQUEST['actions'][(int) $topic] = $_REQUEST['qaction'];
  659. }
  660. // Weird... how'd you get here?
  661. if (empty($_REQUEST['actions']))
  662. redirectexit($redirect_url);
  663. // Validate each action.
  664. $temp = array();
  665. foreach ($_REQUEST['actions'] as $topic => $action)
  666. {
  667. if (in_array($action, $possibleActions))
  668. $temp[(int) $topic] = $action;
  669. }
  670. $_REQUEST['actions'] = $temp;
  671. if (!empty($_REQUEST['actions']))
  672. {
  673. // Find all topics...
  674. $request = $smcFunc['db_query']('', '
  675. SELECT id_topic, id_member_started, id_board, locked, approved, unapproved_posts
  676. FROM {db_prefix}topics
  677. WHERE id_topic IN ({array_int:action_topic_ids})
  678. LIMIT ' . count($_REQUEST['actions']),
  679. array(
  680. 'action_topic_ids' => array_keys($_REQUEST['actions']),
  681. )
  682. );
  683. while ($row = $smcFunc['db_fetch_assoc']($request))
  684. {
  685. if (!empty($board))
  686. {
  687. if ($row['id_board'] != $board || ($modSettings['postmod_active'] && !$row['approved'] && !allowedTo('approve_posts')))
  688. unset($_REQUEST['actions'][$row['id_topic']]);
  689. }
  690. else
  691. {
  692. // Don't allow them to act on unapproved posts they can't see...
  693. if ($modSettings['postmod_active'] && !$row['approved'] && !in_array(0, $boards_can['approve_posts']) && !in_array($row['id_board'], $boards_can['approve_posts']))
  694. unset($_REQUEST['actions'][$row['id_topic']]);
  695. // Goodness, this is fun. We need to validate the action.
  696. elseif ($_REQUEST['actions'][$row['id_topic']] == 'sticky' && !in_array(0, $boards_can['make_sticky']) && !in_array($row['id_board'], $boards_can['make_sticky']))
  697. unset($_REQUEST['actions'][$row['id_topic']]);
  698. 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']))))
  699. unset($_REQUEST['actions'][$row['id_topic']]);
  700. 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']))))
  701. unset($_REQUEST['actions'][$row['id_topic']]);
  702. // @todo $locked is not set, what are you trying to do? (taking the change it is supposed to be $row['locked'])
  703. 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']))))
  704. unset($_REQUEST['actions'][$row['id_topic']]);
  705. // If the topic is approved then you need permission to approve the posts within.
  706. 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']))))
  707. unset($_REQUEST['actions'][$row['id_topic']]);
  708. }
  709. }
  710. $smcFunc['db_free_result']($request);
  711. }
  712. $stickyCache = array();
  713. $moveCache = array(0 => array(), 1 => array());
  714. $removeCache = array();
  715. $lockCache = array();
  716. $markCache = array();
  717. $approveCache = array();
  718. // Separate the actions.
  719. foreach ($_REQUEST['actions'] as $topic => $action)
  720. {
  721. $topic = (int) $topic;
  722. if ($action == 'markread')
  723. $markCache[] = $topic;
  724. elseif ($action == 'sticky')
  725. $stickyCache[] = $topic;
  726. elseif ($action == 'move')
  727. {
  728. require_once($sourcedir . '/MoveTopic.php');
  729. moveTopicConcurrence();
  730. // $moveCache[0] is the topic, $moveCache[1] is the board to move to.
  731. $moveCache[1][$topic] = (int) (isset($_REQUEST['move_tos'][$topic]) ? $_REQUEST['move_tos'][$topic] : $_REQUEST['move_to']);
  732. if (empty($moveCache[1][$topic]))
  733. continue;
  734. $moveCache[0][] = $topic;
  735. }
  736. elseif ($action == 'remove')
  737. $removeCache[] = $topic;
  738. elseif ($action == 'lock')
  739. $lockCache[] = $topic;
  740. elseif ($action == 'approve')
  741. $approveCache[] = $topic;
  742. }
  743. if (empty($board))
  744. $affectedBoards = array();
  745. else
  746. $affectedBoards = array($board => array(0, 0));
  747. // Do all the stickies...
  748. if (!empty($stickyCache))
  749. {
  750. $smcFunc['db_query']('', '
  751. UPDATE {db_prefix}topics
  752. SET is_sticky = CASE WHEN is_sticky = {int:is_sticky} THEN 0 ELSE 1 END
  753. WHERE id_topic IN ({array_int:sticky_topic_ids})',
  754. array(
  755. 'sticky_topic_ids' => $stickyCache,
  756. 'is_sticky' => 1,
  757. )
  758. );
  759. // Get the board IDs and Sticky status
  760. $request = $smcFunc['db_query']('', '
  761. SELECT id_topic, id_board, is_sticky
  762. FROM {db_prefix}topics
  763. WHERE id_topic IN ({array_int:sticky_topic_ids})
  764. LIMIT ' . count($stickyCache),
  765. array(
  766. 'sticky_topic_ids' => $stickyCache,
  767. )
  768. );
  769. $stickyCacheBoards = array();
  770. $stickyCacheStatus = array();
  771. while ($row = $smcFunc['db_fetch_assoc']($request))
  772. {
  773. $stickyCacheBoards[$row['id_topic']] = $row['id_board'];
  774. $stickyCacheStatus[$row['id_topic']] = empty($row['is_sticky']);
  775. }
  776. $smcFunc['db_free_result']($request);
  777. }
  778. // Move sucka! (this is, by the by, probably the most complicated part....)
  779. if (!empty($moveCache[0]))
  780. {
  781. // I know - I just KNOW you're trying to beat the system. Too bad for you... we CHECK :P.
  782. $request = $smcFunc['db_query']('', '
  783. SELECT t.id_topic, t.id_board, b.count_posts
  784. FROM {db_prefix}topics AS t
  785. LEFT JOIN {db_prefix}boards AS b ON (t.id_board = b.id_board)
  786. WHERE t.id_topic IN ({array_int:move_topic_ids})' . (!empty($board) && !allowedTo('move_any') ? '
  787. AND t.id_member_started = {int:current_member}' : '') . '
  788. LIMIT ' . count($moveCache[0]),
  789. array(
  790. 'current_member' => $user_info['id'],
  791. 'move_topic_ids' => $moveCache[0],
  792. )
  793. );
  794. $moveTos = array();
  795. $moveCache2 = array();
  796. $countPosts = array();
  797. while ($row = $smcFunc['db_fetch_assoc']($request))
  798. {
  799. $to = $moveCache[1][$row['id_topic']];
  800. if (empty($to))
  801. continue;
  802. // Does this topic's board count the posts or not?
  803. $countPosts[$row['id_topic']] = empty($row['count_posts']);
  804. if (!isset($moveTos[$to]))
  805. $moveTos[$to] = array();
  806. $moveTos[$to][] = $row['id_topic'];
  807. // For reporting...
  808. $moveCache2[] = array($row['id_topic'], $row['id_board'], $to);
  809. }
  810. $smcFunc['db_free_result']($request);
  811. $moveCache = $moveCache2;
  812. require_once($sourcedir . '/MoveTopic.php');
  813. // Do the actual moves...
  814. foreach ($moveTos as $to => $topics)
  815. moveTopics($topics, $to);
  816. // Does the post counts need to be updated?
  817. if (!empty($moveTos))
  818. {
  819. $topicRecounts = array();
  820. $request = $smcFunc['db_query']('', '
  821. SELECT id_board, count_posts
  822. FROM {db_prefix}boards
  823. WHERE id_board IN ({array_int:move_boards})',
  824. array(
  825. 'move_boards' => array_keys($moveTos),
  826. )
  827. );
  828. while ($row = $smcFunc['db_fetch_assoc']($request))
  829. {
  830. $cp = empty($row['count_posts']);
  831. // Go through all the topics that are being moved to this board.
  832. foreach ($moveTos[$row['id_board']] as $topic)
  833. {
  834. // If both boards have the same value for post counting then no adjustment needs to be made.
  835. if ($countPosts[$topic] != $cp)
  836. {
  837. // If the board being moved to does count the posts then the other one doesn't so add to their post count.
  838. $topicRecounts[$topic] = $cp ? '+' : '-';
  839. }
  840. }
  841. }
  842. $smcFunc['db_free_result']($request);
  843. if (!empty($topicRecounts))
  844. {
  845. $members = array();
  846. // Get all the members who have posted in the moved topics.
  847. $request = $smcFunc['db_query']('', '
  848. SELECT id_member, id_topic
  849. FROM {db_prefix}messages
  850. WHERE id_topic IN ({array_int:moved_topic_ids})',
  851. array(
  852. 'moved_topic_ids' => array_keys($topicRecounts),
  853. )
  854. );
  855. while ($row = $smcFunc['db_fetch_assoc']($request))
  856. {
  857. if (!isset($members[$row['id_member']]))
  858. $members[$row['id_member']] = 0;
  859. if ($topicRecounts[$row['id_topic']] === '+')
  860. $members[$row['id_member']] += 1;
  861. else
  862. $members[$row['id_member']] -= 1;
  863. }
  864. $smcFunc['db_free_result']($request);
  865. // And now update them member's post counts
  866. foreach ($members as $id_member => $post_adj)
  867. updateMemberData($id_member, array('posts' => 'posts + ' . $post_adj));
  868. }
  869. }
  870. }
  871. // Now delete the topics...
  872. if (!empty($removeCache))
  873. {
  874. // They can only delete their own topics. (we wouldn't be here if they couldn't do that..)
  875. $result = $smcFunc['db_query']('', '
  876. SELECT id_topic, id_board
  877. FROM {db_prefix}topics
  878. WHERE id_topic IN ({array_int:removed_topic_ids})' . (!empty($board) && !allowedTo('remove_any') ? '
  879. AND id_member_started = {int:current_member}' : '') . '
  880. LIMIT ' . count($removeCache),
  881. array(
  882. 'current_member' => $user_info['id'],
  883. 'removed_topic_ids' => $removeCache,
  884. )
  885. );
  886. $removeCache = array();
  887. $removeCacheBoards = array();
  888. while ($row = $smcFunc['db_fetch_assoc']($result))
  889. {
  890. $removeCache[] = $row['id_topic'];
  891. $removeCacheBoards[$row['id_topic']] = $row['id_board'];
  892. }
  893. $smcFunc['db_free_result']($result);
  894. // Maybe *none* were their own topics.
  895. if (!empty($removeCache))
  896. {
  897. // Gotta send the notifications *first*!
  898. foreach ($removeCache as $topic)
  899. {
  900. // Only log the topic ID if it's not in the recycle board.
  901. logAction('remove', array((empty($modSettings['recycle_enable']) || $modSettings['recycle_board'] != $removeCacheBoards[$topic] ? 'topic' : 'old_topic_id') => $topic, 'board' => $removeCacheBoards[$topic]));
  902. sendNotifications($topic, 'remove');
  903. }
  904. require_once($sourcedir . '/RemoveTopic.php');
  905. removeTopics($removeCache);
  906. }
  907. }
  908. // Approve the topics...
  909. if (!empty($approveCache))
  910. {
  911. // We need unapproved topic ids and their authors!
  912. $request = $smcFunc['db_query']('', '
  913. SELECT id_topic, id_member_started
  914. FROM {db_prefix}topics
  915. WHERE id_topic IN ({array_int:approve_topic_ids})
  916. AND approved = {int:not_approved}
  917. LIMIT ' . count($approveCache),
  918. array(
  919. 'approve_topic_ids' => $approveCache,
  920. 'not_approved' => 0,
  921. )
  922. );
  923. $approveCache = array();
  924. $approveCacheMembers = array();
  925. while ($row = $smcFunc['db_fetch_assoc']($request))
  926. {
  927. $approveCache[] = $row['id_topic'];
  928. $approveCacheMembers[$row['id_topic']] = $row['id_member_started'];
  929. }
  930. $smcFunc['db_free_result']($request);
  931. // Any topics to approve?
  932. if (!empty($approveCache))
  933. {
  934. // Handle the approval part...
  935. approveTopics($approveCache);
  936. // Time for some logging!
  937. foreach ($approveCache as $topic)
  938. logAction('approve_topic', array('topic' => $topic, 'member' => $approveCacheMembers[$topic]));
  939. }
  940. }
  941. // And (almost) lastly, lock the topics...
  942. if (!empty($lockCache))
  943. {
  944. $lockStatus = array();
  945. // Gotta make sure they CAN lock/unlock these topics...
  946. if (!empty($board) && !allowedTo('lock_any'))
  947. {
  948. // Make sure they started the topic AND it isn't already locked by someone with higher priv's.
  949. $result = $smcFunc['db_query']('', '
  950. SELECT id_topic, locked, id_board
  951. FROM {db_prefix}topics
  952. WHERE id_topic IN ({array_int:locked_topic_ids})
  953. AND id_member_started = {int:current_member}
  954. AND locked IN (2, 0)
  955. LIMIT ' . count($lockCache),
  956. array(
  957. 'current_member' => $user_info['id'],
  958. 'locked_topic_ids' => $lockCache,
  959. )
  960. );
  961. $lockCache = array();
  962. $lockCacheBoards = array();
  963. while ($row = $smcFunc['db_fetch_assoc']($result))
  964. {
  965. $lockCache[] = $row['id_topic'];
  966. $lockCacheBoards[$row['id_topic']] = $row['id_board'];
  967. $lockStatus[$row['id_topic']] = empty($row['locked']);
  968. }
  969. $smcFunc['db_free_result']($result);
  970. }
  971. else
  972. {
  973. $result = $smcFunc['db_query']('', '
  974. SELECT id_topic, locked, id_board
  975. FROM {db_prefix}topics
  976. WHERE id_topic IN ({array_int:locked_topic_ids})
  977. LIMIT ' . count($lockCache),
  978. array(
  979. 'locked_topic_ids' => $lockCache,
  980. )
  981. );
  982. $lockCacheBoards = array();
  983. while ($row = $smcFunc['db_fetch_assoc']($result))
  984. {
  985. $lockStatus[$row['id_topic']] = empty($row['locked']);
  986. $lockCacheBoards[$row['id_topic']] = $row['id_board'];
  987. }
  988. $smcFunc['db_free_result']($result);
  989. }
  990. // It could just be that *none* were their own topics...
  991. if (!empty($lockCache))
  992. {
  993. // Alternate the locked value.
  994. $smcFunc['db_query']('', '
  995. UPDATE {db_prefix}topics
  996. SET locked = CASE WHEN locked = {int:is_locked} THEN ' . (allowedTo('lock_any') ? '1' : '2') . ' ELSE 0 END
  997. WHERE id_topic IN ({array_int:locked_topic_ids})',
  998. array(
  999. 'locked_topic_ids' => $lockCache,
  1000. 'is_locked' => 0,
  1001. )
  1002. );
  1003. }
  1004. }
  1005. if (!empty($markCache))
  1006. {
  1007. $markArray = array();
  1008. foreach ($markCache as $topic)
  1009. $markArray[] = array($modSettings['maxMsgID'], $user_info['id'], $topic);
  1010. $smcFunc['db_insert']('replace',
  1011. '{db_prefix}log_topics',
  1012. array('id_msg' => 'int', 'id_member' => 'int', 'id_topic' => 'int'),
  1013. $markArray,
  1014. array('id_member', 'id_topic')
  1015. );
  1016. }
  1017. foreach ($moveCache as $topic)
  1018. {
  1019. // Didn't actually move anything!
  1020. if (!isset($topic[0]))
  1021. break;
  1022. logAction('move', array('topic' => $topic[0], 'board_from' => $topic[1], 'board_to' => $topic[2]));
  1023. sendNotifications($topic[0], 'move');
  1024. }
  1025. foreach ($lockCache as $topic)
  1026. {
  1027. logAction($lockStatus[$topic] ? 'lock' : 'unlock', array('topic' => $topic, 'board' => $lockCacheBoards[$topic]));
  1028. sendNotifications($topic, $lockStatus[$topic] ? 'lock' : 'unlock');
  1029. }
  1030. foreach ($stickyCache as $topic)
  1031. {
  1032. logAction($stickyCacheStatus[$topic] ? 'unsticky' : 'sticky', array('topic' => $topic, 'board' => $stickyCacheBoards[$topic]));
  1033. sendNotifications($topic, 'sticky');
  1034. }
  1035. updateStats('topic');
  1036. updateStats('message');
  1037. updateSettings(array(
  1038. 'calendar_updated' => time(),
  1039. ));
  1040. if (!empty($affectedBoards))
  1041. updateLastMessages(array_keys($affectedBoards));
  1042. redirectexit($redirect_url);
  1043. }
  1044. ?>