Advertisement
Guest User

Untitled

a guest
Jan 24th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. <?php
  2. namespace Zoren\SupremeSpoon;
  3. use PDO;
  4.  
  5. class DBHelper
  6. {
  7.  
  8. /**
  9. * @var PDO
  10. */
  11. protected $conn = null;
  12.  
  13. /**
  14. * @var Config
  15. */
  16. protected $config;
  17.  
  18. public function __construct(Config $config) {
  19. $this->config = $config;
  20. }
  21.  
  22. /**
  23. * Returns true if password is found in the database - stored as hash
  24. *
  25. * @return bool
  26. */
  27. public function isValidPassword($username, $userpassword) {
  28. // Get Password from DB
  29. $sqlquery = "select password from users where user = '" . $username . "';";
  30. if($this->conn == null) {
  31. $this->getConnection();
  32. }
  33.  
  34. try {
  35. $stmt = $this->conn->prepare($sqlquery);
  36. $stmt->execute();
  37. $dbpassword = $stmt->fetch();
  38. $hashedpassword = $dbpassword[0];
  39. $this->conn = null;
  40. return password_verify($userpassword, $hashedpassword);
  41. } catch (PDOException $e) {
  42. print "Error!: " . $e->getMessage() . "<br/>";
  43. die();
  44. }
  45. return false; // No password found
  46. } // End isValidLogin function
  47.  
  48. /**
  49. * Returns a password hash
  50. *
  51. * @return string
  52. */
  53. public function getEncryptedPassword($userpassword) {
  54. return password_hash($userpassword, PASSWORD_DEFAULT);
  55. }
  56.  
  57. public function dbAddUser($username, $password) {
  58.  
  59. }
  60.  
  61. /**
  62. * Returns array with result set of query
  63. *
  64. * @return array
  65. */
  66. private function runQuery($query) {
  67. $resp = array();
  68. try {
  69. $conn = getConnection();
  70. $stmt = $this->conn->prepare($query);
  71. $stmt->execute();
  72. while ($row = $stmt->fetch()) {
  73. array_push($resp, $row);
  74. }
  75. $this->conn = null;
  76. return $resp;
  77. } catch (PDOException $e) {
  78. print "Error!: " . $e->getMessage() . "<br/>";
  79. die();
  80. } // End catch
  81. return null;
  82. } // End runQuery function
  83.  
  84. /**
  85. * Retrieves an instance of PDO
  86. *
  87. * @return PDO
  88. */
  89. private function getConnection() {
  90. if (is_null($this->conn)) {
  91. $this->conn = new PDO("mysql:host=".$this->config->host.";dbname=".$this->config->dbname, $this->config->username, $this->config->password);
  92. }
  93. return $this->conn;
  94. }
  95.  
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement