From aebe1c02a2eb2f5135f7a9806059dc5d7f8e64d6 Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Wed, 2 Mar 2016 01:35:07 -0800 Subject: [PATCH] Use idn_to_ascii when available. Signed-off-by: Edward Z. Yang --- library/HTMLPurifier/AttrDef/URI/Host.php | 7 +++++-- tests/HTMLPurifier/AttrDef/URI/HostTest.php | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/library/HTMLPurifier/AttrDef/URI/Host.php b/library/HTMLPurifier/AttrDef/URI/Host.php index e7df800b..371f228a 100644 --- a/library/HTMLPurifier/AttrDef/URI/Host.php +++ b/library/HTMLPurifier/AttrDef/URI/Host.php @@ -89,11 +89,14 @@ class HTMLPurifier_AttrDef_URI_Host extends HTMLPurifier_AttrDef return $string; } + // PHP 5.3 and later support this functionality natively + if (function_exists('idn_to_ascii')) { + return idn_to_ascii($string); + // If we have Net_IDNA2 support, we can support IRIs by // punycoding them. (This is the most portable thing to do, // since otherwise we have to assume browsers support - - if ($config->get('Core.EnableIDNA')) { + } elseif ($config->get('Core.EnableIDNA')) { $idna = new Net_IDNA2(array('encoding' => 'utf8', 'overlong' => false, 'strict' => true)); // we need to encode each period separately $parts = explode('.', $string); diff --git a/tests/HTMLPurifier/AttrDef/URI/HostTest.php b/tests/HTMLPurifier/AttrDef/URI/HostTest.php index 00e56ed4..b645aa4c 100644 --- a/tests/HTMLPurifier/AttrDef/URI/HostTest.php +++ b/tests/HTMLPurifier/AttrDef/URI/HostTest.php @@ -42,7 +42,7 @@ class HTMLPurifier_AttrDef_URI_HostTest extends HTMLPurifier_AttrDefHarness public function testIDNA() { - if (!$GLOBALS['HTMLPurifierTest']['Net_IDNA2']) { + if (!$GLOBALS['HTMLPurifierTest']['Net_IDNA2'] && !function_exists("idn_to_ascii")) { return false; } $this->config->set('Core.EnableIDNA', true); -- 2.11.4.GIT