Bug #581

Attachments not deleted correctly

Added by Sebastian Wunderlich 9 months ago. Updated 9 months ago.

Status:Closed Start:12/09/2009
Priority:Normal Due date:
Assigned to:Ryan Gordon % Done:

100%

Category:Other
Target version:1.4.11
Reproducibility:Always Browser:
Reported In MyBB Version:1.4.10 Database Type:
PHP Version:5.2.9 Database Version:

Description

In MyBB 1.4.10 there is a new check if an attachment is referenced in any other posts before deleting it.
But there is an error in variable name:

inc/functions_upload.php - function remove_attachments($pid, $posthash="")

107    while($attachment = $db->fetch_array($query))
108    {
109        if($attachment['visible'] == 1)
110        {
111            $num_attachments++;
112        }
113        
114        $plugins->run_hooks("remove_attachments_do_delete", $attachment);
115        
116        $db->delete_query("attachments", "aid='".$attachment['aid']."'");
117        
118        // Check if this attachment is referenced in any other posts. If it isn't, then we are safe to delete the actual file.
119        $query = $db->simple_select("attachments", "COUNT(aid) as numreferences", "attachname='".$db->escape_string($attachment['attachname'])."'");
120        if($db->fetch_field($query, "numreferences") == 0)
121        {
122            @unlink($uploadpath."/".$attachment['attachname']);
123            if($attachment['thumbnail'])
124            {
125                @unlink($uploadpath."/".$attachment['thumbnail']);
126            }
127
128            $date_directory = explode('/', $attachment['attachname']);
129            if(@is_dir($uploadpath."/".$date_directory[0]))
130            {
131                @rmdir($uploadpath."/".$date_directory[0]);
132            }
133        }
134    }

There is a variable named $query in while loop but this variable will overwrite the variable used for while loop itself and so the loop will break.
Result: If you delete a post with more than one attached filed only the first attachment will deleted and the others will stay in upload directory.

But there is a very simple fix:

Change this:

118        // Check if this attachment is referenced in any other posts. If it isn't, then we are safe to delete the actual file.
119        $query = $db->simple_select("attachments", "COUNT(aid) as numreferences", "attachname='".$db->escape_string($attachment['attachname'])."'");
120        if($db->fetch_field($query, "numreferences") == 0)

... to this:

118        // Check if this attachment is referenced in any other posts. If it isn't, then we are safe to delete the actual file.
119        $query2 = $db->simple_select("attachments", "COUNT(aid) as numreferences", "attachname='".$db->escape_string($attachment['attachname'])."'");
120        if($db->fetch_field($query2, "numreferences") == 0)

Associated revisions

Revision 4591
Added by Ryan Gordon 9 months ago

Fixes Attachments not deleted correctly (fixes:581)

History

Updated by Stefan T. 9 months ago

  • Status changed from New to Confirmed

Updated by Ryan Gordon 9 months ago

Common SQA...

Updated by Ryan Gordon 9 months ago

  • Status changed from Confirmed to Resolved
  • % Done changed from 0 to 100

Applied in changeset r4591.

Updated by Ryan Gordon 9 months ago

  • Category set to Other
  • Assigned to set to Ryan Gordon
  • Target version set to 1.4.10

This has also been fixed in the official MyBB 1.4.10 package and 1.4.10 changed file package

Updated by Stefan T. 9 months ago

  • Status changed from Resolved to Closed

Updated by Dennis Tsang 9 months ago

  • Target version changed from 1.4.10 to 1.4.11

Also available in: Atom PDF