Subversion Repositories oidplus

Rev

Rev 376 | Rev 402 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
375 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
5
 * Copyright 2019 Daniel Marschall, ViaThinkSoft
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 */
19
 
20
// We do this stuff to avoid that the client includes an externa JavaScript from polyfill.io,
21
// since some users might have a problem with that (in regards privacy of their IP address etc.)
22
// So, the OIDplus webserver will act as proxy.
23
 
376 daniel-mar 24
define('REQUIRED_POLYFILLS', array(
25
        'fetch', // Internet Explorer
26
        'URL'    // Internet Explorer
27
));
375 daniel-mar 28
 
376 daniel-mar 29
define('MINIFY_POLYFILL', true);
30
 
379 daniel-mar 31
define('POLYFILL_CACHE_MAX_AGE', 24*60*60); // 1 day
376 daniel-mar 32
 
33
# ---
34
 
379 daniel-mar 35
require_once __DIR__ . '/includes/functions.inc.php';
376 daniel-mar 36
 
379 daniel-mar 37
if (!isset($_SERVER['HTTP_USER_AGENT'])) {
38
        $out = '/* ERROR: No User Agent available */';
39
        httpOutWithETag($out, 'application/javascript', 'polyfill.js');
40
}
376 daniel-mar 41
 
375 daniel-mar 42
$ua = $_SERVER['HTTP_USER_AGENT'];
43
 
376 daniel-mar 44
if (MINIFY_POLYFILL) {
45
        $cache_file = __DIR__ . '/userdata/cache/.polyfill_'.md5(implode(',',REQUIRED_POLYFILLS)).'_'.md5($ua).'.min.js';
46
} else {
47
        $cache_file = __DIR__ . '/userdata/cache/.polyfill_'.md5(implode(',',REQUIRED_POLYFILLS)).'_'.md5($ua).'.js';
48
}
375 daniel-mar 49
 
376 daniel-mar 50
if (!file_exists($cache_file) || (time()-filemtime($cache_file)) > POLYFILL_CACHE_MAX_AGE) {
375 daniel-mar 51
 
376 daniel-mar 52
        if (MINIFY_POLYFILL) {
53
                $url = 'https://polyfill.io/v3/polyfill.min.js?features='.urlencode(implode(',',REQUIRED_POLYFILLS));
54
        } else {
55
                $url = 'https://polyfill.io/v3/polyfill.js?features='.urlencode(implode(',',REQUIRED_POLYFILLS));
56
        }
57
 
58
        $opts = [
59
            "http" => [
60
                "method" => "GET",
61
                "header" => "User-Agent: ".$ua."\r\n"
62
            ]
63
        ];
64
        $context = stream_context_create($opts);
65
        $cont = @file_get_contents($url, false, $context);
66
 
67
        if ($cont === false) {
68
 
69
                if (file_exists($cache_file)) {
70
                        $out = file_get_contents($cache_file);
379 daniel-mar 71
 
72
                        // In case polyfill.io is down, don't delay further requests
73
                        // Instead, try again after the next max cache age
74
                        touch($cache_file);
376 daniel-mar 75
                } else {
379 daniel-mar 76
                        $out = '/* ERROR: polyfill.io not available and no cache file exists! */';
77
                        httpOutWithETag($out, 'application/javascript', 'polyfill.js');
376 daniel-mar 78
                }
79
 
80
        } else {
81
 
82
                $ua = str_replace('*/', '', $ua);
379 daniel-mar 83
                $cont = '/* Polyfill '.$url.' for '.$ua.' */'.$cont;
376 daniel-mar 84
 
85
                @file_put_contents($cache_file, $cont);
86
 
87
                $out = $cont;
88
 
89
        }
90
 
91
} else {
92
 
93
        $out = file_get_contents($cache_file);
94
 
95
}
96
 
375 daniel-mar 97
# ---
98
 
379 daniel-mar 99
httpOutWithETag($out, 'application/javascript', 'polyfill.js');