| @@ -24,12 +24,49 @@ | ||
| 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 | - $scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https://' : 'http://'; | |
| 30 | - $host = $_SERVER['HTTP_HOST'] ?? 'localhost'; // Fallback to 'localhost' if unavailable | |
| 31 | - $uri = $_SERVER['REQUEST_URI'] ?? '/'; | |
| 58 | + | |
| 59 | + $scheme = 'http://'; | |
| 60 | + | |
| 61 | + if (bwp_request_is_https()) { | |
| 62 | + $scheme = 'https://'; | |
| 63 | + } elseif (function_exists('wp_parse_url') && function_exists('get_option')) { | |
| 64 | + $scheme = (wp_parse_url(get_option('home'), PHP_URL_SCHEME) === 'https') ? 'https://' : 'http://'; | |
| 65 | + } | |
| 66 | + | |
| 67 | + $host = isset($_SERVER['HTTP_HOST']) ? strip_tags(stripslashes($_SERVER['HTTP_HOST'])) : 'localhost'; | |
| 68 | + $uri = isset($_SERVER['REQUEST_URI']) ? strip_tags(stripslashes($_SERVER['REQUEST_URI'])) : '/'; | |
| 32 | 69 | $url = $scheme . $host . $uri; |
| 33 | 70 | return strtolower($url); |
| 34 | 71 | } |
| 35 | 72 | |
| @@ -91,8 +128,36 @@ | ||
| 91 | 128 | |
| 92 | 129 | return $host . $path . '/q-' . $query_hash; |
| 93 | 130 | } |
| 94 | 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 | + | |
| 95 | 160 | function bwp_serve_advanced_cache($serve_from = 'plugin') { |
| 96 | 161 | |
| 97 | 162 | if (php_sapi_name() === 'cli' || (defined('WP_CLI') && WP_CLI)) { |
| 98 | 163 | return; |
| @@ -97,9 +162,15 @@ | ||
| 97 | 162 | if (php_sapi_name() === 'cli' || (defined('WP_CLI') && WP_CLI)) { |
| 98 | 163 | return; |
| 99 | 164 | } |
| 100 | 165 | |
| 166 | + if ( strpos( $_SERVER["REQUEST_URI"], "berqwp-ping" ) !== false ) { | |
| 167 | + echo "PONG"; | |
| 168 | + exit(); | |
| 169 | + } | |
| 170 | + | |
| 101 | 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 | + | |
| 102 | 173 | $berqconfigs = berqConfigs::getInstance(); |
| 103 | 174 | $configs = $berqconfigs->get_configs(); |
| 104 | 175 | $url = bwp_get_request_url(); |
| 105 | 176 | $url = @dropin_remove_ignore_params($url); |
| @@ -117,13 +188,22 @@ | ||
| 117 | 188 | // $compression_enabled = $configs['page_compression'] === true; |
| 118 | 189 | $compression_enabled = true; |
| 119 | 190 | $accept_encoding = $_SERVER['HTTP_ACCEPT_ENCODING'] ?? ''; |
| 120 | 191 | $supports_gzip = strpos($accept_encoding, 'gzip') !== false; |
| 192 | + $domain = isset($_SERVER['HTTP_HOST']) ? strip_tags(stripslashes($_SERVER['HTTP_HOST'])) : ''; | |
| 121 | 193 | |
| 122 | 194 | if (berqwp_dropin_is_page_url_excluded($url)) { |
| 123 | 195 | return; |
| 124 | 196 | } |
| 125 | 197 | |
| 198 | + $request_path = parse_url($url, PHP_URL_PATH) ?? '/'; | |
| 199 | + if ($request_path !== '/' && isset($configs['permalink_trailing_slash']) && $configs['permalink_trailing_slash'] !== null) { | |
| 200 | + $has_trailing_slash = substr($request_path, -1) === '/'; | |
| 201 | + if ($configs['permalink_trailing_slash'] !== $has_trailing_slash) { | |
| 202 | + bwp_redirect_to_canonical_slash($configs['permalink_trailing_slash']); | |
| 203 | + } | |
| 204 | + } | |
| 205 | + | |
| 126 | 206 | if (file_exists($cache_file) && $cache_max_life > time()) { |
| 127 | 207 | $file_content = @file_get_contents($cache_file); |
| 128 | 208 | |
| 129 | 209 | if (!isset($_GET['creating_cache']) && file_exists($cache_file)) { |
| @@ -145,9 +225,10 @@ | ||
| 145 | 225 | header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $lastModified) . ' GMT'); |
| 146 | 226 | header('X-File-Size: ' . filesize($cache_file), true); |
| 147 | 227 | header('Content-Type: text/html; charset=utf-8'); |
| 148 | 228 | header("X-served: $serve_from"); |
| 149 | - header('Vary: Cookie'); | |
| 229 | + header("X-BerqWP-Cache: HIT"); | |
| 230 | + // header('Vary: Cookie'); | |
| 150 | 231 | |
| 151 | 232 | // Check if the client has a cached copy and if it's still valid using Last-Modified |
| 152 | 233 | if ((isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $lastModified) || (isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] === $etag)) { |
| 153 | 234 | // The client's cache is still valid based on Last-Modified, respond with a 304 Not Modified |
| @@ -152,16 +233,32 @@ | ||
| 152 | 233 | if ((isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $lastModified) || (isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] === $etag)) { |
| 153 | 234 | // The client's cache is still valid based on Last-Modified, respond with a 304 Not Modified |
| 154 | 235 | header('HTTP/1.1 304 Not Modified'); |
| 155 | 236 | // header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT'); |
| 156 | - header("Expires: 0"); | |
| 157 | - header('Cache-Control: no-cache, must-revalidate'); | |
| 237 | + // header("Expires: 0"); | |
| 238 | + // header('Cache-Control: no-cache, must-revalidate'); | |
| 239 | + header('Cache-Control: public, max-age=600, s-maxage=2592000, stale-while-revalidate=86400', true); | |
| 240 | + header('cdn-cache-control: max-age=2592000'); | |
| 241 | + header("Cache-Tag: $domain"); | |
| 158 | 242 | exit(); |
| 159 | 243 | |
| 160 | 244 | } |
| 161 | 245 | |
| 246 | + // Prevent PHP/Apache from re-compressing the already-gzipped cache file | |
| 247 | + if (ini_get('zlib.output_compression')) { | |
| 248 | + ini_set('zlib.output_compression', 'Off'); | |
| 249 | + } | |
| 250 | + | |
| 251 | + while (ob_get_level()) { | |
| 252 | + ob_end_clean(); | |
| 253 | + } | |
| 254 | + | |
| 162 | 255 | // header('Cache-Control: public, max-age=0, s-maxage=3600, must-revalidate', true); |
| 163 | - header('Cache-Control: public, max-age=60, s-maxage=3600, stale-while-revalidate=60, must-revalidate', true); | |
| 256 | + // header('Cache-Control: public, max-age=60, s-maxage=3600, stale-while-revalidate=60, must-revalidate', true); | |
| 257 | + header('Cache-Control: public, max-age=600, s-maxage=2592000, stale-while-revalidate=86400', true); | |
| 258 | + header('cdn-cache-control: max-age=2592000'); | |
| 259 | + header("Cache-Tag: $domain"); | |
| 260 | + // header("Content-Security-Policy: script-src 'self' blob: 'unsafe-inline'"); | |
| 164 | 261 | header('Vary: Accept-Encoding, Cookie'); |
| 165 | 262 | header('Content-Encoding: gzip', true); |
| 166 | 263 | header('Content-Length: ' . filesize($cache_file), true); |
| 167 | 264 | readfile($cache_file); |