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