]> git.mxchange.org Git - friendica-addons.git/blob - jappixmini/jappix/php/get.php
GContact moved to src
[friendica-addons.git] / jappixmini / jappix / php / get.php
1 <?php
2
3 /*
4
5 Jappix - An open social platform
6 This is the file get script
7
8 -------------------------------------------------
9
10 License: AGPL
11 Author: Vanaryon
12 Last revision: 03/12/11
13
14 */
15
16 // PHP base
17 define('JAPPIX_BASE', '..');
18
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');
24
25 // Hide PHP errors
26 hideErrors();
27
28 // Get some parameters
29 $is_developer = isDeveloper();
30 $has_compression = hasCompression();
31
32 if($is_developer) {
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');
37 }
38
39 // Else, we put a far away cache date (1 year)
40 else {
41         $expires = 31536000;
42         header('Pragma: public');
43         header('Cache-Control: maxage='.$expires);
44         header('Expires: '.gmdate('D, d M Y H:i:s', (time() + $expires)).' GMT');
45 }
46
47 // Initialize the vars
48 $type = '';
49 $file = '';
50
51 // Read the type var
52 if(isset($_GET['t']) && !empty($_GET['t']) && preg_match('/^(css|js|img|snd|store)$/', $_GET['t']))
53         $type = $_GET['t'];
54
55 // Read the files var
56 if(isset($_GET['f']) && !empty($_GET['f']) && isSafe($_GET['f']))
57         $file = $_GET['f'];
58
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']);
62         
63         // Any data?
64         if($xml_data) {
65                 $xml_read = new SimpleXMLElement($xml_data);
66                 $xml_parse = $xml_read->$type;
67                 
68                 // Files were added to the list before (with file var)?
69                 if($file)
70                         $file .= '~'.$xml_parse;
71                 else
72                         $file = $xml_parse;
73         }
74 }
75
76 // We check if the data was submitted
77 if($file && $type) {
78         // We define some stuffs
79         $dir = '../'.$type.'/';
80         $path = $dir.$file;
81         
82         // Define the real type if this is a "store" file
83         if($type == 'store') {
84                 // Extract the file extension
85                 switch(getFileExt($file)) {
86                         // CSS file
87                         case 'css':
88                                 $type = 'css';
89                                 
90                                 break;
91                         
92                         // JS file
93                         case 'js':
94                                 $type = 'js';
95                                 
96                                 break;
97                         
98                         // Audio file
99                         case 'ogg':
100                         case 'oga':
101                                 $type = 'snd';
102                                 
103                                 break;
104                         
105                         // Image file
106                         case 'png':
107                         case 'jpg':
108                         case 'jpeg':
109                         case 'gif':
110                         case 'bmp':
111                                 $type = 'img';
112                                 
113                                 break;
114                 }
115         }
116         
117         // JS and CSS special stuffs
118         if(($type == 'css') || ($type == 'js')) {
119                 // Compression var
120                 if($has_compression)
121                         $cache_encoding = 'deflate';
122                 else
123                         $cache_encoding = 'plain';
124                 
125                 // Get the vars
126                 $version = getVersion();
127                 $hash = genHash($version);
128                 $cache_hash = md5($path.$hash.staticLocation()).'_'.$cache_encoding;
129                 
130                 // Check if the browser supports DEFLATE
131                 $deflate_support = false;
132                 
133                 if(isset($_SERVER['HTTP_ACCEPT_ENCODING']) && substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'deflate') && hasCompression() && !$is_developer)
134                         $deflate_support = true;
135                 
136                 // Internationalization
137                 if($type == 'js') {
138                         if(isset($_GET['l']) && !empty($_GET['l']) && !preg_match('/\.\.\//', $_GET['l']) && is_dir('../lang/'.$_GET['l']))
139                                 $locale = $_GET['l'];
140                         else
141                                 $locale = 'en';
142                 }
143                 
144                 else
145                         $locale = '';
146                 
147                 // Define the cache lang name
148                 if($locale)
149                         $cache_lang = $cache_hash.'_'.$locale;
150                 else
151                         $cache_lang = $cache_hash;
152         }
153         
154         // Explode the file string
155         if(strpos($file, '~') != false)
156                 $array = explode('~', $file);
157         else
158                 $array = array($file);
159         
160         // Define the reading vars
161         $continue = true;
162         $loop_files = true;
163         
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))
166                 $loop_files = false;
167         
168         // Check if the all the file(s) exist(s)
169         if($loop_files) {
170                 foreach($array as $current) {
171                         // Stop the loop if a file is missing
172                         if(!file_exists($dir.$current)) {
173                                 $continue = false;
174                                 
175                                 break;
176                         }
177                 }
178         }
179         
180         // We can read the file(s)
181         if($continue) {
182                 // We get the file MIME type
183                 $mime = strtolower(preg_replace('/(^)(.+)(\.)(.+)($)/i', '$4', $file));
184                 
185                 // We set up a known MIME type (and some other headers)
186                 if(($type == 'css') || ($type == 'js')) {
187                         // DEFLATE header
188                         if($deflate_support)
189                                 header('Content-Encoding: deflate');
190                         
191                         // MIME header
192                         if($type == 'css')
193                                 header('Content-Type: text/css; charset=utf-8');
194                         else if($type == 'js')
195                                 header('Content-Type: application/javascript; charset=utf-8');
196                 }
197                 
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');
208                 
209                 // Catch the file MIME type
210                 else
211                         header('Content-Type: '.getFileMIME($path));
212                 
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);
218                                 
219                                 if($deflate_support || !$has_compression)
220                                         echo $cache_read;
221                                 else
222                                         echo gzinflate($cache_read);
223                         }
224                         
225                         // Else, we generate the cache
226                         else {
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);
231                                         
232                                         // Filter the cache reference
233                                         if($has_compression)
234                                                 $output = gzinflate($cache_reference);
235                                         else
236                                                 $output = $cache_reference;
237                                 }
238                                 
239                                 // No cache reference, we should generate it
240                                 else {
241                                         // Initialize the loop
242                                         $looped = '';
243                                         
244                                         // Add the content of the current file
245                                         foreach($array as $current)
246                                                 $looped .= rmBOM(file_get_contents($dir.$current))."\n";
247                                         
248                                         // Filter the CSS
249                                         if($type == 'css') {
250                                                 // Apply the CSS logos
251                                                 $looped = setLogos($looped, $array);
252                                                 
253                                                 // Apply the CSS background
254                                                 $looped = setBackground($looped);
255                                                 
256                                                 // Set the Get API paths
257                                                 $looped = setPath($looped, $hash, HOST_STATIC, $type, '');
258                                         }
259                                         
260                                         // Optimize the code rendering
261                                         if($type == 'css') {
262                                                 // Can minify the CSS
263                                                 if($has_compression && !$is_developer)
264                                                         $output = compressCSS($looped);
265                                                 else
266                                                         $output = $looped;
267                                         }
268                                         
269                                         else {
270                                                 // Can minify the JS (sloooooow!)
271                                                 if($has_compression && !$is_developer) {
272                                                         require_once('./jsmin.php');
273                                                         $output = JSMin::minify($looped);
274                                                 }
275                                                 
276                                                 else
277                                                         $output = $looped;
278                                         }
279                                         
280                                         // Generate the reference cache
281                                         if($has_compression)
282                                                 $final = gzdeflate($output, 9);
283                                         else
284                                                 $final = $output;
285                                         
286                                         // Write it!
287                                         genCache($final, $is_developer, $cache_hash);
288                                 }
289                                 
290                                 // Filter the JS
291                                 if($type == 'js') {
292                                         // Set the JS locales
293                                         $output = setLocales($output, $locale);
294                                         
295                                         // Set the JS configuration
296                                         $output = setConfiguration($output, $locale, $version, uploadMaxSize());
297                                         
298                                         // Set the Get API paths
299                                         $output = setPath($output, $hash, HOST_STATIC, $type, $locale);
300                                         
301                                         // Translate the JS script
302                                         require_once('./gettext.php');
303                                         includeTranslation($locale, 'main');
304                                         $output = setTranslation($output);
305                                         
306                                         // Generate the cache
307                                         if($has_compression)
308                                                 $final = gzdeflate($output, 9);
309                                         else
310                                                 $final = $output;
311                                         
312                                         // Write it!
313                                         genCache($final, $is_developer, $cache_lang);
314                                 }
315                                 
316                                 // Output a well-encoded string
317                                 if($deflate_support || !$has_compression)
318                                         echo $final;
319                                 else
320                                         echo gzinflate($final);
321                         }
322                 }
323                 
324                 // Read the binary file (PNG, OGA and others)
325                 else
326                         readfile($path);
327                 
328                 exit;
329         }
330         
331         // The file was not found
332         header('Status: 404 Not Found', true, 404);
333         exit('HTTP/1.1 404 Not Found');
334 }
335
336 // The request is not correct
337 header('Status: 400 Bad Request', true, 400);
338 exit('HTTP/1.1 400 Bad Request');
339
340 ?>