-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathoidplus.min.js.php
150 lines (124 loc) · 5.15 KB
/
oidplus.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/*
* OIDplus 2.0
* Copyright 2019 - 2024 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.
*/
use ViaThinkSoft\OIDplus\Core\OIDplus;
use ViaThinkSoft\OIDplus\Core\OIDplusLanguagePlugin;
use MatthiasMullie\Minify;
require_once __DIR__ . '/includes/oidplus.inc.php';
error_reporting(OIDplus::baseConfig()->getValue('DEBUG') ? E_ALL : 0);
@ini_set('display_errors', OIDplus::baseConfig()->getValue('DEBUG') ? '1' : '0');
$files = array();
# ---
$req_file = ($_REQUEST["file"] ?: "base");
if ($req_file != "base") {
$do_minify = false;
$inc_file = realpath(__DIR__ . DIRECTORY_SEPARATOR . $req_file);
if (!str_starts_with($inc_file, __DIR__ . DIRECTORY_SEPARATOR) || !str_ends_with($inc_file, ".js")) {
http_response_code(404);
} else {
$files[] = $inc_file;
}
} else {
$do_minify = OIDplus::baseConfig()->getValue('MINIFY_JS', true);
$files[] = 'var DEFAULT_LANGUAGE = ' . json_encode(OIDplus::getDefaultLang()) . ';';
OIDplus::registerAllPlugins('language', OIDplusLanguagePlugin::class, null);
$translation_array = OIDplus::getTranslationArray();
$files[] = 'var language_messages = ' . json_encode($translation_array) . ';';
//$tbl_prefix = OIDplus::baseConfig()->getValue('TABLENAME_PREFIX','');
//$files[] = 'var language_tblprefix = '.json_encode($tbl_prefix).';';
$files[] = 'var language_tblprefix = "<tableprefix>";'; // hide TABLENAME_PREFIX from the client
$files[] = 'var oidplus_webpath_relative = ' . js_escape(OIDplus::webpath(null, OIDplus::PATH_RELATIVE_TO_ROOT)) . ';';
$files[] = 'var oidplus_webpath_absolute_canonical = ' . js_escape(OIDplus::webpath(null, OIDplus::PATH_ABSOLUTE_CANONICAL)) . ';';
// The CSRF token is set by index.php
// TODO: can there race-conditions if we set csrf_token here? Or should we set it as inline-script in index.php ?
$files[] = 'var csrf_token = ' . js_escape($_COOKIE['csrf_token'] ?? '') . ';';
$files[] = 'var samesite_policy = ' . js_escape(OIDplus::baseConfig()->getValue('COOKIE_SAMESITE_POLICY', 'Strict')) . ';';
$files[] = process_file(__DIR__ . '/includes/oidplus_functions.js');
$files[] = process_file(__DIR__ . '/includes/oidplus_language.js');
$files[] = process_file(__DIR__ . '/includes/oidplus_base.js');
$manifests = OIDplus::getAllPluginManifests(implode(',', OIDplus::INTERACTIVE_PLUGIN_TYPES), true); // due to interface gridGeneratorLinks (INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_6) this plugin type can also have CSS
foreach ($manifests as $manifest) {
foreach ($manifest->getJSFiles() as $js_file) {
$files[] = process_file($js_file);
}
}
}
# ---
if ($do_minify) {
$minifier = null;
foreach ($files as $file) {
if (is_null($minifier)) {
$minifier = new Minify\JS($file);
} else {
$minifier->add($file);
}
}
$out = is_null($minifier) ? '' : $minifier->minify();
} else {
$out = '';
foreach ($files as $file) {
if (file_exists($file)) {
$out .= file_get_contents($file)."\n";
} else {
$out .= "$file\n";
}
}
}
# ---
if (OIDplus::baseConfig()->getValue('DEBUG')) {
// In debug mode, we might get PHP error messages (see "error_reporting" above),
// so it would be severe if we would allow ETAG! (since $out does not contain the PHP error messages!)
header('Content-Type:application/javascript');
echo $out;
} else {
httpOutWithETag($out, 'application/javascript', 'oidplus_'.sha1($req_file).'.js');
}
# ---
/**
* @param string $filename
* @return string
*/
function process_file(string $filename): string {
global $do_minify;
$filename_min = preg_replace('/\.[^.]+$/', '.min.js', $filename);
$filename_full = $filename;
if ($do_minify) {
if (file_exists($filename_min)) {
$filename = $filename_min;
} else if (file_exists($filename_full)) {
$filename = $filename_full;
} else {
return "console.error('Script file not found: $filename');";
}
} else {
if (file_exists($filename_full)) {
$filename = $filename_full;
} else if (file_exists($filename_min)) {
$filename = $filename_min;
} else {
return "console.error('Script file not found: $filename');";
}
}
$thisdir = __DIR__;
$thisdir = str_replace('\\', '/', $thisdir); // change Windows Backslashes into Web-Slashes
$filename = str_replace('\\', '/', $filename); // change Windows Backslashes into Web-Slashes
$dir = dirname((strpos($filename, $thisdir.'/') === 0) ? substr($filename, strlen($thisdir.'/')) : $filename);
$cont = file_get_contents($filename);
// TODO: WHY???? "DevTools failed to load SourceMap: Could not load content for http://localhost/oidplus/bootstrap.css.map: HTTP error: status code 404, net::ERR_HTTP_RESPONSE_CODE_FAILURE"
$cont = str_replace("//# sourceMappingURL=", "//# sourceMappingURL=".$dir.'/', $cont);
return $cont."\n\n";
}