Subversion Repositories oidplus

Rev

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