PageRenderTime 61ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/sources/controllers/MessageIndex.controller.php

https://github.com/Arantor/Elkarte
PHP | 1201 lines | 946 code | 134 blank | 121 comment | 255 complexity | b3b006bb9eecb4c29f4906c1e747c7c9 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0

Large files files are truncated, but you can click here to view the full file

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

Large files files are truncated, but you can click here to view the full file