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