| @@ -24,10 +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 | - $scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https://' : 'http://'; | |
| 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 | + | |
| 30 | 67 | $host = isset($_SERVER['HTTP_HOST']) ? strip_tags(stripslashes($_SERVER['HTTP_HOST'])) : 'localhost'; |
| 31 | 68 | $uri = isset($_SERVER['REQUEST_URI']) ? strip_tags(stripslashes($_SERVER['REQUEST_URI'])) : '/'; |
| 32 | 69 | $url = $scheme . $host . $uri; |
| 33 | 70 | return strtolower($url); |
| @@ -97,9 +134,15 @@ | ||
| 97 | 134 | if (php_sapi_name() === 'cli' || (defined('WP_CLI') && WP_CLI)) { |
| 98 | 135 | return; |
| 99 | 136 | } |
| 100 | 137 | |
| 138 | + if ( strpos( $_SERVER["REQUEST_URI"], "berqwp-ping" ) !== false ) { | |
| 139 | + echo "PONG"; | |
| 140 | + exit(); | |
| 141 | + } | |
| 142 | + | |
| 101 | 143 | if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'GET' && !bwp_is_user_logged_in() && !bwp_is_ajax() && !berqDetectCrawler::is_crawler() && bwp_pass_cookie_requirement()) { |
| 144 | + | |
| 102 | 145 | $berqconfigs = berqConfigs::getInstance(); |
| 103 | 146 | $configs = $berqconfigs->get_configs(); |
| 104 | 147 | $url = bwp_get_request_url(); |
| 105 | 148 | $url = @dropin_remove_ignore_params($url); |
| @@ -117,13 +160,15 @@ | ||
| 117 | 160 | // $compression_enabled = $configs['page_compression'] === true; |
| 118 | 161 | $compression_enabled = true; |
| 119 | 162 | $accept_encoding = $_SERVER['HTTP_ACCEPT_ENCODING'] ?? ''; |
| 120 | 163 | $supports_gzip = strpos($accept_encoding, 'gzip') !== false; |
| 164 | + $domain = isset($_SERVER['HTTP_HOST']) ? strip_tags(stripslashes($_SERVER['HTTP_HOST'])) : ''; | |
| 121 | 165 | |
| 122 | 166 | if (berqwp_dropin_is_page_url_excluded($url)) { |
| 123 | 167 | return; |
| 124 | 168 | } |
| 125 | 169 | |
| 170 | + | |
| 126 | 171 | if (file_exists($cache_file) && $cache_max_life > time()) { |
| 127 | 172 | $file_content = @file_get_contents($cache_file); |
| 128 | 173 | |
| 129 | 174 | if (!isset($_GET['creating_cache']) && file_exists($cache_file)) { |
| @@ -145,9 +190,10 @@ | ||
| 145 | 190 | header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $lastModified) . ' GMT'); |
| 146 | 191 | header('X-File-Size: ' . filesize($cache_file), true); |
| 147 | 192 | header('Content-Type: text/html; charset=utf-8'); |
| 148 | 193 | header("X-served: $serve_from"); |
| 149 | - header('Vary: Cookie'); | |
| 194 | + header("X-BerqWP-Cache: HIT"); | |
| 195 | + // header('Vary: Cookie'); | |
| 150 | 196 | |
| 151 | 197 | // Check if the client has a cached copy and if it's still valid using Last-Modified |
| 152 | 198 | 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 | 199 | // The client's cache is still valid based on Last-Modified, respond with a 304 Not Modified |
| @@ -152,16 +198,32 @@ | ||
| 152 | 198 | 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 | 199 | // The client's cache is still valid based on Last-Modified, respond with a 304 Not Modified |
| 154 | 200 | header('HTTP/1.1 304 Not Modified'); |
| 155 | 201 | // header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT'); |
| 156 | - header("Expires: 0"); | |
| 157 | - header('Cache-Control: no-cache, must-revalidate'); | |
| 202 | + // header("Expires: 0"); | |
| 203 | + // header('Cache-Control: no-cache, must-revalidate'); | |
| 204 | + header('Cache-Control: public, max-age=600, s-maxage=2592000, stale-while-revalidate=86400', true); | |
| 205 | + header('cdn-cache-control: max-age=2592000'); | |
| 206 | + header("Cache-Tag: $domain"); | |
| 158 | 207 | exit(); |
| 159 | 208 | |
| 160 | 209 | } |
| 161 | 210 | |
| 211 | + // Prevent PHP/Apache from re-compressing the already-gzipped cache file | |
| 212 | + if (ini_get('zlib.output_compression')) { | |
| 213 | + ini_set('zlib.output_compression', 'Off'); | |
| 214 | + } | |
| 215 | + | |
| 216 | + while (ob_get_level()) { | |
| 217 | + ob_end_clean(); | |
| 218 | + } | |
| 219 | + | |
| 162 | 220 | // 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); | |
| 221 | + // header('Cache-Control: public, max-age=60, s-maxage=3600, stale-while-revalidate=60, must-revalidate', true); | |
| 222 | + header('Cache-Control: public, max-age=600, s-maxage=2592000, stale-while-revalidate=86400', true); | |
| 223 | + header('cdn-cache-control: max-age=2592000'); | |
| 224 | + header("Cache-Tag: $domain"); | |
| 225 | + // header("Content-Security-Policy: script-src 'self' blob: 'unsafe-inline'"); | |
| 164 | 226 | header('Vary: Accept-Encoding, Cookie'); |
| 165 | 227 | header('Content-Encoding: gzip', true); |
| 166 | 228 | header('Content-Length: ' . filesize($cache_file), true); |
| 167 | 229 | readfile($cache_file); |