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

View File

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

View File

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

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 {
@ -197,10 +150,8 @@ class BindAPI
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 +166,9 @@ class BindAPI
type: 'nameserver' . $domain);
}
if ($result['header'] == 200) {
print("OK");
return true;
} else {
print("missing");
return false;
}
}
echo PHP_EOL;
@ -227,13 +178,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,11 +196,19 @@ 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);
if ($this->checkNS($domain->domain)) {
echo PHP_EOL . COLOR_DEFAULT . "check: " . COLOR_YELLOW . str_pad(string: $domain->domain, length: $maxDomainName);
if ($this->checkNS(domain: $domain->domain)) {
echo COLOR_GREEN . ' OK';
} else {
echo COLOR_RED . 'Missing';
@ -263,6 +219,8 @@ class BindAPI
echo "Unknown Panel ID: $id" . PHP_EOL;
exit(1);
}
} else {
echo "check all …" . PHP_EOL;
}
}
@ -289,11 +247,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 +319,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 +336,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 +372,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 +385,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 +397,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 +473,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 +513,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 +533,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 +548,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 +576,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 +594,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 +649,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 +671,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 +681,7 @@ class BindAPI
}
}
/**
* @param string $subcommand
*
@ -745,10 +711,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 +735,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 +778,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);
}
@ -826,9 +792,10 @@ class BindAPI
{
$arguments = $this->parseArguments();
$id = $this->arguments[1] ?? 0;
$id = intval(value: $this->arguments[1] ?? 0);
$name = $arguments['name'] ?? '';
print_r($arguments); //findme
print_r(value: $arguments); //findme
print("$id: id" . PHP_EOL);
$panelID = intval(value: $arguments['panel_id'] ?? 0);
$a = $arguments['a'] ?? '';
$aaaa = $arguments['aaaa'] ?? '';
@ -837,11 +804,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 +816,6 @@ class BindAPI
}
function handleNameserversUpdate()
{
$arguments = $this->parseArguments();
@ -864,11 +830,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 +848,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 +869,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;
}
}

View File

@ -1,7 +1,8 @@
<?php
<?php declare(strict_types=1);
namespace App\Controller;
error_reporting(error_level: E_ALL);
/**
*
@ -22,27 +23,27 @@ class CheckController
{
$curl = curl_init();
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 {
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($curl, option: CURLOPT_TIMEOUT_MS, value: 1000);
curl_setopt(handle: $curl, option: CURLOPT_RETURNTRANSFER, value: 1);
curl_setopt(handle: $curl, option: CURLOPT_TIMEOUT_MS, value: 1000);
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 {
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)) {
$httpResponse = curl_getinfo($curl)['http_code'];
if ($resultJSON = curl_exec(handle: $curl)) {
$httpResponse = curl_getinfo(handle: $curl)['http_code'];
switch($httpResponse) {
case 200:
$apiResult = json_decode($resultJSON);
$apiResult = json_decode(json: $resultJSON);
if ($command == "ping" ) {
if ($apiResult->response == "pong") {
$result = $apiResult->response;
@ -60,9 +61,9 @@ class CheckController
$result = 'Unhandled error: ' . $httpResponse;
}
} else {
$result = curl_error($curl);
$result = curl_error(handle: $curl);
}
curl_close($curl);
curl_close(handle: $curl);
return [
'data' => $result,
'header' => $httpResponse ?? ''

View File

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

View File

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

View File

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

View File

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