Subversion Repositories oidplus

Rev

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