RemoveTopic.php 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488
  1. <?php
  2. /**
  3. * The contents of this file handle the deletion of topics, posts, and related
  4. * paraphernalia.
  5. *
  6. * Simple Machines Forum (SMF)
  7. *
  8. * @package SMF
  9. * @author Simple Machines http://www.simplemachines.org
  10. * @copyright 2014 Simple Machines and individual contributors
  11. * @license http://www.simplemachines.org/about/smf/license.php BSD
  12. *
  13. * @version 2.1 Alpha 1
  14. */
  15. if (!defined('SMF'))
  16. die('No direct access...');
  17. /* The contents of this file handle the deletion of topics, posts, and related
  18. paraphernalia. It has the following functions:
  19. */
  20. /**
  21. * Completely remove an entire topic.
  22. * Redirects to the board when completed.
  23. */
  24. function RemoveTopic2()
  25. {
  26. global $user_info, $topic, $board, $sourcedir, $smcFunc, $context, $modSettings;
  27. // Make sure they aren't being lead around by someone. (:@)
  28. checkSession('get');
  29. // This file needs to be included for sendNotifications().
  30. require_once($sourcedir . '/Subs-Post.php');
  31. // Trying to fool us around, are we?
  32. if (empty($topic))
  33. redirectexit();
  34. removeDeleteConcurrence();
  35. $request = $smcFunc['db_query']('', '
  36. SELECT t.id_member_started, ms.subject, t.approved
  37. FROM {db_prefix}topics AS t
  38. INNER JOIN {db_prefix}messages AS ms ON (ms.id_msg = t.id_first_msg)
  39. WHERE t.id_topic = {int:current_topic}
  40. LIMIT 1',
  41. array(
  42. 'current_topic' => $topic,
  43. )
  44. );
  45. list ($starter, $subject, $approved) = $smcFunc['db_fetch_row']($request);
  46. $smcFunc['db_free_result']($request);
  47. if ($starter == $user_info['id'] && !allowedTo('remove_any'))
  48. isAllowedTo('remove_own');
  49. else
  50. isAllowedTo('remove_any');
  51. // Can they see the topic?
  52. if ($modSettings['postmod_active'] && !$approved && $starter != $user_info['id'])
  53. isAllowedTo('approve_posts');
  54. // Notify people that this topic has been removed.
  55. sendNotifications($topic, 'remove');
  56. removeTopics($topic);
  57. // Note, only log topic ID in native form if it's not gone forever.
  58. if (allowedTo('remove_any') || (allowedTo('remove_own') && $starter == $user_info['id']))
  59. logAction('remove', array((empty($modSettings['recycle_enable']) || $modSettings['recycle_board'] != $board ? 'topic' : 'old_topic_id') => $topic, 'subject' => $subject, 'member' => $starter, 'board' => $board));
  60. redirectexit('board=' . $board . '.0');
  61. }
  62. /**
  63. * Remove just a single post.
  64. * On completion redirect to the topic or to the board.
  65. */
  66. function DeleteMessage()
  67. {
  68. global $user_info, $topic, $board, $modSettings, $smcFunc;
  69. checkSession('get');
  70. $_REQUEST['msg'] = (int) $_REQUEST['msg'];
  71. // Is $topic set?
  72. if (empty($topic) && isset($_REQUEST['topic']))
  73. $topic = (int) $_REQUEST['topic'];
  74. removeDeleteConcurrence();
  75. $request = $smcFunc['db_query']('', '
  76. SELECT t.id_member_started, m.id_member, m.subject, m.poster_time, m.approved
  77. FROM {db_prefix}topics AS t
  78. INNER JOIN {db_prefix}messages AS m ON (m.id_msg = {int:id_msg} AND m.id_topic = {int:current_topic})
  79. WHERE t.id_topic = {int:current_topic}
  80. LIMIT 1',
  81. array(
  82. 'current_topic' => $topic,
  83. 'id_msg' => $_REQUEST['msg'],
  84. )
  85. );
  86. list ($starter, $poster, $subject, $post_time, $approved) = $smcFunc['db_fetch_row']($request);
  87. $smcFunc['db_free_result']($request);
  88. // Verify they can see this!
  89. if ($modSettings['postmod_active'] && !$approved && !empty($poster) && $poster != $user_info['id'])
  90. isAllowedTo('approve_posts');
  91. if ($poster == $user_info['id'])
  92. {
  93. if (!allowedTo('delete_own'))
  94. {
  95. if ($starter == $user_info['id'] && !allowedTo('delete_any'))
  96. isAllowedTo('delete_replies');
  97. elseif (!allowedTo('delete_any'))
  98. isAllowedTo('delete_own');
  99. }
  100. elseif (!allowedTo('delete_any') && ($starter != $user_info['id'] || !allowedTo('delete_replies')) && !empty($modSettings['edit_disable_time']) && $post_time + $modSettings['edit_disable_time'] * 60 < time())
  101. fatal_lang_error('modify_post_time_passed', false);
  102. }
  103. elseif ($starter == $user_info['id'] && !allowedTo('delete_any'))
  104. isAllowedTo('delete_replies');
  105. else
  106. isAllowedTo('delete_any');
  107. // If the full topic was removed go back to the board.
  108. $full_topic = removeMessage($_REQUEST['msg']);
  109. if (allowedTo('delete_any') && (!allowedTo('delete_own') || $poster != $user_info['id']))
  110. logAction('delete', array('topic' => $topic, 'subject' => $subject, 'member' => $poster, 'board' => $board));
  111. // We want to redirect back to recent action.
  112. if (isset($_REQUEST['modcenter']))
  113. redirectexit('action=moderate;area=reports;done');
  114. elseif (isset($_REQUEST['recent']))
  115. redirectexit('action=recent');
  116. elseif (isset($_REQUEST['profile'], $_REQUEST['start'], $_REQUEST['u']))
  117. redirectexit('action=profile;u=' . $_REQUEST['u'] . ';area=showposts;start=' . $_REQUEST['start']);
  118. elseif ($full_topic)
  119. redirectexit('board=' . $board . '.0');
  120. else
  121. redirectexit('topic=' . $topic . '.' . $_REQUEST['start']);
  122. }
  123. /**
  124. * So long as you are sure... all old posts will be gone.
  125. * Used in ManageMaintenance.php to prune old topics.
  126. */
  127. function RemoveOldTopics2()
  128. {
  129. global $smcFunc;
  130. isAllowedTo('admin_forum');
  131. checkSession('post', 'admin');
  132. // No boards at all? Forget it then :/.
  133. if (empty($_POST['boards']))
  134. redirectexit('action=admin;area=maintain;sa=topics');
  135. // This should exist, but we can make sure.
  136. $_POST['delete_type'] = isset($_POST['delete_type']) ? $_POST['delete_type'] : 'nothing';
  137. // Custom conditions.
  138. $condition = '';
  139. $condition_params = array(
  140. 'boards' => array_keys($_POST['boards']),
  141. 'poster_time' => time() - 3600 * 24 * $_POST['maxdays'],
  142. );
  143. // Just moved notice topics?
  144. if ($_POST['delete_type'] == 'moved')
  145. {
  146. $condition .= '
  147. AND m.icon = {string:icon}
  148. AND t.locked = {int:locked}';
  149. $condition_params['icon'] = 'moved';
  150. $condition_params['locked'] = 1;
  151. }
  152. // Otherwise, maybe locked topics only?
  153. elseif ($_POST['delete_type'] == 'locked')
  154. {
  155. $condition .= '
  156. AND t.locked = {int:locked}';
  157. $condition_params['locked'] = 1;
  158. }
  159. // Exclude stickies?
  160. if (isset($_POST['delete_old_not_sticky']))
  161. {
  162. $condition .= '
  163. AND t.is_sticky = {int:is_sticky}';
  164. $condition_params['is_sticky'] = 0;
  165. }
  166. // All we're gonna do here is grab the id_topic's and send them to removeTopics().
  167. $request = $smcFunc['db_query']('', '
  168. SELECT t.id_topic
  169. FROM {db_prefix}topics AS t
  170. INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_last_msg)
  171. WHERE
  172. m.poster_time < {int:poster_time}' . $condition . '
  173. AND t.id_board IN ({array_int:boards})',
  174. $condition_params
  175. );
  176. $topics = array();
  177. while ($row = $smcFunc['db_fetch_assoc']($request))
  178. $topics[] = $row['id_topic'];
  179. $smcFunc['db_free_result']($request);
  180. removeTopics($topics, false, true);
  181. // Log an action into the moderation log.
  182. logAction('pruned', array('days' => $_POST['maxdays']));
  183. redirectexit('action=admin;area=maintain;sa=topics;done=purgeold');
  184. }
  185. /**
  186. * Removes the passed id_topic's. (permissions are NOT checked here!).
  187. *
  188. * @param array/int $topics The topics to remove (can be an id or an array of ids).
  189. * @param bool $decreasePostCount if true users' post count will be reduced
  190. * @param bool $ignoreRecycling if true topics are not moved to the recycle board (if it exists).
  191. */
  192. function removeTopics($topics, $decreasePostCount = true, $ignoreRecycling = false)
  193. {
  194. global $sourcedir, $modSettings, $smcFunc;
  195. // Nothing to do?
  196. if (empty($topics))
  197. return;
  198. // Only a single topic.
  199. if (is_numeric($topics))
  200. $topics = array($topics);
  201. $recycle_board = !empty($modSettings['recycle_enable']) && !empty($modSettings['recycle_board']) ? (int) $modSettings['recycle_board'] : 0;
  202. // Decrease the post counts.
  203. if ($decreasePostCount)
  204. {
  205. $requestMembers = $smcFunc['db_query']('', '
  206. SELECT m.id_member, COUNT(*) AS posts
  207. FROM {db_prefix}messages AS m
  208. INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)
  209. WHERE m.id_topic IN ({array_int:topics})' . (!empty($recycle_board) ? '
  210. AND m.id_board != {int:recycled_board}' : '') . '
  211. AND b.count_posts = {int:do_count_posts}
  212. AND m.approved = {int:is_approved}
  213. GROUP BY m.id_member',
  214. array(
  215. 'do_count_posts' => 0,
  216. 'recycled_board' => $recycle_board,
  217. 'topics' => $topics,
  218. 'is_approved' => 1,
  219. )
  220. );
  221. if ($smcFunc['db_num_rows']($requestMembers) > 0)
  222. {
  223. while ($rowMembers = $smcFunc['db_fetch_assoc']($requestMembers))
  224. updateMemberData($rowMembers['id_member'], array('posts' => 'posts - ' . $rowMembers['posts']));
  225. }
  226. $smcFunc['db_free_result']($requestMembers);
  227. }
  228. // Recycle topics that aren't in the recycle board...
  229. if (!empty($recycle_board) && !$ignoreRecycling)
  230. {
  231. $request = $smcFunc['db_query']('', '
  232. SELECT id_topic, id_board, unapproved_posts, approved
  233. FROM {db_prefix}topics
  234. WHERE id_topic IN ({array_int:topics})
  235. AND id_board != {int:recycle_board}
  236. LIMIT ' . count($topics),
  237. array(
  238. 'recycle_board' => $recycle_board,
  239. 'topics' => $topics,
  240. )
  241. );
  242. if ($smcFunc['db_num_rows']($request) > 0)
  243. {
  244. // Get topics that will be recycled.
  245. $recycleTopics = array();
  246. while ($row = $smcFunc['db_fetch_assoc']($request))
  247. {
  248. if (function_exists('apache_reset_timeout'))
  249. @apache_reset_timeout();
  250. $recycleTopics[] = $row['id_topic'];
  251. // Set the id_previous_board for this topic - and make it not sticky.
  252. $smcFunc['db_query']('', '
  253. UPDATE {db_prefix}topics
  254. SET id_previous_board = {int:id_previous_board}, is_sticky = {int:not_sticky}
  255. WHERE id_topic = {int:id_topic}',
  256. array(
  257. 'id_previous_board' => $row['id_board'],
  258. 'id_topic' => $row['id_topic'],
  259. 'not_sticky' => 0,
  260. )
  261. );
  262. }
  263. $smcFunc['db_free_result']($request);
  264. // Move the topics to the recycle board.
  265. require_once($sourcedir . '/MoveTopic.php');
  266. moveTopics($recycleTopics, $modSettings['recycle_board']);
  267. // Close reports that are being recycled.
  268. require_once($sourcedir . '/ModerationCenter.php');
  269. $smcFunc['db_query']('', '
  270. UPDATE {db_prefix}log_reported
  271. SET closed = {int:is_closed}
  272. WHERE id_topic IN ({array_int:recycle_topics})',
  273. array(
  274. 'recycle_topics' => $recycleTopics,
  275. 'is_closed' => 1,
  276. )
  277. );
  278. updateSettings(array('last_mod_report_action' => time()));
  279. recountOpenReports();
  280. // Topics that were recycled don't need to be deleted, so subtract them.
  281. $topics = array_diff($topics, $recycleTopics);
  282. }
  283. else
  284. $smcFunc['db_free_result']($request);
  285. }
  286. // Still topics left to delete?
  287. if (empty($topics))
  288. return;
  289. $adjustBoards = array();
  290. // Find out how many posts we are deleting.
  291. $request = $smcFunc['db_query']('', '
  292. SELECT id_board, approved, COUNT(*) AS num_topics, SUM(unapproved_posts) AS unapproved_posts,
  293. SUM(num_replies) AS num_replies
  294. FROM {db_prefix}topics
  295. WHERE id_topic IN ({array_int:topics})
  296. GROUP BY id_board, approved',
  297. array(
  298. 'topics' => $topics,
  299. )
  300. );
  301. while ($row = $smcFunc['db_fetch_assoc']($request))
  302. {
  303. if (!isset($adjustBoards[$row['id_board']]['num_posts']))
  304. {
  305. $adjustBoards[$row['id_board']] = array(
  306. 'num_posts' => 0,
  307. 'num_topics' => 0,
  308. 'unapproved_posts' => 0,
  309. 'unapproved_topics' => 0,
  310. 'id_board' => $row['id_board']
  311. );
  312. }
  313. // Posts = (num_replies + 1) for each approved topic.
  314. $adjustBoards[$row['id_board']]['num_posts'] += $row['num_replies'] + ($row['approved'] ? $row['num_topics'] : 0);
  315. $adjustBoards[$row['id_board']]['unapproved_posts'] += $row['unapproved_posts'];
  316. // Add the topics to the right type.
  317. if ($row['approved'])
  318. $adjustBoards[$row['id_board']]['num_topics'] += $row['num_topics'];
  319. else
  320. $adjustBoards[$row['id_board']]['unapproved_topics'] += $row['num_topics'];
  321. }
  322. $smcFunc['db_free_result']($request);
  323. // Decrease the posts/topics...
  324. foreach ($adjustBoards as $stats)
  325. {
  326. if (function_exists('apache_reset_timeout'))
  327. @apache_reset_timeout();
  328. $smcFunc['db_query']('', '
  329. UPDATE {db_prefix}boards
  330. SET
  331. num_posts = CASE WHEN {int:num_posts} > num_posts THEN 0 ELSE num_posts - {int:num_posts} END,
  332. num_topics = CASE WHEN {int:num_topics} > num_topics THEN 0 ELSE num_topics - {int:num_topics} END,
  333. unapproved_posts = CASE WHEN {int:unapproved_posts} > unapproved_posts THEN 0 ELSE unapproved_posts - {int:unapproved_posts} END,
  334. unapproved_topics = CASE WHEN {int:unapproved_topics} > unapproved_topics THEN 0 ELSE unapproved_topics - {int:unapproved_topics} END
  335. WHERE id_board = {int:id_board}',
  336. array(
  337. 'id_board' => $stats['id_board'],
  338. 'num_posts' => $stats['num_posts'],
  339. 'num_topics' => $stats['num_topics'],
  340. 'unapproved_posts' => $stats['unapproved_posts'],
  341. 'unapproved_topics' => $stats['unapproved_topics'],
  342. )
  343. );
  344. }
  345. // Remove Polls.
  346. $request = $smcFunc['db_query']('', '
  347. SELECT id_poll
  348. FROM {db_prefix}topics
  349. WHERE id_topic IN ({array_int:topics})
  350. AND id_poll > {int:no_poll}
  351. LIMIT ' . count($topics),
  352. array(
  353. 'no_poll' => 0,
  354. 'topics' => $topics,
  355. )
  356. );
  357. $polls = array();
  358. while ($row = $smcFunc['db_fetch_assoc']($request))
  359. $polls[] = $row['id_poll'];
  360. $smcFunc['db_free_result']($request);
  361. if (!empty($polls))
  362. {
  363. $smcFunc['db_query']('', '
  364. DELETE FROM {db_prefix}polls
  365. WHERE id_poll IN ({array_int:polls})',
  366. array(
  367. 'polls' => $polls,
  368. )
  369. );
  370. $smcFunc['db_query']('', '
  371. DELETE FROM {db_prefix}poll_choices
  372. WHERE id_poll IN ({array_int:polls})',
  373. array(
  374. 'polls' => $polls,
  375. )
  376. );
  377. $smcFunc['db_query']('', '
  378. DELETE FROM {db_prefix}log_polls
  379. WHERE id_poll IN ({array_int:polls})',
  380. array(
  381. 'polls' => $polls,
  382. )
  383. );
  384. }
  385. // Get rid of the attachment, if it exists.
  386. require_once($sourcedir . '/ManageAttachments.php');
  387. $attachmentQuery = array(
  388. 'attachment_type' => 0,
  389. 'id_topic' => $topics,
  390. );
  391. removeAttachments($attachmentQuery, 'messages');
  392. // Delete possible search index entries.
  393. if (!empty($modSettings['search_custom_index_config']))
  394. {
  395. $customIndexSettings = unserialize($modSettings['search_custom_index_config']);
  396. $words = array();
  397. $messages = array();
  398. $request = $smcFunc['db_query']('', '
  399. SELECT id_msg, body
  400. FROM {db_prefix}messages
  401. WHERE id_topic IN ({array_int:topics})',
  402. array(
  403. 'topics' => $topics,
  404. )
  405. );
  406. while ($row = $smcFunc['db_fetch_assoc']($request))
  407. {
  408. if (function_exists('apache_reset_timeout'))
  409. @apache_reset_timeout();
  410. $words = array_merge($words, text2words($row['body'], $customIndexSettings['bytes_per_word'], true));
  411. $messages[] = $row['id_msg'];
  412. }
  413. $smcFunc['db_free_result']($request);
  414. $words = array_unique($words);
  415. if (!empty($words) && !empty($messages))
  416. $smcFunc['db_query']('', '
  417. DELETE FROM {db_prefix}log_search_words
  418. WHERE id_word IN ({array_int:word_list})
  419. AND id_msg IN ({array_int:message_list})',
  420. array(
  421. 'word_list' => $words,
  422. 'message_list' => $messages,
  423. )
  424. );
  425. }
  426. // Delete anything related to the topic.
  427. $smcFunc['db_query']('', '
  428. DELETE FROM {db_prefix}messages
  429. WHERE id_topic IN ({array_int:topics})',
  430. array(
  431. 'topics' => $topics,
  432. )
  433. );
  434. $smcFunc['db_query']('', '
  435. DELETE FROM {db_prefix}calendar
  436. WHERE id_topic IN ({array_int:topics})',
  437. array(
  438. 'topics' => $topics,
  439. )
  440. );
  441. $smcFunc['db_query']('', '
  442. DELETE FROM {db_prefix}log_topics
  443. WHERE id_topic IN ({array_int:topics})',
  444. array(
  445. 'topics' => $topics,
  446. )
  447. );
  448. $smcFunc['db_query']('', '
  449. DELETE FROM {db_prefix}log_notify
  450. WHERE id_topic IN ({array_int:topics})',
  451. array(
  452. 'topics' => $topics,
  453. )
  454. );
  455. $smcFunc['db_query']('', '
  456. DELETE FROM {db_prefix}topics
  457. WHERE id_topic IN ({array_int:topics})',
  458. array(
  459. 'topics' => $topics,
  460. )
  461. );
  462. $smcFunc['db_query']('', '
  463. DELETE FROM {db_prefix}log_search_subjects
  464. WHERE id_topic IN ({array_int:topics})',
  465. array(
  466. 'topics' => $topics,
  467. )
  468. );
  469. // Maybe there's a mod that wants to delete topic related data of its own
  470. call_integration_hook('integrate_remove_topics', array($topics));
  471. // Update the totals...
  472. updateStats('message');
  473. updateStats('topic');
  474. updateSettings(array(
  475. 'calendar_updated' => time(),
  476. ));
  477. require_once($sourcedir . '/Subs-Post.php');
  478. $updates = array();
  479. foreach ($adjustBoards as $stats)
  480. $updates[] = $stats['id_board'];
  481. updateLastMessages($updates);
  482. }
  483. /**
  484. * Remove a specific message (including permission checks).
  485. * - normally, local and global should be the localCookies and globalCookies settings, respectively.
  486. * - uses boardurl to determine these two things.
  487. *
  488. * @param int $message The message id
  489. * @param bool $decreasePostCount if true users' post count will be reduced
  490. * @return array an array to set the cookie on with domain and path in it, in that order
  491. */
  492. function removeMessage($message, $decreasePostCount = true)
  493. {
  494. global $board, $sourcedir, $modSettings, $user_info, $smcFunc, $context;
  495. if (empty($message) || !is_numeric($message))
  496. return false;
  497. $recycle_board = !empty($modSettings['recycle_enable']) && !empty($modSettings['recycle_board']) ? (int) $modSettings['recycle_board'] : 0;
  498. $request = $smcFunc['db_query']('', '
  499. SELECT
  500. m.id_member, m.icon, m.poster_time, m.subject,' . (empty($modSettings['search_custom_index_config']) ? '' : ' m.body,') . '
  501. m.approved, t.id_topic, t.id_first_msg, t.id_last_msg, t.num_replies, t.id_board,
  502. t.id_member_started AS id_member_poster,
  503. b.count_posts
  504. FROM {db_prefix}messages AS m
  505. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
  506. INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
  507. WHERE m.id_msg = {int:id_msg}
  508. LIMIT 1',
  509. array(
  510. 'id_msg' => $message,
  511. )
  512. );
  513. if ($smcFunc['db_num_rows']($request) == 0)
  514. return false;
  515. $row = $smcFunc['db_fetch_assoc']($request);
  516. $smcFunc['db_free_result']($request);
  517. if (empty($board) || $row['id_board'] != $board)
  518. {
  519. $delete_any = boardsAllowedTo('delete_any');
  520. if (!in_array(0, $delete_any) && !in_array($row['id_board'], $delete_any))
  521. {
  522. $delete_own = boardsAllowedTo('delete_own');
  523. $delete_own = in_array(0, $delete_own) || in_array($row['id_board'], $delete_own);
  524. $delete_replies = boardsAllowedTo('delete_replies');
  525. $delete_replies = in_array(0, $delete_replies) || in_array($row['id_board'], $delete_replies);
  526. if ($row['id_member'] == $user_info['id'])
  527. {
  528. if (!$delete_own)
  529. {
  530. if ($row['id_member_poster'] == $user_info['id'])
  531. {
  532. if (!$delete_replies)
  533. fatal_lang_error('cannot_delete_replies', 'permission');
  534. }
  535. else
  536. fatal_lang_error('cannot_delete_own', 'permission');
  537. }
  538. elseif (($row['id_member_poster'] != $user_info['id'] || !$delete_replies) && !empty($modSettings['edit_disable_time']) && $row['poster_time'] + $modSettings['edit_disable_time'] * 60 < time())
  539. fatal_lang_error('modify_post_time_passed', false);
  540. }
  541. elseif ($row['id_member_poster'] == $user_info['id'])
  542. {
  543. if (!$delete_replies)
  544. fatal_lang_error('cannot_delete_replies', 'permission');
  545. }
  546. else
  547. fatal_lang_error('cannot_delete_any', 'permission');
  548. }
  549. // Can't delete an unapproved message, if you can't see it!
  550. if ($modSettings['postmod_active'] && !$row['approved'] && $row['id_member'] != $user_info['id'] && !(in_array(0, $delete_any) || in_array($row['id_board'], $delete_any)))
  551. {
  552. $approve_posts = boardsAllowedTo('approve_posts');
  553. if (!in_array(0, $approve_posts) && !in_array($row['id_board'], $approve_posts))
  554. return false;
  555. }
  556. }
  557. else
  558. {
  559. // Check permissions to delete this message.
  560. if ($row['id_member'] == $user_info['id'])
  561. {
  562. if (!allowedTo('delete_own'))
  563. {
  564. if ($row['id_member_poster'] == $user_info['id'] && !allowedTo('delete_any'))
  565. isAllowedTo('delete_replies');
  566. elseif (!allowedTo('delete_any'))
  567. isAllowedTo('delete_own');
  568. }
  569. elseif (!allowedTo('delete_any') && ($row['id_member_poster'] != $user_info['id'] || !allowedTo('delete_replies')) && !empty($modSettings['edit_disable_time']) && $row['poster_time'] + $modSettings['edit_disable_time'] * 60 < time())
  570. fatal_lang_error('modify_post_time_passed', false);
  571. }
  572. elseif ($row['id_member_poster'] == $user_info['id'] && !allowedTo('delete_any'))
  573. isAllowedTo('delete_replies');
  574. else
  575. isAllowedTo('delete_any');
  576. if ($modSettings['postmod_active'] && !$row['approved'] && $row['id_member'] != $user_info['id'] && !allowedTo('delete_own'))
  577. isAllowedTo('approve_posts');
  578. }
  579. // Delete the *whole* topic, but only if the topic consists of one message.
  580. if ($row['id_first_msg'] == $message)
  581. {
  582. if (empty($board) || $row['id_board'] != $board)
  583. {
  584. $remove_any = boardsAllowedTo('remove_any');
  585. $remove_any = in_array(0, $remove_any) || in_array($row['id_board'], $remove_any);
  586. if (!$remove_any)
  587. {
  588. $remove_own = boardsAllowedTo('remove_own');
  589. $remove_own = in_array(0, $remove_own) || in_array($row['id_board'], $remove_own);
  590. }
  591. if ($row['id_member'] != $user_info['id'] && !$remove_any)
  592. fatal_lang_error('cannot_remove_any', 'permission');
  593. elseif (!$remove_any && !$remove_own)
  594. fatal_lang_error('cannot_remove_own', 'permission');
  595. }
  596. else
  597. {
  598. // Check permissions to delete a whole topic.
  599. if ($row['id_member'] != $user_info['id'])
  600. isAllowedTo('remove_any');
  601. elseif (!allowedTo('remove_any'))
  602. isAllowedTo('remove_own');
  603. }
  604. // ...if there is only one post.
  605. if (!empty($row['num_replies']))
  606. fatal_lang_error('delFirstPost', false);
  607. removeTopics($row['id_topic']);
  608. return true;
  609. }
  610. // Deleting a recycled message can not lower anyone's post count.
  611. if (!empty($recycle_board) && $row['id_board'] == $recycle_board)
  612. $decreasePostCount = false;
  613. // This is the last post, update the last post on the board.
  614. if ($row['id_last_msg'] == $message)
  615. {
  616. // Find the last message, set it, and decrease the post count.
  617. $request = $smcFunc['db_query']('', '
  618. SELECT id_msg, id_member
  619. FROM {db_prefix}messages
  620. WHERE id_topic = {int:id_topic}
  621. AND id_msg != {int:id_msg}
  622. ORDER BY ' . ($modSettings['postmod_active'] ? 'approved DESC, ' : '') . 'id_msg DESC
  623. LIMIT 1',
  624. array(
  625. 'id_topic' => $row['id_topic'],
  626. 'id_msg' => $message,
  627. )
  628. );
  629. $row2 = $smcFunc['db_fetch_assoc']($request);
  630. $smcFunc['db_free_result']($request);
  631. $smcFunc['db_query']('', '
  632. UPDATE {db_prefix}topics
  633. SET
  634. id_last_msg = {int:id_last_msg},
  635. id_member_updated = {int:id_member_updated}' . (!$modSettings['postmod_active'] || $row['approved'] ? ',
  636. num_replies = CASE WHEN num_replies = {int:no_replies} THEN 0 ELSE num_replies - 1 END' : ',
  637. unapproved_posts = CASE WHEN unapproved_posts = {int:no_unapproved} THEN 0 ELSE unapproved_posts - 1 END') . '
  638. WHERE id_topic = {int:id_topic}',
  639. array(
  640. 'id_last_msg' => $row2['id_msg'],
  641. 'id_member_updated' => $row2['id_member'],
  642. 'no_replies' => 0,
  643. 'no_unapproved' => 0,
  644. 'id_topic' => $row['id_topic'],
  645. )
  646. );
  647. }
  648. // Only decrease post counts.
  649. else
  650. $smcFunc['db_query']('', '
  651. UPDATE {db_prefix}topics
  652. SET ' . ($row['approved'] ? '
  653. num_replies = CASE WHEN num_replies = {int:no_replies} THEN 0 ELSE num_replies - 1 END' : '
  654. unapproved_posts = CASE WHEN unapproved_posts = {int:no_unapproved} THEN 0 ELSE unapproved_posts - 1 END') . '
  655. WHERE id_topic = {int:id_topic}',
  656. array(
  657. 'no_replies' => 0,
  658. 'no_unapproved' => 0,
  659. 'id_topic' => $row['id_topic'],
  660. )
  661. );
  662. // Default recycle to false.
  663. $recycle = false;
  664. // If recycle topics has been set, make a copy of this message in the recycle board.
  665. // Make sure we're not recycling messages that are already on the recycle board.
  666. if (!empty($modSettings['recycle_enable']) && $row['id_board'] != $modSettings['recycle_board'] && $row['icon'] != 'recycled')
  667. {
  668. // Check if the recycle board exists and if so get the read status.
  669. $request = $smcFunc['db_query']('', '
  670. SELECT (IFNULL(lb.id_msg, 0) >= b.id_msg_updated) AS is_seen, id_last_msg
  671. FROM {db_prefix}boards AS b
  672. LEFT JOIN {db_prefix}log_boards AS lb ON (lb.id_board = b.id_board AND lb.id_member = {int:current_member})
  673. WHERE b.id_board = {int:recycle_board}',
  674. array(
  675. 'current_member' => $user_info['id'],
  676. 'recycle_board' => $modSettings['recycle_board'],
  677. )
  678. );
  679. if ($smcFunc['db_num_rows']($request) == 0)
  680. fatal_lang_error('recycle_no_valid_board');
  681. list ($isRead, $last_board_msg) = $smcFunc['db_fetch_row']($request);
  682. $smcFunc['db_free_result']($request);
  683. // Is there an existing topic in the recycle board to group this post with?
  684. $request = $smcFunc['db_query']('', '
  685. SELECT id_topic, id_first_msg, id_last_msg
  686. FROM {db_prefix}topics
  687. WHERE id_previous_topic = {int:id_previous_topic}
  688. AND id_board = {int:recycle_board}',
  689. array(
  690. 'id_previous_topic' => $row['id_topic'],
  691. 'recycle_board' => $modSettings['recycle_board'],
  692. )
  693. );
  694. list ($id_recycle_topic, $first_topic_msg, $last_topic_msg) = $smcFunc['db_fetch_row']($request);
  695. $smcFunc['db_free_result']($request);
  696. // Insert a new topic in the recycle board if $id_recycle_topic is empty.
  697. if (empty($id_recycle_topic))
  698. $smcFunc['db_insert']('',
  699. '{db_prefix}topics',
  700. array(
  701. 'id_board' => 'int', 'id_member_started' => 'int', 'id_member_updated' => 'int', 'id_first_msg' => 'int',
  702. 'id_last_msg' => 'int', 'unapproved_posts' => 'int', 'approved' => 'int', 'id_previous_topic' => 'int',
  703. ),
  704. array(
  705. $modSettings['recycle_board'], $row['id_member'], $row['id_member'], $message,
  706. $message, 0, 1, $row['id_topic'],
  707. ),
  708. array('id_topic')
  709. );
  710. // Capture the ID of the new topic...
  711. $topicID = empty($id_recycle_topic) ? $smcFunc['db_insert_id']('{db_prefix}topics', 'id_topic') : $id_recycle_topic;
  712. // If the topic creation went successful, move the message.
  713. if ($topicID > 0)
  714. {
  715. $smcFunc['db_query']('', '
  716. UPDATE {db_prefix}messages
  717. SET
  718. id_topic = {int:id_topic},
  719. id_board = {int:recycle_board},
  720. approved = {int:is_approved}
  721. WHERE id_msg = {int:id_msg}',
  722. array(
  723. 'id_topic' => $topicID,
  724. 'recycle_board' => $modSettings['recycle_board'],
  725. 'id_msg' => $message,
  726. 'is_approved' => 1,
  727. )
  728. );
  729. // Take any reported posts with us...
  730. $smcFunc['db_query']('', '
  731. UPDATE {db_prefix}log_reported
  732. SET
  733. id_topic = {int:id_topic},
  734. id_board = {int:recycle_board}
  735. WHERE id_msg = {int:id_msg}',
  736. array(
  737. 'id_topic' => $topicID,
  738. 'recycle_board' => $modSettings['recycle_board'],
  739. 'id_msg' => $message,
  740. )
  741. );
  742. // Mark recycled topic as read.
  743. if (!$user_info['is_guest'])
  744. $smcFunc['db_insert']('replace',
  745. '{db_prefix}log_topics',
  746. array('id_topic' => 'int', 'id_member' => 'int', 'id_msg' => 'int', 'unwatched' => 'int'),
  747. array($topicID, $user_info['id'], $modSettings['maxMsgID'], 0),
  748. array('id_topic', 'id_member')
  749. );
  750. // Mark recycle board as seen, if it was marked as seen before.
  751. if (!empty($isRead) && !$user_info['is_guest'])
  752. $smcFunc['db_insert']('replace',
  753. '{db_prefix}log_boards',
  754. array('id_board' => 'int', 'id_member' => 'int', 'id_msg' => 'int'),
  755. array($modSettings['recycle_board'], $user_info['id'], $modSettings['maxMsgID']),
  756. array('id_board', 'id_member')
  757. );
  758. // Add one topic and post to the recycle bin board.
  759. $smcFunc['db_query']('', '
  760. UPDATE {db_prefix}boards
  761. SET
  762. num_topics = num_topics + {int:num_topics_inc},
  763. num_posts = num_posts + 1' .
  764. ($message > $last_board_msg ? ', id_last_msg = {int:id_merged_msg}' : '') . '
  765. WHERE id_board = {int:recycle_board}',
  766. array(
  767. 'num_topics_inc' => empty($id_recycle_topic) ? 1 : 0,
  768. 'recycle_board' => $modSettings['recycle_board'],
  769. 'id_merged_msg' => $message,
  770. )
  771. );
  772. // Lets increase the num_replies, and the first/last message ID as appropriate.
  773. if (!empty($id_recycle_topic))
  774. $smcFunc['db_query']('', '
  775. UPDATE {db_prefix}topics
  776. SET num_replies = num_replies + 1' .
  777. ($message > $last_topic_msg ? ', id_last_msg = {int:id_merged_msg}' : '') .
  778. ($message < $first_topic_msg ? ', id_first_msg = {int:id_merged_msg}' : '') . '
  779. WHERE id_topic = {int:id_recycle_topic}',
  780. array(
  781. 'id_recycle_topic' => $id_recycle_topic,
  782. 'id_merged_msg' => $message,
  783. )
  784. );
  785. // Make sure this message isn't getting deleted later on.
  786. $recycle = true;
  787. // Make sure we update the search subject index.
  788. updateStats('subject', $topicID, $row['subject']);
  789. }
  790. // If it wasn't approved don't keep it in the queue.
  791. if (!$row['approved'])
  792. $smcFunc['db_query']('', '
  793. DELETE FROM {db_prefix}approval_queue
  794. WHERE id_msg = {int:id_msg}
  795. AND id_attach = {int:id_attach}',
  796. array(
  797. 'id_msg' => $message,
  798. 'id_attach' => 0,
  799. )
  800. );
  801. }
  802. $smcFunc['db_query']('', '
  803. UPDATE {db_prefix}boards
  804. SET ' . ($row['approved'] ? '
  805. num_posts = CASE WHEN num_posts = {int:no_posts} THEN 0 ELSE num_posts - 1 END' : '
  806. unapproved_posts = CASE WHEN unapproved_posts = {int:no_unapproved} THEN 0 ELSE unapproved_posts - 1 END') . '
  807. WHERE id_board = {int:id_board}',
  808. array(
  809. 'no_posts' => 0,
  810. 'no_unapproved' => 0,
  811. 'id_board' => $row['id_board'],
  812. )
  813. );
  814. // If the poster was registered and the board this message was on incremented
  815. // the member's posts when it was posted, decrease his or her post count.
  816. if (!empty($row['id_member']) && $decreasePostCount && empty($row['count_posts']) && $row['approved'])
  817. updateMemberData($row['id_member'], array('posts' => '-'));
  818. // Only remove posts if they're not recycled.
  819. if (!$recycle)
  820. {
  821. // Remove the message!
  822. $smcFunc['db_query']('', '
  823. DELETE FROM {db_prefix}messages
  824. WHERE id_msg = {int:id_msg}',
  825. array(
  826. 'id_msg' => $message,
  827. )
  828. );
  829. if (!empty($modSettings['search_custom_index_config']))
  830. {
  831. $customIndexSettings = unserialize($modSettings['search_custom_index_config']);
  832. $words = text2words($row['body'], $customIndexSettings['bytes_per_word'], true);
  833. if (!empty($words))
  834. $smcFunc['db_query']('', '
  835. DELETE FROM {db_prefix}log_search_words
  836. WHERE id_word IN ({array_int:word_list})
  837. AND id_msg = {int:id_msg}',
  838. array(
  839. 'word_list' => $words,
  840. 'id_msg' => $message,
  841. )
  842. );
  843. }
  844. // Delete attachment(s) if they exist.
  845. require_once($sourcedir . '/ManageAttachments.php');
  846. $attachmentQuery = array(
  847. 'attachment_type' => 0,
  848. 'id_msg' => $message,
  849. );
  850. removeAttachments($attachmentQuery);
  851. // Allow mods to remove message related data of their own (likes, maybe?)
  852. call_integration_hook('integrate_remove_message', array($message));
  853. }
  854. // Update the pesky statistics.
  855. updateStats('message');
  856. updateStats('topic');
  857. updateSettings(array(
  858. 'calendar_updated' => time(),
  859. ));
  860. // And now to update the last message of each board we messed with.
  861. require_once($sourcedir . '/Subs-Post.php');
  862. if ($recycle)
  863. updateLastMessages(array($row['id_board'], $modSettings['recycle_board']));
  864. else
  865. updateLastMessages($row['id_board']);
  866. // Close any moderation reports for this message.
  867. $smcFunc['db_query']('', '
  868. UPDATE {db_prefix}log_reported
  869. SET closed = {int:is_closed}
  870. WHERE id_msg = {int:id_msg}',
  871. array(
  872. 'is_closed' => 1,
  873. 'id_msg' => $message,
  874. )
  875. );
  876. if ($smcFunc['db_affected_rows']() != 0)
  877. {
  878. require_once($sourcedir . '/ModerationCenter.php');
  879. updateSettings(array('last_mod_report_action' => time()));
  880. recountOpenReports();
  881. }
  882. return false;
  883. }
  884. /**
  885. * Move back a topic from the recycle board to its original board.
  886. */
  887. function RestoreTopic()
  888. {
  889. global $context, $smcFunc, $modSettings, $sourcedir;
  890. // Check session.
  891. checkSession('get');
  892. // Is recycled board enabled?
  893. if (empty($modSettings['recycle_enable']))
  894. fatal_lang_error('restored_disabled', 'critical');
  895. // Can we be in here?
  896. isAllowedTo('move_any', $modSettings['recycle_board']);
  897. // We need this file.
  898. require_once($sourcedir . '/MoveTopic.php');
  899. $unfound_messages = array();
  900. $topics_to_restore = array();
  901. // Restoring messages?
  902. if (!empty($_REQUEST['msgs']))
  903. {
  904. $msgs = explode(',', $_REQUEST['msgs']);
  905. foreach ($msgs as $k => $msg)
  906. $msgs[$k] = (int) $msg;
  907. // Get the id_previous_board and id_previous_topic.
  908. $request = $smcFunc['db_query']('', '
  909. SELECT m.id_topic, m.id_msg, m.id_board, m.subject, m.id_member, t.id_previous_board, t.id_previous_topic,
  910. t.id_first_msg, b.count_posts, IFNULL(pt.id_board, 0) AS possible_prev_board
  911. FROM {db_prefix}messages AS m
  912. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
  913. INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)
  914. LEFT JOIN {db_prefix}topics AS pt ON (pt.id_topic = t.id_previous_topic)
  915. WHERE m.id_msg IN ({array_int:messages})',
  916. array(
  917. 'messages' => $msgs,
  918. )
  919. );
  920. $actioned_messages = array();
  921. $previous_topics = array();
  922. while ($row = $smcFunc['db_fetch_assoc']($request))
  923. {
  924. // Restoring the first post means topic.
  925. if ($row['id_msg'] == $row['id_first_msg'] && $row['id_previous_topic'] == $row['id_topic'])
  926. {
  927. $topics_to_restore[] = $row['id_topic'];
  928. continue;
  929. }
  930. // Don't know where it's going?
  931. if (empty($row['id_previous_topic']))
  932. {
  933. $unfound_messages[$row['id_msg']] = $row['subject'];
  934. continue;
  935. }
  936. $previous_topics[] = $row['id_previous_topic'];
  937. if (empty($actioned_messages[$row['id_previous_topic']]))
  938. $actioned_messages[$row['id_previous_topic']] = array(
  939. 'msgs' => array(),
  940. 'count_posts' => $row['count_posts'],
  941. 'subject' => $row['subject'],
  942. 'previous_board' => $row['id_previous_board'],
  943. 'possible_prev_board' => $row['possible_prev_board'],
  944. 'current_topic' => $row['id_topic'],
  945. 'current_board' => $row['id_board'],
  946. 'members' => array(),
  947. );
  948. $actioned_messages[$row['id_previous_topic']]['msgs'][$row['id_msg']] = $row['subject'];
  949. if ($row['id_member'])
  950. $actioned_messages[$row['id_previous_topic']]['members'][] = $row['id_member'];
  951. }
  952. $smcFunc['db_free_result']($request);
  953. // Check for topics we are going to fully restore.
  954. foreach ($actioned_messages as $topic => $data)
  955. if (in_array($topic, $topics_to_restore))
  956. unset($actioned_messages[$topic]);
  957. // Load any previous topics to check they exist.
  958. if (!empty($previous_topics))
  959. {
  960. $request = $smcFunc['db_query']('', '
  961. SELECT t.id_topic, t.id_board, m.subject
  962. FROM {db_prefix}topics AS t
  963. INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
  964. WHERE t.id_topic IN ({array_int:previous_topics})',
  965. array(
  966. 'previous_topics' => $previous_topics,
  967. )
  968. );
  969. $previous_topics = array();
  970. while ($row = $smcFunc['db_fetch_assoc']($request))
  971. $previous_topics[$row['id_topic']] = array(
  972. 'board' => $row['id_board'],
  973. 'subject' => $row['subject'],
  974. );
  975. $smcFunc['db_free_result']($request);
  976. }
  977. // Restore each topic.
  978. $messages = array();
  979. foreach ($actioned_messages as $topic => $data)
  980. {
  981. // If we have topics we are going to restore the whole lot ignore them.
  982. if (in_array($topic, $topics_to_restore))
  983. {
  984. unset($actioned_messages[$topic]);
  985. continue;
  986. }
  987. // Move the posts back then!
  988. if (isset($previous_topics[$topic]))
  989. {
  990. mergePosts(array_keys($data['msgs']), $data['current_topic'], $topic);
  991. // Log em.
  992. logAction('restore_posts', array('topic' => $topic, 'subject' => $previous_topics[$topic]['subject'], 'board' => empty($data['previous_board']) ? $data['possible_prev_board'] : $data['previous_board']));
  993. $messages = array_merge(array_keys($data['msgs']), $messages);
  994. }
  995. else
  996. {
  997. foreach ($data['msgs'] as $msg)
  998. $unfound_messages[$msg['id']] = $msg['subject'];
  999. }
  1000. }
  1001. }
  1002. // Now any topics?
  1003. if (!empty($_REQUEST['topics']))
  1004. {
  1005. $topics = explode(',', $_REQUEST['topics']);
  1006. foreach ($topics as $key => $id)
  1007. $topics_to_restore[] = (int) $id;
  1008. }
  1009. if (!empty($topics_to_restore))
  1010. {
  1011. // Lets get the data for these topics.
  1012. $request = $smcFunc['db_query']('', '
  1013. SELECT t.id_topic, t.id_previous_board, t.id_board, t.id_first_msg, m.subject
  1014. FROM {db_prefix}topics AS t
  1015. INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
  1016. WHERE t.id_topic IN ({array_int:topics})',
  1017. array(
  1018. 'topics' => $topics_to_restore,
  1019. )
  1020. );
  1021. while ($row = $smcFunc['db_fetch_assoc']($request))
  1022. {
  1023. // We can only restore if the previous board is set.
  1024. if (empty($row['id_previous_board']))
  1025. {
  1026. $unfound_messages[$row['id_first_msg']] = $row['subject'];
  1027. continue;
  1028. }
  1029. // Ok we got here so me move them from here to there.
  1030. moveTopics($row['id_topic'], $row['id_previous_board']);
  1031. // Lets see if the board that we are returning to has post count enabled.
  1032. $request2 = $smcFunc['db_query']('', '
  1033. SELECT count_posts
  1034. FROM {db_prefix}boards
  1035. WHERE id_board = {int:board}',
  1036. array(
  1037. 'board' => $row['id_previous_board'],
  1038. )
  1039. );
  1040. list ($count_posts) = $smcFunc['db_fetch_row']($request2);
  1041. $smcFunc['db_free_result']($request2);
  1042. if (empty($count_posts))
  1043. {
  1044. // Lets get the members that need their post count restored.
  1045. $request2 = $smcFunc['db_query']('', '
  1046. SELECT id_member, COUNT(id_msg) AS post_count
  1047. FROM {db_prefix}messages
  1048. WHERE id_topic = {int:topic}
  1049. AND approved = {int:is_approved}
  1050. GROUP BY id_member',
  1051. array(
  1052. 'topic' => $row['id_topic'],
  1053. 'is_approved' => 1,
  1054. )
  1055. );
  1056. while ($member = $smcFunc['db_fetch_assoc']($request2))
  1057. updateMemberData($member['id_member'], array('posts' => 'posts + ' . $member['post_count']));
  1058. $smcFunc['db_free_result']($request2);
  1059. }
  1060. // Log it.
  1061. logAction('restore_topic', array('topic' => $row['id_topic'], 'board' => $row['id_board'], 'board_to' => $row['id_previous_board']));
  1062. }
  1063. $smcFunc['db_free_result']($request);
  1064. }
  1065. // Didn't find some things?
  1066. if (!empty($unfound_messages))
  1067. fatal_lang_error('restore_not_found', false, array(implode('<br />', $unfound_messages)));
  1068. // Just send them to the index if they get here.
  1069. redirectexit();
  1070. }
  1071. /**
  1072. * Take a load of messages from one place and stick them in a topic
  1073. *
  1074. * @param array $msgs
  1075. * @param integer $from_topic
  1076. * @param integer $target_topic
  1077. */
  1078. function mergePosts($msgs = array(), $from_topic, $target_topic)
  1079. {
  1080. global $context, $smcFunc, $modSettings, $sourcedir;
  1081. //!!! This really needs to be rewritten to take a load of messages from ANY topic, it's also inefficient.
  1082. // Is it an array?
  1083. if (!is_array($msgs))
  1084. $msgs = array($msgs);
  1085. // Lets make sure they are int.
  1086. foreach ($msgs as $key => $msg)
  1087. $msgs[$key] = (int) $msg;
  1088. // Get the source information.
  1089. $request = $smcFunc['db_query']('', '
  1090. SELECT t.id_board, t.id_first_msg, t.num_replies, t.unapproved_posts
  1091. FROM {db_prefix}topics AS t
  1092. INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
  1093. WHERE t.id_topic = {int:from_topic}',
  1094. array(
  1095. 'from_topic' => $from_topic,
  1096. )
  1097. );
  1098. list ($from_board, $from_first_msg, $from_replies, $from_unapproved_posts) = $smcFunc['db_fetch_row']($request);
  1099. $smcFunc['db_free_result']($request);
  1100. // Get some target topic and board stats.
  1101. $request = $smcFunc['db_query']('', '
  1102. SELECT t.id_board, t.id_first_msg, t.num_replies, t.unapproved_posts, b.count_posts
  1103. FROM {db_prefix}topics AS t
  1104. INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
  1105. WHERE t.id_topic = {int:target_topic}',
  1106. array(
  1107. 'target_topic' => $target_topic,
  1108. )
  1109. );
  1110. list ($target_board, $target_first_msg, $target_replies, $target_unapproved_posts, $count_posts) = $smcFunc['db_fetch_row']($request);
  1111. $smcFunc['db_free_result']($request);
  1112. // Lets see if the board that we are returning to has post count enabled.
  1113. if (empty($count_posts))
  1114. {
  1115. // Lets get the members that need their post count restored.
  1116. $request = $smcFunc['db_query']('', '
  1117. SELECT id_member
  1118. FROM {db_prefix}messages
  1119. WHERE id_msg IN ({array_int:messages})
  1120. AND approved = {int:is_approved}',
  1121. array(
  1122. 'messages' => $msgs,
  1123. 'is_approved' => 1,
  1124. )
  1125. );
  1126. while ($row = $smcFunc['db_fetch_assoc']($request))
  1127. updateMemberData($row['id_member'], array('posts' => '+'));
  1128. }
  1129. // Time to move the messages.
  1130. $smcFunc['db_query']('', '
  1131. UPDATE {db_prefix}messages
  1132. SET
  1133. id_topic = {int:target_topic},
  1134. id_board = {int:target_board},
  1135. WHERE id_msg IN({array_int:msgs})',
  1136. array(
  1137. 'target_topic' => $target_topic,
  1138. 'target_board' => $target_board,
  1139. 'msgs' => $msgs,
  1140. )
  1141. );
  1142. // Fix the id_first_msg and id_last_msg for the target topic.
  1143. $target_topic_data = array(
  1144. 'num_replies' => 0,
  1145. 'unapproved_posts' => 0,
  1146. 'id_first_msg' => 9999999999,
  1147. );
  1148. $request = $smcFunc['db_query']('', '
  1149. SELECT MIN(id_msg) AS id_first_msg, MAX(id_msg) AS id_last_msg, COUNT(*) AS message_count, approved
  1150. FROM {db_prefix}messages
  1151. WHERE id_topic = {int:target_topic}
  1152. GROUP BY id_topic, approved
  1153. ORDER BY approved ASC
  1154. LIMIT 2',
  1155. array(
  1156. 'target_topic' => $target_topic,
  1157. )
  1158. );
  1159. while ($row = $smcFunc['db_fetch_assoc']($request))
  1160. {
  1161. if ($row['id_first_msg'] < $target_topic_data['id_first_msg'])
  1162. $target_topic_data['id_first_msg'] = $row['id_first_msg'];
  1163. $target_topic_data['id_last_msg'] = $row['id_last_msg'];
  1164. if (!$row['approved'])
  1165. $target_topic_data['unapproved_posts'] = $row['message_count'];
  1166. else
  1167. $target_topic_data['num_replies'] = max(0, $row['message_count'] - 1);
  1168. }
  1169. $smcFunc['db_free_result']($request);
  1170. // We have a new post count for the board.
  1171. $smcFunc['db_query']('', '
  1172. UPDATE {db_prefix}boards
  1173. SET
  1174. num_posts = num_posts + {int:diff_replies},
  1175. unapproved_posts = unapproved_posts + {int:diff_unapproved_posts}
  1176. WHERE id_board = {int:target_board}',
  1177. array(
  1178. 'diff_replies' => $target_topic_data['num_replies'] - $target_replies, // Lets keep in mind that the first message in a topic counts towards num_replies in a board.
  1179. 'diff_unapproved_posts' => $target_topic_data['unapproved_posts'] - $target_unapproved_posts,
  1180. 'target_board' => $target_board,
  1181. )
  1182. );
  1183. // In some cases we merged the only post in a topic so the topic data is left behind in the topic table.
  1184. $request = $smcFunc['db_query']('', '
  1185. SELECT id_topic
  1186. FROM {db_prefix}messages
  1187. WHERE id_topic = {int:from_topic}',
  1188. array(
  1189. 'from_topic' => $from_topic,
  1190. )
  1191. );
  1192. // Remove the topic if it doesn't have any messages.
  1193. $topic_exists = true;
  1194. if ($smcFunc['db_num_rows']($request) == 0)
  1195. {
  1196. removeTopics($from_topic, false, true);
  1197. $topic_exists = false;
  1198. }
  1199. $smcFunc['db_free_result']($request);
  1200. // Recycled topic.
  1201. if ($topic_exists == true)
  1202. {
  1203. // Fix the id_first_msg and id_last_msg for the source topic.
  1204. $source_topic_data = array(
  1205. 'num_replies' => 0,
  1206. 'unapproved_posts' => 0,
  1207. 'id_first_msg' => 9999999999,
  1208. );
  1209. $request = $smcFunc['db_query']('', '
  1210. SELECT MIN(id_msg) AS id_first_msg, MAX(id_msg) AS id_last_msg, COUNT(*) AS message_count, approved, subject
  1211. FROM {db_prefix}messages
  1212. WHERE id_topic = {int:from_topic}
  1213. GROUP BY id_topic, approved
  1214. ORDER BY approved ASC
  1215. LIMIT 2',
  1216. array(
  1217. 'from_topic' => $from_topic,
  1218. )
  1219. );
  1220. while ($row = $smcFunc['db_fetch_assoc']($request))
  1221. {
  1222. if ($row['id_first_msg'] < $source_topic_data['id_first_msg'])
  1223. $source_topic_data['id_first_msg'] = $row['id_first_msg'];
  1224. $source_topic_data['id_last_msg'] = $row['id_last_msg'];
  1225. if (!$row['approved'])
  1226. $source_topic_data['unapproved_posts'] = $row['message_count'];
  1227. else
  1228. $source_topic_data['num_replies'] = max(0, $row['message_count'] - 1);
  1229. }
  1230. $smcFunc['db_free_result']($request);
  1231. // Update the topic details for the source topic.
  1232. $smcFunc['db_query']('', '
  1233. UPDATE {db_prefix}topics
  1234. SET
  1235. id_first_msg = {int:id_first_msg},
  1236. id_last_msg = {int:id_last_msg},
  1237. num_replies = {int:num_replies},
  1238. unapproved_posts = {int:unapproved_posts}
  1239. WHERE id_topic = {int:from_topic}',
  1240. array(
  1241. 'id_first_msg' => $source_topic_data['id_first_msg'],
  1242. 'id_last_msg' => $source_topic_data['id_last_msg'],
  1243. 'num_replies' => $source_topic_data['num_replies'],
  1244. 'unapproved_posts' => $source_topic_data['unapproved_posts'],
  1245. 'from_topic' => $from_topic,
  1246. )
  1247. );
  1248. // We have a new post count for the source board.
  1249. $smcFunc['db_query']('', '
  1250. UPDATE {db_prefix}boards
  1251. SET
  1252. num_posts = num_posts + {int:diff_replies},
  1253. unapproved_posts = unapproved_posts + {int:diff_unapproved_posts}
  1254. WHERE id_board = {int:from_board}',
  1255. array(
  1256. 'diff_replies' => $source_topic_data['num_replies'] - $from_replies, // Lets keep in mind that the first message in a topic counts towards num_replies in a board.
  1257. 'diff_unapproved_posts' => $source_topic_data['unapproved_posts'] - $from_unapproved_posts,
  1258. 'from_board' => $from_board,
  1259. )
  1260. );
  1261. }
  1262. // Finally get around to updating the destination topic, now all indexes etc on the source are fixed.
  1263. $smcFunc['db_query']('', '
  1264. UPDATE {db_prefix}topics
  1265. SET
  1266. id_first_msg = {int:id_first_msg},
  1267. id_last_msg = {int:id_last_msg},
  1268. num_replies = {int:num_replies},
  1269. unapproved_posts = {int:unapproved_posts}
  1270. WHERE id_topic = {int:target_topic}',
  1271. array(
  1272. 'id_first_msg' => $target_topic_data['id_first_msg'],
  1273. 'id_last_msg' => $target_topic_data['id_last_msg'],
  1274. 'num_replies' => $target_topic_data['num_replies'],
  1275. 'unapproved_posts' => $target_topic_data['unapproved_posts'],
  1276. 'target_topic' => $target_topic,
  1277. )
  1278. );
  1279. // Need it to update some stats.
  1280. require_once($sourcedir . '/Subs-Post.php');
  1281. // Update stats.
  1282. updateStats('topic');
  1283. updateStats('message');
  1284. // Subject cache?
  1285. $cache_updates = array();
  1286. if ($target_first_msg != $target_topic_data['id_first_msg'])
  1287. $cache_updates[] = $target_topic_data['id_first_msg'];
  1288. if (!empty($source_topic_data['id_first_msg']) && $from_first_msg != $source_topic_data['id_first_msg'])
  1289. $cache_updates[] = $source_topic_data['id_first_msg'];
  1290. if (!empty($cache_updates))
  1291. {
  1292. $request = $smcFunc['db_query']('', '
  1293. SELECT id_topic, subject
  1294. FROM {db_prefix}messages
  1295. WHERE id_msg IN ({array_int:first_messages})',
  1296. array(
  1297. 'first_messages' => $cache_updates,
  1298. )
  1299. );
  1300. while ($row = $smcFunc['db_fetch_assoc']($request))
  1301. updateStats('subject', $row['id_topic'], $row['subject']);
  1302. $smcFunc['db_free_result']($request);
  1303. }
  1304. updateLastMessages(array($from_board, $target_board));
  1305. }
  1306. /**
  1307. * Try to determine if the topic has already been deleted by another user.
  1308. */
  1309. function removeDeleteConcurrence()
  1310. {
  1311. global $modSettings, $board, $topic, $smcFunc, $scripturl, $context;
  1312. // No recycle no need to go further
  1313. if (empty($modSettings['recycle_enable']) || empty($modSettings['recycle_board']))
  1314. return false;
  1315. // If it's confirmed go on and delete (from recycle)
  1316. if (isset($_GET['confirm_delete']))
  1317. return true;
  1318. if (empty($board))
  1319. return false;
  1320. if ($modSettings['recycle_board'] != $board)
  1321. return true;
  1322. elseif (isset($_REQUEST['msg']))
  1323. $confirm_url = $scripturl . '?action=deletemsg;confirm_delete;topic=' . $context['current_topic'] . '.0;msg=' . $_REQUEST['msg'] . ';' . $context['session_var'] . '=' . $context['session_id'];
  1324. else
  1325. $confirm_url = $scripturl . '?action=removetopic2;confirm_delete;topic=' . $context['current_topic'] . '.0;' . $context['session_var'] . '=' . $context['session_id'];
  1326. fatal_lang_error('post_already_deleted', false, array($confirm_url));
  1327. }
  1328. ?>