From 7689465261ccca0ebac72061e87921447ef2f420 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Sun, 25 Aug 2024 10:57:39 +1000 Subject: [PATCH 1/7] Allow specifying of WordPress branch. --- .../website/public/plugin-proxy.php | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/packages/playground/website/public/plugin-proxy.php b/packages/playground/website/public/plugin-proxy.php index 57095b60b3..70f1a494f1 100644 --- a/packages/playground/website/public/plugin-proxy.php +++ b/packages/playground/website/public/plugin-proxy.php @@ -334,6 +334,32 @@ function ($curl, $body) use (&$extra_headers_sent, $default_response_headers) { } $downloader->streamFromGithubReleases($_GET['repo'], $_GET['name']); + } else if ( isset( $_GET['branch'] ) ) { + $branch = strtolower( $_GET['branch'] ); + if ( $branch === 'trunk' ) { + $branch = 'master'; + // If the brach is of the form x.x append '-branch' to it. + } elseif ( preg_match( '/^\d+\.\d+$/', $branch ) ) { + $branch .= '-branch'; + } elseif ( ! preg_match( '/^\d+\.\d+-branch$/', $branch ) ) { + throw new ApiException( 'Invalid branch' ); + } + + $url = "https://codeload.github.com/WordPress/WordPress/zip/refs/heads/{$branch}"; + + streamHttpResponse( + $url, + 'GET', + [], + file_get_contents('php://input'), + null, + [ + 'Content-Disposition: attachment; filename="wordpress.zip"', + "X-Proxy-Zip-Path: WordPress-{$branch}" + ] + ); + + } else if (isset($_GET['url'])) { // Proxy the current request to $_GET['url'] and return the response, // but only if the URL is allowlisted. From 034cad2954e68a23d1b95833d1d0afd3550ca5f5 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Sun, 25 Aug 2024 12:44:28 +1000 Subject: [PATCH 2/7] Add a final attempt to determine the WordPress path. --- .../playground/website/public/plugin-proxy.php | 1 - packages/playground/wordpress/src/index.ts | 15 ++++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/playground/website/public/plugin-proxy.php b/packages/playground/website/public/plugin-proxy.php index 70f1a494f1..32ed124e0d 100644 --- a/packages/playground/website/public/plugin-proxy.php +++ b/packages/playground/website/public/plugin-proxy.php @@ -355,7 +355,6 @@ function ($curl, $body) use (&$extra_headers_sent, $default_response_headers) { null, [ 'Content-Disposition: attachment; filename="wordpress.zip"', - "X-Proxy-Zip-Path: WordPress-{$branch}" ] ); diff --git a/packages/playground/wordpress/src/index.ts b/packages/playground/wordpress/src/index.ts index f1e9134d79..a1075d339d 100644 --- a/packages/playground/wordpress/src/index.ts +++ b/packages/playground/wordpress/src/index.ts @@ -329,12 +329,25 @@ export async function unzipWordPress(php: PHP, wpZip: File) { // @TODO: Don't make so many guesses about the zip file contents. Allow the // API consumer to specify the exact "coordinates" of WordPress inside // the zip archive. - const wpPath = php.fileExists('/tmp/unzipped-wordpress/wordpress') + let wpPath = php.fileExists('/tmp/unzipped-wordpress/wordpress') ? '/tmp/unzipped-wordpress/wordpress' : php.fileExists('/tmp/unzipped-wordpress/build') ? '/tmp/unzipped-wordpress/build' : '/tmp/unzipped-wordpress'; + // One last guess if the path does not contain the sample config file. + if (!php.fileExists(joinPaths(wpPath, 'wp-config-sample.php'))) { + // Still don't know the directory structure of the zip file. + // 1. Get the first item in path. + const firstDir = php.listFiles(wpPath)[0]; + // 2. If it's a directory that contains wp-config-sample.php, use it. + if ( + php.fileExists(joinPaths(wpPath, firstDir, 'wp-config-sample.php')) + ) { + wpPath = joinPaths(wpPath, firstDir); + } + } + if ( php.isDir(php.documentRoot) && isCleanDirContainingSiteMetadata(php.documentRoot, php) From 976d59480076d2d3d874d08cf2237ccbd694f9b0 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Sun, 25 Aug 2024 12:58:40 +1000 Subject: [PATCH 3/7] Accept master for the master branch. --- packages/playground/website/public/plugin-proxy.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/playground/website/public/plugin-proxy.php b/packages/playground/website/public/plugin-proxy.php index 32ed124e0d..ea7b73249f 100644 --- a/packages/playground/website/public/plugin-proxy.php +++ b/packages/playground/website/public/plugin-proxy.php @@ -336,7 +336,7 @@ function ($curl, $body) use (&$extra_headers_sent, $default_response_headers) { $downloader->streamFromGithubReleases($_GET['repo'], $_GET['name']); } else if ( isset( $_GET['branch'] ) ) { $branch = strtolower( $_GET['branch'] ); - if ( $branch === 'trunk' ) { + if ( $branch === 'trunk' || $branch === 'master' ) { $branch = 'master'; // If the brach is of the form x.x append '-branch' to it. } elseif ( preg_match( '/^\d+\.\d+$/', $branch ) ) { From cea0d9a9b2744313136b315ef8ba605c5da81585 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Sun, 25 Aug 2024 13:02:25 +1000 Subject: [PATCH 4/7] Ensure there are files to check. --- packages/playground/wordpress/src/index.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/packages/playground/wordpress/src/index.ts b/packages/playground/wordpress/src/index.ts index a1075d339d..c8f323d2d7 100644 --- a/packages/playground/wordpress/src/index.ts +++ b/packages/playground/wordpress/src/index.ts @@ -339,12 +339,17 @@ export async function unzipWordPress(php: PHP, wpZip: File) { if (!php.fileExists(joinPaths(wpPath, 'wp-config-sample.php'))) { // Still don't know the directory structure of the zip file. // 1. Get the first item in path. - const firstDir = php.listFiles(wpPath)[0]; - // 2. If it's a directory that contains wp-config-sample.php, use it. - if ( - php.fileExists(joinPaths(wpPath, firstDir, 'wp-config-sample.php')) - ) { - wpPath = joinPaths(wpPath, firstDir); + const files = php.listFiles(wpPath); + if (files.length) { + const firstDir = files[0]; + // 2. If it's a directory that contains wp-config-sample.php, use it. + if ( + php.fileExists( + joinPaths(wpPath, firstDir, 'wp-config-sample.php') + ) + ) { + wpPath = joinPaths(wpPath, firstDir); + } } } From 3d82fc1d6901f810bb38fc2a9c886e80478cb804 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Sun, 25 Aug 2024 13:17:13 +1000 Subject: [PATCH 5/7] CS: Remove excess line break. --- packages/playground/website/public/plugin-proxy.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/playground/website/public/plugin-proxy.php b/packages/playground/website/public/plugin-proxy.php index ea7b73249f..5e6daf2482 100644 --- a/packages/playground/website/public/plugin-proxy.php +++ b/packages/playground/website/public/plugin-proxy.php @@ -357,8 +357,6 @@ function ($curl, $body) use (&$extra_headers_sent, $default_response_headers) { 'Content-Disposition: attachment; filename="wordpress.zip"', ] ); - - } else if (isset($_GET['url'])) { // Proxy the current request to $_GET['url'] and return the response, // but only if the URL is allowlisted. From e9bb6dfc3153008693be0d6c76cc3ff312075022 Mon Sep 17 00:00:00 2001 From: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> Date: Wed, 28 Aug 2024 08:07:33 +1000 Subject: [PATCH 6/7] Clarify comment explaining check for sample config file. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Adam ZieliƄski --- packages/playground/wordpress/src/index.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/playground/wordpress/src/index.ts b/packages/playground/wordpress/src/index.ts index c8f323d2d7..711a9dcc8b 100644 --- a/packages/playground/wordpress/src/index.ts +++ b/packages/playground/wordpress/src/index.ts @@ -335,7 +335,9 @@ export async function unzipWordPress(php: PHP, wpZip: File) { ? '/tmp/unzipped-wordpress/build' : '/tmp/unzipped-wordpress'; - // One last guess if the path does not contain the sample config file. + // Dive one directory deeper if the zip root does not contain the sample + // config file. This is relevant when unzipping a zipped branch from the + // https://github.com/WordPress/WordPress repository. if (!php.fileExists(joinPaths(wpPath, 'wp-config-sample.php'))) { // Still don't know the directory structure of the zip file. // 1. Get the first item in path. From 7179bf6337cdacdceafcbfec64f3141e7e623708 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Wed, 28 Aug 2024 08:14:42 +1000 Subject: [PATCH 7/7] Rename parameter wordpress-branch to be application specific. --- packages/playground/website/public/plugin-proxy.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/playground/website/public/plugin-proxy.php b/packages/playground/website/public/plugin-proxy.php index 5e6daf2482..9981abce61 100644 --- a/packages/playground/website/public/plugin-proxy.php +++ b/packages/playground/website/public/plugin-proxy.php @@ -334,8 +334,8 @@ function ($curl, $body) use (&$extra_headers_sent, $default_response_headers) { } $downloader->streamFromGithubReleases($_GET['repo'], $_GET['name']); - } else if ( isset( $_GET['branch'] ) ) { - $branch = strtolower( $_GET['branch'] ); + } else if ( isset( $_GET['wordpress-branch'] ) ) { + $branch = strtolower( $_GET['wordpress-branch'] ); if ( $branch === 'trunk' || $branch === 'master' ) { $branch = 'master'; // If the brach is of the form x.x append '-branch' to it.