| @@ -24,14 +24,47 @@ | ||
| 24 | 24 | |
| 25 | 25 | return true; |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | +function bwp_request_is_https() { | |
| 29 | + if (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') { | |
| 30 | + return true; | |
| 31 | + } | |
| 32 | + | |
| 33 | + // Reverse proxy / load balancer terminating TLS upstream | |
| 34 | + if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO'])) { | |
| 35 | + // may be a comma-separated chain: "https,http" — first hop is the client | |
| 36 | + $proto = strtolower(trim(explode(',', $_SERVER['HTTP_X_FORWARDED_PROTO'])[0])); | |
| 37 | + if ($proto === 'https') { | |
| 38 | + return true; | |
| 39 | + } | |
| 40 | + } | |
| 41 | + | |
| 42 | + if (!empty($_SERVER['HTTP_X_FORWARDED_SSL']) && strtolower($_SERVER['HTTP_X_FORWARDED_SSL']) === 'on') { | |
| 43 | + return true; | |
| 44 | + } | |
| 45 | + | |
| 46 | + if (!empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) === 'on') { | |
| 47 | + return true; | |
| 48 | + } | |
| 49 | + | |
| 50 | + if (!empty($_SERVER['SERVER_PORT']) && (int) $_SERVER['SERVER_PORT'] === 443) { | |
| 51 | + return true; | |
| 52 | + } | |
| 53 | + | |
| 54 | + return false; | |
| 55 | +} | |
| 56 | + | |
| 28 | 57 | function bwp_get_request_url() { |
| 29 | - if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') { | |
| 58 | + | |
| 59 | + $scheme = 'http://'; | |
| 60 | + | |
| 61 | + if (bwp_request_is_https()) { | |
| 30 | 62 | $scheme = 'https://'; |
| 31 | - } else { | |
| 63 | + } elseif (function_exists('wp_parse_url') && function_exists('get_option')) { | |
| 32 | 64 | $scheme = (wp_parse_url(get_option('home'), PHP_URL_SCHEME) === 'https') ? 'https://' : 'http://'; |
| 33 | 65 | } |
| 66 | + | |
| 34 | 67 | $host = isset($_SERVER['HTTP_HOST']) ? strip_tags(stripslashes($_SERVER['HTTP_HOST'])) : 'localhost'; |
| 35 | 68 | $uri = isset($_SERVER['REQUEST_URI']) ? strip_tags(stripslashes($_SERVER['REQUEST_URI'])) : '/'; |
| 36 | 69 | $url = $scheme . $host . $uri; |
| 37 | 70 | return strtolower($url); |
| @@ -95,8 +128,36 @@ | ||
| 95 | 128 | |
| 96 | 129 | return $host . $path . '/q-' . $query_hash; |
| 97 | 130 | } |
| 98 | 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 | + | |
| 99 | 160 | function bwp_serve_advanced_cache($serve_from = 'plugin') { |
| 100 | 161 | |
| 101 | 162 | if (php_sapi_name() === 'cli' || (defined('WP_CLI') && WP_CLI)) { |
| 102 | 163 | return; |
| @@ -101,9 +162,15 @@ | ||
| 101 | 162 | if (php_sapi_name() === 'cli' || (defined('WP_CLI') && WP_CLI)) { |
| 102 | 163 | return; |
| 103 | 164 | } |
| 104 | 165 | |
| 166 | + if ( strpos( $_SERVER["REQUEST_URI"], "berqwp-ping" ) !== false ) { | |
| 167 | + echo "PONG"; | |
| 168 | + exit(); | |
| 169 | + } | |
| 170 | + | |
| 105 | 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()) { |
| 172 | + | |
| 106 | 173 | $berqconfigs = berqConfigs::getInstance(); |
| 107 | 174 | $configs = $berqconfigs->get_configs(); |
| 108 | 175 | $url = bwp_get_request_url(); |
| 109 | 176 | $url = @dropin_remove_ignore_params($url); |
| @@ -125,8 +192,23 @@ | ||
| 125 | 192 | $domain = isset($_SERVER['HTTP_HOST']) ? strip_tags(stripslashes($_SERVER['HTTP_HOST'])) : ''; |
| 126 | 193 | |
| 127 | 194 | if (berqwp_dropin_is_page_url_excluded($url)) { |
| 128 | 195 | return; |
| 196 | + } | |
| 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 | + } | |
| 129 | 211 | } |
| 130 | 212 | |
| 131 | 213 | if (file_exists($cache_file) && $cache_max_life > time()) { |
| 132 | 214 | $file_content = @file_get_contents($cache_file); |