diff options
author | Nicolas LÅ“uillet <nicolas.loeuillet@gmail.com> | 2013-08-02 22:40:51 +0200 |
---|---|---|
committer | Nicolas LÅ“uillet <nicolas.loeuillet@gmail.com> | 2013-08-02 22:40:51 +0200 |
commit | a4565e88edbc8e3bd092a475469769c86a4c350c (patch) | |
tree | a6a3c935b03a23ff87575c8c315cf8ba78fe68c2 /inc/Twig/NodeVisitor/Sandbox.php | |
parent | f6c9baab3efeec1d0efa151e276fc08d5b58f9e9 (diff) | |
download | wallabag-a4565e88edbc8e3bd092a475469769c86a4c350c.tar.gz wallabag-a4565e88edbc8e3bd092a475469769c86a4c350c.tar.zst wallabag-a4565e88edbc8e3bd092a475469769c86a4c350c.zip |
add Twig & refactor poche
Diffstat (limited to 'inc/Twig/NodeVisitor/Sandbox.php')
-rw-r--r-- | inc/Twig/NodeVisitor/Sandbox.php | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/inc/Twig/NodeVisitor/Sandbox.php b/inc/Twig/NodeVisitor/Sandbox.php new file mode 100644 index 00000000..fb27045b --- /dev/null +++ b/inc/Twig/NodeVisitor/Sandbox.php | |||
@@ -0,0 +1,92 @@ | |||
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 | } | ||