Rev 112 | Rev 139 | 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(); |
24 | private static /*OIDplusObject*/ $objectTypes = array(); |
||
74 | daniel-mar | 25 | private static /*OIDplusObject*/ $disabledObjectTypes = array(); |
2 | daniel-mar | 26 | |
27 | private function __construct() { |
||
28 | } |
||
29 | |||
30 | public static function db() { |
||
86 | daniel-mar | 31 | static $database = null; |
32 | if (is_null($database)) { |
||
33 | $database = new OIDplusDataBaseMySQL(); |
||
2 | daniel-mar | 34 | } |
86 | daniel-mar | 35 | return $database; |
2 | daniel-mar | 36 | } |
37 | |||
38 | public static function config() { |
||
86 | daniel-mar | 39 | static $config = null; |
40 | if (is_null($config)) { |
||
41 | $config = new OIDplusConfig(); |
||
2 | daniel-mar | 42 | } |
86 | daniel-mar | 43 | return $config; |
2 | daniel-mar | 44 | } |
45 | |||
46 | public static function gui() { |
||
86 | daniel-mar | 47 | static $gui = null; |
48 | if (is_null($gui)) { |
||
49 | $gui = new OIDplusGui(); |
||
50 | } |
||
51 | return $gui; |
||
2 | daniel-mar | 52 | } |
53 | |||
54 | public static function authUtils() { |
||
86 | daniel-mar | 55 | static $authUtils = null; |
56 | if (is_null($authUtils)) { |
||
57 | $authUtils = new OIDplusAuthUtils(); |
||
58 | } |
||
59 | return $authUtils; |
||
2 | daniel-mar | 60 | } |
61 | |||
115 | daniel-mar | 62 | public static function logger() { |
63 | static $logger = null; |
||
64 | if (is_null($logger)) { |
||
65 | $logger = new OIDplusLogger(); |
||
66 | } |
||
67 | return $logger; |
||
68 | } |
||
69 | |||
86 | daniel-mar | 70 | public static function sesHandler() { |
71 | static $sesHandler = null; |
||
72 | if (is_null($sesHandler)) { |
||
73 | $sesHandler = new OIDplusSessionHandler(OIDPLUS_SESSION_SECRET); |
||
74 | } |
||
75 | return $sesHandler; |
||
76 | } |
||
77 | |||
83 | daniel-mar | 78 | public static function system_url($relative=false) { |
74 | daniel-mar | 79 | if (!isset($_SERVER["REQUEST_URI"])) return false; |
80 | |||
81 | $test_dir = dirname($_SERVER['SCRIPT_FILENAME']); |
||
82 | $c = 0; |
||
83 | while (!file_exists($test_dir.'/oidplus.js')) { |
||
84 | $test_dir = dirname($test_dir); |
||
85 | $c++; |
||
86 | if ($c == 1000) return false; |
||
87 | } |
||
88 | |||
80 | daniel-mar | 89 | $res = dirname($_SERVER['REQUEST_URI'].'xxx'); |
74 | daniel-mar | 90 | |
91 | for ($i=1; $i<=$c; $i++) { |
||
76 | daniel-mar | 92 | $res = dirname($res); |
74 | daniel-mar | 93 | } |
94 | |||
76 | daniel-mar | 95 | $res .= '/'; |
96 | |||
83 | daniel-mar | 97 | if (!$relative) { |
98 | $res = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]" . $res; // TODO: also add port? |
||
99 | } |
||
82 | daniel-mar | 100 | |
74 | daniel-mar | 101 | return $res; |
2 | daniel-mar | 102 | } |
103 | |||
61 | daniel-mar | 104 | public static function registerPagePlugin(OIDplusPagePlugin $plugin) { |
105 | $type = $plugin->type(); |
||
106 | if ($type === false) return false; |
||
107 | |||
108 | $prio = $plugin->priority(); |
||
109 | if ($prio === false) return false; |
||
110 | |||
111 | if (!isset(self::$pagePlugins[$type])) self::$pagePlugins[$type] = array(); |
||
112 | self::$pagePlugins[$type][$prio] = $plugin; |
||
113 | |||
114 | return true; |
||
115 | } |
||
116 | |||
117 | public static function getPagePlugins($type) { |
||
118 | if ($type == '*') { |
||
119 | $res = array(); |
||
120 | foreach (self::$pagePlugins as $data) { |
||
121 | $res = array_merge($res, $data); |
||
122 | } |
||
123 | } else { |
||
104 | daniel-mar | 124 | $res = isset(self::$pagePlugins[$type]) ? self::$pagePlugins[$type] : array(); |
61 | daniel-mar | 125 | } |
126 | ksort($res); |
||
127 | return $res; |
||
128 | } |
||
129 | |||
130 | public static function registerObjectType($ot) { |
||
66 | daniel-mar | 131 | $ns = $ot::ns(); |
132 | |||
133 | if (empty($ns)) die("Attention: Empty NS at $ot\n"); |
||
134 | |||
135 | $ns_found = false; |
||
136 | foreach (OIDplus::getRegisteredObjectTypes() as $test_ot) { |
||
137 | if ($test_ot::ns() == $ns) { |
||
138 | $ns_found = true; |
||
139 | break; |
||
140 | } |
||
141 | } |
||
142 | if ($ns_found) { |
||
143 | throw new Exception("Attention: Two objectType plugins use the same namespace \"$ns\"!"); |
||
144 | } |
||
145 | |||
146 | $init = OIDplus::config()->getValue("objecttypes_initialized"); |
||
147 | $init_ary = empty($init) ? array() : explode(';', $init); |
||
70 | daniel-mar | 148 | $init_ary = array_map('trim', $init_ary); |
66 | daniel-mar | 149 | |
150 | $enabled = OIDplus::config()->getValue("objecttypes_enabled"); |
||
151 | $enabled_ary = empty($enabled) ? array() : explode(';', $enabled); |
||
70 | daniel-mar | 152 | $enabled_ary = array_map('trim', $enabled_ary); |
66 | daniel-mar | 153 | |
79 | daniel-mar | 154 | $do_enable = false; |
155 | if (in_array($ns, $enabled_ary)) { |
||
156 | $do_enable = true; |
||
157 | } else { |
||
158 | if (!OIDplus::config()->getValue('registration_done')) { |
||
159 | $do_enable = $ns == 'oid'; |
||
160 | } else { |
||
161 | $do_enable = !in_array($ns, $init_ary); |
||
162 | } |
||
163 | } |
||
164 | |||
165 | if ($do_enable) { |
||
66 | daniel-mar | 166 | self::$objectTypes[] = $ot; |
167 | usort(self::$objectTypes, function($a, $b) { |
||
168 | $enabled = OIDplus::config()->getValue("objecttypes_enabled"); |
||
169 | $enabled_ary = explode(';', $enabled); |
||
170 | |||
171 | $idx_a = array_search($a::ns(), $enabled_ary); |
||
172 | $idx_b = array_search($b::ns(), $enabled_ary); |
||
173 | |||
174 | if ($idx_a == $idx_b) { |
||
175 | return 0; |
||
176 | } |
||
177 | return ($idx_a > $idx_b) ? +1 : -1; |
||
178 | }); |
||
74 | daniel-mar | 179 | } else { |
180 | self::$disabledObjectTypes[] = $ot; |
||
66 | daniel-mar | 181 | } |
182 | |||
183 | if (!in_array($ns, $init_ary)) { |
||
184 | // Was never initialized before, so we add it to the list of enabled object types once |
||
185 | |||
79 | daniel-mar | 186 | if ($do_enable) { |
187 | $enabled_ary[] = $ns; |
||
188 | OIDplus::config()->setValue("objecttypes_enabled", implode(';', $enabled_ary)); |
||
189 | } |
||
66 | daniel-mar | 190 | |
191 | $init_ary[] = $ns; |
||
192 | OIDplus::config()->setValue("objecttypes_initialized", implode(';', $init_ary)); |
||
193 | } |
||
61 | daniel-mar | 194 | } |
195 | |||
196 | public static function getRegisteredObjectTypes() { |
||
197 | return self::$objectTypes; |
||
198 | } |
||
199 | |||
74 | daniel-mar | 200 | public static function getDisabledObjectTypes() { |
201 | return self::$disabledObjectTypes; |
||
202 | } |
||
203 | |||
204 | public static function system_id($oid=false) { |
||
205 | if (!self::pkiStatus(true)) return false; |
||
206 | $pubKey = OIDplus::config()->getValue('oidplus_public_key'); |
||
207 | if (preg_match('@BEGIN PUBLIC KEY\-+(.+)\-+END PUBLIC KEY@ismU', $pubKey, $m)) { |
||
208 | return ($oid ? '1.3.6.1.4.1.37476.30.9.' : '').smallhash(base64_decode($m[1])); |
||
209 | } |
||
210 | return false; |
||
211 | } |
||
212 | |||
213 | public static function pkiStatus($try_generate=true) { |
||
214 | if (!function_exists('openssl_pkey_new')) return false; |
||
215 | |||
216 | $privKey = OIDplus::config()->getValue('oidplus_private_key'); |
||
217 | $pubKey = OIDplus::config()->getValue('oidplus_public_key'); |
||
218 | |||
219 | if ($try_generate && !verify_private_public_key($privKey, $pubKey)) { |
||
220 | $config = array( |
||
221 | "digest_alg" => "sha512", |
||
222 | "private_key_bits" => 2048, |
||
223 | "private_key_type" => OPENSSL_KEYTYPE_RSA, |
||
224 | ); |
||
225 | |||
226 | // Create the private and public key |
||
227 | $res = openssl_pkey_new($config); |
||
228 | |||
229 | // Extract the private key from $res to $privKey |
||
230 | openssl_pkey_export($res, $privKey); |
||
231 | |||
232 | OIDplus::config()->setValue('oidplus_private_key', $privKey); |
||
233 | |||
234 | // Extract the public key from $res to $pubKey |
||
235 | $pubKey = openssl_pkey_get_details($res); |
||
236 | $pubKey = $pubKey["key"]; |
||
237 | |||
238 | OIDplus::config()->setValue('oidplus_public_key', $pubKey); |
||
239 | } |
||
240 | |||
241 | return verify_private_public_key($privKey, $pubKey); |
||
242 | } |
||
243 | |||
2 | daniel-mar | 244 | public static function init($html=true) { |
42 | daniel-mar | 245 | define('OIDPLUS_HTML_OUTPUT', $html); |
246 | |||
2 | daniel-mar | 247 | // Include config file |
74 | daniel-mar | 248 | |
2 | daniel-mar | 249 | if (file_exists(__DIR__ . '/../config.inc.php')) { |
250 | include_once __DIR__ . '/../config.inc.php'; |
||
251 | } else { |
||
252 | if ($html) { |
||
74 | daniel-mar | 253 | if (!is_dir('setup')) { |
2 | daniel-mar | 254 | echo 'Error: Setup directory missing.'; |
255 | } else { |
||
42 | daniel-mar | 256 | header('Location:setup/'); |
2 | daniel-mar | 257 | } |
258 | } else { |
||
259 | echo 'Error: Setup directory missing!'; |
||
260 | } |
||
261 | die(); |
||
262 | } |
||
263 | |||
264 | // Auto-fill non-existing config values |
||
74 | daniel-mar | 265 | |
2 | daniel-mar | 266 | if (!defined('OIDPLUS_CONFIG_VERSION')) define('OIDPLUS_CONFIG_VERSION', 0.0); |
267 | if (!defined('OIDPLUS_ADMIN_PASSWORD')) define('OIDPLUS_ADMIN_PASSWORD', ''); |
||
268 | if (!defined('OIDPLUS_MYSQL_HOST')) define('OIDPLUS_MYSQL_HOST', 'localhost'); |
||
269 | if (!defined('OIDPLUS_MYSQL_USERNAME')) define('OIDPLUS_MYSQL_USERNAME', 'root'); |
||
270 | if (!defined('OIDPLUS_MYSQL_PASSWORD')) define('OIDPLUS_MYSQL_PASSWORD', ''); |
||
271 | if (!defined('OIDPLUS_MYSQL_DATABASE')) define('OIDPLUS_MYSQL_DATABASE', 'oidplus'); |
||
272 | if (!defined('OIDPLUS_TABLENAME_PREFIX')) define('OIDPLUS_TABLENAME_PREFIX', ''); |
||
273 | if (!defined('OIDPLUS_SESSION_SECRET')) define('OIDPLUS_SESSION_SECRET', ''); |
||
27 | daniel-mar | 274 | if (!defined('RECAPTCHA_ENABLED')) define('RECAPTCHA_ENABLED', false); |
275 | if (!defined('RECAPTCHA_PUBLIC')) define('RECAPTCHA_PUBLIC', ''); |
||
276 | if (!defined('RECAPTCHA_PRIVATE')) define('RECAPTCHA_PRIVATE', ''); |
||
111 | daniel-mar | 277 | if (!defined('OIDPLUS_ENFORCE_SSL')) define('OIDPLUS_ENFORCE_SSL', 2 /* Auto */); |
2 | daniel-mar | 278 | |
279 | // Check version of the config file |
||
74 | daniel-mar | 280 | |
76 | daniel-mar | 281 | if (OIDPLUS_CONFIG_VERSION != 2.0) { |
2 | daniel-mar | 282 | if ($html) { |
283 | echo '<h1>Error</h1><p>The information located in <b>includes/config.inc.php</b> is outdated.</p><p>Please run <a href="setup/">setup</a> again.</p>'; |
||
284 | } else { |
||
285 | echo 'The information located in includes/config.inc.php is outdated. Please run setup again.'; |
||
286 | } |
||
287 | die(); |
||
288 | } |
||
42 | daniel-mar | 289 | |
290 | // Do redirect stuff etc. |
||
74 | daniel-mar | 291 | |
42 | daniel-mar | 292 | define('OIDPLUS_SSL_AVAILABLE', self::isSslAvailable()); |
61 | daniel-mar | 293 | |
66 | daniel-mar | 294 | // System config settings |
295 | |||
75 | daniel-mar | 296 | OIDplus::config()->prepareConfigKey('objecttypes_initialized', 'List of object type plugins that were initialized once', '', 1, 1); |
297 | 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 | 298 | |
75 | daniel-mar | 299 | OIDplus::config()->prepareConfigKey('oidplus_private_key', 'Private key for this system', '', 1, 0); |
80 | daniel-mar | 300 | 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 | 301 | |
302 | // Initialize public / private keys |
||
303 | |||
304 | OIDplus::pkiStatus(true); |
||
305 | |||
61 | daniel-mar | 306 | // Register plugins |
74 | daniel-mar | 307 | |
308 | $ary = glob(__DIR__ . '/../../plugins/system/'.'*'.'/plugin.inc.php'); |
||
309 | foreach ($ary as $a) include $a; |
||
61 | daniel-mar | 310 | $ary = glob(__DIR__ . '/../../plugins/publicPages/'.'*'.'/plugin.inc.php'); |
311 | foreach ($ary as $a) include $a; |
||
312 | $ary = glob(__DIR__ . '/../../plugins/raPages/'.'*'.'/plugin.inc.php'); |
||
313 | foreach ($ary as $a) include $a; |
||
314 | $ary = glob(__DIR__ . '/../../plugins/adminPages/'.'*'.'/plugin.inc.php'); |
||
315 | foreach ($ary as $a) include $a; |
||
66 | daniel-mar | 316 | $ary = glob(__DIR__ . '/../../plugins/objectTypes/'.'*'.'/*.class.php'); |
317 | foreach ($ary as $a) include $a; |
||
74 | daniel-mar | 318 | |
319 | // Initialize plugins |
||
320 | |||
321 | foreach (OIDplus::getPagePlugins('*') as $plugin) { |
||
322 | $plugin->init($html); |
||
323 | } |
||
2 | daniel-mar | 324 | } |
42 | daniel-mar | 325 | |
111 | daniel-mar | 326 | public static function getVersion() { |
327 | $status = @shell_exec('svnversion '.realpath(__FILE__)); |
||
328 | if (preg_match('/\d+/', $status, $match)) { |
||
329 | return 'svn-'.$match[0]; |
||
330 | } else { |
||
331 | return false; |
||
332 | } |
||
333 | } |
||
334 | |||
42 | daniel-mar | 335 | private static function isSslAvailable() { |
49 | daniel-mar | 336 | $timeout = 2; |
80 | daniel-mar | 337 | $already_ssl = isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == "on"); |
338 | $ssl_port = 443; |
||
83 | daniel-mar | 339 | $cookie_path = OIDplus::system_url(true); |
340 | if (empty($cookie_path)) $cookie_path = '/'; |
||
42 | daniel-mar | 341 | |
54 | daniel-mar | 342 | if (php_sapi_name() == 'cli') return false; |
343 | |||
80 | daniel-mar | 344 | if (OIDPLUS_ENFORCE_SSL == 0) { |
345 | // No SSL available |
||
346 | return $already_ssl; |
||
347 | } |
||
348 | |||
349 | if (OIDPLUS_ENFORCE_SSL == 1) { |
||
350 | // Force SSL |
||
351 | if ($already_ssl) { |
||
352 | return true; |
||
42 | daniel-mar | 353 | } else { |
80 | daniel-mar | 354 | $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
355 | header('Location:'.$location); |
||
111 | daniel-mar | 356 | die('Redirect to HTTPS'); |
80 | daniel-mar | 357 | return true; |
358 | } |
||
359 | } |
||
360 | |||
361 | if (OIDPLUS_ENFORCE_SSL == 2) { |
||
362 | // Automatic SSL detection |
||
363 | |||
364 | if ($already_ssl) { |
||
365 | // we are already on HTTPS |
||
83 | daniel-mar | 366 | setcookie('SSL_CHECK', '1', 0, $cookie_path, '', false, true); |
80 | daniel-mar | 367 | return true; |
368 | } else { |
||
369 | if (isset($_COOKIE['SSL_CHECK'])) { |
||
370 | // We already had the HTTPS detection done before. |
||
371 | if ($_COOKIE['SSL_CHECK']) { |
||
372 | // HTTPS was detected before, but we are HTTP. Redirect now |
||
373 | $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
||
374 | header('Location:'.$location); |
||
375 | die('Redirect to HTTPS'); |
||
376 | return true; |
||
377 | } else { |
||
378 | // No HTTPS available. Do nothing. |
||
379 | return false; |
||
380 | } |
||
49 | daniel-mar | 381 | } else { |
80 | daniel-mar | 382 | // This is our first check (or the browser didn't accept the SSL_CHECK cookie) |
383 | if (@fsockopen($_SERVER['HTTP_HOST'], $ssl_port, $errno, $errstr, $timeout)) { |
||
384 | // HTTPS detected. Redirect now, and remember that we had detected HTTPS |
||
83 | daniel-mar | 385 | setcookie('SSL_CHECK', '1', 0, $cookie_path, '', false, true); |
80 | daniel-mar | 386 | $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
387 | header('Location:'.$location); |
||
111 | daniel-mar | 388 | die('Redirect to HTTPS'); |
80 | daniel-mar | 389 | return true; |
390 | } else { |
||
391 | // No HTTPS detected. Do nothing, and next time, don't try to detect HTTPS again. |
||
83 | daniel-mar | 392 | setcookie('SSL_CHECK', '0', 0, $cookie_path, '', false, true); |
80 | daniel-mar | 393 | return false; |
394 | } |
||
49 | daniel-mar | 395 | } |
42 | daniel-mar | 396 | } |
397 | } |
||
398 | } |
||
2 | daniel-mar | 399 | } |