RemoveTopic.php 47 KB

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