4 * Generic cached assset server... -- No security om this.. should only return compressed CSS/JS
6 * Does a few tricks with headers to improve caching...
9 * Also includes code to generate assets...
11 * methods outputJavascriptDir / outputCssDir generate links to
12 * BASEURL/Asset/css/xxxx.yyy.zzz
13 * BASEURL/Asset/js/xxxx.yyy.zzz
16 * we deliver the file from
17 * SESSION-DIR/{$www-user}-{$ff->project}-$ff->version}-{js|css}-compile/{filename}/PATH';
22 require_once 'Pman.php';
24 class Pman_Core_Asset extends Pman {
28 'js' => 'text/javascript',
29 'map' => 'application/json'
38 function get($s='', $opts = Array())
40 $this->sessionState(0);
42 $bits = explode('/', $s);
44 if (empty($bits[0]) || empty($bits[1]) || !isset($this->types[$bits[0]])) {
45 $this->jerr("invalid url");
49 if (preg_match('/\.map$/',$_SERVER['REQUEST_URI'])) {
53 $s = str_replace('/', '-', $bits[1]);
56 $ui = posix_getpwuid(posix_geteuid());
57 $ff = HTML_FlexyFramework::get();
59 $compile = self::getCompileDir($bits[0], '', false);
61 $fn = $compile . '/'. $s .'.'. $ext;
63 if (!file_exists($fn)) {
64 header('Content-Type: '. $this->types[$ext]);
66 echo "// compiled file not found = $fn";
70 $supportsGzip = !empty($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos( $_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip' ) !== false;
73 $last_modified_time = filemtime($fn);
78 isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
79 strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time
83 isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
84 trim($_SERVER['HTTP_IF_NONE_MATCH']) == md5($fn)
86 header("HTTP/1.1 304 Not Modified");
90 header('Content-Type: '. $this->types[$bits[0]]);
93 header("Pragma: public");
95 header('Cache-Control: max-age=2592000, public');
96 header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 2592000));
97 header('Last-Modified: '.gmdate('D, d M Y H:i:s \G\M\T', $last_modified_time));
98 header('Etag: '. md5($fn));
100 if ( $supportsGzip ) {
101 $content = gzencode( file_get_contents($fn) , 9);
103 header('Content-Encoding: gzip');
104 header('Vary: Accept-Encoding');
105 header('Content-Length: '. strlen($content));
112 $fh = fopen($fn,'r');
124 function post($s='') {
125 if(!empty($_REQUEST['_clear_cache'])) {
126 $this->clearCompiledFilesCache();
132 static function getCompileDir($type, $module = '', $is_mkdir = true)
134 $ff = HTML_FlexyFramework::get();
136 $ui = posix_getpwuid(posix_geteuid());
138 $compile_dir = session_save_path() . "/";
140 if (empty($module)) {
141 $module = $ff->project . (isset($ff->appNameShort) ? '_' . $ff->appNameShort : '');
149 $compile_dir .= implode("-", array(
161 if (file_exists($compile_dir)) {
169 if(mkdir($compile_dir, 0700, true)) {
176 function clearCompiledFilesCache()
178 $au = $this->getAuthUser();
179 if (!$au && !in_array($_SERVER['REMOTE_ADDR'] , array('127.0.0.1','::1'))) {
180 $this->jerr("Cache can only be cleared by authenticated users");
183 require_once 'System.php';
184 $ff = HTML_FlexyFramework::get();
186 $mods = $this->modulesList();
187 $mods[] = $ff->project; // Pman - this was the old format...
188 $mods[] = ''; // Pman + appshortname..
190 foreach ($mods as $module) {
191 $compile_dir = $this->getCompileDir('js', $module, false);
193 if(!empty($compile_dir)) {
194 System::rm(array('-r', $compile_dir));
196 $compile_dir = $this->getCompileDir('css', $module, false);
198 if(!empty($compile_dir)) {
199 System::rm(array('-r', $compile_dir));