1 | <?php
|
---|
2 |
|
---|
3 | /** This file is part of KCFinder project
|
---|
4 | *
|
---|
5 | * @desc HTTP cache helper class
|
---|
6 | * @package KCFinder
|
---|
7 | * @version 2.51
|
---|
8 | * @author Pavel Tzonkov <pavelc@users.sourceforge.net>
|
---|
9 | * @copyright 2010, 2011 KCFinder Project
|
---|
10 | * @license http://www.opensource.org/licenses/gpl-2.0.php GPLv2
|
---|
11 | * @license http://www.opensource.org/licenses/lgpl-2.1.php LGPLv2
|
---|
12 | * @link http://kcfinder.sunhater.com
|
---|
13 | */
|
---|
14 |
|
---|
15 | class httpCache {
|
---|
16 | const DEFAULT_TYPE = "text/html";
|
---|
17 | const DEFAULT_EXPIRE = 604800; // in seconds
|
---|
18 |
|
---|
19 | /** Cache a file. The $type parameter might define the MIME type of the file
|
---|
20 | * or path to magic file to autodetect the MIME type. If you skip $type
|
---|
21 | * parameter the method will try with the default magic file. Autodetection
|
---|
22 | * of MIME type requires Fileinfo PHP extension used in file::getMimeType()
|
---|
23 | * @param string $file
|
---|
24 | * @param string $type
|
---|
25 | * @param integer $expire
|
---|
26 | * @param array $headers */
|
---|
27 |
|
---|
28 | static function file($file, $type=null, $expire=null, array $headers=null) {
|
---|
29 | $mtime = @filemtime($file);
|
---|
30 | if ($mtime !== false) self::checkMTime($mtime);
|
---|
31 |
|
---|
32 | if ($type === null) {
|
---|
33 | $magic = ((substr($type, 0, 1) == "/") || preg_match('/^[a-z]\:/i', $type))
|
---|
34 | ? $type : null;
|
---|
35 | $type = file::getMimeType($file, $magic);
|
---|
36 | if (!$type) $type = null;
|
---|
37 | }
|
---|
38 |
|
---|
39 | self::content(@file_get_contents($file), $mtime, $type, $expire, $headers, false);
|
---|
40 | }
|
---|
41 |
|
---|
42 | /** Cache the given $content with $mtime modification time.
|
---|
43 | * @param binary $content
|
---|
44 | * @param integer $mtime
|
---|
45 | * @param string $type
|
---|
46 | * @param integer $expire
|
---|
47 | * @param array $headers
|
---|
48 | * @param bool $checkMTime */
|
---|
49 |
|
---|
50 | static function content($content, $mtime, $type=null, $expire=null, array $headers=null, $checkMTime=true) {
|
---|
51 | if ($checkMTime) self::checkMTime($mtime);
|
---|
52 | if ($type === null) $type = self::DEFAULT_TYPE;
|
---|
53 | if ($expire === null) $expire = self::DEFAULT_EXPIRE;
|
---|
54 | $size = strlen($content);
|
---|
55 | $expires = gmdate("D, d M Y H:i:s", time() + $expire) . " GMT";
|
---|
56 | header("Content-Type: $type");
|
---|
57 | header("Expires: $expires");
|
---|
58 | header("Cache-Control: max-age=$expire");
|
---|
59 | header("Pragma: !invalid");
|
---|
60 | header("Content-Length: $size");
|
---|
61 | if ($headers !== null) foreach ($headers as $header) header($header);
|
---|
62 | echo $content;
|
---|
63 | }
|
---|
64 |
|
---|
65 | /** Check if given modification time is newer than client-side one. If not,
|
---|
66 | * the method will tell the client to get the content from its own cache.
|
---|
67 | * Afterwards the script process will be terminated. This feature requires
|
---|
68 | * the PHP to be configured as Apache module.
|
---|
69 | * @param integer $mtime */
|
---|
70 |
|
---|
71 | static function checkMTime($mtime, $sendHeaders=null) {
|
---|
72 | header("Last-Modified: " . gmdate("D, d M Y H:i:s", $mtime) . " GMT");
|
---|
73 |
|
---|
74 | $headers = function_exists("getallheaders")
|
---|
75 | ? getallheaders()
|
---|
76 | : (function_exists("apache_request_headers")
|
---|
77 | ? apache_request_headers()
|
---|
78 | : false);
|
---|
79 |
|
---|
80 | if (is_array($headers) && isset($headers['If-Modified-Since'])) {
|
---|
81 | $client_mtime = explode(';', $headers['If-Modified-Since']);
|
---|
82 | $client_mtime = @strtotime($client_mtime[0]);
|
---|
83 | if ($client_mtime >= $mtime) {
|
---|
84 | header('HTTP/1.1 304 Not Modified');
|
---|
85 | if (is_array($sendHeaders) && count($sendHeaders))
|
---|
86 | foreach ($sendHeaders as $header)
|
---|
87 | header($header);
|
---|
88 | elseif ($sendHeaders !== null)
|
---|
89 | header($sendHeaders);
|
---|
90 |
|
---|
91 |
|
---|
92 | die;
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | ?>
|
---|