Advertisement
rama_astadipati

db_function

Sep 1st, 2018
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. <?php
  2.  
  3. class DB_Functions {
  4.  
  5. private $conn;
  6.  
  7. // constructor
  8. function __construct() {
  9. require_once 'DB_Connect.php';
  10. // koneksi ke database
  11. $db = new Db_Connect();
  12. $this->conn = $db->connect();
  13. }
  14.  
  15. // destructor
  16. function __destruct() {
  17.  
  18. }
  19.  
  20. public function simpanUser($nama, $email, $password, $idperkara) {
  21. $uuid = uniqid('', true);
  22. $hash = $this->hashSSHA($password);
  23. $encrypted_password = $hash["encrypted"]; // encrypted password
  24. $salt = $hash["salt"]; // salt
  25.  
  26. $stmt = $this->conn->prepare("INSERT INTO userweb(unique_id, IDperkara, nama, email, encrypted_password, salt) VALUES(?, ?, ?, ?, ?, ?)");
  27. $stmt->bind_param("ssssss", $uuid, $idperkara, $nama, $email, $encrypted_password, $salt);
  28. $result = $stmt->execute();
  29. $stmt->close();
  30.  
  31. // cek jika sudah sukses
  32. if ($result) {
  33. $stmt = $this->conn->prepare("SELECT * FROM userweb WHERE email = ?");
  34. $stmt->bind_param("s", $email);
  35. $stmt->execute();
  36. $user = $stmt->get_result()->fetch_assoc();
  37. $stmt->close();
  38.  
  39. return $user;
  40. } else {
  41. return false;
  42. }
  43. }
  44.  
  45. /**
  46. * Get user berdasarkan email dan password
  47. */
  48. public function getUserByEmailAndPassword($email, $password) {
  49.  
  50. $stmt = $this->conn->prepare("SELECT * FROM userweb WHERE email = ?");
  51.  
  52. $stmt->bind_param("s", $email);
  53.  
  54. if ($stmt->execute()) {
  55. $user = $stmt->get_result()->fetch_assoc();
  56. $stmt->close();
  57.  
  58. // verifikasi password user
  59. $salt = $user['salt'];
  60. $encrypted_password = $user['encrypted_password'];
  61. $hash = $this->checkhashSSHA($salt, $password);
  62. // cek password jika sesuai
  63. if ($encrypted_password == $hash) {
  64. // autentikasi user berhasil
  65. return $user;
  66. }
  67. } else {
  68. return NULL;
  69. }
  70. }
  71.  
  72. /**
  73. * Cek User ada atau tidak
  74. */
  75. public function isUserExisted($email) {
  76. $stmt = $this->conn->prepare("SELECT email from userweb WHERE email = ?");
  77. $stmt->bind_param("s", $email);
  78. $stmt->execute();
  79. $stmt->store_result();
  80. if ($stmt->num_rows > 0) {
  81. // user telah ada
  82. $stmt->close();
  83. return true;
  84. } else {
  85. // user belum ada
  86. $stmt->close();
  87. return false;
  88. }
  89. }
  90.  
  91. public function idkosong($IDPerkara) {
  92. $stmt = $this->conn->prepare("SELECT IDPerkara from dataumumweb WHERE IDPerkara = ?");
  93. $stmt->bind_param("s", $IDPerkara);
  94. $stmt->execute();
  95. $stmt->store_result();
  96. if ($stmt->num_rows == 0) {
  97. //id user ada
  98. $stmt->close();
  99. return false;
  100. } else {
  101. // id user belum ada
  102. $stmt->close();
  103. return false;
  104. }
  105. }
  106.  
  107. /**
  108. * Encrypting password
  109. * @param password
  110. * returns salt and encrypted password
  111. */
  112. public function hashSSHA($password) {
  113.  
  114. $salt = sha1(rand());
  115. $salt = substr($salt, 0, 10);
  116. $encrypted = base64_encode(sha1($password . $salt, true) . $salt);
  117. $hash = array("salt" => $salt, "encrypted" => $encrypted);
  118. return $hash;
  119. }
  120.  
  121. /**
  122. * Decrypting password
  123. * @param salt, password
  124. * returns hash string
  125. */
  126. public function checkhashSSHA($salt, $password) {
  127.  
  128. $hash = base64_encode(sha1($password . $salt, true) . $salt);
  129.  
  130. return $hash;
  131. }
  132.  
  133. }
  134.  
  135. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement