Autosave: 20260301-005825
This commit is contained in:
parent
6d7a771341
commit
5fae92efe8
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
|||||||
node_modules/
|
node_modules/
|
||||||
*/node_modules/
|
*/node_modules/
|
||||||
*/build/
|
*/build/
|
||||||
|
db/.install_lock
|
||||||
|
|||||||
@ -11,17 +11,27 @@ $users_count = 0;
|
|||||||
$lpas_count = 0;
|
$lpas_count = 0;
|
||||||
$users = [];
|
$users = [];
|
||||||
$lpas = [];
|
$lpas = [];
|
||||||
|
$migration_history = [];
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
$db = db();
|
||||||
// Get stats
|
// Get stats
|
||||||
$users_count = db()->query("SELECT COUNT(*) FROM users")->fetchColumn();
|
$users_count = $db->query("SELECT COUNT(*) FROM users")->fetchColumn();
|
||||||
$lpas_count = db()->query("SELECT COUNT(*) FROM lpa_applications")->fetchColumn();
|
$lpas_count = $db->query("SELECT COUNT(*) FROM lpa_applications")->fetchColumn();
|
||||||
|
|
||||||
// Get all users
|
// Get all users
|
||||||
$users = db()->query("SELECT * FROM users ORDER BY created_at DESC LIMIT 50")->fetchAll();
|
$users = $db->query("SELECT * FROM users ORDER BY created_at DESC LIMIT 50")->fetchAll();
|
||||||
|
|
||||||
// Get all LPAs
|
// Get all LPAs
|
||||||
$lpas = db()->query("SELECT * FROM lpa_applications ORDER BY created_at DESC LIMIT 50")->fetchAll();
|
$lpas = $db->query("SELECT * FROM lpa_applications ORDER BY created_at DESC LIMIT 50")->fetchAll();
|
||||||
|
|
||||||
|
// Get migration history
|
||||||
|
try {
|
||||||
|
$migration_history = $db->query("SELECT * FROM migration_history ORDER BY executed_at DESC")->fetchAll();
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
// migration_history might not exist yet if installer hasn't run or table not created
|
||||||
|
$migration_history = [];
|
||||||
|
}
|
||||||
} catch (PDOException $e) {
|
} catch (PDOException $e) {
|
||||||
error_log($e->getMessage());
|
error_log($e->getMessage());
|
||||||
}
|
}
|
||||||
@ -49,11 +59,35 @@ try {
|
|||||||
</nav>
|
</nav>
|
||||||
|
|
||||||
<div class="container py-5">
|
<div class="container py-5">
|
||||||
<div class="row mb-5">
|
<div class="row mb-5 align-items-center">
|
||||||
<div class="col">
|
<div class="col-md-6">
|
||||||
<h1 class="h3 fw-bold mb-1">System Administration</h1>
|
<h1 class="h3 fw-bold mb-1">System Administration</h1>
|
||||||
<p class="text-muted small mb-0">Overview of all users and applications in the system.</p>
|
<p class="text-muted small mb-0">Overview of all users and applications in the system.</p>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="col-md-6 text-md-end mt-3 mt-md-0">
|
||||||
|
<div class="dropdown d-inline-block">
|
||||||
|
<button class="btn btn-white border shadow-sm rounded-pill px-4 dropdown-toggle" type="button" id="maintenanceDropdown" data-bs-toggle="dropdown" aria-expanded="false">
|
||||||
|
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="me-1"><circle cx="12" cy="12" r="3"></circle><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1 0 2.83 2 2 0 0 1-2.83 0l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-2 2 2 2 0 0 1-2-2v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83 0 2 2 0 0 1 0-2.83l.06-.06a1.65 1.65 0 0 0 .33-1.82 1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1-2-2 2 2 0 0 1 2-2h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 0-2.83 2 2 0 0 1 2.83 0l.06.06a1.65 1.65 0 0 0 1.82.33H9a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 2-2 2 2 0 0 1 2 2v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 0 2 2 0 0 1 0 2.83l-.06.06a1.65 1.65 0 0 0-.33 1.82V9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 2 2 2 2 0 0 1-2 2h-.09a1.65 1.65 0 0 0-1.51 1z"></path></svg>
|
||||||
|
System Tools
|
||||||
|
</button>
|
||||||
|
<ul class="dropdown-menu dropdown-menu-end border-0 shadow-lg rounded-3" aria-labelledby="maintenanceDropdown">
|
||||||
|
<li><h6 class="dropdown-header">Database Maintenance</h6></li>
|
||||||
|
<li>
|
||||||
|
<button class="dropdown-item py-2" type="button" onclick="runMigrations()">
|
||||||
|
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="me-2 text-primary"><polyline points="16 16 12 12 8 16"></polyline><line x1="12" y1="12" x2="12" y2="21"></line><path d="M20.39 18.39A5 5 0 0 0 18 9h-1.26A8 8 0 1 0 3 16.3"></path><polyline points="16 16 12 12 8 16"></polyline></svg>
|
||||||
|
Run Pending Migrations
|
||||||
|
</button>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="migration-alert" style="display: none;">
|
||||||
|
<div class="alert alert-info alert-dismissible fade show rounded-3 shadow-sm border-0 mb-4" role="alert">
|
||||||
|
<span id="migration-message"></span>
|
||||||
|
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="row g-4 mb-5">
|
<div class="row g-4 mb-5">
|
||||||
@ -108,8 +142,8 @@ try {
|
|||||||
<td class="ps-4 fw-medium"><?php echo htmlspecialchars($u['name'] ?? 'N/A'); ?></td>
|
<td class="ps-4 fw-medium"><?php echo htmlspecialchars($u['name'] ?? 'N/A'); ?></td>
|
||||||
<td><?php echo htmlspecialchars($u['email']); ?></td>
|
<td><?php echo htmlspecialchars($u['email']); ?></td>
|
||||||
<td>
|
<td>
|
||||||
<span class="badge rounded-pill <?php echo $u['role'] === 'Super User' ? 'bg-danger-subtle text-danger' : 'bg-secondary-subtle text-secondary'; ?>">
|
<span class="badge rounded-pill <?php echo ($u['role'] ?? '') === 'Super User' ? 'bg-danger-subtle text-danger' : 'bg-secondary-subtle text-secondary'; ?>">
|
||||||
<?php echo htmlspecialchars($u['role']); ?>
|
<?php echo htmlspecialchars($u['role'] ?? 'Standard User'); ?>
|
||||||
</span>
|
</span>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
@ -182,6 +216,36 @@ try {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="col-lg-12 mt-5">
|
||||||
|
<div class="card border-0 shadow-sm mb-5">
|
||||||
|
<div class="card-header bg-white py-3 border-bottom-0">
|
||||||
|
<h5 class="fw-bold mb-0">Migration History</h5>
|
||||||
|
</div>
|
||||||
|
<div class="table-responsive">
|
||||||
|
<table class="table table-hover align-middle mb-0">
|
||||||
|
<thead class="bg-light">
|
||||||
|
<tr class="small text-uppercase tracking-wider">
|
||||||
|
<th class="ps-4">File Name</th>
|
||||||
|
<th class="text-end pe-4">Executed At</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<?php if (count($migration_history) > 0): ?>
|
||||||
|
<?php foreach ($migration_history as $m): ?>
|
||||||
|
<tr>
|
||||||
|
<td class="ps-4 fw-medium"><?php echo htmlspecialchars($m['filename']); ?></td>
|
||||||
|
<td class="text-end pe-4 text-muted small"><?php echo date('M d, Y H:i', strtotime($m['executed_at'])); ?></td>
|
||||||
|
</tr>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
<?php else: ?>
|
||||||
|
<tr><td colspan="2" class="text-center py-4 text-muted small">No migration history found. Run migrations to initialize.</td></tr>
|
||||||
|
<?php endif; ?>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@ -202,7 +266,6 @@ try {
|
|||||||
const row = document.getElementById('lpa-row-' + id);
|
const row = document.getElementById('lpa-row-' + id);
|
||||||
if (row) {
|
if (row) {
|
||||||
row.remove();
|
row.remove();
|
||||||
// Update count display if needed or refresh
|
|
||||||
location.reload();
|
location.reload();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -215,6 +278,37 @@ try {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function runMigrations() {
|
||||||
|
if (!confirm('Run database migrations? This will execute all SQL files in db/migrations/ and may take a moment.')) return;
|
||||||
|
|
||||||
|
const alertBox = document.getElementById('migration-alert');
|
||||||
|
const messageEl = document.getElementById('migration-message');
|
||||||
|
|
||||||
|
fetch('api/run_migrations.php', {
|
||||||
|
method: 'POST'
|
||||||
|
})
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(data => {
|
||||||
|
alertBox.style.display = 'block';
|
||||||
|
if (data.success) {
|
||||||
|
messageEl.textContent = data.message + ' Page will reload in 2 seconds.';
|
||||||
|
messageEl.parentElement.className = 'alert alert-success alert-dismissible fade show rounded-3 shadow-sm border-0 mb-4';
|
||||||
|
setTimeout(() => location.reload(), 2000);
|
||||||
|
} else {
|
||||||
|
messageEl.textContent = 'Error: ' + data.error;
|
||||||
|
messageEl.parentElement.className = 'alert alert-danger alert-dismissible fade show rounded-3 shadow-sm border-0 mb-4';
|
||||||
|
}
|
||||||
|
// Scroll to alert
|
||||||
|
alertBox.scrollIntoView({ behavior: 'smooth' });
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
alertBox.style.display = 'block';
|
||||||
|
messageEl.textContent = 'An unexpected error occurred.';
|
||||||
|
messageEl.parentElement.className = 'alert alert-danger alert-dismissible fade show rounded-3 shadow-sm border-0 mb-4';
|
||||||
|
console.error('Error:', error);
|
||||||
|
});
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
69
api/run_migrations.php
Normal file
69
api/run_migrations.php
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
<?php
|
||||||
|
session_start();
|
||||||
|
header('Content-Type: application/json');
|
||||||
|
|
||||||
|
if (!isset($_SESSION["user_id"]) || ($_SESSION["user_role"] ?? '') !== 'Super User') {
|
||||||
|
echo json_encode(['success' => false, 'error' => 'Unauthorized']);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
require_once '../db/config.php';
|
||||||
|
|
||||||
|
try {
|
||||||
|
$db = db();
|
||||||
|
|
||||||
|
// 1. Ensure migration_history table exists
|
||||||
|
$db->exec("CREATE TABLE IF NOT EXISTS migration_history (
|
||||||
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
filename VARCHAR(255) NOT NULL UNIQUE,
|
||||||
|
executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;");
|
||||||
|
|
||||||
|
// 2. Scan migrations directory
|
||||||
|
$migrations_dir = __DIR__ . '/../db/migrations/';
|
||||||
|
$migration_files = glob($migrations_dir . '*.sql');
|
||||||
|
sort($migration_files);
|
||||||
|
|
||||||
|
// 3. Get list of already executed migrations
|
||||||
|
$stmt = $db->query("SELECT filename FROM migration_history");
|
||||||
|
$executed_migrations = $stmt->fetchAll(PDO::FETCH_COLUMN);
|
||||||
|
|
||||||
|
$executed_count = 0;
|
||||||
|
$skipped_count = 0;
|
||||||
|
$new_migrations = [];
|
||||||
|
|
||||||
|
foreach ($migration_files as $file_path) {
|
||||||
|
$filename = basename($file_path);
|
||||||
|
|
||||||
|
if (in_array($filename, $executed_migrations)) {
|
||||||
|
$skipped_count++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$sql = file_get_contents($file_path);
|
||||||
|
if ($sql) {
|
||||||
|
// Execute migration
|
||||||
|
$db->exec($sql);
|
||||||
|
|
||||||
|
// Record migration in history
|
||||||
|
$stmt = $db->prepare("INSERT INTO migration_history (filename) VALUES (?)");
|
||||||
|
$stmt->execute([$filename]);
|
||||||
|
|
||||||
|
$executed_count++;
|
||||||
|
$new_migrations[] = $filename;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
echo json_encode([
|
||||||
|
'success' => true,
|
||||||
|
'message' => "Migration process complete.",
|
||||||
|
'executed' => $executed_count,
|
||||||
|
'skipped' => $skipped_count,
|
||||||
|
'new_migrations' => $new_migrations
|
||||||
|
]);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
echo json_encode([
|
||||||
|
'success' => false,
|
||||||
|
'error' => 'Migration failed: ' . $e->getMessage()
|
||||||
|
]);
|
||||||
|
}
|
||||||
@ -1,17 +1,36 @@
|
|||||||
<?php
|
<?php
|
||||||
// Generated by setup_mariadb_project.sh — edit as needed.
|
// Generated by install.php or platform setup.
|
||||||
define('DB_HOST', '127.0.0.1');
|
|
||||||
define('DB_NAME', 'app_37684');
|
// Attempt to load current configuration
|
||||||
define('DB_USER', 'app_37684');
|
if (!defined('DB_HOST')) {
|
||||||
define('DB_PASS', '0e6af454-f0c3-4977-9e98-1a4682d0c995');
|
define('DB_HOST', '127.0.0.1');
|
||||||
|
define('DB_NAME', 'app_37684');
|
||||||
|
define('DB_USER', 'app_37684');
|
||||||
|
define('DB_PASS', '0e6af454-f0c3-4977-9e98-1a4682d0c995');
|
||||||
|
}
|
||||||
|
|
||||||
function db() {
|
function db() {
|
||||||
static $pdo;
|
static $pdo;
|
||||||
if (!$pdo) {
|
if (!$pdo) {
|
||||||
$pdo = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset=utf8mb4', DB_USER, DB_PASS, [
|
try {
|
||||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
// First, check if configuration is the default/placeholder which likely won't work elsewhere
|
||||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
if (DB_NAME === 'app_37684' && basename($_SERVER['PHP_SELF']) !== 'install.php' && !file_exists(__DIR__ . '/.install_lock')) {
|
||||||
]);
|
// Potentially redirect if it's a new environment and we haven't locked it yet
|
||||||
}
|
// But safer to just try connection first.
|
||||||
return $pdo;
|
}
|
||||||
|
|
||||||
|
$pdo = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset=utf8mb4', DB_USER, DB_PASS, [
|
||||||
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||||
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||||||
|
]);
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
// If connection fails and we are not already on the install page, redirect to install.php
|
||||||
|
if (basename($_SERVER['PHP_SELF']) !== 'install.php' && !headers_sent()) {
|
||||||
|
header('Location: install.php');
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
throw $e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $pdo;
|
||||||
}
|
}
|
||||||
|
|||||||
5
db/migrations/06_create_migration_history_table.sql
Normal file
5
db/migrations/06_create_migration_history_table.sql
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
CREATE TABLE IF NOT EXISTS migration_history (
|
||||||
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
filename VARCHAR(255) NOT NULL UNIQUE,
|
||||||
|
executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||||
216
install.php
Normal file
216
install.php
Normal file
@ -0,0 +1,216 @@
|
|||||||
|
<?php
|
||||||
|
session_start();
|
||||||
|
|
||||||
|
$lock_file = __DIR__ . '/db/.install_lock';
|
||||||
|
if (file_exists($lock_file)) {
|
||||||
|
header('Location: index.php');
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
$error = '';
|
||||||
|
$success = '';
|
||||||
|
$project_name = $_SERVER['PROJECT_NAME'] ?? 'LPA Online';
|
||||||
|
|
||||||
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||||
|
$db_host = $_POST['db_host'] ?? '127.0.0.1';
|
||||||
|
$db_name = $_POST['db_name'] ?? '';
|
||||||
|
$db_user = $_POST['db_user'] ?? '';
|
||||||
|
$db_pass = $_POST['db_pass'] ?? '';
|
||||||
|
|
||||||
|
$admin_name = $_POST['admin_name'] ?? '';
|
||||||
|
$admin_email = $_POST['admin_email'] ?? '';
|
||||||
|
$admin_pass = $_POST['admin_pass'] ?? '';
|
||||||
|
|
||||||
|
if (empty($db_name) || empty($db_user) || empty($admin_email) || empty($admin_pass)) {
|
||||||
|
$error = 'Please fill in all required fields.';
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
// 1. Test connection
|
||||||
|
$dsn = "mysql:host=$db_host;charset=utf8mb4";
|
||||||
|
$pdo = new PDO($dsn, $db_user, $db_pass, [
|
||||||
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||||
|
]);
|
||||||
|
|
||||||
|
// 2. Create database if not exists
|
||||||
|
$pdo->exec("CREATE DATABASE IF NOT EXISTS `$db_name` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
|
||||||
|
$pdo->exec("USE `$db_name` ");
|
||||||
|
|
||||||
|
// 3. Update db/config.php
|
||||||
|
$config_content = '<?php
|
||||||
|
// Generated by install.php or platform setup.
|
||||||
|
|
||||||
|
// Attempt to load current configuration
|
||||||
|
if (!defined(\'DB_HOST\")) {
|
||||||
|
define(\'DB_HOST\', ' . var_export($db_host, true) . ');
|
||||||
|
define(\'DB_NAME\', ' . var_export($db_name, true) . ');
|
||||||
|
define(\'DB_USER\', ' . var_export($db_user, true) . ');
|
||||||
|
define(\'DB_PASS\', ' . var_export($db_pass, true) . ');
|
||||||
|
}
|
||||||
|
|
||||||
|
function db() {
|
||||||
|
static $pdo;
|
||||||
|
if (!$pdo) {
|
||||||
|
try {
|
||||||
|
$pdo = new PDO(\'mysql:host=\'.DB_HOST.\' ;dbname=\'.DB_NAME.\' ;charset=utf8mb4\', DB_USER, DB_PASS, [
|
||||||
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||||
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||||||
|
]);
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
// If connection fails and we are not already on the install page, redirect to install.php
|
||||||
|
if (basename($_SERVER[\'PHP_SELF\']) !== \'install.php\' && !headers_sent()) {
|
||||||
|
header(\'Location: install.php\');
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
throw $e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $pdo;
|
||||||
|
}';
|
||||||
|
|
||||||
|
if (file_put_contents(__DIR__ . '/db/config.php', $config_content) === false) {
|
||||||
|
throw new Exception("Could not write to db/config.php. Please check file permissions.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. Ensure migration_history table exists
|
||||||
|
$pdo->exec("CREATE TABLE IF NOT EXISTS migration_history (
|
||||||
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
filename VARCHAR(255) NOT NULL UNIQUE,
|
||||||
|
executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;");
|
||||||
|
|
||||||
|
// 5. Run migrations
|
||||||
|
$migrations_dir = __DIR__ . '/db/migrations/';
|
||||||
|
$migration_files = glob($migrations_dir . '*.sql');
|
||||||
|
sort($migration_files);
|
||||||
|
|
||||||
|
// Get list of already executed migrations
|
||||||
|
$stmt = $pdo->query("SELECT filename FROM migration_history");
|
||||||
|
$executed_migrations = $stmt->fetchAll(PDO::FETCH_COLUMN);
|
||||||
|
|
||||||
|
foreach ($migration_files as $file_path) {
|
||||||
|
$filename = basename($file_path);
|
||||||
|
|
||||||
|
if (in_array($filename, $executed_migrations)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$sql = file_get_contents($file_path);
|
||||||
|
if ($sql) {
|
||||||
|
// Split SQL by ; to handle multiple statements if any
|
||||||
|
$pdo->exec($sql);
|
||||||
|
|
||||||
|
// Record migration in history
|
||||||
|
$stmt = $pdo->prepare("INSERT INTO migration_history (filename) VALUES (?)");
|
||||||
|
$stmt->execute([$filename]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 6. Create Super User
|
||||||
|
$hashed_pass = password_hash($admin_pass, PASSWORD_DEFAULT);
|
||||||
|
$stmt = $pdo->prepare("INSERT INTO users (name, email, password, is_verified, role) VALUES (?, ?, ?, 1, 'Super User') ON DUPLICATE KEY UPDATE role = 'Super User'");
|
||||||
|
$stmt->execute([$admin_name, $admin_email, $hashed_pass]);
|
||||||
|
|
||||||
|
// 7. Create lock file
|
||||||
|
file_put_contents($lock_file, date('Y-m-d H:i:s'));
|
||||||
|
|
||||||
|
$success = 'Installation successful! Admin account created. Redirecting to login...';
|
||||||
|
header('Refresh: 3; url=login.php');
|
||||||
|
} catch (Exception $e) {
|
||||||
|
$error = 'Setup failed: ' . $e->getMessage();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Install — <?php echo htmlspecialchars($project_name); ?></title>
|
||||||
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||||
|
<link href="assets/css/custom.css" rel="stylesheet">
|
||||||
|
</head>
|
||||||
|
<body class="bg-light d-flex flex-column min-vh-100">
|
||||||
|
<nav class="navbar navbar-expand-lg bg-white border-bottom shadow-sm">
|
||||||
|
<div class="container justify-content-center">
|
||||||
|
<a class="navbar-brand d-flex align-items-center m-0" href="/">
|
||||||
|
<img src="assets/pasted-20260228-235417-eedda424.png" alt="<?php echo htmlspecialchars($project_name); ?>" height="40">
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
</nav>
|
||||||
|
|
||||||
|
<main class="container py-5 flex-grow-1 d-flex align-items-center">
|
||||||
|
<div class="row justify-content-center w-100 m-0">
|
||||||
|
<div class="col-md-8 col-lg-6">
|
||||||
|
<div class="card shadow-lg border-0 p-4 p-md-5 rounded-4">
|
||||||
|
<div class="text-center mb-4">
|
||||||
|
<h1 class="h3 fw-bold">System Setup</h1>
|
||||||
|
<p class="text-muted small">Configure your database and administrator account.</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<?php if ($error): ?>
|
||||||
|
<div class="alert alert-danger small py-2"><?php echo $error; ?></div>
|
||||||
|
<?php endif; ?>
|
||||||
|
|
||||||
|
<?php if ($success): ?>
|
||||||
|
<div class="alert alert-success small py-2"><?php echo $success; ?></div>
|
||||||
|
<?php else: ?>
|
||||||
|
<form action="install.php" method="POST">
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-md-6">
|
||||||
|
<h5 class="fw-bold mb-3">Database</h5>
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="db_host" class="form-label small fw-bold">Host</label>
|
||||||
|
<input type="text" class="form-control" id="db_host" name="db_host" value="127.0.0.1" required>
|
||||||
|
</div>
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="db_name" class="form-label small fw-bold">DB Name</label>
|
||||||
|
<input type="text" class="form-control" id="db_name" name="db_name" placeholder="app_db" required>
|
||||||
|
</div>
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="db_user" class="form-label small fw-bold">User</label>
|
||||||
|
<input type="text" class="form-control" id="db_user" name="db_user" placeholder="root" required>
|
||||||
|
</div>
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="db_pass" class="form-label small fw-bold">Password</label>
|
||||||
|
<input type="password" class="form-control" id="db_pass" name="db_pass" placeholder="••••••••">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="col-md-6 border-start">
|
||||||
|
<h5 class="fw-bold mb-3">Super User</h5>
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="admin_name" class="form-label small fw-bold">Full Name</label>
|
||||||
|
<input type="text" class="form-control" id="admin_name" name="admin_name" placeholder="Administrator" required>
|
||||||
|
</div>
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="admin_email" class="form-label small fw-bold">Admin Email</label>
|
||||||
|
<input type="email" class="form-control" id="admin_email" name="admin_email" placeholder="admin@example.com" required>
|
||||||
|
</div>
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="admin_pass" class="form-label small fw-bold">Admin Password</label>
|
||||||
|
<input type="password" class="form-control" id="admin_pass" name="admin_pass" placeholder="••••••••" required>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mt-4">
|
||||||
|
<button type="submit" class="btn btn-primary btn-lg w-100 rounded-pill">Complete Installation</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
<?php endif; ?>
|
||||||
|
|
||||||
|
<div class="text-center mt-4 pt-2 border-top">
|
||||||
|
<p class="text-muted small mb-0">The installer will create the database, tables, and your admin account.</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</main>
|
||||||
|
|
||||||
|
<footer class="py-4 text-center text-muted">
|
||||||
|
<div class="container">
|
||||||
|
<p class="small mb-0">© <?php echo date('Y'); ?> <?php echo htmlspecialchars($project_name); ?>. All rights reserved.</p>
|
||||||
|
</div>
|
||||||
|
</footer>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@ -1,17 +0,0 @@
|
|||||||
<?php
|
|
||||||
require_once 'mail/MailService.php';
|
|
||||||
|
|
||||||
$to = 'neel@neel.me.uk';
|
|
||||||
$subject = 'Test Email from ' . ($_SERVER['PROJECT_NAME'] ?? 'LPA Online');
|
|
||||||
$html = '<h1>Test</h1><p>This is a test email to verify mail functionality.</p>';
|
|
||||||
$text = 'Test: This is a test email to verify mail functionality.';
|
|
||||||
|
|
||||||
echo "Sending email to $to...\n";
|
|
||||||
$res = MailService::sendMail($to, $subject, $html, $text);
|
|
||||||
|
|
||||||
if ($res['success']) {
|
|
||||||
echo "SUCCESS: Email sent successfully.\n";
|
|
||||||
} else {
|
|
||||||
echo "ERROR: " . ($res['error'] ?? 'Unknown error') . "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user