Some fixes for domain create and hosts

This commit is contained in:
Pinga 2024-11-23 15:28:41 +02:00
parent 918afc8473
commit abde99e73e
2 changed files with 54 additions and 20 deletions

View file

@ -707,19 +707,24 @@ class DomainsController extends Controller
if (!empty($nameservers)) { if (!empty($nameservers)) {
foreach ($nameservers as $index => $nameserver) { foreach ($nameservers as $index => $nameserver) {
$internal_host = false; $parts_host = extractHostTLD($nameserver);
$host_extension = $parts_host['tld'] ?? ''; // Extract only the TLD
$parts_host = extractDomainAndTLD($nameserver);
$host_extension = $parts_host['tld'];
$result = $db->select('SELECT tld FROM domain_tld');
foreach ($result as $row) { // Initialize $internal_host as false
if ('.' . strtoupper($host_extension) === strtoupper($row['tld'])) { $internal_host = false;
$internal_host = true;
break; // 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( $hostName_already_exist = $db->selectValue(
'SELECT id FROM host WHERE name = ? LIMIT 1', 'SELECT id FROM host WHERE name = ? LIMIT 1',
[$nameserver] [$nameserver]
@ -756,16 +761,29 @@ class DomainsController extends Controller
$host_date = $currentDateTime->format('Y-m-d H:i:s.v'); $host_date = $currentDateTime->format('Y-m-d H:i:s.v');
if ($internal_host) { if ($internal_host) {
$db->insert( if (strpos(strtolower($nameserver), strtolower($domainName)) !== false) {
'host', $db->insert(
[ 'host',
'name' => strtolower($nameserver), [
'domain_id' => $domain_id, 'name' => strtolower($nameserver),
'clid' => $clid, 'domain_id' => $domain_id,
'crid' => $clid, 'clid' => $clid,
'crdate' => $host_date '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(); $host_id = $db->getlastInsertId();
} else { } else {
$db->insert( $db->insert(

View file

@ -586,4 +586,20 @@ function toPunycode($value) {
function toUnicode($value) { function toUnicode($value) {
// Convert from Punycode to UTF-8 if it's a valid IDN format // 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; 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];
} }