@@ -237,7 +237,7 @@ public function getFilters()
237
237
// array helpers
238
238
new TwigFilter ('join ' , 'twig_join_filter ' ),
239
239
new TwigFilter ('split ' , 'twig_split_filter ' , ['needs_environment ' => true ]),
240
- new TwigFilter ('sort ' , 'twig_sort_filter ' ),
240
+ new TwigFilter ('sort ' , 'twig_sort_filter ' , [ ' needs_environment ' => true ] ),
241
241
new TwigFilter ('merge ' , 'twig_array_merge ' ),
242
242
new TwigFilter ('batch ' , 'twig_array_batch ' ),
243
243
new TwigFilter ('column ' , 'twig_array_column ' ),
@@ -926,7 +926,7 @@ function twig_reverse_filter(Environment $env, $item, $preserveKeys = false)
926
926
*
927
927
* @return array
928
928
*/
929
- function twig_sort_filter ($ array , $ arrow = null )
929
+ function twig_sort_filter (Environment $ env , $ array , $ arrow = null )
930
930
{
931
931
if ($ array instanceof \Traversable) {
932
932
$ array = iterator_to_array ($ array );
@@ -935,6 +935,8 @@ function twig_sort_filter($array, $arrow = null)
935
935
}
936
936
937
937
if (null !== $ arrow ) {
938
+ twig_check_arrow_in_sandbox ($ env , $ arrow , 'sort ' , 'filter ' );
939
+
938
940
uasort ($ array , $ arrow );
939
941
} else {
940
942
asort ($ array );
@@ -1606,9 +1608,7 @@ function twig_array_filter(Environment $env, $array, $arrow)
1606
1608
throw new RuntimeError (sprintf ('The "filter" filter expects an array or "Traversable", got "%s". ' , \is_object ($ array ) ? \get_class ($ array ) : \gettype ($ array )));
1607
1609
}
1608
1610
1609
- if (!$ arrow instanceof Closure && $ env ->hasExtension ('\Twig\Extension\SandboxExtension ' ) && $ env ->getExtension ('\Twig\Extension\SandboxExtension ' )->isSandboxed ()) {
1610
- throw new RuntimeError ('The callable passed to "filter" filter must be a Closure in sandbox mode. ' );
1611
- }
1611
+ twig_check_arrow_in_sandbox ($ env , $ arrow , 'filter ' , 'filter ' );
1612
1612
1613
1613
if (\is_array ($ array )) {
1614
1614
return array_filter ($ array , $ arrow , \ARRAY_FILTER_USE_BOTH );
@@ -1620,9 +1620,7 @@ function twig_array_filter(Environment $env, $array, $arrow)
1620
1620
1621
1621
function twig_array_map (Environment $ env , $ array , $ arrow )
1622
1622
{
1623
- if (!$ arrow instanceof Closure && $ env ->hasExtension ('\Twig\Extension\SandboxExtension ' ) && $ env ->getExtension ('\Twig\Extension\SandboxExtension ' )->isSandboxed ()) {
1624
- throw new RuntimeError ('The callable passed to the "map" filter must be a Closure in sandbox mode. ' );
1625
- }
1623
+ twig_check_arrow_in_sandbox ($ env , $ arrow , 'map ' , 'filter ' );
1626
1624
1627
1625
$ r = [];
1628
1626
foreach ($ array as $ k => $ v ) {
@@ -1634,9 +1632,7 @@ function twig_array_map(Environment $env, $array, $arrow)
1634
1632
1635
1633
function twig_array_reduce (Environment $ env , $ array , $ arrow , $ initial = null )
1636
1634
{
1637
- if (!$ arrow instanceof Closure && $ env ->hasExtension ('\Twig\Extension\SandboxExtension ' ) && $ env ->getExtension ('\Twig\Extension\SandboxExtension ' )->isSandboxed ()) {
1638
- throw new RuntimeError ('The callable passed to the "reduce" filter must be a Closure in sandbox mode. ' );
1639
- }
1635
+ twig_check_arrow_in_sandbox ($ env , $ arrow , 'reduce ' , 'filter ' );
1640
1636
1641
1637
if (!\is_array ($ array )) {
1642
1638
if (!$ array instanceof \Traversable) {
@@ -1648,4 +1644,11 @@ function twig_array_reduce(Environment $env, $array, $arrow, $initial = null)
1648
1644
1649
1645
return array_reduce ($ array , $ arrow , $ initial );
1650
1646
}
1647
+
1648
+ function twig_check_arrow_in_sandbox (Environment $ env , $ arrow , $ thing , $ type )
1649
+ {
1650
+ if (!$ arrow instanceof Closure && $ env ->hasExtension ('\Twig\Extension\SandboxExtension ' ) && $ env ->getExtension ('\Twig\Extension\SandboxExtension ' )->isSandboxed ()) {
1651
+ throw new RuntimeError (sprintf ('The callable passed to the "%s" %s must be a Closure in sandbox mode. ' , $ thing , $ type ));
1652
+ }
1653
+ }
1651
1654
}
0 commit comments