Make WordPress Core


Ignore:
Timestamp:
07/05/2018 03:15:51 PM (7 years ago)
Author:
johnbillion
Message:

Media: Limit thumbnail file deletions to the same directory as the original file.

Merges [43393] into the 3.8 branch.

File:
1 edited

Legend:

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

    r42311 r43404  
    14321432
    14331433    return rtrim($base, '/') . '/' . ltrim($path, '/');
     1434}
     1435
     1436/**
     1437 * Normalize a filesystem path.
     1438 *
     1439 * On windows systems, replaces backslashes with forward slashes
     1440 * and forces upper-case drive letters.
     1441 * Allows for two leading slashes for Windows network shares, but
     1442 * ensures that all other duplicate slashes are reduced to a single.
     1443 *
     1444 * @since 3.9.0
     1445 * @since 4.4.0 Ensures upper-case drive letters on Windows systems.
     1446 * @since 4.5.0 Allows for Windows network shares.
     1447 * @since 4.9.7 Allows for PHP file wrappers.
     1448 *
     1449 * @param string $path Path to normalize.
     1450 * @return string Normalized path.
     1451 */
     1452function wp_normalize_path( $path ) {
     1453    $wrapper = '';
     1454    if ( wp_is_stream( $path ) ) {
     1455        list( $wrapper, $path ) = explode( '://', $path, 2 );
     1456        $wrapper .= '://';
     1457    }
     1458
     1459    // Standardise all paths to use /
     1460    $path = str_replace( '\\', '/', $path );
     1461
     1462    // Replace multiple slashes down to a singular, allowing for network shares having two slashes.
     1463    $path = preg_replace( '|(?<=.)/+|', '/', $path );
     1464
     1465    // Windows paths should uppercase the drive letter
     1466    if ( ':' === substr( $path, 1, 1 ) ) {
     1467        $path = ucfirst( $path );
     1468    }
     1469
     1470    return $wrapper . $path;
    14341471}
    14351472
     
    42384275    mbstring_binary_safe_encoding( true );
    42394276}
     4277
     4278/**
     4279 * Deletes a file if its path is within the given directory.
     4280 *
     4281 * @since 4.9.7
     4282 *
     4283 * @param string $file      Absolute path to the file to delete.
     4284 * @param string $directory Absolute path to a directory.
     4285 * @return bool True on success, false on failure.
     4286 */
     4287function wp_delete_file_from_directory( $file, $directory ) {
     4288    $real_file = realpath( wp_normalize_path( $file ) );
     4289    $real_directory = realpath( wp_normalize_path( $directory ) );
     4290
     4291    if ( false === $real_file || false === $real_directory || strpos( wp_normalize_path( $real_file ), trailingslashit( wp_normalize_path( $real_directory ) ) ) !== 0 ) {
     4292        return false;
     4293    }
     4294
     4295    /** This filter is documented in wp-admin/custom-header.php */
     4296    $delete = apply_filters( 'wp_delete_file', $file );
     4297    if ( ! empty( $delete ) ) {
     4298        @unlink( $delete );
     4299    }
     4300
     4301    return true;
     4302}
Note: See TracChangeset for help on using the changeset viewer.