user.inc.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. Class User {
  3. private $username = "";
  4. private $userId = NULL;
  5. private $password = "";
  6. private $email = "";
  7. private $loggedIn = false;
  8. public function __construct($username) {
  9. if($username != NULL) {
  10. $this->username = $username;
  11. }
  12. }
  13. public function setLoggedIn($loggedIn) {
  14. $this->loggedIn = $loggedIn;
  15. }
  16. public function getLoggedIn() {
  17. return $this->loggedIn;
  18. }
  19. public function setUserId($userId) {
  20. $this->userId = $userId;
  21. }
  22. public function getUserId() {
  23. return $this->userId;
  24. }
  25. public function login($username, $password) {
  26. global $db;
  27. $this->username = $username;
  28. $this->password = $password;
  29. $user = $db->selectQuery("SELECT * FROM `users` WHERE `username` = '" . $username . "'");
  30. if(sizeof($user) > 1) {
  31. die('Something really went wrong.');
  32. }
  33. if(sizeof($user) < 1) {
  34. die('Something really went wrong.');
  35. }
  36. if($user[0]->password === md5($password)) {
  37. $_SESSION['loggedIn'] = true;
  38. $_SESSION['username'] = $this->username;
  39. $this->loggedIn = true;
  40. header("Location: http://atoffice");
  41. } else {
  42. return false;
  43. }
  44. }
  45. public function loginByUsername($username) {
  46. global $db;
  47. $user = $db->selectQuery("SELECT * FROM `users` WHERE `username` = '" . $username . "'");
  48. if(sizeof($user) > 1) {
  49. die('Something really went wrong.');
  50. }
  51. if(sizeof($user) < 1) {
  52. die('Something really went wrong.');
  53. }
  54. $_SESSION['loggedIn'] = true;
  55. $this->userId = $user[0]->id;
  56. $this->loggedIn = true;
  57. }
  58. public function logout() {
  59. $this->username = "";
  60. $this->password = "";
  61. $this->email = "";
  62. $_SESSION['loggedIn'] = false;
  63. $this->loggedIn = false;
  64. unset($_SESSION['username']);
  65. session_destroy();
  66. header("Location: http://atoffice");
  67. }
  68. public function getClients() {
  69. global $db;
  70. //$clients = $db->query("SELECT * FROM `clients` WHERE `owned_by` = '" . $this->username . "'");
  71. //sizeof($clients);
  72. //return $clients->name;
  73. return $this->username;
  74. }
  75. }
  76. ?>