-
Notifications
You must be signed in to change notification settings - Fork 2
/
rc.php
110 lines (93 loc) · 2.89 KB
/
rc.php
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
<?php
/**
* GZIP-compressed resource output and cache control utility
* Used by static resource consolidation and cache
*
* @package Cotonti
* @link http://www.julienlecomte.net/blog/2007/08/13/
* @license https://github.com/Cotonti/Cotonti/blob/master/License.txt
*/
define('COT_CODE', true);
// Required for PHP 5.3
require_once './datas/config.php';
date_default_timezone_set('GMT');
/*
* Get the path of the target file.
*/
$rc_file = $_GET['rc'];
if (isset($rc_file) && is_string($rc_file) && preg_match('#^[\w\.\-]+\.(js|css)$#', $rc_file, $mt))
{
$src_uri = $cfg['cache_dir'] . '/static/' . $rc_file;
$content_type = $mt[1] == 'js' ? 'text/javascript' : 'text/css';
}
else
{
$protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
header($protocol . ' 400 Bad Request');
echo '<html><body><h1>HTTP 400 - Bad Request</h1></body></html>';
exit;
}
/*
* Verify the existence of the target file.
* Return HTTP 404 if needed.
*/
if (!file_exists($src_uri))
{
$protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
header($protocol . ' 404 Not Found');
echo '<html><body><h1>HTTP 404 - Not Found</h1></body></html>';
exit;
}
/*
* Set the HTTP response headers that will
* tell the client to cache the resource.
*/
$file_last_modified = filemtime($src_uri);
header('Last-Modified: '.date('r', $file_last_modified));
$max_age = 5 * 365 * 24 * 60 * 60; // ~5 years
$expires = $file_last_modified + $max_age;
header('Expires: '.date('r', $expires));
$etag = md5(realpath($src_uri) . filesize($src_uri) . filemtime($src_uri));
header('ETag: ' . $etag);
$cache_control = 'must-revalidate, proxy-revalidate, max-age='.$max_age.', s-maxage='.$max_age;
header('Cache-Control: '.$cache_control);
header('Vary: Accept-Encoding');
/*
* Check if the client should use the cached version.
* Return HTTP 304 if needed.
*/
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']))
{
// convert to unix timestamp
$if_modified_since = strtotime(preg_replace('#;.*$#', '', stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE'])));
}
else
{
$if_modified_since = false;
}
if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) == $etag
&& $if_modified_since >= $file_last_modified)
{
$protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
header($protocol . ' 304 Not Modified');
exit;
}
/*
* Cotonti Static Resources Cache
*/
header('Content-Type: '.$content_type);
readfile($src_uri);
// Gzip compression of CSS and JS files is usually enabled in webserver configuration.
// if (@strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') === FALSE)
// {
// readfile($src_uri);
// }
// else
// {
// header('Content-Encoding: gzip');
// if (!file_exists($src_uri . '.gz'))
// {
// file_put_contents($src_uri . '.gz', gzencode(file_get_contents($src_uri)));
// }
// readfile($src_uri . '.gz');
// }