Skip to content

Commit a8630eb

Browse files
Filesystem API: Pass correct $file value to pre_unzip_file and unzip_file filters.
This commit ensures that the original `$file` argument passed to the function is not unintentionally overwritten by the use of the same variable name in two `foreach` loops. Follow-up to [56689]. Props sanchothefat, westonruter, mukesh27, SergeyBiryukov. Fixes #64398. git-svn-id: https://develop.svn.wordpress.org/trunk@61374 602fd350-edb4-49c9-b593-d223f7449a82
1 parent a3770fe commit a8630eb

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

src/wp-admin/includes/file.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,14 +1896,14 @@ function _unzip_file_pclzip( $file, $to, $needed_dirs = array() ) {
18961896
$uncompressed_size = 0;
18971897

18981898
// Determine any children directories needed (From within the archive).
1899-
foreach ( $archive_files as $file ) {
1900-
if ( str_starts_with( $file['filename'], '__MACOSX/' ) ) { // Skip the OS X-created __MACOSX directory.
1899+
foreach ( $archive_files as $archive_file ) {
1900+
if ( str_starts_with( $archive_file['filename'], '__MACOSX/' ) ) { // Skip the OS X-created __MACOSX directory.
19011901
continue;
19021902
}
19031903

1904-
$uncompressed_size += $file['size'];
1904+
$uncompressed_size += $archive_file['size'];
19051905

1906-
$needed_dirs[] = $to . untrailingslashit( $file['folder'] ? $file['filename'] : dirname( $file['filename'] ) );
1906+
$needed_dirs[] = $to . untrailingslashit( $archive_file['folder'] ? $archive_file['filename'] : dirname( $archive_file['filename'] ) );
19071907
}
19081908

19091909
// Enough space to unzip the file and copy its contents, with a 10% buffer.
@@ -1967,22 +1967,22 @@ function _unzip_file_pclzip( $file, $to, $needed_dirs = array() ) {
19671967
}
19681968

19691969
// Extract the files from the zip.
1970-
foreach ( $archive_files as $file ) {
1971-
if ( $file['folder'] ) {
1970+
foreach ( $archive_files as $archive_file ) {
1971+
if ( $archive_file['folder'] ) {
19721972
continue;
19731973
}
1974-
1975-
if ( str_starts_with( $file['filename'], '__MACOSX/' ) ) { // Don't extract the OS X-created __MACOSX directory files.
1974+
1975+
if ( str_starts_with( $archive_file['filename'], '__MACOSX/' ) ) { // Don't extract the OS X-created __MACOSX directory files.
19761976
continue;
19771977
}
19781978

19791979
// Don't extract invalid files:
1980-
if ( 0 !== validate_file( $file['filename'] ) ) {
1980+
if ( 0 !== validate_file( $archive_file['filename'] ) ) {
19811981
continue;
19821982
}
19831983

1984-
if ( ! $wp_filesystem->put_contents( $to . $file['filename'], $file['content'], FS_CHMOD_FILE ) ) {
1985-
return new WP_Error( 'copy_failed_pclzip', __( 'Could not copy file.' ), $file['filename'] );
1984+
if ( ! $wp_filesystem->put_contents( $to . $archive_file['filename'], $archive_file['content'], FS_CHMOD_FILE ) ) {
1985+
return new WP_Error( 'copy_failed_pclzip', __( 'Could not copy file.' ), $archive_file['filename'] );
19861986
}
19871987
}
19881988

tests/phpunit/tests/filesystem/unzipFilePclzip.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static function set_up_before_class() {
3737
*/
3838
public function test_should_apply_pre_unzip_file_filters() {
3939
$filter = new MockAction();
40-
add_filter( 'pre_unzip_file', array( $filter, 'filter' ) );
40+
add_filter( 'pre_unzip_file', array( $filter, 'filter' ), 10, 2 );
4141

4242
// Prepare test environment.
4343
$unzip_destination = self::$test_data_dir . 'archive/';
@@ -53,7 +53,8 @@ public function test_should_apply_pre_unzip_file_filters() {
5353
$this->rmdir( $unzip_destination );
5454
$this->delete_folders( $unzip_destination );
5555

56-
$this->assertSame( 1, $filter->get_call_count() );
56+
$this->assertSame( 1, $filter->get_call_count(), 'The filter should be called once.' );
57+
$this->assertSame( self::$test_data_dir . 'archive.zip', $filter->get_args()[0][1], 'The $file parameter should be correct.' );
5758
}
5859

5960
/**
@@ -63,7 +64,7 @@ public function test_should_apply_pre_unzip_file_filters() {
6364
*/
6465
public function test_should_apply_unzip_file_filters() {
6566
$filter = new MockAction();
66-
add_filter( 'unzip_file', array( $filter, 'filter' ) );
67+
add_filter( 'unzip_file', array( $filter, 'filter' ), 10, 2 );
6768

6869
// Prepare test environment.
6970
$unzip_destination = self::$test_data_dir . 'archive/';
@@ -79,6 +80,7 @@ public function test_should_apply_unzip_file_filters() {
7980
$this->rmdir( $unzip_destination );
8081
$this->delete_folders( $unzip_destination );
8182

82-
$this->assertSame( 1, $filter->get_call_count() );
83+
$this->assertSame( 1, $filter->get_call_count(), 'The filter should be called once.' );
84+
$this->assertSame( self::$test_data_dir . 'archive.zip', $filter->get_args()[0][1], 'The $file parameter should be correct.' );
8385
}
8486
}

0 commit comments

Comments
 (0)