Cloudflare.php
1 year ago
Fastly.php
1 year ago
LiteSpeed.php
1 year ago
NestifyCDN.php
1 year ago
NginxFastCgi.php
1 year ago
Sucuri.php
1 year ago
Cloudflare.php
53 lines
| 1 | <?php |
| 2 | |
| 3 | namespace NitroPack\Integration\Server; |
| 4 | |
| 5 | // We need this to control Cloudflare in addition to any other proxy potentially provided by the origin host company |
| 6 | class Cloudflare { |
| 7 | const STAGE = "very_early"; |
| 8 | |
| 9 | public static function detect() { |
| 10 | return (!empty($_SERVER["HTTP_CF_CONNECTING_IP"]) && $_SERVER["HTTP_CF_CONNECTING_IP"] != "0.0.0.0") |
| 11 | || !empty($_SERVER["HTTP_CF_RAY"]) |
| 12 | || !empty($_SERVER["HTTP_CF_IPCOUNTRY"]); |
| 13 | } |
| 14 | |
| 15 | public static function isCacheEnabled() { |
| 16 | $siteConfig = get_nitropack()->getSiteConfig(); |
| 17 | if ($siteConfig && !empty($siteConfig["hosting"]) && $siteConfig["hosting"] == "rocketnet") { |
| 18 | return self::detect(); |
| 19 | } |
| 20 | return self::detect() && !empty($_SERVER["HTTP_SEC_CH_UA_MOBILE"]); |
| 21 | } |
| 22 | |
| 23 | public function init($stage) { |
| 24 | if (self::detect()) { |
| 25 | nitropack_header("Accept-CH: Sec-CH-UA-Mobile"); |
| 26 | |
| 27 | if (self::isCacheEnabled()) { |
| 28 | add_action('nitropack_cacheable_cache_headers', [$this, 'allowProxyCache'], PHP_INT_MAX-1); |
| 29 | add_action('nitropack_cachehit_cache_headers', [$this, 'allowProxyCache'], PHP_INT_MAX-1); |
| 30 | } else { |
| 31 | add_action('nitropack_cacheable_cache_headers', [$this, 'preventProxyCache'], PHP_INT_MAX-1); |
| 32 | add_action('nitropack_cachehit_cache_headers', [$this, 'preventProxyCache'], PHP_INT_MAX-1); |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | public function allowProxyCache() { |
| 38 | $siteConfig = get_nitropack()->getSiteConfig(); |
| 39 | if ($siteConfig && !empty($siteConfig["hosting"]) && $siteConfig["hosting"] == "rocketnet") { |
| 40 | nitropack_header( "Cloudflare-CDN-Cache-Control: public, max-age=0, s-maxage=300, stale-while-revalidate=3600" ); |
| 41 | } else if ($siteConfig && !empty($siteConfig["isApoActive"])) { |
| 42 | nitropack_header("Cloudflare-CDN-Cache-Control: public, max-age=0, s-maxage=3600, stale-while-revalidate=3600"); |
| 43 | } else { |
| 44 | nitropack_header("Vary: sec-ch-ua-mobile"); |
| 45 | nitropack_header("Cloudflare-CDN-Cache-Control: public, max-age=0, s-maxage=15, stale-while-revalidate=3600"); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | public function preventProxyCache() { |
| 50 | nitropack_header("Cloudflare-CDN-Cache-Control: no-cache"); |
| 51 | } |
| 52 | } |
| 53 |