install file 2

This commit is contained in:
Flatlogic Bot 2026-02-28 06:01:25 +00:00
parent d2bc35de8c
commit 10eedb49b2

View File

@ -35,8 +35,8 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Test connection
try {
$pdo = new PDO("mysql:host=$host;dbname=$name;charset=utf8mb4", $user, $pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$test_pdo = new PDO("mysql:host=$host;dbname=$name;charset=utf8mb4", $user, $pass);
$test_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Generate config file content
$content = "<?php\n";
@ -48,17 +48,17 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$content .= "\n";
$content .= "if (!function_exists('db')) {\n";
$content .= " function db() {\n";
$content .= " static $pdo;\n";
$content .= " if (!$pdo) {\n";
$content .= " static \$pdo;\n";
$content .= " if (!\$pdo) {\n";
$content .= " try {\n";
$content .= " $pdo = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME . ';charset=utf8mb4', DB_USER, DB_PASS);\n";
$content .= " $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);\n";
$content .= " $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);\n";
$content .= " } catch (PDOException $e) {\n";
$content .= " die('Connection failed: ' . $e->getMessage());\n";
$content .= " \$pdo = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME . ';charset=utf8mb4', DB_USER, DB_PASS);\n";
$content .= " \$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);\n";
$content .= " \$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);\n";
$content .= " } catch (PDOException \$e) {\n";
$content .= " die('Connection failed: ' . \$e->getMessage());\n";
$content .= " }\n";
$content .= " }\n";
$content .= " return $pdo;\n";
$content .= " return \$pdo;\n";
$content .= " }\n";
$content .= "}\n";
@ -84,6 +84,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$pdo = db();
$migrations_dir = __DIR__ . '/db/migrations/';
$files = glob($migrations_dir . '*.sql');
if ($files === false) $files = [];
sort($files);
$applied = 0;
@ -94,29 +95,39 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (empty($sql)) continue;
try {
// PDO::exec might fail if there's an error in the SQL,
// but we want to continue if it's "column already exists" or "table already exists"
$pdo->exec($sql);
$applied++;
// Split SQL into multiple statements if necessary
// Simple split by semicolon. This might fail on some complex SQL,
// but usually works for basic migrations.
$statements = array_filter(array_map('trim', explode(';', $sql)));
foreach ($statements as $stmt_sql) {
if (empty($stmt_sql)) continue;
try {
$pdo->exec($stmt_sql);
} catch (Throwable $e) {
$msg = $e->getMessage();
// Check if it's a common "already exists" error which we can safely ignore
if (strpos($msg, 'Duplicate column name') !== false ||
strpos($msg, 'Duplicate key name') !== false ||
strpos($msg, 'Duplicate table') !== false ||
strpos($msg, 'already exists') !== false) {
$applied++;
continue;
} else {
$errors[] = basename($file) . ": " . $msg;
throw $e;
}
}
}
$applied++;
} catch (Throwable $e) {
$errors[] = basename($file) . ": " . $e->getMessage();
}
}
if (empty($errors)) {
$success = "Successfully applied $applied migrations.";
header('Location: install.php?step=4');
exit;
} else {
$error = "Applied $applied migrations, but some errors occurred:<br><ul><li>" . implode('</li><li>', $errors) . "</li></ul>";
$error = "Applied migrations, but some errors occurred:<br><ul><li>" . implode('</li><li>', $errors) . "</li></ul>";
}
} catch (Throwable $e) {
$error = "Migration failed: " . $e->getMessage();
@ -178,9 +189,12 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
<h2 class="text-center mb-4">Installer</h2>
<div class="text-center step-indicator">
<?php for ($i = 1; $i <= 5; $i++): ?>
<span class="step-dot <?= $i == $step ? 'active' : ($i < $step ? 'completed' : '') ?>"><?= $i ?></span>
<?php endfor; ?>
<?php for ($i = 1; $i <= 5; $i++):
echo "<span class=\"step-dot ";
if ($i == $step) echo "active";
elseif ($i < $step) echo "completed";
echo "\">$i</span>\n";
endfor; ?>
</div>
<?php if ($error): ?>
@ -194,23 +208,23 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
<?php if ($step === 1): ?>
<h4>Step 1: System Requirements</h4>
<ul class="list-group mb-4">
<?php foreach ($requirements as $name => $met): ?>
<li class="list-group-item d-flex justify-content-between align-items-center">
<?= $name ?>
<?php if ($met): ?>
<span class="badge bg-success rounded-pill">OK</span>
<?php else: ?>
<span class="badge bg-danger rounded-pill">Failed</span>
<?php endif; ?>
</li>
<?php endforeach; ?>
<?php foreach ($requirements as $name => $met):
echo "<li class=\"list-group-item d-flex justify-content-between align-items-center\">";
echo "$name\n";
if ($met) {
echo "<span class=\"badge bg-success rounded-pill\">OK</span>";
} else {
echo "<span class=\"badge bg-danger rounded-pill\">Failed</span>";
}
echo "</li>\n";
endforeach; ?>
</ul>
<div class="d-grid">
<?php if ($all_requirements_met): ?>
<a href="install.php?step=2" class="btn btn-primary">Next: Database Config</a>
<?php else: ?>
<button class="btn btn-secondary" disabled>Fix requirements to continue</button>
<?php endif; ?>
<?php else:
echo "<button class=\"btn btn-secondary\" disabled>Fix requirements to continue</button>";
endif; ?>
</div>
<?php elseif ($step === 2): ?>