Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Automatize TLDs update #30

Closed
wants to merge 7 commits into from
Closed
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
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ matrix:
- php: 5.5
env:
- EXECUTE_CS_CHECK=true
- EXECUTE_HOSTNAME_CHECK=true
- php: 5.6
env:
- EXECUTE_TEST_COVERALLS=true
Expand All @@ -46,6 +47,7 @@ script:
- if [[ $EXECUTE_TEST_COVERALLS == 'true' ]]; then ./vendor/bin/phpunit --coverage-clover clover.xml ; fi
- if [[ $EXECUTE_TEST_COVERALLS != 'true' ]]; then ./vendor/bin/phpunit ; fi
- if [[ $EXECUTE_CS_CHECK == 'true' ]]; then ./vendor/bin/php-cs-fixer fix -v --diff --dry-run ; fi
- if [[ $EXECUTE_HOSTNAME_CHECK == "true" && $TRAVIS_PULL_REQUEST == "false" ]]; then bin/update_hostname_validator.php --check-only; fi

after_script:
- if [[ $EXECUTE_TEST_COVERALLS == 'true' ]]; then ./vendor/bin/coveralls ; fi
116 changes: 89 additions & 27 deletions bin/update_hostname_validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
define('IANA_URL', 'https://data.iana.org/TLD/tlds-alpha-by-domain.txt');
define('ZF2_HOSTNAME_VALIDATOR_FILE', __DIR__.'/../src/Hostname.php');


if (! file_exists(ZF2_HOSTNAME_VALIDATOR_FILE) || ! is_readable(ZF2_HOSTNAME_VALIDATOR_FILE)) {
printf("Error: cannont read file '%s'%s", ZF2_HOSTNAME_VALIDATOR_FILE, PHP_EOL);
exit(1);
Expand All @@ -25,35 +26,33 @@
exit(1);
}

// get current list of official TLDs
$client = new Client();
$client->setOptions([
'adapter' => 'Zend\Http\Client\Adapter\Curl',
]);
$client->setUri(IANA_URL);
$client->setMethod('GET');
$response = $client->send();
if (! $response->isSuccess()) {
printf("Error: cannot get '%s'%s", IANA_URL, PHP_EOL);
$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->getBody(), "\n"));
$validatorVersion = getVersionFromString('IanaVersion', file_get_contents(ZF2_HOSTNAME_VALIDATOR_FILE));

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

$decodePunycode = getPunycodeDecoder();

// Get new TLDs from the list previously fetched
$newValidTlds = [];
foreach (preg_grep('/^[^#]/', preg_split("#\r?\n#", $response->getBody())) as $line) {
$newValidTlds []= sprintf(
"%s'%s',\n",
str_repeat(' ', 8),
$decodePunycode(strtolower($line))
);
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
foreach (file(ZF2_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;
}

if ($insertDone === $insertFinish) {
// Outside of $validTlds definition; keep line as-is
$newFileContent []= $line;
Expand All @@ -65,7 +64,7 @@

if ($insertDone) {
// Detect where the $validTlds declaration ends
if (preg_match('/^\s+\);\s*$/', $line)) {
if (preg_match('/^\s+\];\s*$/', $line)) {
$newFileContent []= $line;
$insertFinish = true;
}
Expand All @@ -74,8 +73,8 @@
}

// Detect where the $validTlds declaration begins
if (preg_match('/^\s+protected\s+\$validTlds\s+=\s+array\(\s*$/', $line)) {
$newFileContent = array_merge($newFileContent, $newValidTlds);
if (preg_match('/^\s+protected\s+\$validTlds\s+=\s+\[\s*$/', $line)) {
$newFileContent = array_merge($newFileContent, getNewValidTlds($response->getBody()));
$insertDone = true;
}
}
Expand All @@ -95,9 +94,72 @@
exit(1);
}

printf("Validator TLD file updated.%s", PHP_EOL);
printf('Validator TLD file updated.%s', PHP_EOL);
exit(0);

/**
* Get Official TLDs
*
* @return \Zend\Http\Response
* @throws Exception
*/
function getOfficialTLDs()
{
$client = new Client();
$client->setOptions([
'adapter' => 'Zend\Http\Client\Adapter\Curl',
]);
$client->setUri(IANA_URL);
$client->setMethod('GET');
$response = $client->send();
if (! $response->isSuccess()) {
throw new \Exception(sprintf("Error: cannot get '%s'%s", IANA_URL, PHP_EOL));
}
return $response;
}

/**
* 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.
*
* @param string $string
* @return array
*/
function getNewValidTlds($string)
{
$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))
);
}

return $newValidTlds;
}

/**
* Retrieve and return a punycode decoder.
*
Expand Down
Loading