]> git.immae.eu Git - github/wallabag/wallabag.git/blob - vendor/twig/twig/lib/Twig/NodeVisitor/Sandbox.php
fb27045baaf1e7a81ad22f9306ce9c6fd745baf1
[github/wallabag/wallabag.git] / vendor / twig / twig / lib / Twig / NodeVisitor / Sandbox.php
1 <?php
2
3 /*
4 * This file is part of Twig.
5 *
6 * (c) 2009 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 * @author Fabien Potencier <fabien@symfony.com>
16 */
17 class Twig_NodeVisitor_Sandbox implements Twig_NodeVisitorInterface
18 {
19 protected $inAModule = false;
20 protected $tags;
21 protected $filters;
22 protected $functions;
23
24 /**
25 * Called before child nodes are visited.
26 *
27 * @param Twig_NodeInterface $node The node to visit
28 * @param Twig_Environment $env The Twig environment instance
29 *
30 * @return Twig_NodeInterface The modified node
31 */
32 public function enterNode(Twig_NodeInterface $node, Twig_Environment $env)
33 {
34 if ($node instanceof Twig_Node_Module) {
35 $this->inAModule = true;
36 $this->tags = array();
37 $this->filters = array();
38 $this->functions = array();
39
40 return $node;
41 } elseif ($this->inAModule) {
42 // look for tags
43 if ($node->getNodeTag()) {
44 $this->tags[] = $node->getNodeTag();
45 }
46
47 // look for filters
48 if ($node instanceof Twig_Node_Expression_Filter) {
49 $this->filters[] = $node->getNode('filter')->getAttribute('value');
50 }
51
52 // look for functions
53 if ($node instanceof Twig_Node_Expression_Function) {
54 $this->functions[] = $node->getAttribute('name');
55 }
56
57 // wrap print to check __toString() calls
58 if ($node instanceof Twig_Node_Print) {
59 return new Twig_Node_SandboxedPrint($node->getNode('expr'), $node->getLine(), $node->getNodeTag());
60 }
61 }
62
63 return $node;
64 }
65
66 /**
67 * Called after child nodes are visited.
68 *
69 * @param Twig_NodeInterface $node The node to visit
70 * @param Twig_Environment $env The Twig environment instance
71 *
72 * @return Twig_NodeInterface The modified node
73 */
74 public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env)
75 {
76 if ($node instanceof Twig_Node_Module) {
77 $this->inAModule = false;
78
79 return new Twig_Node_SandboxedModule($node, array_unique($this->filters), array_unique($this->tags), array_unique($this->functions));
80 }
81
82 return $node;
83 }
84
85 /**
86 * {@inheritdoc}
87 */
88 public function getPriority()
89 {
90 return 0;
91 }
92 }