Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TLD updater script #329

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 8 additions & 39 deletions bin/update_hostname_validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

declare(strict_types=1);

use Laminas\Validator\Hostname;

require __DIR__ . '/../vendor/autoload.php';

const IANA_URL = 'https://data.iana.org/TLD/tlds-alpha-by-domain.txt';
Expand All @@ -19,6 +17,11 @@
exit(1);
}

if (! extension_loaded('intl')) {
printf("Error: ext-intl is required by this script%s", PHP_EOL);
exit(1);
}

/** @psalm-var list<string> $newFileContent */
$newFileContent = []; // new file content
$insertDone = false; // becomes 'true' when we find start of $validTlds declaration
Expand Down Expand Up @@ -49,14 +52,14 @@
}

// Detect where the $validTlds declaration begins
if (preg_match('/^\s+protected\s+\$validTlds\s+=\s+\[\s*$/', $line)) {
if (preg_match('/^\s+private\s+array\s+\$validTlds\s+=\s+\[\s*$/', $line)) {
$newFileContent = array_merge($newFileContent, getNewValidTlds($response));
$insertDone = true;
}
}

if (! $insertDone) {
printf('Error: cannot find line with "protected $validTlds"%s', PHP_EOL);
printf('Error: cannot find line with "private array $validTlds"%s', PHP_EOL);
exit(1);
}

Expand Down Expand Up @@ -110,49 +113,15 @@ function getOfficialTLDs(): string
*/
function getNewValidTlds(string $string): array
{
$decodePunycode = getPunycodeDecoder();

// Get new TLDs from the list previously fetched
$newValidTlds = [];
foreach (preg_grep('/^[^#]/', preg_split("#\r?\n#", $string)) as $line) {
$newValidTlds [] = sprintf(
"%s'%s',\n",
str_repeat(' ', 8),
$decodePunycode(strtolower($line))
idn_to_utf8(strtolower($line), 0, INTL_IDNA_VARIANT_UTS46),
);
}

return $newValidTlds;
}

/**
* Retrieve and return a punycode decoder.
*
* TLDs are puny encoded.
*
* We need a decodePunycode function to translate TLDs to UTF-8:
*
* - use idn_to_utf8 if available
* - otherwise, use Hostname::decodePunycode()
*
* @return callable
*/
function getPunycodeDecoder()
{
if (function_exists('idn_to_utf8')) {
return function ($domain) {
return idn_to_utf8($domain, 0, INTL_IDNA_VARIANT_UTS46);
};
}

$hostnameValidator = new Hostname();
$reflection = new ReflectionClass($hostnameValidator::class);
$decodePunyCode = $reflection->getMethod('decodePunycode');

return function ($encode) use ($hostnameValidator, $decodePunyCode) {
if (strpos($encode, 'xn--') === 0) {
return $decodePunyCode->invokeArgs($hostnameValidator, [substr($encode, 4)]);
}
return $encode;
};
}
15 changes: 0 additions & 15 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.25.0@01a8eb06b9e9cc6cfb6a320bf9fb14331919d505">
<file src="bin/update_hostname_validator.php">
<MissingClosureParamType>
<code><![CDATA[$domain]]></code>
<code><![CDATA[$encode]]></code>
</MissingClosureParamType>
<MissingClosureReturnType>
<code><![CDATA[function ($encode) use ($hostnameValidator, $decodePunyCode) {]]></code>
</MissingClosureReturnType>
<MixedArgument>
<code><![CDATA[$decodePunycode(strtolower($line))]]></code>
<code><![CDATA[$domain]]></code>
<code><![CDATA[$encode]]></code>
<code><![CDATA[$encode]]></code>
</MixedArgument>
</file>
<file src="src/AbstractValidator.php">
<MixedArgumentTypeCoercion>
<code><![CDATA[$value]]></code>
Expand Down