Skip to content

Commit 67d676c

Browse files
committed
remove IDNA usage in TLD resolution
1 parent c537ffe commit 67d676c

File tree

5 files changed

+30
-30
lines changed

5 files changed

+30
-30
lines changed

src/IDNAConverterTrait.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,15 @@ private static function getIdnErrors(int $error_bit): string
104104
*
105105
* @param string $domain
106106
*
107-
* @param int $IDNAOption
107+
* @param int $option
108108
*
109109
* @throws InvalidDomain if the string can not be converted to ASCII using IDN UTS46 algorithm
110110
*
111111
* @return string
112112
*/
113-
private function idnToAscii(string $domain, int $IDNAOption = IDNA_DEFAULT): string
113+
private function idnToAscii(string $domain, int $option = IDNA_DEFAULT): string
114114
{
115-
list($domain, ) = $this->transformToAscii($domain, $IDNAOption);
115+
list($domain, ) = $this->transformToAscii($domain, $option);
116116

117117
return $domain;
118118
}
@@ -122,7 +122,6 @@ private function idnToAscii(string $domain, int $IDNAOption = IDNA_DEFAULT): str
122122
*
123123
* @param string $domain
124124
*
125-
* @param int $IDNAOption
126125
* @param int $option
127126
*
128127
* @throws InvalidDomain if the string can not be converted to ASCII using IDN UTS46 algorithm
@@ -163,15 +162,16 @@ private function transformToAscii(string $domain, int $option): array
163162
*
164163
* @param string $domain
165164
*
166-
* @param int $IDNAOption
165+
* @param int $option
167166
*
168-
* @throws InvalidDomain if the string can not be converted to UNICODE using IDN UTS46 algorithm
167+
* @throws InvalidDomain if the string can not be converted to UNICODE using IDN UTS46 algorithm
168+
* @throws UnexpectedValueException if the intl extension is misconfigured
169169
*
170170
* @return string
171171
*/
172-
private function idnToUnicode(string $domain, int $IDNAOption = IDNA_DEFAULT): string
172+
private function idnToUnicode(string $domain, int $option = IDNA_DEFAULT): string
173173
{
174-
$output = idn_to_utf8($domain, $IDNAOption, INTL_IDNA_VARIANT_UTS46, $arr);
174+
$output = idn_to_utf8($domain, $option, INTL_IDNA_VARIANT_UTS46, $arr);
175175
if (0 !== $arr['errors']) {
176176
throw new InvalidDomain(sprintf('The host `%s` is invalid : %s', $domain, self::getIdnErrors($arr['errors'])));
177177
}

src/Manager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public function refreshTLDs(
206206
static $converter;
207207

208208
$converter = $converter ?? new TLDConverter();
209-
$data = json_encode($converter->convert($this->http->getContent($url), $asciiIDNAOption, $unicodeIDNAOption));
209+
$data = json_encode($converter->convert($this->http->getContent($url)));
210210

211211
return $this->cache->set($this->getCacheKey('RZD', $url), $data, $this->filterTtl($ttl) ?? $this->ttl);
212212
}

src/Rules.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ public function __construct(
8787
* @param int $asciiIDNAOption
8888
* @param int $unicodeIDNAOption
8989
*
90+
* @throws CouldNotLoadRules If the rules can not be loaded from the path
91+
*
9092
* @return self
9193
*/
9294
public static function createFromPath(

src/TLDConverter.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@
1818
use DateTimeImmutable;
1919
use Pdp\Exception\CouldNotLoadTLDs;
2020
use SplTempFileObject;
21-
use function compact;
2221
use function preg_match;
2322
use function sprintf;
2423
use function strpos;
2524
use function trim;
2625
use const DATE_ATOM;
27-
use const IDNA_DEFAULT;
2826

2927
/**
3028
* IANA Root Zone Database Parser.
@@ -44,24 +42,21 @@ final class TLDConverter
4442
* Converts the IANA Root Zone Database into a TopLevelDomains associative array.
4543
*
4644
* @param string $content
47-
* @param int $asciiIDNAOption
48-
* @param int $unicodeIDNAOption
45+
*
46+
* @throws CouldNotLoadTLDs if the content is invalid or can not be correctly parsed and converted
4947
*
5048
* @return array
5149
*/
52-
public function convert(
53-
string $content,
54-
int $asciiIDNAOption = IDNA_DEFAULT,
55-
int $unicodeIDNAOption = IDNA_DEFAULT
56-
): array {
50+
public function convert(string $content): array
51+
{
5752
$data = [];
5853
$file = new SplTempFileObject();
5954
$file->fwrite($content);
6055
$file->setFlags(SplTempFileObject::DROP_NEW_LINE | SplTempFileObject::READ_AHEAD | SplTempFileObject::SKIP_EMPTY);
6156
foreach ($file as $line) {
6257
$line = trim($line);
6358
if ([] === $data) {
64-
$data = array_merge($this->extractHeader($line), compact('asciiIDNAOption', 'unicodeIDNAOption'));
59+
$data = $this->extractHeader($line);
6560
continue;
6661
}
6762

@@ -75,7 +70,7 @@ public function convert(
7570
}
7671

7772
if (isset($data['version'], $data['modifiedDate'], $data['records'])) {
78-
return array_merge($data, compact('asciiIDNAOption', 'unicodeIDNAOption'));
73+
return $data;
7974
}
8075

8176
throw new CouldNotLoadTLDs(sprintf('Invalid content: TLD conversion failed'));
@@ -116,8 +111,8 @@ private function extractRootZone(string $content): string
116111
{
117112
try {
118113
$tld = (new PublicSuffix($content))->toAscii();
119-
} catch (Exception $e) {
120-
throw new CouldNotLoadTLDs(sprintf('Invalid Root zone: %s', $content), 0, $e);
114+
} catch (Exception $exception) {
115+
throw new CouldNotLoadTLDs(sprintf('Invalid Root zone: %s', $content), 0, $exception);
121116
}
122117

123118
if (1 !== $tld->count() || '' === $tld->getContent()) {

src/TopLevelDomains.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ public function __construct(
9090
* @param int $asciiIDNAOption
9191
* @param int $unicodeIDNAOption
9292
*
93+
* @throws CouldNotLoadTLDs If the rules can not be loaded from the path
94+
*
9395
* @return self
9496
*/
9597
public static function createFromPath(
@@ -131,14 +133,14 @@ public static function createFromString(
131133

132134
$converter = $converter ?? new TLDConverter();
133135

134-
$data = $converter->convert($content, $asciiIDNAOption, $unicodeIDNAOption);
136+
$data = $converter->convert($content);
135137

136138
return new self(
137139
$data['records'],
138140
$data['version'],
139141
DateTimeImmutable::createFromFormat(DATE_ATOM, $data['modifiedDate']),
140-
$data['asciiIDNAOption'],
141-
$data['unicodeIDNAOption']
142+
$asciiIDNAOption,
143+
$unicodeIDNAOption
142144
);
143145
}
144146

@@ -228,9 +230,12 @@ public function isEmpty(): bool
228230
public function getIterator()
229231
{
230232
foreach ($this->records as $tld) {
231-
$publicSuffix = new PublicSuffix($tld, PublicSuffix::ICANN_DOMAINS, $this->asciiIDNAOption, $this->unicodeIDNAOption);
232-
233-
yield $publicSuffix->toAscii();
233+
yield (new PublicSuffix(
234+
$tld,
235+
PublicSuffix::ICANN_DOMAINS,
236+
$this->asciiIDNAOption,
237+
$this->unicodeIDNAOption
238+
))->toAscii();
234239
}
235240
}
236241

@@ -245,8 +250,6 @@ public function toArray(): array
245250
'version' => $this->version,
246251
'records' => $this->records,
247252
'modifiedDate' => $this->modifiedDate->format(DATE_ATOM),
248-
'asciiIDNAOption'=>$this->asciiIDNAOption,
249-
'unicodeIDNAOption'=>$this->unicodeIDNAOption,
250253
];
251254
}
252255

0 commit comments

Comments
 (0)