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: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143:
<?php
namespace CJPGDK\PhpHttpAuth\Database;
class MySQL extends DB
{
protected $db;
public function __construct($host, $username, $passwd, $dbname = "", $port = 3306, $socket = null)
{
$this->setConnectionSettings($host, $username, $passwd, $dbname, $port, $socket);
}
public function deleteUser($name)
{
$query = "DELETE FROM {$this->usersTableName}
WHERE {$this->tableColumns['username']} = '{$this->getDb()->escape_string($name)}'";
return $this->getDb()->query($query);
}
public function saveUser($name, $passwd)
{
if ($user = $this->getUser($name)) {
$query = "UPDATE {$this->usersTableName}
SET {$this->tableColumns['password']} = '{$this->getDb()->escape_string($passwd)}'
WHERE {$this->tableColumns['id']} = {$user->id};";
} else {
$query = "INSERT INTO {$this->usersTableName}
({$this->tableColumns['username']}, {$this->tableColumns['password']})
VALUES ('{$this->getDb()->escape_string($name)}', '{$this->getDb()->escape_string($passwd)}')";
}
return $this->getDb()->query($query);
}
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 ($result = $this->getDb()->query($query)) {
while ($obj = $result->fetch_object()) {
$res[] = $obj;
}
$result->close();
}
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()->escape_string($name)}' LIMIT 1";
$res = null;
if ($result = $this->getDb()->query($query)) {
while ($obj = $result->fetch_object()) {
$res = $obj;
}
$result->close();
}
return $res;
}
public function getDb()
{
if (!is_null($this->db) && $this->db->ping()) {
return $this->db;
}
$this->db = new \mysqli(
$this->getConnectionHost(), $this->getConnectionUser(), $this->getConnectionPassword(), $this->getConnectionDatabseName(),
$this->getConnectionPort(), $this->getConnectionSocket()
);
if ($this->db->connect_errno) {
throw new Exception($this->db->connect_error, $this->db->connect_errno);
}
return $this->db;
}
public function setConnectionSettings($host, $username, $passwd, $dbname = "", $port = 3306, $socket = null)
{
$this->setConnectionHost($host, ini_get("mysqli.default_host"));
$this->setConnectionUser($username, ini_get("mysqli.default_user"));
$this->setConnectionPassword($passwd, ini_get("mysqli.default_pw"));
$this->setConnectionDatabseName($dbname, "");
$this->setConnectionPort($port, ini_get("mysqli.default_port"));
$this->setConnectionSocket($socket, ini_get("mysqli.default_socket"));
}
public function __destruct()
{
if (!is_null($this->db) && $this->db->ping()) {
$this->db->close();
}
}
}