-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccount.php
More file actions
243 lines (196 loc) · 10.4 KB
/
account.php
File metadata and controls
243 lines (196 loc) · 10.4 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
<?php
// Code for processing Login Form.
require "includes/config.php";
// Check if the user had previously checked remember me.
require "includes/library.php";
// Connect to database
$pdo = connectDB();
$user_id = $_SESSION['user_id'];
$stmt = $pdo->prepare("SELECT * FROM A3_3420_Users WHERE id = ?");
$stmt->execute([$user_id]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
// Initialize an array to hold any error message from form submissions.
$errors = array();
// Handle the POST method for form submission.
if($_SERVER["REQUEST_METHOD"] == "POST") {
if($_POST['submit'] == 'Edit Account'){
// Get the form input
$name = $_POST['name'];
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$confirm_password = $_POST['confirm-password'];
$subscribeemail = isset($_POST['subscribeemail']) ? 1 : 0;
$subscribepromo = isset($_POST['subscribepromo']) ? 1 : 0;
// Sanitize the main inputs.
$name = htmlspecialchars($name);
$username = htmlspecialchars($username);
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
// Validate the input
if (empty($name) || empty($username) || empty($email) || empty($password) || empty($confirm_password)) {
$errors['emptyfields'] = "Please fill in all the fields.";
}
// Error checking for valid emails
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors['email'] = "Invalid email format.";
}
// Error checking for correct password layout
if(strlen($password) < 8 || !preg_match("/[a-z]/i", $password) || !preg_match("/\d/", $password) || !preg_match("/[@$!%*?&]/", $password)) {
$errors['password'] = "Invalid password. It must contain at least 8 characters, one letter, one number, and one special character.";
}
// Error checking for an appropriate alphanumeric username with no spaces
if(!preg_match("/^[a-zA-Z0-9]+$/", $username)) {
$errors['username'] = "Username can only contain alphanumeric characters and no spaces.";
}
// Error checking if the password and confirm password match
if($password !== $confirm_password) {
$errors['confirm-password'] = "Password confirmation does not match.";
}
// If there are no errors, update the user info in the database.
if (count($errors) === 0) {
$passwordHash = password_hash($password, PASSWORD_DEFAULT);
$sql = "UPDATE A3_3420_Users SET name = ?, username = ?, email = ?, password_hash = ?, receive_notifications = ?, receive_promotions = ? WHERE id = ?";
$stmt = $pdo->prepare($sql);
$stmt->execute([$name, $username, $email, $passwordHash, $subscribeemail, $subscribepromo, $user_id]);
}
} elseif($_POST['submit'] == 'Delete Account'){
$sql = "DELETE FROM A3_3420_Users WHERE id = ?";
$stmt = $pdo->prepare($sql);
$stmt->execute([$user_id]);
// Clear session
$_SESSION = array();
session_destroy();
// Redirect to login
header("Location: login.php");
exit();
}
if(isset($_FILES['profile_image']) && $_FILES['profile_image']['error'] != UPLOAD_ERR_NO_FILE && !empty($_FILES['profile_image']['tmp_name'])) {
$file = $_FILES['profile_image'];
// get the file properties
$fileName = $file['name'];
$fileTmpName = $file['tmp_name'];
$fileSize = $file['size'];
$fileError = $file['error'];
$fileType = $file['type'];
// handle file errors
if($fileError !== 0) {
// handle the error
die("There was an error uploading your file.");
}
// check if the uploaded file is an image
if(!in_array(mime_content_type($file['tmp_name']), ['image/jpeg', 'image/png', 'image/gif'])) {
$errors['profile_image'] = "Invalid file type. Please upload a jpeg, png, or gif image.";
}
// check the size of the uploaded file
if($file['size'] > 8000000) { // limit size to ~8MB
$errors['profile_image'] = "The uploaded file is too large. Please upload a file smaller than 8MB.";
}
$fileExtension = explode('.', $fileName);
$fileActualExtension = strtolower(end($fileExtension));
$newFileName = uniqid() . '.' . $fileActualExtension;
$targetPath = '../../../www_data/'.$newFileName;
if(!move_uploaded_file($fileTmpName, $targetPath)) {
die("There was an error moving the file.");
}
$query = "UPDATE A3_3420_Users SET profile_image = ? WHERE id = ?";
$stmt = $pdo->prepare($query);
$stmt->execute([$newFileName, $user_id]);
header("Location: ".$_SERVER['PHP_SELF']);
exit();
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<?php
$PAGE_TITLE = "Your Account";
include "includes/metadata.php";
?>
</head>
<body>
<!-- HEADER -->
<?php include "includes/header.php"?>
<!--The main tag, contains the majority of the form contents for creating an account.-->
<main class="main-content">
<h1>Your Account</h1>
<p>
Here you can edit, view, or delete your account. You can also change your notification settings here.
</p>
<!-- Login Error -->
<?php if(isset($errors['login'])): ?>
<div class="error">
<p><?php echo $errors['login']; ?></p>
</div>
<?php endif; ?>
<!--Form uses the post method for data integrity, requiring username password and a checkbox to remember the login the next time.-->
<form action="<?= htmlentities($_SERVER['PHP_SELF']) ?>" method="post" enctype="multipart/form-data" autocomplete="off" class="whole-form" >
<div class="form-group" id="profile-container">
<label for="file-upload">
<?php
$imagePath = "../../../www_data/".$user['profile_image'];
if (file_exists($imagePath)) {
echo "<img src='$imagePath' alt='Profile Picture' id='profile-pic' />";
} else {
echo "<p>No profile picture found.</p>";
}
?>
<div id="image-overlay">
<p>Edit</p>
</div>
</label>
<input type="file" id="file-upload" name="profile_image" accept="image/*" style="display: none;">
</div>
<div class="form-group">
<label for="name">Name:</label>
<input type="text" id="name" name="name" value="<?php echo htmlspecialchars($user['name']); ?>" class="form-control <?php echo isset($errors['name']) ? 'input-error' : '' ?>" />
<?php if(isset($errors['name'])): ?>
<span class="error-text"><?php echo $errors['name']; ?></span>
<?php endif; ?>
</div>
<div class="form-group">
<label for="username">Username:</label>
<input type="text" id="username" name="username" value="<?php echo htmlspecialchars($user['username']); ?>" placeholder="JohnDoe123" class="form-control <?php echo isset($errors['username']) ? 'input-error' : '' ?>" />
<?php if(isset($errors['username'])): ?>
<span class="error-text"><?php echo $errors['username']; ?></span>
<?php endif; ?>
</div>
<div class="form-group">
<label for="email" class="input-label">Email:</label>
<input type="email" id="email" name="email" value="<?php echo htmlspecialchars($user['email']); ?>" class="form-control <?php echo isset($errors['email']) ? 'input-error' : '' ?>" />
<?php if(isset($errors['email'])): ?>
<span class="error-text"><?php echo $errors['email']; ?></span>
<?php endif; ?>
</div>
<!--The div for the password input field-->
<div class="form-group">
<label for="password">Password:</label>
<input type="password" id="password" name="password" autocomplete="off" class="form-control <?php echo isset($errors['password']) ? 'input-error' : '' ?>" />
<?php if(isset($errors['password'])): ?>
<span class="error-text"><?php echo $errors['password']; ?></span>
<?php endif; ?>
</div>
<div class="form-group">
<label for="confirm-password">Confirm Password:</label>
<input type="password" id="confirm-password" name="confirm-password" autocomplete="off" class="form-control <?php echo isset($errors['confirm-password']) ? 'input-error' : '' ?>" pattern="^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$" />
<?php if(isset($errors['confirm-password'])): ?>
<span class="error-text"><?php echo $errors['confirm-password']; ?></span>
<?php endif; ?>
</div>
<div class="form-check">
<input type="checkbox" id="subscribeemail" name="subscribeemail" value="subscribeemail" class="form-check-input" <?php echo $user['receive_notifications'] ? 'checked' : ''; ?>/>
<label for="subscribeemail" class="form-check-label">Receive email notifications</label>
</div>
<div class="form-check">
<input type="checkbox" id="subscribepromo" name="subscribepromo" value="subscribepromo" class="form-check-input" <?php echo $user['receive_promotions'] ? 'checked' : ''; ?>/>
<label for="subscribepromo" class="form-check-label">Receive promotional email</label>
</div>
<!--Submit button that sends out everything in the form.-->
<input type="submit" value="Edit Account" name="submit" class="btn" />
<input type="submit" value="Delete Account" name="submit" class="del-btn" id="del-btn"/>
</form>
</main>
<!-- FOOTER -->
<?php include "includes/footer.php"?>
</body>
</html>