Skip to content

Commit

Permalink
Refactor DB connection init and configuration loading
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric-anne committed Dec 11, 2024
1 parent cdf5e85 commit a7bc2cb
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 87 deletions.
17 changes: 8 additions & 9 deletions .phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -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\\(\\)\\.$#',
Expand Down Expand Up @@ -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[] = [
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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',
Expand Down
6 changes: 5 additions & 1 deletion dependency_injection/legacyConfigProviders.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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]);
Expand Down
27 changes: 25 additions & 2 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
60 changes: 60 additions & 0 deletions src/Glpi/Config/LegacyConfigurators/InitializeDbConnection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2024 Teclib' and contributors.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/

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);
}
}
50 changes: 50 additions & 0 deletions src/Glpi/Config/LegacyConfigurators/LoadLegacyConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2024 Teclib' and contributors.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/

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();
}
}
125 changes: 59 additions & 66 deletions src/Glpi/Config/LegacyConfigurators/StandardIncludes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
;

Expand Down Expand Up @@ -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(<<<TWIG
<div class="container-fluid mb-4">
<div class="row justify-content-center">
<div class="col-xl-6 col-lg-7 col-md-9 col-sm-12">
<h2>GLPI seems to not be configured properly.</h2>
<p class="mt-2 mb-n2 alert alert-warning">
Database configuration file "{{ config_db }}" is missing or is corrupted.
You have to either restart the install process, either restore this file.
<br />
<br />
{% if install_exists %}
<a class="btn btn-primary" href="{{ path('install/install.php') }}">Go to install page</a>
{% endif %}
</p>
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(<<<TWIG
<div class="container-fluid mb-4">
<div class="row justify-content-center">
<div class="col-xl-6 col-lg-7 col-md-9 col-sm-12">
<h2>GLPI seems to not be configured properly.</h2>
<p class="mt-2 mb-n2 alert alert-warning">
Database configuration file "{{ config_db }}" is missing or is corrupted.
You have to either restart the install process, either restore this file.
<br />
<br />
{% if install_exists %}
<a class="btn btn-primary" href="{{ path('install/install.php') }}">Go to install page</a>
{% endif %}
</p>
</div>
</div>
</div>
</div>
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 (
Expand Down
Loading

0 comments on commit a7bc2cb

Please sign in to comment.