Compare commits

..

11 Commits

Author SHA1 Message Date
tracer 662737318b added missing named parameters, added strict_types
Signed-off-by: tracer <tracer@24unix.net>
2022-01-26 19:36:56 +01:00
tracer 1c1305af89 added missing named parameters, added strict_types
Signed-off-by: tracer <tracer@24unix.net>
2022-01-26 19:36:43 +01:00
tracer 707415e9c5 added missing named parameters, added strict_types
Signed-off-by: tracer <tracer@24unix.net>
2022-01-26 19:36:26 +01:00
tracer 518c4ae90d added missing named parameters, added strict_types
Signed-off-by: tracer <tracer@24unix.net>
2022-01-26 19:36:09 +01:00
tracer 30180e6d71 added missing named parameters, added strict_types
Signed-off-by: tracer <tracer@24unix.net>
2022-01-26 19:35:57 +01:00
tracer ec156db4aa added missing named parameters, added strict_types
Signed-off-by: tracer <tracer@24unix.net>
2022-01-26 19:35:32 +01:00
tracer 367c6f7d2c added missing named parameters, added strict_types
Signed-off-by: tracer <tracer@24unix.net>
2022-01-26 19:35:14 +01:00
tracer 67baa731c3 added missing named parameters, added strict_types
Signed-off-by: tracer <tracer@24unix.net>
2022-01-26 19:01:52 +01:00
tracer 8fc195a558 added missing named parameters, added strict_types
Signed-off-by: tracer <tracer@24unix.net>
2022-01-26 19:01:29 +01:00
tracer 3bc4c21411 added missing named parameters, added strict_types
Signed-off-by: tracer <tracer@24unix.net>
2022-01-26 19:00:19 +01:00
tracer fe1c698935 added missing named parameters, added strict_types
Signed-off-by: tracer <tracer@24unix.net>
2022-01-26 18:48:42 +01:00
10 changed files with 274 additions and 287 deletions

View File

@ -1,9 +1,9 @@
#!/usr/bin/keyhelp-php81 #!/usr/bin/keyhelp-php81
<?php declare(strict_types=1); <?php declare(strict_types=1);
namespace App\Controller; namespace App\Controller;
error_reporting(error_level: E_ALL);
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(fgetc(stream: $handle)); $line = trim(string: fgetc(stream: $handle));
fclose(stream: $handle); fclose(stream: $handle);
if ($line == '') { if ($line == '') {

View File

@ -1,13 +1,14 @@
<?php <?php declare(strict_types=1);
namespace App\Controller; namespace App\Controller;
// 61eec33c8829a.91a1d27d70ba26710092381a073d9e546597dff6033de272 error_reporting(error_level: E_ALL);
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($configFile); $configJSON = file_get_contents(filename: $configFile);
$config = json_decode($configJSON, associative: true); $config = json_decode(json: $configJSON, associative: true);
// TODO only valid clients? // TODO only valid clients?

View File

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

View File

@ -1,12 +1,14 @@
<?php <?php declare(strict_types=1);
namespace App\Controller; namespace App\Controller;
define('COLOR_RED', "\033[31m"); error_reporting(error_level: E_ALL);
define('COLOR_GREEN', "\033[32m");
define('COLOR_YELLOW', "\033[33m");
define('COLOR_BLUE', "\033[34m"); define(constant_name: 'COLOR_RED', value: "\033[31m");
define('COLOR_DEFAULT', "\033[39m"); define(constant_name: 'COLOR_GREEN', value: "\033[32m");
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);
@ -35,10 +37,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($this->databaseConnection); $this->panelController = new PanelController(databaseConnection: $this->databaseConnection);
$this->apiUsers = new ApiKeys($this->databaseConnection); $this->apiUsers = new ApiKeys(databaseConnection: $this->databaseConnection);
$this->domainController = new DomainController($this->databaseConnection, $this->panelController); $this->domainController = new DomainController(databaseConnection: $this->databaseConnection, panelController: $this->panelController);
$this->nameserverController = new NameserverController($this->databaseConnection); $this->nameserverController = new NameserverController(databaseConnection: $this->databaseConnection);
$this->checkController = new CheckController(); $this->checkController = new CheckController();
} }
@ -61,89 +63,38 @@ class BindAPI
} }
/** /**
* @param bool|array $panel * @param array $server
* @param String $type
* *
* @return bool * @return bool
*/ */
public function checkNSPing(bool|array $nameserver): Bool public function checkPing(array $server, String $type): Bool
{ {
$maxName = $this->nameserverController->getLongestEntry(field: 'name');
$maxA = $this->nameserverController->getLongestEntry(field: 'a');
$maxAAAA = $this->nameserverController->getLongestEntry(field: 'aaaa');
$error = false;
if ($this->config['verbose']) {
print(COLOR_YELLOW . str_pad(string: $nameserver['name'], length: $maxName));
}
$a = $nameserver['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->sendCommand(serverName: $nameserver['name'], versionIP: 4, apiKey: $nameserver['apikey'], command: 'ping', type: 'nameserver')) {
if ($this->config['verbose']) {
if ($result == 'pong') {
echo COLOR_GREEN . $result;
} else {
echo COLOR_BLUE . $result; // TODO 'skip';
}
}
} else {
$error = true;
}
}
$aaaa = $nameserver['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->sendCommand(serverName: $nameserver['name'], versionIP: 6, apiKey: $nameserver['apikey'], command: 'ping', type: 'nameserver')) {
if ($this->config['verbose']) {
if ($result == 'pong') {
echo COLOR_GREEN . $result;
} else {
echo COLOR_BLUE . $result; // TODO 'skip';
}
}
} else {
$error = true;
}
}
if ($this->config['verbose']) {
echo PHP_EOL;
}
return $error;
}
/**
* @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; $error = false;
if ($this->config['verbose']) { if ($type == 'nameserver') {
print(COLOR_YELLOW . str_pad(string: $panel['name'], length: $maxName)); $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');
} }
$a = $panel['a'] ?? ''; if ($this->config['verbose']) {
print(COLOR_YELLOW . str_pad(string: $server['name'], length: $maxName));
}
$a = $server['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->sendPanelCommand(serverName: $panel['name'], versionIP: 4, apiKey: $panel['apikey'], command: 'ping')) { if ($result = $this->checkController->sendCommand(serverName: $server['name'], versionIP: 4, apiKey: $server['apikey'], command: 'ping', type: $type)) {
if ($this->config['verbose']) { if ($this->config['verbose']) {
if ($result == 'pong') { if ($result['data'] == 'pong') {
echo COLOR_GREEN . $result; echo COLOR_GREEN . $result['data'];
} else { } else {
echo COLOR_BLUE . 'skip'; echo COLOR_BLUE . 'skip';
} }
@ -152,17 +103,17 @@ class BindAPI
$error = true; $error = true;
} }
} }
$aaaa = $panel['aaaa'] ?? ''; $aaaa = $server['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->sendPanelCommand(serverName: $panel['name'], versionIP: 6, apiKey: $panel['apikey'], command: 'ping')) { if ($result = $this->checkController->sendCommand(serverName: $server['name'], versionIP: 6, apiKey: $server['apikey'], command: 'ping', type: $type)) {
if ($this->config['verbose']) { if ($this->config['verbose']) {
if ($result == 'pong') { if ($result['data'] == 'pong') {
echo COLOR_GREEN . $result; echo COLOR_GREEN . $result['data'];
} else { } else {
echo COLOR_BLUE . 'skip'; echo COLOR_BLUE . $result['data']; // TODO 'skip';
} }
} }
} else { } else {
@ -176,6 +127,8 @@ class BindAPI
} }
function handleChecks(String $subcommand) function handleChecks(String $subcommand)
{ {
try { try {
@ -197,10 +150,8 @@ 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) {
print("nameserver $i"); echo COLOR_DEFAULT . ' ' . $nameServer['name'];
$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,
@ -215,9 +166,9 @@ class BindAPI
type: 'nameserver' . $domain); type: 'nameserver' . $domain);
} }
if ($result['header'] == 200) { if ($result['header'] == 200) {
print("OK"); return true;
} else { } else {
print("missing"); return false;
} }
} }
echo PHP_EOL; echo PHP_EOL;
@ -227,13 +178,10 @@ class BindAPI
function handleCheckPanels() function handleCheckPanels()
{ {
$id = $this->getId(); $id = $this->getId();
$maxDomain = $this->panelController->getLongestEntry(field:'name'); print("id: $id " . PHP_EOL);
print("$maxDomain");
die();
if ($id != 0) { if ($id != 0) {
if ($panel = $this->panelController->findByID($id)) { if ($panel = $this->panelController->findByID(id: $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,
@ -248,11 +196,19 @@ class BindAPI
type: 'panel'); type: 'panel');
} }
$domains = json_decode($result['data']); $domains = json_decode(json: $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($domain->domain, $maxDomain); echo PHP_EOL . COLOR_DEFAULT . "check: " . COLOR_YELLOW . str_pad(string: $domain->domain, length: $maxDomainName);
if ($this->checkNS($domain->domain)) { if ($this->checkNS(domain: $domain->domain)) {
echo COLOR_GREEN . ' OK'; echo COLOR_GREEN . ' OK';
} else { } else {
echo COLOR_RED . 'Missing'; echo COLOR_RED . 'Missing';
@ -263,6 +219,8 @@ 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;
} }
} }
@ -289,11 +247,11 @@ class BindAPI
try { try {
match ($command) { match ($command) {
'check' => $this->handleChecks($subcommand), 'check' => $this->handleChecks(subcommand: $subcommand),
'panels' => $this->handlePanels($subcommand), 'panels' => $this->handlePanels(subcommand: $subcommand),
'apikeys' => $this->handleApiKeys($subcommand), 'apikeys' => $this->handleApiKeys(subcommand: $subcommand),
'domains' => $this->handleDomains($subcommand), 'domains' => $this->handleDomains(subcommand: $subcommand),
'nameservers' => $this->handleNameservers($subcommand) 'nameservers' => $this->handleNameservers(subcommand: $subcommand)
}; };
} catch(UnhandledMatchError) { } catch(UnhandledMatchError) {
echo 'Unknown command: ' . $command . PHP_EOL; echo 'Unknown command: ' . $command . PHP_EOL;
@ -361,8 +319,7 @@ class BindAPI
'list' => $this->handleNameserversList(), 'list' => $this->handleNameserversList(),
'update' => $this->handleNameserversUpdate(), 'update' => $this->handleNameserversUpdate(),
'delete' => $this->handleNameserversDelete(), 'delete' => $this->handleNameserversDelete(),
'apiping' => $this->handleNameserversAPIPing(), 'apiping' => $this->handleAPIPing(type: 'nameserver')
}; };
} catch (UnhandledMatchError) { } catch (UnhandledMatchError) {
echo 'Unknown action: ' . $subcommand . PHP_EOL; echo 'Unknown action: ' . $subcommand . PHP_EOL;
@ -379,18 +336,22 @@ 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
@ -411,8 +372,8 @@ class BindAPI
} }
if ($id != 0) { if ($id != 0) {
if ($nameserver = $this->nameserverController->findByID($id)) { if ($nameserver = $this->nameserverController->findByID(id: $id)) {
if (!$this->checkNSPing($nameserver)) { if (!$this->checkPing(server: $nameserver, type: 'nameserver')) {
$error = true; $error = true;
} }
} else { } else {
@ -424,7 +385,7 @@ class BindAPI
} else { } else {
$nameservers = $this->nameserverController->findAll(); $nameservers = $this->nameserverController->findAll();
foreach ($nameservers as $nameserver) { foreach ($nameservers as $nameserver) {
if (!$this->checkNSPing(nameserver: $nameserver)) { if (!$this->checkPing(server: $nameserver, type: 'nameserver')) {
$error = true; $error = true;
} }
} }
@ -436,37 +397,40 @@ class BindAPI
exit(0); exit(0);
} }
} }
*/
function handleAPIPing(String $type)
function handlePanelsAPIPing()
{ {
$error = false; $error = false;
if (!empty($this->arguments[1])) { $id = $this->getId();
$id = intval(value: $this->arguments[1] ?? 0);
if ($id != $this->arguments[1]) {
echo 'ID has to be a number.' . PHP_EOL;
exit(1);
}
} else {
$id = 0;
}
if ($id != 0) { if ($id != 0) {
if ($panel = $this->panelController->findByID($id)) { if ($type == 'panel') {
if (!$this->checkPanelPing($panel)) { $server = $this->panelController->findByID(id: $id);
} else {
$server = $this->nameserverController->findByID(id: $id);
}
if ($server) {
if (!$this->checkPing(server: $server, type: $type)) {
$error = true; $error = true;
} }
} else { } else {
if ($this->config['verbose']) { if ($this->config['verbose']) {
echo 'Unknown panel ID: $id' . PHP_EOL; echo "Unknown $type ID: $id" . PHP_EOL;
} }
$error = true; $error = true;
} }
} else { } else {
$panels = $this->panelController->findAll(); if ($type == 'panel') {
foreach ($panels as $panel) { $servers = $this->panelController->findAll();
if (!$this->checkPanelPing(panel: $panel)) { } else {
$servers = $this->nameserverController->findAll();
}
foreach ($servers as $server) {
if (!$this->checkPing(server: $server, type: $type)) {
$error = true; $error = true;
} }
} }
@ -509,11 +473,11 @@ class BindAPI
} }
$apikey = $arguments['apikey'] ?? ''; $apikey = $arguments['apikey'] ?? '';
if ($this->panelController->findByName($name)) { if ($this->panelController->findByName(name: $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, $a, $aaaa, $apikey); $result = $this->panelController->insert(name: $name, a: $a, aaaa: $aaaa, apikey: $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);
} }
@ -549,11 +513,11 @@ class BindAPI
} }
$apikey = $argiments['apikey'] ?? ''; $apikey = $argiments['apikey'] ?? '';
if ($this->nameserverController->findByName($name)) { if ($this->nameserverController->findByName(name: $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, $a, $aaaa, $apikey); $result = $this->nameserverController->insert(name: $name, a: $a, aaaa: $aaaa, apikey: $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);
} }
@ -569,7 +533,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($key)] = $value; $arguments[strtolower(string: $key)] = $value;
} }
} }
return $arguments; return $arguments;
@ -584,10 +548,10 @@ class BindAPI
$panels = $this->panelController->findAll(); $panels = $this->panelController->findAll();
if ($panels) { if ($panels) {
$table = new ConsoleTable(); $table = new ConsoleTable();
$table->setHeaders(['ID', 'Name', 'A', 'AAAA', 'API Key']); $table->setHeaders(content: ['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([$panel['id'], $panel['name'], $panel['a'], $panel['aaaa'], $token]); $table->addRow(data: [$panel['id'], $panel['name'], $panel['a'], $panel['aaaa'], $token]);
} }
$table->setPadding(value: 2); $table->setPadding(value: 2);
$table->display(); $table->display();
@ -612,11 +576,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)) { if (!$this->panelController->findByID(id: $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, $name, $a, $aaaa, $apikey) !== false) { if ($this->panelController->update(id: $id, name: $name, a: $a, aaaa: $aaaa, apikey: $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;
@ -630,19 +594,20 @@ class BindAPI
exit(1); exit(1);
} }
$id = intval($this->arguments[1]) ?? 0; $id = intval(value: $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)) { if (!$this->panelController->findByID(id: $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); $this->panelController->delete(id: $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
* *
@ -684,9 +649,9 @@ class BindAPI
$keys = $this->apiUsers->findAll(); $keys = $this->apiUsers->findAll();
if ($keys) { if ($keys) {
$table = new ConsoleTable(); $table = new ConsoleTable();
$table->setHeaders(['ID', 'Name', 'API key prefix']); $table->setHeaders(content: ['ID', 'Name', 'API key prefix']);
foreach ($keys as $key) { foreach ($keys as $key) {
$table->addRow([$key['id'], $key['name'], $key['api_token_prefix']]); $table->addRow(data: [$key['id'], $key['name'], $key['api_token_prefix']]);
} }
$table->setPadding(value: 2); $table->setPadding(value: 2);
$table->display(); $table->display();
@ -706,8 +671,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)) { if ($this->apiUsers->findByID(id: $id)) {
$this->apiUsers->delete($id); $this->apiUsers->delete(id: $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 {
@ -716,6 +681,7 @@ class BindAPI
} }
} }
/** /**
* @param string $subcommand * @param string $subcommand
* *
@ -745,10 +711,10 @@ class BindAPI
$domains = $this->nameserverController->findAll(); $domains = $this->nameserverController->findAll();
if ($domains) { if ($domains) {
$table = new ConsoleTable(); $table = new ConsoleTable();
$table->setHeaders(['ID', 'Name', 'A', 'AAAA', 'API Key']); $table->setHeaders(content: ['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([$domain['id'], $domain['name'], $domain['a'], $domain['aaaa'], $token]); $table->addRow(data: [$domain['id'], $domain['name'], $domain['a'], $domain['aaaa'], $token]);
} }
$table->setPadding(value: 2); $table->setPadding(value: 2);
$table->display(); $table->display();
@ -769,9 +735,9 @@ class BindAPI
$domains = $this->domainController->findAll(); $domains = $this->domainController->findAll();
if ($domains) { if ($domains) {
$table = new ConsoleTable(); $table = new ConsoleTable();
$table->setHeaders(['ID', 'Name', 'Panel', 'A', 'AAAA']); $table->setHeaders(content: ['ID', 'Name', 'Panel', 'A', 'AAAA']);
foreach ($domains as $domain) { foreach ($domains as $domain) {
$table->addRow([$domain['id'], $domain['name'], $domain['panel_id'], $domain['a'], $domain['aaaa']]); $table->addRow(data: [$domain['id'], $domain['name'], $domain['panel_id'], $domain['a'], $domain['aaaa']]);
} }
$table->setPadding(value: 2); $table->setPadding(value: 2);
$table->display(); $table->display();
@ -812,11 +778,11 @@ class BindAPI
exit(0); exit(0);
} }
if ($this->domainController->findByName($name)) { if ($this->domainController->findByName(name: $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, $panelID, $a, $aaaa); $result = $this->domainController->insert(name: $name, panelID: $panelID, a: $a, aaaa: $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);
} }
@ -826,9 +792,10 @@ class BindAPI
{ {
$arguments = $this->parseArguments(); $arguments = $this->parseArguments();
$id = $this->arguments[1] ?? 0; $id = intval(value: $this->arguments[1] ?? 0);
$name = $arguments['name'] ?? ''; $name = $arguments['name'] ?? '';
print_r($arguments); //findme print_r(value: $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'] ?? '';
@ -837,11 +804,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)) { if (!$this->domainController->findByID(id: $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, $name, $panelID, $a, $aaaa) !== false) { if ($this->domainController->update(id: $id, name: $name, panelID: $panelID, a: $a, aaaa: $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;
@ -849,7 +816,6 @@ class BindAPI
} }
function handleNameserversUpdate() function handleNameserversUpdate()
{ {
$arguments = $this->parseArguments(); $arguments = $this->parseArguments();
@ -864,11 +830,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)) { if (!$this->nameserverController->findByID(id: $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, $name, $a, $aaaa, $apikey) !== false) { if ($this->nameserverController->update(id: $id, name: $name, a: $a, aaaa: $aaaa, apikey: $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;
@ -882,16 +848,16 @@ class BindAPI
exit(1); exit(1);
} }
$id = intval($this->arguments[1]) ?? 0; $id = intval(value: $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)) { if (!$this->nameserverController->findByID(id: $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); $this->nameserverController->delete(id: $id);
echo "The nameserver with ID $id has been deleted." .PHP_EOL; echo "The nameserver with ID $id has been deleted." .PHP_EOL;
} }
@ -903,16 +869,16 @@ function handleDomainsDelete()
exit(1); exit(1);
} }
$id = intval($this->arguments[1]) ?? 0; $id = intval(value: $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)) { if (!$this->domainController->findByID(id: $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); $this->domainController->delete(id: $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,8 @@
<?php <?php declare(strict_types=1);
namespace App\Controller; namespace App\Controller;
error_reporting(error_level: E_ALL);
/** /**
* *
@ -22,27 +23,27 @@ class CheckController
{ {
$curl = curl_init(); $curl = curl_init();
if ($type == "panel") { if ($type == "panel") {
curl_setopt($curl, option: CURLOPT_URL, value: "https://$serverName/api/v2/" . $command); curl_setopt(handle: $curl, option: CURLOPT_URL, value: "https://$serverName/api/v2/" . $command);
} else { } else {
curl_setopt($curl, option: CURLOPT_URL, value: "https://$serverName/api/" . $command); curl_setopt(handle: $curl, option: CURLOPT_URL, value: "https://$serverName/api/" . $command);
} }
curl_setopt($curl, option: CURLOPT_RETURNTRANSFER, value: 1); curl_setopt(handle: $curl, option: CURLOPT_RETURNTRANSFER, value: 1);
curl_setopt($curl, option: CURLOPT_TIMEOUT_MS, value: 1000); curl_setopt(handle: $curl, option: CURLOPT_TIMEOUT_MS, value: 1000);
if ($versionIP == 4) { if ($versionIP == 4) {
curl_setopt($curl, option: CURLOPT_IPRESOLVE, value: CURL_IPRESOLVE_V4); curl_setopt(handle: $curl, option: CURLOPT_IPRESOLVE, value: CURL_IPRESOLVE_V4);
} else { } else {
curl_setopt($curl, option: CURLOPT_IPRESOLVE, value: CURL_IPRESOLVE_V6); curl_setopt(handle: $curl, option: CURLOPT_IPRESOLVE, value: CURL_IPRESOLVE_V6);
} }
curl_setopt($curl, option: CURLOPT_HTTPHEADER, value: ["X-API-Key:$apiKey"]); curl_setopt(handle: $curl, option: CURLOPT_HTTPHEADER, value: ["X-API-Key:$apiKey"]);
if ($resultJSON = curl_exec($curl)) { if ($resultJSON = curl_exec(handle: $curl)) {
$httpResponse = curl_getinfo($curl)['http_code']; $httpResponse = curl_getinfo(handle: $curl)['http_code'];
switch($httpResponse) { switch($httpResponse) {
case 200: case 200:
$apiResult = json_decode($resultJSON); $apiResult = json_decode(json: $resultJSON);
if ($command == "ping" ) { if ($command == "ping" ) {
if ($apiResult->response == "pong") { if ($apiResult->response == "pong") {
$result = $apiResult->response; $result = $apiResult->response;
@ -60,9 +61,9 @@ class CheckController
$result = 'Unhandled error: ' . $httpResponse; $result = 'Unhandled error: ' . $httpResponse;
} }
} else { } else {
$result = curl_error($curl); $result = curl_error(handle: $curl);
} }
curl_close($curl); curl_close(handle: $curl);
return [ return [
'data' => $result, 'data' => $result,
'header' => $httpResponse ?? '' 'header' => $httpResponse ?? ''

View File

@ -1,7 +1,9 @@
<?php <?php declare(strict_types=1);
namespace App\Controller; namespace App\Controller;
error_reporting(error_level: E_ALL);
use PDO; use PDO;
use PDOException; use PDOException;
@ -20,7 +22,7 @@ class DatabaseConnection
public function __construct(private array $config) public function __construct(private array $config)
{ {
extract($this->config); extract(array: $this->config);
try { try {
$this->dbConnection = new PDO( $this->dbConnection = new PDO(
@ -29,13 +31,13 @@ class DatabaseConnection
password: $dbPassword password: $dbPassword
); );
$sql = "SHOW TABLES"; $sql = "SHOW TABLES";
$statement = $this->dbConnection->prepare($sql); $statement = $this->dbConnection->prepare(query: $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('Should I try to create them?')) { if (confirm(message: 'Should I try to create them?')) {
$sql = " $sql = "
CREATE TABLE `apikeys` ( CREATE TABLE `apikeys` (
`id` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
@ -44,7 +46,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($sql); $statement = $this->dbConnection->prepare(query: $sql);
$statement->execute(); $statement->execute();
$sql = " $sql = "
@ -56,7 +58,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($sql); $statement = $this->dbConnection->prepare(query: $sql);
$statement->execute(); $statement->execute();
$sql = " $sql = "
@ -68,7 +70,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($sql); $statement = $this->dbConnection->prepare(query: $sql);
$statement->execute(); $statement->execute();
$sql = " $sql = "
@ -80,7 +82,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($sql); $statement = $this->dbConnection->prepare(query: $sql);
$statement->execute(); $statement->execute();
echo 'Tables have been created.' . PHP_EOL; echo 'Tables have been created.' . PHP_EOL;

View File

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

View File

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

View File

@ -1,7 +1,8 @@
<?php <?php declare(strict_types=1);
namespace App\Controller; namespace App\Controller;
error_reporting(error_level: E_ALL);
use UnhandledMatchError; use UnhandledMatchError;
/** /**
@ -24,10 +25,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($requestMethod); $this->requestMethod = strtoupper(string: $requestMethod);
$this->databaseConnection = new DatabaseConnection($this->config); $this->databaseConnection = new DatabaseConnection(config: $this->config);
$this->panelController = new PanelController($this->databaseConnection); $this->panelController = new PanelController(databaseConnection: $this->databaseConnection);
$this->domainController = new DomainController($this->databaseConnection, $this->panelController); $this->domainController = new DomainController(databaseConnection: $this->databaseConnection, panelController: $this->panelController);
} }
@ -66,14 +67,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($this->result); echo json_encode(value: $this->result);
} else { } else {
if ($this->status == 'pong') { if ($this->status == 'pong') {
echo json_encode([ echo json_encode(value: [
'response' => $this->status 'response' => $this->status
]); ]);
} else { } else {
echo json_encode([ echo json_encode(value: [
'status' => $this->status ?? "Error: No status", 'status' => $this->status ?? "Error: No status",
'message' => $this->message ?? "Error: No message." 'message' => $this->message ?? "Error: No message."
]); ]);
@ -87,8 +88,8 @@ class RequestController
*/ */
public function checkPassword(): bool public function checkPassword(): bool
{ {
$headers = array_change_key_case(getallheaders(), 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->header = "401 Unauthorized"; $this->header = "401 Unauthorized";
@ -96,12 +97,12 @@ class RequestController
$this->message = "API key is missing."; $this->message = "API key is missing.";
return false; return false;
} else { } else {
[$prefix,] = explode('.', $apiKey); [$prefix,] = explode(separator: '.', string: $apiKey);
$apiUsers = new ApiKeys($this->databaseConnection); $apiUsers = new ApiKeys(databaseConnection: $this->databaseConnection);
$apiResult = $apiUsers->findByPrefix($prefix); $apiResult = $apiUsers->findByPrefix(prefix: $prefix);
$storedHash = $apiResult['api_token']; $storedHash = $apiResult['api_token'];
if (!password_verify($apiKey, $storedHash)) { if (!password_verify(password: $apiKey, hash: $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.";
@ -119,7 +120,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->findByID(intval($this->uri[3]))) { if ($result = $this->domainController->findByName(name: $this->uri[3])) {
$this->result = $result; $this->result = $result;
} else { } else {
$this->header = "404 Not Found "; $this->header = "404 Not Found ";
@ -147,11 +148,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)) { if ($this->domainController->findByName(name: $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, $panelID, $a, $aaaa); $result = $this->domainController->insert(name: $name, panelID: $panelID, a: $a, aaaa: $aaaa);
$this->status = "201 Created"; $this->status = "201 Created";
$this->message = $result; $this->message = $result;
} }
@ -166,11 +167,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($putData, 512); $data = fread(stream: $putData, length: 512);
$params = explode('&', $data); $params = explode(separator: '&', string: $data);
foreach ($params as $param) { foreach ($params as $param) {
[$key, $value] = explode('=', $param); [$key, $value] = explode(separator: '=', string: $param);
$put[$key] = $value; $put[$key] = $value;
} }
$id = $put['id'] ?? 0; $id = $put['id'] ?? 0;
@ -183,7 +184,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)) { if (!$this->domainController->findByID(id: $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 {
@ -196,9 +197,10 @@ 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, $panelID, $name, $a, $aaaa); $dcResult = $this->domainController->update(id: $id, name: $panelID, panelID: $name, a: $a, aaaa: $aaaa);
$this->header = "201 Updated";
$this->status = "201 Updated"; $this->status = "201 Updated";
$this->message = $dcResult; $this->message = "201 Updated";
} }
} }
} }
@ -211,7 +213,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($deleteData, length: 512); $data = fread(stream: $deleteData, length: 512);
$params = explode(separator: '&', string: $data); $params = explode(separator: '&', string: $data);
foreach ($params as $param) { foreach ($params as $param) {
@ -222,14 +224,17 @@ class RequestController
$id = $delete['id'] ?? 0; $id = $delete['id'] ?? 0;
if ($id == 0) { if ($id == 0) {
$this->status = "404 Bad Request"; $this->header = "400 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)) { if (!$this->domainController->findByID(id: $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); $this->domainController->delete(id: $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.";
} }