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