Ticket #9416: 9416.3.diff

File 9416.3.diff, 3.3 KB (added by sivel, 4 years ago)
Line 
1Index: wp-includes/functions.php
2===================================================================
3--- wp-includes/functions.php   (revision 11025)
4+++ wp-includes/functions.php   (working copy)
5@@ -2011,12 +2011,14 @@
6  * @return string New filename, if given wasn't unique.
7  */
8 function wp_unique_filename( $dir, $filename, $unique_filename_callback = null ) {
9-       $filename = strtolower( $filename );
10+       // sanitize the file name before we begin processing
11+       $filename = sanitize_file_name($filename);
12+
13        // separate the filename into a name and extension
14        $info = pathinfo($filename);
15        $ext = !empty($info['extension']) ? $info['extension'] : '';
16        $name = basename($filename, ".{$ext}");
17-
18+       
19        // edge case: if file is named '.ext', treat as an empty name
20        if( $name === ".$ext" )
21                $name = '';
22@@ -2028,12 +2030,8 @@
23                $number = '';
24 
25                if ( !empty( $ext ) )
26-                       $ext = strtolower( ".$ext" );
27+                       $ext = ".$ext";
28 
29-               $filename = str_replace( $ext, '', $filename );
30-               // Strip % so the server doesn't try to decode entities.
31-               $filename = str_replace('%', '', sanitize_title_with_dashes( $filename ) ) . $ext;
32-
33                while ( file_exists( $dir . "/$filename" ) ) {
34                        if ( '' == "$number$ext" )
35                                $filename = $filename . ++$number . $ext;
36Index: wp-includes/formatting.php
37===================================================================
38--- wp-includes/formatting.php  (revision 11025)
39+++ wp-includes/formatting.php  (working copy)
40@@ -564,27 +564,27 @@
41 }
42 
43 /**
44- * Filters certain characters from the file name.
45+ * Sanitizes a filename replacing whitespace with dashes
46  *
47- * Turns all strings to lowercase removing most characters except alphanumeric
48- * with spaces, dashes and periods. All spaces and underscores are converted to
49- * dashes. Multiple dashes are converted to a single dash. Finally, if the file
50- * name ends with a dash, it is removed.
51+ * Removes special characters that are illegal in filenames on certain
52+ * operating systems and special characters requiring special escaping
53+ * to manipulate at the command line. Replaces spaces and consecutive
54+ * dashes with a single dash. Trim period, dash and underscore from beginning
55+ * and end of filename.
56  *
57  * @since 2.1.0
58  *
59- * @param string $name The file name
60- * @return string Sanitized file name
61+ * @param string $filename The filename to be sanitized
62+ * @return string The sanitized filename
63  */
64-function sanitize_file_name( $name ) { // Like sanitize_title, but with periods
65-       $name = strtolower( $name );
66-       $name = preg_replace('/&.+?;/', '', $name); // kill entities
67-       $name = str_replace( '_', '-', $name );
68-       $name = preg_replace('/[^a-z0-9\s-.]/', '', $name);
69-       $name = preg_replace('/\s+/', '-', $name);
70-       $name = preg_replace('|-+|', '-', $name);
71-       $name = trim($name, '-');
72-       return $name;
73+function sanitize_file_name( $filename ) {
74+       $filename_raw = $filename;
75+       $special_chars = array("?", "[", "]", "/", "\\", "=", "<", ">", ":", ";", ",", "'", "\"", "&", "$", "#", "*", "(", ")", "|", "~", "`", "!", "{", "}");
76+       $special_chars = apply_filters('sanitize_file_name_chars', $special_chars, $filename_raw);
77+       $filename = str_replace($special_chars, '', $filename);
78+       $filename = preg_replace('/[\s-]+/', '-', $filename);
79+       $filename = trim($filename, '.-_');
80+       return apply_filters('sanitize_file_name', $filename, $filename_raw);
81 }
82 
83 /**