LiteSpeed.php
91 lines
| 1 | <?php |
| 2 | |
| 3 | namespace NitroPack\Integration\Server; |
| 4 | |
| 5 | class LiteSpeed { |
| 6 | const STAGE = "very_early"; |
| 7 | const DEVICE_COOKIE = "ls_nitro_device"; |
| 8 | |
| 9 | public static function detect() { |
| 10 | return !empty($_SERVER["X-LSCACHE"]) || ( !empty($_SERVER["SERVER_SOFTWARE"]) && strtolower($_SERVER["SERVER_SOFTWARE"]) == "litespeed" ); |
| 11 | } |
| 12 | |
| 13 | public static function isCacheEnabled() { |
| 14 | return false; |
| 15 | return self::detect() && !empty($_SERVER["X-LSCACHE"]) && in_array("on", array_map("trim", explode(",", $_SERVER["X-LSCACHE"]))); |
| 16 | } |
| 17 | |
| 18 | public static function isCachePossible() { |
| 19 | return isset($_COOKIE[self::DEVICE_COOKIE]); |
| 20 | } |
| 21 | |
| 22 | public static function sendCacheHeader($maxAge = NULL) { |
| 23 | if (!$maxAge) { |
| 24 | header("X-LiteSpeed-Cache-Control: public"); |
| 25 | } else if (is_numeric($maxAge)) { |
| 26 | header("X-LiteSpeed-Cache-Control: public,max-age=" . (int)$maxAge); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | public static function purge($url = NULL, $tag = NULL) { |
| 31 | if ($url || $tag) { |
| 32 | $headerValues = []; |
| 33 | |
| 34 | if ($url) { |
| 35 | $urlObj = new \NitroPack\Url((new \NitroPack\Url($url))->getNormalized()); |
| 36 | if (!$urlObj->getQuery()) { |
| 37 | $headerValues[] = $urlObj->getPath(); |
| 38 | } else { |
| 39 | $headerValues[] = $urlObj->getPath() . "?" . $urlObj->getQuery(); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | if ($tag) { |
| 44 | $headerValues[] = "tag=" . $tag; |
| 45 | } |
| 46 | |
| 47 | header("X-LiteSpeed-Purge: " . implode(", ", $headerValues), false); |
| 48 | } else { |
| 49 | header("X-LiteSpeed-Purge: *", false); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | public function init($stage) { |
| 54 | return; |
| 55 | if (self::isCacheEnabled()) { |
| 56 | add_action('nitropack_integration_purge_url', [$this, 'purgeUrl']); |
| 57 | add_action('nitropack_integration_purge_all', [$this, 'purgeAll']); |
| 58 | add_action('nitropack_early_cache_headers', [$this, 'setupVary']); |
| 59 | add_action('nitropack_cacheable_cache_headers', [$this, 'allowProxyCache']); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | public function purgeUrl($url) { |
| 64 | self::purge($url); |
| 65 | } |
| 66 | |
| 67 | public function purgeAll() { |
| 68 | self::purge(); |
| 69 | } |
| 70 | |
| 71 | public function setupVary() { |
| 72 | header("X-LiteSpeed-Vary: cookie=" . self::DEVICE_COOKIE); |
| 73 | } |
| 74 | |
| 75 | public function allowProxyCache() { |
| 76 | if (self::isCachePossible()) { |
| 77 | self::sendCacheHeader(3600); |
| 78 | } else if (!empty($_SERVER["HTTP_USER_AGENT"])) { |
| 79 | $device = new \NitroPack\SDK\Device($_SERVER["HTTP_USER_AGENT"]); |
| 80 | if ($device->isMobile()) { |
| 81 | nitropack_setcookie(self::DEVICE_COOKIE, "mobile", time() + 86400); |
| 82 | } else if ($device->isTablet()) { |
| 83 | nitropack_setcookie(self::DEVICE_COOKIE, "tablet", time() + 86400); |
| 84 | } else { |
| 85 | nitropack_setcookie(self::DEVICE_COOKIE, "desktop", time() + 86400); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 |