Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a check to ensure that iconv() is defined #3262

Merged
merged 1 commit into from Feb 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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