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())
39 $this->sessionState(0);
41 $bits = explode('/', $s);
43 if (empty($bits[0]) || empty($bits[1]) || !isset($this->types[$bits[0]])) {
44 $this->jerr("invalid url");
47 $s = str_replace('/', '-', $bits[1]);
49 $ui = posix_getpwuid(posix_geteuid());
50 $ff = HTML_FlexyFramework::get();
52 $compile = self::getCompileDir($bits[0], '', false);
54 $fn = $compile . '/'. $s .'.'. $bits[0];
56 if (!file_exists($fn)) {
57 header('Content-Type: '. $this->types[$bits[0]]);
59 echo "// compiled file not found = $fn";
63 $supportsGzip = !empty($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos( $_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip' ) !== false;
66 $last_modified_time = filemtime($fn);
71 isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
72 strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time
76 isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
77 trim($_SERVER['HTTP_IF_NONE_MATCH']) == md5($fn)
79 header("HTTP/1.1 304 Not Modified");
83 header('Content-Type: '. $this->types[$bits[0]]);
86 header("Pragma: public");
88 header('Cache-Control: max-age=2592000, public');
89 header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 2592000));
90 header('Last-Modified: '.gmdate('D, d M Y H:i:s \G\M\T', $last_modified_time));
91 header('Etag: '. md5($fn));
93 if ( $supportsGzip ) {
94 $content = gzencode( file_get_contents($fn) , 9);
96 header('Content-Encoding: gzip');
97 header('Vary: Accept-Encoding');
98 header('Content-Length: '. strlen($content));
105 $fh = fopen($fn,'r');
117 function post($s='') {
118 if(!empty($_REQUEST['_clear_cache'])) {
119 $this->clearCompiledFilesCache();
125 static function getCompileDir($type, $module = '', $is_mkdir = true)
127 $ff = HTML_FlexyFramework::get();
129 $ui = posix_getpwuid(posix_geteuid());
131 $compile_dir = session_save_path() . "/";
133 if (empty($module)) {
134 $module = $ff->project . (isset($ff->appNameShort) ? '_' . $ff->appNameShort : '');
142 $compile_dir .= implode("-", array(
154 if (file_exists($compile_dir)) {
162 if(mkdir($compile_dir, 0700, true)) {
169 function clearCompiledFilesCache()
171 $au = $this->getAuthUser();
172 if (!$au && !in_array($_SERVER['REMOTE_ADDR'] , array('127.0.0.1','::1'))) {
173 $this->jerr("Cache can only be cleared by authenticated users");
176 require_once 'System.php';
177 $ff = HTML_FlexyFramework::get();
179 $mods = $this->modulesList();
180 $mods[] = $ff->project; // Pman - this was the old format...
181 $mods[] = ''; // Pman + appshortname..
183 foreach ($mods as $module) {
184 $compile_dir = $this->getCompileDir('js', $module, false);
186 if(!empty($compile_dir)) {
187 System::rm(array('-r', $compile_dir));
189 $compile_dir = $this->getCompileDir('css', $module, false);
191 if(!empty($compile_dir)) {
192 System::rm(array('-r', $compile_dir));