Browse Source

+ Added a file maintenance check that verifies if files present on the disk have a corresponding entry into the database

emanuele 13 years ago
parent
commit
9526abc17a
2 changed files with 32 additions and 0 deletions
  1. 31 0
      Sources/ManageAttachments.php
  2. 1 0
      Themes/default/languages/Admin.english.php

+ 31 - 0
Sources/ManageAttachments.php

@@ -1456,6 +1456,7 @@ function RepairAttachments()
 				{
 					if ($file == '.' || $file == '..')
 						continue;
+
 					if ($files_checked <= $current_check)
 					{
 						// Temporary file, get rid of it!
@@ -1465,6 +1466,36 @@ function RepairAttachments()
 							if (filemtime($attach_dir . '/' . $file) < time() - 18000)
 								@unlink($attach_dir . '/' . $file);
 						}
+						// That should be an attachment, let's check if we have it in the database
+						elseif (strpos($file, '_') !== false)
+						{
+							$attachID = (int) substr($file, 0, strpos($file, '_'));
+							if (!empty($attachID))
+							{
+								$request = $smcFunc['db_query']('', '
+									SELECT  id_attach
+									FROM {db_prefix}attachments
+									WHERE id_attach = {int:attachment_id}
+									LIMIT 1',
+									array(
+										'attachment_id' => $attachID,
+									)
+								);
+								if ($smcFunc['db_num_rows']($request) == 0)
+								{
+									if ($fix_errors)
+									{
+										@unlink($attach_dir . '/' . $file);
+									}
+									else
+									{
+										$context['repair_errors']['files_without_attachment']++;
+										$to_fix[] = 'files_without_attachment';
+									}
+								}
+								$smcFunc['db_free_result']($request);
+							}
+						}
 					}
 					$current_check++;
 					if ($current_check - $files_checked >= $max_checks)

+ 1 - 0
Themes/default/languages/Admin.english.php

@@ -442,6 +442,7 @@ $txt['attach_repair_file_size_of_zero'] = '%1$d attachments/avatars have a size
 $txt['attach_repair_attachment_no_msg'] = '%1$d attachments no longer have a message associated with them';
 $txt['attach_repair_avatar_no_member'] = '%1$d avatars no longer have a member associated with them';
 $txt['attach_repair_wrong_folder'] = '%1$d attachments are in the wrong folder';
+$txt['attach_repair_files_without_attachment'] = '%1$d files doesn\'t have a corresponding entri into the database. (These will be deleted)';
 
 $txt['news_title'] = 'News and Newsletters';
 $txt['news_settings_desc'] = 'Here you can change the settings and permissions related to news and newsletters.';