Make WordPress Core

Changeset 43395


Ignore:
Timestamp:
07/05/2018 02:50:26 PM (6 years ago)
Author:
johnbillion
Message:

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

Merges [43393] into the 4.7 branch.

Location:
branches/4.7/src/wp-includes
Files:
2 edited

Legend:

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

    r42275 r43395  
    17001700 * @since 4.4.0 Ensures upper-case drive letters on Windows systems.
    17011701 * @since 4.5.0 Allows for Windows network shares.
     1702 * @since 4.9.7 Allows for PHP file wrappers.
    17021703 *
    17031704 * @param string $path Path to normalize.
     
    17051706 */
    17061707function wp_normalize_path( $path ) {
     1708    $wrapper = '';
     1709    if ( wp_is_stream( $path ) ) {
     1710        list( $wrapper, $path ) = explode( '://', $path, 2 );
     1711        $wrapper .= '://';
     1712    }
     1713
     1714    // Standardise all paths to use /
    17071715    $path = str_replace( '\\', '/', $path );
     1716
     1717    // Replace multiple slashes down to a singular, allowing for network shares having two slashes.
    17081718    $path = preg_replace( '|(?<=.)/+|', '/', $path );
     1719
     1720    // Windows paths should uppercase the drive letter
    17091721    if ( ':' === substr( $path, 1, 1 ) ) {
    17101722        $path = ucfirst( $path );
    17111723    }
    1712     return $path;
     1724
     1725    return $wrapper . $path;
    17131726}
    17141727
     
    54475460
    54485461/**
     5462 * Deletes a file if its path is within the given directory.
     5463 *
     5464 * @since 4.9.7
     5465 *
     5466 * @param string $file      Absolute path to the file to delete.
     5467 * @param string $directory Absolute path to a directory.
     5468 * @return bool True on success, false on failure.
     5469 */
     5470function wp_delete_file_from_directory( $file, $directory ) {
     5471    $real_file = realpath( wp_normalize_path( $file ) );
     5472    $real_directory = realpath( wp_normalize_path( $directory ) );
     5473
     5474    if ( false === $real_file || false === $real_directory || strpos( wp_normalize_path( $real_file ), trailingslashit( wp_normalize_path( $real_directory ) ) ) !== 0 ) {
     5475        return false;
     5476    }
     5477
     5478    wp_delete_file( $file );
     5479
     5480    return true;
     5481}
     5482
     5483/**
    54495484 * Outputs a small JS snippet on preview tabs/windows to remove `window.name` on unload.
    54505485 *
  • branches/4.7/src/wp-includes/post.php

    r42058 r43395  
    49154915    do_action( 'deleted_post', $post_id );
    49164916
     4917    wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file );
     4918
     4919    clean_post_cache( $post );
     4920
     4921    return $post;
     4922}
     4923
     4924/**
     4925 * Deletes all files that belong to the given attachment.
     4926 *
     4927 * @since 4.9.7
     4928 *
     4929 * @param int    $post_id      Attachment ID.
     4930 * @param array  $meta         The attachment's meta data.
     4931 * @param array  $backup_sizes The meta data for the attachment's backup images.
     4932 * @param string $file         Absolute path to the attachment's file.
     4933 * @return bool True on success, false on failure.
     4934 */
     4935function wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file ) {
     4936    global $wpdb;
     4937
    49174938    $uploadpath = wp_get_upload_dir();
    4918 
    4919     if ( ! empty($meta['thumb']) ) {
     4939    $deleted    = true;
     4940
     4941    if ( ! empty( $meta['thumb'] ) ) {
    49204942        // Don't delete the thumb if another attachment uses it.
    4921         if (! $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attachment_metadata' AND meta_value LIKE %s AND post_id <> %d", '%' . $wpdb->esc_like( $meta['thumb'] ) . '%', $post_id)) ) {
    4922             $thumbfile = str_replace(basename($file), $meta['thumb'], $file);
    4923             /** This filter is documented in wp-includes/functions.php */
    4924             $thumbfile = apply_filters( 'wp_delete_file', $thumbfile );
    4925             @ unlink( path_join($uploadpath['basedir'], $thumbfile) );
     4943        if ( ! $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attachment_metadata' AND meta_value LIKE %s AND post_id <> %d", '%' . $wpdb->esc_like( $meta['thumb'] ) . '%', $post_id ) ) ) {
     4944            $thumbfile = str_replace( basename( $file ), $meta['thumb'], $file );
     4945            if ( ! empty( $thumbfile ) ) {
     4946                $thumbfile = path_join( $uploadpath['basedir'], $thumbfile );
     4947                $thumbdir  = path_join( $uploadpath['basedir'], dirname( $file ) );
     4948
     4949                if ( ! wp_delete_file_from_directory( $thumbfile, $thumbdir ) ) {
     4950                    $deleted = false;
     4951                }
     4952            }
    49264953        }
    49274954    }
     
    49294956    // Remove intermediate and backup images if there are any.
    49304957    if ( isset( $meta['sizes'] ) && is_array( $meta['sizes'] ) ) {
     4958        $intermediate_dir = path_join( $uploadpath['basedir'], dirname( $file ) );
    49314959        foreach ( $meta['sizes'] as $size => $sizeinfo ) {
    49324960            $intermediate_file = str_replace( basename( $file ), $sizeinfo['file'], $file );
    4933             /** This filter is documented in wp-includes/functions.php */
    4934             $intermediate_file = apply_filters( 'wp_delete_file', $intermediate_file );
    4935             @ unlink( path_join( $uploadpath['basedir'], $intermediate_file ) );
    4936         }
    4937     }
    4938 
    4939     if ( is_array($backup_sizes) ) {
     4961            if ( ! empty( $intermediate_file ) ) {
     4962                $intermediate_file = path_join( $uploadpath['basedir'], $intermediate_file );
     4963
     4964                if ( ! wp_delete_file_from_directory( $intermediate_file, $intermediate_dir ) ) {
     4965                    $deleted = false;
     4966                }
     4967            }
     4968        }
     4969    }
     4970
     4971    if ( is_array( $backup_sizes ) ) {
     4972        $del_dir = path_join( $uploadpath['basedir'], dirname( $meta['file'] ) );
    49404973        foreach ( $backup_sizes as $size ) {
    4941             $del_file = path_join( dirname($meta['file']), $size['file'] );
    4942             /** This filter is documented in wp-includes/functions.php */
    4943             $del_file = apply_filters( 'wp_delete_file', $del_file );
    4944             @ unlink( path_join($uploadpath['basedir'], $del_file) );
    4945         }
    4946     }
    4947 
    4948     wp_delete_file( $file );
    4949 
    4950     clean_post_cache( $post );
    4951 
    4952     return $post;
     4974            $del_file = path_join( dirname( $meta['file'] ), $size['file'] );
     4975            if ( ! empty( $del_file ) ) {
     4976                $del_file = path_join( $uploadpath['basedir'], $del_file );
     4977
     4978                if ( ! wp_delete_file_from_directory( $del_file, $del_dir ) ) {
     4979                    $deleted = false;
     4980                }
     4981            }
     4982        }
     4983    }
     4984
     4985    if ( ! wp_delete_file_from_directory( $file, $uploadpath['basedir'] ) ) {
     4986        $deleted = false;
     4987    }
     4988
     4989    return $deleted;
    49534990}
    49544991
Note: See TracChangeset for help on using the changeset viewer.