forked from imabutahersiddik/CodeStore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.php
More file actions
102 lines (97 loc) · 3.89 KB
/
install.php
File metadata and controls
102 lines (97 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
session_start();
require_once 'config.php';
// Check if admin_users table exists, create if not
$stmt = $pdo->query("SHOW TABLES LIKE 'admin_users'");
$hasAdminTable = $stmt->rowCount() > 0;
if (!$hasAdminTable) {
$pdo->exec("
CREATE TABLE IF NOT EXISTS admin_users (
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50) NOT NULL UNIQUE,
password_hash VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
");
}
// Check if an admin exists
$stmt = $pdo->prepare("SELECT COUNT(*) FROM admin_users");
$stmt->execute();
$adminExists = $stmt->fetchColumn() > 0;
$success = false;
$error = null;
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['install'])) {
$username = trim($_POST['username'] ?? '');
$password = $_POST['password'] ?? '';
$password2 = $_POST['password2'] ?? '';
// Validation
if ($adminExists) {
$error = "An admin account already exists.";
} elseif (!$username || strlen($username) < 3) {
$error = "Username must be at least 3 characters.";
} elseif (!$password || strlen($password) < 6) {
$error = "Password must be at least 6 characters.";
} elseif ($password !== $password2) {
$error = "Passwords do not match.";
} else {
// Insert admin user
$hash = password_hash($password, PASSWORD_DEFAULT);
$stmt = $pdo->prepare("INSERT INTO admin_users (username, password_hash) VALUES (?, ?)");
$stmt->execute([$username, $hash]);
$success = true;
$adminExists = true;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Install Admin Account</title>
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body { background: #f5f5f5; }
.install-container { max-width: 400px; margin: 80px auto; }
</style>
</head>
<body>
<div class="install-container">
<div class="card shadow">
<div class="card-body">
<h2 class="mb-4">Admin Account Installation</h2>
<?php if ($adminExists && !$success): ?>
<div class="alert alert-info">
Admin account already exists.<br>
<a href="admin_login.php" class="btn btn-primary mt-3">Go to Admin Login</a>
</div>
<?php elseif ($success): ?>
<div class="alert alert-success">
Admin account has been created!<br>
<a href="admin_login.php" class="btn btn-success mt-3">Go to Admin Login</a>
</div>
<?php else: ?>
<?php if ($error): ?>
<div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div>
<?php endif; ?>
<form method="post" autocomplete="off">
<div class="mb-3">
<label class="form-label">Admin Username</label>
<input type="text" name="username" class="form-control" required value="<?php echo htmlspecialchars($_POST['username'] ?? '') ?>">
</div>
<div class="mb-3">
<label class="form-label">Admin Password</label>
<input type="password" name="password" class="form-control" required>
</div>
<div class="mb-3">
<label class="form-label">Repeat Password</label>
<input type="password" name="password2" class="form-control" required>
</div>
<button type="submit" name="install" class="btn btn-primary w-100">Create Admin Account</button>
</form>
<?php endif; ?>
</div>
</div>
</div>
</body>
</html>