Skip to content

Commit ff41832

Browse files
authored
fix site specific upload dir regex when determining global upload dir in a multisite install (#1341)
* fix site specific upload dir regex when determining global upload dir in a multisite install * make sure to add ending slash after getting parent directory * ignore possible warning from openbasedir * test tweak
1 parent fd3e476 commit ff41832

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

classes/WpMatomo/Paths.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,9 @@ public function get_gloal_upload_dir_if_possible( $file_to_look_for = '' ) {
146146
return $path_upload_dir;
147147
}
148148

149-
if ( preg_match( '/sites\/(\d)+$/', $path_upload_dir ) ) {
150-
$path_upload_dir = preg_replace( '/sites\/(\d)+$/', '', $path_upload_dir );
149+
$global_path_upload_dir = $this->get_global_path_upload_dir_if_matches_site_specific_dir( $path_upload_dir );
150+
if ( ! empty( $global_path_upload_dir ) ) {
151+
$path_upload_dir = $global_path_upload_dir;
151152
} else {
152153
// re-implement _wp_upload_dir to find hopefully the upload_dir for the network site
153154
$upload_path = trim( get_option( 'upload_path' ) );
@@ -173,7 +174,10 @@ public function get_gloal_upload_dir_if_possible( $file_to_look_for = '' ) {
173174
$parent_dir = $path_upload_dir;
174175
do {
175176
$parent_dir = dirname( $parent_dir );
176-
if ( file_exists( $parent_dir . $file_to_look_for ) ) {
177+
$parent_dir = rtrim( $parent_dir, '/' ) . '/';
178+
// NOTE: file_exists here can trigger an open_basedir warning on some setups
179+
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
180+
if ( @file_exists( $parent_dir . $file_to_look_for ) ) {
177181
return $parent_dir;
178182
}
179183
} while ( strpos( $parent_dir, ABSPATH ) === 0 ); // we don't go outside WP dir
@@ -233,4 +237,20 @@ public function is_upload_dir_writable() {
233237
&& $this->get_host_init_filesystem_succeeded()
234238
&& $is_filesystem_direct_available;
235239
}
240+
241+
/**
242+
* public for tests
243+
*
244+
* @param string $path_upload_dir
245+
* @return string|null
246+
*/
247+
public function get_global_path_upload_dir_if_matches_site_specific_dir( $path_upload_dir ) {
248+
$multisite_path_regex = '%sites/(\d)+/' . preg_quote( MATOMO_UPLOAD_DIR, '%' ) . '$%';
249+
250+
if ( ! preg_match( $multisite_path_regex, $path_upload_dir ) ) {
251+
return null;
252+
}
253+
254+
return preg_replace( $multisite_path_regex, '', $path_upload_dir );
255+
}
236256
}

scripts/local-dev-entrypoint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function handle_cli_command() {
6464
./console $EXECUTE_ARGS
6565
exit $?
6666
elif [[ "$EXECUTE_TARGET" = "phpunit" ]]; then
67-
cd /var/www/html/matomo-for-wordpress
67+
cd /var/www/html/$WORDPRESS_FOLDER/wp-content/plugins/matomo
6868

6969
php -r "\$pdo = new PDO('mysql:host=$WP_DB_HOST', 'root', 'pass');
7070
\$pdo->exec('DROP DATABASE IF EXISTS \`${WP_DB_NAME}_test\`');\

tests/phpunit/wpmatomo/test-paths.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,29 @@ public function test_get_gloal_upload_dir_if_possible_forBlog() {
229229
$this->markTestSkipped( 'Not multisite.' );
230230
return;
231231
}
232+
232233
$blogid1 = self::factory()->blog->create();
233234
switch_to_blog( 2 );
234235
wp_delete_site( $blogid1 );
235236
$this->assertSame( ABSPATH . 'wp-content/uploads/matomo', $this->paths->get_gloal_upload_dir_if_possible() );
236237
}
237238

239+
public function test_get_global_path_upload_dir_if_matches_site_specific_dir() {
240+
if ( ! is_multisite() ) {
241+
$this->markTestSkipped( 'Not multisite.' );
242+
return;
243+
}
244+
245+
$blogid1 = self::factory()->blog->create();
246+
switch_to_blog( 2 );
247+
248+
try {
249+
$upload_path_base_dir = $this->paths->get_upload_base_dir();
250+
251+
$global_upload_path = $this->paths->get_global_path_upload_dir_if_matches_site_specific_dir( $upload_path_base_dir );
252+
$this->assertEquals( ABSPATH . 'wp-content/uploads/', $global_upload_path );
253+
} finally {
254+
wp_delete_site( $blogid1 );
255+
}
256+
}
238257
}

0 commit comments

Comments
 (0)