This repository was archived by the owner on Dec 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunctions.php
More file actions
447 lines (399 loc) · 14.4 KB
/
functions.php
File metadata and controls
447 lines (399 loc) · 14.4 KB
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
<?php
$conn = mysqli_connect('localhost', 'root', '', 'userlogin');
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$username = "";
$fullname = "";
$email = "";
$errors = array();
if (isset($_POST['register_btn'])) {
global $conn, $errors, $username, $fullname, $email;
$username = escape($_POST['username']);
$email = escape($_POST['email']);
$password_1 = escape($_POST['password_1']);
if (mysqli_num_rows(mysqli_query($conn, "SELECT username FROM users WHERE username='$username'")) != 0) {
array_push($errors, "Username đã tồn tại. Vui lòng nhập Username khác");
} elseif (mysqli_num_rows(mysqli_query($conn, "SELECT email FROM users WHERE email='$email'"))) {
array_push($errors, "Email đã tồn tại. Vui lòng nhập Email khác");
} elseif (!preg_match("/(?=.*\d)(?=.*[@#\-_$%^&+=§!\?])(?=.*[a-z])(?=.*[A-Z])[0-9A-Za-z@#\-_$%^&+=§!\?]{8,}/", $password_1)) {
array_push($errors, "Pass phải từ 8 ký tự trở lên và phải có chứ số, chữ in hoa, chữ thường và có ký tự đặc biệt!!");
} else register();
}
function register()
{
global $conn, $errors, $username, $fullname, $email;
$username = escape($_POST['username']);
$fullname = escape($_POST['fullname']);
$email = escape($_POST['email']);
$password_1 = escape($_POST['password_1']);
$password_2 = escape($_POST['password_2']);
if (empty($username)) {
array_push($errors, "Username is required");
}
if (empty($fullname)) {
array_push($errors, "Fullname is required");
}
if (empty($email)) {
array_push($errors, "Email is required");
}
if (empty($password_1)) {
array_push($errors, "Password is required");
}
if ($password_1 != $password_2) {
array_push($errors, "The two passwords do not match");
}
if (count($errors) == 0) {
$user_add = array(
'id' => 0,
'username' => escape($_POST['username']),
'fullname' => escape($_POST['fullname']),
'password' => md5($_POST['password_1']),
'email' => escape($_POST['email']),
);
if (isset($_POST['user_type'])) {
$user_add1 = array(
'id' => 0,
'username' => escape($_POST['username']),
'fullname' => escape($_POST['fullname']),
'user_type' => escape($_POST['user_type']),
'password' => md5($_POST['password_1']),
'email' => escape($_POST['email']),
);
$user_id = save('users', $user_add1);
// $user_type = escape($_POST['user_type']);
// $query = "INSERT INTO users (username,fullname, email, user_type, password)
// VALUES('$username', '$fullname', '$email', '$user_type', '$password')";
// mysqli_query($conn, $query);
$_SESSION['success'] = "New user successfully created!!";
header('location: index.php');
} else {
// $query = "INSERT INTO users (username, fullname, email, user_type, password)
// VALUES('$username', '$fullname', '$email', 'user', '$password')";
// mysqli_query($conn, $query);
$user_id = save('users', $user_add);
$logged_in_user_id = mysqli_insert_id($conn);
$_SESSION['user'] = getUserById($logged_in_user_id); // put logged in user in session
$_SESSION['success'] = "You are now logged in";
header('location: index.php');
}
}
//send mail
include 'lib/config.php';
require 'vendor/autoload.php';
include 'lib/setting.php';
$mail = new PHPMailer(true);
try {
$verificationCode_iduser = md5(uniqid("Email của bạn chưa active. Nhấn vào đây để active nhé!", true));
$verificationCode = PATH_URL . "confirm-user/active.php?code=" . $verificationCode_iduser;
//content
$_SESSION['activeCode'] = $verificationCode_iduser;
$_SESSION['verificationLink'] = $verificationCode;
$htmlStr = "";
$htmlStr .= "Xin chào " . $username . ' (' . $email . "),<br /><br />";
$htmlStr .= "Vui lòng nhấp vào nút bên dưới để xác minh đăng ký của bạn và có quyền truy cập vào trang người dùng cá nhân của PHP Training.<br /><br /><br />";
$htmlStr .= "<a href='{$_SESSION['verificationLink']}' target='_blank' style='padding:1em; font-weight:bold; background-color:blue; color:#fff;'>VERIFY ACCOUNT</a><br /><br /><br />";
$htmlStr .= "Cảm ơn bạn đã tham gia thành một thành viên mới trong website<br><br>";
$htmlStr .= "Trân trọng.<br />";
//Server settings
$mail->CharSet = "UTF-8";
$mail->SMTPDebug = 0; // Enable verbose debug output (0 : ko hiện debug, 1 hiện)
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = SMTP_HOST; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = SMTP_UNAME; // SMTP username
$mail->Password = SMTP_PWORD; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = SMTP_PORT; // TCP port to connect to
//Recipients
$mail->setFrom(SMTP_UNAME, "PHP Training");
$mail->addAddress($_POST['email'], $email); // Add a recipient | name is option tên người nhận
$mail->addReplyTo(SMTP_UNAME, 'PHP TRAINING');
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Verification Users | PHP Training';
$mail->Body = $htmlStr;
$mail->AltBody = $htmlStr; //None HTML
$result = $mail->send();
if (!$result) {
$error = "Có lỗi xảy ra trong quá trình gửi mail";
}
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
$verificationCode_add = array(
'id' => $user_id,
'verificationCode' => $verificationCode_iduser
);
save('users', $verificationCode_add);
}
// function edit($user_id)
// {
// global $conn, $errors, $username, $fullname, $email;
// $username = escape($_POST['username1']);
// $fullname = escape($_POST['fullname1']);
// $email = escape($_POST['email1']);
// $version = intval($_SESSION['version_update'] + 1);
// mysqli_query($conn, "UPDATE `users` SET `username` = '$username', `fullname` = '$fullname', `email`='$email', `version`='$version' WHERE `id` = '$user_id'");
// $_SESSION['success'] = "Change successfully";
// // // header("Refresh:2; url=page2.php");
// if (isset($_COOKIE["user"]) and isset($_COOKIE["pass"])) {
// setcookie("user", '', time() - 3600);
// setcookie("pass", '', time() - 3600);
// }
// unset($_SESSION['version_update']);
// header('location: list.php');
// }
//edit user information
function edit($user_id)
{
global $conn, $errors, $username, $fullname, $email;
$status_entities = 0;
$array_entities = array(
'&', '<', '>', "'", '"', '/'
);
foreach ($array_entities as $entitie) {
if (strlen(strstr(escape($_POST['fullname1']), $entitie)) > 0 || strlen(strstr(escape($_POST['username1']), $entitie)) > 0) {
$status_entities = 1;
}
}
if ($status_entities == 1) {
$_SESSION['mess_entities'] = "Chuỗi bạn nhập vào có ký tự bị cấm. Không thể lưu lại thay đổi";
header('location: index.php');
} else {
$username = escape($_POST['username1']);
$fullname = escape($_POST['fullname1']);
$email = escape($_POST['email1']);
$version = intval($_SESSION['version_update'] + 1);
mysqli_query($conn, "UPDATE `users` SET `username` = '$username', `fullname` = '$fullname', `email`='$email', `version`='$version' WHERE `id` = '$user_id'");
$_SESSION['success'] = "Change successfully";
if (isset($_COOKIE["user"]) and isset($_COOKIE["pass"])) {
setcookie("user", '', time() - 3600);
setcookie("pass", '', time() - 3600);
}
unset($_SESSION['version_update']); // update version edit
$_SESSION['success'] = "This User has successfully changed";
header('location: list.php');
}
}
//end edit
function getUserById($id)
{
global $conn;
$query = "SELECT * FROM users WHERE id=" . $id;
$result = mysqli_query($conn, $query);
$user = mysqli_fetch_assoc($result);
return $user;
}
function escape($val)
{
global $conn;
return mysqli_real_escape_string($conn, trim($val));
}
function display_error()
{
global $errors;
if (count($errors) > 0) {
echo '<div class="error">';
foreach ($errors as $error) {
echo $error . '<br>';
}
echo '</div>';
}
}
function isLoggedIn()
{
if (isset($_SESSION['user'])) {
return true;
} else {
return false;
}
}
// log user out if logout button clicked
if (isset($_GET['logout'])) {
session_destroy();
unset($_SESSION['user']);
if (isset($_COOKIE["user"]) and isset($_COOKIE["pass"])) {
setcookie("user", '', time() - 3600);
setcookie("pass", '', time() - 3600);
}
header("location: login.php");
}
if (isset($_POST['login_btn'])) {
login();
}
// LOGIN USER
function login()
{
global $conn, $username, $errors;
// grap form values
$username = escape($_POST['username']);
$password = escape($_POST['password']);
// make sure form is filled properly
if (empty($username)) {
array_push($errors, "Username is required");
}
if (empty($password)) {
array_push($errors, "Password is required");
}
// attempt login if no errors on form
if (count($errors) == 0) {
$password = md5($password);
$query = "SELECT * FROM users WHERE username='$username' AND password='$password' LIMIT 1";
$query2 = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$results = mysqli_query($conn, $query);
$results2 = mysqli_query($conn, $query2);
$row = mysqli_fetch_array($results2);
if (mysqli_num_rows($results) == 1) { // user found
// check if user is admin or user
$logged_in_user = mysqli_fetch_assoc($results);
if ($logged_in_user['user_type'] == 'admin') {
$_SESSION['user'] = $logged_in_user;
$_SESSION['success'] = "You are now logged in by Admin";
if (isset($_POST['remember'])) {
//thiết lập cookie username và password
setcookie("user", $row['username'], time() + (86400 * 30));
setcookie("pass", $row['password'], time() + (86400 * 30));
}
header('location: home.php');
} else {
$_SESSION['user'] = $logged_in_user;
$_SESSION['success'] = "You are now logged in";
if (isset($_POST['remember'])) {
//thiết lập cookie username và password
setcookie("user", $row['username'], time() + (86400 * 30));
setcookie("pass", $row['password'], time() + (86400 * 30));
}
header('location: index.php');
}
} else {
array_push($errors, "Wrong username/password combination");
}
}
}
function isAdmin()
{
if (isset($_SESSION['user']) && $_SESSION['user']['user_type'] == 'admin') {
return true;
} else {
return false;
}
}
//delete user
function user_delete($id)
{
global $conn;
$id = intval($id);
$sql = "DELETE FROM users WHERE id=$id";
mysqli_query($conn, $sql) or die(mysqli_error($conn));
}
//get value by options
function get_by_options($table, $options = array())
{
$select = isset($options['select']) ? $options['select'] : '*';
$where = isset($options['where']) ? 'WHERE ' . $options['where'] : '';
$order_by = isset($options['order_by']) ? 'ORDER BY ' . $options['order_by'] : '';
$limit = isset($options['offset']) && isset($options['limit']) ? 'LIMIT ' . $options['offset'] . ',' . $options['limit'] : '';
global $conn;
$sql = "SELECT $select FROM `$table` $where $order_by $limit";
$query = mysqli_query($conn, $sql) or die(mysqli_error($conn));
$data = array();
if (mysqli_num_rows($query) > 0) {
while ($row = mysqli_fetch_assoc($query)) {
$data[] = $row;
}
mysqli_free_result($query);
}
return $data;
}
function get_total($table, $options = array())
{
global $conn;
$where = isset($options['where']) ? 'WHERE ' . $options['where'] : '';
$sql = "SELECT COUNT(*) as total FROM `$table` $where";
$query = mysqli_query($conn, $sql) or die(mysqli_error($conn));
$row = mysqli_fetch_assoc($query);
return $row['total'];
}
//pagination admin
function pagination_admin($url, $page, $total)
{
$adjacents = 2;
$out = '<ul class="pagination">';
//first
if ($page == 1) {
$out .= '<li class="page-item disabled"><a class="page-link" href="#" tabindex="-1">Đầu</a></li>';
} else {
$out .= '<li class="page-item"><a class="page-link" href="' . $url . '">Đầu</a></li>';
}
// previous
if ($page == 1) {
$out .= '<li class="page-item disabled"><span class="page-link"><span aria-hidden="true">«</span></li>';
} elseif ($page == 2) {
$out .= '<li class="page-item"><a class="page-link" href="' . $url . '"><span aria-hidden="true">«</span></a></li>';
} else {
$out .= '<li class="page-item"><a class="page-link" href="' . $url . '&page=' . ($page - 1) . '"><span aria-hidden="true">«</span></a></li>';
}
$pmin = ($page > $adjacents) ? ($page - $adjacents) : 1;
$pmax = ($page < ($total - $adjacents)) ? ($page + $adjacents) : $total;
for ($i = $pmin; $i <= $pmax; $i++) {
if ($i == $page) {
$out .= '<li class="page-item active"><span class="page-link">' . $i . '</span></li>';
} elseif ($i == 1) {
$out .= '<li class="page-item"><a class="page-link" href="' . $url . '">' . $i . '</a></li>';
} else {
$out .= '<li class="page-item"><a class="page-link" href="' . $url . "&page=" . $i . '">' . $i . '</a></li>';
}
}
// next
if ($page < $total) {
$out .= '<li class="page-item"><a class="page-link" href="' . $url . '&page=' . ($page + 1) . '"> <span aria-hidden="true">»</span></a></li>';
} else {
$out .= '<li class="page-item disabled"><span class="page-link"><span aria-hidden="true">»</span></span></li>';
}
//last
if ($page < $total) {
$out .= '<li class="page-item"><a class="page-link" href="' . $url . '&page=' . $total . '">Cuối</a></li>';
} else {
$out .= '<li class="page-item disabled"><span class="page-link">Cuối</span></li>';
}
$out .= '</ul>';
return $out;
}
//encode id
function getLink($id)
{
$random = md5(uniqid($id));
$_SESSION['links_edit'][$random] = $_SESSION['info_user_id'][$random] = $id;
return "$random";
}
//lay du lieu tư db theo id
function get_a_record($table, $id, $select = '*')
{
$id = intval($id);
global $conn;
$sql = "SELECT $select FROM `$table` WHERE id=$id";
$query = mysqli_query($conn, $sql) or die(mysqli_error($conn));
$data = NULL;
if (mysqli_num_rows($query) > 0) {
$data = mysqli_fetch_assoc($query);
mysqli_free_result($query);
}
return $data;
}
function save($table, $data = array())
{
$values = array();
global $conn;
foreach ($data as $key => $value) {
$value = mysqli_real_escape_string($conn, $value);
$values[] = "`$key`='$value'";
}
$id = intval($data['id']);
if ($id > 0) {
$sql = "UPDATE `$table` SET " . implode(',', $values) . " WHERE id=$id";
} else {
$sql = "INSERT INTO `$table` SET " . implode(',', $values);
}
mysqli_query($conn, $sql) or die(mysqli_error($conn));
$id = ($id > 0) ? $id : mysqli_insert_id($conn);
return $id;
}