1: <?php
2: 3: 4: 5: 6: 7:
8:
9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24:
25: function smarty_modifier_escape($string, $esc_type = 'html', $char_set = null, $double_encode = true)
26: {
27: static $_double_encode = null;
28: if ($_double_encode === null) {
29: $_double_encode = version_compare(PHP_VERSION, '5.2.3', '>=');
30: }
31:
32: if (!$char_set) {
33: $char_set = Smarty::$_CHARSET;
34: }
35:
36: switch ($esc_type) {
37: case 'html':
38: if ($_double_encode) {
39:
40: return htmlspecialchars($string, ENT_QUOTES, $char_set, $double_encode);
41: } else {
42: if ($double_encode) {
43:
44: return htmlspecialchars($string, ENT_QUOTES, $char_set);
45: } else {
46:
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);
50:
51: return $string;
52: }
53: }
54:
55: case 'htmlall':
56: if (Smarty::$_MBSTRING) {
57:
58: if ($_double_encode) {
59:
60: $string = htmlspecialchars($string, ENT_QUOTES, $char_set, $double_encode);
61: } else {
62: if ($double_encode) {
63:
64: $string = htmlspecialchars($string, ENT_QUOTES, $char_set);
65: } else {
66:
67: $string = preg_replace('!&(#?\w+);!', '%%%SMARTY_START%%%\\1%%%SMARTY_END%%%', $string);
68: $string = htmlspecialchars($string, ENT_QUOTES, $char_set);
69: $string = str_replace(array('%%%SMARTY_START%%%', '%%%SMARTY_END%%%'), array('&', ';'), $string);
70:
71: return $string;
72: }
73: }
74:
75:
76: return mb_convert_encoding($string, 'HTML-ENTITIES', $char_set);
77: }
78:
79:
80: if ($_double_encode) {
81: return htmlentities($string, ENT_QUOTES, $char_set, $double_encode);
82: } else {
83: if ($double_encode) {
84: return htmlentities($string, ENT_QUOTES, $char_set);
85: } else {
86: $string = preg_replace('!&(#?\w+);!', '%%%SMARTY_START%%%\\1%%%SMARTY_END%%%', $string);
87: $string = htmlentities($string, ENT_QUOTES, $char_set);
88: $string = str_replace(array('%%%SMARTY_START%%%', '%%%SMARTY_END%%%'), array('&', ';'), $string);
89:
90: return $string;
91: }
92: }
93:
94: case 'url':
95: return rawurlencode($string);
96:
97: case 'urlpathinfo':
98: return str_replace('%2F', '/', rawurlencode($string));
99:
100: case 'quotes':
101:
102: return preg_replace("%(?<!\\\\)'%", "\\'", $string);
103:
104: case 'hex':
105:
106:
107: $return = '';
108: $_length = strlen($string);
109: for ($x = 0; $x < $_length; $x ++) {
110: $return .= '%' . bin2hex($string[$x]);
111: }
112:
113: return $return;
114:
115: case 'hexentity':
116: $return = '';
117: if (Smarty::$_MBSTRING) {
118: require_once(SMARTY_PLUGINS_DIR . 'shared.mb_unicode.php');
119: $return = '';
120: foreach (smarty_mb_to_unicode($string, Smarty::$_CHARSET) as $unicode) {
121: $return .= '&#x' . strtoupper(dechex($unicode)) . ';';
122: }
123:
124: return $return;
125: }
126:
127: $_length = strlen($string);
128: for ($x = 0; $x < $_length; $x ++) {
129: $return .= '&#x' . bin2hex($string[$x]) . ';';
130: }
131:
132: return $return;
133:
134: case 'decentity':
135: $return = '';
136: if (Smarty::$_MBSTRING) {
137: require_once(SMARTY_PLUGINS_DIR . 'shared.mb_unicode.php');
138: $return = '';
139: foreach (smarty_mb_to_unicode($string, Smarty::$_CHARSET) as $unicode) {
140: $return .= '&#' . $unicode . ';';
141: }
142:
143: return $return;
144: }
145:
146: $_length = strlen($string);
147: for ($x = 0; $x < $_length; $x ++) {
148: $return .= '&#' . ord($string[$x]) . ';';
149: }
150:
151: return $return;
152:
153: case 'javascript':
154:
155: return strtr($string, array('\\' => '\\\\', "'" => "\\'", '"' => '\\"', "\r" => '\\r', "\n" => '\\n', '</' => '<\/'));
156:
157: case 'mail':
158: if (Smarty::$_MBSTRING) {
159: require_once(SMARTY_PLUGINS_DIR . 'shared.mb_str_replace.php');
160:
161: return smarty_mb_str_replace(array('@', '.'), array(' [AT] ', ' [DOT] '), $string);
162: }
163:
164: return str_replace(array('@', '.'), array(' [AT] ', ' [DOT] '), $string);
165:
166: case 'nonstd':
167:
168: $return = '';
169: if (Smarty::$_MBSTRING) {
170: require_once(SMARTY_PLUGINS_DIR . 'shared.mb_unicode.php');
171: foreach (smarty_mb_to_unicode($string, Smarty::$_CHARSET) as $unicode) {
172: if ($unicode >= 126) {
173: $return .= '&#' . $unicode . ';';
174: } else {
175: $return .= chr($unicode);
176: }
177: }
178:
179: return $return;
180: }
181:
182: $_length = strlen($string);
183: for ($_i = 0; $_i < $_length; $_i ++) {
184: $_ord = ord(substr($string, $_i, 1));
185:
186: if ($_ord >= 126) {
187: $return .= '&#' . $_ord . ';';
188: } else {
189: $return .= substr($string, $_i, 1);
190: }
191: }
192:
193: return $return;
194:
195: default:
196: return $string;
197: }
198: }
199: