Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Zoren\SupremeSpoon;
- use PDO;
- class DBHelper
- {
- /**
- * @var PDO
- */
- protected $conn = null;
- /**
- * @var Config
- */
- protected $config;
- public function __construct(Config $config) {
- $this->config = $config;
- }
- /**
- * Returns true if password is found in the database - stored as hash
- *
- * @return bool
- */
- public function isValidPassword($username, $userpassword) {
- // Get Password from DB
- $sqlquery = "select password from users where user = '" . $username . "';";
- if($this->conn == null) {
- $this->getConnection();
- }
- try {
- $stmt = $this->conn->prepare($sqlquery);
- $stmt->execute();
- $dbpassword = $stmt->fetch();
- $hashedpassword = $dbpassword[0];
- $this->conn = null;
- return password_verify($userpassword, $hashedpassword);
- } catch (PDOException $e) {
- print "Error!: " . $e->getMessage() . "<br/>";
- die();
- }
- return false; // No password found
- } // End isValidLogin function
- /**
- * Returns a password hash
- *
- * @return string
- */
- public function getEncryptedPassword($userpassword) {
- return password_hash($userpassword, PASSWORD_DEFAULT);
- }
- public function dbAddUser($username, $password) {
- }
- /**
- * Returns array with result set of query
- *
- * @return array
- */
- private function runQuery($query) {
- $resp = array();
- try {
- $conn = getConnection();
- $stmt = $this->conn->prepare($query);
- $stmt->execute();
- while ($row = $stmt->fetch()) {
- array_push($resp, $row);
- }
- $this->conn = null;
- return $resp;
- } catch (PDOException $e) {
- print "Error!: " . $e->getMessage() . "<br/>";
- die();
- } // End catch
- return null;
- } // End runQuery function
- /**
- * Retrieves an instance of PDO
- *
- * @return PDO
- */
- private function getConnection() {
- if (is_null($this->conn)) {
- $this->conn = new PDO("mysql:host=".$this->config->host.";dbname=".$this->config->dbname, $this->config->username, $this->config->password);
- }
- return $this->conn;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement