default.php
86 lines
| 1 | <?php |
| 2 | |
| 3 | // phpcs:ignoreFile |
| 4 | exit; |
| 5 | |
| 6 | //Check everything exists before using it |
| 7 | if (!isset($_SERVER['HTTP_ACCEPT_ENCODING'])) { |
| 8 | $_SERVER['HTTP_ACCEPT_ENCODING'] = ''; |
| 9 | } |
| 10 | |
| 11 | if (!isset($_SERVER['HTTP_USER_AGENT'])) { |
| 12 | $_SERVER['HTTP_USER_AGENT'] = ''; |
| 13 | } |
| 14 | // Determine supported compression method |
| 15 | $gzip = strstr(sanitize_text_field($_SERVER['HTTP_ACCEPT_ENCODING']), 'gzip'); |
| 16 | $deflate = strstr(sanitize_text_field($_SERVER['HTTP_ACCEPT_ENCODING']), 'deflate'); |
| 17 | // Determine used compression method |
| 18 | $encoding = $gzip ? 'gzip' : ($deflate ? 'deflate' : 'none'); |
| 19 | |
| 20 | // Check for buggy versions of Internet Explorer |
| 21 | if (!strstr($_SERVER['HTTP_USER_AGENT'], 'Opera') && preg_match('/^Mozilla\/4\.0 \(compatible; MSIE ([0-9]\.[0-9])/i', $_SERVER['HTTP_USER_AGENT'], $matches)) { |
| 22 | $version = floatval($matches[1]); |
| 23 | |
| 24 | if ($version < 6) { |
| 25 | $encoding = 'none'; |
| 26 | } |
| 27 | |
| 28 | if ($version == 6 && !strstr($_SERVER['HTTP_USER_AGENT'], 'EV1')) { |
| 29 | $encoding = 'none'; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | //Some servers compress the output of PHP - Don't break in those cases |
| 34 | if (ini_get('output_handler') == 'ob_gzhandler' || ini_get('zlib.output_compression') == 1) { |
| 35 | $encoding = 'none'; |
| 36 | } |
| 37 | $iscompressed = file_exists(__FILE__ . '.' . $encoding); |
| 38 | |
| 39 | if ($encoding != 'none' && $iscompressed == false) { |
| 40 | $flag = ($encoding == 'gzip' ? FORCE_GZIP : FORCE_DEFLATE); |
| 41 | $code = file_get_contents(__FILE__ . '.none'); |
| 42 | $contents = gzencode($code, 9, $flag); |
| 43 | } else { |
| 44 | //Get data |
| 45 | $contents = file_get_contents(__FILE__ . '.' . $encoding); |
| 46 | } |
| 47 | // first check if we have to send 304 |
| 48 | // inspired by http://www.jonasjohn.de/snippets/php/caching.htm |
| 49 | $eTag = md5($contents); |
| 50 | $modTime = filemtime(__FILE__ . '.none'); |
| 51 | $eTagMatch = (isset($_SERVER['HTTP_IF_NONE_MATCH']) && strpos($_SERVER['HTTP_IF_NONE_MATCH'], $eTag)); |
| 52 | $modTimeMatch = (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) === $modTime); |
| 53 | |
| 54 | if (($modTimeMatch) || ($eTagMatch)) { |
| 55 | header('HTTP/1.1 304 Not Modified'); |
| 56 | header('Connection: close'); |
| 57 | } else { |
| 58 | // send all sorts of headers |
| 59 | $expireTime = 60 * 60 * 24 * 355; // 1y max according to RFC |
| 60 | |
| 61 | if ($encoding != 'none') { |
| 62 | header('Content-Encoding: ' . $encoding); |
| 63 | } |
| 64 | header('Vary: Accept-Encoding'); |
| 65 | header('Content-Length: ' . strlen($contents)); |
| 66 | header('Content-type: %%CONTENT%%; charset=utf-8'); |
| 67 | header('Cache-Control: max-age=' . $expireTime . ', public, must-revalidate'); |
| 68 | header('Cache-Control: max-age=' . $expireTime . ', public, immutable'); |
| 69 | header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $expireTime) . ' GMT'); |
| 70 | header('ETag: ' . $eTag); |
| 71 | header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $modTime) . ' GMT'); |
| 72 | // send output |
| 73 | echo wp_kses_post($contents); |
| 74 | |
| 75 | //And write to filesystem cache if not done yet |
| 76 | if ($encoding != 'none' && $iscompressed == false) { |
| 77 | //Write the content we sent |
| 78 | file_put_contents(__FILE__ . '.' . $encoding, $contents); |
| 79 | //And write the new content |
| 80 | $flag = ($encoding == 'gzip' ? FORCE_DEFLATE : FORCE_GZIP); |
| 81 | $ext = ($encoding == 'gzip' ? 'deflate' : 'gzip'); |
| 82 | $contents = gzencode($code, 9, $flag); |
| 83 | file_put_contents(__FILE__ . '.' . $ext, $contents); |
| 84 | } |
| 85 | } |
| 86 |