Make WordPress Core

Ticket #15955: allow-utf8-filenames-on-windows.2.php

File allow-utf8-filenames-on-windows.2.php, 3.9 KB (added by Perepandel, 11 years ago)

Modification to SergeyBiryukov workaround in order to work when setlocale is returning "C" as Windows code page, which seems to be the case in recent PHP versions.

Line 
1<?php
2/*
3Plugin Name: Allow UTF-8 Filenames on Windows
4Plugin URI: http://core.trac.wordpress.org/ticket/15955
5Description: A workaround for correct uploading of files with UTF-8 names on Windows systems.
6Version: 0.1
7Author: Sergey Biryukov
8Author URI: http://profiles.wordpress.org/sergeybiryukov/
9*/
10
11function 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}
41add_filter('wp_delete_file', 'autfw_sanitize_file_name_for_windows');
42add_filter('get_attached_file', 'autfw_sanitize_file_name_for_windows');
43
44function autfw_handle_upload_input($file) {     
45        $file['name'] = autfw_sanitize_file_name_for_windows($file['name']);
46        return $file;
47}
48add_filter('wp_handle_upload_prefilter', 'autfw_handle_upload_input');
49
50function 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}
55add_filter('wp_handle_upload', 'autfw_handle_upload');
56
57function autfw_update_attached_file($file) {
58        return autfw_sanitize_file_name_for_windows($file, true);
59}
60add_filter('update_attached_file', 'autfw_update_attached_file');
61
62function 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}
70add_filter('wp_generate_attachment_metadata', 'autfw_generate_attachment_metadata', 10, 2);
71
72function 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}
84add_filter('wp_update_attachment_metadata', 'autfw_update_attachment_metadata');
85
86function 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}
100add_filter('update_post_metadata', 'autfw_update_post_metadata', 10, 4);
101?>