Add a check to ensure that iconv() is defined

This commit is contained in:
Fabien Potencier
2020-02-11 15:56:57 +01:00
parent d8a22bd505
commit 158a8530c5
3 changed files with 13 additions and 9 deletions
+1 -1
View File
@@ -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)
+8 -4
View File
@@ -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);
}
+4 -4
View File
@@ -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)) {