RemoveTopic.php 45 KB

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