Rev 1055 | Rev 1068 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | daniel-mar | 1 | <?php |
2 | |||
3 | /* |
||
4 | * OIDplus 2.0 |
||
778 | daniel-mar | 5 | * Copyright 2019 - 2022 Daniel Marschall, ViaThinkSoft |
2 | 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 | |||
1050 | daniel-mar | 20 | namespace ViaThinkSoft\OIDplus; |
511 | daniel-mar | 21 | |
730 | daniel-mar | 22 | class OIDplus extends OIDplusBaseClass { |
281 | daniel-mar | 23 | private static /*OIDplusPagePlugin[]*/ $pagePlugins = array(); |
24 | private static /*OIDplusAuthPlugin[]*/ $authPlugins = array(); |
||
289 | daniel-mar | 25 | private static /*OIDplusLoggerPlugin[]*/ $loggerPlugins = array(); |
227 | daniel-mar | 26 | private static /*OIDplusObjectTypePlugin[]*/ $objectTypePlugins = array(); |
27 | private static /*string[]*/ $enabledObjectTypes = array(); |
||
28 | private static /*string[]*/ $disabledObjectTypes = array(); |
||
29 | private static /*OIDplusDatabasePlugin[]*/ $dbPlugins = array(); |
||
702 | daniel-mar | 30 | private static /*OIDplusCaptchaPlugin[]*/ $captchaPlugins = array(); |
274 | daniel-mar | 31 | private static /*OIDplusSqlSlangPlugin[]*/ $sqlSlangPlugins = array(); |
355 | daniel-mar | 32 | private static /*OIDplusLanguagePlugin[]*/ $languagePlugins = array(); |
449 | daniel-mar | 33 | private static /*OIDplusDesignPlugin[]*/ $designPlugins = array(); |
2 | daniel-mar | 34 | |
280 | daniel-mar | 35 | protected static $html = true; |
236 | daniel-mar | 36 | |
812 | daniel-mar | 37 | /*public*/ const PATH_RELATIVE = 1; // e.g. "../" |
38 | /*public*/ const PATH_ABSOLUTE = 2; // e.g. "http://www.example.com/oidplus/" |
||
39 | /*public*/ const PATH_ABSOLUTE_CANONICAL = 3; // e.g. "http://www.example.org/oidplus/" (if baseconfig CANONICAL_SYSTEM_URL is set) |
||
40 | /*public*/ const PATH_RELATIVE_TO_ROOT = 4; // e.g. "/oidplus/" |
||
41 | /*public*/ const PATH_RELATIVE_TO_ROOT_CANONICAL = 5; // e.g. "/oidplus/" (if baseconfig CANONICAL_SYSTEM_URL is set) |
||
801 | daniel-mar | 42 | |
778 | daniel-mar | 43 | // These plugin types can contain HTML code and therefore may |
44 | // emit (non-setup) CSS/JS code via their manifest. |
||
45 | /*public*/ const INTERACTIVE_PLUGIN_TYPES = array( |
||
46 | 'publicPages', |
||
47 | 'raPages', |
||
48 | 'adminPages', |
||
49 | 'objectTypes', |
||
50 | 'captcha' |
||
51 | ); |
||
52 | |||
2 | daniel-mar | 53 | private function __construct() { |
54 | } |
||
295 | daniel-mar | 55 | |
1055 | daniel-mar | 56 | private static function insideSetup() { |
57 | return (strpos($_SERVER['REQUEST_URI'], OIDplus::webpath(null,OIDplus::PATH_RELATIVE_TO_ROOT).'setup/') === 0); |
||
58 | } |
||
59 | |||
1050 | daniel-mar | 60 | // --- Static classes |
274 | daniel-mar | 61 | |
263 | daniel-mar | 62 | private static $baseConfig = null; |
1050 | daniel-mar | 63 | private static $oldConfigFormatLoaded = false; |
261 | daniel-mar | 64 | public static function baseConfig() { |
263 | daniel-mar | 65 | $first_init = false; |
274 | daniel-mar | 66 | |
263 | daniel-mar | 67 | if ($first_init = is_null(self::$baseConfig)) { |
68 | self::$baseConfig = new OIDplusBaseConfig(); |
||
261 | daniel-mar | 69 | } |
70 | |||
1055 | daniel-mar | 71 | if (self::insideSetup()) return self::$baseConfig; |
72 | if ((basename($_SERVER['SCRIPT_NAME']) === 'oidplus.min.js.php') && isset($_REQUEST['noBaseConfig']) && ($_REQUEST['noBaseConfig'] == '1')) return self::$baseConfig; |
||
73 | if ((basename($_SERVER['SCRIPT_NAME']) === 'oidplus.min.css.php') && isset($_REQUEST['noBaseConfig']) && ($_REQUEST['noBaseConfig'] == '1')) return self::$baseConfig; |
||
74 | |||
263 | daniel-mar | 75 | if ($first_init) { |
261 | daniel-mar | 76 | // Include a file containing various size/depth limitations of OIDs |
294 | daniel-mar | 77 | // It is important to include it before userdata/baseconfig/config.inc.php was included, |
78 | // so we can give userdata/baseconfig/config.inc.php the chance to override the values. |
||
261 | daniel-mar | 79 | |
496 | daniel-mar | 80 | include OIDplus::localpath().'includes/oidplus_limits.inc.php'; |
261 | daniel-mar | 81 | |
82 | // Include config file |
||
295 | daniel-mar | 83 | |
496 | daniel-mar | 84 | $config_file = OIDplus::localpath() . 'userdata/baseconfig/config.inc.php'; |
85 | $config_file_old = OIDplus::localpath() . 'includes/config.inc.php'; // backwards compatibility |
||
295 | daniel-mar | 86 | |
294 | daniel-mar | 87 | if (!file_exists($config_file) && file_exists($config_file_old)) { |
88 | $config_file = $config_file_old; |
||
89 | } |
||
261 | daniel-mar | 90 | |
294 | daniel-mar | 91 | if (file_exists($config_file)) { |
1050 | daniel-mar | 92 | if (self::$oldConfigFormatLoaded) { |
263 | daniel-mar | 93 | // Note: We may only include it once due to backwards compatibility, |
94 | // since in version 2.0, the configuration was defined using define() statements |
||
95 | // Attention: This does mean that a full re-init (e.g. for test cases) is not possible |
||
96 | // if a version 2.0 config is used! |
||
1050 | daniel-mar | 97 | |
98 | // We need to do this, because define() cannot be undone |
||
99 | // Note: This can only happen in very special cases (e.g. test cases) where you call init() twice |
||
100 | throw new OIDplusConfigInitializationException(_L('A full re-initialization is not possible if a version 2.0 config file (containing "defines") is used. Please update to a config 2.1 file by running setup again.')); |
||
263 | daniel-mar | 101 | } else { |
1050 | daniel-mar | 102 | $tmp = file_get_contents($config_file); |
103 | $ns = "ViaThinkSoft\OIDplus\OIDplus"; |
||
104 | $uses = "use $ns;"; |
||
105 | if ((strpos($tmp,'OIDplus::') !== false) && (strpos($tmp,$uses) === false)) { |
||
106 | // Migrate config file to namespace class names |
||
107 | // Note: Only config files version 2.1 are affected. Not 2.0 ones |
||
108 | |||
109 | $tmp = "<?php\r\n\r\n$uses /* Automatically added by migration procedure */\r\n?>$tmp"; |
||
110 | $tmp = str_replace('?><?php', '', $tmp); |
||
111 | |||
112 | $tmp = str_replace("\$ns\OIDplusCaptchaPluginRecaptcha::", "OIDplusCaptchaPluginRecaptcha::", $tmp); |
||
113 | $tmp = str_replace("OIDplusCaptchaPluginRecaptcha::", "\$ns\OIDplusCaptchaPluginRecaptcha::", $tmp); |
||
114 | |||
115 | $tmp = str_replace('DISABLE_PLUGIN_OIDplusPagePublicRdap', |
||
116 | 'DISABLE_PLUGIN_Frdlweb\OIDplus\OIDplusPagePublicRdap', $tmp); |
||
117 | $tmp = str_replace('DISABLE_PLUGIN_OIDplusPagePublicAltIds', |
||
118 | 'DISABLE_PLUGIN_Frdlweb\OIDplus\OIDplusPagePublicAltIds', $tmp); |
||
1051 | daniel-mar | 119 | $tmp = str_replace('DISABLE_PLUGIN_OIDplusPagePublicUITweaks', |
120 | 'DISABLE_PLUGIN_TushevOrg\OIDplus\OIDplusPagePublicUITweaks', $tmp); |
||
1050 | daniel-mar | 121 | $tmp = str_replace('DISABLE_PLUGIN_OIDplus', |
122 | 'DISABLE_PLUGIN_ViaThinkSoft\OIDplus\OIDplus', $tmp); |
||
123 | |||
124 | if (@file_put_contents($config_file, $tmp) === false) { |
||
125 | eval('?>'.$tmp); |
||
126 | } else { |
||
127 | include $config_file; |
||
128 | } |
||
129 | } else { |
||
130 | include $config_file; |
||
131 | } |
||
263 | daniel-mar | 132 | } |
261 | daniel-mar | 133 | |
1055 | daniel-mar | 134 | // Backwards compatibility 2.0 => 2.1 |
261 | daniel-mar | 135 | if (defined('OIDPLUS_CONFIG_VERSION') && (OIDPLUS_CONFIG_VERSION == 2.0)) { |
1050 | daniel-mar | 136 | self::$oldConfigFormatLoaded = true; |
261 | daniel-mar | 137 | foreach (get_defined_constants(true)['user'] as $name => $value) { |
138 | $name = str_replace('OIDPLUS_', '', $name); |
||
139 | if ($name == 'SESSION_SECRET') $name = 'SERVER_SECRET'; |
||
140 | if ($name == 'MYSQL_QUERYLOG') $name = 'QUERY_LOGFILE'; |
||
1050 | daniel-mar | 141 | $name = str_replace('DISABLE_PLUGIN_OIDplusPagePublicRdap', |
142 | 'DISABLE_PLUGIN_Frdlweb\OIDplus\OIDplusPagePublicRdap', $name); |
||
143 | $name = str_replace('DISABLE_PLUGIN_OIDplusPagePublicAltIds', |
||
144 | 'DISABLE_PLUGIN_Frdlweb\OIDplus\OIDplusPagePublicAltIds', $name); |
||
1051 | daniel-mar | 145 | $name = str_replace('DISABLE_PLUGIN_OIDplusPagePublicUITweaks', |
146 | 'DISABLE_PLUGIN_TushevOrg\OIDplus\OIDplusPagePublicUITweaks', $name); |
||
1050 | daniel-mar | 147 | $name = str_replace('DISABLE_PLUGIN_OIDplus', |
148 | 'DISABLE_PLUGIN_ViaThinkSoft\OIDplus\OIDplus', $name); |
||
1055 | daniel-mar | 149 | if ($name == 'CONFIG_VERSION') { |
150 | $value = 2.1; |
||
151 | } else if (($name == 'MYSQL_PASSWORD') || ($name == 'ODBC_PASSWORD') || ($name == 'PDO_PASSWORD') || ($name == 'PGSQL_PASSWORD')) { |
||
152 | $value = base64_decode($value); |
||
261 | daniel-mar | 153 | } |
1055 | daniel-mar | 154 | self::$baseConfig->setValue($name, $value); |
261 | daniel-mar | 155 | } |
156 | } |
||
157 | } else { |
||
496 | daniel-mar | 158 | if (!is_dir(OIDplus::localpath().'setup')) { |
1050 | daniel-mar | 159 | throw new OIDplusConfigInitializationException(_L('File %1 is missing, but setup can\'t be started because its directory missing.',$config_file)); |
261 | daniel-mar | 160 | } else { |
280 | daniel-mar | 161 | if (self::$html) { |
1055 | daniel-mar | 162 | if (!self::insideSetup()) { |
801 | daniel-mar | 163 | header('Location:'.OIDplus::webpath(null,OIDplus::PATH_RELATIVE).'setup/'); |
360 | daniel-mar | 164 | die(_L('Redirecting to setup...')); |
349 | daniel-mar | 165 | } else { |
166 | return self::$baseConfig; |
||
167 | } |
||
261 | daniel-mar | 168 | } else { |
169 | // This can be displayed in e.g. ajax.php |
||
1050 | daniel-mar | 170 | throw new OIDplusConfigInitializationException(_L('File %1 is missing. Please run setup again.',$config_file)); |
261 | daniel-mar | 171 | } |
172 | } |
||
173 | } |
||
174 | |||
175 | // Check important config settings |
||
176 | |||
263 | daniel-mar | 177 | if (self::$baseConfig->getValue('CONFIG_VERSION') != 2.1) { |
801 | daniel-mar | 178 | if (strpos($_SERVER['REQUEST_URI'], OIDplus::webpath(null,OIDplus::PATH_RELATIVE).'setup/') !== 0) { |
503 | daniel-mar | 179 | throw new OIDplusConfigInitializationException(_L("The information located in %1 is outdated.",realpath($config_file))); |
180 | } |
||
261 | daniel-mar | 181 | } |
182 | |||
263 | daniel-mar | 183 | if (self::$baseConfig->getValue('SERVER_SECRET', '') === '') { |
801 | daniel-mar | 184 | if (strpos($_SERVER['REQUEST_URI'], OIDplus::webpath(null,OIDplus::PATH_RELATIVE).'setup/') !== 0) { |
503 | daniel-mar | 185 | throw new OIDplusConfigInitializationException(_L("You must set a value for SERVER_SECRET in %1 for the system to operate secure.",realpath($config_file))); |
186 | } |
||
261 | daniel-mar | 187 | } |
188 | } |
||
189 | |||
263 | daniel-mar | 190 | return self::$baseConfig; |
261 | daniel-mar | 191 | } |
192 | |||
263 | daniel-mar | 193 | private static $config = null; |
2 | daniel-mar | 194 | public static function config() { |
263 | daniel-mar | 195 | if ($first_init = is_null(self::$config)) { |
196 | self::$config = new OIDplusConfig(); |
||
2 | daniel-mar | 197 | } |
263 | daniel-mar | 198 | |
199 | if ($first_init) { |
||
200 | // These are important settings for base functionalities and therefore are not inside plugins |
||
201 | self::$config->prepareConfigKey('system_title', 'What is the name of your RA?', 'OIDplus 2.0', OIDplusConfig::PROTECTION_EDITABLE, function($value) { |
||
202 | if (empty($value)) { |
||
360 | daniel-mar | 203 | throw new OIDplusException(_L('Please enter a value for the system title.')); |
263 | daniel-mar | 204 | } |
205 | }); |
||
206 | self::$config->prepareConfigKey('admin_email', 'E-Mail address of the system administrator', '', OIDplusConfig::PROTECTION_EDITABLE, function($value) { |
||
207 | if (!empty($value) && !OIDplus::mailUtils()->validMailAddress($value)) { |
||
360 | daniel-mar | 208 | throw new OIDplusException(_L('This is not a correct email address')); |
263 | daniel-mar | 209 | } |
210 | }); |
||
875 | daniel-mar | 211 | self::$config->prepareConfigKey('global_cc', 'Global CC for all outgoing emails?', '', OIDplusConfig::PROTECTION_EDITABLE, function(&$value) { |
212 | $value = trim($value); |
||
213 | if ($value === '') return; |
||
214 | $addrs = explode(';', $value); |
||
215 | foreach ($addrs as $addr) { |
||
216 | $addr = trim($addr); |
||
217 | if (!empty($addr) && !OIDplus::mailUtils()->validMailAddress($addr)) { |
||
218 | throw new OIDplusException(_L('%1 is not a correct email address',$addr)); |
||
219 | } |
||
263 | daniel-mar | 220 | } |
221 | }); |
||
875 | daniel-mar | 222 | self::$config->prepareConfigKey('global_bcc', 'Global BCC for all outgoing emails?', '', OIDplusConfig::PROTECTION_EDITABLE, function(&$value) { |
223 | $value = trim($value); |
||
224 | if ($value === '') return; |
||
225 | $addrs = explode(';', $value); |
||
226 | foreach ($addrs as $addr) { |
||
227 | $addr = trim($addr); |
||
228 | if (!empty($addr) && !OIDplus::mailUtils()->validMailAddress($addr)) { |
||
229 | throw new OIDplusException(_L('%1 is not a correct email address',$addr)); |
||
230 | } |
||
231 | } |
||
232 | }); |
||
263 | daniel-mar | 233 | self::$config->prepareConfigKey('objecttypes_initialized', 'List of object type plugins that were initialized once', '', OIDplusConfig::PROTECTION_READONLY, function($value) { |
234 | // Nothing here yet |
||
235 | }); |
||
236 | self::$config->prepareConfigKey('objecttypes_enabled', 'Enabled object types and their order, separated with a semicolon (please reload the page so that the change is applied)', '', OIDplusConfig::PROTECTION_EDITABLE, function($value) { |
||
1050 | daniel-mar | 237 | // TODO: when objecttypes_enabled is changed at the admin control panel, we need to do a reload of the page, so that jsTree will be updated. Is there anything we can do? |
263 | daniel-mar | 238 | |
239 | $ary = explode(';',$value); |
||
240 | $uniq_ary = array_unique($ary); |
||
241 | |||
242 | if (count($ary) != count($uniq_ary)) { |
||
360 | daniel-mar | 243 | throw new OIDplusException(_L('Please check your input. Some object types are double.')); |
263 | daniel-mar | 244 | } |
245 | |||
246 | foreach ($ary as $ot_check) { |
||
247 | $ns_found = false; |
||
248 | foreach (OIDplus::getEnabledObjectTypes() as $ot) { |
||
249 | if ($ot::ns() == $ot_check) { |
||
250 | $ns_found = true; |
||
251 | break; |
||
252 | } |
||
253 | } |
||
254 | foreach (OIDplus::getDisabledObjectTypes() as $ot) { |
||
255 | if ($ot::ns() == $ot_check) { |
||
256 | $ns_found = true; |
||
257 | break; |
||
258 | } |
||
259 | } |
||
260 | if (!$ns_found) { |
||
360 | daniel-mar | 261 | throw new OIDplusException(_L('Please check your input. Namespace "%1" is not found',$ot_check)); |
263 | daniel-mar | 262 | } |
263 | } |
||
264 | }); |
||
265 | self::$config->prepareConfigKey('oidplus_private_key', 'Private key for this system', '', OIDplusConfig::PROTECTION_HIDDEN, function($value) { |
||
266 | // Nothing here yet |
||
267 | }); |
||
268 | self::$config->prepareConfigKey('oidplus_public_key', 'Public key for this system. If you "clone" your system, you must delete this key (e.g. using phpMyAdmin), so that a new one is created.', '', OIDplusConfig::PROTECTION_READONLY, function($value) { |
||
269 | // Nothing here yet |
||
270 | }); |
||
324 | daniel-mar | 271 | self::$config->prepareConfigKey('last_known_system_url', 'Last known System URL', '', OIDplusConfig::PROTECTION_HIDDEN, function($value) { |
272 | // Nothing here yet |
||
273 | }); |
||
412 | daniel-mar | 274 | self::$config->prepareConfigKey('last_known_version', 'Last known OIDplus Version', '', OIDplusConfig::PROTECTION_HIDDEN, function($value) { |
275 | // Nothing here yet |
||
276 | }); |
||
635 | daniel-mar | 277 | self::$config->prepareConfigKey('default_ra_auth_method', 'Default auth method used for generating password of RAs (must exist in plugins/[vendorname]/auth/)?', 'A3_bcrypt', OIDplusConfig::PROTECTION_EDITABLE, function($value) { |
453 | daniel-mar | 278 | $good = true; |
279 | if (strpos($value,'/') !== false) $good = false; |
||
280 | if (strpos($value,'\\') !== false) $good = false; |
||
281 | if (strpos($value,'..') !== false) $good = false; |
||
282 | if (!$good) { |
||
283 | throw new OIDplusException(_L('Invalid auth plugin folder name. Do only enter a folder name, not an absolute or relative path')); |
||
284 | } |
||
285 | |||
926 | daniel-mar | 286 | if (!wildcard_is_dir(OIDplus::localpath().'plugins/'.'*'.'/auth/'.$value)) { |
635 | daniel-mar | 287 | throw new OIDplusException(_L('The auth plugin "%1" does not exist in plugin directory %2',$value,'plugins/[vendorname]/auth/')); |
453 | daniel-mar | 288 | } |
289 | }); |
||
263 | daniel-mar | 290 | } |
291 | |||
292 | return self::$config; |
||
2 | daniel-mar | 293 | } |
294 | |||
263 | daniel-mar | 295 | private static $gui = null; |
2 | daniel-mar | 296 | public static function gui() { |
263 | daniel-mar | 297 | if (is_null(self::$gui)) { |
298 | self::$gui = new OIDplusGui(); |
||
86 | daniel-mar | 299 | } |
263 | daniel-mar | 300 | return self::$gui; |
2 | daniel-mar | 301 | } |
302 | |||
263 | daniel-mar | 303 | private static $authUtils = null; |
2 | daniel-mar | 304 | public static function authUtils() { |
263 | daniel-mar | 305 | if (is_null(self::$authUtils)) { |
306 | self::$authUtils = new OIDplusAuthUtils(); |
||
86 | daniel-mar | 307 | } |
263 | daniel-mar | 308 | return self::$authUtils; |
2 | daniel-mar | 309 | } |
310 | |||
263 | daniel-mar | 311 | private static $mailUtils = null; |
250 | daniel-mar | 312 | public static function mailUtils() { |
263 | daniel-mar | 313 | if (is_null(self::$mailUtils)) { |
314 | self::$mailUtils = new OIDplusMailUtils(); |
||
250 | daniel-mar | 315 | } |
263 | daniel-mar | 316 | return self::$mailUtils; |
250 | daniel-mar | 317 | } |
318 | |||
557 | daniel-mar | 319 | private static $cookieUtils = null; |
320 | public static function cookieUtils() { |
||
321 | if (is_null(self::$cookieUtils)) { |
||
322 | self::$cookieUtils = new OIDplusCookieUtils(); |
||
323 | } |
||
324 | return self::$cookieUtils; |
||
325 | } |
||
326 | |||
263 | daniel-mar | 327 | private static $menuUtils = null; |
250 | daniel-mar | 328 | public static function menuUtils() { |
263 | daniel-mar | 329 | if (is_null(self::$menuUtils)) { |
330 | self::$menuUtils = new OIDplusMenuUtils(); |
||
250 | daniel-mar | 331 | } |
263 | daniel-mar | 332 | return self::$menuUtils; |
250 | daniel-mar | 333 | } |
334 | |||
263 | daniel-mar | 335 | private static $logger = null; |
115 | daniel-mar | 336 | public static function logger() { |
263 | daniel-mar | 337 | if (is_null(self::$logger)) { |
338 | self::$logger = new OIDplusLogger(); |
||
115 | daniel-mar | 339 | } |
263 | daniel-mar | 340 | return self::$logger; |
115 | daniel-mar | 341 | } |
342 | |||
1050 | daniel-mar | 343 | // --- SQL slang plugin |
274 | daniel-mar | 344 | |
345 | private static function registerSqlSlangPlugin(OIDplusSqlSlangPlugin $plugin) { |
||
346 | $name = $plugin::id(); |
||
591 | daniel-mar | 347 | if ($name === '') return false; |
274 | daniel-mar | 348 | |
449 | daniel-mar | 349 | if (isset(self::$sqlSlangPlugins[$name])) { |
451 | daniel-mar | 350 | $plugintype_hf = _L('SQL slang'); |
592 | daniel-mar | 351 | throw new OIDplusException(_L('Multiple %1 plugins use the ID %2', $plugintype_hf, $name)); |
449 | daniel-mar | 352 | } |
353 | |||
274 | daniel-mar | 354 | self::$sqlSlangPlugins[$name] = $plugin; |
355 | |||
356 | return true; |
||
357 | } |
||
358 | |||
359 | public static function getSqlSlangPlugins() { |
||
360 | return self::$sqlSlangPlugins; |
||
361 | } |
||
362 | |||
318 | daniel-mar | 363 | public static function getSqlSlangPlugin($id)/*: ?OIDplusSqlSlangPlugin*/ { |
364 | if (isset(self::$sqlSlangPlugins[$id])) { |
||
365 | return self::$sqlSlangPlugins[$id]; |
||
366 | } else { |
||
367 | return null; |
||
368 | } |
||
369 | } |
||
370 | |||
1050 | daniel-mar | 371 | // --- Database plugin |
74 | daniel-mar | 372 | |
227 | daniel-mar | 373 | private static function registerDatabasePlugin(OIDplusDatabasePlugin $plugin) { |
275 | daniel-mar | 374 | $name = $plugin::id(); |
591 | daniel-mar | 375 | if ($name === '') return false; |
150 | daniel-mar | 376 | |
449 | daniel-mar | 377 | if (isset(self::$dbPlugins[$name])) { |
378 | $plugintype_hf = _L('Database'); |
||
592 | daniel-mar | 379 | throw new OIDplusException(_L('Multiple %1 plugins use the ID %2', $plugintype_hf, $name)); |
449 | daniel-mar | 380 | } |
381 | |||
150 | daniel-mar | 382 | self::$dbPlugins[$name] = $plugin; |
383 | |||
384 | return true; |
||
385 | } |
||
386 | |||
387 | public static function getDatabasePlugins() { |
||
388 | return self::$dbPlugins; |
||
389 | } |
||
390 | |||
295 | daniel-mar | 391 | public static function getActiveDatabasePlugin() { |
702 | daniel-mar | 392 | $db_plugin_name = OIDplus::baseConfig()->getValue('DATABASE_PLUGIN',''); |
393 | if ($db_plugin_name === '') { |
||
360 | daniel-mar | 394 | throw new OIDplusConfigInitializationException(_L('No database plugin selected in config file')); |
260 | daniel-mar | 395 | } |
1016 | daniel-mar | 396 | foreach (self::$dbPlugins as $name => $plugin) { |
397 | if (strtolower($name) == strtolower($db_plugin_name)) { |
||
398 | return $plugin; |
||
399 | } |
||
227 | daniel-mar | 400 | } |
1016 | daniel-mar | 401 | throw new OIDplusConfigInitializationException(_L('Database plugin "%1" not found',$db_plugin_name)); |
227 | daniel-mar | 402 | } |
403 | |||
295 | daniel-mar | 404 | private static $dbMainSession = null; |
405 | public static function db() { |
||
406 | if (is_null(self::$dbMainSession)) { |
||
407 | self::$dbMainSession = self::getActiveDatabasePlugin()->newConnection(); |
||
408 | } |
||
409 | if (!self::$dbMainSession->isConnected()) self::$dbMainSession->connect(); |
||
410 | return self::$dbMainSession; |
||
411 | } |
||
412 | |||
413 | private static $dbIsolatedSession = null; |
||
414 | public static function dbIsolated() { |
||
415 | if (is_null(self::$dbIsolatedSession)) { |
||
416 | self::$dbIsolatedSession = self::getActiveDatabasePlugin()->newConnection(); |
||
417 | } |
||
418 | if (!self::$dbIsolatedSession->isConnected()) self::$dbIsolatedSession->connect(); |
||
419 | return self::$dbIsolatedSession; |
||
420 | } |
||
421 | |||
1050 | daniel-mar | 422 | // --- CAPTCHA plugin |
702 | daniel-mar | 423 | |
424 | private static function registerCaptchaPlugin(OIDplusCaptchaPlugin $plugin) { |
||
425 | $name = $plugin::id(); |
||
426 | if ($name === '') return false; |
||
427 | |||
428 | if (isset(self::$captchaPlugins[$name])) { |
||
429 | $plugintype_hf = _L('CAPTCHA'); |
||
430 | throw new OIDplusException(_L('Multiple %1 plugins use the ID %2', $plugintype_hf, $name)); |
||
431 | } |
||
432 | |||
433 | self::$captchaPlugins[$name] = $plugin; |
||
434 | |||
435 | return true; |
||
436 | } |
||
437 | |||
438 | public static function getCaptchaPlugins() { |
||
439 | return self::$captchaPlugins; |
||
440 | } |
||
441 | |||
704 | daniel-mar | 442 | public static function getActiveCaptchaPluginId() { |
702 | daniel-mar | 443 | $captcha_plugin_name = OIDplus::baseConfig()->getValue('CAPTCHA_PLUGIN', ''); |
444 | |||
445 | if (OIDplus::baseConfig()->getValue('RECAPTCHA_ENABLED', false) && ($captcha_plugin_name === '')) { |
||
446 | // Legacy config file support! |
||
1016 | daniel-mar | 447 | $captcha_plugin_name = 'reCAPTCHA'; |
702 | daniel-mar | 448 | } |
449 | |||
704 | daniel-mar | 450 | if ($captcha_plugin_name === '') $captcha_plugin_name = 'None'; // the "None" plugin is a must-have! |
702 | daniel-mar | 451 | |
704 | daniel-mar | 452 | return $captcha_plugin_name; |
453 | } |
||
454 | |||
455 | public static function getActiveCaptchaPlugin() { |
||
456 | $captcha_plugin_name = OIDplus::getActiveCaptchaPluginId(); |
||
1016 | daniel-mar | 457 | foreach (self::$captchaPlugins as $name => $plugin) { |
458 | if (strtolower($name) == strtolower($captcha_plugin_name)) { |
||
459 | return $plugin; |
||
460 | } |
||
702 | daniel-mar | 461 | } |
1016 | daniel-mar | 462 | throw new OIDplusConfigInitializationException(_L('CAPTCHA plugin "%1" not found',$captcha_plugin_name)); |
702 | daniel-mar | 463 | } |
464 | |||
1050 | daniel-mar | 465 | // --- Page plugin |
227 | daniel-mar | 466 | |
224 | daniel-mar | 467 | private static function registerPagePlugin(OIDplusPagePlugin $plugin) { |
281 | daniel-mar | 468 | self::$pagePlugins[] = $plugin; |
61 | daniel-mar | 469 | |
470 | return true; |
||
471 | } |
||
472 | |||
281 | daniel-mar | 473 | public static function getPagePlugins() { |
474 | return self::$pagePlugins; |
||
61 | daniel-mar | 475 | } |
476 | |||
1050 | daniel-mar | 477 | // --- Auth plugin |
227 | daniel-mar | 478 | |
479 | private static function registerAuthPlugin(OIDplusAuthPlugin $plugin) { |
||
456 | daniel-mar | 480 | if (OIDplus::baseConfig()->getValue('DEBUG')) { |
481 | $password = generateRandomString(25); |
||
459 | daniel-mar | 482 | |
461 | daniel-mar | 483 | try { |
484 | $authInfo = $plugin->generate($password); |
||
485 | } catch (OIDplusException $e) { |
||
486 | // This can happen when the AuthKey or Salt is too long |
||
487 | throw new OIDplusException(_L('Auth plugin "%1" is erroneous: %2',basename($plugin->getPluginDirectory()),$e->getMessage())); |
||
488 | } |
||
459 | daniel-mar | 489 | $salt = $authInfo->getSalt(); |
461 | daniel-mar | 490 | $authKey = $authInfo->getAuthKey(); |
459 | daniel-mar | 491 | |
461 | daniel-mar | 492 | $authInfo_SaltDiff = clone $authInfo; |
493 | $authInfo_SaltDiff->setSalt(strrev($authInfo_SaltDiff->getSalt())); |
||
494 | |||
495 | $authInfo_AuthKeyDiff = clone $authInfo; |
||
496 | $authInfo_AuthKeyDiff->setAuthKey(strrev($authInfo_AuthKeyDiff->getAuthKey())); |
||
497 | |||
498 | if ((!$plugin->verify($authInfo,$password)) || |
||
499 | (!empty($salt) && $plugin->verify($authInfo_SaltDiff,$password)) || |
||
500 | ($plugin->verify($authInfo_AuthKeyDiff,$password)) || |
||
501 | ($plugin->verify($authInfo,$password.'x'))) { |
||
456 | daniel-mar | 502 | throw new OIDplusException(_L('Auth plugin "%1" is erroneous: Generate/Verify self test failed',basename($plugin->getPluginDirectory()))); |
503 | } |
||
453 | daniel-mar | 504 | } |
505 | |||
227 | daniel-mar | 506 | self::$authPlugins[] = $plugin; |
507 | return true; |
||
508 | } |
||
509 | |||
221 | daniel-mar | 510 | public static function getAuthPlugins() { |
511 | return self::$authPlugins; |
||
512 | } |
||
513 | |||
1050 | daniel-mar | 514 | // --- Language plugin |
355 | daniel-mar | 515 | |
516 | private static function registerLanguagePlugin(OIDplusLanguagePlugin $plugin) { |
||
517 | self::$languagePlugins[] = $plugin; |
||
518 | return true; |
||
519 | } |
||
520 | |||
521 | public static function getLanguagePlugins() { |
||
522 | return self::$languagePlugins; |
||
523 | } |
||
524 | |||
1050 | daniel-mar | 525 | // --- Design plugin |
449 | daniel-mar | 526 | |
527 | private static function registerDesignPlugin(OIDplusDesignPlugin $plugin) { |
||
528 | self::$designPlugins[] = $plugin; |
||
529 | return true; |
||
530 | } |
||
531 | |||
532 | public static function getDesignPlugins() { |
||
533 | return self::$designPlugins; |
||
534 | } |
||
535 | |||
819 | daniel-mar | 536 | public static function getActiveDesignPlugin() { |
537 | $plugins = OIDplus::getDesignPlugins(); |
||
538 | foreach ($plugins as $plugin) { |
||
539 | if ((basename($plugin->getPluginDirectory())) == OIDplus::config()->getValue('design','default')) { |
||
540 | return $plugin; |
||
541 | } |
||
542 | } |
||
543 | return null; |
||
544 | } |
||
545 | |||
1050 | daniel-mar | 546 | // --- Logger plugin |
289 | daniel-mar | 547 | |
548 | private static function registerLoggerPlugin(OIDplusLoggerPlugin $plugin) { |
||
549 | self::$loggerPlugins[] = $plugin; |
||
550 | return true; |
||
551 | } |
||
552 | |||
553 | public static function getLoggerPlugins() { |
||
554 | return self::$loggerPlugins; |
||
555 | } |
||
556 | |||
1050 | daniel-mar | 557 | // --- Object type plugin |
227 | daniel-mar | 558 | |
559 | private static function registerObjectTypePlugin(OIDplusObjectTypePlugin $plugin) { |
||
560 | self::$objectTypePlugins[] = $plugin; |
||
561 | |||
562 | $ot = $plugin::getObjectTypeClassName(); |
||
563 | self::registerObjectType($ot); |
||
564 | |||
565 | return true; |
||
566 | } |
||
567 | |||
224 | daniel-mar | 568 | private static function registerObjectType($ot) { |
66 | daniel-mar | 569 | $ns = $ot::ns(); |
860 | daniel-mar | 570 | if (empty($ns)) throw new OIDplusException(_L('ObjectType plugin %1 is erroneous: Namespace must not be empty',$ot)); |
66 | daniel-mar | 571 | |
860 | daniel-mar | 572 | // Currently, we must enforce that namespaces in objectType plugins are lowercase, because prefilterQuery() makes all namespaces lowercase and the DBMS should be case-sensitive |
573 | if ($ns != strtolower($ns)) throw new OIDplusException(_L('ObjectType plugin %1 is erroneous: Namespace %2 must be lower-case',$ot,$ns)); |
||
66 | daniel-mar | 574 | |
860 | daniel-mar | 575 | $root = $ot::root(); |
576 | if (!str_starts_with($root,$ns.':')) throw new OIDplusException(_L('ObjectType plugin %1 is erroneous: Root node (%2) is in wrong namespace (needs starts with %3)!',$ot,$root,$ns.':')); |
||
577 | |||
66 | daniel-mar | 578 | $ns_found = false; |
227 | daniel-mar | 579 | foreach (array_merge(OIDplus::getEnabledObjectTypes(), OIDplus::getDisabledObjectTypes()) as $test_ot) { |
66 | daniel-mar | 580 | if ($test_ot::ns() == $ns) { |
581 | $ns_found = true; |
||
582 | break; |
||
583 | } |
||
584 | } |
||
585 | if ($ns_found) { |
||
360 | daniel-mar | 586 | throw new OIDplusException(_L('Attention: Two objectType plugins use the same namespace "%1"!',$ns)); |
66 | daniel-mar | 587 | } |
588 | |||
589 | $init = OIDplus::config()->getValue("objecttypes_initialized"); |
||
590 | $init_ary = empty($init) ? array() : explode(';', $init); |
||
70 | daniel-mar | 591 | $init_ary = array_map('trim', $init_ary); |
66 | daniel-mar | 592 | |
593 | $enabled = OIDplus::config()->getValue("objecttypes_enabled"); |
||
594 | $enabled_ary = empty($enabled) ? array() : explode(';', $enabled); |
||
70 | daniel-mar | 595 | $enabled_ary = array_map('trim', $enabled_ary); |
66 | daniel-mar | 596 | |
79 | daniel-mar | 597 | $do_enable = false; |
598 | if (in_array($ns, $enabled_ary)) { |
||
447 | daniel-mar | 599 | // If it is in the list of enabled object types, it is enabled (obviously) |
79 | daniel-mar | 600 | $do_enable = true; |
601 | } else { |
||
447 | daniel-mar | 602 | if (!OIDplus::config()->getValue('oobe_objects_done')) { |
603 | // If the OOBE wizard is NOT done, then just enable the "oid" object type by default |
||
79 | daniel-mar | 604 | $do_enable = $ns == 'oid'; |
605 | } else { |
||
447 | daniel-mar | 606 | // If the OOBE wizard was done (once), then |
607 | // we will enable all object types which were never initialized |
||
608 | // (i.e. a plugin folder was freshly added) |
||
79 | daniel-mar | 609 | $do_enable = !in_array($ns, $init_ary); |
610 | } |
||
611 | } |
||
612 | |||
613 | if ($do_enable) { |
||
227 | daniel-mar | 614 | self::$enabledObjectTypes[] = $ot; |
615 | usort(self::$enabledObjectTypes, function($a, $b) { |
||
66 | daniel-mar | 616 | $enabled = OIDplus::config()->getValue("objecttypes_enabled"); |
617 | $enabled_ary = explode(';', $enabled); |
||
618 | |||
619 | $idx_a = array_search($a::ns(), $enabled_ary); |
||
620 | $idx_b = array_search($b::ns(), $enabled_ary); |
||
621 | |||
622 | if ($idx_a == $idx_b) { |
||
623 | return 0; |
||
624 | } |
||
625 | return ($idx_a > $idx_b) ? +1 : -1; |
||
626 | }); |
||
74 | daniel-mar | 627 | } else { |
628 | self::$disabledObjectTypes[] = $ot; |
||
66 | daniel-mar | 629 | } |
630 | |||
631 | if (!in_array($ns, $init_ary)) { |
||
632 | // Was never initialized before, so we add it to the list of enabled object types once |
||
633 | |||
79 | daniel-mar | 634 | if ($do_enable) { |
635 | $enabled_ary[] = $ns; |
||
672 | daniel-mar | 636 | // Important: Don't validate the input, because the other object types might not be initialized yet! So use setValueNoCallback() instead setValue(). |
637 | OIDplus::config()->setValueNoCallback("objecttypes_enabled", implode(';', $enabled_ary)); |
||
79 | daniel-mar | 638 | } |
66 | daniel-mar | 639 | |
640 | $init_ary[] = $ns; |
||
641 | OIDplus::config()->setValue("objecttypes_initialized", implode(';', $init_ary)); |
||
642 | } |
||
61 | daniel-mar | 643 | } |
644 | |||
227 | daniel-mar | 645 | public static function getObjectTypePlugins() { |
646 | return self::$objectTypePlugins; |
||
61 | daniel-mar | 647 | } |
648 | |||
227 | daniel-mar | 649 | public static function getObjectTypePluginsEnabled() { |
650 | $res = array(); |
||
651 | foreach (self::$objectTypePlugins as $plugin) { |
||
652 | $ot = $plugin::getObjectTypeClassName(); |
||
653 | if (in_array($ot, self::$enabledObjectTypes)) $res[] = $plugin; |
||
654 | } |
||
655 | return $res; |
||
74 | daniel-mar | 656 | } |
657 | |||
227 | daniel-mar | 658 | public static function getObjectTypePluginsDisabled() { |
659 | $res = array(); |
||
660 | foreach (self::$objectTypePlugins as $plugin) { |
||
661 | $ot = $plugin::getObjectTypeClassName(); |
||
662 | if (in_array($ot, self::$disabledObjectTypes)) $res[] = $plugin; |
||
74 | daniel-mar | 663 | } |
227 | daniel-mar | 664 | return $res; |
74 | daniel-mar | 665 | } |
666 | |||
227 | daniel-mar | 667 | public static function getEnabledObjectTypes() { |
668 | return self::$enabledObjectTypes; |
||
669 | } |
||
74 | daniel-mar | 670 | |
227 | daniel-mar | 671 | public static function getDisabledObjectTypes() { |
672 | return self::$disabledObjectTypes; |
||
673 | } |
||
74 | daniel-mar | 674 | |
1050 | daniel-mar | 675 | // --- Plugin handling functions |
277 | daniel-mar | 676 | |
320 | daniel-mar | 677 | public static function getAllPlugins()/*: array*/ { |
678 | $res = array(); |
||
679 | $res = array_merge($res, self::$pagePlugins); |
||
680 | $res = array_merge($res, self::$authPlugins); |
||
681 | $res = array_merge($res, self::$loggerPlugins); |
||
682 | $res = array_merge($res, self::$objectTypePlugins); |
||
683 | $res = array_merge($res, self::$dbPlugins); |
||
702 | daniel-mar | 684 | $res = array_merge($res, self::$captchaPlugins); |
320 | daniel-mar | 685 | $res = array_merge($res, self::$sqlSlangPlugins); |
355 | daniel-mar | 686 | $res = array_merge($res, self::$languagePlugins); |
449 | daniel-mar | 687 | $res = array_merge($res, self::$designPlugins); |
320 | daniel-mar | 688 | return $res; |
689 | } |
||
690 | |||
321 | daniel-mar | 691 | public static function getPluginByOid($oid)/*: ?OIDplusPlugin*/ { |
320 | daniel-mar | 692 | $plugins = self::getAllPlugins(); |
321 | daniel-mar | 693 | foreach ($plugins as $plugin) { |
694 | if (oid_dotnotation_equal($plugin->getManifest()->getOid(), $oid)) { |
||
695 | return $plugin; |
||
320 | daniel-mar | 696 | } |
697 | } |
||
698 | return null; |
||
699 | } |
||
700 | |||
380 | daniel-mar | 701 | public static function getPluginByClassName($classname)/*: ?OIDplusPlugin*/ { |
702 | $plugins = self::getAllPlugins(); |
||
703 | foreach ($plugins as $plugin) { |
||
704 | if (get_class($plugin) === $classname) { |
||
705 | return $plugin; |
||
706 | } |
||
707 | } |
||
708 | return null; |
||
277 | daniel-mar | 709 | } |
710 | |||
594 | daniel-mar | 711 | /** |
1050 | daniel-mar | 712 | * Checks if the plugin is disabled |
713 | * @return boolean true if plugin is enabled, false if plugin is disabled |
||
714 | * @throws OIDplusException if the class name or config file (disabled setting) does not contain a namespace |
||
715 | */ |
||
716 | private static function pluginCheckDisabled($class_name): bool { |
||
717 | $path = explode('\\', $class_name); |
||
718 | |||
719 | if (count($path) == 1) { |
||
720 | throw new OIDplusException(_L('Plugin "%1" is erroneous',$class_name).': '._L('The plugin uses no namespaces. The new version of OIDplus requires plugin class files to be in a namespace. Please notify your plugin author and ask for an update.')); |
||
721 | } |
||
722 | |||
723 | $class_end = end($path); |
||
724 | if (OIDplus::baseConfig()->getValue('DISABLE_PLUGIN_'.$class_end, false)) { |
||
725 | throw new OIDplusConfigInitializationException(_L('Your base configuration file is outdated. Please change "%1" to "%2".','DISABLE_PLUGIN_'.$class_end,'DISABLE_PLUGIN_'.$class_name)); |
||
726 | } |
||
727 | |||
728 | if (OIDplus::baseConfig()->getValue('DISABLE_PLUGIN_'.$class_name, false)) { |
||
729 | return false; |
||
730 | } |
||
731 | |||
732 | return true; |
||
733 | } |
||
734 | |||
735 | /** |
||
594 | daniel-mar | 736 | * @return array<OIDplusPluginManifest>|array<string,array<string,OIDplusPluginManifest>> |
737 | */ |
||
693 | daniel-mar | 738 | public static function getAllPluginManifests($pluginFolderMasks='*', $flat=true): array { |
277 | daniel-mar | 739 | $out = array(); |
279 | daniel-mar | 740 | // Note: glob() will sort by default, so we do not need a page priority attribute. |
741 | // So you just need to use a numeric plugin directory prefix (padded). |
||
693 | daniel-mar | 742 | $ary = array(); |
743 | foreach (explode(',',$pluginFolderMasks) as $pluginFolderMask) { |
||
744 | $ary = array_merge($ary,glob(OIDplus::localpath().'plugins/'.'*'.'/'.$pluginFolderMask.'/'.'*'.'/manifest.xml')); |
||
745 | } |
||
646 | daniel-mar | 746 | |
747 | // Sort the plugins by their type and name, as if they would be in a single vendor-folder! |
||
748 | uasort($ary, function($a,$b) { |
||
749 | if ($a == $b) return 0; |
||
750 | |||
751 | $ary = explode('/',$a); |
||
752 | $bry = explode('/',$b); |
||
753 | |||
754 | // First sort by type (publicPage, auth, database, language, ...) |
||
755 | $a_type = $ary[count($ary)-1-2]; |
||
756 | $b_type = $bry[count($bry)-1-2]; |
||
757 | if ($a_type < $b_type) return -1; |
||
758 | if ($a_type > $b_type) return 1; |
||
759 | |||
760 | // Then sort by name (090_login, 100_whois, etc.) |
||
761 | $a_name = $ary[count($ary)-1-1]; |
||
762 | $b_name = $bry[count($bry)-1-1]; |
||
763 | if ($a_name < $b_name) return -1; |
||
764 | if ($a_name > $b_name) return 1; |
||
765 | |||
766 | // If it is still equal, then finally sort by vendorname |
||
767 | $a_vendor = $ary[count($ary)-1-3]; |
||
768 | $b_vendor = $bry[count($bry)-1-3]; |
||
769 | if ($a_vendor < $b_vendor) return -1; |
||
770 | if ($a_vendor > $b_vendor) return 1; |
||
771 | return 0; |
||
772 | }); |
||
773 | |||
277 | daniel-mar | 774 | foreach ($ary as $ini) { |
775 | if (!file_exists($ini)) continue; |
||
776 | |||
307 | daniel-mar | 777 | $manifest = new OIDplusPluginManifest(); |
778 | $manifest->loadManifest($ini); |
||
277 | daniel-mar | 779 | |
473 | daniel-mar | 780 | $class_name = $manifest->getPhpMainClass(); |
1050 | daniel-mar | 781 | if ($class_name) if (!self::pluginCheckDisabled($class_name)) continue; |
473 | daniel-mar | 782 | |
307 | daniel-mar | 783 | if ($flat) { |
784 | $out[] = $manifest; |
||
785 | } else { |
||
786 | $plugintype_folder = basename(dirname(dirname($ini))); |
||
787 | $pluginname_folder = basename(dirname($ini)); |
||
788 | |||
789 | if (!isset($out[$plugintype_folder])) $out[$plugintype_folder] = array(); |
||
790 | if (!isset($out[$plugintype_folder][$pluginname_folder])) $out[$plugintype_folder][$pluginname_folder] = array(); |
||
791 | $out[$plugintype_folder][$pluginname_folder] = $manifest; |
||
792 | } |
||
277 | daniel-mar | 793 | } |
794 | return $out; |
||
795 | } |
||
796 | |||
594 | daniel-mar | 797 | /** |
798 | * @return array<string> |
||
799 | */ |
||
277 | daniel-mar | 800 | public static function registerAllPlugins($pluginDirName, $expectedPluginClass, $registerCallback): array { |
801 | $out = array(); |
||
525 | daniel-mar | 802 | if (is_array($pluginDirName)) { |
803 | $ary = array(); |
||
804 | foreach ($pluginDirName as $pluginDirName_) { |
||
805 | $ary = array_merge($ary, self::getAllPluginManifests($pluginDirName_, false)); |
||
806 | } |
||
807 | } else { |
||
808 | $ary = self::getAllPluginManifests($pluginDirName, false); |
||
809 | } |
||
320 | daniel-mar | 810 | $known_plugin_oids = array(); |
456 | daniel-mar | 811 | if (OIDplus::baseConfig()->getValue('DEBUG')) { |
812 | $fake_feature = uuid_to_oid(gen_uuid()); |
||
588 | daniel-mar | 813 | } else { |
814 | $fake_feature = null; |
||
456 | daniel-mar | 815 | } |
277 | daniel-mar | 816 | foreach ($ary as $plugintype_folder => $bry) { |
438 | daniel-mar | 817 | foreach ($bry as $pluginname_folder => $manifest) { |
818 | $class_name = $manifest->getPhpMainClass(); |
||
819 | |||
820 | // Before we load the plugin, we want to make some checks to confirm |
||
821 | // that the plugin is working correctly. |
||
822 | |||
307 | daniel-mar | 823 | if (!$class_name) { |
1050 | daniel-mar | 824 | throw new OIDplusException(_L('Plugin "%1" is erroneous',$plugintype_folder.'/'.$pluginname_folder).': '._L('Manifest does not declare a PHP main class')); |
277 | daniel-mar | 825 | } |
1050 | daniel-mar | 826 | if (!self::pluginCheckDisabled($class_name)) { |
827 | continue; // Plugin is disabled |
||
297 | daniel-mar | 828 | } |
292 | daniel-mar | 829 | if (!class_exists($class_name)) { |
1050 | daniel-mar | 830 | throw new OIDplusException(_L('Plugin "%1" is erroneous',$plugintype_folder.'/'.$pluginname_folder).': '._L('Manifest declares PHP main class as "%1", but it could not be found',$class_name)); |
292 | daniel-mar | 831 | } |
279 | daniel-mar | 832 | if (!is_subclass_of($class_name, $expectedPluginClass)) { |
1050 | daniel-mar | 833 | throw new OIDplusException(_L('Plugin "%1" is erroneous',$plugintype_folder.'/'.$pluginname_folder).': '._L('Plugin main class "%1" is expected to be a subclass of "%2"',$class_name,$expectedPluginClass)); |
279 | daniel-mar | 834 | } |
438 | daniel-mar | 835 | if (($class_name!=$manifest->getTypeClass()) && (!is_subclass_of($class_name,$manifest->getTypeClass()))) { |
1050 | daniel-mar | 836 | throw new OIDplusException(_L('Plugin "%1" is erroneous',$plugintype_folder.'/'.$pluginname_folder).': '._L('Plugin main class "%1" is expected to be a subclass of "%2", according to type declared in manifest',$class_name,$manifest->getTypeClass())); |
308 | daniel-mar | 837 | } |
438 | daniel-mar | 838 | if (($manifest->getTypeClass()!=$expectedPluginClass) && (!is_subclass_of($manifest->getTypeClass(),$expectedPluginClass))) { |
1050 | daniel-mar | 839 | throw new OIDplusException(_L('Plugin "%1" is erroneous',$plugintype_folder.'/'.$pluginname_folder).': '._L('Class declared in manifest is "%1" does not fit expected class for this plugin type "%2"',$manifest->getTypeClass(),$expectedPluginClass)); |
308 | daniel-mar | 840 | } |
841 | |||
438 | daniel-mar | 842 | $plugin_oid = $manifest->getOid(); |
320 | daniel-mar | 843 | if (!$plugin_oid) { |
1050 | daniel-mar | 844 | throw new OIDplusException(_L('Plugin "%1" is erroneous',$plugintype_folder.'/'.$pluginname_folder).': '._L('Does not have an OID')); |
320 | daniel-mar | 845 | } |
846 | if (!oid_valid_dotnotation($plugin_oid, false, false, 2)) { |
||
1050 | daniel-mar | 847 | throw new OIDplusException(_L('Plugin "%1" is erroneous',$plugintype_folder.'/'.$pluginname_folder).': '._L('Plugin OID "%1" is invalid (needs to be valid dot-notation)',$plugin_oid)); |
320 | daniel-mar | 848 | } |
849 | if (isset($known_plugin_oids[$plugin_oid])) { |
||
1050 | daniel-mar | 850 | throw new OIDplusException(_L('Plugin "%1" is erroneous',$plugintype_folder.'/'.$pluginname_folder).': '._L('The OID "%1" is already used by the plugin "%2"',$plugin_oid,$known_plugin_oids[$plugin_oid])); |
320 | daniel-mar | 851 | } |
646 | daniel-mar | 852 | |
632 | daniel-mar | 853 | $full_plugin_dir = dirname($manifest->getManifestFile()); |
854 | $full_plugin_dir = substr($full_plugin_dir, strlen(OIDplus::localpath())); |
||
988 | daniel-mar | 855 | |
856 | $dir_is_viathinksoft = str_starts_with($full_plugin_dir, 'plugins/viathinksoft/') || str_starts_with($full_plugin_dir, 'plugins\\viathinksoft\\'); |
||
857 | $oid_is_viathinksoft = str_starts_with($plugin_oid, '1.3.6.1.4.1.37476.2.5.2.4.'); // { iso(1) identified-organization(3) dod(6) internet(1) private(4) enterprise(1) 37476 products(2) oidplus(5) v2(2) plugins(4) } |
||
1050 | daniel-mar | 858 | $class_is_viathinksoft = str_starts_with($class_name, 'ViaThinkSoft\\'); |
988 | daniel-mar | 859 | if ($dir_is_viathinksoft != $oid_is_viathinksoft) { |
1050 | daniel-mar | 860 | throw new OIDplusException(_L('Plugin "%1" is misplaced',$plugintype_folder.'/'.$pluginname_folder).': '._L('The plugin is in the wrong folder. The folder %1 can only be used by official ViaThinkSoft plugins','plugins/viathinksoft/')); |
632 | daniel-mar | 861 | } |
1050 | daniel-mar | 862 | if ($dir_is_viathinksoft != $class_is_viathinksoft) { |
863 | throw new OIDplusException(_L('Plugin "%1" is erroneous',$plugintype_folder.'/'.$pluginname_folder).': '._L('Third-party plugins must not use the ViaThinkSoft PHP namespace. Please use your own vendor namespace.')); |
||
864 | } |
||
320 | daniel-mar | 865 | |
632 | daniel-mar | 866 | $known_plugin_oids[$plugin_oid] = $plugintype_folder.'/'.$pluginname_folder; |
867 | |||
438 | daniel-mar | 868 | $obj = new $class_name(); |
456 | daniel-mar | 869 | |
870 | if (OIDplus::baseConfig()->getValue('DEBUG')) { |
||
871 | if ($obj->implementsFeature($fake_feature)) { |
||
872 | // see https://devblogs.microsoft.com/oldnewthing/20040211-00/?p=40663 |
||
1050 | daniel-mar | 873 | throw new OIDplusException(_L('Plugin "%1" is erroneous',$plugintype_folder.'/'.$pluginname_folder).': '._L('implementsFeature() always returns true')); |
456 | daniel-mar | 874 | } |
438 | daniel-mar | 875 | } |
876 | |||
778 | daniel-mar | 877 | // TODO: Maybe as additional plugin-test, we should also check if plugins are allowed to define CSS/JS, i.e. the plugin type is element of OIDplus::INTERACTIVE_PLUGIN_TYPES |
988 | daniel-mar | 878 | $tmp = $manifest->getManifestLinkedFiles(); |
438 | daniel-mar | 879 | foreach ($tmp as $file) { |
880 | if (!file_exists($file)) { |
||
1050 | daniel-mar | 881 | throw new OIDplusException(_L('Plugin "%1" is erroneous',$plugintype_folder.'/'.$pluginname_folder).': '._L('File %1 was defined in manifest, but it is not existing',$file)); |
438 | daniel-mar | 882 | } |
883 | } |
||
884 | |||
885 | // Now we can continue |
||
886 | |||
279 | daniel-mar | 887 | $out[] = $class_name; |
888 | if (!is_null($registerCallback)) { |
||
438 | daniel-mar | 889 | call_user_func($registerCallback, $obj); |
444 | daniel-mar | 890 | |
891 | // Alternative approaches: |
||
892 | //$registerCallback[0]::{$registerCallback[1]}($obj); |
||
893 | // or: |
||
894 | //forward_static_call($registerCallback, $obj); |
||
279 | daniel-mar | 895 | } |
277 | daniel-mar | 896 | } |
897 | |||
898 | } |
||
899 | return $out; |
||
900 | } |
||
901 | |||
1050 | daniel-mar | 902 | // --- Initialization of OIDplus |
206 | daniel-mar | 903 | |
374 | daniel-mar | 904 | public static function init($html=true, $keepBaseConfig=true) { |
236 | daniel-mar | 905 | self::$html = $html; |
906 | |||
263 | daniel-mar | 907 | // Reset internal state, so we can re-init verything if required |
274 | daniel-mar | 908 | |
263 | daniel-mar | 909 | self::$config = null; |
374 | daniel-mar | 910 | if (!$keepBaseConfig) self::$baseConfig = null; // for test cases we need to be able to control base config and setting values manually, so $keepBaseConfig needs to be true |
263 | daniel-mar | 911 | self::$gui = null; |
912 | self::$authUtils = null; |
||
913 | self::$mailUtils = null; |
||
914 | self::$menuUtils = null; |
||
915 | self::$logger = null; |
||
295 | daniel-mar | 916 | self::$dbMainSession = null; |
917 | self::$dbIsolatedSession = null; |
||
263 | daniel-mar | 918 | self::$pagePlugins = array(); |
919 | self::$authPlugins = array(); |
||
289 | daniel-mar | 920 | self::$loggerPlugins = array(); |
263 | daniel-mar | 921 | self::$objectTypePlugins = array(); |
922 | self::$enabledObjectTypes = array(); |
||
923 | self::$disabledObjectTypes = array(); |
||
924 | self::$dbPlugins = array(); |
||
702 | daniel-mar | 925 | self::$captchaPlugins = array(); |
274 | daniel-mar | 926 | self::$sqlSlangPlugins = array(); |
355 | daniel-mar | 927 | self::$languagePlugins = array(); |
449 | daniel-mar | 928 | self::$designPlugins = array(); |
263 | daniel-mar | 929 | self::$system_id_cache = null; |
930 | self::$sslAvailableCache = null; |
||
468 | daniel-mar | 931 | self::$translationArray = array(); |
74 | daniel-mar | 932 | |
263 | daniel-mar | 933 | // Continue... |
934 | |||
294 | daniel-mar | 935 | OIDplus::baseConfig(); // this loads the base configuration located in userdata/baseconfig/config.inc.php (once!) |
263 | daniel-mar | 936 | // You can do changes to the configuration afterwards using OIDplus::baseConfig()->... |
937 | |||
150 | daniel-mar | 938 | // Register database types (highest priority) |
939 | |||
274 | daniel-mar | 940 | // SQL slangs |
941 | |||
1050 | daniel-mar | 942 | self::registerAllPlugins('sqlSlang', OIDplusSqlSlangPlugin::class, array(OIDplus::class,'registerSqlSlangPlugin')); |
274 | daniel-mar | 943 | foreach (OIDplus::getSqlSlangPlugins() as $plugin) { |
944 | $plugin->init($html); |
||
945 | } |
||
946 | |||
947 | // Database providers |
||
948 | |||
1050 | daniel-mar | 949 | self::registerAllPlugins('database', OIDplusDatabasePlugin::class, array(OIDplus::class,'registerDatabasePlugin')); |
237 | daniel-mar | 950 | foreach (OIDplus::getDatabasePlugins() as $plugin) { |
951 | $plugin->init($html); |
||
952 | } |
||
953 | |||
42 | daniel-mar | 954 | // Do redirect stuff etc. |
74 | daniel-mar | 955 | |
230 | daniel-mar | 956 | self::isSslAvailable(); // This function does automatic redirects |
61 | daniel-mar | 957 | |
263 | daniel-mar | 958 | // Construct the configuration manager |
66 | daniel-mar | 959 | |
263 | daniel-mar | 960 | OIDplus::config(); // During the construction, various system settings are prepared if required |
66 | daniel-mar | 961 | |
74 | daniel-mar | 962 | // Initialize public / private keys |
963 | |||
227 | daniel-mar | 964 | OIDplus::getPkiStatus(true); |
74 | daniel-mar | 965 | |
237 | daniel-mar | 966 | // Register non-DB plugins |
74 | daniel-mar | 967 | |
1050 | daniel-mar | 968 | self::registerAllPlugins(array('publicPages', 'raPages', 'adminPages'), OIDplusPagePlugin::class, array(OIDplus::class,'registerPagePlugin')); |
969 | self::registerAllPlugins('auth', OIDplusAuthPlugin::class, array(OIDplus::class,'registerAuthPlugin')); |
||
970 | self::registerAllPlugins('logger', OIDplusLoggerPlugin::class, array(OIDplus::class,'registerLoggerPlugin')); |
||
825 | daniel-mar | 971 | OIDplusLogger::reLogMissing(); // Some previous plugins might have tried to log. Repeat that now. |
1050 | daniel-mar | 972 | self::registerAllPlugins('objectTypes', OIDplusObjectTypePlugin::class, array(OIDplus::class,'registerObjectTypePlugin')); |
973 | self::registerAllPlugins('language', OIDplusLanguagePlugin::class, array(OIDplus::class,'registerLanguagePlugin')); |
||
974 | self::registerAllPlugins('design', OIDplusDesignPlugin::class, array(OIDplus::class,'registerDesignPlugin')); |
||
975 | self::registerAllPlugins('captcha', OIDplusCaptchaPlugin::class, array(OIDplus::class,'registerCaptchaPlugin')); |
||
150 | daniel-mar | 976 | |
237 | daniel-mar | 977 | // Initialize non-DB plugins |
224 | daniel-mar | 978 | |
281 | daniel-mar | 979 | foreach (OIDplus::getPagePlugins() as $plugin) { |
74 | daniel-mar | 980 | $plugin->init($html); |
981 | } |
||
230 | daniel-mar | 982 | foreach (OIDplus::getAuthPlugins() as $plugin) { |
983 | $plugin->init($html); |
||
984 | } |
||
289 | daniel-mar | 985 | foreach (OIDplus::getLoggerPlugins() as $plugin) { |
986 | $plugin->init($html); |
||
987 | } |
||
230 | daniel-mar | 988 | foreach (OIDplus::getObjectTypePlugins() as $plugin) { |
989 | $plugin->init($html); |
||
990 | } |
||
355 | daniel-mar | 991 | foreach (OIDplus::getLanguagePlugins() as $plugin) { |
992 | $plugin->init($html); |
||
993 | } |
||
449 | daniel-mar | 994 | foreach (OIDplus::getDesignPlugins() as $plugin) { |
995 | $plugin->init($html); |
||
996 | } |
||
778 | daniel-mar | 997 | foreach (OIDplus::getCaptchaPlugins() as $plugin) { |
998 | $plugin->init($html); |
||
999 | } |
||
412 | daniel-mar | 1000 | |
778 | daniel-mar | 1001 | if (PHP_SAPI != 'cli') { |
1002 | |||
1003 | // Prepare some security related response headers (default values) |
||
1004 | |||
1005 | $content_language = |
||
1006 | strtolower(substr(OIDplus::getCurrentLang(),0,2)) . '-' . |
||
1007 | strtoupper(substr(OIDplus::getCurrentLang(),2,2)); // e.g. 'en-US' |
||
1008 | |||
1009 | $http_headers = array( |
||
1010 | "X-Content-Type-Options" => "nosniff", |
||
1011 | "X-XSS-Protection" => "1; mode=block", |
||
1012 | "X-Frame-Options" => "SAMEORIGIN", |
||
1013 | "Referrer-Policy" => array( |
||
1014 | "no-referrer-when-downgrade" |
||
1015 | ), |
||
1016 | "Cache-Control" => array( |
||
1017 | "no-cache", |
||
1018 | "no-store", |
||
1019 | "must-revalidate" |
||
1020 | ), |
||
1021 | "Pragma" => "no-cache", |
||
1022 | "Content-Language" => $content_language, |
||
1023 | "Expires" => "0", |
||
1024 | "Content-Security-Policy" => array( |
||
1001 | daniel-mar | 1025 | // see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy |
1026 | |||
1027 | // --- Fetch directives --- |
||
1028 | "child-src" => array( |
||
1029 | "'self'", |
||
1030 | "blob:" |
||
1031 | ), |
||
1032 | "connect-src" => array( |
||
1033 | "'self'", |
||
1034 | "blob:" |
||
1035 | ), |
||
778 | daniel-mar | 1036 | "default-src" => array( |
1037 | "'self'", |
||
1038 | "blob:", |
||
1039 | "https://cdnjs.cloudflare.com/" |
||
1040 | ), |
||
1001 | daniel-mar | 1041 | "font-src" => array( |
778 | daniel-mar | 1042 | "'self'", |
1001 | daniel-mar | 1043 | "blob:" |
778 | daniel-mar | 1044 | ), |
1001 | daniel-mar | 1045 | "frame-src" => array( |
1046 | "'self'", |
||
1047 | "blob:" |
||
1048 | ), |
||
778 | daniel-mar | 1049 | "img-src" => array( |
1001 | daniel-mar | 1050 | "blob:", |
778 | daniel-mar | 1051 | "data:", |
1052 | "http:", |
||
1053 | "https:" |
||
1054 | ), |
||
1001 | daniel-mar | 1055 | "manifest-src" => array( |
1056 | "'self'", |
||
1057 | "blob:" |
||
1058 | ), |
||
1059 | "media-src" => array( |
||
1060 | "'self'", |
||
1061 | "blob:" |
||
1062 | ), |
||
1063 | "object-src" => array( |
||
1064 | "'none'" |
||
1065 | ), |
||
1066 | "prefetch-src" => array( |
||
1067 | "'self'", |
||
1068 | "blob:" |
||
1069 | ), |
||
778 | daniel-mar | 1070 | "script-src" => array( |
1071 | "'self'", |
||
1072 | "'unsafe-inline'", |
||
1073 | "'unsafe-eval'", |
||
1074 | "blob:", |
||
1075 | "https://cdnjs.cloudflare.com/", |
||
1076 | "https://polyfill.io/" |
||
1077 | ), |
||
1001 | daniel-mar | 1078 | // script-src-elem not used |
1079 | // script-src-attr not used |
||
1080 | "style-src" => array( |
||
1081 | "'self'", |
||
1082 | "'unsafe-inline'", |
||
1083 | "https://cdnjs.cloudflare.com/" |
||
1084 | ), |
||
1085 | // style-src-elem not used |
||
1086 | // style-src-attr not used |
||
1087 | "worker-src" => array( |
||
1088 | "'self'", |
||
1089 | "blob:" |
||
1090 | ), |
||
1091 | |||
1092 | // --- Navigation directives --- |
||
778 | daniel-mar | 1093 | "frame-ancestors" => array( |
1094 | "'none'" |
||
1095 | ), |
||
1096 | ) |
||
1097 | ); |
||
1098 | |||
780 | daniel-mar | 1099 | // Give plugins the opportunity to manipulate/extend the headers |
778 | daniel-mar | 1100 | |
1101 | foreach (OIDplus::getSqlSlangPlugins() as $plugin) { |
||
1102 | $plugin->httpHeaderCheck($http_headers); |
||
1103 | } |
||
1015 | daniel-mar | 1104 | //foreach (OIDplus::getDatabasePlugins() as $plugin) { |
1105 | if ($plugin = OIDplus::getActiveDatabasePlugin()) { |
||
778 | daniel-mar | 1106 | $plugin->httpHeaderCheck($http_headers); |
1107 | } |
||
1108 | foreach (OIDplus::getPagePlugins() as $plugin) { |
||
1109 | $plugin->httpHeaderCheck($http_headers); |
||
1110 | } |
||
1111 | foreach (OIDplus::getAuthPlugins() as $plugin) { |
||
1112 | $plugin->httpHeaderCheck($http_headers); |
||
1113 | } |
||
1114 | foreach (OIDplus::getLoggerPlugins() as $plugin) { |
||
1115 | $plugin->httpHeaderCheck($http_headers); |
||
1116 | } |
||
1117 | foreach (OIDplus::getObjectTypePlugins() as $plugin) { |
||
1118 | $plugin->httpHeaderCheck($http_headers); |
||
1119 | } |
||
1120 | foreach (OIDplus::getLanguagePlugins() as $plugin) { |
||
1121 | $plugin->httpHeaderCheck($http_headers); |
||
1122 | } |
||
1123 | foreach (OIDplus::getDesignPlugins() as $plugin) { |
||
1124 | $plugin->httpHeaderCheck($http_headers); |
||
1125 | } |
||
1015 | daniel-mar | 1126 | //foreach (OIDplus::getCaptchaPlugins() as $plugin) { |
1127 | if ($plugin = OIDplus::getActiveCaptchaPlugin()) { |
||
778 | daniel-mar | 1128 | $plugin->httpHeaderCheck($http_headers); |
1129 | } |
||
1130 | |||
1131 | // Prepare to send the headers to the client |
||
1132 | // The headers are sent automatically when the first output comes or the script ends |
||
1133 | |||
1134 | foreach ($http_headers as $name => $val) { |
||
1135 | |||
1136 | // Plugins can remove standard OIDplus headers by setting the value to null. |
||
1137 | if (is_null($val)) continue; /** @phpstan-ignore-line */ |
||
1138 | |||
1139 | // Some headers can be written as arrays to make it easier for plugin authors |
||
1140 | // to manipulate/extend the contents. |
||
1141 | if (is_array($val)) { |
||
1142 | if ((strtolower($name) == 'cache-control') || |
||
1143 | (strtolower($name) == 'referrer-policy')) |
||
1144 | { |
||
1145 | if (count($val) == 0) continue; |
||
1146 | $val = implode(', ', $val); |
||
1147 | } else if (strtolower($name) == 'content-security-policy') { |
||
1148 | if (count($val) == 0) continue; |
||
780 | daniel-mar | 1149 | foreach ($val as $tmp1 => &$tmp2) { |
1150 | $tmp2 = array_unique($tmp2); |
||
1151 | $tmp2 = $tmp1.' '.implode(' ', $tmp2); |
||
1152 | } |
||
778 | daniel-mar | 1153 | $val = implode('; ', $val); |
1154 | } else { |
||
779 | daniel-mar | 1155 | throw new OIDplusException(_L('HTTP header "%1" cannot be written as array. A newly installed plugin is probably misusing the method "%2".',$name,'httpHeaderCheck')); |
778 | daniel-mar | 1156 | } |
1157 | } |
||
1158 | |||
1159 | if (is_string($val)) { |
||
1160 | header("$name: $val"); |
||
1161 | } |
||
1162 | } |
||
1163 | |||
1164 | } // endif (PHP_SAPI != 'cli') |
||
1165 | |||
412 | daniel-mar | 1166 | // Initialize other stuff (i.e. things which require the logger!) |
1167 | |||
1168 | OIDplus::recognizeSystemUrl(); // Make sure "last_known_system_url" is set |
||
1169 | OIDplus::recognizeVersion(); // Make sure "last_known_version" is set and a log entry is created |
||
2 | daniel-mar | 1170 | } |
42 | daniel-mar | 1171 | |
1050 | daniel-mar | 1172 | // --- System URL, System ID, PKI, and other functions |
227 | daniel-mar | 1173 | |
412 | daniel-mar | 1174 | private static function recognizeSystemUrl() { |
1175 | try { |
||
801 | daniel-mar | 1176 | $url = OIDplus::webpath(null,self::PATH_ABSOLUTE_CANONICAL); // TODO: canonical or not? |
412 | daniel-mar | 1177 | OIDplus::config()->setValue('last_known_system_url', $url); |
1050 | daniel-mar | 1178 | } catch (\Exception $e) { |
412 | daniel-mar | 1179 | } |
1180 | } |
||
497 | daniel-mar | 1181 | |
496 | daniel-mar | 1182 | private static function getExecutingScriptPathDepth() { |
1183 | if (PHP_SAPI == 'cli') { |
||
1184 | global $argv; |
||
1185 | $test_dir = dirname(realpath($argv[0])); |
||
1186 | } else { |
||
1187 | if (!isset($_SERVER["SCRIPT_FILENAME"])) return false; |
||
1188 | $test_dir = dirname($_SERVER['SCRIPT_FILENAME']); |
||
1189 | } |
||
1190 | $test_dir = str_replace('\\', '/', $test_dir); |
||
1191 | $steps_up = 0; |
||
928 | daniel-mar | 1192 | while (!file_exists($test_dir.'/oidplus.min.css.php')) { // We just assume that only the OIDplus base directory contains "oidplus.min.css.php" and not any subordinate directory! |
496 | daniel-mar | 1193 | $test_dir = dirname($test_dir); |
1194 | $steps_up++; |
||
1195 | if ($steps_up == 1000) return false; // to make sure there will never be an infinite loop |
||
1196 | } |
||
1197 | return $steps_up; |
||
1198 | } |
||
632 | daniel-mar | 1199 | |
580 | daniel-mar | 1200 | public static function isSSL() { |
1201 | return isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] === 'on'); |
||
1202 | } |
||
412 | daniel-mar | 1203 | |
806 | daniel-mar | 1204 | /** |
1205 | * Returns the URL of the system. |
||
1206 | * @param int $mode If true or OIDplus::PATH_RELATIVE, the returning path is relative to the currently executed |
||
1207 | * PHP script (i.e. index.php , not the plugin PHP script!). False or OIDplus::PATH_ABSOLUTE is |
||
1208 | * results in an absolute URL. OIDplus::PATH_ABSOLUTE_CANONICAL is an absolute URL, |
||
1209 | * but a canonical path (set by base config setting CANONICAL_SYSTEM_URL) is preferred. |
||
1210 | * @return string|false The URL, with guaranteed trailing path delimiter for directories |
||
1211 | */ |
||
801 | daniel-mar | 1212 | private static function getSystemUrl($mode) { |
806 | daniel-mar | 1213 | if ($mode === self::PATH_RELATIVE) { |
778 | daniel-mar | 1214 | $steps_up = self::getExecutingScriptPathDepth(); |
1215 | if ($steps_up === false) { |
||
1216 | return false; |
||
1217 | } else { |
||
1218 | return str_repeat('../', $steps_up); |
||
1219 | } |
||
1220 | } else { |
||
806 | daniel-mar | 1221 | if ($mode === self::PATH_ABSOLUTE_CANONICAL) { |
1222 | $tmp = OIDplus::baseConfig()->getValue('CANONICAL_SYSTEM_URL', ''); |
||
1223 | if ($tmp) { |
||
1224 | return rtrim($tmp,'/').'/'; |
||
1225 | } |
||
326 | daniel-mar | 1226 | } |
778 | daniel-mar | 1227 | |
495 | daniel-mar | 1228 | if (PHP_SAPI == 'cli') { |
494 | daniel-mar | 1229 | try { |
1230 | return OIDplus::config()->getValue('last_known_system_url', false); |
||
1050 | daniel-mar | 1231 | } catch (\Exception $e) { |
494 | daniel-mar | 1232 | return false; |
1233 | } |
||
778 | daniel-mar | 1234 | } else { |
1235 | // First, try to find out how many levels we need to go up |
||
1236 | $steps_up = self::getExecutingScriptPathDepth(); |
||
326 | daniel-mar | 1237 | |
778 | daniel-mar | 1238 | // Then go up these amount of levels, based on SCRIPT_NAME/argv[0] |
1239 | $res = dirname($_SERVER['SCRIPT_NAME'].'index.php'); // This fake 'index.php' ensures that SCRIPT_NAME does not end with '/', which would make dirname() fail |
||
1240 | for ($i=0; $i<$steps_up; $i++) { |
||
1241 | $res = dirname($res); |
||
1242 | } |
||
1243 | $res = str_replace('\\', '/', $res); |
||
1244 | if ($res == '/') $res = ''; |
||
227 | daniel-mar | 1245 | |
778 | daniel-mar | 1246 | // Add protocol and hostname |
580 | daniel-mar | 1247 | $is_ssl = self::isSSL(); |
495 | daniel-mar | 1248 | $protocol = $is_ssl ? 'https' : 'http'; // do not translate |
1249 | $host = $_SERVER['HTTP_HOST']; // includes port if it is not 80/443 |
||
778 | daniel-mar | 1250 | |
1251 | return $protocol.'://'.$host.$res.'/'; |
||
495 | daniel-mar | 1252 | } |
227 | daniel-mar | 1253 | } |
1254 | } |
||
1255 | |||
827 | daniel-mar | 1256 | private static function getSystemIdFromPubKey($pubKey) { |
1257 | $m = array(); |
||
1258 | if (preg_match('@BEGIN PUBLIC KEY\-+(.+)\-+END PUBLIC KEY@ismU', $pubKey, $m)) { |
||
1259 | return smallhash(base64_decode($m[1])); |
||
1260 | } |
||
1261 | return false; |
||
1262 | } |
||
1263 | |||
227 | daniel-mar | 1264 | private static $system_id_cache = null; |
1265 | public static function getSystemId($oid=false) { |
||
1266 | if (!is_null(self::$system_id_cache)) { |
||
1267 | $out = self::$system_id_cache; |
||
1268 | } else { |
||
1269 | $out = false; |
||
1270 | |||
1271 | if (self::getPkiStatus(true)) { |
||
830 | daniel-mar | 1272 | $pubKey = OIDplus::getSystemPublicKey(); |
827 | daniel-mar | 1273 | $out = self::getSystemIdFromPubKey($pubKey); |
227 | daniel-mar | 1274 | } |
1275 | self::$system_id_cache = $out; |
||
1276 | } |
||
350 | daniel-mar | 1277 | if (!$out) return false; |
291 | daniel-mar | 1278 | return ($oid ? '1.3.6.1.4.1.37476.30.9.' : '').$out; |
227 | daniel-mar | 1279 | } |
1280 | |||
825 | daniel-mar | 1281 | public static function getOpenSslCnf() { |
1282 | // The following functions need a config file, otherway they don't work |
||
1283 | // - openssl_csr_new |
||
1284 | // - openssl_csr_sign |
||
1285 | // - openssl_pkey_export |
||
1286 | // - openssl_pkey_export_to_file |
||
1287 | // - openssl_pkey_new |
||
1288 | $tmp = @getenv('OPENSSL_CONF'); |
||
1289 | if ($tmp && file_exists($tmp)) return $tmp; |
||
1290 | |||
1291 | // OpenSSL in XAMPP does not work OOBE, since the OPENSSL_CONF is |
||
1292 | // C:/xampp/apache/bin/openssl.cnf and not C:/xampp/apache/conf/openssl.cnf |
||
1293 | // Bug reports are more than 10 years old and nobody cares... |
||
1294 | // Use our own config file |
||
1295 | return __DIR__.'/../../vendor/phpseclib/phpseclib/phpseclib/openssl.cnf'; |
||
1296 | } |
||
1297 | |||
830 | daniel-mar | 1298 | private static function getPrivKeyPassphraseFilename() { |
1299 | return OIDplus::localpath() . 'userdata/privkey_secret.php'; |
||
1300 | } |
||
227 | daniel-mar | 1301 | |
830 | daniel-mar | 1302 | private static function tryCreatePrivKeyPassphrase() { |
1303 | $file = self::getPrivKeyPassphraseFilename(); |
||
827 | daniel-mar | 1304 | |
830 | daniel-mar | 1305 | $passphrase = generateRandomString(64); |
1306 | $cont = "<?php\n"; |
||
1307 | $cont .= "// ATTENTION! This file was automatically generated by OIDplus to encrypt the private key\n"; |
||
1308 | $cont .= "// that is located in your database configuration table. DO NOT ALTER OR DELETE THIS FILE,\n"; |
||
1309 | $cont .= "// otherwise you will lose your OIDplus System-ID and all services connected with it!\n"; |
||
831 | daniel-mar | 1310 | $cont .= "// If multiple systems access the same database, then this file must be synchronous\n"; |
1311 | $cont .= "// between all systems, otherwise you will lose your system ID, too!\n"; |
||
830 | daniel-mar | 1312 | $cont .= "\$passphrase = '$passphrase';\n"; |
1313 | $cont .= "// End of file\n"; |
||
1314 | |||
1315 | @file_put_contents($file, $cont); |
||
1316 | } |
||
1317 | |||
1318 | private static function getPrivKeyPassphrase() { |
||
1319 | $file = self::getPrivKeyPassphraseFilename(); |
||
1320 | if (!file_exists($file)) return false; |
||
1321 | $cont = file_get_contents($file); |
||
1322 | $m = array(); |
||
1323 | if (!preg_match("@'(.+)'@isU", $cont, $m)) return false; |
||
1324 | return $m[1]; |
||
1325 | } |
||
1326 | |||
1327 | public static function getSystemPrivateKey() { |
||
227 | daniel-mar | 1328 | $privKey = OIDplus::config()->getValue('oidplus_private_key'); |
830 | daniel-mar | 1329 | if ($privKey == '') return false; |
1330 | |||
1331 | $passphrase = self::getPrivKeyPassphrase(); |
||
1332 | if ($passphrase !== false) { |
||
1333 | $privKey = decrypt_private_key($privKey, $passphrase); |
||
1334 | } |
||
1335 | |||
1336 | if (is_privatekey_encrypted($privKey)) { |
||
1337 | // This can happen if the key file has vanished |
||
1338 | return false; |
||
1339 | } |
||
1340 | |||
1341 | return $privKey; |
||
1342 | } |
||
1343 | |||
1344 | public static function getSystemPublicKey() { |
||
227 | daniel-mar | 1345 | $pubKey = OIDplus::config()->getValue('oidplus_public_key'); |
830 | daniel-mar | 1346 | if ($pubKey == '') return false; |
1347 | return $pubKey; |
||
1348 | } |
||
227 | daniel-mar | 1349 | |
830 | daniel-mar | 1350 | public static function getPkiStatus($try_generate=false) { |
1351 | if (!function_exists('openssl_pkey_new')) return false; |
||
227 | daniel-mar | 1352 | |
830 | daniel-mar | 1353 | if ($try_generate) { |
1354 | // For debug purposes: Invalidate current key once: |
||
1355 | //OIDplus::config()->setValue('oidplus_private_key', ''); |
||
256 | daniel-mar | 1356 | |
830 | daniel-mar | 1357 | $privKey = OIDplus::getSystemPrivateKey(); |
1358 | $pubKey = OIDplus::getSystemPublicKey(); |
||
1359 | if (!verify_private_public_key($privKey, $pubKey)) { |
||
1360 | if ($pubKey) { |
||
1361 | OIDplus::logger()->log("[WARN]A!", "The private/public key-pair is broken. A new key-pair will now be generated for your system. Your System-ID will change."); |
||
1362 | } |
||
227 | daniel-mar | 1363 | |
830 | daniel-mar | 1364 | $pkey_config = array( |
1365 | "digest_alg" => "sha512", |
||
1366 | "private_key_bits" => defined('OPENSSL_SUPPLEMENT') ? 1024 : 2048, // openssl_supplement.inc.php is based on phpseclib, which is very slow. So we use 1024 bits instead of 2048 bits |
||
1367 | "private_key_type" => OPENSSL_KEYTYPE_RSA, |
||
1368 | "config" => OIDplus::getOpenSslCnf() |
||
1369 | ); |
||
227 | daniel-mar | 1370 | |
830 | daniel-mar | 1371 | // Create the private and public key |
1372 | $res = openssl_pkey_new($pkey_config); |
||
1373 | if ($res === false) return false; |
||
239 | daniel-mar | 1374 | |
830 | daniel-mar | 1375 | // Extract the private key from $res to $privKey |
1376 | if (openssl_pkey_export($res, $privKey, null, $pkey_config) === false) return false; |
||
227 | daniel-mar | 1377 | |
830 | daniel-mar | 1378 | // Extract the public key from $res to $pubKey |
1379 | $tmp = openssl_pkey_get_details($res); |
||
1380 | if ($tmp === false) return false; |
||
1381 | $pubKey = $tmp["key"]; |
||
1382 | |||
1383 | // encrypt new keys using a passphrase stored in a secret file |
||
1384 | self::tryCreatePrivKeyPassphrase(); // *try* (re)generate this file |
||
1385 | $passphrase = self::getPrivKeyPassphrase(); |
||
1386 | if ($passphrase !== false) { |
||
1387 | $privKey = encrypt_private_key($privKey, $passphrase); |
||
1388 | } |
||
1389 | |||
1390 | // Calculate the system ID from the public key |
||
1391 | $system_id = self::getSystemIdFromPubKey($pubKey); |
||
1392 | if ($system_id !== false) { |
||
1393 | // Save the key pair to database |
||
1394 | OIDplus::config()->setValue('oidplus_private_key', $privKey); |
||
1395 | OIDplus::config()->setValue('oidplus_public_key', $pubKey); |
||
1396 | |||
1397 | // Log the new system ID |
||
1398 | OIDplus::logger()->log("[INFO]A!", "A new private/public key-pair for your system had been generated. Your SystemID is now $system_id"); |
||
1399 | } |
||
1400 | } else { |
||
1401 | $passphrase = self::getPrivKeyPassphrase(); |
||
831 | daniel-mar | 1402 | $rawPrivKey = OIDplus::config()->getValue('oidplus_private_key'); |
1403 | if (($passphrase === false) || !is_privatekey_encrypted($rawPrivKey)) { |
||
830 | daniel-mar | 1404 | // Upgrade to new encrypted keys |
1405 | self::tryCreatePrivKeyPassphrase(); // *try* generate this file |
||
1406 | $passphrase = self::getPrivKeyPassphrase(); |
||
1407 | if ($passphrase !== false) { |
||
1408 | $privKey = encrypt_private_key($privKey, $passphrase); |
||
1409 | OIDplus::logger()->log("[INFO]A!", "The private/public key-pair has been upgraded to an encrypted key-pair. The key is saved in ".self::getPrivKeyPassphraseFilename()); |
||
1410 | OIDplus::config()->setValue('oidplus_private_key', $privKey); |
||
1411 | } |
||
1412 | } |
||
227 | daniel-mar | 1413 | } |
1414 | } |
||
1415 | |||
830 | daniel-mar | 1416 | $privKey = OIDplus::getSystemPrivateKey(); |
1417 | $pubKey = OIDplus::getSystemPublicKey(); |
||
227 | daniel-mar | 1418 | return verify_private_public_key($privKey, $pubKey); |
1419 | } |
||
1420 | |||
170 | daniel-mar | 1421 | public static function getInstallType() { |
486 | daniel-mar | 1422 | $counter = 0; |
1423 | |||
661 | daniel-mar | 1424 | if ($new_version_file_exists = file_exists(OIDplus::localpath().'.version.php')) { |
486 | daniel-mar | 1425 | $counter++; |
591 | daniel-mar | 1426 | } |
661 | daniel-mar | 1427 | if ($old_version_file_exists = file_exists(OIDplus::localpath().'oidplus_version.txt')) { |
1428 | $counter++; |
||
1429 | } |
||
1430 | $version_file_exists = $old_version_file_exists | $new_version_file_exists; |
||
681 | daniel-mar | 1431 | if ($svn_dir_exists = (is_dir(OIDplus::localpath().'.svn') || |
1432 | is_dir(OIDplus::localpath().'../.svn'))) { // in case we checked out the root instead of the "trunk" |
||
486 | daniel-mar | 1433 | $counter++; |
591 | daniel-mar | 1434 | } |
681 | daniel-mar | 1435 | // if ($git_dir_exists = is_dir(OIDplus::localpath().'.git')) { |
698 | daniel-mar | 1436 | if ($git_dir_exists = (OIDplus::findGitFolder() !== false)) { |
486 | daniel-mar | 1437 | $counter++; |
591 | daniel-mar | 1438 | } |
486 | daniel-mar | 1439 | |
1440 | if ($counter === 0) { |
||
360 | daniel-mar | 1441 | return 'unknown'; // do not translate |
170 | daniel-mar | 1442 | } |
591 | daniel-mar | 1443 | else if ($counter > 1) { |
360 | daniel-mar | 1444 | return 'ambigous'; // do not translate |
170 | daniel-mar | 1445 | } |
591 | daniel-mar | 1446 | else if ($svn_dir_exists) { |
360 | daniel-mar | 1447 | return 'svn-wc'; // do not translate |
170 | daniel-mar | 1448 | } |
591 | daniel-mar | 1449 | else if ($git_dir_exists) { |
486 | daniel-mar | 1450 | return 'git-wc'; // do not translate |
1451 | } |
||
591 | daniel-mar | 1452 | else if ($version_file_exists) { |
360 | daniel-mar | 1453 | return 'svn-snapshot'; // do not translate |
170 | daniel-mar | 1454 | } |
1455 | } |
||
1456 | |||
412 | daniel-mar | 1457 | private static function recognizeVersion() { |
1458 | try { |
||
1459 | $ver_prev = OIDplus::config()->getValue("last_known_version"); |
||
1460 | $ver_now = OIDplus::getVersion(); |
||
1461 | if (($ver_now != '') && ($ver_prev != '') && ($ver_now != $ver_prev)) { |
||
455 | daniel-mar | 1462 | // TODO: Problem: When the system was updated using SVN, then the IP address of the next random visitor of the website is logged! |
412 | daniel-mar | 1463 | OIDplus::logger()->log("[INFO]A!", "System version changed from '$ver_prev' to '$ver_now'"); |
856 | daniel-mar | 1464 | |
1465 | // Just to be sure, recanonize objects (we don't do it at every page visit due to performance reasons) |
||
857 | daniel-mar | 1466 | self::recanonizeObjects(); |
412 | daniel-mar | 1467 | } |
1468 | OIDplus::config()->setValue("last_known_version", $ver_now); |
||
1050 | daniel-mar | 1469 | } catch (\Exception $e) { |
412 | daniel-mar | 1470 | } |
1471 | } |
||
1472 | |||
111 | daniel-mar | 1473 | public static function getVersion() { |
412 | daniel-mar | 1474 | static $cachedVersion = null; |
1475 | if (!is_null($cachedVersion)) { |
||
1476 | return $cachedVersion; |
||
1477 | } |
||
1478 | |||
486 | daniel-mar | 1479 | $installType = OIDplus::getInstallType(); |
1480 | |||
1481 | if ($installType === 'svn-wc') { |
||
496 | daniel-mar | 1482 | $ver = get_svn_revision(OIDplus::localpath()); |
486 | daniel-mar | 1483 | if ($ver) |
1484 | return ($cachedVersion = 'svn-'.$ver); |
||
558 | daniel-mar | 1485 | $ver = get_svn_revision(OIDplus::localpath().'../'); // in case we checked out the root instead of the "trunk" |
1486 | if ($ver) |
||
1487 | return ($cachedVersion = 'svn-'.$ver); |
||
111 | daniel-mar | 1488 | } |
162 | daniel-mar | 1489 | |
486 | daniel-mar | 1490 | if ($installType === 'git-wc') { |
698 | daniel-mar | 1491 | $ver = OIDplus::getGitsvnRevision(OIDplus::localpath()); |
486 | daniel-mar | 1492 | if ($ver) |
1493 | return ($cachedVersion = 'svn-'.$ver); |
||
162 | daniel-mar | 1494 | } |
1495 | |||
486 | daniel-mar | 1496 | if ($installType === 'svn-snapshot') { |
661 | daniel-mar | 1497 | $cont = ''; |
1498 | if (file_exists($filename = OIDplus::localpath().'oidplus_version.txt')) |
||
1499 | $cont = file_get_contents($filename); |
||
1500 | if (file_exists($filename = OIDplus::localpath().'.version.php')) |
||
1501 | $cont = file_get_contents($filename); |
||
386 | daniel-mar | 1502 | $m = array(); |
360 | daniel-mar | 1503 | if (preg_match('@Revision (\d+)@', $cont, $m)) // do not translate |
412 | daniel-mar | 1504 | return ($cachedVersion = 'svn-'.$m[1]); // do not translate |
162 | daniel-mar | 1505 | } |
1506 | |||
486 | daniel-mar | 1507 | return ($cachedVersion = false); // version ambigous or unknown |
111 | daniel-mar | 1508 | } |
1509 | |||
974 | daniel-mar | 1510 | const ENFORCE_SSL_NO = 0; |
1511 | const ENFORCE_SSL_YES = 1; |
||
1512 | const ENFORCE_SSL_AUTO = 2; |
||
230 | daniel-mar | 1513 | private static $sslAvailableCache = null; |
1514 | public static function isSslAvailable() { |
||
1515 | if (!is_null(self::$sslAvailableCache)) return self::$sslAvailableCache; |
||
256 | daniel-mar | 1516 | |
495 | daniel-mar | 1517 | if (PHP_SAPI == 'cli') { |
230 | daniel-mar | 1518 | self::$sslAvailableCache = false; |
1519 | return false; |
||
1520 | } |
||
1521 | |||
49 | daniel-mar | 1522 | $timeout = 2; |
580 | daniel-mar | 1523 | $already_ssl = self::isSSL(); |
80 | daniel-mar | 1524 | $ssl_port = 443; |
1059 | daniel-mar | 1525 | $host_with_port = $_SERVER['HTTP_HOST']; |
1526 | $host_no_port = explode(':',$host_with_port)[0]; |
||
1527 | $host_ssl = $host_no_port . ($ssl_port != 443 ? ':'.$ssl_port : ''); |
||
42 | daniel-mar | 1528 | |
974 | daniel-mar | 1529 | if ($already_ssl) { |
1059 | daniel-mar | 1530 | OIDplus::cookieUtils()->setcookie('SSL_CHECK', '1', 0, true/*allowJS*/, null/*samesite*/, true/*forceInsecure*/); |
974 | daniel-mar | 1531 | self::$sslAvailableCache = true; |
1532 | return true; |
||
1533 | } else { |
||
1534 | if (isset($_COOKIE['SSL_CHECK']) && ($_COOKIE['SSL_CHECK'] == '1')) { |
||
1535 | // The cookie "SSL_CHECK" is set once a website was loaded with HTTPS. |
||
1536 | // It forces subsequent HTTP calls to redirect to HTTPS (like HSTS). |
||
1537 | // The reason is the following problem: |
||
1538 | // If you open the page with HTTPS first, then the CSRF token cookies will get the "secure" flag |
||
1539 | // If you open the page then with HTTP, the HTTP cannot access the secure CSRF cookies, |
||
1540 | // Chrome will then block "Set-Cookie" since the HTTP cookie would overwrite the HTTPS cookie. |
||
1059 | daniel-mar | 1541 | // So we MUST redirect, even if the Mode is ENFORCE_SSL_NO. |
974 | daniel-mar | 1542 | // Note: SSL_CHECK is NOT a replacement for HSTS! You should use HSTS, |
1059 | daniel-mar | 1543 | // because on there your browser ensures that HTTPS is called, before the server |
1544 | // is even contacted (and therefore, no HTTP connection can be hacked). |
||
974 | daniel-mar | 1545 | $mode = OIDplus::ENFORCE_SSL_YES; |
1546 | } else { |
||
1547 | $mode = OIDplus::baseConfig()->getValue('ENFORCE_SSL', OIDplus::ENFORCE_SSL_AUTO); |
||
1548 | } |
||
261 | daniel-mar | 1549 | |
974 | daniel-mar | 1550 | if ($mode == OIDplus::ENFORCE_SSL_NO) { |
1551 | // No SSL available |
||
1552 | self::$sslAvailableCache = false; |
||
1553 | return false; |
||
1554 | } else if ($mode == OIDplus::ENFORCE_SSL_YES) { |
||
1555 | // Force SSL |
||
1059 | daniel-mar | 1556 | $location = 'https://' . $host_ssl . $_SERVER['REQUEST_URI']; |
80 | daniel-mar | 1557 | header('Location:'.$location); |
360 | daniel-mar | 1558 | die(_L('Redirecting to HTTPS...')); |
974 | daniel-mar | 1559 | } else if ($mode == OIDplus::ENFORCE_SSL_AUTO) { |
1560 | // Automatic SSL detection |
||
80 | daniel-mar | 1561 | if (isset($_COOKIE['SSL_CHECK'])) { |
1562 | // We already had the HTTPS detection done before. |
||
974 | daniel-mar | 1563 | if ($_COOKIE['SSL_CHECK'] == '1') { |
80 | daniel-mar | 1564 | // HTTPS was detected before, but we are HTTP. Redirect now |
1059 | daniel-mar | 1565 | $location = 'https://' . $host_ssl . $_SERVER['REQUEST_URI']; |
80 | daniel-mar | 1566 | header('Location:'.$location); |
360 | daniel-mar | 1567 | die(_L('Redirecting to HTTPS...')); |
80 | daniel-mar | 1568 | } else { |
1569 | // No HTTPS available. Do nothing. |
||
230 | daniel-mar | 1570 | self::$sslAvailableCache = false; |
80 | daniel-mar | 1571 | return false; |
1572 | } |
||
49 | daniel-mar | 1573 | } else { |
80 | daniel-mar | 1574 | // This is our first check (or the browser didn't accept the SSL_CHECK cookie) |
386 | daniel-mar | 1575 | $errno = -1; |
1576 | $errstr = ''; |
||
1059 | daniel-mar | 1577 | if (@fsockopen($host_no_port, $ssl_port, $errno, $errstr, $timeout)) { |
80 | daniel-mar | 1578 | // HTTPS detected. Redirect now, and remember that we had detected HTTPS |
1059 | daniel-mar | 1579 | OIDplus::cookieUtils()->setcookie('SSL_CHECK', '1', 0, true/*allowJS*/, null/*samesite*/, true/*forceInsecure*/); |
1580 | $location = 'https://' . $host_ssl . $_SERVER['REQUEST_URI']; |
||
80 | daniel-mar | 1581 | header('Location:'.$location); |
360 | daniel-mar | 1582 | die(_L('Redirecting to HTTPS...')); |
80 | daniel-mar | 1583 | } else { |
1584 | // No HTTPS detected. Do nothing, and next time, don't try to detect HTTPS again. |
||
1059 | daniel-mar | 1585 | OIDplus::cookieUtils()->setcookie('SSL_CHECK', '0', 0, true/*allowJS*/, null/*samesite*/, true/*forceInsecure*/); |
230 | daniel-mar | 1586 | self::$sslAvailableCache = false; |
80 | daniel-mar | 1587 | return false; |
1588 | } |
||
49 | daniel-mar | 1589 | } |
42 | daniel-mar | 1590 | } |
1591 | } |
||
1592 | } |
||
497 | daniel-mar | 1593 | |
496 | daniel-mar | 1594 | /** |
1595 | * Gets a local path pointing to a resource |
||
1596 | * @param string $target Target resource (file or directory must exist), or null to get the OIDplus base directory |
||
1597 | * @param boolean $relative If true, the returning path is relative to the currently executed PHP file (not the CLI working directory) |
||
590 | daniel-mar | 1598 | * @return string|false The local path, with guaranteed trailing path delimiter for directories |
496 | daniel-mar | 1599 | */ |
1600 | public static function localpath($target=null, $relative=false) { |
||
1601 | if (is_null($target)) { |
||
1602 | $target = __DIR__.'/../../'; |
||
1603 | } |
||
241 | daniel-mar | 1604 | |
496 | daniel-mar | 1605 | if ($relative) { |
1606 | // First, try to find out how many levels we need to go up |
||
1607 | $steps_up = self::getExecutingScriptPathDepth(); |
||
1608 | if ($steps_up === false) return false; |
||
497 | daniel-mar | 1609 | |
496 | daniel-mar | 1610 | // Virtually go back from the executing PHP script to the OIDplus base path |
1611 | $res = str_repeat('../',$steps_up); |
||
497 | daniel-mar | 1612 | |
496 | daniel-mar | 1613 | // Then go to the desired location |
1614 | $basedir = realpath(__DIR__.'/../../'); |
||
1615 | $target = realpath($target); |
||
500 | daniel-mar | 1616 | if ($target === false) return false; |
496 | daniel-mar | 1617 | $res .= substr($target, strlen($basedir)+1); |
1618 | $res = rtrim($res,'/'); // avoid '..//' for localpath(null,true) |
||
1619 | } else { |
||
1620 | $res = realpath($target); |
||
241 | daniel-mar | 1621 | } |
497 | daniel-mar | 1622 | |
801 | daniel-mar | 1623 | if (is_dir($target)) $res .= '/'; |
497 | daniel-mar | 1624 | |
801 | daniel-mar | 1625 | $res = str_replace('/', DIRECTORY_SEPARATOR, $res); |
1626 | |||
496 | daniel-mar | 1627 | return $res; |
241 | daniel-mar | 1628 | } |
355 | daniel-mar | 1629 | |
496 | daniel-mar | 1630 | /** |
1631 | * Gets a URL pointing to a resource |
||
1632 | * @param string $target Target resource (file or directory must exist), or null to get the OIDplus base directory |
||
806 | daniel-mar | 1633 | * @param int|boolean $mode If true or OIDplus::PATH_RELATIVE, the returning path is relative to the currently executed |
1634 | * PHP script (i.e. index.php , not the plugin PHP script!). False or OIDplus::PATH_ABSOLUTE is |
||
1635 | * results in an absolute URL. OIDplus::PATH_ABSOLUTE_CANONICAL is an absolute URL, |
||
1636 | * but a canonical path (set by base config setting CANONICAL_SYSTEM_URL) is preferred. |
||
590 | daniel-mar | 1637 | * @return string|false The URL, with guaranteed trailing path delimiter for directories |
496 | daniel-mar | 1638 | */ |
806 | daniel-mar | 1639 | public static function webpath($target=null, $mode=self::PATH_ABSOLUTE_CANONICAL) { |
801 | daniel-mar | 1640 | // backwards compatibility |
1641 | if ($mode === true) $mode = self::PATH_RELATIVE; |
||
1642 | if ($mode === false) $mode = self::PATH_ABSOLUTE; |
||
1643 | |||
811 | daniel-mar | 1644 | if ($mode == OIDplus::PATH_RELATIVE_TO_ROOT) { |
812 | daniel-mar | 1645 | $tmp = OIDplus::webpath($target,OIDplus::PATH_ABSOLUTE); |
1646 | if ($tmp === false) return false; |
||
1647 | $tmp = parse_url($tmp); |
||
1648 | if ($tmp === false) return false; |
||
1649 | if (!isset($tmp['path'])) return false; |
||
1650 | return $tmp['path']; |
||
811 | daniel-mar | 1651 | } |
1652 | |||
812 | daniel-mar | 1653 | if ($mode == OIDplus::PATH_RELATIVE_TO_ROOT_CANONICAL) { |
1654 | $tmp = OIDplus::webpath($target,OIDplus::PATH_ABSOLUTE_CANONICAL); |
||
1655 | if ($tmp === false) return false; |
||
1656 | $tmp = parse_url($tmp); |
||
1657 | if ($tmp === false) return false; |
||
1658 | if (!isset($tmp['path'])) return false; |
||
1659 | return $tmp['path']; |
||
1660 | } |
||
1661 | |||
801 | daniel-mar | 1662 | $res = self::getSystemUrl($mode); // Note: already contains a trailing path delimiter |
778 | daniel-mar | 1663 | if ($res === false) return false; |
497 | daniel-mar | 1664 | |
496 | daniel-mar | 1665 | if (!is_null($target)) { |
1666 | $basedir = realpath(__DIR__.'/../../'); |
||
1667 | $target = realpath($target); |
||
500 | daniel-mar | 1668 | if ($target === false) return false; |
496 | daniel-mar | 1669 | $tmp = substr($target, strlen($basedir)+1); |
500 | daniel-mar | 1670 | $res .= str_replace(DIRECTORY_SEPARATOR,'/',$tmp); // remove OS specific path delimiters introduced by realpath() |
497 | daniel-mar | 1671 | if (is_dir($target)) $res .= '/'; |
496 | daniel-mar | 1672 | } |
497 | daniel-mar | 1673 | |
496 | daniel-mar | 1674 | return $res; |
1675 | } |
||
497 | daniel-mar | 1676 | |
778 | daniel-mar | 1677 | public static function canonicalURL() { |
1678 | // First part: OIDplus system URL (or canonical system URL) |
||
801 | daniel-mar | 1679 | $sysurl = OIDplus::getSystemUrl(self::PATH_ABSOLUTE_CANONICAL); |
778 | daniel-mar | 1680 | |
1681 | // Second part: Directory |
||
1682 | $basedir = realpath(__DIR__.'/../../'); |
||
1683 | $target = realpath('.'); |
||
1684 | if ($target === false) return false; |
||
1685 | $tmp = substr($target, strlen($basedir)+1); |
||
1686 | $res = str_replace(DIRECTORY_SEPARATOR,'/',$tmp); // remove OS specific path delimiters introduced by realpath() |
||
780 | daniel-mar | 1687 | if (is_dir($target) && ($res != '')) $res .= '/'; |
778 | daniel-mar | 1688 | |
1689 | // Third part: File name |
||
1690 | $tmp = explode('/',$_SERVER['SCRIPT_NAME']); |
||
1691 | $tmp = end($tmp); |
||
1692 | |||
1693 | // Fourth part: Query string (ordered) |
||
1694 | $tmp2 = getSortedQuery(); |
||
1695 | if ($tmp2 != '') $tmp2 = '?'.$tmp2; |
||
1696 | |||
1697 | return $sysurl.$res.$tmp.$tmp2; |
||
1698 | } |
||
1699 | |||
639 | daniel-mar | 1700 | private static $shutdown_functions = array(); |
1701 | public static function register_shutdown_function($func) { |
||
1702 | self::$shutdown_functions[] = $func; |
||
1703 | } |
||
1704 | |||
1705 | public static function invoke_shutdown() { |
||
1706 | foreach (self::$shutdown_functions as $func) { |
||
1707 | $func(); |
||
1708 | } |
||
1709 | } |
||
1710 | |||
360 | daniel-mar | 1711 | public static function getAvailableLangs() { |
1712 | $langs = array(); |
||
1713 | foreach (OIDplus::getAllPluginManifests('language') as $pluginManifest) { |
||
389 | daniel-mar | 1714 | $code = $pluginManifest->getLanguageCode(); |
360 | daniel-mar | 1715 | $langs[] = $code; |
1716 | } |
||
1717 | return $langs; |
||
1718 | } |
||
1719 | |||
1041 | daniel-mar | 1720 | public static function getDefaultLang() { |
1721 | static $thrownOnce = false; // avoid endless loop inside OIDplusConfigInitializationException |
||
1722 | |||
1049 | daniel-mar | 1723 | $lang = self::baseConfig()->getValue('DEFAULT_LANGUAGE', 'enus'); |
1041 | daniel-mar | 1724 | |
1725 | if (!in_array($lang,self::getAvailableLangs())) { |
||
1726 | if (!$thrownOnce) { |
||
1727 | $thrownOnce = true; |
||
1048 | daniel-mar | 1728 | throw new OIDplusConfigInitializationException(_L('DEFAULT_LANGUAGE points to an invalid language plugin. (Consider setting to "enus" = "English USA".)')); |
1041 | daniel-mar | 1729 | } else { |
1730 | return 'enus'; |
||
1731 | } |
||
1732 | } |
||
1733 | |||
1734 | return $lang; |
||
1735 | } |
||
1736 | |||
355 | daniel-mar | 1737 | public static function getCurrentLang() { |
360 | daniel-mar | 1738 | if (isset($_GET['lang'])) { |
1739 | $lang = $_GET['lang']; |
||
1740 | } else if (isset($_POST['lang'])) { |
||
1741 | $lang = $_POST['lang']; |
||
1742 | } else if (isset($_COOKIE['LANGUAGE'])) { |
||
1743 | $lang = $_COOKIE['LANGUAGE']; |
||
1744 | } else { |
||
1041 | daniel-mar | 1745 | $lang = self::getDefaultLang(); |
360 | daniel-mar | 1746 | } |
1747 | $lang = substr(preg_replace('@[^a-z]@ismU', '', $lang),0,4); // sanitize |
||
355 | daniel-mar | 1748 | return $lang; |
1749 | } |
||
1750 | |||
362 | daniel-mar | 1751 | public static function handleLangArgument() { |
1752 | if (isset($_GET['lang'])) { |
||
1753 | // The "?lang=" argument is only for NoScript-Browsers/SearchEngines |
||
1754 | // In case someone who has JavaScript clicks a ?lang= link, they should get |
||
1755 | // the page in that language, but the cookie must be set, otherwise |
||
1756 | // the menu and other stuff would be in their cookie-based-language and not the |
||
1757 | // argument-based-language. |
||
557 | daniel-mar | 1758 | OIDplus::cookieUtils()->setcookie('LANGUAGE', $_GET['lang'], 0, true/*HttpOnly off, because JavaScript also needs translation*/); |
362 | daniel-mar | 1759 | } else if (isset($_POST['lang'])) { |
557 | daniel-mar | 1760 | OIDplus::cookieUtils()->setcookie('LANGUAGE', $_POST['lang'], 0, true/*HttpOnly off, because JavaScript also needs translation*/); |
362 | daniel-mar | 1761 | } |
1762 | } |
||
1763 | |||
468 | daniel-mar | 1764 | private static $translationArray = array(); |
469 | daniel-mar | 1765 | protected static function getTranslationFileContents($translation_file) { |
1766 | // First, try the cache |
||
481 | daniel-mar | 1767 | $cache_file = __DIR__ . '/../../userdata/cache/translation_'.md5($translation_file).'.ser'; |
469 | daniel-mar | 1768 | if (file_exists($cache_file) && (filemtime($cache_file) == filemtime($translation_file))) { |
1769 | $cac = @unserialize(file_get_contents($cache_file)); |
||
1770 | if ($cac) return $cac; |
||
1771 | } |
||
481 | daniel-mar | 1772 | |
469 | daniel-mar | 1773 | // If not successful, then load the XML file |
1774 | $xml = @simplexml_load_string(file_get_contents($translation_file)); |
||
1775 | if (!$xml) return array(); // if there is an UTF-8 or parsing error, don't output any errors, otherwise the JavaScript is corrupt and the page won't render correctly |
||
1776 | $cac = array(); |
||
1777 | foreach ($xml->message as $msg) { |
||
1778 | $src = trim($msg->source->__toString()); |
||
1779 | $dst = trim($msg->target->__toString()); |
||
1780 | $cac[$src] = $dst; |
||
1781 | } |
||
1782 | @file_put_contents($cache_file,serialize($cac)); |
||
1783 | @touch($cache_file,filemtime($translation_file)); |
||
1784 | return $cac; |
||
1785 | } |
||
468 | daniel-mar | 1786 | public static function getTranslationArray($requested_lang='*') { |
362 | daniel-mar | 1787 | foreach (OIDplus::getAllPluginManifests('language') as $pluginManifest) { |
389 | daniel-mar | 1788 | $lang = $pluginManifest->getLanguageCode(); |
362 | daniel-mar | 1789 | if (strpos($lang,'/') !== false) continue; // just to be sure |
1790 | if (strpos($lang,'\\') !== false) continue; // just to be sure |
||
1791 | if (strpos($lang,'..') !== false) continue; // just to be sure |
||
401 | daniel-mar | 1792 | |
468 | daniel-mar | 1793 | if (($requested_lang != '*') && ($lang != $requested_lang)) continue; |
401 | daniel-mar | 1794 | |
468 | daniel-mar | 1795 | if (!isset(self::$translationArray[$lang])) { |
1796 | self::$translationArray[$lang] = array(); |
||
1797 | |||
1798 | $wildcard = $pluginManifest->getLanguageMessages(); |
||
1799 | if (strpos($wildcard,'/') !== false) continue; // just to be sure |
||
1800 | if (strpos($wildcard,'\\') !== false) continue; // just to be sure |
||
1801 | if (strpos($wildcard,'..') !== false) continue; // just to be sure |
||
1802 | |||
635 | daniel-mar | 1803 | $translation_files = glob(__DIR__.'/../../plugins/'.'*'.'/language/'.$lang.'/'.$wildcard); |
468 | daniel-mar | 1804 | sort($translation_files); |
1805 | foreach ($translation_files as $translation_file) { |
||
1806 | if (!file_exists($translation_file)) continue; |
||
469 | daniel-mar | 1807 | $cac = self::getTranslationFileContents($translation_file); |
1808 | foreach ($cac as $src => $dst) { |
||
1809 | self::$translationArray[$lang][$src] = $dst; |
||
468 | daniel-mar | 1810 | } |
401 | daniel-mar | 1811 | } |
362 | daniel-mar | 1812 | } |
1813 | } |
||
468 | daniel-mar | 1814 | return self::$translationArray; |
362 | daniel-mar | 1815 | } |
1816 | |||
699 | daniel-mar | 1817 | public static function getEditionInfo() { |
1818 | return @parse_ini_file(__DIR__.'/../edition.ini', true)['Edition']; |
||
1819 | } |
||
1820 | |||
698 | daniel-mar | 1821 | public static function findGitFolder() { |
1822 | // Git command line saves git information in folder ".git" |
||
1823 | // Plesk git saves git information in folder "../../../git/oidplus/" (or similar) |
||
727 | daniel-mar | 1824 | $dir = OIDplus::localpath(); |
698 | daniel-mar | 1825 | if (is_dir($dir.'/.git')) return $dir.'/.git'; |
1826 | $i = 0; |
||
1827 | do { |
||
1828 | if (is_dir($dir.'/git')) { |
||
719 | daniel-mar | 1829 | $confs = @glob($dir.'/git/'.'*'.'/config'); |
1830 | if ($confs) foreach ($confs as $conf) { |
||
698 | daniel-mar | 1831 | $cont = file_get_contents($conf); |
699 | daniel-mar | 1832 | if (isset(OIDplus::getEditionInfo()['gitrepo']) && (OIDplus::getEditionInfo()['gitrepo'] != '') && (strpos($cont, OIDplus::getEditionInfo()['gitrepo']) !== false)) { |
698 | daniel-mar | 1833 | return dirname($conf); |
1834 | } |
||
1835 | } |
||
1836 | } |
||
1837 | $i++; |
||
719 | daniel-mar | 1838 | } while (($i<100) && ($dir != ($new_dir = @realpath($dir.'/../'))) && ($dir = $new_dir)); |
698 | daniel-mar | 1839 | return false; |
1840 | } |
||
1841 | |||
1842 | public static function getGitsvnRevision($dir='') { |
||
1843 | try { |
||
1844 | // tries command line and binary parsing |
||
699 | daniel-mar | 1845 | // requires vendor/danielmarschall/git_utils.inc.php |
698 | daniel-mar | 1846 | $git_dir = OIDplus::findGitFolder(); |
1847 | if ($git_dir === false) return false; |
||
1848 | $commit_msg = git_get_latest_commit_message($git_dir); |
||
1050 | daniel-mar | 1849 | } catch (\Exception $e) { |
698 | daniel-mar | 1850 | return false; |
1851 | } |
||
1852 | |||
1853 | $m = array(); |
||
1854 | if (preg_match('%git-svn-id: (.+)@(\\d+) %ismU', $commit_msg, $m)) { |
||
1855 | return $m[2]; |
||
1856 | } else { |
||
1857 | return false; |
||
1858 | } |
||
1859 | } |
||
1860 | |||
775 | daniel-mar | 1861 | public static function prefilterQuery($static_node_id, $throw_exception) { |
1862 | // Let namespace be case-insensitive |
||
1863 | $ary = explode(':', $static_node_id, 2); |
||
1864 | $ary[0] = strtolower($ary[0]); |
||
1865 | $static_node_id = implode(':', $ary); |
||
1866 | |||
889 | daniel-mar | 1867 | // Ask plugins if they want to change the node id |
1868 | foreach (OIDplus::getObjectTypePluginsEnabled() as $plugin) { |
||
1869 | $static_node_id = $plugin->prefilterQuery($static_node_id, $throw_exception); |
||
775 | daniel-mar | 1870 | } |
1871 | |||
1872 | return $static_node_id; |
||
1873 | } |
||
849 | daniel-mar | 1874 | |
1875 | public static function isCronjob() { |
||
1876 | return explode('.',basename($_SERVER['SCRIPT_NAME']))[0] === 'cron'; |
||
1877 | } |
||
856 | daniel-mar | 1878 | |
857 | daniel-mar | 1879 | private static function recanonizeObjects() { |
1050 | daniel-mar | 1880 | // |
1881 | // Since OIDplus svn-184, entries in the database need to have a canonical ID |
||
1882 | // If the ID is not canonical (e.g. GUIDs missing hyphens), the object cannot be opened in OIDplus |
||
1883 | // This script re-canonizes the object IDs if required. |
||
1884 | // In SVN Rev 856, the canonization for GUID, IPv4 and IPv6 have changed, requiring another |
||
1885 | // re-canonization |
||
1886 | // |
||
856 | daniel-mar | 1887 | $res = OIDplus::db()->query("select id from ###objects"); |
1888 | while ($row = $res->fetch_array()) { |
||
1889 | $ida = $row['id']; |
||
857 | daniel-mar | 1890 | $obj = OIDplusObject::parse($ida); |
1891 | if (!$obj) continue; |
||
1892 | $idb = $obj->nodeId(); |
||
856 | daniel-mar | 1893 | if (($idb) && ($ida != $idb)) { |
1894 | OIDplus::db()->transaction_begin(); |
||
1895 | OIDplus::db()->query("update ###objects set id = ? where id = ?", array($idb, $ida)); |
||
1896 | OIDplus::db()->query("update ###asn1id set oid = ? where oid = ?", array($idb, $ida)); |
||
1897 | OIDplus::db()->query("update ###iri set oid = ? where oid = ?", array($idb, $ida)); |
||
1898 | OIDplus::db()->query("update ###log_object set id = ? where id = ?", array($idb, $ida)); |
||
1899 | OIDplus::logger()->log("[INFO]A!", "Object name '$ida' has been changed to '$idb' during re-canonization"); |
||
1900 | OIDplus::db()->transaction_commit(); |
||
978 | daniel-mar | 1901 | OIDplusObject::resetObjectInformationCache(); |
856 | daniel-mar | 1902 | } |
1903 | } |
||
1904 | } |
||
1905 | |||
374 | daniel-mar | 1906 | } |