Subversion Repositories oidplus

Rev

Rev 448 | Rev 451 | 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. class OIDplus {
  21.         private static /*OIDplusPagePlugin[]*/ $pagePlugins = array();
  22.         private static /*OIDplusAuthPlugin[]*/ $authPlugins = array();
  23.         private static /*OIDplusLoggerPlugin[]*/ $loggerPlugins = array();
  24.         private static /*OIDplusObjectTypePlugin[]*/ $objectTypePlugins = array();
  25.         private static /*string[]*/ $enabledObjectTypes = array();
  26.         private static /*string[]*/ $disabledObjectTypes = array();
  27.         private static /*OIDplusDatabasePlugin[]*/ $dbPlugins = array();
  28.         private static /*OIDplusSqlSlangPlugin[]*/ $sqlSlangPlugins = array();
  29.         private static /*OIDplusLanguagePlugin[]*/ $languagePlugins = array();
  30.         private static /*OIDplusDesignPlugin[]*/ $designPlugins = array();
  31.  
  32.         protected static $html = true;
  33.  
  34.         /*public*/ const DEFAULT_LANGUAGE = 'enus'; // the language of the source code
  35.  
  36.         private function __construct() {
  37.         }
  38.  
  39.         # --- Static classes
  40.  
  41.         private static $baseConfig = null;
  42.         private static $old_config_format = false;
  43.         public static function baseConfig() {
  44.                 $first_init = false;
  45.  
  46.                 if ($first_init = is_null(self::$baseConfig)) {
  47.                         self::$baseConfig = new OIDplusBaseConfig();
  48.                 }
  49.  
  50.                 if ($first_init) {
  51.                         // Include a file containing various size/depth limitations of OIDs
  52.                         // It is important to include it before userdata/baseconfig/config.inc.php was included,
  53.                         // so we can give userdata/baseconfig/config.inc.php the chance to override the values.
  54.  
  55.                         include OIDplus::basePath().'/includes/oidplus_limits.inc.php';
  56.  
  57.                         // Include config file
  58.  
  59.                         $config_file = OIDplus::basePath() . '/userdata/baseconfig/config.inc.php';
  60.                         $config_file_old = OIDplus::basePath() . '/includes/config.inc.php'; // backwards compatibility
  61.  
  62.                         if (!file_exists($config_file) && file_exists($config_file_old)) {
  63.                                 $config_file = $config_file_old;
  64.                         }
  65.  
  66.                         if (file_exists($config_file)) {
  67.                                 if (self::$old_config_format) {
  68.                                         // Note: We may only include it once due to backwards compatibility,
  69.                                         //       since in version 2.0, the configuration was defined using define() statements
  70.                                         // Attention: This does mean that a full re-init (e.g. for test cases) is not possible
  71.                                         //            if a version 2.0 config is used!
  72.                                         include_once $config_file;
  73.                                 } else {
  74.                                         include $config_file;
  75.                                 }
  76.  
  77.                                 if (defined('OIDPLUS_CONFIG_VERSION') && (OIDPLUS_CONFIG_VERSION == 2.0)) {
  78.                                         self::$old_config_format = true;
  79.  
  80.                                         // Backwards compatibility 2.0 => 2.1
  81.                                         foreach (get_defined_constants(true)['user'] as $name => $value) {
  82.                                                 $name = str_replace('OIDPLUS_', '', $name);
  83.                                                 if ($name == 'SESSION_SECRET') $name = 'SERVER_SECRET';
  84.                                                 if ($name == 'MYSQL_QUERYLOG') $name = 'QUERY_LOGFILE';
  85.                                                 if (($name == 'MYSQL_PASSWORD') || ($name == 'ODBC_PASSWORD') || ($name == 'PDO_PASSWORD') || ($name == 'PGSQL_PASSWORD')) {
  86.                                                         self::$baseConfig->setValue($name, base64_decode($value));
  87.                                                 } else {
  88.                                                         if ($name == 'CONFIG_VERSION') $value = 2.1;
  89.                                                         self::$baseConfig->setValue($name, $value);
  90.                                                 }
  91.                                         }
  92.                                 }
  93.                         } else {
  94.                                 if (!is_dir(OIDplus::basePath().'/setup')) {
  95.                                         throw new OIDplusConfigInitializationException(_L('File %1 is missing, but setup can\'t be started because its directory missing.','userdata/baseconfig/config.inc.php'));
  96.                                 } else {
  97.                                         if (self::$html) {
  98.                                                 if (strpos($_SERVER['REQUEST_URI'], OIDplus::getSystemUrl(true).'setup/') !== 0) {
  99.                                                         header('Location:'.OIDplus::getSystemUrl().'setup/');
  100.                                                         die(_L('Redirecting to setup...'));
  101.                                                 } else {
  102.                                                         return self::$baseConfig;
  103.                                                 }
  104.                                         } else {
  105.                                                 // This can be displayed in e.g. ajax.php
  106.                                                 throw new OIDplusConfigInitializationException(_L('File %1 is missing. Please run setup again.','userdata/baseconfig/config.inc.php'));
  107.                                         }
  108.                                 }
  109.                         }
  110.  
  111.                         // Check important config settings
  112.  
  113.                         if (self::$baseConfig->getValue('CONFIG_VERSION') != 2.1) {
  114.                                 throw new OIDplusConfigInitializationException(_L("The information located in %1 is outdated.",$config_file));
  115.                         }
  116.  
  117.                         if (self::$baseConfig->getValue('SERVER_SECRET', '') === '') {
  118.                                 throw new OIDplusConfigInitializationException(_L("You must set a value for SERVER_SECRET in %1 for the system to operate secure.",$config_file));
  119.                         }
  120.                 }
  121.  
  122.                 return self::$baseConfig;
  123.         }
  124.  
  125.         private static $config = null;
  126.         public static function config() {
  127.                 if ($first_init = is_null(self::$config)) {
  128.                         self::$config = new OIDplusConfig();
  129.                 }
  130.  
  131.                 if ($first_init) {
  132.                         // These are important settings for base functionalities and therefore are not inside plugins
  133.                         self::$config->prepareConfigKey('system_title', 'What is the name of your RA?', 'OIDplus 2.0', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
  134.                                 if (empty($value)) {
  135.                                         throw new OIDplusException(_L('Please enter a value for the system title.'));
  136.                                 }
  137.                         });
  138.                         self::$config->prepareConfigKey('admin_email', 'E-Mail address of the system administrator', '', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
  139.                                 if (!empty($value) && !OIDplus::mailUtils()->validMailAddress($value)) {
  140.                                         throw new OIDplusException(_L('This is not a correct email address'));
  141.                                 }
  142.                         });
  143.                         self::$config->prepareConfigKey('global_cc', 'Global CC for all outgoing emails?', '', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
  144.                                 if (!empty($value) && !OIDplus::mailUtils()->validMailAddress($value)) {
  145.                                         throw new OIDplusException(_L('This is not a correct email address'));
  146.                                 }
  147.                         });
  148.                         self::$config->prepareConfigKey('design', 'Which design to use (must exist in plugins/design/)?', 'default', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
  149.                                 $good = true;
  150.                                 if (strpos($value,'/') !== false) $good = false;
  151.                                 if (strpos($value,'\\') !== false) $good = false;
  152.                                 if (strpos($value,'..') !== false) $good = false;
  153.                                 if (!$good) {
  154.                                         throw new OIDplusException(_L('Invalid design folder name. Do only enter a folder name, not an absolute or relative path'));
  155.                                 }
  156.  
  157.                                 if (!is_dir(__DIR__.'/../../plugins/design/'.$value)) {
  158.                                         throw new OIDplusException(_L('The design "%1" does not exist in plugin directory %2',$value,'plugins/design/'));
  159.                                 }
  160.                         });
  161.                         self::$config->prepareConfigKey('objecttypes_initialized', 'List of object type plugins that were initialized once', '', OIDplusConfig::PROTECTION_READONLY, function($value) {
  162.                                 // Nothing here yet
  163.                         });
  164.                         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) {
  165.                                 # 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?
  166.  
  167.                                 $ary = explode(';',$value);
  168.                                 $uniq_ary = array_unique($ary);
  169.  
  170.                                 if (count($ary) != count($uniq_ary)) {
  171.                                         throw new OIDplusException(_L('Please check your input. Some object types are double.'));
  172.                                 }
  173.  
  174.                                 foreach ($ary as $ot_check) {
  175.                                         $ns_found = false;
  176.                                         foreach (OIDplus::getEnabledObjectTypes() as $ot) {
  177.                                                 if ($ot::ns() == $ot_check) {
  178.                                                         $ns_found = true;
  179.                                                         break;
  180.                                                 }
  181.                                         }
  182.                                         foreach (OIDplus::getDisabledObjectTypes() as $ot) {
  183.                                                 if ($ot::ns() == $ot_check) {
  184.                                                         $ns_found = true;
  185.                                                         break;
  186.                                                 }
  187.                                         }
  188.                                         if (!$ns_found) {
  189.                                                 throw new OIDplusException(_L('Please check your input. Namespace "%1" is not found',$ot_check));
  190.                                         }
  191.                                 }
  192.                         });
  193.                         self::$config->prepareConfigKey('oidplus_private_key', 'Private key for this system', '', OIDplusConfig::PROTECTION_HIDDEN, function($value) {
  194.                                 // Nothing here yet
  195.                         });
  196.                         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) {
  197.                                 // Nothing here yet
  198.                         });
  199.                         self::$config->prepareConfigKey('last_known_system_url', 'Last known System URL', '', OIDplusConfig::PROTECTION_HIDDEN, function($value) {
  200.                                 // Nothing here yet
  201.                         });
  202.                         self::$config->prepareConfigKey('last_known_version', 'Last known OIDplus Version', '', OIDplusConfig::PROTECTION_HIDDEN, function($value) {
  203.                                 // Nothing here yet
  204.                         });
  205.                 }
  206.  
  207.                 return self::$config;
  208.         }
  209.  
  210.         private static $gui = null;
  211.         public static function gui() {
  212.                 if (is_null(self::$gui)) {
  213.                         self::$gui = new OIDplusGui();
  214.                 }
  215.                 return self::$gui;
  216.         }
  217.  
  218.         private static $authUtils = null;
  219.         public static function authUtils() {
  220.                 if (is_null(self::$authUtils)) {
  221.                         self::$authUtils = new OIDplusAuthUtils();
  222.                 }
  223.                 return self::$authUtils;
  224.         }
  225.  
  226.         private static $mailUtils = null;
  227.         public static function mailUtils() {
  228.                 if (is_null(self::$mailUtils)) {
  229.                         self::$mailUtils = new OIDplusMailUtils();
  230.                 }
  231.                 return self::$mailUtils;
  232.         }
  233.  
  234.         private static $menuUtils = null;
  235.         public static function menuUtils() {
  236.                 if (is_null(self::$menuUtils)) {
  237.                         self::$menuUtils = new OIDplusMenuUtils();
  238.                 }
  239.                 return self::$menuUtils;
  240.         }
  241.  
  242.         private static $logger = null;
  243.         public static function logger() {
  244.                 if (is_null(self::$logger)) {
  245.                         self::$logger = new OIDplusLogger();
  246.                 }
  247.                 return self::$logger;
  248.         }
  249.  
  250.         private static $sesHandler = null;
  251.         public static function sesHandler() {
  252.                 if (is_null(self::$sesHandler)) {
  253.                         self::$sesHandler = new OIDplusSessionHandler();
  254.                 }
  255.                 return self::$sesHandler;
  256.         }
  257.  
  258.         # --- SQL slang plugin
  259.  
  260.         private static function registerSqlSlangPlugin(OIDplusSqlSlangPlugin $plugin) {
  261.                 $name = $plugin::id();
  262.                 if ($name === false) return false;
  263.  
  264.                 if (isset(self::$sqlSlangPlugins[$name])) {
  265.                         $plugintype_hf = _L('SQL-Slang');
  266.                         throw new OIDplusException('Multiple %1 plugins use the ID %2', $plugintype_hf, $name);
  267.                 }
  268.  
  269.                 self::$sqlSlangPlugins[$name] = $plugin;
  270.  
  271.                 return true;
  272.         }
  273.  
  274.         public static function getSqlSlangPlugins() {
  275.                 return self::$sqlSlangPlugins;
  276.         }
  277.  
  278.         public static function getSqlSlangPlugin($id)/*: ?OIDplusSqlSlangPlugin*/ {
  279.                 if (isset(self::$sqlSlangPlugins[$id])) {
  280.                         return self::$sqlSlangPlugins[$id];
  281.                 } else {
  282.                         return null;
  283.                 }
  284.         }
  285.  
  286.         # --- Database plugin
  287.  
  288.         private static function registerDatabasePlugin(OIDplusDatabasePlugin $plugin) {
  289.                 $name = $plugin::id();
  290.                 if ($name === false) return false;
  291.  
  292.                 if (isset(self::$dbPlugins[$name])) {
  293.                         $plugintype_hf = _L('Database');
  294.                         throw new OIDplusException('Multiple %1 plugins use the ID %2', $plugintype_hf, $name);
  295.                 }
  296.  
  297.                 self::$dbPlugins[$name] = $plugin;
  298.  
  299.                 return true;
  300.         }
  301.  
  302.         public static function getDatabasePlugins() {
  303.                 return self::$dbPlugins;
  304.         }
  305.  
  306.         public static function getActiveDatabasePlugin() {
  307.                 if (OIDplus::baseConfig()->getValue('DATABASE_PLUGIN', '') === '') {
  308.                         throw new OIDplusConfigInitializationException(_L('No database plugin selected in config file'));
  309.                 }
  310.                 if (!isset(self::$dbPlugins[OIDplus::baseConfig()->getValue('DATABASE_PLUGIN')])) {
  311.                         $db_plugin_name = OIDplus::baseConfig()->getValue('DATABASE_PLUGIN');
  312.                         throw new OIDplusConfigInitializationException(_L('Database plugin "%1" not found',$db_plugin_name));
  313.                 }
  314.                 return self::$dbPlugins[OIDplus::baseConfig()->getValue('DATABASE_PLUGIN')];
  315.         }
  316.  
  317.         private static $dbMainSession = null;
  318.         public static function db() {
  319.                 if (is_null(self::$dbMainSession)) {
  320.                         self::$dbMainSession = self::getActiveDatabasePlugin()->newConnection();
  321.                 }
  322.                 if (!self::$dbMainSession->isConnected()) self::$dbMainSession->connect();
  323.                 return self::$dbMainSession;
  324.         }
  325.  
  326.         private static $dbIsolatedSession = null;
  327.         public static function dbIsolated() {
  328.                 if (is_null(self::$dbIsolatedSession)) {
  329.                         self::$dbIsolatedSession = self::getActiveDatabasePlugin()->newConnection();
  330.                 }
  331.                 if (!self::$dbIsolatedSession->isConnected()) self::$dbIsolatedSession->connect();
  332.                 return self::$dbIsolatedSession;
  333.         }
  334.  
  335.         # --- Page plugin
  336.  
  337.         private static function registerPagePlugin(OIDplusPagePlugin $plugin) {
  338.                 self::$pagePlugins[] = $plugin;
  339.  
  340.                 return true;
  341.         }
  342.  
  343.         public static function getPagePlugins() {
  344.                 return self::$pagePlugins;
  345.         }
  346.  
  347.         # --- Auth plugin
  348.  
  349.         private static function registerAuthPlugin(OIDplusAuthPlugin $plugin) {
  350.                 self::$authPlugins[] = $plugin;
  351.                 return true;
  352.         }
  353.  
  354.         public static function getAuthPlugins() {
  355.                 return self::$authPlugins;
  356.         }
  357.  
  358.         # --- Language plugin
  359.  
  360.         private static function registerLanguagePlugin(OIDplusLanguagePlugin $plugin) {
  361.                 self::$languagePlugins[] = $plugin;
  362.                 return true;
  363.         }
  364.  
  365.         public static function getLanguagePlugins() {
  366.                 return self::$languagePlugins;
  367.         }
  368.  
  369.         # --- Design plugin
  370.  
  371.         private static function registerDesignPlugin(OIDplusDesignPlugin $plugin) {
  372.                 self::$designPlugins[] = $plugin;
  373.                 return true;
  374.         }
  375.  
  376.         public static function getDesignPlugins() {
  377.                 return self::$designPlugins;
  378.         }
  379.  
  380.         # --- Logger plugin
  381.  
  382.         private static function registerLoggerPlugin(OIDplusLoggerPlugin $plugin) {
  383.                 self::$loggerPlugins[] = $plugin;
  384.                 return true;
  385.         }
  386.  
  387.         public static function getLoggerPlugins() {
  388.                 return self::$loggerPlugins;
  389.         }
  390.  
  391.         # --- Object type plugin
  392.  
  393.         private static function registerObjectTypePlugin(OIDplusObjectTypePlugin $plugin) {
  394.                 self::$objectTypePlugins[] = $plugin;
  395.  
  396.                 $ot = $plugin::getObjectTypeClassName();
  397.                 self::registerObjectType($ot);
  398.  
  399.                 return true;
  400.         }
  401.  
  402.         private static function registerObjectType($ot) {
  403.                 $ns = $ot::ns();
  404.  
  405.                 if (empty($ns)) throw new OIDplusException(_L('Attention: Empty NS at %1',$ot));
  406.  
  407.                 $ns_found = false;
  408.                 foreach (array_merge(OIDplus::getEnabledObjectTypes(), OIDplus::getDisabledObjectTypes()) as $test_ot) {
  409.                         if ($test_ot::ns() == $ns) {
  410.                                 $ns_found = true;
  411.                                 break;
  412.                         }
  413.                 }
  414.                 if ($ns_found) {
  415.                         throw new OIDplusException(_L('Attention: Two objectType plugins use the same namespace "%1"!',$ns));
  416.                 }
  417.  
  418.                 $init = OIDplus::config()->getValue("objecttypes_initialized");
  419.                 $init_ary = empty($init) ? array() : explode(';', $init);
  420.                 $init_ary = array_map('trim', $init_ary);
  421.  
  422.                 $enabled = OIDplus::config()->getValue("objecttypes_enabled");
  423.                 $enabled_ary = empty($enabled) ? array() : explode(';', $enabled);
  424.                 $enabled_ary = array_map('trim', $enabled_ary);
  425.  
  426.                 $do_enable = false;
  427.                 if (in_array($ns, $enabled_ary)) {
  428.                         // If it is in the list of enabled object types, it is enabled (obviously)
  429.                         $do_enable = true;
  430.                 } else {
  431.                         if (!OIDplus::config()->getValue('oobe_objects_done')) {
  432.                                 // If the OOBE wizard is NOT done, then just enable the "oid" object type by default
  433.                                 $do_enable = $ns == 'oid';
  434.                         } else {
  435.                                 // If the OOBE wizard was done (once), then
  436.                                 // we will enable all object types which were never initialized
  437.                                 // (i.e. a plugin folder was freshly added)
  438.                                 $do_enable = !in_array($ns, $init_ary);
  439.                         }
  440.                 }
  441.  
  442.                 if ($do_enable) {
  443.                         self::$enabledObjectTypes[] = $ot;
  444.                         usort(self::$enabledObjectTypes, function($a, $b) {
  445.                                 $enabled = OIDplus::config()->getValue("objecttypes_enabled");
  446.                                 $enabled_ary = explode(';', $enabled);
  447.  
  448.                                 $idx_a = array_search($a::ns(), $enabled_ary);
  449.                                 $idx_b = array_search($b::ns(), $enabled_ary);
  450.  
  451.                                 if ($idx_a == $idx_b) {
  452.                                     return 0;
  453.                                 }
  454.                                 return ($idx_a > $idx_b) ? +1 : -1;
  455.                         });
  456.                 } else {
  457.                         self::$disabledObjectTypes[] = $ot;
  458.                 }
  459.  
  460.                 if (!in_array($ns, $init_ary)) {
  461.                         // Was never initialized before, so we add it to the list of enabled object types once
  462.  
  463.                         if ($do_enable) {
  464.                                 $enabled_ary[] = $ns;
  465.                                 OIDplus::config()->setValue("objecttypes_enabled", implode(';', $enabled_ary));
  466.                         }
  467.  
  468.                         $init_ary[] = $ns;
  469.                         OIDplus::config()->setValue("objecttypes_initialized", implode(';', $init_ary));
  470.                 }
  471.         }
  472.  
  473.         public static function getObjectTypePlugins() {
  474.                 return self::$objectTypePlugins;
  475.         }
  476.  
  477.         public static function getObjectTypePluginsEnabled() {
  478.                 $res = array();
  479.                 foreach (self::$objectTypePlugins as $plugin) {
  480.                         $ot = $plugin::getObjectTypeClassName();
  481.                         if (in_array($ot, self::$enabledObjectTypes)) $res[] = $plugin;
  482.                 }
  483.                 return $res;
  484.         }
  485.  
  486.         public static function getObjectTypePluginsDisabled() {
  487.                 $res = array();
  488.                 foreach (self::$objectTypePlugins as $plugin) {
  489.                         $ot = $plugin::getObjectTypeClassName();
  490.                         if (in_array($ot, self::$disabledObjectTypes)) $res[] = $plugin;
  491.                 }
  492.                 return $res;
  493.         }
  494.  
  495.         public static function getEnabledObjectTypes() {
  496.                 return self::$enabledObjectTypes;
  497.         }
  498.  
  499.         public static function getDisabledObjectTypes() {
  500.                 return self::$disabledObjectTypes;
  501.         }
  502.  
  503.         # --- Plugin handling functions
  504.  
  505.         public static function getAllPlugins()/*: array*/ {
  506.                 $res = array();
  507.                 $res = array_merge($res, self::$pagePlugins);
  508.                 $res = array_merge($res, self::$authPlugins);
  509.                 $res = array_merge($res, self::$loggerPlugins);
  510.                 $res = array_merge($res, self::$objectTypePlugins);
  511.                 $res = array_merge($res, self::$dbPlugins);
  512.                 $res = array_merge($res, self::$sqlSlangPlugins);
  513.                 $res = array_merge($res, self::$languagePlugins);
  514.                 $res = array_merge($res, self::$designPlugins);
  515.                 return $res;
  516.         }
  517.  
  518.         public static function getPluginByOid($oid)/*: ?OIDplusPlugin*/ {
  519.                 $plugins = self::getAllPlugins();
  520.                 foreach ($plugins as $plugin) {
  521.                         if (oid_dotnotation_equal($plugin->getManifest()->getOid(), $oid)) {
  522.                                 return $plugin;
  523.                         }
  524.                 }
  525.                 return null;
  526.         }
  527.  
  528.         public static function getPluginByClassName($classname)/*: ?OIDplusPlugin*/ {
  529.                 $plugins = self::getAllPlugins();
  530.                 foreach ($plugins as $plugin) {
  531.                         if (get_class($plugin) === $classname) {
  532.                                 return $plugin;
  533.                         }
  534.                 }
  535.                 return null;
  536.         }
  537.  
  538.         public static function getAllPluginManifests($pluginFolderMask='*', $flat=true): array {
  539.                 $out = array();
  540.                 // Note: glob() will sort by default, so we do not need a page priority attribute.
  541.                 //       So you just need to use a numeric plugin directory prefix (padded).
  542.                 $ary = glob(OIDplus::basePath().'/plugins/'.$pluginFolderMask.'/'.'*'.'/manifest.xml');
  543.                 sort($ary);
  544.                 foreach ($ary as $ini) {
  545.                         if (!file_exists($ini)) continue;
  546.  
  547.                         $manifest = new OIDplusPluginManifest();
  548.                         $manifest->loadManifest($ini);
  549.  
  550.                         if ($flat) {
  551.                                 $out[] = $manifest;
  552.                         } else {
  553.                                 $plugintype_folder = basename(dirname(dirname($ini)));
  554.                                 $pluginname_folder = basename(dirname($ini));
  555.  
  556.                                 if (!isset($out[$plugintype_folder])) $out[$plugintype_folder] = array();
  557.                                 if (!isset($out[$plugintype_folder][$pluginname_folder])) $out[$plugintype_folder][$pluginname_folder] = array();
  558.                                 $out[$plugintype_folder][$pluginname_folder] = $manifest;
  559.                         }
  560.                 }
  561.                 return $out;
  562.         }
  563.  
  564.         public static function registerAllPlugins($pluginDirName, $expectedPluginClass, $registerCallback): array {
  565.                 $out = array();
  566.                 $ary = self::getAllPluginManifests($pluginDirName, false);
  567.                 $known_plugin_oids = array();
  568.                 $fake_feature = uuid_to_oid(gen_uuid());
  569.                 foreach ($ary as $plugintype_folder => $bry) {
  570.                         foreach ($bry as $pluginname_folder => $manifest) {
  571.                                 $class_name = $manifest->getPhpMainClass();
  572.  
  573.                                 // Before we load the plugin, we want to make some checks to confirm
  574.                                 // that the plugin is working correctly.
  575.  
  576.                                 if (!$class_name) {
  577.                                         throw new OIDplusException(_L('Plugin "%1/%2" is erroneous',$plugintype_folder,$pluginname_folder).': '._L('Manifest does not declare a PHP main class'));
  578.                                 }
  579.                                 if (OIDplus::baseConfig()->getValue('DISABLE_PLUGIN_'.$class_name, false)) {
  580.                                         continue;
  581.                                 }
  582.                                 if (!class_exists($class_name)) {
  583.                                         throw new OIDplusException(_L('Plugin "%1/%2" is erroneous',$plugintype_folder,$pluginname_folder).': '._L('Manifest declares PHP main class as "%1", but it could not be found',$class_name));
  584.                                 }
  585.                                 if (!is_subclass_of($class_name, $expectedPluginClass)) {
  586.                                         throw new OIDplusException(_L('Plugin "%1/%2" is erroneous',$plugintype_folder,$pluginname_folder).': '._L('Plugin main class "%1" is expected to be a subclass of "%2"',$class_name,$expectedPluginClass));
  587.                                 }
  588.                                 if (($class_name!=$manifest->getTypeClass()) && (!is_subclass_of($class_name,$manifest->getTypeClass()))) {
  589.                                         throw new OIDplusException(_L('Plugin "%1/%2" is erroneous',$plugintype_folder,$pluginname_folder).': '._L('Plugin main class "%1" is expected to be a subclass of "%2", according to type declared in manifest',$class_name,$manifest->getTypeClass()));
  590.                                 }
  591.                                 if (($manifest->getTypeClass()!=$expectedPluginClass) && (!is_subclass_of($manifest->getTypeClass(),$expectedPluginClass))) {
  592.                                         throw new OIDplusException(_L('Plugin "%1/%2" is erroneous',$plugintype_folder,$pluginname_folder).': '._L('Class declared in manifest is "%1" does not fit expected class for this plugin type "%2"',$manifest->getTypeClass(),$expectedPluginClass));
  593.                                 }
  594.  
  595.                                 $plugin_oid = $manifest->getOid();
  596.                                 if (!$plugin_oid) {
  597.                                         throw new OIDplusException(_L('Plugin "%1/%2" is erroneous',$plugintype_folder,$pluginname_folder).': '._L('Does not have an OID'));
  598.                                 }
  599.                                 if (!oid_valid_dotnotation($plugin_oid, false, false, 2)) {
  600.                                         throw new OIDplusException(_L('Plugin "%1/%2" is erroneous',$plugintype_folder,$pluginname_folder).': '._L('Plugin OID "%1" is invalid (needs to be valid dot-notation)',$plugin_oid));
  601.                                 }
  602.                                 if (isset($known_plugin_oids[$plugin_oid])) {
  603.                                         throw new OIDplusException(_L('Plugin "%1/%2" is erroneous',$plugintype_folder,$pluginname_folder).': '._L('The OID "%1" is already used by the plugin "%2"',$plugin_oid,$known_plugin_oids[$plugin_oid]));
  604.                                 } else {
  605.                                         $known_plugin_oids[$plugin_oid] = $plugintype_folder.'/'.$pluginname_folder;
  606.                                 }
  607.  
  608.                                 $obj = new $class_name();
  609.                                 if ($obj->implementsFeature($fake_feature)) {
  610.                                         // see https://devblogs.microsoft.com/oldnewthing/20040211-00/?p=40663
  611.                                         throw new OIDplusException(_L('Plugin "%1/%2" is erroneous',$plugintype_folder,$pluginname_folder).': '._L('implementsFeature() always returns true'));
  612.                                 }
  613.  
  614.                                 // TODO: Maybe as additional plugin-test, we should also check if plugins are allowed to define CSS/JS (since only page plugins may have them!)
  615.                                 $tmp = array_merge(
  616.                                         $manifest->getJSFiles(),
  617.                                         $manifest->getCSSFiles(),
  618.                                         $manifest->getJSFilesSetup(),
  619.                                         $manifest->getCSSFilesSetup()
  620.                                 );
  621.                                 foreach ($tmp as $file) {
  622.                                         if (!file_exists($file)) {
  623.                                                 throw new OIDplusException(_L('Plugin "%1/%2" is erroneous',$plugintype_folder,$pluginname_folder).': '._L('File %1 was defined in manifest, but it is not existing',$file));
  624.                                         }
  625.                                 }
  626.  
  627.                                 // Now we can continue
  628.  
  629.                                 $out[] = $class_name;
  630.                                 if (!is_null($registerCallback)) {
  631.                                         call_user_func($registerCallback, $obj);
  632.  
  633.                                         // Alternative approaches:
  634.                                         //$registerCallback[0]::{$registerCallback[1]}($obj);
  635.                                         // or:
  636.                                         //forward_static_call($registerCallback, $obj);
  637.                                 }
  638.                         }
  639.  
  640.                 }
  641.                 return $out;
  642.         }
  643.  
  644.         # --- Initialization of OIDplus
  645.  
  646.         public static function init($html=true, $keepBaseConfig=true) {
  647.                 self::$html = $html;
  648.  
  649.                 // Reset internal state, so we can re-init verything if required
  650.  
  651.                 if (self::$old_config_format) {
  652.                         // Note: This can only happen in very special cases (e.g. test cases) where you call init() twice
  653.                         throw new OIDplusConfigInitializationException(_L('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.'));
  654.                 }
  655.  
  656.                 self::$config = null;
  657.                 if (!$keepBaseConfig) self::$baseConfig = null;  // for test cases we need to be able to control base config and setting values manually, so $keepBaseConfig needs to be true
  658.                 self::$gui = null;
  659.                 self::$authUtils = null;
  660.                 self::$mailUtils = null;
  661.                 self::$menuUtils = null;
  662.                 self::$logger = null;
  663.                 self::$sesHandler = null;
  664.                 self::$dbMainSession = null;
  665.                 self::$dbIsolatedSession = null;
  666.                 self::$pagePlugins = array();
  667.                 self::$authPlugins = array();
  668.                 self::$loggerPlugins = array();
  669.                 self::$objectTypePlugins = array();
  670.                 self::$enabledObjectTypes = array();
  671.                 self::$disabledObjectTypes = array();
  672.                 self::$dbPlugins = array();
  673.                 self::$sqlSlangPlugins = array();
  674.                 self::$languagePlugins = array();
  675.                 self::$designPlugins = array();
  676.                 self::$system_id_cache = null;
  677.                 self::$sslAvailableCache = null;
  678.  
  679.                 // Continue...
  680.  
  681.                 OIDplus::baseConfig(); // this loads the base configuration located in userdata/baseconfig/config.inc.php (once!)
  682.                                        // You can do changes to the configuration afterwards using OIDplus::baseConfig()->...
  683.  
  684.                 // Register database types (highest priority)
  685.  
  686.                 // SQL slangs
  687.  
  688.                 self::registerAllPlugins('sqlSlang', 'OIDplusSqlSlangPlugin', array('OIDplus','registerSqlSlangPlugin'));
  689.                 foreach (OIDplus::getSqlSlangPlugins() as $plugin) {
  690.                         $plugin->init($html);
  691.                 }
  692.  
  693.                 // Database providers
  694.  
  695.                 self::registerAllPlugins('database', 'OIDplusDatabasePlugin', array('OIDplus','registerDatabasePlugin'));
  696.                 foreach (OIDplus::getDatabasePlugins() as $plugin) {
  697.                         $plugin->init($html);
  698.                 }
  699.  
  700.                 // Do redirect stuff etc.
  701.  
  702.                 self::isSslAvailable(); // This function does automatic redirects
  703.  
  704.                 // Construct the configuration manager
  705.  
  706.                 OIDplus::config(); // During the construction, various system settings are prepared if required
  707.  
  708.                 // Initialize public / private keys
  709.  
  710.                 OIDplus::getPkiStatus(true);
  711.  
  712.                 // Register non-DB plugins
  713.  
  714.                 self::registerAllPlugins('*Pages', 'OIDplusPagePlugin', array('OIDplus','registerPagePlugin'));
  715.                 self::registerAllPlugins('auth', 'OIDplusAuthPlugin', array('OIDplus','registerAuthPlugin'));
  716.                 self::registerAllPlugins('logger', 'OIDplusLoggerPlugin', array('OIDplus','registerLoggerPlugin'));
  717.                 self::registerAllPlugins('objectTypes', 'OIDplusObjectTypePlugin', array('OIDplus','registerObjectTypePlugin'));
  718.                 self::registerAllPlugins('language', 'OIDplusLanguagePlugin', array('OIDplus','registerLanguagePlugin'));
  719.                 self::registerAllPlugins('design', 'OIDplusDesignPlugin', array('OIDplus','registerDesignPlugin'));
  720.  
  721.                 // Initialize non-DB plugins
  722.  
  723.                 foreach (OIDplus::getPagePlugins() as $plugin) {
  724.                         $plugin->init($html);
  725.                 }
  726.                 foreach (OIDplus::getAuthPlugins() as $plugin) {
  727.                         $plugin->init($html);
  728.                 }
  729.                 foreach (OIDplus::getLoggerPlugins() as $plugin) {
  730.                         $plugin->init($html);
  731.                 }
  732.                 foreach (OIDplus::getObjectTypePlugins() as $plugin) {
  733.                         $plugin->init($html);
  734.                 }
  735.                 foreach (OIDplus::getLanguagePlugins() as $plugin) {
  736.                         $plugin->init($html);
  737.                 }
  738.                 foreach (OIDplus::getDesignPlugins() as $plugin) {
  739.                         $plugin->init($html);
  740.                 }
  741.  
  742.                 // Initialize other stuff (i.e. things which require the logger!)
  743.  
  744.                 OIDplus::recognizeSystemUrl(); // Make sure "last_known_system_url" is set
  745.                 OIDplus::recognizeVersion(); // Make sure "last_known_version" is set and a log entry is created
  746.         }
  747.  
  748.         # --- System URL, System ID, PKI, and other functions
  749.  
  750.         public static function basePath() {
  751.                 return realpath(__DIR__ . '/../../');
  752.         }
  753.  
  754.         private static function recognizeSystemUrl() {
  755.                 try {
  756.                         $url = OIDplus::getSystemUrl();
  757.                         OIDplus::config()->setValue('last_known_system_url', $url);
  758.                 } catch (Exception $e) {
  759.                 }
  760.         }
  761.  
  762.         public static function getSystemUrl($relative=false) {
  763.                 if (!$relative) {
  764.                         $res = OIDplus::baseConfig()->getValue('EXPLICIT_ABSOLUTE_SYSTEM_URL', '');
  765.                         if ($res !== '') {
  766.                                 return $res;
  767.                         }
  768.                 }
  769.  
  770.                 if (!isset($_SERVER["SCRIPT_NAME"])) return false;
  771.  
  772.                 $test_dir = dirname($_SERVER['SCRIPT_FILENAME']);
  773.                 $test_dir = str_replace('\\', '/', $test_dir);
  774.                 $c = 0;
  775.                 // We just assume that only the OIDplus base directory contains "oidplus.min.css.php" and not any subsequent directory!
  776.                 while (!file_exists($test_dir.'/oidplus.min.css.php')) {
  777.                         $test_dir = dirname($test_dir);
  778.                         $c++;
  779.                         if ($c == 1000) return false;
  780.                 }
  781.  
  782.                 $res = dirname($_SERVER['SCRIPT_NAME'].'xxx');
  783.  
  784.                 for ($i=1; $i<=$c; $i++) {
  785.                         $res = dirname($res);
  786.                 }
  787.  
  788.                 $res = str_replace('\\', '/', $res);
  789.                 if ($res == '/') $res = '';
  790.                 $res .= '/';
  791.  
  792.                 if (!$relative) {
  793.                         if (php_sapi_name() == 'cli') {
  794.                                 try {
  795.                                         return OIDplus::config()->getValue('last_known_system_url', false);
  796.                                 } catch (Exception $e) {
  797.                                         return false;
  798.                                 }
  799.                         }
  800.  
  801.                         $is_ssl = isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] === 'on');
  802.                         $protocol = $is_ssl ? 'https' : 'http'; // do not translate
  803.                         $host = $_SERVER['HTTP_HOST']; // includes port if it is not 80/443
  804.                         $res = $protocol.'://'.$host.$res;
  805.                 }
  806.  
  807.                 return $res;
  808.         }
  809.  
  810.         private static $system_id_cache = null;
  811.         public static function getSystemId($oid=false) {
  812.                 if (!is_null(self::$system_id_cache)) {
  813.                         $out = self::$system_id_cache;
  814.                 } else {
  815.                         $out = false;
  816.  
  817.                         if (self::getPkiStatus(true)) {
  818.                                 $pubKey = OIDplus::config()->getValue('oidplus_public_key');
  819.                                 $m = array();
  820.                                 if (preg_match('@BEGIN PUBLIC KEY\-+(.+)\-+END PUBLIC KEY@ismU', $pubKey, $m)) {
  821.                                         $out = smallhash(base64_decode($m[1]));
  822.                                 }
  823.                         }
  824.                         self::$system_id_cache = $out;
  825.                 }
  826.                 if (!$out) return false;
  827.                 return ($oid ? '1.3.6.1.4.1.37476.30.9.' : '').$out;
  828.         }
  829.  
  830.         public static function getPkiStatus($try_generate=true) {
  831.                 if (!function_exists('openssl_pkey_new')) return false;
  832.  
  833.                 $privKey = OIDplus::config()->getValue('oidplus_private_key');
  834.                 $pubKey = OIDplus::config()->getValue('oidplus_public_key');
  835.  
  836.                 if ($try_generate && !verify_private_public_key($privKey, $pubKey)) {
  837.                         $pkey_config = array(
  838.                             "digest_alg" => "sha512",
  839.                             "private_key_bits" => 2048,
  840.                             "private_key_type" => OPENSSL_KEYTYPE_RSA,
  841.                         );
  842.  
  843.                         // Create the private and public key
  844.                         $res = openssl_pkey_new($pkey_config);
  845.  
  846.                         if (!$res) return false;
  847.  
  848.                         // Extract the private key from $res to $privKey
  849.                         openssl_pkey_export($res, $privKey);
  850.  
  851.                         // Extract the public key from $res to $pubKey
  852.                         $pubKey = openssl_pkey_get_details($res)["key"];
  853.  
  854.                         // Log
  855.                         OIDplus::logger()->log("[INFO]A!", "Generating new SystemID using a new key pair");
  856.  
  857.                         // Save the key pair to database
  858.                         OIDplus::config()->setValue('oidplus_private_key', $privKey);
  859.                         OIDplus::config()->setValue('oidplus_public_key', $pubKey);
  860.  
  861.                         // Log the new system ID
  862.                         $m = array();
  863.                         if (preg_match('@BEGIN PUBLIC KEY\-+(.+)\-+END PUBLIC KEY@ismU', $pubKey, $m)) {
  864.                                 $system_id = smallhash(base64_decode($m[1]));
  865.                                 OIDplus::logger()->log("[INFO]A!", "Your SystemID is now $system_id");
  866.                         }
  867.                 }
  868.  
  869.                 return verify_private_public_key($privKey, $pubKey);
  870.         }
  871.  
  872.         public static function getInstallType() {
  873.                 if (!file_exists(OIDplus::basePath().'/oidplus_version.txt') && !is_dir(OIDplus::basePath().'/.svn')) {
  874.                         return 'unknown'; // do not translate
  875.                 }
  876.                 if (file_exists(OIDplus::basePath().'/oidplus_version.txt') && is_dir(OIDplus::basePath().'/.svn')) {
  877.                         return 'ambigous'; // do not translate
  878.                 }
  879.                 if (is_dir(OIDplus::basePath().'/.svn')) {
  880.                         return 'svn-wc'; // do not translate
  881.                 }
  882.                 if (file_exists(OIDplus::basePath().'/oidplus_version.txt')) {
  883.                         return 'svn-snapshot'; // do not translate
  884.                 }
  885.         }
  886.  
  887.         private static function recognizeVersion() {
  888.                 try {
  889.                         $ver_prev = OIDplus::config()->getValue("last_known_version");
  890.                         $ver_now = OIDplus::getVersion();
  891.                         if (($ver_now != '') && ($ver_prev != '') && ($ver_now != $ver_prev)) {
  892.                                 OIDplus::logger()->log("[INFO]A!", "System version changed from '$ver_prev' to '$ver_now'");
  893.                         }
  894.                         OIDplus::config()->setValue("last_known_version", $ver_now);
  895.                 } catch (Exception $e) {
  896.                 }
  897.         }
  898.  
  899.         public static function getVersion() {
  900.                 static $cachedVersion = null;
  901.                 if (!is_null($cachedVersion)) {
  902.                         return $cachedVersion;
  903.                 }
  904.  
  905.                 if (file_exists(OIDplus::basePath().'/oidplus_version.txt') && is_dir(OIDplus::basePath().'/.svn')) {
  906.                         return ($cachedVersion = false); // version is ambiguous
  907.                 }
  908.  
  909.                 if (is_dir(OIDplus::basePath().'/.svn')) {
  910.                         // Try to get the version via SQLite3
  911.                         if (class_exists('SQLite3')) {
  912.                                 try {
  913.                                         $db = new SQLite3(OIDplus::basePath().'/.svn/wc.db');
  914.                                         $results = $db->query('SELECT MIN(revision) AS rev FROM NODES_BASE');
  915.                                         while ($row = $results->fetchArray()) {
  916.                                                 return ($cachedVersion = 'svn-'.$row['rev']); // do not translate
  917.                                         }
  918.                                         $db->close();
  919.                                         $db = null;
  920.                                 } catch (Exception $e) {
  921.                                 }
  922.                         }
  923.                         if (class_exists('PDO')) {
  924.                                 try {
  925.                                         $pdo = new PDO('sqlite:' . OIDplus::basePath().'/.svn/wc.db');
  926.                                         $res = $pdo->query('SELECT MIN(revision) AS rev FROM NODES_BASE');
  927.                                         $row = $res->fetch();
  928.                                         if ($row !== false) {
  929.                                                 return ($cachedVersion = 'svn-'.$row['rev']); // do not translate
  930.                                         }
  931.                                         $pdo = null;
  932.                                 } catch (Exception $e) {
  933.                                 }
  934.                         }
  935.  
  936.                         // Try to find out the SVN version using the shell
  937.                         // We don't prioritize this method, because a failed shell access will flood the apache error log with STDERR messages
  938.                         $output = @shell_exec('svnversion '.escapeshellarg(OIDplus::basePath()));
  939.                         $match = array();
  940.                         if (preg_match('/\d+/', $output, $match)) {
  941.                                 return ($cachedVersion = 'svn-'.$match[0]); // do not translate
  942.                         }
  943.  
  944.                         $output = @shell_exec('svn info '.escapeshellarg(OIDplus::basePath()));
  945.                         if (preg_match('/Revision:\s*(\d+)/m', $output, $match)) { // do not translate
  946.                                 return ($cachedVersion = 'svn-'.$match[1]); // do not translate
  947.                         }
  948.                 }
  949.  
  950.                 if (file_exists(OIDplus::basePath().'/oidplus_version.txt')) {
  951.                         $cont = file_get_contents(OIDplus::basePath().'/oidplus_version.txt');
  952.                         $m = array();
  953.                         if (preg_match('@Revision (\d+)@', $cont, $m)) // do not translate
  954.                                 return ($cachedVersion = 'svn-'.$m[1]); // do not translate
  955.                 }
  956.  
  957.                 return ($cachedVersion = false);
  958.         }
  959.  
  960.         private static $sslAvailableCache = null;
  961.         public static function isSslAvailable() {
  962.                 if (!is_null(self::$sslAvailableCache)) return self::$sslAvailableCache;
  963.  
  964.                 if (php_sapi_name() == 'cli') {
  965.                         self::$sslAvailableCache = false;
  966.                         return false;
  967.                 }
  968.  
  969.                 $timeout = 2;
  970.                 $already_ssl = isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == "on");
  971.                 $ssl_port = 443;
  972.                 $cookie_path = OIDplus::getSystemUrl(true);
  973.                 if (empty($cookie_path)) $cookie_path = '/';
  974.  
  975.                 $mode = OIDplus::baseConfig()->getValue('ENFORCE_SSL', 2/*auto*/);
  976.  
  977.                 if ($mode == 0) {
  978.                         // No SSL available
  979.                         self::$sslAvailableCache = $already_ssl;
  980.                         return $already_ssl;
  981.                 }
  982.  
  983.                 if ($mode == 1) {
  984.                         // Force SSL
  985.                         if ($already_ssl) {
  986.                                 self::$sslAvailableCache = true;
  987.                                 return true;
  988.                         } else {
  989.                                 $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  990.                                 header('Location:'.$location);
  991.                                 die(_L('Redirecting to HTTPS...'));
  992.                                 self::$sslAvailableCache = true;
  993.                                 return true;
  994.                         }
  995.                 }
  996.  
  997.                 if ($mode == 2) {
  998.                         // Automatic SSL detection
  999.  
  1000.                         if ($already_ssl) {
  1001.                                 // we are already on HTTPS
  1002.                                 setcookie('SSL_CHECK', '1', 0, $cookie_path, '', false, true);
  1003.                                 self::$sslAvailableCache = true;
  1004.                                 return true;
  1005.                         } else {
  1006.                                 if (isset($_COOKIE['SSL_CHECK'])) {
  1007.                                         // We already had the HTTPS detection done before.
  1008.                                         if ($_COOKIE['SSL_CHECK']) {
  1009.                                                 // HTTPS was detected before, but we are HTTP. Redirect now
  1010.                                                 $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  1011.                                                 header('Location:'.$location);
  1012.                                                 die(_L('Redirecting to HTTPS...'));
  1013.                                                 self::$sslAvailableCache = true;
  1014.                                                 return true;
  1015.                                         } else {
  1016.                                                 // No HTTPS available. Do nothing.
  1017.                                                 self::$sslAvailableCache = false;
  1018.                                                 return false;
  1019.                                         }
  1020.                                 } else {
  1021.                                         // This is our first check (or the browser didn't accept the SSL_CHECK cookie)
  1022.                                         $errno = -1;
  1023.                                         $errstr = '';
  1024.                                         if (@fsockopen($_SERVER['HTTP_HOST'], $ssl_port, $errno, $errstr, $timeout)) {
  1025.                                                 // HTTPS detected. Redirect now, and remember that we had detected HTTPS
  1026.                                                 setcookie('SSL_CHECK', '1', 0, $cookie_path, '', false, true);
  1027.                                                 $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  1028.                                                 header('Location:'.$location);
  1029.                                                 die(_L('Redirecting to HTTPS...'));
  1030.                                                 self::$sslAvailableCache = true;
  1031.                                                 return true;
  1032.                                         } else {
  1033.                                                 // No HTTPS detected. Do nothing, and next time, don't try to detect HTTPS again.
  1034.                                                 setcookie('SSL_CHECK', '0', 0, $cookie_path, '', false, true);
  1035.                                                 self::$sslAvailableCache = false;
  1036.                                                 return false;
  1037.                                         }
  1038.                                 }
  1039.                         }
  1040.                 }
  1041.         }
  1042.  
  1043.         public static function webpath($target) {
  1044.                 $dir = __DIR__;
  1045.                 $dir = dirname($dir);
  1046.                 $dir = dirname($dir);
  1047.                 $target = substr($target, strlen($dir)+1, strlen($target)-strlen($dir)-1);
  1048.                 if ($target != '') {
  1049.                         $target = str_replace('\\','/',$target).'/';
  1050.                 }
  1051.                 return $target;
  1052.         }
  1053.  
  1054.         public static function getAvailableLangs() {
  1055.                 $langs = array();
  1056.                 foreach (OIDplus::getAllPluginManifests('language') as $pluginManifest) {
  1057.                         $code = $pluginManifest->getLanguageCode();
  1058.                         $langs[] = $code;
  1059.                 }
  1060.                 return $langs;
  1061.         }
  1062.  
  1063.         public static function getCurrentLang() {
  1064.                 if (isset($_GET['lang'])) {
  1065.                         $lang = $_GET['lang'];
  1066.                 } else if (isset($_POST['lang'])) {
  1067.                         $lang = $_POST['lang'];
  1068.                 } else if (isset($_COOKIE['LANGUAGE'])) {
  1069.                         $lang = $_COOKIE['LANGUAGE'];
  1070.                 } else {
  1071.                         $lang = self::DEFAULT_LANGUAGE;
  1072.                 }
  1073.                 $lang = substr(preg_replace('@[^a-z]@ismU', '', $lang),0,4); // sanitize
  1074.                 return $lang;
  1075.         }
  1076.  
  1077.         public static function handleLangArgument() {
  1078.                 if (isset($_GET['lang'])) {
  1079.                         // The "?lang=" argument is only for NoScript-Browsers/SearchEngines
  1080.                         // In case someone who has JavaScript clicks a ?lang= link, they should get
  1081.                         // the page in that language, but the cookie must be set, otherwise
  1082.                         // the menu and other stuff would be in their cookie-based-language and not the
  1083.                         // argument-based-language.
  1084.                         $cookie_path = OIDplus::getSystemUrl(true);
  1085.                         if (empty($cookie_path)) $cookie_path = '/';
  1086.                         setcookie('LANGUAGE', $_GET['lang'], 0, $cookie_path, '', false, false/*HttpOnly off, because JavaScript also needs translation*/);
  1087.                 } else if (isset($_POST['lang'])) {
  1088.                         $cookie_path = OIDplus::getSystemUrl(true);
  1089.                         if (empty($cookie_path)) $cookie_path = '/';
  1090.                         setcookie('LANGUAGE', $_POST['lang'], 0, $cookie_path, '', false, false/*HttpOnly off, because JavaScript also needs translation*/);
  1091.                 }
  1092.         }
  1093.  
  1094.         public static function getTranslationArray() {
  1095.                 $translation_array = array();
  1096.                 foreach (OIDplus::getAllPluginManifests('language') as $pluginManifest) {
  1097.                         $lang = $pluginManifest->getLanguageCode();
  1098.                         $translation_array[$lang] = array();
  1099.                         if (strpos($lang,'/') !== false) continue; // just to be sure
  1100.                         if (strpos($lang,'\\') !== false) continue; // just to be sure
  1101.                         if (strpos($lang,'..') !== false) continue; // just to be sure
  1102.  
  1103.                         $wildcard = $pluginManifest->getLanguageMessages();
  1104.                         if (strpos($wildcard,'/') !== false) continue; // just to be sure
  1105.                         if (strpos($wildcard,'\\') !== false) continue; // just to be sure
  1106.                         if (strpos($wildcard,'..') !== false) continue; // just to be sure
  1107.  
  1108.                         $translation_files = glob(__DIR__.'/../../plugins/language/'.$lang.'/'.$wildcard);
  1109.                         sort($translation_files);
  1110.                         foreach ($translation_files as $translation_file) {
  1111.                                 if (!file_exists($translation_file)) continue;
  1112.                                 $xml = @simplexml_load_string(file_get_contents($translation_file));
  1113.                                 if (!$xml) continue; // if there is an UTF-8 or parsing error, don't output any errors, otherwise the JavaScript is corrupt and the page won't render correctly
  1114.                                 foreach ($xml->message as $msg) {
  1115.                                         $src = trim($msg->source->__toString());
  1116.                                         $dst = trim($msg->target->__toString());
  1117.                                         $translation_array[$lang][$src] = $dst;
  1118.                                 }
  1119.                         }
  1120.                 }
  1121.                 return $translation_array;
  1122.         }
  1123.  
  1124. }
  1125.