-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile.php
More file actions
334 lines (300 loc) · 15 KB
/
profile.php
File metadata and controls
334 lines (300 loc) · 15 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
<?php
require_once 'config/session.php';
// Redirect if not logged in
if (!isLoggedIn()) {
header("Location: login.php");
exit();
}
require_once 'config/database.php';
$database = new Database();
$db = $database->getConnection();
$error = '';
$success = '';
// Get user data
$query = "SELECT * FROM users WHERE id = :user_id";
$stmt = $db->prepare($query);
$stmt->bindParam(":user_id", $_SESSION['user_id']);
$stmt->execute();
$user = $stmt->fetch(PDO::FETCH_ASSOC);
// Normalize profile photo path
if (isset($user['profile_photo']) && $user['profile_photo']) {
// Convert backslashes to forward slashes
$user['profile_photo'] = str_replace('\\', '/', $user['profile_photo']);
// Remove any leading slashes to make it relative
$user['profile_photo'] = ltrim($user['profile_photo'], '/');
// Check if the file exists using absolute path
$absolute_path = __DIR__ . '/' . $user['profile_photo'];
if (!file_exists($absolute_path)) {
// If file doesn't exist, clear the profile photo
$user['profile_photo'] = null;
// Update database to clear the profile photo
$update_query = "UPDATE users SET profile_photo = NULL WHERE id = :user_id";
$update_stmt = $db->prepare($update_query);
$update_stmt->bindParam(":user_id", $_SESSION['user_id']);
$update_stmt->execute();
}
}
// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Debug log
error_log("POST request received");
// Handle profile photo upload first
if (isset($_FILES['profile_photo']) && $_FILES['profile_photo']['error'] === UPLOAD_ERR_OK) {
error_log("Photo upload detected");
// Check if this is a duplicate submission
if (isset($_SESSION['last_upload']) && $_SESSION['last_upload'] === $_FILES['profile_photo']['name']) {
error_log("Duplicate upload detected - redirecting");
header("Location: " . $_SERVER['PHP_SELF']);
exit();
}
$upload_dir = 'uploads/profile_photos/';
if (!file_exists($upload_dir)) {
mkdir($upload_dir, 0777, true);
}
$file_extension = strtolower(pathinfo($_FILES['profile_photo']['name'], PATHINFO_EXTENSION));
$allowed_extensions = ['jpg', 'jpeg', 'png', 'gif'];
if (in_array($file_extension, $allowed_extensions)) {
$new_filename = uniqid('profile_') . '.' . $file_extension;
$upload_path = $upload_dir . $new_filename;
if (move_uploaded_file($_FILES['profile_photo']['tmp_name'], $upload_path)) {
error_log("File moved successfully to: " . $upload_path);
// Delete old photo if exists
if (isset($user['profile_photo']) && $user['profile_photo'] && file_exists($user['profile_photo'])) {
unlink($user['profile_photo']);
error_log("Old photo deleted: " . $user['profile_photo']);
}
// Update only the profile photo
$query = "UPDATE users SET profile_photo = :profile_photo WHERE id = :user_id";
$stmt = $db->prepare($query);
$stmt->bindParam(":profile_photo", $upload_path);
$stmt->bindParam(":user_id", $_SESSION['user_id']);
if ($stmt->execute()) {
error_log("Database updated successfully");
// Store the upload info in session to prevent duplicates
$_SESSION['last_upload'] = $_FILES['profile_photo']['name'];
$_SESSION['success'] = "Profile photo updated successfully!";
// Clear any existing output
if (ob_get_length()) ob_clean();
// Redirect with a unique parameter to prevent caching
header("Location: " . $_SERVER['PHP_SELF'] . "?t=" . time());
exit();
} else {
error_log("Database update failed");
$error = "Failed to update profile photo in database";
}
} else {
error_log("Failed to move uploaded file");
$error = "Failed to upload photo";
}
} else {
error_log("Invalid file type: " . $file_extension);
$error = "Invalid file type. Allowed types: " . implode(', ', $allowed_extensions);
}
}
// Handle other form submissions
$fullname = $_POST['fullname'] ?? '';
$email = $_POST['email'] ?? '';
$phone = $_POST['phone'] ?? '';
$address = $_POST['address'] ?? '';
$vehicle_number = $_POST['vehicle_number'] ?? '';
$vehicle_type = $_POST['vehicle_type'] ?? '';
$current_password = $_POST['current_password'] ?? '';
$new_password = $_POST['new_password'] ?? '';
$confirm_password = $_POST['confirm_password'] ?? '';
// Update basic info
if (!empty($fullname) && !empty($email)) {
try {
// Hash sensitive data
$phone_hash = !empty($phone) ? password_hash($phone, PASSWORD_DEFAULT) : null;
$address_hash = !empty($address) ? password_hash($address, PASSWORD_DEFAULT) : null;
$vehicle_number_hash = !empty($vehicle_number) ? password_hash($vehicle_number, PASSWORD_DEFAULT) : null;
$vehicle_type_hash = !empty($vehicle_type) ? password_hash($vehicle_type, PASSWORD_DEFAULT) : null;
$query = "UPDATE users SET
fullname = :fullname,
email = :email,
phone = :phone,
address = :address,
vehicle_number = :vehicle_number,
vehicle_type = :vehicle_type
WHERE id = :user_id";
$stmt = $db->prepare($query);
$stmt->bindParam(":fullname", $fullname);
$stmt->bindParam(":email", $email);
$stmt->bindParam(":phone", $phone_hash);
$stmt->bindParam(":address", $address_hash);
$stmt->bindParam(":vehicle_number", $vehicle_number_hash);
$stmt->bindParam(":vehicle_type", $vehicle_type_hash);
$stmt->bindParam(":user_id", $_SESSION['user_id']);
if ($stmt->execute()) {
$success = "Profile updated successfully!";
// Refresh user data
$query = "SELECT * FROM users WHERE id = :user_id";
$stmt = $db->prepare($query);
$stmt->bindParam(":user_id", $_SESSION['user_id']);
$stmt->execute();
$user = $stmt->fetch(PDO::FETCH_ASSOC);
} else {
$error = "Failed to update profile";
}
} catch (PDOException $e) {
$error = "Database error: " . $e->getMessage();
}
}
// Update password if provided
if (!empty($current_password) && !empty($new_password) && !empty($confirm_password)) {
if ($new_password !== $confirm_password) {
$error = "New passwords do not match";
} else {
// Verify current password
if (password_verify($current_password, $user['password'])) {
// Check if new password is already in use
$hashed_new_password = password_hash($new_password, PASSWORD_DEFAULT);
$query = "SELECT id FROM users WHERE password = :password AND id != :user_id";
$stmt = $db->prepare($query);
$stmt->bindParam(":password", $hashed_new_password);
$stmt->bindParam(":user_id", $_SESSION['user_id']);
$stmt->execute();
if ($stmt->rowCount() > 0) {
$error = "This password is already in use. Please choose a different one.";
} else {
// Update password
$query = "UPDATE users SET password = :password WHERE id = :user_id";
$stmt = $db->prepare($query);
$stmt->bindParam(":password", $hashed_new_password);
$stmt->bindParam(":user_id", $_SESSION['user_id']);
if ($stmt->execute()) {
$success = "Password updated successfully!";
} else {
$error = "Failed to update password";
}
}
} else {
$error = "Current password is incorrect";
}
}
}
}
// Check for session messages
if (isset($_SESSION['success'])) {
$success = $_SESSION['success'];
unset($_SESSION['success']);
}
// Add this function at the top of the file after the require statements
function verifyHashedData($hashed_value, $input_value) {
if (empty($hashed_value) || empty($input_value)) {
return false;
}
return password_verify($input_value, $hashed_value);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Profile - Parking System</title>
<link rel="stylesheet" href="css/stylesProfile.css">
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&display=swap" rel="stylesheet">
</head>
<body>
<header>
<nav class="navbar">
<a href="index.php" class="nav-brand">
<img src="images/logo.png" alt="Smart Parking System Logo" class="nav-logo">
<span class="nav-brand-text">SMART PARKING SYSTEM</span>
</a>
<div class="nav-links">
<a href="index.php" class="nav-link">Home</a>
<a href="reservation.php" class="nav-link">Reserve Parking</a>
<a href="profile.php" class="nav-link active">Profile</a>
<a href="logout.php" class="nav-link">Logout</a>
</div>
</nav>
</header>
<div class="profile-container" id="profile-container">
<div class="profile-header">
<div class="profile-avatar">
<?php if (isset($user['profile_photo']) && $user['profile_photo'] && file_exists($user['profile_photo'])): ?>
<img src="<?php echo htmlspecialchars($user['profile_photo']); ?>" alt="Profile Photo">
<?php else: ?>
<div class="default-icon">
<img src="images/icon.png" alt="Default Profile">
</div>
<?php endif; ?>
<label for="profile-photo-input" class="upload-overlay">Change Photo</label>
</div>
<div class="profile-info">
<h2><?php echo htmlspecialchars($user['fullname'] ?? ''); ?></h2>
<p>@<?php echo htmlspecialchars($user['username'] ?? ''); ?></p>
</div>
</div>
<?php if ($error): ?>
<div class="error-message"><?php echo htmlspecialchars($error); ?></div>
<?php endif; ?>
<?php if ($success): ?>
<div class="success-message"><?php echo htmlspecialchars($success); ?></div>
<?php endif; ?>
<div class="profile-section">
<h3>Personal Information</h3>
<form method="POST" class="auth-form" enctype="multipart/form-data" id="profileForm">
<input type="file" id="profile-photo-input" name="profile_photo" accept="image/*" style="display: none;">
<div class="form-group">
<label for="fullname">Full Name</label>
<input type="text" id="fullname" name="fullname" class="text-color" style="color: white;" value="<?php echo htmlspecialchars($user['fullname'] ?? ''); ?>" required>
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" id="email" name="email" class="text-color" style="color: white;" value="<?php echo htmlspecialchars($user['email'] ?? ''); ?>" required>
</div>
<div class="form-group">
<label for="phone">Phone Number</label>
<input type="tel" id="phone" name="phone" class="text-color" style="color: white;"
value="<?php echo !empty($user['phone']) && verifyHashedData($user['phone'], $user['phone']) ? htmlspecialchars($user['phone']) : ''; ?>">
</div>
<div class="form-group">
<label for="address">Address</label>
<textarea id="address" name="address" class="text-color" style="color: white;" rows="3"><?php
echo !empty($user['address']) && verifyHashedData($user['address'], $user['address']) ?
htmlspecialchars($user['address']) : '';
?></textarea>
</div>
<div class="form-group">
<label for="vehicle_number">Vehicle Number</label>
<input type="text" id="vehicle_number" name="vehicle_number" class="text-color" style="color: white;"
value="<?php echo !empty($user['vehicle_number']) && verifyHashedData($user['vehicle_number'], $user['vehicle_number']) ?
htmlspecialchars($user['vehicle_number']) : ''; ?>">
</div>
<div class="form-group">
<label for="vehicle_type">Vehicle Type</label>
<input type="text" id="vehicle_type" name="vehicle_type" class="text-color" style="color: white;"
value="<?php echo !empty($user['vehicle_type']) && verifyHashedData($user['vehicle_type'], $user['vehicle_type']) ?
htmlspecialchars($user['vehicle_type']) : ''; ?>">
</div>
<button type="submit" class="auth-button">Update Profile</button>
</form>
</div>
<div class="profile-section">
<h3>Change Password</h3>
<form method="POST" class="auth-form" id="password-form">
<div class="form-group">
<label for="current_password">Current Password</label>
<input type="password" id="current_password" name="current_password" style="color: black;">
</div>
<div class="form-group">
<label for="new_password">New Password</label>
<input type="password" id="new_password" name="new_password" style="color: black;">
</div>
<div class="form-group">
<label for="confirm_password">Confirm New Password</label>
<input type="password" id="confirm_password" name="confirm_password" style="color: black;">
</div>
<button type="submit" class="auth-button">Change Password</button>
</form>
</div>
<div class="auth-links">
<a href="index.php">Back to Dashboard</a>
</div>
</div>
<script src="js/scriptProfile.js"></script>
</body>
</html>