2022-02-01 20:40:29 +01:00
|
|
|
<?php
|
2022-09-17 16:26:14 +02:00
|
|
|
|
2022-01-18 19:14:24 +01:00
|
|
|
namespace App\Controller;
|
|
|
|
|
2022-01-26 19:35:57 +01:00
|
|
|
error_reporting(error_level: E_ALL);
|
|
|
|
|
|
|
|
|
2022-01-18 19:14:24 +01:00
|
|
|
use PDO;
|
|
|
|
use PDOException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class DatabaseConnection
|
|
|
|
{
|
|
|
|
private PDO $dbConnection;
|
|
|
|
|
2022-01-23 16:31:18 +01:00
|
|
|
const TABLE_PREFIX = '';
|
2022-01-22 18:45:34 +01:00
|
|
|
const TABLE_DOMAINS = self::TABLE_PREFIX . "domains";
|
|
|
|
const TABLE_NAMESERVERS = self::TABLE_PREFIX . "nameservers";
|
|
|
|
const TABLE_PANELS = self::TABLE_PREFIX . "panels";
|
2022-01-31 20:58:12 +01:00
|
|
|
const TABLE_APIKEYS = self::TABLE_PREFIX . "apikeys";
|
2022-04-06 16:26:35 +02:00
|
|
|
const TABLE_DYNDNS = self::TABLE_PREFIX . "dyndns";
|
2022-01-22 18:45:34 +01:00
|
|
|
|
2022-09-17 16:26:14 +02:00
|
|
|
public function __construct(private readonly ConfigController $configController)
|
2022-01-18 19:14:24 +01:00
|
|
|
{
|
2022-09-17 16:26:14 +02:00
|
|
|
$dbHost = $this->configController->getConfig(configKey: 'dbHost');
|
|
|
|
$dbPort = $this->configController->getConfig(configKey: 'dbPort');
|
|
|
|
$dbDatabase = $this->configController->getConfig(configKey: 'dbDatabase');
|
|
|
|
$dbUser = $this->configController->getConfig(configKey: 'dbUser');
|
|
|
|
$dbPassword = $this->configController->getConfig(configKey: 'dbPassword');
|
|
|
|
|
2022-07-20 19:09:40 +02:00
|
|
|
// TODO create config => encryption key
|
2022-01-18 19:14:24 +01:00
|
|
|
try {
|
|
|
|
$this->dbConnection = new PDO(
|
2022-09-17 16:26:14 +02:00
|
|
|
dsn : "mysql:host=$dbHost;port=$dbPort;charset=utf8mb4;dbname=$dbDatabase",
|
2022-01-18 19:14:24 +01:00
|
|
|
username: $dbUser,
|
|
|
|
password: $dbPassword
|
|
|
|
);
|
2022-01-23 16:31:18 +01:00
|
|
|
$sql = "SHOW TABLES";
|
2022-01-26 19:35:57 +01:00
|
|
|
$statement = $this->dbConnection->prepare(query: $sql);
|
2022-01-23 19:19:05 +01:00
|
|
|
$statement->execute();
|
|
|
|
$result = $statement->fetch();
|
|
|
|
if (empty($result)) {
|
2022-01-25 20:33:13 +01:00
|
|
|
// ALTER TABLE `domains` ADD `panel_id` INT NULL AFTER `id`;
|
2022-01-23 16:31:18 +01:00
|
|
|
echo 'Error: Cannot find tables.' . PHP_EOL;
|
2022-01-26 19:35:57 +01:00
|
|
|
if (confirm(message: 'Should I try to create them?')) {
|
2022-01-23 16:31:18 +01:00
|
|
|
$sql = "
|
|
|
|
CREATE TABLE `apikeys` (
|
|
|
|
`id` int(11) NOT NULL AUTO_INCREMENT,
|
|
|
|
`name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
|
|
|
|
`api_token_prefix` varchar(13) COLLATE utf8mb4_unicode_ci NOT NULL,
|
|
|
|
`api_token` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
|
|
|
|
PRIMARY KEY (`id`)
|
2022-01-23 19:20:34 +01:00
|
|
|
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
|
2022-01-26 19:35:57 +01:00
|
|
|
$statement = $this->dbConnection->prepare(query: $sql);
|
2022-01-23 16:31:18 +01:00
|
|
|
$statement->execute();
|
|
|
|
|
|
|
|
$sql = "
|
|
|
|
CREATE TABLE `domains` (
|
|
|
|
`id` int(11) NOT NULL AUTO_INCREMENT,
|
|
|
|
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
|
2022-02-22 13:59:33 +01:00
|
|
|
`panel` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
|
2022-01-23 16:31:18 +01:00
|
|
|
PRIMARY KEY (`id`)
|
2022-01-23 19:20:34 +01:00
|
|
|
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
|
2022-01-26 19:35:57 +01:00
|
|
|
$statement = $this->dbConnection->prepare(query: $sql);
|
2022-01-23 16:31:18 +01:00
|
|
|
$statement->execute();
|
|
|
|
|
|
|
|
$sql = "
|
|
|
|
CREATE TABLE `nameservers` (
|
|
|
|
`id` int(11) NOT NULL AUTO_INCREMENT,
|
|
|
|
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
|
|
|
|
`a` varbinary(255) DEFAULT NULL,
|
|
|
|
`aaaa` varbinary(255) DEFAULT NULL,
|
|
|
|
`apikey` varbinary(255) DEFAULT NULL,
|
|
|
|
PRIMARY KEY (`id`)
|
2022-01-23 19:20:34 +01:00
|
|
|
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
|
2022-01-26 19:35:57 +01:00
|
|
|
$statement = $this->dbConnection->prepare(query: $sql);
|
2022-01-23 16:31:18 +01:00
|
|
|
$statement->execute();
|
|
|
|
|
|
|
|
$sql = "
|
|
|
|
CREATE TABLE `panels` (
|
|
|
|
`id` int(11) NOT NULL AUTO_INCREMENT,
|
|
|
|
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
|
|
|
|
`a` varbinary(255) DEFAULT NULL,
|
|
|
|
`aaaa` varbinary(255) DEFAULT NULL,
|
|
|
|
`apikey` varbinary(255) DEFAULT NULL,
|
|
|
|
PRIMARY KEY (`id`)
|
2022-02-22 13:59:33 +01:00
|
|
|
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
|
2022-01-26 19:35:57 +01:00
|
|
|
$statement = $this->dbConnection->prepare(query: $sql);
|
2022-01-23 16:31:18 +01:00
|
|
|
$statement->execute();
|
|
|
|
|
2022-04-06 16:26:35 +02:00
|
|
|
$sql = "
|
|
|
|
CREATE TABLE `dyndns` (
|
|
|
|
`id` int(11) NOT NULL AUTO_INCREMENT,
|
|
|
|
`name` VARCHAR(255) NOT NULL,
|
|
|
|
`a` VARBINARY(255) NOT NULL,
|
|
|
|
`aaaa` VARBINARY(255) NOT NULL,
|
|
|
|
`last_update` TIMESTAMP NOT NULL,
|
|
|
|
PRIMARY KEY (`id`)
|
|
|
|
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
|
|
|
|
|
|
|
|
$statement = $this->dbConnection->prepare(query: $sql);
|
|
|
|
$statement->execute();
|
|
|
|
|
2022-01-23 16:31:18 +01:00
|
|
|
echo 'Tables have been created.' . PHP_EOL;
|
|
|
|
}
|
|
|
|
exit(1);
|
|
|
|
}
|
2022-09-17 16:26:14 +02:00
|
|
|
|
2022-01-18 19:14:24 +01:00
|
|
|
} catch (PDOException $exception) {
|
2022-01-23 16:31:18 +01:00
|
|
|
echo $exception->getMessage() . PHP_EOL;
|
|
|
|
echo 'Did you create the database and adjust the config file?' . PHP_EOL;
|
|
|
|
echo PHP_EOL . 'You can create database an user via a panel or manually in mysql shell:' . PHP_EOL;
|
2022-01-29 14:38:02 +01:00
|
|
|
$password = $this->generatePassword();
|
|
|
|
echo 'Created an initial password: ' . $password . PHP_EOL;
|
|
|
|
echo 'CREATE DATABASE bindAPI;' . PHP_EOL;
|
|
|
|
echo "CREATE USER 'bindAPI'@'localhost' IDENTIFIED BY '$password';" . PHP_EOL;
|
|
|
|
echo "GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, INDEX, DROP, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES ON bindAPI.* TO 'bindAPI'@'localhost';" . PHP_EOL;
|
2022-01-23 16:31:18 +01:00
|
|
|
echo 'There is no need to run FLUSH PRIVILEGES when using GRANT!' . PHP_EOL;
|
|
|
|
exit(1);
|
2022-01-18 19:14:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-31 20:58:12 +01:00
|
|
|
/**
|
|
|
|
* @param int $length
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function generatePassword(int $length = 8): string
|
|
|
|
{
|
2022-01-29 14:38:02 +01:00
|
|
|
$chars = '23456789bcdfhkmnprstvzBCDFHJKLMNPRSTVZ';
|
2022-01-31 20:58:12 +01:00
|
|
|
$shuffled = str_shuffle(string: $chars);
|
|
|
|
return mb_substr(string: $shuffled, start: 0, length: $length);
|
2022-01-29 14:38:02 +01:00
|
|
|
}
|
|
|
|
|
2022-01-18 19:14:24 +01:00
|
|
|
/**
|
|
|
|
* @return \PDO
|
|
|
|
*/
|
|
|
|
public function getConnection(): PDO
|
|
|
|
{
|
|
|
|
return $this->dbConnection;
|
|
|
|
}
|
2022-07-20 19:09:40 +02:00
|
|
|
}
|