| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | Plugin Name: Allow UTF-8 Filenames on Windows |
|---|
| 4 | Plugin URI: http://core.trac.wordpress.org/ticket/15955 |
|---|
| 5 | Description: A workaround for correct uploading of files with UTF-8 names on Windows systems. |
|---|
| 6 | Version: 0.1 |
|---|
| 7 | Author: Sergey Biryukov |
|---|
| 8 | Author URI: http://profiles.wordpress.org/sergeybiryukov/ |
|---|
| 9 | */ |
|---|
| 10 | |
|---|
| 11 | function autfw_sanitize_file_name_for_windows($filename, $utf8 = false) { |
|---|
| 12 | if ( seems_utf8($filename) == $utf8 ) |
|---|
| 13 | return $filename; |
|---|
| 14 | |
|---|
| 15 | // On Windows platforms, PHP will mangle non-ASCII characters, see http://bugs.php.net/bug.php?id=47096 |
|---|
| 16 | if ( 'WIN' == substr( PHP_OS, 0, 3 ) ) { |
|---|
| 17 | 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 |
|---|
| 18 | // thus, we force the locale to be explicitly set to the default system locale |
|---|
| 19 | $codepage = 'Windows-' . trim( strstr( setlocale( LC_CTYPE, '' ), '.' ), '.' ); |
|---|
| 20 | } |
|---|
| 21 | else { |
|---|
| 22 | $codepage = 'Windows-' . trim( strstr( setlocale( LC_CTYPE, 0 ), '.' ), '.' ); |
|---|
| 23 | } |
|---|
| 24 | if ( function_exists( 'iconv' ) ) { |
|---|
| 25 | if ( false == $utf8 ){ |
|---|
| 26 | $filename = iconv( get_option( 'blog_charset' ), $codepage . '//IGNORE', $filename ); |
|---|
| 27 | } |
|---|
| 28 | else { |
|---|
| 29 | $filename = iconv( $codepage, get_option( 'blog_charset' ), $filename ); |
|---|
| 30 | } |
|---|
| 31 | } elseif ( function_exists( 'mb_convert_encoding' ) ) { |
|---|
| 32 | if ( false == $utf8 ) |
|---|
| 33 | $filename = mb_convert_encoding( $filename, $codepage, get_option( 'blog_charset' ) ); |
|---|
| 34 | else |
|---|
| 35 | $filename = mb_convert_encoding( $filename, get_option( 'blog_charset' ), $codepage ); |
|---|
| 36 | } |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | return $filename; |
|---|
| 40 | } |
|---|
| 41 | add_filter('wp_delete_file', 'autfw_sanitize_file_name_for_windows'); |
|---|
| 42 | add_filter('get_attached_file', 'autfw_sanitize_file_name_for_windows'); |
|---|
| 43 | |
|---|
| 44 | function autfw_handle_upload_input($file) { |
|---|
| 45 | $file['name'] = autfw_sanitize_file_name_for_windows($file['name']); |
|---|
| 46 | return $file; |
|---|
| 47 | } |
|---|
| 48 | add_filter('wp_handle_upload_prefilter', 'autfw_handle_upload_input'); |
|---|
| 49 | |
|---|
| 50 | function autfw_handle_upload($file) { |
|---|
| 51 | $file['file'] = autfw_sanitize_file_name_for_windows($file['file'], true); |
|---|
| 52 | $file['url'] = autfw_sanitize_file_name_for_windows($file['url'], true); |
|---|
| 53 | return $file; |
|---|
| 54 | } |
|---|
| 55 | add_filter('wp_handle_upload', 'autfw_handle_upload'); |
|---|
| 56 | |
|---|
| 57 | function autfw_update_attached_file($file) { |
|---|
| 58 | return autfw_sanitize_file_name_for_windows($file, true); |
|---|
| 59 | } |
|---|
| 60 | add_filter('update_attached_file', 'autfw_update_attached_file'); |
|---|
| 61 | |
|---|
| 62 | function autfw_generate_attachment_metadata($metadata, $attachment_id) { |
|---|
| 63 | $file = get_attached_file($attachment_id); |
|---|
| 64 | |
|---|
| 65 | remove_filter('wp_generate_attachment_metadata', 'autfw_generate_attachment_metadata'); |
|---|
| 66 | $metadata = wp_generate_attachment_metadata($attachment_id, $file); |
|---|
| 67 | |
|---|
| 68 | return autfw_update_attachment_metadata($metadata); |
|---|
| 69 | } |
|---|
| 70 | add_filter('wp_generate_attachment_metadata', 'autfw_generate_attachment_metadata', 10, 2); |
|---|
| 71 | |
|---|
| 72 | function autfw_update_attachment_metadata($metadata) { |
|---|
| 73 | $metadata['file'] = autfw_sanitize_file_name_for_windows($metadata['file'], true); |
|---|
| 74 | |
|---|
| 75 | if ( !empty($metadata['sizes']) ) { |
|---|
| 76 | foreach ( (array) $metadata['sizes'] as $size => $resized ) { |
|---|
| 77 | $resized['file'] = autfw_sanitize_file_name_for_windows($resized['file'], true); |
|---|
| 78 | $metadata['sizes'][$size] = $resized; |
|---|
| 79 | } |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | return $metadata; |
|---|
| 83 | } |
|---|
| 84 | add_filter('wp_update_attachment_metadata', 'autfw_update_attachment_metadata'); |
|---|
| 85 | |
|---|
| 86 | function autfw_update_post_metadata($foo, $object_id, $meta_key, $meta_value) { |
|---|
| 87 | if ( '_wp_attachment_backup_sizes' != $meta_key ) |
|---|
| 88 | return null; |
|---|
| 89 | |
|---|
| 90 | foreach ( (array) $meta_value as $size => $resized ) { |
|---|
| 91 | $resized['file'] = autfw_sanitize_file_name_for_windows($resized['file'], true); |
|---|
| 92 | $meta_value[$size] = $resized; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | remove_filter('update_post_metadata', 'autfw_update_post_metadata', 10, 4); |
|---|
| 96 | update_metadata('post', $object_id, $meta_key, $meta_value); |
|---|
| 97 | |
|---|
| 98 | return true; |
|---|
| 99 | } |
|---|
| 100 | add_filter('update_post_metadata', 'autfw_update_post_metadata', 10, 4); |
|---|
| 101 | ?> |
|---|