user.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. class User {
  3. private $id = null;
  4. private $mail = null;
  5. private $admin = null;
  6. public function __construct($mail) {
  7. $user = $GLOBALS['db']->getAllAssoc("users", "mail", $mail);
  8. $this->id = $user[0]['id'];
  9. $this->mail = $user[0]['mail'];
  10. $this->admin = $user[0]['admin'];
  11. }
  12. public static function login($request) {
  13. $hashedPass = $GLOBALS['db']->getString("pass", "users", "mail", $request['mail']);
  14. if($hashedPass == md5($request['pass'])) {
  15. $_SESSION['loggedIn'] = true;
  16. $_SESSION['mail'] = $request['mail'];
  17. header("Location: " . $GLOBALS['conf']['baseURL']);
  18. } else {
  19. echo "PW mismatch, try again.";
  20. exit(1);
  21. }
  22. }
  23. public static function logout() {
  24. session_destroy();
  25. header("Location: " . $GLOBALS['conf']['baseURL']);
  26. }
  27. public static function update($newPassword, $newPasswordConfirmation, $newEmail, $oldEmail, $logout = true) {
  28. if($newPassword && $newPasswordConfirmation) {
  29. if($newPassword == $newPasswordConfirmation) {
  30. $GLOBALS['db']->updateRow("users", "pass", "MD5('" . $newPassword . "')", "id", Model::getUserIDByMail($oldEmail)[0]['id']);
  31. } else {
  32. return "Passwords don't match.";
  33. }
  34. }
  35. $GLOBALS['db']->updateRow("users", "mail", "'" . $newEmail . "'", "id", Model::getUserIDByMail($oldEmail)[0]['id']);
  36. if($logout) {
  37. User::logout();
  38. }
  39. }
  40. public static function invite($email) {
  41. $password = generatePassword();
  42. $invite = generatePassword(16);
  43. $cols = array(
  44. "mail",
  45. "pass"
  46. );
  47. $vals = array(
  48. $email,
  49. $password,
  50. );
  51. $GLOBALS['db']->insertRow("users", $cols, $vals);
  52. self::update($password, $password, $email, $email, false);
  53. $msg = 'Was geht,' . PHP_EOL . "Hier deine Accountdaten:" . PHP_EOL . "Email: Diese Email-Adresse" . PHP_EOL . "Passwort: " . $password . PHP_EOL . "PW bitte ändern!";
  54. mail($email, "Moeflix invite", $msg, 'From: moritz+moeflix@mmnx.de');
  55. }
  56. /**
  57. * Get the value of Id
  58. *
  59. *
  60. * @return mixed
  61. *
  62. */
  63. public function getId() {
  64. return $this->id;
  65. }
  66. /**
  67. * Set the value of Id
  68. *
  69. *
  70. * @param mixed id
  71. *
  72. */
  73. public function setId($id) {
  74. $this->id = $id;
  75. }
  76. /**
  77. * Get the value of Mail
  78. *
  79. *
  80. * @return mixed
  81. *
  82. */
  83. public function getMail() {
  84. return $this->mail;
  85. }
  86. /**
  87. * Set the value of Mail
  88. *
  89. *
  90. * @param mixed mail
  91. *
  92. */
  93. public function setMail($mail) {
  94. $this->mail = $mail;
  95. }
  96. /**
  97. * Get the value of Admin
  98. *
  99. *
  100. * @return mixed
  101. *
  102. */
  103. public function getAdmin() {
  104. return $this->admin;
  105. }
  106. /**
  107. * Set the value of Admin
  108. *
  109. *
  110. * @param mixed admin
  111. *
  112. */
  113. public function setAdmin($admin) {
  114. $this->admin = $admin;
  115. }
  116. }