From 39def3e841ac48edb53a5c391db455bcd88c6322 Mon Sep 17 00:00:00 2001 From: Christian Beeznest Date: Fri, 7 Nov 2025 18:17:51 -0500 Subject: [PATCH] Internal: Add .env detector in public/main/install/index.php - refs #6519 --- public/main/install/index.php | 52 +++++++++++++++++++++++++++++ public/main/install/install.lib.php | 52 +++++++++++++++++++++++++---- 2 files changed, 97 insertions(+), 7 deletions(-) diff --git a/public/main/install/index.php b/public/main/install/index.php index cf10e023600..a6237b683a6 100644 --- a/public/main/install/index.php +++ b/public/main/install/index.php @@ -70,6 +70,58 @@ Container::$session = new HttpSession(); require_once 'install.lib.php'; + +$envFile = api_get_path(SYMFONY_SYS_PATH).'.env'; +$versionInfo = require __DIR__.'/version.php'; +$installerVersion = $versionInfo['new_version'] ?? null; + +if (file_exists($envFile)) { + $dotenv = new Dotenv(); + try { + // Load .env without crashing if incomplete + $dotenv->loadEnv($envFile); + } catch (\Throwable $e) { + // Ignore and let the wizard continue + } + + $appInstalled = (($_ENV['APP_INSTALLED'] ?? getenv('APP_INSTALLED') ?? '') === '1'); + + if ($appInstalled) { + // Try to read DB version; fail soft if DB is unreachable + $dbVersion = null; + try { + $dbHost = $_ENV['DATABASE_HOST'] ?? 'localhost'; + $dbUser = $_ENV['DATABASE_USER'] ?? ''; + $dbPass = $_ENV['DATABASE_PASSWORD'] ?? ''; + $dbName = $_ENV['DATABASE_NAME'] ?? ''; + $dbPort = (int) ($_ENV['DATABASE_PORT'] ?? 3306); + + connectToDatabase($dbHost, $dbUser, $dbPass, $dbName, $dbPort); + $dbVersion = get_config_param_from_db('chamilo_database_version'); + } catch (\Throwable $e) { + // Leave $dbVersion as null + } + + // If DB version is >= installer version, block the wizard + if ($installerVersion && $dbVersion && version_compare($dbVersion, $installerVersion, '>=')) { + header('HTTP/1.1 409 Conflict'); + echo 'Chamilo already installed'; + echo '
'; + echo '

Chamilo is already installed

'; + echo '

Database version: '.htmlspecialchars($dbVersion).'. ' + .'Installer version: '.htmlspecialchars($installerVersion).'.

'; + echo '

The install wizard is disabled because the platform is already installed and up-to-date.

'; + echo '

If you need a fresh install, set APP_INSTALLED=0 or remove .env first.

'; + echo '
'; + exit; + } + + // If DB version < installer version, let the wizard run (upgrade path) + // If we could not read DB version (null), also allow the wizard (conservative). + } + // If APP_INSTALLED != 1, allow a fresh install +} + $httpRequest = Request::createFromGlobals(); $installationLanguage = 'en_US'; diff --git a/public/main/install/install.lib.php b/public/main/install/install.lib.php index cf4b17bb151..4c06c6ff85f 100644 --- a/public/main/install/install.lib.php +++ b/public/main/install/install.lib.php @@ -467,7 +467,7 @@ function display_requirements( $timezone = checkPhpSettingExists('date.timezone'); $phpVersion = phpversion(); - $isVersionPassed = version_compare($phpVersion, REQUIRED_PHP_VERSION, '<=') <= 1; + $isVersionPassed = version_compare($phpVersion, REQUIRED_PHP_VERSION, '>='); $extensions = []; $extensions[] = [ @@ -1800,17 +1800,55 @@ function checkCanCreateFile(string $file): bool */ function isUpdateAvailable(): bool { - $dotenv = new Dotenv(); $envFile = api_get_path(SYMFONY_SYS_PATH) . '.env'; - $dotenv->loadEnv($envFile); + if (!file_exists($envFile)) { + return false; // No .env -> fresh install + } + + $dotenv = new Dotenv(); + try { + $dotenv->loadEnv($envFile); + } catch (\Throwable $e) { + // If .env cannot be parsed, play safe: no update banner + return false; + } + + // Must be an installed platform + if (($_ENV['APP_INSTALLED'] ?? '') !== '1') { + return false; + } - // Check if APP_INSTALLED is set and equals '1' - if (isset($_ENV['APP_INSTALLED']) && $_ENV['APP_INSTALLED'] === '1') { + // Compare DB version vs installer version + $versionInfo = require __DIR__.'/version.php'; + $installerVersion = $versionInfo['new_version'] ?? null; + + if (!$installerVersion) { + // If we cannot know installer version, do not show update banner + return false; + } + + $dbVersion = null; + try { + connectToDatabase( + $_ENV['DATABASE_HOST'] ?? 'localhost', + $_ENV['DATABASE_USER'] ?? '', + $_ENV['DATABASE_PASSWORD'] ?? '', + $_ENV['DATABASE_NAME'] ?? '', + (int) ($_ENV['DATABASE_PORT'] ?? 3306) + ); + $dbVersion = get_config_param_from_db('chamilo_database_version'); + } catch (\Throwable $e) { + // If DB is unreachable but platform is marked installed, allow upgrade path + // so the UI can guide the admin. return true; } - // If APP_INSTALLED is not found or not set to '1', assume the application is not installed - return false; + if (empty($dbVersion)) { + // No version recorded -> offer upgrade path to normalize state + return true; + } + + return version_compare($dbVersion, $installerVersion, '<'); } /**