Overview

Namespaces

  • CJPGDK
    • PhpHttpAuth
      • Database
      • Hash
  • PHP

Classes

  • CJPGDK\PhpHttpAuth\Database\DB
  • CJPGDK\PhpHttpAuth\Database\MySQL
  • CJPGDK\PhpHttpAuth\Database\PDODB
  • CJPGDK\PhpHttpAuth\HttpAuth
  • mysqli
  • PDO

Interfaces

  • Throwable

Traits

  • CJPGDK\PhpHttpAuth\Hash\Apr1Md5
  • CJPGDK\PhpHttpAuth\Hash\Bcrypt
  • CJPGDK\PhpHttpAuth\Hash\Md5
  • CJPGDK\PhpHttpAuth\Hash\Sha

Exceptions

  • Exception
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 
<?php

namespace CJPGDK\PhpHttpAuth\Database;

/**
 * Provides methods for PDO integration
 *
 * This file is only added to show how you may added your own.
 * PDO provides many db backends but this file only supports sqlite and mysql
 *
 * @package CJPGDK
 * @subpackage PhpHttpAuth
 * @author Christian M. Jensen <cmj@cjpg.dk>
 * @version 1.0.0
 * @since 1.0.0
 */
class PDODB extends DB
{
    /** @var \PDO */
    protected $dbh;

    public function __construct($dsn, $username="", $password="")
    {
        $this->dbh = new \PDO($dsn, $username, $password);
    }

    /**
     * Delete a user from database
     * @param string $name
     * @return boolean
     */
    public function deleteUser($name)
    {
        $query = "DELETE FROM {$this->usersTableName}
                  WHERE {$this->tableColumns['username']} = {$this->getDb()->quote($name)}";
        return $this->getDb()->exec($query);
    }

    /**
     * Get the current PDO object
     * @return \PDO
     */
    public function getDb()
    {
        return $this->dbh;
    }

    /**
     * Get user from database
     * @return object[]
     */
    public function getUsers()
    {
        $query = "SELECT {$this->tableColumns['id']} as id,
                {$this->tableColumns['username']} as name,
                {$this->tableColumns['password']} as password
                FROM {$this->usersTableName}";
        $res   = array();
        if ($rows  = $this->getDb()->query($query)) {
            foreach ($rows as $row) {
                $res[] = (object) array(
                        'id'       => $row['id'],
                        'name'     => $row['name'],
                        'password' => $row['password'],
                );
            }
        }
        return $res;
    }

    /**
     * Get user from database
     * @param string $name
     * @return object
     */
    public function getUser($name)
    {
        $query = "SELECT {$this->tableColumns['id']} as id,
                {$this->tableColumns['username']} as name,
                {$this->tableColumns['password']} as password
                FROM {$this->usersTableName} WHERE
                {$this->tableColumns['username']}={$this->getDb()->quote($name)} LIMIT 1";
        $res   = null;
        if ($rows  = $this->getDb()->query($query)) {
            foreach ($rows as $row) {
                $res           = new \stdClass();
                $res->id       = $row['id'];
                $res->name     = $row['name'];
                $res->password = $row['password'];
            }
        }
        return $res;
    }
    
    /**
     * Save or update a user in the database
     * @param string $name
     * @param string $passwd
     * @return boolean
     */
    public function saveUser($name, $passwd)
    {
        if ($user = $this->getUser($name)) {
            $query = "UPDATE {$this->usersTableName}
                SET {$this->tableColumns['password']} = {$this->getDb()->quote($passwd)}
                WHERE {$this->tableColumns['id']} = {$user->id};";
        } else {
            $query = "INSERT INTO {$this->usersTableName}
                ({$this->tableColumns['username']}, {$this->tableColumns['password']})
                VALUES ({$this->getDb()->quote($name)}, {$this->getDb()->quote($passwd)})";
        }
        return $this->getDb()->exec($query);
    }
}
PhpHttpAuth API documentation generated by ApiGen