34765-vm/installer/install.php
Flatlogic Bot 2f8b1db200 rep
2025-10-07 18:45:08 +00:00

153 lines
5.5 KiB
PHP

<?php
$db_host = '127.0.0.1';
$db_user = 'root';
$db_pass = 'root';
$db_name = 'service_portal_db';
$sql_file = __DIR__ . '/../database/demo.sql';
$message = '';
$error = false;
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['install'])) {
try {
// 1. Connect to MySQL server
$pdo = new PDO("mysql:host=$db_host", $db_user, $db_pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// 2. Read SQL file
$sql = file_get_contents($sql_file);
if ($sql === false) {
throw new Exception("Cannot read SQL file: $sql_file");
}
// 3. Execute SQL commands
$pdo->exec($sql);
$message = 'Installation successful! Database and tables created, and demo data imported.';
$error = false;
} catch (PDOException $e) {
$message = "Database error: " . $e->getMessage();
$error = true;
} catch (Exception $e) {
$message = "General error: " . $e->getMessage();
$error = true;
}
}
$extensions = [
'pdo_mysql' => 'PDO MySQL for database connection',
'gd' => 'GD for image processing',
'mbstring' => 'MBString for multibyte strings',
'openssl' => 'OpenSSL for security',
'json' => 'JSON for data interchange',
'fileinfo' => 'FileInfo for file type detection',
'zip' => 'Zip for archive handling',
'dom' => 'DOM for XML/HTML processing',
'ctype' => 'Ctype for character type checking',
];
$php_version = phpversion();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Service Portal Installer</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<style>
body {
background-color: #F4F7F6;
font-family: 'Helvetica Neue', Arial, sans-serif;
}
.installer-container {
max-width: 800px;
margin: 50px auto;
background: #FFFFFF;
padding: 40px;
border-radius: 0.5rem;
box-shadow: 0 10px 30px rgba(0,0,0,0.1);
}
h1, h2 {
font-family: 'Georgia', serif;
color: #333;
}
.btn-primary {
background-image: linear-gradient(135deg, #4A90E2, #50E3C2);
border: none;
padding: 15px 30px;
font-size: 1.2rem;
}
.list-group-item {
display: flex;
justify-content: space-between;
align-items: center;
}
.badge-success { background-color: #50E3C2; color: white; }
.badge-danger { background-color: #E24A4A; color: white; }
</style>
</head>
<body>
<div class="installer-container">
<h1 class="text-center mb-4">Service Portal Installer</h1>
<div class="card mb-4">
<div class="card-header"><h2>1. System Requirements</h2></div>
<div class="card-body">
<ul class="list-group">
<li class="list-group-item">
PHP Version (>= 8.0)
<?php if (version_compare($php_version, '8.0.0', '>=')): ?>
<span class="badge badge-success">✅ (<?php echo $php_version; ?>)</span>
<?php else: ?>
<span class="badge badge-danger">❌ (<?php echo $php_version; ?>)</span>
<?php endif; ?>
</li>
<?php foreach ($extensions as $ext => $desc): ?>
<li class="list-group-item">
<?php echo $desc; ?> (<?php echo $ext; ?>)
<?php if (extension_loaded($ext)): ?>
<span class="badge badge-success">✅ Installed</span>
<?php else: ?>
<span class="badge badge-danger">❌ Not Installed</span>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
<div class="card">
<div class="card-header"><h2>2. Database Installation</h2></div>
<div class="card-body text-center">
<p>This will create the `<?php echo $db_name; ?>` database and import the necessary tables and demo data.</p>
<p class="text-muted">Warning: This may overwrite existing data if tables with the same name exist.</p>
<form method="POST">
<button type="submit" name="install" class="btn btn-primary">Install Now</button>
</form>
</div>
</div>
</div>
<?php if ($_SERVER['REQUEST_METHOD'] === 'POST' && $message): ?>
<script>
document.addEventListener('DOMContentLoaded', function() {
Swal.fire({
title: '<?php echo $error ? "Error!" : "Success!"; ?>',
text: '<?php echo addslashes($message); ?>',
icon: '<?php echo $error ? "error" : "success"; ?>',
confirmButtonText: 'OK'
}).then(() => {
if (!<?php echo $error ? 'true' : 'false'; ?>) {
// Optional: redirect to home or admin page
// window.location.href = '/';
}
});
});
</script>
<?php endif; ?>
</body>
</html>