-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.inc.php
574 lines (495 loc) · 16.4 KB
/
functions.inc.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
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
<?php
// Admin register empty input check
function adminRegisterEmptyInput($name, $email, $pwd, $repwd)
{
if (empty($name) || empty($email) || empty($pwd) || empty($repwd)) {
$result = true;
} else {
$result = false;
}
return $result;
}
// Admin & User register invalid username check (Must be A-Z, a-z, 0-9)
function invaliduName($name)
{
if (!preg_match("/^[a-zA-Z0-9]*$/", $name)) {
$result = true;
} else {
$result = false;
}
return $result;
}
// Admin & User register invalid password check (Must be at least 4 characters)
function invalidPwd($pwd)
{
if (strlen($pwd) >= 4) {
$result = true;
} else {
$result = false;
}
return $result;
}
// Admin & User invalid email check
function invalidEmail($email)
{
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$result = true;
} else {
$result = false;
}
return $result;
}
// Admin & User register password and re-enter password check
function pwdMatch($pwd, $repwd)
{
if ($pwd !== $repwd) {
$result = true;
} else {
$result = false;
}
return $result;
}
// Admin existing username or email check
function adminuNameEmailExists($conn, $name)
{
$sql = "SELECT * FROM admin WHERE adminName = ?";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("location: ../admin/main/register.php?error=stmtFailed");
exit();
}
mysqli_stmt_bind_param($stmt, "s", $name);
mysqli_stmt_execute($stmt);
$resultData = mysqli_stmt_get_result($stmt);
if ($row = mysqli_fetch_assoc($resultData)) {
return $row;
} else {
$result = false;
return $result;
}
mysqli_stmt_close($stmt);
}
// Create new admin account
function adminRegister($conn, $name, $email, $pwd)
{
$sql = "INSERT INTO admin (adminName, adminEmail, adminPwd) VALUES (?, ?, ?);";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("location: ../admin/main/register.php?error=stmtFailed");
exit();
}
$hashedPwd = password_hash($pwd, PASSWORD_DEFAULT);
mysqli_stmt_bind_param($stmt, "sss", $name, $email, $hashedPwd);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
header("location: ../admin/main/register.php?error=none");
exit();
}
// Admin login empty input check
function adminLoginEmptyInput($name, $pwd)
{
if (empty($name) || empty($pwd)) {
$result = true;
} else {
$result = false;
}
return $result;
}
// Admin get issue details of all users
function adminGetIssueDataView($conn)
{
$sql = "SELECT user.*, issue.*, IF(status, 'Solved', 'Pending') status FROM issue, user WHERE issue.userId = user.userId ORDER BY timestamp DESC;";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
return $result;
} else {
header("location: ../admin/issues/viewIssue.php?error=notWorking");
exit();
}
}
// Admin get user details
function adminGetUserData($conn)
{
$sql = "SELECT userId, userName, userEmail, userPhone FROM user";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
return $result;
} else {
header("location: ../admin/users/viewUser.php?error=notWorking");
exit();
}
}
// Admin delete issue
function adminDeleteIssue($conn)
{
if (isset($_GET['issueId']) && is_numeric($_GET['issueId'])) {
$issuesId = $_GET['issueId'];
$sql = "DELETE FROM issue WHERE issueId='$issuesId';";
$delete_issue = mysqli_query($conn, $sql);
if ($delete_issue) {
header("location: viewIssue.php?error=deleted");
exit();
} else {
header("location: viewIssue.php?error=cantDelete");
exit();
}
}
}
// User existing username or email check
function useruNameEmailExists($conn, $name, $email)
{
$sql = "SELECT * FROM user WHERE userName = ? OR userEmail = ?;";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("location: ../user/main/register.php?error=stmtFailed");
exit();
}
mysqli_stmt_bind_param($stmt, "ss", $name, $email);
mysqli_stmt_execute($stmt);
$resultData = mysqli_stmt_get_result($stmt);
if ($row = mysqli_fetch_assoc($resultData)) {
return $row;
} else {
$result = false;
return $result;
}
mysqli_stmt_close($stmt);
}
// User register empty input check
function userRegisterEmptyInput($name, $email, $phone, $pwd, $repwd)
{
if (empty($name) || empty($email) || empty($phone) || empty($pwd) || empty($repwd)) {
$result = true;
} else {
$result = false;
}
return $result;
}
// Create new user account
function userRegister($conn, $name, $email, $phone, $pwd)
{
$sql = "INSERT INTO user (userName, userEmail, userPhone, userPwd) VALUES (?, ?, ?, ?);";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("location: ../user/main/register.php?error=stmtFailed");
exit();
}
$hashedPwd = password_hash($pwd, PASSWORD_DEFAULT);
mysqli_stmt_bind_param($stmt, "ssss", $name, $email, $phone, $hashedPwd);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
header("location: ../user/main/register.php?error=none");
exit();
}
// User login empty input check
function userLoginEmptyInput($name, $pwd)
{
if (empty($name) || empty($pwd)) {
$result = true;
} else {
$result = false;
}
return $result;
}
// User login validation
function userLogin($conn, $name, $pwd)
{
$uNameExists = useruNameEmailExists($conn, $name, $name);
if ($uNameExists === false) {
header("location: ../user/main/login.php?error=invalidLogin");
exit();
}
$pwdHashed = $uNameExists["userPwd"];
$checkPwd = password_verify($pwd, $pwdHashed);
if ($checkPwd === false) {
header("location: ../user/main/login.php?error=invalidLogin");
exit();
} else if ($checkPwd === true) {
session_start();
$_SESSION["userId"] = $uNameExists["userId"];
$_SESSION["userName"] = $uNameExists["userName"];
header("location: ../user/main/dashboard.php");
exit();
}
}
// Admin login validation
function adminLogin($conn, $name, $pwd)
{
$adminnameExists = adminuNameEmailExists($conn, $name);
if ($adminnameExists === false) {
header("location: ../admin/main/login.php?error=invalidLogin");
exit();
}
$pwdHashed = $adminnameExists["adminPwd"];
$checkPwd = password_verify($pwd, $pwdHashed);
if ($checkPwd === false) {
header("location: ../admin/main/login.php?error=invalidLogin");
exit();
} else if ($checkPwd === true) {
session_start();
$_SESSION["adminId"] = $adminnameExists["adminId"];
$_SESSION["adminName"] = $adminnameExists["adminName"];
header("location:../admin/main/dashboard.php");
exit();
}
}
// User add issue empty input check
function userAddIssueEmptyInput($title, $des)
{
if (empty($title) || empty($des)) {
$result = true;
} else {
$result = false;
}
return $result;
}
// User add new issue
function addIssue($conn, $userId, $title, $description, $status)
{
$sql = "INSERT INTO issue (userId ,title, description, status, timestamp) VALUES (?, ?, ?, ?, now());";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("location: ../user/issues/addIssue.php?error=stmtFailed");
exit();
}
mysqli_stmt_bind_param($stmt, "sssi", $userId, $title, $description, $status);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
header("location: ../user/issues/addIssue.php?error=none");
exit();
}
// Admin & User get user details for update
function getUserDataUpdate($conn)
{
if (isset($_GET['userId']) && is_numeric($_GET['userId'])) {
$userId = $_GET['userId'];
$sql = "SELECT userId, userName, userEmail, userPhone FROM user WHERE userId='$userId';";
$get_userId = mysqli_query($conn, $sql);
if (mysqli_num_rows($get_userId) == 1) {
$row = mysqli_fetch_assoc($get_userId);
return ($row);
}
}
}
// Admin update user empty input check
function adminUpdateUserEmptyInput($name, $email, $phone)
{
if (empty($name) || empty($email) || empty($phone)) {
$result = true;
} else {
$result = false;
}
return $result;
}
// Update user details (userName, userEmail, userPhone)
function updateUserData($conn, $userId, $userName, $userEmail, $userPhone)
{
$sql = "UPDATE user SET userName='$userName', userEmail='$userEmail', userPhone='$userPhone' WHERE userId='$userId';";
$update_query = mysqli_query($conn, $sql);
if ($update_query) {
header("location: ../admin/users/viewUser.php?error=none");
exit();
} else {
header("location: ../admin/users/viewUser.php?error=cantUpdate");
exit();
}
}
// Admin delete user account
function deleteUser($conn)
{
if (isset($_GET['userId']) && is_numeric($_GET['userId'])) {
$usersId = $_GET['userId'];
$sql = "DELETE FROM user WHERE userId='$usersId';";
$delete_user = mysqli_query($conn, $sql);
if ($delete_user) {
header("location: viewuser.php?error=deleted");
exit();
} else {
header("location: viewuser.php?error=cantDelete");
exit();
}
}
}
// Admin get issue details for update
function adminGetIssueDataUpdate($conn)
{
if (isset($_GET['issueId']) && is_numeric($_GET['issueId'])) {
$issueId = $_GET['issueId'];
$sql = "SELECT issue.userId, userName, issueId, title, description, IF(status, 'Solved', 'Pending') status FROM issue, user WHERE issue.userId = user.userId AND issueId='$issueId';";
$get_issueId = mysqli_query($conn, $sql);
if (mysqli_num_rows($get_issueId) == 1) {
$row = mysqli_fetch_assoc($get_issueId);
return ($row);
}
}
}
// Admin get issue details for view
function adminGetIssueDataAll($conn)
{
if (isset($_GET['issueId']) && is_numeric($_GET['issueId'])) {
$issueId = $_GET['issueId'];
$sql = "SELECT issueId, title, description, timestamp, issue.userId, userName, userEmail, userPhone, IF(status, 'Solved', 'Pending') status FROM issue, user WHERE issue.userId = user.userId AND issueId='$issueId';";
$get_issueId = mysqli_query($conn, $sql);
if (mysqli_num_rows($get_issueId) == 1) {
$row = mysqli_fetch_assoc($get_issueId);
return ($row);
}
}
}
// Admin update issue (status)
function adminUpdateIssue($conn, $issueId, $status)
{
// $sql = "UPDATE issue SET status=" . $status . " WHERE issueId='$issueId';";
$sql = "UPDATE issue SET status=$status WHERE issueId='$issueId';";
$update_query = mysqli_query($conn, $sql);
if ($update_query) {
header("location: ../admin/issues/viewIssue.php?error=none");
exit();
} else {
header("location: ../admin/issues/viewIssue.php?error=cantUpdate");
exit();
}
}
// User view issues
function yourIssues($conn, $userId)
{
$sql = "SELECT issueId, title, description, timestamp, IF(status, 'Solved', 'Pending') status FROM issue WHERE userId='$userId' ORDER BY timestamp DESC;";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
return $result;
exit();
} else {
// header("location: ../user/issues/viewIssue.php?error=notworking");
echo "<p class=\"success2\">There are no issues available!</p>";
exit();
}
}
// Admin get pending issue details of all users
function adminPendingIssues($conn)
{
$sql = "SELECT issueId, userName, title, description, timestamp, IF(status, 'Solved', 'Pending') status FROM issue, user WHERE issue.userId = user.userId AND status=false ORDER BY timestamp DESC LIMIT 10;";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
return $result;
exit();
} else {
// header("location: ../user/issues/viewIssue.php?error=notworking");
echo "<p class=\"success2\">There are no issues available!</p>";
exit();
}
}
// User get pending issue details
function userPendingIssues($conn, $userId)
{
$sql = "SELECT issueId, title, description, timestamp, IF(status, 'Solved', 'Pending') status FROM issue WHERE userId='$userId' AND status=false ORDER BY timestamp DESC LIMIT 10;";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
return $result;
exit();
} else {
// header("location: ../user/issues/viewIssue.php?error=notworking");
echo "<p class=\"success2\">There are no issues available!</p>";
exit();
}
}
// Admin get solved issue details of all users
function adminSolvedIssues($conn)
{
$sql = "SELECT issueId, userName, title, description, timestamp, IF(status, 'Solved', 'Pending') status FROM issue, user WHERE issue.userId = user.userId AND status=true ORDER BY timestamp DESC LIMIT 10;";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
return $result;
exit();
} else {
// header("location: ../user/issues/viewIssue.php?error=notworking");
echo "<p class=\"success2\">There are no issues available!</p>";
exit();
}
}
// User get solved issue details of all users
function userSolvedIssues($conn, $userId)
{
$sql = "SELECT issueId, title, description, timestamp, IF(status, 'Solved', 'Pending') status FROM issue WHERE userId='$userId' AND status=true ORDER BY timestamp DESC LIMIT 10;";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
return $result;
exit();
} else {
// header("location: ../user/issues/viewIssue.php?error=notworking");
echo "<p class=\"success2\">There are no issues available!</p>";
exit();
}
}
// Admin get most issues by users
function adminMostIssues($conn)
{
$sql = "SELECT user.userId, userName, userEmail, userPhone, COUNT(issueId) FROM user, issue WHERE user.userId = issue.userId GROUP BY issue.userId ORDER BY COUNT(issueId) DESC LIMIT 10";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
return $result;
exit();
} else {
// header("location: ../user/issues/viewIssue.php?error=notworking");
echo "<p class=\"success2\">There are no users available!</p>";
exit();
}
}
// User update profile empty input check (Only password)
function userUpdateEmptyInput($pwd, $repwd)
{
if (empty($pwd) || empty($repwd)) {
$result = true;
} else {
$result = false;
}
return $result;
}
// User update profile (Only password)
function userUpdateProfile($conn, $userId, $pwd)
{
$hashedPwd = password_hash($pwd, PASSWORD_DEFAULT);
$sql = "UPDATE user SET userPwd='$hashedPwd' WHERE userId='$userId';";
$update_query = mysqli_query($conn, $sql);
if ($update_query) {
header("location: ../user/profile/updateUser.php?userId=" . $userId . "&error=none");
exit();
} else {
header("location: ../user/profile/updateUser.php?userId=" . $userId . "&error=cantUpdate");
exit();
}
}
// User delete profile
function userDeleteProfile($conn)
{
if (isset($_GET['userId']) && is_numeric($_GET['userId'])) {
$userId = $_GET['userId'];
$sql = "DELETE FROM user WHERE userId='$userId';";
$delete_user = mysqli_query($conn, $sql);
if ($delete_user) {
session_start();
session_unset();
session_destroy();
header("location: ../main/login.php?error=deleted");
exit();
} else {
header("location: updateUser.php?error=cantDelete");
exit();
}
}
}
// User delete issue
function userDeleteIssue($conn)
{
if (isset($_GET['issueId']) && is_numeric($_GET['issueId'])) {
$issueId = $_GET['issueId'];
$sql = "DELETE FROM issue WHERE issueId='$issueId';";
$delete_issue = mysqli_query($conn, $sql);
if ($delete_issue) {
header("location: ../issues/viewIssue.php?error=deleted");
exit();
} else {
header("location: ../issues/viewIssue.php?error=cantDelete");
exit();
}
}
}