Gazelle/sections/forums/delete.php

133 lines
3.9 KiB
PHP
Raw Normal View History

2011-03-28 14:21:28 +00:00
<?
authorize();
// Quick SQL injection check
2013-05-05 08:00:31 +00:00
if (!isset($_GET['postid']) || !is_number($_GET['postid'])) {
error(0);
}
2011-03-28 14:21:28 +00:00
$PostID = $_GET['postid'];
// Make sure they are moderators
2013-05-05 08:00:31 +00:00
if (!check_perms('site_admin_forums')) {
2011-03-28 14:21:28 +00:00
error(403);
}
// Get topicid, forumid, number of pages
2013-05-05 08:00:31 +00:00
$DB->query("
SELECT
TopicID,
ForumID,
CEIL(COUNT(p.ID)/".POSTS_PER_PAGE.") AS Pages,
CEIL(SUM(IF(p.ID<='$PostID',1,0))/".POSTS_PER_PAGE.") AS Page,
StickyPostID
2011-03-28 14:21:28 +00:00
FROM forums_posts AS p
2013-05-05 08:00:31 +00:00
JOIN forums_topics AS t ON t.ID=p.TopicID
WHERE p.TopicID=(
SELECT TopicID
FROM forums_posts
WHERE ID='$PostID'
)
2012-05-18 13:35:17 +00:00
GROUP BY t.ID");
2012-06-08 08:00:23 +00:00
list($TopicID, $ForumID, $Pages, $Page, $StickyPostID) = $DB->next_record();
2011-03-28 14:21:28 +00:00
// $Pages = number of pages in the thread
// $Page = which page the post is on
// These are set for cache clearing.
$DB->query("DELETE FROM forums_posts WHERE ID='$PostID'");
$DB->query("SELECT MAX(ID) FROM forums_posts WHERE TopicID='$TopicID'");
list($LastID) = $DB->next_record();
2013-05-05 08:00:31 +00:00
$DB->query("
UPDATE forums AS f, forums_topics AS t
SET f.NumPosts=f.NumPosts-1, t.NumPosts=t.NumPosts-1
WHERE f.ID='$ForumID' AND t.ID='$TopicID'");
2011-03-28 14:21:28 +00:00
2013-05-05 08:00:31 +00:00
if ($LastID < $PostID) { // Last post in a topic was removed
$DB->query("
SELECT p.AuthorID, u.Username, p.AddedTime
FROM forums_posts AS p
LEFT JOIN users_main AS u ON u.ID = p.AuthorID
WHERE p.ID='$LastID'");
2011-03-28 14:21:28 +00:00
list($LastAuthorID, $LastAuthorName, $LastTime) = $DB->next_record();
2013-05-05 08:00:31 +00:00
$DB->query("
UPDATE forums_topics
SET
LastPostID='$LastID',
LastPostAuthorID='$LastAuthorID',
LastPostTime='$LastTime'
WHERE ID='$TopicID'");
$DB->query("
SELECT
t.ID,
t.Title,
t.LastPostID,
t.LastPostTime,
t.LastPostAuthorID,
u.Username
2011-03-28 14:21:28 +00:00
FROM forums_topics AS t
2013-05-05 08:00:31 +00:00
LEFT JOIN users_main AS u ON u.ID = t.LastPostAuthorID
2011-03-28 14:21:28 +00:00
WHERE ForumID='$ForumID' AND t.ID<>'$TopicID'
2013-05-05 08:00:31 +00:00
ORDER BY LastPostID DESC
LIMIT 1");
2011-03-28 14:21:28 +00:00
list($LastTopicID, $LastTopicTitle, $LastTopicPostID, $LastTopicPostTime, $LastTopicAuthorID, $LastTopicAuthorName) = $DB->next_record(MYSQLI_BOTH, false);
2013-05-05 08:00:31 +00:00
if ($LastID < $LastTopicPostID) { // Topic is no longer the most recent in its forum
$DB->query("
UPDATE forums
SET
LastPostTopicID='$LastTopicID',
LastPostID='$LastTopicPostID',
LastPostAuthorID='$LastTopicAuthorID',
LastPostTime='$LastTopicPostTime'
WHERE ID='$ForumID'
AND LastPostTopicID='$TopicID'");
2011-03-28 14:21:28 +00:00
$UpdateArrayForums = array(
'NumPosts' => '-1',
'LastPostID' => $LastTopicPostID,
'LastPostAuthorID' => $LastTopicAuthorID,
'LastPostTime' => $LastTopicPostTime,
'LastPostTopicID' => $LastTopicID,
'Title' => $LastTopicTitle);
} else { // Topic is still the most recent in its forum
2013-05-05 08:00:31 +00:00
$DB->query("
UPDATE forums
SET
LastPostID='$LastID',
LastPostAuthorID='$LastAuthorID',
LastPostTime='$LastTime'
WHERE ID='$ForumID'
AND LastPostTopicID='$TopicID'");
2011-03-28 14:21:28 +00:00
$UpdateArrayForums = array(
'NumPosts' => '-1',
'LastPostID' => $LastID,
'LastPostAuthorID' => $LastAuthorID,
'LastPostTime' => $LastTime);
}
$UpdateArrayThread = array('Posts' => '-1', 'LastPostAuthorID' => $LastAuthorID);
} else {
$UpdateArrayForums = array('NumPosts' => '-1');
$UpdateArrayThread = array('Posts' => '-1');
}
2013-05-05 08:00:31 +00:00
if ($StickyPostID == $PostID) {
2012-06-08 08:00:23 +00:00
$DB->query("UPDATE forums_topics SET StickyPostID = 0 WHERE ID = $TopicID");
}
2011-03-28 14:21:28 +00:00
//We need to clear all subsequential catalogues as they've all been bumped with the absence of this post
2013-05-05 08:00:31 +00:00
$ThisCatalogue = floor((POSTS_PER_PAGE * $Page - POSTS_PER_PAGE) / THREAD_CATALOGUE);
$LastCatalogue = floor((POSTS_PER_PAGE * $Pages - POSTS_PER_PAGE) / THREAD_CATALOGUE);
for ($i = $ThisCatalogue; $i <= $LastCatalogue; $i++) {
2011-03-28 14:21:28 +00:00
$Cache->delete('thread_'.$TopicID.'_catalogue_'.$i);
}
$Cache->begin_transaction('thread_'.$TopicID.'_info');
$Cache->update_row(false, $UpdateArrayThread);
$Cache->commit_transaction();
$Cache->begin_transaction('forums_list');
$Cache->update_row($ForumID, $UpdateArrayForums);
$Cache->commit_transaction();
$Cache->delete('forums_'.$ForumID);
?>