PageRenderTime 25ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/forums/delete.php

https://github.com/AndyRixon/LayerBulletin
PHP | 344 lines | 201 code | 71 blank | 72 comment | 23 complexity | b5e36834742700e5cf3fa32be073c26c MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------------
  4. | LayerBulletin
  5. | ========================================
  6. | By The LayerBulletin team
  7. | Released under the Artistic License 2.0
  8. | http://layerbulletin.com/
  9. | ========================================
  10. |+--------------------------------------------------------------------------
  11. | delete.php - deletes posts, topics and relevant polls & attachments
  12. */
  13. if (!defined('LB_RUN'))
  14. {
  15. exit('<h1>ACCESS DENIED</h1>You cannot access this file directly.');
  16. }
  17. $post = (int) $_POST['post_delete_id'];
  18. if ($can_delete_others_posts != 1)
  19. {
  20. /*
  21. Check whether this is their post
  22. */
  23. $query = mysql_query('SELECT member FROM ' . $db_prefix . 'posts WHERE id = ' . $post);
  24. $row = mysql_fetch_assoc($query);
  25. if ($row['member'] != $my_id || $can_delete_own_posts != 1)
  26. {
  27. lb_redirect("index.php?page=error&error=4","error/4");
  28. }
  29. }
  30. if ($_POST['post_delete'] == $lang['button_delete'] && tokenCheck('topic_post_delete', $post))
  31. {
  32. $query21 = '
  33. SELECT p.title, p2.title AS topic_title, p.description, p.content, p.member, p.address, p.topic_id, p.forum_id, p.time, p.last_post_time
  34. FROM ' . $db_prefix . 'posts p
  35. INNER JOIN ' . $db_prefix . 'posts p2
  36. ON p.topic_id = p2.topic_id AND p2.title != ""
  37. WHERE p.id = ' . $post;
  38. $result21 = mysql_query($query21) or die("delete.php - Error in query: $query21");
  39. while ($results21 = mysql_fetch_array($result21))
  40. {
  41. $title = $results21['title'];
  42. $topic_title = $results21['topic_title'];
  43. $desc = $results21['description'];
  44. $content = $results21['content'];
  45. $member = $results21['member'];
  46. $addr = $results21['address'];
  47. $topic_id = $results21['topic_id'];
  48. $forum_id = $results21['forum_id'];
  49. $time = $results21['time'];
  50. $last_post_time = $results21['last_post_time'];
  51. }
  52. /*
  53. If the trashcan forum is enabled then we don't delete the post.
  54. Instead, it gets moved to the trashcan
  55. (unless of course, it's already in the trashcan; then it goes for good)...
  56. */
  57. if ($trashcan_enabled && $forum_id != $trashcan_forum)
  58. {
  59. /*
  60. So, are we dealing with a topic or single post?
  61. */
  62. if ($title != '')
  63. {
  64. # Move & lock it...
  65. mysql_query('
  66. UPDATE ' . $db_prefix . 'posts
  67. SET forum_id = ' . $trashcan_forum . ', original_forum_id = ' . $forum_id . ', trashcan_time = ' . time() . ', locked = 1
  68. WHERE title != "" AND topic_id = ' . $topic_id
  69. );
  70. /*
  71. If any posts from this topic were previously deleted, merge them back
  72. */
  73. mysql_query('
  74. UPDATE ' . $db_prefix . 'posts
  75. SET topic_id = ' . $topic_id . ', original_topic_id = 0, forum_id = ' . $trashcan_forum . ', title = ""
  76. WHERE original_topic_id = ' . $topic_id
  77. );
  78. mysql_query('
  79. UPDATE ' . $db_prefix . 'posts
  80. SET forum_id = ' . $trashcan_forum . ', original_forum_id = ' . $forum_id . '
  81. WHERE topic_id = ' . $topic_id
  82. );
  83. }
  84. else
  85. {
  86. /*
  87. Deleted posts will be grouped into topics to make them easier to restore.
  88. First see if another post from this topic is already present.
  89. */
  90. $query = mysql_query('
  91. SELECT topic_id, forum_id
  92. FROM ' . $db_prefix . 'posts
  93. WHERE forum_id = ' . $trashcan_forum . ' AND original_topic_id = ' . $topic_id
  94. );
  95. $row = mysql_fetch_assoc($query);
  96. if (!empty($row))
  97. {
  98. # Topic already exists, simply move the post
  99. mysql_query('
  100. UPDATE ' . $db_prefix . 'posts
  101. SET
  102. original_topic_id = ' . $topic_id . ',
  103. topic_id = ' . $row['topic_id'] . ',
  104. original_forum_id = ' . $forum_id . ',
  105. forum_id = ' . $trashcan_forum . '
  106. WHERE id = ' . $post
  107. );
  108. }
  109. else
  110. {
  111. /*
  112. A topic for this post isn't present in the trashcan.
  113. Create a fake topic to group deleted replies together.
  114. First, find the id to be given to the new topic:
  115. */
  116. $query = mysql_query('SELECT topic_id FROM ' . $db_prefix . 'posts WHERE title != "" ORDER BY topic_id DESC LIMIT 1');
  117. $row = mysql_fetch_assoc($query);
  118. $new_topic_id = $row['topic_id'] + 1;
  119. /*
  120. Now create a new topic with these details
  121. */
  122. mysql_query('
  123. INSERT INTO ' . $db_prefix . 'posts
  124. (
  125. title, description, content, member, address, time, topic_id, original_topic_id, forum_id, original_forum_id, trashcan_time,
  126. last_post_time, locked
  127. )
  128. VALUES
  129. (
  130. "' . $topic_title . '",
  131. "' . $desc . '",
  132. "' . $content . '",
  133. ' . $member . ',
  134. "' . $addr . '",
  135. ' . $time . ',
  136. ' . $new_topic_id . ',
  137. ' . $topic_id . ',
  138. ' . $trashcan_forum . ',
  139. ' . $forum_id . ',
  140. ' . time() . ',
  141. ' . $last_post_time . ',
  142. 1
  143. )
  144. ');
  145. /*
  146. Move attachments to the new post
  147. */
  148. $new_id = mysql_insert_id();
  149. mysql_query('UPDATE ' . $db_prefix . 'attachments SET postid = ' . $new_id . ', topicid = ' . $new_topic_id . ' WHERE postid = ' . $post);
  150. /*
  151. Now delete the orignal post.
  152. */
  153. mysql_query('DELETE FROM ' . $db_prefix . 'posts WHERE id = ' . $post);
  154. }
  155. }
  156. /*
  157. Run auto-cache to show updated information.
  158. */
  159. # Auto-cache overwrites $topic_id, so use a different name
  160. $topic = $topic_id;
  161. include 'scripts/php/auto_cache.php';
  162. /*
  163. And redirect the user back to the topic.
  164. */
  165. template_hook('forums/delete.template.php', 'form_2');
  166. lb_redirect('index.php?topic=' . $topic, 'topic/' . $topic_title . '-' . $topic);
  167. }
  168. else
  169. {
  170. if ($title != '')
  171. {
  172. $query212 = "select ID from {$db_prefix}posts WHERE TOPIC_ID='$topic_id'";
  173. $result212 = mysql_query($query212) or die("delete.php - Error in query: $query212");
  174. while ($results212 = mysql_fetch_array($result212))
  175. {
  176. $remove_id = $results212['ID'];
  177. /*
  178. Delete the attachments
  179. */
  180. $query2121 = "select FILENAME from {$db_prefix}attachments WHERE POSTID='$remove_id'";
  181. $result2121 = mysql_query($query2121) or die("delete.php - Error in query: $query2121");
  182. while ($results2121 = mysql_fetch_array($result2121))
  183. {
  184. unlink($lb_root . 'uploads/attachments/' . $results2121['FILENAME']);
  185. unlink($lb_root . 'uploads/attachments/t_' . $results2121['FILENAME']);
  186. mysql_query("DELETE FROM {$db_prefix}attachments WHERE postid ='$remove_id'");
  187. }
  188. /*
  189. If it was in the moderation queue, remove it..
  190. */
  191. mysql_query("DELETE FROM {$db_prefix}moderate WHERE postid='$remove_id'");
  192. }
  193. /*
  194. Remove the posts and any edits made to them
  195. */
  196. mysql_query('DELETE FROM ' . $db_prefix . 'posts WHERE topic_id = ' . $topic_id);
  197. mysql_query('DELETE FROM ' . $db_prefix . 'posts_edit WHERE topic = ' . $topic_id);
  198. /*
  199. Poll present? Remove that also...
  200. */
  201. mysql_query('
  202. DELETE
  203. p.*, pv.*
  204. FROM
  205. ' . $db_prefix . 'polls p
  206. INNER JOIN ' . $db_prefix . 'polls_votes pv
  207. ON p.id = pv.poll_id
  208. WHERE
  209. p.topic_id = ' . $topic_id
  210. );
  211. /*
  212. Auto-Cache
  213. */
  214. include 'scripts/php/auto_cache.php';
  215. /*
  216. Finish off & redirect
  217. */
  218. $forum_title = forum_title($forum_id);
  219. template_hook('forums/delete.template.php', 'form_1');
  220. lb_redirect('index.php?forum=' . $forum_id, 'forum/' . $forum_title . '-' . $redirect);
  221. }
  222. else
  223. {
  224. $post=escape_string($_GET['post']);
  225. mysql_query("DELETE FROM {$db_prefix}moderate WHERE postid ='$post'");
  226. // Replace the last reply in the database...
  227. $query21 = "select TOPIC_ID from {$db_prefix}posts WHERE ID='$post'" ;
  228. $result21 = mysql_query($query21) or die("delete.php - Error in query: $query21") ;
  229. $topic_id = mysql_result($result21, 0);
  230. mysql_query("DELETE FROM {$db_prefix}posts WHERE id ='$post'");
  231. $query2 = "select ID, TIME, FORUM_ID, TOPIC_ID from {$db_prefix}posts WHERE TOPIC_ID='$topic_id' ORDER BY ID desc LIMIT 1" ;
  232. $result2 = mysql_query($query2) or die("newpost.php - Error in query: $query2") ;
  233. while ($results2 = mysql_fetch_array($result2)){
  234. $post_id = $results2['ID'];
  235. $post_time = $results2['TIME'];
  236. $post_forum = $results2['FORUM_ID'];
  237. $post_topic = $results2['TOPIC_ID'];
  238. }
  239. $query2 = "select TITLE from {$db_prefix}posts WHERE TITLE!='' AND TOPIC_ID='$post_topic'" ;
  240. $result2 = mysql_query($query2) or die("newpost.php - Error in query: $query2") ;
  241. $post_title = mysql_result($result2, 0);
  242. $query21 = "select TIME from {$db_prefix}posts WHERE TOPIC_ID='$topic_id' ORDER BY ID desc" ;
  243. $result21 = mysql_query($query21) or die("delete.php - Error in query: $query21") ;
  244. $time = mysql_result($result21, 0);
  245. mysql_query("UPDATE {$db_prefix}posts SET last_post_time='$time' WHERE topic_id = '$topic_id' AND TITLE!=''");
  246. $query2121 = "select FILENAME from {$db_prefix}attachments WHERE POSTID='$post'" ;
  247. $result2121 = mysql_query($query2121) or die("delete.php - Error in query: $query2121") ;
  248. while ($results2121 = mysql_fetch_array($result2121)){
  249. $filename = $results2121['FILENAME'];
  250. foreach (glob("uploads/attachments/$filename") as $filename_original) {
  251. unlink($filename_original);
  252. }
  253. foreach (glob("uploads/attachments/t_$filename") as $filename_thumb) {
  254. unlink($filename_thumb);
  255. }
  256. mysql_query("DELETE FROM {$db_prefix}attachments WHERE postid ='$post'");
  257. }
  258. $redirect=$topic_id;
  259. // perform auto-cache
  260. include "scripts/php/auto_cache.php";
  261. template_hook("forums/delete.template.php", "form_2");
  262. $topic_title = topic_title($redirect);
  263. lb_redirect("index.php?topic=$redirect","topic/$topic_title-$redirect");
  264. }
  265. }
  266. }
  267. else
  268. {
  269. lb_redirect('index.php?page=error&error=28', 'error/28');
  270. }
  271. ?>