Rev 289 | Rev 292 | 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 | } |
||
256 | 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 |
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; |
274 | daniel-mar | 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 { |
||
280 | daniel-mar | 86 | if (self::$html) { |
261 | daniel-mar | 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 | |||
274 | daniel-mar | 225 | # --- SQL slang plugin |
226 | |||
227 | private static function registerSqlSlangPlugin(OIDplusSqlSlangPlugin $plugin) { |
||
228 | $name = $plugin::id(); |
||
229 | if ($name === false) return false; |
||
230 | |||
231 | self::$sqlSlangPlugins[$name] = $plugin; |
||
232 | |||
233 | return true; |
||
234 | } |
||
235 | |||
236 | public static function getSqlSlangPlugins() { |
||
237 | return self::$sqlSlangPlugins; |
||
238 | } |
||
239 | |||
230 | daniel-mar | 240 | # --- Database plugin |
74 | daniel-mar | 241 | |
227 | daniel-mar | 242 | private static function registerDatabasePlugin(OIDplusDatabasePlugin $plugin) { |
275 | daniel-mar | 243 | $name = $plugin::id(); |
150 | daniel-mar | 244 | if ($name === false) return false; |
245 | |||
246 | self::$dbPlugins[$name] = $plugin; |
||
247 | |||
248 | return true; |
||
249 | } |
||
250 | |||
251 | public static function getDatabasePlugins() { |
||
252 | return self::$dbPlugins; |
||
253 | } |
||
254 | |||
227 | daniel-mar | 255 | public static function db() { |
261 | daniel-mar | 256 | if (OIDplus::baseConfig()->getValue('DATABASE_PLUGIN', '') === '') { |
260 | daniel-mar | 257 | throw new OIDplusConfigInitializationException("No database plugin selected in config file"); |
258 | } |
||
261 | daniel-mar | 259 | if (!isset(self::$dbPlugins[OIDplus::baseConfig()->getValue('DATABASE_PLUGIN')])) { |
260 | throw new OIDplusConfigInitializationException("Database plugin '".OIDplus::baseConfig()->getValue('DATABASE_PLUGIN')."' not found"); |
||
227 | daniel-mar | 261 | } |
261 | daniel-mar | 262 | $obj = self::$dbPlugins[OIDplus::baseConfig()->getValue('DATABASE_PLUGIN')]; |
227 | daniel-mar | 263 | if (!$obj->isConnected()) $obj->connect(); |
264 | return $obj; |
||
265 | } |
||
266 | |||
267 | # --- Page plugin |
||
268 | |||
224 | daniel-mar | 269 | private static function registerPagePlugin(OIDplusPagePlugin $plugin) { |
281 | daniel-mar | 270 | self::$pagePlugins[] = $plugin; |
61 | daniel-mar | 271 | |
272 | return true; |
||
273 | } |
||
274 | |||
281 | daniel-mar | 275 | public static function getPagePlugins() { |
276 | return self::$pagePlugins; |
||
61 | daniel-mar | 277 | } |
278 | |||
227 | daniel-mar | 279 | # --- Auth plugin |
280 | |||
281 | private static function registerAuthPlugin(OIDplusAuthPlugin $plugin) { |
||
282 | self::$authPlugins[] = $plugin; |
||
283 | return true; |
||
284 | } |
||
285 | |||
221 | daniel-mar | 286 | public static function getAuthPlugins() { |
287 | return self::$authPlugins; |
||
288 | } |
||
289 | |||
289 | daniel-mar | 290 | # --- Logger plugin |
291 | |||
292 | private static function registerLoggerPlugin(OIDplusLoggerPlugin $plugin) { |
||
293 | self::$loggerPlugins[] = $plugin; |
||
294 | return true; |
||
295 | } |
||
296 | |||
297 | public static function getLoggerPlugins() { |
||
298 | return self::$loggerPlugins; |
||
299 | } |
||
300 | |||
227 | daniel-mar | 301 | # --- Object type plugin |
302 | |||
303 | private static function registerObjectTypePlugin(OIDplusObjectTypePlugin $plugin) { |
||
304 | self::$objectTypePlugins[] = $plugin; |
||
305 | |||
306 | $ot = $plugin::getObjectTypeClassName(); |
||
307 | self::registerObjectType($ot); |
||
308 | |||
309 | return true; |
||
310 | } |
||
311 | |||
224 | daniel-mar | 312 | private static function registerObjectType($ot) { |
66 | daniel-mar | 313 | $ns = $ot::ns(); |
314 | |||
250 | daniel-mar | 315 | if (empty($ns)) throw new OIDplusException("Attention: Empty NS at $ot\n"); |
66 | daniel-mar | 316 | |
317 | $ns_found = false; |
||
227 | daniel-mar | 318 | foreach (array_merge(OIDplus::getEnabledObjectTypes(), OIDplus::getDisabledObjectTypes()) as $test_ot) { |
66 | daniel-mar | 319 | if ($test_ot::ns() == $ns) { |
320 | $ns_found = true; |
||
321 | break; |
||
322 | } |
||
323 | } |
||
324 | if ($ns_found) { |
||
250 | daniel-mar | 325 | throw new OIDplusException("Attention: Two objectType plugins use the same namespace \"$ns\"!"); |
66 | daniel-mar | 326 | } |
327 | |||
328 | $init = OIDplus::config()->getValue("objecttypes_initialized"); |
||
329 | $init_ary = empty($init) ? array() : explode(';', $init); |
||
70 | daniel-mar | 330 | $init_ary = array_map('trim', $init_ary); |
66 | daniel-mar | 331 | |
332 | $enabled = OIDplus::config()->getValue("objecttypes_enabled"); |
||
333 | $enabled_ary = empty($enabled) ? array() : explode(';', $enabled); |
||
70 | daniel-mar | 334 | $enabled_ary = array_map('trim', $enabled_ary); |
66 | daniel-mar | 335 | |
79 | daniel-mar | 336 | $do_enable = false; |
337 | if (in_array($ns, $enabled_ary)) { |
||
338 | $do_enable = true; |
||
339 | } else { |
||
340 | if (!OIDplus::config()->getValue('registration_done')) { |
||
341 | $do_enable = $ns == 'oid'; |
||
342 | } else { |
||
343 | $do_enable = !in_array($ns, $init_ary); |
||
344 | } |
||
345 | } |
||
346 | |||
347 | if ($do_enable) { |
||
227 | daniel-mar | 348 | self::$enabledObjectTypes[] = $ot; |
349 | usort(self::$enabledObjectTypes, function($a, $b) { |
||
66 | daniel-mar | 350 | $enabled = OIDplus::config()->getValue("objecttypes_enabled"); |
351 | $enabled_ary = explode(';', $enabled); |
||
352 | |||
353 | $idx_a = array_search($a::ns(), $enabled_ary); |
||
354 | $idx_b = array_search($b::ns(), $enabled_ary); |
||
355 | |||
356 | if ($idx_a == $idx_b) { |
||
357 | return 0; |
||
358 | } |
||
359 | return ($idx_a > $idx_b) ? +1 : -1; |
||
360 | }); |
||
74 | daniel-mar | 361 | } else { |
362 | self::$disabledObjectTypes[] = $ot; |
||
66 | daniel-mar | 363 | } |
364 | |||
365 | if (!in_array($ns, $init_ary)) { |
||
366 | // Was never initialized before, so we add it to the list of enabled object types once |
||
367 | |||
79 | daniel-mar | 368 | if ($do_enable) { |
369 | $enabled_ary[] = $ns; |
||
370 | OIDplus::config()->setValue("objecttypes_enabled", implode(';', $enabled_ary)); |
||
371 | } |
||
66 | daniel-mar | 372 | |
373 | $init_ary[] = $ns; |
||
374 | OIDplus::config()->setValue("objecttypes_initialized", implode(';', $init_ary)); |
||
375 | } |
||
61 | daniel-mar | 376 | } |
377 | |||
227 | daniel-mar | 378 | public static function getObjectTypePlugins() { |
379 | return self::$objectTypePlugins; |
||
61 | daniel-mar | 380 | } |
381 | |||
227 | daniel-mar | 382 | public static function getObjectTypePluginsEnabled() { |
383 | $res = array(); |
||
384 | foreach (self::$objectTypePlugins as $plugin) { |
||
385 | $ot = $plugin::getObjectTypeClassName(); |
||
386 | if (in_array($ot, self::$enabledObjectTypes)) $res[] = $plugin; |
||
387 | } |
||
388 | return $res; |
||
74 | daniel-mar | 389 | } |
390 | |||
227 | daniel-mar | 391 | public static function getObjectTypePluginsDisabled() { |
392 | $res = array(); |
||
393 | foreach (self::$objectTypePlugins as $plugin) { |
||
394 | $ot = $plugin::getObjectTypeClassName(); |
||
395 | if (in_array($ot, self::$disabledObjectTypes)) $res[] = $plugin; |
||
74 | daniel-mar | 396 | } |
227 | daniel-mar | 397 | return $res; |
74 | daniel-mar | 398 | } |
399 | |||
227 | daniel-mar | 400 | public static function getEnabledObjectTypes() { |
401 | return self::$enabledObjectTypes; |
||
402 | } |
||
74 | daniel-mar | 403 | |
227 | daniel-mar | 404 | public static function getDisabledObjectTypes() { |
405 | return self::$disabledObjectTypes; |
||
406 | } |
||
74 | daniel-mar | 407 | |
277 | daniel-mar | 408 | # --- Plugin handling functions |
409 | |||
410 | public static function getPluginInfo($class_name): array { |
||
411 | $reflector = new ReflectionClass($class_name); |
||
412 | $ini = dirname($reflector->getFileName()).'/manifest.ini'; |
||
413 | if (!file_exists($ini)) return array(); |
||
414 | $bry = parse_ini_file($ini, true, INI_SCANNER_TYPED); |
||
415 | if (!isset($bry['Plugin'])) return array(); |
||
416 | return $bry['Plugin']; |
||
417 | } |
||
418 | |||
419 | public static function getAllPluginManifests($pluginFolderMask='*'): array { |
||
420 | $out = array(); |
||
279 | daniel-mar | 421 | // Note: glob() will sort by default, so we do not need a page priority attribute. |
422 | // So you just need to use a numeric plugin directory prefix (padded). |
||
277 | daniel-mar | 423 | $ary = glob(__DIR__ . '/../../plugins/'.$pluginFolderMask.'/'.'*'.'/manifest.ini'); |
424 | foreach ($ary as $ini) { |
||
425 | if (!file_exists($ini)) continue; |
||
426 | $bry = parse_ini_file($ini, true, INI_SCANNER_TYPED); |
||
427 | |||
428 | $plugintype_folder = basename(dirname(dirname($ini))); |
||
429 | $pluginname_folder = basename(dirname($ini)); |
||
430 | |||
431 | if (!isset($out[$plugintype_folder])) $out[$plugintype_folder] = array(); |
||
432 | if (!isset($out[$plugintype_folder][$pluginname_folder])) $out[$plugintype_folder][$pluginname_folder] = array(); |
||
433 | $out[$plugintype_folder][$pluginname_folder] = $bry; |
||
434 | } |
||
435 | return $out; |
||
436 | } |
||
437 | |||
438 | public static function registerAllPlugins($pluginDirName, $expectedPluginClass, $registerCallback): array { |
||
439 | $out = array(); |
||
279 | daniel-mar | 440 | $ary = self::getAllPluginManifests($pluginDirName); |
277 | daniel-mar | 441 | foreach ($ary as $plugintype_folder => $bry) { |
442 | foreach ($bry as $pluginname_folder => $cry) { |
||
279 | daniel-mar | 443 | if (!isset($cry['PHP']) || !isset($cry['PHP']['pluginclass'])) { |
444 | throw new OIDplusException("Plugin '$plugintype_folder/$pluginname_folder' is errornous: Plugin class is not defined (manifest.ini section 'PHP', key 'pluginclass'"); |
||
277 | daniel-mar | 445 | } |
279 | daniel-mar | 446 | $class_name = $cry['PHP']['pluginclass']; |
447 | if (!is_subclass_of($class_name, $expectedPluginClass)) { |
||
448 | throw new OIDplusException("Plugin '$plugintype_folder/$pluginname_folder' is errornous: Plugin class '$class_name' is expected to be a subclass of '$expectedPluginClass'"); |
||
449 | } |
||
450 | $out[] = $class_name; |
||
451 | if (!is_null($registerCallback)) { |
||
452 | call_user_func($registerCallback, new $class_name()); |
||
453 | } |
||
277 | daniel-mar | 454 | } |
455 | |||
456 | } |
||
457 | return $out; |
||
458 | } |
||
459 | |||
227 | daniel-mar | 460 | # --- Initialization of OIDplus |
206 | daniel-mar | 461 | |
2 | daniel-mar | 462 | public static function init($html=true) { |
236 | daniel-mar | 463 | self::$html = $html; |
464 | |||
263 | daniel-mar | 465 | // Reset internal state, so we can re-init verything if required |
274 | daniel-mar | 466 | |
263 | daniel-mar | 467 | if (self::$old_config_format) { |
468 | // Note: This can only happen in very special cases (e.g. test cases) where you call init() twice |
||
469 | 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.'); |
||
470 | } |
||
274 | daniel-mar | 471 | |
263 | daniel-mar | 472 | self::$config = null; |
473 | self::$baseConfig = null; |
||
474 | self::$gui = null; |
||
475 | self::$authUtils = null; |
||
476 | self::$mailUtils = null; |
||
477 | self::$menuUtils = null; |
||
478 | self::$logger = null; |
||
479 | self::$sesHandler = null; |
||
480 | self::$pagePlugins = array(); |
||
481 | self::$authPlugins = array(); |
||
289 | daniel-mar | 482 | self::$loggerPlugins = array(); |
263 | daniel-mar | 483 | self::$objectTypePlugins = array(); |
484 | self::$enabledObjectTypes = array(); |
||
485 | self::$disabledObjectTypes = array(); |
||
486 | self::$dbPlugins = array(); |
||
274 | daniel-mar | 487 | self::$sqlSlangPlugins = array(); |
263 | daniel-mar | 488 | self::$system_id_cache = null; |
489 | self::$sslAvailableCache = null; |
||
74 | daniel-mar | 490 | |
263 | daniel-mar | 491 | // Continue... |
492 | |||
493 | OIDplus::baseConfig(); // this loads the base configuration located in config.inc.php (once!) |
||
494 | // You can do changes to the configuration afterwards using OIDplus::baseConfig()->... |
||
495 | |||
150 | daniel-mar | 496 | // Register database types (highest priority) |
497 | |||
274 | daniel-mar | 498 | // SQL slangs |
499 | |||
277 | daniel-mar | 500 | self::registerAllPlugins('sql_slang', 'OIDplusSqlSlangPlugin', array('OIDplus','registerSqlSlangPlugin')); |
274 | daniel-mar | 501 | foreach (OIDplus::getSqlSlangPlugins() as $plugin) { |
502 | $plugin->init($html); |
||
503 | } |
||
504 | |||
505 | // Database providers |
||
506 | |||
277 | daniel-mar | 507 | self::registerAllPlugins('database', 'OIDplusDatabasePlugin', array('OIDplus','registerDatabasePlugin')); |
237 | daniel-mar | 508 | foreach (OIDplus::getDatabasePlugins() as $plugin) { |
509 | $plugin->init($html); |
||
510 | } |
||
511 | |||
42 | daniel-mar | 512 | // Do redirect stuff etc. |
74 | daniel-mar | 513 | |
230 | daniel-mar | 514 | self::isSslAvailable(); // This function does automatic redirects |
61 | daniel-mar | 515 | |
263 | daniel-mar | 516 | // Construct the configuration manager |
66 | daniel-mar | 517 | |
263 | daniel-mar | 518 | OIDplus::config(); // During the construction, various system settings are prepared if required |
66 | daniel-mar | 519 | |
74 | daniel-mar | 520 | // Initialize public / private keys |
521 | |||
227 | daniel-mar | 522 | OIDplus::getPkiStatus(true); |
74 | daniel-mar | 523 | |
237 | daniel-mar | 524 | // Register non-DB plugins |
74 | daniel-mar | 525 | |
277 | daniel-mar | 526 | self::registerAllPlugins('*Pages', 'OIDplusPagePlugin', array('OIDplus','registerPagePlugin')); |
527 | self::registerAllPlugins('auth', 'OIDplusAuthPlugin', array('OIDplus','registerAuthPlugin')); |
||
289 | daniel-mar | 528 | self::registerAllPlugins('logger', 'OIDplusLoggerPlugin', array('OIDplus','registerLoggerPlugin')); |
277 | daniel-mar | 529 | self::registerAllPlugins('objectTypes', 'OIDplusObjectTypePlugin', array('OIDplus','registerObjectTypePlugin')); |
150 | daniel-mar | 530 | |
237 | daniel-mar | 531 | // Initialize non-DB plugins |
224 | daniel-mar | 532 | |
281 | daniel-mar | 533 | foreach (OIDplus::getPagePlugins() as $plugin) { |
74 | daniel-mar | 534 | $plugin->init($html); |
535 | } |
||
230 | daniel-mar | 536 | foreach (OIDplus::getAuthPlugins() as $plugin) { |
537 | $plugin->init($html); |
||
538 | } |
||
289 | daniel-mar | 539 | foreach (OIDplus::getLoggerPlugins() as $plugin) { |
540 | $plugin->init($html); |
||
541 | } |
||
230 | daniel-mar | 542 | foreach (OIDplus::getObjectTypePlugins() as $plugin) { |
543 | $plugin->init($html); |
||
544 | } |
||
2 | daniel-mar | 545 | } |
42 | daniel-mar | 546 | |
227 | daniel-mar | 547 | # --- System URL, System ID, PKI, and other functions |
548 | |||
549 | public static function getSystemUrl($relative=false) { |
||
550 | if (!isset($_SERVER["SCRIPT_NAME"])) return false; |
||
551 | |||
552 | $test_dir = dirname($_SERVER['SCRIPT_FILENAME']); |
||
269 | daniel-mar | 553 | $test_dir = str_replace('\\', '/', $test_dir); |
227 | daniel-mar | 554 | $c = 0; |
555 | while (!file_exists($test_dir.'/oidplus_base.js')) { |
||
556 | $test_dir = dirname($test_dir); |
||
557 | $c++; |
||
558 | if ($c == 1000) return false; |
||
559 | } |
||
560 | |||
561 | $res = dirname($_SERVER['SCRIPT_NAME'].'xxx'); |
||
562 | |||
563 | for ($i=1; $i<=$c; $i++) { |
||
564 | $res = dirname($res); |
||
565 | } |
||
566 | |||
269 | daniel-mar | 567 | $res = str_replace('\\', '/', $res); |
227 | daniel-mar | 568 | if ($res == '/') $res = ''; |
569 | $res .= '/'; |
||
570 | |||
571 | if (!$relative) { |
||
228 | daniel-mar | 572 | $is_ssl = isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] === 'on'); |
573 | $protocol = $is_ssl ? 'https' : 'http'; |
||
574 | $host = $_SERVER['HTTP_HOST']; |
||
575 | $port = $_SERVER['SERVER_PORT']; |
||
576 | if ($is_ssl && ($port != 443)) { |
||
577 | $port_add = ":$port"; |
||
578 | } else if (!$is_ssl && ($port != 80)) { |
||
579 | $port_add = ":$port"; |
||
580 | } else { |
||
581 | $port_add = ""; |
||
582 | } |
||
583 | $res = $protocol.'://'.$host.$port_add.$res; |
||
227 | daniel-mar | 584 | } |
585 | |||
586 | return $res; |
||
587 | } |
||
588 | |||
589 | private static $system_id_cache = null; |
||
590 | public static function getSystemId($oid=false) { |
||
591 | if (!is_null(self::$system_id_cache)) { |
||
592 | $out = self::$system_id_cache; |
||
593 | } else { |
||
594 | $out = false; |
||
595 | |||
596 | if (self::getPkiStatus(true)) { |
||
597 | $pubKey = OIDplus::config()->getValue('oidplus_public_key'); |
||
598 | if (preg_match('@BEGIN PUBLIC KEY\-+(.+)\-+END PUBLIC KEY@ismU', $pubKey, $m)) { |
||
599 | $out = smallhash(base64_decode($m[1])); |
||
600 | } |
||
601 | } |
||
602 | self::$system_id_cache = $out; |
||
603 | } |
||
291 | daniel-mar | 604 | return ($oid ? '1.3.6.1.4.1.37476.30.9.' : '').$out; |
227 | daniel-mar | 605 | } |
606 | |||
607 | public static function getPkiStatus($try_generate=true) { |
||
608 | if (!function_exists('openssl_pkey_new')) return false; |
||
609 | |||
610 | $privKey = OIDplus::config()->getValue('oidplus_private_key'); |
||
611 | $pubKey = OIDplus::config()->getValue('oidplus_public_key'); |
||
612 | |||
613 | if ($try_generate && !verify_private_public_key($privKey, $pubKey)) { |
||
263 | daniel-mar | 614 | $pkey_config = array( |
227 | daniel-mar | 615 | "digest_alg" => "sha512", |
616 | "private_key_bits" => 2048, |
||
617 | "private_key_type" => OPENSSL_KEYTYPE_RSA, |
||
618 | ); |
||
619 | |||
620 | // Create the private and public key |
||
263 | daniel-mar | 621 | $res = openssl_pkey_new($pkey_config); |
256 | daniel-mar | 622 | |
239 | daniel-mar | 623 | if (!$res) return false; |
227 | daniel-mar | 624 | |
625 | // Extract the private key from $res to $privKey |
||
626 | openssl_pkey_export($res, $privKey); |
||
627 | |||
628 | // Extract the public key from $res to $pubKey |
||
629 | $pubKey = openssl_pkey_get_details($res)["key"]; |
||
630 | |||
239 | daniel-mar | 631 | // Log |
288 | daniel-mar | 632 | OIDplus::logger()->log("[INFO]A!", "Generating new SystemID using a new key pair"); |
239 | daniel-mar | 633 | |
227 | daniel-mar | 634 | // Save the key pair to database |
635 | OIDplus::config()->setValue('oidplus_private_key', $privKey); |
||
636 | OIDplus::config()->setValue('oidplus_public_key', $pubKey); |
||
637 | |||
638 | // Log the new system ID |
||
639 | if (preg_match('@BEGIN PUBLIC KEY\-+(.+)\-+END PUBLIC KEY@ismU', $pubKey, $m)) { |
||
640 | $system_id = smallhash(base64_decode($m[1])); |
||
288 | daniel-mar | 641 | OIDplus::logger()->log("[INFO]A!", "Your SystemID is now $system_id"); |
227 | daniel-mar | 642 | } |
643 | } |
||
644 | |||
645 | return verify_private_public_key($privKey, $pubKey); |
||
646 | } |
||
647 | |||
170 | daniel-mar | 648 | public static function getInstallType() { |
649 | if (!file_exists(__DIR__ . '/../../oidplus_version.txt') && !is_dir(__DIR__ . '/../../.svn')) { |
||
650 | return 'unknown'; |
||
651 | } |
||
652 | if (file_exists(__DIR__ . '/../../oidplus_version.txt') && is_dir(__DIR__ . '/../../.svn')) { |
||
653 | return 'ambigous'; |
||
654 | } |
||
655 | if (is_dir(__DIR__ . '/../../.svn')) { |
||
656 | return 'svn-wc'; |
||
657 | } |
||
658 | if (file_exists(__DIR__ . '/../../oidplus_version.txt')) { |
||
659 | return 'svn-snapshot'; |
||
660 | } |
||
661 | } |
||
662 | |||
111 | daniel-mar | 663 | public static function getVersion() { |
162 | daniel-mar | 664 | if (file_exists(__DIR__ . '/../../oidplus_version.txt') && is_dir(__DIR__ . '/../../.svn')) { |
239 | daniel-mar | 665 | return false; // version is ambigous |
111 | daniel-mar | 666 | } |
162 | daniel-mar | 667 | |
668 | if (is_dir(__DIR__ . '/../../.svn')) { |
||
285 | daniel-mar | 669 | // Try to get the version via SQLite3 |
670 | if (class_exists('SQLite3')) { |
||
671 | try { |
||
672 | $db = new SQLite3(__DIR__ . '/../../.svn/wc.db'); |
||
673 | $results = $db->query('SELECT MIN(revision) AS rev FROM NODES_BASE'); |
||
674 | while ($row = $results->fetchArray()) { |
||
675 | return 'svn-'.$row['rev']; |
||
676 | } |
||
677 | $db->close(); |
||
678 | $db = null; |
||
679 | } catch (Exception $e) { |
||
680 | } |
||
681 | } |
||
682 | if (class_exists('PDO')) { |
||
683 | try { |
||
684 | $pdo = new PDO('sqlite:' . __DIR__ . '/../../.svn/wc.db'); |
||
685 | $res = $pdo->query('SELECT MIN(revision) AS rev FROM NODES_BASE'); |
||
686 | $row = $res->fetch(); |
||
687 | if ($row !== false) return 'svn-'.$row['rev']; |
||
688 | $pdo = null; |
||
689 | } catch (Exception $e) { |
||
690 | } |
||
691 | } |
||
692 | |||
162 | daniel-mar | 693 | // Try to find out the SVN version using the shell |
285 | daniel-mar | 694 | // We don't prioritize this method, because a failed shell access will flood the apache error log with STDERR messages |
695 | $output = @shell_exec('svnversion '.escapeshellarg(realpath(__DIR__ . '/../../'))); |
||
696 | if (preg_match('/\d+/', $output, $match)) { |
||
239 | daniel-mar | 697 | return 'svn-'.$match[0]; |
162 | daniel-mar | 698 | } |
699 | |||
285 | daniel-mar | 700 | $output = @shell_exec('svn info '.escapeshellarg(realpath(__DIR__ . '/../../'))); |
701 | if (preg_match('/Revision:\s*(\d+)/m', $output, $match)) { |
||
702 | return 'svn-'.$match[1]; |
||
162 | daniel-mar | 703 | } |
704 | } |
||
705 | |||
706 | if (file_exists(__DIR__ . '/../../oidplus_version.txt')) { |
||
707 | $cont = file_get_contents(__DIR__ . '/../../oidplus_version.txt'); |
||
239 | daniel-mar | 708 | if (preg_match('@Revision (\d+)@', $cont, $m)) |
709 | return 'svn-'.$m[1]; |
||
162 | daniel-mar | 710 | } |
711 | |||
239 | daniel-mar | 712 | return false; |
111 | daniel-mar | 713 | } |
714 | |||
230 | daniel-mar | 715 | private static $sslAvailableCache = null; |
716 | public static function isSslAvailable() { |
||
717 | if (!is_null(self::$sslAvailableCache)) return self::$sslAvailableCache; |
||
256 | daniel-mar | 718 | |
230 | daniel-mar | 719 | if (php_sapi_name() == 'cli') { |
720 | self::$sslAvailableCache = false; |
||
721 | return false; |
||
722 | } |
||
723 | |||
49 | daniel-mar | 724 | $timeout = 2; |
80 | daniel-mar | 725 | $already_ssl = isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == "on"); |
726 | $ssl_port = 443; |
||
227 | daniel-mar | 727 | $cookie_path = OIDplus::getSystemUrl(true); |
83 | daniel-mar | 728 | if (empty($cookie_path)) $cookie_path = '/'; |
42 | daniel-mar | 729 | |
261 | daniel-mar | 730 | $mode = OIDplus::baseConfig()->getValue('ENFORCE_SSL', 2/*auto*/); |
731 | |||
732 | if ($mode == 0) { |
||
80 | daniel-mar | 733 | // No SSL available |
230 | daniel-mar | 734 | self::$sslAvailableCache = $already_ssl; |
80 | daniel-mar | 735 | return $already_ssl; |
736 | } |
||
737 | |||
261 | daniel-mar | 738 | if ($mode == 1) { |
80 | daniel-mar | 739 | // Force SSL |
740 | if ($already_ssl) { |
||
230 | daniel-mar | 741 | self::$sslAvailableCache = true; |
80 | daniel-mar | 742 | return true; |
42 | daniel-mar | 743 | } else { |
80 | daniel-mar | 744 | $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
745 | header('Location:'.$location); |
||
240 | daniel-mar | 746 | die('Redirecting to HTTPS...'); |
230 | daniel-mar | 747 | self::$sslAvailableCache = true; |
80 | daniel-mar | 748 | return true; |
749 | } |
||
750 | } |
||
751 | |||
261 | daniel-mar | 752 | if ($mode == 2) { |
80 | daniel-mar | 753 | // Automatic SSL detection |
754 | |||
755 | if ($already_ssl) { |
||
756 | // we are already on HTTPS |
||
83 | daniel-mar | 757 | setcookie('SSL_CHECK', '1', 0, $cookie_path, '', false, true); |
230 | daniel-mar | 758 | self::$sslAvailableCache = true; |
80 | daniel-mar | 759 | return true; |
760 | } else { |
||
761 | if (isset($_COOKIE['SSL_CHECK'])) { |
||
762 | // We already had the HTTPS detection done before. |
||
763 | if ($_COOKIE['SSL_CHECK']) { |
||
764 | // HTTPS was detected before, but we are HTTP. Redirect now |
||
765 | $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
||
766 | header('Location:'.$location); |
||
240 | daniel-mar | 767 | die('Redirecting to HTTPS...'); |
230 | daniel-mar | 768 | self::$sslAvailableCache = true; |
80 | daniel-mar | 769 | return true; |
770 | } else { |
||
771 | // No HTTPS available. Do nothing. |
||
230 | daniel-mar | 772 | self::$sslAvailableCache = false; |
80 | daniel-mar | 773 | return false; |
774 | } |
||
49 | daniel-mar | 775 | } else { |
80 | daniel-mar | 776 | // This is our first check (or the browser didn't accept the SSL_CHECK cookie) |
777 | if (@fsockopen($_SERVER['HTTP_HOST'], $ssl_port, $errno, $errstr, $timeout)) { |
||
778 | // HTTPS detected. Redirect now, and remember that we had detected HTTPS |
||
83 | daniel-mar | 779 | setcookie('SSL_CHECK', '1', 0, $cookie_path, '', false, true); |
80 | daniel-mar | 780 | $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
781 | header('Location:'.$location); |
||
240 | daniel-mar | 782 | die('Redirecting to HTTPS...'); |
230 | daniel-mar | 783 | self::$sslAvailableCache = true; |
80 | daniel-mar | 784 | return true; |
785 | } else { |
||
786 | // No HTTPS detected. Do nothing, and next time, don't try to detect HTTPS again. |
||
83 | daniel-mar | 787 | setcookie('SSL_CHECK', '0', 0, $cookie_path, '', false, true); |
230 | daniel-mar | 788 | self::$sslAvailableCache = false; |
80 | daniel-mar | 789 | return false; |
790 | } |
||
49 | daniel-mar | 791 | } |
42 | daniel-mar | 792 | } |
793 | } |
||
794 | } |
||
241 | daniel-mar | 795 | |
796 | public static function webpath($target) { |
||
797 | $dir = __DIR__; |
||
798 | $dir = dirname($dir); |
||
799 | $dir = dirname($dir); |
||
800 | $target = substr($target, strlen($dir)+1, strlen($target)-strlen($dir)-1); |
||
801 | if ($target != '') { |
||
802 | $target = str_replace('\\','/',$target).'/'; |
||
803 | } |
||
804 | return $target; |
||
805 | } |
||
2 | daniel-mar | 806 | } |