-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.php
More file actions
232 lines (192 loc) · 6.41 KB
/
admin.php
File metadata and controls
232 lines (192 loc) · 6.41 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
<?php
require_once 'config.php';
// Function to generate a 6-digit OTP
function generateOTP()
{
return strval(rand(100000, 999999));
}
// Function to send OTP
function sendOTP($email, $otp)
{
$url = 'https://python-mailsend.onrender.com/send-email';
$data = [
'to' => $email,
'subject' => 'Your OTP for Admin Login',
'body' => "Your OTP is: <b>$otp</b><br>This OTP is valid for 10 minutes."
];
$headers = [
'Content-Type: application/json'
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
$response = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http_status === 200) {
return true;
} else {
error_log("Failed to send OTP via API. Response: $response");
return false;
}
}
// Handle OTP request
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['request_otp'])) {
$email = 'satyamyadav9uv@gmail.com';
$otp = generateOTP();
// Store OTP in session with expiration time (10 minutes)
$_SESSION['admin_otp'] = $otp;
$_SESSION['admin_otp_expiry'] = time() + 600; // 10 minutes from now
if (sendOTP($email, $otp)) {
$_SESSION['otp_sent'] = true;
echo "OTP sent!";
} else {
echo "Failed to send OTP.";
}
echo "<script>window.location.href = '/admin.php';</script>";
exit();
}
// Handle OTP verification
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['verify_otp'])) {
$user_otp = $_POST['otp'] ?? '';
if (empty($user_otp)) {
$_SESSION['error'] = 'Please enter the OTP.';
} elseif (!isset($_SESSION['admin_otp']) || !isset($_SESSION['admin_otp_expiry'])) {
$_SESSION['error'] = 'OTP expired or not requested. Please request a new OTP.';
} elseif (time() > $_SESSION['admin_otp_expiry']) {
$_SESSION['error'] = 'OTP has expired. Please request a new OTP.';
} elseif ($user_otp === $_SESSION['admin_otp']) {
// OTP is valid
$_SESSION['admin_authenticated'] = true;
unset($_SESSION['admin_otp']);
unset($_SESSION['admin_otp_expiry']);
echo "<script>window.location.href = '/bulldashboard/bull-dashboard.php';</script>";
exit();
} else {
$_SESSION['error'] = 'Invalid OTP. Please try again.';
}
echo "<script>window.location.href = '/admin.php';</script>";
exit();
}
// Check if already authenticated
if (isset($_SESSION['admin_authenticated']) && $_SESSION['admin_authenticated'] === true) {
echo "<script>window.location.href = '/bulldashboard/bull-dashboard.php';</script>";
exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin Login</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #f3f4f6;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.login-container {
background-color: white;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
padding: 2rem;
width: 350px;
text-align: center;
}
.login-container h2 {
margin-bottom: 1.5rem;
color: #1f2937;
}
.otp-input {
display: flex;
justify-content: center;
gap: 10px;
margin-bottom: 1.5rem;
}
.otp-input input {
width: 40px;
height: 40px;
text-align: center;
font-size: 1.2rem;
border: 1px solid #d1d5db;
border-radius: 5px;
}
.btn {
background-color: #3b82f6;
color: white;
border: none;
padding: 0.75rem 1.5rem;
border-radius: 5px;
cursor: pointer;
font-weight: 500;
transition: background-color 0.2s;
}
.btn:hover {
background-color: #2563eb;
}
.btn:disabled {
background-color: #9ca3af;
cursor: not-allowed;
}
.message {
margin: 1rem 0;
padding: 0.75rem;
border-radius: 5px;
}
.success {
background-color: #d1fae5;
color: #065f46;
}
.error {
background-color: #fee2e2;
color: #b91c1c;
}
.resend {
margin-top: 1rem;
color: #6b7280;
}
.resend a {
color: #3b82f6;
text-decoration: none;
}
</style>
</head>
<body>
<div class="login-container">
<h2>Admin Login</h2>
<?php if (isset($_SESSION['message'])): ?>
<div class="message success"><?php echo $_SESSION['message'];
unset($_SESSION['message']); ?></div>
<?php endif; ?>
<?php if (isset($_SESSION['error'])): ?>
<div class="message error"><?php echo $_SESSION['error'];
unset($_SESSION['error']); ?></div>
<?php endif; ?>
<?php if (!isset($_SESSION['otp_sent'])): ?>
<form method="POST">
<p>Click the button below to receive an OTP on your admin email.</p>
<button type="submit" name="request_otp" class="btn">Send OTP</button>
</form>
<?php else: ?>
<form method="POST">
<p>Enter the 6-digit OTP sent to your email:</p>
<div class="otp-input ">
<input type="text" name="otp" maxlength="6" pattern="\d{6}" title="Please enter exactly 6 digits" required autofocus>
</div>
<button type="submit" name="verify_otp" class="btn">Verify OTP</button>
<div class="resend">
Didn't receive OTP? <a href="admin.php">Resend</a>
</div>
</form>
<?php endif; ?>
</div>
</body>
</html>