diff --git a/cp/app/Controllers/DomainsController.php b/cp/app/Controllers/DomainsController.php index a1a25e1..baa46bc 100644 --- a/cp/app/Controllers/DomainsController.php +++ b/cp/app/Controllers/DomainsController.php @@ -707,19 +707,24 @@ class DomainsController extends Controller if (!empty($nameservers)) { foreach ($nameservers as $index => $nameserver) { - $internal_host = false; - - $parts_host = extractDomainAndTLD($nameserver); - $host_extension = $parts_host['tld']; - $result = $db->select('SELECT tld FROM domain_tld'); + $parts_host = extractHostTLD($nameserver); + $host_extension = $parts_host['tld'] ?? ''; // Extract only the TLD - foreach ($result as $row) { - if ('.' . strtoupper($host_extension) === strtoupper($row['tld'])) { - $internal_host = true; - break; - } + // Initialize $internal_host as false + $internal_host = false; + + // Validate the extracted TLD before querying + if (!empty($host_extension)) { + $tldExists = $db->selectValue( + 'SELECT 1 FROM domain_tld WHERE tld = ? LIMIT 1', + ['.' . strtolower($host_extension)] + ); + + // Correctly set $internal_host to true only if $tldExists is not NULL + $internal_host = $tldExists !== null; } + // Check if the host name already exists $hostName_already_exist = $db->selectValue( 'SELECT id FROM host WHERE name = ? LIMIT 1', [$nameserver] @@ -756,16 +761,29 @@ class DomainsController extends Controller $host_date = $currentDateTime->format('Y-m-d H:i:s.v'); if ($internal_host) { - $db->insert( - 'host', - [ - 'name' => strtolower($nameserver), - 'domain_id' => $domain_id, - 'clid' => $clid, - 'crid' => $clid, - 'crdate' => $host_date - ] - ); + if (strpos(strtolower($nameserver), strtolower($domainName)) !== false) { + $db->insert( + 'host', + [ + 'name' => strtolower($nameserver), + 'domain_id' => $domain_id, + 'clid' => $clid, + 'crid' => $clid, + 'crdate' => $host_date + ] + ); + } else { + $db->insert( + 'host', + [ + 'name' => strtolower($nameserver), + 'domain_id' => null, + 'clid' => $clid, + 'crid' => $clid, + 'crdate' => $host_date + ] + ); + } $host_id = $db->getlastInsertId(); } else { $db->insert( diff --git a/cp/bootstrap/helper.php b/cp/bootstrap/helper.php index e109477..0478770 100644 --- a/cp/bootstrap/helper.php +++ b/cp/bootstrap/helper.php @@ -586,4 +586,20 @@ function toPunycode($value) { function toUnicode($value) { // Convert from Punycode to UTF-8 if it's a valid IDN format return (strpos($value, 'xn--') === 0) ? idn_to_utf8($value, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46) : $value; +} + +function extractHostTLD(string $hostname): array +{ + $parts = explode('.', $hostname); + + if (count($parts) < 2) { + // Invalid hostname; return empty values + return ['host' => '', 'tld' => '']; + } + + // Extract host and TLD + $tld = array_pop($parts); // Get the last part as TLD + $host = array_pop($parts); // Get the second last part as host + + return ['host' => $host, 'tld' => $tld]; } \ No newline at end of file