Guest User

Untitled

a guest
Feb 19th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. class DB_Functions {
  5.  
  6. private $conn;
  7.  
  8. // constructor
  9. function __construct() {
  10. require_once 'DB_Connect.php';
  11. // connecting to database
  12. $db = new Db_Connect();
  13. $this->conn = $db->connect();
  14. }
  15.  
  16. // destructor
  17. function __destruct() {
  18.  
  19. }
  20.  
  21. /**
  22. * Storing new user
  23. * returns user details
  24. */
  25. public function storeUser($name, $email, $password) {
  26. $uuid = uniqid('', true);
  27. $hash = $this->hashSSHA($password);
  28. $encrypted_password = $hash["encrypted"]; // encrypted password
  29. $salt = $hash["salt"]; // salt
  30.  
  31. $stmt = $this->conn->prepare("INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES(?, ?, ?, ?, ?, NOW())");
  32. $stmt->bind_param("sssss", $uuid, $name, $email, $encrypted_password, $salt);
  33. $result = $stmt->execute();
  34. $stmt->close();
  35.  
  36. // check for successful store
  37. if ($result) {
  38. $stmt = $this->conn->prepare("SELECT * FROM users WHERE email = ?");
  39. $stmt->bind_param("s", $email);
  40. $stmt->execute();
  41. $user = $stmt->get_result()->fetch_assoc();
  42. $stmt->close();
  43.  
  44. return $user;
  45. } else {
  46. return false;
  47. }
  48. }
  49.  
  50. /**
  51. * Get user by email and password
  52. */
  53. public function getUserByEmailAndPassword($email, $password) {
  54.  
  55. $stmt = $this->conn->prepare("SELECT * FROM users WHERE email = ?");
  56.  
  57. $stmt->bind_param("s", $email);
  58.  
  59. if ($stmt->execute()) {
  60. $user = $stmt->get_result()->fetch_assoc();
  61. $stmt->close();
  62.  
  63. // verifying user password
  64. $salt = $user['salt'];
  65. $encrypted_password = $user['encrypted_password'];
  66. $hash = $this->checkhashSSHA($salt, $password);
  67. // check for password equality
  68. if ($encrypted_password == $hash) {
  69. // user authentication details are correct
  70. return $user;
  71. }
  72. } else {
  73. return NULL;
  74. }
  75. }
  76.  
  77. /**
  78. * Check user is existed or not
  79. */
  80. public function isUserExisted($email) {
  81. $stmt = $this->conn->prepare("SELECT email from users WHERE email = ?");
  82.  
  83. $stmt->bind_param("s", $email);
  84.  
  85. $stmt->execute();
  86.  
  87. $stmt->store_result();
  88.  
  89. if ($stmt->num_rows > 0) {
  90. // user existed
  91. $stmt->close();
  92. return true;
  93. } else {
  94. // user not existed
  95. $stmt->close();
  96. return false;
  97. }
  98. }
  99.  
  100. /**
  101. * Encrypting password
  102. * @param password
  103. * returns salt and encrypted password
  104. */
  105. public function hashSSHA($password) {
  106.  
  107. $salt = sha1(rand());
  108. $salt = substr($salt, 0, 10);
  109. $encrypted = base64_encode(sha1($password . $salt, true) . $salt);
  110. $hash = array("salt" => $salt, "encrypted" => $encrypted);
  111. return $hash;
  112. }
  113.  
  114. /**
  115. * Decrypting password
  116. * @param salt, password
  117. * returns hash string
  118. */
  119. public function checkhashSSHA($salt, $password) {
  120.  
  121. $hash = base64_encode(sha1($password . $salt, true) . $salt);
  122.  
  123. return $hash;
  124. }
  125.  
  126. }
  127.  
  128. ?>
Add Comment
Please, Sign In to add comment