added missing named parameters, added strict_types

Signed-off-by: tracer <tracer@24unix.net>
This commit is contained in:
tracer 2022-01-26 19:01:52 +01:00
parent 8fc195a558
commit 67baa731c3
1 changed files with 126 additions and 162 deletions

View File

@ -1,12 +1,14 @@
<?php
<?php declare(strict_types=1);
namespace App\Controller;
define('COLOR_RED', "\033[31m");
define('COLOR_GREEN', "\033[32m");
define('COLOR_YELLOW', "\033[33m");
define('COLOR_BLUE', "\033[34m");
define('COLOR_DEFAULT', "\033[39m");
error_reporting(error_level: E_ALL);
define(constant_name: 'COLOR_RED', value: "\033[31m");
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
error_reporting(error_level: E_ALL);
@ -35,10 +37,10 @@ class BindAPI
public function __construct(private array $config, private int $argumentsCount, private array $arguments)
{
$this->databaseConnection = new DatabaseConnection(config: $this->config);
$this->panelController = new PanelController($this->databaseConnection);
$this->apiUsers = new ApiKeys($this->databaseConnection);
$this->domainController = new DomainController($this->databaseConnection, $this->panelController);
$this->nameserverController = new NameserverController($this->databaseConnection);
$this->panelController = new PanelController(databaseConnection: $this->databaseConnection);
$this->apiUsers = new ApiKeys(databaseConnection: $this->databaseConnection);
$this->domainController = new DomainController(databaseConnection: $this->databaseConnection, panelController: $this->panelController);
$this->nameserverController = new NameserverController(databaseConnection: $this->databaseConnection);
$this->checkController = new CheckController();
}
@ -61,89 +63,38 @@ class BindAPI
}
/**
* @param bool|array $panel
* @param array $server
* @param String $type
*
* @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;
if ($this->config['verbose']) {
print(COLOR_YELLOW . str_pad(string: $panel['name'], length: $maxName));
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');
}
$a = $panel['a'] ?? '';
if ($this->config['verbose']) {
print(COLOR_YELLOW . str_pad(string: $server['name'], length: $maxName));
}
$a = $server['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 ($result = $this->checkController->sendCommand(serverName: $server['name'], versionIP: 4, apiKey: $server['apikey'], command: 'ping', type: $type)) {
if ($this->config['verbose']) {
if ($result == 'pong') {
echo COLOR_GREEN . $result;
if ($result['data'] == 'pong') {
echo COLOR_GREEN . $result['data'];
} else {
echo COLOR_BLUE . 'skip';
}
@ -152,17 +103,17 @@ class BindAPI
$error = true;
}
}
$aaaa = $panel['aaaa'] ?? '';
$aaaa = $server['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 ($result = $this->checkController->sendCommand(serverName: $server['name'], versionIP: 6, apiKey: $server['apikey'], command: 'ping', type: $type)) {
if ($this->config['verbose']) {
if ($result == 'pong') {
echo COLOR_GREEN . $result;
if ($result['data'] == 'pong') {
echo COLOR_GREEN . $result['data'];
} else {
echo COLOR_BLUE . 'skip';
echo COLOR_BLUE . $result['data']; // TODO 'skip';
}
}
} else {
@ -176,6 +127,8 @@ class BindAPI
}
function handleChecks(String $subcommand)
{
try {
@ -192,15 +145,12 @@ class BindAPI
/**
* @param String $domain
*
* @return bool|void
*/
function checkNS(String $domain)
{
$nameServers = $this->nameserverController->findAll();
$i = 1;
foreach($nameServers as $nameServer) {
print("nameserver $i");
$i++;
echo COLOR_DEFAULT . ' ' . $nameServer['name'];
if (!empty($nameServer['aaaa'])) {
$result = $this->checkController->sendCommand(serverName: $nameServer['name'],
versionIP: 6,
@ -215,9 +165,9 @@ class BindAPI
type: 'nameserver' . $domain);
}
if ($result['header'] == 200) {
print("OK");
echo COLOR_GREEN . ' OK';
} else {
print("missing");
echo COLOR_RED . ' missing';
}
}
echo PHP_EOL;
@ -227,13 +177,10 @@ class BindAPI
function handleCheckPanels()
{
$id = $this->getId();
$maxDomain = $this->panelController->getLongestEntry(field:'name');
print("$maxDomain");
die();
print("id: $id " . PHP_EOL);
if ($id != 0) {
if ($panel = $this->panelController->findByID($id)) {
if ($panel = $this->panelController->findByID(id: $id)) {
if (!empty($panel['aaaa'])) {
$result = $this->checkController->sendCommand(serverName: $panel['name'],
versionIP: 6,
@ -248,10 +195,18 @@ class BindAPI
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) {
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)) {
echo COLOR_GREEN . ' OK';
} else {
@ -263,6 +218,8 @@ class BindAPI
echo "Unknown Panel ID: $id" . PHP_EOL;
exit(1);
}
} else {
echo "check all …" . PHP_EOL;
}
}
@ -289,11 +246,11 @@ class BindAPI
try {
match ($command) {
'check' => $this->handleChecks($subcommand),
'panels' => $this->handlePanels($subcommand),
'apikeys' => $this->handleApiKeys($subcommand),
'domains' => $this->handleDomains($subcommand),
'nameservers' => $this->handleNameservers($subcommand)
'check' => $this->handleChecks(subcommand: $subcommand),
'panels' => $this->handlePanels(subcommand: $subcommand),
'apikeys' => $this->handleApiKeys(subcommand: $subcommand),
'domains' => $this->handleDomains(subcommand: $subcommand),
'nameservers' => $this->handleNameservers(subcommand: $subcommand)
};
} catch(UnhandledMatchError) {
echo 'Unknown command: ' . $command . PHP_EOL;
@ -361,8 +318,7 @@ class BindAPI
'list' => $this->handleNameserversList(),
'update' => $this->handleNameserversUpdate(),
'delete' => $this->handleNameserversDelete(),
'apiping' => $this->handleNameserversAPIPing(),
'apiping' => $this->handleAPIPing(type: 'nameserver')
};
} catch (UnhandledMatchError) {
echo 'Unknown action: ' . $subcommand . PHP_EOL;
@ -379,18 +335,22 @@ class BindAPI
{
try {
match ($subcommand) {
'apiping' => $this->handlePanelsAPIPing(),
'create' => $this->handlePanelsCreate(),
'list' => $this->handlePanelsList(),
'update' => $this->handlePanelsUpdate(),
'delete' => $this->handlePanelsDelete(),
'apiping' => $this->handleAPIPing(type: 'panel')
};
} catch (UnhandledMatchError) {
echo 'Unknown action: ' . $subcommand . PHP_EOL;
}
}
/*
*
* TODO remove
*/
/*
function handleNameserversAPIPing()
{
// TODO get apikey from stdin
@ -411,8 +371,8 @@ class BindAPI
}
if ($id != 0) {
if ($nameserver = $this->nameserverController->findByID($id)) {
if (!$this->checkNSPing($nameserver)) {
if ($nameserver = $this->nameserverController->findByID(id: $id)) {
if (!$this->checkPing(server: $nameserver, type: 'nameserver')) {
$error = true;
}
} else {
@ -424,7 +384,7 @@ class BindAPI
} else {
$nameservers = $this->nameserverController->findAll();
foreach ($nameservers as $nameserver) {
if (!$this->checkNSPing(nameserver: $nameserver)) {
if (!$this->checkPing(server: $nameserver, type: 'nameserver')) {
$error = true;
}
}
@ -436,37 +396,40 @@ class BindAPI
exit(0);
}
}
*/
function handlePanelsAPIPing()
function handleAPIPing(String $type)
{
$error = false;
if (!empty($this->arguments[1])) {
$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;
}
$id = $this->getId();
if ($id != 0) {
if ($panel = $this->panelController->findByID($id)) {
if (!$this->checkPanelPing($panel)) {
if ($type == '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;
}
} else {
if ($this->config['verbose']) {
echo 'Unknown panel ID: $id' . PHP_EOL;
echo "Unknown $type ID: $id" . PHP_EOL;
}
$error = true;
}
} else {
$panels = $this->panelController->findAll();
foreach ($panels as $panel) {
if (!$this->checkPanelPing(panel: $panel)) {
if ($type == 'panel') {
$servers = $this->panelController->findAll();
} else {
$servers = $this->nameserverController->findAll();
}
foreach ($servers as $server) {
if (!$this->checkPing(server: $server, type: $type)) {
$error = true;
}
}
@ -509,11 +472,11 @@ class BindAPI
}
$apikey = $arguments['apikey'] ?? '';
if ($this->panelController->findByName($name)) {
if ($this->panelController->findByName(name: $name)) {
echo "Panel: $name already exists." . PHP_EOL;
exit(1);
} 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;
exit(0);
}
@ -549,11 +512,11 @@ class BindAPI
}
$apikey = $argiments['apikey'] ?? '';
if ($this->nameserverController->findByName($name)) {
if ($this->nameserverController->findByName(name: $name)) {
echo "Nameserver: $name already exists." . PHP_EOL;
exit(1);
} 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;
exit(0);
}
@ -569,7 +532,7 @@ class BindAPI
foreach ($this->arguments as $argument) {
if (str_contains(haystack: $argument, needle: '=')) {
[$key, $value] = explode(separator: '=', string: $argument);
$arguments[strtolower($key)] = $value;
$arguments[strtolower(string: $key)] = $value;
}
}
return $arguments;
@ -584,10 +547,10 @@ class BindAPI
$panels = $this->panelController->findAll();
if ($panels) {
$table = new ConsoleTable();
$table->setHeaders(['ID', 'Name', 'A', 'AAAA', 'API Key']);
$table->setHeaders(content: ['ID', 'Name', 'A', 'AAAA', 'API Key']);
foreach ($panels as $panel) {
$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->display();
@ -612,11 +575,11 @@ class BindAPI
echo 'An ID is required' . PHP_EOL;
exit(1);
}
if (!$this->panelController->findByID($id)) {
if (!$this->panelController->findByID(id: $id)) {
echo "Panel with ID : $id doesn't exist." . PHP_EOL;
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;
} else {
echo 'Error while updating domain server.' . PHP_EOL;
@ -630,19 +593,20 @@ class BindAPI
exit(1);
}
$id = intval($this->arguments[1]) ?? 0;
$id = intval(value: $this->arguments[1]) ?? 0;
if ($id == 0) {
echo "Panel with ID $id not found." . PHP_EOL;
exit(1);
}
if (!$this->panelController->findByID($id)) {
if (!$this->panelController->findByID(id: $id)) {
echo "There is no panel with ID $id." . PHP_EOL;
exit(1);
}
$this->panelController->delete($id);
$this->panelController->delete(id: $id);
echo "The panel with ID $id has been deleted." .PHP_EOL;
}
/**
* @param string $subcommand
*
@ -684,9 +648,9 @@ class BindAPI
$keys = $this->apiUsers->findAll();
if ($keys) {
$table = new ConsoleTable();
$table->setHeaders(['ID', 'Name', 'API key prefix']);
$table->setHeaders(content: ['ID', 'Name', 'API key prefix']);
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->display();
@ -706,8 +670,8 @@ class BindAPI
echo 'You need to add the ID of the API key.' . PHP_EOL;
exit(1);
}
if ($this->apiUsers->findByID($id)) {
$this->apiUsers->delete($id);
if ($this->apiUsers->findByID(id: $id)) {
$this->apiUsers->delete(id: $id);
echo 'API key ' . $id . ' has been deleted.' . PHP_EOL;
exit(0);
} else {
@ -716,6 +680,7 @@ class BindAPI
}
}
/**
* @param string $subcommand
*
@ -745,10 +710,10 @@ class BindAPI
$domains = $this->nameserverController->findAll();
if ($domains) {
$table = new ConsoleTable();
$table->setHeaders(['ID', 'Name', 'A', 'AAAA', 'API Key']);
$table->setHeaders(content: ['ID', 'Name', 'A', 'AAAA', 'API Key']);
foreach ($domains as $domain) {
$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->display();
@ -769,9 +734,9 @@ class BindAPI
$domains = $this->domainController->findAll();
if ($domains) {
$table = new ConsoleTable();
$table->setHeaders(['ID', 'Name', 'Panel', 'A', 'AAAA']);
$table->setHeaders(content: ['ID', 'Name', 'Panel', 'A', 'AAAA']);
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->display();
@ -812,11 +777,11 @@ class BindAPI
exit(0);
}
if ($this->domainController->findByName($name)) {
if ($this->domainController->findByName(name: $name)) {
echo "Domain: $name already exists." . PHP_EOL;
exit(1);
} 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;
exit(0);
}
@ -828,7 +793,7 @@ class BindAPI
$id = $this->arguments[1] ?? 0;
$name = $arguments['name'] ?? '';
print_r($arguments); //findme
print_r(value: $arguments); //findme
$panelID = intval(value: $arguments['panel_id'] ?? 0);
$a = $arguments['a'] ?? '';
$aaaa = $arguments['aaaa'] ?? '';
@ -837,11 +802,11 @@ class BindAPI
echo 'An ID is required' . PHP_EOL;
exit(1);
}
if (!$this->domainController->findByID($id)) {
if (!$this->domainController->findByID(id: $id)) {
echo "Domain with ID : $id doesn't exist." . PHP_EOL;
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;
} else {
echo 'Error while updating domain server.' . PHP_EOL;
@ -849,7 +814,6 @@ class BindAPI
}
function handleNameserversUpdate()
{
$arguments = $this->parseArguments();
@ -864,11 +828,11 @@ class BindAPI
echo 'An ID is required' . PHP_EOL;
exit(1);
}
if (!$this->nameserverController->findByID($id)) {
if (!$this->nameserverController->findByID(id: $id)) {
echo "Nameserver with ID : $id doesn't exist." . PHP_EOL;
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;
} else {
echo 'Error while updating nameserver.' . PHP_EOL;
@ -882,16 +846,16 @@ class BindAPI
exit(1);
}
$id = intval($this->arguments[1]) ?? 0;
$id = intval(value: $this->arguments[1]) ?? 0;
if ($id == 0) {
echo "Nameserver with ID $id not found." . PHP_EOL;
exit(1);
}
if (!$this->nameserverController->findByID($id)) {
if (!$this->nameserverController->findByID(id: $id)) {
echo "There is no nameserver with ID $id." . PHP_EOL;
exit(1);
}
$this->nameserverController->delete($id);
$this->nameserverController->delete(id: $id);
echo "The nameserver with ID $id has been deleted." .PHP_EOL;
}
@ -903,16 +867,16 @@ function handleDomainsDelete()
exit(1);
}
$id = intval($this->arguments[1]) ?? 0;
$id = intval(value: $this->arguments[1]) ?? 0;
if ($id == 0) {
echo "Domain with ID $id not found." . PHP_EOL;
exit(1);
}
if (!$this->domainController->findByID($id)) {
if (!$this->domainController->findByID(id: $id)) {
echo "There is no domain with ID $id." . PHP_EOL;
exit(1);
}
$this->domainController->delete($id);
$this->domainController->delete(id: $id);
echo "The domain with ID $id has been deleted." .PHP_EOL;
}
}