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