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