Make WordPress Core

Changeset 43399


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

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

Merges [43393] into the 4.3 branch.

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

Legend:

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

    r42291 r43399  
    15911591 * Normalize a filesystem path.
    15921592 *
    1593  * Replaces backslashes with forward slashes for Windows systems, and ensures
    1594  * no duplicate slashes exist.
     1593 * On windows systems, replaces backslashes with forward slashes
     1594 * and forces upper-case drive letters.
     1595 * Allows for two leading slashes for Windows network shares, but
     1596 * ensures that all other duplicate slashes are reduced to a single.
    15951597 *
    15961598 * @since 3.9.0
     1599 * @since 4.4.0 Ensures upper-case drive letters on Windows systems.
     1600 * @since 4.5.0 Allows for Windows network shares.
     1601 * @since 4.9.7 Allows for PHP file wrappers.
    15971602 *
    15981603 * @param string $path Path to normalize.
     
    16001605 */
    16011606function wp_normalize_path( $path ) {
     1607    $wrapper = '';
     1608    if ( wp_is_stream( $path ) ) {
     1609        list( $wrapper, $path ) = explode( '://', $path, 2 );
     1610        $wrapper .= '://';
     1611    }
     1612
     1613    // Standardise all paths to use /
    16021614    $path = str_replace( '\\', '/', $path );
    1603     $path = preg_replace( '|/+|','/', $path );
    1604     return $path;
     1615
     1616    // Replace multiple slashes down to a singular, allowing for network shares having two slashes.
     1617    $path = preg_replace( '|(?<=.)/+|', '/', $path );
     1618
     1619    // Windows paths should uppercase the drive letter
     1620    if ( ':' === substr( $path, 1, 1 ) ) {
     1621        $path = ucfirst( $path );
     1622    }
     1623
     1624    return $wrapper . $path;
    16051625}
    16061626
     
    50105030
    50115031/**
     5032 * Deletes a file if its path is within the given directory.
     5033 *
     5034 * @since 4.9.7
     5035 *
     5036 * @param string $file      Absolute path to the file to delete.
     5037 * @param string $directory Absolute path to a directory.
     5038 * @return bool True on success, false on failure.
     5039 */
     5040function wp_delete_file_from_directory( $file, $directory ) {
     5041    $real_file = realpath( wp_normalize_path( $file ) );
     5042    $real_directory = realpath( wp_normalize_path( $directory ) );
     5043
     5044    if ( false === $real_file || false === $real_directory || strpos( wp_normalize_path( $real_file ), trailingslashit( wp_normalize_path( $real_directory ) ) ) !== 0 ) {
     5045        return false;
     5046    }
     5047
     5048    wp_delete_file( $file );
     5049
     5050    return true;
     5051}
     5052
     5053/**
    50125054 * Outputs a small JS snippet on preview tabs/windows to remove `window.name` on unload.
    50135055 *
  • branches/4.3/src/wp-includes/post.php

    r42062 r43399  
    49324932    do_action( 'deleted_post', $post_id );
    49334933
     4934    wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file );
     4935
     4936    clean_post_cache( $post );
     4937
     4938    return $post;
     4939}
     4940
     4941/**
     4942 * Deletes all files that belong to the given attachment.
     4943 *
     4944 * @since 4.9.7
     4945 *
     4946 * @param int    $post_id      Attachment ID.
     4947 * @param array  $meta         The attachment's meta data.
     4948 * @param array  $backup_sizes The meta data for the attachment's backup images.
     4949 * @param string $file         Absolute path to the attachment's file.
     4950 * @return bool True on success, false on failure.
     4951 */
     4952function wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file ) {
     4953    global $wpdb;
     4954
    49344955    $uploadpath = wp_upload_dir();
     4956    $deleted    = true;
    49354957
    49364958    if ( ! empty($meta['thumb']) ) {
    49374959        // Don't delete the thumb if another attachment uses it.
    49384960        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)) ) {
    4939             $thumbfile = str_replace(basename($file), $meta['thumb'], $file);
    4940             /** This filter is documented in wp-includes/functions.php */
    4941             $thumbfile = apply_filters( 'wp_delete_file', $thumbfile );
    4942             @ unlink( path_join($uploadpath['basedir'], $thumbfile) );
     4961            $thumbfile = str_replace( basename( $file ), $meta['thumb'], $file );
     4962            if ( ! empty( $thumbfile ) ) {
     4963                $thumbfile = path_join( $uploadpath['basedir'], $thumbfile );
     4964                $thumbdir  = path_join( $uploadpath['basedir'], dirname( $file ) );
     4965
     4966                if ( ! wp_delete_file_from_directory( $thumbfile, $thumbdir ) ) {
     4967                    $deleted = false;
     4968                }
     4969            }
    49434970        }
    49444971    }
     
    49464973    // Remove intermediate and backup images if there are any.
    49474974    if ( isset( $meta['sizes'] ) && is_array( $meta['sizes'] ) ) {
     4975        $intermediate_dir = path_join( $uploadpath['basedir'], dirname( $file ) );
    49484976        foreach ( $meta['sizes'] as $size => $sizeinfo ) {
    49494977            $intermediate_file = str_replace( basename( $file ), $sizeinfo['file'], $file );
    4950             /** This filter is documented in wp-includes/functions.php */
    4951             $intermediate_file = apply_filters( 'wp_delete_file', $intermediate_file );
    4952             @ unlink( path_join( $uploadpath['basedir'], $intermediate_file ) );
     4978            if ( ! empty( $intermediate_file ) ) {
     4979                $intermediate_file = path_join( $uploadpath['basedir'], $intermediate_file );
     4980
     4981                if ( ! wp_delete_file_from_directory( $intermediate_file, $intermediate_dir ) ) {
     4982                    $deleted = false;
     4983                }
     4984            }
    49534985        }
    49544986    }
    49554987
    49564988    if ( is_array($backup_sizes) ) {
     4989        $del_dir = path_join( $uploadpath['basedir'], dirname( $meta['file'] ) );
    49574990        foreach ( $backup_sizes as $size ) {
    4958             $del_file = path_join( dirname($meta['file']), $size['file'] );
    4959             /** This filter is documented in wp-includes/functions.php */
    4960             $del_file = apply_filters( 'wp_delete_file', $del_file );
    4961             @ unlink( path_join($uploadpath['basedir'], $del_file) );
    4962         }
    4963     }
    4964 
    4965     wp_delete_file( $file );
    4966 
    4967     clean_post_cache( $post );
    4968 
    4969     return $post;
     4991            $del_file = path_join( dirname( $meta['file'] ), $size['file'] );
     4992            if ( ! empty( $del_file ) ) {
     4993                $del_file = path_join( $uploadpath['basedir'], $del_file );
     4994
     4995                if ( ! wp_delete_file_from_directory( $del_file, $del_dir ) ) {
     4996                    $deleted = false;
     4997                }
     4998            }
     4999        }
     5000    }
     5001
     5002    if ( ! wp_delete_file_from_directory( $file, $uploadpath['basedir'] ) ) {
     5003        $deleted = false;
     5004    }
     5005
     5006    return $deleted;
    49705007}
    49715008
Note: See TracChangeset for help on using the changeset viewer.