Skip to content

Commit

Permalink
Merge pull request #191 from laminas/iana-tld-list-update
Browse files Browse the repository at this point in the history
Update IANA TLDs for Hostname validator and drop IanaVersion comment from Hostname validator
  • Loading branch information
Ocramius committed Jun 12, 2023
2 parents d03afd7 + ce7afa7 commit b9db2ea
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 51 deletions.
65 changes: 20 additions & 45 deletions bin/update_hostname_validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,15 @@
}

/** @psalm-var list<string> $newFileContent */
$newFileContent = []; // new file content
$insertDone = false; // becomes 'true' when we find start of $validTlds declaration
$insertFinish = false; // becomes 'true' when we find end of $validTlds declaration
$checkOnly = isset($argv[1]) ? $argv[1] === '--check-only' : false;
$response = getOfficialTLDs();
$ianaVersion = getVersionFromString('Version', strtok($response, "\n"));
$validatorVersion = getVersionFromString('IanaVersion', file_get_contents(LAMINAS_HOSTNAME_VALIDATOR_FILE));

if ($checkOnly && $ianaVersion > $validatorVersion) {
printf(
'TLDs must be updated, please run `php bin/update_hostname_validator.php` and push your changes%s',
PHP_EOL
);
exit(1);
}

if ($checkOnly) {
printf('TLDs are up-to-date%s', PHP_EOL);
exit(0);
}
$newFileContent = []; // new file content
$insertDone = false; // becomes 'true' when we find start of $validTlds declaration
$insertFinish = false; // becomes 'true' when we find end of $validTlds declaration
$checkOnly = isset($argv[1]) ? $argv[1] === '--check-only' : false;
$response = getOfficialTLDs();

foreach (file(LAMINAS_HOSTNAME_VALIDATOR_FILE) as $line) {
// Replace old version number with new one
if (preg_match('/\*\s+IanaVersion\s+\d+/', $line, $matches)) {
$newFileContent[] = sprintf(" * IanaVersion %s\n", $ianaVersion);
continue;
}
$currentFileContent = file(LAMINAS_HOSTNAME_VALIDATOR_FILE);

foreach ($currentFileContent as $line) {
if ($insertDone === $insertFinish) {
// Outside of $validTlds definition; keep line as-is
$newFileContent[] = $line;
Expand Down Expand Up @@ -84,6 +65,19 @@
exit(1);
}

if ($currentFileContent === $newFileContent) {
printf('Nothing to do. Validator has no TLD changes.%s', PHP_EOL);
exit(0);
}

if ($checkOnly) {
printf(
'TLDs must be updated, please run `php bin/update_hostname_validator.php` and push your changes%s',
PHP_EOL
);
exit(1);
}

if (false === @file_put_contents(LAMINAS_HOSTNAME_VALIDATOR_FILE, $newFileContent)) {
printf('Error: cannot write info file "%s"%s', LAMINAS_HOSTNAME_VALIDATOR_FILE, PHP_EOL);
exit(1);
Expand All @@ -109,25 +103,6 @@ function getOfficialTLDs(): string
}
}

/**
* Extract the first match of a string like
* "Version 2015072300" from the given string
*
* @param string $prefix
* @param string $string
* @return string
* @throws Exception
*/
function getVersionFromString($prefix, $string)
{
$matches = [];
if (! preg_match(sprintf('/%s\s+((\d+)?)/', $prefix), $string, $matches)) {
throw new Exception('Error: cannot get last update date');
}

return $matches[1];
}

/**
* Extract new Valid TLDs from a string containing one per line.
*
Expand Down
6 changes: 0 additions & 6 deletions src/Hostname.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ class Hostname extends AbstractValidator

/**
* Array of valid top-level-domains
* IanaVersion 2023060100
*
* @see ftp://data.iana.org/TLD/tlds-alpha-by-domain.txt List of all TLDs by domain
* @see http://www.iana.org/domains/root/db/ Official list of supported TLDs
Expand All @@ -99,7 +98,6 @@ class Hostname extends AbstractValidator
protected $validTlds = [
'aaa',
'aarp',
'abarth',
'abb',
'abbott',
'abbvie',
Expand Down Expand Up @@ -134,7 +132,6 @@ class Hostname extends AbstractValidator
'airtel',
'akdn',
'al',
'alfaromeo',
'alibaba',
'alipay',
'allfinanz',
Expand Down Expand Up @@ -508,7 +505,6 @@ class Hostname extends AbstractValidator
'ferrari',
'ferrero',
'fi',
'fiat',
'fidelity',
'fido',
'film',
Expand Down Expand Up @@ -775,7 +771,6 @@ class Hostname extends AbstractValidator
'lamborghini',
'lamer',
'lancaster',
'lancia',
'land',
'landrover',
'lanxess',
Expand Down Expand Up @@ -849,7 +844,6 @@ class Hostname extends AbstractValidator
'markets',
'marriott',
'marshalls',
'maserati',
'mattel',
'mba',
'mc',
Expand Down

0 comments on commit b9db2ea

Please sign in to comment.