2
0

RemoveTopic.php 46 KB

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