1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2024-12-19 10:55:56 +01:00
vichan/inc/lib/Twig/NodeVisitor/Escaper.php

155 lines
4.9 KiB
PHP
Raw Normal View History

2011-10-05 06:22:53 +02:00
<?php
/*
* This file is part of Twig.
*
2018-05-10 12:24:53 +02:00
* (c) Fabien Potencier
2011-10-05 06:22:53 +02:00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Twig_NodeVisitor_Escaper implements output escaping.
*
2018-05-10 12:24:53 +02:00
* @final
*
2013-08-01 21:20:12 +02:00
* @author Fabien Potencier <fabien@symfony.com>
2011-10-05 06:22:53 +02:00
*/
2018-05-10 12:24:53 +02:00
class Twig_NodeVisitor_Escaper extends Twig_BaseNodeVisitor
2011-10-05 06:22:53 +02:00
{
protected $statusStack = array();
protected $blocks = array();
protected $safeAnalysis;
protected $traverser;
2013-08-01 21:20:12 +02:00
protected $defaultStrategy = false;
protected $safeVars = array();
2011-10-05 06:22:53 +02:00
2013-08-01 21:20:12 +02:00
public function __construct()
2011-10-05 06:22:53 +02:00
{
$this->safeAnalysis = new Twig_NodeVisitor_SafeAnalysis();
}
2018-05-10 12:24:53 +02:00
protected function doEnterNode(Twig_Node $node, Twig_Environment $env)
2011-10-05 06:22:53 +02:00
{
2013-08-01 21:20:12 +02:00
if ($node instanceof Twig_Node_Module) {
2018-05-10 12:24:53 +02:00
if ($env->hasExtension('Twig_Extension_Escaper') && $defaultStrategy = $env->getExtension('Twig_Extension_Escaper')->getDefaultStrategy($node->getTemplateName())) {
2013-08-01 21:20:12 +02:00
$this->defaultStrategy = $defaultStrategy;
}
$this->safeVars = array();
2018-05-10 12:24:53 +02:00
$this->blocks = array();
2013-08-01 21:20:12 +02:00
} elseif ($node instanceof Twig_Node_AutoEscape) {
2011-10-05 06:22:53 +02:00
$this->statusStack[] = $node->getAttribute('value');
} elseif ($node instanceof Twig_Node_Block) {
$this->statusStack[] = isset($this->blocks[$node->getAttribute('name')]) ? $this->blocks[$node->getAttribute('name')] : $this->needEscaping($env);
2013-08-01 21:20:12 +02:00
} elseif ($node instanceof Twig_Node_Import) {
$this->safeVars[] = $node->getNode('var')->getAttribute('name');
2011-10-05 06:22:53 +02:00
}
return $node;
}
2018-05-10 12:24:53 +02:00
protected function doLeaveNode(Twig_Node $node, Twig_Environment $env)
2011-10-05 06:22:53 +02:00
{
2013-08-01 21:20:12 +02:00
if ($node instanceof Twig_Node_Module) {
$this->defaultStrategy = false;
$this->safeVars = array();
2018-05-10 12:24:53 +02:00
$this->blocks = array();
2013-08-01 21:20:12 +02:00
} elseif ($node instanceof Twig_Node_Expression_Filter) {
2011-10-05 06:22:53 +02:00
return $this->preEscapeFilterNode($node, $env);
} elseif ($node instanceof Twig_Node_Print) {
return $this->escapePrintNode($node, $env, $this->needEscaping($env));
}
if ($node instanceof Twig_Node_AutoEscape || $node instanceof Twig_Node_Block) {
array_pop($this->statusStack);
} elseif ($node instanceof Twig_Node_BlockReference) {
$this->blocks[$node->getAttribute('name')] = $this->needEscaping($env);
}
return $node;
}
protected function escapePrintNode(Twig_Node_Print $node, Twig_Environment $env, $type)
{
if (false === $type) {
return $node;
}
$expression = $node->getNode('expr');
if ($this->isSafeFor($type, $expression, $env)) {
return $node;
}
$class = get_class($node);
return new $class(
$this->getEscaperFilter($type, $expression),
2018-05-10 12:24:53 +02:00
$node->getTemplateLine()
2011-10-05 06:22:53 +02:00
);
}
protected function preEscapeFilterNode(Twig_Node_Expression_Filter $filter, Twig_Environment $env)
{
$name = $filter->getNode('filter')->getAttribute('value');
2013-08-01 21:20:12 +02:00
$type = $env->getFilter($name)->getPreEscape();
if (null === $type) {
return $filter;
}
2011-10-05 06:22:53 +02:00
2013-08-01 21:20:12 +02:00
$node = $filter->getNode('node');
if ($this->isSafeFor($type, $node, $env)) {
2011-10-05 06:22:53 +02:00
return $filter;
}
2013-08-01 21:20:12 +02:00
$filter->setNode('node', $this->getEscaperFilter($type, $node));
2011-10-05 06:22:53 +02:00
return $filter;
}
protected function isSafeFor($type, Twig_NodeInterface $expression, $env)
{
$safe = $this->safeAnalysis->getSafe($expression);
if (null === $safe) {
if (null === $this->traverser) {
$this->traverser = new Twig_NodeTraverser($env, array($this->safeAnalysis));
}
2013-08-01 21:20:12 +02:00
$this->safeAnalysis->setSafeVars($this->safeVars);
2011-10-05 06:22:53 +02:00
$this->traverser->traverse($expression);
$safe = $this->safeAnalysis->getSafe($expression);
}
return in_array($type, $safe) || in_array('all', $safe);
}
protected function needEscaping(Twig_Environment $env)
{
if (count($this->statusStack)) {
return $this->statusStack[count($this->statusStack) - 1];
}
2013-08-01 21:20:12 +02:00
return $this->defaultStrategy ? $this->defaultStrategy : false;
2011-10-05 06:22:53 +02:00
}
protected function getEscaperFilter($type, Twig_NodeInterface $node)
{
2018-05-10 12:24:53 +02:00
$line = $node->getTemplateLine();
2011-10-05 06:22:53 +02:00
$name = new Twig_Node_Expression_Constant('escape', $line);
2013-08-01 21:20:12 +02:00
$args = new Twig_Node(array(new Twig_Node_Expression_Constant((string) $type, $line), new Twig_Node_Expression_Constant(null, $line), new Twig_Node_Expression_Constant(true, $line)));
2011-10-05 06:22:53 +02:00
return new Twig_Node_Expression_Filter($node, $name, $args, $line);
}
public function getPriority()
{
return 0;
}
}
2018-05-10 12:24:53 +02:00
class_alias('Twig_NodeVisitor_Escaper', 'Twig\NodeVisitor\EscaperNodeVisitor', false);