Rev 1182 | 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 |
||
511 | daniel-mar | 5 | * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft |
375 | daniel-mar | 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 | |||
891 | daniel-mar | 24 | // see https://polyfill.io/v3/url-builder/ |
1116 | daniel-mar | 25 | const REQUIRED_POLYFILLS = array( |
1349 | daniel-mar | 26 | // For various AJAX calls |
427 | daniel-mar | 27 | 'fetch', |
402 | daniel-mar | 28 | 'URL', |
427 | daniel-mar | 29 | |
1349 | daniel-mar | 30 | // For OIDplusPagePublicWhois.js, OIDplusPageAdminSoftwareUpdate.js, and OIDplusPageAdminColors.js |
891 | daniel-mar | 31 | 'String.prototype.includes', |
32 | |||
1349 | daniel-mar | 33 | // 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 ) |
402 | daniel-mar | 34 | 'document.currentScript' |
1116 | daniel-mar | 35 | ); |
375 | daniel-mar | 36 | |
1116 | daniel-mar | 37 | const MINIFY_POLYFILL = true; // TODO: put into baseconfig? |
376 | daniel-mar | 38 | |
1116 | daniel-mar | 39 | const POLYFILL_CACHE_MAX_AGE = 24 * 60 * 60; // 1 day, TODO: put into baseconfig? |
376 | daniel-mar | 40 | |
41 | # --- |
||
42 | |||
1182 | daniel-mar | 43 | require_once __DIR__ . '/includes/oidplus.inc.php'; |
376 | daniel-mar | 44 | |
481 | daniel-mar | 45 | error_reporting(0); |
46 | |||
379 | daniel-mar | 47 | if (!isset($_SERVER['HTTP_USER_AGENT'])) { |
48 | $out = '/* ERROR: No User Agent available */'; |
||
427 | daniel-mar | 49 | httpOutWithETag($out, 'application/javascript', 'polyfill'.(MINIFY_POLYFILL ? '.min' : '').'.js'); |
379 | daniel-mar | 50 | } |
376 | daniel-mar | 51 | |
375 | daniel-mar | 52 | $ua = $_SERVER['HTTP_USER_AGENT']; |
53 | |||
376 | daniel-mar | 54 | if (MINIFY_POLYFILL) { |
481 | daniel-mar | 55 | $cache_file = __DIR__ . '/userdata/cache/js_polyfill_'.md5(implode(',',REQUIRED_POLYFILLS)).'_'.md5($ua).'.min.js'; |
376 | daniel-mar | 56 | } else { |
481 | daniel-mar | 57 | $cache_file = __DIR__ . '/userdata/cache/js_polyfill_'.md5(implode(',',REQUIRED_POLYFILLS)).'_'.md5($ua).'.js'; |
376 | daniel-mar | 58 | } |
375 | daniel-mar | 59 | |
376 | daniel-mar | 60 | if (!file_exists($cache_file) || (time()-filemtime($cache_file)) > POLYFILL_CACHE_MAX_AGE) { |
375 | daniel-mar | 61 | |
376 | daniel-mar | 62 | if (MINIFY_POLYFILL) { |
63 | $url = 'https://polyfill.io/v3/polyfill.min.js?features='.urlencode(implode(',',REQUIRED_POLYFILLS)); |
||
64 | } else { |
||
65 | $url = 'https://polyfill.io/v3/polyfill.js?features='.urlencode(implode(',',REQUIRED_POLYFILLS)); |
||
66 | } |
||
67 | |||
1182 | daniel-mar | 68 | if (!url_get_contents_available(true, $reason)) { |
69 | $out = '/* ERROR: Cannot connect to the Internet. '.$reason.' */'; |
||
70 | httpOutWithETag($out, 'application/javascript', 'polyfill'.(MINIFY_POLYFILL ? '.min' : '').'.js'); |
||
71 | } |
||
72 | |||
1149 | daniel-mar | 73 | $cont = url_get_contents($url, [], $ua); |
376 | daniel-mar | 74 | |
75 | if ($cont === false) { |
||
76 | |||
77 | if (file_exists($cache_file)) { |
||
78 | $out = file_get_contents($cache_file); |
||
379 | daniel-mar | 79 | |
80 | // In case polyfill.io is down, don't delay further requests |
||
81 | // Instead, try again after the next max cache age |
||
82 | touch($cache_file); |
||
376 | daniel-mar | 83 | } else { |
1182 | daniel-mar | 84 | $out = '/* ERROR: polyfill.io (' . $url . ') not available and no cache file exists! */'; |
85 | httpOutWithETag($out, 'application/javascript', 'polyfill' . (MINIFY_POLYFILL ? '.min' : '') . '.js'); |
||
376 | daniel-mar | 86 | } |
87 | |||
88 | } else { |
||
89 | |||
90 | $ua = str_replace('*/', '', $ua); |
||
1182 | daniel-mar | 91 | $cont = '/* Polyfill ' . $url . ' for ' . $ua . ' */' . $cont; |
376 | daniel-mar | 92 | |
427 | daniel-mar | 93 | if (POLYFILL_CACHE_MAX_AGE > 0) { |
94 | @file_put_contents($cache_file, $cont); |
||
95 | } |
||
376 | daniel-mar | 96 | |
97 | $out = $cont; |
||
98 | } |
||
99 | |||
100 | } else { |
||
101 | |||
102 | $out = file_get_contents($cache_file); |
||
103 | |||
104 | } |
||
105 | |||
375 | daniel-mar | 106 | # --- |
107 | |||
1143 | daniel-mar | 108 | if (!$out) $out = ''; |
427 | daniel-mar | 109 | httpOutWithETag($out, 'application/javascript', 'polyfill'.(MINIFY_POLYFILL ? '.min' : '').'.js'); |