Subversion Repositories oidplus

Rev

Rev 240 | Rev 246 | 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();
221 daniel-mar 24
        private static /*OIDplusAuthPlugin[][]*/ $authPlugins = array();
227 daniel-mar 25
        private static /*OIDplusObjectTypePlugin[]*/ $objectTypePlugins = array();
26
        private static /*string[]*/ $enabledObjectTypes = array();
27
        private static /*string[]*/ $disabledObjectTypes = array();
28
        private static /*OIDplusDatabasePlugin[]*/ $dbPlugins = array();
2 daniel-mar 29
 
236 daniel-mar 30
        protected static $html = null;
31
 
2 daniel-mar 32
        private function __construct() {
33
        }
241 daniel-mar 34
 
227 daniel-mar 35
        # --- Singleton classes
2 daniel-mar 36
 
37
        public static function config() {
86 daniel-mar 38
                static $config = null;
39
                if (is_null($config)) {
40
                        $config = new OIDplusConfig();
2 daniel-mar 41
                }
86 daniel-mar 42
                return $config;
2 daniel-mar 43
        }
44
 
45
        public static function gui() {
86 daniel-mar 46
                static $gui = null;
47
                if (is_null($gui)) {
48
                        $gui = new OIDplusGui();
49
                }
50
                return $gui;
2 daniel-mar 51
        }
52
 
53
        public static function authUtils() {
86 daniel-mar 54
                static $authUtils = null;
55
                if (is_null($authUtils)) {
56
                        $authUtils = new OIDplusAuthUtils();
57
                }
58
                return $authUtils;
2 daniel-mar 59
        }
60
 
115 daniel-mar 61
        public static function logger() {
62
                static $logger = null;
63
                if (is_null($logger)) {
64
                        $logger = new OIDplusLogger();
65
                }
66
                return $logger;
67
        }
68
 
86 daniel-mar 69
        public static function sesHandler() {
70
                static $sesHandler = null;
71
                if (is_null($sesHandler)) {
72
                        $sesHandler = new OIDplusSessionHandler(OIDPLUS_SESSION_SECRET);
73
                }
74
                return $sesHandler;
75
        }
76
 
230 daniel-mar 77
        # --- Database plugin
74 daniel-mar 78
 
227 daniel-mar 79
        private static function registerDatabasePlugin(OIDplusDatabasePlugin $plugin) {
150 daniel-mar 80
                $name = $plugin->name();
81
                if ($name === false) return false;
82
 
83
                self::$dbPlugins[$name] = $plugin;
84
 
85
                return true;
86
        }
87
 
88
        public static function getDatabasePlugins() {
89
                return self::$dbPlugins;
90
        }
91
 
227 daniel-mar 92
        public static function db() {
93
                if (!isset(self::$dbPlugins[OIDPLUS_DATABASE_PLUGIN])) {
240 daniel-mar 94
                        throw new OIDplusConfigInitializationException("Database plugin '".OIDPLUS_DATABASE_PLUGIN."' not found");
227 daniel-mar 95
                }
96
                $obj = self::$dbPlugins[OIDPLUS_DATABASE_PLUGIN];
97
                if (!$obj->isConnected()) $obj->connect();
98
                return $obj;
99
        }
100
 
101
        # --- Page plugin
102
 
224 daniel-mar 103
        private static function registerPagePlugin(OIDplusPagePlugin $plugin) {
61 daniel-mar 104
                $type = $plugin->type();
105
                if ($type === false) return false;
106
 
107
                $prio = $plugin->priority();
108
                if ($prio === false) return false;
109
 
110
                if (!isset(self::$pagePlugins[$type])) self::$pagePlugins[$type] = array();
111
                self::$pagePlugins[$type][$prio] = $plugin;
112
 
113
                return true;
114
        }
115
 
230 daniel-mar 116
        public static function getPagePlugins($type='*') {
117
                if ($type === '*') {
61 daniel-mar 118
                        $res = array();
119
                        foreach (self::$pagePlugins as $data) {
120
                                $res = array_merge($res, $data);
121
                        }
122
                } else {
230 daniel-mar 123
                        $types = explode(',', $type);
124
                        foreach ($types as $type) {
125
                                $res = isset(self::$pagePlugins[$type]) ? self::$pagePlugins[$type] : array();
126
                        }
61 daniel-mar 127
                }
128
                ksort($res);
129
                return $res;
130
        }
131
 
227 daniel-mar 132
        # --- Auth plugin
133
 
134
        private static function registerAuthPlugin(OIDplusAuthPlugin $plugin) {
135
                self::$authPlugins[] = $plugin;
136
                return true;
137
        }
138
 
221 daniel-mar 139
        public static function getAuthPlugins() {
140
                return self::$authPlugins;
141
        }
142
 
227 daniel-mar 143
        # --- Object type plugin
144
 
145
        private static function registerObjectTypePlugin(OIDplusObjectTypePlugin $plugin) {
146
                self::$objectTypePlugins[] = $plugin;
147
 
148
                $ot = $plugin::getObjectTypeClassName();
149
                self::registerObjectType($ot);
150
 
151
                return true;
152
        }
153
 
224 daniel-mar 154
        private static function registerObjectType($ot) {
66 daniel-mar 155
                $ns = $ot::ns();
156
 
240 daniel-mar 157
                if (empty($ns)) throw new Exception("Attention: Empty NS at $ot\n");
66 daniel-mar 158
 
159
                $ns_found = false;
227 daniel-mar 160
                foreach (array_merge(OIDplus::getEnabledObjectTypes(), OIDplus::getDisabledObjectTypes()) as $test_ot) {
66 daniel-mar 161
                        if ($test_ot::ns() == $ns) {
162
                                $ns_found = true;
163
                                break;
164
                        }
165
                }
166
                if ($ns_found) {
167
                        throw new Exception("Attention: Two objectType plugins use the same namespace \"$ns\"!");
168
                }
169
 
170
                $init = OIDplus::config()->getValue("objecttypes_initialized");
171
                $init_ary = empty($init) ? array() : explode(';', $init);
70 daniel-mar 172
                $init_ary = array_map('trim', $init_ary);
66 daniel-mar 173
 
174
                $enabled = OIDplus::config()->getValue("objecttypes_enabled");
175
                $enabled_ary = empty($enabled) ? array() : explode(';', $enabled);
70 daniel-mar 176
                $enabled_ary = array_map('trim', $enabled_ary);
66 daniel-mar 177
 
79 daniel-mar 178
                $do_enable = false;
179
                if (in_array($ns, $enabled_ary)) {
180
                        $do_enable = true;
181
                } else {
182
                        if (!OIDplus::config()->getValue('registration_done')) {
183
                                $do_enable = $ns == 'oid';
184
                        } else {
185
                                $do_enable = !in_array($ns, $init_ary);
186
                        }
187
                }
188
 
189
                if ($do_enable) {
227 daniel-mar 190
                        self::$enabledObjectTypes[] = $ot;
191
                        usort(self::$enabledObjectTypes, function($a, $b) {
66 daniel-mar 192
                                $enabled = OIDplus::config()->getValue("objecttypes_enabled");
193
                                $enabled_ary = explode(';', $enabled);
194
 
195
                                $idx_a = array_search($a::ns(), $enabled_ary);
196
                                $idx_b = array_search($b::ns(), $enabled_ary);
197
 
198
                                if ($idx_a == $idx_b) {
199
                                    return 0;
200
                                }
201
                                return ($idx_a > $idx_b) ? +1 : -1;
202
                        });
74 daniel-mar 203
                } else {
204
                        self::$disabledObjectTypes[] = $ot;
66 daniel-mar 205
                }
206
 
207
                if (!in_array($ns, $init_ary)) {
208
                        // Was never initialized before, so we add it to the list of enabled object types once
209
 
79 daniel-mar 210
                        if ($do_enable) {
211
                                $enabled_ary[] = $ns;
212
                                OIDplus::config()->setValue("objecttypes_enabled", implode(';', $enabled_ary));
213
                        }
66 daniel-mar 214
 
215
                        $init_ary[] = $ns;
216
                        OIDplus::config()->setValue("objecttypes_initialized", implode(';', $init_ary));
217
                }
61 daniel-mar 218
        }
219
 
227 daniel-mar 220
        public static function getObjectTypePlugins() {
221
                return self::$objectTypePlugins;
61 daniel-mar 222
        }
223
 
227 daniel-mar 224
        public static function getObjectTypePluginsEnabled() {
225
                $res = array();
226
                foreach (self::$objectTypePlugins as $plugin) {
227
                        $ot = $plugin::getObjectTypeClassName();
228
                        if (in_array($ot, self::$enabledObjectTypes)) $res[] = $plugin;
229
                }
230
                return $res;
74 daniel-mar 231
        }
232
 
227 daniel-mar 233
        public static function getObjectTypePluginsDisabled() {
234
                $res = array();
235
                foreach (self::$objectTypePlugins as $plugin) {
236
                        $ot = $plugin::getObjectTypeClassName();
237
                        if (in_array($ot, self::$disabledObjectTypes)) $res[] = $plugin;
74 daniel-mar 238
                }
227 daniel-mar 239
                return $res;
74 daniel-mar 240
        }
241
 
227 daniel-mar 242
        public static function getEnabledObjectTypes() {
243
                return self::$enabledObjectTypes;
244
        }
74 daniel-mar 245
 
227 daniel-mar 246
        public static function getDisabledObjectTypes() {
247
                return self::$disabledObjectTypes;
248
        }
74 daniel-mar 249
 
227 daniel-mar 250
        # --- Initialization of OIDplus
206 daniel-mar 251
 
2 daniel-mar 252
        public static function init($html=true) {
236 daniel-mar 253
                self::$html = $html;
254
 
2 daniel-mar 255
                // Include config file
74 daniel-mar 256
 
2 daniel-mar 257
                if (file_exists(__DIR__ . '/../config.inc.php')) {
258
                        include_once __DIR__ . '/../config.inc.php';
259
                } else {
240 daniel-mar 260
                        if (!is_dir(__DIR__.'/../../setup')) {
261
                                throw new OIDplusConfigInitializationException('File includes/config.inc.php is missing, but setup can\'t be started because its directory missing.');
262
                        } else {
263
                                if ($html) {
264
                                        header('Location:'.OIDplus::getSystemUrl().'setup/');
265
                                        die('Redirecting to setup...');
2 daniel-mar 266
                                } else {
240 daniel-mar 267
                                        // This can be displayed in e.g. ajax.php
268
                                        throw new OIDplusConfigInitializationException('File includes/config.inc.php is missing. Please run setup again.');
2 daniel-mar 269
                                }
270
                        }
271
                }
272
 
230 daniel-mar 273
                // Auto-fill non-existing config values, so that there won't be any PHP errors
274
                // if something would be missing in config.inc.php (which should not happen!)
74 daniel-mar 275
 
2 daniel-mar 276
                if (!defined('OIDPLUS_CONFIG_VERSION'))   define('OIDPLUS_CONFIG_VERSION',   0.0);
277
                if (!defined('OIDPLUS_ADMIN_PASSWORD'))   define('OIDPLUS_ADMIN_PASSWORD',   '');
150 daniel-mar 278
                if (!defined('OIDPLUS_DATABASE_PLUGIN'))  define('OIDPLUS_DATABASE_PLUGIN',  'MySQL');
2 daniel-mar 279
                if (!defined('OIDPLUS_MYSQL_HOST'))       define('OIDPLUS_MYSQL_HOST',       'localhost');
280
                if (!defined('OIDPLUS_MYSQL_USERNAME'))   define('OIDPLUS_MYSQL_USERNAME',   'root');
150 daniel-mar 281
                if (!defined('OIDPLUS_MYSQL_PASSWORD'))   define('OIDPLUS_MYSQL_PASSWORD',   ''); // base64 encoded
2 daniel-mar 282
                if (!defined('OIDPLUS_MYSQL_DATABASE'))   define('OIDPLUS_MYSQL_DATABASE',   'oidplus');
283
                if (!defined('OIDPLUS_TABLENAME_PREFIX')) define('OIDPLUS_TABLENAME_PREFIX', '');
284
                if (!defined('OIDPLUS_SESSION_SECRET'))   define('OIDPLUS_SESSION_SECRET',   '');
27 daniel-mar 285
                if (!defined('RECAPTCHA_ENABLED'))        define('RECAPTCHA_ENABLED',        false);
286
                if (!defined('RECAPTCHA_PUBLIC'))         define('RECAPTCHA_PUBLIC',         '');
287
                if (!defined('RECAPTCHA_PRIVATE'))        define('RECAPTCHA_PRIVATE',        '');
111 daniel-mar 288
                if (!defined('OIDPLUS_ENFORCE_SSL'))      define('OIDPLUS_ENFORCE_SSL',      2 /* Auto */);
2 daniel-mar 289
 
290
                // Check version of the config file
74 daniel-mar 291
 
76 daniel-mar 292
                if (OIDPLUS_CONFIG_VERSION != 2.0) {
240 daniel-mar 293
                        throw new OIDplusConfigInitializationException("The information located in includes/config.inc.php is outdated.");
2 daniel-mar 294
                }
42 daniel-mar 295
 
150 daniel-mar 296
                // Register database types (highest priority)
297
 
298
                $ary = glob(__DIR__ . '/../../plugins/database/'.'*'.'/plugin.inc.php');
299
                foreach ($ary as $a) include $a;
300
 
224 daniel-mar 301
                foreach (get_declared_classes() as $c) {
227 daniel-mar 302
                        if (is_subclass_of($c, 'OIDplusDataBasePlugin')) {
224 daniel-mar 303
                                self::registerDatabasePlugin(new $c());
304
                        }
305
                }
306
 
237 daniel-mar 307
                foreach (OIDplus::getDatabasePlugins() as $plugin) {
308
                        $plugin->init($html);
309
                }
310
 
42 daniel-mar 311
                // Do redirect stuff etc.
74 daniel-mar 312
 
230 daniel-mar 313
                self::isSslAvailable(); // This function does automatic redirects
61 daniel-mar 314
 
66 daniel-mar 315
                // System config settings
316
 
75 daniel-mar 317
                OIDplus::config()->prepareConfigKey('objecttypes_initialized', 'List of object type plugins that were initialized once', '', 1, 1);
318
                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 319
 
75 daniel-mar 320
                OIDplus::config()->prepareConfigKey('oidplus_private_key', 'Private key for this system', '', 1, 0);
80 daniel-mar 321
                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 322
 
323
                // Initialize public / private keys
324
 
227 daniel-mar 325
                OIDplus::getPkiStatus(true);
74 daniel-mar 326
 
237 daniel-mar 327
                // Register non-DB plugins
74 daniel-mar 328
 
222 daniel-mar 329
                $ary = glob(__DIR__ . '/../../plugins/objectTypes/'.'*'.'/plugin.inc.php');
150 daniel-mar 330
                foreach ($ary as $a) include $a;
331
 
61 daniel-mar 332
                $ary = glob(__DIR__ . '/../../plugins/publicPages/'.'*'.'/plugin.inc.php');
333
                foreach ($ary as $a) include $a;
334
                $ary = glob(__DIR__ . '/../../plugins/raPages/'.'*'.'/plugin.inc.php');
335
                foreach ($ary as $a) include $a;
336
                $ary = glob(__DIR__ . '/../../plugins/adminPages/'.'*'.'/plugin.inc.php');
337
                foreach ($ary as $a) include $a;
74 daniel-mar 338
 
221 daniel-mar 339
                $ary = glob(__DIR__ . '/../../plugins/auth/'.'*'.'/plugin.inc.php');
340
                foreach ($ary as $a) include $a;
341
 
224 daniel-mar 342
                foreach (get_declared_classes() as $c) {
343
                        if (is_subclass_of($c, 'OIDplusPagePlugin')) {
344
                                self::registerPagePlugin(new $c());
345
                        }
346
                        if (is_subclass_of($c, 'OIDplusAuthPlugin')) {
347
                                self::registerAuthPlugin(new $c());
348
                        }
227 daniel-mar 349
                        if (is_subclass_of($c, 'OIDplusObjectTypePlugin')) {
350
                                self::registerObjectTypePlugin(new $c());
224 daniel-mar 351
                        }
352
                }
74 daniel-mar 353
 
237 daniel-mar 354
                // Initialize non-DB plugins
224 daniel-mar 355
 
74 daniel-mar 356
                foreach (OIDplus::getPagePlugins('*') as $plugin) {
357
                        $plugin->init($html);
358
                }
230 daniel-mar 359
                foreach (OIDplus::getAuthPlugins() as $plugin) {
360
                        $plugin->init($html);
361
                }
362
                foreach (OIDplus::getObjectTypePlugins() as $plugin) {
363
                        $plugin->init($html);
364
                }
2 daniel-mar 365
        }
42 daniel-mar 366
 
227 daniel-mar 367
        # --- System URL, System ID, PKI, and other functions
368
 
369
        public static function getSystemUrl($relative=false) {
370
                if (!isset($_SERVER["SCRIPT_NAME"])) return false;
371
 
372
                $test_dir = dirname($_SERVER['SCRIPT_FILENAME']);
373
                $c = 0;
374
                while (!file_exists($test_dir.'/oidplus_base.js')) {
375
                        $test_dir = dirname($test_dir);
376
                        $c++;
377
                        if ($c == 1000) return false;
378
                }
379
 
380
                $res = dirname($_SERVER['SCRIPT_NAME'].'xxx');
381
 
382
                for ($i=1; $i<=$c; $i++) {
383
                        $res = dirname($res);
384
                }
385
 
386
                if ($res == '/') $res = '';
387
                $res .= '/';
388
 
389
                if (!$relative) {
228 daniel-mar 390
                        $is_ssl = isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] === 'on');
391
                        $protocol = $is_ssl ? 'https' : 'http';
392
                        $host = $_SERVER['HTTP_HOST'];
393
                        $port = $_SERVER['SERVER_PORT'];
394
                        if ($is_ssl && ($port != 443)) {
395
                                $port_add = ":$port";
396
                        } else if (!$is_ssl && ($port != 80)) {
397
                                $port_add = ":$port";
398
                        } else {
399
                                $port_add = "";
400
                        }
401
                        $res = $protocol.'://'.$host.$port_add.$res;
227 daniel-mar 402
                }
403
 
404
                return $res;
405
        }
406
 
407
        private static $system_id_cache = null;
408
        public static function getSystemId($oid=false) {
409
                if (!is_null(self::$system_id_cache)) {
410
                        $out = self::$system_id_cache;
411
                } else {
412
                        $out = false;
413
 
414
                        if (self::getPkiStatus(true)) {
415
                                $pubKey = OIDplus::config()->getValue('oidplus_public_key');
416
                                if (preg_match('@BEGIN PUBLIC KEY\-+(.+)\-+END PUBLIC KEY@ismU', $pubKey, $m)) {
417
                                        $out = smallhash(base64_decode($m[1]));
418
                                }
419
                        }
420
                        self::$system_id_cache = $out;
421
                }
239 daniel-mar 422
                return ($out ? '1.3.6.1.4.1.37476.30.9.' : '').$out;
227 daniel-mar 423
        }
424
 
425
        public static function getPkiStatus($try_generate=true) {
426
                if (!function_exists('openssl_pkey_new')) return false;
427
 
428
                $privKey = OIDplus::config()->getValue('oidplus_private_key');
429
                $pubKey = OIDplus::config()->getValue('oidplus_public_key');
430
 
431
                if ($try_generate && !verify_private_public_key($privKey, $pubKey)) {
432
                        $config = array(
433
                            "digest_alg" => "sha512",
434
                            "private_key_bits" => 2048,
435
                            "private_key_type" => OPENSSL_KEYTYPE_RSA,
436
                        );
437
 
438
                        // Create the private and public key
439
                        $res = openssl_pkey_new($config);
239 daniel-mar 440
 
441
                        if (!$res) return false;
227 daniel-mar 442
 
443
                        // Extract the private key from $res to $privKey
444
                        openssl_pkey_export($res, $privKey);
445
 
446
                        // Extract the public key from $res to $pubKey
447
                        $pubKey = openssl_pkey_get_details($res)["key"];
448
 
239 daniel-mar 449
                        // Log
450
                        OIDplus::logger()->log("A!", "Generating new SystemID using a new key pair");
451
 
227 daniel-mar 452
                        // Save the key pair to database
453
                        OIDplus::config()->setValue('oidplus_private_key', $privKey);
454
                        OIDplus::config()->setValue('oidplus_public_key', $pubKey);
455
 
456
                        // Log the new system ID
457
                        if (preg_match('@BEGIN PUBLIC KEY\-+(.+)\-+END PUBLIC KEY@ismU', $pubKey, $m)) {
458
                                $system_id = smallhash(base64_decode($m[1]));
459
                                OIDplus::logger()->log("A!", "Your SystemID is now $system_id");
460
                        }
461
                }
462
 
463
                return verify_private_public_key($privKey, $pubKey);
464
        }
465
 
170 daniel-mar 466
        public static function getInstallType() {
467
                if (!file_exists(__DIR__ . '/../../oidplus_version.txt') && !is_dir(__DIR__ . '/../../.svn')) {
468
                        return 'unknown';
469
                }
470
                if (file_exists(__DIR__ . '/../../oidplus_version.txt') && is_dir(__DIR__ . '/../../.svn')) {
471
                        return 'ambigous';
472
                }
473
                if (is_dir(__DIR__ . '/../../.svn')) {
474
                        return 'svn-wc';
475
                }
476
                if (file_exists(__DIR__ . '/../../oidplus_version.txt')) {
477
                        return 'svn-snapshot';
478
                }
479
        }
480
 
111 daniel-mar 481
        public static function getVersion() {
162 daniel-mar 482
                if (file_exists(__DIR__ . '/../../oidplus_version.txt') && is_dir(__DIR__ . '/../../.svn')) {
239 daniel-mar 483
                        return false; // version is ambigous
111 daniel-mar 484
                }
162 daniel-mar 485
 
486
                if (is_dir(__DIR__ . '/../../.svn')) {
487
                        // Try to find out the SVN version using the shell
239 daniel-mar 488
                        // TODO: das müllt die log files voll!
162 daniel-mar 489
                        $status = @shell_exec('svnversion '.realpath(__FILE__));
490
                        if (preg_match('/\d+/', $status, $match)) {
239 daniel-mar 491
                                return 'svn-'.$match[0];
162 daniel-mar 492
                        }
493
 
494
                        // If that failed, try to get the version via SQLite3
239 daniel-mar 495
                        if (class_exists('SQLite3')) {
162 daniel-mar 496
                                $db = new SQLite3(__DIR__ . '/../../.svn/wc.db');
497
                                $results = $db->query('SELECT MIN(revision) AS rev FROM NODES_BASE');
498
                                while ($row = $results->fetchArray()) {
239 daniel-mar 499
                                        return 'svn-'.$row['rev'];
162 daniel-mar 500
                                }
501
                        }
502
                }
503
 
504
                if (file_exists(__DIR__ . '/../../oidplus_version.txt')) {
505
                        $cont = file_get_contents(__DIR__ . '/../../oidplus_version.txt');
239 daniel-mar 506
                        if (preg_match('@Revision (\d+)@', $cont, $m))
507
                                return 'svn-'.$m[1];
162 daniel-mar 508
                }
509
 
239 daniel-mar 510
                return false;
111 daniel-mar 511
        }
512
 
230 daniel-mar 513
        private static $sslAvailableCache = null;
514
        public static function isSslAvailable() {
515
                if (!is_null(self::$sslAvailableCache)) return self::$sslAvailableCache;
516
 
517
                if (php_sapi_name() == 'cli') {
518
                        self::$sslAvailableCache = false;
519
                        return false;
520
                }
521
 
49 daniel-mar 522
                $timeout = 2;
80 daniel-mar 523
                $already_ssl = isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == "on");
524
                $ssl_port = 443;
227 daniel-mar 525
                $cookie_path = OIDplus::getSystemUrl(true);
83 daniel-mar 526
                if (empty($cookie_path)) $cookie_path = '/';
42 daniel-mar 527
 
80 daniel-mar 528
                if (OIDPLUS_ENFORCE_SSL == 0) {
529
                        // No SSL available
230 daniel-mar 530
                        self::$sslAvailableCache = $already_ssl;
80 daniel-mar 531
                        return $already_ssl;
532
                }
533
 
534
                if (OIDPLUS_ENFORCE_SSL == 1) {
535
                        // Force SSL
536
                        if ($already_ssl) {
230 daniel-mar 537
                                self::$sslAvailableCache = true;
80 daniel-mar 538
                                return true;
42 daniel-mar 539
                        } else {
80 daniel-mar 540
                                $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
541
                                header('Location:'.$location);
240 daniel-mar 542
                                die('Redirecting to HTTPS...');
230 daniel-mar 543
                                self::$sslAvailableCache = true;
80 daniel-mar 544
                                return true;
545
                        }
546
                }
547
 
548
                if (OIDPLUS_ENFORCE_SSL == 2) {
549
                        // Automatic SSL detection
550
 
551
                        if ($already_ssl) {
552
                                // we are already on HTTPS
83 daniel-mar 553
                                setcookie('SSL_CHECK', '1', 0, $cookie_path, '', false, true);
230 daniel-mar 554
                                self::$sslAvailableCache = true;
80 daniel-mar 555
                                return true;
556
                        } else {
557
                                if (isset($_COOKIE['SSL_CHECK'])) {
558
                                        // We already had the HTTPS detection done before.
559
                                        if ($_COOKIE['SSL_CHECK']) {
560
                                                // HTTPS was detected before, but we are HTTP. Redirect now
561
                                                $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
562
                                                header('Location:'.$location);
240 daniel-mar 563
                                                die('Redirecting to HTTPS...');
230 daniel-mar 564
                                                self::$sslAvailableCache = true;
80 daniel-mar 565
                                                return true;
566
                                        } else {
567
                                                // No HTTPS available. Do nothing.
230 daniel-mar 568
                                                self::$sslAvailableCache = false;
80 daniel-mar 569
                                                return false;
570
                                        }
49 daniel-mar 571
                                } else {
80 daniel-mar 572
                                        // This is our first check (or the browser didn't accept the SSL_CHECK cookie)
573
                                        if (@fsockopen($_SERVER['HTTP_HOST'], $ssl_port, $errno, $errstr, $timeout)) {
574
                                                // HTTPS detected. Redirect now, and remember that we had detected HTTPS
83 daniel-mar 575
                                                setcookie('SSL_CHECK', '1', 0, $cookie_path, '', false, true);
80 daniel-mar 576
                                                $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
577
                                                header('Location:'.$location);
240 daniel-mar 578
                                                die('Redirecting to HTTPS...');
230 daniel-mar 579
                                                self::$sslAvailableCache = true;
80 daniel-mar 580
                                                return true;
581
                                        } else {
582
                                                // No HTTPS detected. Do nothing, and next time, don't try to detect HTTPS again.
83 daniel-mar 583
                                                setcookie('SSL_CHECK', '0', 0, $cookie_path, '', false, true);
230 daniel-mar 584
                                                self::$sslAvailableCache = false;
80 daniel-mar 585
                                                return false;
586
                                        }
49 daniel-mar 587
                                }
42 daniel-mar 588
                        }
589
                }
590
        }
241 daniel-mar 591
 
592
        public static function webpath($target) {
593
                $dir = __DIR__;
594
                $dir = dirname($dir);
595
                $dir = dirname($dir);
596
                $target = substr($target, strlen($dir)+1, strlen($target)-strlen($dir)-1);
597
                if ($target != '') {
598
                        $target = str_replace('\\','/',$target).'/';
599
                }
600
                return $target;
601
        }
2 daniel-mar 602
}