Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
SafeAnalysisNodeVisitor.php
001 <?php
002
003 /*
004 * This file is part of Twig.
005 *
006 * (c) Fabien Potencier
007 *
008 * For the full copyright and license information, please view the LICENSE
009 * file that was distributed with this source code.
010 */
011
012 namespace Twig\NodeVisitor;
013
014 use Twig\Environment;
015 use Twig\Node\Expression\BlockReferenceExpression;
016 use Twig\Node\Expression\ConditionalExpression;
017 use Twig\Node\Expression\ConstantExpression;
018 use Twig\Node\Expression\FilterExpression;
019 use Twig\Node\Expression\FunctionExpression;
020 use Twig\Node\Expression\GetAttrExpression;
021 use Twig\Node\Expression\MethodCallExpression;
022 use Twig\Node\Expression\NameExpression;
023 use Twig\Node\Expression\ParentExpression;
024 use Twig\Node\Node;
025
026 final class SafeAnalysisNodeVisitor extends AbstractNodeVisitor
027 {
028 private $data = [];
029 private $safeVars = [];
030
031 public function setSafeVars($safeVars)
032 {
033 $this->safeVars = $safeVars;
034 }
035
036 public function getSafe(Node $node)
037 {
038 $hash = spl_object_hash($node);
039 if (!isset($this->data[$hash])) {
040 return;
041 }
042
043 foreach ($this->data[$hash] as $bucket) {
044 if ($bucket['key'] !== $node) {
045 continue;
046 }
047
048 if (\in_array('html_attr', $bucket['value'])) {
049 $bucket['value'][] = 'html';
050 }
051
052 return $bucket['value'];
053 }
054 }
055
056 private function setSafe(Node $node, array $safe)
057 {
058 $hash = spl_object_hash($node);
059 if (isset($this->data[$hash])) {
060 foreach ($this->data[$hash] as &$bucket) {
061 if ($bucket['key'] === $node) {
062 $bucket['value'] = $safe;
063
064 return;
065 }
066 }
067 }
068 $this->data[$hash][] = [
069 'key' => $node,
070 'value' => $safe,
071 ];
072 }
073
074 protected function doEnterNode(Node $node, Environment $env)
075 {
076 return $node;
077 }
078
079 protected function doLeaveNode(Node $node, Environment $env)
080 {
081 if ($node instanceof ConstantExpression) {
082 // constants are marked safe for all
083 $this->setSafe($node, ['all']);
084 } elseif ($node instanceof BlockReferenceExpression) {
085 // blocks are safe by definition
086 $this->setSafe($node, ['all']);
087 } elseif ($node instanceof ParentExpression) {
088 // parent block is safe by definition
089 $this->setSafe($node, ['all']);
090 } elseif ($node instanceof ConditionalExpression) {
091 // intersect safeness of both operands
092 $safe = $this->intersectSafe($this->getSafe($node->getNode('expr2')), $this->getSafe($node->getNode('expr3')));
093 $this->setSafe($node, $safe);
094 } elseif ($node instanceof FilterExpression) {
095 // filter expression is safe when the filter is safe
096 $name = $node->getNode('filter')->getAttribute('value');
097 $args = $node->getNode('arguments');
098 if (false !== $filter = $env->getFilter($name)) {
099 $safe = $filter->getSafe($args);
100 if (null === $safe) {
101 $safe = $this->intersectSafe($this->getSafe($node->getNode('node')), $filter->getPreservesSafety());
102 }
103 $this->setSafe($node, $safe);
104 } else {
105 $this->setSafe($node, []);
106 }
107 } elseif ($node instanceof FunctionExpression) {
108 // function expression is safe when the function is safe
109 $name = $node->getAttribute('name');
110 $args = $node->getNode('arguments');
111 $function = $env->getFunction($name);
112 if (false !== $function) {
113 $this->setSafe($node, $function->getSafe($args));
114 } else {
115 $this->setSafe($node, []);
116 }
117 } elseif ($node instanceof MethodCallExpression) {
118 if ($node->getAttribute('safe')) {
119 $this->setSafe($node, ['all']);
120 } else {
121 $this->setSafe($node, []);
122 }
123 } elseif ($node instanceof GetAttrExpression && $node->getNode('node') instanceof NameExpression) {
124 $name = $node->getNode('node')->getAttribute('name');
125 if (\in_array($name, $this->safeVars)) {
126 $this->setSafe($node, ['all']);
127 } else {
128 $this->setSafe($node, []);
129 }
130 } else {
131 $this->setSafe($node, []);
132 }
133
134 return $node;
135 }
136
137 private function intersectSafe(array $a = null, array $b = null): array
138 {
139 if (null === $a || null === $b) {
140 return [];
141 }
142
143 if (\in_array('all', $a)) {
144 return $b;
145 }
146
147 if (\in_array('all', $b)) {
148 return $a;
149 }
150
151 return array_intersect($a, $b);
152 }
153
154 public function getPriority()
155 {
156 return 0;
157 }
158 }
159
160 class_alias('Twig\NodeVisitor\SafeAnalysisNodeVisitor', 'Twig_NodeVisitor_SafeAnalysis');
161