Rev 500 | Rev 511 | 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 |
||
5 | * Copyright 2019 Daniel Marschall, ViaThinkSoft |
||
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 | |||
20 | class OIDplus { |
||
281 | daniel-mar | 21 | private static /*OIDplusPagePlugin[]*/ $pagePlugins = array(); |
22 | private static /*OIDplusAuthPlugin[]*/ $authPlugins = array(); |
||
289 | daniel-mar | 23 | private static /*OIDplusLoggerPlugin[]*/ $loggerPlugins = array(); |
227 | daniel-mar | 24 | private static /*OIDplusObjectTypePlugin[]*/ $objectTypePlugins = array(); |
25 | private static /*string[]*/ $enabledObjectTypes = array(); |
||
26 | private static /*string[]*/ $disabledObjectTypes = array(); |
||
27 | private static /*OIDplusDatabasePlugin[]*/ $dbPlugins = array(); |
||
274 | daniel-mar | 28 | private static /*OIDplusSqlSlangPlugin[]*/ $sqlSlangPlugins = array(); |
355 | daniel-mar | 29 | private static /*OIDplusLanguagePlugin[]*/ $languagePlugins = array(); |
449 | daniel-mar | 30 | private static /*OIDplusDesignPlugin[]*/ $designPlugins = array(); |
2 | daniel-mar | 31 | |
280 | daniel-mar | 32 | protected static $html = true; |
236 | daniel-mar | 33 | |
362 | daniel-mar | 34 | /*public*/ const DEFAULT_LANGUAGE = 'enus'; // the language of the source code |
355 | daniel-mar | 35 | |
2 | daniel-mar | 36 | private function __construct() { |
37 | } |
||
295 | daniel-mar | 38 | |
263 | daniel-mar | 39 | # --- Static classes |
274 | daniel-mar | 40 | |
263 | daniel-mar | 41 | private static $baseConfig = null; |
42 | private static $old_config_format = false; |
||
261 | daniel-mar | 43 | public static function baseConfig() { |
263 | daniel-mar | 44 | $first_init = false; |
274 | daniel-mar | 45 | |
263 | daniel-mar | 46 | if ($first_init = is_null(self::$baseConfig)) { |
47 | self::$baseConfig = new OIDplusBaseConfig(); |
||
261 | daniel-mar | 48 | } |
49 | |||
263 | daniel-mar | 50 | if ($first_init) { |
261 | daniel-mar | 51 | // Include a file containing various size/depth limitations of OIDs |
294 | daniel-mar | 52 | // It is important to include it before userdata/baseconfig/config.inc.php was included, |
53 | // so we can give userdata/baseconfig/config.inc.php the chance to override the values. |
||
261 | daniel-mar | 54 | |
496 | daniel-mar | 55 | include OIDplus::localpath().'includes/oidplus_limits.inc.php'; |
261 | daniel-mar | 56 | |
57 | // Include config file |
||
295 | daniel-mar | 58 | |
496 | daniel-mar | 59 | $config_file = OIDplus::localpath() . 'userdata/baseconfig/config.inc.php'; |
60 | $config_file_old = OIDplus::localpath() . 'includes/config.inc.php'; // backwards compatibility |
||
295 | daniel-mar | 61 | |
294 | daniel-mar | 62 | if (!file_exists($config_file) && file_exists($config_file_old)) { |
63 | $config_file = $config_file_old; |
||
64 | } |
||
261 | daniel-mar | 65 | |
294 | daniel-mar | 66 | if (file_exists($config_file)) { |
263 | daniel-mar | 67 | if (self::$old_config_format) { |
68 | // Note: We may only include it once due to backwards compatibility, |
||
69 | // since in version 2.0, the configuration was defined using define() statements |
||
70 | // Attention: This does mean that a full re-init (e.g. for test cases) is not possible |
||
71 | // if a version 2.0 config is used! |
||
294 | daniel-mar | 72 | include_once $config_file; |
263 | daniel-mar | 73 | } else { |
294 | daniel-mar | 74 | include $config_file; |
263 | daniel-mar | 75 | } |
261 | daniel-mar | 76 | |
77 | if (defined('OIDPLUS_CONFIG_VERSION') && (OIDPLUS_CONFIG_VERSION == 2.0)) { |
||
263 | daniel-mar | 78 | self::$old_config_format = true; |
274 | daniel-mar | 79 | |
261 | daniel-mar | 80 | // Backwards compatibility 2.0 => 2.1 |
81 | foreach (get_defined_constants(true)['user'] as $name => $value) { |
||
82 | $name = str_replace('OIDPLUS_', '', $name); |
||
83 | if ($name == 'SESSION_SECRET') $name = 'SERVER_SECRET'; |
||
84 | if ($name == 'MYSQL_QUERYLOG') $name = 'QUERY_LOGFILE'; |
||
85 | if (($name == 'MYSQL_PASSWORD') || ($name == 'ODBC_PASSWORD') || ($name == 'PDO_PASSWORD') || ($name == 'PGSQL_PASSWORD')) { |
||
263 | daniel-mar | 86 | self::$baseConfig->setValue($name, base64_decode($value)); |
261 | daniel-mar | 87 | } else { |
88 | if ($name == 'CONFIG_VERSION') $value = 2.1; |
||
263 | daniel-mar | 89 | self::$baseConfig->setValue($name, $value); |
261 | daniel-mar | 90 | } |
91 | } |
||
92 | } |
||
93 | } else { |
||
496 | daniel-mar | 94 | if (!is_dir(OIDplus::localpath().'setup')) { |
360 | daniel-mar | 95 | 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 | 96 | } else { |
280 | daniel-mar | 97 | if (self::$html) { |
496 | daniel-mar | 98 | if (strpos($_SERVER['REQUEST_URI'], OIDplus::webpath(null,true).'setup/') !== 0) { |
99 | header('Location:'.OIDplus::webpath().'setup/'); |
||
360 | daniel-mar | 100 | die(_L('Redirecting to setup...')); |
349 | daniel-mar | 101 | } else { |
102 | return self::$baseConfig; |
||
103 | } |
||
261 | daniel-mar | 104 | } else { |
105 | // This can be displayed in e.g. ajax.php |
||
360 | daniel-mar | 106 | throw new OIDplusConfigInitializationException(_L('File %1 is missing. Please run setup again.','userdata/baseconfig/config.inc.php')); |
261 | daniel-mar | 107 | } |
108 | } |
||
109 | } |
||
110 | |||
111 | // Check important config settings |
||
112 | |||
263 | daniel-mar | 113 | if (self::$baseConfig->getValue('CONFIG_VERSION') != 2.1) { |
503 | daniel-mar | 114 | if (strpos($_SERVER['REQUEST_URI'], OIDplus::webpath(null,true).'setup/') !== 0) { |
115 | throw new OIDplusConfigInitializationException(_L("The information located in %1 is outdated.",realpath($config_file))); |
||
116 | } |
||
261 | daniel-mar | 117 | } |
118 | |||
263 | daniel-mar | 119 | if (self::$baseConfig->getValue('SERVER_SECRET', '') === '') { |
503 | daniel-mar | 120 | if (strpos($_SERVER['REQUEST_URI'], OIDplus::webpath(null,true).'setup/') !== 0) { |
121 | throw new OIDplusConfigInitializationException(_L("You must set a value for SERVER_SECRET in %1 for the system to operate secure.",realpath($config_file))); |
||
122 | } |
||
261 | daniel-mar | 123 | } |
124 | } |
||
125 | |||
263 | daniel-mar | 126 | return self::$baseConfig; |
261 | daniel-mar | 127 | } |
128 | |||
263 | daniel-mar | 129 | private static $config = null; |
2 | daniel-mar | 130 | public static function config() { |
263 | daniel-mar | 131 | if ($first_init = is_null(self::$config)) { |
132 | self::$config = new OIDplusConfig(); |
||
2 | daniel-mar | 133 | } |
263 | daniel-mar | 134 | |
135 | if ($first_init) { |
||
136 | // These are important settings for base functionalities and therefore are not inside plugins |
||
137 | self::$config->prepareConfigKey('system_title', 'What is the name of your RA?', 'OIDplus 2.0', OIDplusConfig::PROTECTION_EDITABLE, function($value) { |
||
138 | if (empty($value)) { |
||
360 | daniel-mar | 139 | throw new OIDplusException(_L('Please enter a value for the system title.')); |
263 | daniel-mar | 140 | } |
141 | }); |
||
142 | self::$config->prepareConfigKey('admin_email', 'E-Mail address of the system administrator', '', OIDplusConfig::PROTECTION_EDITABLE, function($value) { |
||
143 | if (!empty($value) && !OIDplus::mailUtils()->validMailAddress($value)) { |
||
360 | daniel-mar | 144 | throw new OIDplusException(_L('This is not a correct email address')); |
263 | daniel-mar | 145 | } |
146 | }); |
||
147 | self::$config->prepareConfigKey('global_cc', 'Global CC for all outgoing emails?', '', OIDplusConfig::PROTECTION_EDITABLE, function($value) { |
||
148 | if (!empty($value) && !OIDplus::mailUtils()->validMailAddress($value)) { |
||
360 | daniel-mar | 149 | throw new OIDplusException(_L('This is not a correct email address')); |
263 | daniel-mar | 150 | } |
151 | }); |
||
152 | self::$config->prepareConfigKey('objecttypes_initialized', 'List of object type plugins that were initialized once', '', OIDplusConfig::PROTECTION_READONLY, function($value) { |
||
153 | // Nothing here yet |
||
154 | }); |
||
155 | 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) { |
||
156 | # 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? |
||
157 | |||
158 | $ary = explode(';',$value); |
||
159 | $uniq_ary = array_unique($ary); |
||
160 | |||
161 | if (count($ary) != count($uniq_ary)) { |
||
360 | daniel-mar | 162 | throw new OIDplusException(_L('Please check your input. Some object types are double.')); |
263 | daniel-mar | 163 | } |
164 | |||
165 | foreach ($ary as $ot_check) { |
||
166 | $ns_found = false; |
||
167 | foreach (OIDplus::getEnabledObjectTypes() as $ot) { |
||
168 | if ($ot::ns() == $ot_check) { |
||
169 | $ns_found = true; |
||
170 | break; |
||
171 | } |
||
172 | } |
||
173 | foreach (OIDplus::getDisabledObjectTypes() as $ot) { |
||
174 | if ($ot::ns() == $ot_check) { |
||
175 | $ns_found = true; |
||
176 | break; |
||
177 | } |
||
178 | } |
||
179 | if (!$ns_found) { |
||
360 | daniel-mar | 180 | throw new OIDplusException(_L('Please check your input. Namespace "%1" is not found',$ot_check)); |
263 | daniel-mar | 181 | } |
182 | } |
||
183 | }); |
||
184 | self::$config->prepareConfigKey('oidplus_private_key', 'Private key for this system', '', OIDplusConfig::PROTECTION_HIDDEN, function($value) { |
||
185 | // Nothing here yet |
||
186 | }); |
||
187 | 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) { |
||
188 | // Nothing here yet |
||
189 | }); |
||
324 | daniel-mar | 190 | self::$config->prepareConfigKey('last_known_system_url', 'Last known System URL', '', OIDplusConfig::PROTECTION_HIDDEN, function($value) { |
191 | // Nothing here yet |
||
192 | }); |
||
412 | daniel-mar | 193 | self::$config->prepareConfigKey('last_known_version', 'Last known OIDplus Version', '', OIDplusConfig::PROTECTION_HIDDEN, function($value) { |
194 | // Nothing here yet |
||
195 | }); |
||
456 | daniel-mar | 196 | self::$config->prepareConfigKey('default_ra_auth_method', 'Default auth method used for generating password of RAs (must exist in plugins/auth/)?', 'A3_bcrypt', OIDplusConfig::PROTECTION_EDITABLE, function($value) { |
453 | daniel-mar | 197 | $good = true; |
198 | if (strpos($value,'/') !== false) $good = false; |
||
199 | if (strpos($value,'\\') !== false) $good = false; |
||
200 | if (strpos($value,'..') !== false) $good = false; |
||
201 | if (!$good) { |
||
202 | throw new OIDplusException(_L('Invalid auth plugin folder name. Do only enter a folder name, not an absolute or relative path')); |
||
203 | } |
||
204 | |||
496 | daniel-mar | 205 | if (!is_dir(OIDplus::localpath().'plugins/auth/'.$value)) { |
453 | daniel-mar | 206 | throw new OIDplusException(_L('The auth plugin "%1" does not exist in plugin directory %2',$value,'plugins/auth/')); |
207 | } |
||
208 | }); |
||
263 | daniel-mar | 209 | } |
210 | |||
211 | return self::$config; |
||
2 | daniel-mar | 212 | } |
213 | |||
263 | daniel-mar | 214 | private static $gui = null; |
2 | daniel-mar | 215 | public static function gui() { |
263 | daniel-mar | 216 | if (is_null(self::$gui)) { |
217 | self::$gui = new OIDplusGui(); |
||
86 | daniel-mar | 218 | } |
263 | daniel-mar | 219 | return self::$gui; |
2 | daniel-mar | 220 | } |
221 | |||
263 | daniel-mar | 222 | private static $authUtils = null; |
2 | daniel-mar | 223 | public static function authUtils() { |
263 | daniel-mar | 224 | if (is_null(self::$authUtils)) { |
225 | self::$authUtils = new OIDplusAuthUtils(); |
||
86 | daniel-mar | 226 | } |
263 | daniel-mar | 227 | return self::$authUtils; |
2 | daniel-mar | 228 | } |
229 | |||
263 | daniel-mar | 230 | private static $mailUtils = null; |
250 | daniel-mar | 231 | public static function mailUtils() { |
263 | daniel-mar | 232 | if (is_null(self::$mailUtils)) { |
233 | self::$mailUtils = new OIDplusMailUtils(); |
||
250 | daniel-mar | 234 | } |
263 | daniel-mar | 235 | return self::$mailUtils; |
250 | daniel-mar | 236 | } |
237 | |||
263 | daniel-mar | 238 | private static $menuUtils = null; |
250 | daniel-mar | 239 | public static function menuUtils() { |
263 | daniel-mar | 240 | if (is_null(self::$menuUtils)) { |
241 | self::$menuUtils = new OIDplusMenuUtils(); |
||
250 | daniel-mar | 242 | } |
263 | daniel-mar | 243 | return self::$menuUtils; |
250 | daniel-mar | 244 | } |
245 | |||
263 | daniel-mar | 246 | private static $logger = null; |
115 | daniel-mar | 247 | public static function logger() { |
263 | daniel-mar | 248 | if (is_null(self::$logger)) { |
249 | self::$logger = new OIDplusLogger(); |
||
115 | daniel-mar | 250 | } |
263 | daniel-mar | 251 | return self::$logger; |
115 | daniel-mar | 252 | } |
253 | |||
263 | daniel-mar | 254 | private static $sesHandler = null; |
86 | daniel-mar | 255 | public static function sesHandler() { |
263 | daniel-mar | 256 | if (is_null(self::$sesHandler)) { |
257 | self::$sesHandler = new OIDplusSessionHandler(); |
||
86 | daniel-mar | 258 | } |
263 | daniel-mar | 259 | return self::$sesHandler; |
86 | daniel-mar | 260 | } |
261 | |||
274 | daniel-mar | 262 | # --- SQL slang plugin |
263 | |||
264 | private static function registerSqlSlangPlugin(OIDplusSqlSlangPlugin $plugin) { |
||
265 | $name = $plugin::id(); |
||
266 | if ($name === false) return false; |
||
267 | |||
449 | daniel-mar | 268 | if (isset(self::$sqlSlangPlugins[$name])) { |
451 | daniel-mar | 269 | $plugintype_hf = _L('SQL slang'); |
449 | daniel-mar | 270 | throw new OIDplusException('Multiple %1 plugins use the ID %2', $plugintype_hf, $name); |
271 | } |
||
272 | |||
274 | daniel-mar | 273 | self::$sqlSlangPlugins[$name] = $plugin; |
274 | |||
275 | return true; |
||
276 | } |
||
277 | |||
278 | public static function getSqlSlangPlugins() { |
||
279 | return self::$sqlSlangPlugins; |
||
280 | } |
||
281 | |||
318 | daniel-mar | 282 | public static function getSqlSlangPlugin($id)/*: ?OIDplusSqlSlangPlugin*/ { |
283 | if (isset(self::$sqlSlangPlugins[$id])) { |
||
284 | return self::$sqlSlangPlugins[$id]; |
||
285 | } else { |
||
286 | return null; |
||
287 | } |
||
288 | } |
||
289 | |||
230 | daniel-mar | 290 | # --- Database plugin |
74 | daniel-mar | 291 | |
227 | daniel-mar | 292 | private static function registerDatabasePlugin(OIDplusDatabasePlugin $plugin) { |
275 | daniel-mar | 293 | $name = $plugin::id(); |
150 | daniel-mar | 294 | if ($name === false) return false; |
295 | |||
449 | daniel-mar | 296 | if (isset(self::$dbPlugins[$name])) { |
297 | $plugintype_hf = _L('Database'); |
||
298 | throw new OIDplusException('Multiple %1 plugins use the ID %2', $plugintype_hf, $name); |
||
299 | } |
||
300 | |||
150 | daniel-mar | 301 | self::$dbPlugins[$name] = $plugin; |
302 | |||
303 | return true; |
||
304 | } |
||
305 | |||
306 | public static function getDatabasePlugins() { |
||
307 | return self::$dbPlugins; |
||
308 | } |
||
309 | |||
295 | daniel-mar | 310 | public static function getActiveDatabasePlugin() { |
261 | daniel-mar | 311 | if (OIDplus::baseConfig()->getValue('DATABASE_PLUGIN', '') === '') { |
360 | daniel-mar | 312 | throw new OIDplusConfigInitializationException(_L('No database plugin selected in config file')); |
260 | daniel-mar | 313 | } |
261 | daniel-mar | 314 | if (!isset(self::$dbPlugins[OIDplus::baseConfig()->getValue('DATABASE_PLUGIN')])) { |
360 | daniel-mar | 315 | $db_plugin_name = OIDplus::baseConfig()->getValue('DATABASE_PLUGIN'); |
316 | throw new OIDplusConfigInitializationException(_L('Database plugin "%1" not found',$db_plugin_name)); |
||
227 | daniel-mar | 317 | } |
295 | daniel-mar | 318 | return self::$dbPlugins[OIDplus::baseConfig()->getValue('DATABASE_PLUGIN')]; |
227 | daniel-mar | 319 | } |
320 | |||
295 | daniel-mar | 321 | private static $dbMainSession = null; |
322 | public static function db() { |
||
323 | if (is_null(self::$dbMainSession)) { |
||
324 | self::$dbMainSession = self::getActiveDatabasePlugin()->newConnection(); |
||
325 | } |
||
326 | if (!self::$dbMainSession->isConnected()) self::$dbMainSession->connect(); |
||
327 | return self::$dbMainSession; |
||
328 | } |
||
329 | |||
330 | private static $dbIsolatedSession = null; |
||
331 | public static function dbIsolated() { |
||
332 | if (is_null(self::$dbIsolatedSession)) { |
||
333 | self::$dbIsolatedSession = self::getActiveDatabasePlugin()->newConnection(); |
||
334 | } |
||
335 | if (!self::$dbIsolatedSession->isConnected()) self::$dbIsolatedSession->connect(); |
||
336 | return self::$dbIsolatedSession; |
||
337 | } |
||
338 | |||
227 | daniel-mar | 339 | # --- Page plugin |
340 | |||
224 | daniel-mar | 341 | private static function registerPagePlugin(OIDplusPagePlugin $plugin) { |
281 | daniel-mar | 342 | self::$pagePlugins[] = $plugin; |
61 | daniel-mar | 343 | |
344 | return true; |
||
345 | } |
||
346 | |||
281 | daniel-mar | 347 | public static function getPagePlugins() { |
348 | return self::$pagePlugins; |
||
61 | daniel-mar | 349 | } |
350 | |||
227 | daniel-mar | 351 | # --- Auth plugin |
352 | |||
353 | private static function registerAuthPlugin(OIDplusAuthPlugin $plugin) { |
||
456 | daniel-mar | 354 | if (OIDplus::baseConfig()->getValue('DEBUG')) { |
355 | $password = generateRandomString(25); |
||
459 | daniel-mar | 356 | |
461 | daniel-mar | 357 | try { |
358 | $authInfo = $plugin->generate($password); |
||
359 | } catch (OIDplusException $e) { |
||
360 | // This can happen when the AuthKey or Salt is too long |
||
361 | throw new OIDplusException(_L('Auth plugin "%1" is erroneous: %2',basename($plugin->getPluginDirectory()),$e->getMessage())); |
||
362 | } |
||
459 | daniel-mar | 363 | $salt = $authInfo->getSalt(); |
461 | daniel-mar | 364 | $authKey = $authInfo->getAuthKey(); |
459 | daniel-mar | 365 | |
461 | daniel-mar | 366 | $authInfo_SaltDiff = clone $authInfo; |
367 | $authInfo_SaltDiff->setSalt(strrev($authInfo_SaltDiff->getSalt())); |
||
368 | |||
369 | $authInfo_AuthKeyDiff = clone $authInfo; |
||
370 | $authInfo_AuthKeyDiff->setAuthKey(strrev($authInfo_AuthKeyDiff->getAuthKey())); |
||
371 | |||
372 | if ((!$plugin->verify($authInfo,$password)) || |
||
373 | (!empty($salt) && $plugin->verify($authInfo_SaltDiff,$password)) || |
||
374 | ($plugin->verify($authInfo_AuthKeyDiff,$password)) || |
||
375 | ($plugin->verify($authInfo,$password.'x'))) { |
||
456 | daniel-mar | 376 | throw new OIDplusException(_L('Auth plugin "%1" is erroneous: Generate/Verify self test failed',basename($plugin->getPluginDirectory()))); |
377 | } |
||
453 | daniel-mar | 378 | } |
379 | |||
227 | daniel-mar | 380 | self::$authPlugins[] = $plugin; |
381 | return true; |
||
382 | } |
||
383 | |||
221 | daniel-mar | 384 | public static function getAuthPlugins() { |
385 | return self::$authPlugins; |
||
386 | } |
||
387 | |||
355 | daniel-mar | 388 | # --- Language plugin |
389 | |||
390 | private static function registerLanguagePlugin(OIDplusLanguagePlugin $plugin) { |
||
391 | self::$languagePlugins[] = $plugin; |
||
392 | return true; |
||
393 | } |
||
394 | |||
395 | public static function getLanguagePlugins() { |
||
396 | return self::$languagePlugins; |
||
397 | } |
||
398 | |||
449 | daniel-mar | 399 | # --- Design plugin |
400 | |||
401 | private static function registerDesignPlugin(OIDplusDesignPlugin $plugin) { |
||
402 | self::$designPlugins[] = $plugin; |
||
403 | return true; |
||
404 | } |
||
405 | |||
406 | public static function getDesignPlugins() { |
||
407 | return self::$designPlugins; |
||
408 | } |
||
409 | |||
289 | daniel-mar | 410 | # --- Logger plugin |
411 | |||
412 | private static function registerLoggerPlugin(OIDplusLoggerPlugin $plugin) { |
||
413 | self::$loggerPlugins[] = $plugin; |
||
414 | return true; |
||
415 | } |
||
416 | |||
417 | public static function getLoggerPlugins() { |
||
418 | return self::$loggerPlugins; |
||
419 | } |
||
420 | |||
227 | daniel-mar | 421 | # --- Object type plugin |
422 | |||
423 | private static function registerObjectTypePlugin(OIDplusObjectTypePlugin $plugin) { |
||
424 | self::$objectTypePlugins[] = $plugin; |
||
425 | |||
426 | $ot = $plugin::getObjectTypeClassName(); |
||
427 | self::registerObjectType($ot); |
||
428 | |||
429 | return true; |
||
430 | } |
||
431 | |||
224 | daniel-mar | 432 | private static function registerObjectType($ot) { |
66 | daniel-mar | 433 | $ns = $ot::ns(); |
434 | |||
360 | daniel-mar | 435 | if (empty($ns)) throw new OIDplusException(_L('Attention: Empty NS at %1',$ot)); |
66 | daniel-mar | 436 | |
437 | $ns_found = false; |
||
227 | daniel-mar | 438 | foreach (array_merge(OIDplus::getEnabledObjectTypes(), OIDplus::getDisabledObjectTypes()) as $test_ot) { |
66 | daniel-mar | 439 | if ($test_ot::ns() == $ns) { |
440 | $ns_found = true; |
||
441 | break; |
||
442 | } |
||
443 | } |
||
444 | if ($ns_found) { |
||
360 | daniel-mar | 445 | throw new OIDplusException(_L('Attention: Two objectType plugins use the same namespace "%1"!',$ns)); |
66 | daniel-mar | 446 | } |
447 | |||
448 | $init = OIDplus::config()->getValue("objecttypes_initialized"); |
||
449 | $init_ary = empty($init) ? array() : explode(';', $init); |
||
70 | daniel-mar | 450 | $init_ary = array_map('trim', $init_ary); |
66 | daniel-mar | 451 | |
452 | $enabled = OIDplus::config()->getValue("objecttypes_enabled"); |
||
453 | $enabled_ary = empty($enabled) ? array() : explode(';', $enabled); |
||
70 | daniel-mar | 454 | $enabled_ary = array_map('trim', $enabled_ary); |
66 | daniel-mar | 455 | |
79 | daniel-mar | 456 | $do_enable = false; |
457 | if (in_array($ns, $enabled_ary)) { |
||
447 | daniel-mar | 458 | // If it is in the list of enabled object types, it is enabled (obviously) |
79 | daniel-mar | 459 | $do_enable = true; |
460 | } else { |
||
447 | daniel-mar | 461 | if (!OIDplus::config()->getValue('oobe_objects_done')) { |
462 | // If the OOBE wizard is NOT done, then just enable the "oid" object type by default |
||
79 | daniel-mar | 463 | $do_enable = $ns == 'oid'; |
464 | } else { |
||
447 | daniel-mar | 465 | // If the OOBE wizard was done (once), then |
466 | // we will enable all object types which were never initialized |
||
467 | // (i.e. a plugin folder was freshly added) |
||
79 | daniel-mar | 468 | $do_enable = !in_array($ns, $init_ary); |
469 | } |
||
470 | } |
||
471 | |||
472 | if ($do_enable) { |
||
227 | daniel-mar | 473 | self::$enabledObjectTypes[] = $ot; |
474 | usort(self::$enabledObjectTypes, function($a, $b) { |
||
66 | daniel-mar | 475 | $enabled = OIDplus::config()->getValue("objecttypes_enabled"); |
476 | $enabled_ary = explode(';', $enabled); |
||
477 | |||
478 | $idx_a = array_search($a::ns(), $enabled_ary); |
||
479 | $idx_b = array_search($b::ns(), $enabled_ary); |
||
480 | |||
481 | if ($idx_a == $idx_b) { |
||
482 | return 0; |
||
483 | } |
||
484 | return ($idx_a > $idx_b) ? +1 : -1; |
||
485 | }); |
||
74 | daniel-mar | 486 | } else { |
487 | self::$disabledObjectTypes[] = $ot; |
||
66 | daniel-mar | 488 | } |
489 | |||
490 | if (!in_array($ns, $init_ary)) { |
||
491 | // Was never initialized before, so we add it to the list of enabled object types once |
||
492 | |||
79 | daniel-mar | 493 | if ($do_enable) { |
494 | $enabled_ary[] = $ns; |
||
495 | OIDplus::config()->setValue("objecttypes_enabled", implode(';', $enabled_ary)); |
||
496 | } |
||
66 | daniel-mar | 497 | |
498 | $init_ary[] = $ns; |
||
499 | OIDplus::config()->setValue("objecttypes_initialized", implode(';', $init_ary)); |
||
500 | } |
||
61 | daniel-mar | 501 | } |
502 | |||
227 | daniel-mar | 503 | public static function getObjectTypePlugins() { |
504 | return self::$objectTypePlugins; |
||
61 | daniel-mar | 505 | } |
506 | |||
227 | daniel-mar | 507 | public static function getObjectTypePluginsEnabled() { |
508 | $res = array(); |
||
509 | foreach (self::$objectTypePlugins as $plugin) { |
||
510 | $ot = $plugin::getObjectTypeClassName(); |
||
511 | if (in_array($ot, self::$enabledObjectTypes)) $res[] = $plugin; |
||
512 | } |
||
513 | return $res; |
||
74 | daniel-mar | 514 | } |
515 | |||
227 | daniel-mar | 516 | public static function getObjectTypePluginsDisabled() { |
517 | $res = array(); |
||
518 | foreach (self::$objectTypePlugins as $plugin) { |
||
519 | $ot = $plugin::getObjectTypeClassName(); |
||
520 | if (in_array($ot, self::$disabledObjectTypes)) $res[] = $plugin; |
||
74 | daniel-mar | 521 | } |
227 | daniel-mar | 522 | return $res; |
74 | daniel-mar | 523 | } |
524 | |||
227 | daniel-mar | 525 | public static function getEnabledObjectTypes() { |
526 | return self::$enabledObjectTypes; |
||
527 | } |
||
74 | daniel-mar | 528 | |
227 | daniel-mar | 529 | public static function getDisabledObjectTypes() { |
530 | return self::$disabledObjectTypes; |
||
531 | } |
||
74 | daniel-mar | 532 | |
277 | daniel-mar | 533 | # --- Plugin handling functions |
534 | |||
320 | daniel-mar | 535 | public static function getAllPlugins()/*: array*/ { |
536 | $res = array(); |
||
537 | $res = array_merge($res, self::$pagePlugins); |
||
538 | $res = array_merge($res, self::$authPlugins); |
||
539 | $res = array_merge($res, self::$loggerPlugins); |
||
540 | $res = array_merge($res, self::$objectTypePlugins); |
||
541 | $res = array_merge($res, self::$dbPlugins); |
||
542 | $res = array_merge($res, self::$sqlSlangPlugins); |
||
355 | daniel-mar | 543 | $res = array_merge($res, self::$languagePlugins); |
449 | daniel-mar | 544 | $res = array_merge($res, self::$designPlugins); |
320 | daniel-mar | 545 | return $res; |
546 | } |
||
547 | |||
321 | daniel-mar | 548 | public static function getPluginByOid($oid)/*: ?OIDplusPlugin*/ { |
320 | daniel-mar | 549 | $plugins = self::getAllPlugins(); |
321 | daniel-mar | 550 | foreach ($plugins as $plugin) { |
551 | if (oid_dotnotation_equal($plugin->getManifest()->getOid(), $oid)) { |
||
552 | return $plugin; |
||
320 | daniel-mar | 553 | } |
554 | } |
||
555 | return null; |
||
556 | } |
||
557 | |||
380 | daniel-mar | 558 | public static function getPluginByClassName($classname)/*: ?OIDplusPlugin*/ { |
559 | $plugins = self::getAllPlugins(); |
||
560 | foreach ($plugins as $plugin) { |
||
561 | if (get_class($plugin) === $classname) { |
||
562 | return $plugin; |
||
563 | } |
||
564 | } |
||
565 | return null; |
||
277 | daniel-mar | 566 | } |
567 | |||
307 | daniel-mar | 568 | public static function getAllPluginManifests($pluginFolderMask='*', $flat=true): array { |
277 | daniel-mar | 569 | $out = array(); |
279 | daniel-mar | 570 | // Note: glob() will sort by default, so we do not need a page priority attribute. |
571 | // So you just need to use a numeric plugin directory prefix (padded). |
||
496 | daniel-mar | 572 | $ary = glob(OIDplus::localpath().'plugins/'.$pluginFolderMask.'/'.'*'.'/manifest.xml'); |
426 | daniel-mar | 573 | sort($ary); |
277 | daniel-mar | 574 | foreach ($ary as $ini) { |
575 | if (!file_exists($ini)) continue; |
||
576 | |||
307 | daniel-mar | 577 | $manifest = new OIDplusPluginManifest(); |
578 | $manifest->loadManifest($ini); |
||
277 | daniel-mar | 579 | |
473 | daniel-mar | 580 | $class_name = $manifest->getPhpMainClass(); |
581 | if (OIDplus::baseConfig()->getValue('DISABLE_PLUGIN_'.$class_name, false)) { |
||
582 | continue; |
||
583 | } |
||
584 | |||
307 | daniel-mar | 585 | if ($flat) { |
586 | $out[] = $manifest; |
||
587 | } else { |
||
588 | $plugintype_folder = basename(dirname(dirname($ini))); |
||
589 | $pluginname_folder = basename(dirname($ini)); |
||
590 | |||
591 | if (!isset($out[$plugintype_folder])) $out[$plugintype_folder] = array(); |
||
592 | if (!isset($out[$plugintype_folder][$pluginname_folder])) $out[$plugintype_folder][$pluginname_folder] = array(); |
||
593 | $out[$plugintype_folder][$pluginname_folder] = $manifest; |
||
594 | } |
||
277 | daniel-mar | 595 | } |
596 | return $out; |
||
597 | } |
||
598 | |||
599 | public static function registerAllPlugins($pluginDirName, $expectedPluginClass, $registerCallback): array { |
||
600 | $out = array(); |
||
307 | daniel-mar | 601 | $ary = self::getAllPluginManifests($pluginDirName, false); |
320 | daniel-mar | 602 | $known_plugin_oids = array(); |
456 | daniel-mar | 603 | if (OIDplus::baseConfig()->getValue('DEBUG')) { |
604 | $fake_feature = uuid_to_oid(gen_uuid()); |
||
605 | } |
||
277 | daniel-mar | 606 | foreach ($ary as $plugintype_folder => $bry) { |
438 | daniel-mar | 607 | foreach ($bry as $pluginname_folder => $manifest) { |
608 | $class_name = $manifest->getPhpMainClass(); |
||
609 | |||
610 | // Before we load the plugin, we want to make some checks to confirm |
||
611 | // that the plugin is working correctly. |
||
612 | |||
307 | daniel-mar | 613 | if (!$class_name) { |
438 | daniel-mar | 614 | 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 | 615 | } |
297 | daniel-mar | 616 | if (OIDplus::baseConfig()->getValue('DISABLE_PLUGIN_'.$class_name, false)) { |
617 | continue; |
||
618 | } |
||
292 | daniel-mar | 619 | if (!class_exists($class_name)) { |
438 | daniel-mar | 620 | 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 | 621 | } |
279 | daniel-mar | 622 | if (!is_subclass_of($class_name, $expectedPluginClass)) { |
438 | daniel-mar | 623 | 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 | 624 | } |
438 | daniel-mar | 625 | if (($class_name!=$manifest->getTypeClass()) && (!is_subclass_of($class_name,$manifest->getTypeClass()))) { |
626 | 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 | 627 | } |
438 | daniel-mar | 628 | if (($manifest->getTypeClass()!=$expectedPluginClass) && (!is_subclass_of($manifest->getTypeClass(),$expectedPluginClass))) { |
629 | 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 | 630 | } |
631 | |||
438 | daniel-mar | 632 | $plugin_oid = $manifest->getOid(); |
320 | daniel-mar | 633 | if (!$plugin_oid) { |
438 | daniel-mar | 634 | throw new OIDplusException(_L('Plugin "%1/%2" is erroneous',$plugintype_folder,$pluginname_folder).': '._L('Does not have an OID')); |
320 | daniel-mar | 635 | } |
636 | if (!oid_valid_dotnotation($plugin_oid, false, false, 2)) { |
||
438 | daniel-mar | 637 | 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 | 638 | } |
639 | if (isset($known_plugin_oids[$plugin_oid])) { |
||
438 | daniel-mar | 640 | 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 | 641 | } else { |
642 | $known_plugin_oids[$plugin_oid] = $plugintype_folder.'/'.$pluginname_folder; |
||
643 | } |
||
644 | |||
438 | daniel-mar | 645 | $obj = new $class_name(); |
456 | daniel-mar | 646 | |
647 | if (OIDplus::baseConfig()->getValue('DEBUG')) { |
||
648 | if ($obj->implementsFeature($fake_feature)) { |
||
649 | // see https://devblogs.microsoft.com/oldnewthing/20040211-00/?p=40663 |
||
650 | throw new OIDplusException(_L('Plugin "%1/%2" is erroneous',$plugintype_folder,$pluginname_folder).': '._L('implementsFeature() always returns true')); |
||
651 | } |
||
438 | daniel-mar | 652 | } |
653 | |||
654 | // TODO: Maybe as additional plugin-test, we should also check if plugins are allowed to define CSS/JS (since only page plugins may have them!) |
||
655 | $tmp = array_merge( |
||
656 | $manifest->getJSFiles(), |
||
657 | $manifest->getCSSFiles(), |
||
658 | $manifest->getJSFilesSetup(), |
||
659 | $manifest->getCSSFilesSetup() |
||
660 | ); |
||
661 | foreach ($tmp as $file) { |
||
662 | if (!file_exists($file)) { |
||
663 | 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)); |
||
664 | } |
||
665 | } |
||
666 | |||
667 | // Now we can continue |
||
668 | |||
279 | daniel-mar | 669 | $out[] = $class_name; |
670 | if (!is_null($registerCallback)) { |
||
438 | daniel-mar | 671 | call_user_func($registerCallback, $obj); |
444 | daniel-mar | 672 | |
673 | // Alternative approaches: |
||
674 | //$registerCallback[0]::{$registerCallback[1]}($obj); |
||
675 | // or: |
||
676 | //forward_static_call($registerCallback, $obj); |
||
279 | daniel-mar | 677 | } |
277 | daniel-mar | 678 | } |
679 | |||
680 | } |
||
681 | return $out; |
||
682 | } |
||
683 | |||
227 | daniel-mar | 684 | # --- Initialization of OIDplus |
206 | daniel-mar | 685 | |
374 | daniel-mar | 686 | public static function init($html=true, $keepBaseConfig=true) { |
236 | daniel-mar | 687 | self::$html = $html; |
688 | |||
263 | daniel-mar | 689 | // Reset internal state, so we can re-init verything if required |
274 | daniel-mar | 690 | |
263 | daniel-mar | 691 | if (self::$old_config_format) { |
692 | // Note: This can only happen in very special cases (e.g. test cases) where you call init() twice |
||
360 | daniel-mar | 693 | 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 | 694 | } |
274 | daniel-mar | 695 | |
263 | daniel-mar | 696 | self::$config = null; |
374 | daniel-mar | 697 | 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 | 698 | self::$gui = null; |
699 | self::$authUtils = null; |
||
700 | self::$mailUtils = null; |
||
701 | self::$menuUtils = null; |
||
702 | self::$logger = null; |
||
703 | self::$sesHandler = null; |
||
295 | daniel-mar | 704 | self::$dbMainSession = null; |
705 | self::$dbIsolatedSession = null; |
||
263 | daniel-mar | 706 | self::$pagePlugins = array(); |
707 | self::$authPlugins = array(); |
||
289 | daniel-mar | 708 | self::$loggerPlugins = array(); |
263 | daniel-mar | 709 | self::$objectTypePlugins = array(); |
710 | self::$enabledObjectTypes = array(); |
||
711 | self::$disabledObjectTypes = array(); |
||
712 | self::$dbPlugins = array(); |
||
274 | daniel-mar | 713 | self::$sqlSlangPlugins = array(); |
355 | daniel-mar | 714 | self::$languagePlugins = array(); |
449 | daniel-mar | 715 | self::$designPlugins = array(); |
263 | daniel-mar | 716 | self::$system_id_cache = null; |
717 | self::$sslAvailableCache = null; |
||
468 | daniel-mar | 718 | self::$translationArray = array(); |
74 | daniel-mar | 719 | |
263 | daniel-mar | 720 | // Continue... |
721 | |||
294 | daniel-mar | 722 | OIDplus::baseConfig(); // this loads the base configuration located in userdata/baseconfig/config.inc.php (once!) |
263 | daniel-mar | 723 | // You can do changes to the configuration afterwards using OIDplus::baseConfig()->... |
724 | |||
150 | daniel-mar | 725 | // Register database types (highest priority) |
726 | |||
274 | daniel-mar | 727 | // SQL slangs |
728 | |||
294 | daniel-mar | 729 | self::registerAllPlugins('sqlSlang', 'OIDplusSqlSlangPlugin', array('OIDplus','registerSqlSlangPlugin')); |
274 | daniel-mar | 730 | foreach (OIDplus::getSqlSlangPlugins() as $plugin) { |
731 | $plugin->init($html); |
||
732 | } |
||
733 | |||
734 | // Database providers |
||
735 | |||
277 | daniel-mar | 736 | self::registerAllPlugins('database', 'OIDplusDatabasePlugin', array('OIDplus','registerDatabasePlugin')); |
237 | daniel-mar | 737 | foreach (OIDplus::getDatabasePlugins() as $plugin) { |
738 | $plugin->init($html); |
||
739 | } |
||
740 | |||
42 | daniel-mar | 741 | // Do redirect stuff etc. |
74 | daniel-mar | 742 | |
230 | daniel-mar | 743 | self::isSslAvailable(); // This function does automatic redirects |
61 | daniel-mar | 744 | |
263 | daniel-mar | 745 | // Construct the configuration manager |
66 | daniel-mar | 746 | |
263 | daniel-mar | 747 | OIDplus::config(); // During the construction, various system settings are prepared if required |
66 | daniel-mar | 748 | |
74 | daniel-mar | 749 | // Initialize public / private keys |
750 | |||
227 | daniel-mar | 751 | OIDplus::getPkiStatus(true); |
74 | daniel-mar | 752 | |
237 | daniel-mar | 753 | // Register non-DB plugins |
74 | daniel-mar | 754 | |
277 | daniel-mar | 755 | self::registerAllPlugins('*Pages', 'OIDplusPagePlugin', array('OIDplus','registerPagePlugin')); |
756 | self::registerAllPlugins('auth', 'OIDplusAuthPlugin', array('OIDplus','registerAuthPlugin')); |
||
289 | daniel-mar | 757 | self::registerAllPlugins('logger', 'OIDplusLoggerPlugin', array('OIDplus','registerLoggerPlugin')); |
277 | daniel-mar | 758 | self::registerAllPlugins('objectTypes', 'OIDplusObjectTypePlugin', array('OIDplus','registerObjectTypePlugin')); |
355 | daniel-mar | 759 | self::registerAllPlugins('language', 'OIDplusLanguagePlugin', array('OIDplus','registerLanguagePlugin')); |
449 | daniel-mar | 760 | self::registerAllPlugins('design', 'OIDplusDesignPlugin', array('OIDplus','registerDesignPlugin')); |
150 | daniel-mar | 761 | |
237 | daniel-mar | 762 | // Initialize non-DB plugins |
224 | daniel-mar | 763 | |
281 | daniel-mar | 764 | foreach (OIDplus::getPagePlugins() as $plugin) { |
74 | daniel-mar | 765 | $plugin->init($html); |
766 | } |
||
230 | daniel-mar | 767 | foreach (OIDplus::getAuthPlugins() as $plugin) { |
768 | $plugin->init($html); |
||
769 | } |
||
289 | daniel-mar | 770 | foreach (OIDplus::getLoggerPlugins() as $plugin) { |
771 | $plugin->init($html); |
||
772 | } |
||
230 | daniel-mar | 773 | foreach (OIDplus::getObjectTypePlugins() as $plugin) { |
774 | $plugin->init($html); |
||
775 | } |
||
355 | daniel-mar | 776 | foreach (OIDplus::getLanguagePlugins() as $plugin) { |
777 | $plugin->init($html); |
||
778 | } |
||
449 | daniel-mar | 779 | foreach (OIDplus::getDesignPlugins() as $plugin) { |
780 | $plugin->init($html); |
||
781 | } |
||
412 | daniel-mar | 782 | |
783 | // Initialize other stuff (i.e. things which require the logger!) |
||
784 | |||
785 | OIDplus::recognizeSystemUrl(); // Make sure "last_known_system_url" is set |
||
786 | OIDplus::recognizeVersion(); // Make sure "last_known_version" is set and a log entry is created |
||
2 | daniel-mar | 787 | } |
42 | daniel-mar | 788 | |
227 | daniel-mar | 789 | # --- System URL, System ID, PKI, and other functions |
790 | |||
412 | daniel-mar | 791 | private static function recognizeSystemUrl() { |
792 | try { |
||
496 | daniel-mar | 793 | $url = OIDplus::webpath(); |
412 | daniel-mar | 794 | OIDplus::config()->setValue('last_known_system_url', $url); |
795 | } catch (Exception $e) { |
||
796 | } |
||
797 | } |
||
497 | daniel-mar | 798 | |
496 | daniel-mar | 799 | private static function getExecutingScriptPathDepth() { |
800 | if (PHP_SAPI == 'cli') { |
||
801 | global $argv; |
||
802 | $test_dir = dirname(realpath($argv[0])); |
||
803 | } else { |
||
804 | if (!isset($_SERVER["SCRIPT_FILENAME"])) return false; |
||
805 | $test_dir = dirname($_SERVER['SCRIPT_FILENAME']); |
||
806 | } |
||
807 | $test_dir = str_replace('\\', '/', $test_dir); |
||
808 | $steps_up = 0; |
||
809 | 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! |
||
810 | $test_dir = dirname($test_dir); |
||
811 | $steps_up++; |
||
812 | if ($steps_up == 1000) return false; // to make sure there will never be an infinite loop |
||
813 | } |
||
814 | return $steps_up; |
||
815 | } |
||
412 | daniel-mar | 816 | |
496 | daniel-mar | 817 | private static function getSystemUrl($relative=false) { |
326 | daniel-mar | 818 | if (!$relative) { |
819 | $res = OIDplus::baseConfig()->getValue('EXPLICIT_ABSOLUTE_SYSTEM_URL', ''); |
||
820 | if ($res !== '') { |
||
494 | daniel-mar | 821 | return rtrim($res,'/').'/'; |
326 | daniel-mar | 822 | } |
495 | daniel-mar | 823 | if (PHP_SAPI == 'cli') { |
494 | daniel-mar | 824 | try { |
825 | return OIDplus::config()->getValue('last_known_system_url', false); |
||
826 | } catch (Exception $e) { |
||
827 | return false; |
||
828 | } |
||
829 | } |
||
326 | daniel-mar | 830 | } |
831 | |||
494 | daniel-mar | 832 | // First, try to find out how many levels we need to go up |
496 | daniel-mar | 833 | $steps_up = self::getExecutingScriptPathDepth(); |
834 | if ($steps_up === false) return false; |
||
227 | daniel-mar | 835 | |
496 | daniel-mar | 836 | // Now go up these amount of levels, based on SCRIPT_NAME/argv[0] |
495 | daniel-mar | 837 | if (PHP_SAPI == 'cli') { |
838 | if ($relative) { |
||
839 | return str_repeat('../',$steps_up); |
||
840 | } else { |
||
841 | return false; |
||
842 | } |
||
843 | } else { |
||
844 | $res = dirname($_SERVER['SCRIPT_NAME'].'index.php'); // This fake 'index.php' ensures that SCRIPT_NAME does not end with '/', which would make dirname() fail |
||
845 | for ($i=0; $i<$steps_up; $i++) { |
||
846 | $res = dirname($res); |
||
847 | } |
||
848 | $res = str_replace('\\', '/', $res); |
||
849 | if ($res == '/') $res = ''; |
||
850 | $res .= '/'; |
||
496 | daniel-mar | 851 | |
495 | daniel-mar | 852 | // Do we want to have an absolute URI? |
853 | if (!$relative) { |
||
854 | $is_ssl = isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] === 'on'); |
||
855 | $protocol = $is_ssl ? 'https' : 'http'; // do not translate |
||
856 | $host = $_SERVER['HTTP_HOST']; // includes port if it is not 80/443 |
||
857 | $res = $protocol.'://'.$host.$res; |
||
858 | } |
||
496 | daniel-mar | 859 | |
495 | daniel-mar | 860 | return $res; |
227 | daniel-mar | 861 | } |
862 | } |
||
863 | |||
864 | private static $system_id_cache = null; |
||
865 | public static function getSystemId($oid=false) { |
||
866 | if (!is_null(self::$system_id_cache)) { |
||
867 | $out = self::$system_id_cache; |
||
868 | } else { |
||
869 | $out = false; |
||
870 | |||
871 | if (self::getPkiStatus(true)) { |
||
872 | $pubKey = OIDplus::config()->getValue('oidplus_public_key'); |
||
386 | daniel-mar | 873 | $m = array(); |
227 | daniel-mar | 874 | if (preg_match('@BEGIN PUBLIC KEY\-+(.+)\-+END PUBLIC KEY@ismU', $pubKey, $m)) { |
875 | $out = smallhash(base64_decode($m[1])); |
||
876 | } |
||
877 | } |
||
878 | self::$system_id_cache = $out; |
||
879 | } |
||
350 | daniel-mar | 880 | if (!$out) return false; |
291 | daniel-mar | 881 | return ($oid ? '1.3.6.1.4.1.37476.30.9.' : '').$out; |
227 | daniel-mar | 882 | } |
883 | |||
884 | public static function getPkiStatus($try_generate=true) { |
||
885 | if (!function_exists('openssl_pkey_new')) return false; |
||
886 | |||
887 | $privKey = OIDplus::config()->getValue('oidplus_private_key'); |
||
888 | $pubKey = OIDplus::config()->getValue('oidplus_public_key'); |
||
889 | |||
890 | if ($try_generate && !verify_private_public_key($privKey, $pubKey)) { |
||
263 | daniel-mar | 891 | $pkey_config = array( |
227 | daniel-mar | 892 | "digest_alg" => "sha512", |
893 | "private_key_bits" => 2048, |
||
894 | "private_key_type" => OPENSSL_KEYTYPE_RSA, |
||
895 | ); |
||
896 | |||
897 | // Create the private and public key |
||
263 | daniel-mar | 898 | $res = openssl_pkey_new($pkey_config); |
256 | daniel-mar | 899 | |
239 | daniel-mar | 900 | if (!$res) return false; |
227 | daniel-mar | 901 | |
902 | // Extract the private key from $res to $privKey |
||
903 | openssl_pkey_export($res, $privKey); |
||
904 | |||
905 | // Extract the public key from $res to $pubKey |
||
906 | $pubKey = openssl_pkey_get_details($res)["key"]; |
||
907 | |||
239 | daniel-mar | 908 | // Log |
288 | daniel-mar | 909 | OIDplus::logger()->log("[INFO]A!", "Generating new SystemID using a new key pair"); |
239 | daniel-mar | 910 | |
227 | daniel-mar | 911 | // Save the key pair to database |
912 | OIDplus::config()->setValue('oidplus_private_key', $privKey); |
||
913 | OIDplus::config()->setValue('oidplus_public_key', $pubKey); |
||
914 | |||
915 | // Log the new system ID |
||
386 | daniel-mar | 916 | $m = array(); |
227 | daniel-mar | 917 | if (preg_match('@BEGIN PUBLIC KEY\-+(.+)\-+END PUBLIC KEY@ismU', $pubKey, $m)) { |
918 | $system_id = smallhash(base64_decode($m[1])); |
||
288 | daniel-mar | 919 | OIDplus::logger()->log("[INFO]A!", "Your SystemID is now $system_id"); |
227 | daniel-mar | 920 | } |
921 | } |
||
922 | |||
923 | return verify_private_public_key($privKey, $pubKey); |
||
924 | } |
||
925 | |||
170 | daniel-mar | 926 | public static function getInstallType() { |
486 | daniel-mar | 927 | $counter = 0; |
928 | |||
496 | daniel-mar | 929 | if ($version_file_exists = file_exists(OIDplus::localpath().'oidplus_version.txt')) |
486 | daniel-mar | 930 | $counter++; |
496 | daniel-mar | 931 | if ($svn_dir_exists = is_dir(OIDplus::localpath().'.svn')) |
486 | daniel-mar | 932 | $counter++; |
496 | daniel-mar | 933 | if ($git_dir_exists = is_dir(OIDplus::localpath().'.git')) |
486 | daniel-mar | 934 | $counter++; |
935 | |||
936 | if ($counter === 0) { |
||
360 | daniel-mar | 937 | return 'unknown'; // do not translate |
170 | daniel-mar | 938 | } |
486 | daniel-mar | 939 | if ($counter > 1) { |
360 | daniel-mar | 940 | return 'ambigous'; // do not translate |
170 | daniel-mar | 941 | } |
486 | daniel-mar | 942 | if ($svn_dir_exists) { |
360 | daniel-mar | 943 | return 'svn-wc'; // do not translate |
170 | daniel-mar | 944 | } |
486 | daniel-mar | 945 | if ($git_dir_exists) { |
946 | return 'git-wc'; // do not translate |
||
947 | } |
||
948 | if ($version_file_exists) { |
||
360 | daniel-mar | 949 | return 'svn-snapshot'; // do not translate |
170 | daniel-mar | 950 | } |
951 | } |
||
952 | |||
412 | daniel-mar | 953 | private static function recognizeVersion() { |
954 | try { |
||
955 | $ver_prev = OIDplus::config()->getValue("last_known_version"); |
||
956 | $ver_now = OIDplus::getVersion(); |
||
957 | if (($ver_now != '') && ($ver_prev != '') && ($ver_now != $ver_prev)) { |
||
455 | daniel-mar | 958 | // 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 | 959 | OIDplus::logger()->log("[INFO]A!", "System version changed from '$ver_prev' to '$ver_now'"); |
960 | } |
||
961 | OIDplus::config()->setValue("last_known_version", $ver_now); |
||
962 | } catch (Exception $e) { |
||
963 | } |
||
964 | } |
||
965 | |||
111 | daniel-mar | 966 | public static function getVersion() { |
412 | daniel-mar | 967 | static $cachedVersion = null; |
968 | if (!is_null($cachedVersion)) { |
||
969 | return $cachedVersion; |
||
970 | } |
||
971 | |||
486 | daniel-mar | 972 | $installType = OIDplus::getInstallType(); |
973 | |||
974 | if ($installType === 'svn-wc') { |
||
496 | daniel-mar | 975 | $ver = get_svn_revision(OIDplus::localpath()); |
486 | daniel-mar | 976 | if ($ver) |
977 | return ($cachedVersion = 'svn-'.$ver); |
||
111 | daniel-mar | 978 | } |
162 | daniel-mar | 979 | |
486 | daniel-mar | 980 | if ($installType === 'git-wc') { |
496 | daniel-mar | 981 | $ver = get_gitsvn_revision(OIDplus::localpath()); |
486 | daniel-mar | 982 | if ($ver) |
983 | return ($cachedVersion = 'svn-'.$ver); |
||
162 | daniel-mar | 984 | } |
985 | |||
486 | daniel-mar | 986 | if ($installType === 'svn-snapshot') { |
496 | daniel-mar | 987 | $cont = file_get_contents(OIDplus::localpath().'oidplus_version.txt'); |
386 | daniel-mar | 988 | $m = array(); |
360 | daniel-mar | 989 | if (preg_match('@Revision (\d+)@', $cont, $m)) // do not translate |
412 | daniel-mar | 990 | return ($cachedVersion = 'svn-'.$m[1]); // do not translate |
162 | daniel-mar | 991 | } |
992 | |||
486 | daniel-mar | 993 | return ($cachedVersion = false); // version ambigous or unknown |
111 | daniel-mar | 994 | } |
995 | |||
230 | daniel-mar | 996 | private static $sslAvailableCache = null; |
997 | public static function isSslAvailable() { |
||
998 | if (!is_null(self::$sslAvailableCache)) return self::$sslAvailableCache; |
||
256 | daniel-mar | 999 | |
495 | daniel-mar | 1000 | if (PHP_SAPI == 'cli') { |
230 | daniel-mar | 1001 | self::$sslAvailableCache = false; |
1002 | return false; |
||
1003 | } |
||
1004 | |||
49 | daniel-mar | 1005 | $timeout = 2; |
80 | daniel-mar | 1006 | $already_ssl = isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == "on"); |
1007 | $ssl_port = 443; |
||
496 | daniel-mar | 1008 | $cookie_path = OIDplus::webpath(null,true); |
83 | daniel-mar | 1009 | if (empty($cookie_path)) $cookie_path = '/'; |
42 | daniel-mar | 1010 | |
261 | daniel-mar | 1011 | $mode = OIDplus::baseConfig()->getValue('ENFORCE_SSL', 2/*auto*/); |
1012 | |||
1013 | if ($mode == 0) { |
||
80 | daniel-mar | 1014 | // No SSL available |
230 | daniel-mar | 1015 | self::$sslAvailableCache = $already_ssl; |
80 | daniel-mar | 1016 | return $already_ssl; |
1017 | } |
||
1018 | |||
261 | daniel-mar | 1019 | if ($mode == 1) { |
80 | daniel-mar | 1020 | // Force SSL |
1021 | if ($already_ssl) { |
||
230 | daniel-mar | 1022 | self::$sslAvailableCache = true; |
80 | daniel-mar | 1023 | return true; |
42 | daniel-mar | 1024 | } else { |
80 | daniel-mar | 1025 | $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
1026 | header('Location:'.$location); |
||
360 | daniel-mar | 1027 | die(_L('Redirecting to HTTPS...')); |
230 | daniel-mar | 1028 | self::$sslAvailableCache = true; |
80 | daniel-mar | 1029 | return true; |
1030 | } |
||
1031 | } |
||
1032 | |||
261 | daniel-mar | 1033 | if ($mode == 2) { |
80 | daniel-mar | 1034 | // Automatic SSL detection |
1035 | |||
1036 | if ($already_ssl) { |
||
1037 | // we are already on HTTPS |
||
83 | daniel-mar | 1038 | setcookie('SSL_CHECK', '1', 0, $cookie_path, '', false, true); |
230 | daniel-mar | 1039 | self::$sslAvailableCache = true; |
80 | daniel-mar | 1040 | return true; |
1041 | } else { |
||
1042 | if (isset($_COOKIE['SSL_CHECK'])) { |
||
1043 | // We already had the HTTPS detection done before. |
||
1044 | if ($_COOKIE['SSL_CHECK']) { |
||
1045 | // HTTPS was detected before, but we are HTTP. Redirect now |
||
1046 | $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
||
1047 | header('Location:'.$location); |
||
360 | daniel-mar | 1048 | die(_L('Redirecting to HTTPS...')); |
230 | daniel-mar | 1049 | self::$sslAvailableCache = true; |
80 | daniel-mar | 1050 | return true; |
1051 | } else { |
||
1052 | // No HTTPS available. Do nothing. |
||
230 | daniel-mar | 1053 | self::$sslAvailableCache = false; |
80 | daniel-mar | 1054 | return false; |
1055 | } |
||
49 | daniel-mar | 1056 | } else { |
80 | daniel-mar | 1057 | // This is our first check (or the browser didn't accept the SSL_CHECK cookie) |
386 | daniel-mar | 1058 | $errno = -1; |
1059 | $errstr = ''; |
||
80 | daniel-mar | 1060 | if (@fsockopen($_SERVER['HTTP_HOST'], $ssl_port, $errno, $errstr, $timeout)) { |
1061 | // HTTPS detected. Redirect now, and remember that we had detected HTTPS |
||
83 | daniel-mar | 1062 | setcookie('SSL_CHECK', '1', 0, $cookie_path, '', false, true); |
80 | daniel-mar | 1063 | $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
1064 | header('Location:'.$location); |
||
360 | daniel-mar | 1065 | die(_L('Redirecting to HTTPS...')); |
230 | daniel-mar | 1066 | self::$sslAvailableCache = true; |
80 | daniel-mar | 1067 | return true; |
1068 | } else { |
||
1069 | // No HTTPS detected. Do nothing, and next time, don't try to detect HTTPS again. |
||
83 | daniel-mar | 1070 | setcookie('SSL_CHECK', '0', 0, $cookie_path, '', false, true); |
230 | daniel-mar | 1071 | self::$sslAvailableCache = false; |
80 | daniel-mar | 1072 | return false; |
1073 | } |
||
49 | daniel-mar | 1074 | } |
42 | daniel-mar | 1075 | } |
1076 | } |
||
1077 | } |
||
497 | daniel-mar | 1078 | |
496 | daniel-mar | 1079 | /** |
1080 | * Gets a local path pointing to a resource |
||
1081 | * @param string $target Target resource (file or directory must exist), or null to get the OIDplus base directory |
||
1082 | * @param boolean $relative If true, the returning path is relative to the currently executed PHP file (not the CLI working directory) |
||
1083 | * @return string The local path, with guaranteed trailing path delimiter for directories |
||
1084 | */ |
||
1085 | public static function localpath($target=null, $relative=false) { |
||
1086 | if (is_null($target)) { |
||
1087 | $target = __DIR__.'/../../'; |
||
1088 | } |
||
241 | daniel-mar | 1089 | |
496 | daniel-mar | 1090 | if ($relative) { |
1091 | // First, try to find out how many levels we need to go up |
||
1092 | $steps_up = self::getExecutingScriptPathDepth(); |
||
1093 | if ($steps_up === false) return false; |
||
497 | daniel-mar | 1094 | |
496 | daniel-mar | 1095 | // Virtually go back from the executing PHP script to the OIDplus base path |
1096 | $res = str_repeat('../',$steps_up); |
||
497 | daniel-mar | 1097 | |
496 | daniel-mar | 1098 | // Then go to the desired location |
1099 | $basedir = realpath(__DIR__.'/../../'); |
||
1100 | $target = realpath($target); |
||
500 | daniel-mar | 1101 | if ($target === false) return false; |
496 | daniel-mar | 1102 | $res .= substr($target, strlen($basedir)+1); |
1103 | $res = rtrim($res,'/'); // avoid '..//' for localpath(null,true) |
||
1104 | } else { |
||
1105 | $res = realpath($target); |
||
241 | daniel-mar | 1106 | } |
497 | daniel-mar | 1107 | |
498 | daniel-mar | 1108 | if (is_null($target) || is_dir($target)) $res .= DIRECTORY_SEPARATOR; |
497 | daniel-mar | 1109 | |
496 | daniel-mar | 1110 | return $res; |
241 | daniel-mar | 1111 | } |
355 | daniel-mar | 1112 | |
496 | daniel-mar | 1113 | /** |
1114 | * Gets a URL pointing to a resource |
||
1115 | * @param string $target Target resource (file or directory must exist), or null to get the OIDplus base directory |
||
1116 | * @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!) |
||
1117 | * @return string The URL, with guaranteed trailing path delimiter for directories |
||
1118 | */ |
||
1119 | public static function webpath($target=null, $relative=false) { |
||
1120 | $res = self::getSystemUrl($relative); // Note: already contains a trailing path delimiter |
||
1121 | if (!$res) return false; |
||
497 | daniel-mar | 1122 | |
496 | daniel-mar | 1123 | if (!is_null($target)) { |
1124 | $basedir = realpath(__DIR__.'/../../'); |
||
1125 | $target = realpath($target); |
||
500 | daniel-mar | 1126 | if ($target === false) return false; |
496 | daniel-mar | 1127 | $tmp = substr($target, strlen($basedir)+1); |
500 | daniel-mar | 1128 | $res .= str_replace(DIRECTORY_SEPARATOR,'/',$tmp); // remove OS specific path delimiters introduced by realpath() |
497 | daniel-mar | 1129 | if (is_dir($target)) $res .= '/'; |
496 | daniel-mar | 1130 | } |
497 | daniel-mar | 1131 | |
496 | daniel-mar | 1132 | return $res; |
1133 | } |
||
497 | daniel-mar | 1134 | |
360 | daniel-mar | 1135 | public static function getAvailableLangs() { |
1136 | $langs = array(); |
||
1137 | foreach (OIDplus::getAllPluginManifests('language') as $pluginManifest) { |
||
389 | daniel-mar | 1138 | $code = $pluginManifest->getLanguageCode(); |
360 | daniel-mar | 1139 | $langs[] = $code; |
1140 | } |
||
1141 | return $langs; |
||
1142 | } |
||
1143 | |||
355 | daniel-mar | 1144 | public static function getCurrentLang() { |
360 | daniel-mar | 1145 | if (isset($_GET['lang'])) { |
1146 | $lang = $_GET['lang']; |
||
1147 | } else if (isset($_POST['lang'])) { |
||
1148 | $lang = $_POST['lang']; |
||
1149 | } else if (isset($_COOKIE['LANGUAGE'])) { |
||
1150 | $lang = $_COOKIE['LANGUAGE']; |
||
1151 | } else { |
||
1152 | $lang = self::DEFAULT_LANGUAGE; |
||
1153 | } |
||
1154 | $lang = substr(preg_replace('@[^a-z]@ismU', '', $lang),0,4); // sanitize |
||
355 | daniel-mar | 1155 | return $lang; |
1156 | } |
||
1157 | |||
362 | daniel-mar | 1158 | public static function handleLangArgument() { |
1159 | if (isset($_GET['lang'])) { |
||
1160 | // The "?lang=" argument is only for NoScript-Browsers/SearchEngines |
||
1161 | // In case someone who has JavaScript clicks a ?lang= link, they should get |
||
1162 | // the page in that language, but the cookie must be set, otherwise |
||
1163 | // the menu and other stuff would be in their cookie-based-language and not the |
||
1164 | // argument-based-language. |
||
496 | daniel-mar | 1165 | $cookie_path = OIDplus::webpath(null,true); |
362 | daniel-mar | 1166 | if (empty($cookie_path)) $cookie_path = '/'; |
1167 | setcookie('LANGUAGE', $_GET['lang'], 0, $cookie_path, '', false, false/*HttpOnly off, because JavaScript also needs translation*/); |
||
1168 | } else if (isset($_POST['lang'])) { |
||
496 | daniel-mar | 1169 | $cookie_path = OIDplus::webpath(null,true); |
362 | daniel-mar | 1170 | if (empty($cookie_path)) $cookie_path = '/'; |
1171 | setcookie('LANGUAGE', $_POST['lang'], 0, $cookie_path, '', false, false/*HttpOnly off, because JavaScript also needs translation*/); |
||
1172 | } |
||
1173 | } |
||
1174 | |||
468 | daniel-mar | 1175 | private static $translationArray = array(); |
469 | daniel-mar | 1176 | protected static function getTranslationFileContents($translation_file) { |
1177 | // First, try the cache |
||
481 | daniel-mar | 1178 | $cache_file = __DIR__ . '/../../userdata/cache/translation_'.md5($translation_file).'.ser'; |
469 | daniel-mar | 1179 | if (file_exists($cache_file) && (filemtime($cache_file) == filemtime($translation_file))) { |
1180 | $cac = @unserialize(file_get_contents($cache_file)); |
||
1181 | if ($cac) return $cac; |
||
1182 | } |
||
481 | daniel-mar | 1183 | |
469 | daniel-mar | 1184 | // If not successful, then load the XML file |
1185 | $xml = @simplexml_load_string(file_get_contents($translation_file)); |
||
1186 | 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 |
||
1187 | $cac = array(); |
||
1188 | foreach ($xml->message as $msg) { |
||
1189 | $src = trim($msg->source->__toString()); |
||
1190 | $dst = trim($msg->target->__toString()); |
||
1191 | $cac[$src] = $dst; |
||
1192 | } |
||
1193 | @file_put_contents($cache_file,serialize($cac)); |
||
1194 | @touch($cache_file,filemtime($translation_file)); |
||
1195 | return $cac; |
||
1196 | } |
||
468 | daniel-mar | 1197 | public static function getTranslationArray($requested_lang='*') { |
362 | daniel-mar | 1198 | foreach (OIDplus::getAllPluginManifests('language') as $pluginManifest) { |
389 | daniel-mar | 1199 | $lang = $pluginManifest->getLanguageCode(); |
362 | daniel-mar | 1200 | if (strpos($lang,'/') !== false) continue; // just to be sure |
1201 | if (strpos($lang,'\\') !== false) continue; // just to be sure |
||
1202 | if (strpos($lang,'..') !== false) continue; // just to be sure |
||
401 | daniel-mar | 1203 | |
468 | daniel-mar | 1204 | if (($requested_lang != '*') && ($lang != $requested_lang)) continue; |
401 | daniel-mar | 1205 | |
468 | daniel-mar | 1206 | if (!isset(self::$translationArray[$lang])) { |
1207 | self::$translationArray[$lang] = array(); |
||
1208 | |||
1209 | $wildcard = $pluginManifest->getLanguageMessages(); |
||
1210 | if (strpos($wildcard,'/') !== false) continue; // just to be sure |
||
1211 | if (strpos($wildcard,'\\') !== false) continue; // just to be sure |
||
1212 | if (strpos($wildcard,'..') !== false) continue; // just to be sure |
||
1213 | |||
1214 | $translation_files = glob(__DIR__.'/../../plugins/language/'.$lang.'/'.$wildcard); |
||
1215 | sort($translation_files); |
||
1216 | foreach ($translation_files as $translation_file) { |
||
1217 | if (!file_exists($translation_file)) continue; |
||
469 | daniel-mar | 1218 | $cac = self::getTranslationFileContents($translation_file); |
1219 | foreach ($cac as $src => $dst) { |
||
1220 | self::$translationArray[$lang][$src] = $dst; |
||
468 | daniel-mar | 1221 | } |
401 | daniel-mar | 1222 | } |
362 | daniel-mar | 1223 | } |
1224 | } |
||
468 | daniel-mar | 1225 | return self::$translationArray; |
362 | daniel-mar | 1226 | } |
1227 | |||
374 | daniel-mar | 1228 | } |