6 * @subpackage PluginsModifierCompiler
10 * Smarty escape modifier plugin
13 * Purpose: escape string for output
15 * @link http://www.smarty.net/docsv2/en/language.modifier.escape count_characters (Smarty online manual)
18 * @param array $params parameters
21 * @return string with compiled code
23 function smarty_modifiercompiler_escape($params, $compiler)
25 static $_double_encode = null;
26 if (!is_callable('smarty_literal_compiler_param')) {
27 require_once(SMARTY_PLUGINS_DIR . 'shared.literal_compiler_param.php');
29 if ($_double_encode === null) {
30 $_double_encode = version_compare(PHP_VERSION, '5.2.3', '>=');
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);
39 $char_set = Smarty::$_CHARSET;
44 if ($_double_encode) {
45 return 'htmlspecialchars(' . $params[ 0 ] . ', ENT_QUOTES, ' . var_export($char_set, true) . ', ' .
46 var_export($double_encode, true) . ')';
47 } elseif ($double_encode) {
48 return 'htmlspecialchars(' . $params[ 0 ] . ', ENT_QUOTES, ' . var_export($char_set, true) . ')';
50 // fall back to modifier.escape.php
54 if (Smarty::$_MBSTRING) {
55 if ($_double_encode) {
56 // php >=5.2.3 - go native
57 return 'mb_convert_encoding(htmlspecialchars(' . $params[ 0 ] . ', ENT_QUOTES, ' .
58 var_export($char_set, true) . ', ' . var_export($double_encode, true) .
59 '), "HTML-ENTITIES", ' . var_export($char_set, true) . ')';
60 } elseif ($double_encode) {
61 // php <5.2.3 - only handle double encoding
62 return 'mb_convert_encoding(htmlspecialchars(' . $params[ 0 ] . ', ENT_QUOTES, ' .
63 var_export($char_set, true) . '), "HTML-ENTITIES", ' . var_export($char_set, true) . ')';
65 // fall back to modifier.escape.php
69 // no MBString fallback
70 if ($_double_encode) {
71 // php >=5.2.3 - go native
72 return 'htmlentities(' . $params[ 0 ] . ', ENT_QUOTES, ' . var_export($char_set, true) . ', ' .
73 var_export($double_encode, true) . ')';
74 } elseif ($double_encode) {
75 // php <5.2.3 - only handle double encoding
76 return 'htmlentities(' . $params[ 0 ] . ', ENT_QUOTES, ' . var_export($char_set, true) . ')';
78 // fall back to modifier.escape.php
82 return 'rawurlencode(' . $params[ 0 ] . ')';
85 return 'str_replace("%2F", "/", rawurlencode(' . $params[ 0 ] . '))';
88 // escape unescaped single quotes
89 return 'preg_replace("%(?<!\\\\\\\\)\'%", "\\\'",' . $params[ 0 ] . ')';
92 // escape quotes and backslashes, newlines, etc.
93 return 'strtr(' . $params[ 0 ] .
94 ', array("\\\\" => "\\\\\\\\", "\'" => "\\\\\'", "\"" => "\\\\\"", "\\r" => "\\\\r", "\\n" => "\\\n", "</" => "<\/" ))';
97 catch (SmartyException $e) {
98 // pass through to regular plugin fallback
101 // could not optimize |escape call, so fallback to regular plugin
102 if ($compiler->template->caching && ($compiler->tag_nocache | $compiler->nocache)) {
103 $compiler->parent_compiler->template->compiled->required_plugins[ 'nocache' ][ 'escape' ][ 'modifier' ][ 'file' ] =
104 SMARTY_PLUGINS_DIR . 'modifier.escape.php';
105 $compiler->parent_compiler->template->compiled->required_plugins[ 'nocache' ][ 'escape' ][ 'modifier' ][ 'function' ] =
106 'smarty_modifier_escape';
108 $compiler->parent_compiler->template->compiled->required_plugins[ 'compiled' ][ 'escape' ][ 'modifier' ][ 'file' ] =
109 SMARTY_PLUGINS_DIR . 'modifier.escape.php';
110 $compiler->parent_compiler->template->compiled->required_plugins[ 'compiled' ][ 'escape' ][ 'modifier' ][ 'function' ] =
111 'smarty_modifier_escape';
114 return 'smarty_modifier_escape(' . join(', ', $params) . ')';