Subversion Repositories oidplus

Rev

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