sync
[Pman.Core] / Asset.php
1 <?php
2  
3  /**
4   * Generic cached assset server... -- No security om this.. should only return compressed CSS/JS
5   *
6   * Does a few tricks with headers to improve caching...
7   *
8   *
9   * Also includes code to generate assets...
10   *
11   * methods outputJavascriptDir / outputCssDir generate links to 
12   *    BASEURL/Asset/css/xxxx.yyy.zzz
13   *    BASEURL/Asset/js/xxxx.yyy.zzz
14   *
15   *   then
16   *   we deliver the file from
17   *       SESSION-DIR/{$www-user}-{$ff->project}-$ff->version}-{js|css}-compile/{filename}/PATH';
18   *
19   *   
20   */
21  
22 require_once 'Pman.php';
23
24 class Pman_Core_Asset extends Pman {
25      
26     var $types = array(
27         'css' => 'text/css',
28         'js' => 'text/javascript',
29     );
30     
31     function getAuth()
32     {
33         return true;
34     }
35     
36     
37     function get($s='', $opts = Array())
38     {
39         if(
40             !empty($_REQUEST['_clear_cache']) &&
41             !empty($_REQUEST['_clear_module'])
42         ) {
43             $this->clearCompiledFilesCache($_REQUEST['_clear_module']);
44         }
45         
46         
47         $this->sessionState(0);
48         
49         $bits = explode('/', $s);
50         
51         if (empty($bits[0]) || empty($bits[1])  || !isset($this->types[$bits[0]])) {
52             $this->jerr("invalid url");
53         }
54        
55         $s = str_replace('/', '-', $bits[1]);
56         
57         $ui = posix_getpwuid(posix_geteuid());
58         $ff = HTML_FlexyFramework::get();
59         
60         $compile = session_save_path() . '/' .
61                 $ui['name'] . '-' . $ff->project . '-' . $ff->version .  '-'. $bits[0] . 'compile';
62      
63         $fn = $compile . '/'. $s .'.'. $bits[0];
64         
65         if (!file_exists($fn)) {
66             header('Content-Type: '. $this->types[$bits[0]]);
67         
68             echo "// compiled file not found = $fn";
69             exit;
70         }
71         
72         $supportsGzip = !empty($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos( $_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip' ) !== false;
73
74         
75         $last_modified_time = filemtime($fn);
76         
77         
78         if (
79             (
80                 isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
81                 strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time
82             )
83             ||
84             (
85                  isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
86                 trim($_SERVER['HTTP_IF_NONE_MATCH']) == md5($fn)
87             )) { 
88             header("HTTP/1.1 304 Not Modified");
89             exit;
90         }
91         
92         header('Content-Type: '. $this->types[$bits[0]]);
93         
94         
95         header("Pragma: public");
96         
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)); 
101         
102          if ( $supportsGzip ) {
103             $content = gzencode( file_get_contents($fn) , 9);
104             
105             header('Content-Encoding: gzip');
106             header('Vary: Accept-Encoding');
107             header('Content-Length: '. strlen($content));
108             
109             echo $content;
110             
111         } else {
112             
113             
114             $fh = fopen($fn,'r');
115             fpassthru($fh);
116             fclose($fh);
117             $content = $data;
118         }
119         
120         
121         
122         exit;
123         
124     }
125     
126     function post($s='') {
127         die('invalid');
128     }
129      
130     static function getCompileDir($type, $module = '', $is_mkdir = true)
131     {
132         $ff = HTML_FlexyFramework::get();
133         
134         $ui = posix_getpwuid(posix_geteuid());
135         
136         $compile_dir = session_save_path() . "/";
137         
138         $module = (empty($module)) ? $ff->project : $module;
139         
140         switch($type) {
141             case 'js':
142             case 'css':
143                 $compile_dir .= implode("-", array(
144                     $ui['name'],
145                     $module,
146                     $ff->version,
147                     "{$type}compile"
148                 ));
149                 break;
150             // template
151             default:
152                 return false;
153         }
154         
155         if (file_exists($compile_dir)) {
156             return $compile_dir;
157         }
158         
159         if(!$is_mkdir) {
160             return false;
161         }
162         
163         if(mkdir($compile_dir, 0700, true)) {
164             return $compile_dir;
165         }
166         
167         return false;
168     }
169     
170     function clearCompiledFilesCache($module)
171     {
172         $compile_dir = $this->getCompileDir('js', $module, false);
173         
174         if(empty($compile_dir)) {
175             $this->jok('EMPTY');
176         }
177         
178         foreach(glob($compile_dir.'/*.*') as $v){
179             unlink($v);
180         }
181         
182         $this->jok('DONE');
183     }
184 }