| 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 |
namespace ElementorDeps\Twig\NodeVisitor; |
| 12 |
|
| 13 |
use ElementorDeps\Twig\Environment; |
| 14 |
use ElementorDeps\Twig\Node\CheckSecurityCallNode; |
| 15 |
use ElementorDeps\Twig\Node\CheckSecurityNode; |
| 16 |
use ElementorDeps\Twig\Node\CheckToStringNode; |
| 17 |
use ElementorDeps\Twig\Node\Expression\ArrayExpression; |
| 18 |
use ElementorDeps\Twig\Node\Expression\Binary\ConcatBinary; |
| 19 |
use ElementorDeps\Twig\Node\Expression\Binary\RangeBinary; |
| 20 |
use ElementorDeps\Twig\Node\Expression\FilterExpression; |
| 21 |
use ElementorDeps\Twig\Node\Expression\FunctionExpression; |
| 22 |
use ElementorDeps\Twig\Node\Expression\GetAttrExpression; |
| 23 |
use ElementorDeps\Twig\Node\Expression\NameExpression; |
| 24 |
use ElementorDeps\Twig\Node\Expression\Unary\SpreadUnary; |
| 25 |
use ElementorDeps\Twig\Node\ModuleNode; |
| 26 |
use ElementorDeps\Twig\Node\Node; |
| 27 |
use ElementorDeps\Twig\Node\PrintNode; |
| 28 |
use ElementorDeps\Twig\Node\SetNode; |
| 29 |
/** |
| 30 |
* @author Fabien Potencier <fabien@symfony.com> |
| 31 |
* |
| 32 |
* @internal |
| 33 |
*/ |
| 34 |
final class SandboxNodeVisitor implements NodeVisitorInterface |
| 35 |
{ |
| 36 |
private $inAModule = \false; |
| 37 |
/** @var array<string, int> */ |
| 38 |
private $tags; |
| 39 |
/** @var array<string, int> */ |
| 40 |
private $filters; |
| 41 |
/** @var array<string, int> */ |
| 42 |
private $functions; |
| 43 |
private $needsToStringWrap = \false; |
| 44 |
public function enterNode(Node $node, Environment $env) : Node |
| 45 |
{ |
| 46 |
if ($node instanceof ModuleNode) { |
| 47 |
$this->inAModule = \true; |
| 48 |
$this->tags = []; |
| 49 |
$this->filters = []; |
| 50 |
$this->functions = []; |
| 51 |
return $node; |
| 52 |
} elseif ($this->inAModule) { |
| 53 |
// look for tags |
| 54 |
if ($node->getNodeTag() && !isset($this->tags[$node->getNodeTag()])) { |
| 55 |
$this->tags[$node->getNodeTag()] = $node->getTemplateLine(); |
| 56 |
} |
| 57 |
// look for filters |
| 58 |
if ($node instanceof FilterExpression && !isset($this->filters[$node->getNode('filter')->getAttribute('value')])) { |
| 59 |
$this->filters[$node->getNode('filter')->getAttribute('value')] = $node->getTemplateLine(); |
| 60 |
} |
| 61 |
// look for functions |
| 62 |
if ($node instanceof FunctionExpression && !isset($this->functions[$node->getAttribute('name')])) { |
| 63 |
$this->functions[$node->getAttribute('name')] = $node->getTemplateLine(); |
| 64 |
} |
| 65 |
// the .. operator is equivalent to the range() function |
| 66 |
if ($node instanceof RangeBinary && !isset($this->functions['range'])) { |
| 67 |
$this->functions['range'] = $node->getTemplateLine(); |
| 68 |
} |
| 69 |
if ($node instanceof PrintNode) { |
| 70 |
$this->needsToStringWrap = \true; |
| 71 |
$this->wrapNode($node, 'expr'); |
| 72 |
} |
| 73 |
if ($node instanceof SetNode && !$node->getAttribute('capture')) { |
| 74 |
$this->needsToStringWrap = \true; |
| 75 |
} |
| 76 |
// wrap outer nodes that can implicitly call __toString() |
| 77 |
if ($this->needsToStringWrap) { |
| 78 |
if ($node instanceof ConcatBinary) { |
| 79 |
$this->wrapNode($node, 'left'); |
| 80 |
$this->wrapNode($node, 'right'); |
| 81 |
} |
| 82 |
if ($node instanceof FilterExpression) { |
| 83 |
$this->wrapNode($node, 'node'); |
| 84 |
$this->wrapArrayNode($node, 'arguments'); |
| 85 |
} |
| 86 |
if ($node instanceof FunctionExpression) { |
| 87 |
$this->wrapArrayNode($node, 'arguments'); |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
return $node; |
| 92 |
} |
| 93 |
public function leaveNode(Node $node, Environment $env) : ?Node |
| 94 |
{ |
| 95 |
if ($node instanceof ModuleNode) { |
| 96 |
$this->inAModule = \false; |
| 97 |
$node->setNode('constructor_end', new Node([new CheckSecurityCallNode(), $node->getNode('constructor_end')])); |
| 98 |
$node->setNode('class_end', new Node([new CheckSecurityNode($this->filters, $this->tags, $this->functions), $node->getNode('class_end')])); |
| 99 |
} elseif ($this->inAModule) { |
| 100 |
if ($node instanceof PrintNode || $node instanceof SetNode) { |
| 101 |
$this->needsToStringWrap = \false; |
| 102 |
} |
| 103 |
} |
| 104 |
return $node; |
| 105 |
} |
| 106 |
private function wrapNode(Node $node, string $name) : void |
| 107 |
{ |
| 108 |
$expr = $node->getNode($name); |
| 109 |
if (($expr instanceof NameExpression || $expr instanceof GetAttrExpression) && !$expr->isGenerator()) { |
| 110 |
// Simplify in 4.0 as the spread attribute has been removed there |
| 111 |
$new = new CheckToStringNode($expr); |
| 112 |
if ($expr->hasAttribute('spread')) { |
| 113 |
$new->setAttribute('spread', $expr->getAttribute('spread')); |
| 114 |
} |
| 115 |
$node->setNode($name, $new); |
| 116 |
} elseif ($expr instanceof SpreadUnary) { |
| 117 |
$this->wrapNode($expr, 'node'); |
| 118 |
} elseif ($expr instanceof ArrayExpression) { |
| 119 |
foreach ($expr as $name => $_) { |
| 120 |
$this->wrapNode($expr, $name); |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
private function wrapArrayNode(Node $node, string $name) : void |
| 125 |
{ |
| 126 |
$args = $node->getNode($name); |
| 127 |
foreach ($args as $name => $_) { |
| 128 |
$this->wrapNode($args, $name); |
| 129 |
} |
| 130 |
} |
| 131 |
public function getPriority() : int |
| 132 |
{ |
| 133 |
return 0; |
| 134 |
} |
| 135 |
} |
| 136 |
|