-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.php
273 lines (233 loc) · 7.7 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
class Query
{
private $conn;
public function __construct()
{
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "marketplace";
$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 = htmlspecialchars($value);
$value = mysqli_real_escape_string($this->conn, $value);
return $value;
}
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 insert($table, $data)
{
$keys = implode(', ', array_keys($data));
$values = "'" . implode("', '", array_values($data)) . "'";
$sql = "INSERT INTO $table ($keys) 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)
{
$key = "AccountPassword";
return hash_hmac('sha256', $password, $key);
}
public function authenticate($username, $password, $table)
{
$username = $this->validate($username);
$condition = "WHERE username = '" . $username . "' AND password = '" . $this->hashPassword($password) . "'";
return $this->select($table, "*", $condition);
}
public function registerUser($name, $number, $email, $username, $password, $profile_image, $role)
{
$name = $this->validate($name);
$number = $this->validate($number);
$email = $this->validate($email);
$username = $this->validate($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;
}
function saveImagesToDatabase($files, $path, $productId)
{
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") . rand(1, 9999999) . $productId) . "." . $file_extension;
if (move_uploaded_file($tmp_name, $path . $new_file_name)) {
$uploaded_files[] = $new_file_name;
$this->insert('product_images', array('product_id' => $productId, 'image_url' => $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") . rand(1, 9999999) . $productId) . "." . $file_format;
if (move_uploaded_file($file_tmp, $path . $new_file_name)) {
$this->insert('product_images', array('product_id' => $productId, 'image_url' => $new_file_name));
return $new_file_name;
}
return false;
}
}
function saveImage($file, $path)
{
$file_name = $file['name'];
$file_tmp = $file['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")) . rand(1, 9999999) . "." . $file_format;
if (move_uploaded_file($file_tmp, $path . $new_file_name)) {
return $new_file_name;
}
return false;
}
public function getCategories()
{
$categories = array();
$result = $this->select('categories', 'id, category_name');
foreach ($result as $row) {
$categories[$row['id']] = $row['category_name'];
}
return $categories;
}
public function getProduct($product_id)
{
$result = $this->select('products', '*', 'WHERE id = ' . $product_id);
return $result[0];
}
public function getProductImageID($product_id)
{
$images = $this->select('product_images', 'id', 'WHERE product_id = ' . $product_id);
$id = array();
foreach ($images as $image) {
$id[] = $image['id'];
}
return $id;
}
function getProductImage($id)
{
global $query;
$result = $this->select('product_images', 'image_url', 'WHERE id = ' . $id);
return $result[0]['image_url'];
}
public function getCartItems($user_id)
{
$sql = "SELECT
p.id,
p.name,
p.price_current,
p.price_old,
c.number_of_products,
(p.price_current * c.number_of_products) AS total_price
FROM
cart c
JOIN
products p ON c.product_id = p.id
WHERE
c.user_id = ?";
$stmt = $this->conn->prepare($sql);
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();
$cartItems = $result->fetch_all(MYSQLI_ASSOC);
$stmt->close();
return $cartItems;
}
public function getProductImages($product_id)
{
$sql = "SELECT image_url FROM product_images WHERE product_id = $product_id";
$result = $this->executeQuery($sql)->fetch_all(MYSQLI_ASSOC);
$imageUrls = array();
foreach ($result as $row) {
$imageUrls[] = $row['image_url'];
}
return $imageUrls;
}
public function getWishes($user_id)
{
$sql = "SELECT
p.name,
p.price_current,
p.price_old,
w.product_id
FROM
wishes w
JOIN
products p ON w.product_id = p.id
WHERE
w.user_id = ?";
$stmt = $this->conn->prepare($sql);
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();
$wishes = $result->fetch_all(MYSQLI_ASSOC);
$stmt->close();
return $wishes;
}
public function lastInsertId($table, $data)
{
$keys = implode(', ', array_keys($data));
$values = "'" . implode("', '", array_values($data)) . "'";
$sql = "INSERT INTO $table ($keys) VALUES ($values)";
$insert_result = $this->executeQuery($sql);
if ($insert_result) {
return $this->conn->insert_id;
} else {
return false;
}
}
}