added options for testing

This commit is contained in:
tracer 2022-10-08 10:55:25 +02:00
parent 91ea53275e
commit 2965e9b7ce
1 changed files with 20 additions and 19 deletions

View File

@ -2,7 +2,7 @@
namespace App\Controller; namespace App\Controller;
error_reporting(error_level: E_ALL); //error_reporting(error_level: E_ALL);
use PDO; use PDO;
@ -15,31 +15,32 @@ class DatabaseConnection
{ {
private PDO $dbConnection; private PDO $dbConnection;
const TABLE_PREFIX = ''; const TABLE_PREFIX = '';
const TABLE_DOMAINS = self::TABLE_PREFIX . "domains"; const TABLE_DOMAINS = self::TABLE_PREFIX . "domains";
const TABLE_NAMESERVERS = self::TABLE_PREFIX . "nameservers"; const TABLE_NAMESERVERS = self::TABLE_PREFIX . "nameservers";
const TABLE_PANELS = self::TABLE_PREFIX . "panels"; const TABLE_PANELS = self::TABLE_PREFIX . "panels";
const TABLE_APIKEYS = self::TABLE_PREFIX . "apikeys"; const TABLE_APIKEYS = self::TABLE_PREFIX . "apikeys";
const TABLE_DYNDNS = self::TABLE_PREFIX . "dyndns"; const TABLE_DYNDNS = self::TABLE_PREFIX . "dyndns";
public function __construct(private readonly ConfigController $configController) public function __construct(private readonly ConfigController $configController)
{ {
$dbHost = $this->configController->getConfig(configKey: 'dbHost'); $dbHost = $this->configController->getConfig(configKey: 'dbHost');
$dbPort = $this->configController->getConfig(configKey: 'dbPort'); $dbPort = $this->configController->getConfig(configKey: 'dbPort');
$dbDatabase = $this->configController->getConfig(configKey: 'dbDatabase'); $dbDatabase = $this->configController->getConfig(configKey: 'dbDatabase');
$dbUser = $this->configController->getConfig(configKey: 'dbUser'); $dbUser = $this->configController->getConfig(configKey: 'dbUser');
$dbPassword = $this->configController->getConfig(configKey: 'dbPassword'); $dbPassword = $this->configController->getConfig(configKey: 'dbPassword');
$this->dbConnection = new PDO(
dsn: "mysql:host=$dbHost;port=$dbPort;charset=utf8mb4;dbname=$dbDatabase",
username: $dbUser,
password: $dbPassword
);
if (!$this->configController->getConfig(configKey: 'test')) { if (!$this->configController->getConfig(configKey: 'test')) {
// TODO create config => encryption key // TODO create config => encryption key
try { try {
$this->dbConnection = new PDO(
dsn: "mysql:host=$dbHost;port=$dbPort;charset=utf8mb4;dbname=$dbDatabase",
username: $dbUser,
password: $dbPassword
);
$sql = "SHOW TABLES"; $sql = "SHOW TABLES";
$statement = $this->dbConnection->prepare(query: $sql); $statement = $this->dbConnection->prepare(query: $sql);
$statement->execute(); $statement->execute();
$result = $statement->fetch(); $result = $statement->fetch();
@ -47,7 +48,7 @@ class DatabaseConnection
// ALTER TABLE `domains` ADD `panel_id` INT NULL AFTER `id`; // ALTER TABLE `domains` ADD `panel_id` INT NULL AFTER `id`;
echo 'Error: Cannot find tables.' . PHP_EOL; echo 'Error: Cannot find tables.' . PHP_EOL;
if (confirm(message: 'Should I try to create them?')) { if (confirm(message: 'Should I try to create them?')) {
$sql = " $sql = "
CREATE TABLE `apikeys` ( CREATE TABLE `apikeys` (
`id` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
@ -58,7 +59,7 @@ class DatabaseConnection
$statement = $this->dbConnection->prepare(query: $sql); $statement = $this->dbConnection->prepare(query: $sql);
$statement->execute(); $statement->execute();
$sql = " $sql = "
CREATE TABLE `domains` ( CREATE TABLE `domains` (
`id` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
@ -68,7 +69,7 @@ class DatabaseConnection
$statement = $this->dbConnection->prepare(query: $sql); $statement = $this->dbConnection->prepare(query: $sql);
$statement->execute(); $statement->execute();
$sql = " $sql = "
CREATE TABLE `nameservers` ( CREATE TABLE `nameservers` (
`id` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
@ -80,7 +81,7 @@ class DatabaseConnection
$statement = $this->dbConnection->prepare(query: $sql); $statement = $this->dbConnection->prepare(query: $sql);
$statement->execute(); $statement->execute();
$sql = " $sql = "
CREATE TABLE `panels` ( CREATE TABLE `panels` (
`id` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
@ -133,7 +134,7 @@ class DatabaseConnection
*/ */
function generatePassword(int $length = 8): string function generatePassword(int $length = 8): string
{ {
$chars = '23456789bcdfhkmnprstvzBCDFHJKLMNPRSTVZ'; $chars = '23456789bcdfhkmnprstvzBCDFHJKLMNPRSTVZ';
$shuffled = str_shuffle(string: $chars); $shuffled = str_shuffle(string: $chars);
return mb_substr(string: $shuffled, start: 0, length: $length); return mb_substr(string: $shuffled, start: 0, length: $length);
} }