Skip to content

Commit

Permalink
Some fixes for domain create and hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
getpinga committed Nov 23, 2024
1 parent 918afc8 commit abde99e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 20 deletions.
58 changes: 38 additions & 20 deletions cp/app/Controllers/DomainsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -707,19 +707,24 @@ public function createDomain(Request $request, Response $response)
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');

foreach ($result as $row) {
if ('.' . strtoupper($host_extension) === strtoupper($row['tld'])) {
$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(
'SELECT id FROM host WHERE name = ? LIMIT 1',
[$nameserver]
Expand Down Expand Up @@ -756,16 +761,29 @@ public function createDomain(Request $request, Response $response)
$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(
Expand Down
16 changes: 16 additions & 0 deletions cp/bootstrap/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

0 comments on commit abde99e

Please sign in to comment.