1
|
|
|
<?php defined('SYSPATH') or die('No direct access allowed.'); |
2
|
|
|
/** |
3
|
|
|
* Controls headers that effect client caching of pages |
4
|
|
|
* |
5
|
|
|
* $Id: expires.php 4272 2009-04-25 21:47:26Z zombor $ |
6
|
|
|
* |
7
|
|
|
* @package Core |
8
|
|
|
* @author Kohana Team |
9
|
|
|
* @copyright (c) 2007-2008 Kohana Team |
10
|
|
|
* @license http://kohanaphp.com/license.html |
11
|
|
|
*/ |
12
|
|
|
class expires_Core |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Sets the amount of time before a page expires |
17
|
|
|
* |
18
|
|
|
* @param integer Seconds before the page expires |
19
|
|
|
* @return boolean |
|
|
|
|
20
|
|
|
*/ |
21
|
|
|
public static function set($seconds = 60) |
22
|
|
|
{ |
23
|
|
|
if (expires::check_headers()) { |
24
|
|
|
$now = $expires = time(); |
25
|
|
|
|
26
|
|
|
// Set the expiration timestamp |
27
|
|
|
$expires += $seconds; |
28
|
|
|
|
29
|
|
|
// Send headers |
30
|
|
|
header('Last-Modified: '.gmdate('D, d M Y H:i:s T', $now)); |
31
|
|
|
header('Expires: '.gmdate('D, d M Y H:i:s T', $expires)); |
32
|
|
|
header('Cache-Control: max-age='.$seconds); |
33
|
|
|
|
34
|
|
|
return $expires; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return false; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Checks to see if a page should be updated or send Not Modified status |
42
|
|
|
* |
43
|
|
|
* @param integer Seconds added to the modified time received to calculate what should be sent |
44
|
|
|
* @return null|false FALSE when the request needs to be updated |
45
|
|
|
*/ |
46
|
|
|
public static function check($seconds = 60) |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
if (! empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) and expires::check_headers()) { |
49
|
|
|
if (($strpos = strpos($_SERVER['HTTP_IF_MODIFIED_SINCE'], ';')) !== false) { |
50
|
|
|
// IE6 and perhaps other IE versions send length too, compensate here |
51
|
|
|
$mod_time = substr($_SERVER['HTTP_IF_MODIFIED_SINCE'], 0, $strpos); |
52
|
|
|
} else { |
53
|
|
|
$mod_time = $_SERVER['HTTP_IF_MODIFIED_SINCE']; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$mod_time = strtotime($mod_time); |
57
|
|
|
$mod_time_diff = $mod_time + $seconds - time(); |
58
|
|
|
|
59
|
|
|
if ($mod_time_diff > 0) { |
60
|
|
|
// Re-send headers |
61
|
|
|
header('Last-Modified: '.gmdate('D, d M Y H:i:s T', $mod_time)); |
62
|
|
|
header('Expires: '.gmdate('D, d M Y H:i:s T', time() + $mod_time_diff)); |
63
|
|
|
header('Cache-Control: max-age='.$mod_time_diff); |
64
|
|
|
header('Status: 304 Not Modified', true, 304); |
65
|
|
|
|
66
|
|
|
// Prevent any output |
67
|
|
|
Event::add('system.display', array('expires', 'prevent_output')); |
68
|
|
|
|
69
|
|
|
// Exit to prevent other output |
70
|
|
|
exit; |
|
|
|
|
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return false; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Check headers already created to not step on download or Img_lib's feet |
79
|
|
|
* |
80
|
|
|
* @return boolean |
81
|
|
|
*/ |
82
|
|
|
public static function check_headers() |
83
|
|
|
{ |
84
|
|
|
foreach (headers_list() as $header) { |
85
|
|
|
if ((session_cache_limiter() == '' and stripos($header, 'Last-Modified:') === 0) |
86
|
|
|
or stripos($header, 'Expires:') === 0) { |
87
|
|
|
return false; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return true; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Prevent any output from being displayed. Executed during system.display. |
96
|
|
|
* |
97
|
|
|
* @return void |
98
|
|
|
*/ |
99
|
|
|
public static function prevent_output() |
100
|
|
|
{ |
101
|
|
|
Kohana::$output = ''; |
102
|
|
|
} |
103
|
|
|
} // End expires |
104
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.