Skip to content

Commit da1941b

Browse files
committed
Graby: Use consistent arguments for idn_to_ascii across PHP versions
As per <https://www.php.net/manual/en/function.idn-to-ascii.php>, PHP 7.2 deprecated the `INTL_IDNA_VARIANT_2003` constant. But until PHP 7.4, it was still used as the default value of the `$variant` argument and will trigger a deprecation warning (https://github.com/php/php-src/blob/php-7.3.0/ext/intl/idn/idn.c#L318-L320). This in turn broke tests on PHP 7.2 and 7.3: 1x: idn_to_ascii(): INTL_IDNA_VARIANT_2003 is deprecated 1x in GrabyTest::testUrlWithAccent from Tests\Graby This is not an issue on `master` since we require PHP ≥ 7.4 there. One option would be passing the `INTL_IDNA_VARIANT_UTS46` explicitly to the `$variant` argument so that it is consistent across all supported PHP versions. Unfortunately, this would require raising the requirements to ICU ≥ 4.6, which would break systems that have `intl` extension linked against ancient ICU (allowed on PHP before 7.4: https://github.com/php/php-src/blob/php-7.2.0/ext/intl/idn/idn.c#L85-L87). Polyfill does support emulating `INTL_IDNA_VARIANT_UTS46` but it will not do anything if the presence of any `intl` extension is detected (https://github.com/symfony/polyfill-intl-idn/blob/v1.26.0/bootstrap.php#L14-L16). So it will not help us when the constant we want does not actually exist. We could silence the warning with `@` but that is always iffy. For now, let’s use the polyfill explicitly on PHP 7.2 or 7.3 built with `intl` extension linked against ICU < 4.6. The PHP implementation will be a bit slower but the situation is pretty unlikely in my opinion.
1 parent 1d2de31 commit da1941b

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/Graby.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Readability\Readability;
2020
use Smalot\PdfParser\Parser as PdfParser;
2121
use Symfony\Component\OptionsResolver\OptionsResolver;
22+
use Symfony\Polyfill\Intl\Idn\Idn as SymfonyIdn;
2223

2324
/**
2425
* @todo add proxy
@@ -491,7 +492,15 @@ private function validateUrl($url)
491492
$uri = new Uri((string) $url);
492493

493494
if (preg_match('/[\x80-\xff]/', $uri->getHost())) {
494-
$uriIdnSafe = idn_to_ascii($uri->getHost());
495+
$uriIdnSafe = \defined('INTL_IDNA_VARIANT_UTS46') ? idn_to_ascii(
496+
$uri->getHost(),
497+
\IDNA_DEFAULT,
498+
\INTL_IDNA_VARIANT_UTS46
499+
) : SymfonyIdn::idn_to_ascii(
500+
$uri->getHost(),
501+
SymfonyIdn::IDNA_DEFAULT,
502+
SymfonyIdn::INTL_IDNA_VARIANT_UTS46
503+
);
495504

496505
if (false === $uriIdnSafe) {
497506
throw new \InvalidArgumentException(\sprintf('Url "%s" is not valid IDN to ascii.', $url));

0 commit comments

Comments
 (0)