Browse code

Add a check to ensure that iconv() is defined

Fabien Potencier authored on 11/02/2020 14:56:57
Showing 3 changed files
... ...
@@ -1,6 +1,6 @@
1 1
 * 2.12.5 (2020-XX-XX)
2 2
 
3
- * n/a
3
+ * Add a check to ensure that iconv() is defined
4 4
 
5 5
 * 2.12.4 (2020-02-11)
6 6
 
... ...
@@ -411,7 +411,7 @@ function twig_random(Environment $env, $values = null, $max = null)
411 411
         $charset = $env->getCharset();
412 412
 
413 413
         if ('UTF-8' !== $charset) {
414
-            $values = iconv($charset, 'UTF-8', $values);
414
+            $values = twig_convert_encoding($values, 'UTF-8', $charset);
415 415
         }
416 416
 
417 417
         // unicode version of str_split()
... ...
@@ -420,7 +420,7 @@ function twig_random(Environment $env, $values = null, $max = null)
420 420
 
421 421
         if ('UTF-8' !== $charset) {
422 422
             foreach ($values as $i => $value) {
423
-                $values[$i] = iconv('UTF-8', $charset, $value);
423
+                $values[$i] = twig_convert_encoding($value, $charset, 'UTF-8');
424 424
             }
425 425
         }
426 426
     }
... ...
@@ -885,7 +885,7 @@ function twig_reverse_filter(Environment $env, $item, $preserveKeys = false)
885 885
     $charset = $env->getCharset();
886 886
 
887 887
     if ('UTF-8' !== $charset) {
888
-        $item = iconv($charset, 'UTF-8', $string);
888
+        $item = twig_convert_encoding($string, 'UTF-8', $charset);
889 889
     }
890 890
 
891 891
     preg_match_all('/./us', $item, $matches);
... ...
@@ -893,7 +893,7 @@ function twig_reverse_filter(Environment $env, $item, $preserveKeys = false)
893 893
     $string = implode('', array_reverse($matches[0]));
894 894
 
895 895
     if ('UTF-8' !== $charset) {
896
-        $string = iconv('UTF-8', $charset, $string);
896
+        $string = twig_convert_encoding($string, $charset, 'UTF-8');
897 897
     }
898 898
 
899 899
     return $string;
... ...
@@ -997,6 +997,10 @@ function twig_spaceless($content)
997 997
 
998 998
 function twig_convert_encoding($string, $to, $from)
999 999
 {
1000
+    if (!function_exists('iconv')) {
1001
+        throw new RuntimeError('Unable to convert encoding: required function iconv() does not exist. You should install ext-iconv or symfony/polyfill-iconv.');
1002
+    }
1003
+
1000 1004
     return iconv($from, $to, $string);
1001 1005
 }
1002 1006
 
... ...
@@ -244,7 +244,7 @@ function twig_escape_filter(Environment $env, $string, $strategy = 'html', $char
244 244
                 return htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE, $charset);
245 245
             }
246 246
 
247
-            $string = iconv($charset, 'UTF-8', $string);
247
+            $string = twig_convert_encoding($string, 'UTF-8', $charset);
248 248
             $string = htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
249 249
 
250 250
             return iconv('UTF-8', $charset, $string);
... ...
@@ -253,7 +253,7 @@ function twig_escape_filter(Environment $env, $string, $strategy = 'html', $char
253 253
             // escape all non-alphanumeric characters
254 254
             // into their \x or \uHHHH representations
255 255
             if ('UTF-8' !== $charset) {
256
-                $string = iconv($charset, 'UTF-8', $string);
256
+                $string = twig_convert_encoding($string, 'UTF-8', $charset);
257 257
             }
258 258
 
259 259
             if (!preg_match('//u', $string)) {
... ...
@@ -301,7 +301,7 @@ function twig_escape_filter(Environment $env, $string, $strategy = 'html', $char
301 301
 
302 302
         case 'css':
303 303
             if ('UTF-8' !== $charset) {
304
-                $string = iconv($charset, 'UTF-8', $string);
304
+                $string = twig_convert_encoding($string, 'UTF-8', $charset);
305 305
             }
306 306
 
307 307
             if (!preg_match('//u', $string)) {
... ...
@@ -322,7 +322,7 @@ function twig_escape_filter(Environment $env, $string, $strategy = 'html', $char
322 322
 
323 323
         case 'html_attr':
324 324
             if ('UTF-8' !== $charset) {
325
-                $string = iconv($charset, 'UTF-8', $string);
325
+                $string = twig_convert_encoding($string, 'UTF-8', $charset);
326 326
             }
327 327
 
328 328
             if (!preg_match('//u', $string)) {