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