-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.php
206 lines (177 loc) · 7.25 KB
/
update.php
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
<!DOCTYPE html>
<head>
<title>Account</title>
<link rel="stylesheet" href="css/styles.css">
</head>
<?php include "connection.php"; ?>
<?php
session_start();
$errCtr = 0;
$errMsgs = [];
// Performs validation when the form is submitted
if (isset($_POST['save'])) { // checks if the login form has been submitted
// Stores what was in 'uname' (the username prompt) in variable if it's valid
$username = validUsername($_REQUEST['uname']);
// Stores what was in 'psw' (the password prompt) in variable if it's valid
$password = validPassword($_REQUEST['psw']);
}
?>
<body>
<div id="wrapper3">
<div id="container">
<?php
update();
/**
* Used to check if a username that was entered is valid
*
* @param String $uname represents the username that was entered
*
* @category Description
* @package MIT
* @author Lucas Chapman <lucas_chapman@stu.indianhills.edu>
* @license http://www.php.net/license/3_01.txt PHP License 3.01
* @version PHP: 7.4.9
* @link http://www.php.net
* @return String returns either null or a valid username
*/
function validUsername(String $uname)
{
global $errCtr, $errMsgs;
// Represents length (not including any whitespace characters such as spaces)
$lenTrimUname = strlen(str_replace(" ", "", $uname));
if (empty(trim($uname))) {
$uname = null;
$errMsgs[] = "\t<p>No username was entered. Note: Any white space characters that were entered were trimmed</p>";
$errCtr++;
} elseif ($lenTrimUname < 2) {
$uname = null;
$errMsgs[] = "\t<p>Invalid username, " . str_replace(" ", "", $uname) . " should be at least 2 characters long and all whitespace characters will be removed</p>";
$errCtr++;
} else {
// Used to remove all whitespace characters
$uname = str_replace(" ", "", $uname);
}
return $uname;
}
/**
* Used to check if a password that was entered is valid
*
* @param String $psw represents the password that was entered
*
* @category Description
* @package MIT
* @author Lucas Chapman <lucas_chapman@stu.indianhills.edu>
* @license http://www.php.net/license/3_01.txt PHP License 3.01
* @version PHP: 7.4.9
* @link http://www.php.net
* @return String returns either null or a valid password
*/
function validPassword(String $psw)
{
global $errCtr, $errMsgs;
// Represents the length
$pattern = "/(?=.*[A-Z|a-z])(?=.*\d)(?=.*[\W|_])[A-Za-z\d\W_]{8,}$/";
$format = preg_match($pattern, $psw) && preg_match("/^\S{8,}$/", $psw);
if (empty(trim($psw))) { // checks if password was null when user tried to login
$errMsgs[] = "\t<p>No password was entered</p>";
$errCtr++;
$psw = null;
} else {
// Checks if the length of the password is in the correct format
if (!$format) {
$errMsgs[] = "\t<p>Invalid password, $psw should be at least 8 characters long with no whitespace characters and contain at least one letter, one number, and one sepcial chatacter</p>";
$errCtr++;
$psw = null;
}
}
return $psw;
}
/**
* Checks if the user's account should be created
*
* @return void
*/
function update()
{
global $errCtr, $errMsgs, $username;
if ($errCtr == 0) {
$updated = updateUser();
if ($updated) {
$_SESSION["uname"] = $username;
header("Location: http://localhost/PHPatriots/account.php");
} else {
displayErrors();
}
} else {
displayErrors();
}
}
/**
* Prints error messages and error page
*
* @return void
*/
function displayErrors()
{
global $errMsgs;
echo "\t<h1>Error</h1>\n";
reset($errMsgs);
foreach ($errMsgs as $errMsg) {
echo $errMsg;
}
echo "\t<a href='account.php'>Click here to try again</a>";
}
/**
* Updates users in the database
*
* @return bool
*/
function updateUser()
{
global $username, $password, $con, $errMsgs;
try {
$encPassword = encPsw();
if (!password_verify($password, $encPassword)) {
// Prepare and binds placeholders
$query = "UPDATE Accounts SET username = ?, password = ? WHERE username = ?";
$prep_update = $con->prepare($query);
$prep_update->bind_param("sss", $uname1, $psw, $uname2);
// Sets parameters and execute prepare statement
$uname1 = $username;
$psw = password_hash($password, PASSWORD_DEFAULT);
$uname2 = $_SESSION["uname"];
$prep_update->execute();
// End prepare statement
$prep_update->close();
return true;
} else {
$errMsgs[] = "\t<p>A new password should be used</p>";
return false;
}
} catch (Exception $e) {
echo "<script>console.error('" . $e->getMessage() . "');</script>";
return false;
}
}
/**
* Gets the encrypted password
*
* @return String
*/
function encPsw()
{
global $con;
$query = "SELECT username, password FROM Accounts";
$result = $con->query($query);
while ($row = $result->fetch_assoc()) {
if ($_SESSION["uname"] == $row["username"]) {
$encPsw = $row["password"];
break;
}
}
return $encPsw;
}
?>
</div>
</div>
</body>