query('SELECT poster, poster_id, message, smilies, topic_id FROM '.$db->prefix.'posts WHERE id='.$id) or error('Unable to fetch post info', __FILE__, __LINE__, $db->error()); if (!$db->num_rows($result)) message($lang_common['Bad request']); $cur_post = $db->fetch_assoc($result); // Determine whether this post is the "topic post" or not $result = $db->query('SELECT id FROM '.$db->prefix.'posts WHERE topic_id='.$cur_post['topic_id'].' ORDER BY posted LIMIT 1') or error('Unable to fetch post info', __FILE__, __LINE__, $db->error()); $topicpost_id = $db->result($result, 0); $is_topicpost = ($id == $topicpost_id) ? true : false; // Fetch some info from the topic in which the post is located $result = $db->query('SELECT subject, closed, forum_id FROM '.$db->prefix.'topics WHERE id='.$cur_post['topic_id']) or error('Unable to fetch topic info', __FILE__, __LINE__, $db->error()); list($subject, $topic_closed, $forum_id) = $db->fetch_row($result); $forum_closed = '0'; $is_admmod = is_admmod($forum_id, $forum_closed, $admmod_only); // If the current user isn't an administrator or a moderator of this forum if (!$is_admmod) { if ($admmod_only == '1' && $cur_user['status'] < 1 || $topic_closed == '1' || $forum_closed == '1' || $permissions['users_del_post'] == '0' && $cur_user['status'] < 1 || $is_topicpost && $permissions['users_del_topic'] == '0' && $cur_user['status'] < 1 || $cur_post['poster_id'] != $cur_user['id']) message($lang_common['No permission']); } if (isset($_POST['delete'])) { if ($is_admmod) confirm_referer('delete.php'); require 'include/searchidx.php'; // If it isn't the topic post if (!$is_topicpost) { $result = $db->query('SELECT id, poster, posted FROM '.$db->prefix.'posts WHERE topic_id='.$cur_post['topic_id'].' ORDER BY posted DESC LIMIT 2') or error('Unable to fetch post info', __FILE__, __LINE__, $db->error()); list($last_id, ,) = $db->fetch_row($result); list($second_last_id, $second_poster, $second_posted) = $db->fetch_row($result); // Delete the post (start transaction) $db->query('DELETE FROM '.$db->prefix.'posts WHERE id='.$id, PUN_TRANS_START) or error('Unable to delete post', __FILE__, __LINE__, $db->error()); strip_search_index($id); // If the message we deleted is the most recent in the topic (at the end of the topic) if ($last_id == $id) { // If there is a $second_last_id there is more than 1 reply to the topic if ($second_last_id != NULL) $db->query('UPDATE '.$db->prefix.'topics SET last_post='.$second_posted.', last_post_id='.$second_last_id.', last_poster=\''.addslashes($second_poster).'\', num_replies=num_replies-1 WHERE id='.$cur_post['topic_id']) or error('Unable to update topic', __FILE__, __LINE__, $db->error()); else // We deleted the only reply, so now last_post/last_post_id/last_poster is posted/id/poster from the topic itself $db->query('UPDATE '.$db->prefix.'topics SET last_post=posted, last_post_id=id, last_poster=poster, num_replies=num_replies-1 WHERE id='.$cur_post['topic_id']) or error('Unable to update topic', __FILE__, __LINE__, $db->error()); } else // Otherwise we just decrement the reply counter $db->query('UPDATE '.$db->prefix.'topics SET num_replies=num_replies-1 WHERE id='.$cur_post['topic_id']) or error('Unable to update topic', __FILE__, __LINE__, $db->error()); update_forum($forum_id, PUN_TRANS_END); // end transaction redirect('viewtopic.php?id='.$cur_post['topic_id'], $lang_delete['Post del redirect']); } else // It's the topic post { // Delete the topic and any redirect topics (start transaction) $db->query('DELETE FROM '.$db->prefix.'topics WHERE id='.$cur_post['topic_id'].' OR moved_to='.$cur_post['topic_id'], PUN_TRANS_START) or error('Unable to delete topic', __FILE__, __LINE__, $db->error()); // Create a list of the post ID's in this topic and then strip the search index $result = $db->query('SELECT id FROM '.$db->prefix.'posts WHERE topic_id='.$cur_post['topic_id']) or error('Unable to fetch posts', __FILE__, __LINE__, $db->error()); while ($row = $db->fetch_row($result)) $post_ids .= ($post_ids != '') ? ','.$row[0] : $row[0]; strip_search_index($post_ids); // Delete posts in topic $db->query('DELETE FROM '.$db->prefix.'posts WHERE topic_id='.$cur_post['topic_id']) or error('Unable to delete posts', __FILE__, __LINE__, $db->error()); update_forum($forum_id, PUN_TRANS_END); // end transaction redirect('viewforum.php?id='.$forum_id, $lang_delete['Topic del redirect']); } } else { $page_title = htmlspecialchars($options['board_title']).' / '.$lang_delete['Delete post']; require 'header.php'; require 'include/parser.php'; $cur_post['message'] = parse_message($cur_post['message'], $cur_post['smilies']); ?>