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