spacejewel-ipn-communication/source/dbtools/Application.php

105 lines
2.7 KiB
PHP
Raw Permalink Normal View History

2018-06-07 10:56:13 +00:00
<?php
class Application
{
private $database = [];
2018-06-08 20:12:01 +00:00
private static $db;
2018-06-07 10:56:13 +00:00
public function __construct()
{
$config = new Config();
$cConfigArray = $config->getConfig();
2018-06-07 10:56:13 +00:00
$this->database['username'] = $cConfigArray['database']['username'];
$this->database['password'] = $cConfigArray['database']['password'];
$this->database['hostname'] = $cConfigArray['database']['hostname'];
$this->database['dbname'] = $cConfigArray['database']['dbname'];
2018-06-08 17:58:18 +00:00
$dsn = 'mysql:dbname=' . $this->database['dbname'] . ';host=' . $this->database['hostname'];
2018-06-08 17:58:18 +00:00
$username = $this->database['username'];
$password = $this->database['password'];
2018-06-07 10:56:13 +00:00
2018-06-08 20:12:01 +00:00
$instance = \ParagonIE\EasyDB\Factory::create($dsn, $username, $password);
self::$db = $instance;
2018-06-08 17:58:18 +00:00
2018-06-07 10:56:13 +00:00
}
2018-06-07 10:56:13 +00:00
public function Exists($Table, $IDRowColumnName, $SearchValue)
{
2018-06-08 20:12:01 +00:00
2018-06-07 10:56:13 +00:00
$exists = $db->cell(
"SELECT count(ID) FROM $Table WHERE $IDRowColumnName = ?",
$SearhValue
);
// Query might not return what we're looking for, an integer. Use vardump if otherwise.
return ($exists == 1) ? true : false;
}
public function adminExists($AdminID)
{
$this->Exists("Administrators", "ID", $ID);
}
public function addAdministrator($Username, $Name, $Email, $Password, $KeyID)
{
$this->db->insert('Administrators', [
'AdministratorName' => $Name,
'AdministratorUsername' => $Username,
'AdministratorEmail' => $Email,
'AdministratorPassword' => $Password
]);
}
public function listAdminsByName($AdminUsername)
{
$Admin = $this->db->row(
"SELECT * FROM Administrators WHERE Username = ?",
$Username
);
}
public function addKey($AdminID, $Keyname)
{
$key = password_hash(openssl_random_pseudo_bytes(32), PASSWORD_BCYPT);
$this->db->insert('APIKeys', [
'AdminID' => $AdminID,
'Keyname' => $Keyname,
'Keytext' => $key
]);
return $key;
}
public function ApiKeyExists($AdminID)
{
$this->Exists("APIKeys", "AdminID", $AdminID);
}
public function ApiKeyToAdminId($Key)
{
$apiKey = $this->db->row(
"SELECT * FROM APIKeys WHERE Keytext = ?",
$Key
);
// Expecting an array. Var dump if else
if ($apiKey == null && !is_array($apiKey))
{
throw new LogicException("Illegal data from DB: ApiKeyToAdminId");
}
}
}