-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.php
159 lines (140 loc) · 4.06 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
<?php
class Database
{
private $conn;
public function __construct()
{
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "Iqbolshoh";
$this->conn = new mysqli($servername, $username, $password, $dbname);
if ($this->conn->connect_error) {
die("Connection failed: " . $this->conn->connect_error);
}
}
public function __destruct()
{
if ($this->conn) {
$this->conn->close();
}
}
function validate($value)
{
$value = trim($value);
$value = stripslashes($value);
$value = str_replace(
[
'‘',
'’',
'“',
'”',
'"',
'„',
'‟',
'‹',
'›',
'«',
'»',
'`',
'´',
'❛',
'❜',
'❝',
'❞',
'〝',
'〞'
],
"'",
$value
);
return $value;
}
function eQuery($sql, $params = [])
{
if ($stmt = $this->conn->prepare($sql)) {
if (!empty($params)) {
$types = str_repeat('s', count($params));
$stmt->bind_param($types, ...$params);
}
if ($stmt->execute()) {
if (strpos($sql, 'SELECT') === 0) {
$result = $stmt->get_result();
return $result->fetch_all(MYSQLI_ASSOC);
}
return true;
} else {
error_log("Xato: " . $stmt->error);
return false;
}
} else {
error_log("Xato: " . $this->conn->error);
return false;
}
}
public function executeQuery($sql)
{
$result = $this->conn->query($sql);
if ($result === false) {
die("Xatolik: " . $this->conn->error);
}
return $result;
}
public function select($table, $columns = "*", $condition = "")
{
$sql = "SELECT $columns FROM $table $condition";
return $this->executeQuery($sql)->fetch_all(MYSQLI_ASSOC);
}
public function getById($table, $id)
{
$id = intval($id);
$condition = "WHERE id = $id";
$result = $this->select($table, "*", $condition);
return $result ? $result[0] : null;
}
function insert($table, $data)
{
$columns = implode(", ", array_keys($data));
$values = implode(", ", array_map(function ($item) {
return "'" . addslashes($item) . "'";
}, array_values($data)));
$sql = "INSERT INTO $table ($columns) VALUES ($values)";
return $this->executeQuery($sql);
}
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);
}
public function delete($table, $condition = "")
{
$sql = "DELETE FROM $table $condition";
return $this->executeQuery($sql);
}
function hashPassword($password)
{
return hash_hmac('sha256', $password, "AccountPassword");
}
public function login($username, $password, $table)
{
$username = $this->validate($username);
$condition = "WHERE username = '" . $username . "' AND password = '" . $this->hashPassword($password) . "'";
return $this->select($table, "*", $condition);
}
public function count($table)
{
$userId = $_SESSION['id'];
$result = $this->executeQuery("SELECT COUNT(*) AS total_elements FROM $table WHERE user_id = $userId");
$row = $result->fetch_assoc();
return $row['total_elements'];
}
function lastInsertId()
{
return $this->conn->insert_id;
}
}