PageRenderTime 53ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/Sources/MessageIndex.php

https://github.com/smf-portal/SMF2.1
PHP | 1210 lines | 954 code | 134 blank | 122 comment | 263 complexity | 23befb1f107431b56c629c9826d27afb MD5 | raw file

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

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

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