Welcome to mirror list, hosted at ThFree Co, Russian Federation.

get.php « server - github.com/jappix/jappix.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ed51041eeb6fe48cf6b0e4ceca54dca83b29b8b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
<?php

/*

Jappix - An open social platform
This is the file get script

-------------------------------------------------

License: AGPL
Author: Valérian Saliou

*/

// PHP base
define('JAPPIX_BASE', '..');

// We get the needed files
require_once('./functions.php');
require_once('./functions-get.php');
require_once('./read-main.php');
require_once('./read-hosts.php');

// Prepare application
enableErrorSink();
hideErrors();

// Get some parameters
$is_developer = isDeveloper();
$has_compression = hasCompression();

// Cache control (for development & production)
if($is_developer) {
    header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
} else if(hasCaching()) {
    $expires = 31536000;

    header('Cache-Control: maxage='.$expires);
    header('Expires: '.gmdate('D, d M Y H:i:s', (time() + $expires)).' GMT');
}

// Initialize the vars
$type = '';
$file = '';

// Read the type var
if(isset($_GET['t']) && !empty($_GET['t']) && preg_match('/^(css|stylesheets|js|javascripts|img|images|snd|sounds|fonts|store)$/', $_GET['t'])) {
    $type = $_GET['t'];
}

// Read the files var
if(isset($_GET['f']) && !empty($_GET['f']) && isSafe($_GET['f'])) {
    $file = $_GET['f'];
}

// Read the group var (only for text files)
if(isset($_GET['g']) && !empty($_GET['g']) && preg_match('/^(\S+)\.xml$/', $_GET['g']) && preg_match('/^(css|stylesheets|js|javascripts)$/', $type) && isSafe($_GET['g']) && file_exists(JAPPIX_BASE.'/app/bundles/'.$_GET['g'])) {
    $xml_data = file_get_contents(JAPPIX_BASE.'/app/bundles/'.$_GET['g']);

    // Any data?
    if($xml_data) {
        $xml_read = new SimpleXMLElement($xml_data);
        $xml_parse = $xml_read->$type;

        // Files were added to the list before (with file var)?
        if($file) {
            $file .= '~'.$xml_parse;
        } else {
            $file = $xml_parse;
        }
    }
}

// We check if the data was submitted
if($file && $type) {
    // We define some stuffs
    $type = normalizeFileType($type);
    $dir = JAPPIX_BASE.'/app/'.$type.'/';
    $path = $dir.$file;

    // Read request headers
    $if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? trim($_SERVER['HTTP_IF_MODIFIED_SINCE']) : null;
    $if_modified_since = $if_modified_since ? strtotime($if_modified_since) : null;

    // Define the real type if this is a "store" file
    if($type == 'store') {
        // Rewrite path
        $dir = JAPPIX_BASE.'/store/';
        $path = $dir.$file;

        // Extract the file extension
        switch(getFileExt($file)) {
            // CSS file
            case 'css':
                $type = 'stylesheets'; break;

            // JS file
            case 'js':
                $type = 'javascripts';  break;

            // Audio file
            case 'ogg':
            case 'oga':
            case 'mp3':
                $type = 'sounds'; break;

            // Image file
            case 'png':
            case 'jpg':
            case 'jpeg':
            case 'gif':
            case 'bmp':
                $type = 'images'; break;

            // Image file
            case 'woff':
            case 'ttf':
            case 'eot':
            case 'svg':
                $type = 'fonts'; break;
        }
    }

    // JS and CSS special stuffs
    if(($type == 'stylesheets') || ($type == 'javascripts')) {
        // Compression var
        if($has_compression) {
            $cache_encoding = 'deflate';
        } else {
            $cache_encoding = 'plain';
        }

        // Get the vars
        $version = getVersion();
        $hash = genHash($version);
        $cache_hash = md5($path.$hash.staticLocation()).'_'.$cache_encoding;

        // Check if the browser supports DEFLATE
        $deflate_support = false;

        if(isset($_SERVER['HTTP_ACCEPT_ENCODING']) && substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'deflate') && hasCompression() && !$is_developer) {
            $deflate_support = true;
        }

        // Internationalization
        if($type == 'javascripts') {
            if(isset($_GET['l']) && !empty($_GET['l']) && !preg_match('/\.\.\//', $_GET['l']) && is_dir(JAPPIX_BASE.'/i18n/'.$_GET['l'])) {
                $locale = $_GET['l'];
            } else {
                $locale = 'en';
            }
        } else {
            $locale = '';
        }

        // Define the cache lang name
        if($locale) {
            $cache_lang = $cache_hash.'_'.$locale;
        } else {
            $cache_lang = $cache_hash;
        }
    }

    // Explode the file string
    if(strpos($file, '~') !== false) {
        $array = explode('~', $file);
    } else {
        $array = array($file);
    }

    // Define the reading vars
    $continue = true;
    $loop_files = true;

    // Check the cache exists for text files (avoid the heavy file_exists loop!)
    if(!$is_developer && (($type == 'stylesheets') || ($type == 'javascripts')) && hasCache($cache_lang)) {
        $loop_files = false;
    }

    // Check if the all the file(s) exist(s)
    if($loop_files) {
        foreach($array as $current) {
            // Stop the loop if a file is missing
            if(!file_exists($dir.$current)) {
                $continue = false;
                break;
            }
        }
    }

    // We can read the file(s)
    if($continue) {
        // We get the file MIME type
        $mime = strtolower(preg_replace('/(^)(.+)(\.)(.+)($)/i', '$4', $file));

        // We set up a known MIME type (and some other headers)
        if(($type == 'stylesheets') || ($type == 'javascripts')) {
            // DEFLATE header
            if($deflate_support) {
                header('Content-Encoding: deflate');
            }

            // MIME header
            if($type == 'stylesheets') {
                header('Content-Type: text/css; charset=utf-8');
            } else if($type == 'javascripts') {
                header('Content-Type: application/javascript; charset=utf-8');
            }
        } else {
            switch($mime) {
                case 'png':
                    header('Content-Type: image/png'); break;
                case 'gif':
                    header('Content-Type: image/gif'); break;
                case 'jpg':
                    header('Content-Type: image/jpeg'); break;
                case 'bmp':
                    header('Content-Type: image/bmp'); break;
                case 'oga':
                case 'ogg':
                    header('Content-Type: audio/ogg'); break;
                case 'mp3':
                    header('Content-Type: audio/mpeg'); break;
                case 'woff':
                    header('Content-Type: application/font-woff'); break;
                case 'ttf':
                    header('Content-Type: application/x-font-ttf'); break;
                case 'eot':
                    header('Content-Type: application/vnd.ms-fontobject'); break;
                case 'svg':
                    header('Content-Type: image/svg+xml'); break;
                default:
                    header('Content-Type: '.getFileMIME($path));
            }
        }

        // Read the text file(s) (CSS & JS)
        if(($type == 'stylesheets') || ($type == 'javascripts')) {
            // Storage vars
            $last_modified = $output_data = null;

            // If there's a cache file, read it
            if(hasCache($cache_lang) && !$is_developer) {
                $last_modified = filemtime(pathCache($cache_lang));
                $cache_read = readCache($cache_lang);

                if($deflate_support || !$has_compression) {
                    $output_data = $cache_read;
                } else {
                    $output_data = gzinflate($cache_read);
                }
            }

            // Else, we generate the cache
            else {
                // First try to read the cache reference
                if(hasCache($cache_hash) && !$is_developer) {
                    // Read the reference
                    $last_modified = filemtime(pathCache($cache_hash));
                    $cache_reference = readCache($cache_hash);

                    // Filter the cache reference
                    if($has_compression) {
                        $output = gzinflate($cache_reference);
                    } else {
                        $output = $cache_reference;
                    }
                }

                // No cache reference, we should generate it
                else {
                    // Last modified date is now
                    $last_modified = time();

                    // Initialize the loop
                    $looped = '';

                    // Add the content of the current file
                    foreach($array as $current) {
                        $looped .= rmBOM(file_get_contents($dir.$current))."\n";
                    }

                    // Filter the CSS
                    if($type == 'stylesheets') {
                        // Apply the CSS logos
                        $looped = setLogos($looped, $array);

                        // Apply the CSS background
                        $looped = setBackground($looped);

                        // Set the Get API paths
                        $looped = setPath($looped, $hash, HOST_STATIC, $type, '');
                    }

                    // Optimize the code rendering
                    if($type == 'stylesheets') {
                        // Can minify the CSS
                        if($has_compression && !$is_developer) {
                            $output = compressCSS($looped);
                        } else {
                            $output = $looped;
                        }
                    } else {
                        // Can minify the JS (sloooooow!)
                        if($has_compression && !$is_developer) {
                            require_once('./jsmin.php');
                            $output = JSMin::minify($looped);
                        } else {
                            $output = $looped;
                        }
                    }

                    // Generate the reference cache
                    if($has_compression) {
                        $final = gzdeflate($output, 9);
                    } else {
                        $final = $output;
                    }

                    // Write it!
                    genCache($final, $is_developer, $cache_hash);
                }

                // Filter the JS
                if($type == 'javascripts') {
                    // Set the JS locales
                    $output = setLocales($output, $locale);

                    // Set the JS configuration
                    $output = setConfiguration($output, $hash, $locale, $version, uploadMaxSize());

                    // Set the Get API paths
                    $output = setPath($output, $hash, HOST_STATIC, $type, $locale);

                    // Translate the JS script
                    require_once('./gettext.php');
                    includeTranslation($locale, 'main');
                    $output = setTranslation($output);

                    // Generate the cache
                    if($has_compression) {
                        $final = gzdeflate($output, 9);
                    } else {
                        $final = $output;
                    }

                    // Write it!
                    genCache($final, $is_developer, $cache_lang);
                }

                // Output a well-encoded string
                if($deflate_support || !$has_compression) {
                    $output_data = $final;
                } else {
                    $output_data = gzinflate($final);
                }
            }

            // Any data to output?
            if($output_data) {
                // Last-Modified HTTP header
                if(!$is_developer && hasCaching()) {
                    header('Last-Modified: '.gmdate('D, d M Y H:i:s', $last_modified).' GMT');
                }

                // Check browser cache
                if(!$is_developer && hasCaching() &&
                    ($if_modified_since && ($last_modified <= $if_modified_since))) {
                    // Use browser cache
                    header('Status: 304 Not Modified', true, 304);
                    exit;
                } else {
                    // More file HTTP headers
                    header('Content-Length: '.strlen($output_data));

                    // Output data
                    echo($output_data);
                }
            }

            // Free up memory (prevents leaks)
            unset($output_data);
        }

        // Read the binary file (PNG, OGA and others)
        else {
            // Process re-usable HTTP headers values
            $last_modified = filemtime($path);

            // Last-Modified HTTP header
            if(!$is_developer && hasCaching()) {
                header('Last-Modified: '.gmdate('D, d M Y H:i:s', $last_modified).' GMT');
            }

            // Check browser cache
            if(!$is_developer && hasCaching() &&
                ($if_modified_since && ($last_modified <= $if_modified_since))) {
                // Use browser cache
                header('Status: 304 Not Modified', true, 304);
                exit;
            } else {
                // More file HTTP headers
                header('Content-Length: '.filesize($path));

                // Simple binary read (no packing needed)
                readfile($path);
            }
        }

        exit;
    }

    // The file was not found
    header('Status: 404 Not Found', true, 404);
    exit('HTTP/1.1 404 Not Found');
}

// The request is not correct
header('Status: 400 Bad Request', true, 400);
exit('HTTP/1.1 400 Bad Request');

?>