forked from ojarva/status.futurice.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
staticserve.php
54 lines (45 loc) · 1.49 KB
/
staticserve.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
<?php
// This file serves static files and handles updating statistics.
// Static files are cached using html5 application cache (whenever it's supported).
// Many browsers don't update application cache correctly, if http cache headers
// are set.
require_once("lib/redis.php");
require_once("lib/userstats.php");
if (!isset($_GET["filename"])) {
// No filename set
// Update statistics
$redis->incr("stats:web:static:invalid");
$redis->incr("stats:web:invalid");
stat_update("web:static:invalid");
stat_update("web:invalid");
header("HTTP/1.1 404 Not Found");
readfile("404.html");
exit();
}
$filename = $_GET["filename"];
$pathinfo = pathinfo($filename);
$dir = $pathinfo["dirname"];
if (!in_array($dir, array("css", "js", "img")) || !file_exists($filename)) {
error_log("staticserve.php: Invalid directory or file ($filename) doesn't exist");
$redis->incr("stats:web:static:invalid");
$redis->incr("stats:web:invalid");
stat_update("web:static:invalid");
stat_update("web:invalid");
header("HTTP/1.1 404 Not Found");
readfile("404.html");
exit();
}
if ($dir == "css") { $ct = "text/css"; }
elseif ($dir == "js") { $ct = "application/javascript"; }
elseif ($dir == "img") {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$ct = finfo_file($finfo, $filename);
} else {
$ct = "text/plain";
}
$redis->incr("stats:web:static:served");
stat_update("web:static:served");
http_cache_etag();
http_send_content_type($ct);
http_send_file($filename);
?>