1
|
<?php
|
2
|
|
3
|
/*
|
4
|
* This file is part of Twig.
|
5
|
*
|
6
|
* (c) 2009 Fabien Potencier
|
7
|
*
|
8
|
* For the full copyright and license information, please view the LICENSE
|
9
|
* file that was distributed with this source code.
|
10
|
*/
|
11
|
class Twig_Extension_Escaper extends Twig_Extension
|
12
|
{
|
13
|
protected $defaultStrategy;
|
14
|
|
15
|
/**
|
16
|
* Constructor.
|
17
|
*
|
18
|
* @param string|false|callable $defaultStrategy An escaping strategy
|
19
|
*
|
20
|
* @see setDefaultStrategy()
|
21
|
*/
|
22
|
public function __construct($defaultStrategy = 'html')
|
23
|
{
|
24
|
$this->setDefaultStrategy($defaultStrategy);
|
25
|
}
|
26
|
|
27
|
public function getTokenParsers()
|
28
|
{
|
29
|
return array(new Twig_TokenParser_AutoEscape());
|
30
|
}
|
31
|
|
32
|
public function getNodeVisitors()
|
33
|
{
|
34
|
return array(new Twig_NodeVisitor_Escaper());
|
35
|
}
|
36
|
|
37
|
public function getFilters()
|
38
|
{
|
39
|
return array(
|
40
|
new Twig_SimpleFilter('raw', 'twig_raw_filter', array('is_safe' => array('all'))),
|
41
|
);
|
42
|
}
|
43
|
|
44
|
/**
|
45
|
* Sets the default strategy to use when not defined by the user.
|
46
|
*
|
47
|
* The strategy can be a valid PHP callback that takes the template
|
48
|
* "filename" as an argument and returns the strategy to use.
|
49
|
*
|
50
|
* @param string|false|callable $defaultStrategy An escaping strategy
|
51
|
*/
|
52
|
public function setDefaultStrategy($defaultStrategy)
|
53
|
{
|
54
|
// for BC
|
55
|
if (true === $defaultStrategy) {
|
56
|
@trigger_error('Using "true" as the default strategy is deprecated since version 1.21. Use "html" instead.', E_USER_DEPRECATED);
|
57
|
|
58
|
$defaultStrategy = 'html';
|
59
|
}
|
60
|
|
61
|
if ('filename' === $defaultStrategy) {
|
62
|
$defaultStrategy = array('Twig_FileExtensionEscapingStrategy', 'guess');
|
63
|
}
|
64
|
|
65
|
$this->defaultStrategy = $defaultStrategy;
|
66
|
}
|
67
|
|
68
|
/**
|
69
|
* Gets the default strategy to use when not defined by the user.
|
70
|
*
|
71
|
* @param string $filename The template "filename"
|
72
|
*
|
73
|
* @return string|false The default strategy to use for the template
|
74
|
*/
|
75
|
public function getDefaultStrategy($filename)
|
76
|
{
|
77
|
// disable string callables to avoid calling a function named html or js,
|
78
|
// or any other upcoming escaping strategy
|
79
|
if (!is_string($this->defaultStrategy) && false !== $this->defaultStrategy) {
|
80
|
return call_user_func($this->defaultStrategy, $filename);
|
81
|
}
|
82
|
|
83
|
return $this->defaultStrategy;
|
84
|
}
|
85
|
|
86
|
public function getName()
|
87
|
{
|
88
|
return 'escaper';
|
89
|
}
|
90
|
}
|
91
|
|
92
|
/**
|
93
|
* Marks a variable as being safe.
|
94
|
*
|
95
|
* @param string $string A PHP variable
|
96
|
*
|
97
|
* @return string
|
98
|
*/
|
99
|
function twig_raw_filter($string)
|
100
|
{
|
101
|
return $string;
|
102
|
}
|