[ Index ]

PHP Cross Reference of phpBB-3.3.5-deutsch

title

Body

[close]

/vendor/twig/twig/src/NodeVisitor/ -> SandboxNodeVisitor.php (source)

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


Generated: Mon Oct 4 17:42:11 2021 Cross-referenced by PHPXref 0.7.1