6 * @subpackage PluginsModifier
10 * Smarty escape modifier plugin
13 * Purpose: escape string for output
15 * @link http://www.smarty.net/docs/en/language.modifier.escape
16 * @author Monte Ohrt <monte at ohrt dot com>
18 * @param string $string input string
19 * @param string $esc_type escape type
20 * @param string $char_set character set, used for htmlspecialchars() or htmlentities()
21 * @param boolean $double_encode encode already encoded entitites again, used for htmlspecialchars() or htmlentities()
23 * @return string escaped input string
25 function smarty_modifier_escape($string, $esc_type = 'html', $char_set = null, $double_encode = true)
27 static $_double_encode = null;
28 if ($_double_encode === null) {
29 $_double_encode = version_compare(PHP_VERSION, '5.2.3', '>=');
33 $char_set = Smarty::$_CHARSET;
38 if ($_double_encode) {
39 // php >=5.3.2 - go native
40 return htmlspecialchars($string, ENT_QUOTES, $char_set, $double_encode);
43 // php <5.2.3 - only handle double encoding
44 return htmlspecialchars($string, ENT_QUOTES, $char_set);
46 // php <5.2.3 - prevent double encoding
47 $string = preg_replace('!&(#?\w+);!', '%%%SMARTY_START%%%\\1%%%SMARTY_END%%%', $string);
48 $string = htmlspecialchars($string, ENT_QUOTES, $char_set);
49 $string = str_replace(array('%%%SMARTY_START%%%', '%%%SMARTY_END%%%'), array('&', ';'), $string);
56 if (Smarty::$_MBSTRING) {
57 // mb_convert_encoding ignores htmlspecialchars()
58 if ($_double_encode) {
59 // php >=5.3.2 - go native
60 $string = htmlspecialchars($string, ENT_QUOTES, $char_set, $double_encode);
63 // php <5.2.3 - only handle double encoding
64 $string = htmlspecialchars($string, ENT_QUOTES, $char_set);
66 // php <5.2.3 - prevent double encoding
67 $string = preg_replace('!&(#?\w+);!', '%%%SMARTY_START%%%\\1%%%SMARTY_END%%%', $string);
68 $string = htmlspecialchars($string, ENT_QUOTES, $char_set);
70 str_replace(array('%%%SMARTY_START%%%', '%%%SMARTY_END%%%'), array('&', ';'), $string);
76 // htmlentities() won't convert everything, so use mb_convert_encoding
77 return mb_convert_encoding($string, 'HTML-ENTITIES', $char_set);
80 // no MBString fallback
81 if ($_double_encode) {
82 return htmlentities($string, ENT_QUOTES, $char_set, $double_encode);
85 return htmlentities($string, ENT_QUOTES, $char_set);
87 $string = preg_replace('!&(#?\w+);!', '%%%SMARTY_START%%%\\1%%%SMARTY_END%%%', $string);
88 $string = htmlentities($string, ENT_QUOTES, $char_set);
89 $string = str_replace(array('%%%SMARTY_START%%%', '%%%SMARTY_END%%%'), array('&', ';'), $string);
96 return rawurlencode($string);
99 return str_replace('%2F', '/', rawurlencode($string));
102 // escape unescaped single quotes
103 return preg_replace("%(?<!\\\\)'%", "\\'", $string);
106 // escape every byte into hex
107 // Note that the UTF-8 encoded character รค will be represented as %c3%a4
109 $_length = strlen($string);
110 for ($x = 0; $x < $_length; $x ++) {
111 $return .= '%' . bin2hex($string[ $x ]);
118 if (Smarty::$_MBSTRING) {
119 if (!is_callable('smarty_mb_to_unicode')) {
120 require_once(SMARTY_PLUGINS_DIR . 'shared.mb_unicode.php');
123 foreach (smarty_mb_to_unicode($string, Smarty::$_CHARSET) as $unicode) {
124 $return .= '&#x' . strtoupper(dechex($unicode)) . ';';
129 // no MBString fallback
130 $_length = strlen($string);
131 for ($x = 0; $x < $_length; $x ++) {
132 $return .= '&#x' . bin2hex($string[ $x ]) . ';';
139 if (Smarty::$_MBSTRING) {
140 if (!is_callable('smarty_mb_to_unicode')) {
141 require_once(SMARTY_PLUGINS_DIR . 'shared.mb_unicode.php');
144 foreach (smarty_mb_to_unicode($string, Smarty::$_CHARSET) as $unicode) {
145 $return .= '&#' . $unicode . ';';
150 // no MBString fallback
151 $_length = strlen($string);
152 for ($x = 0; $x < $_length; $x ++) {
153 $return .= '&#' . ord($string[ $x ]) . ';';
159 // escape quotes and backslashes, newlines, etc.
160 return strtr($string, array('\\' => '\\\\', "'" => "\\'", '"' => '\\"', "\r" => '\\r', "\n" => '\\n',
164 if (Smarty::$_MBSTRING) {
165 if (!is_callable('smarty_mb_str_replace')) {
166 require_once(SMARTY_PLUGINS_DIR . 'shared.mb_str_replace.php');
168 return smarty_mb_str_replace(array('@', '.'), array(' [AT] ', ' [DOT] '), $string);
170 // no MBString fallback
171 return str_replace(array('@', '.'), array(' [AT] ', ' [DOT] '), $string);
174 // escape non-standard chars, such as ms document quotes
176 if (Smarty::$_MBSTRING) {
177 if (!is_callable('smarty_mb_to_unicode')) {
178 require_once(SMARTY_PLUGINS_DIR . 'shared.mb_unicode.php');
180 foreach (smarty_mb_to_unicode($string, Smarty::$_CHARSET) as $unicode) {
181 if ($unicode >= 126) {
182 $return .= '&#' . $unicode . ';';
184 $return .= chr($unicode);
191 $_length = strlen($string);
192 for ($_i = 0; $_i < $_length; $_i ++) {
193 $_ord = ord(substr($string, $_i, 1));
194 // non-standard char, escape it
196 $return .= '&#' . $_ord . ';';
198 $return .= substr($string, $_i, 1);