1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
<?php
/** * Checks for invalid UTF8 in a string. * * @since 2.8.0 * * @staticvar bool $is_utf8 * @staticvar bool $utf8_pcre * * @param string $string The text which is to be checked. * @param bool $strip Optional. Whether to attempt to strip out invalid UTF8. Default is false. * * @return string The checked text. */ function n2_check_invalid_utf8($string, $strip = false) { $string = (string)$string;
if (0 === strlen($string)) { return ''; }
// Store the site charset as a static to avoid multiple calls to get_option() static $is_utf8 = null; if (!isset($is_utf8)) { $is_utf8 = in_array(N2Platform::getCharset(), array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' )); } if (!$is_utf8) { return $string; }
// Check for support for utf8 in the installed PCRE library once and store the result in a static static $utf8_pcre = null; if (!isset($utf8_pcre)) { $utf8_pcre = @preg_match('/^./u', 'a'); } // We can't demand utf8 in the PCRE installation, so just return the string in those cases if (!$utf8_pcre) { return $string; }
// preg_match fails when it encounters invalid UTF8 in $string if (1 === @preg_match('/^./us', $string)) { return $string; }
// Attempt to strip the bad chars if requested (not recommended) if ($strip && function_exists('iconv')) { return iconv('utf-8', 'utf-8', $string); }
return ''; }
/** * Converts a number of special characters into their HTML entities. * * Specifically deals with: &, <, >, ", and '. * * $quote_style can be set to ENT_COMPAT to encode " to * ", or ENT_QUOTES to do both. Default is ENT_NOQUOTES where no quotes are encoded. * * @since 1.2.2 * @access private * * @staticvar string $_charset * * @param string $string The text which is to be encoded. * @param int|string $quote_style Optional. Converts double quotes if set to ENT_COMPAT, * both single and double if set to ENT_QUOTES or none if set to ENT_NOQUOTES. * Also compatible with old values; converting single quotes if set to 'single', * double if set to 'double' or both if otherwise set. * Default is ENT_NOQUOTES. * @param string $charset Optional. The character encoding of the string. Default is false. * @param bool $double_encode Optional. Whether to encode existing html entities. Default is false. * * @return string The encoded text with HTML entities. */ function _n2_specialchars($string, $quote_style = ENT_NOQUOTES, $charset = false, $double_encode = false) { $string = (string)$string;
if (0 === strlen($string)) return '';
// Don't bother if there are no specialchars - saves some processing if (!preg_match('/[&<>"\']/', $string)) return $string;
// Account for the previous behaviour of the function when the $quote_style is not an accepted value if (empty($quote_style)) $quote_style = ENT_NOQUOTES; else if (!in_array($quote_style, array( 0, 2, 3, 'single', 'double' ), true)) $quote_style = ENT_QUOTES;
// Store the site charset as a static to avoid multiple calls to wp_load_alloptions() if (!$charset) { static $_charset = null; if (!isset($_charset)) { $_charset = N2Platform::getCharset(); } $charset = $_charset; }
if (in_array($charset, array( 'utf8', 'utf-8', 'UTF8' ))) $charset = 'UTF-8';
$_quote_style = $quote_style;
if ($quote_style === 'double') { $quote_style = ENT_COMPAT; $_quote_style = ENT_COMPAT; } else if ($quote_style === 'single') { $quote_style = ENT_NOQUOTES; }
if (!$double_encode) { // Guarantee every &entity; is valid, convert &garbage; into &garbage; // This is required for PHP < 5.4.0 because ENT_HTML401 flag is unavailable. $string = n2_kses_normalize_entities($string); }
$string = @htmlspecialchars($string, $quote_style, $charset, $double_encode);
// Back-compat. if ('single' === $_quote_style) $string = str_replace("'", ''', $string);
return $string; }
/** * Converts and fixes HTML entities. * * This function normalizes HTML entities. It will convert `AT&T` to the correct * `AT&T`, `:` to `:`, `&#XYZZY;` to `&#XYZZY;` and so on. * * @since 1.0.0 * * @param string $string Content to normalize entities * * @return string Content with normalized entities */ function n2_kses_normalize_entities($string) { // Disarm all entities by converting & to & $string = str_replace('&', '&', $string);
// Change back the allowed entities in our entity whitelist $string = preg_replace_callback('/&([A-Za-z]{2,8}[0-9]{0,2});/', 'n2_kses_named_entities', $string); $string = preg_replace_callback('/&#(0*[0-9]{1,7});/', 'n2_kses_normalize_entities2', $string); $string = preg_replace_callback('/&#[Xx](0*[0-9A-Fa-f]{1,6});/', 'n2_kses_normalize_entities3', $string);
return $string; }
/** * Callback for n2_kses_normalize_entities() regular expression. * * This function only accepts valid named entity references, which are finite, * case-sensitive, and highly scrutinized by HTML and XML validators. * * @since 3.0.0 * * @global array $allowedentitynames * * @param array $matches preg_replace_callback() matches array * * @return string Correctly encoded entity */ function n2_kses_named_entities($matches) { global $allowedentitynames;
if (empty($matches[1])) return '';
$i = $matches[1];
return (!in_array($i, $allowedentitynames)) ? "&$i;" : "&$i;"; }
/** * Callback for n2_kses_normalize_entities() regular expression. * * This function helps n2_kses_normalize_entities() to only accept 16-bit * values and nothing more for `&#number;` entities. * * @access private * @since 1.0.0 * * @param array $matches preg_replace_callback() matches array * * @return string Correctly encoded entity */ function n2_kses_normalize_entities2($matches) { if (empty($matches[1])) return '';
$i = $matches[1]; if (n2_valid_unicode($i)) { $i = str_pad(ltrim($i, '0'), 3, '0', STR_PAD_LEFT); $i = "&#$i;"; } else { $i = "&#$i;"; }
return $i; }
/** * Callback for n2_kses_normalize_entities() for regular expression. * * This function helps n2_kses_normalize_entities() to only accept valid Unicode * numeric entities in hex form. * * @access private * * @param array $matches preg_replace_callback() matches array * * @return string Correctly encoded entity */ function n2_kses_normalize_entities3($matches) { if (empty($matches[1])) return '';
$hexchars = $matches[1];
return (!n2_valid_unicode(hexdec($hexchars))) ? "&#x$hexchars;" : '&#x' . ltrim($hexchars, '0') . ';'; }
/** * Helper function to determine if a Unicode value is valid. * * @param int $i Unicode value * * @return bool True if the value was a valid Unicode number */ function n2_valid_unicode($i) { return ($i == 0x9 || $i == 0xa || $i == 0xd || ($i >= 0x20 && $i <= 0xd7ff) || ($i >= 0xe000 && $i <= 0xfffd) || ($i >= 0x10000 && $i <= 0x10ffff)); }
/** * Escape single quotes, htmlspecialchar " < > &, and fix line endings. * * Escapes text strings for echoing in JS. It is intended to be used for inline JS * (in a tag attribute, for example onclick="..."). Note that the strings have to * be in single quotes. The {@see 'js_escape'} filter is also applied here. * * @since 2.8.0 * * @param string $text The text to be escaped. * * @return string Escaped text. */ function n2_esc_js($text) { $safe_text = n2_check_invalid_utf8($text); $safe_text = _n2_specialchars($safe_text, ENT_COMPAT); $safe_text = preg_replace('/&#(x)?0*(?(1)27|39);?/i', "'", stripslashes($safe_text)); $safe_text = str_replace("\r", '', $safe_text); $safe_text = str_replace("\n", '\\n', addslashes($safe_text));
return $safe_text; }
/** * Escaping for HTML blocks. * * @since 2.8.0 * * @param string $text * * @return string */ function n2_esc_html($text) { $safe_text = n2_check_invalid_utf8($text); $safe_text = _n2_specialchars($safe_text, ENT_QUOTES);
return $safe_text; }
/** * Escaping for HTML attributes. * * @since 2.8.0 * * @param string $text * * @return string */ function n2_esc_attr($text) { $safe_text = n2_check_invalid_utf8($text); $safe_text = _n2_specialchars($safe_text, ENT_QUOTES);
return $safe_text; }
/** * Escaping for textarea values. * * @since 3.1.0 * * @param string $text * * @return string */ function n2_esc_textarea($text) { $safe_text = htmlspecialchars($text, ENT_QUOTES, N2Platform::getCharset());
return $safe_text; }
function n2_esc_css_value($text) { $safe_text = n2_check_invalid_utf8($text);
return preg_replace_callback('/<\/style.*?>/i', 'n2_esc_css_value_callback', $safe_text); }
function n2_esc_css_value_callback($a) { return n2_esc_html($a[0]); }
|