Advertisement
Guest User

Untitled

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