-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_profile.php
More file actions
325 lines (275 loc) · 12.7 KB
/
Copy pathuser_profile.php
File metadata and controls
325 lines (275 loc) · 12.7 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
<?php
/**
* User Profile Management Page
*
* This script allows logged-in users to update their profile information, including their name, position,
* and email address. It also provides an option to request a password change. The page fetches user details
* from the database, displays them in a form, and handles form submissions to update the profile or request a
* password reset.
*
*/
require 'session_config.php';
require 'dbcon.php'; // Database connection
require 'config.php'; // Configuration file for email settings
require 'header.php'; // Include the header file
require_once __DIR__ . '/includes/mailer.php';
// Check if the user is logged in
if (!isset($_SESSION['username'])) {
$currentUrl = urlencode($_SERVER['REQUEST_URI']);
header("Location: index.php?redirect=$currentUrl");
exit; // Exit to ensure no further code is executed
}
// Generate CSRF token if not already set
if (empty($_SESSION['csrf_token'])) {
$_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}
// Fetch user details from the database
$username = $_SESSION['username'];
$query = "SELECT * FROM users WHERE username = ?";
$stmt = $con->prepare($query);
$stmt->bind_param("s", $username);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_assoc();
$stmt->close();
$updateMessage = ''; // Initialize message for profile update
// Function to generate initials from the user's name
function generateInitials($name)
{
$parts = explode(" ", $name);
$initials = "";
foreach ($parts as $part) {
if (!empty($part) && ctype_alpha($part[0])) {
$initials .= strtoupper($part[0]);
}
}
return substr($initials, 0, 3); // Return up to 3 characters
}
// Function to ensure unique initials
function ensureUniqueInitials($con, $initials, $currentUsername)
{
$uniqueInitials = substr($initials, 0, 3); // Limit initials to a maximum of 3 characters
$suffix = 1;
$maxLength = 10; // Define the maximum length for initials including suffix
$checkQuery = "SELECT initials FROM users WHERE initials = ? AND username != ?";
$stmt = $con->prepare($checkQuery);
if (!$stmt) {
error_log("Failed to prepare statement: " . $con->error);
return $initials; // Return the original initials if statement preparation fails
}
do {
$stmt->bind_param("ss", $uniqueInitials, $currentUsername);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0) {
$uniqueInitials = substr($initials, 0, 3) . $suffix; // Ensure initials part is still limited to 3 characters
$suffix++;
} else {
break;
}
$stmt->free_result(); // Clear the result set for the next iteration
} while (strlen($uniqueInitials) <= $maxLength);
$stmt->close();
if (strlen($uniqueInitials) > $maxLength) {
$uniqueInitials = substr($uniqueInitials, 0, $maxLength);
}
return $uniqueInitials;
}
// Handle form submission for profile update
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['update_profile'])) {
// Validate CSRF token
if (!isset($_POST['csrf_token']) || $_POST['csrf_token'] !== $_SESSION['csrf_token']) {
die('CSRF token validation failed');
}
$submittedUsername = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_EMAIL);
// If the username field was not submitted (e.g. demo mode hides it) or is empty, keep the existing one
$newUsername = !empty($submittedUsername) ? $submittedUsername : $username;
$name = trim($_POST['name'] ?? '');
$initials = trim($_POST['initials'] ?? '');
$position = trim($_POST['position'] ?? '');
// Check if the email address (username) has changed
$emailChanged = ($newUsername !== $username);
// Ensure initials are unique
$uniqueInitials = ensureUniqueInitials($con, $initials, $username);
if ($uniqueInitials !== $initials) {
$updateMessage = "The initials '$initials' are already in use by another user. Please choose different initials.";
} else {
// Update user details in the database
$updateQuery = "UPDATE users SET username = ?, name = ?, position = ?, initials = ?";
if ($emailChanged) {
$updateQuery .= ", email_verified = 0";
}
$updateQuery .= " WHERE username = ?";
$updateStmt = $con->prepare($updateQuery);
if ($emailChanged) {
$updateStmt->bind_param("sssss", $newUsername, $name, $position, $uniqueInitials, $username);
} else {
$updateStmt->bind_param("sssss", $newUsername, $name, $position, $uniqueInitials, $username);
}
if ($updateStmt->execute()) {
// Update the session username if it was changed
if ($emailChanged) {
$_SESSION['username'] = $newUsername;
$username = $newUsername;
$updateMessage = "Profile information updated successfully. Please log out and log back in to reflect the changes everywhere.";
} else {
$updateMessage = "Profile information updated successfully.";
}
} else {
$updateMessage = "An error occurred while updating the profile. Please try again.";
}
$updateStmt->close();
// Refresh user data
$stmt = $con->prepare($query);
$stmt->bind_param("s", $username);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_assoc();
$stmt->close();
}
}
// Handle form submission for password reset
$resultMessage = ''; // Initialize message for password reset
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['reset'])) {
// Validate CSRF token
if (!isset($_POST['csrf_token']) || $_POST['csrf_token'] !== $_SESSION['csrf_token']) {
die('CSRF token validation failed');
}
$email = $username;
// Check if the email exists in the database
$query = "SELECT * FROM users WHERE username = ?";
$stmt = $con->prepare($query);
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows == 1) {
// Email exists, generate and save a reset token
$resetToken = bin2hex(random_bytes(32));
$expirationTimeUnix = time() + 3600; // 1 hour expiration time
$expirationTime = date('Y-m-d H:i:s', $expirationTimeUnix);
$updateQuery = "UPDATE users SET reset_token = ?, reset_token_expiration = ?, login_attempts = 0, account_locked = NULL WHERE username = ?";
$updateStmt = $con->prepare($updateQuery);
$updateStmt->bind_param("sss", $resetToken, $expirationTime, $email);
$updateStmt->execute();
// Send the password reset email
$resetLink = "https://" . $url . "/reset_password.php?token=$resetToken";
$to = $email;
$subject = 'Password Reset';
$message = "To reset your password, click the following link:\n$resetLink";
[$mailOk, $mailErr] = mv_send_mail($to, $subject, $message, ['is_html' => false]);
if ($mailOk) {
$resultMessage = "Password reset instructions have been sent to your email address.";
} else {
$resultMessage = "Email could not be sent. Error: " . $mailErr;
}
} else {
$resultMessage = "Email address not found in our records. Please try again.";
}
$stmt->close();
if (isset($updateStmt)) {
$updateStmt->close();
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>User Profile</title>
<style>
.container {
max-width: 900px;
margin-top: 50px;
margin-bottom: 50px;
padding: 20px;
border: 1px solid var(--bs-border-color);
border-radius: 5px;
background-color: var(--bs-tertiary-bg);
}
.btn-profile {
display: block;
width: 100%;
padding: 10px;
border-radius: 6px;
font-weight: 500;
}
.result-message,
.update-message {
text-align: center;
margin-top: 15px;
padding: 10px;
border-radius: 5px;
}
.note {
color: var(--bs-secondary-color);
text-align: center;
margin-top: 10px;
}
.note1 {
color: var(--bs-secondary-color);
}
</style>
</head>
<body>
<div class="container mt-4 content">
<h1 class="text-center">User Profile</h1>
<form method="POST" action="">
<input type="hidden" name="csrf_token" value="<?php echo htmlspecialchars($_SESSION['csrf_token']); ?>">
<div class="mb-3">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="name" value="<?php echo htmlspecialchars($user['name']); ?>" required>
</div>
<br>
<div class="mb-3">
<label for="initials">Initials <span class="note1">(Your Initials will be displayed in Cage Card)</span></label>
<input type="text" class="form-control" id="initials" name="initials" value="<?php echo htmlspecialchars($user['initials']); ?>" maxlength="3" required>
</div>
<br>
<div class="mb-3">
<label for="position">Position</label>
<select class="form-control" id="position" name="position">
<option value="" disabled>Select Position</option>
<option value="Principal Investigator" <?php echo ($user['position'] == 'Principal Investigator') ? 'selected' : ''; ?>>Principal Investigator</option>
<option value="Research Scientist" <?php echo ($user['position'] == 'Research Scientist') ? 'selected' : ''; ?>>Research Scientist</option>
<option value="Postdoctoral Researcher" <?php echo ($user['position'] == 'Postdoctoral Researcher') ? 'selected' : ''; ?>>Postdoctoral Researcher</option>
<option value="PhD Student" <?php echo ($user['position'] == 'PhD Student') ? 'selected' : ''; ?>>PhD Student</option>
<option value="Masters Student" <?php echo ($user['position'] == 'Masters Student') ? 'selected' : ''; ?>>Masters Student</option>
<option value="Undergraduate" <?php echo ($user['position'] == 'Undergraduate') ? 'selected' : ''; ?>>Undergraduate</option>
<option value="Laboratory Technician" <?php echo ($user['position'] == 'Laboratory Technician') ? 'selected' : ''; ?>>Laboratory Technician</option>
<option value="Research Associate" <?php echo ($user['position'] == 'Research Associate') ? 'selected' : ''; ?>>Research Associate</option>
<option value="Vivarium Manager" <?php echo ($user['position'] == 'Vivarium Manager') ? 'selected' : ''; ?>>Vivarium Manager</option>
<option value="Animal Care Technician" <?php echo ($user['position'] == 'Animal Care Technician') ? 'selected' : ''; ?>>Animal Care Technician</option>
<option value="Interns and Volunteers" <?php echo ($user['position'] == 'Interns and Volunteers') ? 'selected' : ''; ?>>Interns and Volunteers</option>
</select>
</div>
<br>
<?php if ($demo !== "yes" && $demo !== "invite") : ?>
<div class="mb-3">
<label for="username">Email Address</label>
<input type="email" class="form-control" id="username" name="username" value="<?php echo htmlspecialchars($user['username']); ?>" required>
</div>
<?php endif; ?>
<br>
<button type="submit" class="btn btn-primary btn-profile" name="update_profile">Update Profile</button>
</form>
<?php if ($updateMessage) {
echo "<div class='alert alert-success text-center'>" . htmlspecialchars($updateMessage) . "</div>";
} ?>
<hr class="my-4">
<h2>Request Password Change</h2>
<br>
<form method="POST" action="">
<input type="hidden" name="csrf_token" value="<?php echo htmlspecialchars($_SESSION['csrf_token']); ?>">
<button type="submit" class="btn btn-warning btn-profile" name="reset">Request Password Change</button>
</form>
<?php if ($resultMessage) {
echo "<div class='alert alert-success text-center'>" . htmlspecialchars($resultMessage) . "</div>";
} ?>
<br>
<p class="note">In order to reflect the changes everywhere, please log out and log back in.</p>
</div>
<?php include 'footer.php'; ?>
</body>
</html>
<?php mysqli_close($con); ?>