diff --git a/CHANGELOG b/CHANGELOG index a73beb7de8..66c073fc32 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,6 @@ * 2.12.5 (2020-XX-XX) - * n/a + * Add a check to ensure that iconv() is defined * 2.12.4 (2020-02-11) diff --git a/src/Extension/CoreExtension.php b/src/Extension/CoreExtension.php index 360efb8499..8bc9c0d632 100644 --- a/src/Extension/CoreExtension.php +++ b/src/Extension/CoreExtension.php @@ -411,7 +411,7 @@ function twig_random(Environment $env, $values = null, $max = null) $charset = $env->getCharset(); if ('UTF-8' !== $charset) { - $values = iconv($charset, 'UTF-8', $values); + $values = twig_convert_encoding($values, 'UTF-8', $charset); } // unicode version of str_split() @@ -420,7 +420,7 @@ function twig_random(Environment $env, $values = null, $max = null) if ('UTF-8' !== $charset) { foreach ($values as $i => $value) { - $values[$i] = iconv('UTF-8', $charset, $value); + $values[$i] = twig_convert_encoding($value, $charset, 'UTF-8'); } } } @@ -885,7 +885,7 @@ function twig_reverse_filter(Environment $env, $item, $preserveKeys = false) $charset = $env->getCharset(); if ('UTF-8' !== $charset) { - $item = iconv($charset, 'UTF-8', $string); + $item = twig_convert_encoding($string, 'UTF-8', $charset); } preg_match_all('/./us', $item, $matches); @@ -893,7 +893,7 @@ function twig_reverse_filter(Environment $env, $item, $preserveKeys = false) $string = implode('', array_reverse($matches[0])); if ('UTF-8' !== $charset) { - $string = iconv('UTF-8', $charset, $string); + $string = twig_convert_encoding($string, $charset, 'UTF-8'); } return $string; @@ -997,6 +997,10 @@ function twig_spaceless($content) function twig_convert_encoding($string, $to, $from) { + if (!function_exists('iconv')) { + throw new RuntimeError('Unable to convert encoding: required function iconv() does not exist. You should install ext-iconv or symfony/polyfill-iconv.'); + } + return iconv($from, $to, $string); } diff --git a/src/Extension/EscaperExtension.php b/src/Extension/EscaperExtension.php index b8dabccf11..136f75f702 100644 --- a/src/Extension/EscaperExtension.php +++ b/src/Extension/EscaperExtension.php @@ -244,7 +244,7 @@ function twig_escape_filter(Environment $env, $string, $strategy = 'html', $char return htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE, $charset); } - $string = iconv($charset, 'UTF-8', $string); + $string = twig_convert_encoding($string, 'UTF-8', $charset); $string = htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); return iconv('UTF-8', $charset, $string); @@ -253,7 +253,7 @@ function twig_escape_filter(Environment $env, $string, $strategy = 'html', $char // escape all non-alphanumeric characters // into their \x or \uHHHH representations if ('UTF-8' !== $charset) { - $string = iconv($charset, 'UTF-8', $string); + $string = twig_convert_encoding($string, 'UTF-8', $charset); } if (!preg_match('//u', $string)) { @@ -301,7 +301,7 @@ function twig_escape_filter(Environment $env, $string, $strategy = 'html', $char case 'css': if ('UTF-8' !== $charset) { - $string = iconv($charset, 'UTF-8', $string); + $string = twig_convert_encoding($string, 'UTF-8', $charset); } if (!preg_match('//u', $string)) { @@ -322,7 +322,7 @@ function twig_escape_filter(Environment $env, $string, $strategy = 'html', $char case 'html_attr': if ('UTF-8' !== $charset) { - $string = iconv($charset, 'UTF-8', $string); + $string = twig_convert_encoding($string, 'UTF-8', $charset); } if (!preg_match('//u', $string)) {