1
|
<?php
|
2
|
|
3
|
class Twig_NodeVisitor_SafeAnalysis implements Twig_NodeVisitorInterface
|
4
|
{
|
5
|
protected $data = array();
|
6
|
protected $safeVars = array();
|
7
|
|
8
|
public function setSafeVars($safeVars)
|
9
|
{
|
10
|
$this->safeVars = $safeVars;
|
11
|
}
|
12
|
|
13
|
public function getSafe(Twig_NodeInterface $node)
|
14
|
{
|
15
|
$hash = spl_object_hash($node);
|
16
|
if (isset($this->data[$hash])) {
|
17
|
foreach ($this->data[$hash] as $bucket) {
|
18
|
if ($bucket['key'] === $node) {
|
19
|
return $bucket['value'];
|
20
|
}
|
21
|
}
|
22
|
}
|
23
|
|
24
|
return null;
|
25
|
}
|
26
|
|
27
|
protected function setSafe(Twig_NodeInterface $node, array $safe)
|
28
|
{
|
29
|
$hash = spl_object_hash($node);
|
30
|
if (isset($this->data[$hash])) {
|
31
|
foreach ($this->data[$hash] as &$bucket) {
|
32
|
if ($bucket['key'] === $node) {
|
33
|
$bucket['value'] = $safe;
|
34
|
|
35
|
return;
|
36
|
}
|
37
|
}
|
38
|
}
|
39
|
$this->data[$hash][] = array(
|
40
|
'key' => $node,
|
41
|
'value' => $safe,
|
42
|
);
|
43
|
}
|
44
|
|
45
|
public function enterNode(Twig_NodeInterface $node, Twig_Environment $env)
|
46
|
{
|
47
|
return $node;
|
48
|
}
|
49
|
|
50
|
public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env)
|
51
|
{
|
52
|
if ($node instanceof Twig_Node_Expression_Constant) {
|
53
|
// constants are marked safe for all
|
54
|
$this->setSafe($node, array('all'));
|
55
|
} elseif ($node instanceof Twig_Node_Expression_BlockReference) {
|
56
|
// blocks are safe by definition
|
57
|
$this->setSafe($node, array('all'));
|
58
|
} elseif ($node instanceof Twig_Node_Expression_Parent) {
|
59
|
// parent block is safe by definition
|
60
|
$this->setSafe($node, array('all'));
|
61
|
} elseif ($node instanceof Twig_Node_Expression_Conditional) {
|
62
|
// intersect safeness of both operands
|
63
|
$safe = $this->intersectSafe($this->getSafe($node->getNode('expr2')), $this->getSafe($node->getNode('expr3')));
|
64
|
$this->setSafe($node, $safe);
|
65
|
} elseif ($node instanceof Twig_Node_Expression_Filter) {
|
66
|
// filter expression is safe when the filter is safe
|
67
|
$name = $node->getNode('filter')->getAttribute('value');
|
68
|
$args = $node->getNode('arguments');
|
69
|
if (false !== $filter = $env->getFilter($name)) {
|
70
|
$safe = $filter->getSafe($args);
|
71
|
if (null === $safe) {
|
72
|
$safe = $this->intersectSafe($this->getSafe($node->getNode('node')), $filter->getPreservesSafety());
|
73
|
}
|
74
|
$this->setSafe($node, $safe);
|
75
|
} else {
|
76
|
$this->setSafe($node, array());
|
77
|
}
|
78
|
} elseif ($node instanceof Twig_Node_Expression_Function) {
|
79
|
// function expression is safe when the function is safe
|
80
|
$name = $node->getAttribute('name');
|
81
|
$args = $node->getNode('arguments');
|
82
|
$function = $env->getFunction($name);
|
83
|
if (false !== $function) {
|
84
|
$this->setSafe($node, $function->getSafe($args));
|
85
|
} else {
|
86
|
$this->setSafe($node, array());
|
87
|
}
|
88
|
} elseif ($node instanceof Twig_Node_Expression_MethodCall) {
|
89
|
if ($node->getAttribute('safe')) {
|
90
|
$this->setSafe($node, array('all'));
|
91
|
} else {
|
92
|
$this->setSafe($node, array());
|
93
|
}
|
94
|
} elseif ($node instanceof Twig_Node_Expression_GetAttr && $node->getNode('node') instanceof Twig_Node_Expression_Name) {
|
95
|
$name = $node->getNode('node')->getAttribute('name');
|
96
|
// attributes on template instances are safe
|
97
|
if ('_self' == $name || in_array($name, $this->safeVars)) {
|
98
|
$this->setSafe($node, array('all'));
|
99
|
} else {
|
100
|
$this->setSafe($node, array());
|
101
|
}
|
102
|
} else {
|
103
|
$this->setSafe($node, array());
|
104
|
}
|
105
|
|
106
|
return $node;
|
107
|
}
|
108
|
|
109
|
protected function intersectSafe(array $a = null, array $b = null)
|
110
|
{
|
111
|
if (null === $a || null === $b) {
|
112
|
return array();
|
113
|
}
|
114
|
|
115
|
if (in_array('all', $a)) {
|
116
|
return $b;
|
117
|
}
|
118
|
|
119
|
if (in_array('all', $b)) {
|
120
|
return $a;
|
121
|
}
|
122
|
|
123
|
return array_intersect($a, $b);
|
124
|
}
|
125
|
|
126
|
/**
|
127
|
* {@inheritdoc}
|
128
|
*/
|
129
|
public function getPriority()
|
130
|
{
|
131
|
return 0;
|
132
|
}
|
133
|
}
|