Skip to content

Commit c9dce06

Browse files
committed
Media: Limit thumbnail file deletions to the same directory as the original file.
Built from https://develop.svn.wordpress.org/trunk@43392 git-svn-id: http://core.svn.wordpress.org/trunk@43220 1a063a9b-81f0-0310-95a4-ce76da25c4cd
1 parent 44b8c2e commit c9dce06

File tree

3 files changed

+73
-14
lines changed

3 files changed

+73
-14
lines changed

wp-includes/functions.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5847,6 +5847,28 @@ function wp_delete_file( $file ) {
58475847
}
58485848
}
58495849

5850+
/**
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+
*/
5859+
function 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+
58505872
/**
58515873
* Outputs a small JS snippet on preview tabs/windows to remove `window.name` on unload.
58525874
*

wp-includes/post.php

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5311,42 +5311,79 @@ function wp_delete_attachment( $post_id, $force_delete = false ) {
53115311
/** This action is documented in wp-includes/post.php */
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+
*/
5332+
function 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'] ) ) {
53175339
// Don't delete the thumb if another attachment uses it.
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
}
53255352

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 ) );
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+
}
53425379
}
53435380
}
53445381

5345-
wp_delete_file( $file );
5346-
5347-
clean_post_cache( $post );
5382+
if ( ! wp_delete_file_from_directory( $file, $uploadpath['basedir'] ) ) {
5383+
$deleted = false;
5384+
}
53485385

5349-
return $post;
5386+
return $deleted;
53505387
}
53515388

53525389
/**

wp-includes/version.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* @global string $wp_version
66
*/
7-
$wp_version = '5.0-alpha-43391';
7+
$wp_version = '5.0-alpha-43392';
88

99
/**
1010
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.

0 commit comments

Comments
 (0)