Compare commits

..

No commits in common. "b536316a84755b624fcd144f6075586491bd3a2d" and "d5bdb1bade0cf52b2b4e0e4571eb203fef810d30" have entirely different histories.

14 changed files with 1840 additions and 2428 deletions

13
bin/console Executable file → Normal file
View File

@ -26,13 +26,11 @@ require dirname(path: __DIR__) . '/vendor/autoload.php';
$shortOpts = 'v::'; // version $shortOpts = 'v::'; // version
$shortOpts = 'q::'; // version
$shortOpts .= "V::"; // verbose $shortOpts .= "V::"; // verbose
$shortOpts .= "h::"; // help $shortOpts .= "h::"; // help
$longOpts = [ $longOpts = [
'version::', 'version::',
'quiet::',
'verbose::', 'verbose::',
'help::' 'help::'
]; ];
@ -49,13 +47,6 @@ if (array_key_exists(key: 'h', array: $options) || array_key_exists(key: 'help',
exit(0); exit(0);
} }
if (array_key_exists(key: 'q', array: $options) || array_key_exists(key: 'quiet', array: $options)) {
$quiet = true;
} else {
$quiet = false;
}
if (array_key_exists(key: 'V', array: $options) || array_key_exists(key: 'verbose', array: $options)) { if (array_key_exists(key: 'V', array: $options) || array_key_exists(key: 'verbose', array: $options)) {
$verbose = true; $verbose = true;
} else { } else {
@ -65,8 +56,8 @@ if (array_key_exists(key: 'V', array: $options) || array_key_exists(key: 'verbos
$arguments = array_slice(array: $argv, offset: $restIndex); $arguments = array_slice(array: $argv, offset: $restIndex);
try { try {
$app = new BindAPI(verbose: $verbose, quiet: $quiet); $app = new BindAPI(verbose: $verbose );
$app->runCommand(arguments: $arguments); $app->runCommand(argumentsCount: count(value: $arguments), arguments: $arguments);
} catch (DependencyException|NotFoundException|Exception $e) { } catch (DependencyException|NotFoundException|Exception $e) {
echo $e->getMessage() . PHP_EOL; echo $e->getMessage() . PHP_EOL;

View File

@ -8,12 +8,8 @@ use App\Repository\DomainRepository;
use App\Repository\DynDNSRepository; use App\Repository\DynDNSRepository;
use DI\Container; use DI\Container;
use DI\ContainerBuilder; use DI\ContainerBuilder;
use DI\DependencyException;
use DI\NotFoundException;
use Exception;
use Monolog\Formatter\LineFormatter; use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler; use Monolog\Handler\StreamHandler;
use Monolog\Level;
use Monolog\Logger; use Monolog\Logger;
use function DI\autowire; use function DI\autowire;
@ -24,11 +20,12 @@ class BindAPI
{ {
private Logger $logger; private Logger $logger;
private Container $container; private Container $container;
/** /**
* @throws Exception * @throws \Exception
*/ */
public function __construct($verbose = false, $quiet = false) public function __construct($verbose = false)
{ {
// init the logger // init the logger
$dateFormat = "Y:m:d H:i:s"; $dateFormat = "Y:m:d H:i:s";
@ -37,9 +34,9 @@ class BindAPI
$debug = (new ConfigController)->getConfig(configKey: 'debug'); $debug = (new ConfigController)->getConfig(configKey: 'debug');
if ($debug) { if ($debug) {
$stream = new StreamHandler(stream: dirname(path: __DIR__, levels: 2) . '/bindAPI.log', level: Level::Debug); $stream = new StreamHandler(stream: dirname(path: __DIR__, levels: 2) . '/bindAPI.log', level: Logger::DEBUG);
} else { } else {
$stream = new StreamHandler(stream: dirname(path: __DIR__, levels: 2) . '/bindAPI.log', level: Level::Info); $stream = new StreamHandler(stream: dirname(path: __DIR__, levels: 2) . '/bindAPI.log', level: Logger::INFO);
} }
$stream->setFormatter(formatter: $formatter); $stream->setFormatter(formatter: $formatter);
@ -51,7 +48,6 @@ class BindAPI
$containerBuilder = new ContainerBuilder(); $containerBuilder = new ContainerBuilder();
$containerBuilder->addDefinitions([ $containerBuilder->addDefinitions([
ConfigController::class => autowire() ConfigController::class => autowire()
->constructorParameter(parameter: 'quiet', value: $quiet)
->constructorParameter(parameter: 'verbose', value: $verbose), ->constructorParameter(parameter: 'verbose', value: $verbose),
CLIController::class => autowire() CLIController::class => autowire()
->constructorParameter(parameter: 'logger', value: $this->logger), ->constructorParameter(parameter: 'logger', value: $this->logger),
@ -69,8 +65,8 @@ class BindAPI
/** /**
* @throws DependencyException * @throws \DI\DependencyException
* @throws NotFoundException * @throws \DI\NotFoundException
*/ */
public function runCommand(array $arguments): void public function runCommand(array $arguments): void
{ {
@ -81,8 +77,8 @@ class BindAPI
/** /**
* @throws DependencyException * @throws \DI\DependencyException
* @throws NotFoundException * @throws \DI\NotFoundException
*/ */
public function handleRequest(string $requestMethod, array $uri): void public function handleRequest(string $requestMethod, array $uri): void
{ {

File diff suppressed because it is too large Load Diff

View File

@ -1,68 +0,0 @@
<?php
namespace App\Controller\Commands;
use Closure;
/**
*
*/
class Command
{
public function __construct(
private readonly string $name,
private readonly Closure $callback,
private readonly array $mandatoryParameters = [],
private readonly array $optionalParameters = [],
private readonly string $description = ''
)
{
// no body
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* @return array
*/
public function getMandatoryParameters(): array
{
return $this->mandatoryParameters;
}
/**
* @return array
*/
public function getOptionalParameters(): array
{
return $this->optionalParameters;
}
/**
* @return string|null
*/
public function getDescription(): ?string
{
return $this->description;
}
/**
* @return Closure
*/
public function getCallback(): Closure
{
return $this->callback;
}
public function exec(): void
{
call_user_func(callback: $this->callback);
}
}

View File

@ -1,68 +0,0 @@
<?php
namespace App\Controller\Commands;
/**
*
*/
class CommandGroup
{
private array $commands = [];
public function __construct(private readonly string $name, private readonly string $description)
{
// no body
}
public function addCommand(Command $command): ?CommandGroup
{
$this->commands[] = $command;
return $this;
}
public function getName(): string
{
return $this->name;
}
public function printCommands(int $longestCommandLength): void
{
echo COLOR_YELLOW . str_pad(string: $this->name, length: $longestCommandLength + 1) . COLOR_WHITE . $this->description . COLOR_DEFAULT . PHP_EOL;
foreach ($this->commands as $command) {
echo COLOR_GREEN . str_pad(string: ' ', length: $longestCommandLength + 1, pad_type: STR_PAD_LEFT) . $this->name . ':' . $command->getName();
foreach ($command->getMandatoryParameters() as $parameter) {
echo ' <' . $parameter . '>';
}
foreach ($command->getOptionalParameters() as $parameter) {
echo ' {' . $parameter . '}';
}
echo COLOR_WHITE . ' ' . $command->getDescription();
echo COLOR_DEFAULT . PHP_EOL;
}
}
public function findCommandByName(string $command): ?Command
{
foreach ($this->commands as $currentCommand) {
if ($command === $currentCommand->getName()) {
return $currentCommand;
}
}
return null;
}
public function exec(string $subcommand): void
{
if ($command = $this->findCommandByName(command: $subcommand)) {
$command->exec();
} else {
echo COLOR_DEFAULT . 'Command ' . COLOR_YELLOW . $this->name . ':' . $subcommand . COLOR_DEFAULT . ' not found.' . PHP_EOL;
exit(1);
}
}
}

View File

@ -1,73 +0,0 @@
<?php
namespace App\Controller\Commands;
/**
*
*/
class CommandGroupContainer
{
private array $commandGroups = [];
public function addCommandGroup(CommandGroup $commandGroup): CommandGroupContainer
{
$this->commandGroups[] = $commandGroup;
return $this;
}
/**
* @return void
*/
public function printCommands(): void
{
$longestCommandLength = $this->getLongestCommandLength();
foreach ($this->commandGroups as $commandGroup) {
$commandGroup->printCommands($longestCommandLength);
}
}
/**
* @return int
*/
public function getLongestCommandLength(): int
{
$longest = 0;
foreach ($this->commandGroups as $group) {
$len = strlen(string: $group->getName());
if ($len > $longest) {
$longest = $len;
}
}
return $longest;
}
/**
* @param string $command
* @return ?CommandGroup
*/
private function findGroupByName(string $command): ?CommandGroup
{
foreach ($this->commandGroups as $group) {
if ($group->getName() === $command) {
return $group;
}
}
return null;
}
public function run(string $command, string $subcommand): void
{
if ($group = $this->findGroupByName(command: $command)) {
$group->exec(subcommand: $subcommand);
} else {
echo COLOR_DEFAULT . 'Unknown command ' . COLOR_YELLOW . $command . COLOR_DEFAULT . '.' . PHP_EOL;
exit(1);
}
}
}

View File

@ -9,7 +9,7 @@ class ConfigController
{ {
private array $config; private array $config;
public function __construct(bool $verbose = false, bool $quiet = false) { public function __construct(bool $verbose = false) {
$configFile = dirname(path: __DIR__, levels: 2) . "/config.json.local"; $configFile = dirname(path: __DIR__, levels: 2) . "/config.json.local";
if (!file_exists(filename: $configFile)) { if (!file_exists(filename: $configFile)) {
$configFile = dirname(path: __DIR__, levels: 2) . "/config.json"; $configFile = dirname(path: __DIR__, levels: 2) . "/config.json";
@ -41,11 +41,6 @@ class ConfigController
} else { } else {
$this->config['verbose'] = false; $this->config['verbose'] = false;
} }
if ($quiet) {
$this->config['quiet'] = true;
} else {
$this->config['quiet'] = false;
}
} }
public function getConfig(string $configKey): string { public function getConfig(string $configKey): string {

View File

@ -83,7 +83,7 @@ class DomainController
} }
$this->createSlaveZoneFile(domain: $domain); $this->createSlaveZoneFile(domain: $domain);
} }
$this->createIncludeFile(); $this->createIncludeFile();
} }
@ -98,30 +98,16 @@ class DomainController
'name' => $domain->getName() 'name' => $domain->getName()
]; ];
if (!empty($nameserver->getAaaa())) { if (!empty($nameserver->getAaaa())) {
$this->checkController->sendCommand( $this->checkController->sendCommand(requestType: 'DELETE', serverName: $nameserver->getName(), versionIP: 6, apiKey: $nameserver->getApikey(), command: 'delete', serverType: 'nameserver', body: $body);
requestType: 'DELETE',
serverName: $nameserver->getName(),
versionIP: 6,
apiKey: $nameserver->getApikey(),
command: 'delete',
serverType: 'nameserver',
body: $body);
} else { } else {
$this->checkController->sendCommand( $this->checkController->sendCommand(requestType: 'DELETE', serverName: $nameserver->getName(), versionIP: 4, apiKey: $nameserver->getApikey(), command: 'delete', serverType: 'nameserver', body: $body);
requestType: 'DELETE',
serverName: $nameserver->getName(),
versionIP: 4,
apiKey: $nameserver->getApikey(),
command: 'delete',
serverType: 'nameserver',
body: $body);
} }
} }
} }
/** /**
* @param Domain $domain * @param \App\Entity\Domain $domain
* *
* @return void * @return void
*/ */
@ -244,34 +230,25 @@ class DomainController
$domains = $this->domainRepository->findAll(); $domains = $this->domainRepository->findAll();
foreach ($domains as $domain) { foreach ($domains as $domain) {
$idString = '(' . strval(value: $domain->getId()) . ') '; echo COLOR_YELLOW . str_pad(string: $domain->getName(), length: $maxNameLength + 1) . COLOR_DEFAULT;
echo COLOR_YELLOW .
str_pad(string: $domain->getName(), length: $maxNameLength + 1)
. COLOR_DEFAULT
. str_pad(string: $idString, length: 7, pad_type: STR_PAD_LEFT);
$hasError = false;
if ($this->isMasterZone(domain: $domain)) { if ($this->isMasterZone(domain: $domain)) {
echo 'Master Zone lies on this panel.'; echo 'Master Zone lies on this panel.';
} else { } else {
if (!str_contains(haystack: $localZones, needle: $domain->getName())) { if (!str_contains(haystack: $localZones, needle: $domain->getName())) {
echo COLOR_RED . 'is missing in ' . COLOR_YELLOW . $this->localZoneFile . COLOR_DEFAULT; echo COLOR_RED . ' is missing in ' . COLOR_YELLOW . $this->localZoneFile . COLOR_DEFAULT;
$hasError = true;
} else { } else {
echo COLOR_GREEN . 'OK'; echo $domain->getName() . ' exists in ' . COLOR_YELLOW . $this->localZoneFile;
} }
$zoneFile = $this->localZonesDir . $domain->getName(); $zoneFile = $this->localZonesDir . $domain->getName();
if (!file_exists(filename: $zoneFile)) { if (!file_exists(filename: $zoneFile)) {
echo ' Missing zone file for ' . COLOR_YELLOW . $zoneFile . COLOR_DEFAULT; echo "Missing zone file for $zoneFile . Update zone to create it";
$hasError = true;
} }
if ($hasError) {
echo " Update zone (Domain) to create it.";
}
} }
echo COLOR_DEFAULT . PHP_EOL; echo COLOR_DEFAULT . PHP_EOL;
} }
@ -279,7 +256,7 @@ class DomainController
/** /**
* @param Domain $domain * @param \App\Entity\Domain $domain
* *
* @return void * @return void
*/ */
@ -290,11 +267,11 @@ class DomainController
// check if we're a master zone // check if we're a master zone
if ($this->isMasterZone(domain: $domain)) { if ($this->isMasterZone(domain: $domain)) {
//echo 'We are zone master for ' . $domainName . PHP_EOL; echo 'We are zone master for ' . $domainName . PHP_EOL;
return; exit(1);
} }
if ($zoneFile = fopen(filename: $this->localZonesDir . $domainName, mode: 'w')) { if ($zonefile = fopen(filename: $this->localZonesDir . $domainName, mode: 'w')) {
$panelName = $domain->getPanel(); $panelName = $domain->getPanel();
if (!$panel = $this->panelRepository->findByName(name: $panelName)) { if (!$panel = $this->panelRepository->findByName(name: $panelName)) {
echo "Error: Panel $panelName doesn't exist." . PHP_EOL; echo "Error: Panel $panelName doesn't exist." . PHP_EOL;
@ -302,18 +279,18 @@ class DomainController
} }
$a = $panel->getA(); $a = $panel->getA();
$aaaa = $panel->getAaaa(); $aaaa = $panel->getAaaa();
fputs(stream: $zoneFile, data: 'zone "' . $domainName . '"' . ' IN {' . PHP_EOL); fputs(stream: $zonefile, data: 'zone "' . $domainName . '"' . ' IN {' . PHP_EOL);
fputs(stream: $zoneFile, data: "\ttype slave;" . PHP_EOL); fputs(stream: $zonefile, data: "\ttype slave;" . PHP_EOL);
fputs(stream: $zoneFile, data: "\tfile \"" . $this->zoneCachePath . $domainName . '.db";' . PHP_EOL); fputs(stream: $zonefile, data: "\tfile \"" . $this->zoneCachePath . $domainName . '.db";' . PHP_EOL);
fputs(stream: $zoneFile, data: "\tmasters {" . PHP_EOL); fputs(stream: $zonefile, data: "\tmasters {" . PHP_EOL);
if (!empty($a)) { if (!empty($a)) {
fputs(stream: $zoneFile, data: "\t\t" . $a . ';' . PHP_EOL); fputs(stream: $zonefile, data: "\t\t" . $a . ';' . PHP_EOL);
} }
if (!empty($aaaa)) { if (!empty($aaaa)) {
fputs(stream: $zoneFile, data: "\t\t" . $aaaa . ';' . PHP_EOL); fputs(stream: $zonefile, data: "\t\t" . $aaaa . ';' . PHP_EOL);
} }
fputs(stream: $zoneFile, data: "\t};" . PHP_EOL); fputs(stream: $zonefile, data: "\t};" . PHP_EOL);
fputs(stream: $zoneFile, data: "};" . PHP_EOL); fputs(stream: $zonefile, data: "};" . PHP_EOL);
} }
} }

View File

@ -20,13 +20,11 @@ class EncryptionController
*/ */
function safeEncrypt(string $message, string $key): string function safeEncrypt(string $message, string $key): string
{ {
$binKey = sodium_hex2bin(string: $key);
$nonce = random_bytes(length: SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); $nonce = random_bytes(length: SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
$cipher = base64_encode(string: $nonce . sodium_crypto_secretbox(message: $message, nonce: $nonce, key: $binKey)); $cipher = base64_encode(string: $nonce . sodium_crypto_secretbox(message: $message, nonce: $nonce, key: $key));
sodium_memzero(string: $message); sodium_memzero(string: $message);
sodium_memzero(string: $key); sodium_memzero(string: $key);
sodium_memzero(string: $binKey);
return $cipher; return $cipher;
} }
@ -41,23 +39,19 @@ class EncryptionController
*/ */
function safeDecrypt(string $encrypted, string $key): string function safeDecrypt(string $encrypted, string $key): string
{ {
$binKey = sodium_hex2bin(string: $key);
$decoded = base64_decode(string: $encrypted); $decoded = base64_decode(string: $encrypted);
if ($decoded === false) { if ($decoded === false) {
throw new Exception(message: 'Decoding broken. Wrong payload.'); throw new Exception(message: 'Decoding broken. Wrong key?');
} }
if (mb_strlen(string: $decoded, encoding: '8bit') < (SODIUM_CRYPTO_SECRETBOX_NONCEBYTES + SODIUM_CRYPTO_SECRETBOX_MACBYTES)) { if (mb_strlen(string: $decoded, encoding: '8bit') < (SODIUM_CRYPTO_SECRETBOX_NONCEBYTES + SODIUM_CRYPTO_SECRETBOX_MACBYTES)) {
throw new Exception(message: 'Decoding broken. Incomplete message.'); throw new Exception(message: 'Decoding broken. Incomplete message.');
} }
$nonce = mb_substr(string: $decoded, start: 0, length: SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, encoding: '8bit'); $nonce = mb_substr(string: $decoded, start: 0, length: SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, encoding: '8bit');
$ciphertext = mb_substr(string: $decoded, start: SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, length: null, encoding: '8bit'); $ciphertext = mb_substr(string: $decoded, start: SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, length: null, encoding: '8bit');
$plain = sodium_crypto_secretbox_open(ciphertext: $ciphertext, nonce: $nonce, key: $binKey); $plain = sodium_crypto_secretbox_open(ciphertext: $ciphertext, nonce: $nonce, key: $key);
if ($plain === false) { if ($plain === false) {
throw new Exception(message: ' Incorrect key.'); throw new Exception(message: 'The message was tampered with in transit');
} }
sodium_memzero(string: $ciphertext); sodium_memzero(string: $ciphertext);
sodium_memzero(string: $key); sodium_memzero(string: $key);

View File

@ -11,6 +11,7 @@ use App\Repository\DomainRepository;
use App\Repository\DynDNSRepository; use App\Repository\DynDNSRepository;
use App\Repository\PanelRepository; use App\Repository\PanelRepository;
use Monolog\Logger; use Monolog\Logger;
use OpenApi\Annotations as OA;
use OpenApi\Attributes as OAT; use OpenApi\Attributes as OAT;
use UnhandledMatchError; use UnhandledMatchError;
@ -57,13 +58,13 @@ class RequestController
/** /**
* @param ApiController $apiController * @param \App\Controller\ApiController $apiController
* @param ApikeyRepository $apikeyRepository * @param \App\Repository\ApikeyRepository $apikeyRepository
* @param DomainController $domainController * @param \App\Controller\DomainController $domainController
* @param DomainRepository $domainRepository * @param \App\Repository\DomainRepository $domainRepository
* @param DynDNSRepository $dynDNSRepository * @param \App\Repository\DynDNSRepository $dynDNSRepository
* @param PanelRepository $panelRepository * @param \App\Repository\PanelRepository $panelRepository
* @param Logger $logger * @param \Monolog\Logger $logger
*/ */
public function __construct( public function __construct(
private readonly ApiController $apiController, private readonly ApiController $apiController,
@ -288,7 +289,7 @@ class RequestController
{ {
$headers = array_change_key_case(array: getallheaders(), case: CASE_UPPER); $headers = array_change_key_case(array: getallheaders(), case: CASE_UPPER);
$apiKey = $headers['X-API-KEY'] ?? ''; $apiKey = $headers['X-API-KEY'] ?? '';
if (empty($apiKey)) { if (empty($apiKey)) {
$this->status = "401 Unauthorized"; $this->status = "401 Unauthorized";
$this->message = "API key is missing."; $this->message = "API key is missing.";

View File

@ -2,106 +2,89 @@
namespace App\Entity; namespace App\Entity;
use App\Controller\ConfigController;
use App\Controller\EncryptionController;
use Exception;
use SodiumException;
/** /**
* *
*/ */
class Apikey class Apikey
{ {
private int $id;
public function __construct( private string $name;
private int $id = 0, private string $apiTokenPrefix;
private string $name = '', private string $apiToken;
private string $apiTokenPrefix = '',
private string $apiToken = '', public function __construct(string $name, string $apiTokenPrefix, string $apiToken, int $id = 0)
private readonly string $passphrase = '' {
) $this->id = $id;
{ $this->name = $name;
if ($this->passphrase) { $this->apiTokenPrefix = $apiTokenPrefix;
$configController = new ConfigController(); $this->apiToken = $apiToken;
$encryptionController = new EncryptionController(); }
$encryptionKey = $configController->getConfig(configKey: 'encryptionKey');
/**
$this->apiTokenPrefix = strtok(string: $this->passphrase, token: '.'); * @return String
*/
try { public function getApiToken(): string
$this->apiToken = $encryptionController->safeEncrypt(message: $this->passphrase, key: $encryptionKey); {
} catch (Exception|SodiumException $e) { return $this->apiToken;
die($e->getMessage() . PHP_EOL); }
}
} /**
} * @return string
*/
public function getApiTokenPrefix(): string
/** {
* @return String return $this->apiTokenPrefix;
*/ }
public function getApiToken(): string
{
return $this->apiToken; /**
} * @return int
*/
/** public function getId(): int
* @return string {
*/ return $this->id;
public function getApiTokenPrefix(): string }
{
return $this->apiTokenPrefix; /**
} * @param int $id
*/
public function setId(int $id): void
/** {
* @return int $this->id = $id;
*/ }
public function getId(): int
{ /**
return $this->id; * @return String
} */
public function getName(): string
/** {
* @param int $id return $this->name;
*/ }
public function setId(int $id): void
{ /**
$this->id = $id; * @param string $apiTokenPrefix
} */
public function setApiTokenPrefix(string $apiTokenPrefix): void
/** {
* @return String $this->apiTokenPrefix = $apiTokenPrefix;
*/ }
public function getName(): string
{ /**
return $this->name; * @param String $apiToken
} */
public function setApiToken(string $apiToken): void
/** {
* @param string $apiTokenPrefix $this->apiToken = $apiToken;
*/ }
public function setApiTokenPrefix(string $apiTokenPrefix): void
{
$this->apiTokenPrefix = $apiTokenPrefix; /**
} * @param String $name
*/
/** public function setName(string $name): void
* @param String $apiToken {
*/ $this->name = $name;
public function setApiToken(string $apiToken): void }
{
$this->apiToken = $apiToken;
}
/**
* @param String $name
*/
public function setName(string $name): void
{
$this->name = $name;
}
} }

View File

@ -2,11 +2,7 @@
namespace App\Entity; namespace App\Entity;
use App\Controller\ConfigController;
use App\Controller\EncryptionController;
use Exception;
use OpenApi\Attributes as OAT; use OpenApi\Attributes as OAT;
use SodiumException;
/** /**
* *
@ -15,109 +11,73 @@ use SodiumException;
class Nameserver class Nameserver
{ {
/** private int $id;
* @param string $name private String $name;
* @param int $id private String $a;
* @param string $a private String $aaaa;
* @param string $aaaa private String $apikey;
* @param string $passphrase
* @param string $apikey public function __construct(String $name, int $id = 0, String $a = '', String $aaaa = '', String $apikey = '')
* @param string $apikeyPrefix
*/
public function __construct(
private string $name,
private int $id = 0,
private string $a = '',
private string $aaaa = '',
private readonly string $passphrase = '',
private string $apikey = '',
private string $apikeyPrefix = '')
{ {
if ($this->passphrase) { $this->id = $id;
$configController = new ConfigController(); $this->name = $name;
$encryptionController = new EncryptionController(); $this->a = $a;
$this->aaaa = $aaaa;
$encryptionKey = $configController->getConfig(configKey: 'encryptionKey'); $this->apikey = $apikey;
}
[$this->apikeyPrefix] = explode(separator: '.', string: $this->passphrase);
try {
$this->apikey = $encryptionController->safeEncrypt(message: $this->passphrase, key: $encryptionKey);
} catch (Exception|SodiumException $e) {
die($e->getMessage() . PHP_EOL);
}
}
}
/**
* @return string
*/
public function getApikeyPrefix(): string
{
return $this->apikeyPrefix;
}
/**
* @param string $apikeyPrefix
*/
public function setApikeyPrefix(string $apikeyPrefix): void
{
$this->apikeyPrefix = $apikeyPrefix;
}
/** /**
* @return string * @return String
*/ */
#[OAT\Property(type: 'string')] #[OAT\Property(type: 'string')]
public function getA(): string public function getA(): string
{ {
return $this->a; return $this->a;
} }
/** /**
* @param string $a * @param String $a
*/ */
public function setA(string $a): void public function setA(string $a): void
{ {
$this->a = $a; $this->a = $a;
} }
/** /**
* @return string * @return String
*/ */
#[OAT\Property(type: 'string')] #[OAT\Property(type: 'string')]
public function getAaaa(): string public function getAaaa(): string
{ {
return $this->aaaa; return $this->aaaa;
} }
/** /**
* @param string $aaaa * @param String $aaaa
*/ */
public function setAaaa(string $aaaa): void public function setAaaa(string $aaaa): void
{ {
$this->aaaa = $aaaa; $this->aaaa = $aaaa;
} }
/** /**
* @return string * @return String
*/ */
#[OAT\Property(type: 'string')] #[OAT\Property(type: 'string')]
public function getApikey(): string public function getApikey(): string
{ {
return $this->apikey; return $this->apikey;
} }
/** /**
* @param string $apikey * @param String $apikey
*/ */
public function setApikey(string $apikey): void public function setApikey(string $apikey): void
{ {
$this->apikey = $apikey; $this->apikey = $apikey;
} }
/** /**
* @return int * @return int
*/ */
@ -126,7 +86,7 @@ class Nameserver
{ {
return $this->id; return $this->id;
} }
/** /**
* @param int $id * @param int $id
*/ */
@ -134,30 +94,22 @@ class Nameserver
{ {
$this->id = $id; $this->id = $id;
} }
/** /**
* @return string * @return String
*/ */
#[OAT\Property(type: 'string')] #[OAT\Property(type: 'string')]
public function getName(): string public function getName(): string
{ {
return $this->name; return $this->name;
} }
/** /**
* @param string $name * @param String $name
*/ */
public function setName(string $name): void public function setName(string $name): void
{ {
$this->name = $name; $this->name = $name;
} }
/**
* @return string
*/
public function getPassphrase(): string
{
return $this->passphrase;
}
} }

View File

@ -2,160 +2,133 @@
namespace App\Entity; namespace App\Entity;
use App\Controller\ConfigController;
use App\Controller\EncryptionController;
use Exception;
use SodiumException;
/** /**
* *
*/ */
class Panel class Panel
{ {
/** private int $id;
* @param string $name private String $name;
* @param int $id private String $a;
* @param string $a private String $aaaa;
* @param string $aaaa private String $apikey;
* @param string $passphrase private int $self;
* @param string $apikey
* @param string $apikeyPrefix /**
* @param string $self * @param String $name
*/ * @param int $id
public function __construct( * @param String $a
private string $name, * @param String $aaaa
private int $id = 0, * @param String $apikey
private string $a = '', * @param int $self
private string $aaaa = '', */
private readonly string $passphrase = '', public function __construct(String $name, int $id = 0, String $a = '', String $aaaa = '', String $apikey = '', int $self = 0)
private string $apikey = '', {
private string $apikeyPrefix = '', $this->id = $id;
private string $self = 'no', $this->name = $name;
) $this->a = $a;
{ $this->aaaa = $aaaa;
if ($this->passphrase) { $this->apikey = $apikey;
$configController = new ConfigController(); $this->self = $self;
$encryptionController = new EncryptionController(); }
$encryptionKey = $configController->getConfig(configKey: 'encryptionKey'); /**
* @return int
$this->apikeyPrefix = strtok(string: $this->passphrase, token: '.'); */
public function getSelf(): int
try { {
$this->apikey = $encryptionController->safeEncrypt(message: $this->passphrase, key: $encryptionKey); return $this->self;
} catch (Exception|SodiumException $e) { }
die($e->getMessage() . PHP_EOL);
} /**
} * @param int $self
} */
public function setSelf(int $self): void
/** {
* @return string $this->self = $self;
*/ }
public function getPassphrase(): string
{
return $this->passphrase; /**
} * @return String
*/
public function getA(): string
/** {
* @return string return $this->a;
*/ }
public function getName(): string
{ /**
return $this->name; * @return String
} */
public function getAaaa(): string
/** {
* @param string $name return $this->aaaa;
*/ }
public function setName(string $name): void
{ /**
$this->name = $name; * @return String
} */
public function getApikey(): string
/** {
* @return int return $this->apikey;
*/ }
public function getId(): int
{ /**
return $this->id; * @return int
} */
public function getId(): int
/** {
* @param int $id return $this->id;
*/ }
public function setId(int $id): void
{ /**
$this->id = $id; * @param int $id
} */
public function setId(int $id): void
/** {
* @return string $this->id = $id;
*/ }
public function getA(): string
{ /**
return $this->a; * @return String
} */
public function getName(): string
{
/** return $this->name;
* @return string }
*/
public function getSelf(): string
{ /**
return $this->self; * @param String $apikey
} */
public function setApikey(string $apikey): void
/** {
* @param string $self $this->apikey = $apikey;
*/ }
public function setSelf(string $self): void
{
$this->self = $self; /**
} * @param String $name
*/
public function setName(string $name): void
/** {
* @return string $this->name = $name;
*/ }
public function getAaaa(): string
{ /**
return $this->aaaa; * @param String $a
} */
public function setA(string $a): void
/** {
* @return string $this->a = $a;
*/ }
public function getApikey(): string
{ /**
return $this->apikey; * @param String $aaaa
} */
public function setAaaa(string $aaaa): void
{
/** $this->aaaa = $aaaa;
* @param string $a }
*/
public function setA(string $a): void
{
$this->a = $a;
}
/**
* @param string $aaaa
*/
public function setAaaa(string $aaaa): void
{
$this->aaaa = $aaaa;
}
/**
* @return string
*/
public function getApikeyPrefix(): string
{
return $this->apikeyPrefix;
}
} }

View File

@ -3,6 +3,7 @@
namespace App\Repository; namespace App\Repository;
use App\Controller\DatabaseConnection; use App\Controller\DatabaseConnection;
use App\Entity\Domain;
use App\Entity\Panel; use App\Entity\Panel;
use PDO; use PDO;
use PDOException; use PDOException;
@ -12,249 +13,243 @@ use PDOException;
*/ */
class PanelRepository class PanelRepository
{ {
public function __construct(private readonly DatabaseConnection $databaseConnection) public function __construct(private readonly DatabaseConnection $databaseConnection)
{ {}
// no body
} public function findSelf(): array
{
/** $sql = "
* @return array|null SELECT id, name, a, aaaa, apikey, self
*/
public function findSelf(): ?array
{
$sql = "
SELECT id, name, a, aaaa, apikey, apikey_prefix, self
FROM " . DatabaseConnection::TABLE_PANELS . " FROM " . DatabaseConnection::TABLE_PANELS . "
WHERE self = 1"; WHERE self = 1";
$panels = []; $panels = [];
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->execute(); $statement->execute();
while ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) { while ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
$panel = new Panel(name: $result['name'], id: $result['id'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], apikeyPrefix: $result['apikey_prefix'], self: $result['self']); $panel = new Panel(name: $result['name'], id: $result['id'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], self: $result['self']);
$panels[] = $panel; $panels[] = $panel;
} }
return $panels; return $panels;
} } catch (PDOException $e) {
exit($e->getMessage());
}
public function findAll(): ?array
{
$panels = []; }
$sql = "
SELECT id, name, a, aaaa, apikey, apikey_prefix, self /**
* @return array
*/
public function findAll(): array
{
$panels = [];
$sql = "
SELECT id, name, a, aaaa, apikey, self
FROM " . DatabaseConnection::TABLE_PANELS . " FROM " . DatabaseConnection::TABLE_PANELS . "
ORDER BY name"; ORDER BY name";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->execute(); $statement->execute();
while ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) { while ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
$panel = new Panel(name: $result['name'], id: $result['id'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], apikeyPrefix: $result['apikey_prefix'], self: $result['self']); $panel = new Panel(name: $result['name'], id: $result['id'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], self: $result['self']);
$panels[] = $panel; $panels[] = $panel;
} }
return $panels; return $panels;
} catch (PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
} }
} }
/** /**
* @param int $id * @param int $id
* *
* @return null|Panel * @return null|\App\Entity\Panel
*/ */
public function findByID(int $id): ?Panel public function findByID(int $id): ?Panel
{ {
$sql = " $sql = "
SELECT id, name, a, aaaa, apikey, apikey_prefix, self SELECT id, name, a, aaaa, apikey, self
FROM . " . DatabaseConnection::TABLE_PANELS . " FROM . " . DatabaseConnection::TABLE_PANELS . "
WHERE id = :id"; WHERE id = :id";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':id', var: $id); $statement->bindParam(param: ':id', var: $id);
$statement->execute(); $statement->execute();
if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) { if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
return new Panel(name: $result['name'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], apikeyPrefix: $result['apikey_prefix'], self: $result['self']); return new Panel(name: $result['name'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], self: $result['self']);
} else { } else {
return null; return null;
} }
} catch (PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
} }
} }
/** /**
* @param String $name * @param String $name
* *
* @return Panel|null * @return \App\Entity\Panel|bool
*/ */
public function findByName(string $name): ?Panel public function findByName(string $name): Panel|bool
{ {
$sql = " $sql = "
SELECT id, name, a, aaaa, apikey, apikey_prefix, self SELECT id, name, a, aaaa, apikey, self
FROM " . DatabaseConnection::TABLE_PANELS . " FROM " . DatabaseConnection::TABLE_PANELS . "
WHERE name = :name"; WHERE name = :name";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':name', var: $name); $statement->bindParam(param: ':name', var: $name);
$statement->execute(); $statement->execute();
if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) { if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
return new Panel(name: $result['name'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], apikeyPrefix: $result['apikey_prefix'], self: $result['self']); return new Panel(name: $result['name'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], self: $result['self']);
} else { } else {
return null; return false;
} }
} catch (PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
} }
} }
/** /**
* @param Panel $panel * @param String $name
* @return int|null * @param String $a
*/ * @param String $aaaa
public function insert(Panel $panel): ?int * @param String $apikey
{ *
$name = $panel->getName(); * @return string|false
$a = $panel->getA(); */
$aaaa = $panel->getAaaa(); public function insert(string $name, string $a, string $aaaa, String $apikey, int $self): bool|string
$apikey = $panel->getApikey(); {
$apikeyPrefix = $panel->getApikeyPrefix(); $sql = "
$self = $panel->getSelf(); INSERT INTO " . DatabaseConnection::TABLE_PANELS . " (name, a, aaaa, apikey, self)
VALUES (:name, :a, :aaaa, :apikey, :self)";
$sql = "
INSERT INTO " . DatabaseConnection::TABLE_PANELS . " (name, a, aaaa, apikey, apikey_prefix, self) try {
VALUES (:name, :a, :aaaa, :apikey, :prefix, :self)"; $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':name', var: $name);
try { $statement->bindParam(param: ':a', var: $a);
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement->bindParam(param: ':aaaa', var: $aaaa);
$statement->bindParam(param: ':name', var: $name); $statement->bindParam(param: ':apikey', var: $apikey);
$statement->bindParam(param: ':a', var: $a); $statement->bindParam(param: ':self', var: $self);
$statement->bindParam(param: ':aaaa', var: $aaaa); $statement->execute();
$statement->bindParam(param: ':apikey', var: $apikey);
$statement->bindParam(param: ':prefix', var: $apikeyPrefix); return $this->databaseConnection->getConnection()->lastInsertId();
$statement->bindParam(param: ':self', var: $self); } catch (PDOException $e) {
$statement->execute(); exit($e->getMessage());
}
return intval(value: $this->databaseConnection->getConnection()->lastInsertId()); }
} catch (PDOException $e) {
exit($e->getMessage());
} /**
} * @param Int $id
* @param String $name
* @param String $a
/** * @param String $aaaa
* @param Panel $panel * @param String $apikey
* @return int|null *
*/ * @return false|int
public function update(Panel $panel): ?int */
{ public function update(int $id, string $name, string $a, string $aaaa, String $apikey, int $self): bool|int
$id = $panel->getId(); {
$name = $panel->getName(); $current = $this->findByID(id: $id);
$a = $panel->getA();
$aaaa = $panel->getAaaa(); if (empty($name)) {
$apikey = $panel->getApikey(); $name = $current->getName();
$apikeyPrefix = $panel->getApikeyPrefix(); }
$passphrase = $panel->getPassphrase(); if (empty($a)) {
$a = $current->getA();
$current = $this->findByID(id: $id); }
if (empty($aaaa)) {
$aaaa = $current->getAaaa();
if (empty($name)) { }
$name = $current->getName(); if (empty($apikey)) {
} $apikey = $current->getApikey();
if (empty($a)) { }
$a = $current->getA();
} if (empty($self)) {
if (empty($aaaa)) { echo "self is empty";
$aaaa = $current->getAaaa(); $self = $current->getSelf();
} } else {
if ($self == -1) {
if (empty($passphrase)) { $self = 0;
$apikey = $current->getApikey(); }
$apikeyPrefix = $current->getApikeyPrefix(); }
}
$sql = "
if (empty($self)) {
$self = $current->getSelf();
}
$sql = "
UPDATE " . DatabaseConnection::TABLE_PANELS . " SET UPDATE " . DatabaseConnection::TABLE_PANELS . " SET
name = :name, name = :name,
a = :a, a = :a,
aaaa = :aaaa, aaaa = :aaaa,
apikey = :apikey, apikey = :apikey,
apikey_prefix = :apikey_prefix,
self = :self self = :self
WHERE id = :id"; WHERE id = :id";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: 'id', var: $id); $statement->bindParam(param: 'id', var: $id);
$statement->bindParam(param: 'name', var: $name); $statement->bindParam(param: 'name', var: $name);
$statement->bindParam(param: 'a', var: $a); $statement->bindParam(param: 'a', var: $a);
$statement->bindParam(param: 'aaaa', var: $aaaa); $statement->bindParam(param: 'aaaa', var: $aaaa);
$statement->bindParam(param: 'apikey', var: $apikey); $statement->bindParam(param: 'apikey', var: $apikey);
$statement->bindParam(param: 'apikey_prefix', var: $apikeyPrefix); $statement->bindParam(param: 'self', var: $self);
$statement->bindParam(param: 'self', var: $self); $statement->execute();
$statement->execute();
return $statement->rowCount();
return intval(value: $statement->rowCount()); } catch (PDOException $e) {
} catch (PDOException $e) { echo $e->getMessage();
echo $e->getMessage(); return false;
return null; }
} }
}
/**
/** * @param $id
* @param $id *
* * @return int
* @return int|null */
*/ public function delete($id): int
public function delete($id): ?int {
{ $sql = "
$sql = "
DELETE FROM " . DatabaseConnection::TABLE_PANELS . " DELETE FROM " . DatabaseConnection::TABLE_PANELS . "
WHERE id = :id"; WHERE id = :id";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: 'id', var: $id); $statement->bindParam(param: 'id', var: $id);
$statement->execute(); $statement->execute();
return intval(value: $statement->rowCount()); return $statement->rowCount();
} catch (PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
} }
} }
/** /**
* @param String $field * @param String $field
* *
* @return int|null * @return int
*/ */
public function getLongestEntry(string $field): ?int public function getLongestEntry(String $field): int
{ {
$sql = " $sql = "
SELECT MAX(LENGTH(" . $field . ")) as length FROM " . DatabaseConnection::TABLE_PANELS; SELECT MAX(LENGTH(" . $field . ")) as length FROM " . DatabaseConnection::TABLE_PANELS;
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->execute(); $statement->execute();
$result = $statement->fetch(); $result = $statement->fetch();
return $result['length']; return $result['length'];
} catch (PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
} }
} }
} }