This repository has been archived by the owner on Jan 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathupdate_hostname_validator.php
196 lines (167 loc) · 5.44 KB
/
update_hostname_validator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
use Zend\Http\Client;
use Zend\Validator\Hostname;
require __DIR__ . '/../vendor/autoload.php';
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);
}
if (! is_writable(ZF2_HOSTNAME_VALIDATOR_FILE)) {
printf("Error: Cannot update file '%s'%s", ZF2_HOSTNAME_VALIDATOR_FILE, PHP_EOL);
exit(1);
}
$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 ($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);
}
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;
}
if ($insertFinish) {
continue;
}
if ($insertDone) {
// Detect where the $validTlds declaration ends
if (preg_match('/^\s+\];\s*$/', $line)) {
$newFileContent[] = $line;
$insertFinish = true;
}
continue;
}
// Detect where the $validTlds declaration begins
if (preg_match('/^\s+protected\s+\$validTlds\s+=\s+\[\s*$/', $line)) {
$newFileContent = array_merge($newFileContent, getNewValidTlds($response->getBody()));
$insertDone = true;
}
}
if (! $insertDone) {
printf('Error: cannot find line with "protected $validTlds"%s', PHP_EOL);
exit(1);
}
if (!$insertFinish) {
printf('Error: cannot find end of $validTlds declaration%s', PHP_EOL);
exit(1);
}
if (false === @file_put_contents(ZF2_HOSTNAME_VALIDATOR_FILE, $newFileContent)) {
printf('Error: cannot write info file "%s"%s', ZF2_HOSTNAME_VALIDATOR_FILE, PHP_EOL);
exit(1);
}
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.
*
* 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(get_class($hostnameValidator));
$decodePunyCode = $reflection->getMethod('decodePunycode');
$decodePunyCode->setAccessible(true);
return function ($encode) use ($hostnameValidator, $decodePunyCode) {
if (strpos($encode, 'xn--') === 0) {
return $decodePunyCode->invokeArgs($hostnameValidator, [substr($encode, 4)]);
}
return $encode;
};
}