author | ymh <ymh.work@gmail.com> |
Mon, 14 Oct 2019 18:28:13 +0200 | |
changeset 9 | 177826044cd9 |
parent 7 | cf61fcea0001 |
child 16 | a86126ab1dd4 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
3 |
* kses 0.2.2 - HTML/XHTML filter that only allows some elements and attributes |
|
4 |
* Copyright (C) 2002, 2003, 2005 Ulf Harnhammar |
|
5 |
* |
|
6 |
* This program is free software and open source software; you can redistribute |
|
7 |
* it and/or modify it under the terms of the GNU General Public License as |
|
8 |
* published by the Free Software Foundation; either version 2 of the License, |
|
9 |
* or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
|
14 |
* more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License along |
|
17 |
* with this program; if not, write to the Free Software Foundation, Inc., |
|
18 |
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA |
|
19 |
* http://www.gnu.org/licenses/gpl.html |
|
20 |
* |
|
21 |
* [kses strips evil scripts!] |
|
22 |
* |
|
23 |
* Added wp_ prefix to avoid conflicts with existing kses users |
|
24 |
* |
|
25 |
* @version 0.2.2 |
|
26 |
* @copyright (C) 2002, 2003, 2005 |
|
27 |
* @author Ulf Harnhammar <http://advogato.org/person/metaur/> |
|
28 |
* |
|
29 |
* @package External |
|
30 |
* @subpackage KSES |
|
31 |
*/ |
|
32 |
||
33 |
/** |
|
9 | 34 |
* Specifies the default allowable HTML tags. |
0 | 35 |
* |
9 | 36 |
* Using `CUSTOM_TAGS` is not recommended and should be considered deprecated. The |
37 |
* {@see 'wp_kses_allowed_html'} filter is more powerful and supplies context. |
|
0 | 38 |
* |
39 |
* @see wp_kses_allowed_html() |
|
9 | 40 |
* @since 1.2.0 |
0 | 41 |
* |
9 | 42 |
* @var array[]|bool Array of default allowable HTML tags, or false to use the defaults. |
0 | 43 |
*/ |
9 | 44 |
if ( ! defined( 'CUSTOM_TAGS' ) ) { |
0 | 45 |
define( 'CUSTOM_TAGS', false ); |
9 | 46 |
} |
0 | 47 |
|
5 | 48 |
// Ensure that these variables are added to the global namespace |
49 |
// (e.g. if using namespaces / autoload in the current PHP environment). |
|
50 |
global $allowedposttags, $allowedtags, $allowedentitynames; |
|
51 |
||
0 | 52 |
if ( ! CUSTOM_TAGS ) { |
53 |
/** |
|
9 | 54 |
* KSES global for default allowable HTML tags. |
0 | 55 |
* |
9 | 56 |
* Can be overridden with the `CUSTOM_TAGS` constant. |
0 | 57 |
* |
9 | 58 |
* @var array[] $allowedposttags Array of default allowable HTML tags. |
0 | 59 |
* @since 2.0.0 |
60 |
*/ |
|
61 |
$allowedposttags = array( |
|
9 | 62 |
'address' => array(), |
63 |
'a' => array( |
|
64 |
'href' => true, |
|
65 |
'rel' => true, |
|
66 |
'rev' => true, |
|
67 |
'name' => true, |
|
68 |
'target' => true, |
|
69 |
'download' => array( |
|
70 |
'valueless' => 'y', |
|
71 |
), |
|
0 | 72 |
), |
9 | 73 |
'abbr' => array(), |
74 |
'acronym' => array(), |
|
75 |
'area' => array( |
|
76 |
'alt' => true, |
|
0 | 77 |
'coords' => true, |
9 | 78 |
'href' => true, |
0 | 79 |
'nohref' => true, |
9 | 80 |
'shape' => true, |
0 | 81 |
'target' => true, |
82 |
), |
|
9 | 83 |
'article' => array( |
84 |
'align' => true, |
|
85 |
'dir' => true, |
|
86 |
'lang' => true, |
|
0 | 87 |
'xml:lang' => true, |
88 |
), |
|
9 | 89 |
'aside' => array( |
90 |
'align' => true, |
|
91 |
'dir' => true, |
|
92 |
'lang' => true, |
|
0 | 93 |
'xml:lang' => true, |
94 |
), |
|
9 | 95 |
'audio' => array( |
5 | 96 |
'autoplay' => true, |
97 |
'controls' => true, |
|
9 | 98 |
'loop' => true, |
99 |
'muted' => true, |
|
100 |
'preload' => true, |
|
101 |
'src' => true, |
|
5 | 102 |
), |
9 | 103 |
'b' => array(), |
104 |
'bdo' => array( |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
105 |
'dir' => true, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
106 |
), |
9 | 107 |
'big' => array(), |
0 | 108 |
'blockquote' => array( |
9 | 109 |
'cite' => true, |
110 |
'lang' => true, |
|
0 | 111 |
'xml:lang' => true, |
112 |
), |
|
9 | 113 |
'br' => array(), |
114 |
'button' => array( |
|
0 | 115 |
'disabled' => true, |
9 | 116 |
'name' => true, |
117 |
'type' => true, |
|
118 |
'value' => true, |
|
0 | 119 |
), |
9 | 120 |
'caption' => array( |
0 | 121 |
'align' => true, |
122 |
), |
|
9 | 123 |
'cite' => array( |
124 |
'dir' => true, |
|
0 | 125 |
'lang' => true, |
126 |
), |
|
9 | 127 |
'code' => array(), |
128 |
'col' => array( |
|
129 |
'align' => true, |
|
130 |
'char' => true, |
|
0 | 131 |
'charoff' => true, |
9 | 132 |
'span' => true, |
133 |
'dir' => true, |
|
134 |
'valign' => true, |
|
135 |
'width' => true, |
|
0 | 136 |
), |
9 | 137 |
'colgroup' => array( |
138 |
'align' => true, |
|
139 |
'char' => true, |
|
5 | 140 |
'charoff' => true, |
9 | 141 |
'span' => true, |
142 |
'valign' => true, |
|
143 |
'width' => true, |
|
5 | 144 |
), |
9 | 145 |
'del' => array( |
0 | 146 |
'datetime' => true, |
147 |
), |
|
9 | 148 |
'dd' => array(), |
149 |
'dfn' => array(), |
|
150 |
'details' => array( |
|
151 |
'align' => true, |
|
152 |
'dir' => true, |
|
153 |
'lang' => true, |
|
154 |
'open' => true, |
|
0 | 155 |
'xml:lang' => true, |
156 |
), |
|
9 | 157 |
'div' => array( |
158 |
'align' => true, |
|
159 |
'dir' => true, |
|
160 |
'lang' => true, |
|
0 | 161 |
'xml:lang' => true, |
162 |
), |
|
9 | 163 |
'dl' => array(), |
164 |
'dt' => array(), |
|
165 |
'em' => array(), |
|
166 |
'fieldset' => array(), |
|
167 |
'figure' => array( |
|
168 |
'align' => true, |
|
169 |
'dir' => true, |
|
170 |
'lang' => true, |
|
0 | 171 |
'xml:lang' => true, |
172 |
), |
|
173 |
'figcaption' => array( |
|
9 | 174 |
'align' => true, |
175 |
'dir' => true, |
|
176 |
'lang' => true, |
|
0 | 177 |
'xml:lang' => true, |
178 |
), |
|
9 | 179 |
'font' => array( |
0 | 180 |
'color' => true, |
9 | 181 |
'face' => true, |
182 |
'size' => true, |
|
0 | 183 |
), |
9 | 184 |
'footer' => array( |
185 |
'align' => true, |
|
186 |
'dir' => true, |
|
187 |
'lang' => true, |
|
0 | 188 |
'xml:lang' => true, |
189 |
), |
|
9 | 190 |
'h1' => array( |
191 |
'align' => true, |
|
0 | 192 |
), |
9 | 193 |
'h2' => array( |
0 | 194 |
'align' => true, |
195 |
), |
|
9 | 196 |
'h3' => array( |
0 | 197 |
'align' => true, |
198 |
), |
|
9 | 199 |
'h4' => array( |
0 | 200 |
'align' => true, |
201 |
), |
|
9 | 202 |
'h5' => array( |
0 | 203 |
'align' => true, |
204 |
), |
|
9 | 205 |
'h6' => array( |
0 | 206 |
'align' => true, |
207 |
), |
|
9 | 208 |
'header' => array( |
209 |
'align' => true, |
|
210 |
'dir' => true, |
|
211 |
'lang' => true, |
|
0 | 212 |
'xml:lang' => true, |
213 |
), |
|
9 | 214 |
'hgroup' => array( |
215 |
'align' => true, |
|
216 |
'dir' => true, |
|
217 |
'lang' => true, |
|
0 | 218 |
'xml:lang' => true, |
219 |
), |
|
9 | 220 |
'hr' => array( |
221 |
'align' => true, |
|
0 | 222 |
'noshade' => true, |
9 | 223 |
'size' => true, |
224 |
'width' => true, |
|
0 | 225 |
), |
9 | 226 |
'i' => array(), |
227 |
'img' => array( |
|
228 |
'alt' => true, |
|
229 |
'align' => true, |
|
230 |
'border' => true, |
|
231 |
'height' => true, |
|
232 |
'hspace' => true, |
|
0 | 233 |
'longdesc' => true, |
9 | 234 |
'vspace' => true, |
235 |
'src' => true, |
|
236 |
'usemap' => true, |
|
237 |
'width' => true, |
|
0 | 238 |
), |
9 | 239 |
'ins' => array( |
0 | 240 |
'datetime' => true, |
9 | 241 |
'cite' => true, |
0 | 242 |
), |
9 | 243 |
'kbd' => array(), |
244 |
'label' => array( |
|
0 | 245 |
'for' => true, |
246 |
), |
|
9 | 247 |
'legend' => array( |
0 | 248 |
'align' => true, |
249 |
), |
|
9 | 250 |
'li' => array( |
0 | 251 |
'align' => true, |
252 |
'value' => true, |
|
253 |
), |
|
9 | 254 |
'map' => array( |
0 | 255 |
'name' => true, |
256 |
), |
|
9 | 257 |
'mark' => array(), |
258 |
'menu' => array( |
|
0 | 259 |
'type' => true, |
260 |
), |
|
9 | 261 |
'nav' => array( |
262 |
'align' => true, |
|
263 |
'dir' => true, |
|
264 |
'lang' => true, |
|
0 | 265 |
'xml:lang' => true, |
266 |
), |
|
9 | 267 |
'p' => array( |
268 |
'align' => true, |
|
269 |
'dir' => true, |
|
270 |
'lang' => true, |
|
0 | 271 |
'xml:lang' => true, |
272 |
), |
|
9 | 273 |
'pre' => array( |
0 | 274 |
'width' => true, |
275 |
), |
|
9 | 276 |
'q' => array( |
0 | 277 |
'cite' => true, |
278 |
), |
|
9 | 279 |
's' => array(), |
280 |
'samp' => array(), |
|
281 |
'span' => array( |
|
282 |
'dir' => true, |
|
283 |
'align' => true, |
|
284 |
'lang' => true, |
|
0 | 285 |
'xml:lang' => true, |
286 |
), |
|
9 | 287 |
'section' => array( |
288 |
'align' => true, |
|
289 |
'dir' => true, |
|
290 |
'lang' => true, |
|
0 | 291 |
'xml:lang' => true, |
292 |
), |
|
9 | 293 |
'small' => array(), |
294 |
'strike' => array(), |
|
295 |
'strong' => array(), |
|
296 |
'sub' => array(), |
|
297 |
'summary' => array( |
|
298 |
'align' => true, |
|
299 |
'dir' => true, |
|
300 |
'lang' => true, |
|
0 | 301 |
'xml:lang' => true, |
302 |
), |
|
9 | 303 |
'sup' => array(), |
304 |
'table' => array( |
|
305 |
'align' => true, |
|
306 |
'bgcolor' => true, |
|
307 |
'border' => true, |
|
0 | 308 |
'cellpadding' => true, |
309 |
'cellspacing' => true, |
|
9 | 310 |
'dir' => true, |
311 |
'rules' => true, |
|
312 |
'summary' => true, |
|
313 |
'width' => true, |
|
0 | 314 |
), |
9 | 315 |
'tbody' => array( |
316 |
'align' => true, |
|
317 |
'char' => true, |
|
0 | 318 |
'charoff' => true, |
9 | 319 |
'valign' => true, |
0 | 320 |
), |
9 | 321 |
'td' => array( |
322 |
'abbr' => true, |
|
323 |
'align' => true, |
|
324 |
'axis' => true, |
|
0 | 325 |
'bgcolor' => true, |
9 | 326 |
'char' => true, |
0 | 327 |
'charoff' => true, |
328 |
'colspan' => true, |
|
9 | 329 |
'dir' => true, |
0 | 330 |
'headers' => true, |
9 | 331 |
'height' => true, |
332 |
'nowrap' => true, |
|
0 | 333 |
'rowspan' => true, |
9 | 334 |
'scope' => true, |
335 |
'valign' => true, |
|
336 |
'width' => true, |
|
0 | 337 |
), |
9 | 338 |
'textarea' => array( |
339 |
'cols' => true, |
|
340 |
'rows' => true, |
|
0 | 341 |
'disabled' => true, |
9 | 342 |
'name' => true, |
0 | 343 |
'readonly' => true, |
344 |
), |
|
9 | 345 |
'tfoot' => array( |
346 |
'align' => true, |
|
347 |
'char' => true, |
|
0 | 348 |
'charoff' => true, |
9 | 349 |
'valign' => true, |
0 | 350 |
), |
9 | 351 |
'th' => array( |
352 |
'abbr' => true, |
|
353 |
'align' => true, |
|
354 |
'axis' => true, |
|
0 | 355 |
'bgcolor' => true, |
9 | 356 |
'char' => true, |
0 | 357 |
'charoff' => true, |
358 |
'colspan' => true, |
|
359 |
'headers' => true, |
|
9 | 360 |
'height' => true, |
361 |
'nowrap' => true, |
|
0 | 362 |
'rowspan' => true, |
9 | 363 |
'scope' => true, |
364 |
'valign' => true, |
|
365 |
'width' => true, |
|
0 | 366 |
), |
9 | 367 |
'thead' => array( |
368 |
'align' => true, |
|
369 |
'char' => true, |
|
0 | 370 |
'charoff' => true, |
9 | 371 |
'valign' => true, |
0 | 372 |
), |
9 | 373 |
'title' => array(), |
374 |
'tr' => array( |
|
375 |
'align' => true, |
|
0 | 376 |
'bgcolor' => true, |
9 | 377 |
'char' => true, |
0 | 378 |
'charoff' => true, |
9 | 379 |
'valign' => true, |
0 | 380 |
), |
9 | 381 |
'track' => array( |
5 | 382 |
'default' => true, |
9 | 383 |
'kind' => true, |
384 |
'label' => true, |
|
385 |
'src' => true, |
|
5 | 386 |
'srclang' => true, |
387 |
), |
|
9 | 388 |
'tt' => array(), |
389 |
'u' => array(), |
|
390 |
'ul' => array( |
|
0 | 391 |
'type' => true, |
392 |
), |
|
9 | 393 |
'ol' => array( |
394 |
'start' => true, |
|
395 |
'type' => true, |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
396 |
'reversed' => true, |
0 | 397 |
), |
9 | 398 |
'var' => array(), |
399 |
'video' => array( |
|
5 | 400 |
'autoplay' => true, |
401 |
'controls' => true, |
|
9 | 402 |
'height' => true, |
403 |
'loop' => true, |
|
404 |
'muted' => true, |
|
405 |
'poster' => true, |
|
406 |
'preload' => true, |
|
407 |
'src' => true, |
|
408 |
'width' => true, |
|
5 | 409 |
), |
0 | 410 |
); |
411 |
||
412 |
/** |
|
9 | 413 |
* @var array[] $allowedtags Array of KSES allowed HTML elements. |
0 | 414 |
* @since 1.0.0 |
415 |
*/ |
|
416 |
$allowedtags = array( |
|
9 | 417 |
'a' => array( |
418 |
'href' => true, |
|
0 | 419 |
'title' => true, |
420 |
), |
|
9 | 421 |
'abbr' => array( |
0 | 422 |
'title' => true, |
423 |
), |
|
9 | 424 |
'acronym' => array( |
0 | 425 |
'title' => true, |
426 |
), |
|
9 | 427 |
'b' => array(), |
0 | 428 |
'blockquote' => array( |
429 |
'cite' => true, |
|
430 |
), |
|
9 | 431 |
'cite' => array(), |
432 |
'code' => array(), |
|
433 |
'del' => array( |
|
0 | 434 |
'datetime' => true, |
435 |
), |
|
9 | 436 |
'em' => array(), |
437 |
'i' => array(), |
|
438 |
'q' => array( |
|
0 | 439 |
'cite' => true, |
440 |
), |
|
9 | 441 |
's' => array(), |
442 |
'strike' => array(), |
|
443 |
'strong' => array(), |
|
0 | 444 |
); |
445 |
||
9 | 446 |
/** |
447 |
* @var string[] $allowedentitynames Array of KSES allowed HTML entitity names. |
|
448 |
* @since 1.0.0 |
|
449 |
*/ |
|
0 | 450 |
$allowedentitynames = array( |
9 | 451 |
'nbsp', |
452 |
'iexcl', |
|
453 |
'cent', |
|
454 |
'pound', |
|
455 |
'curren', |
|
456 |
'yen', |
|
457 |
'brvbar', |
|
458 |
'sect', |
|
459 |
'uml', |
|
460 |
'copy', |
|
461 |
'ordf', |
|
462 |
'laquo', |
|
463 |
'not', |
|
464 |
'shy', |
|
465 |
'reg', |
|
466 |
'macr', |
|
467 |
'deg', |
|
468 |
'plusmn', |
|
469 |
'acute', |
|
470 |
'micro', |
|
471 |
'para', |
|
472 |
'middot', |
|
473 |
'cedil', |
|
474 |
'ordm', |
|
475 |
'raquo', |
|
476 |
'iquest', |
|
477 |
'Agrave', |
|
478 |
'Aacute', |
|
479 |
'Acirc', |
|
480 |
'Atilde', |
|
481 |
'Auml', |
|
482 |
'Aring', |
|
483 |
'AElig', |
|
484 |
'Ccedil', |
|
485 |
'Egrave', |
|
486 |
'Eacute', |
|
487 |
'Ecirc', |
|
488 |
'Euml', |
|
489 |
'Igrave', |
|
490 |
'Iacute', |
|
491 |
'Icirc', |
|
492 |
'Iuml', |
|
493 |
'ETH', |
|
494 |
'Ntilde', |
|
495 |
'Ograve', |
|
496 |
'Oacute', |
|
497 |
'Ocirc', |
|
498 |
'Otilde', |
|
499 |
'Ouml', |
|
500 |
'times', |
|
501 |
'Oslash', |
|
502 |
'Ugrave', |
|
503 |
'Uacute', |
|
504 |
'Ucirc', |
|
505 |
'Uuml', |
|
506 |
'Yacute', |
|
507 |
'THORN', |
|
508 |
'szlig', |
|
509 |
'agrave', |
|
510 |
'aacute', |
|
511 |
'acirc', |
|
512 |
'atilde', |
|
513 |
'auml', |
|
514 |
'aring', |
|
515 |
'aelig', |
|
516 |
'ccedil', |
|
517 |
'egrave', |
|
518 |
'eacute', |
|
519 |
'ecirc', |
|
520 |
'euml', |
|
521 |
'igrave', |
|
522 |
'iacute', |
|
523 |
'icirc', |
|
524 |
'iuml', |
|
525 |
'eth', |
|
526 |
'ntilde', |
|
527 |
'ograve', |
|
528 |
'oacute', |
|
529 |
'ocirc', |
|
530 |
'otilde', |
|
531 |
'ouml', |
|
532 |
'divide', |
|
533 |
'oslash', |
|
534 |
'ugrave', |
|
535 |
'uacute', |
|
536 |
'ucirc', |
|
537 |
'uuml', |
|
538 |
'yacute', |
|
539 |
'thorn', |
|
540 |
'yuml', |
|
541 |
'quot', |
|
542 |
'amp', |
|
543 |
'lt', |
|
544 |
'gt', |
|
545 |
'apos', |
|
546 |
'OElig', |
|
547 |
'oelig', |
|
548 |
'Scaron', |
|
549 |
'scaron', |
|
550 |
'Yuml', |
|
551 |
'circ', |
|
552 |
'tilde', |
|
553 |
'ensp', |
|
554 |
'emsp', |
|
555 |
'thinsp', |
|
556 |
'zwnj', |
|
557 |
'zwj', |
|
558 |
'lrm', |
|
559 |
'rlm', |
|
560 |
'ndash', |
|
561 |
'mdash', |
|
562 |
'lsquo', |
|
563 |
'rsquo', |
|
564 |
'sbquo', |
|
565 |
'ldquo', |
|
566 |
'rdquo', |
|
567 |
'bdquo', |
|
568 |
'dagger', |
|
569 |
'Dagger', |
|
570 |
'permil', |
|
571 |
'lsaquo', |
|
572 |
'rsaquo', |
|
573 |
'euro', |
|
574 |
'fnof', |
|
575 |
'Alpha', |
|
576 |
'Beta', |
|
577 |
'Gamma', |
|
578 |
'Delta', |
|
579 |
'Epsilon', |
|
580 |
'Zeta', |
|
581 |
'Eta', |
|
582 |
'Theta', |
|
583 |
'Iota', |
|
584 |
'Kappa', |
|
585 |
'Lambda', |
|
586 |
'Mu', |
|
587 |
'Nu', |
|
588 |
'Xi', |
|
589 |
'Omicron', |
|
590 |
'Pi', |
|
591 |
'Rho', |
|
592 |
'Sigma', |
|
593 |
'Tau', |
|
594 |
'Upsilon', |
|
595 |
'Phi', |
|
596 |
'Chi', |
|
597 |
'Psi', |
|
598 |
'Omega', |
|
599 |
'alpha', |
|
600 |
'beta', |
|
601 |
'gamma', |
|
602 |
'delta', |
|
603 |
'epsilon', |
|
604 |
'zeta', |
|
605 |
'eta', |
|
606 |
'theta', |
|
607 |
'iota', |
|
608 |
'kappa', |
|
609 |
'lambda', |
|
610 |
'mu', |
|
611 |
'nu', |
|
612 |
'xi', |
|
613 |
'omicron', |
|
614 |
'pi', |
|
615 |
'rho', |
|
616 |
'sigmaf', |
|
617 |
'sigma', |
|
618 |
'tau', |
|
619 |
'upsilon', |
|
620 |
'phi', |
|
621 |
'chi', |
|
622 |
'psi', |
|
623 |
'omega', |
|
624 |
'thetasym', |
|
625 |
'upsih', |
|
626 |
'piv', |
|
627 |
'bull', |
|
628 |
'hellip', |
|
629 |
'prime', |
|
630 |
'Prime', |
|
631 |
'oline', |
|
632 |
'frasl', |
|
633 |
'weierp', |
|
634 |
'image', |
|
635 |
'real', |
|
636 |
'trade', |
|
637 |
'alefsym', |
|
638 |
'larr', |
|
639 |
'uarr', |
|
640 |
'rarr', |
|
641 |
'darr', |
|
642 |
'harr', |
|
643 |
'crarr', |
|
644 |
'lArr', |
|
645 |
'uArr', |
|
646 |
'rArr', |
|
647 |
'dArr', |
|
648 |
'hArr', |
|
649 |
'forall', |
|
650 |
'part', |
|
651 |
'exist', |
|
652 |
'empty', |
|
653 |
'nabla', |
|
654 |
'isin', |
|
655 |
'notin', |
|
656 |
'ni', |
|
657 |
'prod', |
|
658 |
'sum', |
|
659 |
'minus', |
|
660 |
'lowast', |
|
661 |
'radic', |
|
662 |
'prop', |
|
663 |
'infin', |
|
664 |
'ang', |
|
665 |
'and', |
|
666 |
'or', |
|
667 |
'cap', |
|
668 |
'cup', |
|
669 |
'int', |
|
670 |
'sim', |
|
671 |
'cong', |
|
672 |
'asymp', |
|
673 |
'ne', |
|
674 |
'equiv', |
|
675 |
'le', |
|
676 |
'ge', |
|
677 |
'sub', |
|
678 |
'sup', |
|
679 |
'nsub', |
|
680 |
'sube', |
|
681 |
'supe', |
|
682 |
'oplus', |
|
683 |
'otimes', |
|
684 |
'perp', |
|
685 |
'sdot', |
|
686 |
'lceil', |
|
687 |
'rceil', |
|
688 |
'lfloor', |
|
689 |
'rfloor', |
|
690 |
'lang', |
|
691 |
'rang', |
|
692 |
'loz', |
|
693 |
'spades', |
|
694 |
'clubs', |
|
695 |
'hearts', |
|
696 |
'diams', |
|
697 |
'sup1', |
|
698 |
'sup2', |
|
699 |
'sup3', |
|
700 |
'frac14', |
|
701 |
'frac12', |
|
702 |
'frac34', |
|
5 | 703 |
'there4', |
0 | 704 |
); |
705 |
||
706 |
$allowedposttags = array_map( '_wp_add_global_attributes', $allowedposttags ); |
|
707 |
} else { |
|
9 | 708 |
$allowedtags = wp_kses_array_lc( $allowedtags ); |
0 | 709 |
$allowedposttags = wp_kses_array_lc( $allowedposttags ); |
710 |
} |
|
711 |
||
712 |
/** |
|
9 | 713 |
* Filters text content and strips out disallowed HTML. |
0 | 714 |
* |
715 |
* This function makes sure that only the allowed HTML element names, attribute |
|
9 | 716 |
* names, attribute values, and HTML entities will occur in the given text string. |
0 | 717 |
* |
9 | 718 |
* This function expects unslashed data. |
719 |
* |
|
720 |
* @see wp_kses_post() for specifically filtering post content and fields. |
|
721 |
* @see wp_allowed_protocols() for the default allowed protocols in link URLs. |
|
0 | 722 |
* |
723 |
* @since 1.0.0 |
|
724 |
* |
|
9 | 725 |
* @param string $string Text content to filter. |
726 |
* @param array[]|string $allowed_html An array of allowed HTML elements and attributes, or a |
|
727 |
* context name such as 'post'. |
|
728 |
* @param string[] $allowed_protocols Array of allowed URL protocols. |
|
729 |
* @return string Filtered content containing only the allowed HTML. |
|
0 | 730 |
*/ |
731 |
function wp_kses( $string, $allowed_html, $allowed_protocols = array() ) { |
|
9 | 732 |
if ( empty( $allowed_protocols ) ) { |
0 | 733 |
$allowed_protocols = wp_allowed_protocols(); |
9 | 734 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
735 |
$string = wp_kses_no_null( $string, array( 'slash_zero' => 'keep' ) ); |
9 | 736 |
$string = wp_kses_normalize_entities( $string ); |
737 |
$string = wp_kses_hook( $string, $allowed_html, $allowed_protocols ); |
|
738 |
return wp_kses_split( $string, $allowed_html, $allowed_protocols ); |
|
0 | 739 |
} |
740 |
||
741 |
/** |
|
9 | 742 |
* Filters one HTML attribute and ensures its value is allowed. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
743 |
* |
9 | 744 |
* This function can escape data in some situations where `wp_kses()` must strip the whole attribute. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
745 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
746 |
* @since 4.2.3 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
747 |
* |
9 | 748 |
* @param string $string The 'whole' attribute, including name and value. |
749 |
* @param string $element The HTML element name to which the attribute belongs. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
750 |
* @return string Filtered attribute. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
751 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
752 |
function wp_kses_one_attr( $string, $element ) { |
9 | 753 |
$uris = wp_kses_uri_attributes(); |
754 |
$allowed_html = wp_kses_allowed_html( 'post' ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
755 |
$allowed_protocols = wp_allowed_protocols(); |
9 | 756 |
$string = wp_kses_no_null( $string, array( 'slash_zero' => 'keep' ) ); |
757 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
758 |
// Preserve leading and trailing whitespace. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
759 |
$matches = array(); |
9 | 760 |
preg_match( '/^\s*/', $string, $matches ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
761 |
$lead = $matches[0]; |
9 | 762 |
preg_match( '/\s*$/', $string, $matches ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
763 |
$trail = $matches[0]; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
764 |
if ( empty( $trail ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
765 |
$string = substr( $string, strlen( $lead ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
766 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
767 |
$string = substr( $string, strlen( $lead ), -strlen( $trail ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
768 |
} |
9 | 769 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
770 |
// Parse attribute name and value from input. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
771 |
$split = preg_split( '/\s*=\s*/', $string, 2 ); |
9 | 772 |
$name = $split[0]; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
773 |
if ( count( $split ) == 2 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
774 |
$value = $split[1]; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
775 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
776 |
// Remove quotes surrounding $value. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
777 |
// Also guarantee correct quoting in $string for this one attribute. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
778 |
if ( '' == $value ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
779 |
$quote = ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
780 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
781 |
$quote = $value[0]; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
782 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
783 |
if ( '"' == $quote || "'" == $quote ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
784 |
if ( substr( $value, -1 ) != $quote ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
785 |
return ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
786 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
787 |
$value = substr( $value, 1, -1 ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
788 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
789 |
$quote = '"'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
790 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
791 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
792 |
// Sanitize quotes, angle braces, and entities. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
793 |
$value = esc_attr( $value ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
794 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
795 |
// Sanitize URI values. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
796 |
if ( in_array( strtolower( $name ), $uris ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
797 |
$value = wp_kses_bad_protocol( $value, $allowed_protocols ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
798 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
799 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
800 |
$string = "$name=$quote$value$quote"; |
9 | 801 |
$vless = 'n'; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
802 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
803 |
$value = ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
804 |
$vless = 'y'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
805 |
} |
9 | 806 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
807 |
// Sanitize attribute by name. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
808 |
wp_kses_attr_check( $name, $value, $string, $vless, $element, $allowed_html ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
809 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
810 |
// Restore whitespace. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
811 |
return $lead . $string . $trail; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
812 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
813 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
814 |
/** |
9 | 815 |
* Returns an array of allowed HTML tags and attributes for a given context. |
0 | 816 |
* |
817 |
* @since 3.5.0 |
|
9 | 818 |
* @since 5.0.1 `form` removed as allowable HTML tag. |
0 | 819 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
820 |
* @global array $allowedposttags |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
821 |
* @global array $allowedtags |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
822 |
* @global array $allowedentitynames |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
823 |
* |
9 | 824 |
* @param string|array $context The context for which to retrieve tags. Allowed values are 'post', |
825 |
* 'strip', 'data', 'entities', or the name of a field filter such as |
|
826 |
* 'pre_user_description'. |
|
827 |
* @return array Array of allowed HTML tags and their allowed attributes. |
|
0 | 828 |
*/ |
829 |
function wp_kses_allowed_html( $context = '' ) { |
|
830 |
global $allowedposttags, $allowedtags, $allowedentitynames; |
|
831 |
||
5 | 832 |
if ( is_array( $context ) ) { |
833 |
/** |
|
9 | 834 |
* Filters the HTML that is allowed for a given context. |
5 | 835 |
* |
836 |
* @since 3.5.0 |
|
837 |
* |
|
9 | 838 |
* @param array[]|string $context Context to judge allowed tags by. |
839 |
* @param string $context_type Context name. |
|
5 | 840 |
*/ |
0 | 841 |
return apply_filters( 'wp_kses_allowed_html', $context, 'explicit' ); |
5 | 842 |
} |
0 | 843 |
|
844 |
switch ( $context ) { |
|
845 |
case 'post': |
|
5 | 846 |
/** This filter is documented in wp-includes/kses.php */ |
9 | 847 |
$tags = apply_filters( 'wp_kses_allowed_html', $allowedposttags, $context ); |
848 |
||
849 |
// 5.0.1 removed the `<form>` tag, allow it if a filter is allowing it's sub-elements `<input>` or `<select>`. |
|
850 |
if ( ! CUSTOM_TAGS && ! isset( $tags['form'] ) && ( isset( $tags['input'] ) || isset( $tags['select'] ) ) ) { |
|
851 |
$tags = $allowedposttags; |
|
852 |
||
853 |
$tags['form'] = array( |
|
854 |
'action' => true, |
|
855 |
'accept' => true, |
|
856 |
'accept-charset' => true, |
|
857 |
'enctype' => true, |
|
858 |
'method' => true, |
|
859 |
'name' => true, |
|
860 |
'target' => true, |
|
861 |
); |
|
862 |
||
863 |
/** This filter is documented in wp-includes/kses.php */ |
|
864 |
$tags = apply_filters( 'wp_kses_allowed_html', $tags, $context ); |
|
865 |
} |
|
866 |
||
867 |
return $tags; |
|
5 | 868 |
|
0 | 869 |
case 'user_description': |
870 |
case 'pre_user_description': |
|
9 | 871 |
$tags = $allowedtags; |
0 | 872 |
$tags['a']['rel'] = true; |
5 | 873 |
/** This filter is documented in wp-includes/kses.php */ |
0 | 874 |
return apply_filters( 'wp_kses_allowed_html', $tags, $context ); |
5 | 875 |
|
0 | 876 |
case 'strip': |
5 | 877 |
/** This filter is documented in wp-includes/kses.php */ |
0 | 878 |
return apply_filters( 'wp_kses_allowed_html', array(), $context ); |
5 | 879 |
|
0 | 880 |
case 'entities': |
5 | 881 |
/** This filter is documented in wp-includes/kses.php */ |
9 | 882 |
return apply_filters( 'wp_kses_allowed_html', $allowedentitynames, $context ); |
5 | 883 |
|
0 | 884 |
case 'data': |
885 |
default: |
|
5 | 886 |
/** This filter is documented in wp-includes/kses.php */ |
0 | 887 |
return apply_filters( 'wp_kses_allowed_html', $allowedtags, $context ); |
888 |
} |
|
889 |
} |
|
890 |
||
891 |
/** |
|
9 | 892 |
* You add any KSES hooks here. |
0 | 893 |
* |
9 | 894 |
* There is currently only one KSES WordPress hook, {@see 'pre_kses'}, and it is called here. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
895 |
* All parameters are passed to the hooks and expected to receive a string. |
0 | 896 |
* |
897 |
* @since 1.0.0 |
|
898 |
* |
|
9 | 899 |
* @param string $string Content to filter through KSES. |
900 |
* @param array[]|string $allowed_html List of allowed HTML elements. |
|
901 |
* @param string[] $allowed_protocols Array of allowed URL protocols. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
902 |
* @return string Filtered content through {@see 'pre_kses'} hook. |
0 | 903 |
*/ |
904 |
function wp_kses_hook( $string, $allowed_html, $allowed_protocols ) { |
|
5 | 905 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
906 |
* Filters content to be run through kses. |
5 | 907 |
* |
908 |
* @since 2.3.0 |
|
909 |
* |
|
9 | 910 |
* @param string $string Content to run through KSES. |
911 |
* @param array[]|string $allowed_html Allowed HTML elements. |
|
912 |
* @param string[] $allowed_protocols Array of allowed URL protocols. |
|
5 | 913 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
914 |
return apply_filters( 'pre_kses', $string, $allowed_html, $allowed_protocols ); |
0 | 915 |
} |
916 |
||
917 |
/** |
|
9 | 918 |
* Returns the version number of KSES. |
0 | 919 |
* |
920 |
* @since 1.0.0 |
|
921 |
* |
|
9 | 922 |
* @return string KSES version number. |
0 | 923 |
*/ |
924 |
function wp_kses_version() { |
|
925 |
return '0.2.2'; |
|
926 |
} |
|
927 |
||
928 |
/** |
|
929 |
* Searches for HTML tags, no matter how malformed. |
|
930 |
* |
|
9 | 931 |
* It also matches stray `>` characters. |
0 | 932 |
* |
933 |
* @since 1.0.0 |
|
934 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
935 |
* @global array $pass_allowed_html |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
936 |
* @global array $pass_allowed_protocols |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
937 |
* |
9 | 938 |
* @param string $string Content to filter. |
939 |
* @param array $allowed_html Allowed HTML elements. |
|
940 |
* @param string[] $allowed_protocols Array of allowed URL protocols. |
|
0 | 941 |
* @return string Content with fixed HTML tags |
942 |
*/ |
|
943 |
function wp_kses_split( $string, $allowed_html, $allowed_protocols ) { |
|
944 |
global $pass_allowed_html, $pass_allowed_protocols; |
|
9 | 945 |
$pass_allowed_html = $allowed_html; |
0 | 946 |
$pass_allowed_protocols = $allowed_protocols; |
947 |
return preg_replace_callback( '%(<!--.*?(-->|$))|(<[^>]*(>|$)|>)%', '_wp_kses_split_callback', $string ); |
|
948 |
} |
|
949 |
||
950 |
/** |
|
9 | 951 |
* Helper function listing HTML attributes containing a URL. |
952 |
* |
|
953 |
* This function returns a list of all HTML attributes that must contain |
|
954 |
* a URL according to the HTML specification. |
|
955 |
* |
|
956 |
* This list includes URI attributes both allowed and disallowed by KSES. |
|
957 |
* |
|
958 |
* @link https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes |
|
959 |
* |
|
960 |
* @since 5.0.1 |
|
961 |
* |
|
962 |
* @return array HTML attributes that must include a URL. |
|
963 |
*/ |
|
964 |
function wp_kses_uri_attributes() { |
|
965 |
$uri_attributes = array( |
|
966 |
'action', |
|
967 |
'archive', |
|
968 |
'background', |
|
969 |
'cite', |
|
970 |
'classid', |
|
971 |
'codebase', |
|
972 |
'data', |
|
973 |
'formaction', |
|
974 |
'href', |
|
975 |
'icon', |
|
976 |
'longdesc', |
|
977 |
'manifest', |
|
978 |
'poster', |
|
979 |
'profile', |
|
980 |
'src', |
|
981 |
'usemap', |
|
982 |
'xmlns', |
|
983 |
); |
|
984 |
||
985 |
/** |
|
986 |
* Filters the list of attributes that are required to contain a URL. |
|
987 |
* |
|
988 |
* Use this filter to add any `data-` attributes that are required to be |
|
989 |
* validated as a URL. |
|
990 |
* |
|
991 |
* @since 5.0.1 |
|
992 |
* |
|
993 |
* @param array $uri_attributes HTML attributes requiring validation as a URL. |
|
994 |
*/ |
|
995 |
$uri_attributes = apply_filters( 'wp_kses_uri_attributes', $uri_attributes ); |
|
996 |
||
997 |
return $uri_attributes; |
|
998 |
} |
|
999 |
||
1000 |
/** |
|
1001 |
* Callback for `wp_kses_split()`. |
|
0 | 1002 |
* |
1003 |
* @since 3.1.0 |
|
1004 |
* @access private |
|
9 | 1005 |
* @ignore |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1006 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1007 |
* @global array $pass_allowed_html |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1008 |
* @global array $pass_allowed_protocols |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1009 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1010 |
* @return string |
0 | 1011 |
*/ |
1012 |
function _wp_kses_split_callback( $match ) { |
|
1013 |
global $pass_allowed_html, $pass_allowed_protocols; |
|
1014 |
return wp_kses_split2( $match[0], $pass_allowed_html, $pass_allowed_protocols ); |
|
1015 |
} |
|
1016 |
||
1017 |
/** |
|
9 | 1018 |
* Callback for `wp_kses_split()` for fixing malformed HTML tags. |
0 | 1019 |
* |
1020 |
* This function does a lot of work. It rejects some very malformed things like |
|
9 | 1021 |
* `<:::>`. It returns an empty string, if the element isn't allowed (look ma, no |
1022 |
* `strip_tags()`!). Otherwise it splits the tag into an element and an attribute |
|
0 | 1023 |
* list. |
1024 |
* |
|
1025 |
* After the tag is split into an element and an attribute list, it is run |
|
1026 |
* through another filter which will remove illegal attributes and once that is |
|
1027 |
* completed, will be returned. |
|
1028 |
* |
|
1029 |
* @access private |
|
9 | 1030 |
* @ignore |
0 | 1031 |
* @since 1.0.0 |
1032 |
* |
|
9 | 1033 |
* @param string $string Content to filter. |
1034 |
* @param array $allowed_html Allowed HTML elements. |
|
1035 |
* @param string[] $allowed_protocols Array of allowed URL protocols. |
|
0 | 1036 |
* @return string Fixed HTML element |
1037 |
*/ |
|
9 | 1038 |
function wp_kses_split2( $string, $allowed_html, $allowed_protocols ) { |
1039 |
$string = wp_kses_stripslashes( $string ); |
|
0 | 1040 |
|
9 | 1041 |
// It matched a ">" character. |
1042 |
if ( substr( $string, 0, 1 ) != '<' ) { |
|
0 | 1043 |
return '>'; |
9 | 1044 |
} |
0 | 1045 |
|
9 | 1046 |
// Allow HTML comments. |
0 | 1047 |
if ( '<!--' == substr( $string, 0, 4 ) ) { |
9 | 1048 |
$string = str_replace( array( '<!--', '-->' ), '', $string ); |
1049 |
while ( $string != ( $newstring = wp_kses( $string, $allowed_html, $allowed_protocols ) ) ) { |
|
0 | 1050 |
$string = $newstring; |
9 | 1051 |
} |
1052 |
if ( $string == '' ) { |
|
0 | 1053 |
return ''; |
9 | 1054 |
} |
0 | 1055 |
// prevent multiple dashes in comments |
9 | 1056 |
$string = preg_replace( '/--+/', '-', $string ); |
0 | 1057 |
// prevent three dashes closing a comment |
9 | 1058 |
$string = preg_replace( '/-$/', '', $string ); |
0 | 1059 |
return "<!--{$string}-->"; |
1060 |
} |
|
1061 |
||
9 | 1062 |
// It's seriously malformed. |
1063 |
if ( ! preg_match( '%^<\s*(/\s*)?([a-zA-Z0-9-]+)([^>]*)>?$%', $string, $matches ) ) { |
|
0 | 1064 |
return ''; |
9 | 1065 |
} |
0 | 1066 |
|
9 | 1067 |
$slash = trim( $matches[1] ); |
1068 |
$elem = $matches[2]; |
|
0 | 1069 |
$attrlist = $matches[3]; |
1070 |
||
9 | 1071 |
if ( ! is_array( $allowed_html ) ) { |
0 | 1072 |
$allowed_html = wp_kses_allowed_html( $allowed_html ); |
9 | 1073 |
} |
0 | 1074 |
|
9 | 1075 |
// They are using a not allowed HTML element. |
1076 |
if ( ! isset( $allowed_html[ strtolower( $elem ) ] ) ) { |
|
0 | 1077 |
return ''; |
9 | 1078 |
} |
0 | 1079 |
|
9 | 1080 |
// No attributes are allowed for closing elements. |
1081 |
if ( $slash != '' ) { |
|
0 | 1082 |
return "</$elem>"; |
9 | 1083 |
} |
0 | 1084 |
|
1085 |
return wp_kses_attr( $elem, $attrlist, $allowed_html, $allowed_protocols ); |
|
1086 |
} |
|
1087 |
||
1088 |
/** |
|
1089 |
* Removes all attributes, if none are allowed for this element. |
|
1090 |
* |
|
9 | 1091 |
* If some are allowed it calls `wp_kses_hair()` to split them further, and then |
1092 |
* it builds up new HTML code from the data that `kses_hair()` returns. It also |
|
1093 |
* removes `<` and `>` characters, if there are any left. One more thing it does |
|
0 | 1094 |
* is to check if the tag has a closing XHTML slash, and if it does, it puts one |
1095 |
* in the returned code as well. |
|
1096 |
* |
|
1097 |
* @since 1.0.0 |
|
1098 |
* |
|
9 | 1099 |
* @param string $element HTML element/tag. |
1100 |
* @param string $attr HTML attributes from HTML element to closing HTML element tag. |
|
1101 |
* @param array $allowed_html Allowed HTML elements. |
|
1102 |
* @param string[] $allowed_protocols Array of allowed URL protocols. |
|
1103 |
* @return string Sanitized HTML element. |
|
0 | 1104 |
*/ |
9 | 1105 |
function wp_kses_attr( $element, $attr, $allowed_html, $allowed_protocols ) { |
1106 |
if ( ! is_array( $allowed_html ) ) { |
|
0 | 1107 |
$allowed_html = wp_kses_allowed_html( $allowed_html ); |
9 | 1108 |
} |
0 | 1109 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1110 |
// Is there a closing XHTML slash at the end of the attributes? |
0 | 1111 |
$xhtml_slash = ''; |
9 | 1112 |
if ( preg_match( '%\s*/\s*$%', $attr ) ) { |
0 | 1113 |
$xhtml_slash = ' /'; |
9 | 1114 |
} |
0 | 1115 |
|
5 | 1116 |
// Are any attributes allowed at all for this element? |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1117 |
$element_low = strtolower( $element ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1118 |
if ( empty( $allowed_html[ $element_low ] ) || true === $allowed_html[ $element_low ] ) { |
0 | 1119 |
return "<$element$xhtml_slash>"; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1120 |
} |
0 | 1121 |
|
5 | 1122 |
// Split it |
9 | 1123 |
$attrarr = wp_kses_hair( $attr, $allowed_protocols ); |
0 | 1124 |
|
5 | 1125 |
// Go through $attrarr, and save the allowed attributes for this element |
1126 |
// in $attr2 |
|
0 | 1127 |
$attr2 = ''; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1128 |
foreach ( $attrarr as $arreach ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1129 |
if ( wp_kses_attr_check( $arreach['name'], $arreach['value'], $arreach['whole'], $arreach['vless'], $element, $allowed_html ) ) { |
9 | 1130 |
$attr2 .= ' ' . $arreach['whole']; |
0 | 1131 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1132 |
} |
0 | 1133 |
|
5 | 1134 |
// Remove any "<" or ">" characters |
9 | 1135 |
$attr2 = preg_replace( '/[<>]/', '', $attr2 ); |
0 | 1136 |
|
1137 |
return "<$element$attr2$xhtml_slash>"; |
|
1138 |
} |
|
1139 |
||
1140 |
/** |
|
9 | 1141 |
* Determines whether an attribute is allowed. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1142 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1143 |
* @since 4.2.3 |
9 | 1144 |
* @since 5.0.0 Add support for `data-*` wildcard attributes. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1145 |
* |
9 | 1146 |
* @param string $name The attribute name. Passed by reference. Returns empty string when not allowed. |
1147 |
* @param string $value The attribute value. Passed by reference. Returns a filtered value. |
|
1148 |
* @param string $whole The `name=value` input. Passed by reference. Returns filtered input. |
|
1149 |
* @param string $vless Whether the attribute is valueless. Use 'y' or 'n'. |
|
1150 |
* @param string $element The name of the element to which this attribute belongs. |
|
1151 |
* @param array $allowed_html The full list of allowed elements and attributes. |
|
1152 |
* @return bool Whether or not the attribute is allowed. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1153 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1154 |
function wp_kses_attr_check( &$name, &$value, &$whole, $vless, $element, $allowed_html ) { |
9 | 1155 |
$allowed_attr = $allowed_html[ strtolower( $element ) ]; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1156 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1157 |
$name_low = strtolower( $name ); |
9 | 1158 |
if ( ! isset( $allowed_attr[ $name_low ] ) || '' == $allowed_attr[ $name_low ] ) { |
1159 |
/* |
|
1160 |
* Allow `data-*` attributes. |
|
1161 |
* |
|
1162 |
* When specifying `$allowed_html`, the attribute name should be set as |
|
1163 |
* `data-*` (not to be mixed with the HTML 4.0 `data` attribute, see |
|
1164 |
* https://www.w3.org/TR/html40/struct/objects.html#adef-data). |
|
1165 |
* |
|
1166 |
* Note: the attribute name should only contain `A-Za-z0-9_-` chars, |
|
1167 |
* double hyphens `--` are not accepted by WordPress. |
|
1168 |
*/ |
|
1169 |
if ( strpos( $name_low, 'data-' ) === 0 && ! empty( $allowed_attr['data-*'] ) && preg_match( '/^data(?:-[a-z0-9_]+)+$/', $name_low, $match ) ) { |
|
1170 |
/* |
|
1171 |
* Add the whole attribute name to the allowed attributes and set any restrictions |
|
1172 |
* for the `data-*` attribute values for the current element. |
|
1173 |
*/ |
|
1174 |
$allowed_attr[ $match[0] ] = $allowed_attr['data-*']; |
|
1175 |
} else { |
|
1176 |
$name = $value = $whole = ''; |
|
1177 |
return false; |
|
1178 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1179 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1180 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1181 |
if ( 'style' == $name_low ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1182 |
$new_value = safecss_filter_attr( $value ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1183 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1184 |
if ( empty( $new_value ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1185 |
$name = $value = $whole = ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1186 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1187 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1188 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1189 |
$whole = str_replace( $value, $new_value, $whole ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1190 |
$value = $new_value; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1191 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1192 |
|
9 | 1193 |
if ( is_array( $allowed_attr[ $name_low ] ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1194 |
// there are some checks |
9 | 1195 |
foreach ( $allowed_attr[ $name_low ] as $currkey => $currval ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1196 |
if ( ! wp_kses_check_attr_val( $value, $vless, $currkey, $currval ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1197 |
$name = $value = $whole = ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1198 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1199 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1200 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1201 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1202 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1203 |
return true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1204 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1205 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1206 |
/** |
0 | 1207 |
* Builds an attribute list from string containing attributes. |
1208 |
* |
|
1209 |
* This function does a lot of work. It parses an attribute list into an array |
|
1210 |
* with attribute data, and tries to do the right thing even if it gets weird |
|
1211 |
* input. It will add quotes around attribute values that don't have any quotes |
|
1212 |
* or apostrophes around them, to make it easier to produce HTML code that will |
|
1213 |
* conform to W3C's HTML specification. It will also remove bad URL protocols |
|
1214 |
* from attribute values. It also reduces duplicate attributes by using the |
|
9 | 1215 |
* attribute defined first (`foo='bar' foo='baz'` will result in `foo='bar'`). |
0 | 1216 |
* |
1217 |
* @since 1.0.0 |
|
1218 |
* |
|
9 | 1219 |
* @param string $attr Attribute list from HTML element to closing HTML element tag. |
1220 |
* @param string[] $allowed_protocols Array of allowed URL protocols. |
|
1221 |
* @return array[] Array of attribute information after parsing. |
|
0 | 1222 |
*/ |
9 | 1223 |
function wp_kses_hair( $attr, $allowed_protocols ) { |
1224 |
$attrarr = array(); |
|
1225 |
$mode = 0; |
|
0 | 1226 |
$attrname = ''; |
9 | 1227 |
$uris = wp_kses_uri_attributes(); |
0 | 1228 |
|
5 | 1229 |
// Loop through the whole attribute list |
0 | 1230 |
|
9 | 1231 |
while ( strlen( $attr ) != 0 ) { |
5 | 1232 |
$working = 0; // Was the last operation successful? |
0 | 1233 |
|
9 | 1234 |
switch ( $mode ) { |
1235 |
case 0: |
|
1236 |
if ( preg_match( '/^([-a-zA-Z:]+)/', $attr, $match ) ) { |
|
1237 |
$attrname = $match[1]; |
|
1238 |
$working = $mode = 1; |
|
1239 |
$attr = preg_replace( '/^[-a-zA-Z:]+/', '', $attr ); |
|
1240 |
} |
|
1241 |
||
1242 |
break; |
|
0 | 1243 |
|
9 | 1244 |
case 1: |
1245 |
if ( preg_match( '/^\s*=\s*/', $attr ) ) { // equals sign |
|
1246 |
$working = 1; |
|
1247 |
$mode = 2; |
|
1248 |
$attr = preg_replace( '/^\s*=\s*/', '', $attr ); |
|
1249 |
break; |
|
1250 |
} |
|
1251 |
||
1252 |
if ( preg_match( '/^\s+/', $attr ) ) { // valueless |
|
1253 |
$working = 1; |
|
1254 |
$mode = 0; |
|
1255 |
if ( false === array_key_exists( $attrname, $attrarr ) ) { |
|
1256 |
$attrarr[ $attrname ] = array( |
|
1257 |
'name' => $attrname, |
|
1258 |
'value' => '', |
|
1259 |
'whole' => $attrname, |
|
1260 |
'vless' => 'y', |
|
1261 |
); |
|
1262 |
} |
|
1263 |
$attr = preg_replace( '/^\s+/', '', $attr ); |
|
0 | 1264 |
} |
1265 |
||
1266 |
break; |
|
1267 |
||
9 | 1268 |
case 2: |
1269 |
if ( preg_match( '%^"([^"]*)"(\s+|/?$)%', $attr, $match ) ) { |
|
1270 |
// "value" |
|
1271 |
$thisval = $match[1]; |
|
1272 |
if ( in_array( strtolower( $attrname ), $uris ) ) { |
|
1273 |
$thisval = wp_kses_bad_protocol( $thisval, $allowed_protocols ); |
|
1274 |
} |
|
0 | 1275 |
|
9 | 1276 |
if ( false === array_key_exists( $attrname, $attrarr ) ) { |
1277 |
$attrarr[ $attrname ] = array( |
|
1278 |
'name' => $attrname, |
|
1279 |
'value' => $thisval, |
|
1280 |
'whole' => "$attrname=\"$thisval\"", |
|
1281 |
'vless' => 'n', |
|
1282 |
); |
|
1283 |
} |
|
0 | 1284 |
$working = 1; |
9 | 1285 |
$mode = 0; |
1286 |
$attr = preg_replace( '/^"[^"]*"(\s+|$)/', '', $attr ); |
|
0 | 1287 |
break; |
1288 |
} |
|
1289 |
||
9 | 1290 |
if ( preg_match( "%^'([^']*)'(\s+|/?$)%", $attr, $match ) ) { |
1291 |
// 'value' |
|
1292 |
$thisval = $match[1]; |
|
1293 |
if ( in_array( strtolower( $attrname ), $uris ) ) { |
|
1294 |
$thisval = wp_kses_bad_protocol( $thisval, $allowed_protocols ); |
|
0 | 1295 |
} |
1296 |
||
9 | 1297 |
if ( false === array_key_exists( $attrname, $attrarr ) ) { |
1298 |
$attrarr[ $attrname ] = array( |
|
1299 |
'name' => $attrname, |
|
1300 |
'value' => $thisval, |
|
1301 |
'whole' => "$attrname='$thisval'", |
|
1302 |
'vless' => 'n', |
|
1303 |
); |
|
0 | 1304 |
} |
1305 |
$working = 1; |
|
9 | 1306 |
$mode = 0; |
1307 |
$attr = preg_replace( "/^'[^']*'(\s+|$)/", '', $attr ); |
|
0 | 1308 |
break; |
1309 |
} |
|
1310 |
||
9 | 1311 |
if ( preg_match( "%^([^\s\"']+)(\s+|/?$)%", $attr, $match ) ) { |
1312 |
// value |
|
0 | 1313 |
$thisval = $match[1]; |
9 | 1314 |
if ( in_array( strtolower( $attrname ), $uris ) ) { |
1315 |
$thisval = wp_kses_bad_protocol( $thisval, $allowed_protocols ); |
|
0 | 1316 |
} |
1317 |
||
9 | 1318 |
if ( false === array_key_exists( $attrname, $attrarr ) ) { |
1319 |
$attrarr[ $attrname ] = array( |
|
1320 |
'name' => $attrname, |
|
1321 |
'value' => $thisval, |
|
1322 |
'whole' => "$attrname=\"$thisval\"", |
|
1323 |
'vless' => 'n', |
|
1324 |
); |
|
0 | 1325 |
} |
5 | 1326 |
// We add quotes to conform to W3C's HTML spec. |
0 | 1327 |
$working = 1; |
9 | 1328 |
$mode = 0; |
1329 |
$attr = preg_replace( "%^[^\s\"']+(\s+|$)%", '', $attr ); |
|
0 | 1330 |
} |
1331 |
||
1332 |
break; |
|
5 | 1333 |
} // switch |
0 | 1334 |
|
9 | 1335 |
if ( $working == 0 ) { // not well formed, remove and try again |
1336 |
$attr = wp_kses_html_error( $attr ); |
|
0 | 1337 |
$mode = 0; |
1338 |
} |
|
5 | 1339 |
} // while |
0 | 1340 |
|
9 | 1341 |
if ( $mode == 1 && false === array_key_exists( $attrname, $attrarr ) ) { |
5 | 1342 |
// special case, for when the attribute list ends with a valueless |
1343 |
// attribute like "selected" |
|
9 | 1344 |
$attrarr[ $attrname ] = array( |
1345 |
'name' => $attrname, |
|
1346 |
'value' => '', |
|
1347 |
'whole' => $attrname, |
|
1348 |
'vless' => 'y', |
|
1349 |
); |
|
1350 |
} |
|
0 | 1351 |
|
1352 |
return $attrarr; |
|
1353 |
} |
|
1354 |
||
1355 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1356 |
* Finds all attributes of an HTML element. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1357 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1358 |
* Does not modify input. May return "evil" output. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1359 |
* |
9 | 1360 |
* Based on `wp_kses_split2()` and `wp_kses_attr()`. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1361 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1362 |
* @since 4.2.3 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1363 |
* |
9 | 1364 |
* @param string $element HTML element. |
1365 |
* @return array|bool List of attributes found in the element. Returns false on failure. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1366 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1367 |
function wp_kses_attr_parse( $element ) { |
9 | 1368 |
$valid = preg_match( '%^(<\s*)(/\s*)?([a-zA-Z0-9]+\s*)([^>]*)(>?)$%', $element, $matches ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1369 |
if ( 1 !== $valid ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1370 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1371 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1372 |
|
9 | 1373 |
$begin = $matches[1]; |
1374 |
$slash = $matches[2]; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1375 |
$elname = $matches[3]; |
9 | 1376 |
$attr = $matches[4]; |
1377 |
$end = $matches[5]; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1378 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1379 |
if ( '' !== $slash ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1380 |
// Closing elements do not get parsed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1381 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1382 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1383 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1384 |
// Is there a closing XHTML slash at the end of the attributes? |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1385 |
if ( 1 === preg_match( '%\s*/\s*$%', $attr, $matches ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1386 |
$xhtml_slash = $matches[0]; |
9 | 1387 |
$attr = substr( $attr, 0, -strlen( $xhtml_slash ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1388 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1389 |
$xhtml_slash = ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1390 |
} |
9 | 1391 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1392 |
// Split it |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1393 |
$attrarr = wp_kses_hair_parse( $attr ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1394 |
if ( false === $attrarr ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1395 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1396 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1397 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1398 |
// Make sure all input is returned by adding front and back matter. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1399 |
array_unshift( $attrarr, $begin . $slash . $elname ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1400 |
array_push( $attrarr, $xhtml_slash . $end ); |
9 | 1401 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1402 |
return $attrarr; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1403 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1404 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1405 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1406 |
* Builds an attribute list from string containing attributes. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1407 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1408 |
* Does not modify input. May return "evil" output. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1409 |
* In case of unexpected input, returns false instead of stripping things. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1410 |
* |
9 | 1411 |
* Based on `wp_kses_hair()` but does not return a multi-dimensional array. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1412 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1413 |
* @since 4.2.3 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1414 |
* |
9 | 1415 |
* @param string $attr Attribute list from HTML element to closing HTML element tag. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1416 |
* @return array|bool List of attributes found in $attr. Returns false on failure. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1417 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1418 |
function wp_kses_hair_parse( $attr ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1419 |
if ( '' === $attr ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1420 |
return array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1421 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1422 |
|
9 | 1423 |
// phpcs:disable Squiz.Strings.ConcatenationSpacing.PaddingFound -- don't remove regex indentation |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1424 |
$regex = |
9 | 1425 |
'(?:' |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1426 |
. '[-a-zA-Z:]+' // Attribute name. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1427 |
. '|' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1428 |
. '\[\[?[^\[\]]+\]\]?' // Shortcode in the name position implies unfiltered_html. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1429 |
. ')' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1430 |
. '(?:' // Attribute value. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1431 |
. '\s*=\s*' // All values begin with '=' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1432 |
. '(?:' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1433 |
. '"[^"]*"' // Double-quoted |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1434 |
. '|' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1435 |
. "'[^']*'" // Single-quoted |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1436 |
. '|' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1437 |
. '[^\s"\']+' // Non-quoted |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1438 |
. '(?:\s|$)' // Must have a space |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1439 |
. ')' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1440 |
. '|' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1441 |
. '(?:\s|$)' // If attribute has no value, space is required. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1442 |
. ')' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1443 |
. '\s*'; // Trailing space is optional except as mentioned above. |
9 | 1444 |
// phpcs:enable |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1445 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1446 |
// Although it is possible to reduce this procedure to a single regexp, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1447 |
// we must run that regexp twice to get exactly the expected result. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1448 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1449 |
$validation = "%^($regex)+$%"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1450 |
$extraction = "%$regex%"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1451 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1452 |
if ( 1 === preg_match( $validation, $attr ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1453 |
preg_match_all( $extraction, $attr, $attrarr ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1454 |
return $attrarr[0]; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1455 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1456 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1457 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1458 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1459 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1460 |
/** |
0 | 1461 |
* Performs different checks for attribute values. |
1462 |
* |
|
9 | 1463 |
* The currently implemented checks are "maxlen", "minlen", "maxval", "minval", |
0 | 1464 |
* and "valueless". |
1465 |
* |
|
1466 |
* @since 1.0.0 |
|
1467 |
* |
|
9 | 1468 |
* @param string $value Attribute value. |
1469 |
* @param string $vless Whether the attribute is valueless. Use 'y' or 'n'. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1470 |
* @param string $checkname What $checkvalue is checking for. |
9 | 1471 |
* @param mixed $checkvalue What constraint the value should pass. |
1472 |
* @return bool Whether check passes. |
|
0 | 1473 |
*/ |
9 | 1474 |
function wp_kses_check_attr_val( $value, $vless, $checkname, $checkvalue ) { |
0 | 1475 |
$ok = true; |
1476 |
||
9 | 1477 |
switch ( strtolower( $checkname ) ) { |
1478 |
case 'maxlen': |
|
5 | 1479 |
// The maxlen check makes sure that the attribute value has a length not |
1480 |
// greater than the given value. This can be used to avoid Buffer Overflows |
|
1481 |
// in WWW clients and various Internet servers. |
|
0 | 1482 |
|
9 | 1483 |
if ( strlen( $value ) > $checkvalue ) { |
0 | 1484 |
$ok = false; |
9 | 1485 |
} |
0 | 1486 |
break; |
1487 |
||
9 | 1488 |
case 'minlen': |
5 | 1489 |
// The minlen check makes sure that the attribute value has a length not |
1490 |
// smaller than the given value. |
|
0 | 1491 |
|
9 | 1492 |
if ( strlen( $value ) < $checkvalue ) { |
0 | 1493 |
$ok = false; |
9 | 1494 |
} |
0 | 1495 |
break; |
1496 |
||
9 | 1497 |
case 'maxval': |
5 | 1498 |
// The maxval check does two things: it checks that the attribute value is |
1499 |
// an integer from 0 and up, without an excessive amount of zeroes or |
|
1500 |
// whitespace (to avoid Buffer Overflows). It also checks that the attribute |
|
1501 |
// value is not greater than the given value. |
|
1502 |
// This check can be used to avoid Denial of Service attacks. |
|
0 | 1503 |
|
9 | 1504 |
if ( ! preg_match( '/^\s{0,6}[0-9]{1,6}\s{0,6}$/', $value ) ) { |
0 | 1505 |
$ok = false; |
9 | 1506 |
} |
1507 |
if ( $value > $checkvalue ) { |
|
0 | 1508 |
$ok = false; |
9 | 1509 |
} |
0 | 1510 |
break; |
1511 |
||
9 | 1512 |
case 'minval': |
5 | 1513 |
// The minval check makes sure that the attribute value is a positive integer, |
1514 |
// and that it is not smaller than the given value. |
|
0 | 1515 |
|
9 | 1516 |
if ( ! preg_match( '/^\s{0,6}[0-9]{1,6}\s{0,6}$/', $value ) ) { |
0 | 1517 |
$ok = false; |
9 | 1518 |
} |
1519 |
if ( $value < $checkvalue ) { |
|
0 | 1520 |
$ok = false; |
9 | 1521 |
} |
0 | 1522 |
break; |
1523 |
||
9 | 1524 |
case 'valueless': |
5 | 1525 |
// The valueless check makes sure if the attribute has a value |
9 | 1526 |
// (like `<a href="blah">`) or not (`<option selected>`). If the given value |
5 | 1527 |
// is a "y" or a "Y", the attribute must not have a value. |
9 | 1528 |
// If the given value is an "n" or an "N", the attribute must have a value. |
0 | 1529 |
|
9 | 1530 |
if ( strtolower( $checkvalue ) != $vless ) { |
0 | 1531 |
$ok = false; |
9 | 1532 |
} |
0 | 1533 |
break; |
5 | 1534 |
} // switch |
0 | 1535 |
|
1536 |
return $ok; |
|
1537 |
} |
|
1538 |
||
1539 |
/** |
|
9 | 1540 |
* Sanitizes a string and removed disallowed URL protocols. |
0 | 1541 |
* |
9 | 1542 |
* This function removes all non-allowed protocols from the beginning of the |
1543 |
* string. It ignores whitespace and the case of the letters, and it does |
|
1544 |
* understand HTML entities. It does its work recursively, so it won't be |
|
1545 |
* fooled by a string like `javascript:javascript:alert(57)`. |
|
0 | 1546 |
* |
1547 |
* @since 1.0.0 |
|
1548 |
* |
|
9 | 1549 |
* @param string $string Content to filter bad protocols from. |
1550 |
* @param string[] $allowed_protocols Array of allowed URL protocols. |
|
1551 |
* @return string Filtered content. |
|
0 | 1552 |
*/ |
9 | 1553 |
function wp_kses_bad_protocol( $string, $allowed_protocols ) { |
1554 |
$string = wp_kses_no_null( $string ); |
|
0 | 1555 |
$iterations = 0; |
1556 |
||
1557 |
do { |
|
1558 |
$original_string = $string; |
|
9 | 1559 |
$string = wp_kses_bad_protocol_once( $string, $allowed_protocols ); |
0 | 1560 |
} while ( $original_string != $string && ++$iterations < 6 ); |
1561 |
||
9 | 1562 |
if ( $original_string != $string ) { |
0 | 1563 |
return ''; |
9 | 1564 |
} |
0 | 1565 |
|
1566 |
return $string; |
|
1567 |
} |
|
1568 |
||
1569 |
/** |
|
9 | 1570 |
* Removes any invalid control characters in a text string. |
5 | 1571 |
* |
9 | 1572 |
* Also removes any instance of the `\0` string. |
0 | 1573 |
* |
1574 |
* @since 1.0.0 |
|
1575 |
* |
|
9 | 1576 |
* @param string $string Content to filter null characters from. |
1577 |
* @param array $options Set 'slash_zero' => 'keep' when '\0' is allowed. Default is 'remove'. |
|
1578 |
* @return string Filtered content. |
|
0 | 1579 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1580 |
function wp_kses_no_null( $string, $options = null ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1581 |
if ( ! isset( $options['slash_zero'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1582 |
$options = array( 'slash_zero' => 'remove' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1583 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1584 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1585 |
$string = preg_replace( '/[\x00-\x08\x0B\x0C\x0E-\x1F]/', '', $string ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1586 |
if ( 'remove' == $options['slash_zero'] ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1587 |
$string = preg_replace( '/\\\\+0+/', '', $string ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1588 |
} |
0 | 1589 |
|
1590 |
return $string; |
|
1591 |
} |
|
1592 |
||
1593 |
/** |
|
1594 |
* Strips slashes from in front of quotes. |
|
1595 |
* |
|
9 | 1596 |
* This function changes the character sequence `\"` to just `"`. It leaves all other |
1597 |
* slashes alone. The quoting from `preg_replace(//e)` requires this. |
|
0 | 1598 |
* |
1599 |
* @since 1.0.0 |
|
1600 |
* |
|
9 | 1601 |
* @param string $string String to strip slashes from. |
1602 |
* @return string Fixed string with quoted slashes. |
|
0 | 1603 |
*/ |
9 | 1604 |
function wp_kses_stripslashes( $string ) { |
1605 |
return preg_replace( '%\\\\"%', '"', $string ); |
|
0 | 1606 |
} |
1607 |
||
1608 |
/** |
|
9 | 1609 |
* Converts the keys of an array to lowercase. |
0 | 1610 |
* |
1611 |
* @since 1.0.0 |
|
1612 |
* |
|
9 | 1613 |
* @param array $inarray Unfiltered array. |
1614 |
* @return array Fixed array with all lowercase keys. |
|
0 | 1615 |
*/ |
9 | 1616 |
function wp_kses_array_lc( $inarray ) { |
1617 |
$outarray = array(); |
|
0 | 1618 |
|
9 | 1619 |
foreach ( (array) $inarray as $inkey => $inval ) { |
1620 |
$outkey = strtolower( $inkey ); |
|
1621 |
$outarray[ $outkey ] = array(); |
|
0 | 1622 |
|
9 | 1623 |
foreach ( (array) $inval as $inkey2 => $inval2 ) { |
1624 |
$outkey2 = strtolower( $inkey2 ); |
|
1625 |
$outarray[ $outkey ][ $outkey2 ] = $inval2; |
|
1626 |
} |
|
1627 |
} |
|
0 | 1628 |
|
1629 |
return $outarray; |
|
1630 |
} |
|
1631 |
||
1632 |
/** |
|
9 | 1633 |
* Handles parsing errors in `wp_kses_hair()`. |
0 | 1634 |
* |
1635 |
* The general plan is to remove everything to and including some whitespace, |
|
1636 |
* but it deals with quotes and apostrophes as well. |
|
1637 |
* |
|
1638 |
* @since 1.0.0 |
|
1639 |
* |
|
1640 |
* @param string $string |
|
1641 |
* @return string |
|
1642 |
*/ |
|
9 | 1643 |
function wp_kses_html_error( $string ) { |
1644 |
return preg_replace( '/^("[^"]*("|$)|\'[^\']*(\'|$)|\S)*\s*/', '', $string ); |
|
0 | 1645 |
} |
1646 |
||
1647 |
/** |
|
1648 |
* Sanitizes content from bad protocols and other characters. |
|
1649 |
* |
|
9 | 1650 |
* This function searches for URL protocols at the beginning of the string, while |
0 | 1651 |
* handling whitespace and HTML entities. |
1652 |
* |
|
1653 |
* @since 1.0.0 |
|
1654 |
* |
|
9 | 1655 |
* @param string $string Content to check for bad protocols. |
1656 |
* @param string[] $allowed_protocols Array of allowed URL protocols. |
|
1657 |
* @return string Sanitized content. |
|
0 | 1658 |
*/ |
9 | 1659 |
function wp_kses_bad_protocol_once( $string, $allowed_protocols, $count = 1 ) { |
1660 |
$string = preg_replace( '/(�*58(?![;0-9])|�*3a(?![;a-f0-9]))/i', '$1;', $string ); |
|
0 | 1661 |
$string2 = preg_split( '/:|�*58;|�*3a;/i', $string, 2 ); |
9 | 1662 |
if ( isset( $string2[1] ) && ! preg_match( '%/\?%', $string2[0] ) ) { |
1663 |
$string = trim( $string2[1] ); |
|
0 | 1664 |
$protocol = wp_kses_bad_protocol_once2( $string2[0], $allowed_protocols ); |
1665 |
if ( 'feed:' == $protocol ) { |
|
9 | 1666 |
if ( $count > 2 ) { |
0 | 1667 |
return ''; |
9 | 1668 |
} |
0 | 1669 |
$string = wp_kses_bad_protocol_once( $string, $allowed_protocols, ++$count ); |
9 | 1670 |
if ( empty( $string ) ) { |
0 | 1671 |
return $string; |
9 | 1672 |
} |
0 | 1673 |
} |
1674 |
$string = $protocol . $string; |
|
1675 |
} |
|
1676 |
||
1677 |
return $string; |
|
1678 |
} |
|
1679 |
||
1680 |
/** |
|
9 | 1681 |
* Callback for `wp_kses_bad_protocol_once()` regular expression. |
0 | 1682 |
* |
1683 |
* This function processes URL protocols, checks to see if they're in the |
|
1684 |
* whitelist or not, and returns different data depending on the answer. |
|
1685 |
* |
|
1686 |
* @access private |
|
9 | 1687 |
* @ignore |
0 | 1688 |
* @since 1.0.0 |
1689 |
* |
|
9 | 1690 |
* @param string $string URI scheme to check against the whitelist. |
1691 |
* @param string[] $allowed_protocols Array of allowed URL protocols. |
|
1692 |
* @return string Sanitized content. |
|
0 | 1693 |
*/ |
1694 |
function wp_kses_bad_protocol_once2( $string, $allowed_protocols ) { |
|
9 | 1695 |
$string2 = wp_kses_decode_entities( $string ); |
1696 |
$string2 = preg_replace( '/\s/', '', $string2 ); |
|
1697 |
$string2 = wp_kses_no_null( $string2 ); |
|
1698 |
$string2 = strtolower( $string2 ); |
|
0 | 1699 |
|
1700 |
$allowed = false; |
|
9 | 1701 |
foreach ( (array) $allowed_protocols as $one_protocol ) { |
1702 |
if ( strtolower( $one_protocol ) == $string2 ) { |
|
0 | 1703 |
$allowed = true; |
1704 |
break; |
|
1705 |
} |
|
9 | 1706 |
} |
0 | 1707 |
|
9 | 1708 |
if ( $allowed ) { |
0 | 1709 |
return "$string2:"; |
9 | 1710 |
} else { |
0 | 1711 |
return ''; |
9 | 1712 |
} |
0 | 1713 |
} |
1714 |
||
1715 |
/** |
|
1716 |
* Converts and fixes HTML entities. |
|
1717 |
* |
|
5 | 1718 |
* This function normalizes HTML entities. It will convert `AT&T` to the correct |
1719 |
* `AT&T`, `:` to `:`, `&#XYZZY;` to `&#XYZZY;` and so on. |
|
0 | 1720 |
* |
1721 |
* @since 1.0.0 |
|
1722 |
* |
|
9 | 1723 |
* @param string $string Content to normalize entities. |
1724 |
* @return string Content with normalized entities. |
|
0 | 1725 |
*/ |
9 | 1726 |
function wp_kses_normalize_entities( $string ) { |
5 | 1727 |
// Disarm all entities by converting & to & |
9 | 1728 |
$string = str_replace( '&', '&', $string ); |
0 | 1729 |
|
5 | 1730 |
// Change back the allowed entities in our entity whitelist |
9 | 1731 |
$string = preg_replace_callback( '/&([A-Za-z]{2,8}[0-9]{0,2});/', 'wp_kses_named_entities', $string ); |
1732 |
$string = preg_replace_callback( '/&#(0*[0-9]{1,7});/', 'wp_kses_normalize_entities2', $string ); |
|
1733 |
$string = preg_replace_callback( '/&#[Xx](0*[0-9A-Fa-f]{1,6});/', 'wp_kses_normalize_entities3', $string ); |
|
0 | 1734 |
|
1735 |
return $string; |
|
1736 |
} |
|
1737 |
||
1738 |
/** |
|
9 | 1739 |
* Callback for `wp_kses_normalize_entities()` regular expression. |
0 | 1740 |
* |
1741 |
* This function only accepts valid named entity references, which are finite, |
|
1742 |
* case-sensitive, and highly scrutinized by HTML and XML validators. |
|
1743 |
* |
|
1744 |
* @since 3.0.0 |
|
1745 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1746 |
* @global array $allowedentitynames |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1747 |
* |
9 | 1748 |
* @param array $matches preg_replace_callback() matches array. |
1749 |
* @return string Correctly encoded entity. |
|
0 | 1750 |
*/ |
9 | 1751 |
function wp_kses_named_entities( $matches ) { |
0 | 1752 |
global $allowedentitynames; |
1753 |
||
9 | 1754 |
if ( empty( $matches[1] ) ) { |
0 | 1755 |
return ''; |
9 | 1756 |
} |
0 | 1757 |
|
1758 |
$i = $matches[1]; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1759 |
return ( ! in_array( $i, $allowedentitynames ) ) ? "&$i;" : "&$i;"; |
0 | 1760 |
} |
1761 |
||
1762 |
/** |
|
9 | 1763 |
* Callback for `wp_kses_normalize_entities()` regular expression. |
0 | 1764 |
* |
9 | 1765 |
* This function helps `wp_kses_normalize_entities()` to only accept 16-bit |
5 | 1766 |
* values and nothing more for `&#number;` entities. |
0 | 1767 |
* |
1768 |
* @access private |
|
9 | 1769 |
* @ignore |
0 | 1770 |
* @since 1.0.0 |
1771 |
* |
|
9 | 1772 |
* @param array $matches `preg_replace_callback()` matches array. |
1773 |
* @return string Correctly encoded entity. |
|
0 | 1774 |
*/ |
9 | 1775 |
function wp_kses_normalize_entities2( $matches ) { |
1776 |
if ( empty( $matches[1] ) ) { |
|
0 | 1777 |
return ''; |
9 | 1778 |
} |
0 | 1779 |
|
1780 |
$i = $matches[1]; |
|
9 | 1781 |
if ( valid_unicode( $i ) ) { |
1782 |
$i = str_pad( ltrim( $i, '0' ), 3, '0', STR_PAD_LEFT ); |
|
0 | 1783 |
$i = "&#$i;"; |
1784 |
} else { |
|
1785 |
$i = "&#$i;"; |
|
1786 |
} |
|
1787 |
||
1788 |
return $i; |
|
1789 |
} |
|
1790 |
||
1791 |
/** |
|
9 | 1792 |
* Callback for `wp_kses_normalize_entities()` for regular expression. |
0 | 1793 |
* |
9 | 1794 |
* This function helps `wp_kses_normalize_entities()` to only accept valid Unicode |
0 | 1795 |
* numeric entities in hex form. |
1796 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1797 |
* @since 2.7.0 |
0 | 1798 |
* @access private |
9 | 1799 |
* @ignore |
0 | 1800 |
* |
9 | 1801 |
* @param array $matches `preg_replace_callback()` matches array. |
1802 |
* @return string Correctly encoded entity. |
|
0 | 1803 |
*/ |
9 | 1804 |
function wp_kses_normalize_entities3( $matches ) { |
1805 |
if ( empty( $matches[1] ) ) { |
|
0 | 1806 |
return ''; |
9 | 1807 |
} |
0 | 1808 |
|
1809 |
$hexchars = $matches[1]; |
|
9 | 1810 |
return ( ! valid_unicode( hexdec( $hexchars ) ) ) ? "&#x$hexchars;" : '&#x' . ltrim( $hexchars, '0' ) . ';'; |
0 | 1811 |
} |
1812 |
||
1813 |
/** |
|
9 | 1814 |
* Determines if a Unicode codepoint is valid. |
0 | 1815 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1816 |
* @since 2.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1817 |
* |
9 | 1818 |
* @param int $i Unicode codepoint. |
1819 |
* @return bool Whether or not the codepoint is a valid Unicode codepoint. |
|
0 | 1820 |
*/ |
9 | 1821 |
function valid_unicode( $i ) { |
0 | 1822 |
return ( $i == 0x9 || $i == 0xa || $i == 0xd || |
9 | 1823 |
( $i >= 0x20 && $i <= 0xd7ff ) || |
1824 |
( $i >= 0xe000 && $i <= 0xfffd ) || |
|
1825 |
( $i >= 0x10000 && $i <= 0x10ffff ) ); |
|
0 | 1826 |
} |
1827 |
||
1828 |
/** |
|
9 | 1829 |
* Converts all numeric HTML entities to their named counterparts. |
0 | 1830 |
* |
5 | 1831 |
* This function decodes numeric HTML entities (`A` and `A`). |
9 | 1832 |
* It doesn't do anything with named entities like `ä`, but we don't |
5 | 1833 |
* need them in the URL protocol whitelisting system anyway. |
0 | 1834 |
* |
1835 |
* @since 1.0.0 |
|
1836 |
* |
|
9 | 1837 |
* @param string $string Content to change entities. |
1838 |
* @return string Content after decoded entities. |
|
0 | 1839 |
*/ |
9 | 1840 |
function wp_kses_decode_entities( $string ) { |
1841 |
$string = preg_replace_callback( '/&#([0-9]+);/', '_wp_kses_decode_entities_chr', $string ); |
|
1842 |
$string = preg_replace_callback( '/&#[Xx]([0-9A-Fa-f]+);/', '_wp_kses_decode_entities_chr_hexdec', $string ); |
|
0 | 1843 |
|
1844 |
return $string; |
|
1845 |
} |
|
1846 |
||
1847 |
/** |
|
9 | 1848 |
* Regex callback for `wp_kses_decode_entities()`. |
0 | 1849 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1850 |
* @since 2.9.0 |
9 | 1851 |
* @access private |
1852 |
* @ignore |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1853 |
* |
0 | 1854 |
* @param array $match preg match |
1855 |
* @return string |
|
1856 |
*/ |
|
1857 |
function _wp_kses_decode_entities_chr( $match ) { |
|
1858 |
return chr( $match[1] ); |
|
1859 |
} |
|
1860 |
||
1861 |
/** |
|
9 | 1862 |
* Regex callback for `wp_kses_decode_entities()`. |
0 | 1863 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1864 |
* @since 2.9.0 |
9 | 1865 |
* @access private |
1866 |
* @ignore |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1867 |
* |
0 | 1868 |
* @param array $match preg match |
1869 |
* @return string |
|
1870 |
*/ |
|
1871 |
function _wp_kses_decode_entities_chr_hexdec( $match ) { |
|
1872 |
return chr( hexdec( $match[1] ) ); |
|
1873 |
} |
|
1874 |
||
1875 |
/** |
|
9 | 1876 |
* Sanitize content with allowed HTML KSES rules. |
1877 |
* |
|
1878 |
* This function expects slashed data. |
|
0 | 1879 |
* |
1880 |
* @since 1.0.0 |
|
1881 |
* |
|
9 | 1882 |
* @param string $data Content to filter, expected to be escaped with slashes. |
1883 |
* @return string Filtered content. |
|
0 | 1884 |
*/ |
1885 |
function wp_filter_kses( $data ) { |
|
1886 |
return addslashes( wp_kses( stripslashes( $data ), current_filter() ) ); |
|
1887 |
} |
|
1888 |
||
1889 |
/** |
|
9 | 1890 |
* Sanitize content with allowed HTML KSES rules. |
1891 |
* |
|
1892 |
* This function expects unslashed data. |
|
0 | 1893 |
* |
1894 |
* @since 2.9.0 |
|
1895 |
* |
|
9 | 1896 |
* @param string $data Content to filter, expected to not be escaped. |
1897 |
* @return string Filtered content. |
|
0 | 1898 |
*/ |
1899 |
function wp_kses_data( $data ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1900 |
return wp_kses( $data, current_filter() ); |
0 | 1901 |
} |
1902 |
||
1903 |
/** |
|
9 | 1904 |
* Sanitizes content for allowed HTML tags for post content. |
0 | 1905 |
* |
9 | 1906 |
* Post content refers to the page contents of the 'post' type and not `$_POST` |
0 | 1907 |
* data from forms. |
1908 |
* |
|
9 | 1909 |
* This function expects slashed data. |
1910 |
* |
|
0 | 1911 |
* @since 2.0.0 |
1912 |
* |
|
9 | 1913 |
* @param string $data Post content to filter, expected to be escaped with slashes. |
0 | 1914 |
* @return string Filtered post content with allowed HTML tags and attributes intact. |
1915 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1916 |
function wp_filter_post_kses( $data ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1917 |
return addslashes( wp_kses( stripslashes( $data ), 'post' ) ); |
0 | 1918 |
} |
1919 |
||
1920 |
/** |
|
9 | 1921 |
* Sanitizes content for allowed HTML tags for post content. |
0 | 1922 |
* |
9 | 1923 |
* Post content refers to the page contents of the 'post' type and not `$_POST` |
0 | 1924 |
* data from forms. |
1925 |
* |
|
9 | 1926 |
* This function expects unslashed data. |
1927 |
* |
|
0 | 1928 |
* @since 2.9.0 |
1929 |
* |
|
9 | 1930 |
* @param string $data Post content to filter. |
0 | 1931 |
* @return string Filtered post content with allowed HTML tags and attributes intact. |
1932 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1933 |
function wp_kses_post( $data ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1934 |
return wp_kses( $data, 'post' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1935 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1936 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1937 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1938 |
* Navigates through an array, object, or scalar, and sanitizes content for |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1939 |
* allowed HTML tags for post content. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1940 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1941 |
* @since 4.4.2 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1942 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1943 |
* @see map_deep() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1944 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1945 |
* @param mixed $data The array, object, or scalar value to inspect. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1946 |
* @return mixed The filtered content. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1947 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1948 |
function wp_kses_post_deep( $data ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1949 |
return map_deep( $data, 'wp_kses_post' ); |
0 | 1950 |
} |
1951 |
||
1952 |
/** |
|
9 | 1953 |
* Strips all HTML from a text string. |
1954 |
* |
|
1955 |
* This function expects slashed data. |
|
0 | 1956 |
* |
1957 |
* @since 2.1.0 |
|
1958 |
* |
|
9 | 1959 |
* @param string $data Content to strip all HTML from. |
1960 |
* @return string Filtered content without any HTML. |
|
0 | 1961 |
*/ |
1962 |
function wp_filter_nohtml_kses( $data ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1963 |
return addslashes( wp_kses( stripslashes( $data ), 'strip' ) ); |
0 | 1964 |
} |
1965 |
||
1966 |
/** |
|
9 | 1967 |
* Adds all KSES input form content filters. |
0 | 1968 |
* |
9 | 1969 |
* All hooks have default priority. The `wp_filter_kses()` function is added to |
0 | 1970 |
* the 'pre_comment_content' and 'title_save_pre' hooks. |
1971 |
* |
|
9 | 1972 |
* The `wp_filter_post_kses()` function is added to the 'content_save_pre', |
0 | 1973 |
* 'excerpt_save_pre', and 'content_filtered_save_pre' hooks. |
1974 |
* |
|
1975 |
* @since 2.0.0 |
|
1976 |
*/ |
|
1977 |
function kses_init_filters() { |
|
1978 |
// Normal filtering |
|
9 | 1979 |
add_filter( 'title_save_pre', 'wp_filter_kses' ); |
0 | 1980 |
|
1981 |
// Comment filtering |
|
9 | 1982 |
if ( current_user_can( 'unfiltered_html' ) ) { |
0 | 1983 |
add_filter( 'pre_comment_content', 'wp_filter_post_kses' ); |
9 | 1984 |
} else { |
0 | 1985 |
add_filter( 'pre_comment_content', 'wp_filter_kses' ); |
9 | 1986 |
} |
0 | 1987 |
|
1988 |
// Post filtering |
|
9 | 1989 |
add_filter( 'content_save_pre', 'wp_filter_post_kses' ); |
1990 |
add_filter( 'excerpt_save_pre', 'wp_filter_post_kses' ); |
|
1991 |
add_filter( 'content_filtered_save_pre', 'wp_filter_post_kses' ); |
|
0 | 1992 |
} |
1993 |
||
1994 |
/** |
|
9 | 1995 |
* Removes all KSES input form content filters. |
0 | 1996 |
* |
9 | 1997 |
* A quick procedural method to removing all of the filters that KSES uses for |
0 | 1998 |
* content in WordPress Loop. |
1999 |
* |
|
9 | 2000 |
* Does not remove the `kses_init()` function from {@see 'init'} hook (priority is |
2001 |
* default). Also does not remove `kses_init()` function from {@see 'set_current_user'} |
|
0 | 2002 |
* hook (priority is also default). |
2003 |
* |
|
2004 |
* @since 2.0.6 |
|
2005 |
*/ |
|
2006 |
function kses_remove_filters() { |
|
2007 |
// Normal filtering |
|
9 | 2008 |
remove_filter( 'title_save_pre', 'wp_filter_kses' ); |
0 | 2009 |
|
2010 |
// Comment filtering |
|
2011 |
remove_filter( 'pre_comment_content', 'wp_filter_post_kses' ); |
|
2012 |
remove_filter( 'pre_comment_content', 'wp_filter_kses' ); |
|
2013 |
||
2014 |
// Post filtering |
|
9 | 2015 |
remove_filter( 'content_save_pre', 'wp_filter_post_kses' ); |
2016 |
remove_filter( 'excerpt_save_pre', 'wp_filter_post_kses' ); |
|
2017 |
remove_filter( 'content_filtered_save_pre', 'wp_filter_post_kses' ); |
|
0 | 2018 |
} |
2019 |
||
2020 |
/** |
|
9 | 2021 |
* Sets up most of the KSES filters for input form content. |
0 | 2022 |
* |
9 | 2023 |
* First removes all of the KSES filters in case the current user does not need |
2024 |
* to have KSES filter the content. If the user does not have `unfiltered_html` |
|
2025 |
* capability, then KSES filters are added. |
|
0 | 2026 |
* |
2027 |
* @since 2.0.0 |
|
2028 |
*/ |
|
2029 |
function kses_init() { |
|
2030 |
kses_remove_filters(); |
|
2031 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2032 |
if ( ! current_user_can( 'unfiltered_html' ) ) { |
0 | 2033 |
kses_init_filters(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2034 |
} |
0 | 2035 |
} |
2036 |
||
2037 |
/** |
|
9 | 2038 |
* Filters an inline style attribute and removes disallowed rules. |
0 | 2039 |
* |
2040 |
* @since 2.8.1 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2041 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2042 |
* @param string $css A string of CSS rules. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2043 |
* @param string $deprecated Not used. |
9 | 2044 |
* @return string Filtered string of CSS rules. |
0 | 2045 |
*/ |
2046 |
function safecss_filter_attr( $css, $deprecated = '' ) { |
|
9 | 2047 |
if ( ! empty( $deprecated ) ) { |
0 | 2048 |
_deprecated_argument( __FUNCTION__, '2.8.1' ); // Never implemented |
9 | 2049 |
} |
0 | 2050 |
|
9 | 2051 |
$css = wp_kses_no_null( $css ); |
2052 |
$css = str_replace( array( "\n", "\r", "\t" ), '', $css ); |
|
0 | 2053 |
|
9 | 2054 |
$allowed_protocols = wp_allowed_protocols(); |
0 | 2055 |
|
2056 |
$css_array = explode( ';', trim( $css ) ); |
|
5 | 2057 |
|
2058 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2059 |
* Filters list of allowed CSS attributes. |
5 | 2060 |
* |
2061 |
* @since 2.8.1 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2062 |
* @since 4.4.0 Added support for `min-height`, `max-height`, `min-width`, and `max-width`. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2063 |
* @since 4.6.0 Added support for `list-style-type`. |
9 | 2064 |
* @since 5.0.0 Added support for `background-image`. |
2065 |
* @since 5.1.0 Added support for `text-transform`. |
|
2066 |
* @since 5.2.0 Added support for `background-position` and `grid-template-columns` |
|
5 | 2067 |
* |
9 | 2068 |
* @param string[] $attr Array of allowed CSS attributes. |
5 | 2069 |
*/ |
9 | 2070 |
$allowed_attr = apply_filters( |
2071 |
'safe_style_css', |
|
2072 |
array( |
|
2073 |
'background', |
|
2074 |
'background-color', |
|
2075 |
'background-image', |
|
2076 |
'background-position', |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2077 |
|
9 | 2078 |
'border', |
2079 |
'border-width', |
|
2080 |
'border-color', |
|
2081 |
'border-style', |
|
2082 |
'border-right', |
|
2083 |
'border-right-color', |
|
2084 |
'border-right-style', |
|
2085 |
'border-right-width', |
|
2086 |
'border-bottom', |
|
2087 |
'border-bottom-color', |
|
2088 |
'border-bottom-style', |
|
2089 |
'border-bottom-width', |
|
2090 |
'border-left', |
|
2091 |
'border-left-color', |
|
2092 |
'border-left-style', |
|
2093 |
'border-left-width', |
|
2094 |
'border-top', |
|
2095 |
'border-top-color', |
|
2096 |
'border-top-style', |
|
2097 |
'border-top-width', |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2098 |
|
9 | 2099 |
'border-spacing', |
2100 |
'border-collapse', |
|
2101 |
'caption-side', |
|
2102 |
||
2103 |
'color', |
|
2104 |
'font', |
|
2105 |
'font-family', |
|
2106 |
'font-size', |
|
2107 |
'font-style', |
|
2108 |
'font-variant', |
|
2109 |
'font-weight', |
|
2110 |
'letter-spacing', |
|
2111 |
'line-height', |
|
2112 |
'text-align', |
|
2113 |
'text-decoration', |
|
2114 |
'text-indent', |
|
2115 |
'text-transform', |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2116 |
|
9 | 2117 |
'height', |
2118 |
'min-height', |
|
2119 |
'max-height', |
|
2120 |
||
2121 |
'width', |
|
2122 |
'min-width', |
|
2123 |
'max-width', |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2124 |
|
9 | 2125 |
'margin', |
2126 |
'margin-right', |
|
2127 |
'margin-bottom', |
|
2128 |
'margin-left', |
|
2129 |
'margin-top', |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2130 |
|
9 | 2131 |
'padding', |
2132 |
'padding-right', |
|
2133 |
'padding-bottom', |
|
2134 |
'padding-left', |
|
2135 |
'padding-top', |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2136 |
|
9 | 2137 |
'clear', |
2138 |
'cursor', |
|
2139 |
'direction', |
|
2140 |
'float', |
|
2141 |
'overflow', |
|
2142 |
'vertical-align', |
|
2143 |
'list-style-type', |
|
2144 |
'grid-template-columns', |
|
2145 |
) |
|
2146 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2147 |
|
9 | 2148 |
/* |
2149 |
* CSS attributes that accept URL data types. |
|
2150 |
* |
|
2151 |
* This is in accordance to the CSS spec and unrelated to |
|
2152 |
* the sub-set of supported attributes above. |
|
2153 |
* |
|
2154 |
* See: https://developer.mozilla.org/en-US/docs/Web/CSS/url |
|
2155 |
*/ |
|
2156 |
$css_url_data_types = array( |
|
2157 |
'background', |
|
2158 |
'background-image', |
|
2159 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2160 |
'cursor', |
0 | 2161 |
|
9 | 2162 |
'list-style', |
2163 |
'list-style-image', |
|
2164 |
); |
|
2165 |
||
2166 |
if ( empty( $allowed_attr ) ) { |
|
0 | 2167 |
return $css; |
9 | 2168 |
} |
0 | 2169 |
|
2170 |
$css = ''; |
|
2171 |
foreach ( $css_array as $css_item ) { |
|
9 | 2172 |
if ( $css_item == '' ) { |
0 | 2173 |
continue; |
9 | 2174 |
} |
2175 |
||
2176 |
$css_item = trim( $css_item ); |
|
2177 |
$css_test_string = $css_item; |
|
2178 |
$found = false; |
|
2179 |
$url_attr = false; |
|
2180 |
||
0 | 2181 |
if ( strpos( $css_item, ':' ) === false ) { |
2182 |
$found = true; |
|
2183 |
} else { |
|
9 | 2184 |
$parts = explode( ':', $css_item, 2 ); |
2185 |
$css_selector = trim( $parts[0] ); |
|
2186 |
||
2187 |
if ( in_array( $css_selector, $allowed_attr, true ) ) { |
|
2188 |
$found = true; |
|
2189 |
$url_attr = in_array( $css_selector, $css_url_data_types, true ); |
|
2190 |
} |
|
0 | 2191 |
} |
9 | 2192 |
|
2193 |
if ( $found && $url_attr ) { |
|
2194 |
// Simplified: matches the sequence `url(*)`. |
|
2195 |
preg_match_all( '/url\([^)]+\)/', $parts[1], $url_matches ); |
|
2196 |
||
2197 |
foreach ( $url_matches[0] as $url_match ) { |
|
2198 |
// Clean up the URL from each of the matches above. |
|
2199 |
preg_match( '/^url\(\s*([\'\"]?)(.*)(\g1)\s*\)$/', $url_match, $url_pieces ); |
|
2200 |
||
2201 |
if ( empty( $url_pieces[2] ) ) { |
|
2202 |
$found = false; |
|
2203 |
break; |
|
2204 |
} |
|
2205 |
||
2206 |
$url = trim( $url_pieces[2] ); |
|
2207 |
||
2208 |
if ( empty( $url ) || $url !== wp_kses_bad_protocol( $url, $allowed_protocols ) ) { |
|
2209 |
$found = false; |
|
2210 |
break; |
|
2211 |
} else { |
|
2212 |
// Remove the whole `url(*)` bit that was matched above from the CSS. |
|
2213 |
$css_test_string = str_replace( $url_match, '', $css_test_string ); |
|
2214 |
} |
|
2215 |
} |
|
2216 |
} |
|
2217 |
||
2218 |
// Remove any CSS containing containing \ ( & } = or comments, except for url() useage checked above. |
|
2219 |
if ( $found && ! preg_match( '%[\\\(&=}]|/\*%', $css_test_string ) ) { |
|
2220 |
if ( $css != '' ) { |
|
0 | 2221 |
$css .= ';'; |
9 | 2222 |
} |
2223 |
||
0 | 2224 |
$css .= $css_item; |
2225 |
} |
|
2226 |
} |
|
2227 |
||
2228 |
return $css; |
|
2229 |
} |
|
2230 |
||
2231 |
/** |
|
2232 |
* Helper function to add global attributes to a tag in the allowed html list. |
|
2233 |
* |
|
2234 |
* @since 3.5.0 |
|
9 | 2235 |
* @since 5.0.0 Add support for `data-*` wildcard attributes. |
0 | 2236 |
* @access private |
9 | 2237 |
* @ignore |
0 | 2238 |
* |
2239 |
* @param array $value An array of attributes. |
|
2240 |
* @return array The array of attributes with global attributes added. |
|
2241 |
*/ |
|
2242 |
function _wp_add_global_attributes( $value ) { |
|
2243 |
$global_attributes = array( |
|
9 | 2244 |
'aria-describedby' => true, |
2245 |
'aria-details' => true, |
|
2246 |
'aria-label' => true, |
|
2247 |
'aria-labelledby' => true, |
|
2248 |
'aria-hidden' => true, |
|
2249 |
'class' => true, |
|
2250 |
'id' => true, |
|
2251 |
'style' => true, |
|
2252 |
'title' => true, |
|
2253 |
'role' => true, |
|
2254 |
'data-*' => true, |
|
0 | 2255 |
); |
2256 |
||
9 | 2257 |
if ( true === $value ) { |
0 | 2258 |
$value = array(); |
9 | 2259 |
} |
0 | 2260 |
|
9 | 2261 |
if ( is_array( $value ) ) { |
0 | 2262 |
return array_merge( $value, $global_attributes ); |
9 | 2263 |
} |
0 | 2264 |
|
2265 |
return $value; |
|
2266 |
} |