-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyProfile.php
More file actions
213 lines (198 loc) · 9.52 KB
/
myProfile.php
File metadata and controls
213 lines (198 loc) · 9.52 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
<?php
session_start();
require_once('config/config.php');
require_once('includes/nav.php');
require_once('util/userUtil.php');
require_once('util/inputValidationUtil.php');
if(!isset($_SESSION['UserType']) || $_SESSION['UserType'] === UserType::visitor->value){
$_SESSION['UserType'] = UserType::visitor->value;
header("Location: index.php");
exit();
}
$pageTitle = $APP_PROPERTIES['app_name'].' - My Profile';
if(!isset($_SESSION['UserId']) || !filter_var($_SESSION['UserId'], FILTER_VALIDATE_INT))
{
header("Location: logout.php");
exit();
}
$user = new User($_SESSION['UserId']);
$msgErr = $msgRecordSaved = "";
$usernameErr = $lastnameErr = $firstnameErr = $emailErr = $bioErr = $newPasswordErr = $newPasswordRepeatedErr = $profilePicErr = "";
if($_SERVER['REQUEST_METHOD'] === "POST")
{
$var = validateInput($_POST["username"], InputType::username);
$user->setUsername($var['data']);
$usernameErr = $var['error'];
$var = validateInput($_POST["lastname"], InputType::lastname);
$user->setLastname($var['data']);
$lastnameErr = $var['error'];
$var = validateInput($_POST["firstname"], InputType::firstname);
$user->setFirstname($var['data']);
$firstnameErr = $var['error'];
$var = validateInput($_POST["email"], InputType::email);
$user->setEmail($var['data']);
$emailErr = $var['error'];
$var = validateInput($_POST["bio"], InputType::bio, false);
$user->setBio($var['data']);
$bioErr = $var['error'];
$user->setPublicProfile(isset($_POST["publicProfile"]) ? 1 : 0);
if (empty($_POST["newPassword"]) && !empty($_POST["newPasswordRepeated"]))
$newPasswordErr = "Password is required";
elseif (!empty($_POST["newPassword"]) && empty($_POST["newPasswordRepeated"]))
$newPasswordRepeatedErr = "Password repeated is required";
elseif (!empty($_POST["newPassword"]) && !empty($_POST["newPasswordRepeated"]))
{
if($_POST["newPassword"] !== $_POST["newPasswordRepeated"])
$newPasswordRepeatedErr = "Password repeated must be equal with Password";
else
{
$var = validateInput($_POST["newPassword"], InputType::password);
$newPasswordErr = $var['error'];
if(empty($newPasswordErr))
$user->setPassword($var['data']);
}
}
if(empty($usernameErr) && empty($lastnameErr) && empty($firstnameErr) && empty($emailErr)
&& empty($bio) && empty($newPasswordErr) && empty($newPasswordRepeatedErr))
{
// Only handle file after correct input
$var = $user->setProfilePictureFromUpload($_FILES["profilePic"]);
$profilePicErr = $var['error'];
if(empty($profilePicErr)){
$result = $user->saveInDb();
if ($result['success'])
$msgRecordSaved = 'Profile updated successfully.';
else
$msgErr = $result['error'];
}
else
$msgErr = "An error has occurred.";
}
else{
$msgErr = "An error has occurred.";
//if(isset($_FILES["profilePic"]))
if (!empty($_FILES['profilePic']['name']))
$profilePicErr = "Because there were errors in the form, the uploaded file could not be kept. Please correct the errors and try again.";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<?php require_once('includes/head-includes.php') ?>
<title><?php echo $pageTitle;?></title>
</head>
<body class="d-flex flex-column min-vh-100">
<?php
$navbar = new Navbar(UserType::getEnumFromValue($_SESSION['UserType']));
echo $navbar->getNavBar();
?>
<main class="container flex-grow-1 mt-3 mt-md-5 pt-md-4">
<div class="text-break">
<h1>My Profile</h1>
<?php
if(!empty($msgErr))
echo'
<div class="mb-3 fw-semibold text-danger">
<p>Error: '.$msgErr.'</p>
</div>
';
elseif(!empty($msgRecordSaved))
echo'
<div class="mb-3 fw-semibold text-success">
<p>Success: '.$msgRecordSaved.'</p>
</div>
';
?>
</div>
<form class="fw-semibold" method="POST" action="" enctype="multipart/form-data" autocomplete="off">
<div class="mb-3">
<label class="form-check-label mb-2">
Profile picture
<?php echo empty($profilePicErr) ? "" : " <span class='text-danger'>($profilePicErr)</span>";?>
</label>
<div class="text-center">
<img src="<?php echo ((empty($user->getProfilePicturePath())) || (! file_exists($user->getProfilePicturePath()))) ? 'resources/images/person.svg' : htmlspecialchars($user->getProfilePicturePath()); ?>"
class="profile-pic-edit mb-2"
alt="Profile picture">
</div>
<input type="file" accept="image/jpeg,image/png" id="profilePic" name="profilePic" class="form-control">
</div>
<hr>
<div class="mb-3 form-check">
<label class="form-check-label" for="publicProfile">
Public profile
</label>
<input type="checkbox" class="form-check-input" id="publicProfile" name="publicProfile" <?php echo $user->getPublicProfile() == 1 ? "checked" : "";?>>
</div>
<div class="mb-3">
<label for="username" class="form-label">
Username*
<?php echo empty($usernameErr) ? "" : " <span class='text-danger'>($usernameErr)</span>";?>
</label>
<input type="text" class="form-control" id="username" name ="username"
value="<?php echo htmlspecialchars($user->getUsername());?>"
maxlength="<?php echo maxLengthsOfInputType[InputType::username->name];?>"
required>
</div>
<div class="mb-3">
<label for="lastname" class="form-label">
Lastname*
<?php echo empty($lastnameErr) ? "" : " <span class='text-danger'>($lastnameErr)</span>";?>
</label>
<input type="text" class="form-control" id="lastname" name="lastname"
value="<?php echo htmlspecialchars($user->getLastname());?>"
maxlength="<?php echo maxLengthsOfInputType[InputType::lastname->name];?>"
required>
</div>
<div class="mb-3">
<label for="firstname" class="form-label">
Firstname*
<?php echo empty($firstnameErr) ? "" : " <span class='text-danger'>($firstnameErr)</span>";?>
</label>
<input type="text" class="form-control" id="firstname" name="firstname"
value="<?php echo htmlspecialchars($user->getFirstname());?>"
maxlength="<?php echo maxLengthsOfInputType[InputType::firstname->name];?>"
required>
</div>
<div class="mb-3">
<label for="email" class="form-label">
Email*
<?php echo empty($emailErr) ? "" : " <span class='text-danger'>($emailErr)</span>";?>
</label>
<input type="email" class="form-control" id="email" name="email"
value="<?php echo htmlspecialchars($user->getEmail());?>"
maxlength="<?php echo maxLengthsOfInputType[InputType::email->name];?>"
required>
</div>
<div class="mb-3">
<label for="bio" class="form-label">
Bio
<?php echo empty($bioErr) ? "" : " <span class='text-danger'>($bioErr)</span>";?>
</label>
<textarea name="bio" class="form-control" rows="3" maxlength="<?php echo maxLengthsOfInputType[InputType::bio->name];?>"><?php echo htmlspecialchars($user->getBio()); ?></textarea>
</div>
<hr>
<div class="mb-3">
<label for="newPassword" class="form-label">
New password
<?php echo empty($newPasswordErr) ? "" : " <span class='text-danger'>($newPasswordErr)</span>";?>
</label>
<input type="password" class="form-control" id="newPassword" name="newPassword" value="">
</div>
<div class="mb-3">
<label for="newPasswordRepeated" class="form-label">
New password repeated
<?php echo empty($newPasswordRepeatedErr) ? "" : " <span class='text-danger'>($newPasswordRepeatedErr)</span>";?>
</label>
<input type="password" class="form-control" id="newPasswordRepeated" name="newPasswordRepeated" value="">
</div>
<button class="btn btn-success btn-md" type="submit">
<i class="bi bi-floppy"></i>
Save
</button>
</form>
</main>
<?php require_once('./includes/footer.php'); ?>
</body>
</html>