Guest User

Untitled

a guest
Aug 25th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. namespace Auth;
  2. class User
  3. {
  4. private $id;
  5. private $username;
  6. private $db;
  7. private $user_id;
  8. private $db_host = "ip:port";
  9. private $db_name = "name";
  10. private $db_user = "user";
  11. private $db_pass = "password";
  12.  
  13. private $is_authorized = false;
  14.  
  15. public function __construct($username = null, $password = null)
  16. {
  17. $this->username = $username;
  18. $this->connectDb($this->db_name, $this->db_user, $this->db_pass, $this->db_host);
  19. }
  20.  
  21. public function __destruct()
  22. {
  23. $this->db = null;
  24. }
  25.  
  26. public static function isAuthorized()
  27. {
  28. if (!empty($_SESSION["user_id"])) {
  29. return (bool) $_SESSION["user_id"];
  30. }
  31. return false;
  32. }
  33.  
  34. public function passwordHash($password, $salt = null, $iterations = 10)
  35. {
  36. $salt || $salt = uniqid();
  37. $hash = md5(md5($password . md5(sha1($salt))));
  38.  
  39. for ($i = 0; $i < $iterations; ++$i) {
  40. $hash = md5(md5(sha1($hash)));
  41. }
  42.  
  43. return array('hash' => $hash, 'salt' => $salt);
  44. }
  45.  
  46. public function getSalt($username) {
  47. $query = "select salt from users where username = :username limit 1";
  48. $sth = $this->db->prepare($query);
  49. $sth->execute(
  50. array(
  51. ":username" => $username
  52. )
  53. );
  54. $row = $sth->fetch();
  55. if (!$row) {
  56. return false;
  57. }
  58. return $row["salt"];
  59. }
  60.  
  61. public function authorize($username, $password, $remember=false)
  62. {
  63. $query = "select id, username from users where
  64. username = :username and password = :password limit 1";
  65. $sth = $this->db->prepare($query);
  66. $salt = $this->getSalt($username);
  67.  
  68. if (!$salt) {
  69. return false;
  70. }
  71.  
  72. $hashes = $this->passwordHash($password, $salt);
  73. $sth->execute(
  74. array(
  75. ":username" => $username,
  76. ":password" => $hashes['hash'],
  77. )
  78. );
  79. $this->user = $sth->fetch();
  80.  
  81. if (!$this->user) {
  82. $this->is_authorized = false;
  83. } else {
  84. $this->is_authorized = true;
  85. $this->user_id = $this->user['id'];
  86. $this->saveSession($remember);
  87. }
  88.  
  89. return $this->is_authorized;
  90. }
  91.  
  92. public function logout()
  93. {
  94. if (!empty($_SESSION["user_id"])) {
  95. unset($_SESSION["user_id"]);
  96. }
  97. }
  98.  
  99. public function saveSession($remember = false, $http_only = true, $days = 7)
  100. {
  101. $_SESSION["user_id"] = $this->user_id;
  102.  
  103. if ($remember) {
  104. // Save session id in cookies
  105. $sid = session_id();
  106.  
  107. $expire = time() + $days * 24 * 3600;
  108. $domain = ""; // default domain
  109. $secure = false;
  110. $path = "/";
  111.  
  112. $cookie = setcookie("sid", $sid, $expire, $path, $domain, $secure, $http_only);
  113. }
  114. }
  115.  
  116. public function create($username, $password) {
  117. $user_exists = $this->getSalt($username);
  118.  
  119. if ($user_exists) {
  120. throw new Exception("User exists: " . $username, 1);
  121. }
  122.  
  123. $query = "insert into users (username, password, salt)
  124. values (:username, :password, :salt)";
  125. $hashes = $this->passwordHash($password);
  126. $sth = $this->db->prepare($query);
  127.  
  128. try {
  129. $this->db->beginTransaction();
  130. $result = $sth->execute(
  131. array(
  132. ':username' => $username,
  133. ':password' => $hashes['hash'],
  134. ':salt' => $hashes['salt'],
  135. )
  136. );
  137. $this->db->commit();
  138. } catch (PDOException $e) {
  139. $this->db->rollback();
  140. echo "Database error: " . $e->getMessage();
  141. die();
  142. }
  143.  
  144. if (!$result) {
  145. $info = $sth->errorInfo();
  146. printf("Database error %d %s", $info[1], $info[2]);
  147. die();
  148. }
  149.  
  150. return $result;
  151. }
  152.  
  153. public function connectdb($db_name, $db_user, $db_pass, $db_host = "localhost")
  154. {
  155. try {
  156. $this->db = new pdo("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass);
  157. } catch (pdoexception $e) {
  158. echo "database error: " . $e->getmessage();
  159. die();
  160. }
  161. $this->db->query('set names utf8');
  162.  
  163. return $this;
  164. }
  165. }
Add Comment
Please, Sign In to add comment