| @@ -128,8 +128,36 @@ | ||
| 128 | 128 | |
| 129 | 129 | return $host . $path . '/q-' . $query_hash; |
| 130 | 130 | } |
| 131 | 131 | |
| 132 | +/** | |
| 133 | + * Redirect a mismatched-trailing-slash request to its canonical form, the same | |
| 134 | + * way WordPress's redirect_canonical() would — but without needing WordPress | |
| 135 | + * to boot first. Used by the advanced-cache.php dropin, which runs before the | |
| 136 | + * DB connection is available. | |
| 137 | + */ | |
| 138 | +function bwp_redirect_to_canonical_slash($wants_trailing_slash) { | |
| 139 | + $uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/'; | |
| 140 | + $parts = explode('?', $uri, 2); | |
| 141 | + $path = $parts[0]; | |
| 142 | + $query = isset($parts[1]) ? '?' . $parts[1] : ''; | |
| 143 | + | |
| 144 | + if ($wants_trailing_slash) { | |
| 145 | + $path = rtrim($path, '/') . '/'; | |
| 146 | + } else { | |
| 147 | + $path = rtrim($path, '/'); | |
| 148 | + if ($path === '') { | |
| 149 | + $path = '/'; | |
| 150 | + } | |
| 151 | + } | |
| 152 | + | |
| 153 | + $scheme = bwp_request_is_https() ? 'https://' : 'http://'; | |
| 154 | + $host = isset($_SERVER['HTTP_HOST']) ? strip_tags(stripslashes($_SERVER['HTTP_HOST'])) : ''; | |
| 155 | + | |
| 156 | + header('Location: ' . $scheme . $host . $path . $query, true, 301); | |
| 157 | + exit(); | |
| 158 | +} | |
| 159 | + | |
| 132 | 160 | function bwp_serve_advanced_cache($serve_from = 'plugin') { |
| 133 | 161 | |
| 134 | 162 | if (php_sapi_name() === 'cli' || (defined('WP_CLI') && WP_CLI)) { |
| 135 | 163 | return; |
| @@ -134,8 +162,13 @@ | ||
| 134 | 162 | if (php_sapi_name() === 'cli' || (defined('WP_CLI') && WP_CLI)) { |
| 135 | 163 | return; |
| 136 | 164 | } |
| 137 | 165 | |
| 166 | + if ( strpos( $_SERVER["REQUEST_URI"], "berqwp-ping" ) !== false ) { | |
| 167 | + echo "PONG"; | |
| 168 | + exit(); | |
| 169 | + } | |
| 170 | + | |
| 138 | 171 | if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'GET' && !bwp_is_user_logged_in() && !bwp_is_ajax() && !berqDetectCrawler::is_crawler() && bwp_pass_cookie_requirement()) { |
| 139 | 172 | |
| 140 | 173 | $berqconfigs = berqConfigs::getInstance(); |
| 141 | 174 | $configs = $berqconfigs->get_configs(); |
| @@ -161,8 +194,22 @@ | ||
| 161 | 194 | if (berqwp_dropin_is_page_url_excluded($url)) { |
| 162 | 195 | return; |
| 163 | 196 | } |
| 164 | 197 | |
| 198 | + $request_path = parse_url($url, PHP_URL_PATH) ?? '/'; | |
| 199 | + $extension = strtolower(pathinfo($request_path, PATHINFO_EXTENSION)); | |
| 200 | + $allowed_extension = ['html', 'htm']; | |
| 201 | + | |
| 202 | + if (!empty($extension) && !in_array($extension, $allowed_extension)) { | |
| 203 | + return; | |
| 204 | + } | |
| 205 | + | |
| 206 | + if ($request_path !== '/' && isset($configs['permalink_trailing_slash']) && $configs['permalink_trailing_slash'] !== null) { | |
| 207 | + $has_trailing_slash = substr($request_path, -1) === '/'; | |
| 208 | + if ($configs['permalink_trailing_slash'] !== $has_trailing_slash) { | |
| 209 | + bwp_redirect_to_canonical_slash($configs['permalink_trailing_slash']); | |
| 210 | + } | |
| 211 | + } | |
| 165 | 212 | |
| 166 | 213 | if (file_exists($cache_file) && $cache_max_life > time()) { |
| 167 | 214 | $file_content = @file_get_contents($cache_file); |
| 168 | 215 | |