1 | <?php |
---|
2 | |
---|
3 | /* |
---|
4 | * This file is part of Twig. |
---|
5 | * |
---|
6 | * (c) 2010 Fabien Potencier |
---|
7 | * |
---|
8 | * For the full copyright and license information, please view the LICENSE |
---|
9 | * file that was distributed with this source code. |
---|
10 | */ |
---|
11 | |
---|
12 | /** |
---|
13 | * Twig_Node_SandboxedPrint adds a check for the __toString() method |
---|
14 | * when the variable is an object and the sandbox is activated. |
---|
15 | * |
---|
16 | * When there is a simple Print statement, like {{ article }}, |
---|
17 | * and if the sandbox is enabled, we need to check that the __toString() |
---|
18 | * method is allowed if 'article' is an object. |
---|
19 | * |
---|
20 | * @package twig |
---|
21 | * @author Fabien Potencier <fabien.potencier@symfony-project.com> |
---|
22 | */ |
---|
23 | class Twig_Node_SandboxedPrint extends Twig_Node_Print |
---|
24 | { |
---|
25 | public function __construct(Twig_Node_Expression $expr, $lineno, $tag = null) |
---|
26 | { |
---|
27 | parent::__construct($expr, $lineno, $tag); |
---|
28 | } |
---|
29 | |
---|
30 | /** |
---|
31 | * Compiles the node to PHP. |
---|
32 | * |
---|
33 | * @param Twig_Compiler A Twig_Compiler instance |
---|
34 | */ |
---|
35 | public function compile(Twig_Compiler $compiler) |
---|
36 | { |
---|
37 | $compiler |
---|
38 | ->addDebugInfo($this) |
---|
39 | ->write('echo $this->env->getExtension(\'sandbox\')->ensureToStringAllowed(') |
---|
40 | ->subcompile($this->getNode('expr')) |
---|
41 | ->raw(");\n") |
---|
42 | ; |
---|
43 | } |
---|
44 | |
---|
45 | /** |
---|
46 | * Removes node filters. |
---|
47 | * |
---|
48 | * This is mostly needed when another visitor adds filters (like the escaper one). |
---|
49 | * |
---|
50 | * @param Twig_Node $node A Node |
---|
51 | */ |
---|
52 | protected function removeNodeFilter($node) |
---|
53 | { |
---|
54 | if ($node instanceof Twig_Node_Expression_Filter) { |
---|
55 | return $this->removeNodeFilter($node->getNode('node')); |
---|
56 | } |
---|
57 | |
---|
58 | return $node; |
---|
59 | } |
---|
60 | } |
---|