Compare commits

...

7 Commits

Author SHA1 Message Date
tracer 879feb9044 added name and author
Signed-off-by: tracer <tracer@24unix.net>
2022-02-12 19:39:47 +01:00
tracer a97b8e926a added config.json.local
Signed-off-by: tracer <tracer@24unix.net>
2022-02-12 19:39:05 +01:00
tracer 5d4586aff3 adapted to /domains/name path
Signed-off-by: tracer <tracer@24unix.net>
2022-02-12 19:37:24 +01:00
tracer ac21b53bb9 removed IP addresses from DB
Signed-off-by: tracer <tracer@24unix.net>
2022-02-12 19:36:33 +01:00
tracer cf8fb56d2d removed ip added content
Signed-off-by: tracer <tracer@24unix.net>
2022-02-12 19:30:25 +01:00
tracer 05db619afd added debugging
Signed-off-by: tracer <tracer@24unix.net>
2022-02-12 19:29:42 +01:00
tracer 9271752843 added logger & /name path
Signed-off-by: tracer <tracer@24unix.net>
2022-02-12 19:27:38 +01:00
7 changed files with 384 additions and 287 deletions

View File

@ -1,4 +1,4 @@
#!/usr/local/bin/php #!/usr/local/bin/php81
<?php declare(strict_types=1); <?php declare(strict_types=1);
namespace App\Controller; namespace App\Controller;
//#!/usr/bin/keyhelp-php81 //#!/usr/bin/keyhelp-php81
@ -15,7 +15,11 @@ $version = '0.0.1';
require dirname(path: __DIR__) . '/vendor/autoload.php'; require dirname(path: __DIR__) . '/vendor/autoload.php';
$configFile = dirname(path: __DIR__) ."/config.json"; $configFile = dirname(path: __DIR__) ."/config.json.local";
if (!file_exists(filename: $configFile)) {
$configFile = dirname(path: __DIR__) ."/config.json";
}
if (!file_exists(filename: $configFile)) { if (!file_exists(filename: $configFile)) {
echo 'Missing config file' . PHP_EOL; echo 'Missing config file' . PHP_EOL;
if (confirm(message: 'Should I create a new config based on config.json.sample?')) { if (confirm(message: 'Should I create a new config based on config.json.sample?')) {

View File

@ -1,4 +1,11 @@
{ {
"name": "tracer/bindppi",
"authors": [
{
"name": "Micha Espey",
"email": "tracer@24unix.net"
}
],
"type": "project", "type": "project",
"license": "proprietary", "license": "proprietary",
"minimum-stability": "stable", "minimum-stability": "stable",

View File

@ -162,8 +162,8 @@ class BindAPI
echo COLOR_YELLOW . "domains" . COLOR_DEFAULT . " domains this server is responsible for" . PHP_EOL; echo COLOR_YELLOW . "domains" . COLOR_DEFAULT . " domains this server is responsible for" . PHP_EOL;
echo COLOR_GREEN . "\t domains:list" . PHP_EOL; echo COLOR_GREEN . "\t domains:list" . PHP_EOL;
echo COLOR_GREEN . "\t domains:create <name> {panel_id=<ID>} | {A=<IPv4>} {AAAA=<IPv6>}" . PHP_EOL; echo COLOR_GREEN . "\t domains:create <name> {panel=<name>} | {A=<IPv4>} {AAAA=<IPv6>}" . PHP_EOL;
echo COLOR_GREEN . "\t domains:update <ID> {name=<name>} {panel_id=<ID>} | {A=<IPv4>} {AAAA=<IPv6>}" . PHP_EOL; echo COLOR_GREEN . "\t domains:update <ID> {name=<name>} {panel=<name>} | {A=<IPv4>} {AAAA=<IPv6>}" . PHP_EOL;
echo COLOR_GREEN . "\t domains:delete <ID>" . PHP_EOL; echo COLOR_GREEN . "\t domains:delete <ID>" . PHP_EOL;
echo COLOR_YELLOW . "apikeys" . COLOR_DEFAULT . "\t API keys for other nameservers" . PHP_EOL; echo COLOR_YELLOW . "apikeys" . COLOR_DEFAULT . "\t API keys for other nameservers" . PHP_EOL;
@ -219,7 +219,7 @@ class BindAPI
$id = intval(value: $this->arguments[1] ?? 0); $id = intval(value: $this->arguments[1] ?? 0);
if ($id != 0) { if ($id != 0) {
if ($panel = $this->panelController->findByID(id: $id)) { if ($panel = $this->panelRepository->findByID(id: $id)) {
$this->checkSinglePanel(panel: $panel); $this->checkSinglePanel(panel: $panel);
} else { } else {
echo "Unknown panel ID: $id" . PHP_EOL; echo "Unknown panel ID: $id" . PHP_EOL;
@ -367,7 +367,7 @@ class BindAPI
serverName: $nameServer->getName(), serverName: $nameServer->getName(),
versionIP: 6, versionIP: 6,
apiKey: $nameServer->getApikey(), apiKey: $nameServer->getApikey(),
command: 'domains/' . $domainName, command: 'domains/name/' . $domainName,
serverType: 'nameserver'); serverType: 'nameserver');
} else { } else {
$result = $this->apiController->sendCommand( $result = $this->apiController->sendCommand(
@ -375,49 +375,57 @@ class BindAPI
serverName: $nameServer->getName(), serverName: $nameServer->getName(),
versionIP: 4, versionIP: 4,
apiKey: $nameServer->getApikey(), apiKey: $nameServer->getApikey(),
command: 'domains', command: 'domains/name/',
serverType: 'nameserver' . $domainName); serverType: 'nameserver' . $domainName);
} }
} catch (DependencyException|NotFoundException $e) { } catch (DependencyException|NotFoundException $e) {
echo $e->getMessage(); echo $e->getMessage();
exit(1); exit(1);
} }
if ($result['header'] == 200) {
echo COLOR_GREEN . ' OK'; switch ($result['header']) {
} else { case 200:
echo COLOR_RED . $result['header'] . COLOR_DEFAULT; echo COLOR_GREEN . ' OK';
$arguments = $this->parseArguments(); break;
if (!empty($arguments['fix']) && $arguments['fix'] == 'yes') { case 404:
echo 'trying to fix …'; echo COLOR_RED . $result['header'] . COLOR_DEFAULT;
$body = [ $arguments = $this->parseArguments();
'name' => $domainName, if (!empty($arguments['fix']) && $arguments['fix'] == 'yes') {
'panel_id' => $panel['id'] echo 'trying to fix …';
]; $body = [
try { 'name' => $domainName,
if (!empty($nameServer['aaaa'])) { 'panel_id' => $panel['id']
$this->apiController->sendCommand( ];
requestType: 'POST', try {
serverName: $nameServer['name'], if (!empty($nameServer['aaaa'])) {
versionIP: 6, $this->apiController->sendCommand(
apiKey: $nameServer['apikey'], requestType: 'POST',
command: 'domains', serverName: $nameServer['name'],
serverType: 'nameserver', versionIP: 6,
body: $body); apiKey: $nameServer['apikey'],
} else { command: 'domains/name',
$this->apiController->sendCommand( serverType: 'nameserver',
requestType: 'POST', body: $body);
serverName: $nameServer['name'], } else {
versionIP: 4, $this->apiController->sendCommand(
apiKey: $nameServer['apikey'], requestType: 'POST',
command: 'domains', serverName: $nameServer['name'],
serverType: 'nameserver', versionIP: 4,
body: $body); apiKey: $nameServer['apikey'],
command: 'domains/name',
serverType: 'nameserver',
body: $body);
}
} catch (DependencyException|NotFoundException $e) {
echo $e->getMessage();
exit(1);
} }
} catch (DependencyException|NotFoundException $e) {
echo $e->getMessage();
exit(1);
} }
} break;
default:
echo 'Server error' . PHP_EOL;
print_r(value: $result);
exit(1);
} }
} }
echo PHP_EOL; echo PHP_EOL;
@ -949,16 +957,28 @@ class BindAPI
} }
if (!empty($domains)) { if (!empty($domains)) {
$table = new ConsoleTable(); $table = new ConsoleTable();
$table->setHeaders(content: ['ID', 'Name', 'Panel', 'A', 'AAAA']); $table->setHeaders(content: ['ID', 'Name', 'Content']);
/** @var Domain $domain */ /** @var Domain $domain */
foreach ($domains as $domain) { foreach ($domains as $domain) {
$row = []; $row = [];
try { try {
$row[] = $domain->getId(); $row[] = $domain->getId();
$row[] = $domain->getName(); $row[] = $domain->getName();
$row[] = $domain->getPanelID(); $content = json_decode(json: $domain->getContent());
$row[] = $domain->getA(); $column = '';
$row[] = $domain->getAAAA(); if (!empty($content->panel)) {
$column = 'Panel: ' . $content->panel;
}
if (!empty($content->custom)) {
$column .= 'Custom: ';
if (!empty($content->custom->a)) {
$column .= 'A: ' . $content->custom->a;
}
if (!empty($content->custom->aaaa)) {
$column .= ' AAAA: ' . $content->custom->a;
}
}
$row[] = $column;
} catch (DependencyException|NotFoundException $e) { } catch (DependencyException|NotFoundException $e) {
echo $e->getMessage(); echo $e->getMessage();
} }
@ -992,12 +1012,14 @@ class BindAPI
$arguments = $this->parseArguments(); $arguments = $this->parseArguments();
$a = $arguments['a'] ?? "";
$aaaa = $arguments['aaaa'] ?? "";
$panelID = $arguments['panel_id'] ?? 0;
if (empty($a) && empty($aaaa) && empty($panelID)) { $a = $arguments['a'] ?? '';
echo 'At least one IP address or panel ID is required.' . PHP_EOL; $aaaa = $arguments['aaaa'] ?? '';
$panel = $arguments['panel'] ?? '';
print("panel: $panel");
if (empty($a) && empty($aaaa) && empty($panel)) {
echo 'At least one IP address or panel is required.' . PHP_EOL;
exit(0); exit(0);
} }
@ -1006,16 +1028,27 @@ class BindAPI
echo "Domain: $name already exists." . PHP_EOL; echo "Domain: $name already exists." . PHP_EOL;
exit(1); exit(1);
} else { } else {
if (!empty($panelID)) { if (!empty($panel)) {
if ($panel = $this->panelController->findByID(id: $panelID)) { if ($this->panelRepository->findByName(name: $panel)) {
$a = $panel['a']; $content = $this->domainController->createPanelContent(panel: $panel);
$aaaa = $panel['aaaa']; } else {
echo 'Unknown panel: ' . $panel;
exit(1);
} }
} else {
$content = [];
if (!empty($a)) {
$content['a'] = $a;
}
if (!empty($aaaa)) {
$content['aaaa'] = $aaaa;
}
$content = $this->domainController->createIPContent(ip: $content);
} }
$domain = new Domain(name: $name, id: $panelID, panelID: $a, a: $aaaa); $domain = new Domain(name: $name, content: $content);
$result = $this->domainRepository->insert(domain: $domain); $result = $this->domainRepository->insert(domain: $domain);
echo "Domain $name has been created with id $result" . PHP_EOL; echo "Domain $name has been created with id $result" . PHP_EOL;
$this->domainController->createZoneFile(domain: $domain); $this->domainController->createSlaveZoneFile(domain: $domain);
exit(0); exit(0);
} }
} catch (DependencyException|NotFoundException $e) { } catch (DependencyException|NotFoundException $e) {
@ -1033,7 +1066,7 @@ class BindAPI
$id = intval(value: $this->arguments[1] ?? 0); $id = intval(value: $this->arguments[1] ?? 0);
$name = $arguments['name'] ?? ''; $name = $arguments['name'] ?? '';
$panelID = intval(value: $arguments['panel_id'] ?? 0); $panel = $arguments['panel'] ?? '';
$a = $arguments['a'] ?? ''; $a = $arguments['a'] ?? '';
$aaaa = $arguments['aaaa'] ?? ''; $aaaa = $arguments['aaaa'] ?? '';
@ -1045,15 +1078,39 @@ class BindAPI
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 (!empty($panelID)) {
$panel = $this->panelController->findByID(id: $panelID); $content = [];
$a = $panel['a']; if (!empty($panel)) {
$aaaa = $panel['aaaa']; $panel = $this->panelRepository->findByName(name: $panel);
$content = $this->domainController->createPanelContent(panel: $panel->getName());
} else {
$addresses = [];
if (!empty($a)) {
$addresses[] = [
'a' => $a
];
}
if (!empty($aaaa)) {
$addresses[] = [
'aaaa' => $aaaa
];
}
if (!empty($addresses)) {
$content = $this->domainController->createIPContent(ip: $content);
}
}
if (empty($name) && empty($content)) {
echo 'Either a new name or new content (panel or addresses) is needed.' . PHP_EOL;
exit(1);
}
if (empty($content)) {
$newDomain = new Domain(name: $name, id: $domain->getId(), content: '');
} else {
$newDomain = new Domain(name: $name, id: $domain->getId(), content: $content);
} }
$newDomain = new Domain(name: $name, id: $panelID, panelID: $a, a: $aaaa);
if ($this->domainRepository->update(domain: $newDomain) !== false) { if ($this->domainRepository->update(domain: $newDomain) !== false) {
echo 'Domain server has been updated' . PHP_EOL; echo 'Domain server has been updated' . PHP_EOL;
$this->domainController->createZoneFile(domain: $domain); $this->domainController->createSlaveZoneFile(domain: $domain);
} else { } else {
echo 'Error while updating domain server.' . PHP_EOL; echo 'Error while updating domain server.' . PHP_EOL;
} }
@ -1080,6 +1137,7 @@ class BindAPI
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->domainRepository->delete(domain: $domain);
$this->domainController->deleteZone(domain: $domain); $this->domainController->deleteZone(domain: $domain);
echo "The domain with ID $id has been deleted." . PHP_EOL; echo "The domain with ID $id has been deleted." . PHP_EOL;
} }

View File

@ -215,7 +215,7 @@ class DomainController
* *
* @return void * @return void
*/ */
public function createZoneFile(Domain $domain): void public function createSlaveZoneFile(Domain $domain): void
{ {
if ($this->config['debug']) { if ($this->config['debug']) {
$domainName = $domain->getName(); $domainName = $domain->getName();
@ -223,15 +223,16 @@ class DomainController
} }
if ($zonefile = fopen(filename: $this->localZonesDir . $domain->getName(), mode: 'w')) { if ($zonefile = fopen(filename: $this->localZonesDir . $domain->getName(), mode: 'w')) {
fputs(stream: $zonefile, data: 'zone \"' . $domain->getA() . '"' . ' IN {' . PHP_EOL); // TODO fixme soon
//fputs(stream: $zonefile, data: 'zone \"' . $domain->getA() . '"' . ' IN {' . PHP_EOL);
fputs(stream: $zonefile, data: "\ttype slave;" . PHP_EOL); fputs(stream: $zonefile, data: "\ttype slave;" . PHP_EOL);
fputs(stream: $zonefile, data: "\tfile \"" . $this->zoneCachePath . $domain->getName() . '.db";' . PHP_EOL); fputs(stream: $zonefile, data: "\tfile \"" . $this->zoneCachePath . $domain->getName() . '.db";' . PHP_EOL);
fputs(stream: $zonefile, data: "\tmasters {" . PHP_EOL); fputs(stream: $zonefile, data: "\tmasters {" . PHP_EOL);
if (!empty($a)) { if (!empty($a)) {
fputs(stream: $zonefile, data: "\t\t" . $domain->getA() . ';' . PHP_EOL); // fputs(stream: $zonefile, data: "\t\t" . $domain->getA() . ';' . PHP_EOL);
} }
if (!empty($aaaa)) { if (!empty($aaaa)) {
fputs(stream: $zonefile, data: "\t\t" . $domain->getAaaa() . ';' . PHP_EOL); // fputs(stream: $zonefile, data: "\t\t" . $domain->getAaaa() . ';' . PHP_EOL);
} }
fputs(stream: $zonefile, data: "\t};" . PHP_EOL); fputs(stream: $zonefile, data: "\t};" . PHP_EOL);
fputs(stream: $zonefile, data: "};" . PHP_EOL); fputs(stream: $zonefile, data: "};" . PHP_EOL);
@ -240,4 +241,39 @@ class DomainController
// TODO add on nameservers // TODO add on nameservers
} }
/**
* @param String $panel
*
* @return String
*/
function createPanelContent(String $panel): String
{
return json_encode(value: [
'panel' => $panel
]
);
}
/**
* @param String $panel
*
* @return false|string
*/
function createIPContent(array $ip): String
{
$result = [];
if (!empty($ip['a'])) {
$result['a'] = $ip['a'];
}
if (!empty($ip['aaaa'])) {
$result['aaaa'] = $ip['aaaa'];
}
return json_encode(value: [
'custom' => $result
]
);
}
} }

View File

@ -9,6 +9,9 @@ use App\Repository\ApikeyRepository;
use App\Repository\DomainRepository; use App\Repository\DomainRepository;
use DI\Container; use DI\Container;
use DI\ContainerBuilder; use DI\ContainerBuilder;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use OpenApi\Generator; use OpenApi\Generator;
use UnhandledMatchError; use UnhandledMatchError;
use function DI\autowire; use function DI\autowire;
@ -45,6 +48,8 @@ use OpenApi\Attributes as OAT;
)] )]
class RequestController class RequestController
{ {
private Logger $log;
//private DatabaseConnection $databaseConnection; //private DatabaseConnection $databaseConnection;
private DomainRepository $domainRepository; private DomainRepository $domainRepository;
private ApikeyRepository $apikeyRepository; private ApikeyRepository $apikeyRepository;
@ -65,9 +70,25 @@ class RequestController
{ {
$this->requestMethod = strtoupper(string: $requestMethod); $this->requestMethod = strtoupper(string: $requestMethod);
$dateFormat = "Y:m:d H:i:s";
$output = "%datetime% %channel%.%level_name% %message%\n"; // %context% %extra%
$formatter = new LineFormatter(format: $output, dateFormat: $dateFormat);
$stream = new StreamHandler(stream: dirname(path: __DIR__, levels: 2) . '/bindAPI.log');
$stream->setFormatter(formatter: $formatter);
$this->log = new Logger(name: 'bindAPI');
$this->log->pushHandler(handler: $stream);
$containerBuilder = new ContainerBuilder(); $containerBuilder = new ContainerBuilder();
$containerBuilder->addDefinitions([ $containerBuilder->addDefinitions([
DatabaseConnection::class => autowire()->constructorParameter(parameter: 'config', value: $this->config), DatabaseConnection::class => autowire()->constructorParameter(parameter: 'config', value: $this->config),
DomainRepository::class => autowire()
->constructorParameter(parameter: 'config', value: $this->config)
->constructorParameter(parameter: 'log', value: $this->log),
]); ]);
$this->container = $containerBuilder->build(); $this->container = $containerBuilder->build();
@ -112,11 +133,9 @@ class RequestController
$resultDomain = []; $resultDomain = [];
foreach ($domains as $singleDomain) { foreach ($domains as $singleDomain) {
$domain = [ $domain = [
'id' => $singleDomain->getId(), 'id' => $singleDomain->getId(),
'name' => $singleDomain->getName(), 'name' => $singleDomain->getName(),
'panel_id' => $singleDomain->getPanelId(), 'content' => json_decode(json: $singleDomain->getContent())
'a' => $singleDomain->getA(),
'aaaa' => $singleDomain->getAaaa()
]; ];
$resultDomain[] = $domain; $resultDomain[] = $domain;
} }
@ -292,16 +311,12 @@ class RequestController
*/ */
public function handleDomainGetRequest(): void public function handleDomainGetRequest(): void
{ {
if (empty($this->uri[3])) { if ($this->uri[3] == 'name') {
$this->handleAllDomainsGetRequest(); if ($result = $this->domainRepository->findByName(name: $this->uri[4])) {
} else {
if ($result = $this->domainRepository->findById(id: $this->uri[3])) {
$domain = [ $domain = [
'id' => $result->getId(), 'id' => $result->getId(),
'name' => $result->getName(), 'name' => $result->getName(),
'panel_id' => $result->getPanelId(), 'content' => json_decode(json: $result->getContent())
'a' => $result->getA(),
'aaaa' => $result->getAaaa()
]; ];
$this->result = $domain; $this->result = $domain;
} else { } else {
@ -309,127 +324,142 @@ class RequestController
$this->status = "404 Not Found "; $this->status = "404 Not Found ";
$this->message = "The specified domain was not found."; $this->message = "The specified domain was not found.";
} }
}
}
/**
* @return void
*/
public function handleDomainPostRequest(): void
{
$name = $_POST['name'] ?? '';
$panelID = intval(value: $_POST['panel_id'] ?? 0);
$a = $_POST['a'] ?? '';
$aaaa = $_POST['aaaa'] ?? '';
if (empty($name)) {
$this->header = "400 Bad Request";
$this->status = "400 Bad Request";
$this->message = "A name is required";
} else { } else {
if (empty($a) && empty($aaaa) && empty($panelID)) { if (empty($this->uri[3])) {
$this->header = "400 Bad Request"; $this->handleAllDomainsGetRequest();
$this->status = "400 Bad Request";
$this->message = "At least one IP address or panel ID is required.";
} else { } else {
if ($this->domainRepository->findByName(name: $name)) { if ($result = $this->domainRepository->findById(id: $this->uri[3])) {
$this->header = "400 Bad request"; $domain = [
$this->status = "400 Bad request"; 'id' => $result->getId(),
$this->message = "Domain: $name already exists."; 'name' => $result->getName(),
'content' => json_decode(json: $result->getContent())
];
$this->result = $domain;
} else { } else {
$domain = new Domain(name: $name, panelID: $panelID, a: $a, aaaa: $aaaa); $this->header = "404 Not Found ";
$result = $this->domainRepository->insert(domain: $domain); $this->status = "404 Not Found ";
$this->status = "201 Created"; $this->message = "The specified domain was not found.";
$this->message = $result;
} }
} }
} }
} }
/**
* @return void
*/
public
function handleDomainPutRequest(): void
{
$putData = fopen(filename: 'php://input', mode: 'r');
$data = fread(stream: $putData, length: 512);
$params = explode(separator: '&', string: $data);
foreach ($params as $param) {
[$key, $value] = explode(separator: '=', string: $param);
$put[$key] = $value;
}
$id = $put['id'] ?? 0;
$name = $put['name'] ?? '';
$panelID = $put['panel_id'] ?? "";
$a = $put['a'] ?? "";
$aaaa = $put['aaaa'] ?? "";
if ($id == 0) { /**
$this->status = "400 Bad Request"; * @return void
$this->message = "An ID is required"; */
} else { public
if (!$this->domainRepository->findByID(id: $id)) { function handleDomainPostRequest(): void
$this->status = "404 Not Found"; {
$this->message = "Domain with ID : $id doesn't exist."; $name = $_POST['name'] ?? '';
$panelID = intval(value: $_POST['panel_id'] ?? 0);
$content = $_POST['content'] ?? '';
if (empty($name)) {
$this->header = "400 Bad Request";
$this->status = "400 Bad Request";
$this->message = "A name is required";
} else { } else {
// TODO not required, as we rely on the ID if (empty($a) && empty($aaaa) && empty($panelID)) {
if (empty($name)) { $this->header = "400 Bad Request";
$this->status = "400 Bad Request"; $this->status = "400 Bad Request";
$this->message = "A name is required"; $this->message = "At least one IP address or panel ID is required.";
} else { } else {
if (empty($a) && empty($aaaa)) { if ($this->domainRepository->findByName(name: $name)) {
$this->status = "400 Bad Request"; $this->header = "400 Bad request";
$this->message = "At least one IP address is required."; $this->status = "400 Bad request";
$this->message = "Domain: $name already exists.";
} else { } else {
$domain = new Domain(name: $panelID, id: $id, panelID: $name, a: $a, aaaa: $aaaa); $domain = new Domain(name: $name, content: $content);
$this->domainRepository->update(domain: $domain); $result = $this->domainRepository->insert(domain: $domain);
$this->header = "201 Updated"; $this->status = "201 Created";
$this->status = "201 Updated"; $this->message = $result;
$this->message = "201 Updated";
} }
} }
} }
} }
}
/**
* @return void
*/
public
function handleDomainDeleteRequest(): void
{
$deleteData = fopen(filename: 'php://input', mode: 'r');
$data = fread(stream: $deleteData, length: 512);
$params = explode(separator: '&', string: $data);
foreach ($params as $param) {
[$key, $value] = explode(separator: '=', string: $param);
$delete[$key] = $value;
}
$id = $delete['id'] ?? 0; /**
* @return void
if ($id == 0) { */
$this->header = "400 Bad Request"; public
$this->status = "400 Bad Request"; function handleDomainPutRequest(): void
$this->message = "You need to supply an ID."; {
} else { $putData = fopen(filename: 'php://input', mode: 'r');
$data = fread(stream: $putData, length: 512);
$params = explode(separator: '&', string: $data);
if (!$domain = $this->domainRepository->findByID(id: $id)) { foreach ($params as $param) {
$this->header = "400 Bad Request"; [$key, $value] = explode(separator: '=', string: $param);
$put[$key] = $value;
}
$id = $put['id'] ?? 0;
$name = $put['name'] ?? '';
$content = $put['content'] ?? "";
if ($id == 0) {
$this->status = "400 Bad Request"; $this->status = "400 Bad Request";
$this->message = "There is no domain with ID $id."; $this->message = "An ID is required";
} else { } else {
$this->domainRepository->delete(domain: $domain); if (!$this->domainRepository->findByID(id: $id)) {
$this->header = "204 No content."; $this->status = "404 Not Found";
$this->status = "204 No content."; $this->message = "Domain with ID : $id doesn't exist.";
$this->message = "The domain $id has been deleted."; } else {
// TODO not required, as we rely on the ID
if (empty($name)) {
$this->status = "400 Bad Request";
$this->message = "A name is required";
} else {
if (empty($a) && empty($aaaa)) {
$this->status = "400 Bad Request";
$this->message = "At least one IP address is required.";
} else {
$domain = new Domain(name: $name, id: $id, content: $content);
$this->domainRepository->update(domain: $domain);
$this->header = "201 Updated";
$this->status = "201 Updated";
$this->message = "201 Updated";
}
}
}
} }
} }
/**
* @return void
*/
public
function handleDomainDeleteRequest(): void
{
$deleteData = fopen(filename: 'php://input', mode: 'r');
$data = fread(stream: $deleteData, length: 512);
$params = explode(separator: '&', string: $data);
foreach ($params as $param) {
[$key, $value] = explode(separator: '=', string: $param);
$delete[$key] = $value;
}
$id = $delete['id'] ?? 0;
if ($id == 0) {
$this->header = "400 Bad Request";
$this->status = "400 Bad Request";
$this->message = "You need to supply an ID.";
} else {
if (!$domain = $this->domainRepository->findByID(id: $id)) {
$this->header = "400 Bad Request";
$this->status = "400 Bad Request";
$this->message = "There is no domain with ID $id.";
} else {
$this->domainRepository->delete(domain: $domain);
$this->header = "204 No content.";
$this->status = "204 No content.";
$this->message = "The domain $id has been deleted.";
}
}
}
} }
}

View File

@ -2,41 +2,38 @@
namespace App\Entity; namespace App\Entity;
use App\Enums\PanelType;
/** /**
* *
*/ */
class Domain class Domain
{ {
private int $id;
private int $panelID;
private String $name;
private String $a;
private String $aaaa;
public function __construct(String $name, int $id = 0, int $panelID = 0, String $a = '', String $aaaa = '') /**
*/
public function __construct(private string $name, private int $id = 0, private string $content = '')
{}
/**
* @return String
*/
public function getName(): string
{
return $this->name;
}
/**
* @param String $name
*/
public function setName(string $name): void
{ {
$this->id = $id;
$this->panelID = $panelID;
$this->name = $name; $this->name = $name;
$this->a = $a;
$this->aaaa = $aaaa;
} }
/**
* @return String
*/
public function getA(): string
{
return $this->a;
}
/**
* @return String
*/
public function getAaaa(): string
{
return $this->aaaa;
}
/** /**
* @return int * @return int
@ -55,51 +52,20 @@ class Domain
} }
/** /**
* @return String * @return string
*/ */
public function getName(): string public function getContent(): string
{ {
return $this->name; return $this->content;
} }
/** /**
* @return int * @param string $content
*/ */
public function getPanelID(): int public function setContent(string $content): void
{ {
return $this->panelID; $this->content = $content;
} }
/**
* @param int $panelID
*/
public function setPanelID(int $panelID): void
{
$this->panelID = $panelID;
}
/**
* @param String $name
*/
public function setName(string $name): void
{
$this->name = $name;
}
/**
* @param String $a
*/
public function setA(string $a): void
{
$this->a = $a;
}
/**
* @param String $aaaa
*/
public function setAaaa(string $aaaa): void
{
$this->aaaa = $aaaa;
}
} }

View File

@ -14,7 +14,12 @@ use PDOException;
class DomainRepository class DomainRepository
{ {
public function __construct(private DatabaseConnection $databaseConnection, private array $config, private Logger $log) public function __construct(private DatabaseConnection $databaseConnection, private array $config, private Logger $log)
{} {
if ($this->config['debug']) {
$this->log->debug(message: "DomainRepository::__construct()");
}
}
/** /**
@ -22,9 +27,13 @@ class DomainRepository
*/ */
public function findAll(): array public function findAll(): array
{ {
if ($this->config['debug']) {
$this->log->debug(message: "findAll()");
}
$domains = []; $domains = [];
$sql = " $sql = "
SELECT id, name, panel_id, a, aaaa SELECT id, name, content
FROM " . DatabaseConnection::TABLE_DOMAINS . " FROM " . DatabaseConnection::TABLE_DOMAINS . "
ORDER BY name"; ORDER BY name";
@ -32,7 +41,9 @@ class DomainRepository
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->execute(); $statement->execute();
while ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) { while ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
$domain = new Domain(name: $result['name'], id: $result['id'], panelID: $result['panel_id'], a: $result['a'], aaaa: $result['aaaa']); //print_r($result);
//die();
$domain = new Domain(name: $result['name'], id: $result['id'], content: $result['content']);
$domains[] = $domain; $domains[] = $domain;
} }
return $domains; return $domains;
@ -49,8 +60,12 @@ class DomainRepository
*/ */
public function findByID(int $id): bool|Domain public function findByID(int $id): bool|Domain
{ {
if ($this->config['debug']) {
$this->log->debug(message: "findById($id)");
}
$sql = " $sql = "
SELECT id, name, panel_id, a, aaaa SELECT id, name, content
FROM . " . DatabaseConnection::TABLE_DOMAINS . " FROM . " . DatabaseConnection::TABLE_DOMAINS . "
WHERE id = :id"; WHERE id = :id";
@ -60,7 +75,7 @@ class DomainRepository
$statement->execute(); $statement->execute();
if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) { if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
return new Domain(name: $result['name'], panelID: $result['panel_id'], a: $result['a'], aaaa: $result['aaaa']); return new Domain(name: $result['name'], id: $result['id'], content: $result['content']);
} else { } else {
return false; return false;
} }
@ -77,8 +92,12 @@ class DomainRepository
*/ */
public function findByName(string $name): Domain|bool public function findByName(string $name): Domain|bool
{ {
if ($this->config['debug']) {
$this->log->debug(message: "findByName($name)");
}
print("xxx: $name");
$sql = " $sql = "
SELECT id, name, panel_id, a, aaaa SELECT id, name, content
FROM " . DatabaseConnection::TABLE_DOMAINS . " FROM " . DatabaseConnection::TABLE_DOMAINS . "
WHERE name = :name"; WHERE name = :name";
@ -87,12 +106,10 @@ class DomainRepository
$statement->bindParam(param: ':name', var: $name); $statement->bindParam(param: ':name', var: $name);
$statement->execute(); $statement->execute();
if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) { if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
return new Domain(name: $result['name'], id: $result['id'], panelID: $result['panel_id'], a: $result['a'], aaaa: $result['aaaa']); return new Domain(name: $result['name'], id: $result['id'], content: $result['content']);
} else { } else {
return false; return false;
} }
} catch (PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
} }
@ -112,20 +129,15 @@ class DomainRepository
} }
$sql = " $sql = "
INSERT INTO " . DatabaseConnection::TABLE_DOMAINS . " (name, panel_id, a, aaaa) INSERT INTO " . DatabaseConnection::TABLE_DOMAINS . " (name, content)
VALUES (:name, :panel_id, :a, :aaaa)"; VALUES (:name, :content)";
try { try {
$name = $domain->getName(); $name = $domain->getName();
$panelID = $domain->getPanelID(); $content = $domain->getContent();
$a = $domain->getA();
$aaaa = $domain->getAaaa();
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':name', var: $name); $statement->bindParam(param: ':name', var: $name);
$statement->bindParam(param: ':panel_id', var: $panelID); $statement->bindParam(param: ':content', var: $content);
$statement->bindParam(param: ':a', var: $a);
$statement->bindParam(param: ':aaaa', var: $aaaa);
$statement->execute(); $statement->execute();
return $this->databaseConnection->getConnection()->lastInsertId(); return $this->databaseConnection->getConnection()->lastInsertId();
@ -147,47 +159,31 @@ class DomainRepository
$this->log->debug(message: "update($domainName)"); $this->log->debug(message: "update($domainName)");
} }
$current = $this->findByID(id: $domain->getId()); $id = $domain->getId();
$current = $this->findByID(id: $id);
/* doesn't work
$statement = "
INSERT INTO domains(id, name, a, aaaa)
VALUES(:id, :name, :a, :aaaa)
ON DUPLICATE KEY UPDATE
name=COALESCE(VALUES(name), :name),
a=COALESCE(:a, a),
aaaa=COALESCE(:aaaa, aaaa)";
*/
if (empty($domain->getName())) { if (empty($domain->getName())) {
$name = $current['name']; $name = $current->getName();
} else {
$name = $domain->getName();
} }
if (empty($domain->getPanelID())) { if (empty($domain->getContent())) {
$panelID = $current['panel_id']; $content = $current->getContent();
} } else {
$panelID = intval(value: $panelID); $content = $domain->getContent();
if (empty($a)) {
$a = $current['a'];
}
if (empty($aaaa)) {
$aaaa = $current['aaaa'];
} }
$sql = " $sql = "
UPDATE " . DatabaseConnection::TABLE_DOMAINS . " SET UPDATE " . DatabaseConnection::TABLE_DOMAINS . " SET
name = :name, name = :name,
panel_id = :panel_id, content = :content
a = :a,
aaaa = :aaaa
WHERE id = :id"; WHERE id = :id";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: 'id', var: $id); $statement->bindParam(param: 'id', var: $id);
$statement->bindParam(param: 'name', var: $name); $statement->bindParam(param: 'name', var: $name);
$statement->bindParam(param: 'panel_id', var: $panelID); $statement->bindParam(param: 'content', var: $content);
$statement->bindParam(param: 'a', var: $a);
$statement->bindParam(param: 'aaaa', var: $aaaa);
$statement->execute(); $statement->execute();
return $statement->rowCount(); return $statement->rowCount();