-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.php
100 lines (89 loc) · 3.02 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
<?php
class Query
{
private $conn;
// Constructor: Initializes the database connection
public function __construct()
{
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database";
$this->conn = new mysqli($servername, $username, $password, $dbname);
if ($this->conn->connect_error) {
die("Connection failed: " . $this->conn->connect_error);
}
}
// Destructor: Closes the database connection
public function __destruct()
{
if ($this->conn) {
$this->conn->close();
}
}
// validate(): Escapes special characters to prevent HTML injection
public function validate($data)
{
foreach ($data as $key => $value) {
$value = trim($value); // Remove whitespace from the beginning and end
$value = stripslashes($value); // Remove backslashes
$value = htmlspecialchars($value); // Convert special characters to HTML entities
$data[$key] = $value;
}
return $data;
}
// executeQuery(): Executes a given 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 HMAC with SHA-256
public function hashPassword($password)
{
$key = "AccountPassword";
$hashed_password = hash_hmac('sha256', $password, $key);
return $hashed_password;
}
// authenticate(): Checks user credentials for login
public function authenticate($username, $password, $table)
{
$password_hash = $this->hashPassword($password);
$condition = "WHERE username = '$username' AND password = '$password_hash'";
return $this->select($table, "*", $condition);
}
}