addDefinitions([ DatabaseConnection::class => autowire()->constructorParameter(parameter: 'config', value: $this->config), ]); $this->container = $containerBuilder->build(); //$this->databaseConnection = $this->container->get(name: DatabaseConnection::class); $this->panelController = $this->container->get(name: PanelController::class); $this->checkController = $this->container->get(name: CheckController::class); $this->nameserverController = $this->container->get(name: NameserverController::class); $this->domainController = $this->container->get(name: DomainController::class); $this->domainRepository = $this->container->get(name: DomainRepository::class); $this->nameserverRepository = $this->container->get(name: NameserverRepository::class); $this->apikeyRepository = $this->container->get(name: ApikeyRepository::class); $this->panelRepository = $this->container->get(name: PanelRepository::class); } function runCommand() { if ($this->argumentsCount < 1) { $this->showUsage(); exit(0); } if (str_contains(haystack: $this->arguments[0], needle: ':')) { [$command, $subcommand] = explode(separator: ':', string: $this->arguments[0]); } else { $command = $this->arguments[0]; $subcommand = ''; } try { match ($command) { '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; exit(1); } } /** * @return void */ function showUsage(): void { echo COLOR_YELLOW . 'Usage:' . PHP_EOL; echo COLOR_DEFAULT . "\t./bin/console {options} {arguments}" . PHP_EOL . PHP_EOL; echo COLOR_YELLOW . 'Options:' . PHP_EOL; echo COLOR_GREEN . "\t-v, --version\t\t" . COLOR_DEFAULT . "Display the version of the API" . PHP_EOL; echo COLOR_GREEN . "\t-V, --verbose\t\t" . COLOR_DEFAULT . "All :list command are auto-verbose" . PHP_EOL . PHP_EOL; echo COLOR_YELLOW . "check" . COLOR_DEFAULT . "\t health checks the system can perform" . PHP_EOL; echo COLOR_GREEN . "\t check:permissions" . PHP_EOL; echo COLOR_GREEN . "\t check:panels {ID} {fix=yes}" . PHP_EOL; echo COLOR_GREEN . "\t check:domains {ID} {fix=yes}" . PHP_EOL; echo COLOR_YELLOW . "panels" . COLOR_DEFAULT . "\t all Keyhelp systems configured" . PHP_EOL; echo COLOR_GREEN . "\t panels:list" . PHP_EOL; echo COLOR_GREEN . "\t panels:create {A=} {AAAA=} {apikey=}" . PHP_EOL; echo COLOR_GREEN . "\t panels:update {name=} {A=} {AAAA=} {apikey=}" . PHP_EOL; echo COLOR_GREEN . "\t panels:delete" . PHP_EOL; echo COLOR_GREEN . "\t panels:apiping {}" . PHP_EOL; echo COLOR_YELLOW . "nameservers" . COLOR_DEFAULT . " available nameservers" . PHP_EOL; echo COLOR_GREEN . "\t nameservers:list" . PHP_EOL; echo COLOR_GREEN . "\t nameservers:create {A=} {AAAA=} {apikey=}" . PHP_EOL; echo COLOR_GREEN . "\t nameservers:update {name=} {panel_id=} {A=} {AAAA=} {apikey=}" . PHP_EOL; echo COLOR_GREEN . "\t nameservers:delete " . PHP_EOL; echo COLOR_GREEN . "\t nameservers:apiping {}" . 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:create {A=} {AAAA=}" . PHP_EOL; echo COLOR_GREEN . "\t domains:update {name=} {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; echo COLOR_GREEN . "\t apikeys:list" . PHP_EOL; echo COLOR_GREEN . "\t apikeys:create {name=}" . PHP_EOL; echo COLOR_GREEN . "\t apikeys:update name=" . PHP_EOL; echo COLOR_GREEN . "\t apikeys:delete " . PHP_EOL; echo PHP_EOL . "\033[39me.g. ./bin/console apikeys:list" . PHP_EOL; } function handleChecks(string $subcommand) { try { match ($subcommand) { 'permissions' => $this->handleCheckPermissions(), 'panels' => $this->handleCheckPanels(), }; } catch (UnhandledMatchError) { echo 'Unknown action: ' . $subcommand . PHP_EOL; } catch (DependencyException|NotFoundException $e) { echo $e->getMessage(); } } /** * @throws \DI\DependencyException * @throws \DI\NotFoundException */ function handleCheckPermissions() { $this->domainController->checkPermissions(); } /** * @throws \DI\DependencyException * @throws \DI\NotFoundException */ function handleCheckPanels() { $id = intval(value: $this->arguments[1] ?? 0); if ($id != 0) { if ($panel = $this->panelController->findByID(id: $id)) { $this->checkSinglePanel(panel: $panel); } else { echo "Unknown panel ID: $id" . PHP_EOL; } } else { echo "check all …" . PHP_EOL; $panels = $this->panelRepository->findAll(); foreach ($panels as $panel) { $this->checkSinglePanel(panel: $panel); } } } /** * @param array $panel * * @return void */ public function checkSinglePanel(Panel $panel): void { echo COLOR_DEFAULT . 'Keyhelp-Panel: ' . COLOR_YELLOW . $panel->getName() . PHP_EOL; if (!empty($panel->getAaaa())) { try { $result = $this->checkController->sendCommand( requestType: 'GET', serverName: $panel->getName(), versionIP: 6, apiKey: $panel->getApikey(), command: 'domains', serverType: 'panel' ); } catch (DependencyException|NotFoundException $e) { echo $e->getMessage(); } } else { try { $result = $this->checkController->sendCommand( requestType: 'GET', serverName: $panel->getName(), versionIP: 4, apiKey: $panel->getApikey(), command: 'domains', serverType: 'panel'); } catch (DependencyException|NotFoundException $e) { echo $e->getMessage(); } } if (!empty($result['error'])) { echo $result['data'] . PHP_EOL; exit(1); } if (!empty($result['data'])) { $domains = json_decode(json: $result['data']); } else { echo 'No domains found' . PHP_EOL; exit(1); } $maxDomainName = 0; // TODO this is ugly code ↓↓↓ foreach ($domains as $domain) { if ($this->isValidSecondLevelDomain(domainName: $domain->domain, panel: $panel->getName(), parent: $domain->id_parent_domain) && (strlen(string: $domain->domain) > $maxDomainName)) { $maxDomainName = strlen(string: $domain->domain); } } $domainCount = 0; foreach ($domains as $domain) { if ($this->isValidSecondLevelDomain(domainName: $domain->domain, panel: $panel->getName(), parent: $domain->id_parent_domain)) { echo COLOR_DEFAULT . "Domain: " . COLOR_YELLOW . str_pad(string: $domain->domain, length: $maxDomainName); $this->checkNS(domainName: $domain->domain, panel: $panel); $domainCount++; } } if ($domainCount == 0) { echo 'No second level domains found.' . PHP_EOL; } } function isValidSecondLevelDomain(string $domainName, string $panel, int $parent): bool { // subdomain if ($parent != 0) { return false; } // system domain if (str_contains(haystack: $domainName, needle: $panel)) { return false; } // valid second level domain if (!Validator::endsWithTld(value: $domainName)) { return false; } // no second level domain if (substr_count(haystack: $domainName, needle: '.') > 1) { return false; } return true; } /** * @param String $domainName * @param $panel * * @return void */ function checkNS(string $domainName, $panel) { try { $nameServers = $this->nameserverRepository->findAll(); } catch (DependencyException|NotFoundException $e) { echo $e->getMessage(); exit(1); } foreach ($nameServers as $nameServer) { try { echo COLOR_YELLOW . ' ' . $nameServer->getName(); } catch (DependencyException|NotFoundException $e) { echo $e->getMessage(); exit(1); } try { if (!empty($nameServer->getName())) { $result = $this->checkController->sendCommand( requestType: 'GET', serverName: $nameServer->getName(), versionIP: 6, apiKey: $nameServer->getApikey(), command: 'domains/' . $domainName, serverType: 'nameserver'); } else { $result = $this->checkController->sendCommand( requestType: 'GET', serverName: $nameServer->getName(), versionIP: 4, apiKey: $nameServer->getApikey(), command: 'domains', serverType: 'nameserver' . $domainName); } } catch (DependencyException|NotFoundException $e) { echo $e->getMessage(); exit(1); } if ($result['header'] == 200) { echo COLOR_GREEN . ' OK'; } else { echo COLOR_RED . ' missing' . 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->checkController->sendCommand( requestType: 'POST', serverName: $nameServer['name'], versionIP: 6, apiKey: $nameServer['apikey'], command: 'domains', serverType: 'nameserver', body: $body); } else { $this->checkController->sendCommand( requestType: 'POST', serverName: $nameServer['name'], versionIP: 4, apiKey: $nameServer['apikey'], command: 'domains', serverType: 'nameserver', body: $body); } } catch (DependencyException|NotFoundException $e) { echo $e->getMessage(); exit(1); } } } } echo PHP_EOL; } /** * @return array */ public function parseArguments(): array { $arguments = []; foreach ($this->arguments as $argument) { if (str_contains(haystack: $argument, needle: '=')) { [$key, $value] = explode(separator: '=', string: $argument); $arguments[strtolower(string: $key)] = $value; } else { $arguments[strtolower(string: $argument)] = $argument; } } return $arguments; } /** * @param string $subcommand * * @return void */ public function handlePanels(string $subcommand): void { try { match ($subcommand) { '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; } catch (DependencyException|NotFoundException $e) { echo $e->getMessage(); } } /** * @return void */ function handlePanelsCreate(): void { $name = $this->arguments[1] ?? ''; if (empty($name)) { echo 'You need to supply the panel name.' . PHP_EOL; exit(1); } $filteredName = filter_var(value: $name, filter: FILTER_VALIDATE_DOMAIN, options: FILTER_FLAG_HOSTNAME); if (!empty($filteredName) && str_contains(haystack: $filteredName, needle: '.')) { $name = $filteredName; } else { echo "$name is no valid DNS domain name." . PHP_EOL; exit(1); } $arguments = $this->parseArguments(); $a = $arguments['a'] ?? ''; $aaaa = $arguments['aaaa'] ?? ''; if (empty($a) && empty($aaaa)) { echo 'At least one IP address is required.' . PHP_EOL; exit(0); } $apikey = $arguments['apikey'] ?? ''; try { if ($this->panelRepository->findByName(name: $name)) { echo "Panel: $name already exists." . PHP_EOL; exit(1); } else { $result = $this->panelRepository->insert(name: $name, a: $a, aaaa: $aaaa, apikey: $apikey); echo "Panel $name has been created with id $result" . PHP_EOL; exit(0); } } catch (DependencyException|NotFoundException $e) { echo $e->getMessage(); exit(1); } } /** * @return void */ function handlePanelsList(): void { echo 'All available panels:' . PHP_EOL; try { $panels = $this->panelRepository->findAll(); } catch (DependencyException|NotFoundException $e) { echo $e->getMessage(); } if (!empty($panels)) { $table = new ConsoleTable(); $table->setHeaders(content: ['ID', 'Name', 'A', 'AAAA', 'API Key']); foreach ($panels as $panel) { $row = []; try { $token = strtok(string: $panel->getApikey(), token: '.'); $row[] = $panel->getID(); $row[] = $panel->getName(); $row[] = $panel->getA(); $row[] = $panel->getAaaa(); $row[] = $token; } catch (DependencyException|NotFoundException $e) { echo $e->getMessage(); exit(1); } $table->addRow(data: $row); } $table->setPadding(value: 2); $table->display(); } else { echo 'No panels found.' . PHP_EOL; exit(1); } exit(0); } /** * @throws \DI\DependencyException * @throws \DI\NotFoundException */ function handlePanelsUpdate() { $arguments = $this->parseArguments(); $id = $this->arguments[1] ?? 0; $name = $arguments['name'] ?? ''; $a = $arguments['a'] ?? ''; $aaaa = $arguments['aaaa'] ?? ''; $apikey = $arguments['apikey'] ?? ''; if ($id == 0) { echo 'An ID is required' . PHP_EOL; exit(1); } if (!$this->panelController->findByID(id: $id)) { echo "Panel with ID : $id doesn't exist." . PHP_EOL; exit(1); } 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; } } /** * @throws \DI\DependencyException * @throws \DI\NotFoundException */ function handlePanelsDelete() { if (empty($this->arguments[1])) { echo "You need to supply an ID." . PHP_EOL; exit(1); } $id = intval(value: $this->arguments[1]) ?? 0; if ($id == 0) { echo "Panel with ID $id not found." . PHP_EOL; exit(1); } if (!$this->panelRepository->findByID(id: $id)) { echo "There is no panel with ID $id." . PHP_EOL; exit(1); } $this->panelRepository->delete(id: $id); echo "The panel with ID $id has been deleted." . PHP_EOL; } /** * @throws \DI\NotFoundException * @throws \DI\DependencyException */ function handleAPIPing(string $type) { $error = false; $id = $this->getId(); if ($id != 0) { 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 $type ID: $id" . PHP_EOL; } $error = true; } } else { 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; } } } echo PHP_EOL; if ($error) { exit(1); } else { exit(0); } } /** * @return int|void */ public function getId() { 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; } return $id; } /** * @param array $server * @param String $type * * @return bool */ public function checkPing(array $server, string $type): bool { $error = false; try { 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'); } } catch (DependencyException|NotFoundException $e) { echo $e->getMessage(); exit(1); } 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) . ' '; } try { if ($result = $this->checkController->sendCommand( requestType: 'GET', serverName: $server['name'], versionIP: 4, apiKey: $server['apikey'], command: 'ping', serverType: $type)) { if ($this->config['verbose']) { if ($result['data'] == 'pong') { echo COLOR_GREEN . $result['data']; } else { echo COLOR_BLUE . 'skip'; } } } else { $error = true; } } catch (DependencyException|NotFoundException $e) { echo $e->getMessage(); } } $aaaa = $server['aaaa'] ?? ''; if (!empty($aaaa)) { if ($this->config['verbose']) { echo COLOR_DEFAULT . ' ' . str_pad(string: $aaaa, length: $maxAAAA, pad_type: STR_PAD_LEFT) . ' '; } try { if ($result = $this->checkController->sendCommand( requestType: 'GET', serverName: $server['name'], versionIP: 6, apiKey: $server['apikey'], command: 'ping', serverType: $type)) { if ($this->config['verbose']) { if ($result['data'] == 'pong') { echo COLOR_GREEN . $result['data']; } else { echo COLOR_BLUE . $result['data']; // TODO 'skip'; } } } else { $error = true; } } catch (DependencyException|NotFoundException $e) { echo $e->getMessage(); exit(1); } } if ($this->config['verbose']) { echo PHP_EOL; } return $error; } /** * @param string $subcommand * * @return void */ public function handleApiKeys(string $subcommand): void { try { match ($subcommand) { 'create' => $this->handleApikeysCreate(), 'list' => $this->handleApikeysList(), 'update' => $this->handleApikeysUpdate(), 'delete' => $this->handleApikeysDelete(), }; } catch (UnhandledMatchError) { echo 'Unknown action: ' . $subcommand . PHP_EOL; } catch (DependencyException|NotFoundException $e) { echo $e->getMessage(); exit(1); } } /** * @return void */ function handleApikeysCreate(): void { $arguments = $this->parseArguments(); $name = $arguments['name'] ?? ''; try { $result = $this->apikeyRepository->create(name: $name); } catch (DependencyException|NotFoundException $e) { echo $e->getMessage(); exit(1); } echo 'API key ' . $result['row'] . ' has been generated. Store it in a save place, it cannot be recovered.' . PHP_EOL; echo "\033[32m\t" . $result['tokenPrefix'] . '.' . $result['key'] . PHP_EOL; exit(0); } /** * @return void */ function handleApikeysList(): void { echo 'All valid API keys:' . PHP_EOL; try { $keys = $this->apikeyRepository->findAll(); } catch (DependencyException|NotFoundException $e) { echo $e->getMessage(); exit(1); } if (!empty($keys)) { $table = new ConsoleTable(); $table->setHeaders(content: ['ID', 'Name', 'API key prefix']); foreach ($keys as $key) { $row = []; try { $row[] = $key->getID(); $row[] = $key->getName(); $row[] = $key->getApiTokenPrefix(); } catch (DependencyException|NotFoundException $e) { echo $e->getMessage(); exit(1); } $table->addRow(data: $row); } $table->setPadding(value: 2); $table->display(); } else { echo 'No keys found.' . PHP_EOL; } exit(0); } /** * @throws \DI\DependencyException * @throws \DI\NotFoundException */ function handleApikeysUpdate() { $arguments = $this->parseArguments(); $id = $this->arguments[1] ?? 0; $name = $arguments['name'] ?? ''; if ($id == 0) { echo 'An ID is required' . PHP_EOL; exit(1); } if (empty($name)) { echo 'You need to supply the new name.' . PHP_EOL; exit(1); } if (!$this->apikeyRepository->findByID(id: intval(value: $id))) { echo "Apikeys with ID : $id doesn't exist." . PHP_EOL; exit(1); } if ($this->apikeyRepository->update(id: intval(value: $id), name: $name) !== false) { echo 'Apikey has been updated' . PHP_EOL; } else { echo 'Error while updating apikey.' . PHP_EOL; } } /** * @return void */ function handleApikeysDelete(): void { $id = intval(value: $this->arguments[1] ?? 0); if ($id == 0) { echo 'You need to add the ID of the API key.' . PHP_EOL; exit(1); } try { if ($this->apikeyRepository->findByID(id: $id)) { $this->apikeyRepository->delete(id: $id); echo 'API key ' . $id . ' has been deleted.' . PHP_EOL; exit(0); } else { echo 'Unknown ID: ' . $id . PHP_EOL; exit(1); } } catch (DependencyException|NotFoundException $e) { echo $e->getMessage(); } } /** * @param string $subcommand * * @return void */ public function handleDomains(string $subcommand): void { try { match ($subcommand) { 'list' => $this->handleDomainsList(), 'create' => $this->handleDomainsCreate(), 'update' => $this->handleDomainsUpdate(), 'delete' => $this->handleDomainsDelete(), }; } catch (UnhandledMatchError) { echo "Unknown Command: $subcommand" . PHP_EOL; exit(1); } catch (DependencyException|NotFoundException $e) { echo $e->getMessage(); } } /** * @return void */ function handleDomainsList(): void { echo 'All available domains:' . PHP_EOL; try { $domains = $this->domainRepository->findAll(); } catch (DependencyException|NotFoundException $e) { echo $e->getMessage(); exit(1); } if (!empty($domains)) { $table = new ConsoleTable(); $table->setHeaders(content: ['ID', 'Name', 'Panel', 'A', 'AAAA']); /** @var Domain $domain */ foreach ($domains as $domain) { $row = []; try { $row[] = $domain->getId(); $row[] = $domain->getName(); $row[] = $domain->getPanelID(); $row[] = $domain->getA(); $row[] = $domain->getAAAA(); } catch (DependencyException|NotFoundException $e) { echo $e->getMessage(); } $table->addRow(data: $row); } $table->setPadding(value: 2); $table->display(); } exit(0); } /** * @return void */ function handleDomainsCreate(): void { $name = $this->arguments[1] ?? ""; if (empty($name)) { echo 'You need to supply the domain name.' . PHP_EOL; exit(1); } $filteredName = filter_var(value: $name, filter: FILTER_VALIDATE_DOMAIN, options: FILTER_FLAG_HOSTNAME); if (!empty($filteredName) && str_contains(haystack: $filteredName, needle: '.')) { $name = $filteredName; } else { echo "$name is no valid DNS domain name." . PHP_EOL; exit(1); } $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; exit(0); } try { if ($this->domainRepository->findByName(name: $name)) { echo "Domain: $name already exists." . PHP_EOL; exit(1); } else { $result = $this->domainRepository->insert(name: $name, panelID: $panelID, a: $a, aaaa: $aaaa); echo "Domain $name has been created with id $result" . PHP_EOL; if (!empty($panelID)) { if ($panel = $this->panelController->findByID(id: $panelID)) { $a = $panel['a']; $aaaa = $panel['aaaa']; } } $this->domainController->createZoneFile(name: $name, a: $a, aaaa: $aaaa); exit(0); } } catch (DependencyException|NotFoundException $e) { echo $e->getMessage(); } } /** * @throws \DI\DependencyException * @throws \DI\NotFoundException */ function handleDomainsUpdate() { $arguments = $this->parseArguments(); $id = intval(value: $this->arguments[1] ?? 0); $name = $arguments['name'] ?? ''; $panelID = intval(value: $arguments['panel_id'] ?? 0); $a = $arguments['a'] ?? ''; $aaaa = $arguments['aaaa'] ?? ''; if ($id == 0) { echo 'An ID is required' . PHP_EOL; exit(1); } if (!$domain = $this->domainRepository->findByID(id: $id)) { echo "Domain with ID : $id doesn't exist." . PHP_EOL; exit(1); } if ($this->domainRepository->update(id: $id, name: $name, panelID: $panelID, a: $a, aaaa: $aaaa) !== false) { echo 'Domain server has been updated' . PHP_EOL; if (!empty($panelID)) { $panel = $this->panelController->findByID(id: $panelID); $a = $panel['a']; $aaaa = $panel['aaaa']; } $this->domainController->createZoneFile(name: $domain['name'], a: $a, aaaa: $aaaa); } else { echo 'Error while updating domain server.' . PHP_EOL; } } /** * @throws \DI\DependencyException * @throws \DI\NotFoundException */ function handleDomainsDelete() { if (empty($this->arguments[1])) { echo "You need to supply an ID." . PHP_EOL; exit(1); } $id = intval(value: $this->arguments[1]) ?? 0; if ($id == 0) { echo "Domain with ID $id not found." . PHP_EOL; exit(1); } if (!$this->domainRepository->findByID(id: $id)) { echo "There is no domain with ID $id." . PHP_EOL; exit(1); } $this->domainController->deleteZone(id: $id); echo "The domain with ID $id has been deleted." . PHP_EOL; } /** * @param string $subcommand * * @return void */ public function handleNameservers(string $subcommand): void { try { match ($subcommand) { 'create' => $this->handleNameserversCreate(), 'list' => $this->handleNameserversList(), 'update' => $this->handleNameserversUpdate(), 'delete' => $this->handleNameserversDelete(), 'apiping' => $this->handleAPIPing(type: 'nameserver') }; } catch (UnhandledMatchError) { echo 'Unknown action: ' . $subcommand . PHP_EOL; } catch (DependencyException|NotFoundException $e) { echo $e->getMessage(); } } /** * @return void */ function handleNameserversCreate(): void { $name = $this->arguments[1] ?? ''; if (empty($name)) { echo 'You need to supply the nameserver name.' . PHP_EOL; exit(1); } $filteredName = filter_var(value: $name, filter: FILTER_VALIDATE_DOMAIN); if (!empty($filteredName) && str_contains(haystack: $filteredName, needle: '.')) { $name = $filteredName; } else { echo "$name is no valid nameserver name." . PHP_EOL; exit(1); } $arguments = $this->parseArguments(); $a = $arguments['a'] ?? ''; $aaaa = $arguments['aaaa'] ?? ''; if (empty($a) && empty($aaaa)) { echo 'At least one IP address is required.' . PHP_EOL; exit(0); } $apikey = $argiments['apikey'] ?? ''; try { if ($this->nameserverRepository->findByName(name: $name)) { echo "Nameserver: $name already exists." . PHP_EOL; exit(1); } else { $result = $this->nameserverRepository->insert(name: $name, a: $a, aaaa: $aaaa, apikey: $apikey); echo "Nameserver $name has been created with id $result" . PHP_EOL; exit(0); } } catch (DependencyException|NotFoundException $e) { echo $e->getMessage(); exit(1); } } /** * @return void */ function handleNameserversList(): void { echo 'All available nameservers:' . PHP_EOL; try { $nameservers = $this->nameserverRepository->findAll(); } catch (DependencyException|NotFoundException $e) { echo $e->getMessage(); } if (!empty($nameservers)) { $table = new ConsoleTable(); $table->setHeaders(content: ['ID', 'Name', 'A', 'AAAA', 'API Key']); foreach ($nameservers as $nameserver) { $row = []; try { $token = strtok(string: $nameserver->getApiKey(), token: '.'); $row[] = $nameserver->getId(); $row[] = $nameserver->getName(); $row[] = $nameserver->getA(); $row[] = $nameserver->getAaaa(); $row[] = $token; } catch (DependencyException|NotFoundException $e) { echo $e->getMessage(); exit(1); } $table->addRow(data: $row); } $table->setPadding(value: 2); $table->display(); } else { echo 'No nameservers found.' . PHP_EOL; exit(1); } exit(0); } /** * @throws \DI\DependencyException * @throws \DI\NotFoundException */ function handleNameserversUpdate() { $arguments = $this->parseArguments(); $id = $this->arguments[1] ?? 0; $name = $arguments['name'] ?? ''; $a = $arguments['a'] ?? ''; $aaaa = $arguments['aaaa'] ?? ''; $apikey = $arguments['apikey'] ?? ''; if ($id == 0) { echo 'An ID is required' . PHP_EOL; exit(1); } if (!$this->nameserverRepository->findByID(id: intval(value: $id))) { echo "Nameserver with ID : $id doesn't exist." . PHP_EOL; exit(1); } if ($this->nameserverRepository->update(id: intval(value: $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; } } /** * @throws \DI\DependencyException * @throws \DI\NotFoundException */ function handleNameserversDelete() { if (empty($this->arguments[1])) { echo "You need to supply an ID." . PHP_EOL; exit(1); } $id = intval(value: $this->arguments[1]) ?? 0; if ($id == 0) { echo "Nameserver with ID $id not found." . PHP_EOL; exit(1); } if (!$this->nameserverRepository->findByID(id: $id)) { echo "There is no nameserver with ID $id." . PHP_EOL; exit(1); } $this->nameserverRepository->delete(id: $id); echo "The nameserver with ID $id has been deleted." . PHP_EOL; } }