Rev 222 | Rev 225 | 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 | |||
112 | daniel-mar | 20 | if (!defined('IN_OIDPLUS')) die(); |
21 | |||
2 | daniel-mar | 22 | class OIDplus { |
61 | daniel-mar | 23 | private static /*OIDplusPagePlugin[][]*/ $pagePlugins = array(); |
221 | daniel-mar | 24 | private static /*OIDplusAuthPlugin[][]*/ $authPlugins = array(); |
150 | daniel-mar | 25 | private static /*OIDplusObject[]*/ $objectTypes = array(); |
26 | private static /*OIDplusObject[]*/ $disabledObjectTypes = array(); |
||
27 | private static /*OIDplusDatabase[]*/ $dbPlugins = array(); |
||
2 | daniel-mar | 28 | |
29 | private function __construct() { |
||
30 | } |
||
31 | |||
32 | public static function db() { |
||
150 | daniel-mar | 33 | if (!isset(self::$dbPlugins[OIDPLUS_DATABASE_PLUGIN])) { |
34 | throw new Exception("Database plugin '".htmlentities(OIDPLUS_DATABASE_PLUGIN)."' not found. Please check config.inc.php or run <a href=\"setup/\">setup</a> again."); |
||
2 | daniel-mar | 35 | } |
150 | daniel-mar | 36 | $obj = self::$dbPlugins[OIDPLUS_DATABASE_PLUGIN]; |
37 | if (!$obj->isConnected()) $obj->connect(); |
||
38 | return $obj; |
||
2 | daniel-mar | 39 | } |
40 | |||
41 | public static function config() { |
||
86 | daniel-mar | 42 | static $config = null; |
43 | if (is_null($config)) { |
||
44 | $config = new OIDplusConfig(); |
||
2 | daniel-mar | 45 | } |
86 | daniel-mar | 46 | return $config; |
2 | daniel-mar | 47 | } |
48 | |||
49 | public static function gui() { |
||
86 | daniel-mar | 50 | static $gui = null; |
51 | if (is_null($gui)) { |
||
52 | $gui = new OIDplusGui(); |
||
53 | } |
||
54 | return $gui; |
||
2 | daniel-mar | 55 | } |
56 | |||
57 | public static function authUtils() { |
||
86 | daniel-mar | 58 | static $authUtils = null; |
59 | if (is_null($authUtils)) { |
||
60 | $authUtils = new OIDplusAuthUtils(); |
||
61 | } |
||
62 | return $authUtils; |
||
2 | daniel-mar | 63 | } |
64 | |||
115 | daniel-mar | 65 | public static function logger() { |
66 | static $logger = null; |
||
67 | if (is_null($logger)) { |
||
68 | $logger = new OIDplusLogger(); |
||
69 | } |
||
70 | return $logger; |
||
71 | } |
||
72 | |||
86 | daniel-mar | 73 | public static function sesHandler() { |
74 | static $sesHandler = null; |
||
75 | if (is_null($sesHandler)) { |
||
76 | $sesHandler = new OIDplusSessionHandler(OIDPLUS_SESSION_SECRET); |
||
77 | } |
||
78 | return $sesHandler; |
||
79 | } |
||
80 | |||
83 | daniel-mar | 81 | public static function system_url($relative=false) { |
74 | daniel-mar | 82 | if (!isset($_SERVER["REQUEST_URI"])) return false; |
83 | |||
84 | $test_dir = dirname($_SERVER['SCRIPT_FILENAME']); |
||
85 | $c = 0; |
||
86 | while (!file_exists($test_dir.'/oidplus.js')) { |
||
87 | $test_dir = dirname($test_dir); |
||
88 | $c++; |
||
89 | if ($c == 1000) return false; |
||
90 | } |
||
91 | |||
80 | daniel-mar | 92 | $res = dirname($_SERVER['REQUEST_URI'].'xxx'); |
74 | daniel-mar | 93 | |
94 | for ($i=1; $i<=$c; $i++) { |
||
76 | daniel-mar | 95 | $res = dirname($res); |
74 | daniel-mar | 96 | } |
97 | |||
159 | daniel-mar | 98 | if ($res == '/') $res = ''; |
76 | daniel-mar | 99 | $res .= '/'; |
100 | |||
83 | daniel-mar | 101 | if (!$relative) { |
102 | $res = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]" . $res; // TODO: also add port? |
||
103 | } |
||
82 | daniel-mar | 104 | |
74 | daniel-mar | 105 | return $res; |
2 | daniel-mar | 106 | } |
107 | |||
224 | daniel-mar | 108 | private static function registerDatabasePlugin(OIDplusDatabase $plugin) { |
150 | daniel-mar | 109 | $name = $plugin->name(); |
110 | if ($name === false) return false; |
||
111 | |||
112 | self::$dbPlugins[$name] = $plugin; |
||
113 | |||
114 | return true; |
||
115 | } |
||
116 | |||
117 | public static function getDatabasePlugins() { |
||
118 | return self::$dbPlugins; |
||
119 | } |
||
120 | |||
224 | daniel-mar | 121 | private static function registerPagePlugin(OIDplusPagePlugin $plugin) { |
61 | daniel-mar | 122 | $type = $plugin->type(); |
123 | if ($type === false) return false; |
||
124 | |||
125 | $prio = $plugin->priority(); |
||
126 | if ($prio === false) return false; |
||
127 | |||
128 | if (!isset(self::$pagePlugins[$type])) self::$pagePlugins[$type] = array(); |
||
129 | self::$pagePlugins[$type][$prio] = $plugin; |
||
130 | |||
131 | return true; |
||
132 | } |
||
133 | |||
224 | daniel-mar | 134 | private static function registerAuthPlugin(OIDplusAuthPlugin $plugin) { |
221 | daniel-mar | 135 | self::$authPlugins[] = $plugin; |
136 | return true; |
||
137 | } |
||
138 | |||
61 | daniel-mar | 139 | public static function getPagePlugins($type) { |
140 | if ($type == '*') { |
||
141 | $res = array(); |
||
142 | foreach (self::$pagePlugins as $data) { |
||
143 | $res = array_merge($res, $data); |
||
144 | } |
||
145 | } else { |
||
104 | daniel-mar | 146 | $res = isset(self::$pagePlugins[$type]) ? self::$pagePlugins[$type] : array(); |
61 | daniel-mar | 147 | } |
148 | ksort($res); |
||
149 | return $res; |
||
150 | } |
||
151 | |||
221 | daniel-mar | 152 | public static function getAuthPlugins() { |
153 | return self::$authPlugins; |
||
154 | } |
||
155 | |||
224 | daniel-mar | 156 | private static function registerObjectType($ot) { |
66 | daniel-mar | 157 | $ns = $ot::ns(); |
158 | |||
159 | if (empty($ns)) die("Attention: Empty NS at $ot\n"); |
||
160 | |||
161 | $ns_found = false; |
||
162 | foreach (OIDplus::getRegisteredObjectTypes() as $test_ot) { |
||
163 | if ($test_ot::ns() == $ns) { |
||
164 | $ns_found = true; |
||
165 | break; |
||
166 | } |
||
167 | } |
||
168 | if ($ns_found) { |
||
169 | throw new Exception("Attention: Two objectType plugins use the same namespace \"$ns\"!"); |
||
170 | } |
||
171 | |||
172 | $init = OIDplus::config()->getValue("objecttypes_initialized"); |
||
173 | $init_ary = empty($init) ? array() : explode(';', $init); |
||
70 | daniel-mar | 174 | $init_ary = array_map('trim', $init_ary); |
66 | daniel-mar | 175 | |
176 | $enabled = OIDplus::config()->getValue("objecttypes_enabled"); |
||
177 | $enabled_ary = empty($enabled) ? array() : explode(';', $enabled); |
||
70 | daniel-mar | 178 | $enabled_ary = array_map('trim', $enabled_ary); |
66 | daniel-mar | 179 | |
79 | daniel-mar | 180 | $do_enable = false; |
181 | if (in_array($ns, $enabled_ary)) { |
||
182 | $do_enable = true; |
||
183 | } else { |
||
184 | if (!OIDplus::config()->getValue('registration_done')) { |
||
185 | $do_enable = $ns == 'oid'; |
||
186 | } else { |
||
187 | $do_enable = !in_array($ns, $init_ary); |
||
188 | } |
||
189 | } |
||
190 | |||
191 | if ($do_enable) { |
||
66 | daniel-mar | 192 | self::$objectTypes[] = $ot; |
193 | usort(self::$objectTypes, function($a, $b) { |
||
194 | $enabled = OIDplus::config()->getValue("objecttypes_enabled"); |
||
195 | $enabled_ary = explode(';', $enabled); |
||
196 | |||
197 | $idx_a = array_search($a::ns(), $enabled_ary); |
||
198 | $idx_b = array_search($b::ns(), $enabled_ary); |
||
199 | |||
200 | if ($idx_a == $idx_b) { |
||
201 | return 0; |
||
202 | } |
||
203 | return ($idx_a > $idx_b) ? +1 : -1; |
||
204 | }); |
||
74 | daniel-mar | 205 | } else { |
206 | self::$disabledObjectTypes[] = $ot; |
||
66 | daniel-mar | 207 | } |
208 | |||
209 | if (!in_array($ns, $init_ary)) { |
||
210 | // Was never initialized before, so we add it to the list of enabled object types once |
||
211 | |||
79 | daniel-mar | 212 | if ($do_enable) { |
213 | $enabled_ary[] = $ns; |
||
214 | OIDplus::config()->setValue("objecttypes_enabled", implode(';', $enabled_ary)); |
||
215 | } |
||
66 | daniel-mar | 216 | |
217 | $init_ary[] = $ns; |
||
218 | OIDplus::config()->setValue("objecttypes_initialized", implode(';', $init_ary)); |
||
219 | } |
||
61 | daniel-mar | 220 | } |
221 | |||
222 | public static function getRegisteredObjectTypes() { |
||
223 | return self::$objectTypes; |
||
224 | } |
||
225 | |||
74 | daniel-mar | 226 | public static function getDisabledObjectTypes() { |
227 | return self::$disabledObjectTypes; |
||
228 | } |
||
229 | |||
139 | daniel-mar | 230 | private static $system_id_cache = null; |
74 | daniel-mar | 231 | public static function system_id($oid=false) { |
139 | daniel-mar | 232 | if (!is_null(self::$system_id_cache)) { |
233 | $out = self::$system_id_cache; |
||
234 | } else { |
||
235 | $out = false; |
||
236 | |||
237 | if (self::pkiStatus(true)) { |
||
238 | $pubKey = OIDplus::config()->getValue('oidplus_public_key'); |
||
239 | if (preg_match('@BEGIN PUBLIC KEY\-+(.+)\-+END PUBLIC KEY@ismU', $pubKey, $m)) { |
||
240 | $out = smallhash(base64_decode($m[1])); |
||
241 | } |
||
242 | } |
||
243 | self::$system_id_cache = $out; |
||
74 | daniel-mar | 244 | } |
139 | daniel-mar | 245 | return ($oid ? '1.3.6.1.4.1.37476.30.9.' : '').$out; |
74 | daniel-mar | 246 | } |
247 | |||
248 | public static function pkiStatus($try_generate=true) { |
||
249 | if (!function_exists('openssl_pkey_new')) return false; |
||
250 | |||
251 | $privKey = OIDplus::config()->getValue('oidplus_private_key'); |
||
252 | $pubKey = OIDplus::config()->getValue('oidplus_public_key'); |
||
253 | |||
254 | if ($try_generate && !verify_private_public_key($privKey, $pubKey)) { |
||
206 | daniel-mar | 255 | OIDplus::logger()->log("A!", "Generating new SystemID using a new key pair"); |
256 | |||
74 | daniel-mar | 257 | $config = array( |
258 | "digest_alg" => "sha512", |
||
259 | "private_key_bits" => 2048, |
||
260 | "private_key_type" => OPENSSL_KEYTYPE_RSA, |
||
261 | ); |
||
262 | |||
263 | // Create the private and public key |
||
264 | $res = openssl_pkey_new($config); |
||
265 | |||
266 | // Extract the private key from $res to $privKey |
||
267 | openssl_pkey_export($res, $privKey); |
||
268 | |||
269 | // Extract the public key from $res to $pubKey |
||
206 | daniel-mar | 270 | $pubKey = openssl_pkey_get_details($res)["key"]; |
74 | daniel-mar | 271 | |
206 | daniel-mar | 272 | // Save the key pair to database |
273 | OIDplus::config()->setValue('oidplus_private_key', $privKey); |
||
74 | daniel-mar | 274 | OIDplus::config()->setValue('oidplus_public_key', $pubKey); |
206 | daniel-mar | 275 | |
276 | // Log the new system ID |
||
277 | if (preg_match('@BEGIN PUBLIC KEY\-+(.+)\-+END PUBLIC KEY@ismU', $pubKey, $m)) { |
||
278 | $system_id = smallhash(base64_decode($m[1])); |
||
279 | OIDplus::logger()->log("A!", "Your SystemID is now $system_id"); |
||
280 | } |
||
74 | daniel-mar | 281 | } |
282 | |||
283 | return verify_private_public_key($privKey, $pubKey); |
||
284 | } |
||
285 | |||
2 | daniel-mar | 286 | public static function init($html=true) { |
42 | daniel-mar | 287 | define('OIDPLUS_HTML_OUTPUT', $html); |
288 | |||
2 | daniel-mar | 289 | // Include config file |
74 | daniel-mar | 290 | |
2 | daniel-mar | 291 | if (file_exists(__DIR__ . '/../config.inc.php')) { |
292 | include_once __DIR__ . '/../config.inc.php'; |
||
293 | } else { |
||
294 | if ($html) { |
||
74 | daniel-mar | 295 | if (!is_dir('setup')) { |
2 | daniel-mar | 296 | echo 'Error: Setup directory missing.'; |
297 | } else { |
||
42 | daniel-mar | 298 | header('Location:setup/'); |
2 | daniel-mar | 299 | } |
300 | } else { |
||
301 | echo 'Error: Setup directory missing!'; |
||
302 | } |
||
303 | die(); |
||
304 | } |
||
305 | |||
306 | // Auto-fill non-existing config values |
||
74 | daniel-mar | 307 | |
2 | daniel-mar | 308 | if (!defined('OIDPLUS_CONFIG_VERSION')) define('OIDPLUS_CONFIG_VERSION', 0.0); |
309 | if (!defined('OIDPLUS_ADMIN_PASSWORD')) define('OIDPLUS_ADMIN_PASSWORD', ''); |
||
150 | daniel-mar | 310 | if (!defined('OIDPLUS_DATABASE_PLUGIN')) define('OIDPLUS_DATABASE_PLUGIN', 'MySQL'); |
2 | daniel-mar | 311 | if (!defined('OIDPLUS_MYSQL_HOST')) define('OIDPLUS_MYSQL_HOST', 'localhost'); |
312 | if (!defined('OIDPLUS_MYSQL_USERNAME')) define('OIDPLUS_MYSQL_USERNAME', 'root'); |
||
150 | daniel-mar | 313 | if (!defined('OIDPLUS_MYSQL_PASSWORD')) define('OIDPLUS_MYSQL_PASSWORD', ''); // base64 encoded |
2 | daniel-mar | 314 | if (!defined('OIDPLUS_MYSQL_DATABASE')) define('OIDPLUS_MYSQL_DATABASE', 'oidplus'); |
315 | if (!defined('OIDPLUS_TABLENAME_PREFIX')) define('OIDPLUS_TABLENAME_PREFIX', ''); |
||
316 | if (!defined('OIDPLUS_SESSION_SECRET')) define('OIDPLUS_SESSION_SECRET', ''); |
||
27 | daniel-mar | 317 | if (!defined('RECAPTCHA_ENABLED')) define('RECAPTCHA_ENABLED', false); |
318 | if (!defined('RECAPTCHA_PUBLIC')) define('RECAPTCHA_PUBLIC', ''); |
||
319 | if (!defined('RECAPTCHA_PRIVATE')) define('RECAPTCHA_PRIVATE', ''); |
||
111 | daniel-mar | 320 | if (!defined('OIDPLUS_ENFORCE_SSL')) define('OIDPLUS_ENFORCE_SSL', 2 /* Auto */); |
2 | daniel-mar | 321 | |
322 | // Check version of the config file |
||
74 | daniel-mar | 323 | |
76 | daniel-mar | 324 | if (OIDPLUS_CONFIG_VERSION != 2.0) { |
2 | daniel-mar | 325 | if ($html) { |
326 | 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>'; |
||
327 | } else { |
||
328 | echo 'The information located in includes/config.inc.php is outdated. Please run setup again.'; |
||
329 | } |
||
330 | die(); |
||
331 | } |
||
42 | daniel-mar | 332 | |
150 | daniel-mar | 333 | // Register database types (highest priority) |
334 | |||
335 | $ary = glob(__DIR__ . '/../../plugins/database/'.'*'.'/plugin.inc.php'); |
||
336 | foreach ($ary as $a) include $a; |
||
337 | |||
224 | daniel-mar | 338 | foreach (get_declared_classes() as $c) { |
339 | if (is_subclass_of($c, 'OIDplusDataBase')) { |
||
340 | self::registerDatabasePlugin(new $c()); |
||
341 | } |
||
342 | } |
||
343 | |||
42 | daniel-mar | 344 | // Do redirect stuff etc. |
74 | daniel-mar | 345 | |
42 | daniel-mar | 346 | define('OIDPLUS_SSL_AVAILABLE', self::isSslAvailable()); |
61 | daniel-mar | 347 | |
66 | daniel-mar | 348 | // System config settings |
349 | |||
75 | daniel-mar | 350 | OIDplus::config()->prepareConfigKey('objecttypes_initialized', 'List of object type plugins that were initialized once', '', 1, 1); |
351 | OIDplus::config()->prepareConfigKey('objecttypes_enabled', 'Enabled object types and their order, separated with a semicolon (please reload the page so that the change is applied)', '', 0, 1); |
||
66 | daniel-mar | 352 | |
75 | daniel-mar | 353 | OIDplus::config()->prepareConfigKey('oidplus_private_key', 'Private key for this system', '', 1, 0); |
80 | daniel-mar | 354 | OIDplus::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.', '', 1, 1); |
74 | daniel-mar | 355 | |
356 | // Initialize public / private keys |
||
357 | |||
358 | OIDplus::pkiStatus(true); |
||
359 | |||
61 | daniel-mar | 360 | // Register plugins |
74 | daniel-mar | 361 | |
222 | daniel-mar | 362 | $ary = glob(__DIR__ . '/../../plugins/objectTypes/'.'*'.'/plugin.inc.php'); |
150 | daniel-mar | 363 | foreach ($ary as $a) include $a; |
364 | |||
61 | daniel-mar | 365 | $ary = glob(__DIR__ . '/../../plugins/publicPages/'.'*'.'/plugin.inc.php'); |
366 | foreach ($ary as $a) include $a; |
||
367 | $ary = glob(__DIR__ . '/../../plugins/raPages/'.'*'.'/plugin.inc.php'); |
||
368 | foreach ($ary as $a) include $a; |
||
369 | $ary = glob(__DIR__ . '/../../plugins/adminPages/'.'*'.'/plugin.inc.php'); |
||
370 | foreach ($ary as $a) include $a; |
||
74 | daniel-mar | 371 | |
221 | daniel-mar | 372 | $ary = glob(__DIR__ . '/../../plugins/auth/'.'*'.'/plugin.inc.php'); |
373 | foreach ($ary as $a) include $a; |
||
374 | |||
224 | daniel-mar | 375 | foreach (get_declared_classes() as $c) { |
376 | if (is_subclass_of($c, 'OIDplusPagePlugin')) { |
||
377 | self::registerPagePlugin(new $c()); |
||
378 | } |
||
379 | if (is_subclass_of($c, 'OIDplusAuthPlugin')) { |
||
380 | self::registerAuthPlugin(new $c()); |
||
381 | } |
||
382 | if (is_subclass_of($c, 'OIDplusObject')) { |
||
383 | self::registerObjectType($c); |
||
384 | } |
||
385 | } |
||
74 | daniel-mar | 386 | |
224 | daniel-mar | 387 | // Initialize page plugins |
388 | |||
74 | daniel-mar | 389 | foreach (OIDplus::getPagePlugins('*') as $plugin) { |
390 | $plugin->init($html); |
||
391 | } |
||
2 | daniel-mar | 392 | } |
42 | daniel-mar | 393 | |
170 | daniel-mar | 394 | public static function getInstallType() { |
395 | if (!file_exists(__DIR__ . '/../../oidplus_version.txt') && !is_dir(__DIR__ . '/../../.svn')) { |
||
396 | return 'unknown'; |
||
397 | } |
||
398 | if (file_exists(__DIR__ . '/../../oidplus_version.txt') && is_dir(__DIR__ . '/../../.svn')) { |
||
399 | return 'ambigous'; |
||
400 | } |
||
401 | if (is_dir(__DIR__ . '/../../.svn')) { |
||
402 | return 'svn-wc'; |
||
403 | } |
||
404 | if (file_exists(__DIR__ . '/../../oidplus_version.txt')) { |
||
405 | return 'svn-snapshot'; |
||
406 | } |
||
407 | } |
||
408 | |||
111 | daniel-mar | 409 | public static function getVersion() { |
162 | daniel-mar | 410 | $svn_version = null; |
411 | |||
412 | if (file_exists(__DIR__ . '/../../oidplus_version.txt') && is_dir(__DIR__ . '/../../.svn')) { |
||
413 | return false; // ambigous |
||
111 | daniel-mar | 414 | } |
162 | daniel-mar | 415 | |
416 | if (is_dir(__DIR__ . '/../../.svn')) { |
||
417 | // Try to find out the SVN version using the shell |
||
418 | $status = @shell_exec('svnversion '.realpath(__FILE__)); |
||
419 | if (preg_match('/\d+/', $status, $match)) { |
||
420 | $svn_version = 'svn-'.$match[0]; |
||
421 | } |
||
422 | |||
423 | // If that failed, try to get the version via SQLite3 |
||
424 | if (is_null($svn_version)) { |
||
425 | $db = new SQLite3(__DIR__ . '/../../.svn/wc.db'); |
||
426 | $results = $db->query('SELECT MIN(revision) AS rev FROM NODES_BASE'); |
||
427 | while ($row = $results->fetchArray()) { |
||
428 | $svn_version = 'svn-'.$row['rev']; |
||
429 | } |
||
430 | } |
||
431 | } |
||
432 | |||
433 | if (file_exists(__DIR__ . '/../../oidplus_version.txt')) { |
||
434 | $cont = file_get_contents(__DIR__ . '/../../oidplus_version.txt'); |
||
435 | if (!preg_match('@Revision (\d+)@', $cont, $m)) |
||
436 | return false; // File has unknown format |
||
437 | $svn_version = 'svn-'.$m[1]; |
||
438 | } |
||
439 | |||
440 | return $svn_version; |
||
111 | daniel-mar | 441 | } |
442 | |||
42 | daniel-mar | 443 | private static function isSslAvailable() { |
49 | daniel-mar | 444 | $timeout = 2; |
80 | daniel-mar | 445 | $already_ssl = isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == "on"); |
446 | $ssl_port = 443; |
||
83 | daniel-mar | 447 | $cookie_path = OIDplus::system_url(true); |
448 | if (empty($cookie_path)) $cookie_path = '/'; |
||
42 | daniel-mar | 449 | |
54 | daniel-mar | 450 | if (php_sapi_name() == 'cli') return false; |
451 | |||
80 | daniel-mar | 452 | if (OIDPLUS_ENFORCE_SSL == 0) { |
453 | // No SSL available |
||
454 | return $already_ssl; |
||
455 | } |
||
456 | |||
457 | if (OIDPLUS_ENFORCE_SSL == 1) { |
||
458 | // Force SSL |
||
459 | if ($already_ssl) { |
||
460 | return true; |
||
42 | daniel-mar | 461 | } else { |
80 | daniel-mar | 462 | $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
463 | header('Location:'.$location); |
||
111 | daniel-mar | 464 | die('Redirect to HTTPS'); |
80 | daniel-mar | 465 | return true; |
466 | } |
||
467 | } |
||
468 | |||
469 | if (OIDPLUS_ENFORCE_SSL == 2) { |
||
470 | // Automatic SSL detection |
||
471 | |||
472 | if ($already_ssl) { |
||
473 | // we are already on HTTPS |
||
83 | daniel-mar | 474 | setcookie('SSL_CHECK', '1', 0, $cookie_path, '', false, true); |
80 | daniel-mar | 475 | return true; |
476 | } else { |
||
477 | if (isset($_COOKIE['SSL_CHECK'])) { |
||
478 | // We already had the HTTPS detection done before. |
||
479 | if ($_COOKIE['SSL_CHECK']) { |
||
480 | // HTTPS was detected before, but we are HTTP. Redirect now |
||
481 | $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
||
482 | header('Location:'.$location); |
||
483 | die('Redirect to HTTPS'); |
||
484 | return true; |
||
485 | } else { |
||
486 | // No HTTPS available. Do nothing. |
||
487 | return false; |
||
488 | } |
||
49 | daniel-mar | 489 | } else { |
80 | daniel-mar | 490 | // This is our first check (or the browser didn't accept the SSL_CHECK cookie) |
491 | if (@fsockopen($_SERVER['HTTP_HOST'], $ssl_port, $errno, $errstr, $timeout)) { |
||
492 | // HTTPS detected. Redirect now, and remember that we had detected HTTPS |
||
83 | daniel-mar | 493 | setcookie('SSL_CHECK', '1', 0, $cookie_path, '', false, true); |
80 | daniel-mar | 494 | $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
495 | header('Location:'.$location); |
||
111 | daniel-mar | 496 | die('Redirect to HTTPS'); |
80 | daniel-mar | 497 | return true; |
498 | } else { |
||
499 | // No HTTPS detected. Do nothing, and next time, don't try to detect HTTPS again. |
||
83 | daniel-mar | 500 | setcookie('SSL_CHECK', '0', 0, $cookie_path, '', false, true); |
80 | daniel-mar | 501 | return false; |
502 | } |
||
49 | daniel-mar | 503 | } |
42 | daniel-mar | 504 | } |
505 | } |
||
506 | } |
||
2 | daniel-mar | 507 | } |