-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassets.php
104 lines (82 loc) · 3.91 KB
/
assets.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
<?php
/*
-------------------------------------------------------------------------------
Qumulus - Personal cloud software
This file is part of Qumulus.
Qumulus is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Qumulus is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Qumulus. If not, see <http://www.gnu.org/licenses/>.
Authors:
Florian BETIL : [email protected]
-------------------------------------------------------------------------------
*/
define('DS', DIRECTORY_SEPARATOR);
define('ROOT_PATH', pathinfo($_SERVER["SCRIPT_FILENAME"])['dirname']);
class Assets {
static function Render($type, $sources = null){
if (!$sources) die();
if (!in_array($type, array('javascript', 'css'))) die();
$files = array();
$modified = 0;
$root = pathinfo($_SERVER["SCRIPT_FILENAME"])['dirname'].DS;
foreach(explode(',', $sources) as $file){
if (!file_exists($root.$file) || (strpos(ROOT_PATH.DS.'assets'.DS, realpath($root.$file))!==false) || !in_array(pathinfo($root.$file, PATHINFO_EXTENSION), array('js', 'css'))) continue;
$files[] = $root.$file;
$age = filemtime($root.$file);
if($age > $modified) {
$modified = $age;
}
}
$offset = 60 * 60 * 24 * 7;
header ('Expires: ' . gmdate ("D, d M Y H:i:s", time() + $offset) . ' GMT');
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $modified) {
header("HTTP/1.0 304 Not Modified");
header ('Cache-Control:');
} else {
header ('Cache-Control: max-age=' . $offset);
header ('Content-type: text/'.$type.'; charset=UTF-8');
header ('Pragma:');
header ("Last-Modified: ".gmdate("D, d M Y H:i:s", $modified )." GMT");
ob_start('ob_gzhandler');
foreach($files as $file) {
if(strpos(basename($file),'.min.')===false) {
ob_start(array('Assets','Minify'.ucfirst($type)));
include($file);
ob_end_flush();
} else {
include($file);
}
}
ob_end_flush();
}
}
static function MinifyJavascript($buffer) {
/* remove comments */
$buffer = preg_replace("/((?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:\/\/.*))/", "", $buffer);
/* remove tabs, spaces, newlines, etc. */
$buffer = str_replace(array("\r\n","\r","\t","\n",' ',' ',' '), '', $buffer);
/* remove other spaces before/after ) */
$buffer = preg_replace(array('(( )+\))','(\)( )+)'), ')', $buffer);
return $buffer;
}
static function MinifyCss($buffer) {
/* remove comments */
$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
/* remove tabs, spaces, newlines, etc. */
$buffer = str_replace(array("\r\n","\r","\n","\t",' ',' ',' '), '', $buffer);
/* remove other spaces before/after ; */
$buffer = preg_replace(array('(( )+{)','({( )+)'), '{', $buffer);
$buffer = preg_replace(array('(( )+})','(}( )+)','(;( )*})'), '}', $buffer);
$buffer = preg_replace(array('(;( )+)','(( )+;)'), ';', $buffer);
return $buffer;
}
}
if (isset($_GET['js'])) Assets::Render('javascript', $_GET['js']);
if (isset($_GET['css'])) Assets::Render('css', $_GET['css']);