mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-12 06:18:52 +00:00
Correctly delete message attachments. (Bug #23755) Also revamped and simplified delete_attachments() - it looks like we did not touch it for a long long time.
git-svn-id: file:///svn/phpbb/branches/phpBB-3_0_0@8890 89ea8834-ac86-4346-8a33-228a782c2dd0
This commit is contained in:
parent
a2a9b8b7ab
commit
e12fce5f3d
3 changed files with 103 additions and 126 deletions
|
@ -118,6 +118,7 @@
|
||||||
<li>[Fix] Set secure cookie for style switcher if required. (Bug #19625)</li>
|
<li>[Fix] Set secure cookie for style switcher if required. (Bug #19625)</li>
|
||||||
<li>[Fix] Fix native full text search on postgresql while using excluding keyword matches. (Bug #19195)</li>
|
<li>[Fix] Fix native full text search on postgresql while using excluding keyword matches. (Bug #19195)</li>
|
||||||
<li>[Fix] Pass S_SEARCH_ACTION through append_sid() in search.php. (Bug #21585)</li>
|
<li>[Fix] Pass S_SEARCH_ACTION through append_sid() in search.php. (Bug #21585)</li>
|
||||||
|
<li>[Fix] Correctly delete message attachments. (Bug #23755)</li>
|
||||||
|
|
||||||
<li>[Change] No longer allow the direct use of MULTI_INSERT in sql_build_array. sql_multi_insert() must be used.</li>
|
<li>[Change] No longer allow the direct use of MULTI_INSERT in sql_build_array. sql_multi_insert() must be used.</li>
|
||||||
<li>[Change] Display warning in ACP if config.php file is left writable.</li>
|
<li>[Change] Display warning in ACP if config.php file is left writable.</li>
|
||||||
|
|
|
@ -791,8 +791,8 @@ function delete_posts($where_type, $where_ids, $auto_sync = true, $posted_sync =
|
||||||
/**
|
/**
|
||||||
* Delete Attachments
|
* Delete Attachments
|
||||||
*
|
*
|
||||||
* @param string $mode can be: post|topic|attach|user
|
* @param string $mode can be: post|message|topic|attach|user
|
||||||
* @param mixed $ids can be: post_ids, topic_ids, attach_ids, user_ids
|
* @param mixed $ids can be: post_ids, message_ids, topic_ids, attach_ids, user_ids
|
||||||
* @param bool $resync set this to false if you are deleting posts or topics
|
* @param bool $resync set this to false if you are deleting posts or topics
|
||||||
*/
|
*/
|
||||||
function delete_attachments($mode, $ids, $resync = true)
|
function delete_attachments($mode, $ids, $resync = true)
|
||||||
|
@ -814,42 +814,55 @@ function delete_attachments($mode, $ids, $resync = true)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$sql_id = ($mode == 'user') ? 'poster_id' : (($mode == 'post') ? 'post_msg_id' : (($mode == 'topic') ? 'topic_id' : 'attach_id'));
|
switch ($mode)
|
||||||
|
|
||||||
$post_ids = $topic_ids = $physical = array();
|
|
||||||
|
|
||||||
// Collect post and topics ids for later use
|
|
||||||
if ($mode == 'attach' || $mode == 'user' || ($mode == 'topic' && $resync))
|
|
||||||
{
|
{
|
||||||
$sql = 'SELECT post_msg_id as post_id, topic_id, physical_filename, thumbnail, filesize
|
case 'post':
|
||||||
|
case 'message':
|
||||||
|
$sql_id = 'post_msg_id';
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'topic':
|
||||||
|
$sql_id = 'topic_id';
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'user':
|
||||||
|
$sql_id = 'poster_id';
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'attach':
|
||||||
|
default:
|
||||||
|
$sql_id = 'attach_id';
|
||||||
|
$mode = 'attach';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
$post_ids = $message_ids = $topic_ids = $physical = array();
|
||||||
|
|
||||||
|
// Collect post and topic ids for later use if we need to touch remaining entries (if resync is enabled)
|
||||||
|
$sql = 'SELECT post_msg_id, topic_id, in_message, physical_filename, thumbnail, filesize, is_orphan
|
||||||
FROM ' . ATTACHMENTS_TABLE . '
|
FROM ' . ATTACHMENTS_TABLE . '
|
||||||
WHERE ' . $db->sql_in_set($sql_id, $ids);
|
WHERE ' . $db->sql_in_set($sql_id, $ids);
|
||||||
$result = $db->sql_query($sql);
|
$result = $db->sql_query($sql);
|
||||||
|
|
||||||
while ($row = $db->sql_fetchrow($result))
|
while ($row = $db->sql_fetchrow($result))
|
||||||
{
|
|
||||||
$post_ids[] = $row['post_id'];
|
|
||||||
$topic_ids[] = $row['topic_id'];
|
|
||||||
$physical[] = array('filename' => $row['physical_filename'], 'thumbnail' => $row['thumbnail'], 'filesize' => $row['filesize']);
|
|
||||||
}
|
|
||||||
$db->sql_freeresult($result);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($mode == 'post')
|
|
||||||
{
|
{
|
||||||
$sql = 'SELECT topic_id, physical_filename, thumbnail, filesize
|
// We only need to store post/message/topic ids if resync is enabled and the file is not orphaned
|
||||||
FROM ' . ATTACHMENTS_TABLE . '
|
if ($resync && !$row['is_orphan'])
|
||||||
WHERE ' . $db->sql_in_set('post_msg_id', $ids) . '
|
|
||||||
AND in_message = 0';
|
|
||||||
$result = $db->sql_query($sql);
|
|
||||||
|
|
||||||
while ($row = $db->sql_fetchrow($result))
|
|
||||||
{
|
{
|
||||||
$topic_ids[] = $row['topic_id'];
|
if (!$row['in_message'])
|
||||||
$physical[] = array('filename' => $row['physical_filename'], 'thumbnail' => $row['thumbnail'], 'filesize' => $row['filesize']);
|
{
|
||||||
|
$post_ids[] = $row['post_msg_id'];
|
||||||
|
$topic_ids[] = $row['topic_id'];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$message_ids[] = $row['post_msg_id'];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
$db->sql_freeresult($result);
|
|
||||||
|
$physical[] = array('filename' => $row['physical_filename'], 'thumbnail' => $row['thumbnail'], 'filesize' => $row['filesize'], 'is_orphan' => $row['is_orphan']);
|
||||||
}
|
}
|
||||||
|
$db->sql_freeresult($result);
|
||||||
|
|
||||||
// Delete attachments
|
// Delete attachments
|
||||||
$sql = 'DELETE FROM ' . ATTACHMENTS_TABLE . '
|
$sql = 'DELETE FROM ' . ATTACHMENTS_TABLE . '
|
||||||
|
@ -866,8 +879,9 @@ function delete_attachments($mode, $ids, $resync = true)
|
||||||
$space_removed = $files_removed = 0;
|
$space_removed = $files_removed = 0;
|
||||||
foreach ($physical as $file_ary)
|
foreach ($physical as $file_ary)
|
||||||
{
|
{
|
||||||
if (phpbb_unlink($file_ary['filename'], 'file', true))
|
if (phpbb_unlink($file_ary['filename'], 'file', true) && !$file_ary['is_orphan'])
|
||||||
{
|
{
|
||||||
|
// Only non-orphaned files count to the file size
|
||||||
$space_removed += $file_ary['filesize'];
|
$space_removed += $file_ary['filesize'];
|
||||||
$files_removed++;
|
$files_removed++;
|
||||||
}
|
}
|
||||||
|
@ -877,122 +891,72 @@ function delete_attachments($mode, $ids, $resync = true)
|
||||||
phpbb_unlink($file_ary['filename'], 'thumbnail', true);
|
phpbb_unlink($file_ary['filename'], 'thumbnail', true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
set_config('upload_dir_size', $config['upload_dir_size'] - $space_removed, true);
|
|
||||||
set_config('num_files', $config['num_files'] - $files_removed, true);
|
|
||||||
|
|
||||||
if ($mode == 'topic' && !$resync)
|
if ($space_removed || $files_removed)
|
||||||
|
{
|
||||||
|
set_config('upload_dir_size', $config['upload_dir_size'] - $space_removed, true);
|
||||||
|
set_config('num_files', $config['num_files'] - $files_removed, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we do not resync, we do not need to adjust any message, post, topic or user entries
|
||||||
|
if (!$resync)
|
||||||
{
|
{
|
||||||
return $num_deleted;
|
return $num_deleted;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($mode == 'post')
|
// No more use for the original ids
|
||||||
{
|
|
||||||
$post_ids = $ids;
|
|
||||||
}
|
|
||||||
unset($ids);
|
unset($ids);
|
||||||
|
|
||||||
|
// Now, we need to resync posts, messages, topics. We go through every one of them
|
||||||
$post_ids = array_unique($post_ids);
|
$post_ids = array_unique($post_ids);
|
||||||
|
$message_ids = array_unique($message_ids);
|
||||||
$topic_ids = array_unique($topic_ids);
|
$topic_ids = array_unique($topic_ids);
|
||||||
|
|
||||||
// Update post indicators
|
// Update post indicators for posts now no longer having attachments
|
||||||
if (sizeof($post_ids))
|
if (sizeof($post_ids))
|
||||||
{
|
{
|
||||||
if ($mode == 'post' || $mode == 'topic')
|
$sql = 'UPDATE ' . POSTS_TABLE . '
|
||||||
{
|
SET post_attachment = 0
|
||||||
$sql = 'UPDATE ' . POSTS_TABLE . '
|
WHERE ' . $db->sql_in_set('post_id', $post_ids);
|
||||||
SET post_attachment = 0
|
$db->sql_query($sql);
|
||||||
WHERE ' . $db->sql_in_set('post_id', $post_ids);
|
|
||||||
$db->sql_query($sql);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($mode == 'user' || $mode == 'attach')
|
|
||||||
{
|
|
||||||
$remaining = array();
|
|
||||||
|
|
||||||
$sql = 'SELECT post_msg_id
|
|
||||||
FROM ' . ATTACHMENTS_TABLE . '
|
|
||||||
WHERE ' . $db->sql_in_set('post_msg_id', $post_ids) . '
|
|
||||||
AND in_message = 0';
|
|
||||||
$result = $db->sql_query($sql);
|
|
||||||
|
|
||||||
while ($row = $db->sql_fetchrow($result))
|
|
||||||
{
|
|
||||||
$remaining[] = $row['post_msg_id'];
|
|
||||||
}
|
|
||||||
$db->sql_freeresult($result);
|
|
||||||
|
|
||||||
$unset_ids = array_diff($post_ids, $remaining);
|
|
||||||
|
|
||||||
if (sizeof($unset_ids))
|
|
||||||
{
|
|
||||||
$sql = 'UPDATE ' . POSTS_TABLE . '
|
|
||||||
SET post_attachment = 0
|
|
||||||
WHERE ' . $db->sql_in_set('post_id', $unset_ids);
|
|
||||||
$db->sql_query($sql);
|
|
||||||
}
|
|
||||||
|
|
||||||
$remaining = array();
|
|
||||||
|
|
||||||
$sql = 'SELECT post_msg_id
|
|
||||||
FROM ' . ATTACHMENTS_TABLE . '
|
|
||||||
WHERE ' . $db->sql_in_set('post_msg_id', $post_ids) . '
|
|
||||||
AND in_message = 1';
|
|
||||||
$result = $db->sql_query($sql);
|
|
||||||
|
|
||||||
while ($row = $db->sql_fetchrow($result))
|
|
||||||
{
|
|
||||||
$remaining[] = $row['post_msg_id'];
|
|
||||||
}
|
|
||||||
$db->sql_freeresult($result);
|
|
||||||
|
|
||||||
$unset_ids = array_diff($post_ids, $remaining);
|
|
||||||
|
|
||||||
if (sizeof($unset_ids))
|
|
||||||
{
|
|
||||||
$sql = 'UPDATE ' . PRIVMSGS_TABLE . '
|
|
||||||
SET message_attachment = 0
|
|
||||||
WHERE ' . $db->sql_in_set('msg_id', $unset_ids);
|
|
||||||
$db->sql_query($sql);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update message table if messages are affected
|
||||||
|
if (sizeof($message_ids))
|
||||||
|
{
|
||||||
|
$sql = 'UPDATE ' . PRIVMSGS_TABLE . '
|
||||||
|
SET message_attachment = 0
|
||||||
|
WHERE ' . $db->sql_in_set('msg_id', $message_ids);
|
||||||
|
$db->sql_query($sql);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now update the topics. This is a bit trickier, because there could be posts still having attachments within the topic
|
||||||
if (sizeof($topic_ids))
|
if (sizeof($topic_ids))
|
||||||
{
|
{
|
||||||
// Update topic indicator
|
// Just check which topics are still having an assigned attachment not orphaned by querying the attachments table (much less entries expected)
|
||||||
if ($mode == 'topic')
|
$sql = 'SELECT topic_id
|
||||||
|
FROM ' . ATTACHMENTS_TABLE . '
|
||||||
|
WHERE ' . $db->sql_in_set('topic_id', $topic_ids) . '
|
||||||
|
AND is_orphan = 0';
|
||||||
|
$result = $db->sql_query($sql);
|
||||||
|
|
||||||
|
$remaining_ids = array();
|
||||||
|
while ($row = $db->sql_fetchrow($result))
|
||||||
|
{
|
||||||
|
$remaining_ids[] = $row['topic_id'];
|
||||||
|
}
|
||||||
|
$db->sql_freeresult($result);
|
||||||
|
|
||||||
|
// Now only unset those ids remaining
|
||||||
|
$topic_ids = array_diff($topic_ids, $remaining_ids);
|
||||||
|
|
||||||
|
if (sizeof($topic_ids))
|
||||||
{
|
{
|
||||||
$sql = 'UPDATE ' . TOPICS_TABLE . '
|
$sql = 'UPDATE ' . TOPICS_TABLE . '
|
||||||
SET topic_attachment = 0
|
SET topic_attachment = 0
|
||||||
WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
|
WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
|
||||||
$db->sql_query($sql);
|
$db->sql_query($sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($mode == 'post' || $mode == 'user' || $mode == 'attach')
|
|
||||||
{
|
|
||||||
$remaining = array();
|
|
||||||
|
|
||||||
$sql = 'SELECT topic_id
|
|
||||||
FROM ' . ATTACHMENTS_TABLE . '
|
|
||||||
WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
|
|
||||||
$result = $db->sql_query($sql);
|
|
||||||
|
|
||||||
while ($row = $db->sql_fetchrow($result))
|
|
||||||
{
|
|
||||||
$remaining[] = $row['topic_id'];
|
|
||||||
}
|
|
||||||
$db->sql_freeresult($result);
|
|
||||||
|
|
||||||
$unset_ids = array_diff($topic_ids, $remaining);
|
|
||||||
|
|
||||||
if (sizeof($unset_ids))
|
|
||||||
{
|
|
||||||
$sql = 'UPDATE ' . TOPICS_TABLE . '
|
|
||||||
SET topic_attachment = 0
|
|
||||||
WHERE ' . $db->sql_in_set('topic_id', $unset_ids);
|
|
||||||
$db->sql_query($sql);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $num_deleted;
|
return $num_deleted;
|
||||||
|
|
|
@ -930,7 +930,7 @@ function handle_mark_actions($user_id, $mark_action)
|
||||||
*/
|
*/
|
||||||
function delete_pm($user_id, $msg_ids, $folder_id)
|
function delete_pm($user_id, $msg_ids, $folder_id)
|
||||||
{
|
{
|
||||||
global $db, $user;
|
global $db, $user, $phpbb_root_path, $phpEx;
|
||||||
|
|
||||||
$user_id = (int) $user_id;
|
$user_id = (int) $user_id;
|
||||||
$folder_id = (int) $folder_id;
|
$folder_id = (int) $folder_id;
|
||||||
|
@ -979,6 +979,8 @@ function delete_pm($user_id, $msg_ids, $folder_id)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$db->sql_transaction('begin');
|
||||||
|
|
||||||
// if no one has read the message yet (meaning it is in users outbox)
|
// if no one has read the message yet (meaning it is in users outbox)
|
||||||
// then mark the message as deleted...
|
// then mark the message as deleted...
|
||||||
if ($folder_id == PRIVMSGS_OUTBOX)
|
if ($folder_id == PRIVMSGS_OUTBOX)
|
||||||
|
@ -1056,11 +1058,21 @@ function delete_pm($user_id, $msg_ids, $folder_id)
|
||||||
|
|
||||||
if (sizeof($delete_ids))
|
if (sizeof($delete_ids))
|
||||||
{
|
{
|
||||||
|
// Check if there are any attachments we need to remove
|
||||||
|
if (!function_exists('delete_attachments'))
|
||||||
|
{
|
||||||
|
include($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
|
||||||
|
}
|
||||||
|
|
||||||
|
delete_attachments('message', $delete_ids, false);
|
||||||
|
|
||||||
$sql = 'DELETE FROM ' . PRIVMSGS_TABLE . '
|
$sql = 'DELETE FROM ' . PRIVMSGS_TABLE . '
|
||||||
WHERE ' . $db->sql_in_set('msg_id', $delete_ids);
|
WHERE ' . $db->sql_in_set('msg_id', $delete_ids);
|
||||||
$db->sql_query($sql);
|
$db->sql_query($sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$db->sql_transaction('commit');
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue