-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.php
215 lines (188 loc) · 6.44 KB
/
config.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
<?php
session_start();
class Query
{
private $conn;
public function __construct()
{
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "Roles";
$this->conn = new mysqli($servername, $username, $password, $dbname);
if ($this->conn->connect_error) {
die("Connection error: " . $this->conn->connect_error);
}
}
public function __destruct()
{
if ($this->conn) {
$this->conn->close();
}
}
// sanitize() sanitizes input by escaping special characters
function sanitize($value)
{
$value = trim($value);
$value = stripslashes($value);
$value = htmlspecialchars($value);
$value = mysqli_real_escape_string($this->conn, $value);
return $value;
}
// executeQuery() executes a SQL query
public function executeQuery($sql)
{
$result = $this->conn->query($sql);
if ($result === false) {
die("Error: " . $this->conn->error);
}
return $result;
}
// select() retrieves data from the database
public function select($table, $columns = "*", $condition = "")
{
$sql = "SELECT $columns FROM $table $condition";
return $this->executeQuery($sql)->fetch_all(MYSQLI_ASSOC);
}
// insert() inserts data into the database
public function insert($table, $data)
{
$keys = implode(', ', array_keys($data));
$values = "'" . implode("', '", array_values($data)) . "'";
$sql = "INSERT INTO $table ($keys) VALUES ($values)";
return $this->executeQuery($sql);
}
// update() updates data in the database
public function update($table, $data, $condition = "")
{
$set = '';
foreach ($data as $key => $value) {
$set .= "$key = '$value', ";
}
$set = rtrim($set, ', ');
$sql = "UPDATE $table SET $set $condition";
return $this->executeQuery($sql);
}
// delete() deletes data from the database
public function delete($table, $condition = "")
{
$sql = "DELETE FROM $table $condition";
return $this->executeQuery($sql);
}
// hashPassword() hashes a password using a key
function hashPassword($password)
{
$key = "AccountPassword";
return hash_hmac('sha256', $password, $key);
}
// authenticate() verifies user credentials
public function authenticate($username, $password, $table)
{
$username = $this->sanitize($username);
$condition = "WHERE username = '" . $username . "' AND password = '" . $this->hashPassword($password) . "'";
return $this->select($table, "*", $condition);
}
// registerUser() registers a new user
public function registerUser($name, $number, $email, $username, $password, $profile_image, $role)
{
$name = $this->sanitize($name);
$number = $this->sanitize($number);
$email = $this->sanitize($email);
$username = $this->sanitize($username);
$password_hash = $this->hashPassword($password);
$data = array(
'name' => $name,
'number' => $number,
'email' => $email,
'username' => $username,
'password' => $password_hash,
'profile_image' => $profile_image,
'role' => $role
);
$user_id = $this->insert('accounts', $data);
if ($user_id) {
return $user_id;
}
return false;
}
// saveImage() handles file upload and saving
function saveImage($files, $path)
{
if (is_array($files['tmp_name'])) {
$uploaded_files = array();
foreach ($files['tmp_name'] as $index => $tmp_name) {
$file_name = $files['name'][$index];
$file_info = pathinfo($file_name);
$file_extension = $file_info['extension'];
$new_file_name = md5($tmp_name . date("Y-m-d_H-i-s") . $_SESSION['username']) . "." . $file_extension;
if (move_uploaded_file($tmp_name, $path . $new_file_name)) {
$uploaded_files[] = $new_file_name;
}
}
return $uploaded_files;
} else {
$file_name = $files['name'];
$file_tmp = $files['tmp_name'];
$file_info = pathinfo($file_name);
$file_format = $file_info['extension'];
$new_file_name = md5($file_tmp . date("Y-m-d_H-i-s") . $_SESSION['username']) . "." . $file_format;
if (move_uploaded_file($file_tmp, $path . $new_file_name)) {
return $new_file_name;
}
return false;
}
}
// isBlocked() checks if a user is blocked
public function isBlocked()
{
if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] === true) {
$result = $this->select('accounts', 'status', 'WHERE username = "' . $_SESSION['username'] . '"');
return $result[0]['status'] === 'blocked';
}
return false;
}
// checkAuthentication() redirects users based on their role
function checkAuthentication()
{
if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] === true) {
if ($this->isBlocked()) {
header("Location: /blocked_page.php");
exit;
} elseif ($_SESSION['role'] === 'admin') {
header("Location: /admin/");
exit;
} elseif ($_SESSION['role'] === 'user') {
header("Location: /");
exit;
}
} else {
header("Location: /login/");
exit;
}
}
// checkAdminRole() restricts access to admin users only
function checkAdminRole()
{
if ($_SESSION['role'] !== 'admin' or $this->isBlocked()) {
$this->checkAuthentication();
exit;
}
}
// checkUserRole() restricts access to general users only
function checkUserRole()
{
if ($_SESSION['role'] !== 'user' or $this->isBlocked()) {
$this->checkAuthentication();
exit;
}
}
// getCategories() retrieves categories from the database
public function getCategories()
{
$categories = array();
foreach ($this->select('categories', 'category_name') as $row) {
$categories[] = $row['category_name'];
}
return $categories;
}
}