Rev 775 | Rev 779 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | daniel-mar | 1 | <?php |
2 | |||
3 | /* |
||
4 | * OIDplus 2.0 |
||
778 | daniel-mar | 5 | * Copyright 2019 - 2022 Daniel Marschall, ViaThinkSoft |
2 | daniel-mar | 6 | * |
7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
||
8 | * you may not use this file except in compliance with the License. |
||
9 | * You may obtain a copy of the License at |
||
10 | * |
||
11 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
12 | * |
||
13 | * Unless required by applicable law or agreed to in writing, software |
||
14 | * distributed under the License is distributed on an "AS IS" BASIS, |
||
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
16 | * See the License for the specific language governing permissions and |
||
17 | * limitations under the License. |
||
18 | */ |
||
19 | |||
511 | daniel-mar | 20 | if (!defined('INSIDE_OIDPLUS')) die(); |
21 | |||
730 | daniel-mar | 22 | class OIDplus extends OIDplusBaseClass { |
281 | daniel-mar | 23 | private static /*OIDplusPagePlugin[]*/ $pagePlugins = array(); |
24 | private static /*OIDplusAuthPlugin[]*/ $authPlugins = array(); |
||
289 | daniel-mar | 25 | private static /*OIDplusLoggerPlugin[]*/ $loggerPlugins = array(); |
227 | daniel-mar | 26 | private static /*OIDplusObjectTypePlugin[]*/ $objectTypePlugins = array(); |
27 | private static /*string[]*/ $enabledObjectTypes = array(); |
||
28 | private static /*string[]*/ $disabledObjectTypes = array(); |
||
29 | private static /*OIDplusDatabasePlugin[]*/ $dbPlugins = array(); |
||
702 | daniel-mar | 30 | private static /*OIDplusCaptchaPlugin[]*/ $captchaPlugins = array(); |
274 | daniel-mar | 31 | private static /*OIDplusSqlSlangPlugin[]*/ $sqlSlangPlugins = array(); |
355 | daniel-mar | 32 | private static /*OIDplusLanguagePlugin[]*/ $languagePlugins = array(); |
449 | daniel-mar | 33 | private static /*OIDplusDesignPlugin[]*/ $designPlugins = array(); |
2 | daniel-mar | 34 | |
280 | daniel-mar | 35 | protected static $html = true; |
236 | daniel-mar | 36 | |
362 | daniel-mar | 37 | /*public*/ const DEFAULT_LANGUAGE = 'enus'; // the language of the source code |
355 | daniel-mar | 38 | |
778 | daniel-mar | 39 | // These plugin types can contain HTML code and therefore may |
40 | // emit (non-setup) CSS/JS code via their manifest. |
||
41 | /*public*/ const INTERACTIVE_PLUGIN_TYPES = array( |
||
42 | 'publicPages', |
||
43 | 'raPages', |
||
44 | 'adminPages', |
||
45 | 'objectTypes', |
||
46 | 'captcha' |
||
47 | ); |
||
48 | |||
2 | daniel-mar | 49 | private function __construct() { |
50 | } |
||
295 | daniel-mar | 51 | |
263 | daniel-mar | 52 | # --- Static classes |
274 | daniel-mar | 53 | |
263 | daniel-mar | 54 | private static $baseConfig = null; |
55 | private static $old_config_format = false; |
||
261 | daniel-mar | 56 | public static function baseConfig() { |
263 | daniel-mar | 57 | $first_init = false; |
274 | daniel-mar | 58 | |
263 | daniel-mar | 59 | if ($first_init = is_null(self::$baseConfig)) { |
60 | self::$baseConfig = new OIDplusBaseConfig(); |
||
261 | daniel-mar | 61 | } |
62 | |||
263 | daniel-mar | 63 | if ($first_init) { |
261 | daniel-mar | 64 | // Include a file containing various size/depth limitations of OIDs |
294 | daniel-mar | 65 | // It is important to include it before userdata/baseconfig/config.inc.php was included, |
66 | // so we can give userdata/baseconfig/config.inc.php the chance to override the values. |
||
261 | daniel-mar | 67 | |
496 | daniel-mar | 68 | include OIDplus::localpath().'includes/oidplus_limits.inc.php'; |
261 | daniel-mar | 69 | |
70 | // Include config file |
||
295 | daniel-mar | 71 | |
496 | daniel-mar | 72 | $config_file = OIDplus::localpath() . 'userdata/baseconfig/config.inc.php'; |
73 | $config_file_old = OIDplus::localpath() . 'includes/config.inc.php'; // backwards compatibility |
||
295 | daniel-mar | 74 | |
294 | daniel-mar | 75 | if (!file_exists($config_file) && file_exists($config_file_old)) { |
76 | $config_file = $config_file_old; |
||
77 | } |
||
261 | daniel-mar | 78 | |
294 | daniel-mar | 79 | if (file_exists($config_file)) { |
263 | daniel-mar | 80 | if (self::$old_config_format) { |
81 | // Note: We may only include it once due to backwards compatibility, |
||
82 | // since in version 2.0, the configuration was defined using define() statements |
||
83 | // Attention: This does mean that a full re-init (e.g. for test cases) is not possible |
||
84 | // if a version 2.0 config is used! |
||
294 | daniel-mar | 85 | include_once $config_file; |
263 | daniel-mar | 86 | } else { |
294 | daniel-mar | 87 | include $config_file; |
263 | daniel-mar | 88 | } |
261 | daniel-mar | 89 | |
90 | if (defined('OIDPLUS_CONFIG_VERSION') && (OIDPLUS_CONFIG_VERSION == 2.0)) { |
||
263 | daniel-mar | 91 | self::$old_config_format = true; |
274 | daniel-mar | 92 | |
261 | daniel-mar | 93 | // Backwards compatibility 2.0 => 2.1 |
94 | foreach (get_defined_constants(true)['user'] as $name => $value) { |
||
95 | $name = str_replace('OIDPLUS_', '', $name); |
||
96 | if ($name == 'SESSION_SECRET') $name = 'SERVER_SECRET'; |
||
97 | if ($name == 'MYSQL_QUERYLOG') $name = 'QUERY_LOGFILE'; |
||
98 | if (($name == 'MYSQL_PASSWORD') || ($name == 'ODBC_PASSWORD') || ($name == 'PDO_PASSWORD') || ($name == 'PGSQL_PASSWORD')) { |
||
263 | daniel-mar | 99 | self::$baseConfig->setValue($name, base64_decode($value)); |
261 | daniel-mar | 100 | } else { |
101 | if ($name == 'CONFIG_VERSION') $value = 2.1; |
||
263 | daniel-mar | 102 | self::$baseConfig->setValue($name, $value); |
261 | daniel-mar | 103 | } |
104 | } |
||
105 | } |
||
106 | } else { |
||
496 | daniel-mar | 107 | if (!is_dir(OIDplus::localpath().'setup')) { |
360 | daniel-mar | 108 | throw new OIDplusConfigInitializationException(_L('File %1 is missing, but setup can\'t be started because its directory missing.','userdata/baseconfig/config.inc.php')); |
261 | daniel-mar | 109 | } else { |
280 | daniel-mar | 110 | if (self::$html) { |
496 | daniel-mar | 111 | if (strpos($_SERVER['REQUEST_URI'], OIDplus::webpath(null,true).'setup/') !== 0) { |
112 | header('Location:'.OIDplus::webpath().'setup/'); |
||
360 | daniel-mar | 113 | die(_L('Redirecting to setup...')); |
349 | daniel-mar | 114 | } else { |
115 | return self::$baseConfig; |
||
116 | } |
||
261 | daniel-mar | 117 | } else { |
118 | // This can be displayed in e.g. ajax.php |
||
360 | daniel-mar | 119 | throw new OIDplusConfigInitializationException(_L('File %1 is missing. Please run setup again.','userdata/baseconfig/config.inc.php')); |
261 | daniel-mar | 120 | } |
121 | } |
||
122 | } |
||
123 | |||
124 | // Check important config settings |
||
125 | |||
263 | daniel-mar | 126 | if (self::$baseConfig->getValue('CONFIG_VERSION') != 2.1) { |
503 | daniel-mar | 127 | if (strpos($_SERVER['REQUEST_URI'], OIDplus::webpath(null,true).'setup/') !== 0) { |
128 | throw new OIDplusConfigInitializationException(_L("The information located in %1 is outdated.",realpath($config_file))); |
||
129 | } |
||
261 | daniel-mar | 130 | } |
131 | |||
263 | daniel-mar | 132 | if (self::$baseConfig->getValue('SERVER_SECRET', '') === '') { |
503 | daniel-mar | 133 | if (strpos($_SERVER['REQUEST_URI'], OIDplus::webpath(null,true).'setup/') !== 0) { |
134 | throw new OIDplusConfigInitializationException(_L("You must set a value for SERVER_SECRET in %1 for the system to operate secure.",realpath($config_file))); |
||
135 | } |
||
261 | daniel-mar | 136 | } |
137 | } |
||
138 | |||
263 | daniel-mar | 139 | return self::$baseConfig; |
261 | daniel-mar | 140 | } |
141 | |||
263 | daniel-mar | 142 | private static $config = null; |
2 | daniel-mar | 143 | public static function config() { |
263 | daniel-mar | 144 | if ($first_init = is_null(self::$config)) { |
145 | self::$config = new OIDplusConfig(); |
||
2 | daniel-mar | 146 | } |
263 | daniel-mar | 147 | |
148 | if ($first_init) { |
||
149 | // These are important settings for base functionalities and therefore are not inside plugins |
||
150 | self::$config->prepareConfigKey('system_title', 'What is the name of your RA?', 'OIDplus 2.0', OIDplusConfig::PROTECTION_EDITABLE, function($value) { |
||
151 | if (empty($value)) { |
||
360 | daniel-mar | 152 | throw new OIDplusException(_L('Please enter a value for the system title.')); |
263 | daniel-mar | 153 | } |
154 | }); |
||
155 | self::$config->prepareConfigKey('admin_email', 'E-Mail address of the system administrator', '', OIDplusConfig::PROTECTION_EDITABLE, function($value) { |
||
156 | if (!empty($value) && !OIDplus::mailUtils()->validMailAddress($value)) { |
||
360 | daniel-mar | 157 | throw new OIDplusException(_L('This is not a correct email address')); |
263 | daniel-mar | 158 | } |
159 | }); |
||
160 | self::$config->prepareConfigKey('global_cc', 'Global CC for all outgoing emails?', '', OIDplusConfig::PROTECTION_EDITABLE, function($value) { |
||
161 | if (!empty($value) && !OIDplus::mailUtils()->validMailAddress($value)) { |
||
360 | daniel-mar | 162 | throw new OIDplusException(_L('This is not a correct email address')); |
263 | daniel-mar | 163 | } |
164 | }); |
||
165 | self::$config->prepareConfigKey('objecttypes_initialized', 'List of object type plugins that were initialized once', '', OIDplusConfig::PROTECTION_READONLY, function($value) { |
||
166 | // Nothing here yet |
||
167 | }); |
||
168 | 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) { |
||
169 | # 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? |
||
170 | |||
171 | $ary = explode(';',$value); |
||
172 | $uniq_ary = array_unique($ary); |
||
173 | |||
174 | if (count($ary) != count($uniq_ary)) { |
||
360 | daniel-mar | 175 | throw new OIDplusException(_L('Please check your input. Some object types are double.')); |
263 | daniel-mar | 176 | } |
177 | |||
178 | foreach ($ary as $ot_check) { |
||
179 | $ns_found = false; |
||
180 | foreach (OIDplus::getEnabledObjectTypes() as $ot) { |
||
181 | if ($ot::ns() == $ot_check) { |
||
182 | $ns_found = true; |
||
183 | break; |
||
184 | } |
||
185 | } |
||
186 | foreach (OIDplus::getDisabledObjectTypes() as $ot) { |
||
187 | if ($ot::ns() == $ot_check) { |
||
188 | $ns_found = true; |
||
189 | break; |
||
190 | } |
||
191 | } |
||
192 | if (!$ns_found) { |
||
360 | daniel-mar | 193 | throw new OIDplusException(_L('Please check your input. Namespace "%1" is not found',$ot_check)); |
263 | daniel-mar | 194 | } |
195 | } |
||
196 | }); |
||
197 | self::$config->prepareConfigKey('oidplus_private_key', 'Private key for this system', '', OIDplusConfig::PROTECTION_HIDDEN, function($value) { |
||
198 | // Nothing here yet |
||
199 | }); |
||
200 | 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) { |
||
201 | // Nothing here yet |
||
202 | }); |
||
324 | daniel-mar | 203 | self::$config->prepareConfigKey('last_known_system_url', 'Last known System URL', '', OIDplusConfig::PROTECTION_HIDDEN, function($value) { |
204 | // Nothing here yet |
||
205 | }); |
||
412 | daniel-mar | 206 | self::$config->prepareConfigKey('last_known_version', 'Last known OIDplus Version', '', OIDplusConfig::PROTECTION_HIDDEN, function($value) { |
207 | // Nothing here yet |
||
208 | }); |
||
635 | daniel-mar | 209 | self::$config->prepareConfigKey('default_ra_auth_method', 'Default auth method used for generating password of RAs (must exist in plugins/[vendorname]/auth/)?', 'A3_bcrypt', OIDplusConfig::PROTECTION_EDITABLE, function($value) { |
453 | daniel-mar | 210 | $good = true; |
211 | if (strpos($value,'/') !== false) $good = false; |
||
212 | if (strpos($value,'\\') !== false) $good = false; |
||
213 | if (strpos($value,'..') !== false) $good = false; |
||
214 | if (!$good) { |
||
215 | throw new OIDplusException(_L('Invalid auth plugin folder name. Do only enter a folder name, not an absolute or relative path')); |
||
216 | } |
||
217 | |||
635 | daniel-mar | 218 | if (!rec_is_dir(OIDplus::localpath().'plugins/'.'*'.'/auth/'.$value)) { |
219 | throw new OIDplusException(_L('The auth plugin "%1" does not exist in plugin directory %2',$value,'plugins/[vendorname]/auth/')); |
||
453 | daniel-mar | 220 | } |
221 | }); |
||
263 | daniel-mar | 222 | } |
223 | |||
224 | return self::$config; |
||
2 | daniel-mar | 225 | } |
226 | |||
263 | daniel-mar | 227 | private static $gui = null; |
2 | daniel-mar | 228 | public static function gui() { |
263 | daniel-mar | 229 | if (is_null(self::$gui)) { |
230 | self::$gui = new OIDplusGui(); |
||
86 | daniel-mar | 231 | } |
263 | daniel-mar | 232 | return self::$gui; |
2 | daniel-mar | 233 | } |
234 | |||
263 | daniel-mar | 235 | private static $authUtils = null; |
2 | daniel-mar | 236 | public static function authUtils() { |
263 | daniel-mar | 237 | if (is_null(self::$authUtils)) { |
238 | self::$authUtils = new OIDplusAuthUtils(); |
||
86 | daniel-mar | 239 | } |
263 | daniel-mar | 240 | return self::$authUtils; |
2 | daniel-mar | 241 | } |
242 | |||
263 | daniel-mar | 243 | private static $mailUtils = null; |
250 | daniel-mar | 244 | public static function mailUtils() { |
263 | daniel-mar | 245 | if (is_null(self::$mailUtils)) { |
246 | self::$mailUtils = new OIDplusMailUtils(); |
||
250 | daniel-mar | 247 | } |
263 | daniel-mar | 248 | return self::$mailUtils; |
250 | daniel-mar | 249 | } |
250 | |||
557 | daniel-mar | 251 | private static $cookieUtils = null; |
252 | public static function cookieUtils() { |
||
253 | if (is_null(self::$cookieUtils)) { |
||
254 | self::$cookieUtils = new OIDplusCookieUtils(); |
||
255 | } |
||
256 | return self::$cookieUtils; |
||
257 | } |
||
258 | |||
263 | daniel-mar | 259 | private static $menuUtils = null; |
250 | daniel-mar | 260 | public static function menuUtils() { |
263 | daniel-mar | 261 | if (is_null(self::$menuUtils)) { |
262 | self::$menuUtils = new OIDplusMenuUtils(); |
||
250 | daniel-mar | 263 | } |
263 | daniel-mar | 264 | return self::$menuUtils; |
250 | daniel-mar | 265 | } |
266 | |||
263 | daniel-mar | 267 | private static $logger = null; |
115 | daniel-mar | 268 | public static function logger() { |
263 | daniel-mar | 269 | if (is_null(self::$logger)) { |
270 | self::$logger = new OIDplusLogger(); |
||
115 | daniel-mar | 271 | } |
263 | daniel-mar | 272 | return self::$logger; |
115 | daniel-mar | 273 | } |
274 | |||
274 | daniel-mar | 275 | # --- SQL slang plugin |
276 | |||
277 | private static function registerSqlSlangPlugin(OIDplusSqlSlangPlugin $plugin) { |
||
278 | $name = $plugin::id(); |
||
591 | daniel-mar | 279 | if ($name === '') return false; |
274 | daniel-mar | 280 | |
449 | daniel-mar | 281 | if (isset(self::$sqlSlangPlugins[$name])) { |
451 | daniel-mar | 282 | $plugintype_hf = _L('SQL slang'); |
592 | daniel-mar | 283 | throw new OIDplusException(_L('Multiple %1 plugins use the ID %2', $plugintype_hf, $name)); |
449 | daniel-mar | 284 | } |
285 | |||
274 | daniel-mar | 286 | self::$sqlSlangPlugins[$name] = $plugin; |
287 | |||
288 | return true; |
||
289 | } |
||
290 | |||
291 | public static function getSqlSlangPlugins() { |
||
292 | return self::$sqlSlangPlugins; |
||
293 | } |
||
294 | |||
318 | daniel-mar | 295 | public static function getSqlSlangPlugin($id)/*: ?OIDplusSqlSlangPlugin*/ { |
296 | if (isset(self::$sqlSlangPlugins[$id])) { |
||
297 | return self::$sqlSlangPlugins[$id]; |
||
298 | } else { |
||
299 | return null; |
||
300 | } |
||
301 | } |
||
302 | |||
230 | daniel-mar | 303 | # --- Database plugin |
74 | daniel-mar | 304 | |
227 | daniel-mar | 305 | private static function registerDatabasePlugin(OIDplusDatabasePlugin $plugin) { |
275 | daniel-mar | 306 | $name = $plugin::id(); |
591 | daniel-mar | 307 | if ($name === '') return false; |
150 | daniel-mar | 308 | |
449 | daniel-mar | 309 | if (isset(self::$dbPlugins[$name])) { |
310 | $plugintype_hf = _L('Database'); |
||
592 | daniel-mar | 311 | throw new OIDplusException(_L('Multiple %1 plugins use the ID %2', $plugintype_hf, $name)); |
449 | daniel-mar | 312 | } |
313 | |||
150 | daniel-mar | 314 | self::$dbPlugins[$name] = $plugin; |
315 | |||
316 | return true; |
||
317 | } |
||
318 | |||
319 | public static function getDatabasePlugins() { |
||
320 | return self::$dbPlugins; |
||
321 | } |
||
322 | |||
295 | daniel-mar | 323 | public static function getActiveDatabasePlugin() { |
702 | daniel-mar | 324 | $db_plugin_name = OIDplus::baseConfig()->getValue('DATABASE_PLUGIN',''); |
325 | if ($db_plugin_name === '') { |
||
360 | daniel-mar | 326 | throw new OIDplusConfigInitializationException(_L('No database plugin selected in config file')); |
260 | daniel-mar | 327 | } |
702 | daniel-mar | 328 | if (!isset(self::$dbPlugins[$db_plugin_name])) { |
360 | daniel-mar | 329 | throw new OIDplusConfigInitializationException(_L('Database plugin "%1" not found',$db_plugin_name)); |
227 | daniel-mar | 330 | } |
702 | daniel-mar | 331 | return self::$dbPlugins[$db_plugin_name]; |
227 | daniel-mar | 332 | } |
333 | |||
295 | daniel-mar | 334 | private static $dbMainSession = null; |
335 | public static function db() { |
||
336 | if (is_null(self::$dbMainSession)) { |
||
337 | self::$dbMainSession = self::getActiveDatabasePlugin()->newConnection(); |
||
338 | } |
||
339 | if (!self::$dbMainSession->isConnected()) self::$dbMainSession->connect(); |
||
340 | return self::$dbMainSession; |
||
341 | } |
||
342 | |||
343 | private static $dbIsolatedSession = null; |
||
344 | public static function dbIsolated() { |
||
345 | if (is_null(self::$dbIsolatedSession)) { |
||
346 | self::$dbIsolatedSession = self::getActiveDatabasePlugin()->newConnection(); |
||
347 | } |
||
348 | if (!self::$dbIsolatedSession->isConnected()) self::$dbIsolatedSession->connect(); |
||
349 | return self::$dbIsolatedSession; |
||
350 | } |
||
351 | |||
702 | daniel-mar | 352 | # --- CAPTCHA plugin |
353 | |||
354 | private static function registerCaptchaPlugin(OIDplusCaptchaPlugin $plugin) { |
||
355 | $name = $plugin::id(); |
||
356 | if ($name === '') return false; |
||
357 | |||
358 | if (isset(self::$captchaPlugins[$name])) { |
||
359 | $plugintype_hf = _L('CAPTCHA'); |
||
360 | throw new OIDplusException(_L('Multiple %1 plugins use the ID %2', $plugintype_hf, $name)); |
||
361 | } |
||
362 | |||
363 | self::$captchaPlugins[$name] = $plugin; |
||
364 | |||
365 | return true; |
||
366 | } |
||
367 | |||
368 | public static function getCaptchaPlugins() { |
||
369 | return self::$captchaPlugins; |
||
370 | } |
||
371 | |||
704 | daniel-mar | 372 | public static function getActiveCaptchaPluginId() { |
702 | daniel-mar | 373 | $captcha_plugin_name = OIDplus::baseConfig()->getValue('CAPTCHA_PLUGIN', ''); |
374 | |||
375 | if (OIDplus::baseConfig()->getValue('RECAPTCHA_ENABLED', false) && ($captcha_plugin_name === '')) { |
||
376 | // Legacy config file support! |
||
377 | $captcha_plugin_name = 'ReCAPTCHA'; |
||
378 | } |
||
379 | |||
704 | daniel-mar | 380 | if ($captcha_plugin_name === '') $captcha_plugin_name = 'None'; // the "None" plugin is a must-have! |
702 | daniel-mar | 381 | |
704 | daniel-mar | 382 | return $captcha_plugin_name; |
383 | } |
||
384 | |||
385 | public static function getActiveCaptchaPlugin() { |
||
386 | $captcha_plugin_name = OIDplus::getActiveCaptchaPluginId(); |
||
387 | |||
702 | daniel-mar | 388 | if (!isset(self::$captchaPlugins[$captcha_plugin_name])) { |
389 | throw new OIDplusConfigInitializationException(_L('CAPTCHA plugin "%1" not found',$captcha_plugin_name)); |
||
390 | } |
||
391 | return self::$captchaPlugins[$captcha_plugin_name]; |
||
392 | } |
||
393 | |||
227 | daniel-mar | 394 | # --- Page plugin |
395 | |||
224 | daniel-mar | 396 | private static function registerPagePlugin(OIDplusPagePlugin $plugin) { |
281 | daniel-mar | 397 | self::$pagePlugins[] = $plugin; |
61 | daniel-mar | 398 | |
399 | return true; |
||
400 | } |
||
401 | |||
281 | daniel-mar | 402 | public static function getPagePlugins() { |
403 | return self::$pagePlugins; |
||
61 | daniel-mar | 404 | } |
405 | |||
227 | daniel-mar | 406 | # --- Auth plugin |
407 | |||
408 | private static function registerAuthPlugin(OIDplusAuthPlugin $plugin) { |
||
456 | daniel-mar | 409 | if (OIDplus::baseConfig()->getValue('DEBUG')) { |
410 | $password = generateRandomString(25); |
||
459 | daniel-mar | 411 | |
461 | daniel-mar | 412 | try { |
413 | $authInfo = $plugin->generate($password); |
||
414 | } catch (OIDplusException $e) { |
||
415 | // This can happen when the AuthKey or Salt is too long |
||
416 | throw new OIDplusException(_L('Auth plugin "%1" is erroneous: %2',basename($plugin->getPluginDirectory()),$e->getMessage())); |
||
417 | } |
||
459 | daniel-mar | 418 | $salt = $authInfo->getSalt(); |
461 | daniel-mar | 419 | $authKey = $authInfo->getAuthKey(); |
459 | daniel-mar | 420 | |
461 | daniel-mar | 421 | $authInfo_SaltDiff = clone $authInfo; |
422 | $authInfo_SaltDiff->setSalt(strrev($authInfo_SaltDiff->getSalt())); |
||
423 | |||
424 | $authInfo_AuthKeyDiff = clone $authInfo; |
||
425 | $authInfo_AuthKeyDiff->setAuthKey(strrev($authInfo_AuthKeyDiff->getAuthKey())); |
||
426 | |||
427 | if ((!$plugin->verify($authInfo,$password)) || |
||
428 | (!empty($salt) && $plugin->verify($authInfo_SaltDiff,$password)) || |
||
429 | ($plugin->verify($authInfo_AuthKeyDiff,$password)) || |
||
430 | ($plugin->verify($authInfo,$password.'x'))) { |
||
456 | daniel-mar | 431 | throw new OIDplusException(_L('Auth plugin "%1" is erroneous: Generate/Verify self test failed',basename($plugin->getPluginDirectory()))); |
432 | } |
||
453 | daniel-mar | 433 | } |
434 | |||
227 | daniel-mar | 435 | self::$authPlugins[] = $plugin; |
436 | return true; |
||
437 | } |
||
438 | |||
221 | daniel-mar | 439 | public static function getAuthPlugins() { |
440 | return self::$authPlugins; |
||
441 | } |
||
442 | |||
355 | daniel-mar | 443 | # --- Language plugin |
444 | |||
445 | private static function registerLanguagePlugin(OIDplusLanguagePlugin $plugin) { |
||
446 | self::$languagePlugins[] = $plugin; |
||
447 | return true; |
||
448 | } |
||
449 | |||
450 | public static function getLanguagePlugins() { |
||
451 | return self::$languagePlugins; |
||
452 | } |
||
453 | |||
449 | daniel-mar | 454 | # --- Design plugin |
455 | |||
456 | private static function registerDesignPlugin(OIDplusDesignPlugin $plugin) { |
||
457 | self::$designPlugins[] = $plugin; |
||
458 | return true; |
||
459 | } |
||
460 | |||
461 | public static function getDesignPlugins() { |
||
462 | return self::$designPlugins; |
||
463 | } |
||
464 | |||
289 | daniel-mar | 465 | # --- Logger plugin |
466 | |||
467 | private static function registerLoggerPlugin(OIDplusLoggerPlugin $plugin) { |
||
468 | self::$loggerPlugins[] = $plugin; |
||
469 | return true; |
||
470 | } |
||
471 | |||
472 | public static function getLoggerPlugins() { |
||
473 | return self::$loggerPlugins; |
||
474 | } |
||
475 | |||
227 | daniel-mar | 476 | # --- Object type plugin |
477 | |||
478 | private static function registerObjectTypePlugin(OIDplusObjectTypePlugin $plugin) { |
||
479 | self::$objectTypePlugins[] = $plugin; |
||
480 | |||
481 | $ot = $plugin::getObjectTypeClassName(); |
||
482 | self::registerObjectType($ot); |
||
483 | |||
484 | return true; |
||
485 | } |
||
486 | |||
224 | daniel-mar | 487 | private static function registerObjectType($ot) { |
66 | daniel-mar | 488 | $ns = $ot::ns(); |
489 | |||
360 | daniel-mar | 490 | if (empty($ns)) throw new OIDplusException(_L('Attention: Empty NS at %1',$ot)); |
66 | daniel-mar | 491 | |
492 | $ns_found = false; |
||
227 | daniel-mar | 493 | foreach (array_merge(OIDplus::getEnabledObjectTypes(), OIDplus::getDisabledObjectTypes()) as $test_ot) { |
66 | daniel-mar | 494 | if ($test_ot::ns() == $ns) { |
495 | $ns_found = true; |
||
496 | break; |
||
497 | } |
||
498 | } |
||
499 | if ($ns_found) { |
||
360 | daniel-mar | 500 | throw new OIDplusException(_L('Attention: Two objectType plugins use the same namespace "%1"!',$ns)); |
66 | daniel-mar | 501 | } |
502 | |||
503 | $init = OIDplus::config()->getValue("objecttypes_initialized"); |
||
504 | $init_ary = empty($init) ? array() : explode(';', $init); |
||
70 | daniel-mar | 505 | $init_ary = array_map('trim', $init_ary); |
66 | daniel-mar | 506 | |
507 | $enabled = OIDplus::config()->getValue("objecttypes_enabled"); |
||
508 | $enabled_ary = empty($enabled) ? array() : explode(';', $enabled); |
||
70 | daniel-mar | 509 | $enabled_ary = array_map('trim', $enabled_ary); |
66 | daniel-mar | 510 | |
79 | daniel-mar | 511 | $do_enable = false; |
512 | if (in_array($ns, $enabled_ary)) { |
||
447 | daniel-mar | 513 | // If it is in the list of enabled object types, it is enabled (obviously) |
79 | daniel-mar | 514 | $do_enable = true; |
515 | } else { |
||
447 | daniel-mar | 516 | if (!OIDplus::config()->getValue('oobe_objects_done')) { |
517 | // If the OOBE wizard is NOT done, then just enable the "oid" object type by default |
||
79 | daniel-mar | 518 | $do_enable = $ns == 'oid'; |
519 | } else { |
||
447 | daniel-mar | 520 | // If the OOBE wizard was done (once), then |
521 | // we will enable all object types which were never initialized |
||
522 | // (i.e. a plugin folder was freshly added) |
||
79 | daniel-mar | 523 | $do_enable = !in_array($ns, $init_ary); |
524 | } |
||
525 | } |
||
526 | |||
527 | if ($do_enable) { |
||
227 | daniel-mar | 528 | self::$enabledObjectTypes[] = $ot; |
529 | usort(self::$enabledObjectTypes, function($a, $b) { |
||
66 | daniel-mar | 530 | $enabled = OIDplus::config()->getValue("objecttypes_enabled"); |
531 | $enabled_ary = explode(';', $enabled); |
||
532 | |||
533 | $idx_a = array_search($a::ns(), $enabled_ary); |
||
534 | $idx_b = array_search($b::ns(), $enabled_ary); |
||
535 | |||
536 | if ($idx_a == $idx_b) { |
||
537 | return 0; |
||
538 | } |
||
539 | return ($idx_a > $idx_b) ? +1 : -1; |
||
540 | }); |
||
74 | daniel-mar | 541 | } else { |
542 | self::$disabledObjectTypes[] = $ot; |
||
66 | daniel-mar | 543 | } |
544 | |||
545 | if (!in_array($ns, $init_ary)) { |
||
546 | // Was never initialized before, so we add it to the list of enabled object types once |
||
547 | |||
79 | daniel-mar | 548 | if ($do_enable) { |
549 | $enabled_ary[] = $ns; |
||
672 | daniel-mar | 550 | // Important: Don't validate the input, because the other object types might not be initialized yet! So use setValueNoCallback() instead setValue(). |
551 | OIDplus::config()->setValueNoCallback("objecttypes_enabled", implode(';', $enabled_ary)); |
||
79 | daniel-mar | 552 | } |
66 | daniel-mar | 553 | |
554 | $init_ary[] = $ns; |
||
555 | OIDplus::config()->setValue("objecttypes_initialized", implode(';', $init_ary)); |
||
556 | } |
||
61 | daniel-mar | 557 | } |
558 | |||
227 | daniel-mar | 559 | public static function getObjectTypePlugins() { |
560 | return self::$objectTypePlugins; |
||
61 | daniel-mar | 561 | } |
562 | |||
227 | daniel-mar | 563 | public static function getObjectTypePluginsEnabled() { |
564 | $res = array(); |
||
565 | foreach (self::$objectTypePlugins as $plugin) { |
||
566 | $ot = $plugin::getObjectTypeClassName(); |
||
567 | if (in_array($ot, self::$enabledObjectTypes)) $res[] = $plugin; |
||
568 | } |
||
569 | return $res; |
||
74 | daniel-mar | 570 | } |
571 | |||
227 | daniel-mar | 572 | public static function getObjectTypePluginsDisabled() { |
573 | $res = array(); |
||
574 | foreach (self::$objectTypePlugins as $plugin) { |
||
575 | $ot = $plugin::getObjectTypeClassName(); |
||
576 | if (in_array($ot, self::$disabledObjectTypes)) $res[] = $plugin; |
||
74 | daniel-mar | 577 | } |
227 | daniel-mar | 578 | return $res; |
74 | daniel-mar | 579 | } |
580 | |||
227 | daniel-mar | 581 | public static function getEnabledObjectTypes() { |
582 | return self::$enabledObjectTypes; |
||
583 | } |
||
74 | daniel-mar | 584 | |
227 | daniel-mar | 585 | public static function getDisabledObjectTypes() { |
586 | return self::$disabledObjectTypes; |
||
587 | } |
||
74 | daniel-mar | 588 | |
277 | daniel-mar | 589 | # --- Plugin handling functions |
590 | |||
320 | daniel-mar | 591 | public static function getAllPlugins()/*: array*/ { |
592 | $res = array(); |
||
593 | $res = array_merge($res, self::$pagePlugins); |
||
594 | $res = array_merge($res, self::$authPlugins); |
||
595 | $res = array_merge($res, self::$loggerPlugins); |
||
596 | $res = array_merge($res, self::$objectTypePlugins); |
||
597 | $res = array_merge($res, self::$dbPlugins); |
||
702 | daniel-mar | 598 | $res = array_merge($res, self::$captchaPlugins); |
320 | daniel-mar | 599 | $res = array_merge($res, self::$sqlSlangPlugins); |
355 | daniel-mar | 600 | $res = array_merge($res, self::$languagePlugins); |
449 | daniel-mar | 601 | $res = array_merge($res, self::$designPlugins); |
320 | daniel-mar | 602 | return $res; |
603 | } |
||
604 | |||
321 | daniel-mar | 605 | public static function getPluginByOid($oid)/*: ?OIDplusPlugin*/ { |
320 | daniel-mar | 606 | $plugins = self::getAllPlugins(); |
321 | daniel-mar | 607 | foreach ($plugins as $plugin) { |
608 | if (oid_dotnotation_equal($plugin->getManifest()->getOid(), $oid)) { |
||
609 | return $plugin; |
||
320 | daniel-mar | 610 | } |
611 | } |
||
612 | return null; |
||
613 | } |
||
614 | |||
380 | daniel-mar | 615 | public static function getPluginByClassName($classname)/*: ?OIDplusPlugin*/ { |
616 | $plugins = self::getAllPlugins(); |
||
617 | foreach ($plugins as $plugin) { |
||
618 | if (get_class($plugin) === $classname) { |
||
619 | return $plugin; |
||
620 | } |
||
621 | } |
||
622 | return null; |
||
277 | daniel-mar | 623 | } |
624 | |||
594 | daniel-mar | 625 | /** |
626 | * @return array<OIDplusPluginManifest>|array<string,array<string,OIDplusPluginManifest>> |
||
627 | */ |
||
693 | daniel-mar | 628 | public static function getAllPluginManifests($pluginFolderMasks='*', $flat=true): array { |
277 | daniel-mar | 629 | $out = array(); |
279 | daniel-mar | 630 | // Note: glob() will sort by default, so we do not need a page priority attribute. |
631 | // So you just need to use a numeric plugin directory prefix (padded). |
||
693 | daniel-mar | 632 | $ary = array(); |
633 | foreach (explode(',',$pluginFolderMasks) as $pluginFolderMask) { |
||
634 | $ary = array_merge($ary,glob(OIDplus::localpath().'plugins/'.'*'.'/'.$pluginFolderMask.'/'.'*'.'/manifest.xml')); |
||
635 | } |
||
646 | daniel-mar | 636 | |
637 | // Sort the plugins by their type and name, as if they would be in a single vendor-folder! |
||
638 | uasort($ary, function($a,$b) { |
||
639 | if ($a == $b) return 0; |
||
640 | |||
641 | $ary = explode('/',$a); |
||
642 | $bry = explode('/',$b); |
||
643 | |||
644 | // First sort by type (publicPage, auth, database, language, ...) |
||
645 | $a_type = $ary[count($ary)-1-2]; |
||
646 | $b_type = $bry[count($bry)-1-2]; |
||
647 | if ($a_type < $b_type) return -1; |
||
648 | if ($a_type > $b_type) return 1; |
||
649 | |||
650 | // Then sort by name (090_login, 100_whois, etc.) |
||
651 | $a_name = $ary[count($ary)-1-1]; |
||
652 | $b_name = $bry[count($bry)-1-1]; |
||
653 | if ($a_name < $b_name) return -1; |
||
654 | if ($a_name > $b_name) return 1; |
||
655 | |||
656 | // If it is still equal, then finally sort by vendorname |
||
657 | $a_vendor = $ary[count($ary)-1-3]; |
||
658 | $b_vendor = $bry[count($bry)-1-3]; |
||
659 | if ($a_vendor < $b_vendor) return -1; |
||
660 | if ($a_vendor > $b_vendor) return 1; |
||
661 | return 0; |
||
662 | }); |
||
663 | |||
277 | daniel-mar | 664 | foreach ($ary as $ini) { |
665 | if (!file_exists($ini)) continue; |
||
666 | |||
307 | daniel-mar | 667 | $manifest = new OIDplusPluginManifest(); |
668 | $manifest->loadManifest($ini); |
||
277 | daniel-mar | 669 | |
473 | daniel-mar | 670 | $class_name = $manifest->getPhpMainClass(); |
671 | if (OIDplus::baseConfig()->getValue('DISABLE_PLUGIN_'.$class_name, false)) { |
||
672 | continue; |
||
673 | } |
||
674 | |||
307 | daniel-mar | 675 | if ($flat) { |
676 | $out[] = $manifest; |
||
677 | } else { |
||
678 | $plugintype_folder = basename(dirname(dirname($ini))); |
||
679 | $pluginname_folder = basename(dirname($ini)); |
||
680 | |||
681 | if (!isset($out[$plugintype_folder])) $out[$plugintype_folder] = array(); |
||
682 | if (!isset($out[$plugintype_folder][$pluginname_folder])) $out[$plugintype_folder][$pluginname_folder] = array(); |
||
683 | $out[$plugintype_folder][$pluginname_folder] = $manifest; |
||
684 | } |
||
277 | daniel-mar | 685 | } |
686 | return $out; |
||
687 | } |
||
688 | |||
594 | daniel-mar | 689 | /** |
690 | * @return array<string> |
||
691 | */ |
||
277 | daniel-mar | 692 | public static function registerAllPlugins($pluginDirName, $expectedPluginClass, $registerCallback): array { |
693 | $out = array(); |
||
525 | daniel-mar | 694 | if (is_array($pluginDirName)) { |
695 | $ary = array(); |
||
696 | foreach ($pluginDirName as $pluginDirName_) { |
||
697 | $ary = array_merge($ary, self::getAllPluginManifests($pluginDirName_, false)); |
||
698 | } |
||
699 | } else { |
||
700 | $ary = self::getAllPluginManifests($pluginDirName, false); |
||
701 | } |
||
320 | daniel-mar | 702 | $known_plugin_oids = array(); |
456 | daniel-mar | 703 | if (OIDplus::baseConfig()->getValue('DEBUG')) { |
704 | $fake_feature = uuid_to_oid(gen_uuid()); |
||
588 | daniel-mar | 705 | } else { |
706 | $fake_feature = null; |
||
456 | daniel-mar | 707 | } |
277 | daniel-mar | 708 | foreach ($ary as $plugintype_folder => $bry) { |
438 | daniel-mar | 709 | foreach ($bry as $pluginname_folder => $manifest) { |
710 | $class_name = $manifest->getPhpMainClass(); |
||
711 | |||
712 | // Before we load the plugin, we want to make some checks to confirm |
||
713 | // that the plugin is working correctly. |
||
714 | |||
307 | daniel-mar | 715 | if (!$class_name) { |
438 | daniel-mar | 716 | throw new OIDplusException(_L('Plugin "%1/%2" is erroneous',$plugintype_folder,$pluginname_folder).': '._L('Manifest does not declare a PHP main class')); |
277 | daniel-mar | 717 | } |
297 | daniel-mar | 718 | if (OIDplus::baseConfig()->getValue('DISABLE_PLUGIN_'.$class_name, false)) { |
719 | continue; |
||
720 | } |
||
292 | daniel-mar | 721 | if (!class_exists($class_name)) { |
438 | daniel-mar | 722 | throw new OIDplusException(_L('Plugin "%1/%2" 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 | 723 | } |
279 | daniel-mar | 724 | if (!is_subclass_of($class_name, $expectedPluginClass)) { |
438 | daniel-mar | 725 | throw new OIDplusException(_L('Plugin "%1/%2" 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 | 726 | } |
438 | daniel-mar | 727 | if (($class_name!=$manifest->getTypeClass()) && (!is_subclass_of($class_name,$manifest->getTypeClass()))) { |
728 | throw new OIDplusException(_L('Plugin "%1/%2" 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 | 729 | } |
438 | daniel-mar | 730 | if (($manifest->getTypeClass()!=$expectedPluginClass) && (!is_subclass_of($manifest->getTypeClass(),$expectedPluginClass))) { |
731 | throw new OIDplusException(_L('Plugin "%1/%2" 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 | 732 | } |
733 | |||
438 | daniel-mar | 734 | $plugin_oid = $manifest->getOid(); |
320 | daniel-mar | 735 | if (!$plugin_oid) { |
438 | daniel-mar | 736 | throw new OIDplusException(_L('Plugin "%1/%2" is erroneous',$plugintype_folder,$pluginname_folder).': '._L('Does not have an OID')); |
320 | daniel-mar | 737 | } |
738 | if (!oid_valid_dotnotation($plugin_oid, false, false, 2)) { |
||
438 | daniel-mar | 739 | throw new OIDplusException(_L('Plugin "%1/%2" is erroneous',$plugintype_folder,$pluginname_folder).': '._L('Plugin OID "%1" is invalid (needs to be valid dot-notation)',$plugin_oid)); |
320 | daniel-mar | 740 | } |
741 | if (isset($known_plugin_oids[$plugin_oid])) { |
||
438 | daniel-mar | 742 | throw new OIDplusException(_L('Plugin "%1/%2" 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 | 743 | } |
646 | daniel-mar | 744 | |
632 | daniel-mar | 745 | $full_plugin_dir = dirname($manifest->getManifestFile()); |
746 | $full_plugin_dir = substr($full_plugin_dir, strlen(OIDplus::localpath())); |
||
633 | daniel-mar | 747 | // { iso(1) identified-organization(3) dod(6) internet(1) private(4) enterprise(1) 37476 products(2) oidplus(5) v2(2) plugins(4) } |
635 | daniel-mar | 748 | if (str_starts_with($full_plugin_dir, 'plugins/viathinksoft/') != str_starts_with($plugin_oid, '1.3.6.1.4.1.37476.2.5.2.4.')) { |
749 | throw new OIDplusException(_L('Plugin "%1/%2" 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 | 750 | } |
320 | daniel-mar | 751 | |
632 | daniel-mar | 752 | $known_plugin_oids[$plugin_oid] = $plugintype_folder.'/'.$pluginname_folder; |
753 | |||
438 | daniel-mar | 754 | $obj = new $class_name(); |
456 | daniel-mar | 755 | |
756 | if (OIDplus::baseConfig()->getValue('DEBUG')) { |
||
757 | if ($obj->implementsFeature($fake_feature)) { |
||
758 | // see https://devblogs.microsoft.com/oldnewthing/20040211-00/?p=40663 |
||
759 | throw new OIDplusException(_L('Plugin "%1/%2" is erroneous',$plugintype_folder,$pluginname_folder).': '._L('implementsFeature() always returns true')); |
||
760 | } |
||
438 | daniel-mar | 761 | } |
762 | |||
778 | daniel-mar | 763 | // TODO: Maybe as additional plugin-test, we should also check if plugins are allowed to define CSS/JS, i.e. the plugin type is element of OIDplus::INTERACTIVE_PLUGIN_TYPES |
438 | daniel-mar | 764 | $tmp = array_merge( |
765 | $manifest->getJSFiles(), |
||
766 | $manifest->getCSSFiles(), |
||
767 | $manifest->getJSFilesSetup(), |
||
768 | $manifest->getCSSFilesSetup() |
||
769 | ); |
||
770 | foreach ($tmp as $file) { |
||
771 | if (!file_exists($file)) { |
||
772 | throw new OIDplusException(_L('Plugin "%1/%2" is erroneous',$plugintype_folder,$pluginname_folder).': '._L('File %1 was defined in manifest, but it is not existing',$file)); |
||
773 | } |
||
774 | } |
||
775 | |||
776 | // Now we can continue |
||
777 | |||
279 | daniel-mar | 778 | $out[] = $class_name; |
779 | if (!is_null($registerCallback)) { |
||
438 | daniel-mar | 780 | call_user_func($registerCallback, $obj); |
444 | daniel-mar | 781 | |
782 | // Alternative approaches: |
||
783 | //$registerCallback[0]::{$registerCallback[1]}($obj); |
||
784 | // or: |
||
785 | //forward_static_call($registerCallback, $obj); |
||
279 | daniel-mar | 786 | } |
277 | daniel-mar | 787 | } |
788 | |||
789 | } |
||
790 | return $out; |
||
791 | } |
||
792 | |||
227 | daniel-mar | 793 | # --- Initialization of OIDplus |
206 | daniel-mar | 794 | |
374 | daniel-mar | 795 | public static function init($html=true, $keepBaseConfig=true) { |
236 | daniel-mar | 796 | self::$html = $html; |
797 | |||
263 | daniel-mar | 798 | // Reset internal state, so we can re-init verything if required |
274 | daniel-mar | 799 | |
263 | daniel-mar | 800 | if (self::$old_config_format) { |
778 | daniel-mar | 801 | // We need to do this, because define() cannot be undone |
263 | daniel-mar | 802 | // Note: This can only happen in very special cases (e.g. test cases) where you call init() twice |
360 | daniel-mar | 803 | 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 | 804 | } |
274 | daniel-mar | 805 | |
263 | daniel-mar | 806 | self::$config = null; |
374 | daniel-mar | 807 | 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 | 808 | self::$gui = null; |
809 | self::$authUtils = null; |
||
810 | self::$mailUtils = null; |
||
811 | self::$menuUtils = null; |
||
812 | self::$logger = null; |
||
295 | daniel-mar | 813 | self::$dbMainSession = null; |
814 | self::$dbIsolatedSession = null; |
||
263 | daniel-mar | 815 | self::$pagePlugins = array(); |
816 | self::$authPlugins = array(); |
||
289 | daniel-mar | 817 | self::$loggerPlugins = array(); |
263 | daniel-mar | 818 | self::$objectTypePlugins = array(); |
819 | self::$enabledObjectTypes = array(); |
||
820 | self::$disabledObjectTypes = array(); |
||
821 | self::$dbPlugins = array(); |
||
702 | daniel-mar | 822 | self::$captchaPlugins = array(); |
274 | daniel-mar | 823 | self::$sqlSlangPlugins = array(); |
355 | daniel-mar | 824 | self::$languagePlugins = array(); |
449 | daniel-mar | 825 | self::$designPlugins = array(); |
263 | daniel-mar | 826 | self::$system_id_cache = null; |
827 | self::$sslAvailableCache = null; |
||
468 | daniel-mar | 828 | self::$translationArray = array(); |
74 | daniel-mar | 829 | |
263 | daniel-mar | 830 | // Continue... |
831 | |||
294 | daniel-mar | 832 | OIDplus::baseConfig(); // this loads the base configuration located in userdata/baseconfig/config.inc.php (once!) |
263 | daniel-mar | 833 | // You can do changes to the configuration afterwards using OIDplus::baseConfig()->... |
834 | |||
150 | daniel-mar | 835 | // Register database types (highest priority) |
836 | |||
274 | daniel-mar | 837 | // SQL slangs |
838 | |||
294 | daniel-mar | 839 | self::registerAllPlugins('sqlSlang', 'OIDplusSqlSlangPlugin', array('OIDplus','registerSqlSlangPlugin')); |
274 | daniel-mar | 840 | foreach (OIDplus::getSqlSlangPlugins() as $plugin) { |
841 | $plugin->init($html); |
||
842 | } |
||
843 | |||
844 | // Database providers |
||
845 | |||
277 | daniel-mar | 846 | self::registerAllPlugins('database', 'OIDplusDatabasePlugin', array('OIDplus','registerDatabasePlugin')); |
237 | daniel-mar | 847 | foreach (OIDplus::getDatabasePlugins() as $plugin) { |
848 | $plugin->init($html); |
||
849 | } |
||
850 | |||
42 | daniel-mar | 851 | // Do redirect stuff etc. |
74 | daniel-mar | 852 | |
230 | daniel-mar | 853 | self::isSslAvailable(); // This function does automatic redirects |
61 | daniel-mar | 854 | |
263 | daniel-mar | 855 | // Construct the configuration manager |
66 | daniel-mar | 856 | |
263 | daniel-mar | 857 | OIDplus::config(); // During the construction, various system settings are prepared if required |
66 | daniel-mar | 858 | |
74 | daniel-mar | 859 | // Initialize public / private keys |
860 | |||
227 | daniel-mar | 861 | OIDplus::getPkiStatus(true); |
74 | daniel-mar | 862 | |
237 | daniel-mar | 863 | // Register non-DB plugins |
74 | daniel-mar | 864 | |
525 | daniel-mar | 865 | self::registerAllPlugins(array('publicPages', 'raPages', 'adminPages'), 'OIDplusPagePlugin', array('OIDplus','registerPagePlugin')); |
277 | daniel-mar | 866 | self::registerAllPlugins('auth', 'OIDplusAuthPlugin', array('OIDplus','registerAuthPlugin')); |
289 | daniel-mar | 867 | self::registerAllPlugins('logger', 'OIDplusLoggerPlugin', array('OIDplus','registerLoggerPlugin')); |
277 | daniel-mar | 868 | self::registerAllPlugins('objectTypes', 'OIDplusObjectTypePlugin', array('OIDplus','registerObjectTypePlugin')); |
355 | daniel-mar | 869 | self::registerAllPlugins('language', 'OIDplusLanguagePlugin', array('OIDplus','registerLanguagePlugin')); |
449 | daniel-mar | 870 | self::registerAllPlugins('design', 'OIDplusDesignPlugin', array('OIDplus','registerDesignPlugin')); |
778 | daniel-mar | 871 | self::registerAllPlugins('captcha', 'OIDplusCaptchaPlugin', array('OIDplus','registerCaptchaPlugin')); |
150 | daniel-mar | 872 | |
237 | daniel-mar | 873 | // Initialize non-DB plugins |
224 | daniel-mar | 874 | |
281 | daniel-mar | 875 | foreach (OIDplus::getPagePlugins() as $plugin) { |
74 | daniel-mar | 876 | $plugin->init($html); |
877 | } |
||
230 | daniel-mar | 878 | foreach (OIDplus::getAuthPlugins() as $plugin) { |
879 | $plugin->init($html); |
||
880 | } |
||
289 | daniel-mar | 881 | foreach (OIDplus::getLoggerPlugins() as $plugin) { |
882 | $plugin->init($html); |
||
883 | } |
||
230 | daniel-mar | 884 | foreach (OIDplus::getObjectTypePlugins() as $plugin) { |
885 | $plugin->init($html); |
||
886 | } |
||
355 | daniel-mar | 887 | foreach (OIDplus::getLanguagePlugins() as $plugin) { |
888 | $plugin->init($html); |
||
889 | } |
||
449 | daniel-mar | 890 | foreach (OIDplus::getDesignPlugins() as $plugin) { |
891 | $plugin->init($html); |
||
892 | } |
||
778 | daniel-mar | 893 | foreach (OIDplus::getCaptchaPlugins() as $plugin) { |
894 | $plugin->init($html); |
||
895 | } |
||
412 | daniel-mar | 896 | |
778 | daniel-mar | 897 | if (PHP_SAPI != 'cli') { |
898 | |||
899 | // Prepare some security related response headers (default values) |
||
900 | |||
901 | $content_language = |
||
902 | strtolower(substr(OIDplus::getCurrentLang(),0,2)) . '-' . |
||
903 | strtoupper(substr(OIDplus::getCurrentLang(),2,2)); // e.g. 'en-US' |
||
904 | |||
905 | $http_headers = array( |
||
906 | "X-Content-Type-Options" => "nosniff", |
||
907 | "X-XSS-Protection" => "1; mode=block", |
||
908 | "X-Frame-Options" => "SAMEORIGIN", |
||
909 | "Referrer-Policy" => array( |
||
910 | "no-referrer-when-downgrade" |
||
911 | ), |
||
912 | "Cache-Control" => array( |
||
913 | "no-cache", |
||
914 | "no-store", |
||
915 | "must-revalidate" |
||
916 | ), |
||
917 | "Pragma" => "no-cache", |
||
918 | "Content-Language" => $content_language, |
||
919 | "Expires" => "0", |
||
920 | "Content-Security-Policy" => array( |
||
921 | "default-src" => array( |
||
922 | "'self'", |
||
923 | "blob:", |
||
924 | "https://fonts.gstatic.com", |
||
925 | "https://www.google.com/", |
||
926 | "https://www.gstatic.com/", |
||
927 | "https://cdnjs.cloudflare.com/" |
||
928 | ), |
||
929 | "style-src" => array( |
||
930 | "'self'", |
||
931 | "'unsafe-inline'", |
||
932 | "https://cdnjs.cloudflare.com/" |
||
933 | ), |
||
934 | "img-src" => array( |
||
935 | "blob:", |
||
936 | "data:", |
||
937 | "http:", |
||
938 | "https:" |
||
939 | ), |
||
940 | "script-src" => array( |
||
941 | "'self'", |
||
942 | "'unsafe-inline'", |
||
943 | "'unsafe-eval'", |
||
944 | "blob:", |
||
945 | "https://www.google.com/", |
||
946 | "https://www.gstatic.com/", |
||
947 | "https://cdnjs.cloudflare.com/", |
||
948 | "https://polyfill.io/" |
||
949 | ), |
||
950 | "frame-ancestors" => array( |
||
951 | "'none'" |
||
952 | ), |
||
953 | "object-src" => array( |
||
954 | "'none'" |
||
955 | ) |
||
956 | ) |
||
957 | ); |
||
958 | |||
959 | // Give plugins the possibility to manipulate/extend the headers |
||
960 | |||
961 | foreach (OIDplus::getSqlSlangPlugins() as $plugin) { |
||
962 | $plugin->httpHeaderCheck($http_headers); |
||
963 | } |
||
964 | foreach (OIDplus::getDatabasePlugins() as $plugin) { |
||
965 | $plugin->httpHeaderCheck($http_headers); |
||
966 | } |
||
967 | foreach (OIDplus::getPagePlugins() as $plugin) { |
||
968 | $plugin->httpHeaderCheck($http_headers); |
||
969 | } |
||
970 | foreach (OIDplus::getAuthPlugins() as $plugin) { |
||
971 | $plugin->httpHeaderCheck($http_headers); |
||
972 | } |
||
973 | foreach (OIDplus::getLoggerPlugins() as $plugin) { |
||
974 | $plugin->httpHeaderCheck($http_headers); |
||
975 | } |
||
976 | foreach (OIDplus::getObjectTypePlugins() as $plugin) { |
||
977 | $plugin->httpHeaderCheck($http_headers); |
||
978 | } |
||
979 | foreach (OIDplus::getLanguagePlugins() as $plugin) { |
||
980 | $plugin->httpHeaderCheck($http_headers); |
||
981 | } |
||
982 | foreach (OIDplus::getDesignPlugins() as $plugin) { |
||
983 | $plugin->httpHeaderCheck($http_headers); |
||
984 | } |
||
985 | foreach (OIDplus::getCaptchaPlugins() as $plugin) { |
||
986 | $plugin->httpHeaderCheck($http_headers); |
||
987 | } |
||
988 | |||
989 | // Prepare to send the headers to the client |
||
990 | // The headers are sent automatically when the first output comes or the script ends |
||
991 | |||
992 | foreach ($http_headers as $name => $val) { |
||
993 | |||
994 | // Plugins can remove standard OIDplus headers by setting the value to null. |
||
995 | if (is_null($val)) continue; /** @phpstan-ignore-line */ |
||
996 | |||
997 | // Some headers can be written as arrays to make it easier for plugin authors |
||
998 | // to manipulate/extend the contents. |
||
999 | if (is_array($val)) { |
||
1000 | if ((strtolower($name) == 'cache-control') || |
||
1001 | (strtolower($name) == 'referrer-policy')) |
||
1002 | { |
||
1003 | if (count($val) == 0) continue; |
||
1004 | $val = implode(', ', $val); |
||
1005 | } else if (strtolower($name) == 'content-security-policy') { |
||
1006 | if (count($val) == 0) continue; |
||
1007 | foreach ($val as $tmp1 => &$tmp2) $tmp2 = $tmp1.' '.implode(' ', $tmp2); |
||
1008 | $val = implode('; ', $val); |
||
1009 | } else { |
||
1010 | throw new OIDplusException(_L('HTTP header "%1" cannot be written as array. A newly installed plugin might have misused the header manipulation feature.',$name)); |
||
1011 | } |
||
1012 | } |
||
1013 | |||
1014 | if (is_string($val)) { |
||
1015 | header("$name: $val"); |
||
1016 | } |
||
1017 | } |
||
1018 | |||
1019 | } // endif (PHP_SAPI != 'cli') |
||
1020 | |||
412 | daniel-mar | 1021 | // Initialize other stuff (i.e. things which require the logger!) |
1022 | |||
1023 | OIDplus::recognizeSystemUrl(); // Make sure "last_known_system_url" is set |
||
1024 | OIDplus::recognizeVersion(); // Make sure "last_known_version" is set and a log entry is created |
||
2 | daniel-mar | 1025 | } |
42 | daniel-mar | 1026 | |
227 | daniel-mar | 1027 | # --- System URL, System ID, PKI, and other functions |
1028 | |||
412 | daniel-mar | 1029 | private static function recognizeSystemUrl() { |
1030 | try { |
||
496 | daniel-mar | 1031 | $url = OIDplus::webpath(); |
412 | daniel-mar | 1032 | OIDplus::config()->setValue('last_known_system_url', $url); |
1033 | } catch (Exception $e) { |
||
1034 | } |
||
1035 | } |
||
497 | daniel-mar | 1036 | |
496 | daniel-mar | 1037 | private static function getExecutingScriptPathDepth() { |
1038 | if (PHP_SAPI == 'cli') { |
||
1039 | global $argv; |
||
1040 | $test_dir = dirname(realpath($argv[0])); |
||
1041 | } else { |
||
1042 | if (!isset($_SERVER["SCRIPT_FILENAME"])) return false; |
||
1043 | $test_dir = dirname($_SERVER['SCRIPT_FILENAME']); |
||
1044 | } |
||
1045 | $test_dir = str_replace('\\', '/', $test_dir); |
||
1046 | $steps_up = 0; |
||
1047 | 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 subsequent directory! |
||
1048 | $test_dir = dirname($test_dir); |
||
1049 | $steps_up++; |
||
1050 | if ($steps_up == 1000) return false; // to make sure there will never be an infinite loop |
||
1051 | } |
||
1052 | return $steps_up; |
||
1053 | } |
||
632 | daniel-mar | 1054 | |
580 | daniel-mar | 1055 | public static function isSSL() { |
1056 | return isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] === 'on'); |
||
1057 | } |
||
412 | daniel-mar | 1058 | |
496 | daniel-mar | 1059 | private static function getSystemUrl($relative=false) { |
778 | daniel-mar | 1060 | if ($relative) { |
1061 | $steps_up = self::getExecutingScriptPathDepth(); |
||
1062 | if ($steps_up === false) { |
||
1063 | return false; |
||
1064 | } else { |
||
1065 | return str_repeat('../', $steps_up); |
||
1066 | } |
||
1067 | } else { |
||
326 | daniel-mar | 1068 | $res = OIDplus::baseConfig()->getValue('EXPLICIT_ABSOLUTE_SYSTEM_URL', ''); |
1069 | if ($res !== '') { |
||
494 | daniel-mar | 1070 | return rtrim($res,'/').'/'; |
326 | daniel-mar | 1071 | } |
778 | daniel-mar | 1072 | |
495 | daniel-mar | 1073 | if (PHP_SAPI == 'cli') { |
494 | daniel-mar | 1074 | try { |
1075 | return OIDplus::config()->getValue('last_known_system_url', false); |
||
1076 | } catch (Exception $e) { |
||
1077 | return false; |
||
1078 | } |
||
778 | daniel-mar | 1079 | } else { |
1080 | // First, try to find out how many levels we need to go up |
||
1081 | $steps_up = self::getExecutingScriptPathDepth(); |
||
326 | daniel-mar | 1082 | |
778 | daniel-mar | 1083 | // Then go up these amount of levels, based on SCRIPT_NAME/argv[0] |
1084 | $res = dirname($_SERVER['SCRIPT_NAME'].'index.php'); // This fake 'index.php' ensures that SCRIPT_NAME does not end with '/', which would make dirname() fail |
||
1085 | for ($i=0; $i<$steps_up; $i++) { |
||
1086 | $res = dirname($res); |
||
1087 | } |
||
1088 | $res = str_replace('\\', '/', $res); |
||
1089 | if ($res == '/') $res = ''; |
||
227 | daniel-mar | 1090 | |
778 | daniel-mar | 1091 | // Add protocol and hostname |
580 | daniel-mar | 1092 | $is_ssl = self::isSSL(); |
495 | daniel-mar | 1093 | $protocol = $is_ssl ? 'https' : 'http'; // do not translate |
1094 | $host = $_SERVER['HTTP_HOST']; // includes port if it is not 80/443 |
||
778 | daniel-mar | 1095 | |
1096 | return $protocol.'://'.$host.$res.'/'; |
||
495 | daniel-mar | 1097 | } |
227 | daniel-mar | 1098 | } |
1099 | } |
||
1100 | |||
1101 | private static $system_id_cache = null; |
||
1102 | public static function getSystemId($oid=false) { |
||
1103 | if (!is_null(self::$system_id_cache)) { |
||
1104 | $out = self::$system_id_cache; |
||
1105 | } else { |
||
1106 | $out = false; |
||
1107 | |||
1108 | if (self::getPkiStatus(true)) { |
||
1109 | $pubKey = OIDplus::config()->getValue('oidplus_public_key'); |
||
386 | daniel-mar | 1110 | $m = array(); |
227 | daniel-mar | 1111 | if (preg_match('@BEGIN PUBLIC KEY\-+(.+)\-+END PUBLIC KEY@ismU', $pubKey, $m)) { |
1112 | $out = smallhash(base64_decode($m[1])); |
||
1113 | } |
||
1114 | } |
||
1115 | self::$system_id_cache = $out; |
||
1116 | } |
||
350 | daniel-mar | 1117 | if (!$out) return false; |
291 | daniel-mar | 1118 | return ($oid ? '1.3.6.1.4.1.37476.30.9.' : '').$out; |
227 | daniel-mar | 1119 | } |
1120 | |||
1121 | public static function getPkiStatus($try_generate=true) { |
||
1122 | if (!function_exists('openssl_pkey_new')) return false; |
||
1123 | |||
1124 | $privKey = OIDplus::config()->getValue('oidplus_private_key'); |
||
1125 | $pubKey = OIDplus::config()->getValue('oidplus_public_key'); |
||
1126 | |||
1127 | if ($try_generate && !verify_private_public_key($privKey, $pubKey)) { |
||
263 | daniel-mar | 1128 | $pkey_config = array( |
227 | daniel-mar | 1129 | "digest_alg" => "sha512", |
1130 | "private_key_bits" => 2048, |
||
1131 | "private_key_type" => OPENSSL_KEYTYPE_RSA, |
||
1132 | ); |
||
1133 | |||
1134 | // Create the private and public key |
||
263 | daniel-mar | 1135 | $res = openssl_pkey_new($pkey_config); |
256 | daniel-mar | 1136 | |
239 | daniel-mar | 1137 | if (!$res) return false; |
227 | daniel-mar | 1138 | |
1139 | // Extract the private key from $res to $privKey |
||
1140 | openssl_pkey_export($res, $privKey); |
||
1141 | |||
1142 | // Extract the public key from $res to $pubKey |
||
1143 | $pubKey = openssl_pkey_get_details($res)["key"]; |
||
1144 | |||
239 | daniel-mar | 1145 | // Log |
288 | daniel-mar | 1146 | OIDplus::logger()->log("[INFO]A!", "Generating new SystemID using a new key pair"); |
239 | daniel-mar | 1147 | |
227 | daniel-mar | 1148 | // Save the key pair to database |
1149 | OIDplus::config()->setValue('oidplus_private_key', $privKey); |
||
1150 | OIDplus::config()->setValue('oidplus_public_key', $pubKey); |
||
1151 | |||
1152 | // Log the new system ID |
||
386 | daniel-mar | 1153 | $m = array(); |
227 | daniel-mar | 1154 | if (preg_match('@BEGIN PUBLIC KEY\-+(.+)\-+END PUBLIC KEY@ismU', $pubKey, $m)) { |
1155 | $system_id = smallhash(base64_decode($m[1])); |
||
288 | daniel-mar | 1156 | OIDplus::logger()->log("[INFO]A!", "Your SystemID is now $system_id"); |
227 | daniel-mar | 1157 | } |
1158 | } |
||
1159 | |||
1160 | return verify_private_public_key($privKey, $pubKey); |
||
1161 | } |
||
1162 | |||
170 | daniel-mar | 1163 | public static function getInstallType() { |
486 | daniel-mar | 1164 | $counter = 0; |
1165 | |||
661 | daniel-mar | 1166 | if ($new_version_file_exists = file_exists(OIDplus::localpath().'.version.php')) { |
486 | daniel-mar | 1167 | $counter++; |
591 | daniel-mar | 1168 | } |
661 | daniel-mar | 1169 | if ($old_version_file_exists = file_exists(OIDplus::localpath().'oidplus_version.txt')) { |
1170 | $counter++; |
||
1171 | } |
||
1172 | $version_file_exists = $old_version_file_exists | $new_version_file_exists; |
||
681 | daniel-mar | 1173 | if ($svn_dir_exists = (is_dir(OIDplus::localpath().'.svn') || |
1174 | is_dir(OIDplus::localpath().'../.svn'))) { // in case we checked out the root instead of the "trunk" |
||
486 | daniel-mar | 1175 | $counter++; |
591 | daniel-mar | 1176 | } |
681 | daniel-mar | 1177 | // if ($git_dir_exists = is_dir(OIDplus::localpath().'.git')) { |
698 | daniel-mar | 1178 | if ($git_dir_exists = (OIDplus::findGitFolder() !== false)) { |
486 | daniel-mar | 1179 | $counter++; |
591 | daniel-mar | 1180 | } |
486 | daniel-mar | 1181 | |
1182 | if ($counter === 0) { |
||
360 | daniel-mar | 1183 | return 'unknown'; // do not translate |
170 | daniel-mar | 1184 | } |
591 | daniel-mar | 1185 | else if ($counter > 1) { |
360 | daniel-mar | 1186 | return 'ambigous'; // do not translate |
170 | daniel-mar | 1187 | } |
591 | daniel-mar | 1188 | else if ($svn_dir_exists) { |
360 | daniel-mar | 1189 | return 'svn-wc'; // do not translate |
170 | daniel-mar | 1190 | } |
591 | daniel-mar | 1191 | else if ($git_dir_exists) { |
486 | daniel-mar | 1192 | return 'git-wc'; // do not translate |
1193 | } |
||
591 | daniel-mar | 1194 | else if ($version_file_exists) { |
360 | daniel-mar | 1195 | return 'svn-snapshot'; // do not translate |
170 | daniel-mar | 1196 | } |
1197 | } |
||
1198 | |||
412 | daniel-mar | 1199 | private static function recognizeVersion() { |
1200 | try { |
||
1201 | $ver_prev = OIDplus::config()->getValue("last_known_version"); |
||
1202 | $ver_now = OIDplus::getVersion(); |
||
1203 | if (($ver_now != '') && ($ver_prev != '') && ($ver_now != $ver_prev)) { |
||
455 | daniel-mar | 1204 | // TODO: Problem: When the system was updated using SVN, then the IP address of the next random visitor of the website is logged! |
412 | daniel-mar | 1205 | OIDplus::logger()->log("[INFO]A!", "System version changed from '$ver_prev' to '$ver_now'"); |
1206 | } |
||
1207 | OIDplus::config()->setValue("last_known_version", $ver_now); |
||
1208 | } catch (Exception $e) { |
||
1209 | } |
||
1210 | } |
||
1211 | |||
111 | daniel-mar | 1212 | public static function getVersion() { |
412 | daniel-mar | 1213 | static $cachedVersion = null; |
1214 | if (!is_null($cachedVersion)) { |
||
1215 | return $cachedVersion; |
||
1216 | } |
||
1217 | |||
486 | daniel-mar | 1218 | $installType = OIDplus::getInstallType(); |
1219 | |||
1220 | if ($installType === 'svn-wc') { |
||
496 | daniel-mar | 1221 | $ver = get_svn_revision(OIDplus::localpath()); |
486 | daniel-mar | 1222 | if ($ver) |
1223 | return ($cachedVersion = 'svn-'.$ver); |
||
558 | daniel-mar | 1224 | $ver = get_svn_revision(OIDplus::localpath().'../'); // in case we checked out the root instead of the "trunk" |
1225 | if ($ver) |
||
1226 | return ($cachedVersion = 'svn-'.$ver); |
||
111 | daniel-mar | 1227 | } |
162 | daniel-mar | 1228 | |
486 | daniel-mar | 1229 | if ($installType === 'git-wc') { |
698 | daniel-mar | 1230 | $ver = OIDplus::getGitsvnRevision(OIDplus::localpath()); |
486 | daniel-mar | 1231 | if ($ver) |
1232 | return ($cachedVersion = 'svn-'.$ver); |
||
162 | daniel-mar | 1233 | } |
1234 | |||
486 | daniel-mar | 1235 | if ($installType === 'svn-snapshot') { |
661 | daniel-mar | 1236 | $cont = ''; |
1237 | if (file_exists($filename = OIDplus::localpath().'oidplus_version.txt')) |
||
1238 | $cont = file_get_contents($filename); |
||
1239 | if (file_exists($filename = OIDplus::localpath().'.version.php')) |
||
1240 | $cont = file_get_contents($filename); |
||
386 | daniel-mar | 1241 | $m = array(); |
360 | daniel-mar | 1242 | if (preg_match('@Revision (\d+)@', $cont, $m)) // do not translate |
412 | daniel-mar | 1243 | return ($cachedVersion = 'svn-'.$m[1]); // do not translate |
162 | daniel-mar | 1244 | } |
1245 | |||
486 | daniel-mar | 1246 | return ($cachedVersion = false); // version ambigous or unknown |
111 | daniel-mar | 1247 | } |
1248 | |||
230 | daniel-mar | 1249 | private static $sslAvailableCache = null; |
1250 | public static function isSslAvailable() { |
||
1251 | if (!is_null(self::$sslAvailableCache)) return self::$sslAvailableCache; |
||
256 | daniel-mar | 1252 | |
495 | daniel-mar | 1253 | if (PHP_SAPI == 'cli') { |
230 | daniel-mar | 1254 | self::$sslAvailableCache = false; |
1255 | return false; |
||
1256 | } |
||
1257 | |||
49 | daniel-mar | 1258 | $timeout = 2; |
580 | daniel-mar | 1259 | $already_ssl = self::isSSL(); |
80 | daniel-mar | 1260 | $ssl_port = 443; |
42 | daniel-mar | 1261 | |
261 | daniel-mar | 1262 | $mode = OIDplus::baseConfig()->getValue('ENFORCE_SSL', 2/*auto*/); |
1263 | |||
1264 | if ($mode == 0) { |
||
80 | daniel-mar | 1265 | // No SSL available |
230 | daniel-mar | 1266 | self::$sslAvailableCache = $already_ssl; |
80 | daniel-mar | 1267 | return $already_ssl; |
1268 | } |
||
1269 | |||
261 | daniel-mar | 1270 | if ($mode == 1) { |
80 | daniel-mar | 1271 | // Force SSL |
1272 | if ($already_ssl) { |
||
230 | daniel-mar | 1273 | self::$sslAvailableCache = true; |
80 | daniel-mar | 1274 | return true; |
42 | daniel-mar | 1275 | } else { |
80 | daniel-mar | 1276 | $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
1277 | header('Location:'.$location); |
||
360 | daniel-mar | 1278 | die(_L('Redirecting to HTTPS...')); |
591 | daniel-mar | 1279 | #self::$sslAvailableCache = true; |
1280 | #return true; |
||
80 | daniel-mar | 1281 | } |
1282 | } |
||
1283 | |||
261 | daniel-mar | 1284 | if ($mode == 2) { |
80 | daniel-mar | 1285 | // Automatic SSL detection |
1286 | |||
1287 | if ($already_ssl) { |
||
1288 | // we are already on HTTPS |
||
557 | daniel-mar | 1289 | OIDplus::cookieUtils()->setcookie('SSL_CHECK', '1', 0, false); |
230 | daniel-mar | 1290 | self::$sslAvailableCache = true; |
80 | daniel-mar | 1291 | return true; |
1292 | } else { |
||
1293 | if (isset($_COOKIE['SSL_CHECK'])) { |
||
1294 | // We already had the HTTPS detection done before. |
||
1295 | if ($_COOKIE['SSL_CHECK']) { |
||
1296 | // HTTPS was detected before, but we are HTTP. Redirect now |
||
1297 | $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
||
1298 | header('Location:'.$location); |
||
360 | daniel-mar | 1299 | die(_L('Redirecting to HTTPS...')); |
591 | daniel-mar | 1300 | #self::$sslAvailableCache = true; |
1301 | #return true; |
||
80 | daniel-mar | 1302 | } else { |
1303 | // No HTTPS available. Do nothing. |
||
230 | daniel-mar | 1304 | self::$sslAvailableCache = false; |
80 | daniel-mar | 1305 | return false; |
1306 | } |
||
49 | daniel-mar | 1307 | } else { |
80 | daniel-mar | 1308 | // This is our first check (or the browser didn't accept the SSL_CHECK cookie) |
386 | daniel-mar | 1309 | $errno = -1; |
1310 | $errstr = ''; |
||
80 | daniel-mar | 1311 | if (@fsockopen($_SERVER['HTTP_HOST'], $ssl_port, $errno, $errstr, $timeout)) { |
1312 | // HTTPS detected. Redirect now, and remember that we had detected HTTPS |
||
557 | daniel-mar | 1313 | OIDplus::cookieUtils()->setcookie('SSL_CHECK', '1', 0, false); |
80 | daniel-mar | 1314 | $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
1315 | header('Location:'.$location); |
||
360 | daniel-mar | 1316 | die(_L('Redirecting to HTTPS...')); |
591 | daniel-mar | 1317 | #self::$sslAvailableCache = true; |
1318 | #return true; |
||
80 | daniel-mar | 1319 | } else { |
1320 | // No HTTPS detected. Do nothing, and next time, don't try to detect HTTPS again. |
||
557 | daniel-mar | 1321 | OIDplus::cookieUtils()->setcookie('SSL_CHECK', '0', 0, false); |
230 | daniel-mar | 1322 | self::$sslAvailableCache = false; |
80 | daniel-mar | 1323 | return false; |
1324 | } |
||
49 | daniel-mar | 1325 | } |
42 | daniel-mar | 1326 | } |
1327 | } |
||
1328 | } |
||
497 | daniel-mar | 1329 | |
496 | daniel-mar | 1330 | /** |
1331 | * Gets a local path pointing to a resource |
||
1332 | * @param string $target Target resource (file or directory must exist), or null to get the OIDplus base directory |
||
1333 | * @param boolean $relative If true, the returning path is relative to the currently executed PHP file (not the CLI working directory) |
||
590 | daniel-mar | 1334 | * @return string|false The local path, with guaranteed trailing path delimiter for directories |
496 | daniel-mar | 1335 | */ |
1336 | public static function localpath($target=null, $relative=false) { |
||
1337 | if (is_null($target)) { |
||
1338 | $target = __DIR__.'/../../'; |
||
1339 | } |
||
241 | daniel-mar | 1340 | |
496 | daniel-mar | 1341 | if ($relative) { |
1342 | // First, try to find out how many levels we need to go up |
||
1343 | $steps_up = self::getExecutingScriptPathDepth(); |
||
1344 | if ($steps_up === false) return false; |
||
497 | daniel-mar | 1345 | |
496 | daniel-mar | 1346 | // Virtually go back from the executing PHP script to the OIDplus base path |
1347 | $res = str_repeat('../',$steps_up); |
||
497 | daniel-mar | 1348 | |
496 | daniel-mar | 1349 | // Then go to the desired location |
1350 | $basedir = realpath(__DIR__.'/../../'); |
||
1351 | $target = realpath($target); |
||
500 | daniel-mar | 1352 | if ($target === false) return false; |
496 | daniel-mar | 1353 | $res .= substr($target, strlen($basedir)+1); |
1354 | $res = rtrim($res,'/'); // avoid '..//' for localpath(null,true) |
||
1355 | } else { |
||
1356 | $res = realpath($target); |
||
241 | daniel-mar | 1357 | } |
497 | daniel-mar | 1358 | |
591 | daniel-mar | 1359 | if (is_dir($target)) $res .= DIRECTORY_SEPARATOR; |
497 | daniel-mar | 1360 | |
496 | daniel-mar | 1361 | return $res; |
241 | daniel-mar | 1362 | } |
355 | daniel-mar | 1363 | |
496 | daniel-mar | 1364 | /** |
1365 | * Gets a URL pointing to a resource |
||
1366 | * @param string $target Target resource (file or directory must exist), or null to get the OIDplus base directory |
||
1367 | * @param boolean $relative If true, the returning path is relative to the currently executed PHP script (i.e. index.php , not the plugin PHP script!) |
||
590 | daniel-mar | 1368 | * @return string|false The URL, with guaranteed trailing path delimiter for directories |
496 | daniel-mar | 1369 | */ |
1370 | public static function webpath($target=null, $relative=false) { |
||
1371 | $res = self::getSystemUrl($relative); // Note: already contains a trailing path delimiter |
||
778 | daniel-mar | 1372 | if ($res === false) return false; |
497 | daniel-mar | 1373 | |
496 | daniel-mar | 1374 | if (!is_null($target)) { |
1375 | $basedir = realpath(__DIR__.'/../../'); |
||
1376 | $target = realpath($target); |
||
500 | daniel-mar | 1377 | if ($target === false) return false; |
496 | daniel-mar | 1378 | $tmp = substr($target, strlen($basedir)+1); |
500 | daniel-mar | 1379 | $res .= str_replace(DIRECTORY_SEPARATOR,'/',$tmp); // remove OS specific path delimiters introduced by realpath() |
497 | daniel-mar | 1380 | if (is_dir($target)) $res .= '/'; |
496 | daniel-mar | 1381 | } |
497 | daniel-mar | 1382 | |
496 | daniel-mar | 1383 | return $res; |
1384 | } |
||
497 | daniel-mar | 1385 | |
778 | daniel-mar | 1386 | public static function canonicalURL() { |
1387 | // First part: OIDplus system URL (or canonical system URL) |
||
1388 | $sysurl = OIDplus::baseConfig()->getValue('CANONICAL_SYSTEM_URL', ''); |
||
1389 | if ($sysurl !== '') { |
||
1390 | $sysurl = rtrim($sysurl,'/').'/'; |
||
1391 | } else { |
||
1392 | $sysurl = OIDplus::getSystemUrl(); |
||
1393 | } |
||
1394 | |||
1395 | // Second part: Directory |
||
1396 | $basedir = realpath(__DIR__.'/../../'); |
||
1397 | $target = realpath('.'); |
||
1398 | if ($target === false) return false; |
||
1399 | $tmp = substr($target, strlen($basedir)+1); |
||
1400 | $res = str_replace(DIRECTORY_SEPARATOR,'/',$tmp); // remove OS specific path delimiters introduced by realpath() |
||
1401 | if (is_dir($target)) $res .= '/'; |
||
1402 | |||
1403 | // Third part: File name |
||
1404 | $tmp = explode('/',$_SERVER['SCRIPT_NAME']); |
||
1405 | $tmp = end($tmp); |
||
1406 | |||
1407 | // Fourth part: Query string (ordered) |
||
1408 | $tmp2 = getSortedQuery(); |
||
1409 | if ($tmp2 != '') $tmp2 = '?'.$tmp2; |
||
1410 | |||
1411 | return $sysurl.$res.$tmp.$tmp2; |
||
1412 | } |
||
1413 | |||
639 | daniel-mar | 1414 | private static $shutdown_functions = array(); |
1415 | public static function register_shutdown_function($func) { |
||
1416 | self::$shutdown_functions[] = $func; |
||
1417 | } |
||
1418 | |||
1419 | public static function invoke_shutdown() { |
||
1420 | foreach (self::$shutdown_functions as $func) { |
||
1421 | $func(); |
||
1422 | } |
||
1423 | } |
||
1424 | |||
360 | daniel-mar | 1425 | public static function getAvailableLangs() { |
1426 | $langs = array(); |
||
1427 | foreach (OIDplus::getAllPluginManifests('language') as $pluginManifest) { |
||
389 | daniel-mar | 1428 | $code = $pluginManifest->getLanguageCode(); |
360 | daniel-mar | 1429 | $langs[] = $code; |
1430 | } |
||
1431 | return $langs; |
||
1432 | } |
||
1433 | |||
355 | daniel-mar | 1434 | public static function getCurrentLang() { |
360 | daniel-mar | 1435 | if (isset($_GET['lang'])) { |
1436 | $lang = $_GET['lang']; |
||
1437 | } else if (isset($_POST['lang'])) { |
||
1438 | $lang = $_POST['lang']; |
||
1439 | } else if (isset($_COOKIE['LANGUAGE'])) { |
||
1440 | $lang = $_COOKIE['LANGUAGE']; |
||
1441 | } else { |
||
1442 | $lang = self::DEFAULT_LANGUAGE; |
||
1443 | } |
||
1444 | $lang = substr(preg_replace('@[^a-z]@ismU', '', $lang),0,4); // sanitize |
||
355 | daniel-mar | 1445 | return $lang; |
1446 | } |
||
1447 | |||
362 | daniel-mar | 1448 | public static function handleLangArgument() { |
1449 | if (isset($_GET['lang'])) { |
||
1450 | // The "?lang=" argument is only for NoScript-Browsers/SearchEngines |
||
1451 | // In case someone who has JavaScript clicks a ?lang= link, they should get |
||
1452 | // the page in that language, but the cookie must be set, otherwise |
||
1453 | // the menu and other stuff would be in their cookie-based-language and not the |
||
1454 | // argument-based-language. |
||
557 | daniel-mar | 1455 | OIDplus::cookieUtils()->setcookie('LANGUAGE', $_GET['lang'], 0, true/*HttpOnly off, because JavaScript also needs translation*/); |
362 | daniel-mar | 1456 | } else if (isset($_POST['lang'])) { |
557 | daniel-mar | 1457 | OIDplus::cookieUtils()->setcookie('LANGUAGE', $_POST['lang'], 0, true/*HttpOnly off, because JavaScript also needs translation*/); |
362 | daniel-mar | 1458 | } |
1459 | } |
||
1460 | |||
468 | daniel-mar | 1461 | private static $translationArray = array(); |
469 | daniel-mar | 1462 | protected static function getTranslationFileContents($translation_file) { |
1463 | // First, try the cache |
||
481 | daniel-mar | 1464 | $cache_file = __DIR__ . '/../../userdata/cache/translation_'.md5($translation_file).'.ser'; |
469 | daniel-mar | 1465 | if (file_exists($cache_file) && (filemtime($cache_file) == filemtime($translation_file))) { |
1466 | $cac = @unserialize(file_get_contents($cache_file)); |
||
1467 | if ($cac) return $cac; |
||
1468 | } |
||
481 | daniel-mar | 1469 | |
469 | daniel-mar | 1470 | // If not successful, then load the XML file |
1471 | $xml = @simplexml_load_string(file_get_contents($translation_file)); |
||
1472 | 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 |
||
1473 | $cac = array(); |
||
1474 | foreach ($xml->message as $msg) { |
||
1475 | $src = trim($msg->source->__toString()); |
||
1476 | $dst = trim($msg->target->__toString()); |
||
1477 | $cac[$src] = $dst; |
||
1478 | } |
||
1479 | @file_put_contents($cache_file,serialize($cac)); |
||
1480 | @touch($cache_file,filemtime($translation_file)); |
||
1481 | return $cac; |
||
1482 | } |
||
468 | daniel-mar | 1483 | public static function getTranslationArray($requested_lang='*') { |
362 | daniel-mar | 1484 | foreach (OIDplus::getAllPluginManifests('language') as $pluginManifest) { |
389 | daniel-mar | 1485 | $lang = $pluginManifest->getLanguageCode(); |
362 | daniel-mar | 1486 | if (strpos($lang,'/') !== false) continue; // just to be sure |
1487 | if (strpos($lang,'\\') !== false) continue; // just to be sure |
||
1488 | if (strpos($lang,'..') !== false) continue; // just to be sure |
||
401 | daniel-mar | 1489 | |
468 | daniel-mar | 1490 | if (($requested_lang != '*') && ($lang != $requested_lang)) continue; |
401 | daniel-mar | 1491 | |
468 | daniel-mar | 1492 | if (!isset(self::$translationArray[$lang])) { |
1493 | self::$translationArray[$lang] = array(); |
||
1494 | |||
1495 | $wildcard = $pluginManifest->getLanguageMessages(); |
||
1496 | if (strpos($wildcard,'/') !== false) continue; // just to be sure |
||
1497 | if (strpos($wildcard,'\\') !== false) continue; // just to be sure |
||
1498 | if (strpos($wildcard,'..') !== false) continue; // just to be sure |
||
1499 | |||
635 | daniel-mar | 1500 | $translation_files = glob(__DIR__.'/../../plugins/'.'*'.'/language/'.$lang.'/'.$wildcard); |
468 | daniel-mar | 1501 | sort($translation_files); |
1502 | foreach ($translation_files as $translation_file) { |
||
1503 | if (!file_exists($translation_file)) continue; |
||
469 | daniel-mar | 1504 | $cac = self::getTranslationFileContents($translation_file); |
1505 | foreach ($cac as $src => $dst) { |
||
1506 | self::$translationArray[$lang][$src] = $dst; |
||
468 | daniel-mar | 1507 | } |
401 | daniel-mar | 1508 | } |
362 | daniel-mar | 1509 | } |
1510 | } |
||
468 | daniel-mar | 1511 | return self::$translationArray; |
362 | daniel-mar | 1512 | } |
1513 | |||
699 | daniel-mar | 1514 | public static function getEditionInfo() { |
1515 | return @parse_ini_file(__DIR__.'/../edition.ini', true)['Edition']; |
||
1516 | } |
||
1517 | |||
698 | daniel-mar | 1518 | public static function findGitFolder() { |
1519 | // Git command line saves git information in folder ".git" |
||
1520 | // Plesk git saves git information in folder "../../../git/oidplus/" (or similar) |
||
727 | daniel-mar | 1521 | $dir = OIDplus::localpath(); |
698 | daniel-mar | 1522 | if (is_dir($dir.'/.git')) return $dir.'/.git'; |
1523 | $i = 0; |
||
1524 | do { |
||
1525 | if (is_dir($dir.'/git')) { |
||
719 | daniel-mar | 1526 | $confs = @glob($dir.'/git/'.'*'.'/config'); |
1527 | if ($confs) foreach ($confs as $conf) { |
||
698 | daniel-mar | 1528 | $cont = file_get_contents($conf); |
699 | daniel-mar | 1529 | if (isset(OIDplus::getEditionInfo()['gitrepo']) && (OIDplus::getEditionInfo()['gitrepo'] != '') && (strpos($cont, OIDplus::getEditionInfo()['gitrepo']) !== false)) { |
698 | daniel-mar | 1530 | return dirname($conf); |
1531 | } |
||
1532 | } |
||
1533 | } |
||
1534 | $i++; |
||
719 | daniel-mar | 1535 | } while (($i<100) && ($dir != ($new_dir = @realpath($dir.'/../'))) && ($dir = $new_dir)); |
698 | daniel-mar | 1536 | return false; |
1537 | } |
||
1538 | |||
1539 | public static function getGitsvnRevision($dir='') { |
||
1540 | try { |
||
1541 | // tries command line and binary parsing |
||
699 | daniel-mar | 1542 | // requires vendor/danielmarschall/git_utils.inc.php |
698 | daniel-mar | 1543 | $git_dir = OIDplus::findGitFolder(); |
1544 | if ($git_dir === false) return false; |
||
1545 | $commit_msg = git_get_latest_commit_message($git_dir); |
||
1546 | } catch (Exception $e) { |
||
1547 | return false; |
||
1548 | } |
||
1549 | |||
1550 | $m = array(); |
||
1551 | if (preg_match('%git-svn-id: (.+)@(\\d+) %ismU', $commit_msg, $m)) { |
||
1552 | return $m[2]; |
||
1553 | } else { |
||
1554 | return false; |
||
1555 | } |
||
1556 | } |
||
1557 | |||
775 | daniel-mar | 1558 | public static function prefilterQuery($static_node_id, $throw_exception) { |
1559 | // Let namespace be case-insensitive |
||
1560 | $ary = explode(':', $static_node_id, 2); |
||
1561 | $ary[0] = strtolower($ary[0]); |
||
1562 | $static_node_id = implode(':', $ary); |
||
1563 | |||
1564 | // Convert WEID to OID |
||
1565 | if ((substr($static_node_id,0,5) == 'weid:') && class_exists('WeidOidConverter')) { |
||
1566 | $ary = explode('$', $static_node_id, 2); |
||
1567 | $weid = $ary[0]; |
||
1568 | $oid = WeidOidConverter::weid2oid($weid); |
||
1569 | if ($oid === false) { |
||
1570 | if ($throw_exception) throw new OIDplusException('This is not a valid WEID'); |
||
1571 | } else { |
||
1572 | $ary[0] = $oid; |
||
1573 | $static_node_id = 'oid:'.implode('$', $ary); |
||
1574 | } |
||
1575 | } |
||
1576 | |||
1577 | return $static_node_id; |
||
1578 | } |
||
374 | daniel-mar | 1579 | } |