RemoveTopic.php 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490
  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. // Update the totals...
  469. updateStats('message');
  470. updateStats('topic');
  471. updateSettings(array(
  472. 'calendar_updated' => time(),
  473. ));
  474. require_once($sourcedir . '/Subs-Post.php');
  475. $updates = array();
  476. foreach ($adjustBoards as $stats)
  477. $updates[] = $stats['id_board'];
  478. updateLastMessages($updates);
  479. }
  480. // Remove a specific message (including permission checks).
  481. function removeMessage($message, $decreasePostCount = true)
  482. {
  483. global $board, $sourcedir, $modSettings, $user_info, $smcFunc, $context;
  484. if (empty($message) || !is_numeric($message))
  485. return false;
  486. $request = $smcFunc['db_query']('', '
  487. SELECT
  488. m.id_member, m.icon, m.poster_time, m.subject,' . (empty($modSettings['search_custom_index_config']) ? '' : ' m.body,') . '
  489. m.approved, t.id_topic, t.id_first_msg, t.id_last_msg, t.num_replies, t.id_board,
  490. t.id_member_started AS id_member_poster,
  491. b.count_posts
  492. FROM {db_prefix}messages AS m
  493. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
  494. INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
  495. WHERE m.id_msg = {int:id_msg}
  496. LIMIT 1',
  497. array(
  498. 'id_msg' => $message,
  499. )
  500. );
  501. if ($smcFunc['db_num_rows']($request) == 0)
  502. return false;
  503. $row = $smcFunc['db_fetch_assoc']($request);
  504. $smcFunc['db_free_result']($request);
  505. if (empty($board) || $row['id_board'] != $board)
  506. {
  507. $delete_any = boardsAllowedTo('delete_any');
  508. if (!in_array(0, $delete_any) && !in_array($row['id_board'], $delete_any))
  509. {
  510. $delete_own = boardsAllowedTo('delete_own');
  511. $delete_own = in_array(0, $delete_own) || in_array($row['id_board'], $delete_own);
  512. $delete_replies = boardsAllowedTo('delete_replies');
  513. $delete_replies = in_array(0, $delete_replies) || in_array($row['id_board'], $delete_replies);
  514. if ($row['id_member'] == $user_info['id'])
  515. {
  516. if (!$delete_own)
  517. {
  518. if ($row['id_member_poster'] == $user_info['id'])
  519. {
  520. if (!$delete_replies)
  521. fatal_lang_error('cannot_delete_replies', 'permission');
  522. }
  523. else
  524. fatal_lang_error('cannot_delete_own', 'permission');
  525. }
  526. elseif (($row['id_member_poster'] != $user_info['id'] || !$delete_replies) && !empty($modSettings['edit_disable_time']) && $row['poster_time'] + $modSettings['edit_disable_time'] * 60 < time())
  527. fatal_lang_error('modify_post_time_passed', false);
  528. }
  529. elseif ($row['id_member_poster'] == $user_info['id'])
  530. {
  531. if (!$delete_replies)
  532. fatal_lang_error('cannot_delete_replies', 'permission');
  533. }
  534. else
  535. fatal_lang_error('cannot_delete_any', 'permission');
  536. }
  537. // Can't delete an unapproved message, if you can't see it!
  538. if ($modSettings['postmod_active'] && !$row['approved'] && $row['id_member'] != $user_info['id'] && !(in_array(0, $delete_any) || in_array($row['id_board'], $delete_any)))
  539. {
  540. $approve_posts = boardsAllowedTo('approve_posts');
  541. if (!in_array(0, $approve_posts) && !in_array($row['id_board'], $approve_posts))
  542. return false;
  543. }
  544. }
  545. else
  546. {
  547. // Check permissions to delete this message.
  548. if ($row['id_member'] == $user_info['id'])
  549. {
  550. if (!allowedTo('delete_own'))
  551. {
  552. if ($row['id_member_poster'] == $user_info['id'] && !allowedTo('delete_any'))
  553. isAllowedTo('delete_replies');
  554. elseif (!allowedTo('delete_any'))
  555. isAllowedTo('delete_own');
  556. }
  557. 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())
  558. fatal_lang_error('modify_post_time_passed', false);
  559. }
  560. elseif ($row['id_member_poster'] == $user_info['id'] && !allowedTo('delete_any'))
  561. isAllowedTo('delete_replies');
  562. else
  563. isAllowedTo('delete_any');
  564. if ($modSettings['postmod_active'] && !$row['approved'] && $row['id_member'] != $user_info['id'] && !allowedTo('delete_own'))
  565. isAllowedTo('approve_posts');
  566. }
  567. // Close any moderation reports for this message.
  568. $smcFunc['db_query']('', '
  569. UPDATE {db_prefix}log_reported
  570. SET closed = {int:is_closed}
  571. WHERE id_msg = {int:id_msg}',
  572. array(
  573. 'is_closed' => 1,
  574. 'id_msg' => $message,
  575. )
  576. );
  577. if ($smcFunc['db_affected_rows']() != 0)
  578. {
  579. require_once($sourcedir . '/ModerationCenter.php');
  580. updateSettings(array('last_mod_report_action' => time()));
  581. recountOpenReports();
  582. }
  583. // Delete the *whole* topic, but only if the topic consists of one message.
  584. if ($row['id_first_msg'] == $message)
  585. {
  586. if (empty($board) || $row['id_board'] != $board)
  587. {
  588. $remove_any = boardsAllowedTo('remove_any');
  589. $remove_any = in_array(0, $remove_any) || in_array($row['id_board'], $remove_any);
  590. if (!$remove_any)
  591. {
  592. $remove_own = boardsAllowedTo('remove_own');
  593. $remove_own = in_array(0, $remove_own) || in_array($row['id_board'], $remove_own);
  594. }
  595. if ($row['id_member'] != $user_info['id'] && !$remove_any)
  596. fatal_lang_error('cannot_remove_any', 'permission');
  597. elseif (!$remove_any && !$remove_own)
  598. fatal_lang_error('cannot_remove_own', 'permission');
  599. }
  600. else
  601. {
  602. // Check permissions to delete a whole topic.
  603. if ($row['id_member'] != $user_info['id'])
  604. isAllowedTo('remove_any');
  605. elseif (!allowedTo('remove_any'))
  606. isAllowedTo('remove_own');
  607. }
  608. // ...if there is only one post.
  609. if (!empty($row['num_replies']))
  610. fatal_lang_error('delFirstPost', false);
  611. removeTopics($row['id_topic']);
  612. return true;
  613. }
  614. // Deleting a recycled message can not lower anyone's post count.
  615. if ($row['icon'] == 'recycled')
  616. $decreasePostCount = false;
  617. // This is the last post, update the last post on the board.
  618. if ($row['id_last_msg'] == $message)
  619. {
  620. // Find the last message, set it, and decrease the post count.
  621. $request = $smcFunc['db_query']('', '
  622. SELECT id_msg, id_member
  623. FROM {db_prefix}messages
  624. WHERE id_topic = {int:id_topic}
  625. AND id_msg != {int:id_msg}
  626. ORDER BY ' . ($modSettings['postmod_active'] ? 'approved DESC, ' : '') . 'id_msg DESC
  627. LIMIT 1',
  628. array(
  629. 'id_topic' => $row['id_topic'],
  630. 'id_msg' => $message,
  631. )
  632. );
  633. $row2 = $smcFunc['db_fetch_assoc']($request);
  634. $smcFunc['db_free_result']($request);
  635. $smcFunc['db_query']('', '
  636. UPDATE {db_prefix}topics
  637. SET
  638. id_last_msg = {int:id_last_msg},
  639. id_member_updated = {int:id_member_updated}' . (!$modSettings['postmod_active'] || $row['approved'] ? ',
  640. num_replies = CASE WHEN num_replies = {int:no_replies} THEN 0 ELSE num_replies - 1 END' : ',
  641. unapproved_posts = CASE WHEN unapproved_posts = {int:no_unapproved} THEN 0 ELSE unapproved_posts - 1 END') . '
  642. WHERE id_topic = {int:id_topic}',
  643. array(
  644. 'id_last_msg' => $row2['id_msg'],
  645. 'id_member_updated' => $row2['id_member'],
  646. 'no_replies' => 0,
  647. 'no_unapproved' => 0,
  648. 'id_topic' => $row['id_topic'],
  649. )
  650. );
  651. }
  652. // Only decrease post counts.
  653. else
  654. $smcFunc['db_query']('', '
  655. UPDATE {db_prefix}topics
  656. SET ' . ($row['approved'] ? '
  657. num_replies = CASE WHEN num_replies = {int:no_replies} THEN 0 ELSE num_replies - 1 END' : '
  658. unapproved_posts = CASE WHEN unapproved_posts = {int:no_unapproved} THEN 0 ELSE unapproved_posts - 1 END') . '
  659. WHERE id_topic = {int:id_topic}',
  660. array(
  661. 'no_replies' => 0,
  662. 'no_unapproved' => 0,
  663. 'id_topic' => $row['id_topic'],
  664. )
  665. );
  666. // Default recycle to false.
  667. $recycle = false;
  668. // If recycle topics has been set, make a copy of this message in the recycle board.
  669. // Make sure we're not recycling messages that are already on the recycle board.
  670. if (!empty($modSettings['recycle_enable']) && $row['id_board'] != $modSettings['recycle_board'] && $row['icon'] != 'recycled')
  671. {
  672. // Check if the recycle board exists and if so get the read status.
  673. $request = $smcFunc['db_query']('', '
  674. SELECT (IFNULL(lb.id_msg, 0) >= b.id_msg_updated) AS is_seen, id_last_msg
  675. FROM {db_prefix}boards AS b
  676. LEFT JOIN {db_prefix}log_boards AS lb ON (lb.id_board = b.id_board AND lb.id_member = {int:current_member})
  677. WHERE b.id_board = {int:recycle_board}',
  678. array(
  679. 'current_member' => $user_info['id'],
  680. 'recycle_board' => $modSettings['recycle_board'],
  681. )
  682. );
  683. if ($smcFunc['db_num_rows']($request) == 0)
  684. fatal_lang_error('recycle_no_valid_board');
  685. list ($isRead, $last_board_msg) = $smcFunc['db_fetch_row']($request);
  686. $smcFunc['db_free_result']($request);
  687. // Is there an existing topic in the recycle board to group this post with?
  688. $request = $smcFunc['db_query']('', '
  689. SELECT id_topic, id_first_msg, id_last_msg
  690. FROM {db_prefix}topics
  691. WHERE id_previous_topic = {int:id_previous_topic}
  692. AND id_board = {int:recycle_board}',
  693. array(
  694. 'id_previous_topic' => $row['id_topic'],
  695. 'recycle_board' => $modSettings['recycle_board'],
  696. )
  697. );
  698. list ($id_recycle_topic, $first_topic_msg, $last_topic_msg) = $smcFunc['db_fetch_row']($request);
  699. $smcFunc['db_free_result']($request);
  700. // Insert a new topic in the recycle board if $id_recycle_topic is empty.
  701. if (empty($id_recycle_topic))
  702. $smcFunc['db_insert']('',
  703. '{db_prefix}topics',
  704. array(
  705. 'id_board' => 'int', 'id_member_started' => 'int', 'id_member_updated' => 'int', 'id_first_msg' => 'int',
  706. 'id_last_msg' => 'int', 'unapproved_posts' => 'int', 'approved' => 'int', 'id_previous_topic' => 'int',
  707. ),
  708. array(
  709. $modSettings['recycle_board'], $row['id_member'], $row['id_member'], $message,
  710. $message, 0, 1, $row['id_topic'],
  711. ),
  712. array('id_topic')
  713. );
  714. // Capture the ID of the new topic...
  715. $topicID = empty($id_recycle_topic) ? $smcFunc['db_insert_id']('{db_prefix}topics', 'id_topic') : $id_recycle_topic;
  716. // If the topic creation went successful, move the message.
  717. if ($topicID > 0)
  718. {
  719. $smcFunc['db_query']('', '
  720. UPDATE {db_prefix}messages
  721. SET
  722. id_topic = {int:id_topic},
  723. id_board = {int:recycle_board},
  724. icon = {string:recycled},
  725. approved = {int:is_approved}
  726. WHERE id_msg = {int:id_msg}',
  727. array(
  728. 'id_topic' => $topicID,
  729. 'recycle_board' => $modSettings['recycle_board'],
  730. 'id_msg' => $message,
  731. 'recycled' => 'recycled',
  732. 'is_approved' => 1,
  733. )
  734. );
  735. // Take any reported posts with us...
  736. $smcFunc['db_query']('', '
  737. UPDATE {db_prefix}log_reported
  738. SET
  739. id_topic = {int:id_topic},
  740. id_board = {int:recycle_board}
  741. WHERE id_msg = {int:id_msg}',
  742. array(
  743. 'id_topic' => $topicID,
  744. 'recycle_board' => $modSettings['recycle_board'],
  745. 'id_msg' => $message,
  746. )
  747. );
  748. // Mark recycled topic as read.
  749. if (!$user_info['is_guest'])
  750. $smcFunc['db_insert']('replace',
  751. '{db_prefix}log_topics',
  752. array('id_topic' => 'int', 'id_member' => 'int', 'id_msg' => 'int'),
  753. array($topicID, $user_info['id'], $modSettings['maxMsgID']),
  754. array('id_topic', 'id_member')
  755. );
  756. // Mark recycle board as seen, if it was marked as seen before.
  757. if (!empty($isRead) && !$user_info['is_guest'])
  758. $smcFunc['db_insert']('replace',
  759. '{db_prefix}log_boards',
  760. array('id_board' => 'int', 'id_member' => 'int', 'id_msg' => 'int'),
  761. array($modSettings['recycle_board'], $user_info['id'], $modSettings['maxMsgID']),
  762. array('id_board', 'id_member')
  763. );
  764. // Add one topic and post to the recycle bin board.
  765. $smcFunc['db_query']('', '
  766. UPDATE {db_prefix}boards
  767. SET
  768. num_topics = num_topics + {int:num_topics_inc},
  769. num_posts = num_posts + 1' .
  770. ($message > $last_board_msg ? ', id_last_msg = {int:id_merged_msg}' : '') . '
  771. WHERE id_board = {int:recycle_board}',
  772. array(
  773. 'num_topics_inc' => empty($id_recycle_topic) ? 1 : 0,
  774. 'recycle_board' => $modSettings['recycle_board'],
  775. 'id_merged_msg' => $message,
  776. )
  777. );
  778. // Lets increase the num_replies, and the first/last message ID as appropriate.
  779. if (!empty($id_recycle_topic))
  780. $smcFunc['db_query']('', '
  781. UPDATE {db_prefix}topics
  782. SET num_replies = num_replies + 1' .
  783. ($message > $last_topic_msg ? ', id_last_msg = {int:id_merged_msg}' : '') .
  784. ($message < $first_topic_msg ? ', id_first_msg = {int:id_merged_msg}' : '') . '
  785. WHERE id_topic = {int:id_recycle_topic}',
  786. array(
  787. 'id_recycle_topic' => $id_recycle_topic,
  788. 'id_merged_msg' => $message,
  789. )
  790. );
  791. // Make sure this message isn't getting deleted later on.
  792. $recycle = true;
  793. // Make sure we update the search subject index.
  794. updateStats('subject', $topicID, $row['subject']);
  795. }
  796. // If it wasn't approved don't keep it in the queue.
  797. if (!$row['approved'])
  798. $smcFunc['db_query']('', '
  799. DELETE FROM {db_prefix}approval_queue
  800. WHERE id_msg = {int:id_msg}
  801. AND id_attach = {int:id_attach}',
  802. array(
  803. 'id_msg' => $message,
  804. 'id_attach' => 0,
  805. )
  806. );
  807. }
  808. $smcFunc['db_query']('', '
  809. UPDATE {db_prefix}boards
  810. SET ' . ($row['approved'] ? '
  811. num_posts = CASE WHEN num_posts = {int:no_posts} THEN 0 ELSE num_posts - 1 END' : '
  812. unapproved_posts = CASE WHEN unapproved_posts = {int:no_unapproved} THEN 0 ELSE unapproved_posts - 1 END') . '
  813. WHERE id_board = {int:id_board}',
  814. array(
  815. 'no_posts' => 0,
  816. 'no_unapproved' => 0,
  817. 'id_board' => $row['id_board'],
  818. )
  819. );
  820. // If the poster was registered and the board this message was on incremented
  821. // the member's posts when it was posted, decrease his or her post count.
  822. if (!empty($row['id_member']) && $decreasePostCount && empty($row['count_posts']) && $row['approved'])
  823. updateMemberData($row['id_member'], array('posts' => '-'));
  824. // Only remove posts if they're not recycled.
  825. if (!$recycle)
  826. {
  827. // Remove the message!
  828. $smcFunc['db_query']('', '
  829. DELETE FROM {db_prefix}messages
  830. WHERE id_msg = {int:id_msg}',
  831. array(
  832. 'id_msg' => $message,
  833. )
  834. );
  835. if (!empty($modSettings['search_custom_index_config']))
  836. {
  837. $customIndexSettings = unserialize($modSettings['search_custom_index_config']);
  838. $words = text2words($row['body'], $customIndexSettings['bytes_per_word'], true);
  839. if (!empty($words))
  840. $smcFunc['db_query']('', '
  841. DELETE FROM {db_prefix}log_search_words
  842. WHERE id_word IN ({array_int:word_list})
  843. AND id_msg = {int:id_msg}',
  844. array(
  845. 'word_list' => $words,
  846. 'id_msg' => $message,
  847. )
  848. );
  849. }
  850. // Delete attachment(s) if they exist.
  851. require_once($sourcedir . '/ManageAttachments.php');
  852. $attachmentQuery = array(
  853. 'attachment_type' => 0,
  854. 'id_msg' => $message,
  855. );
  856. removeAttachments($attachmentQuery);
  857. }
  858. // Update the pesky statistics.
  859. updateStats('message');
  860. updateStats('topic');
  861. updateSettings(array(
  862. 'calendar_updated' => time(),
  863. ));
  864. // And now to update the last message of each board we messed with.
  865. require_once($sourcedir . '/Subs-Post.php');
  866. if ($recycle)
  867. updateLastMessages(array($row['id_board'], $modSettings['recycle_board']));
  868. else
  869. updateLastMessages($row['id_board']);
  870. return false;
  871. }
  872. function RestoreTopic()
  873. {
  874. global $context, $smcFunc, $modSettings, $sourcedir;
  875. // Check session.
  876. checkSession('get');
  877. // Is recycled board enabled?
  878. if (empty($modSettings['recycle_enable']))
  879. fatal_lang_error('restored_disabled', 'critical');
  880. // Can we be in here?
  881. isAllowedTo('move_any', $modSettings['recycle_board']);
  882. // We need this file.
  883. require_once($sourcedir . '/MoveTopic.php');
  884. $unfound_messages = array();
  885. $topics_to_restore = array();
  886. // Restoring messages?
  887. if (!empty($_REQUEST['msgs']))
  888. {
  889. $msgs = explode(',', $_REQUEST['msgs']);
  890. foreach ($msgs as $k => $msg)
  891. $msgs[$k] = (int) $msg;
  892. // Get the id_previous_board and id_previous_topic.
  893. $request = $smcFunc['db_query']('', '
  894. SELECT m.id_topic, m.id_msg, m.id_board, m.subject, m.id_member, t.id_previous_board, t.id_previous_topic,
  895. t.id_first_msg, b.count_posts, IFNULL(pt.id_board, 0) AS possible_prev_board
  896. FROM {db_prefix}messages AS m
  897. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
  898. INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)
  899. LEFT JOIN {db_prefix}topics AS pt ON (pt.id_topic = t.id_previous_topic)
  900. WHERE m.id_msg IN ({array_int:messages})',
  901. array(
  902. 'messages' => $msgs,
  903. )
  904. );
  905. $actioned_messages = array();
  906. $previous_topics = array();
  907. while ($row = $smcFunc['db_fetch_assoc']($request))
  908. {
  909. // Restoring the first post means topic.
  910. if ($row['id_msg'] == $row['id_first_msg'] && $row['id_previous_topic'] == $row['id_topic'])
  911. {
  912. $topics_to_restore[] = $row['id_topic'];
  913. continue;
  914. }
  915. // Don't know where it's going?
  916. if (empty($row['id_previous_topic']))
  917. {
  918. $unfound_messages[$row['id_msg']] = $row['subject'];
  919. continue;
  920. }
  921. $previous_topics[] = $row['id_previous_topic'];
  922. if (empty($actioned_messages[$row['id_previous_topic']]))
  923. $actioned_messages[$row['id_previous_topic']] = array(
  924. 'msgs' => array(),
  925. 'count_posts' => $row['count_posts'],
  926. 'subject' => $row['subject'],
  927. 'previous_board' => $row['id_previous_board'],
  928. 'possible_prev_board' => $row['possible_prev_board'],
  929. 'current_topic' => $row['id_topic'],
  930. 'current_board' => $row['id_board'],
  931. 'members' => array(),
  932. );
  933. $actioned_messages[$row['id_previous_topic']]['msgs'][$row['id_msg']] = $row['subject'];
  934. if ($row['id_member'])
  935. $actioned_messages[$row['id_previous_topic']]['members'][] = $row['id_member'];
  936. }
  937. $smcFunc['db_free_result']($request);
  938. // Check for topics we are going to fully restore.
  939. foreach ($actioned_messages as $topic => $data)
  940. if (in_array($topic, $topics_to_restore))
  941. unset($actioned_messages[$topic]);
  942. // Load any previous topics to check they exist.
  943. if (!empty($previous_topics))
  944. {
  945. $request = $smcFunc['db_query']('', '
  946. SELECT t.id_topic, t.id_board, m.subject
  947. FROM {db_prefix}topics AS t
  948. INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
  949. WHERE t.id_topic IN ({array_int:previous_topics})',
  950. array(
  951. 'previous_topics' => $previous_topics,
  952. )
  953. );
  954. $previous_topics = array();
  955. while ($row = $smcFunc['db_fetch_assoc']($request))
  956. $previous_topics[$row['id_topic']] = array(
  957. 'board' => $row['id_board'],
  958. 'subject' => $row['subject'],
  959. );
  960. $smcFunc['db_free_result']($request);
  961. }
  962. // Restore each topic.
  963. $messages = array();
  964. foreach ($actioned_messages as $topic => $data)
  965. {
  966. // If we have topics we are going to restore the whole lot ignore them.
  967. if (in_array($topic, $topics_to_restore))
  968. {
  969. unset($actioned_messages[$topic]);
  970. continue;
  971. }
  972. // Move the posts back then!
  973. if (isset($previous_topics[$topic]))
  974. {
  975. mergePosts(array_keys($data['msgs']), $data['current_topic'], $topic);
  976. // Log em.
  977. logAction('restore_posts', array('topic' => $topic, 'subject' => $previous_topics[$topic]['subject'], 'board' => empty($data['previous_board']) ? $data['possible_prev_board'] : $data['previous_board']));
  978. $messages = array_merge(array_keys($data['msgs']), $messages);
  979. }
  980. else
  981. {
  982. foreach ($data['msgs'] as $msg)
  983. $unfound_messages[$msg['id']] = $msg['subject'];
  984. }
  985. }
  986. // Put the icons back.
  987. if (!empty($messages))
  988. $smcFunc['db_query']('', '
  989. UPDATE {db_prefix}messages
  990. SET icon = {string:icon}
  991. WHERE id_msg IN ({array_int:messages})',
  992. array(
  993. 'icon' => 'xx',
  994. 'messages' => $messages,
  995. )
  996. );
  997. }
  998. // Now any topics?
  999. if (!empty($_REQUEST['topics']))
  1000. {
  1001. $topics = explode(',', $_REQUEST['topics']);
  1002. foreach ($topics as $key => $id)
  1003. $topics_to_restore[] = (int) $id;
  1004. }
  1005. if (!empty($topics_to_restore))
  1006. {
  1007. // Lets get the data for these topics.
  1008. $request = $smcFunc['db_query']('', '
  1009. SELECT t.id_topic, t.id_previous_board, t.id_board, t.id_first_msg, m.subject
  1010. FROM {db_prefix}topics AS t
  1011. INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
  1012. WHERE t.id_topic IN ({array_int:topics})',
  1013. array(
  1014. 'topics' => $topics_to_restore,
  1015. )
  1016. );
  1017. while ($row = $smcFunc['db_fetch_assoc']($request))
  1018. {
  1019. // We can only restore if the previous board is set.
  1020. if (empty($row['id_previous_board']))
  1021. {
  1022. $unfound_messages[$row['id_first_msg']] = $row['subject'];
  1023. continue;
  1024. }
  1025. // Ok we got here so me move them from here to there.
  1026. moveTopics($row['id_topic'], $row['id_previous_board']);
  1027. // Lets remove the recycled icon.
  1028. $smcFunc['db_query']('', '
  1029. UPDATE {db_prefix}messages
  1030. SET icon = {string:icon}
  1031. WHERE id_topic = {int:id_topic}',
  1032. array(
  1033. 'icon' => 'xx',
  1034. 'id_topic' => $row['id_topic'],
  1035. )
  1036. );
  1037. // Lets see if the board that we are returning to has post count enabled.
  1038. $request2 = $smcFunc['db_query']('', '
  1039. SELECT count_posts
  1040. FROM {db_prefix}boards
  1041. WHERE id_board = {int:board}',
  1042. array(
  1043. 'board' => $row['id_previous_board'],
  1044. )
  1045. );
  1046. list ($count_posts) = $smcFunc['db_fetch_row']($request2);
  1047. $smcFunc['db_free_result']($request2);
  1048. if (empty($count_posts))
  1049. {
  1050. // Lets get the members that need their post count restored.
  1051. $request2 = $smcFunc['db_query']('', '
  1052. SELECT id_member, COUNT(id_msg) AS post_count
  1053. FROM {db_prefix}messages
  1054. WHERE id_topic = {int:topic}
  1055. AND approved = {int:is_approved}
  1056. GROUP BY id_member',
  1057. array(
  1058. 'topic' => $row['id_topic'],
  1059. 'is_approved' => 1,
  1060. )
  1061. );
  1062. while ($member = $smcFunc['db_fetch_assoc']($request2))
  1063. updateMemberData($member['id_member'], array('posts' => 'posts + ' . $member['post_count']));
  1064. $smcFunc['db_free_result']($request2);
  1065. }
  1066. // Log it.
  1067. logAction('restore_topic', array('topic' => $row['id_topic'], 'board' => $row['id_board'], 'board_to' => $row['id_previous_board']));
  1068. }
  1069. $smcFunc['db_free_result']($request);
  1070. }
  1071. // Didn't find some things?
  1072. if (!empty($unfound_messages))
  1073. fatal_lang_error('restore_not_found', false, array(implode('<br />', $unfound_messages)));
  1074. // Just send them to the index if they get here.
  1075. redirectexit();
  1076. }
  1077. // Take a load of messages from one place and stick them in a topic.
  1078. function mergePosts($msgs = array(), $from_topic, $target_topic)
  1079. {
  1080. global $context, $smcFunc, $modSettings, $sourcedir;
  1081. //!!! This really needs to be rewritten to take a load of messages from ANY topic, it's also inefficient.
  1082. // Is it an array?
  1083. if (!is_array($msgs))
  1084. $msgs = array($msgs);
  1085. // Lets make sure they are int.
  1086. foreach ($msgs as $key => $msg)
  1087. $msgs[$key] = (int) $msg;
  1088. // Get the source information.
  1089. $request = $smcFunc['db_query']('', '
  1090. SELECT t.id_board, t.id_first_msg, t.num_replies, t.unapproved_posts
  1091. FROM {db_prefix}topics AS t
  1092. INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
  1093. WHERE t.id_topic = {int:from_topic}',
  1094. array(
  1095. 'from_topic' => $from_topic,
  1096. )
  1097. );
  1098. list ($from_board, $from_first_msg, $from_replies, $from_unapproved_posts) = $smcFunc['db_fetch_row']($request);
  1099. $smcFunc['db_free_result']($request);
  1100. // Get some target topic and board stats.
  1101. $request = $smcFunc['db_query']('', '
  1102. SELECT t.id_board, t.id_first_msg, t.num_replies, t.unapproved_posts, b.count_posts
  1103. FROM {db_prefix}topics AS t
  1104. INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
  1105. WHERE t.id_topic = {int:target_topic}',
  1106. array(
  1107. 'target_topic' => $target_topic,
  1108. )
  1109. );
  1110. list ($target_board, $target_first_msg, $target_replies, $target_unapproved_posts, $count_posts) = $smcFunc['db_fetch_row']($request);
  1111. $smcFunc['db_free_result']($request);
  1112. // Lets see if the board that we are returning to has post count enabled.
  1113. if (empty($count_posts))
  1114. {
  1115. // Lets get the members that need their post count restored.
  1116. $request = $smcFunc['db_query']('', '
  1117. SELECT id_member
  1118. FROM {db_prefix}messages
  1119. WHERE id_msg IN ({array_int:messages})
  1120. AND approved = {int:is_approved}',
  1121. array(
  1122. 'messages' => $msgs,
  1123. 'is_approved' => 1,
  1124. )
  1125. );
  1126. while ($row = $smcFunc['db_fetch_assoc']($request))
  1127. updateMemberData($row['id_member'], array('posts' => '+'));
  1128. }
  1129. // Time to move the messages.
  1130. $smcFunc['db_query']('', '
  1131. UPDATE {db_prefix}messages
  1132. SET
  1133. id_topic = {int:target_topic},
  1134. id_board = {int:target_board},
  1135. icon = {string:icon}
  1136. WHERE id_msg IN({array_int:msgs})',
  1137. array(
  1138. 'target_topic' => $target_topic,
  1139. 'target_board' => $target_board,
  1140. 'icon' => $target_board == $modSettings['recycle_board'] ? 'recycled' : 'xx',
  1141. 'msgs' => $msgs,
  1142. )
  1143. );
  1144. // Fix the id_first_msg and id_last_msg for the target topic.
  1145. $target_topic_data = array(
  1146. 'num_replies' => 0,
  1147. 'unapproved_posts' => 0,
  1148. 'id_first_msg' => 9999999999,
  1149. );
  1150. $request = $smcFunc['db_query']('', '
  1151. SELECT MIN(id_msg) AS id_first_msg, MAX(id_msg) AS id_last_msg, COUNT(*) AS message_count, approved
  1152. FROM {db_prefix}messages
  1153. WHERE id_topic = {int:target_topic}
  1154. GROUP BY id_topic, approved
  1155. ORDER BY approved ASC
  1156. LIMIT 2',
  1157. array(
  1158. 'target_topic' => $target_topic,
  1159. )
  1160. );
  1161. while ($row = $smcFunc['db_fetch_assoc']($request))
  1162. {
  1163. if ($row['id_first_msg'] < $target_topic_data['id_first_msg'])
  1164. $target_topic_data['id_first_msg'] = $row['id_first_msg'];
  1165. $target_topic_data['id_last_msg'] = $row['id_last_msg'];
  1166. if (!$row['approved'])
  1167. $target_topic_data['unapproved_posts'] = $row['message_count'];
  1168. else
  1169. $target_topic_data['num_replies'] = max(0, $row['message_count'] - 1);
  1170. }
  1171. $smcFunc['db_free_result']($request);
  1172. // We have a new post count for the board.
  1173. $smcFunc['db_query']('', '
  1174. UPDATE {db_prefix}boards
  1175. SET
  1176. num_posts = num_posts + {int:diff_replies},
  1177. unapproved_posts = unapproved_posts + {int:diff_unapproved_posts}
  1178. WHERE id_board = {int:target_board}',
  1179. array(
  1180. '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.
  1181. 'diff_unapproved_posts' => $target_topic_data['unapproved_posts'] - $target_unapproved_posts,
  1182. 'target_board' => $target_board,
  1183. )
  1184. );
  1185. // In some cases we merged the only post in a topic so the topic data is left behind in the topic table.
  1186. $request = $smcFunc['db_query']('', '
  1187. SELECT id_topic
  1188. FROM {db_prefix}messages
  1189. WHERE id_topic = {int:from_topic}',
  1190. array(
  1191. 'from_topic' => $from_topic,
  1192. )
  1193. );
  1194. // Remove the topic if it doesn't have any messages.
  1195. $topic_exists = true;
  1196. if ($smcFunc['db_num_rows']($request) == 0)
  1197. {
  1198. removeTopics($from_topic, false, true);
  1199. $topic_exists = false;
  1200. }
  1201. $smcFunc['db_free_result']($request);
  1202. // Recycled topic.
  1203. if ($topic_exists == true)
  1204. {
  1205. // Fix the id_first_msg and id_last_msg for the source topic.
  1206. $source_topic_data = array(
  1207. 'num_replies' => 0,
  1208. 'unapproved_posts' => 0,
  1209. 'id_first_msg' => 9999999999,
  1210. );
  1211. $request = $smcFunc['db_query']('', '
  1212. SELECT MIN(id_msg) AS id_first_msg, MAX(id_msg) AS id_last_msg, COUNT(*) AS message_count, approved, subject
  1213. FROM {db_prefix}messages
  1214. WHERE id_topic = {int:from_topic}
  1215. GROUP BY id_topic, approved
  1216. ORDER BY approved ASC
  1217. LIMIT 2',
  1218. array(
  1219. 'from_topic' => $from_topic,
  1220. )
  1221. );
  1222. while ($row = $smcFunc['db_fetch_assoc']($request))
  1223. {
  1224. if ($row['id_first_msg'] < $source_topic_data['id_first_msg'])
  1225. $source_topic_data['id_first_msg'] = $row['id_first_msg'];
  1226. $source_topic_data['id_last_msg'] = $row['id_last_msg'];
  1227. if (!$row['approved'])
  1228. $source_topic_data['unapproved_posts'] = $row['message_count'];
  1229. else
  1230. $source_topic_data['num_replies'] = max(0, $row['message_count'] - 1);
  1231. }
  1232. $smcFunc['db_free_result']($request);
  1233. // Update the topic details for the source topic.
  1234. $smcFunc['db_query']('', '
  1235. UPDATE {db_prefix}topics
  1236. SET
  1237. id_first_msg = {int:id_first_msg},
  1238. id_last_msg = {int:id_last_msg},
  1239. num_replies = {int:num_replies},
  1240. unapproved_posts = {int:unapproved_posts}
  1241. WHERE id_topic = {int:from_topic}',
  1242. array(
  1243. 'id_first_msg' => $source_topic_data['id_first_msg'],
  1244. 'id_last_msg' => $source_topic_data['id_last_msg'],
  1245. 'num_replies' => $source_topic_data['num_replies'],
  1246. 'unapproved_posts' => $source_topic_data['unapproved_posts'],
  1247. 'from_topic' => $from_topic,
  1248. )
  1249. );
  1250. // We have a new post count for the source board.
  1251. $smcFunc['db_query']('', '
  1252. UPDATE {db_prefix}boards
  1253. SET
  1254. num_posts = num_posts + {int:diff_replies},
  1255. unapproved_posts = unapproved_posts + {int:diff_unapproved_posts}
  1256. WHERE id_board = {int:from_board}',
  1257. array(
  1258. '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.
  1259. 'diff_unapproved_posts' => $source_topic_data['unapproved_posts'] - $from_unapproved_posts,
  1260. 'from_board' => $from_board,
  1261. )
  1262. );
  1263. }
  1264. // Finally get around to updating the destination topic, now all indexes etc on the source are fixed.
  1265. $smcFunc['db_query']('', '
  1266. UPDATE {db_prefix}topics
  1267. SET
  1268. id_first_msg = {int:id_first_msg},
  1269. id_last_msg = {int:id_last_msg},
  1270. num_replies = {int:num_replies},
  1271. unapproved_posts = {int:unapproved_posts}
  1272. WHERE id_topic = {int:target_topic}',
  1273. array(
  1274. 'id_first_msg' => $target_topic_data['id_first_msg'],
  1275. 'id_last_msg' => $target_topic_data['id_last_msg'],
  1276. 'num_replies' => $target_topic_data['num_replies'],
  1277. 'unapproved_posts' => $target_topic_data['unapproved_posts'],
  1278. 'target_topic' => $target_topic,
  1279. )
  1280. );
  1281. // Need it to update some stats.
  1282. require_once($sourcedir . '/Subs-Post.php');
  1283. // Update stats.
  1284. updateStats('topic');
  1285. updateStats('message');
  1286. // Subject cache?
  1287. $cache_updates = array();
  1288. if ($target_first_msg != $target_topic_data['id_first_msg'])
  1289. $cache_updates[] = $target_topic_data['id_first_msg'];
  1290. if (!empty($source_topic_data['id_first_msg']) && $from_first_msg != $source_topic_data['id_first_msg'])
  1291. $cache_updates[] = $source_topic_data['id_first_msg'];
  1292. if (!empty($cache_updates))
  1293. {
  1294. $request = $smcFunc['db_query']('', '
  1295. SELECT id_topic, subject
  1296. FROM {db_prefix}messages
  1297. WHERE id_msg IN ({array_int:first_messages})',
  1298. array(
  1299. 'first_messages' => $cache_updates,
  1300. )
  1301. );
  1302. while ($row = $smcFunc['db_fetch_assoc']($request))
  1303. updateStats('subject', $row['id_topic'], $row['subject']);
  1304. $smcFunc['db_free_result']($request);
  1305. }
  1306. updateLastMessages(array($from_board, $target_board));
  1307. }
  1308. function removeDeleteConcurrence()
  1309. {
  1310. global $modSettings, $board, $topic, $smcFunc, $scripturl, $context;
  1311. // No recycle no need to go further
  1312. if (empty($modSettings['recycle_enable']) || empty($modSettings['recycle_board']))
  1313. return false;
  1314. // If it's confirmed go on and delete (from recycle)
  1315. if (isset($_GET['confirm_delete']))
  1316. return true;
  1317. if (empty($board))
  1318. return false;
  1319. if ($modSettings['recycle_board'] != $board)
  1320. return true;
  1321. elseif (isset($_REQUEST['msg']))
  1322. $confirm_url = $scripturl . '?action=deletemsg;confirm_delete;topic=' . $context['current_topic'] . '.0;msg=' . $_REQUEST['msg'] . ';' . $context['session_var'] . '=' . $context['session_id'];
  1323. else
  1324. $confirm_url = $scripturl . '?action=removetopic2;confirm_delete;topic=' . $context['current_topic'] . '.0;' . $context['session_var'] . '=' . $context['session_id'];
  1325. fatal_lang_error('post_already_deleted', false, array($confirm_url));
  1326. }
  1327. ?>