Asset.php
[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(!empty($_REQUEST['_clear_cache'])) {
40             $this->clearCompiledFilesCache();
41         }
42         
43         
44         $this->sessionState(0);
45         
46         $bits = explode('/', $s);
47         
48         if (empty($bits[0]) || empty($bits[1])  || !isset($this->types[$bits[0]])) {
49             $this->jerr("invalid url");
50         }
51        
52         $s = str_replace('/', '-', $bits[1]);
53         
54         $ui = posix_getpwuid(posix_geteuid());
55         $ff = HTML_FlexyFramework::get();
56         
57         $compile = session_save_path() . '/' .
58                 $ui['name'] . '-' . $ff->project . '-' . $ff->version .  '-'. $bits[0] . 'compile';
59      
60         $fn = $compile . '/'. $s .'.'. $bits[0];
61         
62         if (!file_exists($fn)) {
63             header('Content-Type: '. $this->types[$bits[0]]);
64         
65             echo "// compiled file not found = $fn";
66             exit;
67         }
68         
69         $supportsGzip = !empty($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos( $_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip' ) !== false;
70
71         
72         $last_modified_time = filemtime($fn);
73         
74         
75         if (
76             (
77                 isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
78                 strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time
79             )
80             ||
81             (
82                  isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
83                 trim($_SERVER['HTTP_IF_NONE_MATCH']) == md5($fn)
84             )) { 
85             header("HTTP/1.1 304 Not Modified");
86             exit;
87         }
88         
89         header('Content-Type: '. $this->types[$bits[0]]);
90         
91         
92         header("Pragma: public");
93         
94         header('Cache-Control: max-age=2592000, public');
95         header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 2592000));
96         header('Last-Modified: '.gmdate('D, d M Y H:i:s \G\M\T', $last_modified_time));
97         header('Etag: '. md5($fn)); 
98         
99          if ( $supportsGzip ) {
100             $content = gzencode( file_get_contents($fn) , 9);
101             
102             header('Content-Encoding: gzip');
103             header('Vary: Accept-Encoding');
104             header('Content-Length: '. strlen($content));
105             
106             echo $content;
107             
108         } else {
109             
110             
111             $fh = fopen($fn,'r');
112             fpassthru($fh);
113             fclose($fh);
114             $content = $data;
115         }
116         
117         
118         
119         exit;
120         
121     }
122     
123     function post($s='') {
124         die('invalid');
125     }
126      
127     static function getCompileDir($type, $module = '', $is_mkdir = true)
128     {
129         $ff = HTML_FlexyFramework::get();
130         
131         $ui = posix_getpwuid(posix_geteuid());
132         
133         $compile_dir = session_save_path() . "/";
134         
135         $module = (empty($module)) ? $ff->project : $module;
136         
137         switch($type) {
138             case 'js':
139             case 'css':
140                 $compile_dir .= implode("-", array(
141                     $ui['name'],
142                     $module,
143                     $ff->version,
144                     "{$type}compile"
145                 ));
146                 break;
147             // template config?
148             default:
149                 return false;
150         }
151         
152         if (file_exists($compile_dir)) {
153             return $compile_dir;
154         }
155         
156         if(!$is_mkdir) {
157             return false;
158         }
159         
160         if(mkdir($compile_dir, 0700, true)) {
161             return $compile_dir;
162         }
163         
164         return false;
165     }
166     
167     function clearCompiledFilesCache()
168     {
169         require_once 'System.php';
170         $mods = $this->modulesList();
171         
172         foreach ($mods as $mod) {
173             $compile_dir = $this->getCompileDir('js', $module, false);
174         
175             if(!empty($compile_dir)) {
176                 System::rm(array('-r', $compiledir));
177             }
178             $compile_dir = $this->getCompileDir('css', $module, false);
179         
180             if(!empty($compile_dir)) {
181                 System::rm(array('-r', $compiledir));
182             }
183         }
184          
185         $this->jok('DONE');
186     }
187 }