You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Authentication.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace SCF\Core;
  3. class Authentication
  4. {
  5. /**
  6. * @var Database
  7. */
  8. private $_db;
  9. /**
  10. * Login constructor.
  11. * @param Database $db
  12. */
  13. public function __construct(Database $db)
  14. {
  15. $this->_db = $db;
  16. }
  17. /**
  18. * @return string
  19. * @throws \Exception
  20. */
  21. private function createToken()
  22. {
  23. if (function_exists('random_bytes')) {
  24. $bytes = random_bytes(16);
  25. $str = bin2hex($bytes);
  26. } else if (function_exists('openssl_random_pseudo_bytes')) {
  27. $bytes = openssl_random_pseudo_bytes(16);
  28. $str = bin2hex($bytes);
  29. } else {
  30. $str = hash('sha256', uniqid(BLOWFISH, true));
  31. }
  32. return $str;
  33. }
  34. /**
  35. * @param string $username
  36. * @param string $password
  37. * @param $rememberMe
  38. * @return bool
  39. * @throws \Exception
  40. */
  41. public function loginAction($username = '', $password = '', $rememberMe)
  42. {
  43. $this->_db->query("SELECT `userId`, `username`, `password`, `email` FROM users WHERE username = :username OR email = :username LIMIT 0,1");
  44. $this->_db->bind(':username', $username);
  45. $temp = $this->_db->single();
  46. if ($temp !== false && hash('sha256',$password) == $temp['password']) {
  47. $_SESSION['scf_userId'] = $temp['userId'];
  48. if (isset($rememberMe)) {
  49. $identifier = $this->createToken();
  50. $securityToken = $this->createToken();
  51. $time = date("Y-m-d H:i:s", time());
  52. $this->_db->query("INSERT INTO users_tokens (userId, identifier, securitytoken, createDate) VALUES (:userId, :identifier, :securitytoken, :created)");
  53. $this->_db->bind(':userId', $temp['userId']);
  54. $this->_db->bind(':identifier', $identifier);
  55. $this->_db->bind(':securitytoken', sha1($securityToken));
  56. $this->_db->bind(':created', $time);
  57. $this->_db->execute();
  58. setcookie('scf_identifier', $identifier, time() + (3600 * 24 * 30));
  59. setcookie('scf_token', $securityToken, time() + (3600 * 24 * 30));
  60. }
  61. return true;
  62. } else {
  63. session_destroy();
  64. return false;
  65. }
  66. }
  67. /**
  68. * @return bool
  69. */
  70. public function isLogin()
  71. {
  72. $this->checkLogin();
  73. if (isset($_SESSION['scf_userId'])) {
  74. return true;
  75. } else {
  76. return false;
  77. }
  78. }
  79. /**
  80. * Check if LoginToken for user exists
  81. */
  82. public function checkLogin()
  83. {
  84. if (!isset($_SESSION['scf_userId']) && isset($_COOKIE['scf_identifier']) && isset($_COOKIE['scf_token'])) {
  85. $identifier = $_COOKIE['scf_identifier'];
  86. $securityToken = $_COOKIE['scf_token'];
  87. $this->_db->query("SELECT * FROM users_tokens WHERE identifier = :identifier");
  88. $this->_db->bind(':identifier', $identifier);
  89. $this->_db->execute();
  90. $temp = $this->_db->single();
  91. if (sha1($securityToken) !== $temp['securitytoken']) {
  92. } else {
  93. $newToken = $this->createToken();
  94. $this->_db->query("UPDATE users_tokens SET securitytoken = :token WHERE identifier = :identifier");
  95. $this->_db->bind(':token', $newToken);
  96. $this->_db->bind(':identifier', $identifier);
  97. setcookie('scf_identifier', $identifier, time() + (3600 * 24 * 30));
  98. setcookie('scf_token', $newToken, time() + (3600 * 24 * 30));
  99. $_SESSION['scf_userId'] = $temp['userId'];
  100. }
  101. }
  102. }
  103. public function logout()
  104. {
  105. if(isset($_COOKIE['scf_identifier'])) {
  106. $this->_db->query("DELETE FROM users_tokens WHERE userId = {$_SESSION['scf_userId']}");
  107. }
  108. setcookie('scf_identifier', null , -1);
  109. setcookie('scf_token', null , -1);
  110. session_destroy();
  111. return true;
  112. }
  113. /**
  114. * @param string $username
  115. * @param string $password
  116. * @param string $email
  117. * @param string $gender
  118. * @return string
  119. * @throws \Exception
  120. */
  121. public function registerAccount($username = '', $password = '', $email = '', $gender = 'n')
  122. {
  123. $this->_db->query("SELECT username, email FROM users WHERE username = :reguser OR email = :regemail");
  124. $this->_db->bind(':reguser', $username);
  125. $this->_db->bind(':regemail', $email);
  126. $this->_db->execute();
  127. if ($this->_db->rowCount() == 1) {
  128. return 'register_error';
  129. } else {
  130. $this->saveDataInDatabase($username, $password, $email, $gender);
  131. return 'register_success';
  132. }
  133. }
  134. /**
  135. * @param $username
  136. * @param $password
  137. * @param $email
  138. * @param $gender
  139. * @throws \Exception
  140. */
  141. private function saveDataInDatabase($username, $password, $email, $gender)
  142. {
  143. $this->_db->query("INSERT INTO users (username, passwort, avatar, gender, email, accountActive, activationHash)
  144. VALUES (:username, :password, 'm2', :gender, :email, '0', :activation)");
  145. $this->_db->bind(':username', $username);
  146. $this->_db->bind(':password', password_hash($password, PASSWORD_BCRYPT));
  147. $this->_db->bind(':gender', $gender);
  148. $this->_db->bind(':email', $email);
  149. $this->_db->bind(':activation', $this->createToken());
  150. $this->_db->execute();
  151. }
  152. }