Rev 402 | Rev 481 | 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 | |||
427 | daniel-mar | 20 | // We do this stuff to avoid that the client includes an external JavaScript from polyfill.io, |
375 | daniel-mar | 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( |
402 | daniel-mar | 25 | // Internet Explorer for various AJAX calls |
427 | daniel-mar | 26 | 'fetch', |
402 | daniel-mar | 27 | 'URL', |
427 | daniel-mar | 28 | |
402 | daniel-mar | 29 | // Internet Explorer for TinyMCE if it is included inside oidplus.min.js.php ( https://github.com/tinymce/tinymce/blob/5c1702a119e683f93e03ecc2231f11d17ce57395/modules/tinymce/src/core/main/ts/api/EditorManager.ts#L271 ) |
30 | 'document.currentScript' |
||
376 | daniel-mar | 31 | )); |
375 | daniel-mar | 32 | |
427 | daniel-mar | 33 | define('MINIFY_POLYFILL', true); // TODO: put into baseconfig? |
376 | daniel-mar | 34 | |
427 | daniel-mar | 35 | define('POLYFILL_CACHE_MAX_AGE', 24*60*60); // 1 day, TODO: put into baseconfig? |
376 | daniel-mar | 36 | |
37 | # --- |
||
38 | |||
379 | daniel-mar | 39 | require_once __DIR__ . '/includes/functions.inc.php'; |
376 | daniel-mar | 40 | |
379 | daniel-mar | 41 | if (!isset($_SERVER['HTTP_USER_AGENT'])) { |
42 | $out = '/* ERROR: No User Agent available */'; |
||
427 | daniel-mar | 43 | httpOutWithETag($out, 'application/javascript', 'polyfill'.(MINIFY_POLYFILL ? '.min' : '').'.js'); |
379 | daniel-mar | 44 | } |
376 | daniel-mar | 45 | |
375 | daniel-mar | 46 | $ua = $_SERVER['HTTP_USER_AGENT']; |
47 | |||
376 | daniel-mar | 48 | if (MINIFY_POLYFILL) { |
49 | $cache_file = __DIR__ . '/userdata/cache/.polyfill_'.md5(implode(',',REQUIRED_POLYFILLS)).'_'.md5($ua).'.min.js'; |
||
50 | } else { |
||
51 | $cache_file = __DIR__ . '/userdata/cache/.polyfill_'.md5(implode(',',REQUIRED_POLYFILLS)).'_'.md5($ua).'.js'; |
||
52 | } |
||
375 | daniel-mar | 53 | |
376 | daniel-mar | 54 | if (!file_exists($cache_file) || (time()-filemtime($cache_file)) > POLYFILL_CACHE_MAX_AGE) { |
375 | daniel-mar | 55 | |
376 | daniel-mar | 56 | if (MINIFY_POLYFILL) { |
57 | $url = 'https://polyfill.io/v3/polyfill.min.js?features='.urlencode(implode(',',REQUIRED_POLYFILLS)); |
||
58 | } else { |
||
59 | $url = 'https://polyfill.io/v3/polyfill.js?features='.urlencode(implode(',',REQUIRED_POLYFILLS)); |
||
60 | } |
||
61 | |||
62 | $opts = [ |
||
63 | "http" => [ |
||
64 | "method" => "GET", |
||
65 | "header" => "User-Agent: ".$ua."\r\n" |
||
66 | ] |
||
67 | ]; |
||
68 | $context = stream_context_create($opts); |
||
69 | $cont = @file_get_contents($url, false, $context); |
||
70 | |||
71 | if ($cont === false) { |
||
72 | |||
73 | if (file_exists($cache_file)) { |
||
74 | $out = file_get_contents($cache_file); |
||
379 | daniel-mar | 75 | |
76 | // In case polyfill.io is down, don't delay further requests |
||
77 | // Instead, try again after the next max cache age |
||
78 | touch($cache_file); |
||
376 | daniel-mar | 79 | } else { |
379 | daniel-mar | 80 | $out = '/* ERROR: polyfill.io not available and no cache file exists! */'; |
427 | daniel-mar | 81 | httpOutWithETag($out, 'application/javascript', 'polyfill'.(MINIFY_POLYFILL ? '.min' : '').'.js'); |
376 | daniel-mar | 82 | } |
83 | |||
84 | } else { |
||
85 | |||
86 | $ua = str_replace('*/', '', $ua); |
||
379 | daniel-mar | 87 | $cont = '/* Polyfill '.$url.' for '.$ua.' */'.$cont; |
376 | daniel-mar | 88 | |
427 | daniel-mar | 89 | if (POLYFILL_CACHE_MAX_AGE > 0) { |
90 | @file_put_contents($cache_file, $cont); |
||
91 | } |
||
376 | daniel-mar | 92 | |
93 | $out = $cont; |
||
94 | } |
||
95 | |||
96 | } else { |
||
97 | |||
98 | $out = file_get_contents($cache_file); |
||
99 | |||
100 | } |
||
101 | |||
375 | daniel-mar | 102 | # --- |
103 | |||
427 | daniel-mar | 104 | httpOutWithETag($out, 'application/javascript', 'polyfill'.(MINIFY_POLYFILL ? '.min' : '').'.js'); |