Advertisement
Guest User

Untitled

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