Make WordPress Core

Changeset 43397


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

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

Merges [43393] into the 4.5 branch.

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

Legend:

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

    r42283 r43397  
    16941694 * @since 4.4.0 Ensures upper-case drive letters on Windows systems.
    16951695 * @since 4.5.0 Allows for Windows network shares.
     1696 * @since 4.9.7 Allows for PHP file wrappers.
    16961697 *
    16971698 * @param string $path Path to normalize.
    … …  
    16991700 */
    17001701function wp_normalize_path( $path ) {
     1702        $wrapper = '';
     1703        if ( wp_is_stream( $path ) ) {
     1704                list( $wrapper, $path ) = explode( '://', $path, 2 );
     1705                $wrapper .= '://';
     1706        }
     1707
     1708        // Standardise all paths to use /
    17011709        $path = str_replace( '\\', '/', $path );
     1710
     1711        // Replace multiple slashes down to a singular, allowing for network shares having two slashes.
    17021712        $path = preg_replace( '|(?<=.)/+|', '/', $path );
     1713
     1714        // Windows paths should uppercase the drive letter
    17031715        if ( ':' === substr( $path, 1, 1 ) ) {
    17041716                $path = ucfirst( $path );
    17051717        }
    1706         return $path;
     1718
     1719        return $wrapper . $path;
    17071720}
    17081721
    … …  
    52735286
    52745287/**
     5288 * Deletes a file if its path is within the given directory.
     5289 *
     5290 * @since 4.9.7
     5291 *
     5292 * @param string $file      Absolute path to the file to delete.
     5293 * @param string $directory Absolute path to a directory.
     5294 * @return bool True on success, false on failure.
     5295 */
     5296function wp_delete_file_from_directory( $file, $directory ) {
     5297        $real_file = realpath( wp_normalize_path( $file ) );
     5298        $real_directory = realpath( wp_normalize_path( $directory ) );
     5299
     5300        if ( false === $real_file || false === $real_directory || strpos( wp_normalize_path( $real_file ), trailingslashit( wp_normalize_path( $real_directory ) ) ) !== 0 ) {
     5301                return false;
     5302        }
     5303
     5304        wp_delete_file( $file );
     5305
     5306        return true;
     5307}
     5308
     5309/**
    52755310 * Outputs a small JS snippet on preview tabs/windows to remove `window.name` on unload.
    52765311 *
  • branches/4.5/src/wp-includes/post.php

    r42060 r43397  
    48964896        do_action( 'deleted_post', $post_id );
    48974897
     4898        wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file );
     4899
     4900        clean_post_cache( $post );
     4901
     4902        return $post;
     4903}
     4904
     4905/**
     4906 * Deletes all files that belong to the given attachment.
     4907 *
     4908 * @since 4.9.7
     4909 *
     4910 * @param int    $post_id      Attachment ID.
     4911 * @param array  $meta         The attachment's meta data.
     4912 * @param array  $backup_sizes The meta data for the attachment's backup images.
     4913 * @param string $file         Absolute path to the attachment's file.
     4914 * @return bool True on success, false on failure.
     4915 */
     4916function wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file ) {
     4917        global $wpdb;
     4918
    48984919        $uploadpath = wp_get_upload_dir();
    4899 
    4900         if ( ! empty($meta['thumb']) ) {
     4920        $deleted    = true;
     4921
     4922        if ( ! empty( $meta['thumb'] ) ) {
    49014923                // Don't delete the thumb if another attachment uses it.
    4902                 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)) ) {
    4903                         $thumbfile = str_replace(basename($file), $meta['thumb'], $file);
    4904                         /** This filter is documented in wp-includes/functions.php */
    4905                         $thumbfile = apply_filters( 'wp_delete_file', $thumbfile );
    4906                         @ unlink( path_join($uploadpath['basedir'], $thumbfile) );
     4924                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 ) ) ) {
     4925                        $thumbfile = str_replace( basename( $file ), $meta['thumb'], $file );
     4926                        if ( ! empty( $thumbfile ) ) {
     4927                                $thumbfile = path_join( $uploadpath['basedir'], $thumbfile );
     4928                                $thumbdir  = path_join( $uploadpath['basedir'], dirname( $file ) );
     4929
     4930                                if ( ! wp_delete_file_from_directory( $thumbfile, $thumbdir ) ) {
     4931                                        $deleted = false;
     4932                                }
     4933                        }
    49074934                }
    49084935        }
    … …  
    49104937        // Remove intermediate and backup images if there are any.
    49114938        if ( isset( $meta['sizes'] ) && is_array( $meta['sizes'] ) ) {
     4939                $intermediate_dir = path_join( $uploadpath['basedir'], dirname( $file ) );
    49124940                foreach ( $meta['sizes'] as $size => $sizeinfo ) {
    49134941                        $intermediate_file = str_replace( basename( $file ), $sizeinfo['file'], $file );
    4914                         /** This filter is documented in wp-includes/functions.php */
    4915                         $intermediate_file = apply_filters( 'wp_delete_file', $intermediate_file );
    4916                         @ unlink( path_join( $uploadpath['basedir'], $intermediate_file ) );
    4917                 }
    4918         }
    4919 
    4920         if ( is_array($backup_sizes) ) {
     4942                        if ( ! empty( $intermediate_file ) ) {
     4943                                $intermediate_file = path_join( $uploadpath['basedir'], $intermediate_file );
     4944
     4945                                if ( ! wp_delete_file_from_directory( $intermediate_file, $intermediate_dir ) ) {
     4946                                        $deleted = false;
     4947                                }
     4948                        }
     4949                }
     4950        }
     4951
     4952        if ( is_array( $backup_sizes ) ) {
     4953                $del_dir = path_join( $uploadpath['basedir'], dirname( $meta['file'] ) );
    49214954                foreach ( $backup_sizes as $size ) {
    4922                         $del_file = path_join( dirname($meta['file']), $size['file'] );
    4923                         /** This filter is documented in wp-includes/functions.php */
    4924                         $del_file = apply_filters( 'wp_delete_file', $del_file );
    4925                         @ unlink( path_join($uploadpath['basedir'], $del_file) );
    4926                 }
    4927         }
    4928 
    4929         wp_delete_file( $file );
    4930 
    4931         clean_post_cache( $post );
    4932 
    4933         return $post;
     4955                        $del_file = path_join( dirname( $meta['file'] ), $size['file'] );
     4956                        if ( ! empty( $del_file ) ) {
     4957                                $del_file = path_join( $uploadpath['basedir'], $del_file );
     4958
     4959                                if ( ! wp_delete_file_from_directory( $del_file, $del_dir ) ) {
     4960                                        $deleted = false;
     4961                                }
     4962                        }
     4963                }
     4964        }
     4965
     4966        if ( ! wp_delete_file_from_directory( $file, $uploadpath['basedir'] ) ) {
     4967                $deleted = false;
     4968        }
     4969
     4970        return $deleted;
    49344971}
    49354972
Note: See TracChangeset for help on using the changeset viewer.