-
Notifications
You must be signed in to change notification settings - Fork 5
/
functions.php
326 lines (262 loc) · 8.99 KB
/
functions.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<?php
session_start();
mysqli_report(MYSQLI_REPORT_OFF);
function connect()
{
return mysqli_connect('localhost', 'root', '', 'ebookapps');
}
function query($query, $associative = false)
{
$conn = connect();
$result = mysqli_query($conn, $query);
if ($associative) {
$rows = [];
while ($row = mysqli_fetch_assoc($result)) {
$rows[] = $row;
}
return $rows;
}
return mysqli_fetch_assoc($result);
}
function addEbook($ebook)
{
$conn = connect();
$username = $_SESSION['username'];
$title = htmlspecialchars($ebook['title']);
$author = htmlspecialchars($ebook['author']);
$category = htmlspecialchars($ebook['category']);
$type = htmlspecialchars($ebook['type']);
$link = htmlspecialchars($ebook['link']);
$cover = uploadCover();
if (empty($title) || empty($author) || empty($category) || empty($type) || empty($link)) {
return false;
}
if (is_null($cover)) {
$cover = 'default-cover.jpg';
} elseif ($cover === false) {
return false;
}
if (!filter_var($link, FILTER_VALIDATE_URL)) {
$link = "https://$link";
} else {
$link = str_replace('http://', 'https://', $link);
}
mysqli_query($conn, "INSERT INTO ebooks (added_by, title, author, category, type, link, cover) VALUES ('$username', '$title', '$author', '$category', '$type', '$link', '$cover')");
if (mysqli_affected_rows($conn) < 0 && $cover !== 'default-cover.jpg') {
unlink("assets/img/ebook/$cover");
}
return mysqli_affected_rows($conn);
}
function uploadCover()
{
$coverName = $_FILES['cover']['name'];
$coverType = $_FILES['cover']['type'];
$coverTmpName = $_FILES['cover']['tmp_name'];
$coverError = $_FILES['cover']['error'];
$coverSize = $_FILES['cover']['size'];
if ($coverError === 4) {
return null;
}
$coverExtension = strtolower(pathinfo($coverName, PATHINFO_EXTENSION));
$allowedExtensions = ['jpg', 'jpeg', 'png'];
if (!in_array($coverExtension, $allowedExtensions)) {
return false;
}
if ($coverType !== 'image/jpeg' && $coverType !== 'image/png') {
return false;
}
if ($coverSize > 1048576) {
return false;
}
$newId = getNewId();
if ($newId < 10) {
$newCoverName = 'IMG-' . date('dmY') . "-EA00$newId.$coverExtension";
} elseif ($newId < 100) {
$newCoverName = 'IMG-' . date('dmY') . "-EA0$newId.$coverExtension";
} else {
$newCoverName = 'IMG-' . date('dmY') . "-EA$newId.$coverExtension";
}
$pathFile = "assets/img/ebook/$newCoverName";
if (file_exists($pathFile)) {
return false;
}
move_uploaded_file($coverTmpName, $pathFile);
return $newCoverName;
}
function getNewId()
{
$ebooks = query('SELECT cover FROM ebooks ORDER BY id DESC', true);
if (!empty($ebooks)) {
foreach ($ebooks as $ebook) {
if (strpos($ebook['cover'], 'IMG') !== false) {
$cover = $ebook['cover'];
break;
} else {
$cover = '';
}
}
$cover = explode('-', $cover);
$coverDate = $cover[1] ?? null;
$coverId = $cover[2] ?? null;
if ($coverDate === date('dmY')) {
$newId = (int)explode('EA', $coverId)[1];
$newId += 1;
} else {
$newId = 1;
}
} else {
$newId = 1;
}
return $newId;
}
function deleteEbook($ebook)
{
$conn = connect();
$id = htmlspecialchars($ebook['id']);
$cover = query("SELECT cover FROM ebooks WHERE id = $id")['cover'];
mysqli_query($conn, "DELETE FROM ebooks WHERE id = $id");
if (mysqli_affected_rows($conn) > 0 && $cover !== 'default-cover.jpg') {
unlink("assets/img/ebook/$cover");
}
return mysqli_affected_rows($conn);
}
function updateEbook($ebook)
{
$conn = connect();
$id = htmlspecialchars($ebook['id']);
$title = htmlspecialchars($ebook['title']);
$author = htmlspecialchars($ebook['author']);
$category = htmlspecialchars($ebook['category']);
$type = htmlspecialchars($ebook['type']);
$link = htmlspecialchars($ebook['link']);
$status = htmlspecialchars($ebook['status']);
$oldCover = htmlspecialchars($ebook['oldCover']);
$cover = uploadCover();
if (empty($id) || empty($title) || empty($author) || empty($category) || empty($type) || empty($link) || empty($status) || empty($oldCover)) {
return false;
}
if (is_null($cover)) {
$cover = $oldCover;
} elseif ($cover === false) {
return false;
}
if (!filter_var($link, FILTER_VALIDATE_URL) && !str_contains($link, 'https://')) {
$link = "https://$link";
} else {
$link = str_replace('http://', 'https://', $link);
}
mysqli_query($conn, "UPDATE ebooks SET title = '$title', author = '$author', category = '$category', type = '$type', link = '$link', status = '$status', cover = '$cover' WHERE id = $id");
if (mysqli_affected_rows($conn) > 0 && $cover !== $oldCover && $oldCover !== 'default-cover.jpg') {
unlink("assets/img/ebook/$oldCover");
} elseif (mysqli_affected_rows($conn) < 0 && $cover !== $oldCover) {
unlink("assets/img/ebook/$cover");
}
return mysqli_affected_rows($conn);
}
function searchEbook($keyword, $index = null, $ebookPerPage = null)
{
if (isset($index, $ebookPerPage)) {
return query("SELECT * FROM ebooks WHERE title LIKE '%$keyword%' OR author LIKE '%$keyword%' OR category LIKE '%$keyword%' OR type LIKE '%$keyword%' OR status LIKE '%$keyword%' ORDER BY id DESC LIMIT $index, $ebookPerPage", true);
}
return query("SELECT * FROM ebooks WHERE title LIKE '%$keyword%' OR author LIKE '%$keyword%' OR category LIKE '%$keyword%' OR type LIKE '%$keyword%' OR status LIKE '%$keyword%'", true);
}
function userSignUp($user)
{
$conn = connect();
$username = str_replace(' ', '', strtolower(mysqli_real_escape_string($conn, $user['username'])));
$password = mysqli_real_escape_string($conn, $user['password']);
$confirmPassword = mysqli_real_escape_string($conn, $user['confirmPassword']);
if (empty(trim($username)) || empty(trim($password))) {
return [
'error' => true,
'message' => 'Invalid username or password!'
];
}
$users = query("SELECT username FROM users WHERE username = '$username'", true);
if (!empty($users)) {
return [
'error' => true,
'message' => 'Username already exists!'
];
}
if (!preg_match("/^.{8,}$/", $password)) {
return [
'error' => true,
'message' => 'Password must be at least 8 characters long!'
];
}
if ($password !== $confirmPassword) {
return [
'error' => true,
'message' => "Password didn't match!"
];
}
$password = password_hash($password, PASSWORD_DEFAULT);
mysqli_query($conn, "INSERT INTO users (username, password) VALUES ('$username', '$password')");
if (mysqli_affected_rows($conn) > 0) {
return [
'success' => true,
'message' => 'Sign up success!'
];
} else {
return [
'error' => true,
'message' => 'Failed to sign up. Try again!'
];
}
}
function userLogin($user)
{
$conn = connect();
$username = mysqli_real_escape_string($conn, $user['username']);
$password = mysqli_real_escape_string($conn, $user['password']);
$user = query("SELECT * FROM users WHERE username = '$username'");
if (empty($user)) {
return false;
}
if (password_verify($password, $user['password'])) {
$_SESSION['username'] = $user['username'];
$_SESSION['role'] = $user['role'];
if (isset($_POST['remember'])) {
setcookie('user_id', $user['id'], time() + (60 * 60 * 24 * 3));
setcookie('user_key', hash('sha256', $user['username']), time() + (60 * 60 * 24 * 3));
setcookie('user_auth', hash('sha256', $user['password']), time() + (60 * 60 * 24 * 3));
}
return true;
}
return false;
}
function checkCookieLogin()
{
if (isset($_COOKIE['user_id'], $_COOKIE['user_key'], $_COOKIE['user_auth'])) {
$id = $_COOKIE['user_id'];
$key = $_COOKIE['user_key'];
$auth = $_COOKIE['user_auth'];
$user = query("SELECT * FROM users WHERE id = $id");
if ($key === hash('sha256', $user['username']) && $auth === hash('sha256', $user['password'])) {
$_SESSION['username'] = $user['username'];
$_SESSION['role'] = $user['role'];
}
}
}
function checkUserLogin()
{
if (!isset($_SESSION['username'])) {
header('Location: login');
exit();
}
}
function checkUserRole()
{
if (isset($_SESSION['username']) && $_SESSION['role'] !== 'admin') {
header('Location: list');
exit();
}
}
function checkUrlId()
{
if (!isset($_GET['id'])) {
header('Location: list');
exit();
}
}