= 7.4' => version_compare(PHP_VERSION, '7.4.0', '>='),
'PDO Extension' => extension_loaded('pdo'),
'PDO MySQL Extension' => extension_loaded('pdo_mysql'),
'Config Directory Writable' => is_writable(__DIR__ . '/../db/'),
'Uploads Directory Writable' => is_writable(__DIR__ . '/../uploads/') || (mkdir(__DIR__ . '/../uploads/', 0777, true) && is_writable(__DIR__ . '/../uploads/')),
];
$all_requirements_met = !in_array(false, $requirements, true);
// Current step
$step = isset($_GET['step']) ? (int)$_GET['step'] : 1;
// Handle form submissions
$error = '';
$success = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if ($step === 2) {
// Save database configuration
$host = $_POST['db_host'] ?? '127.0.0.1';
$name = $_POST['db_name'] ?? 'app_database';
$user = $_POST['db_user'] ?? 'root';
$pass = $_POST['db_pass'] ?? '';
// Test connection
try {
$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 = "setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
";
$content .= " \$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
";
$content .= " } catch (PDOException \$e) {\n";
$content .= " die('Connection failed: ' . \$e->getMessage());\n";
$content .= " }\n";
$content .= " }\n";
$content .= " return \$pdo;\n";
$content .= " }\n";
$content .= "}\n";
if (file_put_contents($config_file, $content)) {
header('Location: install.php?step=3');
exit;
} else {
$error = "Failed to write configuration file to $config_file. Please check permissions.";
}
} catch (PDOException $e) {
$error = "Connection failed: " . $e->getMessage();
}
} elseif ($step === 3) {
// Run migrations
if (!file_exists($config_file)) {
$error = "Configuration file not found. Please go back to Step 2.";
} else {
try {
require_once $config_file;
if (!function_exists('db')) {
throw new Exception("The 'db()' function is not defined in your config file.");
}
$pdo = db();
$migrations_dir = __DIR__ . '/../db/migrations/';
$files = glob($migrations_dir . '*.sql');
if ($files === false) $files = [];
sort($files);
$applied = 0;
$errors = [];
foreach ($files as $file) {
$sql = file_get_contents($file);
if (empty($sql)) continue;
try {
// Split SQL into multiple statements if necessary
$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();
if (strpos($msg, 'Duplicate column name') !== false ||
strpos($msg, 'Duplicate key name') !== false ||
strpos($msg, 'Duplicate table') !== false ||
strpos($msg, 'already exists') !== false) {
continue;
} else {
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 migrations, but some errors occurred:
We will now run the SQL scripts to set up your database tables.
The system is ready to use. For security, please delete install.php or rename it.