]> git.mxchange.org Git - friendica.git/blob - vendor/smarty/smarty/libs/plugins/modifier.escape.php
Add Smarty to Composer
[friendica.git] / vendor / smarty / smarty / libs / plugins / modifier.escape.php
1 <?php
2 /**
3  * Smarty plugin
4  *
5  * @package    Smarty
6  * @subpackage PluginsModifier
7  */
8
9 /**
10  * Smarty escape modifier plugin
11  * Type:     modifier<br>
12  * Name:     escape<br>
13  * Purpose:  escape string for output
14  *
15  * @link   http://www.smarty.net/docs/en/language.modifier.escape
16  * @author Monte Ohrt <monte at ohrt dot com>
17  *
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()
22  *
23  * @return string escaped input string
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                 // php >=5.3.2 - go native
40                 return htmlspecialchars($string, ENT_QUOTES, $char_set, $double_encode);
41             } else {
42                 if ($double_encode) {
43                     // php <5.2.3 - only handle double encoding
44                     return htmlspecialchars($string, ENT_QUOTES, $char_set);
45                 } else {
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);
50
51                     return $string;
52                 }
53             }
54
55         case 'htmlall':
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);
61                 } else {
62                     if ($double_encode) {
63                         // php <5.2.3 - only handle double encoding
64                         $string = htmlspecialchars($string, ENT_QUOTES, $char_set);
65                     } else {
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);
69                         $string =
70                             str_replace(array('%%%SMARTY_START%%%', '%%%SMARTY_END%%%'), array('&', ';'), $string);
71
72                         return $string;
73                     }
74                 }
75
76                 // htmlentities() won't convert everything, so use mb_convert_encoding
77                 return mb_convert_encoding($string, 'HTML-ENTITIES', $char_set);
78             }
79
80             // no MBString fallback
81             if ($_double_encode) {
82                 return htmlentities($string, ENT_QUOTES, $char_set, $double_encode);
83             } else {
84                 if ($double_encode) {
85                     return htmlentities($string, ENT_QUOTES, $char_set);
86                 } else {
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);
90
91                     return $string;
92                 }
93             }
94
95         case 'url':
96             return rawurlencode($string);
97
98         case 'urlpathinfo':
99             return str_replace('%2F', '/', rawurlencode($string));
100
101         case 'quotes':
102             // escape unescaped single quotes
103             return preg_replace("%(?<!\\\\)'%", "\\'", $string);
104
105         case 'hex':
106             // escape every byte into hex
107             // Note that the UTF-8 encoded character รค will be represented as %c3%a4
108             $return = '';
109             $_length = strlen($string);
110             for ($x = 0; $x < $_length; $x ++) {
111                 $return .= '%' . bin2hex($string[ $x ]);
112             }
113
114             return $return;
115
116         case 'hexentity':
117             $return = '';
118             if (Smarty::$_MBSTRING) {
119                 if (!is_callable('smarty_mb_to_unicode')) {
120                     require_once(SMARTY_PLUGINS_DIR . 'shared.mb_unicode.php');
121                 }
122                 $return = '';
123                 foreach (smarty_mb_to_unicode($string, Smarty::$_CHARSET) as $unicode) {
124                     $return .= '&#x' . strtoupper(dechex($unicode)) . ';';
125                 }
126
127                 return $return;
128             }
129             // no MBString fallback
130             $_length = strlen($string);
131             for ($x = 0; $x < $_length; $x ++) {
132                 $return .= '&#x' . bin2hex($string[ $x ]) . ';';
133             }
134
135             return $return;
136
137         case 'decentity':
138             $return = '';
139             if (Smarty::$_MBSTRING) {
140                 if (!is_callable('smarty_mb_to_unicode')) {
141                     require_once(SMARTY_PLUGINS_DIR . 'shared.mb_unicode.php');
142                 }
143                 $return = '';
144                 foreach (smarty_mb_to_unicode($string, Smarty::$_CHARSET) as $unicode) {
145                     $return .= '&#' . $unicode . ';';
146                 }
147
148                 return $return;
149             }
150             // no MBString fallback
151             $_length = strlen($string);
152             for ($x = 0; $x < $_length; $x ++) {
153                 $return .= '&#' . ord($string[ $x ]) . ';';
154             }
155
156             return $return;
157
158         case 'javascript':
159             // escape quotes and backslashes, newlines, etc.
160             return strtr($string, array('\\' => '\\\\', "'" => "\\'", '"' => '\\"', "\r" => '\\r', "\n" => '\\n',
161                                         '</' => '<\/'));
162
163         case 'mail':
164             if (Smarty::$_MBSTRING) {
165                 if (!is_callable('smarty_mb_str_replace')) {
166                     require_once(SMARTY_PLUGINS_DIR . 'shared.mb_str_replace.php');
167                 }
168                 return smarty_mb_str_replace(array('@', '.'), array(' [AT] ', ' [DOT] '), $string);
169             }
170             // no MBString fallback
171             return str_replace(array('@', '.'), array(' [AT] ', ' [DOT] '), $string);
172
173         case 'nonstd':
174             // escape non-standard chars, such as ms document quotes
175             $return = '';
176             if (Smarty::$_MBSTRING) {
177                 if (!is_callable('smarty_mb_to_unicode')) {
178                     require_once(SMARTY_PLUGINS_DIR . 'shared.mb_unicode.php');
179                 }
180                 foreach (smarty_mb_to_unicode($string, Smarty::$_CHARSET) as $unicode) {
181                     if ($unicode >= 126) {
182                         $return .= '&#' . $unicode . ';';
183                     } else {
184                         $return .= chr($unicode);
185                     }
186                 }
187
188                 return $return;
189             }
190
191             $_length = strlen($string);
192             for ($_i = 0; $_i < $_length; $_i ++) {
193                 $_ord = ord(substr($string, $_i, 1));
194                 // non-standard char, escape it
195                 if ($_ord >= 126) {
196                     $return .= '&#' . $_ord . ';';
197                 } else {
198                     $return .= substr($string, $_i, 1);
199                 }
200             }
201
202             return $return;
203
204         default:
205             return $string;
206     }
207 }