get_configs(); $default = ['berqwpnocache', 'wp-postpass', 'comment_author', 'woocommerce_cart_hash', 'edd_items_in_cart']; $excluded_cookies = array_merge($configs['exclude_cookies'], $default); if (!empty($excluded_cookies)) { foreach ($excluded_cookies as $cookie_id) { // Skip if empty if (empty($cookie_id)) { continue; } foreach ($_COOKIE as $key => $value) { if (strpos($key, $cookie_id) === 0) { return false; } } } } return true; } function bwp_request_is_https() { if (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') { return true; } // Reverse proxy / load balancer terminating TLS upstream if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO'])) { // may be a comma-separated chain: "https,http" — first hop is the client $proto = strtolower(trim(explode(',', $_SERVER['HTTP_X_FORWARDED_PROTO'])[0])); if ($proto === 'https') { return true; } } if (!empty($_SERVER['HTTP_X_FORWARDED_SSL']) && strtolower($_SERVER['HTTP_X_FORWARDED_SSL']) === 'on') { return true; } if (!empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) === 'on') { return true; } if (!empty($_SERVER['SERVER_PORT']) && (int) $_SERVER['SERVER_PORT'] === 443) { return true; } return false; } function bwp_get_request_url() { $scheme = 'http://'; if (bwp_request_is_https()) { $scheme = 'https://'; } elseif (function_exists('wp_parse_url') && function_exists('get_option')) { $scheme = (wp_parse_url(get_option('home'), PHP_URL_SCHEME) === 'https') ? 'https://' : 'http://'; } $host = isset($_SERVER['HTTP_HOST']) ? strip_tags(stripslashes($_SERVER['HTTP_HOST'])) : 'localhost'; $uri = isset($_SERVER['REQUEST_URI']) ? strip_tags(stripslashes($_SERVER['REQUEST_URI'])) : '/'; $url = $scheme . $host . $uri; return strtolower($url); } // function bwp_build_cache_path($url) { // $parts = parse_url($url); // $host = $parts['host'] ?? ''; // $path = $parts['path'] ?? '/'; // // normalize // $path = '/' . ltrim($path, '/'); // $path = rtrim($path, '/'); // if ($path === '') { // $path = '/'; // } // // sanitize // $host = preg_replace('#[^a-zA-Z0-9\.\-]#', '', $host); // $path = preg_replace('#[^a-zA-Z0-9/_-]#', '', $path); // return $host . $path; // } function bwp_build_cache_path($url) { $parts = parse_url($url); $host = $parts['host'] ?? ''; $path = $parts['path'] ?? '/'; $query = $parts['query'] ?? ''; // normalize path $path = '/' . ltrim($path, '/'); $path = rtrim($path, '/'); if ($path === '') { $path = '/'; } // sanitize host $host = preg_replace('#[^a-zA-Z0-9\.\-]#', '', $host); // Normalize percent-encoded chars to raw UTF-8, then strip only chars that // are unsafe on filesystems (null bytes, path traversal sequences). $path = urldecode($path); $path = str_replace("\0", '', $path); $path = preg_replace('#\.\.+#', '', $path); $path = preg_replace('#[<>:"\\\\|?*]#', '', $path); if ($query === '') { return $path === '/' ? $host : $host . $path; } // normalize query param order so ?b=2&a=1 and ?a=1&b=2 hit the same cache parse_str($query, $params); ksort($params); $normalized_query = http_build_query($params); // hash it — query strings can contain anything and be very long $query_hash = substr(md5($normalized_query), 0, 12); return $host . $path . '/q-' . $query_hash; } function bwp_serve_advanced_cache($serve_from = 'plugin') { if (php_sapi_name() === 'cli' || (defined('WP_CLI') && WP_CLI)) { return; } if ( strpos( $_SERVER["REQUEST_URI"], "berqwp-ping" ) !== false ) { echo "PONG"; exit(); } if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'GET' && !bwp_is_user_logged_in() && !bwp_is_ajax() && !berqDetectCrawler::is_crawler() && bwp_pass_cookie_requirement()) { $berqconfigs = berqConfigs::getInstance(); $configs = $berqconfigs->get_configs(); $url = bwp_get_request_url(); $url = @dropin_remove_ignore_params($url); $cache_key = md5($url); $cache_directory = WP_CONTENT_DIR . '/cache/berqwp/html/'; if (defined('MULTISITE') && MULTISITE) { $blog_id = berqConfigs::detect_blog_id_from_request(); $cache_directory .= 'site-' . $blog_id . '/'; } $cache_file = $cache_directory . $cache_key . '.html'; $cache_file_gz = $cache_directory . $cache_key . '.gz'; $cache_file_gz = $cache_directory . bwp_build_cache_path($url) . '/index.html.gz'; $cache_file = $cache_file_gz; $cache_max_life = file_exists($cache_file) ? @filemtime($cache_file) + $configs['cache_lifespan'] : null; // $compression_enabled = $configs['page_compression'] === true; $compression_enabled = true; $accept_encoding = $_SERVER['HTTP_ACCEPT_ENCODING'] ?? ''; $supports_gzip = strpos($accept_encoding, 'gzip') !== false; $domain = isset($_SERVER['HTTP_HOST']) ? strip_tags(stripslashes($_SERVER['HTTP_HOST'])) : ''; if (berqwp_dropin_is_page_url_excluded($url)) { return; } if (file_exists($cache_file) && $cache_max_life > time()) { $file_content = @file_get_contents($cache_file); if (!isset($_GET['creating_cache']) && file_exists($cache_file)) { if (strpos($file_content, "Optimized with BerqWP's instant cache") !== false && (filemtime($cache_file) + DAY_IN_SECONDS) < time()) { return; } // Prepare gzip cache file if ($compression_enabled && file_exists($cache_file_gz)) { $cache_file = $cache_file_gz; // header('Content-Encoding: gzip'); header_remove('Content-Encoding'); } $lastModified = filemtime($cache_file); $etag = md5_file($cache_file); header('ETag: ' . $etag); header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $lastModified) . ' GMT'); header('X-File-Size: ' . filesize($cache_file), true); header('Content-Type: text/html; charset=utf-8'); header("X-served: $serve_from"); header("X-BerqWP-Cache: HIT"); // header('Vary: Cookie'); // Check if the client has a cached copy and if it's still valid using Last-Modified 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)) { // The client's cache is still valid based on Last-Modified, respond with a 304 Not Modified header('HTTP/1.1 304 Not Modified'); // header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // header("Expires: 0"); // header('Cache-Control: no-cache, must-revalidate'); header('Cache-Control: public, max-age=600, s-maxage=2592000, stale-while-revalidate=86400', true); header('cdn-cache-control: max-age=2592000'); header("Cache-Tag: $domain"); exit(); } // Prevent PHP/Apache from re-compressing the already-gzipped cache file if (ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); } while (ob_get_level()) { ob_end_clean(); } // header('Cache-Control: public, max-age=0, s-maxage=3600, must-revalidate', true); // header('Cache-Control: public, max-age=60, s-maxage=3600, stale-while-revalidate=60, must-revalidate', true); header('Cache-Control: public, max-age=600, s-maxage=2592000, stale-while-revalidate=86400', true); header('cdn-cache-control: max-age=2592000'); header("Cache-Tag: $domain"); // header("Content-Security-Policy: script-src 'self' blob: 'unsafe-inline'"); header('Vary: Accept-Encoding, Cookie'); header('Content-Encoding: gzip', true); header('Content-Length: ' . filesize($cache_file), true); readfile($cache_file); exit(); } } } }