Rev 326 | Rev 350 | 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 { |
||
281 | daniel-mar | 21 | private static /*OIDplusPagePlugin[]*/ $pagePlugins = array(); |
22 | private static /*OIDplusAuthPlugin[]*/ $authPlugins = array(); |
||
289 | daniel-mar | 23 | private static /*OIDplusLoggerPlugin[]*/ $loggerPlugins = array(); |
227 | daniel-mar | 24 | private static /*OIDplusObjectTypePlugin[]*/ $objectTypePlugins = array(); |
25 | private static /*string[]*/ $enabledObjectTypes = array(); |
||
26 | private static /*string[]*/ $disabledObjectTypes = array(); |
||
27 | private static /*OIDplusDatabasePlugin[]*/ $dbPlugins = array(); |
||
274 | daniel-mar | 28 | private static /*OIDplusSqlSlangPlugin[]*/ $sqlSlangPlugins = array(); |
2 | daniel-mar | 29 | |
280 | daniel-mar | 30 | protected static $html = true; |
236 | daniel-mar | 31 | |
2 | daniel-mar | 32 | private function __construct() { |
33 | } |
||
295 | daniel-mar | 34 | |
263 | daniel-mar | 35 | # --- Static classes |
274 | daniel-mar | 36 | |
263 | daniel-mar | 37 | private static $baseConfig = null; |
38 | private static $old_config_format = false; |
||
261 | daniel-mar | 39 | public static function baseConfig() { |
263 | daniel-mar | 40 | $first_init = false; |
274 | daniel-mar | 41 | |
263 | daniel-mar | 42 | if ($first_init = is_null(self::$baseConfig)) { |
43 | self::$baseConfig = new OIDplusBaseConfig(); |
||
261 | daniel-mar | 44 | } |
45 | |||
263 | daniel-mar | 46 | if ($first_init) { |
261 | daniel-mar | 47 | // Include a file containing various size/depth limitations of OIDs |
294 | daniel-mar | 48 | // It is important to include it before userdata/baseconfig/config.inc.php was included, |
49 | // so we can give userdata/baseconfig/config.inc.php the chance to override the values. |
||
261 | daniel-mar | 50 | |
294 | daniel-mar | 51 | include OIDplus::basePath().'/includes/limits.inc.php'; |
261 | daniel-mar | 52 | |
53 | // Include config file |
||
295 | daniel-mar | 54 | |
294 | daniel-mar | 55 | $config_file = OIDplus::basePath() . '/userdata/baseconfig/config.inc.php'; |
56 | $config_file_old = OIDplus::basePath() . '/includes/config.inc.php'; // backwards compatibility |
||
295 | daniel-mar | 57 | |
294 | daniel-mar | 58 | if (!file_exists($config_file) && file_exists($config_file_old)) { |
59 | $config_file = $config_file_old; |
||
60 | } |
||
261 | daniel-mar | 61 | |
294 | daniel-mar | 62 | if (file_exists($config_file)) { |
263 | daniel-mar | 63 | if (self::$old_config_format) { |
64 | // Note: We may only include it once due to backwards compatibility, |
||
65 | // since in version 2.0, the configuration was defined using define() statements |
||
66 | // Attention: This does mean that a full re-init (e.g. for test cases) is not possible |
||
67 | // if a version 2.0 config is used! |
||
294 | daniel-mar | 68 | include_once $config_file; |
263 | daniel-mar | 69 | } else { |
294 | daniel-mar | 70 | include $config_file; |
263 | daniel-mar | 71 | } |
261 | daniel-mar | 72 | |
73 | if (defined('OIDPLUS_CONFIG_VERSION') && (OIDPLUS_CONFIG_VERSION == 2.0)) { |
||
263 | daniel-mar | 74 | self::$old_config_format = true; |
274 | daniel-mar | 75 | |
261 | daniel-mar | 76 | // Backwards compatibility 2.0 => 2.1 |
77 | foreach (get_defined_constants(true)['user'] as $name => $value) { |
||
78 | $name = str_replace('OIDPLUS_', '', $name); |
||
79 | if ($name == 'SESSION_SECRET') $name = 'SERVER_SECRET'; |
||
80 | if ($name == 'MYSQL_QUERYLOG') $name = 'QUERY_LOGFILE'; |
||
81 | if (($name == 'MYSQL_PASSWORD') || ($name == 'ODBC_PASSWORD') || ($name == 'PDO_PASSWORD') || ($name == 'PGSQL_PASSWORD')) { |
||
263 | daniel-mar | 82 | self::$baseConfig->setValue($name, base64_decode($value)); |
261 | daniel-mar | 83 | } else { |
84 | if ($name == 'CONFIG_VERSION') $value = 2.1; |
||
263 | daniel-mar | 85 | self::$baseConfig->setValue($name, $value); |
261 | daniel-mar | 86 | } |
87 | } |
||
88 | } |
||
89 | } else { |
||
294 | daniel-mar | 90 | if (!is_dir(OIDplus::basePath().'/setup')) { |
91 | throw new OIDplusConfigInitializationException('File userdata/baseconfig/config.inc.php is missing, but setup can\'t be started because its directory missing.'); |
||
261 | daniel-mar | 92 | } else { |
280 | daniel-mar | 93 | if (self::$html) { |
349 | daniel-mar | 94 | if (strpos($_SERVER['REQUEST_URI'], OIDplus::getSystemUrl(true).'setup/') !== 0) { |
95 | header('Location:'.OIDplus::getSystemUrl().'setup/'); |
||
96 | die('Redirecting to setup...'); |
||
97 | } else { |
||
98 | return self::$baseConfig; |
||
99 | } |
||
261 | daniel-mar | 100 | } else { |
101 | // This can be displayed in e.g. ajax.php |
||
294 | daniel-mar | 102 | throw new OIDplusConfigInitializationException('File userdata/baseconfig/config.inc.php is missing. Please run setup again.'); |
261 | daniel-mar | 103 | } |
104 | } |
||
105 | } |
||
106 | |||
107 | // Check important config settings |
||
108 | |||
263 | daniel-mar | 109 | if (self::$baseConfig->getValue('CONFIG_VERSION') != 2.1) { |
294 | daniel-mar | 110 | throw new OIDplusConfigInitializationException("The information located in $config_file is outdated."); |
261 | daniel-mar | 111 | } |
112 | |||
263 | daniel-mar | 113 | if (self::$baseConfig->getValue('SERVER_SECRET', '') === '') { |
294 | daniel-mar | 114 | throw new OIDplusConfigInitializationException("You must set a value for SERVER_SECRET in $config_file for the system to operate secure."); |
261 | daniel-mar | 115 | } |
116 | } |
||
117 | |||
263 | daniel-mar | 118 | return self::$baseConfig; |
261 | daniel-mar | 119 | } |
120 | |||
263 | daniel-mar | 121 | private static $config = null; |
2 | daniel-mar | 122 | public static function config() { |
263 | daniel-mar | 123 | if ($first_init = is_null(self::$config)) { |
124 | self::$config = new OIDplusConfig(); |
||
2 | daniel-mar | 125 | } |
263 | daniel-mar | 126 | |
127 | if ($first_init) { |
||
128 | // These are important settings for base functionalities and therefore are not inside plugins |
||
129 | self::$config->prepareConfigKey('system_title', 'What is the name of your RA?', 'OIDplus 2.0', OIDplusConfig::PROTECTION_EDITABLE, function($value) { |
||
130 | if (empty($value)) { |
||
131 | throw new OIDplusException("Please enter a value for the system title."); |
||
132 | } |
||
133 | }); |
||
134 | self::$config->prepareConfigKey('admin_email', 'E-Mail address of the system administrator', '', OIDplusConfig::PROTECTION_EDITABLE, function($value) { |
||
135 | if (!empty($value) && !OIDplus::mailUtils()->validMailAddress($value)) { |
||
136 | throw new OIDplusException("This is not a correct email address"); |
||
137 | } |
||
138 | }); |
||
139 | self::$config->prepareConfigKey('global_cc', 'Global CC for all outgoing emails?', '', OIDplusConfig::PROTECTION_EDITABLE, function($value) { |
||
140 | if (!empty($value) && !OIDplus::mailUtils()->validMailAddress($value)) { |
||
141 | throw new OIDplusException("This is not a correct email address"); |
||
142 | } |
||
143 | }); |
||
144 | self::$config->prepareConfigKey('objecttypes_initialized', 'List of object type plugins that were initialized once', '', OIDplusConfig::PROTECTION_READONLY, function($value) { |
||
145 | // Nothing here yet |
||
146 | }); |
||
147 | self::$config->prepareConfigKey('objecttypes_enabled', 'Enabled object types and their order, separated with a semicolon (please reload the page so that the change is applied)', '', OIDplusConfig::PROTECTION_EDITABLE, function($value) { |
||
148 | # TODO: when objecttypes_enabled is changed at the admin control panel, we need to do a reload of the page, so that jsTree will be updated. Is there anything we can do? |
||
149 | |||
150 | $ary = explode(';',$value); |
||
151 | $uniq_ary = array_unique($ary); |
||
152 | |||
153 | if (count($ary) != count($uniq_ary)) { |
||
154 | throw new OIDplusException("Please check your input. Some object types are double."); |
||
155 | } |
||
156 | |||
157 | foreach ($ary as $ot_check) { |
||
158 | $ns_found = false; |
||
159 | foreach (OIDplus::getEnabledObjectTypes() as $ot) { |
||
160 | if ($ot::ns() == $ot_check) { |
||
161 | $ns_found = true; |
||
162 | break; |
||
163 | } |
||
164 | } |
||
165 | foreach (OIDplus::getDisabledObjectTypes() as $ot) { |
||
166 | if ($ot::ns() == $ot_check) { |
||
167 | $ns_found = true; |
||
168 | break; |
||
169 | } |
||
170 | } |
||
171 | if (!$ns_found) { |
||
172 | throw new OIDplusException("Please check your input. Namespace \"$ot_check\" is not found"); |
||
173 | } |
||
174 | } |
||
175 | }); |
||
176 | self::$config->prepareConfigKey('oidplus_private_key', 'Private key for this system', '', OIDplusConfig::PROTECTION_HIDDEN, function($value) { |
||
177 | // Nothing here yet |
||
178 | }); |
||
179 | self::$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.', '', OIDplusConfig::PROTECTION_READONLY, function($value) { |
||
180 | // Nothing here yet |
||
181 | }); |
||
324 | daniel-mar | 182 | self::$config->prepareConfigKey('last_known_system_url', 'Last known System URL', '', OIDplusConfig::PROTECTION_HIDDEN, function($value) { |
183 | // Nothing here yet |
||
184 | }); |
||
263 | daniel-mar | 185 | } |
186 | |||
187 | return self::$config; |
||
2 | daniel-mar | 188 | } |
189 | |||
263 | daniel-mar | 190 | private static $gui = null; |
2 | daniel-mar | 191 | public static function gui() { |
263 | daniel-mar | 192 | if (is_null(self::$gui)) { |
193 | self::$gui = new OIDplusGui(); |
||
86 | daniel-mar | 194 | } |
263 | daniel-mar | 195 | return self::$gui; |
2 | daniel-mar | 196 | } |
197 | |||
263 | daniel-mar | 198 | private static $authUtils = null; |
2 | daniel-mar | 199 | public static function authUtils() { |
263 | daniel-mar | 200 | if (is_null(self::$authUtils)) { |
201 | self::$authUtils = new OIDplusAuthUtils(); |
||
86 | daniel-mar | 202 | } |
263 | daniel-mar | 203 | return self::$authUtils; |
2 | daniel-mar | 204 | } |
205 | |||
263 | daniel-mar | 206 | private static $mailUtils = null; |
250 | daniel-mar | 207 | public static function mailUtils() { |
263 | daniel-mar | 208 | if (is_null(self::$mailUtils)) { |
209 | self::$mailUtils = new OIDplusMailUtils(); |
||
250 | daniel-mar | 210 | } |
263 | daniel-mar | 211 | return self::$mailUtils; |
250 | daniel-mar | 212 | } |
213 | |||
263 | daniel-mar | 214 | private static $menuUtils = null; |
250 | daniel-mar | 215 | public static function menuUtils() { |
263 | daniel-mar | 216 | if (is_null(self::$menuUtils)) { |
217 | self::$menuUtils = new OIDplusMenuUtils(); |
||
250 | daniel-mar | 218 | } |
263 | daniel-mar | 219 | return self::$menuUtils; |
250 | daniel-mar | 220 | } |
221 | |||
263 | daniel-mar | 222 | private static $logger = null; |
115 | daniel-mar | 223 | public static function logger() { |
263 | daniel-mar | 224 | if (is_null(self::$logger)) { |
225 | self::$logger = new OIDplusLogger(); |
||
115 | daniel-mar | 226 | } |
263 | daniel-mar | 227 | return self::$logger; |
115 | daniel-mar | 228 | } |
229 | |||
263 | daniel-mar | 230 | private static $sesHandler = null; |
86 | daniel-mar | 231 | public static function sesHandler() { |
263 | daniel-mar | 232 | if (is_null(self::$sesHandler)) { |
233 | self::$sesHandler = new OIDplusSessionHandler(); |
||
86 | daniel-mar | 234 | } |
263 | daniel-mar | 235 | return self::$sesHandler; |
86 | daniel-mar | 236 | } |
237 | |||
274 | daniel-mar | 238 | # --- SQL slang plugin |
239 | |||
240 | private static function registerSqlSlangPlugin(OIDplusSqlSlangPlugin $plugin) { |
||
241 | $name = $plugin::id(); |
||
242 | if ($name === false) return false; |
||
243 | |||
244 | self::$sqlSlangPlugins[$name] = $plugin; |
||
245 | |||
246 | return true; |
||
247 | } |
||
248 | |||
249 | public static function getSqlSlangPlugins() { |
||
250 | return self::$sqlSlangPlugins; |
||
251 | } |
||
252 | |||
318 | daniel-mar | 253 | public static function getSqlSlangPlugin($id)/*: ?OIDplusSqlSlangPlugin*/ { |
254 | if (isset(self::$sqlSlangPlugins[$id])) { |
||
255 | return self::$sqlSlangPlugins[$id]; |
||
256 | } else { |
||
257 | return null; |
||
258 | } |
||
259 | } |
||
260 | |||
230 | daniel-mar | 261 | # --- Database plugin |
74 | daniel-mar | 262 | |
227 | daniel-mar | 263 | private static function registerDatabasePlugin(OIDplusDatabasePlugin $plugin) { |
275 | daniel-mar | 264 | $name = $plugin::id(); |
150 | daniel-mar | 265 | if ($name === false) return false; |
266 | |||
267 | self::$dbPlugins[$name] = $plugin; |
||
268 | |||
269 | return true; |
||
270 | } |
||
271 | |||
272 | public static function getDatabasePlugins() { |
||
273 | return self::$dbPlugins; |
||
274 | } |
||
275 | |||
295 | daniel-mar | 276 | public static function getActiveDatabasePlugin() { |
261 | daniel-mar | 277 | if (OIDplus::baseConfig()->getValue('DATABASE_PLUGIN', '') === '') { |
260 | daniel-mar | 278 | throw new OIDplusConfigInitializationException("No database plugin selected in config file"); |
279 | } |
||
261 | daniel-mar | 280 | if (!isset(self::$dbPlugins[OIDplus::baseConfig()->getValue('DATABASE_PLUGIN')])) { |
281 | throw new OIDplusConfigInitializationException("Database plugin '".OIDplus::baseConfig()->getValue('DATABASE_PLUGIN')."' not found"); |
||
227 | daniel-mar | 282 | } |
295 | daniel-mar | 283 | return self::$dbPlugins[OIDplus::baseConfig()->getValue('DATABASE_PLUGIN')]; |
227 | daniel-mar | 284 | } |
285 | |||
295 | daniel-mar | 286 | private static $dbMainSession = null; |
287 | public static function db() { |
||
288 | if (is_null(self::$dbMainSession)) { |
||
289 | self::$dbMainSession = self::getActiveDatabasePlugin()->newConnection(); |
||
290 | } |
||
291 | if (!self::$dbMainSession->isConnected()) self::$dbMainSession->connect(); |
||
292 | return self::$dbMainSession; |
||
293 | } |
||
294 | |||
295 | private static $dbIsolatedSession = null; |
||
296 | public static function dbIsolated() { |
||
297 | if (is_null(self::$dbIsolatedSession)) { |
||
298 | self::$dbIsolatedSession = self::getActiveDatabasePlugin()->newConnection(); |
||
299 | } |
||
300 | if (!self::$dbIsolatedSession->isConnected()) self::$dbIsolatedSession->connect(); |
||
301 | return self::$dbIsolatedSession; |
||
302 | } |
||
303 | |||
227 | daniel-mar | 304 | # --- Page plugin |
305 | |||
224 | daniel-mar | 306 | private static function registerPagePlugin(OIDplusPagePlugin $plugin) { |
281 | daniel-mar | 307 | self::$pagePlugins[] = $plugin; |
61 | daniel-mar | 308 | |
309 | return true; |
||
310 | } |
||
311 | |||
281 | daniel-mar | 312 | public static function getPagePlugins() { |
313 | return self::$pagePlugins; |
||
61 | daniel-mar | 314 | } |
315 | |||
227 | daniel-mar | 316 | # --- Auth plugin |
317 | |||
318 | private static function registerAuthPlugin(OIDplusAuthPlugin $plugin) { |
||
319 | self::$authPlugins[] = $plugin; |
||
320 | return true; |
||
321 | } |
||
322 | |||
221 | daniel-mar | 323 | public static function getAuthPlugins() { |
324 | return self::$authPlugins; |
||
325 | } |
||
326 | |||
289 | daniel-mar | 327 | # --- Logger plugin |
328 | |||
329 | private static function registerLoggerPlugin(OIDplusLoggerPlugin $plugin) { |
||
330 | self::$loggerPlugins[] = $plugin; |
||
331 | return true; |
||
332 | } |
||
333 | |||
334 | public static function getLoggerPlugins() { |
||
335 | return self::$loggerPlugins; |
||
336 | } |
||
337 | |||
227 | daniel-mar | 338 | # --- Object type plugin |
339 | |||
340 | private static function registerObjectTypePlugin(OIDplusObjectTypePlugin $plugin) { |
||
341 | self::$objectTypePlugins[] = $plugin; |
||
342 | |||
343 | $ot = $plugin::getObjectTypeClassName(); |
||
344 | self::registerObjectType($ot); |
||
345 | |||
346 | return true; |
||
347 | } |
||
348 | |||
224 | daniel-mar | 349 | private static function registerObjectType($ot) { |
66 | daniel-mar | 350 | $ns = $ot::ns(); |
351 | |||
250 | daniel-mar | 352 | if (empty($ns)) throw new OIDplusException("Attention: Empty NS at $ot\n"); |
66 | daniel-mar | 353 | |
354 | $ns_found = false; |
||
227 | daniel-mar | 355 | foreach (array_merge(OIDplus::getEnabledObjectTypes(), OIDplus::getDisabledObjectTypes()) as $test_ot) { |
66 | daniel-mar | 356 | if ($test_ot::ns() == $ns) { |
357 | $ns_found = true; |
||
358 | break; |
||
359 | } |
||
360 | } |
||
361 | if ($ns_found) { |
||
250 | daniel-mar | 362 | throw new OIDplusException("Attention: Two objectType plugins use the same namespace \"$ns\"!"); |
66 | daniel-mar | 363 | } |
364 | |||
365 | $init = OIDplus::config()->getValue("objecttypes_initialized"); |
||
366 | $init_ary = empty($init) ? array() : explode(';', $init); |
||
70 | daniel-mar | 367 | $init_ary = array_map('trim', $init_ary); |
66 | daniel-mar | 368 | |
369 | $enabled = OIDplus::config()->getValue("objecttypes_enabled"); |
||
370 | $enabled_ary = empty($enabled) ? array() : explode(';', $enabled); |
||
70 | daniel-mar | 371 | $enabled_ary = array_map('trim', $enabled_ary); |
66 | daniel-mar | 372 | |
79 | daniel-mar | 373 | $do_enable = false; |
374 | if (in_array($ns, $enabled_ary)) { |
||
375 | $do_enable = true; |
||
376 | } else { |
||
377 | if (!OIDplus::config()->getValue('registration_done')) { |
||
378 | $do_enable = $ns == 'oid'; |
||
379 | } else { |
||
380 | $do_enable = !in_array($ns, $init_ary); |
||
381 | } |
||
382 | } |
||
383 | |||
384 | if ($do_enable) { |
||
227 | daniel-mar | 385 | self::$enabledObjectTypes[] = $ot; |
386 | usort(self::$enabledObjectTypes, function($a, $b) { |
||
66 | daniel-mar | 387 | $enabled = OIDplus::config()->getValue("objecttypes_enabled"); |
388 | $enabled_ary = explode(';', $enabled); |
||
389 | |||
390 | $idx_a = array_search($a::ns(), $enabled_ary); |
||
391 | $idx_b = array_search($b::ns(), $enabled_ary); |
||
392 | |||
393 | if ($idx_a == $idx_b) { |
||
394 | return 0; |
||
395 | } |
||
396 | return ($idx_a > $idx_b) ? +1 : -1; |
||
397 | }); |
||
74 | daniel-mar | 398 | } else { |
399 | self::$disabledObjectTypes[] = $ot; |
||
66 | daniel-mar | 400 | } |
401 | |||
402 | if (!in_array($ns, $init_ary)) { |
||
403 | // Was never initialized before, so we add it to the list of enabled object types once |
||
404 | |||
79 | daniel-mar | 405 | if ($do_enable) { |
406 | $enabled_ary[] = $ns; |
||
407 | OIDplus::config()->setValue("objecttypes_enabled", implode(';', $enabled_ary)); |
||
408 | } |
||
66 | daniel-mar | 409 | |
410 | $init_ary[] = $ns; |
||
411 | OIDplus::config()->setValue("objecttypes_initialized", implode(';', $init_ary)); |
||
412 | } |
||
61 | daniel-mar | 413 | } |
414 | |||
227 | daniel-mar | 415 | public static function getObjectTypePlugins() { |
416 | return self::$objectTypePlugins; |
||
61 | daniel-mar | 417 | } |
418 | |||
227 | daniel-mar | 419 | public static function getObjectTypePluginsEnabled() { |
420 | $res = array(); |
||
421 | foreach (self::$objectTypePlugins as $plugin) { |
||
422 | $ot = $plugin::getObjectTypeClassName(); |
||
423 | if (in_array($ot, self::$enabledObjectTypes)) $res[] = $plugin; |
||
424 | } |
||
425 | return $res; |
||
74 | daniel-mar | 426 | } |
427 | |||
227 | daniel-mar | 428 | public static function getObjectTypePluginsDisabled() { |
429 | $res = array(); |
||
430 | foreach (self::$objectTypePlugins as $plugin) { |
||
431 | $ot = $plugin::getObjectTypeClassName(); |
||
432 | if (in_array($ot, self::$disabledObjectTypes)) $res[] = $plugin; |
||
74 | daniel-mar | 433 | } |
227 | daniel-mar | 434 | return $res; |
74 | daniel-mar | 435 | } |
436 | |||
227 | daniel-mar | 437 | public static function getEnabledObjectTypes() { |
438 | return self::$enabledObjectTypes; |
||
439 | } |
||
74 | daniel-mar | 440 | |
227 | daniel-mar | 441 | public static function getDisabledObjectTypes() { |
442 | return self::$disabledObjectTypes; |
||
443 | } |
||
74 | daniel-mar | 444 | |
277 | daniel-mar | 445 | # --- Plugin handling functions |
446 | |||
320 | daniel-mar | 447 | public static function getAllPlugins()/*: array*/ { |
448 | $res = array(); |
||
449 | $res = array_merge($res, self::$pagePlugins); |
||
450 | $res = array_merge($res, self::$authPlugins); |
||
451 | $res = array_merge($res, self::$loggerPlugins); |
||
452 | $res = array_merge($res, self::$objectTypePlugins); |
||
453 | $res = array_merge($res, self::$dbPlugins); |
||
454 | $res = array_merge($res, self::$sqlSlangPlugins); |
||
455 | return $res; |
||
456 | } |
||
457 | |||
321 | daniel-mar | 458 | public static function getPluginByOid($oid)/*: ?OIDplusPlugin*/ { |
320 | daniel-mar | 459 | $plugins = self::getAllPlugins(); |
321 | daniel-mar | 460 | foreach ($plugins as $plugin) { |
461 | if (oid_dotnotation_equal($plugin->getManifest()->getOid(), $oid)) { |
||
462 | return $plugin; |
||
320 | daniel-mar | 463 | } |
464 | } |
||
465 | return null; |
||
466 | } |
||
467 | |||
307 | daniel-mar | 468 | public static function getPluginManifest($class_name)/*: ?OIDplusPluginManifest*/ { |
277 | daniel-mar | 469 | $reflector = new ReflectionClass($class_name); |
308 | daniel-mar | 470 | $ini = dirname($reflector->getFileName()).'/manifest.xml'; |
307 | daniel-mar | 471 | $manifest = new OIDplusPluginManifest(); |
472 | return $manifest->loadManifest($ini) ? $manifest : null; |
||
277 | daniel-mar | 473 | } |
474 | |||
307 | daniel-mar | 475 | public static function getAllPluginManifests($pluginFolderMask='*', $flat=true): array { |
277 | daniel-mar | 476 | $out = array(); |
279 | daniel-mar | 477 | // Note: glob() will sort by default, so we do not need a page priority attribute. |
478 | // So you just need to use a numeric plugin directory prefix (padded). |
||
308 | daniel-mar | 479 | $ary = glob(OIDplus::basePath().'/plugins/'.$pluginFolderMask.'/'.'*'.'/manifest.xml'); |
277 | daniel-mar | 480 | foreach ($ary as $ini) { |
481 | if (!file_exists($ini)) continue; |
||
482 | |||
307 | daniel-mar | 483 | $manifest = new OIDplusPluginManifest(); |
484 | $manifest->loadManifest($ini); |
||
277 | daniel-mar | 485 | |
307 | daniel-mar | 486 | if ($flat) { |
487 | $out[] = $manifest; |
||
488 | } else { |
||
489 | $plugintype_folder = basename(dirname(dirname($ini))); |
||
490 | $pluginname_folder = basename(dirname($ini)); |
||
491 | |||
492 | if (!isset($out[$plugintype_folder])) $out[$plugintype_folder] = array(); |
||
493 | if (!isset($out[$plugintype_folder][$pluginname_folder])) $out[$plugintype_folder][$pluginname_folder] = array(); |
||
494 | $out[$plugintype_folder][$pluginname_folder] = $manifest; |
||
495 | } |
||
277 | daniel-mar | 496 | } |
497 | return $out; |
||
498 | } |
||
499 | |||
500 | public static function registerAllPlugins($pluginDirName, $expectedPluginClass, $registerCallback): array { |
||
501 | $out = array(); |
||
307 | daniel-mar | 502 | $ary = self::getAllPluginManifests($pluginDirName, false); |
320 | daniel-mar | 503 | $known_plugin_oids = array(); |
277 | daniel-mar | 504 | foreach ($ary as $plugintype_folder => $bry) { |
505 | foreach ($bry as $pluginname_folder => $cry) { |
||
307 | daniel-mar | 506 | $class_name = $cry->getPhpMainClass(); |
507 | if (!$class_name) { |
||
508 | throw new OIDplusException("Plugin '$plugintype_folder/$pluginname_folder' is errornous: Manifest does not declare a PHP main class"); |
||
277 | daniel-mar | 509 | } |
297 | daniel-mar | 510 | if (OIDplus::baseConfig()->getValue('DISABLE_PLUGIN_'.$class_name, false)) { |
511 | continue; |
||
512 | } |
||
292 | daniel-mar | 513 | if (!class_exists($class_name)) { |
307 | daniel-mar | 514 | throw new OIDplusException("Plugin '$plugintype_folder/$pluginname_folder' is errornous: Manifest declares PHP main class as '$class_name', but it could not be found"); |
292 | daniel-mar | 515 | } |
279 | daniel-mar | 516 | if (!is_subclass_of($class_name, $expectedPluginClass)) { |
307 | daniel-mar | 517 | throw new OIDplusException("Plugin '$plugintype_folder/$pluginname_folder' is errornous: Plugin main class '$class_name' is expected to be a subclass of '$expectedPluginClass'"); |
279 | daniel-mar | 518 | } |
308 | daniel-mar | 519 | if (($class_name!=$cry->getTypeClass()) && (!is_subclass_of($class_name,$cry->getTypeClass()))) { |
520 | throw new OIDplusException("Plugin '$plugintype_folder/$pluginname_folder' is errornous: Plugin main class '$class_name' is expected to be a subclass of '".$cry->getTypeClass()."', according to type declared in manifest"); |
||
521 | } |
||
522 | if (($cry->getTypeClass()!=$expectedPluginClass) && (!is_subclass_of($cry->getTypeClass(),$expectedPluginClass))) { |
||
523 | throw new OIDplusException("Plugin '$plugintype_folder/$pluginname_folder' is errornous: Class declared in manifest is '".$cry->getTypeClasS()."' does not fit expected class for this plugin type '$expectedPluginClass'"); |
||
524 | } |
||
525 | |||
320 | daniel-mar | 526 | $plugin_oid = $cry->getOid(); |
527 | if (!$plugin_oid) { |
||
528 | throw new OIDplusException("Plugin '$plugintype_folder/$pluginname_folder' is errornous: Does not have an OID"); |
||
529 | } |
||
530 | if (!oid_valid_dotnotation($plugin_oid, false, false, 2)) { |
||
531 | throw new OIDplusException("Plugin '$plugintype_folder/$pluginname_folder' is errornous: Plugin OID '$plugin_oid' is invalid (needs to be valid dot-notation)"); |
||
532 | } |
||
533 | if (isset($known_plugin_oids[$plugin_oid])) { |
||
534 | throw new OIDplusException("Plugin '$plugintype_folder/$pluginname_folder' is errornous: The OID '$plugin_oid' is already used by the plugin '".$known_plugin_oids[$plugin_oid]."'"); |
||
535 | } else { |
||
536 | $known_plugin_oids[$plugin_oid] = $plugintype_folder.'/'.$pluginname_folder; |
||
537 | } |
||
538 | |||
279 | daniel-mar | 539 | $out[] = $class_name; |
540 | if (!is_null($registerCallback)) { |
||
541 | call_user_func($registerCallback, new $class_name()); |
||
542 | } |
||
277 | daniel-mar | 543 | } |
544 | |||
545 | } |
||
546 | return $out; |
||
547 | } |
||
548 | |||
227 | daniel-mar | 549 | # --- Initialization of OIDplus |
206 | daniel-mar | 550 | |
2 | daniel-mar | 551 | public static function init($html=true) { |
236 | daniel-mar | 552 | self::$html = $html; |
553 | |||
263 | daniel-mar | 554 | // Reset internal state, so we can re-init verything if required |
274 | daniel-mar | 555 | |
263 | daniel-mar | 556 | if (self::$old_config_format) { |
557 | // Note: This can only happen in very special cases (e.g. test cases) where you call init() twice |
||
558 | throw new OIDplusConfigInitializationException('A full re-initialization is not possible if a version 2.0 config file (containing "defines") is used. Please update to a config 2.1 file by running setup again.'); |
||
559 | } |
||
274 | daniel-mar | 560 | |
263 | daniel-mar | 561 | self::$config = null; |
562 | self::$baseConfig = null; |
||
563 | self::$gui = null; |
||
564 | self::$authUtils = null; |
||
565 | self::$mailUtils = null; |
||
566 | self::$menuUtils = null; |
||
567 | self::$logger = null; |
||
568 | self::$sesHandler = null; |
||
295 | daniel-mar | 569 | self::$dbMainSession = null; |
570 | self::$dbIsolatedSession = null; |
||
263 | daniel-mar | 571 | self::$pagePlugins = array(); |
572 | self::$authPlugins = array(); |
||
289 | daniel-mar | 573 | self::$loggerPlugins = array(); |
263 | daniel-mar | 574 | self::$objectTypePlugins = array(); |
575 | self::$enabledObjectTypes = array(); |
||
576 | self::$disabledObjectTypes = array(); |
||
577 | self::$dbPlugins = array(); |
||
274 | daniel-mar | 578 | self::$sqlSlangPlugins = array(); |
263 | daniel-mar | 579 | self::$system_id_cache = null; |
580 | self::$sslAvailableCache = null; |
||
74 | daniel-mar | 581 | |
263 | daniel-mar | 582 | // Continue... |
583 | |||
294 | daniel-mar | 584 | OIDplus::baseConfig(); // this loads the base configuration located in userdata/baseconfig/config.inc.php (once!) |
263 | daniel-mar | 585 | // You can do changes to the configuration afterwards using OIDplus::baseConfig()->... |
586 | |||
150 | daniel-mar | 587 | // Register database types (highest priority) |
588 | |||
274 | daniel-mar | 589 | // SQL slangs |
590 | |||
294 | daniel-mar | 591 | self::registerAllPlugins('sqlSlang', 'OIDplusSqlSlangPlugin', array('OIDplus','registerSqlSlangPlugin')); |
274 | daniel-mar | 592 | foreach (OIDplus::getSqlSlangPlugins() as $plugin) { |
593 | $plugin->init($html); |
||
594 | } |
||
595 | |||
596 | // Database providers |
||
597 | |||
277 | daniel-mar | 598 | self::registerAllPlugins('database', 'OIDplusDatabasePlugin', array('OIDplus','registerDatabasePlugin')); |
237 | daniel-mar | 599 | foreach (OIDplus::getDatabasePlugins() as $plugin) { |
600 | $plugin->init($html); |
||
601 | } |
||
602 | |||
42 | daniel-mar | 603 | // Do redirect stuff etc. |
74 | daniel-mar | 604 | |
230 | daniel-mar | 605 | self::isSslAvailable(); // This function does automatic redirects |
61 | daniel-mar | 606 | |
263 | daniel-mar | 607 | // Construct the configuration manager |
66 | daniel-mar | 608 | |
263 | daniel-mar | 609 | OIDplus::config(); // During the construction, various system settings are prepared if required |
66 | daniel-mar | 610 | |
74 | daniel-mar | 611 | // Initialize public / private keys |
612 | |||
227 | daniel-mar | 613 | OIDplus::getPkiStatus(true); |
74 | daniel-mar | 614 | |
237 | daniel-mar | 615 | // Register non-DB plugins |
74 | daniel-mar | 616 | |
277 | daniel-mar | 617 | self::registerAllPlugins('*Pages', 'OIDplusPagePlugin', array('OIDplus','registerPagePlugin')); |
618 | self::registerAllPlugins('auth', 'OIDplusAuthPlugin', array('OIDplus','registerAuthPlugin')); |
||
289 | daniel-mar | 619 | self::registerAllPlugins('logger', 'OIDplusLoggerPlugin', array('OIDplus','registerLoggerPlugin')); |
277 | daniel-mar | 620 | self::registerAllPlugins('objectTypes', 'OIDplusObjectTypePlugin', array('OIDplus','registerObjectTypePlugin')); |
150 | daniel-mar | 621 | |
237 | daniel-mar | 622 | // Initialize non-DB plugins |
224 | daniel-mar | 623 | |
281 | daniel-mar | 624 | foreach (OIDplus::getPagePlugins() as $plugin) { |
74 | daniel-mar | 625 | $plugin->init($html); |
626 | } |
||
230 | daniel-mar | 627 | foreach (OIDplus::getAuthPlugins() as $plugin) { |
628 | $plugin->init($html); |
||
629 | } |
||
289 | daniel-mar | 630 | foreach (OIDplus::getLoggerPlugins() as $plugin) { |
631 | $plugin->init($html); |
||
632 | } |
||
230 | daniel-mar | 633 | foreach (OIDplus::getObjectTypePlugins() as $plugin) { |
634 | $plugin->init($html); |
||
635 | } |
||
2 | daniel-mar | 636 | } |
42 | daniel-mar | 637 | |
227 | daniel-mar | 638 | # --- System URL, System ID, PKI, and other functions |
639 | |||
294 | daniel-mar | 640 | public static function basePath() { |
641 | return realpath(__DIR__ . '/../../'); |
||
642 | } |
||
643 | |||
227 | daniel-mar | 644 | public static function getSystemUrl($relative=false) { |
326 | daniel-mar | 645 | if (!$relative) { |
646 | $res = OIDplus::baseConfig()->getValue('EXPLICIT_ABSOLUTE_SYSTEM_URL', ''); |
||
647 | if ($res !== '') { |
||
349 | daniel-mar | 648 | try { |
649 | OIDplus::config()->setValue('last_known_system_url', $res); |
||
650 | return $res; |
||
651 | } catch (Exception $e) { |
||
652 | } |
||
326 | daniel-mar | 653 | } |
654 | } |
||
655 | |||
227 | daniel-mar | 656 | if (!isset($_SERVER["SCRIPT_NAME"])) return false; |
657 | |||
658 | $test_dir = dirname($_SERVER['SCRIPT_FILENAME']); |
||
269 | daniel-mar | 659 | $test_dir = str_replace('\\', '/', $test_dir); |
227 | daniel-mar | 660 | $c = 0; |
661 | while (!file_exists($test_dir.'/oidplus_base.js')) { |
||
662 | $test_dir = dirname($test_dir); |
||
663 | $c++; |
||
664 | if ($c == 1000) return false; |
||
665 | } |
||
666 | |||
667 | $res = dirname($_SERVER['SCRIPT_NAME'].'xxx'); |
||
668 | |||
669 | for ($i=1; $i<=$c; $i++) { |
||
670 | $res = dirname($res); |
||
671 | } |
||
672 | |||
269 | daniel-mar | 673 | $res = str_replace('\\', '/', $res); |
227 | daniel-mar | 674 | if ($res == '/') $res = ''; |
675 | $res .= '/'; |
||
676 | |||
677 | if (!$relative) { |
||
315 | daniel-mar | 678 | if (php_sapi_name() == 'cli') { |
349 | daniel-mar | 679 | try { |
680 | return OIDplus::config()->getValue('last_known_system_url', false); |
||
681 | } catch (Exception $e) { |
||
682 | } |
||
315 | daniel-mar | 683 | } |
684 | |||
228 | daniel-mar | 685 | $is_ssl = isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] === 'on'); |
686 | $protocol = $is_ssl ? 'https' : 'http'; |
||
687 | $host = $_SERVER['HTTP_HOST']; |
||
688 | $port = $_SERVER['SERVER_PORT']; |
||
689 | if ($is_ssl && ($port != 443)) { |
||
690 | $port_add = ":$port"; |
||
691 | } else if (!$is_ssl && ($port != 80)) { |
||
692 | $port_add = ":$port"; |
||
693 | } else { |
||
694 | $port_add = ""; |
||
695 | } |
||
696 | $res = $protocol.'://'.$host.$port_add.$res; |
||
324 | daniel-mar | 697 | |
349 | daniel-mar | 698 | try { |
699 | OIDplus::config()->setValue('last_known_system_url', $res); |
||
700 | } catch (Exception $e) { |
||
701 | } |
||
227 | daniel-mar | 702 | } |
703 | |||
704 | return $res; |
||
705 | } |
||
706 | |||
707 | private static $system_id_cache = null; |
||
708 | public static function getSystemId($oid=false) { |
||
709 | if (!is_null(self::$system_id_cache)) { |
||
710 | $out = self::$system_id_cache; |
||
711 | } else { |
||
712 | $out = false; |
||
713 | |||
714 | if (self::getPkiStatus(true)) { |
||
715 | $pubKey = OIDplus::config()->getValue('oidplus_public_key'); |
||
716 | if (preg_match('@BEGIN PUBLIC KEY\-+(.+)\-+END PUBLIC KEY@ismU', $pubKey, $m)) { |
||
717 | $out = smallhash(base64_decode($m[1])); |
||
718 | } |
||
719 | } |
||
720 | self::$system_id_cache = $out; |
||
721 | } |
||
291 | daniel-mar | 722 | return ($oid ? '1.3.6.1.4.1.37476.30.9.' : '').$out; |
227 | daniel-mar | 723 | } |
724 | |||
725 | public static function getPkiStatus($try_generate=true) { |
||
726 | if (!function_exists('openssl_pkey_new')) return false; |
||
727 | |||
728 | $privKey = OIDplus::config()->getValue('oidplus_private_key'); |
||
729 | $pubKey = OIDplus::config()->getValue('oidplus_public_key'); |
||
730 | |||
731 | if ($try_generate && !verify_private_public_key($privKey, $pubKey)) { |
||
263 | daniel-mar | 732 | $pkey_config = array( |
227 | daniel-mar | 733 | "digest_alg" => "sha512", |
734 | "private_key_bits" => 2048, |
||
735 | "private_key_type" => OPENSSL_KEYTYPE_RSA, |
||
736 | ); |
||
737 | |||
738 | // Create the private and public key |
||
263 | daniel-mar | 739 | $res = openssl_pkey_new($pkey_config); |
256 | daniel-mar | 740 | |
239 | daniel-mar | 741 | if (!$res) return false; |
227 | daniel-mar | 742 | |
743 | // Extract the private key from $res to $privKey |
||
744 | openssl_pkey_export($res, $privKey); |
||
745 | |||
746 | // Extract the public key from $res to $pubKey |
||
747 | $pubKey = openssl_pkey_get_details($res)["key"]; |
||
748 | |||
239 | daniel-mar | 749 | // Log |
288 | daniel-mar | 750 | OIDplus::logger()->log("[INFO]A!", "Generating new SystemID using a new key pair"); |
239 | daniel-mar | 751 | |
227 | daniel-mar | 752 | // Save the key pair to database |
753 | OIDplus::config()->setValue('oidplus_private_key', $privKey); |
||
754 | OIDplus::config()->setValue('oidplus_public_key', $pubKey); |
||
755 | |||
756 | // Log the new system ID |
||
757 | if (preg_match('@BEGIN PUBLIC KEY\-+(.+)\-+END PUBLIC KEY@ismU', $pubKey, $m)) { |
||
758 | $system_id = smallhash(base64_decode($m[1])); |
||
288 | daniel-mar | 759 | OIDplus::logger()->log("[INFO]A!", "Your SystemID is now $system_id"); |
227 | daniel-mar | 760 | } |
761 | } |
||
762 | |||
763 | return verify_private_public_key($privKey, $pubKey); |
||
764 | } |
||
765 | |||
170 | daniel-mar | 766 | public static function getInstallType() { |
294 | daniel-mar | 767 | if (!file_exists(OIDplus::basePath().'/oidplus_version.txt') && !is_dir(OIDplus::basePath().'/.svn')) { |
170 | daniel-mar | 768 | return 'unknown'; |
769 | } |
||
294 | daniel-mar | 770 | if (file_exists(OIDplus::basePath().'/oidplus_version.txt') && is_dir(OIDplus::basePath().'/.svn')) { |
170 | daniel-mar | 771 | return 'ambigous'; |
772 | } |
||
294 | daniel-mar | 773 | if (is_dir(OIDplus::basePath().'/.svn')) { |
170 | daniel-mar | 774 | return 'svn-wc'; |
775 | } |
||
294 | daniel-mar | 776 | if (file_exists(OIDplus::basePath().'/oidplus_version.txt')) { |
170 | daniel-mar | 777 | return 'svn-snapshot'; |
778 | } |
||
779 | } |
||
780 | |||
111 | daniel-mar | 781 | public static function getVersion() { |
294 | daniel-mar | 782 | if (file_exists(OIDplus::basePath().'/oidplus_version.txt') && is_dir(OIDplus::basePath().'/.svn')) { |
239 | daniel-mar | 783 | return false; // version is ambigous |
111 | daniel-mar | 784 | } |
162 | daniel-mar | 785 | |
294 | daniel-mar | 786 | if (is_dir(OIDplus::basePath().'/.svn')) { |
285 | daniel-mar | 787 | // Try to get the version via SQLite3 |
788 | if (class_exists('SQLite3')) { |
||
789 | try { |
||
294 | daniel-mar | 790 | $db = new SQLite3(OIDplus::basePath().'/.svn/wc.db'); |
285 | daniel-mar | 791 | $results = $db->query('SELECT MIN(revision) AS rev FROM NODES_BASE'); |
792 | while ($row = $results->fetchArray()) { |
||
793 | return 'svn-'.$row['rev']; |
||
794 | } |
||
795 | $db->close(); |
||
796 | $db = null; |
||
797 | } catch (Exception $e) { |
||
798 | } |
||
799 | } |
||
800 | if (class_exists('PDO')) { |
||
801 | try { |
||
294 | daniel-mar | 802 | $pdo = new PDO('sqlite:' . OIDplus::basePath().'/.svn/wc.db'); |
285 | daniel-mar | 803 | $res = $pdo->query('SELECT MIN(revision) AS rev FROM NODES_BASE'); |
804 | $row = $res->fetch(); |
||
805 | if ($row !== false) return 'svn-'.$row['rev']; |
||
806 | $pdo = null; |
||
807 | } catch (Exception $e) { |
||
808 | } |
||
809 | } |
||
810 | |||
162 | daniel-mar | 811 | // Try to find out the SVN version using the shell |
285 | daniel-mar | 812 | // We don't prioritize this method, because a failed shell access will flood the apache error log with STDERR messages |
294 | daniel-mar | 813 | $output = @shell_exec('svnversion '.escapeshellarg(OIDplus::basePath())); |
285 | daniel-mar | 814 | if (preg_match('/\d+/', $output, $match)) { |
239 | daniel-mar | 815 | return 'svn-'.$match[0]; |
162 | daniel-mar | 816 | } |
817 | |||
294 | daniel-mar | 818 | $output = @shell_exec('svn info '.escapeshellarg(OIDplus::basePath())); |
285 | daniel-mar | 819 | if (preg_match('/Revision:\s*(\d+)/m', $output, $match)) { |
820 | return 'svn-'.$match[1]; |
||
162 | daniel-mar | 821 | } |
822 | } |
||
823 | |||
294 | daniel-mar | 824 | if (file_exists(OIDplus::basePath().'/oidplus_version.txt')) { |
825 | $cont = file_get_contents(OIDplus::basePath().'/oidplus_version.txt'); |
||
239 | daniel-mar | 826 | if (preg_match('@Revision (\d+)@', $cont, $m)) |
827 | return 'svn-'.$m[1]; |
||
162 | daniel-mar | 828 | } |
829 | |||
239 | daniel-mar | 830 | return false; |
111 | daniel-mar | 831 | } |
832 | |||
230 | daniel-mar | 833 | private static $sslAvailableCache = null; |
834 | public static function isSslAvailable() { |
||
835 | if (!is_null(self::$sslAvailableCache)) return self::$sslAvailableCache; |
||
256 | daniel-mar | 836 | |
230 | daniel-mar | 837 | if (php_sapi_name() == 'cli') { |
838 | self::$sslAvailableCache = false; |
||
839 | return false; |
||
840 | } |
||
841 | |||
49 | daniel-mar | 842 | $timeout = 2; |
80 | daniel-mar | 843 | $already_ssl = isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == "on"); |
844 | $ssl_port = 443; |
||
227 | daniel-mar | 845 | $cookie_path = OIDplus::getSystemUrl(true); |
83 | daniel-mar | 846 | if (empty($cookie_path)) $cookie_path = '/'; |
42 | daniel-mar | 847 | |
261 | daniel-mar | 848 | $mode = OIDplus::baseConfig()->getValue('ENFORCE_SSL', 2/*auto*/); |
849 | |||
850 | if ($mode == 0) { |
||
80 | daniel-mar | 851 | // No SSL available |
230 | daniel-mar | 852 | self::$sslAvailableCache = $already_ssl; |
80 | daniel-mar | 853 | return $already_ssl; |
854 | } |
||
855 | |||
261 | daniel-mar | 856 | if ($mode == 1) { |
80 | daniel-mar | 857 | // Force SSL |
858 | if ($already_ssl) { |
||
230 | daniel-mar | 859 | self::$sslAvailableCache = true; |
80 | daniel-mar | 860 | return true; |
42 | daniel-mar | 861 | } else { |
80 | daniel-mar | 862 | $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
863 | header('Location:'.$location); |
||
240 | daniel-mar | 864 | die('Redirecting to HTTPS...'); |
230 | daniel-mar | 865 | self::$sslAvailableCache = true; |
80 | daniel-mar | 866 | return true; |
867 | } |
||
868 | } |
||
869 | |||
261 | daniel-mar | 870 | if ($mode == 2) { |
80 | daniel-mar | 871 | // Automatic SSL detection |
872 | |||
873 | if ($already_ssl) { |
||
874 | // we are already on HTTPS |
||
83 | daniel-mar | 875 | setcookie('SSL_CHECK', '1', 0, $cookie_path, '', false, true); |
230 | daniel-mar | 876 | self::$sslAvailableCache = true; |
80 | daniel-mar | 877 | return true; |
878 | } else { |
||
879 | if (isset($_COOKIE['SSL_CHECK'])) { |
||
880 | // We already had the HTTPS detection done before. |
||
881 | if ($_COOKIE['SSL_CHECK']) { |
||
882 | // HTTPS was detected before, but we are HTTP. Redirect now |
||
883 | $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
||
884 | header('Location:'.$location); |
||
240 | daniel-mar | 885 | die('Redirecting to HTTPS...'); |
230 | daniel-mar | 886 | self::$sslAvailableCache = true; |
80 | daniel-mar | 887 | return true; |
888 | } else { |
||
889 | // No HTTPS available. Do nothing. |
||
230 | daniel-mar | 890 | self::$sslAvailableCache = false; |
80 | daniel-mar | 891 | return false; |
892 | } |
||
49 | daniel-mar | 893 | } else { |
80 | daniel-mar | 894 | // This is our first check (or the browser didn't accept the SSL_CHECK cookie) |
895 | if (@fsockopen($_SERVER['HTTP_HOST'], $ssl_port, $errno, $errstr, $timeout)) { |
||
896 | // HTTPS detected. Redirect now, and remember that we had detected HTTPS |
||
83 | daniel-mar | 897 | setcookie('SSL_CHECK', '1', 0, $cookie_path, '', false, true); |
80 | daniel-mar | 898 | $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
899 | header('Location:'.$location); |
||
240 | daniel-mar | 900 | die('Redirecting to HTTPS...'); |
230 | daniel-mar | 901 | self::$sslAvailableCache = true; |
80 | daniel-mar | 902 | return true; |
903 | } else { |
||
904 | // No HTTPS detected. Do nothing, and next time, don't try to detect HTTPS again. |
||
83 | daniel-mar | 905 | setcookie('SSL_CHECK', '0', 0, $cookie_path, '', false, true); |
230 | daniel-mar | 906 | self::$sslAvailableCache = false; |
80 | daniel-mar | 907 | return false; |
908 | } |
||
49 | daniel-mar | 909 | } |
42 | daniel-mar | 910 | } |
911 | } |
||
912 | } |
||
241 | daniel-mar | 913 | |
914 | public static function webpath($target) { |
||
915 | $dir = __DIR__; |
||
916 | $dir = dirname($dir); |
||
917 | $dir = dirname($dir); |
||
918 | $target = substr($target, strlen($dir)+1, strlen($target)-strlen($dir)-1); |
||
919 | if ($target != '') { |
||
920 | $target = str_replace('\\','/',$target).'/'; |
||
921 | } |
||
922 | return $target; |
||
923 | } |
||
2 | daniel-mar | 924 | } |