RemoveTopic.php 46 KB

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