6 * @subpackage PluginsModifierCompiler
\r
12 require_once( SMARTY_PLUGINS_DIR .'shared.literal_compiler_param.php' );
\r
15 * Smarty escape modifier plugin
\r
17 * Type: modifier<br>
\r
19 * Purpose: escape string for output
\r
21 * @link http://www.smarty.net/docsv2/en/language.modifier.escape count_characters (Smarty online manual)
\r
22 * @author Rodney Rehm
\r
23 * @param array $params parameters
\r
24 * @return string with compiled code
\r
26 function smarty_modifiercompiler_escape($params, $compiler)
\r
28 static $_double_encode = null;
\r
29 if ($_double_encode === null) {
\r
30 $_double_encode = version_compare(PHP_VERSION, '5.2.3', '>=');
\r
34 $esc_type = smarty_literal_compiler_param($params, 1, 'html');
\r
35 $char_set = smarty_literal_compiler_param($params, 2, Smarty::$_CHARSET);
\r
36 $double_encode = smarty_literal_compiler_param($params, 3, true);
\r
39 $char_set = Smarty::$_CHARSET;
\r
42 switch ($esc_type) {
\r
44 if ($_double_encode) {
\r
45 return 'htmlspecialchars('
\r
46 . $params[0] .', ENT_QUOTES, '
\r
47 . var_export($char_set, true) . ', '
\r
48 . var_export($double_encode, true) . ')';
\r
49 } else if ($double_encode) {
\r
50 return 'htmlspecialchars('
\r
51 . $params[0] .', ENT_QUOTES, '
\r
52 . var_export($char_set, true) . ')';
\r
54 // fall back to modifier.escape.php
\r
58 if (Smarty::$_MBSTRING) {
\r
59 if ($_double_encode) {
\r
60 // php >=5.3.2 - go native
\r
61 return 'mb_convert_encoding(htmlspecialchars('
\r
62 . $params[0] .', ENT_QUOTES, '
\r
63 . var_export($char_set, true) . ', '
\r
64 . var_export($double_encode, true)
\r
65 . '), "HTML-ENTITIES", '
\r
66 . var_export($char_set, true) . ')';
\r
67 } else if ($double_encode) {
\r
68 // php <5.3.2 - only handle double encoding
\r
69 return 'mb_convert_encoding(htmlspecialchars('
\r
70 . $params[0] .', ENT_QUOTES, '
\r
71 . var_export($char_set, true)
\r
72 . '), "HTML-ENTITIES", '
\r
73 . var_export($char_set, true) . ')';
\r
75 // fall back to modifier.escape.php
\r
79 // no MBString fallback
\r
80 if ($_double_encode) {
\r
81 // php >=5.3.2 - go native
\r
82 return 'htmlentities('
\r
83 . $params[0] .', ENT_QUOTES, '
\r
84 . var_export($char_set, true) . ', '
\r
85 . var_export($double_encode, true) . ')';
\r
86 } else if ($double_encode) {
\r
87 // php <5.3.2 - only handle double encoding
\r
88 return 'htmlentities('
\r
89 . $params[0] .', ENT_QUOTES, '
\r
90 . var_export($char_set, true) . ')';
\r
92 // fall back to modifier.escape.php
\r
96 return 'rawurlencode(' . $params[0] . ')';
\r
99 return 'str_replace("%2F", "/", rawurlencode(' . $params[0] . '))';
\r
102 // escape unescaped single quotes
\r
103 return 'preg_replace("%(?<!\\\\\\\\)\'%", "\\\'",' . $params[0] . ')';
\r
106 // escape quotes and backslashes, newlines, etc.
\r
107 return 'strtr(' . $params[0] . ', array("\\\\" => "\\\\\\\\", "\'" => "\\\\\'", "\"" => "\\\\\"", "\\r" => "\\\\r", "\\n" => "\\\n", "</" => "<\/" ))';
\r
110 } catch(SmartyException $e) {
\r
111 // pass through to regular plugin fallback
\r
114 // could not optimize |escape call, so fallback to regular plugin
\r
115 if ($compiler->tag_nocache | $compiler->nocache) {
\r
116 $compiler->template->required_plugins['nocache']['escape']['modifier']['file'] = SMARTY_PLUGINS_DIR .'modifier.escape.php';
\r
117 $compiler->template->required_plugins['nocache']['escape']['modifier']['function'] = 'smarty_modifier_escape';
\r
119 $compiler->template->required_plugins['compiled']['escape']['modifier']['file'] = SMARTY_PLUGINS_DIR .'modifier.escape.php';
\r
120 $compiler->template->required_plugins['compiled']['escape']['modifier']['function'] = 'smarty_modifier_escape';
\r
122 return 'smarty_modifier_escape(' . join( ', ', $params ) . ')';
\r