Make WordPress Core

Changeset 43392


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

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

Location:
trunk/src/wp-includes
Files:
2 edited

Legend:

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

    r43389 r43392  
    58495849
    58505850/**
     5851 * Deletes a file if its path is within the given directory.
     5852 *
     5853 * @since 4.9.7
     5854 *
     5855 * @param string $file      Absolute path to the file to delete.
     5856 * @param string $directory Absolute path to a directory.
     5857 * @return bool True on success, false on failure.
     5858 */
     5859function wp_delete_file_from_directory( $file, $directory ) {
     5860        $real_file = realpath( wp_normalize_path( $file ) );
     5861        $real_directory = realpath( wp_normalize_path( $directory ) );
     5862
     5863        if ( false === $real_file || false === $real_directory || strpos( wp_normalize_path( $real_file ), trailingslashit( wp_normalize_path( $real_directory ) ) ) !== 0 ) {
     5864                return false;
     5865        }
     5866
     5867        wp_delete_file( $file );
     5868
     5869        return true;
     5870}
     5871
     5872/**
    58515873 * Outputs a small JS snippet on preview tabs/windows to remove `window.name` on unload.
    58525874 *
  • trunk/src/wp-includes/post.php

    r43378 r43392  
    53125312        do_action( 'deleted_post', $post_id );
    53135313
     5314        wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file );
     5315
     5316        clean_post_cache( $post );
     5317
     5318        return $post;
     5319}
     5320
     5321/**
     5322 * Deletes all files that belong to the given attachment.
     5323 *
     5324 * @since 4.9.7
     5325 *
     5326 * @param int    $post_id      Attachment ID.
     5327 * @param array  $meta         The attachment's meta data.
     5328 * @param array  $backup_sizes The meta data for the attachment's backup images.
     5329 * @param string $file         Absolute path to the attachment's file.
     5330 * @return bool True on success, false on failure.
     5331 */
     5332function wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file ) {
     5333        global $wpdb;
     5334
    53145335        $uploadpath = wp_get_upload_dir();
     5336        $deleted    = true;
    53155337
    53165338        if ( ! empty( $meta['thumb'] ) ) {
     
    53185340                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 ) ) ) {
    53195341                        $thumbfile = str_replace( basename( $file ), $meta['thumb'], $file );
    5320                         /** This filter is documented in wp-includes/functions.php */
    5321                         $thumbfile = apply_filters( 'wp_delete_file', $thumbfile );
    5322                         @ unlink( path_join( $uploadpath['basedir'], $thumbfile ) );
     5342                        if ( ! empty( $thumbfile ) ) {
     5343                                $thumbfile = path_join( $uploadpath['basedir'], $thumbfile );
     5344                                $thumbdir  = path_join( $uploadpath['basedir'], dirname( $file ) );
     5345
     5346                                if ( ! wp_delete_file_from_directory( $thumbfile, $thumbdir ) ) {
     5347                                        $deleted = false;
     5348                                }
     5349                        }
    53235350                }
    53245351        }
     
    53265353        // Remove intermediate and backup images if there are any.
    53275354        if ( isset( $meta['sizes'] ) && is_array( $meta['sizes'] ) ) {
     5355                $intermediate_dir = path_join( $uploadpath['basedir'], dirname( $file ) );
    53285356                foreach ( $meta['sizes'] as $size => $sizeinfo ) {
    53295357                        $intermediate_file = str_replace( basename( $file ), $sizeinfo['file'], $file );
    5330                         /** This filter is documented in wp-includes/functions.php */
    5331                         $intermediate_file = apply_filters( 'wp_delete_file', $intermediate_file );
    5332                         @ unlink( path_join( $uploadpath['basedir'], $intermediate_file ) );
     5358                        if ( ! empty( $intermediate_file ) ) {
     5359                                $intermediate_file = path_join( $uploadpath['basedir'], $intermediate_file );
     5360
     5361                                if ( ! wp_delete_file_from_directory( $intermediate_file, $intermediate_dir ) ) {
     5362                                        $deleted = false;
     5363                                }
     5364                        }
    53335365                }
    53345366        }
    53355367
    53365368        if ( is_array( $backup_sizes ) ) {
     5369                $del_dir = path_join( $uploadpath['basedir'], dirname( $meta['file'] ) );
    53375370                foreach ( $backup_sizes as $size ) {
    53385371                        $del_file = path_join( dirname( $meta['file'] ), $size['file'] );
    5339                         /** This filter is documented in wp-includes/functions.php */
    5340                         $del_file = apply_filters( 'wp_delete_file', $del_file );
    5341                         @ unlink( path_join( $uploadpath['basedir'], $del_file ) );
    5342                 }
    5343         }
    5344 
    5345         wp_delete_file( $file );
    5346 
    5347         clean_post_cache( $post );
    5348 
    5349         return $post;
     5372                        if ( ! empty( $del_file ) ) {
     5373                                $del_file = path_join( $uploadpath['basedir'], $del_file );
     5374
     5375                                if ( ! wp_delete_file_from_directory( $del_file, $del_dir ) ) {
     5376                                        $deleted = false;
     5377                                }
     5378                        }
     5379                }
     5380        }
     5381
     5382        if ( ! wp_delete_file_from_directory( $file, $uploadpath['basedir'] ) ) {
     5383                $deleted = false;
     5384        }
     5385
     5386        return $deleted;
    53505387}
    53515388
Note: See TracChangeset for help on using the changeset viewer.