PluginProbe
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript / 4.0.17
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript v4.0.17
4.1.16 4.1.15 4.1.14 4.1.13 4.1.12 4.1.11 4.1.10 4.0.30 4.0.29 4.0.28 4.0.27 4.0.26 4.0.24 4.0.25 4.0.23 4.0.22 4.0.21 4.0.19 4.0.18 4.0.17 4.0.16 1.9.3 1.9.4 1.9.5 1.9.6 All 170 releases
searchpro / inc / common-functions.php

common-functions.php in BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript 4.0.17, at inc/common-functions.php

170 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 function bwp_pass_cookie_requirement() {
4 $berqconfigs = berqConfigs::getInstance();
5 $configs = $berqconfigs->get_configs();
6 $default = ['berqwpnocache', 'wp-postpass', 'comment_author', 'woocommerce_cart_hash', 'edd_items_in_cart'];
7 $excluded_cookies = array_merge($configs['exclude_cookies'], $default);
8
9 if (!empty($excluded_cookies)) {
10 foreach ($excluded_cookies as $cookie_id) {
11
12 // Skip if empty
13 if (empty($cookie_id)) {
14 continue;
15 }
16
17 foreach ($_COOKIE as $key => $value) {
18 if (strpos($key, $cookie_id) === 0) {
19 return false;
20 }
21 }
22 }
23 }
24
25 return true;
26 }
27
28 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'] ?? '/';
32 $url = $scheme . $host . $uri;
33 return strtolower($url);
34 }
35
36 // function bwp_build_cache_path($url) {
37 // $parts = parse_url($url);
38
39 // $host = $parts['host'] ?? '';
40 // $path = $parts['path'] ?? '/';
41
42 // // normalize
43 // $path = '/' . ltrim($path, '/');
44 // $path = rtrim($path, '/');
45
46 // if ($path === '') {
47 // $path = '/';
48 // }
49
50 // // sanitize
51 // $host = preg_replace('#[^a-zA-Z0-9\.\-]#', '', $host);
52 // $path = preg_replace('#[^a-zA-Z0-9/_-]#', '', $path);
53
54 // return $host . $path;
55 // }
56
57 function bwp_build_cache_path($url) {
58 $parts = parse_url($url);
59 $host = $parts['host'] ?? '';
60 $path = $parts['path'] ?? '/';
61 $query = $parts['query'] ?? '';
62
63 // normalize path
64 $path = '/' . ltrim($path, '/');
65 $path = rtrim($path, '/');
66 if ($path === '') {
67 $path = '/';
68 }
69
70 // sanitize host
71 $host = preg_replace('#[^a-zA-Z0-9\.\-]#', '', $host);
72
73 // Normalize percent-encoded chars to raw UTF-8, then strip only chars that
74 // are unsafe on filesystems (null bytes, path traversal sequences).
75 $path = urldecode($path);
76 $path = str_replace("\0", '', $path);
77 $path = preg_replace('#\.\.+#', '', $path);
78 $path = preg_replace('#[<>:"\\\\|?*]#', '', $path);
79
80 if ($query === '') {
81 return $path === '/' ? $host : $host . $path;
82 }
83
84 // normalize query param order so ?b=2&a=1 and ?a=1&b=2 hit the same cache
85 parse_str($query, $params);
86 ksort($params);
87 $normalized_query = http_build_query($params);
88
89 // hash it — query strings can contain anything and be very long
90 $query_hash = substr(md5($normalized_query), 0, 12);
91
92 return $host . $path . '/q-' . $query_hash;
93 }
94
95 function bwp_serve_advanced_cache($serve_from = 'plugin') {
96 if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'GET' && !bwp_is_user_logged_in() && !bwp_is_ajax() && !berqDetectCrawler::is_crawler() && bwp_pass_cookie_requirement()) {
97 $berqconfigs = berqConfigs::getInstance();
98 $configs = $berqconfigs->get_configs();
99 $url = bwp_get_request_url();
100 $url = @dropin_remove_ignore_params($url);
101 $cache_key = md5($url);
102 $cache_directory = WP_CONTENT_DIR . '/cache/berqwp/html/';
103 if (defined('MULTISITE') && MULTISITE) {
104 $blog_id = berqConfigs::detect_blog_id_from_request();
105 $cache_directory .= 'site-' . $blog_id . '/';
106 }
107 $cache_file = $cache_directory . $cache_key . '.html';
108 $cache_file_gz = $cache_directory . $cache_key . '.gz';
109 $cache_file_gz = $cache_directory . bwp_build_cache_path($url) . '/index.html.gz';
110 $cache_file = $cache_file_gz;
111 $cache_max_life = file_exists($cache_file) ? @filemtime($cache_file) + $configs['cache_lifespan'] : null;
112 // $compression_enabled = $configs['page_compression'] === true;
113 $compression_enabled = true;
114 $accept_encoding = $_SERVER['HTTP_ACCEPT_ENCODING'] ?? '';
115 $supports_gzip = strpos($accept_encoding, 'gzip') !== false;
116
117 if (berqwp_dropin_is_page_url_excluded($url)) {
118 return;
119 }
120
121 if (file_exists($cache_file) && $cache_max_life > time()) {
122 $file_content = @file_get_contents($cache_file);
123
124 if (!isset($_GET['creating_cache']) && file_exists($cache_file)) {
125
126 if (strpos($file_content, "Optimized with BerqWP's instant cache") !== false && (filemtime($cache_file) + DAY_IN_SECONDS) < time()) {
127 return;
128 }
129
130 // Prepare gzip cache file
131 if ($compression_enabled && file_exists($cache_file_gz)) {
132 $cache_file = $cache_file_gz;
133 // header('Content-Encoding: gzip');
134 header_remove('Content-Encoding');
135 }
136
137 $lastModified = filemtime($cache_file);
138 $etag = md5_file($cache_file);
139 header('ETag: ' . $etag);
140 header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $lastModified) . ' GMT');
141 header('X-File-Size: ' . filesize($cache_file), true);
142 header('Content-Type: text/html; charset=utf-8');
143 header("X-served: $serve_from");
144 header('Vary: Cookie');
145
146 // Check if the client has a cached copy and if it's still valid using Last-Modified
147 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)) {
148 // The client's cache is still valid based on Last-Modified, respond with a 304 Not Modified
149 header('HTTP/1.1 304 Not Modified');
150 // header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
151 header("Expires: 0");
152 header('Cache-Control: no-cache, must-revalidate');
153 exit();
154
155 }
156
157 // header('Cache-Control: public, max-age=0, s-maxage=3600, must-revalidate', true);
158 header('Cache-Control: public, max-age=60, s-maxage=3600, stale-while-revalidate=60, must-revalidate', true);
159 header('Vary: Accept-Encoding, Cookie');
160 header('Content-Encoding: gzip', true);
161 header('Content-Length: ' . filesize($cache_file), true);
162 readfile($cache_file);
163 exit();
164 }
165
166 }
167
168 }
169 }
170