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