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.
|
|
|
|
*/
|
2018-05-10 12:24:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @final
|
|
|
|
*/
|
2011-10-05 06:22:53 +02:00
|
|
|
class Twig_Extension_Escaper extends Twig_Extension
|
|
|
|
{
|
2013-08-01 21:20:12 +02:00
|
|
|
protected $defaultStrategy;
|
2011-10-05 06:22:53 +02:00
|
|
|
|
2018-05-10 12:24:53 +02:00
|
|
|
/**
|
|
|
|
* @param string|false|callable $defaultStrategy An escaping strategy
|
|
|
|
*
|
|
|
|
* @see setDefaultStrategy()
|
|
|
|
*/
|
2013-08-01 21:20:12 +02:00
|
|
|
public function __construct($defaultStrategy = 'html')
|
2011-10-05 06:22:53 +02:00
|
|
|
{
|
2013-08-01 21:20:12 +02:00
|
|
|
$this->setDefaultStrategy($defaultStrategy);
|
2011-10-05 06:22:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getTokenParsers()
|
|
|
|
{
|
|
|
|
return array(new Twig_TokenParser_AutoEscape());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getNodeVisitors()
|
|
|
|
{
|
|
|
|
return array(new Twig_NodeVisitor_Escaper());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFilters()
|
|
|
|
{
|
|
|
|
return array(
|
2013-08-01 21:20:12 +02:00
|
|
|
new Twig_SimpleFilter('raw', 'twig_raw_filter', array('is_safe' => array('all'))),
|
2011-10-05 06:22:53 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-08-01 21:20:12 +02:00
|
|
|
/**
|
|
|
|
* Sets the default strategy to use when not defined by the user.
|
|
|
|
*
|
|
|
|
* The strategy can be a valid PHP callback that takes the template
|
2018-05-10 12:24:53 +02:00
|
|
|
* name as an argument and returns the strategy to use.
|
2013-08-01 21:20:12 +02:00
|
|
|
*
|
2018-05-10 12:24:53 +02:00
|
|
|
* @param string|false|callable $defaultStrategy An escaping strategy
|
2013-08-01 21:20:12 +02:00
|
|
|
*/
|
|
|
|
public function setDefaultStrategy($defaultStrategy)
|
|
|
|
{
|
|
|
|
// for BC
|
|
|
|
if (true === $defaultStrategy) {
|
2018-05-10 12:24:53 +02:00
|
|
|
@trigger_error('Using "true" as the default strategy is deprecated since version 1.21. Use "html" instead.', E_USER_DEPRECATED);
|
|
|
|
|
2013-08-01 21:20:12 +02:00
|
|
|
$defaultStrategy = 'html';
|
|
|
|
}
|
|
|
|
|
2018-05-10 12:24:53 +02:00
|
|
|
if ('filename' === $defaultStrategy) {
|
|
|
|
@trigger_error('Using "filename" as the default strategy is deprecated since version 1.27. Use "name" instead.', E_USER_DEPRECATED);
|
|
|
|
|
|
|
|
$defaultStrategy = 'name';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('name' === $defaultStrategy) {
|
|
|
|
$defaultStrategy = array('Twig_FileExtensionEscapingStrategy', 'guess');
|
|
|
|
}
|
|
|
|
|
2013-08-01 21:20:12 +02:00
|
|
|
$this->defaultStrategy = $defaultStrategy;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the default strategy to use when not defined by the user.
|
|
|
|
*
|
2018-05-10 12:24:53 +02:00
|
|
|
* @param string $name The template name
|
2013-08-01 21:20:12 +02:00
|
|
|
*
|
2018-05-10 12:24:53 +02:00
|
|
|
* @return string|false The default strategy to use for the template
|
2013-08-01 21:20:12 +02:00
|
|
|
*/
|
2018-05-10 12:24:53 +02:00
|
|
|
public function getDefaultStrategy($name)
|
2011-10-05 06:22:53 +02:00
|
|
|
{
|
2013-08-01 21:20:12 +02:00
|
|
|
// disable string callables to avoid calling a function named html or js,
|
|
|
|
// or any other upcoming escaping strategy
|
2018-05-10 12:24:53 +02:00
|
|
|
if (!is_string($this->defaultStrategy) && false !== $this->defaultStrategy) {
|
|
|
|
return call_user_func($this->defaultStrategy, $name);
|
2013-08-01 21:20:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->defaultStrategy;
|
2011-10-05 06:22:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return 'escaper';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Marks a variable as being safe.
|
|
|
|
*
|
|
|
|
* @param string $string A PHP variable
|
2018-05-10 12:24:53 +02:00
|
|
|
*
|
|
|
|
* @return string
|
2011-10-05 06:22:53 +02:00
|
|
|
*/
|
|
|
|
function twig_raw_filter($string)
|
|
|
|
{
|
|
|
|
return $string;
|
|
|
|
}
|
2018-05-10 12:24:53 +02:00
|
|
|
|
|
|
|
class_alias('Twig_Extension_Escaper', 'Twig\Extension\EscaperExtension', false);
|