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
40         
41         $this->sessionState(0);
42         
43         $bits = explode('/', $s);
44         
45         if (empty($bits[0]) || empty($bits[1])  || !isset($this->types[$bits[0]])) {
46             $this->jerr("invalid url");
47         }
48        
49         $s = str_replace('/', '-', $bits[1]);
50         
51         $ui = posix_getpwuid(posix_geteuid());
52         $ff = HTML_FlexyFramework::get();
53         
54         $compile = self::getCompileDir($bits[0], '', false);
55         
56         $fn = $compile . '/'. $s .'.'. $bits[0];
57         
58         if (!file_exists($fn)) {
59             header('Content-Type: '. $this->types[$bits[0]]);
60         
61             echo "// compiled file not found = $fn";
62             exit;
63         }
64         
65         $supportsGzip = !empty($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos( $_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip' ) !== false;
66
67         
68         $last_modified_time = filemtime($fn);
69         
70         
71         if (
72             (
73                 isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
74                 strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time
75             )
76             ||
77             (
78                  isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
79                 trim($_SERVER['HTTP_IF_NONE_MATCH']) == md5($fn)
80             )) { 
81             header("HTTP/1.1 304 Not Modified");
82             exit;
83         }
84         
85         header('Content-Type: '. $this->types[$bits[0]]);
86         
87         
88         header("Pragma: public");
89         
90         header('Cache-Control: max-age=2592000, public');
91         header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 2592000));
92         header('Last-Modified: '.gmdate('D, d M Y H:i:s \G\M\T', $last_modified_time));
93         header('Etag: '. md5($fn)); 
94         
95          if ( $supportsGzip ) {
96             $content = gzencode( file_get_contents($fn) , 9);
97             
98             header('Content-Encoding: gzip');
99             header('Vary: Accept-Encoding');
100             header('Content-Length: '. strlen($content));
101             
102             echo $content;
103             
104         } else {
105             
106             
107             $fh = fopen($fn,'r');
108             fpassthru($fh);
109             fclose($fh);
110             $content = $data;
111         }
112         
113         
114         
115         exit;
116         
117     }
118     
119     function post($s='') {
120         if(!empty($_REQUEST['_clear_cache'])) {
121             $this->clearCompiledFilesCache();
122         }
123         
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         if (empty($module)) {
136             $module = $ff->project . (isset($ff->appNameShort) ?  '_' . $ff->appNameShort : '');
137         }
138         
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 config?
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()
171     {
172         $au = $this->getAuthUser();
173         if (!$au && !in_array($_SERVER['REMOTE_ADDR'] , array('127.0.0.1','::1'))) {
174             $this->jerr("Cache can only be cleared by authenticated users");
175         }
176         
177         require_once 'System.php';
178         $ff = HTML_FlexyFramework::get();
179         
180         $mods = $this->modulesList();
181         $mods[] = $ff->project; // Pman - this was the old format...
182         $mods[] = ''; // Pman + appshortname..
183         
184         foreach ($mods as $module) {
185             $compile_dir = $this->getCompileDir('js', $module, false);
186         
187             if(!empty($compile_dir)) {
188                 System::rm(array('-r', $compile_dir));
189             }
190             $compile_dir = $this->getCompileDir('css', $module, false);
191         
192             if(!empty($compile_dir)) {
193                 System::rm(array('-r', $compile_dir));
194             }
195         }
196          
197         $this->jok('DONE');
198     }
199 }