forked from leftypol/leftypol
inc/contrib -> inc/lib
This commit is contained in:
parent
69c9a9dd45
commit
874b8cbf00
186 changed files with 6 additions and 6 deletions
161
inc/lib/Twig/NodeVisitor/Escaper.php
Normal file
161
inc/lib/Twig/NodeVisitor/Escaper.php
Normal file
|
@ -0,0 +1,161 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Twig.
|
||||
*
|
||||
* (c) 2009 Fabien Potencier
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* @package twig
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
class Twig_NodeVisitor_Escaper implements Twig_NodeVisitorInterface
|
||||
{
|
||||
protected $statusStack = array();
|
||||
protected $blocks = array();
|
||||
|
||||
protected $safeAnalysis;
|
||||
protected $traverser;
|
||||
|
||||
function __construct()
|
||||
{
|
||||
$this->safeAnalysis = new Twig_NodeVisitor_SafeAnalysis();
|
||||
}
|
||||
|
||||
/**
|
||||
* Called before child nodes are visited.
|
||||
*
|
||||
* @param Twig_NodeInterface $node The node to visit
|
||||
* @param Twig_Environment $env The Twig environment instance
|
||||
*
|
||||
* @param Twig_NodeInterface The modified node
|
||||
*/
|
||||
public function enterNode(Twig_NodeInterface $node, Twig_Environment $env)
|
||||
{
|
||||
if ($node instanceof Twig_Node_AutoEscape) {
|
||||
$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);
|
||||
}
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called after child nodes are visited.
|
||||
*
|
||||
* @param Twig_NodeInterface $node The node to visit
|
||||
* @param Twig_Environment $env The Twig environment instance
|
||||
*
|
||||
* @param Twig_NodeInterface The modified node
|
||||
*/
|
||||
public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env)
|
||||
{
|
||||
if ($node instanceof Twig_Node_Expression_Filter) {
|
||||
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),
|
||||
$node->getLine()
|
||||
);
|
||||
}
|
||||
|
||||
protected function preEscapeFilterNode(Twig_Node_Expression_Filter $filter, Twig_Environment $env)
|
||||
{
|
||||
$name = $filter->getNode('filter')->getAttribute('value');
|
||||
|
||||
if (false !== $f = $env->getFilter($name)) {
|
||||
$type = $f->getPreEscape();
|
||||
if (null === $type) {
|
||||
return $filter;
|
||||
}
|
||||
|
||||
$node = $filter->getNode('node');
|
||||
if ($this->isSafeFor($type, $node, $env)) {
|
||||
return $filter;
|
||||
}
|
||||
|
||||
$filter->setNode('node', $this->getEscaperFilter($type, $node));
|
||||
|
||||
return $filter;
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
$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];
|
||||
}
|
||||
|
||||
if ($env->hasExtension('escaper') && $env->getExtension('escaper')->isGlobal()) {
|
||||
return 'html';
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected function getEscaperFilter($type, Twig_NodeInterface $node)
|
||||
{
|
||||
$line = $node->getLine();
|
||||
$name = new Twig_Node_Expression_Constant('escape', $line);
|
||||
$args = new Twig_Node(array(new Twig_Node_Expression_Constant((string) $type, $line)));
|
||||
return new Twig_Node_Expression_Filter($node, $name, $args, $line);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getPriority()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
190
inc/lib/Twig/NodeVisitor/Optimizer.php
Normal file
190
inc/lib/Twig/NodeVisitor/Optimizer.php
Normal file
|
@ -0,0 +1,190 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Twig.
|
||||
*
|
||||
* (c) 2010 Fabien Potencier
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Twig_NodeVisitor_Optimizer tries to optimizes the AST.
|
||||
*
|
||||
* This visitor is always the last registered one.
|
||||
*
|
||||
* You can configure which optimizations you want to activate via the
|
||||
* optimizer mode.
|
||||
*
|
||||
* @package twig
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
class Twig_NodeVisitor_Optimizer implements Twig_NodeVisitorInterface
|
||||
{
|
||||
const OPTIMIZE_ALL = -1;
|
||||
const OPTIMIZE_NONE = 0;
|
||||
const OPTIMIZE_FOR = 2;
|
||||
const OPTIMIZE_RAW_FILTER = 4;
|
||||
|
||||
protected $loops = array();
|
||||
protected $optimizers;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param integer $optimizers The optimizer mode
|
||||
*/
|
||||
public function __construct($optimizers = -1)
|
||||
{
|
||||
if (!is_int($optimizers) || $optimizers > 2) {
|
||||
throw new InvalidArgumentException(sprintf('Optimizer mode "%s" is not valid.', $optimizers));
|
||||
}
|
||||
|
||||
$this->optimizers = $optimizers;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function enterNode(Twig_NodeInterface $node, Twig_Environment $env)
|
||||
{
|
||||
if (self::OPTIMIZE_FOR === (self::OPTIMIZE_FOR & $this->optimizers)) {
|
||||
$this->enterOptimizeFor($node, $env);
|
||||
}
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env)
|
||||
{
|
||||
if (self::OPTIMIZE_FOR === (self::OPTIMIZE_FOR & $this->optimizers)) {
|
||||
$this->leaveOptimizeFor($node, $env);
|
||||
}
|
||||
|
||||
if (self::OPTIMIZE_RAW_FILTER === (self::OPTIMIZE_RAW_FILTER & $this->optimizers)) {
|
||||
$node = $this->optimizeRawFilter($node, $env);
|
||||
}
|
||||
|
||||
$node = $this->optimizeRenderBlock($node, $env);
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces "echo $this->renderBlock()" with "$this->displayBlock()".
|
||||
*
|
||||
* @param Twig_NodeInterface $node A Node
|
||||
* @param Twig_Environment $env The current Twig environment
|
||||
*/
|
||||
protected function optimizeRenderBlock($node, $env)
|
||||
{
|
||||
if ($node instanceof Twig_Node_Print && $node->getNode('expr') instanceof Twig_Node_Expression_BlockReference) {
|
||||
$node->getNode('expr')->setAttribute('output', true);
|
||||
|
||||
return $node->getNode('expr');
|
||||
}
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes "raw" filters.
|
||||
*
|
||||
* @param Twig_NodeInterface $node A Node
|
||||
* @param Twig_Environment $env The current Twig environment
|
||||
*/
|
||||
protected function optimizeRawFilter($node, $env)
|
||||
{
|
||||
if ($node instanceof Twig_Node_Expression_Filter && 'raw' == $node->getNode('filter')->getAttribute('value')) {
|
||||
return $node->getNode('node');
|
||||
}
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
/**
|
||||
* Optimizes "for" tag by removing the "loop" variable creation whenever possible.
|
||||
*
|
||||
* @param Twig_NodeInterface $node A Node
|
||||
* @param Twig_Environment $env The current Twig environment
|
||||
*/
|
||||
protected function enterOptimizeFor($node, $env)
|
||||
{
|
||||
if ($node instanceof Twig_Node_For) {
|
||||
// disable the loop variable by default
|
||||
$node->setAttribute('with_loop', false);
|
||||
array_unshift($this->loops, $node);
|
||||
} elseif (!$this->loops) {
|
||||
// we are outside a loop
|
||||
return;
|
||||
}
|
||||
|
||||
// when do we need to add the loop variable back?
|
||||
|
||||
// the loop variable is referenced for the current loop
|
||||
elseif ($node instanceof Twig_Node_Expression_Name && 'loop' === $node->getAttribute('name')) {
|
||||
$this->addLoopToCurrent();
|
||||
}
|
||||
|
||||
// block reference
|
||||
elseif ($node instanceof Twig_Node_BlockReference || $node instanceof Twig_Node_Expression_BlockReference) {
|
||||
$this->addLoopToCurrent();
|
||||
}
|
||||
|
||||
// include without the only attribute
|
||||
elseif ($node instanceof Twig_Node_Include && !$node->getAttribute('only')) {
|
||||
$this->addLoopToAll();
|
||||
}
|
||||
|
||||
// the loop variable is referenced via an attribute
|
||||
elseif ($node instanceof Twig_Node_Expression_GetAttr
|
||||
&& (!$node->getNode('attribute') instanceof Twig_Node_Expression_Constant
|
||||
|| 'parent' === $node->getNode('attribute')->getAttribute('value')
|
||||
)
|
||||
&& (true === $this->loops[0]->getAttribute('with_loop')
|
||||
|| ($node->getNode('node') instanceof Twig_Node_Expression_Name
|
||||
&& 'loop' === $node->getNode('node')->getAttribute('name')
|
||||
)
|
||||
)
|
||||
) {
|
||||
$this->addLoopToAll();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Optimizes "for" tag by removing the "loop" variable creation whenever possible.
|
||||
*
|
||||
* @param Twig_NodeInterface $node A Node
|
||||
* @param Twig_Environment $env The current Twig environment
|
||||
*/
|
||||
protected function leaveOptimizeFor($node, $env)
|
||||
{
|
||||
if ($node instanceof Twig_Node_For) {
|
||||
array_shift($this->loops);
|
||||
}
|
||||
}
|
||||
|
||||
protected function addLoopToCurrent()
|
||||
{
|
||||
$this->loops[0]->setAttribute('with_loop', true);
|
||||
}
|
||||
|
||||
protected function addLoopToAll()
|
||||
{
|
||||
foreach ($this->loops as $loop) {
|
||||
$loop->setAttribute('with_loop', true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getPriority()
|
||||
{
|
||||
return 255;
|
||||
}
|
||||
}
|
107
inc/lib/Twig/NodeVisitor/SafeAnalysis.php
Normal file
107
inc/lib/Twig/NodeVisitor/SafeAnalysis.php
Normal file
|
@ -0,0 +1,107 @@
|
|||
<?php
|
||||
|
||||
class Twig_NodeVisitor_SafeAnalysis implements Twig_NodeVisitorInterface
|
||||
{
|
||||
protected $data = array();
|
||||
|
||||
public function getSafe(Twig_NodeInterface $node)
|
||||
{
|
||||
$hash = spl_object_hash($node);
|
||||
if (isset($this->data[$hash])) {
|
||||
foreach($this->data[$hash] as $bucket) {
|
||||
if ($bucket['key'] === $node) {
|
||||
return $bucket['value'];
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
protected function setSafe(Twig_NodeInterface $node, array $safe)
|
||||
{
|
||||
$hash = spl_object_hash($node);
|
||||
if (isset($this->data[$hash])) {
|
||||
foreach($this->data[$hash] as &$bucket) {
|
||||
if ($bucket['key'] === $node) {
|
||||
$bucket['value'] = $safe;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->data[$hash][] = array(
|
||||
'key' => $node,
|
||||
'value' => $safe,
|
||||
);
|
||||
}
|
||||
|
||||
public function enterNode(Twig_NodeInterface $node, Twig_Environment $env)
|
||||
{
|
||||
return $node;
|
||||
}
|
||||
|
||||
public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env)
|
||||
{
|
||||
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)) {
|
||||
$this->setSafe($node, $filter->getSafe($args));
|
||||
} 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());
|
||||
}
|
||||
} 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getPriority()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
93
inc/lib/Twig/NodeVisitor/Sandbox.php
Normal file
93
inc/lib/Twig/NodeVisitor/Sandbox.php
Normal file
|
@ -0,0 +1,93 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Twig.
|
||||
*
|
||||
* (c) 2009 Fabien Potencier
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Twig_NodeVisitor_Sandbox implements sandboxing.
|
||||
*
|
||||
* @package twig
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
class Twig_NodeVisitor_Sandbox implements Twig_NodeVisitorInterface
|
||||
{
|
||||
protected $inAModule = false;
|
||||
protected $tags;
|
||||
protected $filters;
|
||||
protected $functions;
|
||||
|
||||
/**
|
||||
* Called before child nodes are visited.
|
||||
*
|
||||
* @param Twig_NodeInterface $node The node to visit
|
||||
* @param Twig_Environment $env The Twig environment instance
|
||||
*
|
||||
* @param Twig_NodeInterface The modified node
|
||||
*/
|
||||
public function enterNode(Twig_NodeInterface $node, Twig_Environment $env)
|
||||
{
|
||||
if ($node instanceof Twig_Node_Module) {
|
||||
$this->inAModule = true;
|
||||
$this->tags = array();
|
||||
$this->filters = array();
|
||||
$this->functions = array();
|
||||
|
||||
return $node;
|
||||
} elseif ($this->inAModule) {
|
||||
// look for tags
|
||||
if ($node->getNodeTag()) {
|
||||
$this->tags[] = $node->getNodeTag();
|
||||
}
|
||||
|
||||
// look for filters
|
||||
if ($node instanceof Twig_Node_Expression_Filter) {
|
||||
$this->filters[] = $node->getNode('filter')->getAttribute('value');
|
||||
}
|
||||
|
||||
// look for functions
|
||||
if ($node instanceof Twig_Node_Expression_Function) {
|
||||
$this->functions[] = $node->getAttribute('name');
|
||||
}
|
||||
|
||||
// wrap print to check __toString() calls
|
||||
if ($node instanceof Twig_Node_Print) {
|
||||
return new Twig_Node_SandboxedPrint($node->getNode('expr'), $node->getLine(), $node->getNodeTag());
|
||||
}
|
||||
}
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called after child nodes are visited.
|
||||
*
|
||||
* @param Twig_NodeInterface $node The node to visit
|
||||
* @param Twig_Environment $env The Twig environment instance
|
||||
*
|
||||
* @param Twig_NodeInterface The modified node
|
||||
*/
|
||||
public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env)
|
||||
{
|
||||
if ($node instanceof Twig_Node_Module) {
|
||||
$this->inAModule = false;
|
||||
|
||||
return new Twig_Node_SandboxedModule($node, array_unique($this->filters), array_unique($this->tags), array_unique($this->functions));
|
||||
}
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getPriority()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue