-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
190 lines (147 loc) · 5.18 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
<?php
function query($query) {
global $connection;
return mysqli_query($connection, $query);
}
function imagePlaceholder($image=null) {
if(!$image) {
return './images/image_1.jpg';
}
else {
return $image;
}
}
function redirect($location) {
header("Location: " . $location);
exit();
}
// to check if a sql query passed or failed
function confirmQuery($result) {
global $connection;
if(!$result) {
die('Query failded. ' . mysqli_error($connection));
}
}
function username_exists($username) {
global $connection;
$query = "SELECT username FROM users WHERE username = '$username'";
$result = mysqli_query($connection, $query);
confirmQuery($result);
if(mysqli_num_rows($result) > 0) {
return true;
}
else {
return false;
}
}
function email_exists($email) {
global $connection;
$query = "SELECT user_email FROM users WHERE user_email = '$email'";
$result = mysqli_query($connection, $query);
confirmQuery($result);
if(mysqli_num_rows($result) > 0) {
return true;
}
else {
return false;
}
}
function register_user($username, $email, $password) {
global $connection;
$username = mysqli_real_escape_string($connection , $username);
$email = mysqli_real_escape_string($connection , $email);
$password = mysqli_real_escape_string($connection , $password);
// new password encryption
$password = password_hash($password, PASSWORD_BCRYPT, array('cost' => 12));
$query = "INSERT INTO users (username, user_email, user_password, user_role) ";
$query .= "VALUES('{$username}', '{$email}', '{$password}', 'subscriber')";
$register_user_query = mysqli_query($connection, $query);
confirmQuery($register_user_query);
}
function login_user($username, $password) {
global $connection;
$username = trim($username);
$password = trim($password);
$username = mysqli_real_escape_string($connection, $username);
$password = mysqli_real_escape_string($connection, $password);
$query = "SELECT * FROM users WHERE username = '{$username}' ";
$select_user_query = mysqli_query($connection, $query);
confirmQuery($select_user_query);
while($row = mysqli_fetch_assoc($select_user_query)) {
$db_user_id = $row['user_id'];
$db_username = $row['username'];
$db_user_password = $row['user_password'];
$db_user_firstname = $row['user_firstname'];
$db_user_lastname = $row['user_lastname'];
$db_user_role = $row['user_role'];
}
// password verification
if(password_verify($password, $db_user_password)) {
// storing value in session as key/value pair (i.e. as associative array)
$_SESSION['user_id'] = $db_user_id;
$_SESSION['username'] = $db_username;
$_SESSION['firstname'] = $db_user_firstname;
$_SESSION['lastname'] = $db_user_lastname;
$_SESSION['user_role'] = $db_user_role;
redirect("./admin/index.php");
}
else {
redirect("./index.php");
}
}
function is_Admin($username = "") {
global $connection;
$query = "SELECT user_role FROM users WHERE username = '$username'";
$result = mysqli_query($connection, $query);
confirmQuery($result);
$row = mysqli_fetch_array($result);
if($row['user_role'] === 'admin') {
return true;
}
else {
return false;
}
}
function ifItIsMethod($method=null) {
if($_SERVER['REQUEST_METHOD'] === strtoupper($method)) {
return true;
}
return false;
}
function isLoggedIn() {
if(isset($_SESSION['user_role'])) {
return true;
}
return false;
}
function checkIfUserIsLoggedInAndRedirect($redirectLocation=null) {
if(isLoggedIn()) {
redirect($redirectLocation);
}
}
function loggedInUserId() {
global $connection;
if(isLoggedIn()) {
$result = query("SELECT * FROM users WHERE username='" . $_SESSION['username']. "'");
confirmQuery($result);
$user = mysqli_fetch_array($result);
if(mysqli_num_rows($result) >= 1) {
return $user['user_id'];
}
}
return false;
}
function userLikedThisPost($post_id='') {
if(loggedInUserId()) {
$result = query("SELECT * FROM likes WHERE user_id=" . loggedInUserId() . " AND post_id={$post_id}");
confirmQuery($result);
return mysqli_num_rows($result) >=1 ? true : false;
}
return false;
}
function getPostLikes($post_id) {
$result = query("SELECT * FROM likes where post_id=$post_id");
confirmQuery($result);
echo mysqli_num_rows($result);
}
?>