-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.php
More file actions
359 lines (307 loc) · 13.3 KB
/
index.php
File metadata and controls
359 lines (307 loc) · 13.3 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<?php
session_start();
require_once 'config.php';
// Function to get user's IP address
function getUserIP() {
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
return $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
return $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
return $_SERVER['REMOTE_ADDR'];
}
}
// Function to get user's browser details
function getUserAgent() {
return $_SERVER['HTTP_USER_AGENT'];
}
// Function to detect device type
function getDeviceType() {
$agent = strtolower($_SERVER['HTTP_USER_AGENT']);
if (strpos($agent, 'mobile') !== false) return 'Mobile';
elseif (strpos($agent, 'tablet') !== false) return 'Tablet';
else return 'Desktop';
}
// Function to detect OS
function getOperatingSystem() {
$agent = strtolower($_SERVER['HTTP_USER_AGENT']);
if (strpos($agent, 'windows') !== false) return 'Windows';
elseif (strpos($agent, 'mac') !== false) return 'MacOS';
elseif (strpos($agent, 'linux') !== false) return 'Linux';
elseif (strpos($agent, 'android') !== false) return 'Android';
elseif (strpos($agent, 'iphone') !== false) return 'iOS';
else return 'Unknown';
}
// 🚀 Redirect logged-in users
if (isset($_SESSION['username'])) {
header("Location: " . ($_SESSION['is_admin'] ? './admin/index.php' : 'dashboard.php'));
exit;
}
$error = ''; // Initialize error variable to prevent undefined warnings
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = trim($_POST['username']);
$password = $_POST['password'];
$ip_address = getUserIP();
$user_agent = getUserAgent();
$device_type = getDeviceType();
$operating_system = getOperatingSystem();
$session_id = session_id();
$cookies = json_encode($_COOKIE); // Save cookies as JSON
$failed_attempts = 0;
if (isset($_POST['register'])) {
$licenseKey = trim($_POST['license_key']);
$stmt = $conn->prepare("SELECT * FROM license_keys WHERE key_value = ? AND is_used = 0");
$stmt->execute([$licenseKey]);
$key = $stmt->fetch(PDO::FETCH_ASSOC);
if ($key) {
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
// ✅ Get the next UID (Sequential number)
$stmt = $conn->prepare("SELECT MAX(uid) AS max_uid FROM users");
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$nextUid = $result['max_uid'] ? $result['max_uid'] + 1 : 1; // Start from 1 if empty
// ✅ Insert user with UID
$stmt = $conn->prepare("INSERT INTO users (uid, username, password, license_key) VALUES (?, ?, ?, ?)");
if ($stmt->execute([$nextUid, $username, $hashedPassword, $licenseKey])) {
$conn->prepare("UPDATE license_keys SET is_used = 1 WHERE id = ?")->execute([$key['id']]);
$error = '<div class="alert alert-success">User registered successfully. Please log in.</div>';
} else {
$error = '<div class="alert alert-danger">Error registering user.</div>';
}
} else {
$error = '<div class="alert alert-warning">Invalid or already used license key.</div>';
}
} elseif (isset($_POST['login'])) {
$stmt = $conn->prepare("SELECT * FROM users WHERE username = ?");
$stmt->execute([$username]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
if ($user) {
// Check if account is banned/suspended
if ($user['status'] != 'Active') {
$error = '<div class="alert alert-danger">Your account is ' . htmlspecialchars($user['status']) . '.</div>';
}
// Check if user has exceeded login attempts
elseif ($user['failed_login_attempts'] >= 5) {
$error = '<div class="alert alert-danger">Too many failed login attempts. Try again later.</div>';
}
elseif (password_verify($password, $user['password'])) {
$_SESSION['username'] = $username;
$_SESSION['is_admin'] = (int) $user['is_admin']; // Ensure it is properly set as integer
// ✅ Log Successful Login
$logStmt = $conn->prepare("INSERT INTO login_logs (username, session_id, ip_address, user_agent, device_type, operating_system, cookies) VALUES (?, ?, ?, ?, ?, ?, ?)");
$logStmt->execute([$username, $session_id, $ip_address, $user_agent, $device_type, $operating_system, $cookies]);
// ✅ Reset failed attempts & update last login
$conn->prepare("UPDATE users SET failed_login_attempts = 0, last_login = NOW() WHERE username = ?")->execute([$username]);
header("Location: " . ($_SESSION['is_admin'] ? './admin/index.php' : 'dashboard.php'));
exit;
} else {
// Log failed attempt
$failed_attempts = $user['failed_login_attempts'] + 1;
$conn->prepare("UPDATE users SET failed_login_attempts = ? WHERE username = ?")->execute([$failed_attempts, $username]);
$logStmt = $conn->prepare("INSERT INTO login_logs (username, session_id, ip_address, user_agent, device_type, operating_system, cookies, failed_attempt) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
$logStmt->execute([$username, $session_id, $ip_address, $user_agent, $device_type, $operating_system, $cookies, $failed_attempts]);
$error = '<div class="alert alert-danger">Invalid username or password.</div>';
}
} else {
$error = '<div class="alert alert-danger">Invalid username or password.</div>';
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome - Login or Register</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap" rel="stylesheet">
<style>
body {
background: linear-gradient(135deg, #0f0c29, #302b63, #24243e);
color: #fff;
font-family: 'Inter', sans-serif;
min-height: 100vh;
margin: 0;
display: flex;
align-items: center;
justify-content: center;
padding: 20px;
}
.auth-container {
width: 100%;
max-width: 450px;
margin: auto;
}
.auth-card {
background: rgba(255, 255, 255, 0.05);
backdrop-filter: blur(10px);
border-radius: 20px;
padding: 40px;
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}
.auth-header {
text-align: center;
margin-bottom: 35px;
}
.auth-header h2 {
font-weight: 600;
font-size: 28px;
margin-bottom: 10px;
background: linear-gradient(135deg, #fff, #a5a5a5);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
.auth-header p {
color: #a5a5a5;
margin: 0;
}
.form-control {
background: rgba(255, 255, 255, 0.08);
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 12px;
color: #fff;
padding: 12px 16px;
height: auto;
transition: all 0.3s ease;
}
.form-control:focus {
background: rgba(255, 255, 255, 0.12);
border-color: rgba(255, 255, 255, 0.2);
box-shadow: 0 0 0 4px rgba(255, 255, 255, 0.1);
color: #fff;
}
.form-label {
color: #a5a5a5;
font-size: 14px;
margin-bottom: 8px;
}
.input-group-text {
background: rgba(255, 255, 255, 0.08);
border: 1px solid rgba(255, 255, 255, 0.1);
color: #a5a5a5;
}
.btn-auth {
width: 100%;
padding: 12px;
border-radius: 12px;
font-weight: 500;
transition: all 0.3s ease;
text-transform: uppercase;
letter-spacing: 0.5px;
}
.btn-login {
background: linear-gradient(135deg, #6366f1, #8b5cf6);
border: none;
color: white;
}
.btn-register {
background: transparent;
border: 1px solid rgba(255, 255, 255, 0.2);
color: #fff;
}
.btn-login:hover {
background: linear-gradient(135deg, #5a5ff9, #7c4ef3);
transform: translateY(-2px);
}
.btn-register:hover {
background: rgba(255, 255, 255, 0.05);
transform: translateY(-2px);
}
.form-floating {
margin-bottom: 20px;
}
.alert {
border-radius: 12px;
padding: 16px;
margin-bottom: 25px;
border: none;
}
.alert-danger {
background: rgba(220, 38, 38, 0.1);
color: #ef4444;
}
.alert-success {
background: rgba(34, 197, 94, 0.1);
color: #22c55e;
}
.alert-warning {
background: rgba(234, 179, 8, 0.1);
color: #eab308;
}
.toggle-view {
text-align: center;
margin-top: 20px;
color: #a5a5a5;
font-size: 14px;
}
.toggle-view a {
color: #fff;
text-decoration: none;
font-weight: 500;
}
.toggle-view a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<div class="auth-container">
<div class="auth-card">
<div class="auth-header">
<h2>Welcome Back</h2>
<p>Please enter your details to continue</p>
</div>
<?php echo $error; ?>
<form action="index.php" method="post" id="authForm">
<div class="form-floating mb-3">
<input type="text" class="form-control" id="username" name="username" placeholder="Username" required>
<label for="username">Username</label>
</div>
<div class="form-floating mb-3">
<input type="password" class="form-control" id="password" name="password" placeholder="Password" required>
<label for="password">Password</label>
</div>
<div class="form-floating mb-4 license-key-field" style="display: none;">
<input type="text" class="form-control" id="license_key" name="license_key" placeholder="License Key">
<label for="license_key">License Key</label>
</div>
<button type="submit" class="btn btn-auth btn-login mb-3" name="login">Sign In</button>
<button type="submit" class="btn btn-auth btn-register" name="register" style="display: none;">Create Account</button>
<div class="toggle-view">
<span class="login-text">Don't have an account? <a href="#" onclick="toggleView('register'); return false;">Register</a></span>
<span class="register-text" style="display: none;">Already have an account? <a href="#" onclick="toggleView('login'); return false;">Login</a></span>
</div>
</form>
</div>
</div>
<script>
function toggleView(view) {
const licenseKeyField = document.querySelector('.license-key-field');
const loginButton = document.querySelector('.btn-login');
const registerButton = document.querySelector('.btn-register');
const loginText = document.querySelector('.login-text');
const registerText = document.querySelector('.register-text');
const header = document.querySelector('.auth-header h2');
const subheader = document.querySelector('.auth-header p');
if (view === 'register') {
licenseKeyField.style.display = 'block';
loginButton.style.display = 'none';
registerButton.style.display = 'block';
loginText.style.display = 'none';
registerText.style.display = 'block';
header.textContent = 'Create Account';
subheader.textContent = 'Please fill in your information';
} else {
licenseKeyField.style.display = 'none';
loginButton.style.display = 'block';
registerButton.style.display = 'none';
loginText.style.display = 'block';
registerText.style.display = 'none';
header.textContent = 'Welcome Back';
subheader.textContent = 'Please enter your details to continue';
}
}
</script>
</body>
</html>