1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
* Controls headers that effect client caching of pages
*
* @package Kohana
* @author Kohana Team
* @copyright (c) 2007-2009 Kohana Team
* @license http://kohanaphp.com/license
*/
class expires_Core {
/**
* Sets the amount of time before content expires
*
* @param integer Seconds before the content expires
* @param integer Last modified timestamp in seconds(optional)
* @return integer Timestamp when the content expires
*/
public static function set($seconds = 60, $last_modified=null)
{
$now = time();
$expires = $now + $seconds;
if (empty($last_modified))
{
$last_modified = $now;
}
header('Last-Modified: '.gmdate('D, d M Y H:i:s T', $last_modified));
// HTTP 1.0
header('Expires: '.gmdate('D, d M Y H:i:s T', $expires));
// HTTP 1.1
header('Cache-Control: public,max-age='.$seconds);
return $expires;
}
/**
* Parses the If-Modified-Since header
*
* @return integer|boolean Timestamp or FALSE when header is lacking or malformed
*/
public static function get()
{
if ( ! empty($_SERVER['HTTP_IF_MODIFIED_SINCE']))
{
// Some versions of IE6 append "; length=####"
if (($strpos = strpos($_SERVER['HTTP_IF_MODIFIED_SINCE'], ';')) !== FALSE)
{
$mod_time = substr($_SERVER['HTTP_IF_MODIFIED_SINCE'], 0, $strpos);
}
else
{
$mod_time = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
}
return strtotime($mod_time);
}
return FALSE;
}
/**
* Checks to see if content should be updated otherwise sends Not Modified status
* and exits.
*
* @uses exit()
* @uses expires::get()
*
* @param integer Maximum age of the content in seconds
* @param integer Last modified timestamp in seconds(optional)
* @return integer|boolean Timestamp of the If-Modified-Since header or FALSE when header is lacking or malformed
*/
public static function check($seconds = 60, $modified=null)
{
if ($last_modified = expires::get())
{
$now = time();
if (empty($last_modified))
{
$last_modified = $now;
}
if ($modified <= $last_modified)
{
// Content has not expired
header($_SERVER['SERVER_PROTOCOL'].' 304 Not Modified');
header('Last-Modified: '.gmdate('D, d M Y H:i:s T', $last_modified));
$expires = $now + $seconds;
// HTTP 1.0
header('Expires: '.gmdate('D, d M Y H:i:s T', $expires));
// HTTP 1.1
header('Cache-Control: public,max-age='.$seconds);
// Clear any output
Event::add('system.display', create_function('', 'Kohana::$output = "";'));
exit;
}
}
return $last_modified;
}
/**
* Check if expiration headers are already set
*
* @return boolean
*/
public static function headers_set()
{
foreach (headers_list() as $header)
{
if (strncasecmp($header, 'Expires:', 8) === 0
OR strncasecmp($header, 'Cache-Control:', 14) === 0
OR strncasecmp($header, 'Last-Modified:', 14) === 0)
{
return TRUE;
}
}
return FALSE;
}
} // End expires
|