-
Notifications
You must be signed in to change notification settings - Fork 0
/
cacheable.php
81 lines (74 loc) · 2.78 KB
/
cacheable.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
<?php
// cacheable.php -- HotCRP cacheability helper
// HotCRP is Copyright (c) 2006-2016 Eddie Kohler and Regents of the UC
// Distributed under an MIT-like license; see LICENSE
session_cache_limiter("");
header("Cache-Control: max-age=315576000, public");
header("Expires: " . gmdate("D, d M Y H:i:s", time() + 315576000) . " GMT");
// *** NB This file does not include all of the HotCRP infrastructure! ***
$zlib_output_compression = false;
if (function_exists("zlib_get_coding_type"))
$zlib_output_compression = zlib_get_coding_type();
if ($zlib_output_compression) {
header("Content-Encoding: $zlib_output_compression");
header("Vary: Accept-Encoding", false);
}
function fail() {
global $zlib_output_compression;
header("Content-Type: text/plain; charset=utf-8");
if (!$zlib_output_compression)
header("Content-Length: 10");
echo "Go away.\r\n";
exit;
}
$file = isset($_GET["file"]) ? $_GET["file"] : null;
if (!$file)
fail();
$mtime = @filemtime($file);
$prefix = "";
if (preg_match(',\A(?:images|scripts|stylesheets)(?:/[^./][^/]+)+\z,', $file)
&& preg_match(',.*([.][a-z]*)\z,', $file, $m)) {
$s = $m[1];
if ($s === ".js") {
header("Content-Type: text/javascript; charset=utf-8");
if (isset($_GET["strictjs"]) && $_GET["strictjs"])
$prefix = "\"use strict\";\n";
} else if ($s === ".map")
header("Content-Type: application/json; charset=utf-8");
else if ($s === ".css")
header("Content-Type: text/css; charset=utf-8");
else if ($s === ".gif")
header("Content-Type: image/gif");
else if ($s === ".jpg")
header("Content-Type: image/jpeg");
else if ($s === ".png")
header("Content-Type: image/png");
else if ($s === ".mp3")
header("Content-Type: audio/mpeg");
else
fail();
header("Access-Control-Allow-Origin: *");
} else
fail();
$last_modified = gmdate("D, d M Y H:i:s", $mtime) . " GMT";
$etag = '"' . md5($last_modified) . '"';
header("Last-Modified: $last_modified");
header("ETag: $etag");
// check for a conditional request
$if_modified_since = isset($_SERVER["HTTP_IF_MODIFIED_SINCE"]) ? $_SERVER["HTTP_IF_MODIFIED_SINCE"] : 0;
$if_none_match = isset($_SERVER["HTTP_IF_NONE_MATCH"]) ? $_SERVER["HTTP_IF_NONE_MATCH"] : 0;
if (($if_modified_since || $if_none_match)
&& (!$if_modified_since || $if_modified_since === $last_modified)
&& (!$if_none_match || $if_none_match === $etag))
header("HTTP/1.0 304 Not Modified");
else if (function_exists("ob_gzhandler") && !$zlib_output_compression) {
ob_start("ob_gzhandler");
echo $prefix;
readfile($file);
ob_end_flush();
} else {
if (!$zlib_output_compression)
header("Content-Length: " . (filesize($file) + strlen($prefix)));
echo $prefix;
readfile($file);
}