Merge branch 'maint/7.0'
[ninja.git] / system / helpers / expires.php
blobbb8862ccb57ae01be739d4ead45f58872df2d4d3
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
2 /**
3 * Controls headers that effect client caching of pages
5 * $Id: expires.php 3917 2009-01-21 03:06:22Z zombor $
7 * @package Core
8 * @author Kohana Team
9 * @copyright (c) 2007-2008 Kohana Team
10 * @license http://kohanaphp.com/license.html
12 class expires {
14 /**
15 * Sets the amount of time before a page expires
17 * @param integer Seconds before the page expires
18 * @return boolean
20 public static function set($seconds = 60)
22 if (expires::check_headers())
24 $now = $expires = time();
26 // Set the expiration timestamp
27 $expires += $seconds;
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);
34 return $expires;
37 return FALSE;
40 /**
41 * Checks to see if a page should be updated or send Not Modified status
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
46 public static function check($seconds = 60)
48 if ( ! empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) AND expires::check_headers())
50 if (($strpos = strpos($_SERVER['HTTP_IF_MODIFIED_SINCE'], ';')) !== FALSE)
52 // IE6 and perhaps other IE versions send length too, compensate here
53 $mod_time = substr($_SERVER['HTTP_IF_MODIFIED_SINCE'], 0, $strpos);
55 else
57 $mod_time = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
60 $mod_time = strtotime($mod_time);
61 $mod_time_diff = $mod_time + $seconds - time();
63 if ($mod_time_diff > 0)
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);
71 // Prevent any output
72 Event::add('system.display', array('expires', 'prevent_output'));
74 // Exit to prevent other output
75 exit;
79 return FALSE;
82 /**
83 * Check headers already created to not step on download or Img_lib's feet
85 * @return boolean
87 public static function check_headers()
89 foreach (headers_list() as $header)
91 if (stripos($header, 'Last-Modified:') === 0 OR stripos($header, 'Expires:') === 0)
93 return FALSE;
97 return TRUE;
101 * Prevent any output from being displayed. Executed during system.display.
103 * @return void
105 public static function prevent_output()
107 Kohana::$output = '';
110 } // End expires