Browse code

Merge branch '2.x' into 3.x

* 2.x:
Add a check to ensure that iconv() is defined

Fabien Potencier authored on 11/02/2020 15:22:55
Showing 3 changed files
... ...
@@ -36,7 +36,7 @@
36 36
 
37 37
 * 2.12.5 (2020-XX-XX)
38 38
 
39
- * n/a
39
+ * Add a check to ensure that iconv() is defined
40 40
 
41 41
 * 2.12.4 (2020-02-11)
42 42
 
... ...
@@ -373,7 +373,7 @@ function twig_random(Environment $env, $values = null, $max = null)
373 373
         $charset = $env->getCharset();
374 374
 
375 375
         if ('UTF-8' !== $charset) {
376
-            $values = iconv($charset, 'UTF-8', $values);
376
+            $values = twig_convert_encoding($values, 'UTF-8', $charset);
377 377
         }
378 378
 
379 379
         // unicode version of str_split()
... ...
@@ -382,7 +382,7 @@ function twig_random(Environment $env, $values = null, $max = null)
382 382
 
383 383
         if ('UTF-8' !== $charset) {
384 384
             foreach ($values as $i => $value) {
385
-                $values[$i] = iconv('UTF-8', $charset, $value);
385
+                $values[$i] = twig_convert_encoding($value, $charset, 'UTF-8');
386 386
             }
387 387
         }
388 388
     }
... ...
@@ -847,7 +847,7 @@ function twig_reverse_filter(Environment $env, $item, $preserveKeys = false)
847 847
     $charset = $env->getCharset();
848 848
 
849 849
     if ('UTF-8' !== $charset) {
850
-        $item = iconv($charset, 'UTF-8', $string);
850
+        $item = twig_convert_encoding($string, 'UTF-8', $charset);
851 851
     }
852 852
 
853 853
     preg_match_all('/./us', $item, $matches);
... ...
@@ -855,7 +855,7 @@ function twig_reverse_filter(Environment $env, $item, $preserveKeys = false)
855 855
     $string = implode('', array_reverse($matches[0]));
856 856
 
857 857
     if ('UTF-8' !== $charset) {
858
-        $string = iconv('UTF-8', $charset, $string);
858
+        $string = twig_convert_encoding($string, $charset, 'UTF-8');
859 859
     }
860 860
 
861 861
     return $string;
... ...
@@ -1029,6 +1029,10 @@ function twig_spaceless($content)
1029 1029
 
1030 1030
 function twig_convert_encoding($string, $to, $from)
1031 1031
 {
1032
+    if (!function_exists('iconv')) {
1033
+        throw new RuntimeError('Unable to convert encoding: required function iconv() does not exist. You should install ext-iconv or symfony/polyfill-iconv.');
1034
+    }
1035
+
1032 1036
     return iconv($from, $to, $string);
1033 1037
 }
1034 1038
 
... ...
@@ -239,7 +239,7 @@ function twig_escape_filter(Environment $env, $string, $strategy = 'html', $char
239 239
                 return htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE, $charset);
240 240
             }
241 241
 
242
-            $string = iconv($charset, 'UTF-8', $string);
242
+            $string = twig_convert_encoding($string, 'UTF-8', $charset);
243 243
             $string = htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
244 244
 
245 245
             return iconv('UTF-8', $charset, $string);
... ...
@@ -248,7 +248,7 @@ function twig_escape_filter(Environment $env, $string, $strategy = 'html', $char
248 248
             // escape all non-alphanumeric characters
249 249
             // into their \x or \uHHHH representations
250 250
             if ('UTF-8' !== $charset) {
251
-                $string = iconv($charset, 'UTF-8', $string);
251
+                $string = twig_convert_encoding($string, 'UTF-8', $charset);
252 252
             }
253 253
 
254 254
             if (!preg_match('//u', $string)) {
... ...
@@ -296,7 +296,7 @@ function twig_escape_filter(Environment $env, $string, $strategy = 'html', $char
296 296
 
297 297
         case 'css':
298 298
             if ('UTF-8' !== $charset) {
299
-                $string = iconv($charset, 'UTF-8', $string);
299
+                $string = twig_convert_encoding($string, 'UTF-8', $charset);
300 300
             }
301 301
 
302 302
             if (!preg_match('//u', $string)) {
... ...
@@ -317,7 +317,7 @@ function twig_escape_filter(Environment $env, $string, $strategy = 'html', $char
317 317
 
318 318
         case 'html_attr':
319 319
             if ('UTF-8' !== $charset) {
320
-                $string = iconv($charset, 'UTF-8', $string);
320
+                $string = twig_convert_encoding($string, 'UTF-8', $charset);
321 321
             }
322 322
 
323 323
             if (!preg_match('//u', $string)) {