Subversion Repositories oidplus

Rev

Rev 247 | Rev 256 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  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. if (!defined('IN_OIDPLUS')) die();
  21.  
  22. class OIDplus {
  23.         private static /*OIDplusPagePlugin[][]*/ $pagePlugins = array();
  24.         private static /*OIDplusAuthPlugin[][]*/ $authPlugins = array();
  25.         private static /*OIDplusObjectTypePlugin[]*/ $objectTypePlugins = array();
  26.         private static /*string[]*/ $enabledObjectTypes = array();
  27.         private static /*string[]*/ $disabledObjectTypes = array();
  28.         private static /*OIDplusDatabasePlugin[]*/ $dbPlugins = array();
  29.  
  30.         protected static $html = null;
  31.  
  32.         private function __construct() {
  33.         }
  34.        
  35.         # --- Singleton classes
  36.  
  37.         public static function config() {
  38.                 static $config = null;
  39.                 if (is_null($config)) {
  40.                         $config = new OIDplusConfig();
  41.                 }
  42.                 return $config;
  43.         }
  44.  
  45.         public static function gui() {
  46.                 static $gui = null;
  47.                 if (is_null($gui)) {
  48.                         $gui = new OIDplusGui();
  49.                 }
  50.                 return $gui;
  51.         }
  52.  
  53.         public static function authUtils() {
  54.                 static $authUtils = null;
  55.                 if (is_null($authUtils)) {
  56.                         $authUtils = new OIDplusAuthUtils();
  57.                 }
  58.                 return $authUtils;
  59.         }
  60.  
  61.         public static function mailUtils() {
  62.                 static $mailUtils = null;
  63.                 if (is_null($mailUtils)) {
  64.                         $mailUtils = new OIDplusMailUtils();
  65.                 }
  66.                 return $mailUtils;
  67.         }
  68.  
  69.         public static function menuUtils() {
  70.                 static $menuUtils = null;
  71.                 if (is_null($menuUtils)) {
  72.                         $menuUtils = new OIDplusMenuUtils();
  73.                 }
  74.                 return $menuUtils;
  75.         }
  76.  
  77.         public static function logger() {
  78.                 static $logger = null;
  79.                 if (is_null($logger)) {
  80.                         $logger = new OIDplusLogger();
  81.                 }
  82.                 return $logger;
  83.         }
  84.  
  85.         public static function sesHandler() {
  86.                 static $sesHandler = null;
  87.                 if (is_null($sesHandler)) {
  88.                         $sesHandler = new OIDplusSessionHandler(OIDPLUS_SESSION_SECRET);
  89.                 }
  90.                 return $sesHandler;
  91.         }
  92.  
  93.         # --- Database plugin
  94.  
  95.         private static function registerDatabasePlugin(OIDplusDatabasePlugin $plugin) {
  96.                 $name = $plugin->name();
  97.                 if ($name === false) return false;
  98.  
  99.                 self::$dbPlugins[$name] = $plugin;
  100.  
  101.                 return true;
  102.         }
  103.  
  104.         public static function getDatabasePlugins() {
  105.                 return self::$dbPlugins;
  106.         }
  107.  
  108.         public static function db() {
  109.                 if (!isset(self::$dbPlugins[OIDPLUS_DATABASE_PLUGIN])) {
  110.                         throw new OIDplusConfigInitializationException("Database plugin '".OIDPLUS_DATABASE_PLUGIN."' not found");
  111.                 }
  112.                 $obj = self::$dbPlugins[OIDPLUS_DATABASE_PLUGIN];
  113.                 if (!$obj->isConnected()) $obj->connect();
  114.                 return $obj;
  115.         }
  116.  
  117.         # --- Page plugin
  118.  
  119.         private static function registerPagePlugin(OIDplusPagePlugin $plugin) {
  120.                 $type = $plugin->type();
  121.                 if ($type === false) return false;
  122.  
  123.                 $prio = $plugin->priority();
  124.                 if ($prio === false) return false;
  125.  
  126.                 if (!isset(self::$pagePlugins[$type])) self::$pagePlugins[$type] = array();
  127.                 self::$pagePlugins[$type][$prio] = $plugin;
  128.  
  129.                 return true;
  130.         }
  131.  
  132.         public static function getPagePlugins($type='*') {
  133.                 if ($type === '*') {
  134.                         $res = array();
  135.                         foreach (self::$pagePlugins as $data) {
  136.                                 $res = array_merge($res, $data);
  137.                         }
  138.                 } else {
  139.                         $types = explode(',', $type);
  140.                         foreach ($types as $type) {
  141.                                 $res = isset(self::$pagePlugins[$type]) ? self::$pagePlugins[$type] : array();
  142.                         }
  143.                 }
  144.                 ksort($res);
  145.                 return $res;
  146.         }
  147.  
  148.         # --- Auth plugin
  149.  
  150.         private static function registerAuthPlugin(OIDplusAuthPlugin $plugin) {
  151.                 self::$authPlugins[] = $plugin;
  152.                 return true;
  153.         }
  154.  
  155.         public static function getAuthPlugins() {
  156.                 return self::$authPlugins;
  157.         }
  158.  
  159.         # --- Object type plugin
  160.  
  161.         private static function registerObjectTypePlugin(OIDplusObjectTypePlugin $plugin) {
  162.                 self::$objectTypePlugins[] = $plugin;
  163.  
  164.                 $ot = $plugin::getObjectTypeClassName();
  165.                 self::registerObjectType($ot);
  166.  
  167.                 return true;
  168.         }
  169.  
  170.         private static function registerObjectType($ot) {
  171.                 $ns = $ot::ns();
  172.  
  173.                 if (empty($ns)) throw new OIDplusException("Attention: Empty NS at $ot\n");
  174.  
  175.                 $ns_found = false;
  176.                 foreach (array_merge(OIDplus::getEnabledObjectTypes(), OIDplus::getDisabledObjectTypes()) as $test_ot) {
  177.                         if ($test_ot::ns() == $ns) {
  178.                                 $ns_found = true;
  179.                                 break;
  180.                         }
  181.                 }
  182.                 if ($ns_found) {
  183.                         throw new OIDplusException("Attention: Two objectType plugins use the same namespace \"$ns\"!");
  184.                 }
  185.  
  186.                 $init = OIDplus::config()->getValue("objecttypes_initialized");
  187.                 $init_ary = empty($init) ? array() : explode(';', $init);
  188.                 $init_ary = array_map('trim', $init_ary);
  189.  
  190.                 $enabled = OIDplus::config()->getValue("objecttypes_enabled");
  191.                 $enabled_ary = empty($enabled) ? array() : explode(';', $enabled);
  192.                 $enabled_ary = array_map('trim', $enabled_ary);
  193.  
  194.                 $do_enable = false;
  195.                 if (in_array($ns, $enabled_ary)) {
  196.                         $do_enable = true;
  197.                 } else {
  198.                         if (!OIDplus::config()->getValue('registration_done')) {
  199.                                 $do_enable = $ns == 'oid';
  200.                         } else {
  201.                                 $do_enable = !in_array($ns, $init_ary);
  202.                         }
  203.                 }
  204.  
  205.                 if ($do_enable) {
  206.                         self::$enabledObjectTypes[] = $ot;
  207.                         usort(self::$enabledObjectTypes, function($a, $b) {
  208.                                 $enabled = OIDplus::config()->getValue("objecttypes_enabled");
  209.                                 $enabled_ary = explode(';', $enabled);
  210.  
  211.                                 $idx_a = array_search($a::ns(), $enabled_ary);
  212.                                 $idx_b = array_search($b::ns(), $enabled_ary);
  213.  
  214.                                 if ($idx_a == $idx_b) {
  215.                                     return 0;
  216.                                 }
  217.                                 return ($idx_a > $idx_b) ? +1 : -1;
  218.                         });
  219.                 } else {
  220.                         self::$disabledObjectTypes[] = $ot;
  221.                 }
  222.  
  223.                 if (!in_array($ns, $init_ary)) {
  224.                         // Was never initialized before, so we add it to the list of enabled object types once
  225.  
  226.                         if ($do_enable) {
  227.                                 $enabled_ary[] = $ns;
  228.                                 OIDplus::config()->setValue("objecttypes_enabled", implode(';', $enabled_ary));
  229.                         }
  230.  
  231.                         $init_ary[] = $ns;
  232.                         OIDplus::config()->setValue("objecttypes_initialized", implode(';', $init_ary));
  233.                 }
  234.         }
  235.  
  236.         public static function getObjectTypePlugins() {
  237.                 return self::$objectTypePlugins;
  238.         }
  239.  
  240.         public static function getObjectTypePluginsEnabled() {
  241.                 $res = array();
  242.                 foreach (self::$objectTypePlugins as $plugin) {
  243.                         $ot = $plugin::getObjectTypeClassName();
  244.                         if (in_array($ot, self::$enabledObjectTypes)) $res[] = $plugin;
  245.                 }
  246.                 return $res;
  247.         }
  248.  
  249.         public static function getObjectTypePluginsDisabled() {
  250.                 $res = array();
  251.                 foreach (self::$objectTypePlugins as $plugin) {
  252.                         $ot = $plugin::getObjectTypeClassName();
  253.                         if (in_array($ot, self::$disabledObjectTypes)) $res[] = $plugin;
  254.                 }
  255.                 return $res;
  256.         }
  257.  
  258.         public static function getEnabledObjectTypes() {
  259.                 return self::$enabledObjectTypes;
  260.         }
  261.  
  262.         public static function getDisabledObjectTypes() {
  263.                 return self::$disabledObjectTypes;
  264.         }
  265.  
  266.         # --- Initialization of OIDplus
  267.  
  268.         public static function init($html=true) {
  269.                 self::$html = $html;
  270.  
  271.                 // Include config file
  272.  
  273.                 if (file_exists(__DIR__ . '/../config.inc.php')) {
  274.                         include_once __DIR__ . '/../config.inc.php';
  275.                 } else {
  276.                         if (!is_dir(__DIR__.'/../../setup')) {
  277.                                 throw new OIDplusConfigInitializationException('File includes/config.inc.php is missing, but setup can\'t be started because its directory missing.');
  278.                         } else {
  279.                                 if ($html) {
  280.                                         header('Location:'.OIDplus::getSystemUrl().'setup/');
  281.                                         die('Redirecting to setup...');
  282.                                 } else {
  283.                                         // This can be displayed in e.g. ajax.php
  284.                                         throw new OIDplusConfigInitializationException('File includes/config.inc.php is missing. Please run setup again.');
  285.                                 }
  286.                         }
  287.                 }
  288.  
  289.                 // Auto-fill non-existing config values, so that there won't be any PHP errors
  290.                 // if something would be missing in config.inc.php (which should not happen!)
  291.  
  292.                 if (!defined('OIDPLUS_CONFIG_VERSION'))   define('OIDPLUS_CONFIG_VERSION',   0.0);
  293.                 if (!defined('OIDPLUS_ADMIN_PASSWORD'))   define('OIDPLUS_ADMIN_PASSWORD',   '');
  294.                 if (!defined('OIDPLUS_DATABASE_PLUGIN'))  define('OIDPLUS_DATABASE_PLUGIN',  'MySQL');
  295.                 if (!defined('OIDPLUS_MYSQL_HOST'))       define('OIDPLUS_MYSQL_HOST',       'localhost');
  296.                 if (!defined('OIDPLUS_MYSQL_USERNAME'))   define('OIDPLUS_MYSQL_USERNAME',   'root');
  297.                 if (!defined('OIDPLUS_MYSQL_PASSWORD'))   define('OIDPLUS_MYSQL_PASSWORD',   ''); // base64 encoded
  298.                 if (!defined('OIDPLUS_MYSQL_DATABASE'))   define('OIDPLUS_MYSQL_DATABASE',   'oidplus');
  299.                 if (!defined('OIDPLUS_TABLENAME_PREFIX')) define('OIDPLUS_TABLENAME_PREFIX', '');
  300.                 if (!defined('OIDPLUS_SESSION_SECRET'))   define('OIDPLUS_SESSION_SECRET',   '');
  301.                 if (!defined('RECAPTCHA_ENABLED'))        define('RECAPTCHA_ENABLED',        false);
  302.                 if (!defined('RECAPTCHA_PUBLIC'))         define('RECAPTCHA_PUBLIC',         '');
  303.                 if (!defined('RECAPTCHA_PRIVATE'))        define('RECAPTCHA_PRIVATE',        '');
  304.                 if (!defined('OIDPLUS_ENFORCE_SSL'))      define('OIDPLUS_ENFORCE_SSL',      2 /* Auto */);
  305.                
  306.                 // Now include a file containing various size/depth limitations of OIDs
  307.                 // It is important to include it after config.inc.php was included,
  308.                 // so we can give config.inc.php the chance to override the values
  309.                 // by defining the constants first.
  310.  
  311.                 include_once __DIR__ . '/../limits.inc.php';
  312.  
  313.                 // Check version of the config file
  314.  
  315.                 if (OIDPLUS_CONFIG_VERSION != 2.0) {
  316.                         throw new OIDplusConfigInitializationException("The information located in includes/config.inc.php is outdated.");
  317.                 }
  318.  
  319.                 // Register database types (highest priority)
  320.  
  321.                 $ary = glob(__DIR__ . '/../../plugins/database/'.'*'.'/plugin.inc.php');
  322.                 foreach ($ary as $a) include $a;
  323.  
  324.                 foreach (get_declared_classes() as $c) {
  325.                         if (is_subclass_of($c, 'OIDplusDatabasePlugin')) {
  326.                                 self::registerDatabasePlugin(new $c());
  327.                         }
  328.                 }
  329.  
  330.                 foreach (OIDplus::getDatabasePlugins() as $plugin) {
  331.                         $plugin->init($html);
  332.                 }
  333.  
  334.                 // Do redirect stuff etc.
  335.  
  336.                 self::isSslAvailable(); // This function does automatic redirects
  337.  
  338.                 // System config settings
  339.  
  340.                 OIDplus::config()->prepareConfigKey('objecttypes_initialized', 'List of object type plugins that were initialized once', '', 1, 1);
  341.                 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);
  342.  
  343.                 OIDplus::config()->prepareConfigKey('oidplus_private_key', 'Private key for this system', '', 1, 0);
  344.                 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);
  345.  
  346.                 // Initialize public / private keys
  347.  
  348.                 OIDplus::getPkiStatus(true);
  349.  
  350.                 // Register non-DB plugins
  351.  
  352.                 $ary = glob(__DIR__ . '/../../plugins/objectTypes/'.'*'.'/plugin.inc.php');
  353.                 foreach ($ary as $a) include $a;
  354.  
  355.                 $ary = glob(__DIR__ . '/../../plugins/publicPages/'.'*'.'/plugin.inc.php');
  356.                 foreach ($ary as $a) include $a;
  357.                 $ary = glob(__DIR__ . '/../../plugins/raPages/'.'*'.'/plugin.inc.php');
  358.                 foreach ($ary as $a) include $a;
  359.                 $ary = glob(__DIR__ . '/../../plugins/adminPages/'.'*'.'/plugin.inc.php');
  360.                 foreach ($ary as $a) include $a;
  361.  
  362.                 $ary = glob(__DIR__ . '/../../plugins/auth/'.'*'.'/plugin.inc.php');
  363.                 foreach ($ary as $a) include $a;
  364.  
  365.                 foreach (get_declared_classes() as $c) {
  366.                         if (is_subclass_of($c, 'OIDplusPagePlugin')) {
  367.                                 self::registerPagePlugin(new $c());
  368.                         }
  369.                         if (is_subclass_of($c, 'OIDplusAuthPlugin')) {
  370.                                 self::registerAuthPlugin(new $c());
  371.                         }
  372.                         if (is_subclass_of($c, 'OIDplusObjectTypePlugin')) {
  373.                                 self::registerObjectTypePlugin(new $c());
  374.                         }
  375.                 }
  376.  
  377.                 // Initialize non-DB plugins
  378.  
  379.                 foreach (OIDplus::getPagePlugins('*') as $plugin) {
  380.                         $plugin->init($html);
  381.                 }
  382.                 foreach (OIDplus::getAuthPlugins() as $plugin) {
  383.                         $plugin->init($html);
  384.                 }
  385.                 foreach (OIDplus::getObjectTypePlugins() as $plugin) {
  386.                         $plugin->init($html);
  387.                 }
  388.         }
  389.  
  390.         # --- System URL, System ID, PKI, and other functions
  391.  
  392.         public static function getSystemUrl($relative=false) {
  393.                 if (!isset($_SERVER["SCRIPT_NAME"])) return false;
  394.  
  395.                 $test_dir = dirname($_SERVER['SCRIPT_FILENAME']);
  396.                 $c = 0;
  397.                 while (!file_exists($test_dir.'/oidplus_base.js')) {
  398.                         $test_dir = dirname($test_dir);
  399.                         $c++;
  400.                         if ($c == 1000) return false;
  401.                 }
  402.  
  403.                 $res = dirname($_SERVER['SCRIPT_NAME'].'xxx');
  404.  
  405.                 for ($i=1; $i<=$c; $i++) {
  406.                         $res = dirname($res);
  407.                 }
  408.  
  409.                 if ($res == '/') $res = '';
  410.                 $res .= '/';
  411.  
  412.                 if (!$relative) {
  413.                         $is_ssl = isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] === 'on');
  414.                         $protocol = $is_ssl ? 'https' : 'http';
  415.                         $host = $_SERVER['HTTP_HOST'];
  416.                         $port = $_SERVER['SERVER_PORT'];
  417.                         if ($is_ssl && ($port != 443)) {
  418.                                 $port_add = ":$port";
  419.                         } else if (!$is_ssl && ($port != 80)) {
  420.                                 $port_add = ":$port";
  421.                         } else {
  422.                                 $port_add = "";
  423.                         }
  424.                         $res = $protocol.'://'.$host.$port_add.$res;
  425.                 }
  426.  
  427.                 return $res;
  428.         }
  429.  
  430.         private static $system_id_cache = null;
  431.         public static function getSystemId($oid=false) {
  432.                 if (!is_null(self::$system_id_cache)) {
  433.                         $out = self::$system_id_cache;
  434.                 } else {
  435.                         $out = false;
  436.  
  437.                         if (self::getPkiStatus(true)) {
  438.                                 $pubKey = OIDplus::config()->getValue('oidplus_public_key');
  439.                                 if (preg_match('@BEGIN PUBLIC KEY\-+(.+)\-+END PUBLIC KEY@ismU', $pubKey, $m)) {
  440.                                         $out = smallhash(base64_decode($m[1]));
  441.                                 }
  442.                         }
  443.                         self::$system_id_cache = $out;
  444.                 }
  445.                 return ($out ? '1.3.6.1.4.1.37476.30.9.' : '').$out;
  446.         }
  447.  
  448.         public static function getPkiStatus($try_generate=true) {
  449.                 if (!function_exists('openssl_pkey_new')) return false;
  450.  
  451.                 $privKey = OIDplus::config()->getValue('oidplus_private_key');
  452.                 $pubKey = OIDplus::config()->getValue('oidplus_public_key');
  453.  
  454.                 if ($try_generate && !verify_private_public_key($privKey, $pubKey)) {
  455.                         $config = array(
  456.                             "digest_alg" => "sha512",
  457.                             "private_key_bits" => 2048,
  458.                             "private_key_type" => OPENSSL_KEYTYPE_RSA,
  459.                         );
  460.  
  461.                         // Create the private and public key
  462.                         $res = openssl_pkey_new($config);
  463.                        
  464.                         if (!$res) return false;
  465.  
  466.                         // Extract the private key from $res to $privKey
  467.                         openssl_pkey_export($res, $privKey);
  468.  
  469.                         // Extract the public key from $res to $pubKey
  470.                         $pubKey = openssl_pkey_get_details($res)["key"];
  471.  
  472.                         // Log
  473.                         OIDplus::logger()->log("A!", "Generating new SystemID using a new key pair");
  474.  
  475.                         // Save the key pair to database
  476.                         OIDplus::config()->setValue('oidplus_private_key', $privKey);
  477.                         OIDplus::config()->setValue('oidplus_public_key', $pubKey);
  478.  
  479.                         // Log the new system ID
  480.                         if (preg_match('@BEGIN PUBLIC KEY\-+(.+)\-+END PUBLIC KEY@ismU', $pubKey, $m)) {
  481.                                 $system_id = smallhash(base64_decode($m[1]));
  482.                                 OIDplus::logger()->log("A!", "Your SystemID is now $system_id");
  483.                         }
  484.                 }
  485.  
  486.                 return verify_private_public_key($privKey, $pubKey);
  487.         }
  488.  
  489.         public static function getInstallType() {
  490.                 if (!file_exists(__DIR__ . '/../../oidplus_version.txt') && !is_dir(__DIR__ . '/../../.svn')) {
  491.                         return 'unknown';
  492.                 }
  493.                 if (file_exists(__DIR__ . '/../../oidplus_version.txt') && is_dir(__DIR__ . '/../../.svn')) {
  494.                         return 'ambigous';
  495.                 }
  496.                 if (is_dir(__DIR__ . '/../../.svn')) {
  497.                         return 'svn-wc';
  498.                 }
  499.                 if (file_exists(__DIR__ . '/../../oidplus_version.txt')) {
  500.                         return 'svn-snapshot';
  501.                 }
  502.         }
  503.  
  504.         public static function getVersion() {
  505.                 if (file_exists(__DIR__ . '/../../oidplus_version.txt') && is_dir(__DIR__ . '/../../.svn')) {
  506.                         return false; // version is ambigous
  507.                 }
  508.  
  509.                 if (is_dir(__DIR__ . '/../../.svn')) {
  510.                         // Try to find out the SVN version using the shell
  511.                         // TODO: das müllt die log files voll!
  512.                         $status = @shell_exec('svnversion '.realpath(__FILE__));
  513.                         if (preg_match('/\d+/', $status, $match)) {
  514.                                 return 'svn-'.$match[0];
  515.                         }
  516.  
  517.                         // If that failed, try to get the version via SQLite3
  518.                         if (class_exists('SQLite3')) {
  519.                                 $db = new SQLite3(__DIR__ . '/../../.svn/wc.db');
  520.                                 $results = $db->query('SELECT MIN(revision) AS rev FROM NODES_BASE');
  521.                                 while ($row = $results->fetchArray()) {
  522.                                         return 'svn-'.$row['rev'];
  523.                                 }
  524.                         }
  525.                 }
  526.  
  527.                 if (file_exists(__DIR__ . '/../../oidplus_version.txt')) {
  528.                         $cont = file_get_contents(__DIR__ . '/../../oidplus_version.txt');
  529.                         if (preg_match('@Revision (\d+)@', $cont, $m))
  530.                                 return 'svn-'.$m[1];
  531.                 }
  532.  
  533.                 return false;
  534.         }
  535.  
  536.         private static $sslAvailableCache = null;
  537.         public static function isSslAvailable() {
  538.                 if (!is_null(self::$sslAvailableCache)) return self::$sslAvailableCache;
  539.        
  540.                 if (php_sapi_name() == 'cli') {
  541.                         self::$sslAvailableCache = false;
  542.                         return false;
  543.                 }
  544.  
  545.                 $timeout = 2;
  546.                 $already_ssl = isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == "on");
  547.                 $ssl_port = 443;
  548.                 $cookie_path = OIDplus::getSystemUrl(true);
  549.                 if (empty($cookie_path)) $cookie_path = '/';
  550.  
  551.                 if (OIDPLUS_ENFORCE_SSL == 0) {
  552.                         // No SSL available
  553.                         self::$sslAvailableCache = $already_ssl;
  554.                         return $already_ssl;
  555.                 }
  556.  
  557.                 if (OIDPLUS_ENFORCE_SSL == 1) {
  558.                         // Force SSL
  559.                         if ($already_ssl) {
  560.                                 self::$sslAvailableCache = true;
  561.                                 return true;
  562.                         } else {
  563.                                 $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  564.                                 header('Location:'.$location);
  565.                                 die('Redirecting to HTTPS...');
  566.                                 self::$sslAvailableCache = true;
  567.                                 return true;
  568.                         }
  569.                 }
  570.  
  571.                 if (OIDPLUS_ENFORCE_SSL == 2) {
  572.                         // Automatic SSL detection
  573.  
  574.                         if ($already_ssl) {
  575.                                 // we are already on HTTPS
  576.                                 setcookie('SSL_CHECK', '1', 0, $cookie_path, '', false, true);
  577.                                 self::$sslAvailableCache = true;
  578.                                 return true;
  579.                         } else {
  580.                                 if (isset($_COOKIE['SSL_CHECK'])) {
  581.                                         // We already had the HTTPS detection done before.
  582.                                         if ($_COOKIE['SSL_CHECK']) {
  583.                                                 // HTTPS was detected before, but we are HTTP. Redirect now
  584.                                                 $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  585.                                                 header('Location:'.$location);
  586.                                                 die('Redirecting to HTTPS...');
  587.                                                 self::$sslAvailableCache = true;
  588.                                                 return true;
  589.                                         } else {
  590.                                                 // No HTTPS available. Do nothing.
  591.                                                 self::$sslAvailableCache = false;
  592.                                                 return false;
  593.                                         }
  594.                                 } else {
  595.                                         // This is our first check (or the browser didn't accept the SSL_CHECK cookie)
  596.                                         if (@fsockopen($_SERVER['HTTP_HOST'], $ssl_port, $errno, $errstr, $timeout)) {
  597.                                                 // HTTPS detected. Redirect now, and remember that we had detected HTTPS
  598.                                                 setcookie('SSL_CHECK', '1', 0, $cookie_path, '', false, true);
  599.                                                 $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  600.                                                 header('Location:'.$location);
  601.                                                 die('Redirecting to HTTPS...');
  602.                                                 self::$sslAvailableCache = true;
  603.                                                 return true;
  604.                                         } else {
  605.                                                 // No HTTPS detected. Do nothing, and next time, don't try to detect HTTPS again.
  606.                                                 setcookie('SSL_CHECK', '0', 0, $cookie_path, '', false, true);
  607.                                                 self::$sslAvailableCache = false;
  608.                                                 return false;
  609.                                         }
  610.                                 }
  611.                         }
  612.                 }
  613.         }
  614.  
  615.         public static function webpath($target) {
  616.                 $dir = __DIR__;
  617.                 $dir = dirname($dir);
  618.                 $dir = dirname($dir);
  619.                 $target = substr($target, strlen($dir)+1, strlen($target)-strlen($dir)-1);
  620.                 if ($target != '') {
  621.                         $target = str_replace('\\','/',$target).'/';
  622.                 }
  623.                 return $target;
  624.         }
  625. }
  626.