-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
244 lines (202 loc) · 6.52 KB
/
functions.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
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
<?php
//session start
session_start();
ob_start();
// connect to database
$db = mysqli_connect('localhost', 'root', '', 'hackumassvi');
// variable declaration
$username = "";
$first = "";
$last = "";
$errors = array();
//CHANGE PASSWORD:
if (isset($_POST['pass'])) {
$old_pass = e($_POST['old_pass']);
$password_1 = e($_POST['password_1']);
$password_2 = e($_POST['password_2']);
if(isset($_SESSION['user'])){
$username = $_SESSION['user']['username'];
$pass = $_SESSION['user']['password'];
$prev = $_SESSION['user']['prev_pass'];
if (empty($old_pass)) {
array_push($errors, "Old Password is required");
}
if (empty($password_1)) {
array_push($errors, "New Password is required");
}
if ($pass != $old_pass) {
array_push($errors, "The old password entered is incorrect");
}
if ($password_1 != $password_2) {
array_push($errors, "The two passwords do not match, please confirm the new password");
}
if ($old_pass == $password_1){
array_push($errors, "Both the new and old Password are the same!");
}
if ($password_1 == $prev){
array_push($errors, "New password should not match the previous two passwords used");
}
if (count($errors)==0){
$query = "UPDATE users SET password = '$password_1', prev_pass = '$old_pass' WHERE username = '$username'";
mysqli_query($db, $query);
$_SESSION['username'] = $username;
$_SESSION['success'] = "Password Changed";
if (isset($_POST['user_type'])) {
header('location: home.php');
}
else{
header('location: index.php');
}
}
}
}
//CANCEL EDIT/DELETE from View Users Page
if (isset($_POST['can'])){
header('location: user_info.php');
}
//CANCEL BUTTON RETURN HOME from options under settings menu
if (isset($_POST['cancel'])){
if(isAdmin()){
header('location: home.php');
}
else{
header('location: index.php');
}
}
// return user array from their id
function getUserById($id){
global $db;
$query = "SELECT * FROM users WHERE id=" . $id;
$result = mysqli_query($db, $query);
$user = mysqli_fetch_assoc($result);
return $user;
}
// escape string
function e($val){
global $db;
return mysqli_real_escape_string($db, trim($val));
}
function display_error() {
global $errors;
if (count($errors) > 0){
echo '<div class="error">';
foreach ($errors as $error){
echo $error .'<br>';
}
echo '</div>';
}
}
function isLoggedIn(){
if (isset($_SESSION['user'])) {
return true;
}else{
return false;
}
}
// register function if register_btn is clicked
if (isset($_POST['register_btn'])) {
// call these variables with the global keyword to make them available in function
global $db, $errors, $username, $first, $last;
// receive all input values from the form. Call the e() function
// defined below to escape form values
$username = e($_POST['username']);
$first = e($_POST['first']);
$last = e($_POST['last']);
$password_1 = e($_POST['password_1']);
$password_2 = e($_POST['password_2']);
// form validation: ensure that the form is correctly filled
if (empty($username)) {
array_push($errors, "Username is required");
}
if (empty($first)) {
array_push($errors, "First Name is required");
}
if (empty($last)) {
array_push($errors, "Last Name is required");
}
if (empty($password_1)) {
array_push($errors, "Password is required");
}
if ($password_1 != $password_2) {
array_push($errors, "The two passwords do not match");
}
$query = "SELECT * FROM user WHERE username='$username'";
if(!empty($query)){
$result = mysqli_query($db, $query);
if (mysqli_num_rows($result) != 0)
{
array_push($errors, "Username is Taken");
}
}
// register user if there are no errors in the form
if (count($errors) == 0) {
$password = ($password_1);//md5 encrypt the password before saving in the database
$query_insert = "INSERT INTO user (username, first, last, pass, prev_pass)
VALUES('$username', '$first', '$last', '$password', '')";
mysqli_query($db, $query_insert);
$query = "SELECT * FROM user WHERE username = '$username' AND pass = '$password' LIMIT 1";
$result = mysqli_query($db, $query);
$logged_in_user = mysqli_fetch_assoc($result);
$_SESSION['user'] = $logged_in_user;
header('location: home.php');
}
}
// log user out if logout button clicked
if (isset($_GET['logout'])) {
session_destroy();
unset($_SESSION['user']);
header("location: login.php");
}
// login function if register_btn is clicked
if (isset($_POST['login_btn'])) {
global $db, $susername, $errors;
// grap form values
$susername = e($_POST['susername']);
$spassword = e($_POST['spassword']);
// make sure form is filled properly
if (empty($susername)) {
array_push($errors, "Username is required for Login");
}
if (empty($spassword)) {
array_push($errors, "Password is required for Login");
}
// attempt login if no errors on form
if (count($errors) == 0) {
$spassword = ($spassword);
$query = "SELECT * FROM user WHERE username = '$susername' AND pass = '$spassword' LIMIT 1";
$results = mysqli_query($db, $query);
if (mysqli_num_rows($results) == 1) { // user found
// check if user is admin or user
$logged_in_user = mysqli_fetch_assoc($results);
/* if($logged_in_user['locked']==1){
array_push($errors, "You are currently locked. Please contact the Administration");
}
else if ($logged_in_user['user_type'] == 'admin') {
$_SESSION['user'] = $logged_in_user;
$_SESSION['success'] = "You are now logged in";
header('location: home.php');
}
else{*/
$_SESSION['user'] = $logged_in_user;
$_SESSION['success'] = "You are now logged in";
header('location: home.php');
// }
// }
}
else {
array_push($errors, "Wrong username/password combination");
}
}
}
// check if user logged in has admin rights
function isAdmin(){
if (isset($_SESSION['user']) && $_SESSION['user']['user_type'] == 'admin' ) {
return true;
}
else if (isset($_SESSION['user']) && $_SESSION['user']['user_type'] == 'super' ) {
return true;
}
else{
return false;
}
}