<?php
/*
Plugin Name: Allow UTF-8 Filenames on Windows
Plugin URI: http://core.trac.wordpress.org/ticket/15955
Description: A workaround for correct uploading of files with UTF-8 names on Windows systems.
Version: 0.1
Author: Sergey Biryukov
Author URI: http://profiles.wordpress.org/sergeybiryukov/
*/

function autfw_sanitize_file_name_for_windows($filename, $utf8 = false) {
	if ( seems_utf8($filename) == $utf8 )
		return $filename;

	// On Windows platforms, PHP will mangle non-ASCII characters, see http://bugs.php.net/bug.php?id=47096 
	if ( 'WIN' == substr( PHP_OS, 0, 3 ) ) {
		if(setlocale( LC_CTYPE, 0 )=='C'){ // Locale has not been set and the default is being used, according to answer by Colin Morelli at http://stackoverflow.com/questions/13788415/how-to-retrieve-the-current-windows-codepage-in-php 
			// thus, we force the locale to be explicitly set to the default system locale
			$codepage = 'Windows-' . trim( strstr( setlocale( LC_CTYPE, '' ), '.' ), '.' );
		}
		else {
			$codepage = 'Windows-' . trim( strstr( setlocale( LC_CTYPE, 0 ), '.' ), '.' );
		}
		if ( function_exists( 'iconv' ) ) {
			if ( false == $utf8 ){
				$filename = iconv( get_option( 'blog_charset' ), $codepage . '//IGNORE', $filename );
			}
			else {
				$filename = iconv( $codepage, get_option( 'blog_charset' ), $filename );
			}
		} elseif ( function_exists( 'mb_convert_encoding' ) ) {
			if ( false == $utf8 )
				$filename = mb_convert_encoding( $filename, $codepage, get_option( 'blog_charset' ) );
			else
				$filename = mb_convert_encoding( $filename, get_option( 'blog_charset' ), $codepage );
		}
	}

	return $filename;
}
add_filter('wp_delete_file', 'autfw_sanitize_file_name_for_windows');
add_filter('get_attached_file', 'autfw_sanitize_file_name_for_windows');

function autfw_handle_upload_input($file) {	
	$file['name'] = autfw_sanitize_file_name_for_windows($file['name']);
	return $file;
}
add_filter('wp_handle_upload_prefilter', 'autfw_handle_upload_input');

function autfw_handle_upload($file) {
	$file['file'] = autfw_sanitize_file_name_for_windows($file['file'], true);
	$file['url'] = autfw_sanitize_file_name_for_windows($file['url'], true);
	return $file;
}
add_filter('wp_handle_upload', 'autfw_handle_upload');

function autfw_update_attached_file($file) {
	return autfw_sanitize_file_name_for_windows($file, true);
}
add_filter('update_attached_file', 'autfw_update_attached_file');

function autfw_generate_attachment_metadata($metadata, $attachment_id) {
	$file = get_attached_file($attachment_id);

	remove_filter('wp_generate_attachment_metadata', 'autfw_generate_attachment_metadata');
	$metadata = wp_generate_attachment_metadata($attachment_id, $file);

	return autfw_update_attachment_metadata($metadata);
}
add_filter('wp_generate_attachment_metadata', 'autfw_generate_attachment_metadata', 10, 2);

function autfw_update_attachment_metadata($metadata) {
	$metadata['file'] = autfw_sanitize_file_name_for_windows($metadata['file'], true);

	if ( !empty($metadata['sizes']) ) {
		foreach ( (array) $metadata['sizes'] as $size => $resized ) {
			$resized['file'] = autfw_sanitize_file_name_for_windows($resized['file'], true);
			$metadata['sizes'][$size] = $resized;
		}
	}

	return $metadata;
}
add_filter('wp_update_attachment_metadata', 'autfw_update_attachment_metadata');

function autfw_update_post_metadata($foo, $object_id, $meta_key, $meta_value) {
	if ( '_wp_attachment_backup_sizes' !=  $meta_key )
		return null;

	foreach ( (array) $meta_value as $size => $resized ) {
		$resized['file'] = autfw_sanitize_file_name_for_windows($resized['file'], true);
		$meta_value[$size] = $resized;
	}

	remove_filter('update_post_metadata', 'autfw_update_post_metadata', 10, 4);
	update_metadata('post', $object_id, $meta_key, $meta_value);

	return true;
}
add_filter('update_post_metadata', 'autfw_update_post_metadata', 10, 4);
?>