Subversion Repositories oidplus

Rev

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