39 lines
1.4 KiB
PHP
39 lines
1.4 KiB
PHP
<?php
|
|
require_once __DIR__ . '/config.php';
|
|
|
|
function setup_database() {
|
|
try {
|
|
$pdo = db();
|
|
// Users table
|
|
$pdo->exec("CREATE TABLE IF NOT EXISTS `users` (
|
|
`id` INT AUTO_INCREMENT PRIMARY KEY,
|
|
`username` VARCHAR(255) NOT NULL UNIQUE,
|
|
`email` VARCHAR(255) NOT NULL UNIQUE,
|
|
`password` VARCHAR(255) NOT NULL,
|
|
`role` ENUM('superadmin', 'reseller', 'subscriber') NOT NULL DEFAULT 'subscriber',
|
|
`status` ENUM('active', 'blocked') NOT NULL DEFAULT 'active',
|
|
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
);");
|
|
|
|
// Check if a superadmin exists
|
|
$stmt = $pdo->query("SELECT COUNT(*) FROM `users` WHERE `role` = 'superadmin'");
|
|
if ($stmt->fetchColumn() == 0) {
|
|
// Create a default superadmin
|
|
$username = 'admin';
|
|
$email = 'admin@example.com';
|
|
$password = password_hash('password', PASSWORD_DEFAULT); // Change this!
|
|
$role = 'superadmin';
|
|
|
|
$insert_stmt = $pdo->prepare("INSERT INTO `users` (username, email, password, role) VALUES (?, ?, ?, ?)");
|
|
$insert_stmt->execute([$username, $email, $password, $role]);
|
|
}
|
|
|
|
} catch (PDOException $e) {
|
|
// In a real app, you'd log this error.
|
|
// For setup, we can die to see the error.
|
|
die("Database setup failed: " . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
setup_database();
|
|
?>
|