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