-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchange_password.php
More file actions
273 lines (234 loc) · 7.35 KB
/
Copy pathchange_password.php
File metadata and controls
273 lines (234 loc) · 7.35 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
<?php
session_start();
if (!isset($_SESSION['logged_in'])) {
die("Not logged in!");
}
// Load configuration
function loadConfig() {
if (!file_exists('config.json')) {
return ['password' => password_hash('changeme', PASSWORD_DEFAULT)];
}
return json_decode(file_get_contents('config.json'), true);
}
// Save configuration
function saveConfig($config) {
return file_put_contents('config.json', json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}
$config = loadConfig();
$error = '';
$success = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// CSRF protection
if (!isset($_POST['csrf_token']) || !hash_equals($_SESSION['csrf_token'], $_POST['csrf_token'])) {
$error = "Invalid request";
} elseif (empty($_POST['old']) || empty($_POST['new']) || empty($_POST['confirm'])) {
$error = "All fields must be filled";
} elseif (!password_verify($_POST['old'], $config['password'])) {
$error = "Current password is incorrect";
} elseif ($_POST['new'] !== $_POST['confirm']) {
$error = "New passwords do not match";
} elseif (strlen($_POST['new']) < 6) {
$error = "New password must be at least 6 characters long";
} else {
$config['password'] = password_hash($_POST['new'], PASSWORD_DEFAULT);
if (saveConfig($config)) {
$success = "Password changed successfully!";
} else {
$error = "Error saving new password";
}
}
}
// Generate CSRF token
if (!isset($_SESSION['csrf_token'])) {
$_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Change Password</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
padding: 20px;
}
.container {
background: white;
border-radius: 12px;
box-shadow: 0 20px 40px rgba(0,0,0,0.1);
padding: 40px;
width: 100%;
max-width: 500px;
}
.header {
text-align: center;
margin-bottom: 30px;
}
.header h1 {
color: #333;
font-size: 28px;
margin-bottom: 8px;
}
.header p {
color: #666;
font-size: 14px;
}
.form-group {
margin-bottom: 20px;
}
.form-group label {
display: block;
margin-bottom: 8px;
color: #333;
font-weight: 500;
}
.form-group input {
width: 100%;
padding: 12px 16px;
border: 2px solid #e1e5e9;
border-radius: 8px;
font-size: 16px;
transition: border-color 0.3s ease;
}
.form-group input:focus {
outline: none;
border-color: #667eea;
}
.btn {
width: 100%;
padding: 12px 24px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
border-radius: 8px;
font-size: 16px;
font-weight: 600;
cursor: pointer;
transition: transform 0.2s ease;
text-decoration: none;
display: inline-block;
text-align: center;
}
.btn:hover {
transform: translateY(-2px);
}
.btn-secondary {
background: #6c757d;
margin-top: 10px;
}
.alert {
padding: 12px 16px;
border-radius: 8px;
margin-bottom: 20px;
font-weight: 500;
}
.alert-error {
background: #fee;
color: #c33;
border: 1px solid #fcc;
}
.alert-success {
background: #efe;
color: #363;
border: 1px solid #cfc;
}
.password-requirements {
background: #f8f9fa;
border: 1px solid #dee2e6;
border-radius: 8px;
padding: 15px;
margin-bottom: 20px;
}
.password-requirements h4 {
color: #495057;
margin-bottom: 10px;
font-size: 14px;
}
.password-requirements ul {
color: #6c757d;
font-size: 13px;
margin-left: 20px;
}
.password-requirements li {
margin-bottom: 3px;
}
.back-link {
text-align: center;
margin-top: 20px;
}
.back-link a {
color: #667eea;
text-decoration: none;
font-size: 14px;
}
.back-link a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>🔑 Change Password</h1>
<p>Change your login password</p>
</div>
<?php if ($error): ?>
<div class="alert alert-error"><?= htmlspecialchars($error) ?></div>
<?php endif; ?>
<?php if ($success): ?>
<div class="alert alert-success"><?= htmlspecialchars($success) ?></div>
<?php endif; ?>
<div class="password-requirements">
<h4>📋 Password Requirements:</h4>
<ul>
<li>At least 6 characters long</li>
<li>Use a combination of letters, numbers and special characters</li>
<li>Avoid simple words or personal information</li>
</ul>
</div>
<form method="POST">
<input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
<div class="form-group">
<label for="old">Current Password</label>
<input type="password" id="old" name="old" required>
</div>
<div class="form-group">
<label for="new">New Password</label>
<input type="password" id="new" name="new" required minlength="6">
</div>
<div class="form-group">
<label for="confirm">Confirm New Password</label>
<input type="password" id="confirm" name="confirm" required minlength="6">
</div>
<button type="submit" class="btn">💾 Change Password</button>
</form>
<div class="back-link">
<a href="index.php">← Back to Dashboard</a>
</div>
</div>
<script>
// Password confirmation validation
document.getElementById('confirm').addEventListener('input', function() {
const newPassword = document.getElementById('new').value;
const confirmPassword = this.value;
if (newPassword !== confirmPassword) {
this.setCustomValidity('Passwords do not match');
} else {
this.setCustomValidity('');
}
});
</script>
</body>
</html>