Make WordPress Core

Changeset 46980


Ignore:
Timestamp:
12/17/2019 09:18:07 PM (5 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 and limit it to run for each file in the directory + 1. Add a unit test to catch that in the future.

Props pbiron, azaozz.
Merges [46966] and [46976] to the 5.3 branch.
Fixes #48975.

Location:
branches/5.3
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/5.3

  • branches/5.3/src/wp-includes/functions.php

    r46979 r46980  
    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     = 0;
     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 ) {
  • branches/5.3/tests/phpunit/tests/functions.php

    r46979 r46980  
    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.