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