2018-06-07 10:56:13 +00:00
|
|
|
<?php
|
|
|
|
|
2018-06-08 15:38:27 +00:00
|
|
|
use \ParagonIE\EasyDB\Exception;
|
|
|
|
|
2018-06-07 10:56:13 +00:00
|
|
|
class Application
|
|
|
|
{
|
|
|
|
private $database = [];
|
|
|
|
|
|
|
|
private $db;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$config = new Config();
|
2018-06-08 15:16:16 +00:00
|
|
|
$cConfigArray = $config->getConfig();
|
2018-06-07 10:56:13 +00:00
|
|
|
|
2018-06-08 15:38:27 +00:00
|
|
|
|
|
|
|
|
2018-06-08 15:16:16 +00:00
|
|
|
$this->database['username'] = $cConfigArray['core']['database']['username'];
|
|
|
|
$this->database['password'] = $cConfigArray['core']['database']['password'];
|
|
|
|
$this->database['hostname'] = $cConfigArray['core']['database']['hostname'];
|
|
|
|
$this->database['dbname'] = $cConfigArray['core']['database']['dbname'];
|
2018-06-07 10:56:13 +00:00
|
|
|
|
2018-06-08 15:20:29 +00:00
|
|
|
$this->db = $this->instDB();
|
2018-06-07 10:56:13 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function instDB()
|
2018-06-08 15:38:27 +00:00
|
|
|
{// ConstructorFailed
|
|
|
|
|
2018-06-07 10:56:13 +00:00
|
|
|
|
2018-06-08 15:38:27 +00:00
|
|
|
try
|
|
|
|
{
|
2018-06-08 16:44:33 +00:00
|
|
|
// FIXME: Find hidden bug ($Conn is null)
|
|
|
|
$Conn = \ParagonIE\EasyDB\Factory::create(
|
2018-06-07 10:56:13 +00:00
|
|
|
'mysql:host=' . $this->database['hostname'] . ';dbname=' . $this->database['dbname'],
|
|
|
|
$this->database['username'],
|
|
|
|
$this->database['password']
|
2018-06-08 15:38:27 +00:00
|
|
|
);
|
2018-06-08 16:44:33 +00:00
|
|
|
|
|
|
|
if (is_null($Conn))
|
|
|
|
{
|
|
|
|
throw new Exception("ERROR! There is no usable connection object.");
|
|
|
|
}
|
2018-06-08 15:38:27 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
catch (ConstructorFailed $Ex)
|
|
|
|
{
|
2018-06-08 16:44:33 +00:00
|
|
|
Analog::log("ALARM! Database connection failed! Message from Wrapper: " . $Ex->getMessage(), Analog::ALERT);
|
|
|
|
|
|
|
|
}
|
|
|
|
catch (Exception $ConEx)
|
|
|
|
{
|
|
|
|
Analog::log("ALERT! Unstable DB connection", Analog::ALERT);
|
2018-06-08 15:38:27 +00:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
2018-06-08 16:44:33 +00:00
|
|
|
|
|
|
|
Analog::log("Returning DB connection\n" . var_dump($Conn), Analog::DEBUG);
|
|
|
|
return $Conn;
|
2018-06-08 15:38:27 +00:00
|
|
|
}
|
2018-06-07 10:56:13 +00:00
|
|
|
}
|
2018-06-08 15:38:27 +00:00
|
|
|
|
2018-06-07 10:56:13 +00:00
|
|
|
public function Exists($Table, $IDRowColumnName, $SearchValue)
|
|
|
|
{
|
|
|
|
$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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|