Merge branch 'package'
[speedfreak] / Server / system / helpers / expires.php
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          * Sets the amount of time before a page expires
16          *
17          * @param  integer Seconds before the page expires 
18          * @return boolean
19          */
20         public static function set($seconds = 60)
21         {
22                 if (expires::check_headers())
23                 {
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  bool     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                 {
50                         if (($strpos = strpos($_SERVER['HTTP_IF_MODIFIED_SINCE'], ';')) !== FALSE)
51                         {
52                                 // IE6 and perhaps other IE versions send length too, compensate here
53                                 $mod_time = substr($_SERVER['HTTP_IF_MODIFIED_SINCE'], 0, $strpos);
54                         }
55                         else
56                         {
57                                 $mod_time = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
58                         }
59
60                         $mod_time = strtotime($mod_time);
61                         $mod_time_diff = $mod_time + $seconds - time();
62
63                         if ($mod_time_diff > 0)
64                         {
65                                 // Re-send headers
66                                 header('Last-Modified: '.gmdate('D, d M Y H:i:s T', $mod_time));
67                                 header('Expires: '.gmdate('D, d M Y H:i:s T', time() + $mod_time_diff));
68                                 header('Cache-Control: max-age='.$mod_time_diff);
69                                 header('Status: 304 Not Modified', TRUE, 304);
70
71                                 // Prevent any output
72                                 Event::add('system.display', array('expires', 'prevent_output'));
73
74                                 // Exit to prevent other output
75                                 exit;
76                         }
77                 }
78
79                 return FALSE;
80         }
81
82         /**
83          * Check headers already created to not step on download or Img_lib's feet
84          *
85          * @return boolean
86          */
87         public static function check_headers()
88         {
89                 foreach (headers_list() as $header)
90                 {
91                         if ((session_cache_limiter() == '' AND stripos($header, 'Last-Modified:') === 0)
92                             OR stripos($header, 'Expires:') === 0)
93                         {
94                                 return FALSE;
95                         }
96                 }
97
98                 return TRUE;
99         }
100
101         /**
102          * Prevent any output from being displayed. Executed during system.display.
103          *
104          * @return  void
105          */
106         public static function prevent_output()
107         {
108                 Kohana::$output = '';
109         }
110
111 } // End expires