Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions public/main/install/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<!doctype html><meta charset="utf-8"><title>Chamilo already installed</title>';
echo '<div style="font-family:system-ui;max-width:760px;margin:64px auto;padding:24px;border:1px solid #e5e7eb;border-radius:12px">';
echo '<h1>Chamilo is already installed</h1>';
echo '<p>Database version: <strong>'.htmlspecialchars($dbVersion).'</strong>. '
.'Installer version: <strong>'.htmlspecialchars($installerVersion).'</strong>.</p>';
echo '<p>The install wizard is disabled because the platform is already installed and up-to-date.</p>';
echo '<p>If you need a fresh install, set <code>APP_INSTALLED=0</code> or remove <code>.env</code> first.</p>';
echo '</div>';
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';

Expand Down
52 changes: 45 additions & 7 deletions public/main/install/install.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [
Expand Down Expand Up @@ -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, '<');
}

/**
Expand Down
Loading