Rev 294 | Rev 297 | 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(); |
2 | daniel-mar | 29 | |
280 | daniel-mar | 30 | protected static $html = true; |
236 | daniel-mar | 31 | |
2 | daniel-mar | 32 | private function __construct() { |
33 | } |
||
295 | daniel-mar | 34 | |
263 | daniel-mar | 35 | # --- Static classes |
274 | daniel-mar | 36 | |
263 | daniel-mar | 37 | private static $baseConfig = null; |
38 | private static $old_config_format = false; |
||
261 | daniel-mar | 39 | public static function baseConfig() { |
263 | daniel-mar | 40 | $first_init = false; |
274 | daniel-mar | 41 | |
263 | daniel-mar | 42 | if ($first_init = is_null(self::$baseConfig)) { |
43 | self::$baseConfig = new OIDplusBaseConfig(); |
||
261 | daniel-mar | 44 | } |
45 | |||
263 | daniel-mar | 46 | if ($first_init) { |
261 | daniel-mar | 47 | // Include a file containing various size/depth limitations of OIDs |
294 | daniel-mar | 48 | // It is important to include it before userdata/baseconfig/config.inc.php was included, |
49 | // so we can give userdata/baseconfig/config.inc.php the chance to override the values. |
||
261 | daniel-mar | 50 | |
294 | daniel-mar | 51 | include OIDplus::basePath().'/includes/limits.inc.php'; |
261 | daniel-mar | 52 | |
53 | // Include config file |
||
295 | daniel-mar | 54 | |
294 | daniel-mar | 55 | $config_file = OIDplus::basePath() . '/userdata/baseconfig/config.inc.php'; |
56 | $config_file_old = OIDplus::basePath() . '/includes/config.inc.php'; // backwards compatibility |
||
295 | daniel-mar | 57 | |
294 | daniel-mar | 58 | if (!file_exists($config_file) && file_exists($config_file_old)) { |
59 | $config_file = $config_file_old; |
||
60 | } |
||
261 | daniel-mar | 61 | |
294 | daniel-mar | 62 | if (file_exists($config_file)) { |
263 | daniel-mar | 63 | if (self::$old_config_format) { |
64 | // Note: We may only include it once due to backwards compatibility, |
||
65 | // since in version 2.0, the configuration was defined using define() statements |
||
66 | // Attention: This does mean that a full re-init (e.g. for test cases) is not possible |
||
67 | // if a version 2.0 config is used! |
||
294 | daniel-mar | 68 | include_once $config_file; |
263 | daniel-mar | 69 | } else { |
294 | daniel-mar | 70 | include $config_file; |
263 | daniel-mar | 71 | } |
261 | daniel-mar | 72 | |
73 | if (defined('OIDPLUS_CONFIG_VERSION') && (OIDPLUS_CONFIG_VERSION == 2.0)) { |
||
263 | daniel-mar | 74 | self::$old_config_format = true; |
274 | daniel-mar | 75 | |
261 | daniel-mar | 76 | // Backwards compatibility 2.0 => 2.1 |
77 | foreach (get_defined_constants(true)['user'] as $name => $value) { |
||
78 | $name = str_replace('OIDPLUS_', '', $name); |
||
79 | if ($name == 'SESSION_SECRET') $name = 'SERVER_SECRET'; |
||
80 | if ($name == 'MYSQL_QUERYLOG') $name = 'QUERY_LOGFILE'; |
||
81 | if (($name == 'MYSQL_PASSWORD') || ($name == 'ODBC_PASSWORD') || ($name == 'PDO_PASSWORD') || ($name == 'PGSQL_PASSWORD')) { |
||
263 | daniel-mar | 82 | self::$baseConfig->setValue($name, base64_decode($value)); |
261 | daniel-mar | 83 | } else { |
84 | if ($name == 'CONFIG_VERSION') $value = 2.1; |
||
263 | daniel-mar | 85 | self::$baseConfig->setValue($name, $value); |
261 | daniel-mar | 86 | } |
87 | } |
||
88 | } |
||
89 | } else { |
||
294 | daniel-mar | 90 | if (!is_dir(OIDplus::basePath().'/setup')) { |
91 | throw new OIDplusConfigInitializationException('File userdata/baseconfig/config.inc.php is missing, but setup can\'t be started because its directory missing.'); |
||
261 | daniel-mar | 92 | } else { |
280 | daniel-mar | 93 | if (self::$html) { |
261 | daniel-mar | 94 | header('Location:'.OIDplus::getSystemUrl().'setup/'); |
95 | die('Redirecting to setup...'); |
||
96 | } else { |
||
97 | // This can be displayed in e.g. ajax.php |
||
294 | daniel-mar | 98 | throw new OIDplusConfigInitializationException('File userdata/baseconfig/config.inc.php is missing. Please run setup again.'); |
261 | daniel-mar | 99 | } |
100 | } |
||
101 | } |
||
102 | |||
103 | // Check important config settings |
||
104 | |||
263 | daniel-mar | 105 | if (self::$baseConfig->getValue('CONFIG_VERSION') != 2.1) { |
294 | daniel-mar | 106 | throw new OIDplusConfigInitializationException("The information located in $config_file is outdated."); |
261 | daniel-mar | 107 | } |
108 | |||
263 | daniel-mar | 109 | if (self::$baseConfig->getValue('SERVER_SECRET', '') === '') { |
294 | daniel-mar | 110 | throw new OIDplusConfigInitializationException("You must set a value for SERVER_SECRET in $config_file for the system to operate secure."); |
261 | daniel-mar | 111 | } |
112 | } |
||
113 | |||
263 | daniel-mar | 114 | return self::$baseConfig; |
261 | daniel-mar | 115 | } |
116 | |||
263 | daniel-mar | 117 | private static $config = null; |
2 | daniel-mar | 118 | public static function config() { |
263 | daniel-mar | 119 | if ($first_init = is_null(self::$config)) { |
120 | self::$config = new OIDplusConfig(); |
||
2 | daniel-mar | 121 | } |
263 | daniel-mar | 122 | |
123 | if ($first_init) { |
||
124 | // These are important settings for base functionalities and therefore are not inside plugins |
||
125 | self::$config->prepareConfigKey('system_title', 'What is the name of your RA?', 'OIDplus 2.0', OIDplusConfig::PROTECTION_EDITABLE, function($value) { |
||
126 | if (empty($value)) { |
||
127 | throw new OIDplusException("Please enter a value for the system title."); |
||
128 | } |
||
129 | }); |
||
130 | self::$config->prepareConfigKey('admin_email', 'E-Mail address of the system administrator', '', OIDplusConfig::PROTECTION_EDITABLE, function($value) { |
||
131 | if (!empty($value) && !OIDplus::mailUtils()->validMailAddress($value)) { |
||
132 | throw new OIDplusException("This is not a correct email address"); |
||
133 | } |
||
134 | }); |
||
135 | self::$config->prepareConfigKey('global_cc', 'Global CC for all outgoing emails?', '', OIDplusConfig::PROTECTION_EDITABLE, function($value) { |
||
136 | if (!empty($value) && !OIDplus::mailUtils()->validMailAddress($value)) { |
||
137 | throw new OIDplusException("This is not a correct email address"); |
||
138 | } |
||
139 | }); |
||
140 | self::$config->prepareConfigKey('objecttypes_initialized', 'List of object type plugins that were initialized once', '', OIDplusConfig::PROTECTION_READONLY, function($value) { |
||
141 | // Nothing here yet |
||
142 | }); |
||
143 | 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) { |
||
144 | # 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? |
||
145 | |||
146 | $ary = explode(';',$value); |
||
147 | $uniq_ary = array_unique($ary); |
||
148 | |||
149 | if (count($ary) != count($uniq_ary)) { |
||
150 | throw new OIDplusException("Please check your input. Some object types are double."); |
||
151 | } |
||
152 | |||
153 | foreach ($ary as $ot_check) { |
||
154 | $ns_found = false; |
||
155 | foreach (OIDplus::getEnabledObjectTypes() as $ot) { |
||
156 | if ($ot::ns() == $ot_check) { |
||
157 | $ns_found = true; |
||
158 | break; |
||
159 | } |
||
160 | } |
||
161 | foreach (OIDplus::getDisabledObjectTypes() as $ot) { |
||
162 | if ($ot::ns() == $ot_check) { |
||
163 | $ns_found = true; |
||
164 | break; |
||
165 | } |
||
166 | } |
||
167 | if (!$ns_found) { |
||
168 | throw new OIDplusException("Please check your input. Namespace \"$ot_check\" is not found"); |
||
169 | } |
||
170 | } |
||
171 | }); |
||
172 | self::$config->prepareConfigKey('oidplus_private_key', 'Private key for this system', '', OIDplusConfig::PROTECTION_HIDDEN, function($value) { |
||
173 | // Nothing here yet |
||
174 | }); |
||
175 | 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) { |
||
176 | // Nothing here yet |
||
177 | }); |
||
178 | |||
179 | } |
||
180 | |||
181 | return self::$config; |
||
2 | daniel-mar | 182 | } |
183 | |||
263 | daniel-mar | 184 | private static $gui = null; |
2 | daniel-mar | 185 | public static function gui() { |
263 | daniel-mar | 186 | if (is_null(self::$gui)) { |
187 | self::$gui = new OIDplusGui(); |
||
86 | daniel-mar | 188 | } |
263 | daniel-mar | 189 | return self::$gui; |
2 | daniel-mar | 190 | } |
191 | |||
263 | daniel-mar | 192 | private static $authUtils = null; |
2 | daniel-mar | 193 | public static function authUtils() { |
263 | daniel-mar | 194 | if (is_null(self::$authUtils)) { |
195 | self::$authUtils = new OIDplusAuthUtils(); |
||
86 | daniel-mar | 196 | } |
263 | daniel-mar | 197 | return self::$authUtils; |
2 | daniel-mar | 198 | } |
199 | |||
263 | daniel-mar | 200 | private static $mailUtils = null; |
250 | daniel-mar | 201 | public static function mailUtils() { |
263 | daniel-mar | 202 | if (is_null(self::$mailUtils)) { |
203 | self::$mailUtils = new OIDplusMailUtils(); |
||
250 | daniel-mar | 204 | } |
263 | daniel-mar | 205 | return self::$mailUtils; |
250 | daniel-mar | 206 | } |
207 | |||
263 | daniel-mar | 208 | private static $menuUtils = null; |
250 | daniel-mar | 209 | public static function menuUtils() { |
263 | daniel-mar | 210 | if (is_null(self::$menuUtils)) { |
211 | self::$menuUtils = new OIDplusMenuUtils(); |
||
250 | daniel-mar | 212 | } |
263 | daniel-mar | 213 | return self::$menuUtils; |
250 | daniel-mar | 214 | } |
215 | |||
263 | daniel-mar | 216 | private static $logger = null; |
115 | daniel-mar | 217 | public static function logger() { |
263 | daniel-mar | 218 | if (is_null(self::$logger)) { |
219 | self::$logger = new OIDplusLogger(); |
||
115 | daniel-mar | 220 | } |
263 | daniel-mar | 221 | return self::$logger; |
115 | daniel-mar | 222 | } |
223 | |||
263 | daniel-mar | 224 | private static $sesHandler = null; |
86 | daniel-mar | 225 | public static function sesHandler() { |
263 | daniel-mar | 226 | if (is_null(self::$sesHandler)) { |
227 | self::$sesHandler = new OIDplusSessionHandler(); |
||
86 | daniel-mar | 228 | } |
263 | daniel-mar | 229 | return self::$sesHandler; |
86 | daniel-mar | 230 | } |
231 | |||
274 | daniel-mar | 232 | # --- SQL slang plugin |
233 | |||
234 | private static function registerSqlSlangPlugin(OIDplusSqlSlangPlugin $plugin) { |
||
235 | $name = $plugin::id(); |
||
236 | if ($name === false) return false; |
||
237 | |||
238 | self::$sqlSlangPlugins[$name] = $plugin; |
||
239 | |||
240 | return true; |
||
241 | } |
||
242 | |||
243 | public static function getSqlSlangPlugins() { |
||
244 | return self::$sqlSlangPlugins; |
||
245 | } |
||
246 | |||
230 | daniel-mar | 247 | # --- Database plugin |
74 | daniel-mar | 248 | |
227 | daniel-mar | 249 | private static function registerDatabasePlugin(OIDplusDatabasePlugin $plugin) { |
275 | daniel-mar | 250 | $name = $plugin::id(); |
150 | daniel-mar | 251 | if ($name === false) return false; |
252 | |||
253 | self::$dbPlugins[$name] = $plugin; |
||
254 | |||
255 | return true; |
||
256 | } |
||
257 | |||
258 | public static function getDatabasePlugins() { |
||
259 | return self::$dbPlugins; |
||
260 | } |
||
261 | |||
295 | daniel-mar | 262 | public static function getActiveDatabasePlugin() { |
261 | daniel-mar | 263 | if (OIDplus::baseConfig()->getValue('DATABASE_PLUGIN', '') === '') { |
260 | daniel-mar | 264 | throw new OIDplusConfigInitializationException("No database plugin selected in config file"); |
265 | } |
||
261 | daniel-mar | 266 | if (!isset(self::$dbPlugins[OIDplus::baseConfig()->getValue('DATABASE_PLUGIN')])) { |
267 | throw new OIDplusConfigInitializationException("Database plugin '".OIDplus::baseConfig()->getValue('DATABASE_PLUGIN')."' not found"); |
||
227 | daniel-mar | 268 | } |
295 | daniel-mar | 269 | return self::$dbPlugins[OIDplus::baseConfig()->getValue('DATABASE_PLUGIN')]; |
227 | daniel-mar | 270 | } |
271 | |||
295 | daniel-mar | 272 | private static $dbMainSession = null; |
273 | public static function db() { |
||
274 | if (is_null(self::$dbMainSession)) { |
||
275 | self::$dbMainSession = self::getActiveDatabasePlugin()->newConnection(); |
||
276 | } |
||
277 | if (!self::$dbMainSession->isConnected()) self::$dbMainSession->connect(); |
||
278 | return self::$dbMainSession; |
||
279 | } |
||
280 | |||
281 | private static $dbIsolatedSession = null; |
||
282 | public static function dbIsolated() { |
||
283 | if (is_null(self::$dbIsolatedSession)) { |
||
284 | self::$dbIsolatedSession = self::getActiveDatabasePlugin()->newConnection(); |
||
285 | } |
||
286 | if (!self::$dbIsolatedSession->isConnected()) self::$dbIsolatedSession->connect(); |
||
287 | return self::$dbIsolatedSession; |
||
288 | } |
||
289 | |||
227 | daniel-mar | 290 | # --- Page plugin |
291 | |||
224 | daniel-mar | 292 | private static function registerPagePlugin(OIDplusPagePlugin $plugin) { |
281 | daniel-mar | 293 | self::$pagePlugins[] = $plugin; |
61 | daniel-mar | 294 | |
295 | return true; |
||
296 | } |
||
297 | |||
281 | daniel-mar | 298 | public static function getPagePlugins() { |
299 | return self::$pagePlugins; |
||
61 | daniel-mar | 300 | } |
301 | |||
227 | daniel-mar | 302 | # --- Auth plugin |
303 | |||
304 | private static function registerAuthPlugin(OIDplusAuthPlugin $plugin) { |
||
305 | self::$authPlugins[] = $plugin; |
||
306 | return true; |
||
307 | } |
||
308 | |||
221 | daniel-mar | 309 | public static function getAuthPlugins() { |
310 | return self::$authPlugins; |
||
311 | } |
||
312 | |||
289 | daniel-mar | 313 | # --- Logger plugin |
314 | |||
315 | private static function registerLoggerPlugin(OIDplusLoggerPlugin $plugin) { |
||
316 | self::$loggerPlugins[] = $plugin; |
||
317 | return true; |
||
318 | } |
||
319 | |||
320 | public static function getLoggerPlugins() { |
||
321 | return self::$loggerPlugins; |
||
322 | } |
||
323 | |||
227 | daniel-mar | 324 | # --- Object type plugin |
325 | |||
326 | private static function registerObjectTypePlugin(OIDplusObjectTypePlugin $plugin) { |
||
327 | self::$objectTypePlugins[] = $plugin; |
||
328 | |||
329 | $ot = $plugin::getObjectTypeClassName(); |
||
330 | self::registerObjectType($ot); |
||
331 | |||
332 | return true; |
||
333 | } |
||
334 | |||
224 | daniel-mar | 335 | private static function registerObjectType($ot) { |
66 | daniel-mar | 336 | $ns = $ot::ns(); |
337 | |||
250 | daniel-mar | 338 | if (empty($ns)) throw new OIDplusException("Attention: Empty NS at $ot\n"); |
66 | daniel-mar | 339 | |
340 | $ns_found = false; |
||
227 | daniel-mar | 341 | foreach (array_merge(OIDplus::getEnabledObjectTypes(), OIDplus::getDisabledObjectTypes()) as $test_ot) { |
66 | daniel-mar | 342 | if ($test_ot::ns() == $ns) { |
343 | $ns_found = true; |
||
344 | break; |
||
345 | } |
||
346 | } |
||
347 | if ($ns_found) { |
||
250 | daniel-mar | 348 | throw new OIDplusException("Attention: Two objectType plugins use the same namespace \"$ns\"!"); |
66 | daniel-mar | 349 | } |
350 | |||
351 | $init = OIDplus::config()->getValue("objecttypes_initialized"); |
||
352 | $init_ary = empty($init) ? array() : explode(';', $init); |
||
70 | daniel-mar | 353 | $init_ary = array_map('trim', $init_ary); |
66 | daniel-mar | 354 | |
355 | $enabled = OIDplus::config()->getValue("objecttypes_enabled"); |
||
356 | $enabled_ary = empty($enabled) ? array() : explode(';', $enabled); |
||
70 | daniel-mar | 357 | $enabled_ary = array_map('trim', $enabled_ary); |
66 | daniel-mar | 358 | |
79 | daniel-mar | 359 | $do_enable = false; |
360 | if (in_array($ns, $enabled_ary)) { |
||
361 | $do_enable = true; |
||
362 | } else { |
||
363 | if (!OIDplus::config()->getValue('registration_done')) { |
||
364 | $do_enable = $ns == 'oid'; |
||
365 | } else { |
||
366 | $do_enable = !in_array($ns, $init_ary); |
||
367 | } |
||
368 | } |
||
369 | |||
370 | if ($do_enable) { |
||
227 | daniel-mar | 371 | self::$enabledObjectTypes[] = $ot; |
372 | usort(self::$enabledObjectTypes, function($a, $b) { |
||
66 | daniel-mar | 373 | $enabled = OIDplus::config()->getValue("objecttypes_enabled"); |
374 | $enabled_ary = explode(';', $enabled); |
||
375 | |||
376 | $idx_a = array_search($a::ns(), $enabled_ary); |
||
377 | $idx_b = array_search($b::ns(), $enabled_ary); |
||
378 | |||
379 | if ($idx_a == $idx_b) { |
||
380 | return 0; |
||
381 | } |
||
382 | return ($idx_a > $idx_b) ? +1 : -1; |
||
383 | }); |
||
74 | daniel-mar | 384 | } else { |
385 | self::$disabledObjectTypes[] = $ot; |
||
66 | daniel-mar | 386 | } |
387 | |||
388 | if (!in_array($ns, $init_ary)) { |
||
389 | // Was never initialized before, so we add it to the list of enabled object types once |
||
390 | |||
79 | daniel-mar | 391 | if ($do_enable) { |
392 | $enabled_ary[] = $ns; |
||
393 | OIDplus::config()->setValue("objecttypes_enabled", implode(';', $enabled_ary)); |
||
394 | } |
||
66 | daniel-mar | 395 | |
396 | $init_ary[] = $ns; |
||
397 | OIDplus::config()->setValue("objecttypes_initialized", implode(';', $init_ary)); |
||
398 | } |
||
61 | daniel-mar | 399 | } |
400 | |||
227 | daniel-mar | 401 | public static function getObjectTypePlugins() { |
402 | return self::$objectTypePlugins; |
||
61 | daniel-mar | 403 | } |
404 | |||
227 | daniel-mar | 405 | public static function getObjectTypePluginsEnabled() { |
406 | $res = array(); |
||
407 | foreach (self::$objectTypePlugins as $plugin) { |
||
408 | $ot = $plugin::getObjectTypeClassName(); |
||
409 | if (in_array($ot, self::$enabledObjectTypes)) $res[] = $plugin; |
||
410 | } |
||
411 | return $res; |
||
74 | daniel-mar | 412 | } |
413 | |||
227 | daniel-mar | 414 | public static function getObjectTypePluginsDisabled() { |
415 | $res = array(); |
||
416 | foreach (self::$objectTypePlugins as $plugin) { |
||
417 | $ot = $plugin::getObjectTypeClassName(); |
||
418 | if (in_array($ot, self::$disabledObjectTypes)) $res[] = $plugin; |
||
74 | daniel-mar | 419 | } |
227 | daniel-mar | 420 | return $res; |
74 | daniel-mar | 421 | } |
422 | |||
227 | daniel-mar | 423 | public static function getEnabledObjectTypes() { |
424 | return self::$enabledObjectTypes; |
||
425 | } |
||
74 | daniel-mar | 426 | |
227 | daniel-mar | 427 | public static function getDisabledObjectTypes() { |
428 | return self::$disabledObjectTypes; |
||
429 | } |
||
74 | daniel-mar | 430 | |
277 | daniel-mar | 431 | # --- Plugin handling functions |
432 | |||
433 | public static function getPluginInfo($class_name): array { |
||
434 | $reflector = new ReflectionClass($class_name); |
||
435 | $ini = dirname($reflector->getFileName()).'/manifest.ini'; |
||
436 | if (!file_exists($ini)) return array(); |
||
437 | $bry = parse_ini_file($ini, true, INI_SCANNER_TYPED); |
||
438 | if (!isset($bry['Plugin'])) return array(); |
||
439 | return $bry['Plugin']; |
||
440 | } |
||
441 | |||
442 | public static function getAllPluginManifests($pluginFolderMask='*'): array { |
||
443 | $out = array(); |
||
279 | daniel-mar | 444 | // Note: glob() will sort by default, so we do not need a page priority attribute. |
445 | // So you just need to use a numeric plugin directory prefix (padded). |
||
294 | daniel-mar | 446 | $ary = glob(OIDplus::basePath().'/plugins/'.$pluginFolderMask.'/'.'*'.'/manifest.ini'); |
277 | daniel-mar | 447 | foreach ($ary as $ini) { |
448 | if (!file_exists($ini)) continue; |
||
449 | $bry = parse_ini_file($ini, true, INI_SCANNER_TYPED); |
||
450 | |||
451 | $plugintype_folder = basename(dirname(dirname($ini))); |
||
452 | $pluginname_folder = basename(dirname($ini)); |
||
453 | |||
454 | if (!isset($out[$plugintype_folder])) $out[$plugintype_folder] = array(); |
||
455 | if (!isset($out[$plugintype_folder][$pluginname_folder])) $out[$plugintype_folder][$pluginname_folder] = array(); |
||
456 | $out[$plugintype_folder][$pluginname_folder] = $bry; |
||
457 | } |
||
458 | return $out; |
||
459 | } |
||
460 | |||
461 | public static function registerAllPlugins($pluginDirName, $expectedPluginClass, $registerCallback): array { |
||
462 | $out = array(); |
||
279 | daniel-mar | 463 | $ary = self::getAllPluginManifests($pluginDirName); |
277 | daniel-mar | 464 | foreach ($ary as $plugintype_folder => $bry) { |
465 | foreach ($bry as $pluginname_folder => $cry) { |
||
279 | daniel-mar | 466 | if (!isset($cry['PHP']) || !isset($cry['PHP']['pluginclass'])) { |
467 | throw new OIDplusException("Plugin '$plugintype_folder/$pluginname_folder' is errornous: Plugin class is not defined (manifest.ini section 'PHP', key 'pluginclass'"); |
||
277 | daniel-mar | 468 | } |
279 | daniel-mar | 469 | $class_name = $cry['PHP']['pluginclass']; |
292 | daniel-mar | 470 | if (!class_exists($class_name)) { |
471 | throw new OIDplusException("Plugin '$plugintype_folder/$pluginname_folder' is errornous: Plugin class '$class_name' not found (manifest.ini section 'PHP', key 'pluginclass'"); |
||
472 | } |
||
279 | daniel-mar | 473 | if (!is_subclass_of($class_name, $expectedPluginClass)) { |
474 | throw new OIDplusException("Plugin '$plugintype_folder/$pluginname_folder' is errornous: Plugin class '$class_name' is expected to be a subclass of '$expectedPluginClass'"); |
||
475 | } |
||
476 | $out[] = $class_name; |
||
477 | if (!is_null($registerCallback)) { |
||
478 | call_user_func($registerCallback, new $class_name()); |
||
479 | } |
||
277 | daniel-mar | 480 | } |
481 | |||
482 | } |
||
483 | return $out; |
||
484 | } |
||
485 | |||
227 | daniel-mar | 486 | # --- Initialization of OIDplus |
206 | daniel-mar | 487 | |
2 | daniel-mar | 488 | public static function init($html=true) { |
236 | daniel-mar | 489 | self::$html = $html; |
490 | |||
263 | daniel-mar | 491 | // Reset internal state, so we can re-init verything if required |
274 | daniel-mar | 492 | |
263 | daniel-mar | 493 | if (self::$old_config_format) { |
494 | // Note: This can only happen in very special cases (e.g. test cases) where you call init() twice |
||
495 | throw new OIDplusConfigInitializationException('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.'); |
||
496 | } |
||
274 | daniel-mar | 497 | |
263 | daniel-mar | 498 | self::$config = null; |
499 | self::$baseConfig = null; |
||
500 | self::$gui = null; |
||
501 | self::$authUtils = null; |
||
502 | self::$mailUtils = null; |
||
503 | self::$menuUtils = null; |
||
504 | self::$logger = null; |
||
505 | self::$sesHandler = null; |
||
295 | daniel-mar | 506 | self::$dbMainSession = null; |
507 | self::$dbIsolatedSession = null; |
||
263 | daniel-mar | 508 | self::$pagePlugins = array(); |
509 | self::$authPlugins = array(); |
||
289 | daniel-mar | 510 | self::$loggerPlugins = array(); |
263 | daniel-mar | 511 | self::$objectTypePlugins = array(); |
512 | self::$enabledObjectTypes = array(); |
||
513 | self::$disabledObjectTypes = array(); |
||
514 | self::$dbPlugins = array(); |
||
274 | daniel-mar | 515 | self::$sqlSlangPlugins = array(); |
263 | daniel-mar | 516 | self::$system_id_cache = null; |
517 | self::$sslAvailableCache = null; |
||
74 | daniel-mar | 518 | |
263 | daniel-mar | 519 | // Continue... |
520 | |||
294 | daniel-mar | 521 | OIDplus::baseConfig(); // this loads the base configuration located in userdata/baseconfig/config.inc.php (once!) |
263 | daniel-mar | 522 | // You can do changes to the configuration afterwards using OIDplus::baseConfig()->... |
523 | |||
150 | daniel-mar | 524 | // Register database types (highest priority) |
525 | |||
274 | daniel-mar | 526 | // SQL slangs |
527 | |||
294 | daniel-mar | 528 | self::registerAllPlugins('sqlSlang', 'OIDplusSqlSlangPlugin', array('OIDplus','registerSqlSlangPlugin')); |
274 | daniel-mar | 529 | foreach (OIDplus::getSqlSlangPlugins() as $plugin) { |
530 | $plugin->init($html); |
||
531 | } |
||
532 | |||
533 | // Database providers |
||
534 | |||
277 | daniel-mar | 535 | self::registerAllPlugins('database', 'OIDplusDatabasePlugin', array('OIDplus','registerDatabasePlugin')); |
237 | daniel-mar | 536 | foreach (OIDplus::getDatabasePlugins() as $plugin) { |
537 | $plugin->init($html); |
||
538 | } |
||
539 | |||
42 | daniel-mar | 540 | // Do redirect stuff etc. |
74 | daniel-mar | 541 | |
230 | daniel-mar | 542 | self::isSslAvailable(); // This function does automatic redirects |
61 | daniel-mar | 543 | |
263 | daniel-mar | 544 | // Construct the configuration manager |
66 | daniel-mar | 545 | |
263 | daniel-mar | 546 | OIDplus::config(); // During the construction, various system settings are prepared if required |
66 | daniel-mar | 547 | |
74 | daniel-mar | 548 | // Initialize public / private keys |
549 | |||
227 | daniel-mar | 550 | OIDplus::getPkiStatus(true); |
74 | daniel-mar | 551 | |
237 | daniel-mar | 552 | // Register non-DB plugins |
74 | daniel-mar | 553 | |
277 | daniel-mar | 554 | self::registerAllPlugins('*Pages', 'OIDplusPagePlugin', array('OIDplus','registerPagePlugin')); |
555 | self::registerAllPlugins('auth', 'OIDplusAuthPlugin', array('OIDplus','registerAuthPlugin')); |
||
289 | daniel-mar | 556 | self::registerAllPlugins('logger', 'OIDplusLoggerPlugin', array('OIDplus','registerLoggerPlugin')); |
277 | daniel-mar | 557 | self::registerAllPlugins('objectTypes', 'OIDplusObjectTypePlugin', array('OIDplus','registerObjectTypePlugin')); |
150 | daniel-mar | 558 | |
237 | daniel-mar | 559 | // Initialize non-DB plugins |
224 | daniel-mar | 560 | |
281 | daniel-mar | 561 | foreach (OIDplus::getPagePlugins() as $plugin) { |
74 | daniel-mar | 562 | $plugin->init($html); |
563 | } |
||
230 | daniel-mar | 564 | foreach (OIDplus::getAuthPlugins() as $plugin) { |
565 | $plugin->init($html); |
||
566 | } |
||
289 | daniel-mar | 567 | foreach (OIDplus::getLoggerPlugins() as $plugin) { |
568 | $plugin->init($html); |
||
569 | } |
||
230 | daniel-mar | 570 | foreach (OIDplus::getObjectTypePlugins() as $plugin) { |
571 | $plugin->init($html); |
||
572 | } |
||
2 | daniel-mar | 573 | } |
42 | daniel-mar | 574 | |
227 | daniel-mar | 575 | # --- System URL, System ID, PKI, and other functions |
576 | |||
294 | daniel-mar | 577 | public static function basePath() { |
578 | return realpath(__DIR__ . '/../../'); |
||
579 | } |
||
580 | |||
227 | daniel-mar | 581 | public static function getSystemUrl($relative=false) { |
582 | if (!isset($_SERVER["SCRIPT_NAME"])) return false; |
||
583 | |||
584 | $test_dir = dirname($_SERVER['SCRIPT_FILENAME']); |
||
269 | daniel-mar | 585 | $test_dir = str_replace('\\', '/', $test_dir); |
227 | daniel-mar | 586 | $c = 0; |
587 | while (!file_exists($test_dir.'/oidplus_base.js')) { |
||
588 | $test_dir = dirname($test_dir); |
||
589 | $c++; |
||
590 | if ($c == 1000) return false; |
||
591 | } |
||
592 | |||
593 | $res = dirname($_SERVER['SCRIPT_NAME'].'xxx'); |
||
594 | |||
595 | for ($i=1; $i<=$c; $i++) { |
||
596 | $res = dirname($res); |
||
597 | } |
||
598 | |||
269 | daniel-mar | 599 | $res = str_replace('\\', '/', $res); |
227 | daniel-mar | 600 | if ($res == '/') $res = ''; |
601 | $res .= '/'; |
||
602 | |||
603 | if (!$relative) { |
||
228 | daniel-mar | 604 | $is_ssl = isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] === 'on'); |
605 | $protocol = $is_ssl ? 'https' : 'http'; |
||
606 | $host = $_SERVER['HTTP_HOST']; |
||
607 | $port = $_SERVER['SERVER_PORT']; |
||
608 | if ($is_ssl && ($port != 443)) { |
||
609 | $port_add = ":$port"; |
||
610 | } else if (!$is_ssl && ($port != 80)) { |
||
611 | $port_add = ":$port"; |
||
612 | } else { |
||
613 | $port_add = ""; |
||
614 | } |
||
615 | $res = $protocol.'://'.$host.$port_add.$res; |
||
227 | daniel-mar | 616 | } |
617 | |||
618 | return $res; |
||
619 | } |
||
620 | |||
621 | private static $system_id_cache = null; |
||
622 | public static function getSystemId($oid=false) { |
||
623 | if (!is_null(self::$system_id_cache)) { |
||
624 | $out = self::$system_id_cache; |
||
625 | } else { |
||
626 | $out = false; |
||
627 | |||
628 | if (self::getPkiStatus(true)) { |
||
629 | $pubKey = OIDplus::config()->getValue('oidplus_public_key'); |
||
630 | if (preg_match('@BEGIN PUBLIC KEY\-+(.+)\-+END PUBLIC KEY@ismU', $pubKey, $m)) { |
||
631 | $out = smallhash(base64_decode($m[1])); |
||
632 | } |
||
633 | } |
||
634 | self::$system_id_cache = $out; |
||
635 | } |
||
291 | daniel-mar | 636 | return ($oid ? '1.3.6.1.4.1.37476.30.9.' : '').$out; |
227 | daniel-mar | 637 | } |
638 | |||
639 | public static function getPkiStatus($try_generate=true) { |
||
640 | if (!function_exists('openssl_pkey_new')) return false; |
||
641 | |||
642 | $privKey = OIDplus::config()->getValue('oidplus_private_key'); |
||
643 | $pubKey = OIDplus::config()->getValue('oidplus_public_key'); |
||
644 | |||
645 | if ($try_generate && !verify_private_public_key($privKey, $pubKey)) { |
||
263 | daniel-mar | 646 | $pkey_config = array( |
227 | daniel-mar | 647 | "digest_alg" => "sha512", |
648 | "private_key_bits" => 2048, |
||
649 | "private_key_type" => OPENSSL_KEYTYPE_RSA, |
||
650 | ); |
||
651 | |||
652 | // Create the private and public key |
||
263 | daniel-mar | 653 | $res = openssl_pkey_new($pkey_config); |
256 | daniel-mar | 654 | |
239 | daniel-mar | 655 | if (!$res) return false; |
227 | daniel-mar | 656 | |
657 | // Extract the private key from $res to $privKey |
||
658 | openssl_pkey_export($res, $privKey); |
||
659 | |||
660 | // Extract the public key from $res to $pubKey |
||
661 | $pubKey = openssl_pkey_get_details($res)["key"]; |
||
662 | |||
239 | daniel-mar | 663 | // Log |
288 | daniel-mar | 664 | OIDplus::logger()->log("[INFO]A!", "Generating new SystemID using a new key pair"); |
239 | daniel-mar | 665 | |
227 | daniel-mar | 666 | // Save the key pair to database |
667 | OIDplus::config()->setValue('oidplus_private_key', $privKey); |
||
668 | OIDplus::config()->setValue('oidplus_public_key', $pubKey); |
||
669 | |||
670 | // Log the new system ID |
||
671 | if (preg_match('@BEGIN PUBLIC KEY\-+(.+)\-+END PUBLIC KEY@ismU', $pubKey, $m)) { |
||
672 | $system_id = smallhash(base64_decode($m[1])); |
||
288 | daniel-mar | 673 | OIDplus::logger()->log("[INFO]A!", "Your SystemID is now $system_id"); |
227 | daniel-mar | 674 | } |
675 | } |
||
676 | |||
677 | return verify_private_public_key($privKey, $pubKey); |
||
678 | } |
||
679 | |||
170 | daniel-mar | 680 | public static function getInstallType() { |
294 | daniel-mar | 681 | if (!file_exists(OIDplus::basePath().'/oidplus_version.txt') && !is_dir(OIDplus::basePath().'/.svn')) { |
170 | daniel-mar | 682 | return 'unknown'; |
683 | } |
||
294 | daniel-mar | 684 | if (file_exists(OIDplus::basePath().'/oidplus_version.txt') && is_dir(OIDplus::basePath().'/.svn')) { |
170 | daniel-mar | 685 | return 'ambigous'; |
686 | } |
||
294 | daniel-mar | 687 | if (is_dir(OIDplus::basePath().'/.svn')) { |
170 | daniel-mar | 688 | return 'svn-wc'; |
689 | } |
||
294 | daniel-mar | 690 | if (file_exists(OIDplus::basePath().'/oidplus_version.txt')) { |
170 | daniel-mar | 691 | return 'svn-snapshot'; |
692 | } |
||
693 | } |
||
694 | |||
111 | daniel-mar | 695 | public static function getVersion() { |
294 | daniel-mar | 696 | if (file_exists(OIDplus::basePath().'/oidplus_version.txt') && is_dir(OIDplus::basePath().'/.svn')) { |
239 | daniel-mar | 697 | return false; // version is ambigous |
111 | daniel-mar | 698 | } |
162 | daniel-mar | 699 | |
294 | daniel-mar | 700 | if (is_dir(OIDplus::basePath().'/.svn')) { |
285 | daniel-mar | 701 | // Try to get the version via SQLite3 |
702 | if (class_exists('SQLite3')) { |
||
703 | try { |
||
294 | daniel-mar | 704 | $db = new SQLite3(OIDplus::basePath().'/.svn/wc.db'); |
285 | daniel-mar | 705 | $results = $db->query('SELECT MIN(revision) AS rev FROM NODES_BASE'); |
706 | while ($row = $results->fetchArray()) { |
||
707 | return 'svn-'.$row['rev']; |
||
708 | } |
||
709 | $db->close(); |
||
710 | $db = null; |
||
711 | } catch (Exception $e) { |
||
712 | } |
||
713 | } |
||
714 | if (class_exists('PDO')) { |
||
715 | try { |
||
294 | daniel-mar | 716 | $pdo = new PDO('sqlite:' . OIDplus::basePath().'/.svn/wc.db'); |
285 | daniel-mar | 717 | $res = $pdo->query('SELECT MIN(revision) AS rev FROM NODES_BASE'); |
718 | $row = $res->fetch(); |
||
719 | if ($row !== false) return 'svn-'.$row['rev']; |
||
720 | $pdo = null; |
||
721 | } catch (Exception $e) { |
||
722 | } |
||
723 | } |
||
724 | |||
162 | daniel-mar | 725 | // Try to find out the SVN version using the shell |
285 | daniel-mar | 726 | // We don't prioritize this method, because a failed shell access will flood the apache error log with STDERR messages |
294 | daniel-mar | 727 | $output = @shell_exec('svnversion '.escapeshellarg(OIDplus::basePath())); |
285 | daniel-mar | 728 | if (preg_match('/\d+/', $output, $match)) { |
239 | daniel-mar | 729 | return 'svn-'.$match[0]; |
162 | daniel-mar | 730 | } |
731 | |||
294 | daniel-mar | 732 | $output = @shell_exec('svn info '.escapeshellarg(OIDplus::basePath())); |
285 | daniel-mar | 733 | if (preg_match('/Revision:\s*(\d+)/m', $output, $match)) { |
734 | return 'svn-'.$match[1]; |
||
162 | daniel-mar | 735 | } |
736 | } |
||
737 | |||
294 | daniel-mar | 738 | if (file_exists(OIDplus::basePath().'/oidplus_version.txt')) { |
739 | $cont = file_get_contents(OIDplus::basePath().'/oidplus_version.txt'); |
||
239 | daniel-mar | 740 | if (preg_match('@Revision (\d+)@', $cont, $m)) |
741 | return 'svn-'.$m[1]; |
||
162 | daniel-mar | 742 | } |
743 | |||
239 | daniel-mar | 744 | return false; |
111 | daniel-mar | 745 | } |
746 | |||
230 | daniel-mar | 747 | private static $sslAvailableCache = null; |
748 | public static function isSslAvailable() { |
||
749 | if (!is_null(self::$sslAvailableCache)) return self::$sslAvailableCache; |
||
256 | daniel-mar | 750 | |
230 | daniel-mar | 751 | if (php_sapi_name() == 'cli') { |
752 | self::$sslAvailableCache = false; |
||
753 | return false; |
||
754 | } |
||
755 | |||
49 | daniel-mar | 756 | $timeout = 2; |
80 | daniel-mar | 757 | $already_ssl = isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == "on"); |
758 | $ssl_port = 443; |
||
227 | daniel-mar | 759 | $cookie_path = OIDplus::getSystemUrl(true); |
83 | daniel-mar | 760 | if (empty($cookie_path)) $cookie_path = '/'; |
42 | daniel-mar | 761 | |
261 | daniel-mar | 762 | $mode = OIDplus::baseConfig()->getValue('ENFORCE_SSL', 2/*auto*/); |
763 | |||
764 | if ($mode == 0) { |
||
80 | daniel-mar | 765 | // No SSL available |
230 | daniel-mar | 766 | self::$sslAvailableCache = $already_ssl; |
80 | daniel-mar | 767 | return $already_ssl; |
768 | } |
||
769 | |||
261 | daniel-mar | 770 | if ($mode == 1) { |
80 | daniel-mar | 771 | // Force SSL |
772 | if ($already_ssl) { |
||
230 | daniel-mar | 773 | self::$sslAvailableCache = true; |
80 | daniel-mar | 774 | return true; |
42 | daniel-mar | 775 | } else { |
80 | daniel-mar | 776 | $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
777 | header('Location:'.$location); |
||
240 | daniel-mar | 778 | die('Redirecting to HTTPS...'); |
230 | daniel-mar | 779 | self::$sslAvailableCache = true; |
80 | daniel-mar | 780 | return true; |
781 | } |
||
782 | } |
||
783 | |||
261 | daniel-mar | 784 | if ($mode == 2) { |
80 | daniel-mar | 785 | // Automatic SSL detection |
786 | |||
787 | if ($already_ssl) { |
||
788 | // we are already on HTTPS |
||
83 | daniel-mar | 789 | setcookie('SSL_CHECK', '1', 0, $cookie_path, '', false, true); |
230 | daniel-mar | 790 | self::$sslAvailableCache = true; |
80 | daniel-mar | 791 | return true; |
792 | } else { |
||
793 | if (isset($_COOKIE['SSL_CHECK'])) { |
||
794 | // We already had the HTTPS detection done before. |
||
795 | if ($_COOKIE['SSL_CHECK']) { |
||
796 | // HTTPS was detected before, but we are HTTP. Redirect now |
||
797 | $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
||
798 | header('Location:'.$location); |
||
240 | daniel-mar | 799 | die('Redirecting to HTTPS...'); |
230 | daniel-mar | 800 | self::$sslAvailableCache = true; |
80 | daniel-mar | 801 | return true; |
802 | } else { |
||
803 | // No HTTPS available. Do nothing. |
||
230 | daniel-mar | 804 | self::$sslAvailableCache = false; |
80 | daniel-mar | 805 | return false; |
806 | } |
||
49 | daniel-mar | 807 | } else { |
80 | daniel-mar | 808 | // This is our first check (or the browser didn't accept the SSL_CHECK cookie) |
809 | if (@fsockopen($_SERVER['HTTP_HOST'], $ssl_port, $errno, $errstr, $timeout)) { |
||
810 | // HTTPS detected. Redirect now, and remember that we had detected HTTPS |
||
83 | daniel-mar | 811 | setcookie('SSL_CHECK', '1', 0, $cookie_path, '', false, true); |
80 | daniel-mar | 812 | $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
813 | header('Location:'.$location); |
||
240 | daniel-mar | 814 | die('Redirecting to HTTPS...'); |
230 | daniel-mar | 815 | self::$sslAvailableCache = true; |
80 | daniel-mar | 816 | return true; |
817 | } else { |
||
818 | // No HTTPS detected. Do nothing, and next time, don't try to detect HTTPS again. |
||
83 | daniel-mar | 819 | setcookie('SSL_CHECK', '0', 0, $cookie_path, '', false, true); |
230 | daniel-mar | 820 | self::$sslAvailableCache = false; |
80 | daniel-mar | 821 | return false; |
822 | } |
||
49 | daniel-mar | 823 | } |
42 | daniel-mar | 824 | } |
825 | } |
||
826 | } |
||
241 | daniel-mar | 827 | |
828 | public static function webpath($target) { |
||
829 | $dir = __DIR__; |
||
830 | $dir = dirname($dir); |
||
831 | $dir = dirname($dir); |
||
832 | $target = substr($target, strlen($dir)+1, strlen($target)-strlen($dir)-1); |
||
833 | if ($target != '') { |
||
834 | $target = str_replace('\\','/',$target).'/'; |
||
835 | } |
||
836 | return $target; |
||
837 | } |
||
2 | daniel-mar | 838 | } |