0
|
1 |
<?php
|
|
2 |
/**
|
|
3 |
* Smarty plugin
|
|
4 |
*
|
|
5 |
* @package Smarty
|
|
6 |
* @subpackage PluginsModifierCompiler
|
|
7 |
*/
|
|
8 |
|
|
9 |
/**
|
|
10 |
* @ignore
|
|
11 |
*/
|
|
12 |
require_once( SMARTY_PLUGINS_DIR .'shared.literal_compiler_param.php' );
|
|
13 |
|
|
14 |
/**
|
|
15 |
* Smarty escape modifier plugin
|
|
16 |
*
|
|
17 |
* Type: modifier<br>
|
|
18 |
* Name: escape<br>
|
|
19 |
* Purpose: escape string for output
|
|
20 |
*
|
|
21 |
* @link http://www.smarty.net/docsv2/en/language.modifier.escape count_characters (Smarty online manual)
|
|
22 |
* @author Rodney Rehm
|
|
23 |
* @param array $params parameters
|
|
24 |
* @return string with compiled code
|
|
25 |
*/
|
|
26 |
function smarty_modifiercompiler_escape($params, $compiler)
|
|
27 |
{
|
|
28 |
static $_double_encode = null;
|
|
29 |
if ($_double_encode === null) {
|
|
30 |
$_double_encode = version_compare(PHP_VERSION, '5.2.3', '>=');
|
|
31 |
}
|
|
32 |
|
|
33 |
try {
|
|
34 |
$esc_type = smarty_literal_compiler_param($params, 1, 'html');
|
|
35 |
$char_set = smarty_literal_compiler_param($params, 2, Smarty::$_CHARSET);
|
|
36 |
$double_encode = smarty_literal_compiler_param($params, 3, true);
|
|
37 |
|
|
38 |
if (!$char_set) {
|
|
39 |
$char_set = Smarty::$_CHARSET;
|
|
40 |
}
|
|
41 |
|
|
42 |
switch ($esc_type) {
|
|
43 |
case 'html':
|
|
44 |
if ($_double_encode) {
|
|
45 |
return 'htmlspecialchars('
|
|
46 |
. $params[0] .', ENT_QUOTES, '
|
|
47 |
. var_export($char_set, true) . ', '
|
|
48 |
. var_export($double_encode, true) . ')';
|
|
49 |
} else if ($double_encode) {
|
|
50 |
return 'htmlspecialchars('
|
|
51 |
. $params[0] .', ENT_QUOTES, '
|
|
52 |
. var_export($char_set, true) . ')';
|
|
53 |
} else {
|
|
54 |
// fall back to modifier.escape.php
|
|
55 |
}
|
|
56 |
|
|
57 |
case 'htmlall':
|
|
58 |
if (Smarty::$_MBSTRING) {
|
|
59 |
if ($_double_encode) {
|
|
60 |
// php >=5.2.3 - go native
|
|
61 |
return 'mb_convert_encoding(htmlspecialchars('
|
|
62 |
. $params[0] .', ENT_QUOTES, '
|
|
63 |
. var_export($char_set, true) . ', '
|
|
64 |
. var_export($double_encode, true)
|
|
65 |
. '), "HTML-ENTITIES", '
|
|
66 |
. var_export($char_set, true) . ')';
|
|
67 |
} else if ($double_encode) {
|
|
68 |
// php <5.2.3 - only handle double encoding
|
|
69 |
return 'mb_convert_encoding(htmlspecialchars('
|
|
70 |
. $params[0] .', ENT_QUOTES, '
|
|
71 |
. var_export($char_set, true)
|
|
72 |
. '), "HTML-ENTITIES", '
|
|
73 |
. var_export($char_set, true) . ')';
|
|
74 |
} else {
|
|
75 |
// fall back to modifier.escape.php
|
|
76 |
}
|
|
77 |
}
|
|
78 |
|
|
79 |
// no MBString fallback
|
|
80 |
if ($_double_encode) {
|
|
81 |
// php >=5.2.3 - go native
|
|
82 |
return 'htmlentities('
|
|
83 |
. $params[0] .', ENT_QUOTES, '
|
|
84 |
. var_export($char_set, true) . ', '
|
|
85 |
. var_export($double_encode, true) . ')';
|
|
86 |
} else if ($double_encode) {
|
|
87 |
// php <5.2.3 - only handle double encoding
|
|
88 |
return 'htmlentities('
|
|
89 |
. $params[0] .', ENT_QUOTES, '
|
|
90 |
. var_export($char_set, true) . ')';
|
|
91 |
} else {
|
|
92 |
// fall back to modifier.escape.php
|
|
93 |
}
|
|
94 |
|
|
95 |
case 'url':
|
|
96 |
return 'rawurlencode(' . $params[0] . ')';
|
|
97 |
|
|
98 |
case 'urlpathinfo':
|
|
99 |
return 'str_replace("%2F", "/", rawurlencode(' . $params[0] . '))';
|
|
100 |
|
|
101 |
case 'quotes':
|
|
102 |
// escape unescaped single quotes
|
|
103 |
return 'preg_replace("%(?<!\\\\\\\\)\'%", "\\\'",' . $params[0] . ')';
|
|
104 |
|
|
105 |
case 'javascript':
|
|
106 |
// escape quotes and backslashes, newlines, etc.
|
|
107 |
return 'strtr(' . $params[0] . ', array("\\\\" => "\\\\\\\\", "\'" => "\\\\\'", "\"" => "\\\\\"", "\\r" => "\\\\r", "\\n" => "\\\n", "</" => "<\/" ))';
|
|
108 |
|
|
109 |
}
|
|
110 |
} catch(SmartyException $e) {
|
|
111 |
// pass through to regular plugin fallback
|
|
112 |
}
|
|
113 |
|
|
114 |
// could not optimize |escape call, so fallback to regular plugin
|
|
115 |
if ($compiler->tag_nocache | $compiler->nocache) {
|
|
116 |
$compiler->template->required_plugins['nocache']['escape']['modifier']['file'] = SMARTY_PLUGINS_DIR .'modifier.escape.php';
|
|
117 |
$compiler->template->required_plugins['nocache']['escape']['modifier']['function'] = 'smarty_modifier_escape';
|
|
118 |
} else {
|
|
119 |
$compiler->template->required_plugins['compiled']['escape']['modifier']['file'] = SMARTY_PLUGINS_DIR .'modifier.escape.php';
|
|
120 |
$compiler->template->required_plugins['compiled']['escape']['modifier']['function'] = 'smarty_modifier_escape';
|
|
121 |
}
|
|
122 |
return 'smarty_modifier_escape(' . join( ', ', $params ) . ')';
|
|
123 |
}
|
|
124 |
|
|
125 |
?> |