-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUser.php
More file actions
778 lines (616 loc) · 24.8 KB
/
User.php
File metadata and controls
778 lines (616 loc) · 24.8 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
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/phpmailer/phpmailer/src/Exception.php';
require 'vendor/phpmailer/phpmailer/src/PHPMailer.php';
require 'vendor/phpmailer/phpmailer/src/SMTP.php';
include_once "mysqldatabase.php";
class User {
public $user_id;
public $fname;
public $lname;
public $email;
public $password;
public $phone;
public $age;
public $gender;
public $weight;
public $weight_unit;
public $height;
public $height_unit;
public $additional_note;
public $group_id;
public $database;
public function __construct($id = -1) {
global $conn;
// Initialize default values
$this->user_id = -1;
$this->fname = "";
$this->lname = "";
$this->email = "";
$this->password = "";
$this->phone = "";
$this->age = -1;
$this->gender = "";
$this->weight = -1;
$this->weight_unit = "";
$this->height = -1;
$this->height_unit = "";
$this->additional_note = "";
$this->group_id = -1;
$this->database = "justbfitness";
// Load user data if a valid ID is provided
if ($id > 0) {
// Fetch user details from the database
$sql = "SELECT * FROM `user` WHERE user_id = ?";
$stmt = $conn->prepare($sql);
// Check if the prepared statement was successful
if (!$stmt) {
throw new Exception("Error preparing statement: " . $conn->error);
}
$stmt->bind_param('i', $id);
$stmt->execute();
$result = $stmt->get_result();
// Check if a user was found
if ($result->num_rows > 0) {
$assocUser = $result->fetch_assoc();
// Populate the object's properties with the fetched data
$this->user_id = $assocUser['user_id'];
$this->fname = $assocUser['fname'];
$this->lname = $assocUser['lname'];
$this->email = $assocUser['email'];
$this->password = $assocUser['password']; // This should be a hashed password
$this->phone = $assocUser['phone'];
$this->age = $assocUser['age'];
$this->gender = $assocUser['gender'];
$this->weight = $assocUser['weight'];
$this->weight_unit = $assocUser['weight_unit'];
$this->height = $assocUser['height'];
$this->height_unit = $assocUser['height_unit'];
$this->additional_note = $assocUser['additional_note'];
$this->group_id = $assocUser['group_id'];
$this->database = "justbfitness";
$stmt->close();
}
}
}
public static function login() {
global $conn;
$sql = "SELECT * FROM user WHERE email = '". $_POST['email'] . "' ";
//var_dump($sql);
$result = $conn->query($sql);
$row = $result->fetch_assoc();
if(password_verify($_POST['password'], $row['password'])){
$user = new User();
$user->user_id = $row['user_id'];
$user->email = $row['email'];
$user->password = $row['password'];
$user->fname = $row['fname'];
$user->lname = $row['lname'];
$user->group_id = $row['group_id'];
$_SESSION['user'] = $user;
/*
$group_id = $_SESSION['user']->group_id;
if ($group_id === 1) {
header("Location: ?controller=user&action=index");
} else if ($group_id === 2) {
header("Location: ?controller=user");
}
*/
header("Location: ?controller=user"); // ADDED
}else{
$_SESSION['alert'] = "Login unsuccessful. Please try again.";
}
}
public function register($firstName, $lastName, $phone, $email, $password, $confirmPassword, $groupId = 1) {
global $conn;
// Validate input data
if (empty($firstName) || empty($lastName) || empty($phone) || empty($email) || empty($password)) {
$error_message ="All fields are required.";
$this->render("Home", "register", array('error' => $error_message));
exit();
//"All fields are required.";
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error_message ="Invalid email format.";
$this->render("Home", "register", array('error' => $error_message));
exit();
//"Invalid email format.";
}
if ($password !== $confirmPassword) {
$error_message = "Passwords do not match.";
$this->render("Home", "register", array('error' => $error_message));
exit();
//return "Passwords do not match.";
}
// Hash password
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
// Prepare database query
$query = 'INSERT INTO user (fname, lname, phone, email, password, group_id) VALUES (?, ?, ?, ?, ?, ?)';
try{
if ($stmt = $conn->prepare($query)) {
// Bind parameters
$stmt->bind_param("sssssi", $firstName, $lastName, $phone, $email, $hashedPassword, $groupId);
// Execute query and check for successful insertion
if ($stmt->execute()) {
return true;
} else {
// Handle errors, e.g., duplicate entry
$error_message = "An error occurred: " . $stmt->error;
$this->render("Home", "register", array('error' => $error_message));
exit();
//"An error occurred: " . $stmt->error;
}
}
else {
// Handle preparation error
$error_message = "An error occurred during query preparation: " . $conn->error;
$this->render("Home", "register", array('error' => $error_message));
exit();
//"An error occurred during query preparation: " . $conn->error;
}
}catch (mysqli_sql_exception $exception) {
// Handle specific error (duplicate entry)
if ($exception->getCode() == 1062) { // 1062 is the MySQL error code for duplicate entry
$error_message = "Email address is already in use.";
$this->render("Home", "register", ['error' => $error_message]);
exit();
} else {
// Handle other exceptions
$error_message = "An unexpected error occurred: " . $exception->getMessage();
$this->render("Home", "register", ['error' => $error_message]);
exit();
}
}
}
public static function generateRandomCode($length = 6) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$code = '';
for ($i = 0; $i < $length; $i++) {
$code .= $characters[rand(0, strlen($characters) - 1)];
}
return $code;
}
public static function sendEmail( $email){
$mail = new PHPMailer(true);
$_SESSION['randomCode'] = self::generateRandomCode();
$_SESSION['email'] = $email;
try {
//Server settings
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'duckdolphin1@gmail.com';
$mail->Password = 'nxdwnindksrmhdiq';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
//Recipient
$mail->setFrom('JUSTBFITNESS@gmail.com', 'JUSTBFITNESS');
$mail->addAddress($email);
//Content
$mail->isHTML(true);
$mail->Subject = 'Confirmation Code!';
$mail->Body = 'Your confirmation code is: ' . $_SESSION['randomCode'];
$mail->send();
echo "<script> alert('Email sent successfully'); </script>";
} catch (Exception $e) {
echo "Error: {$mail->ErrorInfo}";
}
}
public static function confirmCode(){
$enteredCode = $_POST['code'];
if ($enteredCode == $_SESSION['randomCode']) {
echo "<script>alert('Code is valid. Email confirmed!');</script>";
return true;
} else {
// Code is incorrect
return false;
}
}
public static function changePassword($password){
global $conn;
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
$email = $_SESSION['email'];
$sql = 'UPDATE `user` SET password = ? WHERE email = ?';
$stmt = $conn->prepare($sql);
// Check if the prepared statement was successful
if (!$stmt) {
return 0;
}
$stmt->bind_param('ss',$hashedPassword, $email);
$stmt->execute();
if ($stmt->errno) {
$stmt->close();
return 0;
}
$stmt->close();
}
public static function search(){
global $conn;
$lookupTerm = isset($_POST['query']) ? $_POST['query'] : '';
if (!empty($lookupTerm)) {
$sql = "SELECT * FROM `user` WHERE (`fname` LIKE ? OR `lname` LIKE ?) AND `group_id` = 1";
$stmt = $conn->prepare($sql);
$lookupTerm = "%$lookupTerm%";
$stmt->bind_param('ss', $lookupTerm, $lookupTerm);
$stmt->execute();
$result = $stmt->get_result();
$stmt->close();
if ($result->num_rows > 0) {
$rows = array();
while ($row = $result->fetch_assoc()) {
$rows[] = $row;
}
return $rows;
}
}
return null;
}
public static function listByQuery() {
global $conn;
$lookupTerm = isset($_POST['query']) ? $_POST['query'] : '';
// Use a placeholder for the condition in the WHERE clause
$sql = 'SELECT
USER.USER_ID,
USER.FNAME,
USER.LNAME,
USER.EMAIL,
USER.GROUP_ID,
`GROUP`.GROUP_NAME
FROM
USER
INNER JOIN
`GROUP` ON USER.GROUP_ID = `GROUP`.GROUP_ID
WHERE
USER.FNAME LIKE ? OR
USER.LNAME LIKE ? OR
USER.EMAIL LIKE ?';
$stmt = $conn->prepare($sql);
if (!$stmt) {
die("Error in SQL query: " . $conn->error);
}
// Bind the parameters to the placeholders
$searchTerm = "%$lookupTerm%";
$stmt->bind_param("sss", $searchTerm, $searchTerm, $searchTerm);
$stmt->execute();
$result = $stmt->get_result();
// Fetch all rows into an associative array
$data = $result->fetch_all(MYSQLI_ASSOC);
// Close the statement
$stmt->close();
return $data;
}
public static function list() {
global $conn;
$sql = 'SELECT
USER.USER_ID,
USER.FNAME,
USER.LNAME,
USER.EMAIL,
USER.GROUP_ID,
`GROUP`.GROUP_NAME
FROM
USER
INNER JOIN
`GROUP` ON USER.GROUP_ID = `GROUP`.GROUP_ID
WHERE
USER.GROUP_ID = 1';
$stmt = $conn->prepare($sql);
if (!$stmt) {
die("Error in SQL query: " . $conn->error);
}
$stmt->execute();
$result = $stmt->get_result();
// Fetch all rows into an associative array
$data = $result->fetch_all(MYSQLI_ASSOC);
// Close the statement
$stmt->close();
return $data;
}
public static function read() {
global $conn;
// Check if user ID is present in the session
$userId = isset($_SESSION['user']) ? $_SESSION['user']->user_id : null;
$sql = "SELECT * FROM `USER` WHERE USER_ID = ?";
$stmt = $conn->prepare($sql);
// Check if the prepared statement was successful
if (!$stmt) {
return null; // or handle the error as needed
}
$stmt->bind_param('i', $userId);
$stmt->execute();
$result = $stmt->get_result();
// Fetch the result and store it in a variable
$user = $result->fetch_assoc();
// Close the statement
$stmt->close();
// Store user data in the session
$_SESSION['user']->fname = $user['fname'];
$_SESSION['user']->lname = $user['lname'];
$_SESSION['user']->age = $user['age'];
$_SESSION['user']->gender = $user['gender'];
$_SESSION['user']->weight = $user['weight'];
$_SESSION['user']->weight_unit = $user['weight_unit'];
$_SESSION['user']->height = $user['height'];
$_SESSION['user']->height_unit = $user['height_unit'];
$_SESSION['user']->email = $user['email'];
$_SESSION['user']->phone = $user['phone'];
$_SESSION['user']->password = $user['password'];
$_SESSION['user']->additional_note = $user['additional_note'];
// Return the fetched result
return $user;
}
public static function view() {
global $conn;
$userId = isset($_POST['user_id']) ? $_POST['user_id'] : null;
if (isset($_POST['view'])) {
// Prepare and execute the SQL query
$sql = 'SELECT
USER_ID,
FNAME,
LNAME,
EMAIL,
PHONE,
AGE,
GENDER,
WEIGHT,
WEIGHT_UNIT,
HEIGHT,
HEIGHT_UNIT,
ADDITIONAL_NOTE
FROM
USER
WHERE
USER_ID = ? AND
GROUP_ID <> 2'; // Exclude admins
$stmt = $conn->prepare($sql);
$stmt->bind_param('i', $userId);
$stmt->execute();
$result = $stmt->get_result();
// Fetch the result
$data = $result->fetch_assoc();
// Return the fetched data or false if no data is fetched
return $data ? $data : false;
}
// Return false if no data is fetched
return false;
}
public static function logout() {
// Unset all session variables
session_unset();
// Destroy the session
session_destroy();
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Pragma: no-cache");
header("Expires: 0");
// Set additional headers to prevent caching
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header("Location: index.php?controller=home");
exit();
}
public static function update() {
global $conn;
if (isset($_POST['update'])) {
$userId = isset($_SESSION['user']) ? $_SESSION['user']->user_id : null;
// Check if the user ID is set
if ($userId === null) {
return 0;
}
// Retrieve updated user information from the POST data
$fname = isset($_POST['fname']) ? $_POST['fname'] : null;
$lname = isset($_POST['lname']) ? $_POST['lname'] : null;
$email = isset($_POST['email']) ? $_POST['email'] : null;
$password = isset($_POST['password']) ? $_POST['password'] : null;
$phone = isset($_POST['phone']) ? $_POST['phone'] : null;
$age = isset($_POST['age']) ? $_POST['age'] : null;
$gender = isset($_POST['gender']) ? $_POST['gender'] : null;
$weight = isset($_POST['weight']) ? $_POST['weight'] : null;
$weightUnit = isset($_POST['weightUnit']) ? $_POST['weightUnit'] : null;
$height = isset($_POST['height']) ? $_POST['height'] : null;
$heightUnit = isset($_POST['heightUnit']) ? $_POST['heightUnit'] : null;
$additionalNote = isset($_POST['note']) ? $_POST['note'] : null;
// Check if required fields are set
if ($fname === null || $lname === null || $email === null || $password === null) {
return 0;
}
// Hash the password before updating (consider using a proper password hashing function)
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
// Update user information in the database
$sql = 'UPDATE `user` SET fname = ?, lname = ?, email = ?, password = ?, phone = ?, age = ?, gender = ?, weight = ?, weight_unit = ?, height = ?, height_unit = ?, additional_note = ? WHERE user_id = ?';
$stmt = $conn->prepare($sql);
// Check if the prepared statement was successful
if (!$stmt) {
return 0;
}
$stmt->bind_param('ssssssssssssi', $fname, $lname, $email, $hashedPassword, $phone, $age, $gender, $weight, $weightUnit, $height, $heightUnit, $additionalNote, $userId);
$stmt->execute();
if ($stmt->errno) {
$stmt->close();
return 0;
}
$stmt->close();
// Redirect to the user profile page after successful update
header("Location: ?controller=user&action=read");
exit();
}
return false;
}
public static function delete() {
global $conn;
// Check if user ID is present in the session
$userId = isset($_SESSION['user']) ? $_SESSION['user']->user_id : null;
// Check if the 'deleteAccount' key is present in the $_POST array
if (isset($_POST['delete'])) {
// Validate user ID
if (!$userId) {
die('Error: User ID not available. Please log in.');
}
// Prepare and execute the SQL query to delete the user account
$sql = 'DELETE FROM USER WHERE USER_ID = ?';
$stmt = $conn->prepare($sql);
// Check for errors in preparing the statement
if (!$stmt) {
die('Error preparing statement: ' . $conn->error);
}
// Bind parameters and execute the statement
$stmt->bind_param('i', $userId);
$stmt->execute();
// Check for errors in executing the statement
if ($stmt->error) {
// Handle the error (e.g., display an error message or redirect to an error page)
die('Error executing statement: ' . $stmt->error);
}
// Close the statement
$stmt->close();
// Unset all session variables
session_unset();
// Destroy the session
session_destroy();
// Empty cache
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Pragma: no-cache");
header("Expires: 0");
// Set additional headers to prevent caching
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
// Redirect to the home page after successful account deletion
header('Location: ?controller=home');
exit();
}
// Return false if the 'deleteAccount' key is not present in the $_POST array
return false;
}
public static function deleteClient() {
global $conn;
$userId = isset($_POST['user_id']) ? (int)$_POST['user_id'] : null;
// Check if the 'delete' key is present in the $_POST array
if (isset($_POST['deleteClient'])) {
// Validate user ID
if (!$userId) {
die('Error: User ID not available. Please log in.');
}
// Prepare and execute the SQL query to delete the user account
$sql = 'DELETE FROM `user` WHERE `user_id` = ?';
$stmt = $conn->prepare($sql);
// Check for errors in preparing the statement
if (!$stmt) {
die('Error preparing statement: ' . $conn->error);
}
// Bind parameters and execute the statement
$stmt->bind_param('i', $userId);
$stmt->execute();
// Check for errors in executing the statement
if ($stmt->error) {
// Handle the error (e.g., display an error message or redirect to an error page)
die('Error executing statement: ' . $stmt->error);
}
// Redirect to the home page after successful account deletion
header('Location: ?controller=user&action=list');
exit();
}
// Return false if the 'delete' key is not present in the $_POST array
return false;
}
//REVIEW LATER...NEED TO REVIEW/UPDATE DB DEPENDING ON OUR ACTION NAMES
public static function hasRights($classname, $action) {
global $conn;
$sql = "SELECT rights.RIGHTS_ID, rights.ACTION_NAME, rights.CLASS_NAME FROM `user`
INNER JOIN `group` USING (`group_id`)
INNER JOIN group_rights ON (`group`.group_id = group_rights.group_id)
INNER JOIN rights ON (group_rights.rights_id = rights.rights_id)
WHERE rights.ACTION_NAME LIKE '$action' AND rights.CLASS_NAME LIKE '$classname' AND `user`.user_id=$this->user_id;";
echo $sql;
$res = $conn->query($sql);
$r = $res->fetch_assoc();
var_dump($r);
if ($r != NULL) return true;
else return false;
}
// Save a new user
public function saveUser($fname, $lname, $email, $hashed_password, $phone, $userGroup) {
// Prepare an SQL statement to prevent SQL injection
$stmt = $this->conn->prepare("INSERT INTO user (first_name, last_name, email, password, phone, user_group) VALUES (?, ?, ?, ?, ?, ?)");
$stmt->bind_param("ssssss", $fname, $lname, $email, $hashed_password, $phone, $userGroup);
// Execute the statement
$result = $stmt->execute();
if (!$result) {
// Handle error appropriately
die('Execute failed: ' . $stmt->error);
}
// Close the statement
$stmt->close();
return $result;
}
function render($controller, $view, $data = []) {
if($data != null){
extract($data);
include "Views/$controller/$view.php";
}else{
include "Views/$controller/$view.php";
}
}
public function printTables() {
global $conn;
// Validate the connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SHOW TABLES FROM " . $this->database;
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$tableName = $row["Tables_in_" . $this->database];
$tableData = $this->getTableData($tableName);
echo "<h3>Table: $tableName</h3>";
if (empty($tableData)) {
echo "<p>No data available</p>";
} else {
echo "<table border='1'>";
echo "<tr>";
// Display table headers
foreach (array_keys($tableData[0]) as $column) {
echo "<th>$column</th>";
}
echo "</tr>";
// Display table data
foreach ($tableData as $rowData) {
echo "<tr>";
foreach ($rowData as $value) {
echo "<td>$value</td>";
}
echo "</tr>";
}
echo "</table>";
}
echo "<br>";
}
} else {
echo "<p>No tables found in the database</p>";
}
$conn->close();
}
// Move getTableData outside printTables
private function getTableData($tableName) {
global $conn;
// Validate the connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Wrap the table name with backticks to handle reserved keywords
$sql = "SELECT * FROM `" . $tableName . "`";
$result = $conn->query($sql);
$tableData = [];
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$tableData[] = $row;
}
}
return $tableData;
}
}
?>