PDO::ERRMODE_EXCEPTION, "; $content .= " PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, "; $content .= " ]);\n"; $content .= " } catch (PDOException $e) {\n"; $content .= " die('Database connection failed: ' . $e->getMessage());\n"; $content .= " }\n"; $content .= " }\n"; $content .= " return $pdo; "; $content .= "}\n\n"; // Include existing helpers if needed, or keep it clean $content .= "/** * Generates the next reference number */\n"; $content .= "function generateRefNo($type) {\n"; $content .= " $prefix = 'IN'; "; $content .= " if ($type === 'outbound') $prefix = 'OUT'; "; $content .= " if ($type === 'internal') $prefix = 'INT'; "; $content .= " $year = date('Y'); "; $content .= " $pattern = $prefix . '-' . $year . '-%'; "; $content .= " $stmt = db()->prepare(\"SELECT ref_no FROM mailbox WHERE type = ? AND ref_no LIKE ? ORDER BY id DESC LIMIT 1\"); "; $content .= " $stmt->execute([$type, $pattern]); "; $content .= " $last_ref = $stmt->fetchColumn(); "; $content .= " $serial = 1; "; $content .= " if ($last_ref) {\n"; $content .= " $parts = explode('-', $last_ref); "; $content .= " if (count($parts) === 3) {\n"; $content .= " $serial = (int)$parts[2] + 1; "; $content .= " } "; $content .= " } "; $content .= " return $prefix . '-' . $year . '-' . str_pad($serial, 3, '0', STR_PAD_LEFT); "; $content .= "} "; return file_put_contents(__DIR__ . '/db/config.php', $content); } // Step 2: Database Connection Test & Save if ($step == 2 && $_SERVER['REQUEST_METHOD'] == 'POST') { $host = $_POST['db_host'] ?? ''; $name = $_POST['db_name'] ?? ''; $user = $_POST['db_user'] ?? ''; $pass = $_POST['db_pass'] ?? ''; try { $pdo = new PDO("mysql:host=$host;dbname=$name;charset=utf8mb4", $user, $pass); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); if (update_config($host, $name, $user, $pass)) { header('Location: install.php?step=3'); exit; } else { $error = "Failed to write to db/config.php. Check permissions."; } } catch (PDOException $e) { $error = "Connection failed: " . $e->getMessage(); } } // Step 3: Run Migrations if ($step == 3 && isset($_POST['run_migrations'])) { try { require_once $config_file; $pdo = db(); $migrations_dir = __DIR__ . '/db/migrations'; $files = glob($migrations_dir . '/*.sql'); sort($files); foreach ($files as $file) { $sql = file_get_contents($file); // Split by semicolon to execute multiple statements if needed, // but PDO::exec usually handles multiple statements in one call for SQL scripts. $pdo->exec($sql); } $success = "Migrations completed successfully!"; header('Location: install.php?step=4'); exit; } catch (Exception $e) { $error = "Migration error: " . $e->getMessage(); } } // Step 4: Super Admin Setup if ($step == 4 && $_SERVER['REQUEST_METHOD'] == 'POST') { $admin_user = $_POST['admin_user'] ?? ''; $admin_pass = $_POST['admin_pass'] ?? ''; $admin_email = $_POST['admin_email'] ?? ''; if (empty($admin_user) || empty($admin_pass)) { $error = "Username and password are required."; } else { try { require_once $config_file; $pdo = db(); $hash = password_hash($admin_pass, PASSWORD_DEFAULT); // Check if user exists $stmt = $pdo->prepare("SELECT id FROM users WHERE username = ?"); $stmt->execute([$admin_user]); $existing = $stmt->fetch(); if ($existing) { $stmt = $pdo->prepare("UPDATE users SET password = ?, email = ?, is_super_admin = 1 WHERE id = ?"); $stmt->execute([$hash, $admin_email, $existing['id']]); } else { $stmt = $pdo->prepare("INSERT INTO users (username, password, email, is_super_admin) VALUES (?, ?, ?, 1)"); $stmt->execute([$admin_user, $hash, $admin_email]); } // Set some default settings if table exists $pdo->exec("INSERT IGNORE INTO charity_settings (id, site_name) VALUES (1, 'Admin Panel')"); $_SESSION['installed'] = true; header('Location: install.php?step=5'); exit; } catch (Exception $e) { $error = "Admin setup error: " . $e->getMessage(); } } } ?>
Ready to set up the database tables and initial data.
The application has been installed successfully.
install.php file from your server.