diff --git a/.phpstan-baseline.php b/.phpstan-baseline.php index 0915d5a5b40..5efdebc9456 100644 --- a/.phpstan-baseline.php +++ b/.phpstan-baseline.php @@ -2579,7 +2579,7 @@ 'message' => '#^Path in include_once\\(\\) "/config_db\\.php" is not a file or it does not exist\\.$#', 'identifier' => 'includeOnce.fileNotFound', 'count' => 1, - 'path' => __DIR__ . '/src/Glpi/Config/LegacyConfigurators/StandardIncludes.php', + 'path' => __DIR__ . '/src/Glpi/Config/LegacyConfigurators/InitializeDbConnection.php', ]; $ignoreErrors[] = [ 'message' => '#^Call to an undefined method Symfony\\\\Component\\\\Console\\\\Helper\\\\HelperInterface\\:\\:ask\\(\\)\\.$#', @@ -2620,7 +2620,7 @@ $ignoreErrors[] = [ 'message' => '#^Instanceof between DBmysql and DBmysql will always evaluate to true\\.$#', 'identifier' => 'instanceof.alwaysTrue', - 'count' => 3, + 'count' => 2, 'path' => __DIR__ . '/src/Glpi/Console/Application.php', ]; $ignoreErrors[] = [ @@ -2738,13 +2738,6 @@ 'path' => __DIR__ . '/src/Glpi/Controller/ApiController.php', ]; $ignoreErrors[] = [ - // identifier: identical.alwaysFalse - 'message' => '#^Strict comparison using \\=\\=\\= between null and \'development\' will always evaluate to false\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/src/Glpi/Search/Input/QueryBuilder.php', -]; -$ignoreErrors[] = [ - // identifier: identical.alwaysFalse 'message' => '#^Strict comparison using \\=\\=\\= between null and \'development\' will always evaluate to false\\.$#', 'identifier' => 'identical.alwaysFalse', 'count' => 1, @@ -3440,6 +3433,12 @@ 'count' => 2, 'path' => __DIR__ . '/src/Glpi/Search/Input/QueryBuilder.php', ]; +$ignoreErrors[] = [ + 'message' => '#^Strict comparison using \\=\\=\\= between null and \'development\' will always evaluate to false\\.$#', + 'identifier' => 'identical.alwaysFalse', + 'count' => 1, + 'path' => __DIR__ . '/src/Glpi/Search/Input/QueryBuilder.php', +]; $ignoreErrors[] = [ 'message' => '#^Call to an undefined method PhpOffice\\\\PhpSpreadsheet\\\\Writer\\\\IWriter\\:\\:setOrientation\\(\\)\\.$#', 'identifier' => 'method.notFound', diff --git a/dependency_injection/legacyConfigProviders.php b/dependency_injection/legacyConfigProviders.php index 5d27c7ee496..5f8fc1ff2b9 100644 --- a/dependency_injection/legacyConfigProviders.php +++ b/dependency_injection/legacyConfigProviders.php @@ -39,7 +39,9 @@ use Glpi\Config\LegacyConfigurators\ConfigRest; use Glpi\Config\LegacyConfigurators\CustomObjectsAutoloader; use Glpi\Config\LegacyConfigurators\CustomObjectsBootstrap; +use Glpi\Config\LegacyConfigurators\InitializeDbConnection; use Glpi\Config\LegacyConfigurators\InitializePlugins; +use Glpi\Config\LegacyConfigurators\LoadLegacyConfiguration; use Glpi\Config\LegacyConfigurators\ProfilerStart; use Glpi\Config\LegacyConfigurators\SessionConfig; use Glpi\Config\LegacyConfigurators\SessionStart; @@ -63,7 +65,9 @@ * ⚠ Here, ORDER of definition matters! */ - $services->set(ProfilerStart::class)->tag($tagName, ['priority' => 180]); + $services->set(ProfilerStart::class)->tag($tagName, ['priority' => 200]); + $services->set(InitializeDbConnection::class)->tag($tagName, ['priority' => 190]); + $services->set(LoadLegacyConfiguration::class)->tag($tagName, ['priority' => 180]); $services->set(SessionStart::class)->tag($tagName, ['priority' => 170]); $services->set(StandardIncludes::class)->tag($tagName, ['priority' => 160]); $services->set(CleanPHPSelfParam::class)->tag($tagName, ['priority' => 150]); diff --git a/src/Config.php b/src/Config.php index 99ff46333e0..7ef343e5f54 100644 --- a/src/Config.php +++ b/src/Config.php @@ -81,6 +81,12 @@ class Config extends CommonDBTM ]; public static $saferUndisclosedFields = ['admin_email', 'replyto_email']; + /** + * Indicates whether the GLPI configuration has been loaded. + * @var boolean + */ + private static $loaded = false; + public static function getTypeName($nb = 0) { return __('Setup'); @@ -1652,13 +1658,20 @@ public static function getConfigurationValue(string $context, string $name) */ public static function loadLegacyConfiguration() { - /** * @var array $CFG_GLPI - * @var \DBmysql $DB + * @var \DBmysql|null $DB */ global $CFG_GLPI, $DB; + if ( + !($DB instanceof DBmysql) + || !$DB->connected + || !$DB->tableExists('glpi_configs') + ) { + return false; + } + $iterator = $DB->request(['FROM' => 'glpi_configs']); if ($iterator->count() === 0) { @@ -1704,9 +1717,19 @@ public static function loadLegacyConfiguration() $CFG_GLPI['planning_work_days'] = importArrayFromDB($CFG_GLPI['planning_work_days']); } + self::$loaded = true; + return true; } + /** + * Indicates whether the legacy configuration has been correctly loaded. + */ + public static function isLegacyConfigurationLoaded(): bool + { + return self::$loaded; + } + /** * Set config values : create or update entry diff --git a/src/Glpi/Config/LegacyConfigurators/InitializeDbConnection.php b/src/Glpi/Config/LegacyConfigurators/InitializeDbConnection.php new file mode 100644 index 00000000000..09316e97d3b --- /dev/null +++ b/src/Glpi/Config/LegacyConfigurators/InitializeDbConnection.php @@ -0,0 +1,60 @@ +. + * + * --------------------------------------------------------------------- + */ + +namespace Glpi\Config\LegacyConfigurators; + +use DBConnection; +use Glpi\Config\LegacyConfigProviderInterface; + +final readonly class InitializeDbConnection implements LegacyConfigProviderInterface +{ + public function execute(): void + { + if (isset($_SESSION['is_installing'])) { + return; + } + + if (!file_exists(GLPI_CONFIG_DIR . '/config_db.php')) { + return; + } + + include_once(GLPI_CONFIG_DIR . '/config_db.php'); + + if (!\class_exists('DB', false)) { + return; + } + + DBConnection::establishDBConnection(false, false); + } +} diff --git a/src/Glpi/Config/LegacyConfigurators/LoadLegacyConfiguration.php b/src/Glpi/Config/LegacyConfigurators/LoadLegacyConfiguration.php new file mode 100644 index 00000000000..9a08ef57a3d --- /dev/null +++ b/src/Glpi/Config/LegacyConfigurators/LoadLegacyConfiguration.php @@ -0,0 +1,50 @@ +. + * + * --------------------------------------------------------------------- + */ + +namespace Glpi\Config\LegacyConfigurators; + +use Config; +use Glpi\Config\LegacyConfigProviderInterface; + +final readonly class LoadLegacyConfiguration implements LegacyConfigProviderInterface +{ + public function execute(): void + { + if (isset($_SESSION['is_installing'])) { + return; + } + + Config::loadLegacyConfiguration(); + } +} diff --git a/src/Glpi/Config/LegacyConfigurators/StandardIncludes.php b/src/Glpi/Config/LegacyConfigurators/StandardIncludes.php index ec4f6197680..93fd8dd4ceb 100644 --- a/src/Glpi/Config/LegacyConfigurators/StandardIncludes.php +++ b/src/Glpi/Config/LegacyConfigurators/StandardIncludes.php @@ -34,20 +34,21 @@ namespace Glpi\Config\LegacyConfigurators; -use Glpi\System\Requirement\DatabaseTablesEngine; -use Session; use Auth; use DBConnection; +use DBmysql; use Config; -use Html; -use Toolbox; -use Update; use Glpi\Application\View\TemplateRenderer; use Glpi\Cache\CacheManager; +use Glpi\Config\LegacyConfigProviderInterface; +use Glpi\System\Requirement\DatabaseTablesEngine; use Glpi\System\RequirementsManager; use Glpi\Toolbox\VersionParser; -use Glpi\Config\LegacyConfigProviderInterface; +use Html; +use Session; use Symfony\Component\DependencyInjection\Attribute\Autowire; +use Toolbox; +use Update; final readonly class StandardIncludes implements LegacyConfigProviderInterface { @@ -59,10 +60,12 @@ public function __construct( public function execute(): void { /** + * @var \DBmysql|null $DB * @var array $CFG_GLPI * @var \Psr\SimpleCache\CacheInterface $GLPI_CACHE */ - global $CFG_GLPI, + global $DB, + $CFG_GLPI, $GLPI_CACHE ; @@ -106,70 +109,60 @@ public function execute(): void $GLPI_CACHE = $cache_manager->getCoreCacheInstance(); // Check if the DB is configured properly - if (!file_exists(GLPI_CONFIG_DIR . "/config_db.php")) { - $missing_db_config = true; - } else { - include_once(GLPI_CONFIG_DIR . "/config_db.php"); - $missing_db_config = !class_exists('DB', false); - } - if (!$missing_db_config) { - //Database connection - if ( - !DBConnection::establishDBConnection(false, false) - && !$skip_db_checks - ) { - DBConnection::displayMySQLError(); - exit(1); - } + if (!$skip_db_checks) { + if ($DB instanceof DBmysql) { + //Database connection + if (!$DB->connected) { + DBConnection::displayMySQLError(); + exit(1); + } - //Options from DB, do not touch this part. - if ( - !Config::loadLegacyConfiguration() - && !$skip_db_checks - ) { - echo "Error accessing config table"; - exit(1); - } - } elseif (!$skip_db_checks) { - Session::loadLanguage('', false); + //Options from DB, do not touch this part. + if (!Config::isLegacyConfigurationLoaded()) { + echo "Error accessing config table"; + exit(1); + } + } else { + Session::loadLanguage('', false); - if (!isCommandLine()) { - // Prevent inclusion of debug information in footer, as they are based on vars that are not initialized here. - $debug_mode = $_SESSION['glpi_use_mode']; - $_SESSION['glpi_use_mode'] = Session::NORMAL_MODE; - - Html::nullHeader('Missing configuration', $CFG_GLPI["root_doc"]); - $twig_params = [ - 'config_db' => GLPI_CONFIG_DIR . '/config_db.php', - 'install_exists' => file_exists($this->projectDir . '/install/install.php'), - ]; - // language=Twig - echo TemplateRenderer::getInstance()->renderFromStringTemplate(<< -
-
-

GLPI seems to not be configured properly.

-

- Database configuration file "{{ config_db }}" is missing or is corrupted. - You have to either restart the install process, either restore this file. -
-
- {% if install_exists %} - Go to install page - {% endif %} -

+ if (!isCommandLine()) { + // Prevent inclusion of debug information in footer, as they are based on vars that are not initialized here. + $debug_mode = $_SESSION['glpi_use_mode']; + $_SESSION['glpi_use_mode'] = Session::NORMAL_MODE; + + Html::nullHeader('Missing configuration', $CFG_GLPI["root_doc"]); + $twig_params = [ + 'config_db' => GLPI_CONFIG_DIR . '/config_db.php', + 'install_exists' => file_exists($this->projectDir . '/install/install.php'), + ]; + // language=Twig + echo TemplateRenderer::getInstance()->renderFromStringTemplate(<< +
+
+

GLPI seems to not be configured properly.

+

+ Database configuration file "{{ config_db }}" is missing or is corrupted. + You have to either restart the install process, either restore this file. +
+
+ {% if install_exists %} + Go to install page + {% endif %} +

+
-
-TWIG, $twig_params); - Html::nullFooter(); - $_SESSION['glpi_use_mode'] = $debug_mode; - } else { - echo "GLPI seems to not be configured properly.\n"; - echo sprintf('Database configuration file "%s" is missing or is corrupted.', GLPI_CONFIG_DIR . '/config_db.php') . "\n"; - echo "You have to either restart the install process, either restore this file.\n"; + TWIG, $twig_params); + Html::nullFooter(); + $_SESSION['glpi_use_mode'] = $debug_mode; + } else { + echo "GLPI seems to not be configured properly.\n"; + echo sprintf('Database configuration file "%s" is missing or is corrupted.', GLPI_CONFIG_DIR . '/config_db.php') . "\n"; + echo "You have to either restart the install process, either restore this file.\n"; + } + exit(1); } - exit(1); } if ( diff --git a/src/Glpi/Console/Application.php b/src/Glpi/Console/Application.php index 018b1a058ae..f3fe58aaacc 100644 --- a/src/Glpi/Console/Application.php +++ b/src/Glpi/Console/Application.php @@ -434,15 +434,6 @@ private function initConfig() $this->config = &$CFG_GLPI; Config::detectRootDoc(); - - if ( - !($this->db instanceof DBmysql) - || !$this->db->connected - || !$this->db->tableExists('glpi_configs') - ) { - return; - } - Config::loadLegacyConfiguration(); }