/
polyfill.min.js.php
109 lines (80 loc) · 3.47 KB
/
polyfill.min.js.php
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
<?php
/*
* OIDplus 2.0
* Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// We do this stuff to avoid that the client includes an external JavaScript from polyfill.io,
// since some users might have a problem with that (in regards privacy of their IP address etc.)
// So, the OIDplus webserver will act as proxy.
// see https://polyfill.io/v3/url-builder/
const REQUIRED_POLYFILLS = array(
// For various AJAX calls
'fetch',
'URL',
// For OIDplusPagePublicWhois.js, OIDplusPageAdminSoftwareUpdate.js, and OIDplusPageAdminColors.js
'String.prototype.includes',
// 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 )
'document.currentScript'
);
const MINIFY_POLYFILL = true; // TODO: put into baseconfig?
const POLYFILL_CACHE_MAX_AGE = 24 * 60 * 60; // 1 day, TODO: put into baseconfig?
# ---
require_once __DIR__ . '/includes/oidplus.inc.php';
error_reporting(0);
if (!isset($_SERVER['HTTP_USER_AGENT'])) {
$out = '/* ERROR: No User Agent available */';
httpOutWithETag($out, 'application/javascript', 'polyfill'.(MINIFY_POLYFILL ? '.min' : '').'.js');
}
$ua = $_SERVER['HTTP_USER_AGENT'];
if (MINIFY_POLYFILL) {
$cache_file = __DIR__ . '/userdata/cache/js_polyfill_'.md5(implode(',',REQUIRED_POLYFILLS)).'_'.md5($ua).'.min.js';
} else {
$cache_file = __DIR__ . '/userdata/cache/js_polyfill_'.md5(implode(',',REQUIRED_POLYFILLS)).'_'.md5($ua).'.js';
}
if (!file_exists($cache_file) || (time()-filemtime($cache_file)) > POLYFILL_CACHE_MAX_AGE) {
if (MINIFY_POLYFILL) {
$url = 'https://polyfill.io/v3/polyfill.min.js?features='.urlencode(implode(',',REQUIRED_POLYFILLS));
} else {
$url = 'https://polyfill.io/v3/polyfill.js?features='.urlencode(implode(',',REQUIRED_POLYFILLS));
}
if (!url_get_contents_available(true, $reason)) {
$out = '/* ERROR: Cannot connect to the Internet. '.$reason.' */';
httpOutWithETag($out, 'application/javascript', 'polyfill'.(MINIFY_POLYFILL ? '.min' : '').'.js');
}
$cont = url_get_contents($url, [], $ua);
if ($cont === false) {
if (file_exists($cache_file)) {
$out = file_get_contents($cache_file);
// In case polyfill.io is down, don't delay further requests
// Instead, try again after the next max cache age
touch($cache_file);
} else {
$out = '/* ERROR: polyfill.io (' . $url . ') not available and no cache file exists! */';
httpOutWithETag($out, 'application/javascript', 'polyfill' . (MINIFY_POLYFILL ? '.min' : '') . '.js');
}
} else {
$ua = str_replace('*/', '', $ua);
$cont = '/* Polyfill ' . $url . ' for ' . $ua . ' */' . $cont;
if (POLYFILL_CACHE_MAX_AGE > 0) {
@file_put_contents($cache_file, $cont);
}
$out = $cont;
}
} else {
$out = file_get_contents($cache_file);
}
# ---
if (!$out) $out = '';
httpOutWithETag($out, 'application/javascript', 'polyfill'.(MINIFY_POLYFILL ? '.min' : '').'.js');