From 54e8e2733b4eeda6e5873b71e660317fe14d5bb2 Mon Sep 17 00:00:00 2001 From: stodorovic Date: Wed, 13 Sep 2017 17:36:48 +0200 Subject: [PATCH 1/3] Fixes PHP notices (#383) and improve detects of backend request --- wp-cache-phase1.php | 72 +++++++++++++++++++++++++++++++++------------ wp-cache-phase2.php | 10 +++---- wp-cache.php | 2 +- 3 files changed, 60 insertions(+), 24 deletions(-) diff --git a/wp-cache-phase1.php b/wp-cache-phase1.php index c7406f40..2ebb0a29 100644 --- a/wp-cache-phase1.php +++ b/wp-cache-phase1.php @@ -139,7 +139,7 @@ function wp_cache_serve_cache_file() { global $wp_cache_object_cache, $cache_compression, $wp_cache_slash_check, $wp_supercache_304, $wp_cache_home_path, $wp_cache_no_cache_for_get; global $wp_cache_disable_utf8, $wp_cache_mfunc_enabled, $wpsc_served_header; - if ( is_admin() ) { + if ( wpsc_is_backend() ) { wp_cache_debug( 'Not serving wp-admin requests.', 5 ); return false; } @@ -150,7 +150,7 @@ function wp_cache_serve_cache_file() { } if ( $wp_cache_no_cache_for_get && false == empty( $_GET ) ) { - wp_cache_debug( "Non empty GET request. Caching disabled on settings page. " . json_encode( $_GET ), 1 ); + wp_cache_debug( "Non empty GET request. Caching disabled on settings page. " . wpsc_dump_get_request(), 1 ); return false; } @@ -158,7 +158,7 @@ function wp_cache_serve_cache_file() { if ( $wp_cache_object_cache && wp_cache_get_cookies_values() == '' ) { if ( !empty( $_GET ) ) { - wp_cache_debug( "Non empty GET request. Not serving request from object cache. " . json_encode( $_GET ), 1 ); + wp_cache_debug( "Non empty GET request. Not serving request from object cache. " . wpsc_dump_get_request(), 1 ); return false; } @@ -219,7 +219,7 @@ function wp_cache_serve_cache_file() { wp_cache_debug( "No Super Cache file found for current URL: $file" ); return false; } elseif ( false == empty( $_GET ) ) { - wp_cache_debug( "GET array not empty. Cannot serve a supercache file. " . json_encode( $_GET ) ); + wp_cache_debug( "GET array not empty. Cannot serve a supercache file. " . wpsc_dump_get_request() ); return false; } elseif ( wp_cache_get_cookies_values() != '' ) { wp_cache_debug( "Cookies found. Cannot serve a supercache file. " . wp_cache_get_cookies_values() ); @@ -591,6 +591,48 @@ function wp_cache_debug( $message, $level = 1 ) { error_log( $log_message, 3, $log_file ); } +function wpsc_dump_get_request() { + static $string; + + if ( isset( $string) ) { + return $string; + } + + if ( function_exists( 'wp_json_encode' ) ) { + $string = wp_json_encode( $_GET ); + } else { + $string = json_encode( $_GET ); + } + + return $string; +} + +function wpsc_is_backend() { + static $is_backend; + + if ( isset( $is_backend ) ) { + return $is_backend; + } + + $is_backend = is_admin(); + if ( $is_backend ) { + return $is_backend; + } + + $script = isset( $_SERVER['PHP_SELF'] ) ? basename( $_SERVER['PHP_SELF'] ) : ''; + if ( $script !== 'index.php' ) { + if ( in_array( $script, array( 'wp-login.php', 'xmlrpc.php', 'wp-cron.php' ) ) ) { + $is_backend = true; + } elseif ( defined( 'DOING_CRON' ) && DOING_CRON ) { + $is_backend = true; + } elseif ( PHP_SAPI == 'cli' || ( defined( 'WP_CLI' ) && WP_CLI ) ) { + $is_backend = true; + } + } + + return $is_backend; +} + function wp_cache_user_agent_is_rejected() { global $cache_rejected_user_agent; @@ -632,22 +674,16 @@ function get_current_url_supercache_dir( $post_id = 0 ) { * Sometimes site_url doesn't return the siteurl. See http://wordpress.org/support/topic/wp-super-cache-not-refreshing-post-after-comments-made */ $DONOTREMEMBER = 1; - wp_cache_debug( "get_current_url_supercache_dir: warning! site_url ($site_url) not found in permalink ($permalink).", 1 ); - if ( false === strpos( $permalink, $WPSC_HTTP_HOST ) ) { - wp_cache_debug( "get_current_url_supercache_dir: WARNING! SERVER_NAME ({$WPSC_HTTP_HOST}) not found in permalink ($permalink). ", 1 ); - $p = parse_url( $permalink ); - if ( is_array( $p ) ) { - $uri = $p[ 'path' ]; - wp_cache_debug( "get_current_url_supercache_dir: WARNING! Using $uri as permalink. Used parse_url.", 1 ); - } else { - wp_cache_debug( "get_current_url_supercache_dir: WARNING! Permalink ($permalink) could not be understood by parse_url. Using front page.", 1 ); - $uri = ''; + wp_cache_debug( "get_current_url_supercache_dir: WARNING! site_url ($site_url) not found in permalink ($permalink).", 1 ); + if ( preg_match( '#^(https?:)?//([^/]+)(/[^\?\#]*)?((\?|\#).*)?$#i', $permalink, $matches ) && !empty( $matches[2] ) ) { + if ( $WPSC_HTTP_HOST != $matches[2] ) { + wp_cache_debug( "get_current_url_supercache_dir: WARNING! SERVER_NAME ({$WPSC_HTTP_HOST}) not found in permalink ($permalink).", 1 ); } + wp_cache_debug( "get_current_url_supercache_dir: Removing SERVER_NAME ({$matches[2]}) from permalink ($permalink). Is the url right?", 1 ); + $uri = isset( $matches[3] ) ? $matches[3] : ''; } else { - wp_cache_debug( "get_current_url_supercache_dir: Removing SERVER_NAME ({$WPSC_HTTP_HOST}) from permalink ($permalink). Is the url right?", 1 ); - $uri = str_replace( $WPSC_HTTP_HOST, '', $permalink ); - $uri = str_replace( 'http://', '', $uri ); - $uri = str_replace( 'https://', '', $uri ); + wp_cache_debug( "get_current_url_supercache_dir: WARNING! Permalink ($permalink) could not be understood by parsing url. Using front page.", 1 ); + $uri = ''; } } else { $uri = str_replace( $site_url, '', $permalink ); diff --git a/wp-cache-phase2.php b/wp-cache-phase2.php index 9699aa26..98e48d49 100644 --- a/wp-cache-phase2.php +++ b/wp-cache-phase2.php @@ -44,13 +44,13 @@ function wp_cache_phase2() { do_cacheaction( 'add_cacheaction' ); } - if ( is_admin() ) { + if ( wpsc_is_backend() ) { wp_cache_debug( 'Not caching wp-admin requests.', 5 ); return false; } - if ( !empty( $_GET ) && !defined( "DOING_CRON" ) ) { - wp_cache_debug( 'Supercache caching disabled. Only using wp-cache. Non empty GET request. ' . json_encode( $_GET ), 5 ); + if ( !empty( $_GET ) ) { + wp_cache_debug( 'Supercache caching disabled. Only using wp-cache. Non empty GET request. ' . wpsc_dump_get_request(), 5 ); $super_cache_enabled = false; } @@ -415,8 +415,8 @@ function wp_cache_ob_callback( $buffer ) { } elseif ( defined( 'DONOTCACHEPAGE' ) ) { wp_cache_debug( 'DONOTCACHEPAGE defined. Caching disabled.', 2 ); $cache_this_page = false; - } elseif ( $wp_cache_no_cache_for_get && false == empty( $_GET ) && false == defined( 'DOING_CRON' ) ) { - wp_cache_debug( "Non empty GET request. Caching disabled on settings page. " . json_encode( $_GET ), 1 ); + } elseif ( $wp_cache_no_cache_for_get && false == empty( $_GET ) ) { + wp_cache_debug( "Non empty GET request. Caching disabled on settings page. " . wpsc_dump_get_request(), 1 ); $cache_this_page = false; } elseif ( $_SERVER["REQUEST_METHOD"] == 'POST' || !empty( $_POST ) || get_option( 'gzipcompression' ) ) { wp_cache_debug( 'Not caching POST request.', 5 ); diff --git a/wp-cache.php b/wp-cache.php index d7fe6565..bff2662a 100644 --- a/wp-cache.php +++ b/wp-cache.php @@ -2263,7 +2263,7 @@ function wp_cache_remove_index() { } if ( is_dir( $directory . "/meta" ) ) { if ( is_file( $directory . "/meta/index.html" ) ) { - unlink( $directory . "/index.html" ); + unlink( $directory . "/meta/index.html" ); } } } From 40ff00e52501c3764e25b2d895d21f452e943261 Mon Sep 17 00:00:00 2001 From: stodorovic Date: Thu, 14 Sep 2017 11:36:29 +0200 Subject: [PATCH 2/3] Improve regexp in get_current_url_supercache_dir --- wp-cache-base.php | 4 ++++ wp-cache-phase1.php | 11 +++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/wp-cache-base.php b/wp-cache-base.php index f32a2040..f08b0935 100644 --- a/wp-cache-base.php +++ b/wp-cache-base.php @@ -2,6 +2,10 @@ if ( false == isset( $_SERVER[ 'HTTP_HOST' ] ) ) { $cache_enabled = false; $WPSC_HTTP_HOST = ''; + if ( ( defined( 'DOING_CRON' ) && DOING_CRON ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) { + // Workaround for php-cli wp-cron.php and wp-cli. + $WPSC_HTTP_HOST = parse_url( get_option( 'home' ), PHP_URL_HOST ); + } } else { $WPSC_HTTP_HOST = htmlentities( $_SERVER[ 'HTTP_HOST' ] ); } diff --git a/wp-cache-phase1.php b/wp-cache-phase1.php index 2ebb0a29..96361e7f 100644 --- a/wp-cache-phase1.php +++ b/wp-cache-phase1.php @@ -671,16 +671,19 @@ function get_current_url_supercache_dir( $post_id = 0 ) { $permalink = get_permalink( $post_id ); if ( false === strpos( $permalink, $site_url ) ) { /* - * Sometimes site_url doesn't return the siteurl. See http://wordpress.org/support/topic/wp-super-cache-not-refreshing-post-after-comments-made - */ + * Sometimes site_url doesn't return the siteurl. See https://wordpress.org/support/topic/wp-super-cache-not-refreshing-post-after-comments-made + */ $DONOTREMEMBER = 1; wp_cache_debug( "get_current_url_supercache_dir: WARNING! site_url ($site_url) not found in permalink ($permalink).", 1 ); - if ( preg_match( '#^(https?:)?//([^/]+)(/[^\?\#]*)?((\?|\#).*)?$#i', $permalink, $matches ) && !empty( $matches[2] ) ) { + if ( preg_match( '`^(https?:)?//([^/]+)(/.*)?$`i', $permalink, $matches ) ) { if ( $WPSC_HTTP_HOST != $matches[2] ) { wp_cache_debug( "get_current_url_supercache_dir: WARNING! SERVER_NAME ({$WPSC_HTTP_HOST}) not found in permalink ($permalink).", 1 ); } wp_cache_debug( "get_current_url_supercache_dir: Removing SERVER_NAME ({$matches[2]}) from permalink ($permalink). Is the url right?", 1 ); $uri = isset( $matches[3] ) ? $matches[3] : ''; + } elseif ( preg_match( '`^/([^/]+)(/.*)?$`i', $permalink, $matches ) ) { + wp_cache_debug( "get_current_url_supercache_dir: WARNING! Permalink ($permalink) looks as absolute path. Is the url right?", 1 ); + $uri = $permalink; } else { wp_cache_debug( "get_current_url_supercache_dir: WARNING! Permalink ($permalink) could not be understood by parsing url. Using front page.", 1 ); $uri = ''; @@ -693,7 +696,7 @@ function get_current_url_supercache_dir( $post_id = 0 ) { } else { $uri = strtolower( $wp_cache_request_uri ); } - $uri = wpsc_deep_replace( array( '..', '\\', 'index.php', ), preg_replace( '/[ <>\'\"\r\n\t\(\)]/', '', preg_replace( "/(\?.*)?$/", '', $uri ) ) ); + $uri = wpsc_deep_replace( array( '..', '\\', 'index.php', ), preg_replace( '/[ <>\'\"\r\n\t\(\)]/', '', preg_replace( "/(\?.*)?(#.*)?$/", '', $uri ) ) ); $dir = preg_replace( '/:.*$/', '', $WPSC_HTTP_HOST ) . $uri; // To avoid XSS attacks if ( function_exists( "apply_filters" ) ) { $dir = apply_filters( 'supercache_dir', $dir ); From f8f7273ef451d9023c7fcb5e85eb0c8435f73728 Mon Sep 17 00:00:00 2001 From: stodorovic Date: Thu, 26 Oct 2017 12:57:55 +0200 Subject: [PATCH 3/3] Additional checking for $WPSC_HTTP_HOST --- wp-cache-base.php | 5 ----- wp-cache-phase1.php | 4 +++- wp-cache.php | 2 +- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/wp-cache-base.php b/wp-cache-base.php index 420fb85b..f32a2040 100644 --- a/wp-cache-base.php +++ b/wp-cache-base.php @@ -2,11 +2,6 @@ if ( false == isset( $_SERVER[ 'HTTP_HOST' ] ) ) { $cache_enabled = false; $WPSC_HTTP_HOST = ''; -/* We should find better place for this - if ( ( defined( 'DOING_CRON' ) && DOING_CRON ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) { - // Workaround for php-cli wp-cron.php and wp-cli. - $WPSC_HTTP_HOST = parse_url( get_option( 'home' ), PHP_URL_HOST ); - }*/ } else { $WPSC_HTTP_HOST = htmlentities( $_SERVER[ 'HTTP_HOST' ] ); } diff --git a/wp-cache-phase1.php b/wp-cache-phase1.php index ceb07348..544fb69e 100644 --- a/wp-cache-phase1.php +++ b/wp-cache-phase1.php @@ -686,7 +686,9 @@ function get_current_url_supercache_dir( $post_id = 0 ) { $uri = strtolower( $wp_cache_request_uri ); } $uri = wpsc_deep_replace( array( '..', '\\', 'index.php', ), preg_replace( '/[ <>\'\"\r\n\t\(\)]/', '', preg_replace( "/(\?.*)?(#.*)?$/", '', $uri ) ) ); - $dir = preg_replace( '/:.*$/', '', $WPSC_HTTP_HOST ) . $uri; // To avoid XSS attacks + // Get hostname from wp options for wp-cron, wp-cli and similar requests. + $hostname = empty( $WPSC_HTTP_HOST ) ? (string) parse_url( get_option( 'home' ), PHP_URL_HOST ) : $WPSC_HTTP_HOST; + $dir = preg_replace( '/:.*$/', '', $hostname ) . $uri; // To avoid XSS attacks if ( function_exists( "apply_filters" ) ) { $dir = apply_filters( 'supercache_dir', $dir ); } else { diff --git a/wp-cache.php b/wp-cache.php index d75cbf0f..85969edc 100644 --- a/wp-cache.php +++ b/wp-cache.php @@ -2461,7 +2461,7 @@ function wp_cache_verify_config_file() { } $new = true; } - if( $sem_id == 5419 && $cache_path != '' ) { + if ( $sem_id == 5419 && $cache_path != '' && $WPSC_HTTP_HOST != '' ) { $sem_id = crc32( $WPSC_HTTP_HOST . $cache_path ) & 0x7fffffff; wp_cache_replace_line('sem_id', '$sem_id = ' . $sem_id . ';', $wp_cache_config_file); }