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