diff --git a/src/Controller/RequestController.php b/src/Controller/RequestController.php index 23e628c..706389c 100644 --- a/src/Controller/RequestController.php +++ b/src/Controller/RequestController.php @@ -13,9 +13,9 @@ use DI\ContainerBuilder; use Monolog\Formatter\LineFormatter; use Monolog\Handler\StreamHandler; use Monolog\Logger; +use OpenApi\Attributes as OAT; use UnhandledMatchError; use function DI\autowire; -use OpenApi\Attributes as OAT; /** * @@ -60,7 +60,7 @@ class RequestController private array $result; private string $status; private string $message; - + /** * @param array $config @@ -162,7 +162,7 @@ class RequestController $this->status = json_encode(value: ['message' => 'API key is missing or invalid']); } } - + /** * @return void @@ -424,7 +424,7 @@ class RequestController $putData = fopen(filename: 'php://input', mode: 'r'); $data = fread(stream: $putData, length: 8192); $params = explode(separator: '&', string: $data); - + foreach ($params as $param) { [$key, $value] = explode(separator: '=', string: $param); $put[$key] = $value; @@ -499,15 +499,15 @@ class RequestController * * @return string */ - private function getDomain(String $host): string + private function getDomain(string $host): string { $host = strtolower(string: trim(string: $host)); $count = substr_count(haystack: $host, needle: '.'); - if ($count == 2){ + if ($count == 2) { if (strlen(string: explode(separator: '.', string: $host)[1]) > 3) { $host = explode(separator: '.', string: $host, limit: 2)[1]; } - } else if ($count > 2) { + } elseif ($count > 2) { $host = $this->getDomain(host: explode(separator: '.', string: $host, limit: 2)[1]); } return $host; @@ -544,19 +544,19 @@ class RequestController if (!empty($panel->getAaaa())) { $domainData = $this->apiController->sendCommand( requestType: 'GET', - serverName: $panel->getName(), - versionIP: 6, - apiKey: $panel->getApikey(), - command: 'domains/name/' . $domainName, - serverType: 'panel'); + serverName : $panel->getName(), + versionIP : 6, + apiKey : $panel->getApikey(), + command : 'domains/name/' . $domainName, + serverType : 'panel'); } else { $domainData = $this->apiController->sendCommand( requestType: 'GET', - serverName: $panel->getName(), - versionIP: 4, - apiKey: $panel->getApikey(), - command: 'domains/name/' . $domainName, - serverType: 'panel'); + serverName : $panel->getName(), + versionIP : 4, + apiKey : $panel->getApikey(), + command : 'domains/name/' . $domainName, + serverType : 'panel'); } $domainDecodedData = json_decode(json: $domainData['data']); @@ -565,27 +565,30 @@ class RequestController if (!empty($panel->getAaaa())) { $dnsData = $this->apiController->sendCommand( requestType: 'GET', - serverName: $panel->getName(), - versionIP: 6, - apiKey: $panel->getApikey(), - command: 'dns/' . $domainID, - serverType: 'panel'); + serverName : $panel->getName(), + versionIP : 6, + apiKey : $panel->getApikey(), + command : 'dns/' . $domainID, + serverType : 'panel'); } else { $dnsData = $this->apiController->sendCommand( requestType: 'GET', - serverName: $panel->getName(), - versionIP: 4, - apiKey: $panel->getApikey(), - command: 'dns/' . $domainID, - serverType: 'panel'); + serverName : $panel->getName(), + versionIP : 4, + apiKey : $panel->getApikey(), + command : 'dns/' . $domainID, + serverType : 'panel'); } $dnsDataDecoded = json_decode(json: $dnsData['data']); $soa = $dnsDataDecoded->records->soa; $others = $dnsDataDecoded->records->other; - $updateHost = function(object $host) use ($hostName, $a, $aaaa) { + $hostFound = false; + + $updateHost = function (object $host) use ($hostName, $a, $aaaa, &$hostFound) { if ($host->host == $hostName) { + $hostFound = true; if ($host->type == 'A') { if (!empty($a)) { $host->value = $a; @@ -600,42 +603,45 @@ class RequestController array_map(callback: $updateHost, array: $others); - $newDnsData = json_encode(value: [ - 'records' => [ - 'soa' => $soa, - 'other' => $others - ] - ]); - - if (!empty($panel->getAaaa())) { - $result = $this->apiController->sendCommand( - requestType: 'PUT', - serverName: $panel->getName(), - versionIP: 6, - apiKey: $panel->getApikey(), - command: 'dns/' . $domainID, - serverType: 'panel', - body: json_decode(json: $newDnsData, associative: true) - ); + if ($hostFound) { + $newDnsData = json_encode(value: [ + 'records' => [ + 'soa' => $soa, + 'other' => $others + ] + ]); + + if (!empty($panel->getAaaa())) { + $result = $this->apiController->sendCommand( + requestType: 'PUT', + serverName : $panel->getName(), + versionIP : 6, + apiKey : $panel->getApikey(), + command : 'dns/' . $domainID, + serverType : 'panel', + body : json_decode(json: $newDnsData, associative: true) + ); + } else { + $result = $this->apiController->sendCommand( + requestType: 'PUT', + serverName : $panel->getName(), + versionIP : 4, + apiKey : $panel->getApikey(), + command : 'dns/' . $domainID, + serverType : 'panel', + body : json_decode(json: $newDnsData, associative: true) + ); + } + if ($result['header'] == 200) { + $this->header = '200 OK'; + $this->status = json_encode(value: ['message' => 'DynDNS host successfully updated']); + } } else { - $result = $this->apiController->sendCommand( - requestType: 'PUT', - serverName: $panel->getName(), - versionIP: 4, - apiKey: $panel->getApikey(), - command: 'dns/' . $domainID, - serverType: 'panel', - body: json_decode(json: $newDnsData, associative: true) - ); - } - if ($result['header'] == 200) { - $this->header = '200 OK'; - $this->status = json_encode(value: ['message' => 'Domain successfully updated']); + $this->header = '404 Not Found'; + $this->status = 'Host ' . COLOR_YELLOW . $hostName . ' not found'; } } } - } - }