RemoveTopic.php 47 KB

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