5 Jappix - An open social platform
6 This is the file get script
8 -------------------------------------------------
12 Last revision: 03/12/11
17 define('JAPPIX_BASE', '..');
19 // We get the needed files
20 require_once('./functions.php');
21 require_once('./functions-get.php');
22 require_once('./read-main.php');
23 require_once('./read-hosts.php');
28 // Get some parameters
29 $is_developer = isDeveloper();
30 $has_compression = hasCompression();
33 header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
34 header('Cache-Control: no-store, no-cache, must-revalidate');
35 header('Cache-Control: post-check=0, pre-check=0', false);
36 header('Pragma: no-cache');
39 // Else, we put a far away cache date (1 year)
42 header('Pragma: public');
43 header('Cache-Control: maxage='.$expires);
44 header('Expires: '.gmdate('D, d M Y H:i:s', (time() + $expires)).' GMT');
47 // Initialize the vars
52 if(isset($_GET['t']) && !empty($_GET['t']) && preg_match('/^(css|js|img|snd|store)$/', $_GET['t']))
56 if(isset($_GET['f']) && !empty($_GET['f']) && isSafe($_GET['f']))
59 // Read the group var (only for text files)
60 if(isset($_GET['g']) && !empty($_GET['g']) && preg_match('/^(\S+)\.xml$/', $_GET['g']) && preg_match('/^(css|js)$/', $type) && isSafe($_GET['g']) && file_exists('../xml/'.$_GET['g'])) {
61 $xml_data = file_get_contents('../xml/'.$_GET['g']);
65 $xml_read = new SimpleXMLElement($xml_data);
66 $xml_parse = $xml_read->$type;
68 // Files were added to the list before (with file var)?
70 $file .= '~'.$xml_parse;
76 // We check if the data was submitted
78 // We define some stuffs
79 $dir = '../'.$type.'/';
82 // Define the real type if this is a "store" file
83 if($type == 'store') {
84 // Extract the file extension
85 switch(getFileExt($file)) {
117 // JS and CSS special stuffs
118 if(($type == 'css') || ($type == 'js')) {
121 $cache_encoding = 'deflate';
123 $cache_encoding = 'plain';
126 $version = getVersion();
127 $hash = genHash($version);
128 $cache_hash = md5($path.$hash.staticLocation()).'_'.$cache_encoding;
130 // Check if the browser supports DEFLATE
131 $deflate_support = false;
133 if(isset($_SERVER['HTTP_ACCEPT_ENCODING']) && substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'deflate') && hasCompression() && !$is_developer)
134 $deflate_support = true;
136 // Internationalization
138 if(isset($_GET['l']) && !empty($_GET['l']) && !preg_match('/\.\.\//', $_GET['l']) && is_dir('../lang/'.$_GET['l']))
139 $locale = $_GET['l'];
147 // Define the cache lang name
149 $cache_lang = $cache_hash.'_'.$locale;
151 $cache_lang = $cache_hash;
154 // Explode the file string
155 if(strpos($file, '~') != false)
156 $array = explode('~', $file);
158 $array = array($file);
160 // Define the reading vars
164 // Check the cache exists for text files (avoid the heavy file_exists loop!)
165 if(!$is_developer && (($type == 'css') || ($type == 'js')) && hasCache($cache_lang))
168 // Check if the all the file(s) exist(s)
170 foreach($array as $current) {
171 // Stop the loop if a file is missing
172 if(!file_exists($dir.$current)) {
180 // We can read the file(s)
182 // We get the file MIME type
183 $mime = strtolower(preg_replace('/(^)(.+)(\.)(.+)($)/i', '$4', $file));
185 // We set up a known MIME type (and some other headers)
186 if(($type == 'css') || ($type == 'js')) {
189 header('Content-Encoding: deflate');
193 header('Content-Type: text/css; charset=utf-8');
194 else if($type == 'js')
195 header('Content-Type: application/javascript; charset=utf-8');
198 else if($mime == 'png')
199 header('Content-Type: image/png');
200 else if($mime == 'gif')
201 header('Content-Type: image/gif');
202 else if(($mime == 'jpg') || ($mime == 'jpeg'))
203 header('Content-Type: image/jpeg');
204 else if($mime == 'bmp')
205 header('Content-Type: image/bmp');
206 else if(($mime == 'oga') || ($mime == 'ogg'))
207 header('Content-Type: audio/ogg');
209 // Catch the file MIME type
211 header('Content-Type: '.getFileMIME($path));
213 // Read the text file(s) (CSS & JS)
214 if(($type == 'css') || ($type == 'js')) {
215 // If there's a cache file, read it
216 if(hasCache($cache_lang) && !$is_developer) {
217 $cache_read = readCache($cache_lang);
219 if($deflate_support || !$has_compression)
222 echo gzinflate($cache_read);
225 // Else, we generate the cache
227 // First try to read the cache reference
228 if(hasCache($cache_hash) && !$is_developer) {
229 // Read the reference
230 $cache_reference = readCache($cache_hash);
232 // Filter the cache reference
234 $output = gzinflate($cache_reference);
236 $output = $cache_reference;
239 // No cache reference, we should generate it
241 // Initialize the loop
244 // Add the content of the current file
245 foreach($array as $current)
246 $looped .= rmBOM(file_get_contents($dir.$current))."\n";
250 // Apply the CSS logos
251 $looped = setLogos($looped, $array);
253 // Apply the CSS background
254 $looped = setBackground($looped);
256 // Set the Get API paths
257 $looped = setPath($looped, $hash, HOST_STATIC, $type, '');
260 // Optimize the code rendering
262 // Can minify the CSS
263 if($has_compression && !$is_developer)
264 $output = compressCSS($looped);
270 // Can minify the JS (sloooooow!)
271 if($has_compression && !$is_developer) {
272 require_once('./jsmin.php');
273 $output = JSMin::minify($looped);
280 // Generate the reference cache
282 $final = gzdeflate($output, 9);
287 genCache($final, $is_developer, $cache_hash);
292 // Set the JS locales
293 $output = setLocales($output, $locale);
295 // Set the JS configuration
296 $output = setConfiguration($output, $locale, $version, uploadMaxSize());
298 // Set the Get API paths
299 $output = setPath($output, $hash, HOST_STATIC, $type, $locale);
301 // Translate the JS script
302 require_once('./gettext.php');
303 includeTranslation($locale, 'main');
304 $output = setTranslation($output);
306 // Generate the cache
308 $final = gzdeflate($output, 9);
313 genCache($final, $is_developer, $cache_lang);
316 // Output a well-encoded string
317 if($deflate_support || !$has_compression)
320 echo gzinflate($final);
324 // Read the binary file (PNG, OGA and others)
331 // The file was not found
332 header('Status: 404 Not Found', true, 404);
333 exit('HTTP/1.1 404 Not Found');
336 // The request is not correct
337 header('Status: 400 Bad Request', true, 400);
338 exit('HTTP/1.1 400 Bad Request');