Compare commits

..

No commits in common. "662737318b4124c1a4613c6ab15f32fd25c5ee6c" and "383dc29ba5ba2b0b8139c68979af9813a3c26104" have entirely different histories.

10 changed files with 285 additions and 272 deletions

View File

@ -1,8 +1,8 @@
#!/usr/bin/keyhelp-php81 #!/usr/bin/keyhelp-php81
<?php declare(strict_types=1); <?php declare(strict_types=1);
namespace App\Controller;
error_reporting(error_level: E_ALL);
namespace App\Controller;
if (php_sapi_name() !== 'cli') { if (php_sapi_name() !== 'cli') {
exit; exit;
@ -94,7 +94,7 @@ function confirm(String $message = 'Are you sure? ', array $options = ['y', 'n']
echo '): '; echo '): ';
$handle = fopen(filename: "php://stdin", mode: 'r'); $handle = fopen(filename: "php://stdin", mode: 'r');
$line = trim(string: fgetc(stream: $handle)); $line = trim(fgetc(stream: $handle));
fclose(stream: $handle); fclose(stream: $handle);
if ($line == '') { if ($line == '') {

View File

@ -1,14 +1,13 @@
<?php declare(strict_types=1); <?php
namespace App\Controller; namespace App\Controller;
error_reporting(error_level: E_ALL); // 61eec33c8829a.91a1d27d70ba26710092381a073d9e546597dff6033de272
require dirname(path: __DIR__) . '/vendor/autoload.php'; require dirname(path: __DIR__) . '/vendor/autoload.php';
// read config // read config
$configFile = dirname(path: __DIR__) . "/config.json"; $configFile = dirname(path: __DIR__) . "/config.json";
$configJSON = file_get_contents(filename: $configFile); $configJSON = file_get_contents($configFile);
$config = json_decode(json: $configJSON, associative: true); $config = json_decode($configJSON, associative: true);
// TODO only valid clients? // TODO only valid clients?

View File

@ -1,7 +1,6 @@
<?php declare(strict_types=1); <?php declare(strict_types=1);
namespace App\Controller;
error_reporting(error_level: E_ALL); namespace App\Controller;
use Exception; use Exception;
use PDO; use PDO;

View File

@ -1,14 +1,12 @@
<?php declare(strict_types=1); <?php
namespace App\Controller; namespace App\Controller;
error_reporting(error_level: E_ALL); define('COLOR_RED', "\033[31m");
define('COLOR_GREEN', "\033[32m");
define('COLOR_YELLOW', "\033[33m");
define(constant_name: 'COLOR_RED', value: "\033[31m"); define('COLOR_BLUE', "\033[34m");
define(constant_name: 'COLOR_GREEN', value: "\033[32m"); define('COLOR_DEFAULT', "\033[39m");
define(constant_name: 'COLOR_YELLOW', value: "\033[33m");
define(constant_name: 'COLOR_BLUE', value: "\033[34m");
define(constant_name: 'COLOR_DEFAULT', value: "\033[39m");
// TODO add to all Controllers // TODO add to all Controllers
error_reporting(error_level: E_ALL); error_reporting(error_level: E_ALL);
@ -37,10 +35,10 @@ class BindAPI
public function __construct(private array $config, private int $argumentsCount, private array $arguments) public function __construct(private array $config, private int $argumentsCount, private array $arguments)
{ {
$this->databaseConnection = new DatabaseConnection(config: $this->config); $this->databaseConnection = new DatabaseConnection(config: $this->config);
$this->panelController = new PanelController(databaseConnection: $this->databaseConnection); $this->panelController = new PanelController($this->databaseConnection);
$this->apiUsers = new ApiKeys(databaseConnection: $this->databaseConnection); $this->apiUsers = new ApiKeys($this->databaseConnection);
$this->domainController = new DomainController(databaseConnection: $this->databaseConnection, panelController: $this->panelController); $this->domainController = new DomainController($this->databaseConnection, $this->panelController);
$this->nameserverController = new NameserverController(databaseConnection: $this->databaseConnection); $this->nameserverController = new NameserverController($this->databaseConnection);
$this->checkController = new CheckController(); $this->checkController = new CheckController();
} }
@ -63,57 +61,50 @@ class BindAPI
} }
/** /**
* @param array $server * @param bool|array $panel
* @param String $type
* *
* @return bool * @return bool
*/ */
public function checkPing(array $server, String $type): Bool public function checkNSPing(bool|array $nameserver): Bool
{ {
$maxName = $this->nameserverController->getLongestEntry(field: 'name');
$maxA = $this->nameserverController->getLongestEntry(field: 'a');
$maxAAAA = $this->nameserverController->getLongestEntry(field: 'aaaa');
$error = false; $error = false;
if ($type == 'nameserver') {
$maxName = $this->nameserverController->getLongestEntry(field: 'name');
$maxA = $this->nameserverController->getLongestEntry(field: 'a');
$maxAAAA = $this->nameserverController->getLongestEntry(field: 'aaaa');
} else {
$maxName = $this->panelController->getLongestEntry(field: 'name');
$maxA = $this->panelController->getLongestEntry(field: 'a');
$maxAAAA = $this->panelController->getLongestEntry(field: 'aaaa');
}
if ($this->config['verbose']) { if ($this->config['verbose']) {
print(COLOR_YELLOW . str_pad(string: $server['name'], length: $maxName)); print(COLOR_YELLOW . str_pad(string: $nameserver['name'], length: $maxName));
} }
$a = $server['a'] ?? ''; $a = $nameserver['a'] ?? '';
if (!empty($a)) { if (!empty($a)) {
if ($this->config['verbose']) { if ($this->config['verbose']) {
echo COLOR_DEFAULT . ' ' . str_pad(string: $a, length: $maxA, pad_type: STR_PAD_LEFT) . ' '; echo COLOR_DEFAULT . ' ' . str_pad(string: $a, length: $maxA, pad_type: STR_PAD_LEFT) . ' ';
} }
if ($result = $this->checkController->sendCommand(serverName: $server['name'], versionIP: 4, apiKey: $server['apikey'], command: 'ping', type: $type)) { if ($result = $this->checkController->sendCommand(serverName: $nameserver['name'], versionIP: 4, apiKey: $nameserver['apikey'], command: 'ping', type: 'nameserver')) {
if ($this->config['verbose']) { if ($this->config['verbose']) {
if ($result['data'] == 'pong') { if ($result == 'pong') {
echo COLOR_GREEN . $result['data']; echo COLOR_GREEN . $result;
} else { } else {
echo COLOR_BLUE . 'skip'; echo COLOR_BLUE . $result; // TODO 'skip';
} }
} }
} else { } else {
$error = true; $error = true;
} }
} }
$aaaa = $server['aaaa'] ?? ''; $aaaa = $nameserver['aaaa'] ?? '';
if (!empty($aaaa)) { if (!empty($aaaa)) {
if ($this->config['verbose']) { if ($this->config['verbose']) {
echo COLOR_DEFAULT . ' ' . str_pad(string: $aaaa, length: $maxAAAA, pad_type: STR_PAD_LEFT) . ' '; echo COLOR_DEFAULT . ' ' . str_pad(string: $aaaa, length: $maxAAAA, pad_type: STR_PAD_LEFT) . ' ';
} }
if ($result = $this->checkController->sendCommand(serverName: $server['name'], versionIP: 6, apiKey: $server['apikey'], command: 'ping', type: $type)) { if ($result = $this->checkController->sendCommand(serverName: $nameserver['name'], versionIP: 6, apiKey: $nameserver['apikey'], command: 'ping', type: 'nameserver')) {
if ($this->config['verbose']) { if ($this->config['verbose']) {
if ($result['data'] == 'pong') { if ($result == 'pong') {
echo COLOR_GREEN . $result['data']; echo COLOR_GREEN . $result;
} else { } else {
echo COLOR_BLUE . $result['data']; // TODO 'skip'; echo COLOR_BLUE . $result; // TODO 'skip';
} }
} }
} else { } else {
@ -127,6 +118,62 @@ class BindAPI
} }
/**
* @param bool|array $panel
*
* @return bool
*/
public function checkPanelPing(bool|array $panel): Bool
{
$maxName = $this->panelController->getLongestEntry(field: 'name');
$maxA = $this->panelController->getLongestEntry(field: 'a');
$maxAAAA = $this->panelController->getLongestEntry(field: 'aaaa');
$error = false;
if ($this->config['verbose']) {
print(COLOR_YELLOW . str_pad(string: $panel['name'], length: $maxName));
}
$a = $panel['a'] ?? '';
if (!empty($a)) {
if ($this->config['verbose']) {
echo COLOR_DEFAULT . ' ' . str_pad(string: $a, length: $maxA, pad_type: STR_PAD_LEFT) . ' ';
}
if ($result = $this->checkController->sendPanelCommand(serverName: $panel['name'], versionIP: 4, apiKey: $panel['apikey'], command: 'ping')) {
if ($this->config['verbose']) {
if ($result == 'pong') {
echo COLOR_GREEN . $result;
} else {
echo COLOR_BLUE . 'skip';
}
}
} else {
$error = true;
}
}
$aaaa = $panel['aaaa'] ?? '';
if (!empty($aaaa)) {
if ($this->config['verbose']) {
echo COLOR_DEFAULT . ' ' . str_pad(string: $aaaa, length: $maxAAAA, pad_type: STR_PAD_LEFT) . ' ';
}
if ($result = $this->checkController->sendPanelCommand(serverName: $panel['name'], versionIP: 6, apiKey: $panel['apikey'], command: 'ping')) {
if ($this->config['verbose']) {
if ($result == 'pong') {
echo COLOR_GREEN . $result;
} else {
echo COLOR_BLUE . 'skip';
}
}
} else {
$error = true;
}
}
if ($this->config['verbose']) {
echo PHP_EOL;
}
return $error;
}
function handleChecks(String $subcommand) function handleChecks(String $subcommand)
@ -150,8 +197,10 @@ class BindAPI
function checkNS(String $domain) function checkNS(String $domain)
{ {
$nameServers = $this->nameserverController->findAll(); $nameServers = $this->nameserverController->findAll();
$i = 1;
foreach($nameServers as $nameServer) { foreach($nameServers as $nameServer) {
echo COLOR_DEFAULT . ' ' . $nameServer['name']; print("nameserver $i");
$i++;
if (!empty($nameServer['aaaa'])) { if (!empty($nameServer['aaaa'])) {
$result = $this->checkController->sendCommand(serverName: $nameServer['name'], $result = $this->checkController->sendCommand(serverName: $nameServer['name'],
versionIP: 6, versionIP: 6,
@ -166,9 +215,9 @@ class BindAPI
type: 'nameserver' . $domain); type: 'nameserver' . $domain);
} }
if ($result['header'] == 200) { if ($result['header'] == 200) {
return true; print("OK");
} else { } else {
return false; print("missing");
} }
} }
echo PHP_EOL; echo PHP_EOL;
@ -178,10 +227,13 @@ class BindAPI
function handleCheckPanels() function handleCheckPanels()
{ {
$id = $this->getId(); $id = $this->getId();
print("id: $id " . PHP_EOL); $maxDomain = $this->panelController->getLongestEntry(field:'name');
print("$maxDomain");
die();
if ($id != 0) { if ($id != 0) {
if ($panel = $this->panelController->findByID(id: $id)) { if ($panel = $this->panelController->findByID($id)) {
if (!empty($panel['aaaa'])) { if (!empty($panel['aaaa'])) {
$result = $this->checkController->sendCommand(serverName: $panel['name'], $result = $this->checkController->sendCommand(serverName: $panel['name'],
versionIP: 6, versionIP: 6,
@ -196,19 +248,11 @@ class BindAPI
type: 'panel'); type: 'panel');
} }
$domains = json_decode(json: $result['data']); $domains = json_decode($result['data']);
$maxDomainName = 0;
// TODO this is ugly code
foreach ($domains as $domain) {
if (($domain->id_parent_domain == 0 && !str_contains(haystack: $domain->domain, needle: $panel['name'])) && (strlen(string: $domain->domain) > $maxDomainName)) {
$maxDomainName = strlen(string: $domain->domain);
}
}
foreach ($domains as $domain) { foreach ($domains as $domain) {
if ($domain->id_parent_domain == 0 && !str_contains(haystack: $domain->domain, needle: $panel['name'])) { if ($domain->id_parent_domain == 0 && !str_contains(haystack: $domain->domain, needle: $panel['name'])) {
echo PHP_EOL . COLOR_DEFAULT . "check: " . COLOR_YELLOW . str_pad(string: $domain->domain, length: $maxDomainName); echo PHP_EOL . COLOR_DEFAULT . "check: " . COLOR_YELLOW . str_pad($domain->domain, $maxDomain);
if ($this->checkNS(domain: $domain->domain)) { if ($this->checkNS($domain->domain)) {
echo COLOR_GREEN . ' OK'; echo COLOR_GREEN . ' OK';
} else { } else {
echo COLOR_RED . 'Missing'; echo COLOR_RED . 'Missing';
@ -219,8 +263,6 @@ class BindAPI
echo "Unknown Panel ID: $id" . PHP_EOL; echo "Unknown Panel ID: $id" . PHP_EOL;
exit(1); exit(1);
} }
} else {
echo "check all …" . PHP_EOL;
} }
} }
@ -247,11 +289,11 @@ class BindAPI
try { try {
match ($command) { match ($command) {
'check' => $this->handleChecks(subcommand: $subcommand), 'check' => $this->handleChecks($subcommand),
'panels' => $this->handlePanels(subcommand: $subcommand), 'panels' => $this->handlePanels($subcommand),
'apikeys' => $this->handleApiKeys(subcommand: $subcommand), 'apikeys' => $this->handleApiKeys($subcommand),
'domains' => $this->handleDomains(subcommand: $subcommand), 'domains' => $this->handleDomains($subcommand),
'nameservers' => $this->handleNameservers(subcommand: $subcommand) 'nameservers' => $this->handleNameservers($subcommand)
}; };
} catch(UnhandledMatchError) { } catch(UnhandledMatchError) {
echo 'Unknown command: ' . $command . PHP_EOL; echo 'Unknown command: ' . $command . PHP_EOL;
@ -319,7 +361,8 @@ class BindAPI
'list' => $this->handleNameserversList(), 'list' => $this->handleNameserversList(),
'update' => $this->handleNameserversUpdate(), 'update' => $this->handleNameserversUpdate(),
'delete' => $this->handleNameserversDelete(), 'delete' => $this->handleNameserversDelete(),
'apiping' => $this->handleAPIPing(type: 'nameserver') 'apiping' => $this->handleNameserversAPIPing(),
}; };
} catch (UnhandledMatchError) { } catch (UnhandledMatchError) {
echo 'Unknown action: ' . $subcommand . PHP_EOL; echo 'Unknown action: ' . $subcommand . PHP_EOL;
@ -336,22 +379,18 @@ class BindAPI
{ {
try { try {
match ($subcommand) { match ($subcommand) {
'apiping' => $this->handlePanelsAPIPing(),
'create' => $this->handlePanelsCreate(), 'create' => $this->handlePanelsCreate(),
'list' => $this->handlePanelsList(), 'list' => $this->handlePanelsList(),
'update' => $this->handlePanelsUpdate(), 'update' => $this->handlePanelsUpdate(),
'delete' => $this->handlePanelsDelete(), 'delete' => $this->handlePanelsDelete(),
'apiping' => $this->handleAPIPing(type: 'panel')
}; };
} catch (UnhandledMatchError) { } catch (UnhandledMatchError) {
echo 'Unknown action: ' . $subcommand . PHP_EOL; echo 'Unknown action: ' . $subcommand . PHP_EOL;
} }
} }
/*
*
* TODO remove
*/
/*
function handleNameserversAPIPing() function handleNameserversAPIPing()
{ {
// TODO get apikey from stdin // TODO get apikey from stdin
@ -372,8 +411,8 @@ class BindAPI
} }
if ($id != 0) { if ($id != 0) {
if ($nameserver = $this->nameserverController->findByID(id: $id)) { if ($nameserver = $this->nameserverController->findByID($id)) {
if (!$this->checkPing(server: $nameserver, type: 'nameserver')) { if (!$this->checkNSPing($nameserver)) {
$error = true; $error = true;
} }
} else { } else {
@ -385,7 +424,7 @@ class BindAPI
} else { } else {
$nameservers = $this->nameserverController->findAll(); $nameservers = $this->nameserverController->findAll();
foreach ($nameservers as $nameserver) { foreach ($nameservers as $nameserver) {
if (!$this->checkPing(server: $nameserver, type: 'nameserver')) { if (!$this->checkNSPing(nameserver: $nameserver)) {
$error = true; $error = true;
} }
} }
@ -397,40 +436,37 @@ class BindAPI
exit(0); exit(0);
} }
} }
*/
function handleAPIPing(String $type)
function handlePanelsAPIPing()
{ {
$error = false; $error = false;
$id = $this->getId(); if (!empty($this->arguments[1])) {
$id = intval(value: $this->arguments[1] ?? 0);
if ($id != 0) { if ($id != $this->arguments[1]) {
if ($type == 'panel') { echo 'ID has to be a number.' . PHP_EOL;
$server = $this->panelController->findByID(id: $id); exit(1);
} else {
$server = $this->nameserverController->findByID(id: $id);
} }
} else {
if ($server) { $id = 0;
if (!$this->checkPing(server: $server, type: $type)) { }
if ($id != 0) {
if ($panel = $this->panelController->findByID($id)) {
if (!$this->checkPanelPing($panel)) {
$error = true; $error = true;
} }
} else { } else {
if ($this->config['verbose']) { if ($this->config['verbose']) {
echo "Unknown $type ID: $id" . PHP_EOL; echo 'Unknown panel ID: $id' . PHP_EOL;
} }
$error = true; $error = true;
} }
} else { } else {
if ($type == 'panel') { $panels = $this->panelController->findAll();
$servers = $this->panelController->findAll(); foreach ($panels as $panel) {
} else { if (!$this->checkPanelPing(panel: $panel)) {
$servers = $this->nameserverController->findAll();
}
foreach ($servers as $server) {
if (!$this->checkPing(server: $server, type: $type)) {
$error = true; $error = true;
} }
} }
@ -473,11 +509,11 @@ class BindAPI
} }
$apikey = $arguments['apikey'] ?? ''; $apikey = $arguments['apikey'] ?? '';
if ($this->panelController->findByName(name: $name)) { if ($this->panelController->findByName($name)) {
echo "Panel: $name already exists." . PHP_EOL; echo "Panel: $name already exists." . PHP_EOL;
exit(1); exit(1);
} else { } else {
$result = $this->panelController->insert(name: $name, a: $a, aaaa: $aaaa, apikey: $apikey); $result = $this->panelController->insert($name, $a, $aaaa, $apikey);
echo "Panel $name has been created with id $result" . PHP_EOL; echo "Panel $name has been created with id $result" . PHP_EOL;
exit(0); exit(0);
} }
@ -513,11 +549,11 @@ class BindAPI
} }
$apikey = $argiments['apikey'] ?? ''; $apikey = $argiments['apikey'] ?? '';
if ($this->nameserverController->findByName(name: $name)) { if ($this->nameserverController->findByName($name)) {
echo "Nameserver: $name already exists." . PHP_EOL; echo "Nameserver: $name already exists." . PHP_EOL;
exit(1); exit(1);
} else { } else {
$result = $this->nameserverController->insert(name: $name, a: $a, aaaa: $aaaa, apikey: $apikey); $result = $this->nameserverController->insert($name, $a, $aaaa, $apikey);
echo "Nameserver $name has been created with id $result" . PHP_EOL; echo "Nameserver $name has been created with id $result" . PHP_EOL;
exit(0); exit(0);
} }
@ -533,7 +569,7 @@ class BindAPI
foreach ($this->arguments as $argument) { foreach ($this->arguments as $argument) {
if (str_contains(haystack: $argument, needle: '=')) { if (str_contains(haystack: $argument, needle: '=')) {
[$key, $value] = explode(separator: '=', string: $argument); [$key, $value] = explode(separator: '=', string: $argument);
$arguments[strtolower(string: $key)] = $value; $arguments[strtolower($key)] = $value;
} }
} }
return $arguments; return $arguments;
@ -548,10 +584,10 @@ class BindAPI
$panels = $this->panelController->findAll(); $panels = $this->panelController->findAll();
if ($panels) { if ($panels) {
$table = new ConsoleTable(); $table = new ConsoleTable();
$table->setHeaders(content: ['ID', 'Name', 'A', 'AAAA', 'API Key']); $table->setHeaders(['ID', 'Name', 'A', 'AAAA', 'API Key']);
foreach ($panels as $panel) { foreach ($panels as $panel) {
$token = strtok(string: $panel['apikey'], token: '.'); $token = strtok(string: $panel['apikey'], token: '.');
$table->addRow(data: [$panel['id'], $panel['name'], $panel['a'], $panel['aaaa'], $token]); $table->addRow([$panel['id'], $panel['name'], $panel['a'], $panel['aaaa'], $token]);
} }
$table->setPadding(value: 2); $table->setPadding(value: 2);
$table->display(); $table->display();
@ -576,11 +612,11 @@ class BindAPI
echo 'An ID is required' . PHP_EOL; echo 'An ID is required' . PHP_EOL;
exit(1); exit(1);
} }
if (!$this->panelController->findByID(id: $id)) { if (!$this->panelController->findByID($id)) {
echo "Panel with ID : $id doesn't exist." . PHP_EOL; echo "Panel with ID : $id doesn't exist." . PHP_EOL;
exit(1); exit(1);
} }
if ($this->panelController->update(id: $id, name: $name, a: $a, aaaa: $aaaa, apikey: $apikey) !== false) { if ($this->panelController->update($id, $name, $a, $aaaa, $apikey) !== false) {
echo 'Panel has been updated' . PHP_EOL; echo 'Panel has been updated' . PHP_EOL;
} else { } else {
echo 'Error while updating domain server.' . PHP_EOL; echo 'Error while updating domain server.' . PHP_EOL;
@ -594,20 +630,19 @@ class BindAPI
exit(1); exit(1);
} }
$id = intval(value: $this->arguments[1]) ?? 0; $id = intval($this->arguments[1]) ?? 0;
if ($id == 0) { if ($id == 0) {
echo "Panel with ID $id not found." . PHP_EOL; echo "Panel with ID $id not found." . PHP_EOL;
exit(1); exit(1);
} }
if (!$this->panelController->findByID(id: $id)) { if (!$this->panelController->findByID($id)) {
echo "There is no panel with ID $id." . PHP_EOL; echo "There is no panel with ID $id." . PHP_EOL;
exit(1); exit(1);
} }
$this->panelController->delete(id: $id); $this->panelController->delete($id);
echo "The panel with ID $id has been deleted." .PHP_EOL; echo "The panel with ID $id has been deleted." .PHP_EOL;
} }
/** /**
* @param string $subcommand * @param string $subcommand
* *
@ -649,9 +684,9 @@ class BindAPI
$keys = $this->apiUsers->findAll(); $keys = $this->apiUsers->findAll();
if ($keys) { if ($keys) {
$table = new ConsoleTable(); $table = new ConsoleTable();
$table->setHeaders(content: ['ID', 'Name', 'API key prefix']); $table->setHeaders(['ID', 'Name', 'API key prefix']);
foreach ($keys as $key) { foreach ($keys as $key) {
$table->addRow(data: [$key['id'], $key['name'], $key['api_token_prefix']]); $table->addRow([$key['id'], $key['name'], $key['api_token_prefix']]);
} }
$table->setPadding(value: 2); $table->setPadding(value: 2);
$table->display(); $table->display();
@ -671,8 +706,8 @@ class BindAPI
echo 'You need to add the ID of the API key.' . PHP_EOL; echo 'You need to add the ID of the API key.' . PHP_EOL;
exit(1); exit(1);
} }
if ($this->apiUsers->findByID(id: $id)) { if ($this->apiUsers->findByID($id)) {
$this->apiUsers->delete(id: $id); $this->apiUsers->delete($id);
echo 'API key ' . $id . ' has been deleted.' . PHP_EOL; echo 'API key ' . $id . ' has been deleted.' . PHP_EOL;
exit(0); exit(0);
} else { } else {
@ -681,7 +716,6 @@ class BindAPI
} }
} }
/** /**
* @param string $subcommand * @param string $subcommand
* *
@ -711,10 +745,10 @@ class BindAPI
$domains = $this->nameserverController->findAll(); $domains = $this->nameserverController->findAll();
if ($domains) { if ($domains) {
$table = new ConsoleTable(); $table = new ConsoleTable();
$table->setHeaders(content: ['ID', 'Name', 'A', 'AAAA', 'API Key']); $table->setHeaders(['ID', 'Name', 'A', 'AAAA', 'API Key']);
foreach ($domains as $domain) { foreach ($domains as $domain) {
$token = strtok(string: $domain['apikey'], token: '.'); $token = strtok(string: $domain['apikey'], token: '.');
$table->addRow(data: [$domain['id'], $domain['name'], $domain['a'], $domain['aaaa'], $token]); $table->addRow([$domain['id'], $domain['name'], $domain['a'], $domain['aaaa'], $token]);
} }
$table->setPadding(value: 2); $table->setPadding(value: 2);
$table->display(); $table->display();
@ -735,9 +769,9 @@ class BindAPI
$domains = $this->domainController->findAll(); $domains = $this->domainController->findAll();
if ($domains) { if ($domains) {
$table = new ConsoleTable(); $table = new ConsoleTable();
$table->setHeaders(content: ['ID', 'Name', 'Panel', 'A', 'AAAA']); $table->setHeaders(['ID', 'Name', 'Panel', 'A', 'AAAA']);
foreach ($domains as $domain) { foreach ($domains as $domain) {
$table->addRow(data: [$domain['id'], $domain['name'], $domain['panel_id'], $domain['a'], $domain['aaaa']]); $table->addRow([$domain['id'], $domain['name'], $domain['panel_id'], $domain['a'], $domain['aaaa']]);
} }
$table->setPadding(value: 2); $table->setPadding(value: 2);
$table->display(); $table->display();
@ -778,11 +812,11 @@ class BindAPI
exit(0); exit(0);
} }
if ($this->domainController->findByName(name: $name)) { if ($this->domainController->findByName($name)) {
echo "Domain: $name already exists." . PHP_EOL; echo "Domain: $name already exists." . PHP_EOL;
exit(1); exit(1);
} else { } else {
$result = $this->domainController->insert(name: $name, panelID: $panelID, a: $a, aaaa: $aaaa); $result = $this->domainController->insert($name, $panelID, $a, $aaaa);
echo "Domain $name has been created with id $result" . PHP_EOL; echo "Domain $name has been created with id $result" . PHP_EOL;
exit(0); exit(0);
} }
@ -792,10 +826,9 @@ class BindAPI
{ {
$arguments = $this->parseArguments(); $arguments = $this->parseArguments();
$id = intval(value: $this->arguments[1] ?? 0); $id = $this->arguments[1] ?? 0;
$name = $arguments['name'] ?? ''; $name = $arguments['name'] ?? '';
print_r(value: $arguments); //findme print_r($arguments); //findme
print("$id: id" . PHP_EOL);
$panelID = intval(value: $arguments['panel_id'] ?? 0); $panelID = intval(value: $arguments['panel_id'] ?? 0);
$a = $arguments['a'] ?? ''; $a = $arguments['a'] ?? '';
$aaaa = $arguments['aaaa'] ?? ''; $aaaa = $arguments['aaaa'] ?? '';
@ -804,11 +837,11 @@ class BindAPI
echo 'An ID is required' . PHP_EOL; echo 'An ID is required' . PHP_EOL;
exit(1); exit(1);
} }
if (!$this->domainController->findByID(id: $id)) { if (!$this->domainController->findByID($id)) {
echo "Domain with ID : $id doesn't exist." . PHP_EOL; echo "Domain with ID : $id doesn't exist." . PHP_EOL;
exit(1); exit(1);
} }
if ($this->domainController->update(id: $id, name: $name, panelID: $panelID, a: $a, aaaa: $aaaa) !== false) { if ($this->domainController->update($id, $name, $panelID, $a, $aaaa) !== false) {
echo 'Domain server has been updated' . PHP_EOL; echo 'Domain server has been updated' . PHP_EOL;
} else { } else {
echo 'Error while updating domain server.' . PHP_EOL; echo 'Error while updating domain server.' . PHP_EOL;
@ -816,6 +849,7 @@ class BindAPI
} }
function handleNameserversUpdate() function handleNameserversUpdate()
{ {
$arguments = $this->parseArguments(); $arguments = $this->parseArguments();
@ -830,11 +864,11 @@ class BindAPI
echo 'An ID is required' . PHP_EOL; echo 'An ID is required' . PHP_EOL;
exit(1); exit(1);
} }
if (!$this->nameserverController->findByID(id: $id)) { if (!$this->nameserverController->findByID($id)) {
echo "Nameserver with ID : $id doesn't exist." . PHP_EOL; echo "Nameserver with ID : $id doesn't exist." . PHP_EOL;
exit(1); exit(1);
} }
if ($this->nameserverController->update(id: $id, name: $name, a: $a, aaaa: $aaaa, apikey: $apikey) !== false) { if ($this->nameserverController->update($id, $name, $a, $aaaa, $apikey) !== false) {
echo 'Nameserver server has been updated' . PHP_EOL; echo 'Nameserver server has been updated' . PHP_EOL;
} else { } else {
echo 'Error while updating nameserver.' . PHP_EOL; echo 'Error while updating nameserver.' . PHP_EOL;
@ -848,16 +882,16 @@ class BindAPI
exit(1); exit(1);
} }
$id = intval(value: $this->arguments[1]) ?? 0; $id = intval($this->arguments[1]) ?? 0;
if ($id == 0) { if ($id == 0) {
echo "Nameserver with ID $id not found." . PHP_EOL; echo "Nameserver with ID $id not found." . PHP_EOL;
exit(1); exit(1);
} }
if (!$this->nameserverController->findByID(id: $id)) { if (!$this->nameserverController->findByID($id)) {
echo "There is no nameserver with ID $id." . PHP_EOL; echo "There is no nameserver with ID $id." . PHP_EOL;
exit(1); exit(1);
} }
$this->nameserverController->delete(id: $id); $this->nameserverController->delete($id);
echo "The nameserver with ID $id has been deleted." .PHP_EOL; echo "The nameserver with ID $id has been deleted." .PHP_EOL;
} }
@ -869,16 +903,16 @@ function handleDomainsDelete()
exit(1); exit(1);
} }
$id = intval(value: $this->arguments[1]) ?? 0; $id = intval($this->arguments[1]) ?? 0;
if ($id == 0) { if ($id == 0) {
echo "Domain with ID $id not found." . PHP_EOL; echo "Domain with ID $id not found." . PHP_EOL;
exit(1); exit(1);
} }
if (!$this->domainController->findByID(id: $id)) { if (!$this->domainController->findByID($id)) {
echo "There is no domain with ID $id." . PHP_EOL; echo "There is no domain with ID $id." . PHP_EOL;
exit(1); exit(1);
} }
$this->domainController->delete(id: $id); $this->domainController->delete($id);
echo "The domain with ID $id has been deleted." .PHP_EOL; echo "The domain with ID $id has been deleted." .PHP_EOL;
} }
} }

View File

@ -1,7 +1,6 @@
<?php declare(strict_types=1); <?php
namespace App\Controller;
error_reporting(error_level: E_ALL); namespace App\Controller;
/** /**
@ -23,27 +22,27 @@ class CheckController
{ {
$curl = curl_init(); $curl = curl_init();
if ($type == "panel") { if ($type == "panel") {
curl_setopt(handle: $curl, option: CURLOPT_URL, value: "https://$serverName/api/v2/" . $command); curl_setopt($curl, option: CURLOPT_URL, value: "https://$serverName/api/v2/" . $command);
} else { } else {
curl_setopt(handle: $curl, option: CURLOPT_URL, value: "https://$serverName/api/" . $command); curl_setopt($curl, option: CURLOPT_URL, value: "https://$serverName/api/" . $command);
} }
curl_setopt(handle: $curl, option: CURLOPT_RETURNTRANSFER, value: 1); curl_setopt($curl, option: CURLOPT_RETURNTRANSFER, value: 1);
curl_setopt(handle: $curl, option: CURLOPT_TIMEOUT_MS, value: 1000); curl_setopt($curl, option: CURLOPT_TIMEOUT_MS, value: 1000);
if ($versionIP == 4) { if ($versionIP == 4) {
curl_setopt(handle: $curl, option: CURLOPT_IPRESOLVE, value: CURL_IPRESOLVE_V4); curl_setopt($curl, option: CURLOPT_IPRESOLVE, value: CURL_IPRESOLVE_V4);
} else { } else {
curl_setopt(handle: $curl, option: CURLOPT_IPRESOLVE, value: CURL_IPRESOLVE_V6); curl_setopt($curl, option: CURLOPT_IPRESOLVE, value: CURL_IPRESOLVE_V6);
} }
curl_setopt(handle: $curl, option: CURLOPT_HTTPHEADER, value: ["X-API-Key:$apiKey"]); curl_setopt($curl, option: CURLOPT_HTTPHEADER, value: ["X-API-Key:$apiKey"]);
if ($resultJSON = curl_exec(handle: $curl)) { if ($resultJSON = curl_exec($curl)) {
$httpResponse = curl_getinfo(handle: $curl)['http_code']; $httpResponse = curl_getinfo($curl)['http_code'];
switch($httpResponse) { switch($httpResponse) {
case 200: case 200:
$apiResult = json_decode(json: $resultJSON); $apiResult = json_decode($resultJSON);
if ($command == "ping" ) { if ($command == "ping" ) {
if ($apiResult->response == "pong") { if ($apiResult->response == "pong") {
$result = $apiResult->response; $result = $apiResult->response;
@ -61,9 +60,9 @@ class CheckController
$result = 'Unhandled error: ' . $httpResponse; $result = 'Unhandled error: ' . $httpResponse;
} }
} else { } else {
$result = curl_error(handle: $curl); $result = curl_error($curl);
} }
curl_close(handle: $curl); curl_close($curl);
return [ return [
'data' => $result, 'data' => $result,
'header' => $httpResponse ?? '' 'header' => $httpResponse ?? ''

View File

@ -1,9 +1,7 @@
<?php declare(strict_types=1); <?php
namespace App\Controller; namespace App\Controller;
error_reporting(error_level: E_ALL);
use PDO; use PDO;
use PDOException; use PDOException;
@ -22,7 +20,7 @@ class DatabaseConnection
public function __construct(private array $config) public function __construct(private array $config)
{ {
extract(array: $this->config); extract($this->config);
try { try {
$this->dbConnection = new PDO( $this->dbConnection = new PDO(
@ -31,13 +29,13 @@ class DatabaseConnection
password: $dbPassword password: $dbPassword
); );
$sql = "SHOW TABLES"; $sql = "SHOW TABLES";
$statement = $this->dbConnection->prepare(query: $sql); $statement = $this->dbConnection->prepare($sql);
$statement->execute(); $statement->execute();
$result = $statement->fetch(); $result = $statement->fetch();
if (empty($result)) { if (empty($result)) {
// 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('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,
@ -46,7 +44,7 @@ class DatabaseConnection
`api_token` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, `api_token` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
PRIMARY KEY (`id`) PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci"; ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
$statement = $this->dbConnection->prepare(query: $sql); $statement = $this->dbConnection->prepare($sql);
$statement->execute(); $statement->execute();
$sql = " $sql = "
@ -58,7 +56,7 @@ class DatabaseConnection
`aaaa` varbinary(255) DEFAULT NULL, `aaaa` varbinary(255) DEFAULT NULL,
PRIMARY KEY (`id`) PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci"; ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
$statement = $this->dbConnection->prepare(query: $sql); $statement = $this->dbConnection->prepare($sql);
$statement->execute(); $statement->execute();
$sql = " $sql = "
@ -70,7 +68,7 @@ class DatabaseConnection
`apikey` varbinary(255) DEFAULT NULL, `apikey` varbinary(255) DEFAULT NULL,
PRIMARY KEY (`id`) PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci"; ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
$statement = $this->dbConnection->prepare(query: $sql); $statement = $this->dbConnection->prepare($sql);
$statement->execute(); $statement->execute();
$sql = " $sql = "
@ -82,7 +80,7 @@ class DatabaseConnection
`apikey` varbinary(255) DEFAULT NULL, `apikey` varbinary(255) DEFAULT NULL,
PRIMARY KEY (`id`) PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci"; ) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
$statement = $this->dbConnection->prepare(query: $sql); $statement = $this->dbConnection->prepare($sql);
$statement->execute(); $statement->execute();
echo 'Tables have been created.' . PHP_EOL; echo 'Tables have been created.' . PHP_EOL;

View File

@ -1,9 +1,7 @@
<?php declare(strict_types=1); <?php
namespace App\Controller; namespace App\Controller;
error_reporting(error_level: E_ALL);
use PDO; use PDO;
use PDOException; use PDOException;
@ -31,14 +29,13 @@ class DomainController
*/ */
public function findAll(): bool|array public function findAll(): bool|array
{ {
$sql = " $statement = "
SELECT id, name, panel_id, a, aaaa SELECT id, name, panel_id, a, aaaa
FROM " . DatabaseConnection::TABLE_DOMAINS . " FROM " . DatabaseConnection::TABLE_DOMAINS . "
ORDER BY name"; ORDER BY name";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->query($statement);
$statement->execute();
return $statement->fetchAll(mode: PDO::FETCH_ASSOC); return $statement->fetchAll(mode: PDO::FETCH_ASSOC);
} catch (PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
@ -59,10 +56,10 @@ class DomainController
WHERE name = :name"; WHERE name = :name";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare($sql);
$statement->bindParam(param: ':name', var: $name); $statement->bindParam(param: ':name', var: $name);
$statement->execute(); $statement->execute();
return $statement->fetch(mode: PDO::FETCH_ASSOC); return $statement->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
} }
@ -82,10 +79,10 @@ class DomainController
WHERE id = :id"; WHERE id = :id";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare($sql);
$statement->bindParam(param:':id', var: $id); $statement->bindParam(param:':id', var: $id);
$statement->execute(); $statement->execute();
return $statement->fetch(mode: PDO::FETCH_ASSOC); return $statement->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
} }
@ -98,9 +95,9 @@ class DomainController
* @param String $a * @param String $a
* @param String $aaaa * @param String $aaaa
* *
* @return string|false * @return int
*/ */
public function insert(String $name, int $panelID, String $a, String $aaaa): bool|string public function insert(String $name, int $panelID, String $a, String $aaaa): int
{ {
// TODO create zone file and include // TODO create zone file and include
$sql = " $sql = "
@ -108,7 +105,7 @@ class DomainController
VALUES (:name, :panel_id, :a, :aaaa)"; VALUES (:name, :panel_id, :a, :aaaa)";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare($sql);
$statement->bindParam(param: ':name', var: $name); $statement->bindParam(param: ':name', var: $name);
$statement->bindParam(param: ':panel_d', var: $panelID); $statement->bindParam(param: ':panel_d', var: $panelID);
$statement->bindParam(param: ':a', var: $a); $statement->bindParam(param: ':a', var: $a);
@ -116,7 +113,7 @@ class DomainController
$statement->execute(); $statement->execute();
if ($panel = $this->panelController->findByID(id: $panelID)) { if ($panel = $this->panelController->findByID($panelID)) {
$a = $panel['a']; $a = $panel['a'];
$aaaa = $panel['aaaa']; $aaaa = $panel['aaaa'];
} }
@ -124,9 +121,9 @@ class DomainController
$zoneFilename = $this->localZonesDir . $name; $zoneFilename = $this->localZonesDir . $name;
echo $zoneFilename . PHP_EOL; echo $zoneFilename . PHP_EOL;
if ($localZones = fopen(filename: $this->localZoneFile, mode: 'a')) { if ($localZones = fopen($this->localZoneFile, mode: 'a')) {
fputs(stream: $localZones, data: "include \"$zoneFilename\";" . PHP_EOL); fputs($localZones, data: "include \"$zoneFilename\";" . PHP_EOL);
fclose(stream: $localZones); fclose($localZones);
} else { } else {
echo "Error writing to $this->localZoneFile, check permissions"; echo "Error writing to $this->localZoneFile, check permissions";
exit(1); exit(1);
@ -150,7 +147,7 @@ class DomainController
*/ */
public function update(Int $id, String $name, int $panelID, String $a, String $aaaa): bool|int public function update(Int $id, String $name, int $panelID, String $a, String $aaaa): bool|int
{ {
$current = $this->findByID(id: $id); $current = $this->findByID($id);
/* doesn't work /* doesn't work
$statement = " $statement = "
@ -185,7 +182,7 @@ class DomainController
WHERE id = :id"; WHERE id = :id";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare($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: 'panel_id', var: $panelID); $statement->bindParam(param: 'panel_id', var: $panelID);
@ -194,7 +191,7 @@ class DomainController
$statement->execute(); $statement->execute();
// recreate zonefile // recreate zonefile
if ($panel = $this->panelController->findByID(id: intval(value: $panelID))) { if ($panel = $this->panelController->findByID($panelID)) {
$a = $panel['a']; $a = $panel['a'];
$aaaa = $panel['aaaa']; $aaaa = $panel['aaaa'];
} }
@ -217,12 +214,12 @@ class DomainController
public function delete($id): int public function delete($id): int
{ {
// TODO delete zone file and include // TODO delete zone file and include
$sql = " $statement = "
DELETE FROM " . DatabaseConnection::TABLE_DOMAINS . " DELETE FROM " . DatabaseConnection::TABLE_DOMAINS . "
WHERE id = :id"; WHERE id = :id";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare($statement);
$statement->bindParam(param: 'id', var: $id); $statement->bindParam(param: 'id', var: $id);
$statement->execute(); $statement->execute();
return $statement->rowCount(); return $statement->rowCount();
@ -238,11 +235,11 @@ class DomainController
*/ */
public function getLongestEntry(String $field): int public function getLongestEntry(String $field): int
{ {
$sql = " $statement = "
SELECT MAX(LENGTH(" . $field . ")) as length FROM " . DatabaseConnection::TABLE_DOMAINS; SELECT MAX(LENGTH(" . $field . ")) as length FROM " . DatabaseConnection::TABLE_DOMAINS;
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare($statement);
$statement->execute(); $statement->execute();
$result = $statement->fetch(); $result = $statement->fetch();
return $result['length']; return $result['length'];
@ -260,7 +257,7 @@ class DomainController
$uid = posix_geteuid(); $uid = posix_geteuid();
print("UID:\t$uid" . PHP_EOL); print("UID:\t$uid" . PHP_EOL);
$pwuid = posix_getpwuid(user_id: $uid); $pwuid = posix_getpwuid($uid);
$name = $pwuid['name']; $name = $pwuid['name'];
print("Name:\t$name" . PHP_EOL); print("Name:\t$name" . PHP_EOL);
$bindGroup = posix_getgrnam(name: 'bind'); $bindGroup = posix_getgrnam(name: 'bind');
@ -278,8 +275,8 @@ class DomainController
} }
echo "Checking $this->namedConfLocalFile" . PHP_EOL; echo "Checking $this->namedConfLocalFile" . PHP_EOL;
if ($namedConfLocal = file_get_contents(filename: $this->namedConfLocalFile)) { if ($namedConfLocal = file_get_contents($this->namedConfLocalFile)) {
if (!str_contains(haystack: $namedConfLocal, needle: $this->localZoneFile)) { if (!str_contains($namedConfLocal, $this->localZoneFile)) {
echo "\t$this->localZoneFile needs to be included in $this->namedConfLocalFile." . PHP_EOL; echo "\t$this->localZoneFile needs to be included in $this->namedConfLocalFile." . PHP_EOL;
} else { } else {
echo "\t$this->localZoneFile is included in $this->namedConfLocalFile" . PHP_EOL; echo "\t$this->localZoneFile is included in $this->namedConfLocalFile" . PHP_EOL;
@ -303,12 +300,10 @@ class DomainController
*/ */
function checkDomains(): array|bool function checkDomains(): array|bool
{ {
return true;
/*
$domains = $this->findAll(); $domains = $this->findAll();
if ($namedConfLocal = file_get_contents(filename: $this->namedConfLocalFile)) { if ($namedConfLocal = file_get_contents($this->namedConfLocalFile)) {
if (!str_contains(haystack: $namedConfLocal, needle: $this->localZoneFile)) { if (!str_contains($namedConfLocal, $this->localZoneFile)) {
return "$this->localZoneFile needs to be included in $this->namedConfLocalFile."; return "$this->localZoneFile needs to be included in $this->namedConfLocalFile.";
} }
} else { } else {
@ -338,7 +333,6 @@ class DomainController
} else { } else {
return $errors; return $errors;
} }
*/
} }
@ -352,18 +346,18 @@ class DomainController
public function createZoneFile(String $name, String $a, String $aaaa): void public function createZoneFile(String $name, String $a, String $aaaa): void
{ {
if ($zonefile = fopen(filename: $this->localZonesDir . $name, mode: 'w')) { if ($zonefile = fopen(filename: $this->localZonesDir . $name, mode: 'w')) {
fputs(stream: $zonefile, data: "zone \"$name\" IN {" . PHP_EOL); fputs($zonefile, data: "zone \"$name\" IN {" . PHP_EOL);
fputs(stream: $zonefile, data: "\ttype slave;" . PHP_EOL); fputs($zonefile, data: "\ttype slave;" . PHP_EOL);
fputs(stream: $zonefile, data: "\tfile \"" . $this->zoneCachePath . $name . '.db";' . PHP_EOL); fputs($zonefile, data: "\tfile \"" . $this->zoneCachePath . $name . '.db";' . PHP_EOL);
fputs(stream: $zonefile, data: "\tmasters {" . PHP_EOL); fputs($zonefile, data: "\tmasters {" . PHP_EOL);
if (!empty($a)) { if (!empty($a)) {
fputs(stream: $zonefile, data: "\t\t$a;" . PHP_EOL); fputs($zonefile, data: "\t\t$a;" . PHP_EOL);
} }
if (!empty($aaaa)) { if (!empty($aaaa)) {
fputs(stream: $zonefile, data: "\t\t$aaaa;" . PHP_EOL); fputs($zonefile, data: "\t\t$aaaa;" . PHP_EOL);
} }
fputs(stream: $zonefile, data: "\t};" . PHP_EOL); fputs($zonefile, data: "\t};" . PHP_EOL);
fputs(stream: $zonefile, data: "};" . PHP_EOL); fputs($zonefile, data: "};" . PHP_EOL);
} }
// TODO check if ist exist in the include, else create // TODO check if ist exist in the include, else create

View File

@ -1,7 +1,6 @@
<?php declare(strict_types=1); <?php
namespace App\Controller;
error_reporting(error_level: E_ALL); namespace App\Controller;
use PDO; use PDO;
use PDOException; use PDOException;
@ -21,13 +20,12 @@ class NameserverController
*/ */
public function findAll(): bool|array public function findAll(): bool|array
{ {
$sql = " $statement = "
SELECT id, name, a, aaaa, apikey SELECT id, name, a, aaaa, apikey
FROM " . DatabaseConnection::TABLE_NAMESERVERS; FROM " . DatabaseConnection::TABLE_NAMESERVERS;
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->query($statement);
$statement->execute();
return $statement->fetchAll(mode: PDO::FETCH_ASSOC); return $statement->fetchAll(mode: PDO::FETCH_ASSOC);
} catch (PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
@ -48,10 +46,10 @@ class NameserverController
WHERE name = :name"; WHERE name = :name";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare($sql);
$statement->bindParam(param: ':name', var: $name); $statement->bindParam(param: ':name', var: $name);
$statement->execute(); $statement->execute();
return $statement->fetch(mode: PDO::FETCH_ASSOC); return $statement->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
} }
@ -71,10 +69,10 @@ class NameserverController
WHERE id = :id"; WHERE id = :id";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare($sql);
$statement->bindParam(param:':id', var: $id); $statement->bindParam(param:':id', var: $id);
$statement->execute(); $statement->execute();
return $statement->fetch(mode: PDO::FETCH_ASSOC); return $statement->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
} }
@ -87,16 +85,16 @@ class NameserverController
* @param String $aaaa * @param String $aaaa
* @param String $apikey * @param String $apikey
* *
* @return string|false * @return int
*/ */
public function insert(String $name, String $a, String $aaaa, String $apikey): bool|string public function insert(String $name, String $a, String $aaaa, String $apikey): int
{ {
$sql = " $sql = "
INSERT INTO " . DatabaseConnection::TABLE_NAMESERVERS . " (name, a, aaaa, apikey) INSERT INTO " . DatabaseConnection::TABLE_NAMESERVERS . " (name, a, aaaa, apikey)
VALUES (:name, :a, :aaaa, :apikey)"; VALUES (:name, :a, :aaaa, :apikey)";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare($sql);
$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);
@ -121,7 +119,7 @@ class NameserverController
*/ */
public function update(Int $id, String $name, String $a, String $aaaa, String $apikey): bool|int public function update(Int $id, String $name, String $a, String $aaaa, String $apikey): bool|int
{ {
$current = $this->findByID(id: $id); $current = $this->findByID($id);
if (empty($name)) { if (empty($name)) {
$name = $current['name'] ?? ''; $name = $current['name'] ?? '';
@ -145,7 +143,7 @@ class NameserverController
WHERE id = :id"; WHERE id = :id";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare($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);
@ -167,12 +165,12 @@ class NameserverController
*/ */
public function delete($id): int public function delete($id): int
{ {
$sql = " $statement = "
DELETE FROM " . DatabaseConnection::TABLE_NAMESERVERS . " DELETE FROM " . DatabaseConnection::TABLE_NAMESERVERS . "
WHERE id = :id"; WHERE id = :id";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare($statement);
$statement->bindParam(param: 'id', var: $id); $statement->bindParam(param: 'id', var: $id);
$statement->execute(); $statement->execute();
return $statement->rowCount(); return $statement->rowCount();
@ -188,11 +186,11 @@ class NameserverController
*/ */
public function getLongestEntry(String $field): int public function getLongestEntry(String $field): int
{ {
$sql = " $statement = "
SELECT MAX(LENGTH(" . $field . ")) as length FROM " . DatabaseConnection::TABLE_NAMESERVERS; SELECT MAX(LENGTH(" . $field . ")) as length FROM " . DatabaseConnection::TABLE_NAMESERVERS;
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare($statement);
$statement->execute(); $statement->execute();
$result = $statement->fetch(); $result = $statement->fetch();
return $result['length']; return $result['length'];

View File

@ -1,9 +1,7 @@
<?php declare(strict_types=1); <?php
namespace App\Controller; namespace App\Controller;
error_reporting(error_level: E_ALL);
use PDO; use PDO;
use PDOException; use PDOException;
@ -28,8 +26,7 @@ class PanelController
FROM " . DatabaseConnection::TABLE_PANELS; FROM " . DatabaseConnection::TABLE_PANELS;
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $statement); $statement = $this->databaseConnection->getConnection()->query($statement);
$statement->execute();
return $statement->fetchAll(mode: PDO::FETCH_ASSOC); return $statement->fetchAll(mode: PDO::FETCH_ASSOC);
} catch (PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
@ -50,10 +47,10 @@ class PanelController
WHERE name = :name"; WHERE name = :name";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare($sql);
$statement->bindParam(param: ':name', var: $name); $statement->bindParam(param: ':name', var: $name);
$statement->execute(); $statement->execute();
return $statement->fetch(mode: PDO::FETCH_ASSOC); return $statement->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
} }
@ -73,7 +70,7 @@ class PanelController
WHERE id = :id"; WHERE id = :id";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare($sql);
$statement->bindParam(param:':id', var: $id); $statement->bindParam(param:':id', var: $id);
$statement->execute(); $statement->execute();
return $statement->fetch(mode: PDO::FETCH_ASSOC); return $statement->fetch(mode: PDO::FETCH_ASSOC);
@ -89,16 +86,16 @@ class PanelController
* @param String $aaaa * @param String $aaaa
* @param String $apikey * @param String $apikey
* *
* @return string|false * @return int
*/ */
public function insert(String $name, String $a, String $aaaa, String $apikey): bool|string public function insert(String $name, String $a, String $aaaa, String $apikey): int
{ {
$sql = " $sql = "
INSERT INTO " . DatabaseConnection::TABLE_PANELS . " (name, a, aaaa, apikey) INSERT INTO " . DatabaseConnection::TABLE_PANELS . " (name, a, aaaa, apikey)
VALUES (:name, :a, :aaaa, :apikey)"; VALUES (:name, :a, :aaaa, :apikey)";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare($sql);
$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);
@ -123,7 +120,7 @@ class PanelController
*/ */
public function update(Int $id, String $name, String $a, String $aaaa, String $apikey): bool|int public function update(Int $id, String $name, String $a, String $aaaa, String $apikey): bool|int
{ {
$current = $this->findByID(id: $id); $current = $this->findByID($id);
if (empty($name)) { if (empty($name)) {
$name = $current['name']; $name = $current['name'];
@ -147,7 +144,7 @@ class PanelController
WHERE id = :id"; WHERE id = :id";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare($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);
@ -175,7 +172,7 @@ class PanelController
WHERE id = :id"; WHERE id = :id";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $statement); $statement = $this->databaseConnection->getConnection()->prepare($statement);
$statement->bindParam(param: 'id', var: $id); $statement->bindParam(param: 'id', var: $id);
$statement->execute(); $statement->execute();
return $statement->rowCount(); return $statement->rowCount();
@ -196,7 +193,7 @@ class PanelController
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: $statement); $statement = $this->databaseConnection->getConnection()->prepare($statement);
$statement->execute(); $statement->execute();
$result = $statement->fetch(); $result = $statement->fetch();
return $result['length']; return $result['length'];

View File

@ -1,7 +1,6 @@
<?php declare(strict_types=1); <?php
namespace App\Controller;
error_reporting(error_level: E_ALL); namespace App\Controller;
use UnhandledMatchError; use UnhandledMatchError;
@ -25,10 +24,10 @@ class RequestController
*/ */
public function __construct(private array $config, private String $requestMethod, private array $uri) public function __construct(private array $config, private String $requestMethod, private array $uri)
{ {
$this->requestMethod = strtoupper(string: $requestMethod); $this->requestMethod = strtoupper($requestMethod);
$this->databaseConnection = new DatabaseConnection(config: $this->config); $this->databaseConnection = new DatabaseConnection($this->config);
$this->panelController = new PanelController(databaseConnection: $this->databaseConnection); $this->panelController = new PanelController($this->databaseConnection);
$this->domainController = new DomainController(databaseConnection: $this->databaseConnection, panelController: $this->panelController); $this->domainController = new DomainController($this->databaseConnection, $this->panelController);
} }
@ -67,14 +66,14 @@ class RequestController
header(header: $_SERVER['SERVER_PROTOCOL'] . ' ' . $this->header); header(header: $_SERVER['SERVER_PROTOCOL'] . ' ' . $this->header);
} }
if (!empty($this->result)) { if (!empty($this->result)) {
echo json_encode(value: $this->result); echo json_encode($this->result);
} else { } else {
if ($this->status == 'pong') { if ($this->status == 'pong') {
echo json_encode(value: [ echo json_encode([
'response' => $this->status 'response' => $this->status
]); ]);
} else { } else {
echo json_encode(value: [ echo json_encode([
'status' => $this->status ?? "Error: No status", 'status' => $this->status ?? "Error: No status",
'message' => $this->message ?? "Error: No message." 'message' => $this->message ?? "Error: No message."
]); ]);
@ -88,8 +87,8 @@ class RequestController
*/ */
public function checkPassword(): bool public function checkPassword(): bool
{ {
$headers = array_change_key_case(array: getallheaders(), case: CASE_UPPER); $headers = array_change_key_case(getallheaders(), CASE_UPPER);
$apiKey = $headers['X-API-KEY'] ?? ''; $apiKey = $headers['X-API-KEY'] ?? "";
if (empty($apiKey)) { if (empty($apiKey)) {
$this->header = "401 Unauthorized"; $this->header = "401 Unauthorized";
@ -97,12 +96,12 @@ class RequestController
$this->message = "API key is missing."; $this->message = "API key is missing.";
return false; return false;
} else { } else {
[$prefix,] = explode(separator: '.', string: $apiKey); [$prefix,] = explode('.', $apiKey);
$apiUsers = new ApiKeys(databaseConnection: $this->databaseConnection); $apiUsers = new ApiKeys($this->databaseConnection);
$apiResult = $apiUsers->findByPrefix(prefix: $prefix); $apiResult = $apiUsers->findByPrefix($prefix);
$storedHash = $apiResult['api_token']; $storedHash = $apiResult['api_token'];
if (!password_verify(password: $apiKey, hash: $storedHash)) { if (!password_verify($apiKey, $storedHash)) {
$this->header = "401 Unauthorized"; $this->header = "401 Unauthorized";
$this->status = "401 Unauthorized"; $this->status = "401 Unauthorized";
$this->message = "API key mismatch."; $this->message = "API key mismatch.";
@ -120,7 +119,7 @@ class RequestController
if (empty($this->uri[3])) { if (empty($this->uri[3])) {
$this->result = $this->domainController->findAll(); $this->result = $this->domainController->findAll();
} else { } else {
if ($result = $this->domainController->findByName(name: $this->uri[3])) { if ($result = $this->domainController->findByID(intval($this->uri[3]))) {
$this->result = $result; $this->result = $result;
} else { } else {
$this->header = "404 Not Found "; $this->header = "404 Not Found ";
@ -148,11 +147,11 @@ class RequestController
$this->status = "400 Bad Request"; $this->status = "400 Bad Request";
$this->message = "At least one IP address is required."; $this->message = "At least one IP address is required.";
} else { } else {
if ($this->domainController->findByName(name: $name)) { if ($this->domainController->findByName($name)) {
$this->status = "400 Bad request"; $this->status = "400 Bad request";
$this->message = "Domain: $name already exists."; $this->message = "Domain: $name already exists.";
} else { } else {
$result = $this->domainController->insert(name: $name, panelID: $panelID, a: $a, aaaa: $aaaa); $result = $this->domainController->insert($name, $panelID, $a, $aaaa);
$this->status = "201 Created"; $this->status = "201 Created";
$this->message = $result; $this->message = $result;
} }
@ -167,11 +166,11 @@ class RequestController
public function handleDomainPutRequest(): void public function handleDomainPutRequest(): void
{ {
$putData = fopen(filename: 'php://input', mode: 'r'); $putData = fopen(filename: 'php://input', mode: 'r');
$data = fread(stream: $putData, length: 512); $data = fread($putData, 512);
$params = explode(separator: '&', string: $data); $params = explode('&', $data);
foreach ($params as $param) { foreach ($params as $param) {
[$key, $value] = explode(separator: '=', string: $param); [$key, $value] = explode('=', $param);
$put[$key] = $value; $put[$key] = $value;
} }
$id = $put['id'] ?? 0; $id = $put['id'] ?? 0;
@ -184,7 +183,7 @@ class RequestController
$this->status = "400 Bad Request"; $this->status = "400 Bad Request";
$this->message = "An ID is required"; $this->message = "An ID is required";
} else { } else {
if (!$this->domainController->findByID(id: $id)) { if (!$this->domainController->findByID($id)) {
$this->status = "404 Not Found"; $this->status = "404 Not Found";
$this->message = "Domain with ID : $id doesn't exist."; $this->message = "Domain with ID : $id doesn't exist.";
} else { } else {
@ -197,10 +196,9 @@ class RequestController
$this->status = "400 Bad Request"; $this->status = "400 Bad Request";
$this->message = "At least one IP address is required."; $this->message = "At least one IP address is required.";
} else { } else {
$dcResult = $this->domainController->update(id: $id, name: $panelID, panelID: $name, a: $a, aaaa: $aaaa); $dcResult = $this->domainController->update($id, $panelID, $name, $a, $aaaa);
$this->header = "201 Updated";
$this->status = "201 Updated"; $this->status = "201 Updated";
$this->message = "201 Updated"; $this->message = $dcResult;
} }
} }
} }
@ -213,7 +211,7 @@ class RequestController
public function handleDomainDeleteRequest(): void public function handleDomainDeleteRequest(): void
{ {
$deleteData = fopen(filename: 'php://input', mode: 'r'); $deleteData = fopen(filename: 'php://input', mode: 'r');
$data = fread(stream: $deleteData, length: 512); $data = fread($deleteData, length: 512);
$params = explode(separator: '&', string: $data); $params = explode(separator: '&', string: $data);
foreach ($params as $param) { foreach ($params as $param) {
@ -224,17 +222,14 @@ class RequestController
$id = $delete['id'] ?? 0; $id = $delete['id'] ?? 0;
if ($id == 0) { if ($id == 0) {
$this->header = "400 Bad Request"; $this->status = "404 Bad Request";
$this->status = "400 Bad Request";
$this->message = "You need to supply an ID."; $this->message = "You need to supply an ID.";
} else { } else {
if (!$this->domainController->findByID(id: $id)) { if (!$this->domainController->findByID($id)) {
$this->header = "400 Bad Request";
$this->status = "400 Bad Request"; $this->status = "400 Bad Request";
$this->message = "There is no domain with ID $id."; $this->message = "There is no domain with ID $id.";
} else { } else {
$this->domainController->delete(id: $id); $this->domainController->delete($id);
$this->header = "204 No content.";
$this->status = "204 No content."; $this->status = "204 No content.";
$this->message = "The domain $id has been deleted."; $this->message = "The domain $id has been deleted.";
} }