-
Notifications
You must be signed in to change notification settings - Fork 1
/
bootstrap.php
445 lines (380 loc) · 17.6 KB
/
bootstrap.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
<?php
require_once dirname(__FILE__) . "/vendor/autoload.php";
use \NitroPack\Url\Url;
class NitroPack_CookieStore {
static $cookies = array();
}
NitroPack_CookieStore::$cookies = $_COOKIE;
/* You must define these constants before requiring this file for easier installation of SDK updates
define("NITROPACK_HOME_URL", "Your home page URL");
define("NITROPACK_SITE_ID", "your site ID");
define("NITROPACK_SITE_SECRET", "your site secret");
*/
if (!defined("NITROPACK_ENABLE_COMPRESSION")) define("NITROPACK_ENABLE_COMPRESSION", false); // Set this to true to enable compression. Only do this if your server does not already have compression enabled
if (!defined("NITROPACK_WEBHOOK_TOKEN")) define("NITROPACK_WEBHOOK_TOKEN", md5(__FILE__)); // Feel free to set this to a value of your liking
if (!defined("NITROPACK_USE_QUICK_PURGE")) define("NITROPACK_USE_QUICK_PURGE", false); // Feel free to set this to a value of your liking
if (!defined("NITROPACK_STRIP_IGNORENITRO")) define("NITROPACK_STRIP_IGNORENITRO", false); //Change value to true if ignorenitro parameter needs to be removed from REQUEST_URI
if (!defined("NITROPACK_USE_REDIS")) define("NITROPACK_USE_REDIS", false); // Set this to true to enable storing cache in Redis
if (!defined("NITROPACK_REDIS_HOST")) define("NITROPACK_REDIS_HOST", "127.0.0.1"); // Set this to the IP of your Redis server
if (!defined("NITROPACK_REDIS_PORT")) define("NITROPACK_REDIS_PORT", 6379); // Set this to the port of your Redis server
if (!defined("NITROPACK_REDIS_PASS")) define("NITROPACK_REDIS_PASS", NULL); // Set this to the password of your redis server if authentication is needed
if (!defined("NITROPACK_REDIS_DB")) define("NITROPACK_REDIS_DB", NULL); // Set this to the number of the Redis DB if you'd like to not use the default one
if (!defined("NITROPACK_DATA_DIR")) define("NITROPACK_DATA_DIR", NULL); // Set this to the number of the Redis DB if you'd like to not use the default one
if (!defined("NITROPACK_DISABLE_BACKLOG")) define("NITROPACK_DISABLE_BACKLOG", true); // Only allow backlog use if you've prepared a way to replay backlogged entries. Otherwise you might end up with a permanently disabled cache layer and an infinitely growing backlog file
if (NITROPACK_USE_REDIS) {
NitroPack\SDK\Filesystem::setStorageDriver(new NitroPack\SDK\StorageDriver\Redis(
NITROPACK_REDIS_HOST,
NITROPACK_REDIS_PORT,
NITROPACK_REDIS_PASS,
NITROPACK_REDIS_DB
));
}
function nitropack_filter_non_original_cookies(&$cookies) {
$ogNames = is_array(NitroPack_CookieStore::$cookies) ? array_keys(NitroPack_CookieStore::$cookies) : array();
foreach ($cookies as $name=>$val) {
if (!in_array($name, $ogNames)) {
unset($cookies[$name]);
}
}
}
function nitropack_get_instance($siteId = NULL, $siteSecret = NULL, $url = NULL) {
static $instances = [];
$key = $url ? $url : "auto";
if (empty($instances[$key])) {
try {
NitroPack\SDK\NitroPack::addCookieFilter("nitropack_filter_non_original_cookies");
$siteId = $siteId !== NULL ? $siteId : NITROPACK_SITE_ID;
$siteSecret = $siteSecret !== NULL ? $siteSecret : NITROPACK_SITE_SECRET;
if (NITROPACK_DATA_DIR) {
$instances[$key] = new NitroPack\SDK\NitroPack($siteId, $siteSecret, NULL, $url, NITROPACK_DATA_DIR);
} else {
$instances[$key] = new NitroPack\SDK\NitroPack($siteId, $siteSecret, NULL, $url);
}
} catch(\Exception $e) {
$instances[$key] = NULL;
}
}
return $instances[$key];
}
function nitropack_removeCacheBustParam($content) {
$content = preg_replace("/(\?|%26|�?38;|�?26;|&(amp;)?)ignorenitro(%3D|=)[a-fA-F0-9]{32}(?!%26|�?38;|�?26;|&(amp;)?)\/?/mu", "", $content);
return preg_replace("/(\?|%26|�?38;|�?26;|&(amp;)?)ignorenitro(%3D|=)[a-fA-F0-9]{32}(%26|�?38;|�?26;|&(amp;)?)/mu", "$1", $content);
}
function nitropack_handle_request() {
if (isset($_GET["ignorenitro"])) {
unset($_GET["ignorenitro"]);
}
if (defined("NITROPACK_STRIP_IGNORENITRO") && NITROPACK_STRIP_IGNORENITRO && $_SERVER['REQUEST_URI'] != '') {
$_SERVER['REQUEST_URI'] = nitropack_removeCacheBustParam($_SERVER['REQUEST_URI']);
}
header('Cache-Control: no-cache');
header('X-Nitro-Cache: MISS');
if ( !empty($_SERVER["HTTP_HOST"]) && !empty($_SERVER["REQUEST_URI"]) ) {
try {
if (is_valid_nitropack_webhook()) {
nitropack_handle_webhook();
} else {
if (is_valid_nitropack_beacon()) {
nitropack_handle_beacon();
} else {
if ( null !== $nitro = nitropack_get_instance() ) {
if ($nitro->isCacheAllowed()) {
if (NITROPACK_ENABLE_COMPRESSION) {
$nitro->enableCompression();
}
if ($nitro->hasLocalCache()) {
header('X-Nitro-Cache: HIT');
setcookie("nitroCache", "HIT", time() + 10);
$nitro->pageCache->readfile();
exit;
} else {
// We need the following if..else block to handle bot requests which will not be firing our beacon
if (nitropack_is_warmup_request()) {
$nitro->hasRemoteCache("default"); // Only ping the API letting our service know that this page must be cached.
exit; // No need to continue handling this request. The response is not important.
} else if (nitropack_is_lighthouse_request() || nitropack_is_gtmetrix_request() || nitropack_is_pingdom_request()) {
$nitro->hasRemoteCache("default"); // Ping the API letting our service know that this page must be cached.
}
$nitro->pageCache->useInvalidated(true);
if ($nitro->hasLocalCache()) {
header('X-Nitro-Cache: STALE');
$nitro->pageCache->readfile();
exit;
} else {
$nitro->pageCache->useInvalidated(false);
}
}
}
}
}
}
} catch (\Exception $e) {
// Fail silently
}
}
}
function nitropack_validate_webhook_token($token) {
return preg_match("/^([abcdef0-9]{32})$/", strtolower($token)) && $token == NITROPACK_WEBHOOK_TOKEN;
}
function is_valid_nitropack_webhook() {
return !empty($_GET["nitroWebhook"]) && !empty($_GET["token"]) && nitropack_validate_webhook_token($_GET["token"]);
}
function is_valid_nitropack_beacon() {
if (!isset($_POST["nitroBeaconUrl"]) || !isset($_POST["nitroBeaconHash"])) return false;
if (function_exists("hash_hmac") && function_exists("hash_equals")) {
$url = base64_decode($_POST["nitroBeaconUrl"]);
$cookiesJson = !empty($_POST["nitroBeaconCookies"]) ? base64_decode($_POST["nitroBeaconCookies"]) : ""; // We need to fall back to empty string to remain backwards compatible. Otherwise cache files invalidated before an upgrade will never get updated :(
$localHash = hash_hmac("sha512", $url.$cookiesJson, NITROPACK_SITE_SECRET);
return hash_equals($_POST["nitroBeaconHash"], $localHash);
} else {
return !empty($_POST["nitroBeaconUrl"]);
}
}
function nitropack_handle_beacon() {
if (!empty($_POST["nitroBeaconUrl"])) {
$url = base64_decode($_POST["nitroBeaconUrl"]);
if (!empty($_POST["nitroBeaconCookies"])) {
NitroPack_CookieStore::$cookies = json_decode(base64_decode($_POST["nitroBeaconCookies"]), true);
}
if (null !== $nitro = nitropack_get_instance(NITROPACK_SITE_ID, NITROPACK_SITE_SECRET, $url) ) {
try {
if (!$nitro->hasLocalCache(false)) {
header("X-Nitro-Beacon: FORWARD");
$hasCache = $nitro->hasRemoteCache("default", false); // Download the new cache file
$nitro->purgeProxyCache($url);
printf("Cache %s", $hasCache ? "fetched" : "requested");
} else {
header("X-Nitro-Beacon: SKIP");
printf("Cache exists already");
}
} catch (Exception $e) {
// not a critical error, do nothing
}
}
}
exit;
}
function nitropack_handle_webhook() {
if (NITROPACK_WEBHOOK_TOKEN == $_GET["token"]) {
switch($_GET["nitroWebhook"]) {
case "config":
if (null !== $nitro = nitropack_get_instance() ) {
try {
$nitro->fetchConfig();
} catch (\Exception $e) {}
}
break;
case "cache_ready":
if (!empty($_POST["url"])) {
$readyUrl = nitropack_sanitize_url_input($_POST["url"]);
if ($readyUrl && null !== $nitro = nitropack_get_instance(NITROPACK_SITE_ID, NITROPACK_SITE_SECRET, $readyUrl) ) {
$hasCache = $nitro->hasRemoteCache("default", false); // Download the new cache file
$nitro->purgeProxyCache($readyUrl);
}
}
break;
case "cache_clear":
if (!empty($_POST["url"])) {
$urls = is_array($_POST["url"]) ? $_POST["url"] : array($_POST["url"]);
foreach ($urls as $url) {
$sanitizedUrl = nitropack_sanitize_url_input($url);
nitropack_sdk_purge_local($sanitizedUrl);
}
} else {
nitropack_sdk_purge_local();
nitropack_sdk_delete_backlog();
}
break;
}
}
exit;
}
function nitropack_sanitize_url_input($url) {
$result = NULL;
$sanitizedUrl = filter_var($url, FILTER_SANITIZE_URL);
$urlObj = new \NitroPack\Url\Url($url);
if ($sanitizedUrl !== false && $urlObj->isValid() !== false) {
$result = $sanitizedUrl;
}
return $result;
}
function nitropack_sdk_invalidate($url = NULL, $tag = NULL, $reason = NULL) {
if (null !== $nitro = nitropack_get_instance()) {
try {
if ($tag) {
if (is_array($tag)) {
$tag = array_map('nitropack_filter_tag', $tag);
} else {
$tag = nitropack_filter_tag($tag);
}
}
if ($tag != "pageType:home") {
$nitro->invalidateCache(NITROPACK_HOME_URL, "pageType:home", $reason);
}
$nitro->invalidateCache($url, $tag, $reason);
} catch (\Exception $e) {
return false;
}
return true;
}
return false;
}
function nitropack_sdk_purge($url = NULL, $tag = NULL, $reason = NULL, $type = \NitroPack\SDK\PurgeType::COMPLETE) {
if (null !== $nitro = nitropack_get_instance()) {
try {
if ($tag) {
if (is_array($tag)) {
$tag = array_map('nitropack_filter_tag', $tag);
} else {
$tag = nitropack_filter_tag($tag);
}
}
if ($tag != "pageType:home") {
$nitro->invalidateCache(NITROPACK_HOME_URL, "pageType:home", $reason);
}
$nitro->purgeCache($url, $tag, $type, $reason);
} catch (\Exception $e) {
return false;
}
return true;
}
return false;
}
function nitropack_sdk_purge_local($url = NULL) {
if (null !== $nitro = nitropack_get_instance()) {
try {
if ($url) {
$nitro->purgeLocalUrlCache($url);
} else {
$nitro->purgeLocalCache(NITROPACK_USE_QUICK_PURGE);
}
} catch (\Exception $e) {
return false;
}
return true;
}
return false;
}
function nitropack_sdk_delete_backlog() {
if (null !== $nitro = nitropack_get_instance()) {
try {
if ($nitro->backlog->exists()) {
$nitro->backlog->delete();
}
} catch (\Exception $e) {
return false;
}
return true;
}
return false;
}
function nitropack_filter_tag($tag) {
return preg_replace("/[^a-zA-Z0-9:]/", ":", $tag);
}
function nitropack_is_warmup_request() {
return !empty($_SERVER["HTTP_X_NITRO_WARMUP"]);
}
function nitropack_is_lighthouse_request() {
return !empty($_SERVER["HTTP_USER_AGENT"]) && stripos($_SERVER["HTTP_USER_AGENT"], "lighthouse") !== false;
}
function nitropack_is_gtmetrix_request() {
return !empty($_SERVER["HTTP_USER_AGENT"]) && stripos($_SERVER["HTTP_USER_AGENT"], "gtmetrix") !== false;
}
function nitropack_is_pingdom_request() {
return !empty($_SERVER["HTTP_USER_AGENT"]) && stripos($_SERVER["HTTP_USER_AGENT"], "pingdom") !== false;
}
function nitropack_is_optimizer_request() {
return isset($_SERVER["HTTP_X_NITROPACK_REQUEST"]);
}
function nitropack_add_tag($tag = NULL, $flush = false) {
static $addedTags = [];
if ($tag) {
$addedTags[] = $tag;
}
if ($flush) {
if (null !== $nitro = nitropack_get_instance()) {
try {
// Check whether this is the home page and tag this URL with pageType:home
$nitro->getApi()->tagUrl($nitro->getUrl(), array_map("nitropack_filter_tag", $addedTags));
} catch (\Exception $e) {}
}
}
}
function nitropack_get_beacon_script() {
if (null !== $nitro = nitropack_get_instance() ) {
$url = $nitro->getUrl();
$cookiesJson = json_encode($nitro->supportedCookiesFilter(NitroPack\SDK\NitroPack::getCookies()));
if (function_exists("hash_hmac") && function_exists("hash_equals")) {
$hash = hash_hmac("sha512", $url.$cookiesJson, NITROPACK_SITE_SECRET);
} else {
$hash = "";
}
$url = base64_encode($url); // We want only ASCII
$cookiesb64 = base64_encode($cookiesJson);
return "<script nitro-exclude>if (document.cookie.indexOf('nitroCache=HIT') == -1) {var nitroData = new FormData(); nitroData.append('nitroBeaconUrl', '$url'); nitroData.append('nitroBeaconCookies', '$cookiesb64'); nitroData.append('nitroBeaconHash', '$hash'); navigator.sendBeacon(location.href, nitroData);} document.cookie = 'nitroCache=HIT; expires=Thu, 01 Jan 1970 00:00:01 GMT;';</script>";
}
}
function nitropack_init_webhooks() {
$webhookFile = dirname(__FILE__) . "/nitropack_webhooks";
if (!NitroPack\SDK\Filesystem::fileExists($webhookFile)) {
NitroPack\SDK\Filesystem::filePutContents($webhookFile, time());
if (null !== $nitro = nitropack_get_instance() ) {
try {
$configUrl = new Url(NITROPACK_HOME_URL . "?nitroWebhook=config&token=" . NITROPACK_WEBHOOK_TOKEN);
$cacheClearUrl = new Url(NITROPACK_HOME_URL . "?nitroWebhook=cache_clear&token=" . NITROPACK_WEBHOOK_TOKEN);
$cacheReadyUrl = new Url(NITROPACK_HOME_URL . "?nitroWebhook=cache_ready&token=" . NITROPACK_WEBHOOK_TOKEN);
$nitro->getApi()->setWebhook("config", $configUrl);
$nitro->getApi()->setWebhook("cache_clear", $cacheClearUrl);
$nitro->getApi()->setWebhook("cache_ready", $cacheReadyUrl);
} catch (\Exception $e) {}
}
}
}
nitropack_handle_request();
if ( null !== $nitro = nitropack_get_instance() ) {
if ($nitro->isAllowedUrl($nitro->getUrl()) && $nitro->isAllowedRequest(true)) {
nitropack_init_webhooks();
ob_start(function($buffer) {
$isGzipped = substr($buffer, 0, 3) === "\x1f\x8b\x08";
if ($isGzipped && (!function_exists("gzdecode") || !function_exists("gzencode"))) {
return $buffer;
}
if (nitropack_is_optimizer_request()) {
nitropack_add_tag(NULL, true); // Flush registered tags
}
// Get the content type
$respHeaders = headers_list();
$contentType = NULL;
foreach ($respHeaders as $respHeader) {
if (stripos(trim($respHeader), 'Content-Type:') === 0) {
$contentType = $respHeader;
}
}
// If the content type header was detected and it's value does not contain 'text/html',
// don't attach the beacon script.
if ($contentType !== NULL && stripos($contentType, 'text/html') === false) {
return $buffer;
}
if ($isGzipped) {
// Make a copy to return later in case something goes wrong
$bufferCopy = $buffer;
$buffer = gzdecode($buffer);
}
// Remove BOM from output
$bom = pack('H*','EFBBBF');
$buffer = preg_replace("/^($bom)*/", '', $buffer);
if (!preg_match("/<html.*?\s(amp|⚡)(\s|=|>)/", $buffer)) {
$buffer = str_replace("</body", nitropack_get_beacon_script() . "</body", $buffer);
}
if ($isGzipped) {
$buffer = gzencode($buffer);
if (empty($buffer)) {
return $bufferCopy;
}
}
return $buffer;
}, 0, PHP_OUTPUT_HANDLER_FLUSHABLE | PHP_OUTPUT_HANDLER_REMOVABLE);
} else {
header("X-Nitro-Disabled: 1");
}
}