Subversion Repositories oidplus

Rev

Rev 54 | Rev 66 | 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;
61 daniel-mar 23
        private static /*OIDplusPagePlugin[][]*/ $pagePlugins = array();
24
        private static /*OIDplusObject*/ $objectTypes = array();
2 daniel-mar 25
 
26
        private function __construct() {
27
        }
28
 
29
        public static function db() {
30
                if (is_null(self::$database)) {
31
                        self::$database = new OIDplusDataBaseMySQL();
32
                }
33
                return self::$database;
34
        }
35
 
36
        public static function config() {
37
                if (is_null(self::$config)) {
38
                        self::$config = new OIDplusConfig();
39
                }
40
                return self::$config;
41
        }
42
 
43
        public static function gui() {
44
                return new OIDplusGui();
45
        }
46
 
47
        public static function authUtils() {
48
                return new OIDplusAuthUtils();
49
        }
50
 
51
        public static function system_url() {
52
                return dirname($actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]").'/';
53
        }
54
 
42 daniel-mar 55
        public static function sesHandler() {
56
                return new OIDplusSessionHandler(OIDPLUS_SESSION_SECRET);
57
        }
58
 
61 daniel-mar 59
        public static function registerPagePlugin(OIDplusPagePlugin $plugin) {
60
                $type = $plugin->type();
61
                if ($type === false) return false;
62
 
63
                $prio = $plugin->priority();
64
                if ($prio === false) return false;
65
 
66
                if (!isset(self::$pagePlugins[$type])) self::$pagePlugins[$type] = array();
67
                self::$pagePlugins[$type][$prio] = $plugin;
68
 
69
                return true;
70
        }
71
 
72
        public static function getPagePlugins($type) {
73
                if ($type == '*') {
74
                        $res = array();
75
                        foreach (self::$pagePlugins as $data) {
76
                                $res = array_merge($res, $data);
77
                        }
78
                } else {
79
                        $res = self::$pagePlugins[$type];
80
                }
81
                ksort($res);
82
                return $res;
83
        }
84
 
85
        public static function registerObjectType($ot) {
86
                self::$objectTypes[] = $ot;
87
        }
88
 
89
        public static function getRegisteredObjectTypes() {
90
                return self::$objectTypes;
91
        }
92
 
2 daniel-mar 93
        public static function init($html=true) {
42 daniel-mar 94
                define('OIDPLUS_HTML_OUTPUT', $html);
95
 
2 daniel-mar 96
                // Include config file
97
                if (file_exists(__DIR__ . '/../config.inc.php')) {
98
                        include_once __DIR__ . '/../config.inc.php';
99
                } else {
100
                        if ($html) {
101
                                if (!is_dir(__DIR__.'/../setup')) {
102
                                        echo 'Error: Setup directory missing.';
103
                                } else {
42 daniel-mar 104
                                        header('Location:setup/');
2 daniel-mar 105
                                }
106
                        } else {
107
                                echo 'Error: Setup directory missing!';
108
                        }
109
                        die();
110
                }
111
 
112
                // Auto-fill non-existing config values
113
                if (!defined('OIDPLUS_CONFIG_VERSION'))   define('OIDPLUS_CONFIG_VERSION',   0.0);
114
                if (!defined('OIDPLUS_ADMIN_PASSWORD'))   define('OIDPLUS_ADMIN_PASSWORD',   '');
115
                if (!defined('OIDPLUS_ADMIN_EMAIL'))      define('OIDPLUS_ADMIN_EMAIL',      '');
116
                if (!defined('OIDPLUS_MYSQL_HOST'))       define('OIDPLUS_MYSQL_HOST',       'localhost');
117
                if (!defined('OIDPLUS_MYSQL_USERNAME'))   define('OIDPLUS_MYSQL_USERNAME',   'root');
118
                if (!defined('OIDPLUS_MYSQL_PASSWORD'))   define('OIDPLUS_MYSQL_PASSWORD',   '');
119
                if (!defined('OIDPLUS_MYSQL_DATABASE'))   define('OIDPLUS_MYSQL_DATABASE',   'oidplus');
120
                if (!defined('OIDPLUS_TABLENAME_PREFIX')) define('OIDPLUS_TABLENAME_PREFIX', '');
121
                if (!defined('OIDPLUS_SESSION_SECRET'))   define('OIDPLUS_SESSION_SECRET',   '');
27 daniel-mar 122
                if (!defined('RECAPTCHA_ENABLED'))        define('RECAPTCHA_ENABLED',        false);
123
                if (!defined('RECAPTCHA_PUBLIC'))         define('RECAPTCHA_PUBLIC',         '');
124
                if (!defined('RECAPTCHA_PRIVATE'))        define('RECAPTCHA_PRIVATE',        '');
2 daniel-mar 125
 
126
                // Check version of the config file
127
                if (OIDPLUS_CONFIG_VERSION != 0.1) {
128
                        if ($html) {
129
                                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>';
130
                        } else {
131
                                echo 'The information located in includes/config.inc.php is outdated. Please run setup again.';
132
                        }
133
                        die();
134
                }
42 daniel-mar 135
 
136
                // Do redirect stuff etc.
137
                define('OIDPLUS_SSL_AVAILABLE', self::isSslAvailable());
61 daniel-mar 138
 
139
                // Register plugins
140
                $ary = glob(__DIR__ . '/../../plugins/publicPages/'.'*'.'/plugin.inc.php');
141
                foreach ($ary as $a) include $a;
142
                $ary = glob(__DIR__ . '/../../plugins/raPages/'.'*'.'/plugin.inc.php');
143
                foreach ($ary as $a) include $a;
144
                $ary = glob(__DIR__ . '/../../plugins/adminPages/'.'*'.'/plugin.inc.php');
145
                foreach ($ary as $a) include $a;
2 daniel-mar 146
        }
42 daniel-mar 147
 
148
        private static function isSslAvailable() {
49 daniel-mar 149
                $timeout = 2;
42 daniel-mar 150
 
54 daniel-mar 151
                if (php_sapi_name() == 'cli') return false;
152
 
42 daniel-mar 153
                if (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == "on")) {
154
                        // we are already on HTTPS
155
                        setcookie('SSL_CHECK', '1', 0, '', '', false, true);
156
                        return true;
157
                } else {
49 daniel-mar 158
                        if (isset($_COOKIE['SSL_CHECK'])) {
159
                                // We already had the HTTPS detection done before.
61 daniel-mar 160
                                if ($_COOKIE['SSL_CHECK']) {
49 daniel-mar 161
                                        // HTTPS was detected before, but we are HTTP. Redirect now
162
                                        $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
163
                                        header('Location:'.$location);
164
                                        die('Redirect to HTTPS');
165
                                        return true;
166
                                } else {
167
                                        // No HTTPS available. Do nothing.
168
                                        return false;
169
                                }
42 daniel-mar 170
                        } else {
49 daniel-mar 171
                                // This is our first check (or the browser didn't accept the SSL_CHECK cookie)
172
                                if (@fsockopen($_SERVER['HTTP_HOST'], 443, $errno, $errstr, $timeout)) {
173
                                        // HTTPS detected. Redirect now, and remember that we had detected HTTPS
174
                                        setcookie('SSL_CHECK', '1', 0, '', '', false, true);
175
                                        $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
176
                                        header('Location:'.$location);
177
                                        die('Redirect to HTTPS');
178
                                        return true;
179
                                } else {
180
                                        // No HTTPS detected. Do nothing, and next time, don't try to detect HTTPS again.
181
                                        setcookie('SSL_CHECK', '0', 0, '', '', false, true);
182
                                        return false;
183
                                }
42 daniel-mar 184
                        }
185
                }
186
        }
2 daniel-mar 187
}