Subversion Repositories oidplus

Rev

Rev 66 | Rev 74 | 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) {
66 daniel-mar 86
                $ns = $ot::ns();
87
 
88
                if (empty($ns)) die("Attention: Empty NS at $ot\n");
89
 
90
                $ns_found = false;
91
                foreach (OIDplus::getRegisteredObjectTypes() as $test_ot) {
92
                        if ($test_ot::ns() == $ns) {
93
                                $ns_found = true;
94
                                break;
95
                        }
96
                }
97
                if ($ns_found) {
98
                        throw new Exception("Attention: Two objectType plugins use the same namespace \"$ns\"!");
99
                }
100
 
101
                $init = OIDplus::config()->getValue("objecttypes_initialized");
102
                $init_ary = empty($init) ? array() : explode(';', $init);
70 daniel-mar 103
                $init_ary = array_map('trim', $init_ary);
66 daniel-mar 104
 
105
                $enabled = OIDplus::config()->getValue("objecttypes_enabled");
106
                $enabled_ary = empty($enabled) ? array() : explode(';', $enabled);
70 daniel-mar 107
                $enabled_ary = array_map('trim', $enabled_ary);
66 daniel-mar 108
 
109
                if (in_array($ns, $enabled_ary) || !in_array($ns, $init_ary)) {
110
                        self::$objectTypes[] = $ot;
111
                        usort(self::$objectTypes, function($a, $b) {
112
                                $enabled = OIDplus::config()->getValue("objecttypes_enabled");
113
                                $enabled_ary = explode(';', $enabled);
114
 
115
                                $idx_a = array_search($a::ns(), $enabled_ary);
116
                                $idx_b = array_search($b::ns(), $enabled_ary);
117
 
118
                                if ($idx_a == $idx_b) {
119
                                    return 0;
120
                                }
121
                                return ($idx_a > $idx_b) ? +1 : -1;
122
                        });
123
                }
124
 
125
                if (!in_array($ns, $init_ary)) {
126
                        // Was never initialized before, so we add it to the list of enabled object types once
127
 
128
                        $enabled_ary[] = $ns;
129
                        OIDplus::config()->setValue("objecttypes_enabled", implode(';', $enabled_ary));
130
 
131
                        $init_ary[] = $ns;
132
                        OIDplus::config()->setValue("objecttypes_initialized", implode(';', $init_ary));
133
                }
61 daniel-mar 134
        }
135
 
136
        public static function getRegisteredObjectTypes() {
137
                return self::$objectTypes;
138
        }
139
 
2 daniel-mar 140
        public static function init($html=true) {
42 daniel-mar 141
                define('OIDPLUS_HTML_OUTPUT', $html);
142
 
2 daniel-mar 143
                // Include config file
144
                if (file_exists(__DIR__ . '/../config.inc.php')) {
145
                        include_once __DIR__ . '/../config.inc.php';
146
                } else {
147
                        if ($html) {
148
                                if (!is_dir(__DIR__.'/../setup')) {
149
                                        echo 'Error: Setup directory missing.';
150
                                } else {
42 daniel-mar 151
                                        header('Location:setup/');
2 daniel-mar 152
                                }
153
                        } else {
154
                                echo 'Error: Setup directory missing!';
155
                        }
156
                        die();
157
                }
158
 
159
                // Auto-fill non-existing config values
160
                if (!defined('OIDPLUS_CONFIG_VERSION'))   define('OIDPLUS_CONFIG_VERSION',   0.0);
161
                if (!defined('OIDPLUS_ADMIN_PASSWORD'))   define('OIDPLUS_ADMIN_PASSWORD',   '');
162
                if (!defined('OIDPLUS_ADMIN_EMAIL'))      define('OIDPLUS_ADMIN_EMAIL',      '');
163
                if (!defined('OIDPLUS_MYSQL_HOST'))       define('OIDPLUS_MYSQL_HOST',       'localhost');
164
                if (!defined('OIDPLUS_MYSQL_USERNAME'))   define('OIDPLUS_MYSQL_USERNAME',   'root');
165
                if (!defined('OIDPLUS_MYSQL_PASSWORD'))   define('OIDPLUS_MYSQL_PASSWORD',   '');
166
                if (!defined('OIDPLUS_MYSQL_DATABASE'))   define('OIDPLUS_MYSQL_DATABASE',   'oidplus');
167
                if (!defined('OIDPLUS_TABLENAME_PREFIX')) define('OIDPLUS_TABLENAME_PREFIX', '');
168
                if (!defined('OIDPLUS_SESSION_SECRET'))   define('OIDPLUS_SESSION_SECRET',   '');
27 daniel-mar 169
                if (!defined('RECAPTCHA_ENABLED'))        define('RECAPTCHA_ENABLED',        false);
170
                if (!defined('RECAPTCHA_PUBLIC'))         define('RECAPTCHA_PUBLIC',         '');
171
                if (!defined('RECAPTCHA_PRIVATE'))        define('RECAPTCHA_PRIVATE',        '');
2 daniel-mar 172
 
173
                // Check version of the config file
174
                if (OIDPLUS_CONFIG_VERSION != 0.1) {
175
                        if ($html) {
176
                                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>';
177
                        } else {
178
                                echo 'The information located in includes/config.inc.php is outdated. Please run setup again.';
179
                        }
180
                        die();
181
                }
42 daniel-mar 182
 
183
                // Do redirect stuff etc.
184
                define('OIDPLUS_SSL_AVAILABLE', self::isSslAvailable());
61 daniel-mar 185
 
66 daniel-mar 186
                // System config settings
187
 
188
                OIDplus::db()->query("insert into ".OIDPLUS_TABLENAME_PREFIX."config (name, description, value, protected, visible) values ('objecttypes_initialized', 'List of object type plugins that were initialized once', '', 1, 1)");
189
                OIDplus::db()->query("insert into ".OIDPLUS_TABLENAME_PREFIX."config (name, description, value, protected, visible) values ('objecttypes_enabled', 'Enabled object types and their order, separated with a semicolon (please reload the page so that the change is applied)', '', 0, 1)");
190
 
61 daniel-mar 191
                // Register plugins
192
                $ary = glob(__DIR__ . '/../../plugins/publicPages/'.'*'.'/plugin.inc.php');
193
                foreach ($ary as $a) include $a;
194
                $ary = glob(__DIR__ . '/../../plugins/raPages/'.'*'.'/plugin.inc.php');
195
                foreach ($ary as $a) include $a;
196
                $ary = glob(__DIR__ . '/../../plugins/adminPages/'.'*'.'/plugin.inc.php');
197
                foreach ($ary as $a) include $a;
66 daniel-mar 198
                $ary = glob(__DIR__ . '/../../plugins/objectTypes/'.'*'.'/*.class.php');
199
                foreach ($ary as $a) include $a;
2 daniel-mar 200
        }
42 daniel-mar 201
 
202
        private static function isSslAvailable() {
49 daniel-mar 203
                $timeout = 2;
42 daniel-mar 204
 
54 daniel-mar 205
                if (php_sapi_name() == 'cli') return false;
206
 
42 daniel-mar 207
                if (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == "on")) {
208
                        // we are already on HTTPS
209
                        setcookie('SSL_CHECK', '1', 0, '', '', false, true);
210
                        return true;
211
                } else {
49 daniel-mar 212
                        if (isset($_COOKIE['SSL_CHECK'])) {
213
                                // We already had the HTTPS detection done before.
61 daniel-mar 214
                                if ($_COOKIE['SSL_CHECK']) {
49 daniel-mar 215
                                        // HTTPS was detected before, but we are HTTP. Redirect now
216
                                        $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
217
                                        header('Location:'.$location);
218
                                        die('Redirect to HTTPS');
219
                                        return true;
220
                                } else {
221
                                        // No HTTPS available. Do nothing.
222
                                        return false;
223
                                }
42 daniel-mar 224
                        } else {
49 daniel-mar 225
                                // This is our first check (or the browser didn't accept the SSL_CHECK cookie)
226
                                if (@fsockopen($_SERVER['HTTP_HOST'], 443, $errno, $errstr, $timeout)) {
227
                                        // HTTPS detected. Redirect now, and remember that we had detected HTTPS
228
                                        setcookie('SSL_CHECK', '1', 0, '', '', false, true);
229
                                        $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
230
                                        header('Location:'.$location);
231
                                        die('Redirect to HTTPS');
232
                                        return true;
233
                                } else {
234
                                        // No HTTPS detected. Do nothing, and next time, don't try to detect HTTPS again.
235
                                        setcookie('SSL_CHECK', '0', 0, '', '', false, true);
236
                                        return false;
237
                                }
42 daniel-mar 238
                        }
239
                }
240
        }
2 daniel-mar 241
}