RemoveTopic.php 47 KB

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