r4334 fixed some issues with recent commit about static content caching
[racktables] / wwwroot / inc / caching.php
1 <?php
2 /*
3 *
4 * This file is a library of HTTP cache functions.
5 * Intended to be small and effective and to be included only for some request types.
6 *
7 */
8
9 define ('CACHE_DURATION', 604800); // 7 * 24 * 3600
10
11 // if client passes If-Modified-Since header, and it is greater or equal to $creation_ts, and
12 // $expire seconds not elapsed since IMS, the function sends HTTP-304 with $creation_ts
13 // returns TRUE on cache-hit, FALSE otherwise. Calling side should call exit if the result is TRUE.
14 function checkCachedResponse ($creation_ts, $expire)
15 {
16 $client_time = HTTPDateToUnixTime (@$_SERVER['HTTP_IF_MODIFIED_SINCE']);
17 $server_time = time();
18 $result =
19 (
20 $client_time !== FALSE && $client_time !== -1 && // IMS header is readable
21 ! in_array ('no-cache', preg_split ('/\s*,\s*/', @$_SERVER['HTTP_CACHE_CONTROL'])) && // no-cache parameter unset
22 $client_time <= $server_time && // not in future
23 $client_time >= $creation_ts && // not modified since
24 (! $expire || $client_time + $expire >= $server_time) // expiration timeout is not set, or not expired
25 );
26 $last_modified = $creation_ts > 0 ? $creation_ts : ($client_time > 0 ? $client_time : $server_time);
27
28 header ("Cache-Control: private, max-age=$expire, pre-check=$expire");
29 if ($result)
30 header ('Last-Modified: ' . gmdate (DATE_RFC1123, $last_modified), TRUE, 304);
31 else
32 header ('Last-Modified: ' . gmdate (DATE_RFC1123, $last_modified));
33 return $result;
34 }
35
36 function HTTPDateToUnixTime ($string)
37 {
38 //Written per RFC 2616 3.3.1 - Full Date
39 //http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html
40 $month_number = array
41 (
42 'Jan' => 1,
43 'Feb' => 2,
44 'Mar' => 3,
45 'Apr' => 4,
46 'May' => 5,
47 'Jun' => 6,
48 'Jul' => 7,
49 'Aug' => 8,
50 'Sep' => 9,
51 'Oct' => 10,
52 'Nov' => 11,
53 'Dec' => 12,
54 );
55
56 $formats = array();
57 # RFC2616 dictates exchanged timestamps to be in GMT TZ, and RFC822
58 # (which RFC1123 relies on) explicitly defines, that "GMT" is equivalent
59 # to "-0000" and "+0000".
60 $formats['rfc1123'] = '/^(Sun|Mon|Tue|Wed|Thu|Fri|Sat), (\d{2}) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) (\d{4}) (\d{2}):(\d{2}):(\d{2}) (?:GMT|[-+]0000)$/';
61 $formats['rfc850'] = '/^(Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday), (\d{2})-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-(\d{2}) (\d{2}):(\d{2}):(\d{2}) (?:GMT|[-+]0000)$/';
62 $formats['asctime'] = '/^(Sun|Mon|Tue|Wed|Thu|Fri|Sat) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) (\d{2}|\d{1}) (\d{2}):(\d{2}):(\d{2}) (\d{4})$/';
63
64 $matches = array();
65 if (preg_match ($formats['rfc1123'], $string, $matches)) {
66 $hours = $matches[5];
67 $minutes = $matches[6];
68 $seconds = $matches[7];
69 $month = $month_number[$matches[3]];
70 $day = $matches[2];
71 $year = $matches[4];
72 } elseif (preg_match ($formats['rfc850'], $string, $matches)) {
73 $hours = $matches[5];
74 $minutes = $matches[6];
75 $seconds = $matches[7];
76 $month = $month_number[substr($matches[3],0,3)];
77 $day = $matches[2];
78 $year = $matches[4];
79 } elseif (preg_match ($formats['asctime'], $string, $matches)) {
80 $hours = $matches[4];
81 $minutes = $matches[5];
82 $seconds = $matches[6];
83 $month = $month_number[$matches[2]];
84 $day = $matches[3];
85 $year = $matches[7];
86 } else
87 return false;
88 return gmmktime ($hours, $minutes, $seconds, $month, $day, $year);
89 }
90
91 ?>