Subversion Repositories oidplus

Rev

Rev 27 | Rev 49 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
5
 * Copyright 2019 Daniel Marschall, ViaThinkSoft
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 */
19
 
20
class OIDplus {
21
        private static /*OIDplusDataBase*/ $database;
22
        private static /*OIDplusConfig*/ $config;
23
 
24
        private function __construct() {
25
        }
26
 
27
        public static function db() {
28
                if (is_null(self::$database)) {
29
                        self::$database = new OIDplusDataBaseMySQL();
30
                }
31
                return self::$database;
32
        }
33
 
34
        public static function config() {
35
                if (is_null(self::$config)) {
36
                        self::$config = new OIDplusConfig();
37
                }
38
                return self::$config;
39
        }
40
 
41
        public static function gui() {
42
                return new OIDplusGui();
43
        }
44
 
45
        public static function authUtils() {
46
                return new OIDplusAuthUtils();
47
        }
48
 
49
        public static function system_url() {
50
                return dirname($actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]").'/';
51
        }
52
 
42 daniel-mar 53
        public static function sesHandler() {
54
                return new OIDplusSessionHandler(OIDPLUS_SESSION_SECRET);
55
        }
56
 
2 daniel-mar 57
        public static function init($html=true) {
42 daniel-mar 58
                define('OIDPLUS_HTML_OUTPUT', $html);
59
 
2 daniel-mar 60
                // Include config file
61
                if (file_exists(__DIR__ . '/../config.inc.php')) {
62
                        include_once __DIR__ . '/../config.inc.php';
63
                } else {
64
                        if ($html) {
65
                                if (!is_dir(__DIR__.'/../setup')) {
66
                                        echo 'Error: Setup directory missing.';
67
                                } else {
42 daniel-mar 68
                                        header('Location:setup/');
2 daniel-mar 69
                                }
70
                        } else {
71
                                echo 'Error: Setup directory missing!';
72
                        }
73
                        die();
74
                }
75
 
76
                // Auto-fill non-existing config values
77
                if (!defined('OIDPLUS_CONFIG_VERSION'))   define('OIDPLUS_CONFIG_VERSION',   0.0);
78
                if (!defined('OIDPLUS_ADMIN_PASSWORD'))   define('OIDPLUS_ADMIN_PASSWORD',   '');
79
                if (!defined('OIDPLUS_ADMIN_EMAIL'))      define('OIDPLUS_ADMIN_EMAIL',      '');
80
                if (!defined('OIDPLUS_MYSQL_HOST'))       define('OIDPLUS_MYSQL_HOST',       'localhost');
81
                if (!defined('OIDPLUS_MYSQL_USERNAME'))   define('OIDPLUS_MYSQL_USERNAME',   'root');
82
                if (!defined('OIDPLUS_MYSQL_PASSWORD'))   define('OIDPLUS_MYSQL_PASSWORD',   '');
83
                if (!defined('OIDPLUS_MYSQL_DATABASE'))   define('OIDPLUS_MYSQL_DATABASE',   'oidplus');
84
                if (!defined('OIDPLUS_TABLENAME_PREFIX')) define('OIDPLUS_TABLENAME_PREFIX', '');
85
                if (!defined('OIDPLUS_SESSION_SECRET'))   define('OIDPLUS_SESSION_SECRET',   '');
27 daniel-mar 86
                if (!defined('RECAPTCHA_ENABLED'))        define('RECAPTCHA_ENABLED',        false);
87
                if (!defined('RECAPTCHA_PUBLIC'))         define('RECAPTCHA_PUBLIC',         '');
88
                if (!defined('RECAPTCHA_PRIVATE'))        define('RECAPTCHA_PRIVATE',        '');
2 daniel-mar 89
 
90
                // Check version of the config file
91
                if (OIDPLUS_CONFIG_VERSION != 0.1) {
92
                        if ($html) {
93
                                echo '<h1>Error</h1><p>The information located in <b>includes/config.inc.php</b> is outdated.</p><p>Please run <a href="setup/">setup</a> again.</p>';
94
                        } else {
95
                                echo 'The information located in includes/config.inc.php is outdated. Please run setup again.';
96
                        }
97
                        die();
98
                }
42 daniel-mar 99
 
100
                // Do redirect stuff etc.
101
                define('OIDPLUS_SSL_AVAILABLE', self::isSslAvailable());
2 daniel-mar 102
        }
42 daniel-mar 103
 
104
        private static function isSslAvailable() {
105
                $timeout = 1;
106
 
107
                if (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == "on")) {
108
                        // we are already on HTTPS
109
                        setcookie('SSL_CHECK', '1', 0, '', '', false, true);
110
                        return true;
111
                } else {
112
                        if (isset($_COOKIE['SSL_CHECK']) && ($_COOKIE['SSL_CHECK'])) {
113
                                // Redirect now
114
                                $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
115
                                header('Location:'.$location);
116
                                die('Redirect to HTTPS');
117
                                return true;
118
                        }
119
 
120
                        if (@fsockopen($_SERVER['HTTP_HOST'], 443, $errno, $errstr, $timeout)) {
121
                                // Redirect now
122
                                setcookie('SSL_CHECK', '1', 0, '', '', false, true);
123
                                $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
124
                                header('Location:'.$location);
125
                                die('Redirect to HTTPS');
126
                                return true;
127
                        } else {
128
                                // Next time, don't try fsockopen
129
                                setcookie('SSL_CHECK', '0', 0, '', '', false, true);
130
                                return false;
131
                        }
132
                }
133
        }
2 daniel-mar 134
}