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