expires_Core   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 92
rs 10
wmc 13
lcom 0
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 18 2
B check() 0 30 5
B check_headers() 0 11 5
A prevent_output() 0 4 1
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
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|false?

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.

Loading history...
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)
0 ignored issues
show
Coding Style introduced by
check uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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;
0 ignored issues
show
Coding Style Compatibility introduced by
The method check() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
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