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