Make WordPress Core

Changeset 43396


Ignore:
Timestamp:
07/05/2018 02:52:09 PM (8 years ago)
Author:
johnbillion
Message:

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

Merges [43393] to the 4.6 branch.

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

Legend:

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

    r42279 r43396  
    17031703 * @since 4.4.0 Ensures upper-case drive letters on Windows systems.
    17041704 * @since 4.5.0 Allows for Windows network shares.
     1705 * @since 4.9.7 Allows for PHP file wrappers.
    17051706 *
    17061707 * @param string $path Path to normalize.
    … …  
    17081709 */
    17091710function wp_normalize_path( $path ) {
     1711        $wrapper = '';
     1712        if ( wp_is_stream( $path ) ) {
     1713                list( $wrapper, $path ) = explode( '://', $path, 2 );
     1714                $wrapper .= '://';
     1715        }
     1716
     1717        // Standardise all paths to use /
    17101718        $path = str_replace( '\\', '/', $path );
     1719
     1720        // Replace multiple slashes down to a singular, allowing for network shares having two slashes.
    17111721        $path = preg_replace( '|(?<=.)/+|', '/', $path );
     1722
     1723        // Windows paths should uppercase the drive letter
    17121724        if ( ':' === substr( $path, 1, 1 ) ) {
    17131725                $path = ucfirst( $path );
    17141726        }
    1715         return $path;
     1727
     1728        return $wrapper . $path;
    17161729}
    17171730
    … …  
    53545367
    53555368/**
     5369 * Deletes a file if its path is within the given directory.
     5370 *
     5371 * @since 4.9.7
     5372 *
     5373 * @param string $file      Absolute path to the file to delete.
     5374 * @param string $directory Absolute path to a directory.
     5375 * @return bool True on success, false on failure.
     5376 */
     5377function wp_delete_file_from_directory( $file, $directory ) {
     5378        $real_file = realpath( wp_normalize_path( $file ) );
     5379        $real_directory = realpath( wp_normalize_path( $directory ) );
     5380
     5381        if ( false === $real_file || false === $real_directory || strpos( wp_normalize_path( $real_file ), trailingslashit( wp_normalize_path( $real_directory ) ) ) !== 0 ) {
     5382                return false;
     5383        }
     5384
     5385        wp_delete_file( $file );
     5386
     5387        return true;
     5388}
     5389
     5390/**
    53565391 * Outputs a small JS snippet on preview tabs/windows to remove `window.name` on unload.
    53575392 *
  • branches/4.6/src/wp-includes/post.php

    r42059 r43396  
    47984798        do_action( 'deleted_post', $post_id );
    47994799
     4800        wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file );
     4801
     4802        clean_post_cache( $post );
     4803
     4804        return $post;
     4805}
     4806
     4807/**
     4808 * Deletes all files that belong to the given attachment.
     4809 *
     4810 * @since 4.9.7
     4811 *
     4812 * @param int    $post_id      Attachment ID.
     4813 * @param array  $meta         The attachment's meta data.
     4814 * @param array  $backup_sizes The meta data for the attachment's backup images.
     4815 * @param string $file         Absolute path to the attachment's file.
     4816 * @return bool True on success, false on failure.
     4817 */
     4818function wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file ) {
     4819        global $wpdb;
     4820
    48004821        $uploadpath = wp_get_upload_dir();
    4801 
    4802         if ( ! empty($meta['thumb']) ) {
     4822        $deleted    = true;
     4823
     4824        if ( ! empty( $meta['thumb'] ) ) {
    48034825                // Don't delete the thumb if another attachment uses it.
    4804                 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)) ) {
    4805                         $thumbfile = str_replace(basename($file), $meta['thumb'], $file);
    4806                         /** This filter is documented in wp-includes/functions.php */
    4807                         $thumbfile = apply_filters( 'wp_delete_file', $thumbfile );
    4808                         @ unlink( path_join($uploadpath['basedir'], $thumbfile) );
     4826                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 ) ) ) {
     4827                        $thumbfile = str_replace( basename( $file ), $meta['thumb'], $file );
     4828                        if ( ! empty( $thumbfile ) ) {
     4829                                $thumbfile = path_join( $uploadpath['basedir'], $thumbfile );
     4830                                $thumbdir  = path_join( $uploadpath['basedir'], dirname( $file ) );
     4831
     4832                                if ( ! wp_delete_file_from_directory( $thumbfile, $thumbdir ) ) {
     4833                                        $deleted = false;
     4834                                }
     4835                        }
    48094836                }
    48104837        }
    … …  
    48124839        // Remove intermediate and backup images if there are any.
    48134840        if ( isset( $meta['sizes'] ) && is_array( $meta['sizes'] ) ) {
     4841                $intermediate_dir = path_join( $uploadpath['basedir'], dirname( $file ) );
    48144842                foreach ( $meta['sizes'] as $size => $sizeinfo ) {
    48154843                        $intermediate_file = str_replace( basename( $file ), $sizeinfo['file'], $file );
    4816                         /** This filter is documented in wp-includes/functions.php */
    4817                         $intermediate_file = apply_filters( 'wp_delete_file', $intermediate_file );
    4818                         @ unlink( path_join( $uploadpath['basedir'], $intermediate_file ) );
    4819                 }
    4820         }
    4821 
    4822         if ( is_array($backup_sizes) ) {
     4844                        if ( ! empty( $intermediate_file ) ) {
     4845                                $intermediate_file = path_join( $uploadpath['basedir'], $intermediate_file );
     4846
     4847                                if ( ! wp_delete_file_from_directory( $intermediate_file, $intermediate_dir ) ) {
     4848                                        $deleted = false;
     4849                                }
     4850                        }
     4851                }
     4852        }
     4853
     4854        if ( is_array( $backup_sizes ) ) {
     4855                $del_dir = path_join( $uploadpath['basedir'], dirname( $meta['file'] ) );
    48234856                foreach ( $backup_sizes as $size ) {
    4824                         $del_file = path_join( dirname($meta['file']), $size['file'] );
    4825                         /** This filter is documented in wp-includes/functions.php */
    4826                         $del_file = apply_filters( 'wp_delete_file', $del_file );
    4827                         @ unlink( path_join($uploadpath['basedir'], $del_file) );
    4828                 }
    4829         }
    4830 
    4831         wp_delete_file( $file );
    4832 
    4833         clean_post_cache( $post );
    4834 
    4835         return $post;
     4857                        $del_file = path_join( dirname( $meta['file'] ), $size['file'] );
     4858                        if ( ! empty( $del_file ) ) {
     4859                                $del_file = path_join( $uploadpath['basedir'], $del_file );
     4860
     4861                                if ( ! wp_delete_file_from_directory( $del_file, $del_dir ) ) {
     4862                                        $deleted = false;
     4863                                }
     4864                        }
     4865                }
     4866        }
     4867
     4868        if ( ! wp_delete_file_from_directory( $file, $uploadpath['basedir'] ) ) {
     4869                $deleted = false;
     4870        }
     4871
     4872        return $deleted;
    48364873}
    48374874
Note: See TracChangeset for help on using the changeset viewer.