<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[56689] trunk: Filesystem API: Introduce filters for before/after unzipping archives.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/56689">56689</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/56689","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>costdev</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2023-09-25 22:14:11 +0000 (Mon, 25 Sep 2023)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Filesystem API: Introduce filters for before/after unzipping archives.
This introduces the following new filters which wrap the process of unzipping an archive:
- `pre_unzip_file` - Filters archive unzipping to allow an override with a custom process.
- `unzip_file` - Filters the result of unzipping an archive.
Both filters pass the following:
- `string $file` - Full path and filename of ZIP archive.
- `string $to` - Full path on the filesystem to extract archive to.
- `string[] $needed_dirs` - A full list of required folders that need to be created.
- `float|false $required_space` - The space required to unzip the file and copy its contents, with a 10% buffer.
Props dfavor, azaozz, oglekler, afragen, costdev.
Fixes <a href="https://core.trac.wordpress.org/ticket/37719">#37719</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminincludesfilephp">trunk/src/wp-admin/includes/file.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li>trunk/tests/phpunit/data/filesystem/</li>
<li><a href="#trunktestsphpunittestsfilesystem_unzipFilePclzipphp">trunk/tests/phpunit/tests/filesystem/_unzipFilePclzip.php</a></li>
<li><a href="#trunktestsphpunittestsfilesystem_unzipFileZiparchivephp">trunk/tests/phpunit/tests/filesystem/_unzipFileZiparchive.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminincludesfilephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/file.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/file.php 2023-09-25 21:27:51 UTC (rev 56688)
+++ trunk/src/wp-admin/includes/file.php 2023-09-25 22:14:11 UTC (rev 56689)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1697,6 +1697,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Enough space to unzip the file and copy its contents, with a 10% buffer.
+ $required_space = $uncompressed_size * 2.1;
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /*
</span><span class="cx" style="display: block; padding: 0 10px"> * disk_free_space() could return false. Assume that any falsey value is an error.
</span><span class="cx" style="display: block; padding: 0 10px"> * A disk that has zero free bytes has bigger problems.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1705,7 +1708,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> if ( wp_doing_cron() ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $available_space = function_exists( 'disk_free_space' ) ? @disk_free_space( WP_CONTENT_DIR ) : false;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( $available_space && ( $uncompressed_size * 2.1 ) > $available_space ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( $available_space && ( $required_space > $available_space ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error(
</span><span class="cx" style="display: block; padding: 0 10px"> 'disk_full_unzip_file',
</span><span class="cx" style="display: block; padding: 0 10px"> __( 'Could not copy files. You may have run out of disk space.' ),
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1746,8 +1749,27 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'mkdir_failed_ziparchive', __( 'Could not create directory.' ), $_dir );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- unset( $needed_dirs );
</del><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * Filters archive unzipping to override with a custom process.
+ *
+ * @since 6.4.0
+ *
+ * @param null|true|WP_Error $result The result of the override. True on success, otherwise WP Error. Default null.
+ * @param string $file Full path and filename of ZIP archive.
+ * @param string $to Full path on the filesystem to extract archive to.
+ * @param string[] $needed_dirs A full list of required folders that need to be created.
+ * @param float $required_space The space required to unzip the file and copy its contents, with a 10% buffer.
+ */
+ $pre = apply_filters( 'pre_unzip_file', null, $file, $to, $needed_dirs, $required_space );
+
+ if ( null !== $pre ) {
+ // Ensure the ZIP file archive has been closed.
+ $z->close();
+
+ return $pre;
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> for ( $i = 0; $i < $z->numFiles; $i++ ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $info = $z->statIndex( $i );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1781,7 +1803,22 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $z->close();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return true;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * Filters the result of unzipping an archive.
+ *
+ * @since 6.4.0
+ *
+ * @param true|WP_Error $result The result of unzipping the archive. True on success, otherwise WP_Error. Default true.
+ * @param string $file Full path and filename of ZIP archive.
+ * @param string $to Full path on the filesystem the archive was extracted to.
+ * @param string[] $needed_dirs A full list of required folders that were created.
+ * @param float $required_space The space required to unzip the file and copy its contents, with a 10% buffer.
+ */
+ $result = apply_filters( 'unzip_file', true, $file, $to, $needed_dirs, $required_space );
+
+ unset( $needed_dirs );
+
+ return $result;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1838,6 +1875,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $needed_dirs[] = $to . untrailingslashit( $file['folder'] ? $file['filename'] : dirname( $file['filename'] ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Enough space to unzip the file and copy its contents, with a 10% buffer.
+ $required_space = $uncompressed_size * 2.1;
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /*
</span><span class="cx" style="display: block; padding: 0 10px"> * disk_free_space() could return false. Assume that any falsey value is an error.
</span><span class="cx" style="display: block; padding: 0 10px"> * A disk that has zero free bytes has bigger problems.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1846,7 +1886,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> if ( wp_doing_cron() ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $available_space = function_exists( 'disk_free_space' ) ? @disk_free_space( WP_CONTENT_DIR ) : false;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( $available_space && ( $uncompressed_size * 2.1 ) > $available_space ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( $available_space && ( $required_space > $available_space ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error(
</span><span class="cx" style="display: block; padding: 0 10px"> 'disk_full_unzip_file',
</span><span class="cx" style="display: block; padding: 0 10px"> __( 'Could not copy files. You may have run out of disk space.' ),
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1887,8 +1927,14 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'mkdir_failed_pclzip', __( 'Could not create directory.' ), $_dir );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- unset( $needed_dirs );
</del><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /** This filter is documented in src/wp-admin/includes/file.php */
+ $pre = apply_filters( 'pre_unzip_file', null, $file, $to, $needed_dirs, $required_space );
+
+ if ( null !== $pre ) {
+ return $pre;
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Extract the files from the zip.
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( $archive_files as $file ) {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( $file['folder'] ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1909,7 +1955,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return true;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /** This action is documented in src/wp-admin/includes/file.php */
+ $result = apply_filters( 'unzip_file', true, $file, $to, $needed_dirs, $required_space );
+
+ unset( $needed_dirs );
+
+ return $result;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span></span></pre></div>
<a id="trunktestsphpunittestsfilesystem_unzipFilePclzipphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/filesystem/_unzipFilePclzip.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/filesystem/_unzipFilePclzip.php (rev 0)
+++ trunk/tests/phpunit/tests/filesystem/_unzipFilePclzip.php 2023-09-25 22:14:11 UTC (rev 56689)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,76 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * Tests _unzip_file_pclzip().
+ *
+ * @group file.php
+ *
+ * @covers ::_unzip_file_pclzip
+ */
+class Tests_Filesystem_UnzipFilePclzip extends WP_UnitTestCase {
+
+ /**
+ * The test data directory.
+ *
+ * @var string $test_data_dir
+ */
+ private static $test_data_dir;
+
+ /**
+ * Sets up the filesystem and test data directory property
+ * before any tests run.
+ */
+ public static function set_up_before_class() {
+ parent::set_up_before_class();
+
+ require_once ABSPATH . 'wp-admin/includes/file.php';
+ WP_Filesystem();
+
+ self::$test_data_dir = DIR_TESTDATA . '/filesystem/';
+ }
+
+ /**
+ * Tests that _unzip_file_pclzip() applies "pre_unzip_file" filters.
+ *
+ * @ticket 37719
+ */
+ public function test_should_apply_pre_unzip_file_filters() {
+ $filter = new MockAction();
+ add_filter( 'pre_unzip_file', array( $filter, 'filter' ) );
+
+ // Prepare test environment.
+ $unzip_destination = self::$test_data_dir . 'archive/';
+ mkdir( $unzip_destination );
+
+ _unzip_file_pclzip( self::$test_data_dir . 'archive.zip', $unzip_destination );
+
+ // Cleanup test environment.
+ $this->rmdir( $unzip_destination );
+ $this->delete_folders( $unzip_destination );
+
+ $this->assertSame( 1, $filter->get_call_count() );
+ }
+
+ /**
+ * Tests that _unzip_file_pclzip() applies "unzip_file" filters.
+ *
+ * @ticket 37719
+ */
+ public function test_should_apply_unzip_file_filters() {
+ $filter = new MockAction();
+ add_filter( 'unzip_file', array( $filter, 'filter' ) );
+
+ // Prepare test environment.
+ $unzip_destination = self::$test_data_dir . 'archive/';
+ mkdir( $unzip_destination );
+
+ _unzip_file_pclzip( self::$test_data_dir . 'archive.zip', $unzip_destination );
+
+ // Cleanup test environment.
+ $this->rmdir( $unzip_destination );
+ $this->delete_folders( $unzip_destination );
+
+ $this->assertSame( 1, $filter->get_call_count() );
+ }
+
+}
</ins></span></pre></div>
<a id="trunktestsphpunittestsfilesystem_unzipFileZiparchivephp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/filesystem/_unzipFileZiparchive.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/filesystem/_unzipFileZiparchive.php (rev 0)
+++ trunk/tests/phpunit/tests/filesystem/_unzipFileZiparchive.php 2023-09-25 22:14:11 UTC (rev 56689)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,84 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * Tests _unzip_file_ziparchive().
+ *
+ * @group file.php
+ *
+ * @covers ::_unzip_file_ziparchive
+ */
+class Tests_Filesystem_UnzipFileZiparchive extends WP_UnitTestCase {
+
+ /**
+ * The test data directory.
+ *
+ * @var string $test_data_dir
+ */
+ private static $test_data_dir;
+
+ /**
+ * Sets up the filesystem and test data directory property
+ * before any tests run.
+ */
+ public static function set_up_before_class() {
+ parent::set_up_before_class();
+
+ require_once ABSPATH . 'wp-admin/includes/file.php';
+ WP_Filesystem();
+
+ self::$test_data_dir = DIR_TESTDATA . '/filesystem/';
+ }
+
+ /**
+ * Tests that _unzip_file_ziparchive() applies "pre_unzip_file" filters.
+ *
+ * @ticket 37719
+ */
+ public function test_should_apply_pre_unzip_file_filters() {
+ if ( ! class_exists( 'ZipArchive' ) ) {
+ $this->markTestSkipped( 'This test requires the ZipArchive class.' );
+ }
+
+ $filter = new MockAction();
+ add_filter( 'pre_unzip_file', array( $filter, 'filter' ) );
+
+ // Prepare test environment.
+ $unzip_destination = self::$test_data_dir . 'archive/';
+ mkdir( $unzip_destination );
+
+ _unzip_file_ziparchive( self::$test_data_dir . 'archive.zip', $unzip_destination );
+
+ // Cleanup test environment.
+ $this->rmdir( $unzip_destination );
+ $this->delete_folders( $unzip_destination );
+
+ $this->assertSame( 1, $filter->get_call_count() );
+ }
+
+ /**
+ * Tests that _unzip_file_ziparchive() applies "unzip_file" filters.
+ *
+ * @ticket 37719
+ */
+ public function test_should_apply_unzip_file_filters() {
+ if ( ! class_exists( 'ZipArchive' ) ) {
+ $this->markTestSkipped( 'This test requires the ZipArchive class.' );
+ }
+
+ $filter = new MockAction();
+ add_filter( 'unzip_file', array( $filter, 'filter' ) );
+
+ // Prepare test environment.
+ $unzip_destination = self::$test_data_dir . 'archive/';
+ mkdir( $unzip_destination );
+
+ _unzip_file_ziparchive( self::$test_data_dir . 'archive.zip', $unzip_destination );
+
+ // Cleanup test environment.
+ $this->rmdir( $unzip_destination );
+ $this->delete_folders( $unzip_destination );
+
+ $this->assertSame( 1, $filter->get_call_count() );
+ }
+
+}
</ins></span></pre>
</div>
</div>
</body>
</html>