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         $module = (empty($module)) ? $ff->project : $module;
137         
138         switch($type) {
139             case 'js':
140             case 'css':
141                 $compile_dir .= implode("-", array(
142                     $ui['name'],
143                     $module,
144                     $ff->version,
145                     "{$type}compile"
146                 ));
147                 break;
148             // template config?
149             default:
150                 return false;
151         }
152         
153         if (file_exists($compile_dir)) {
154             return $compile_dir;
155         }
156         
157         if(!$is_mkdir) {
158             return false;
159         }
160         
161         if(mkdir($compile_dir, 0700, true)) {
162             return $compile_dir;
163         }
164         
165         return false;
166     }
167     
168     function clearCompiledFilesCache()
169     {
170         $au = $this->getAuthUser();
171         if (!$au && !in_array($_SERVER['REMOTE_ADDR'] , array('127.0.0.1','::1'))) {
172             $this->jerr("Cache can only be cleared by authenticated users");
173         }
174         
175         require_once 'System.php';
176         
177         $mods = $this->modulesList();
178         foreach ($mods as $module) {
179             $compile_dir = $this->getCompileDir('js', $module, false);
180         
181             if(!empty($compile_dir)) {
182                 System::rm(array('-r', $compiledir));
183             }
184             $compile_dir = $this->getCompileDir('css', $module, false);
185         
186             if(!empty($compile_dir)) {
187                 System::rm(array('-r', $compiledir));
188             }
189         }
190          
191         $this->jok('DONE');
192     }
193 }