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