-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin_profile_utils.php
175 lines (138 loc) · 4.12 KB
/
admin_profile_utils.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
<?php
/**
* This script provides blocking/blacklisting utilities for admins.
* It is intended to be 'required' into the profile scripts to access variables such as user_type etc
*/
require_once "database.php";
$banned = false;
$blacklisted = false;
/**
* Get the type of the user
*/
function getUserType($username) {
global $conn;
$sql = "SELECT type FROM accounts WHERE username = ?;";
if ($stmt = $conn->prepare($sql)) {
$stmt->bind_param("s", $param_username);
$param_username = $username;
$type = null;
if ($stmt->execute()) {
$result = $stmt->get_result();
if ($result->num_rows == 1) {
while ($row = $result->fetch_assoc()) {
$type = $row['type'];
}
}
} else {
doSQLError($stmt->error);
}
$stmt->close();
return $type;
} else {
doSQLError($conn->error);
}
}
/**
* Remove the ban if it expired
*/
function removeBan($username) {
global $conn;
$sql = "DELETE FROM banned_users WHERE username = ?;";
if ($stmt = $conn->prepare($sql)) {
$stmt->bind_param("s", $param_user);
$param_user = $username;
if (!$stmt->execute()) {
doSQLError($stmt->error);
}
$stmt->close();
} else {
doSQLError($conn->error);
}
}
/**
* Check if the user is banned or not
*/
function checkBanned($username) {
global $conn;
global $banned;
$sql = "SELECT * FROM banned_users WHERE username = ?;";
if ($stmt = $conn->prepare($sql)) {
$stmt->bind_param("s", $param_user);
$param_user = $username;
if ($stmt->execute()) {
$result = $stmt->get_result();
if ($result->num_rows == 1) {
while ($row = $result->fetch_assoc()) {
$date_to = $row['date_to'];
$date_to = strtotime($date_to);
$cur_time = time();
$banned = $cur_time < $date_to;
if (!$banned) {
removeBan($username);
}
}
}
return $banned;
} else {
doSQLError($stmt->error);
}
$stmt->close();
} else {
doSQLError($conn->error);
}
}
/**
* Check if the user is blacklisted or not
*/
function checkBlacklist($username) {
global $conn;
global $blacklisted;
$sql = "SELECT email FROM email_blacklist NATURAL JOIN accounts WHERE username = ?;";
if ($stmt = $conn->prepare($sql)) {
$stmt->bind_param("s", $param_user);
$param_user = $username;
if ($stmt->execute()) {
$stmt->store_result();
$blacklisted = $stmt->num_rows == 1;
return $blacklisted;
} else {
doSQLError($stmt->error);
}
$stmt->close();
} else {
doSQLError($conn->error);
}
}
/**
* Get the block button to display.
* checkBanned and checkBlacklist should be called before this call
*/
function getBlockButton() {
global $banned;
global $blacklisted;
if (!$blacklisted) {
$button = "<button type=\"button\" class=\"btn btn-danger\" style=\"margin-right: 1vw;\"";
if ($banned) {
$button = $button." onclick=\"ban();\">Unban</button>";
} else {
$button = $button." data-bs-toggle=\"modal\" data-bs-target=\"#banModal\">Ban</button>";
}
echo $button;
}
echo "";
}
/**
* Get the blacklist button to display.
* checkBanned and checkBlacklist should be called before this call
*/
function getBlacklistButton() {
global $blacklisted;
$button = "<button type=\"button\" class=\"btn btn-dark\"";
if ($blacklisted) {
$button = $button." onclick=\"blacklist();\">Unblacklist</button>";
} else {
$button = $button." data-bs-toggle=\"modal\" data-bs-target=\"#blacklistModal\">Blacklist</button>";
}
echo $button;
}
?>