mirror of
https://github.com/getnamingo/registry.git
synced 2025-08-05 01:01:30 +02:00
Domain parsing fixes
This commit is contained in:
parent
d172717f0a
commit
9fe3b3f20a
10 changed files with 175 additions and 23 deletions
|
@ -5,6 +5,12 @@
|
|||
*/
|
||||
|
||||
use Pinga\Auth\Auth;
|
||||
use Pdp\Domain;
|
||||
use Pdp\TopLevelDomains;
|
||||
use League\Flysystem\Local\LocalFilesystemAdapter;
|
||||
use League\Flysystem\Filesystem;
|
||||
use MatthiasMullie\Scrapbook\Adapters\Flysystem as ScrapbookFlysystem;
|
||||
use MatthiasMullie\Scrapbook\Psr6\Pool;
|
||||
|
||||
/**
|
||||
* @return mixed|string|string[]
|
||||
|
@ -206,8 +212,8 @@ function validate_label($label, $db) {
|
|||
}
|
||||
|
||||
// Extract TLD from the domain and prepend a dot
|
||||
$parts = explode('.', $label);
|
||||
$tld = "." . end($parts);
|
||||
$parts = extractDomainAndTLD($label);
|
||||
$tld = "." . $parts['tld'];
|
||||
|
||||
// Check if the TLD exists in the domain_tld table
|
||||
$tldExists = $db->select('SELECT COUNT(*) FROM domain_tld WHERE tld = ?', [$tld]);
|
||||
|
@ -264,4 +270,41 @@ function normalize_v6_address($v6) {
|
|||
$v6 = preg_replace('/:(:0)+/', ':', $v6);
|
||||
|
||||
return $v6;
|
||||
}
|
||||
|
||||
function extractDomainAndTLD($urlString) {
|
||||
$cachePath = __DIR__ . '/../cache'; // Cache directory
|
||||
$adapter = new LocalFilesystemAdapter($cachePath, null, LOCK_EX);
|
||||
$filesystem = new Filesystem($adapter);
|
||||
$cache = new Pool(new ScrapbookFlysystem($filesystem));
|
||||
$cacheKey = 'tlds_alpha_by_domain';
|
||||
$cachedFile = $cache->getItem($cacheKey);
|
||||
$fileContent = $cachedFile->get();
|
||||
|
||||
// Define a list of fake TLDs used in your QA environment
|
||||
$fakeTlds = ['test', 'xx'];
|
||||
|
||||
// Parse the URL to get the host
|
||||
$parts = parse_url($urlString);
|
||||
$host = $parts['host'] ?? $urlString;
|
||||
|
||||
// Check if the TLD is a known fake TLD
|
||||
foreach ($fakeTlds as $fakeTld) {
|
||||
if (str_ends_with($host, ".$fakeTld")) {
|
||||
// Handle the fake TLD case
|
||||
$hostParts = explode('.', $host);
|
||||
$tld = array_pop($hostParts);
|
||||
$sld = array_pop($hostParts);
|
||||
return ['domain' => $sld, 'tld' => $tld];
|
||||
}
|
||||
}
|
||||
|
||||
// Use the PHP Domain Parser library for real TLDs
|
||||
$tlds = TopLevelDomains::fromString($fileContent);
|
||||
$domain = Domain::fromIDNA2008($host);
|
||||
$result = $tlds->resolve($domain);
|
||||
$sld = $result->secondLevelDomain()->toString();
|
||||
$tld = $result->suffix()->toString();
|
||||
|
||||
return ['domain' => $sld, 'tld' => $tld];
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue