Skip to content

Commit

Permalink
bug #3262 Add a check to ensure that iconv() is defined (fabpot)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.x branch.

Discussion
----------

Add a check to ensure that iconv() is defined

Commits
-------

158a853 Add a check to ensure that iconv() is defined
  • Loading branch information
fabpot committed Feb 11, 2020
2 parents d8a22bd + 158a853 commit f0ccbfc
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion 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)

Expand Down
12 changes: 8 additions & 4 deletions src/Extension/CoreExtension.php
Expand Up @@ -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()
Expand All @@ -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');
}
}
}
Expand Down Expand Up @@ -885,15 +885,15 @@ 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);

$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;
Expand Down Expand Up @@ -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);
}

Expand Down
8 changes: 4 additions & 4 deletions src/Extension/EscaperExtension.php
Expand Up @@ -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);
Expand All @@ -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)) {
Expand Down Expand Up @@ -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)) {
Expand All @@ -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)) {
Expand Down

0 comments on commit f0ccbfc

Please sign in to comment.