leftypol/inc/lib/Twig/Extension/Escaper.php

113 lines
2.9 KiB
PHP
Raw Normal View History

2011-10-05 04:22:53 +00:00
<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
2011-10-05 04:22:53 +00:00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @final
*/
2011-10-05 04:22:53 +00:00
class Twig_Extension_Escaper extends Twig_Extension
{
2013-08-01 19:20:12 +00:00
protected $defaultStrategy;
2011-10-05 04:22:53 +00:00
/**
* @param string|false|callable $defaultStrategy An escaping strategy
*
* @see setDefaultStrategy()
*/
2013-08-01 19:20:12 +00:00
public function __construct($defaultStrategy = 'html')
2011-10-05 04:22:53 +00:00
{
2013-08-01 19:20:12 +00:00
$this->setDefaultStrategy($defaultStrategy);
2011-10-05 04:22:53 +00: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 19:20:12 +00:00
new Twig_SimpleFilter('raw', 'twig_raw_filter', array('is_safe' => array('all'))),
2011-10-05 04:22:53 +00:00
);
}
2013-08-01 19:20:12 +00: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
* name as an argument and returns the strategy to use.
2013-08-01 19:20:12 +00:00
*
* @param string|false|callable $defaultStrategy An escaping strategy
2013-08-01 19:20:12 +00:00
*/
public function setDefaultStrategy($defaultStrategy)
{
// for BC
if (true === $defaultStrategy) {
@trigger_error('Using "true" as the default strategy is deprecated since version 1.21. Use "html" instead.', E_USER_DEPRECATED);
2013-08-01 19:20:12 +00:00
$defaultStrategy = 'html';
}
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 19:20:12 +00:00
$this->defaultStrategy = $defaultStrategy;
}
/**
* Gets the default strategy to use when not defined by the user.
*
* @param string $name The template name
2013-08-01 19:20:12 +00:00
*
* @return string|false The default strategy to use for the template
2013-08-01 19:20:12 +00:00
*/
public function getDefaultStrategy($name)
2011-10-05 04:22:53 +00:00
{
2013-08-01 19:20:12 +00:00
// disable string callables to avoid calling a function named html or js,
// or any other upcoming escaping strategy
if (!is_string($this->defaultStrategy) && false !== $this->defaultStrategy) {
return call_user_func($this->defaultStrategy, $name);
2013-08-01 19:20:12 +00:00
}
return $this->defaultStrategy;
2011-10-05 04:22:53 +00:00
}
public function getName()
{
return 'escaper';
}
}
/**
* Marks a variable as being safe.
*
* @param string $string A PHP variable
*
* @return string
2011-10-05 04:22:53 +00:00
*/
function twig_raw_filter($string)
{
return $string;
}
class_alias('Twig_Extension_Escaper', 'Twig\Extension\EscaperExtension', false);