user.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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", strtolower($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. strtolower($email),
  49. md5($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. header("Location: " . $GLOBALS['conf']['baseURL'] . "?view=admin");
  56. }
  57. /**
  58. * Get the value of Id
  59. *
  60. *
  61. * @return mixed
  62. *
  63. */
  64. public function getId() {
  65. return $this->id;
  66. }
  67. /**
  68. * Set the value of Id
  69. *
  70. *
  71. * @param mixed id
  72. *
  73. */
  74. public function setId($id) {
  75. $this->id = $id;
  76. }
  77. /**
  78. * Get the value of Mail
  79. *
  80. *
  81. * @return mixed
  82. *
  83. */
  84. public function getMail() {
  85. return $this->mail;
  86. }
  87. /**
  88. * Set the value of Mail
  89. *
  90. *
  91. * @param mixed mail
  92. *
  93. */
  94. public function setMail($mail) {
  95. $this->mail = $mail;
  96. }
  97. /**
  98. * Get the value of Admin
  99. *
  100. *
  101. * @return mixed
  102. *
  103. */
  104. public function getAdmin() {
  105. return $this->admin;
  106. }
  107. /**
  108. * Set the value of Admin
  109. *
  110. *
  111. * @param mixed admin
  112. *
  113. */
  114. public function setAdmin($admin) {
  115. $this->admin = $admin;
  116. }
  117. }