Make WordPress Core

Changeset 46966


Ignore:
Timestamp:
12/16/2019 11:42:34 PM (7 years ago)
Author:
azaozz
Message:

Upload: Fix the final file name collision test in wp_unique_filename() when uploading a file with upper case extension. Add a unit test to catch that in the future.

Fixes #48975 for trunk.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/functions.php

    r46965 r46966  
    24112411        // Sanitize the file name before we begin processing.
    24122412        $filename = sanitize_file_name( $filename );
     2413        $ext2     = null;
    24132414
    24142415        // Separate the filename into a name and extension.
     
    24862487
    24872488                        if ( ! empty( $files ) ) {
    2488                                 while ( _wp_check_existing_file_names( $filename, $files ) ) {
     2489                                // The extension case may have changed above.
     2490                                $new_ext = ! empty( $ext2 ) ? $ext2 : $ext;
     2491
     2492                                // Ensure this never goes into infinite loop
     2493                                // as it uses pathinfo() and regex in the check but string replacement for the changes.
     2494                                $count = count( $files );
     2495                                $i     = 1;
     2496
     2497                                while ( $i <= $count && _wp_check_existing_file_names( $filename, $files ) ) {
    24892498                                        $new_number = (int) $number + 1;
    2490                                         $filename   = str_replace( array( "-{$number}{$ext}", "{$number}{$ext}" ), "-{$new_number}{$ext}", $filename );
     2499                                        $filename   = str_replace( array( "-{$number}{$new_ext}", "{$number}{$new_ext}" ), "-{$new_number}{$new_ext}", $filename );
    24912500                                        $number     = $new_number;
     2501                                        $i++;
    24922502                                }
    24932503                        }
     
    25312541        }
    25322542
    2533         $regex = '/^' . preg_quote( $fname ) . '-(?:\d+x\d+|scaled|rotated)' . preg_quote( $ext ) . '$/';
     2543        $regex = '/^' . preg_quote( $fname ) . '-(?:\d+x\d+|scaled|rotated)' . preg_quote( $ext ) . '$/i';
    25342544
    25352545        foreach ( $files as $file ) {
  • trunk/tests/phpunit/tests/functions.php

    r46965 r46966  
    209209                // Existing files: one-blue-pixel-100x100.png, one-blue-pixel-1-100x100.png.
    210210                $this->assertEquals( 'one-blue-pixel-2.png', wp_unique_filename( $testdir, 'one-blue-pixel.png' ) );
     211                // Same as above with upper case extension.
     212                $this->assertEquals( 'one-blue-pixel-2.png', wp_unique_filename( $testdir, 'one-blue-pixel.PNG' ) );
    211213
    212214                remove_filter( 'upload_dir', array( $this, 'upload_dir_patch_basedir' ) );
Note: See TracChangeset for help on using the changeset viewer.