Subversion Repositories oidplus

Rev

Rev 225 | Rev 228 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 225 Rev 227
Line 20... Line 20...
20
if (!defined('IN_OIDPLUS')) die();
20
if (!defined('IN_OIDPLUS')) die();
21
 
21
 
22
class OIDplus {
22
class OIDplus {
23
        private static /*OIDplusPagePlugin[][]*/ $pagePlugins = array();
23
        private static /*OIDplusPagePlugin[][]*/ $pagePlugins = array();
24
        private static /*OIDplusAuthPlugin[][]*/ $authPlugins = array();
24
        private static /*OIDplusAuthPlugin[][]*/ $authPlugins = array();
25
        private static /*OIDplusObject[]*/ $objectTypes = array();
25
        private static /*OIDplusObjectTypePlugin[]*/ $objectTypePlugins = array();
-
 
26
        private static /*string[]*/ $enabledObjectTypes = array();
26
        private static /*OIDplusObject[]*/ $disabledObjectTypes = array();
27
        private static /*string[]*/ $disabledObjectTypes = array();
27
        private static /*OIDplusDatabase[]*/ $dbPlugins = array();
28
        private static /*OIDplusDatabasePlugin[]*/ $dbPlugins = array();
28
 
29
 
29
        private function __construct() {
30
        private function __construct() {
30
        }
31
        }
31
 
32
 
32
        public static function db() {
33
        # --- Singleton classes
33
                if (!isset(self::$dbPlugins[OIDPLUS_DATABASE_PLUGIN])) {
-
 
34
                        throw new Exception("Database plugin '".htmlentities(OIDPLUS_DATABASE_PLUGIN)."' not found. Please check config.inc.php or run <a href=\"setup/\">setup</a> again.");
-
 
35
                }
-
 
36
                $obj = self::$dbPlugins[OIDPLUS_DATABASE_PLUGIN];
-
 
37
                if (!$obj->isConnected()) $obj->connect();
-
 
38
                return $obj;
-
 
39
        }
-
 
40
 
34
 
41
        public static function config() {
35
        public static function config() {
42
                static $config = null;
36
                static $config = null;
43
                if (is_null($config)) {
37
                if (is_null($config)) {
44
                        $config = new OIDplusConfig();
38
                        $config = new OIDplusConfig();
Line 76... Line 70...
76
                        $sesHandler = new OIDplusSessionHandler(OIDPLUS_SESSION_SECRET);
70
                        $sesHandler = new OIDplusSessionHandler(OIDPLUS_SESSION_SECRET);
77
                }
71
                }
78
                return $sesHandler;
72
                return $sesHandler;
79
        }
73
        }
80
 
74
 
81
        public static function system_url($relative=false) {
-
 
82
                if (!isset($_SERVER["REQUEST_URI"])) return false;
-
 
83
 
-
 
84
                $test_dir = dirname($_SERVER['SCRIPT_FILENAME']);
-
 
85
                $c = 0;
-
 
86
                while (!file_exists($test_dir.'/oidplus_base.js')) {
-
 
87
                        $test_dir = dirname($test_dir);
-
 
88
                        $c++;
-
 
89
                        if ($c == 1000) return false;
-
 
90
                }
-
 
91
 
-
 
92
                $res = dirname($_SERVER['REQUEST_URI'].'xxx');
-
 
93
 
-
 
94
                for ($i=1; $i<=$c; $i++) {
-
 
95
                        $res = dirname($res);
75
        # --- Database Plugin
96
                }
-
 
97
 
-
 
98
                if ($res == '/') $res = '';
-
 
99
                $res .= '/';
-
 
100
 
-
 
101
                if (!$relative) {
-
 
102
                        $res = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]" . $res; // TODO: also add port?
-
 
103
                }
-
 
104
 
-
 
105
                return $res;
-
 
106
        }
-
 
107
 
76
 
108
        private static function registerDatabasePlugin(OIDplusDatabase $plugin) {
77
        private static function registerDatabasePlugin(OIDplusDatabasePlugin $plugin) {
109
                $name = $plugin->name();
78
                $name = $plugin->name();
110
                if ($name === false) return false;
79
                if ($name === false) return false;
111
 
80
 
112
                self::$dbPlugins[$name] = $plugin;
81
                self::$dbPlugins[$name] = $plugin;
113
 
82
 
Line 116... Line 85...
116
 
85
 
117
        public static function getDatabasePlugins() {
86
        public static function getDatabasePlugins() {
118
                return self::$dbPlugins;
87
                return self::$dbPlugins;
119
        }
88
        }
120
 
89
 
-
 
90
        public static function db() {
-
 
91
                if (!isset(self::$dbPlugins[OIDPLUS_DATABASE_PLUGIN])) {
-
 
92
                        throw new Exception("Database plugin '".htmlentities(OIDPLUS_DATABASE_PLUGIN)."' not found. Please check config.inc.php or run <a href=\"setup/\">setup</a> again.");
-
 
93
                }
-
 
94
                $obj = self::$dbPlugins[OIDPLUS_DATABASE_PLUGIN];
-
 
95
                if (!$obj->isConnected()) $obj->connect();
-
 
96
                return $obj;
-
 
97
        }
-
 
98
 
-
 
99
        # --- Page plugin
-
 
100
 
121
        private static function registerPagePlugin(OIDplusPagePlugin $plugin) {
101
        private static function registerPagePlugin(OIDplusPagePlugin $plugin) {
122
                $type = $plugin->type();
102
                $type = $plugin->type();
123
                if ($type === false) return false;
103
                if ($type === false) return false;
124
 
104
 
125
                $prio = $plugin->priority();
105
                $prio = $plugin->priority();
Line 129... Line 109...
129
                self::$pagePlugins[$type][$prio] = $plugin;
109
                self::$pagePlugins[$type][$prio] = $plugin;
130
 
110
 
131
                return true;
111
                return true;
132
        }
112
        }
133
 
113
 
134
        private static function registerAuthPlugin(OIDplusAuthPlugin $plugin) {
-
 
135
                self::$authPlugins[] = $plugin;
-
 
136
                return true;
-
 
137
        }
-
 
138
 
-
 
139
        public static function getPagePlugins($type) {
114
        public static function getPagePlugins($type) {
140
                if ($type == '*') {
115
                if ($type == '*') {
141
                        $res = array();
116
                        $res = array();
142
                        foreach (self::$pagePlugins as $data) {
117
                        foreach (self::$pagePlugins as $data) {
143
                                $res = array_merge($res, $data);
118
                                $res = array_merge($res, $data);
Line 147... Line 122...
147
                }
122
                }
148
                ksort($res);
123
                ksort($res);
149
                return $res;
124
                return $res;
150
        }
125
        }
151
 
126
 
-
 
127
        # --- Auth plugin
-
 
128
 
-
 
129
        private static function registerAuthPlugin(OIDplusAuthPlugin $plugin) {
-
 
130
                self::$authPlugins[] = $plugin;
-
 
131
                return true;
-
 
132
        }
-
 
133
 
152
        public static function getAuthPlugins() {
134
        public static function getAuthPlugins() {
153
                return self::$authPlugins;
135
                return self::$authPlugins;
154
        }
136
        }
155
 
137
 
-
 
138
        # --- Object type plugin
-
 
139
 
-
 
140
        private static function registerObjectTypePlugin(OIDplusObjectTypePlugin $plugin) {
-
 
141
                self::$objectTypePlugins[] = $plugin;
-
 
142
 
-
 
143
                $ot = $plugin::getObjectTypeClassName();
-
 
144
                self::registerObjectType($ot);
-
 
145
 
-
 
146
                return true;
-
 
147
        }
-
 
148
 
156
        private static function registerObjectType($ot) {
149
        private static function registerObjectType($ot) {
157
                $ns = $ot::ns();
150
                $ns = $ot::ns();
158
 
151
 
159
                if (empty($ns)) die("Attention: Empty NS at $ot\n");
152
                if (empty($ns)) die("Attention: Empty NS at $ot\n");
160
 
153
 
161
                $ns_found = false;
154
                $ns_found = false;
162
                foreach (OIDplus::getRegisteredObjectTypes() as $test_ot) {
155
                foreach (array_merge(OIDplus::getEnabledObjectTypes(), OIDplus::getDisabledObjectTypes()) as $test_ot) {
163
                        if ($test_ot::ns() == $ns) {
156
                        if ($test_ot::ns() == $ns) {
164
                                $ns_found = true;
157
                                $ns_found = true;
165
                                break;
158
                                break;
166
                        }
159
                        }
167
                }
160
                }
Line 187... Line 180...
187
                                $do_enable = !in_array($ns, $init_ary);
180
                                $do_enable = !in_array($ns, $init_ary);
188
                        }
181
                        }
189
                }
182
                }
190
 
183
 
191
                if ($do_enable) {
184
                if ($do_enable) {
192
                        self::$objectTypes[] = $ot;
185
                        self::$enabledObjectTypes[] = $ot;
193
                        usort(self::$objectTypes, function($a, $b) {
186
                        usort(self::$enabledObjectTypes, function($a, $b) {
194
                                $enabled = OIDplus::config()->getValue("objecttypes_enabled");
187
                                $enabled = OIDplus::config()->getValue("objecttypes_enabled");
195
                                $enabled_ary = explode(';', $enabled);
188
                                $enabled_ary = explode(';', $enabled);
196
 
189
 
197
                                $idx_a = array_search($a::ns(), $enabled_ary);
190
                                $idx_a = array_search($a::ns(), $enabled_ary);
198
                                $idx_b = array_search($b::ns(), $enabled_ary);
191
                                $idx_b = array_search($b::ns(), $enabled_ary);
Line 217... Line 210...
217
                        $init_ary[] = $ns;
210
                        $init_ary[] = $ns;
218
                        OIDplus::config()->setValue("objecttypes_initialized", implode(';', $init_ary));
211
                        OIDplus::config()->setValue("objecttypes_initialized", implode(';', $init_ary));
219
                }
212
                }
220
        }
213
        }
221
 
214
 
222
        public static function getRegisteredObjectTypes() {
215
        public static function getObjectTypePlugins() {
223
                return self::$objectTypes;
216
                return self::$objectTypePlugins;
224
        }
-
 
225
 
-
 
226
        public static function getDisabledObjectTypes() {
-
 
227
                return self::$disabledObjectTypes;
-
 
228
        }
217
        }
229
 
218
 
230
        private static $system_id_cache = null;
-
 
231
        public static function system_id($oid=false) {
219
        public static function getObjectTypePluginsEnabled() {
232
                if (!is_null(self::$system_id_cache)) {
-
 
233
                        $out = self::$system_id_cache;
-
 
234
                } else {
-
 
235
                        $out = false;
220
                $res = array();
236
 
-
 
237
                        if (self::pkiStatus(true)) {
221
                foreach (self::$objectTypePlugins as $plugin) {
238
                                $pubKey = OIDplus::config()->getValue('oidplus_public_key');
222
                        $ot = $plugin::getObjectTypeClassName();
239
                                if (preg_match('@BEGIN PUBLIC KEY\-+(.+)\-+END PUBLIC KEY@ismU', $pubKey, $m)) {
223
                        if (in_array($ot, self::$enabledObjectTypes)) $res[] = $plugin;
240
                                        $out = smallhash(base64_decode($m[1]));
-
 
241
                                }
224
                }
-
 
225
                return $res;
242
                        }
226
        }
-
 
227
 
-
 
228
        public static function getObjectTypePluginsDisabled() {
-
 
229
                $res = array();
243
                        self::$system_id_cache = $out;
230
                foreach (self::$objectTypePlugins as $plugin) {
-
 
231
                        $ot = $plugin::getObjectTypeClassName();
-
 
232
                        if (in_array($ot, self::$disabledObjectTypes)) $res[] = $plugin;
244
                }
233
                }
245
                return ($oid ? '1.3.6.1.4.1.37476.30.9.' : '').$out;
234
                return $res;
246
        }
235
        }
247
 
236
 
248
        public static function pkiStatus($try_generate=true) {
237
        public static function getEnabledObjectTypes() {
249
                if (!function_exists('openssl_pkey_new')) return false;
-
 
250
 
-
 
251
                $privKey = OIDplus::config()->getValue('oidplus_private_key');
-
 
252
                $pubKey = OIDplus::config()->getValue('oidplus_public_key');
-
 
253
 
-
 
254
                if ($try_generate && !verify_private_public_key($privKey, $pubKey)) {
-
 
255
                        OIDplus::logger()->log("A!", "Generating new SystemID using a new key pair");
-
 
256
 
-
 
257
                        $config = array(
-
 
258
                            "digest_alg" => "sha512",
-
 
259
                            "private_key_bits" => 2048,
-
 
260
                            "private_key_type" => OPENSSL_KEYTYPE_RSA,
-
 
261
                        );
-
 
262
 
-
 
263
                        // Create the private and public key
-
 
264
                        $res = openssl_pkey_new($config);
238
                return self::$enabledObjectTypes;
265
 
-
 
266
                        // Extract the private key from $res to $privKey
-
 
267
                        openssl_pkey_export($res, $privKey);
-
 
268
 
-
 
269
                        // Extract the public key from $res to $pubKey
-
 
270
                        $pubKey = openssl_pkey_get_details($res)["key"];
-
 
271
 
-
 
272
                        // Save the key pair to database
-
 
273
                        OIDplus::config()->setValue('oidplus_private_key', $privKey);
-
 
274
                        OIDplus::config()->setValue('oidplus_public_key', $pubKey);
-
 
275
 
-
 
276
                        // Log the new system ID
-
 
277
                        if (preg_match('@BEGIN PUBLIC KEY\-+(.+)\-+END PUBLIC KEY@ismU', $pubKey, $m)) {
-
 
278
                                $system_id = smallhash(base64_decode($m[1]));
-
 
279
                                OIDplus::logger()->log("A!", "Your SystemID is now $system_id");
-
 
280
                        }
-
 
281
                }
239
        }
282
 
240
 
-
 
241
        public static function getDisabledObjectTypes() {
283
                return verify_private_public_key($privKey, $pubKey);
242
                return self::$disabledObjectTypes;
284
        }
243
        }
285
 
244
 
-
 
245
        # --- Initialization of OIDplus
-
 
246
 
286
        public static function init($html=true) {
247
        public static function init($html=true) {
287
                define('OIDPLUS_HTML_OUTPUT', $html);
248
                define('OIDPLUS_HTML_OUTPUT', $html);
288
 
249
 
289
                // Include config file
250
                // Include config file
290
 
251
 
Line 334... Line 295...
334
 
295
 
335
                $ary = glob(__DIR__ . '/../../plugins/database/'.'*'.'/plugin.inc.php');
296
                $ary = glob(__DIR__ . '/../../plugins/database/'.'*'.'/plugin.inc.php');
336
                foreach ($ary as $a) include $a;
297
                foreach ($ary as $a) include $a;
337
 
298
 
338
                foreach (get_declared_classes() as $c) {
299
                foreach (get_declared_classes() as $c) {
339
                        if (is_subclass_of($c, 'OIDplusDataBase')) {
300
                        if (is_subclass_of($c, 'OIDplusDataBasePlugin')) {
340
                                self::registerDatabasePlugin(new $c());
301
                                self::registerDatabasePlugin(new $c());
341
                        }
302
                        }
342
                }
303
                }
343
 
304
 
344
                // Do redirect stuff etc.
305
                // Do redirect stuff etc.
Line 353... Line 314...
353
                OIDplus::config()->prepareConfigKey('oidplus_private_key', 'Private key for this system', '', 1, 0);
314
                OIDplus::config()->prepareConfigKey('oidplus_private_key', 'Private key for this system', '', 1, 0);
354
                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);
315
                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);
355
 
316
 
356
                // Initialize public / private keys
317
                // Initialize public / private keys
357
 
318
 
358
                OIDplus::pkiStatus(true);
319
                OIDplus::getPkiStatus(true);
359
 
320
 
360
                // Register plugins
321
                // Register plugins
361
 
322
 
362
                $ary = glob(__DIR__ . '/../../plugins/objectTypes/'.'*'.'/plugin.inc.php');
323
                $ary = glob(__DIR__ . '/../../plugins/objectTypes/'.'*'.'/plugin.inc.php');
363
                foreach ($ary as $a) include $a;
324
                foreach ($ary as $a) include $a;
Line 377... Line 338...
377
                                self::registerPagePlugin(new $c());
338
                                self::registerPagePlugin(new $c());
378
                        }
339
                        }
379
                        if (is_subclass_of($c, 'OIDplusAuthPlugin')) {
340
                        if (is_subclass_of($c, 'OIDplusAuthPlugin')) {
380
                                self::registerAuthPlugin(new $c());
341
                                self::registerAuthPlugin(new $c());
381
                        }
342
                        }
382
                        if (is_subclass_of($c, 'OIDplusObject')) {
343
                        if (is_subclass_of($c, 'OIDplusObjectTypePlugin')) {
383
                                self::registerObjectType($c);
344
                                self::registerObjectTypePlugin(new $c());
384
                        }
345
                        }
385
                }
346
                }
386
 
347
 
387
                // Initialize page plugins
348
                // Initialize page plugins
388
 
349
 
389
                foreach (OIDplus::getPagePlugins('*') as $plugin) {
350
                foreach (OIDplus::getPagePlugins('*') as $plugin) {
390
                        $plugin->init($html);
351
                        $plugin->init($html);
391
                }
352
                }
392
        }
353
        }
393
 
354
 
-
 
355
        # --- System URL, System ID, PKI, and other functions
-
 
356
 
-
 
357
        public static function getSystemUrl($relative=false) {
-
 
358
                if (!isset($_SERVER["SCRIPT_NAME"])) return false;
-
 
359
 
-
 
360
                $test_dir = dirname($_SERVER['SCRIPT_FILENAME']);
-
 
361
                $c = 0;
-
 
362
                while (!file_exists($test_dir.'/oidplus_base.js')) {
-
 
363
                        $test_dir = dirname($test_dir);
-
 
364
                        $c++;
-
 
365
                        if ($c == 1000) return false;
-
 
366
                }
-
 
367
 
-
 
368
                $res = dirname($_SERVER['SCRIPT_NAME'].'xxx');
-
 
369
 
-
 
370
                for ($i=1; $i<=$c; $i++) {
-
 
371
                        $res = dirname($res);
-
 
372
                }
-
 
373
 
-
 
374
                if ($res == '/') $res = '';
-
 
375
                $res .= '/';
-
 
376
 
-
 
377
                if (!$relative) {
-
 
378
                        $res = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]" . $res; // TODO: also add port?
-
 
379
                }
-
 
380
 
-
 
381
                return $res;
-
 
382
        }
-
 
383
 
-
 
384
        private static $system_id_cache = null;
-
 
385
        public static function getSystemId($oid=false) {
-
 
386
                if (!is_null(self::$system_id_cache)) {
-
 
387
                        $out = self::$system_id_cache;
-
 
388
                } else {
-
 
389
                        $out = false;
-
 
390
 
-
 
391
                        if (self::getPkiStatus(true)) {
-
 
392
                                $pubKey = OIDplus::config()->getValue('oidplus_public_key');
-
 
393
                                if (preg_match('@BEGIN PUBLIC KEY\-+(.+)\-+END PUBLIC KEY@ismU', $pubKey, $m)) {
-
 
394
                                        $out = smallhash(base64_decode($m[1]));
-
 
395
                                }
-
 
396
                        }
-
 
397
                        self::$system_id_cache = $out;
-
 
398
                }
-
 
399
                return ($oid ? '1.3.6.1.4.1.37476.30.9.' : '').$out;
-
 
400
        }
-
 
401
 
-
 
402
        public static function getPkiStatus($try_generate=true) {
-
 
403
                if (!function_exists('openssl_pkey_new')) return false;
-
 
404
 
-
 
405
                $privKey = OIDplus::config()->getValue('oidplus_private_key');
-
 
406
                $pubKey = OIDplus::config()->getValue('oidplus_public_key');
-
 
407
 
-
 
408
                if ($try_generate && !verify_private_public_key($privKey, $pubKey)) {
-
 
409
                        OIDplus::logger()->log("A!", "Generating new SystemID using a new key pair");
-
 
410
 
-
 
411
                        $config = array(
-
 
412
                            "digest_alg" => "sha512",
-
 
413
                            "private_key_bits" => 2048,
-
 
414
                            "private_key_type" => OPENSSL_KEYTYPE_RSA,
-
 
415
                        );
-
 
416
 
-
 
417
                        // Create the private and public key
-
 
418
                        $res = openssl_pkey_new($config);
-
 
419
 
-
 
420
                        // Extract the private key from $res to $privKey
-
 
421
                        openssl_pkey_export($res, $privKey);
-
 
422
 
-
 
423
                        // Extract the public key from $res to $pubKey
-
 
424
                        $pubKey = openssl_pkey_get_details($res)["key"];
-
 
425
 
-
 
426
                        // Save the key pair to database
-
 
427
                        OIDplus::config()->setValue('oidplus_private_key', $privKey);
-
 
428
                        OIDplus::config()->setValue('oidplus_public_key', $pubKey);
-
 
429
 
-
 
430
                        // Log the new system ID
-
 
431
                        if (preg_match('@BEGIN PUBLIC KEY\-+(.+)\-+END PUBLIC KEY@ismU', $pubKey, $m)) {
-
 
432
                                $system_id = smallhash(base64_decode($m[1]));
-
 
433
                                OIDplus::logger()->log("A!", "Your SystemID is now $system_id");
-
 
434
                        }
-
 
435
                }
-
 
436
 
-
 
437
                return verify_private_public_key($privKey, $pubKey);
-
 
438
        }
-
 
439
 
394
        public static function getInstallType() {
440
        public static function getInstallType() {
395
                if (!file_exists(__DIR__ . '/../../oidplus_version.txt') && !is_dir(__DIR__ . '/../../.svn')) {
441
                if (!file_exists(__DIR__ . '/../../oidplus_version.txt') && !is_dir(__DIR__ . '/../../.svn')) {
396
                        return 'unknown';
442
                        return 'unknown';
397
                }
443
                }
398
                if (file_exists(__DIR__ . '/../../oidplus_version.txt') && is_dir(__DIR__ . '/../../.svn')) {
444
                if (file_exists(__DIR__ . '/../../oidplus_version.txt') && is_dir(__DIR__ . '/../../.svn')) {
Line 442... Line 488...
442
 
488
 
443
        private static function isSslAvailable() {
489
        private static function isSslAvailable() {
444
                $timeout = 2;
490
                $timeout = 2;
445
                $already_ssl = isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == "on");
491
                $already_ssl = isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == "on");
446
                $ssl_port = 443;
492
                $ssl_port = 443;
447
                $cookie_path = OIDplus::system_url(true);
493
                $cookie_path = OIDplus::getSystemUrl(true);
448
                if (empty($cookie_path)) $cookie_path = '/';
494
                if (empty($cookie_path)) $cookie_path = '/';
449
 
495
 
450
                if (php_sapi_name() == 'cli') return false;
496
                if (php_sapi_name() == 'cli') return false;
451
 
497
 
452
                if (OIDPLUS_ENFORCE_SSL == 0) {
498
                if (OIDPLUS_ENFORCE_SSL == 0) {