PDO::ERRMODE_EXCEPTION, "; $content .= " PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, "; $content .= " ]); "; $content .= " } "; $content .= " return $pdo; "; $content .= " } "; $content .= "} "; return file_put_contents(__DIR__ . '/db/config.php', $content); } // Handle Form Submissions if ($_SERVER['REQUEST_METHOD'] === 'POST') { // Determine step from hidden field or GET, defaulting to 1 $postStep = isset($_POST['step']) ? (int)$_POST['step'] : $step; if ($postStep == 1) { $host = $_POST['db_host'] ?? ''; $name = $_POST['db_name'] ?? ''; $user = $_POST['db_user'] ?? ''; $pass = $_POST['db_pass'] ?? ''; try { // Test Connection $dsn = "mysql:host=$host;dbname=$name;charset=utf8mb4"; $testPdo = new PDO($dsn, $user, $pass, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]); // If successful, write config if (write_db_config($host, $name, $user, $pass)) { $message = "Database connection established and saved!"; $messageType = "success"; // Redirect to avoid resubmission header("Location: install.php?step=2"); exit; } else { $message = "Could not write to db/config.php. Check permissions."; $messageType = "danger"; } } catch (PDOException $e) { $message = "Connection failed: " . $e->getMessage(); $messageType = "danger"; } } elseif ($postStep == 2) { // Step 2: Create Admin & Run Migrations // Include config and migration script if (file_exists(__DIR__ . '/db/config.php')) { require_once __DIR__ . '/db/config.php'; } else { $message = "Configuration file missing. Please go back to Step 1."; $messageType = "danger"; $step = 1; } if (file_exists(__DIR__ . '/db/migrate.php')) { require_once __DIR__ . '/db/migrate.php'; } $email = $_POST['admin_email'] ?? ''; $password = $_POST['admin_pass'] ?? ''; $fullName = $_POST['admin_name'] ?? 'Administrator'; if ($email && $password && defined('DB_HOST')) { try { // 1. Run Migrations $migrationResults = []; if (function_exists('run_migrations')) { $migrationResults = run_migrations(); } // 2. Also ensure basic schema from app.php (just in case) // We do this after migrations so migrations take precedence if they exist if (file_exists(__DIR__ . '/includes/app.php')) { // We catch output to prevent it from messing up headers/layout if app.php has echoes ob_start(); require_once __DIR__ . '/includes/app.php'; if (function_exists('ensure_schema')) { ensure_schema(); } ob_end_clean(); } // 3. Create Admin User $pdo = db(); // Check if admin exists $stmt = $pdo->prepare("SELECT id FROM users WHERE email = ?"); $stmt->execute([$email]); if ($stmt->fetch()) { // Update existing $stmt = $pdo->prepare("UPDATE users SET password = ?, full_name = ?, role = 'admin', status = 'active' WHERE email = ?"); $stmt->execute([password_hash($password, PASSWORD_DEFAULT), $fullName, $email]); } else { // Create new $stmt = $pdo->prepare("INSERT INTO users (email, password, full_name, role, status) VALUES (?, ?, ?, 'admin', 'active')"); $stmt->execute([$email, password_hash($password, PASSWORD_DEFAULT), $fullName]); } $migMsg = implode("
", $migrationResults); $message = "Admin account created successfully!
$migMsg"; $messageType = "success"; $step = 3; // Success page } catch (Exception $e) { $message = "Error: " . $e->getMessage(); $messageType = "danger"; } catch (Throwable $e) { $message = "Fatal Error: " . $e->getMessage(); $messageType = "danger"; } } else { $message = "Please fill in all fields."; $messageType = "danger"; } } } // Load current config values for Step 1 $current_db_host = '127.0.0.1'; $current_db_name = 'app'; $current_db_user = 'root'; $current_db_pass = ''; if (file_exists(__DIR__ . '/db/config.php')) { include __DIR__ . '/db/config.php'; if (defined('DB_HOST')) $current_db_host = DB_HOST; if (defined('DB_NAME')) $current_db_name = DB_NAME; if (defined('DB_USER')) $current_db_user = DB_USER; if (defined('DB_PASS')) $current_db_pass = DB_PASS; } ?> Installation - Step <?php echo $step; ?>

Application Installer

1. Database2. Admin User3. Finish
Current configuration is valid! Skip to Step 2
'; } catch (PDOException $e) { // Silent fail, user sees form } } ?>

Installation Complete!

You can now login to your admin dashboard.

Go to Login