Rev 250 | Rev 257 | 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 | |
227 | daniel-mar | 35 | # --- Singleton classes |
2 | daniel-mar | 36 | |
37 | public static function config() { |
||
86 | daniel-mar | 38 | static $config = null; |
39 | if (is_null($config)) { |
||
40 | $config = new OIDplusConfig(); |
||
2 | daniel-mar | 41 | } |
86 | daniel-mar | 42 | return $config; |
2 | daniel-mar | 43 | } |
44 | |||
45 | public static function gui() { |
||
86 | daniel-mar | 46 | static $gui = null; |
47 | if (is_null($gui)) { |
||
48 | $gui = new OIDplusGui(); |
||
49 | } |
||
50 | return $gui; |
||
2 | daniel-mar | 51 | } |
52 | |||
53 | public static function authUtils() { |
||
86 | daniel-mar | 54 | static $authUtils = null; |
55 | if (is_null($authUtils)) { |
||
56 | $authUtils = new OIDplusAuthUtils(); |
||
57 | } |
||
58 | return $authUtils; |
||
2 | daniel-mar | 59 | } |
60 | |||
250 | daniel-mar | 61 | public static function mailUtils() { |
62 | static $mailUtils = null; |
||
63 | if (is_null($mailUtils)) { |
||
64 | $mailUtils = new OIDplusMailUtils(); |
||
65 | } |
||
66 | return $mailUtils; |
||
67 | } |
||
68 | |||
69 | public static function menuUtils() { |
||
70 | static $menuUtils = null; |
||
71 | if (is_null($menuUtils)) { |
||
72 | $menuUtils = new OIDplusMenuUtils(); |
||
73 | } |
||
74 | return $menuUtils; |
||
75 | } |
||
76 | |||
115 | daniel-mar | 77 | public static function logger() { |
78 | static $logger = null; |
||
79 | if (is_null($logger)) { |
||
80 | $logger = new OIDplusLogger(); |
||
81 | } |
||
82 | return $logger; |
||
83 | } |
||
84 | |||
86 | daniel-mar | 85 | public static function sesHandler() { |
86 | static $sesHandler = null; |
||
87 | if (is_null($sesHandler)) { |
||
88 | $sesHandler = new OIDplusSessionHandler(OIDPLUS_SESSION_SECRET); |
||
89 | } |
||
90 | return $sesHandler; |
||
91 | } |
||
92 | |||
230 | daniel-mar | 93 | # --- Database plugin |
74 | daniel-mar | 94 | |
227 | daniel-mar | 95 | private static function registerDatabasePlugin(OIDplusDatabasePlugin $plugin) { |
150 | daniel-mar | 96 | $name = $plugin->name(); |
97 | if ($name === false) return false; |
||
98 | |||
99 | self::$dbPlugins[$name] = $plugin; |
||
100 | |||
101 | return true; |
||
102 | } |
||
103 | |||
104 | public static function getDatabasePlugins() { |
||
105 | return self::$dbPlugins; |
||
106 | } |
||
107 | |||
227 | daniel-mar | 108 | public static function db() { |
109 | if (!isset(self::$dbPlugins[OIDPLUS_DATABASE_PLUGIN])) { |
||
240 | daniel-mar | 110 | throw new OIDplusConfigInitializationException("Database plugin '".OIDPLUS_DATABASE_PLUGIN."' not found"); |
227 | daniel-mar | 111 | } |
112 | $obj = self::$dbPlugins[OIDPLUS_DATABASE_PLUGIN]; |
||
113 | if (!$obj->isConnected()) $obj->connect(); |
||
114 | return $obj; |
||
115 | } |
||
116 | |||
117 | # --- Page plugin |
||
118 | |||
224 | daniel-mar | 119 | private static function registerPagePlugin(OIDplusPagePlugin $plugin) { |
61 | daniel-mar | 120 | $type = $plugin->type(); |
121 | if ($type === false) return false; |
||
122 | |||
123 | $prio = $plugin->priority(); |
||
256 | daniel-mar | 124 | if (!is_numeric($prio)) throw new OIDplusException('Errornous plugin "'.get_class($plugin).'": Invalid priority'); |
125 | if ($prio < 0) throw new OIDplusException('Errornous plugin "'.get_class($plugin).'": Invalid priority'); |
||
126 | if ($prio > 999) throw new OIDplusException('Errornous plugin "'.get_class($plugin).'": Invalid priority'); |
||
61 | daniel-mar | 127 | |
128 | if (!isset(self::$pagePlugins[$type])) self::$pagePlugins[$type] = array(); |
||
256 | daniel-mar | 129 | self::$pagePlugins[$type][str_pad($prio, 3, '0', STR_PAD_LEFT).get_class($plugin)] = $plugin; |
61 | daniel-mar | 130 | |
131 | return true; |
||
132 | } |
||
133 | |||
230 | daniel-mar | 134 | public static function getPagePlugins($type='*') { |
135 | if ($type === '*') { |
||
61 | daniel-mar | 136 | $res = array(); |
137 | foreach (self::$pagePlugins as $data) { |
||
138 | $res = array_merge($res, $data); |
||
139 | } |
||
140 | } else { |
||
230 | daniel-mar | 141 | $types = explode(',', $type); |
142 | foreach ($types as $type) { |
||
143 | $res = isset(self::$pagePlugins[$type]) ? self::$pagePlugins[$type] : array(); |
||
144 | } |
||
61 | daniel-mar | 145 | } |
146 | ksort($res); |
||
147 | return $res; |
||
148 | } |
||
149 | |||
227 | daniel-mar | 150 | # --- Auth plugin |
151 | |||
152 | private static function registerAuthPlugin(OIDplusAuthPlugin $plugin) { |
||
153 | self::$authPlugins[] = $plugin; |
||
154 | return true; |
||
155 | } |
||
156 | |||
221 | daniel-mar | 157 | public static function getAuthPlugins() { |
158 | return self::$authPlugins; |
||
159 | } |
||
160 | |||
227 | daniel-mar | 161 | # --- Object type plugin |
162 | |||
163 | private static function registerObjectTypePlugin(OIDplusObjectTypePlugin $plugin) { |
||
164 | self::$objectTypePlugins[] = $plugin; |
||
165 | |||
166 | $ot = $plugin::getObjectTypeClassName(); |
||
167 | self::registerObjectType($ot); |
||
168 | |||
169 | return true; |
||
170 | } |
||
171 | |||
224 | daniel-mar | 172 | private static function registerObjectType($ot) { |
66 | daniel-mar | 173 | $ns = $ot::ns(); |
174 | |||
250 | daniel-mar | 175 | if (empty($ns)) throw new OIDplusException("Attention: Empty NS at $ot\n"); |
66 | daniel-mar | 176 | |
177 | $ns_found = false; |
||
227 | daniel-mar | 178 | foreach (array_merge(OIDplus::getEnabledObjectTypes(), OIDplus::getDisabledObjectTypes()) as $test_ot) { |
66 | daniel-mar | 179 | if ($test_ot::ns() == $ns) { |
180 | $ns_found = true; |
||
181 | break; |
||
182 | } |
||
183 | } |
||
184 | if ($ns_found) { |
||
250 | daniel-mar | 185 | throw new OIDplusException("Attention: Two objectType plugins use the same namespace \"$ns\"!"); |
66 | daniel-mar | 186 | } |
187 | |||
188 | $init = OIDplus::config()->getValue("objecttypes_initialized"); |
||
189 | $init_ary = empty($init) ? array() : explode(';', $init); |
||
70 | daniel-mar | 190 | $init_ary = array_map('trim', $init_ary); |
66 | daniel-mar | 191 | |
192 | $enabled = OIDplus::config()->getValue("objecttypes_enabled"); |
||
193 | $enabled_ary = empty($enabled) ? array() : explode(';', $enabled); |
||
70 | daniel-mar | 194 | $enabled_ary = array_map('trim', $enabled_ary); |
66 | daniel-mar | 195 | |
79 | daniel-mar | 196 | $do_enable = false; |
197 | if (in_array($ns, $enabled_ary)) { |
||
198 | $do_enable = true; |
||
199 | } else { |
||
200 | if (!OIDplus::config()->getValue('registration_done')) { |
||
201 | $do_enable = $ns == 'oid'; |
||
202 | } else { |
||
203 | $do_enable = !in_array($ns, $init_ary); |
||
204 | } |
||
205 | } |
||
206 | |||
207 | if ($do_enable) { |
||
227 | daniel-mar | 208 | self::$enabledObjectTypes[] = $ot; |
209 | usort(self::$enabledObjectTypes, function($a, $b) { |
||
66 | daniel-mar | 210 | $enabled = OIDplus::config()->getValue("objecttypes_enabled"); |
211 | $enabled_ary = explode(';', $enabled); |
||
212 | |||
213 | $idx_a = array_search($a::ns(), $enabled_ary); |
||
214 | $idx_b = array_search($b::ns(), $enabled_ary); |
||
215 | |||
216 | if ($idx_a == $idx_b) { |
||
217 | return 0; |
||
218 | } |
||
219 | return ($idx_a > $idx_b) ? +1 : -1; |
||
220 | }); |
||
74 | daniel-mar | 221 | } else { |
222 | self::$disabledObjectTypes[] = $ot; |
||
66 | daniel-mar | 223 | } |
224 | |||
225 | if (!in_array($ns, $init_ary)) { |
||
226 | // Was never initialized before, so we add it to the list of enabled object types once |
||
227 | |||
79 | daniel-mar | 228 | if ($do_enable) { |
229 | $enabled_ary[] = $ns; |
||
230 | OIDplus::config()->setValue("objecttypes_enabled", implode(';', $enabled_ary)); |
||
231 | } |
||
66 | daniel-mar | 232 | |
233 | $init_ary[] = $ns; |
||
234 | OIDplus::config()->setValue("objecttypes_initialized", implode(';', $init_ary)); |
||
235 | } |
||
61 | daniel-mar | 236 | } |
237 | |||
227 | daniel-mar | 238 | public static function getObjectTypePlugins() { |
239 | return self::$objectTypePlugins; |
||
61 | daniel-mar | 240 | } |
241 | |||
227 | daniel-mar | 242 | public static function getObjectTypePluginsEnabled() { |
243 | $res = array(); |
||
244 | foreach (self::$objectTypePlugins as $plugin) { |
||
245 | $ot = $plugin::getObjectTypeClassName(); |
||
246 | if (in_array($ot, self::$enabledObjectTypes)) $res[] = $plugin; |
||
247 | } |
||
248 | return $res; |
||
74 | daniel-mar | 249 | } |
250 | |||
227 | daniel-mar | 251 | public static function getObjectTypePluginsDisabled() { |
252 | $res = array(); |
||
253 | foreach (self::$objectTypePlugins as $plugin) { |
||
254 | $ot = $plugin::getObjectTypeClassName(); |
||
255 | if (in_array($ot, self::$disabledObjectTypes)) $res[] = $plugin; |
||
74 | daniel-mar | 256 | } |
227 | daniel-mar | 257 | return $res; |
74 | daniel-mar | 258 | } |
259 | |||
227 | daniel-mar | 260 | public static function getEnabledObjectTypes() { |
261 | return self::$enabledObjectTypes; |
||
262 | } |
||
74 | daniel-mar | 263 | |
227 | daniel-mar | 264 | public static function getDisabledObjectTypes() { |
265 | return self::$disabledObjectTypes; |
||
266 | } |
||
74 | daniel-mar | 267 | |
227 | daniel-mar | 268 | # --- Initialization of OIDplus |
206 | daniel-mar | 269 | |
2 | daniel-mar | 270 | public static function init($html=true) { |
236 | daniel-mar | 271 | self::$html = $html; |
272 | |||
2 | daniel-mar | 273 | // Include config file |
74 | daniel-mar | 274 | |
2 | daniel-mar | 275 | if (file_exists(__DIR__ . '/../config.inc.php')) { |
276 | include_once __DIR__ . '/../config.inc.php'; |
||
277 | } else { |
||
240 | daniel-mar | 278 | if (!is_dir(__DIR__.'/../../setup')) { |
279 | throw new OIDplusConfigInitializationException('File includes/config.inc.php is missing, but setup can\'t be started because its directory missing.'); |
||
280 | } else { |
||
281 | if ($html) { |
||
282 | header('Location:'.OIDplus::getSystemUrl().'setup/'); |
||
283 | die('Redirecting to setup...'); |
||
2 | daniel-mar | 284 | } else { |
240 | daniel-mar | 285 | // This can be displayed in e.g. ajax.php |
286 | throw new OIDplusConfigInitializationException('File includes/config.inc.php is missing. Please run setup again.'); |
||
2 | daniel-mar | 287 | } |
288 | } |
||
289 | } |
||
290 | |||
230 | daniel-mar | 291 | // Auto-fill non-existing config values, so that there won't be any PHP errors |
292 | // if something would be missing in config.inc.php (which should not happen!) |
||
74 | daniel-mar | 293 | |
2 | daniel-mar | 294 | if (!defined('OIDPLUS_CONFIG_VERSION')) define('OIDPLUS_CONFIG_VERSION', 0.0); |
295 | if (!defined('OIDPLUS_ADMIN_PASSWORD')) define('OIDPLUS_ADMIN_PASSWORD', ''); |
||
150 | daniel-mar | 296 | if (!defined('OIDPLUS_DATABASE_PLUGIN')) define('OIDPLUS_DATABASE_PLUGIN', 'MySQL'); |
2 | daniel-mar | 297 | if (!defined('OIDPLUS_MYSQL_HOST')) define('OIDPLUS_MYSQL_HOST', 'localhost'); |
298 | if (!defined('OIDPLUS_MYSQL_USERNAME')) define('OIDPLUS_MYSQL_USERNAME', 'root'); |
||
150 | daniel-mar | 299 | if (!defined('OIDPLUS_MYSQL_PASSWORD')) define('OIDPLUS_MYSQL_PASSWORD', ''); // base64 encoded |
2 | daniel-mar | 300 | if (!defined('OIDPLUS_MYSQL_DATABASE')) define('OIDPLUS_MYSQL_DATABASE', 'oidplus'); |
301 | if (!defined('OIDPLUS_TABLENAME_PREFIX')) define('OIDPLUS_TABLENAME_PREFIX', ''); |
||
302 | if (!defined('OIDPLUS_SESSION_SECRET')) define('OIDPLUS_SESSION_SECRET', ''); |
||
27 | daniel-mar | 303 | if (!defined('RECAPTCHA_ENABLED')) define('RECAPTCHA_ENABLED', false); |
304 | if (!defined('RECAPTCHA_PUBLIC')) define('RECAPTCHA_PUBLIC', ''); |
||
305 | if (!defined('RECAPTCHA_PRIVATE')) define('RECAPTCHA_PRIVATE', ''); |
||
111 | daniel-mar | 306 | if (!defined('OIDPLUS_ENFORCE_SSL')) define('OIDPLUS_ENFORCE_SSL', 2 /* Auto */); |
256 | daniel-mar | 307 | |
247 | daniel-mar | 308 | // Now include a file containing various size/depth limitations of OIDs |
309 | // It is important to include it after config.inc.php was included, |
||
310 | // so we can give config.inc.php the chance to override the values |
||
256 | daniel-mar | 311 | // by defining the constants first. |
2 | daniel-mar | 312 | |
256 | daniel-mar | 313 | include_once __DIR__ . '/../limits.inc.php'; |
247 | daniel-mar | 314 | |
2 | daniel-mar | 315 | // Check version of the config file |
74 | daniel-mar | 316 | |
76 | daniel-mar | 317 | if (OIDPLUS_CONFIG_VERSION != 2.0) { |
240 | daniel-mar | 318 | throw new OIDplusConfigInitializationException("The information located in includes/config.inc.php is outdated."); |
2 | daniel-mar | 319 | } |
42 | daniel-mar | 320 | |
150 | daniel-mar | 321 | // Register database types (highest priority) |
322 | |||
323 | $ary = glob(__DIR__ . '/../../plugins/database/'.'*'.'/plugin.inc.php'); |
||
324 | foreach ($ary as $a) include $a; |
||
325 | |||
224 | daniel-mar | 326 | foreach (get_declared_classes() as $c) { |
246 | daniel-mar | 327 | if (is_subclass_of($c, 'OIDplusDatabasePlugin')) { |
224 | daniel-mar | 328 | self::registerDatabasePlugin(new $c()); |
329 | } |
||
330 | } |
||
331 | |||
237 | daniel-mar | 332 | foreach (OIDplus::getDatabasePlugins() as $plugin) { |
333 | $plugin->init($html); |
||
334 | } |
||
335 | |||
42 | daniel-mar | 336 | // Do redirect stuff etc. |
74 | daniel-mar | 337 | |
230 | daniel-mar | 338 | self::isSslAvailable(); // This function does automatic redirects |
61 | daniel-mar | 339 | |
66 | daniel-mar | 340 | // System config settings |
341 | |||
75 | daniel-mar | 342 | OIDplus::config()->prepareConfigKey('objecttypes_initialized', 'List of object type plugins that were initialized once', '', 1, 1); |
343 | OIDplus::config()->prepareConfigKey('objecttypes_enabled', 'Enabled object types and their order, separated with a semicolon (please reload the page so that the change is applied)', '', 0, 1); |
||
66 | daniel-mar | 344 | |
75 | daniel-mar | 345 | OIDplus::config()->prepareConfigKey('oidplus_private_key', 'Private key for this system', '', 1, 0); |
80 | daniel-mar | 346 | OIDplus::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.', '', 1, 1); |
74 | daniel-mar | 347 | |
348 | // Initialize public / private keys |
||
349 | |||
227 | daniel-mar | 350 | OIDplus::getPkiStatus(true); |
74 | daniel-mar | 351 | |
237 | daniel-mar | 352 | // Register non-DB plugins |
74 | daniel-mar | 353 | |
222 | daniel-mar | 354 | $ary = glob(__DIR__ . '/../../plugins/objectTypes/'.'*'.'/plugin.inc.php'); |
150 | daniel-mar | 355 | foreach ($ary as $a) include $a; |
356 | |||
256 | daniel-mar | 357 | $ary = glob(__DIR__ . '/../../plugins/*Pages/'.'*'.'/plugin.inc.php'); |
61 | daniel-mar | 358 | foreach ($ary as $a) include $a; |
74 | daniel-mar | 359 | |
221 | daniel-mar | 360 | $ary = glob(__DIR__ . '/../../plugins/auth/'.'*'.'/plugin.inc.php'); |
361 | foreach ($ary as $a) include $a; |
||
362 | |||
224 | daniel-mar | 363 | foreach (get_declared_classes() as $c) { |
256 | daniel-mar | 364 | if (!(new ReflectionClass($c))->isAbstract()) { |
365 | if (is_subclass_of($c, 'OIDplusPagePlugin')) { |
||
366 | self::registerPagePlugin(new $c()); |
||
367 | } |
||
368 | if (is_subclass_of($c, 'OIDplusAuthPlugin')) { |
||
369 | self::registerAuthPlugin(new $c()); |
||
370 | } |
||
371 | if (is_subclass_of($c, 'OIDplusObjectTypePlugin')) { |
||
372 | self::registerObjectTypePlugin(new $c()); |
||
373 | } |
||
224 | daniel-mar | 374 | } |
375 | } |
||
74 | daniel-mar | 376 | |
237 | daniel-mar | 377 | // Initialize non-DB plugins |
224 | daniel-mar | 378 | |
74 | daniel-mar | 379 | foreach (OIDplus::getPagePlugins('*') as $plugin) { |
380 | $plugin->init($html); |
||
381 | } |
||
230 | daniel-mar | 382 | foreach (OIDplus::getAuthPlugins() as $plugin) { |
383 | $plugin->init($html); |
||
384 | } |
||
385 | foreach (OIDplus::getObjectTypePlugins() as $plugin) { |
||
386 | $plugin->init($html); |
||
387 | } |
||
2 | daniel-mar | 388 | } |
42 | daniel-mar | 389 | |
227 | daniel-mar | 390 | # --- System URL, System ID, PKI, and other functions |
391 | |||
392 | public static function getSystemUrl($relative=false) { |
||
393 | if (!isset($_SERVER["SCRIPT_NAME"])) return false; |
||
394 | |||
395 | $test_dir = dirname($_SERVER['SCRIPT_FILENAME']); |
||
396 | $c = 0; |
||
397 | while (!file_exists($test_dir.'/oidplus_base.js')) { |
||
398 | $test_dir = dirname($test_dir); |
||
399 | $c++; |
||
400 | if ($c == 1000) return false; |
||
401 | } |
||
402 | |||
403 | $res = dirname($_SERVER['SCRIPT_NAME'].'xxx'); |
||
404 | |||
405 | for ($i=1; $i<=$c; $i++) { |
||
406 | $res = dirname($res); |
||
407 | } |
||
408 | |||
409 | if ($res == '/') $res = ''; |
||
410 | $res .= '/'; |
||
411 | |||
412 | if (!$relative) { |
||
228 | daniel-mar | 413 | $is_ssl = isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] === 'on'); |
414 | $protocol = $is_ssl ? 'https' : 'http'; |
||
415 | $host = $_SERVER['HTTP_HOST']; |
||
416 | $port = $_SERVER['SERVER_PORT']; |
||
417 | if ($is_ssl && ($port != 443)) { |
||
418 | $port_add = ":$port"; |
||
419 | } else if (!$is_ssl && ($port != 80)) { |
||
420 | $port_add = ":$port"; |
||
421 | } else { |
||
422 | $port_add = ""; |
||
423 | } |
||
424 | $res = $protocol.'://'.$host.$port_add.$res; |
||
227 | daniel-mar | 425 | } |
426 | |||
427 | return $res; |
||
428 | } |
||
429 | |||
430 | private static $system_id_cache = null; |
||
431 | public static function getSystemId($oid=false) { |
||
432 | if (!is_null(self::$system_id_cache)) { |
||
433 | $out = self::$system_id_cache; |
||
434 | } else { |
||
435 | $out = false; |
||
436 | |||
437 | if (self::getPkiStatus(true)) { |
||
438 | $pubKey = OIDplus::config()->getValue('oidplus_public_key'); |
||
439 | if (preg_match('@BEGIN PUBLIC KEY\-+(.+)\-+END PUBLIC KEY@ismU', $pubKey, $m)) { |
||
440 | $out = smallhash(base64_decode($m[1])); |
||
441 | } |
||
442 | } |
||
443 | self::$system_id_cache = $out; |
||
444 | } |
||
239 | daniel-mar | 445 | return ($out ? '1.3.6.1.4.1.37476.30.9.' : '').$out; |
227 | daniel-mar | 446 | } |
447 | |||
448 | public static function getPkiStatus($try_generate=true) { |
||
449 | if (!function_exists('openssl_pkey_new')) return false; |
||
450 | |||
451 | $privKey = OIDplus::config()->getValue('oidplus_private_key'); |
||
452 | $pubKey = OIDplus::config()->getValue('oidplus_public_key'); |
||
453 | |||
454 | if ($try_generate && !verify_private_public_key($privKey, $pubKey)) { |
||
455 | $config = array( |
||
456 | "digest_alg" => "sha512", |
||
457 | "private_key_bits" => 2048, |
||
458 | "private_key_type" => OPENSSL_KEYTYPE_RSA, |
||
459 | ); |
||
460 | |||
461 | // Create the private and public key |
||
462 | $res = openssl_pkey_new($config); |
||
256 | daniel-mar | 463 | |
239 | daniel-mar | 464 | if (!$res) return false; |
227 | daniel-mar | 465 | |
466 | // Extract the private key from $res to $privKey |
||
467 | openssl_pkey_export($res, $privKey); |
||
468 | |||
469 | // Extract the public key from $res to $pubKey |
||
470 | $pubKey = openssl_pkey_get_details($res)["key"]; |
||
471 | |||
239 | daniel-mar | 472 | // Log |
473 | OIDplus::logger()->log("A!", "Generating new SystemID using a new key pair"); |
||
474 | |||
227 | daniel-mar | 475 | // Save the key pair to database |
476 | OIDplus::config()->setValue('oidplus_private_key', $privKey); |
||
477 | OIDplus::config()->setValue('oidplus_public_key', $pubKey); |
||
478 | |||
479 | // Log the new system ID |
||
480 | if (preg_match('@BEGIN PUBLIC KEY\-+(.+)\-+END PUBLIC KEY@ismU', $pubKey, $m)) { |
||
481 | $system_id = smallhash(base64_decode($m[1])); |
||
482 | OIDplus::logger()->log("A!", "Your SystemID is now $system_id"); |
||
483 | } |
||
484 | } |
||
485 | |||
486 | return verify_private_public_key($privKey, $pubKey); |
||
487 | } |
||
488 | |||
170 | daniel-mar | 489 | public static function getInstallType() { |
490 | if (!file_exists(__DIR__ . '/../../oidplus_version.txt') && !is_dir(__DIR__ . '/../../.svn')) { |
||
491 | return 'unknown'; |
||
492 | } |
||
493 | if (file_exists(__DIR__ . '/../../oidplus_version.txt') && is_dir(__DIR__ . '/../../.svn')) { |
||
494 | return 'ambigous'; |
||
495 | } |
||
496 | if (is_dir(__DIR__ . '/../../.svn')) { |
||
497 | return 'svn-wc'; |
||
498 | } |
||
499 | if (file_exists(__DIR__ . '/../../oidplus_version.txt')) { |
||
500 | return 'svn-snapshot'; |
||
501 | } |
||
502 | } |
||
503 | |||
111 | daniel-mar | 504 | public static function getVersion() { |
162 | daniel-mar | 505 | if (file_exists(__DIR__ . '/../../oidplus_version.txt') && is_dir(__DIR__ . '/../../.svn')) { |
239 | daniel-mar | 506 | return false; // version is ambigous |
111 | daniel-mar | 507 | } |
162 | daniel-mar | 508 | |
509 | if (is_dir(__DIR__ . '/../../.svn')) { |
||
510 | // Try to find out the SVN version using the shell |
||
239 | daniel-mar | 511 | // TODO: das müllt die log files voll! |
162 | daniel-mar | 512 | $status = @shell_exec('svnversion '.realpath(__FILE__)); |
513 | if (preg_match('/\d+/', $status, $match)) { |
||
239 | daniel-mar | 514 | return 'svn-'.$match[0]; |
162 | daniel-mar | 515 | } |
516 | |||
517 | // If that failed, try to get the version via SQLite3 |
||
239 | daniel-mar | 518 | if (class_exists('SQLite3')) { |
162 | daniel-mar | 519 | $db = new SQLite3(__DIR__ . '/../../.svn/wc.db'); |
520 | $results = $db->query('SELECT MIN(revision) AS rev FROM NODES_BASE'); |
||
521 | while ($row = $results->fetchArray()) { |
||
239 | daniel-mar | 522 | return 'svn-'.$row['rev']; |
162 | daniel-mar | 523 | } |
524 | } |
||
525 | } |
||
526 | |||
527 | if (file_exists(__DIR__ . '/../../oidplus_version.txt')) { |
||
528 | $cont = file_get_contents(__DIR__ . '/../../oidplus_version.txt'); |
||
239 | daniel-mar | 529 | if (preg_match('@Revision (\d+)@', $cont, $m)) |
530 | return 'svn-'.$m[1]; |
||
162 | daniel-mar | 531 | } |
532 | |||
239 | daniel-mar | 533 | return false; |
111 | daniel-mar | 534 | } |
535 | |||
230 | daniel-mar | 536 | private static $sslAvailableCache = null; |
537 | public static function isSslAvailable() { |
||
538 | if (!is_null(self::$sslAvailableCache)) return self::$sslAvailableCache; |
||
256 | daniel-mar | 539 | |
230 | daniel-mar | 540 | if (php_sapi_name() == 'cli') { |
541 | self::$sslAvailableCache = false; |
||
542 | return false; |
||
543 | } |
||
544 | |||
49 | daniel-mar | 545 | $timeout = 2; |
80 | daniel-mar | 546 | $already_ssl = isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == "on"); |
547 | $ssl_port = 443; |
||
227 | daniel-mar | 548 | $cookie_path = OIDplus::getSystemUrl(true); |
83 | daniel-mar | 549 | if (empty($cookie_path)) $cookie_path = '/'; |
42 | daniel-mar | 550 | |
80 | daniel-mar | 551 | if (OIDPLUS_ENFORCE_SSL == 0) { |
552 | // No SSL available |
||
230 | daniel-mar | 553 | self::$sslAvailableCache = $already_ssl; |
80 | daniel-mar | 554 | return $already_ssl; |
555 | } |
||
556 | |||
557 | if (OIDPLUS_ENFORCE_SSL == 1) { |
||
558 | // Force SSL |
||
559 | if ($already_ssl) { |
||
230 | daniel-mar | 560 | self::$sslAvailableCache = true; |
80 | daniel-mar | 561 | return true; |
42 | daniel-mar | 562 | } else { |
80 | daniel-mar | 563 | $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
564 | header('Location:'.$location); |
||
240 | daniel-mar | 565 | die('Redirecting to HTTPS...'); |
230 | daniel-mar | 566 | self::$sslAvailableCache = true; |
80 | daniel-mar | 567 | return true; |
568 | } |
||
569 | } |
||
570 | |||
571 | if (OIDPLUS_ENFORCE_SSL == 2) { |
||
572 | // Automatic SSL detection |
||
573 | |||
574 | if ($already_ssl) { |
||
575 | // we are already on HTTPS |
||
83 | daniel-mar | 576 | setcookie('SSL_CHECK', '1', 0, $cookie_path, '', false, true); |
230 | daniel-mar | 577 | self::$sslAvailableCache = true; |
80 | daniel-mar | 578 | return true; |
579 | } else { |
||
580 | if (isset($_COOKIE['SSL_CHECK'])) { |
||
581 | // We already had the HTTPS detection done before. |
||
582 | if ($_COOKIE['SSL_CHECK']) { |
||
583 | // HTTPS was detected before, but we are HTTP. Redirect now |
||
584 | $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
||
585 | header('Location:'.$location); |
||
240 | daniel-mar | 586 | die('Redirecting to HTTPS...'); |
230 | daniel-mar | 587 | self::$sslAvailableCache = true; |
80 | daniel-mar | 588 | return true; |
589 | } else { |
||
590 | // No HTTPS available. Do nothing. |
||
230 | daniel-mar | 591 | self::$sslAvailableCache = false; |
80 | daniel-mar | 592 | return false; |
593 | } |
||
49 | daniel-mar | 594 | } else { |
80 | daniel-mar | 595 | // This is our first check (or the browser didn't accept the SSL_CHECK cookie) |
596 | if (@fsockopen($_SERVER['HTTP_HOST'], $ssl_port, $errno, $errstr, $timeout)) { |
||
597 | // HTTPS detected. Redirect now, and remember that we had detected HTTPS |
||
83 | daniel-mar | 598 | setcookie('SSL_CHECK', '1', 0, $cookie_path, '', false, true); |
80 | daniel-mar | 599 | $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
600 | header('Location:'.$location); |
||
240 | daniel-mar | 601 | die('Redirecting to HTTPS...'); |
230 | daniel-mar | 602 | self::$sslAvailableCache = true; |
80 | daniel-mar | 603 | return true; |
604 | } else { |
||
605 | // No HTTPS detected. Do nothing, and next time, don't try to detect HTTPS again. |
||
83 | daniel-mar | 606 | setcookie('SSL_CHECK', '0', 0, $cookie_path, '', false, true); |
230 | daniel-mar | 607 | self::$sslAvailableCache = false; |
80 | daniel-mar | 608 | return false; |
609 | } |
||
49 | daniel-mar | 610 | } |
42 | daniel-mar | 611 | } |
612 | } |
||
613 | } |
||
241 | daniel-mar | 614 | |
615 | public static function webpath($target) { |
||
616 | $dir = __DIR__; |
||
617 | $dir = dirname($dir); |
||
618 | $dir = dirname($dir); |
||
619 | $target = substr($target, strlen($dir)+1, strlen($target)-strlen($dir)-1); |
||
620 | if ($target != '') { |
||
621 | $target = str_replace('\\','/',$target).'/'; |
||
622 | } |
||
623 | return $target; |
||
624 | } |
||
2 | daniel-mar | 625 | } |