1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2024-12-22 20:35:54 +01:00
vichan/inc/lib/Twig/NodeVisitor/SafeAnalysis.php

151 lines
4.7 KiB
PHP
Raw Normal View History

2011-10-05 06:22:53 +02:00
<?php
2018-05-10 12:24:53 +02:00
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @final
*/
class Twig_NodeVisitor_SafeAnalysis extends Twig_BaseNodeVisitor
2011-10-05 06:22:53 +02:00
{
protected $data = array();
2013-08-01 21:20:12 +02:00
protected $safeVars = array();
public function setSafeVars($safeVars)
{
$this->safeVars = $safeVars;
}
2011-10-05 06:22:53 +02:00
public function getSafe(Twig_NodeInterface $node)
{
$hash = spl_object_hash($node);
2018-05-10 12:24:53 +02:00
if (!isset($this->data[$hash])) {
return;
}
foreach ($this->data[$hash] as $bucket) {
if ($bucket['key'] !== $node) {
continue;
2011-10-05 06:22:53 +02:00
}
2018-05-10 12:24:53 +02:00
if (in_array('html_attr', $bucket['value'])) {
$bucket['value'][] = 'html';
}
return $bucket['value'];
2011-10-05 06:22:53 +02:00
}
}
protected function setSafe(Twig_NodeInterface $node, array $safe)
{
$hash = spl_object_hash($node);
if (isset($this->data[$hash])) {
2013-08-01 21:20:12 +02:00
foreach ($this->data[$hash] as &$bucket) {
2011-10-05 06:22:53 +02:00
if ($bucket['key'] === $node) {
$bucket['value'] = $safe;
2013-08-01 21:20:12 +02:00
2011-10-05 06:22:53 +02:00
return;
}
}
}
$this->data[$hash][] = array(
'key' => $node,
'value' => $safe,
);
}
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
{
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
{
if ($node instanceof Twig_Node_Expression_Constant) {
// constants are marked safe for all
$this->setSafe($node, array('all'));
} elseif ($node instanceof Twig_Node_Expression_BlockReference) {
// blocks are safe by definition
$this->setSafe($node, array('all'));
} elseif ($node instanceof Twig_Node_Expression_Parent) {
// parent block is safe by definition
$this->setSafe($node, array('all'));
} elseif ($node instanceof Twig_Node_Expression_Conditional) {
// intersect safeness of both operands
$safe = $this->intersectSafe($this->getSafe($node->getNode('expr2')), $this->getSafe($node->getNode('expr3')));
$this->setSafe($node, $safe);
} elseif ($node instanceof Twig_Node_Expression_Filter) {
// filter expression is safe when the filter is safe
$name = $node->getNode('filter')->getAttribute('value');
$args = $node->getNode('arguments');
if (false !== $filter = $env->getFilter($name)) {
2013-08-01 21:20:12 +02:00
$safe = $filter->getSafe($args);
if (null === $safe) {
$safe = $this->intersectSafe($this->getSafe($node->getNode('node')), $filter->getPreservesSafety());
}
$this->setSafe($node, $safe);
2011-10-05 06:22:53 +02:00
} else {
$this->setSafe($node, array());
}
} elseif ($node instanceof Twig_Node_Expression_Function) {
// function expression is safe when the function is safe
$name = $node->getAttribute('name');
$args = $node->getNode('arguments');
$function = $env->getFunction($name);
if (false !== $function) {
$this->setSafe($node, $function->getSafe($args));
} else {
$this->setSafe($node, array());
}
2013-08-01 21:20:12 +02:00
} elseif ($node instanceof Twig_Node_Expression_MethodCall) {
if ($node->getAttribute('safe')) {
$this->setSafe($node, array('all'));
} else {
$this->setSafe($node, array());
}
} elseif ($node instanceof Twig_Node_Expression_GetAttr && $node->getNode('node') instanceof Twig_Node_Expression_Name) {
$name = $node->getNode('node')->getAttribute('name');
// attributes on template instances are safe
if ('_self' == $name || in_array($name, $this->safeVars)) {
$this->setSafe($node, array('all'));
} else {
$this->setSafe($node, array());
}
2011-10-05 06:22:53 +02:00
} else {
$this->setSafe($node, array());
}
return $node;
}
protected function intersectSafe(array $a = null, array $b = null)
{
if (null === $a || null === $b) {
return array();
}
if (in_array('all', $a)) {
return $b;
}
if (in_array('all', $b)) {
return $a;
}
return array_intersect($a, $b);
}
public function getPriority()
{
return 0;
}
}
2018-05-10 12:24:53 +02:00
class_alias('Twig_NodeVisitor_SafeAnalysis', 'Twig\NodeVisitor\SafeAnalysisNodeVisitor', false);