Browse Source

Let the upgrade to convert all attachments

Signed-off-by: Suki <[email protected]>
Suki 10 years ago
parent
commit
1898f1aa0c
3 changed files with 136 additions and 37 deletions
  1. 50 17
      other/upgrade_2-1_mysql.sql
  2. 43 10
      other/upgrade_2-1_postgresql.sql
  3. 43 10
      other/upgrade_2-1_sqlite.sql

+ 50 - 17
other/upgrade_2-1_mysql.sql

@@ -38,7 +38,7 @@ if (!isset($modSettings['allow_no_censored']))
 		WHERE variable='allow_no_censored'
 		AND id_theme = 1 OR id_theme = '$modSettings[theme_default]'
 	");
-	
+
 	// Is it set for either "default" or the one they've set as default?
 	while ($row = $smcFunc['db_fetch_assoc']($request))
 	{
@@ -48,7 +48,7 @@ if (!isset($modSettings['allow_no_censored']))
 				INSERT INTO {$db_prefix}settings
 				VALUES ('allow_no_censored', 1)
 			");
-			
+
 			// Don't do this twice...
 			break;
 		}
@@ -85,11 +85,12 @@ $is_done = false;
 while (!$is_done)
 {
 	nextSubStep($substep);
+	$fileHash = '';
 
 	$request = upgrade_query("
 		SELECT id_attach, id_folder, filename, file_hash
 		FROM {$db_prefix}attachments
-		WHERE file_hash = ''
+		WHERE attachment_type != 1
 		LIMIT $_GET[a], 100");
 
 	// Finished?
@@ -99,22 +100,54 @@ while (!$is_done)
 	while ($row = $smcFunc['db_fetch_assoc']($request))
 	{
 		// The current folder.
-		$current_folder = !empty($modSettings['currentAttachmentUploadDir']) ? $modSettings['attachmentUploadDir'][$row['id_folder']] : $modSettings['attachmentUploadDir'];
+		$currentFolder = !empty($modSettings['currentAttachmentUploadDir']) ? $modSettings['attachmentUploadDir'][$row['id_folder']] : $modSettings['attachmentUploadDir'];
+
+		// Old School?
+		if (empty($row['file_hash']))
+		{
+			// Remove international characters (windows-1252)
+			// These lines should never be needed again. Still, behave.
+			if (empty($db_character_set) || $db_character_set != 'utf8')
+			{
+				$row['filename'] = strtr($row['filename'],
+					"\x8a\x8e\x9a\x9e\x9f\xc0\xc1\xc2\xc3\xc4\xc5\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xd9\xda\xdb\xdc\xdd\xe0\xe1\xe2\xe3\xe4\xe5\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf1\xf2\xf3\xf4\xf5\xf6\xf8\xf9\xfa\xfb\xfc\xfd\xff",
+					'SZszYAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy');
+				$row['filename'] = strtr($row['filename'], array("\xde" => 'TH', "\xfe" =>
+					'th', "\xd0" => 'DH', "\xf0" => 'dh', "\xdf" => 'ss', "\x8c" => 'OE',
+					"\x9c" => 'oe', "\xc6" => 'AE', "\xe6" => 'ae', "\xb5" => 'u'));
+			}
+			// Sorry, no spaces, dots, or anything else but letters allowed.
+			$row['filename'] = preg_replace(array('/\s/', '/[^\w_\.\-]/'), array('_', ''), $row['filename']);
+
+			// Create a nice hash.
+			$fileHash = sha1(md5($row['filename'] . time()) . mt_rand());
+
+			// The old file, we need to know if the filename was encrypted or not.
+			if (file_exists($currentFolder . '/' . $row['id_attach']. '_' . strtr($row['filename'], '.', '_') . md5($row['filename'])))
+				$oldFile = $currentFolder . '/' . $row['id_attach']. '_' . strtr($row['filename'], '.', '_') . md5($row['filename']);
 
-		// The old location of the file.
-		$old_location = getLegacyAttachmentFilename($row['filename'], $row['id_attach'], $row['id_folder']);
+			else if (file_exists($currentFolder . '/' . $row['filename']));
+				$oldFile = $currentFolder . '/' . $row['filename'];
 
-		// The new file name.
-		$file_hash = getAttachmentFilename($row['filename'], $row['id_attach'], $row['id_folder'], true);
+			// Build the new file.
+			$newFile = $currentFolder . '/' . $row['id_attach'] . '_' . $fileHash .'.dat';
+		}
+
+		// Just rename the file.
+		else
+		{
+			$oldFile = $currentFolder . '/' . $row['id_attach'] . '_' . $row['file_hash'];
+			$newFile = $currentFolder . '/' . $row['id_attach'] . '_' . $row['file_hash'] .'.dat';
+		}
 
 		// And we try to move it.
-		rename($old_location, $current_folder . '/' . $row['id_attach'] . '_' . $file_hash);
+		rename($oldFile, $newFile);
 
-		// Only update thif if it was successful.
-		if (file_exists($current_folder . '/' . $row['id_attach'] . '_' . $file_hash) && !file_exists($old_location))
+		// Only update this if it was successful and the file was using the old system.
+		if (empty($row['file_hash']) && !empty($fileHash) && file_exists($newFile) && !file_exists($oldFile))
 			upgrade_query("
 				UPDATE {$db_prefix}attachments
-				SET file_hash = '$file_hash'
+				SET file_hash = '$fileHash'
 				WHERE id_attach = $row[id_attach]");
 	}
 	$smcFunc['db_free_result']($request);
@@ -595,7 +628,7 @@ $request = upgrade_query("
 	SELECT id_group, add_deny
 	FROM {$db_prefix}permissions
 	WHERE permission = 'profile_identity_own'");
-	
+
 	while ($row = $smcFunc['db_fetch_assoc']($request))
 	{
 		$inserts[] = "($row[id_group], 'profile_password_own', $row[add_deny])";
@@ -623,7 +656,7 @@ $request = upgrade_query("
 	SELECT id_group, add_deny
 	FROM {$db_prefix}permissions
 	WHERE permission = 'profile_extra_own'");
-	
+
 	while ($row = $smcFunc['db_fetch_assoc']($request))
 	{
 		$inserts[] = "($row[id_group], 'profile_blurb_own', $row[add_deny])";
@@ -709,7 +742,7 @@ ADD COLUMN in_inbox tinyint(3) NOT NULL default '1';
 		if (!empty($inserts))
 		{
 			$smcFunc['db_insert']('', '{db_prefix}pm_labels', array('id_member' => 'int', 'name' => 'string-30'), $inserts, array());
-			
+
 			// Clear this out for our next query below
 			$inserts = array();
 		}
@@ -759,7 +792,7 @@ ADD COLUMN in_inbox tinyint(3) NOT NULL default '1';
 		while ($row = $smcFunc['db_fetch_assoc']($get_pm_labels))
 		{
 			$labels = explode(',', $row['labels']);
-			
+
 			foreach ($labels as $a_label)
 			{
 				if ($a_label == '-1')
@@ -812,7 +845,7 @@ ADD COLUMN in_inbox tinyint(3) NOT NULL default '1';
 				array(
 					'actions' => $actions,
 					'id_rule' => $row['id_rule'],
-				)	
+				)
 			);
 		}
 

+ 43 - 10
other/upgrade_2-1_postgresql.sql

@@ -87,11 +87,12 @@ $is_done = false;
 while (!$is_done)
 {
 	nextSubStep($substep);
+	$fileHash = '';
 
 	$request = upgrade_query("
 		SELECT id_attach, id_folder, filename, file_hash
 		FROM {$db_prefix}attachments
-		WHERE file_hash = ''
+		WHERE attachment_type != 1
 		LIMIT $_GET[a], 100");
 
 	// Finished?
@@ -101,22 +102,54 @@ while (!$is_done)
 	while ($row = $smcFunc['db_fetch_assoc']($request))
 	{
 		// The current folder.
-		$current_folder = !empty($modSettings['currentAttachmentUploadDir']) ? $modSettings['attachmentUploadDir'][$row['id_folder']] : $modSettings['attachmentUploadDir'];
+		$currentFolder = !empty($modSettings['currentAttachmentUploadDir']) ? $modSettings['attachmentUploadDir'][$row['id_folder']] : $modSettings['attachmentUploadDir'];
 
-		// The old location of the file.
-		$old_location = getLegacyAttachmentFilename($row['filename'], $row['id_attach'], $row['id_folder']);
+		// Old School?
+		if (empty($row['file_hash']))
+		{
+			// Remove international characters (windows-1252)
+			// These lines should never be needed again. Still, behave.
+			if (empty($db_character_set) || $db_character_set != 'utf8')
+			{
+				$row['filename'] = strtr($row['filename'],
+					"\x8a\x8e\x9a\x9e\x9f\xc0\xc1\xc2\xc3\xc4\xc5\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xd9\xda\xdb\xdc\xdd\xe0\xe1\xe2\xe3\xe4\xe5\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf1\xf2\xf3\xf4\xf5\xf6\xf8\xf9\xfa\xfb\xfc\xfd\xff",
+					'SZszYAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy');
+				$row['filename'] = strtr($row['filename'], array("\xde" => 'TH', "\xfe" =>
+					'th', "\xd0" => 'DH', "\xf0" => 'dh', "\xdf" => 'ss', "\x8c" => 'OE',
+					"\x9c" => 'oe', "\xc6" => 'AE', "\xe6" => 'ae', "\xb5" => 'u'));
+			}
+			// Sorry, no spaces, dots, or anything else but letters allowed.
+			$row['filename'] = preg_replace(array('/\s/', '/[^\w_\.\-]/'), array('_', ''), $row['filename']);
+
+			// Create a nice hash.
+			$fileHash = sha1(md5($row['filename'] . time()) . mt_rand());
+
+			// The old file, we need to know if the filename was encrypted or not.
+			if (file_exists($currentFolder . '/' . $row['id_attach']. '_' . strtr($row['filename'], '.', '_') . md5($row['filename'])))
+				$oldFile = $currentFolder . '/' . $row['id_attach']. '_' . strtr($row['filename'], '.', '_') . md5($row['filename']);
 
-		// The new file name.
-		$file_hash = getAttachmentFilename($row['filename'], $row['id_attach'], $row['id_folder'], true);
+			else if (file_exists($currentFolder . '/' . $row['filename']));
+				$oldFile = $currentFolder . '/' . $row['filename'];
+
+			// Build the new file.
+			$newFile = $currentFolder . '/' . $row['id_attach'] . '_' . $fileHash .'.dat';
+		}
+
+		// Just rename the file.
+		else
+		{
+			$oldFile = $currentFolder . '/' . $row['id_attach'] . '_' . $row['file_hash'];
+			$newFile = $currentFolder . '/' . $row['id_attach'] . '_' . $row['file_hash'] .'.dat';
+		}
 
 		// And we try to move it.
-		rename($old_location, $current_folder . '/' . $row['id_attach'] . '_' . $file_hash);
+		rename($oldFile, $newFile);
 
-		// Only update thif if it was successful.
-		if (file_exists($current_folder . '/' . $row['id_attach'] . '_' . $file_hash) && !file_exists($old_location))
+		// Only update this if it was successful and the file was using the old system.
+		if (empty($row['file_hash']) && !empty($fileHash) && file_exists($newFile) && !file_exists($oldFile))
 			upgrade_query("
 				UPDATE {$db_prefix}attachments
-				SET file_hash = '$file_hash'
+				SET file_hash = '$fileHash'
 				WHERE id_attach = $row[id_attach]");
 	}
 	$smcFunc['db_free_result']($request);

+ 43 - 10
other/upgrade_2-1_sqlite.sql

@@ -85,11 +85,12 @@ $is_done = false;
 while (!$is_done)
 {
 	nextSubStep($substep);
+	$fileHash = '';
 
 	$request = upgrade_query("
 		SELECT id_attach, id_folder, filename, file_hash
 		FROM {$db_prefix}attachments
-		WHERE file_hash = ''
+		WHERE attachment_type != 1
 		LIMIT $_GET[a], 100");
 
 	// Finished?
@@ -99,22 +100,54 @@ while (!$is_done)
 	while ($row = $smcFunc['db_fetch_assoc']($request))
 	{
 		// The current folder.
-		$current_folder = !empty($modSettings['currentAttachmentUploadDir']) ? $modSettings['attachmentUploadDir'][$row['id_folder']] : $modSettings['attachmentUploadDir'];
+		$currentFolder = !empty($modSettings['currentAttachmentUploadDir']) ? $modSettings['attachmentUploadDir'][$row['id_folder']] : $modSettings['attachmentUploadDir'];
 
-		// The old location of the file.
-		$old_location = getLegacyAttachmentFilename($row['filename'], $row['id_attach'], $row['id_folder']);
+		// Old School?
+		if (empty($row['file_hash']))
+		{
+			// Remove international characters (windows-1252)
+			// These lines should never be needed again. Still, behave.
+			if (empty($db_character_set) || $db_character_set != 'utf8')
+			{
+				$row['filename'] = strtr($row['filename'],
+					"\x8a\x8e\x9a\x9e\x9f\xc0\xc1\xc2\xc3\xc4\xc5\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xd9\xda\xdb\xdc\xdd\xe0\xe1\xe2\xe3\xe4\xe5\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf1\xf2\xf3\xf4\xf5\xf6\xf8\xf9\xfa\xfb\xfc\xfd\xff",
+					'SZszYAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy');
+				$row['filename'] = strtr($row['filename'], array("\xde" => 'TH', "\xfe" =>
+					'th', "\xd0" => 'DH', "\xf0" => 'dh', "\xdf" => 'ss', "\x8c" => 'OE',
+					"\x9c" => 'oe', "\xc6" => 'AE', "\xe6" => 'ae', "\xb5" => 'u'));
+			}
+			// Sorry, no spaces, dots, or anything else but letters allowed.
+			$row['filename'] = preg_replace(array('/\s/', '/[^\w_\.\-]/'), array('_', ''), $row['filename']);
+
+			// Create a nice hash.
+			$fileHash = sha1(md5($row['filename'] . time()) . mt_rand());
+
+			// The old file, we need to know if the filename was encrypted or not.
+			if (file_exists($currentFolder . '/' . $row['id_attach']. '_' . strtr($row['filename'], '.', '_') . md5($row['filename'])))
+				$oldFile = $currentFolder . '/' . $row['id_attach']. '_' . strtr($row['filename'], '.', '_') . md5($row['filename']);
 
-		// The new file name.
-		$file_hash = getAttachmentFilename($row['filename'], $row['id_attach'], $row['id_folder'], true);
+			else if (file_exists($currentFolder . '/' . $row['filename']));
+				$oldFile = $currentFolder . '/' . $row['filename'];
+
+			// Build the new file.
+			$newFile = $currentFolder . '/' . $row['id_attach'] . '_' . $fileHash .'.dat';
+		}
+
+		// Just rename the file.
+		else
+		{
+			$oldFile = $currentFolder . '/' . $row['id_attach'] . '_' . $row['file_hash'];
+			$newFile = $currentFolder . '/' . $row['id_attach'] . '_' . $row['file_hash'] .'.dat';
+		}
 
 		// And we try to move it.
-		rename($old_location, $current_folder . '/' . $row['id_attach'] . '_' . $file_hash);
+		rename($oldFile, $newFile);
 
-		// Only update thif if it was successful.
-		if (file_exists($current_folder . '/' . $row['id_attach'] . '_' . $file_hash) && !file_exists($old_location))
+		// Only update this if it was successful and the file was using the old system.
+		if (empty($row['file_hash']) && !empty($fileHash) && file_exists($newFile) && !file_exists($oldFile))
 			upgrade_query("
 				UPDATE {$db_prefix}attachments
-				SET file_hash = '$file_hash'
+				SET file_hash = '$fileHash'
 				WHERE id_attach = $row[id_attach]");
 	}
 	$smcFunc['db_free_result']($request);