Yaffs site version 1.1
[yaffs-website] / vendor / twig / twig / lib / Twig / NodeVisitor / Sandbox.php
1 <?php
2
3 /*
4  * This file is part of Twig.
5  *
6  * (c) 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_NodeVisitor_Sandbox implements sandboxing.
14  *
15  * @final
16  *
17  * @author Fabien Potencier <fabien@symfony.com>
18  */
19 class Twig_NodeVisitor_Sandbox extends Twig_BaseNodeVisitor
20 {
21     protected $inAModule = false;
22     protected $tags;
23     protected $filters;
24     protected $functions;
25
26     protected function doEnterNode(Twig_Node $node, Twig_Environment $env)
27     {
28         if ($node instanceof Twig_Node_Module) {
29             $this->inAModule = true;
30             $this->tags = array();
31             $this->filters = array();
32             $this->functions = array();
33
34             return $node;
35         } elseif ($this->inAModule) {
36             // look for tags
37             if ($node->getNodeTag() && !isset($this->tags[$node->getNodeTag()])) {
38                 $this->tags[$node->getNodeTag()] = $node;
39             }
40
41             // look for filters
42             if ($node instanceof Twig_Node_Expression_Filter && !isset($this->filters[$node->getNode('filter')->getAttribute('value')])) {
43                 $this->filters[$node->getNode('filter')->getAttribute('value')] = $node;
44             }
45
46             // look for functions
47             if ($node instanceof Twig_Node_Expression_Function && !isset($this->functions[$node->getAttribute('name')])) {
48                 $this->functions[$node->getAttribute('name')] = $node;
49             }
50
51             // wrap print to check __toString() calls
52             if ($node instanceof Twig_Node_Print) {
53                 return new Twig_Node_SandboxedPrint($node->getNode('expr'), $node->getTemplateLine(), $node->getNodeTag());
54             }
55         }
56
57         return $node;
58     }
59
60     protected function doLeaveNode(Twig_Node $node, Twig_Environment $env)
61     {
62         if ($node instanceof Twig_Node_Module) {
63             $this->inAModule = false;
64
65             $node->setNode('display_start', new Twig_Node(array(new Twig_Node_CheckSecurity($this->filters, $this->tags, $this->functions), $node->getNode('display_start'))));
66         }
67
68         return $node;
69     }
70
71     public function getPriority()
72     {
73         return 0;
74     }
75 }
76
77 class_alias('Twig_NodeVisitor_Sandbox', 'Twig\NodeVisitor\SandboxNodeVisitor', false);