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;
class PDODB extends DB
{
protected $dbh;
public function __construct($dsn, $username="", $password="")
{
$this->dbh = new \PDO($dsn, $username, $password);
}
public function deleteUser($name)
{
$query = "DELETE FROM {$this->usersTableName}
WHERE {$this->tableColumns['username']} = {$this->getDb()->quote($name)}";
return $this->getDb()->exec($query);
}
public function getDb()
{
return $this->dbh;
}
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;
}
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;
}
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);
}
}