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)) {
foreach ($nameservers as $index => $nameserver) {
$parts_host = extractHostTLD($nameserver);
$host_extension = $parts_host['tld'] ?? ''; // Extract only the TLD
// Initialize $internal_host as false
$internal_host = false;
$parts_host = extractDomainAndTLD($nameserver);
$host_extension = $parts_host['tld'];
$result = $db->select('SELECT tld FROM domain_tld');
// 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)]
);
foreach ($result as $row) {
if ('.' . strtoupper($host_extension) === strtoupper($row['tld'])) {
$internal_host = true;
break;
}
// 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,6 +761,7 @@ class DomainsController extends Controller
$host_date = $currentDateTime->format('Y-m-d H:i:s.v');
if ($internal_host) {
if (strpos(strtolower($nameserver), strtolower($domainName)) !== false) {
$db->insert(
'host',
[
@ -766,6 +772,18 @@ class DomainsController extends Controller
'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(

View file

@ -587,3 +587,19 @@ 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];
}