-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjcss.php
More file actions
102 lines (86 loc) · 3.02 KB
/
jcss.php
File metadata and controls
102 lines (86 loc) · 3.02 KB
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
<?php
/*******************************************************************
* JCSS, PHP Javascript and CSS file cache and compression control.
* Copyright, Turab Garip
* turabgarip@gmail.com
* GNU GPL v3.0
*******************************************************************/
include_once './config.php';
// This file can be cached yes.
header('Cache-Control: public');
if (!isset($_GET['t']) || !in_array($_GET['t'], array('c', 'j')) || !isset($_GET['f']))
exit; // Nothing to serve here.
$type = $_GET['t'] == 'c' ? 'css' : 'js';
if (!is_file($env['cache'] . '/' . $type . '-' . $_GET['f']))
exit; // Again nothing to serve
$files = explode("\n", str_replace(array('{base}', '{template}'), array($env['path'], $env['template']),
file_get_contents($env['cache'] . '/' . $type . '-' . $_GET['f']))
);
// Find the newest modified file
$lastmodified = 0;
foreach ($files as $file)
$lastmodified = max($lastmodified, filemtime($file));
// Send Etag hash and last modified header for these specific files
$hash = $lastmodified . '-' . $_GET['f'];
header("Etag: $hash");
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $lastmodified) . ' GMT');
if ((isset($_SERVER['HTTP_IF_NONE_MATCH']) && stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) == $hash) ||
(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $lastmodified))
{
// Tell the browser nothing has changed
header('HTTP/1.0 304 Not Modified');
header('Content-Length: 0');
exit;
}
else
{
$encoding = 'none';
if ($env['compress_jcss'] && isset($_SERVER['HTTP_ACCEPT_ENCODING']))
{
// Which method to compress with
if (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false)
$encoding = 'gzip';
elseif (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'deflate') !== false)
$encoding = 'deflate';
}
if ($encoding != 'none')
header ("Content-Encoding: $encoding");
header("Content-Type: text/$type");
// First time visit or files were modified
if ($env['cache_jcss'])
{
// Try the cache first to see if the combined files were already generated
$cachefile = $env['cache'] . '/' . $hash . ($encoding != 'none' ? '.' . $encoding : '') . '.' . $type;
if (is_file($cachefile))
{
if ($fp = fopen($cachefile, 'rb'))
{
header('Content-Length: ' . filesize($cachefile));
fpassthru($fp);
fclose($fp);
exit;
}
}
}
// Not cached?
$contents = '';
reset($files);
foreach ($files as $file)
$contents .= '/*' . basename($file) . '*/' . PHP_EOL . file_get_contents($file) . PHP_EOL;
// Send compressed or plain
if ($encoding != 'none')
$contents = gzencode($contents, 9, $encoding == 'gzip' ? FORCE_GZIP : FORCE_DEFLATE);
header('Content-Length: ' . strlen($contents));
echo $contents;
// Store cached content
if ($env['cache_jcss']) {
// Purge the old cache of this exact set first
$old_cache = glob($env['cache'] . '/*-' . $_GET['f'] . '*' . $type);
if (!empty($old_cache))
array_map('unlink', $old_cache);
// Then save the new one
$fp = fopen($cachefile, 'wb');
fwrite($fp, $contents);
fclose($fp);
}
}