Make WordPress Core

Ticket #42437: 42437.3.diff

File 42437.3.diff, 5.7 KB (added by azaozz, 5 years ago)
  • src/wp-includes/functions.php

     
    24092409        // Separate the filename into a name and extension.
    24102410        $ext  = pathinfo( $filename, PATHINFO_EXTENSION );
    24112411        $name = pathinfo( $filename, PATHINFO_BASENAME );
     2412
    24122413        if ( $ext ) {
    24132414                $ext = '.' . $ext;
    24142415        }
     
    24332434                        $filename2 = preg_replace( '|' . preg_quote( $ext ) . '$|', $ext2, $filename );
    24342435
    24352436                        // Check for both lower and upper case extension or image sub-sizes may be overwritten.
    2436                         while ( file_exists( $dir . "/$filename" ) || file_exists( $dir . "/$filename2" ) ) {
     2437                        while ( file_exists( $dir . "/{$filename}" ) || file_exists( $dir . "/{$filename2}" ) ) {
    24372438                                $new_number = (int) $number + 1;
    2438                                 $filename   = str_replace( array( "-$number$ext", "$number$ext" ), "-$new_number$ext", $filename );
    2439                                 $filename2  = str_replace( array( "-$number$ext2", "$number$ext2" ), "-$new_number$ext2", $filename2 );
     2439                                $filename   = str_replace( array( "-{$number}{$ext}", "{$number}{$ext}" ), "-{$new_number}{$ext}", $filename );
     2440                                $filename2  = str_replace( array( "-{$number}{$ext2}", "{$number}{$ext2}" ), "-{$new_number}{$ext2}", $filename2 );
    24402441                                $number     = $new_number;
    24412442                        }
    24422443
    2443                         /**
    2444                          * Filters the result when generating a unique file name.
    2445                          *
    2446                          * @since 4.5.0
    2447                          *
    2448                          * @param string        $filename                 Unique file name.
    2449                          * @param string        $ext                      File extension, eg. ".png".
    2450                          * @param string        $dir                      Directory path.
    2451                          * @param callable|null $unique_filename_callback Callback function that generates the unique file name.
    2452                          */
    2453                         return apply_filters( 'wp_unique_filename', $filename2, $ext, $dir, $unique_filename_callback );
     2444                        $filename = $filename2;
     2445                } else {
     2446                        while ( file_exists( $dir . "/{$filename}" ) ) {
     2447                                $new_number = (int) $number + 1;
     2448
     2449                                if ( '' === "{$number}{$ext}" ) {
     2450                                        $filename = "{$filename}-{$new_number}";
     2451                                } else {
     2452                                        $filename = str_replace( array( "-{$number}{$ext}", "{$number}{$ext}" ), "-{$new_number}{$ext}", $filename );
     2453                                }
     2454
     2455                                $number = $new_number;
     2456                        }
    24542457                }
    24552458
    2456                 while ( file_exists( $dir . "/$filename" ) ) {
    2457                         $new_number = (int) $number + 1;
    2458                         if ( '' == "$number$ext" ) {
    2459                                 $filename = "$filename-" . $new_number;
    2460                         } else {
    2461                                 $filename = str_replace( array( "-$number$ext", "$number$ext" ), '-' . $new_number . $ext, $filename );
     2459                // Prevent collisions with existing file names that contain dimension-like strings
     2460                // (whether they are subsizes or originals uploaded prior to #42437).
     2461
     2462                // The (resized) image files would have name and extension.
     2463                if ( $name && $ext ) {
     2464                        // List of all files and directories contained in $dir (with the "dot" files removed).
     2465                        $files = array_diff( scandir( $dir ), array( '.', '..' ) );
     2466
     2467                        if ( ! empty( $files ) ) {
     2468                                while ( _wp_check_existing_file_names( $filename, $files ) ) {
     2469                                        $new_number = (int) $number + 1;
     2470                                        $filename   = str_replace( array( "-{$number}{$ext}", "{$number}{$ext}" ), "-{$new_number}{$ext}", $filename );
     2471                                        $number     = $new_number;
     2472                                }
    24622473                        }
    2463                         $number = $new_number;
    24642474                }
    24652475        }
    24662476
    2467         /** This filter is documented in wp-includes/functions.php */
     2477        /**
     2478         * Filters the result when generating a unique file name.
     2479         *
     2480         * @since 4.5.0
     2481         *
     2482         * @param string        $filename                 Unique file name.
     2483         * @param string        $ext                      File extension, eg. ".png".
     2484         * @param string        $dir                      Directory path.
     2485         * @param callable|null $unique_filename_callback Callback function that generates the unique file name.
     2486         */
    24682487        return apply_filters( 'wp_unique_filename', $filename, $ext, $dir, $unique_filename_callback );
    24692488}
    24702489
    24712490/**
     2491 * Helper function to check if a file name could match an existing image sub-size file name.
     2492 *
     2493 * @since 5.3.1
     2494 *
     2495 * @param string $filename The file name to check.
     2496 * $param array  $files    An array of existing files in the directory.
     2497 * $return bool True if the tested file name could match an existing file, false otherwise.
     2498 */
     2499function _wp_check_existing_file_names( $filename, $files ) {
     2500        $fname = pathinfo( $filename, PATHINFO_FILENAME );
     2501        $ext   = pathinfo( $filename, PATHINFO_EXTENSION );
     2502
     2503        // Edge case, file names like `.ext`
     2504        if ( empty( $fname ) ) {
     2505                return false;
     2506        }
     2507
     2508        if ( $ext ) {
     2509                $ext = ".$ext";
     2510        }
     2511
     2512        $regex = '/^' . preg_quote( $fname ) . '-(?:\d+x\d+|scaled|rotated)' . preg_quote( $ext ) . '/';
     2513
     2514        foreach ( $files as $file ) {
     2515                if ( preg_match( $regex, $file ) ) {
     2516                        return true;
     2517                }
     2518        }
     2519
     2520        return false;
     2521}
     2522
     2523/**
    24722524 * Create a file in the upload folder with given content.
    24732525 *
    24742526 * If there is an error, then the key 'error' will exist with the error message.
  • tests/phpunit/tests/functions.php

     
    195195                $this->assertEquals( 'abcdefg.png', wp_unique_filename( $testdir, 'abcde\\\fg.png' ), 'Tripple slashed not removed' );
    196196        }
    197197
     198        /**
     199         * @group 42437
     200         */
     201        function test_unique_filename_with_dimension_like_filename() {
     202                $testdir = DIR_TESTDATA . '/images/';
     203
     204                // test collision with "dimension-like" original filename.
     205                $this->assertEquals( 'one-blue-pixel-100x100-1.png', wp_unique_filename( $testdir, 'one-blue-pixel-100x100.png' ) );
     206                // test collision with existing sub-size filename.
     207                $this->assertEquals( 'one-blue-pixel-1.png', wp_unique_filename( $testdir, 'one-blue-pixel.png' ) );
     208        }
     209
    198210        function test_is_serialized() {
    199211                $cases = array(
    200212                        serialize( null ),