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',
37 function get($s='', $opts = Array())
40 !empty($_REQUEST['_clear_cache']) &&
41 !empty($_REQUEST['_clear_module'])
43 $this->clearCompiledFilesCache($_REQUEST['_clear_module']);
47 $this->sessionState(0);
49 $bits = explode('/', $s);
51 if (empty($bits[0]) || empty($bits[1]) || !isset($this->types[$bits[0]])) {
52 $this->jerr("invalid url");
55 $s = str_replace('/', '-', $bits[1]);
57 $ui = posix_getpwuid(posix_geteuid());
58 $ff = HTML_FlexyFramework::get();
60 $compile = session_save_path() . '/' .
61 $ui['name'] . '-' . $ff->project . '-' . $ff->version . '-'. $bits[0] . 'compile';
63 $fn = $compile . '/'. $s .'.'. $bits[0];
65 if (!file_exists($fn)) {
66 header('Content-Type: '. $this->types[$bits[0]]);
68 echo "// compiled file not found = $fn";
72 $supportsGzip = !empty($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos( $_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip' ) !== false;
75 $last_modified_time = filemtime($fn);
80 isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
81 strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time
85 isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
86 trim($_SERVER['HTTP_IF_NONE_MATCH']) == md5($fn)
88 header("HTTP/1.1 304 Not Modified");
92 header('Content-Type: '. $this->types[$bits[0]]);
95 header("Pragma: public");
97 header('Cache-Control: max-age=2592000, public');
98 header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 2592000));
99 header('Last-Modified: '.gmdate('D, d M Y H:i:s \G\M\T', $last_modified_time));
100 header('Etag: '. md5($fn));
102 if ( $supportsGzip ) {
103 $content = gzencode( file_get_contents($fn) , 9);
105 header('Content-Encoding: gzip');
106 header('Vary: Accept-Encoding');
107 header('Content-Length: '. strlen($content));
114 $fh = fopen($fn,'r');
126 function post($s='') {
130 static function getCompileDir($type, $module = '', $is_mkdir = true)
132 $ff = HTML_FlexyFramework::get();
134 $ui = posix_getpwuid(posix_geteuid());
136 $compile_dir = session_save_path() . "/";
138 $module = (empty($module)) ? $ff->project : $module;
143 $compile_dir .= implode("-", array(
155 if (file_exists($compile_dir)) {
163 if(mkdir($compile_dir, 0700, true)) {
170 function clearCompiledFilesCache($module)
173 $mods = $this->modulesList();
175 $url = "http://localhost{$this->local_base_url}/Core/Asset";
177 foreach ($mods as $mod) {
178 $compile_dir = $this->getCompileDir('js', $module, false);
180 if(empty($compile_dir)) {
184 foreach(glob($compile_dir.'/*.*') as $v){