-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgzhandler.php
73 lines (59 loc) · 1.5 KB
/
gzhandler.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
<?php
// show 404 if anything goes wrong
function show404 () {
header('HTTP/1.1 303 See Other');
header('Location: /404.html');
exit();
}
// ========================================
// get variables ready
$root = dirname(__FILE__);
$request = $_SERVER['REQUEST_URI'];
$path = parse_url($request, PHP_URL_PATH);
// check path
if ($path === false) {
show404();
}
// strip reverse dots and add root to path
$path = $root.str_replace('..', '', $path);
// change to board index if first page
if (substr($path, -1) == '/') {
$path .= 'board.html';
}
// if file doesn't exists or not the right file type
if (!file_exists($path) || !preg_match('/\.(html|css|js)$/', $path, $match)) {
show404();
}
// get type and file mod time
$type = $match[1];
$filetime = filemtime($path);
// not modifed
if (
isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $filetime
) {
header('HTTP/1.0 304 Not Modified');
exit();
}
// start gzhandler
if (!ob_start('ob_gzhandler')) {
ob_start();
}
header('Vary: Accept-Encoding');
// set content type
if ($type == 'js') {
header('Content-Type: text/javascript; charset=utf-8');
}
else {
header('Content-Type: text/'.$type.'; charset=utf-8');
}
// expiry headers
header_remove('ETag');
if ($type != 'html') {
header('Cache-Control: public, max-age=604800');
header('Expires: '.gmdate('D, d M Y H:i:s', time() + 604800).' GMT');
}
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $filetime).' GMT');
// read and exit
readfile($path);
ob_end_flush();