-
Notifications
You must be signed in to change notification settings - Fork 7
/
oidplus.min.css.php
171 lines (146 loc) · 5.13 KB
/
oidplus.min.css.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?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\OIDplusDesignPlugin;
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, ".css")) {
http_response_code(404);
} else {
$files[] = $inc_file;
}
} else {
$do_minify = OIDplus::baseConfig()->getValue('MINIFY_CSS', true);
// Loading animation CSS
$files[] = __DIR__ . '/includes/loading.css';
// Find out base CSS
if (isset($_GET['theme'])) {
$theme = $_GET['theme'];
if (strpos($theme,'/') !== false) $theme = 'default';
if (strpos($theme,'\\') !== false) $theme = 'default';
if (strpos($theme,'..') !== false) $theme = 'default';
} else {
$theme = 'default';
}
if (file_exists($fil = OIDplus::getUserDataDir("styles").'oidplus_base.css')) {
// There is a user defined CSS (not recommended, use design plugins instead!)
$files[] = $fil;
} else {
// Use CSS of the design plugin
OIDplus::registerAllPlugins('design', OIDplusDesignPlugin::class, array(OIDplus::class,'registerDesignPlugin'));
$plugins = OIDplus::getDesignPlugins();
foreach ($plugins as $plugin) {
if ($plugin->id() == $theme) {
$manifest = $plugin->getManifest();
foreach ($manifest->getCSSFiles() as $css_file) {
$files[] = $css_file;
}
}
}
}
// Then plugins
$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->getCSSFiles() as $css_file) {
$files[] = $css_file;
}
}
// Now user-defined (additional) definitions
if (file_exists($fil = OIDplus::getUserDataDir("styles").'oidplus_add.css')) {
$files[] = $fil;
}
}
# ---
$out = "";
foreach ($files as $file) {
$out .= process_file($file);
}
$inv = $_GET['invert'] ?? 0;
if ($inv != 0) {
$out = invertColorsOfCSS($out);
}
$hs = $_GET['h_shift'] ?? 0;
$ss = $_GET['s_shift'] ?? 0;
$vs = $_GET['v_shift'] ?? 0;
if (($hs != 0) ||($ss != 0) || ($vs != 0)) {
$out = changeHueOfCSS($out, $hs, $ss, $vs);
}
# ---
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:text/css');
echo $out;
} else {
httpOutWithETag($out, 'text/css', 'oidplus_'.sha1($req_file).'.css');
}
# ---
/**
* @param string $filename
* @return string
*/
function process_file(string $filename): string {
global $do_minify;
$filename_min = preg_replace('/\.[^.]+$/', '.min.css', $filename);
$filename_full = $filename;
if ($do_minify) {
if (file_exists($filename_min)) {
// There is a file which is already minified
$filename = $filename_min;
$cont = file_get_contents($filename);
} else if (file_exists($filename_full)) {
// Otherwise, we minify it ourself
$filename = $filename_full;
$minifier = new Minify\CSS($filename);
$cont = $minifier->minify();
} else {
return '';
}
} else {
if (file_exists($filename_full)) {
$filename = $filename_full;
$cont = file_get_contents($filename);
} else if (file_exists($filename_min)) {
$filename = $filename_min;
$cont = file_get_contents($filename);
} else {
return '';
}
}
$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 = preg_replace('@url\\(\s+@im', 'url(', $cont);
$cont = str_ireplace('url("data:', 'url###("data:', $cont);
$cont = str_ireplace('url("', 'url###("'.$dir.'/', $cont);
$cont = str_ireplace("url('data:", "url###('data:", $cont);
$cont = str_ireplace("url('", "url###('".$dir.'/', $cont);
$cont = str_ireplace("url(data:", "url###(data:", $cont);
$cont = str_ireplace("url(", "url###(".$dir.'/', $cont);
$cont = str_ireplace("url###(", "url(", $cont);
return $cont."\n\n";
}