Issues (4967)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/wp-includes/kses.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
34
/**
35
 * You can override this in a plugin.
36
 *
37
 * The {@see 'wp_kses_allowed_html'} filter is more powerful and supplies context.
38
 *
39
 * `CUSTOM_TAGS` is not recommended and should be considered deprecated.
40
 *
41
 * @see wp_kses_allowed_html()
42
 *
43
 * @since 1.2.0
44
 */
45
if ( ! defined( 'CUSTOM_TAGS' ) )
46
	define( 'CUSTOM_TAGS', false );
47
48
// Ensure that these variables are added to the global namespace
49
// (e.g. if using namespaces / autoload in the current PHP environment).
50
global $allowedposttags, $allowedtags, $allowedentitynames;
51
52
if ( ! CUSTOM_TAGS ) {
53
	/**
54
	 * Kses global for default allowable HTML tags.
55
	 *
56
	 * Can be override by using CUSTOM_TAGS constant.
57
	 *
58
	 * @global array $allowedposttags
59
	 * @since 2.0.0
60
	 */
61
	$allowedposttags = array(
62
		'address' => array(),
63
		'a' => array(
64
			'href' => true,
65
			'rel' => true,
66
			'rev' => true,
67
			'name' => true,
68
			'target' => true,
69
		),
70
		'abbr' => array(),
71
		'acronym' => array(),
72
		'area' => array(
73
			'alt' => true,
74
			'coords' => true,
75
			'href' => true,
76
			'nohref' => true,
77
			'shape' => true,
78
			'target' => true,
79
		),
80
		'article' => array(
81
			'align' => true,
82
			'dir' => true,
83
			'lang' => true,
84
			'xml:lang' => true,
85
		),
86
		'aside' => array(
87
			'align' => true,
88
			'dir' => true,
89
			'lang' => true,
90
			'xml:lang' => true,
91
		),
92
		'audio' => array(
93
			'autoplay' => true,
94
			'controls' => true,
95
			'loop' => true,
96
			'muted' => true,
97
			'preload' => true,
98
			'src' => true,
99
		),
100
		'b' => array(),
101
		'bdo' => array(
102
			'dir' => true,
103
		),
104
		'big' => array(),
105
		'blockquote' => array(
106
			'cite' => true,
107
			'lang' => true,
108
			'xml:lang' => true,
109
		),
110
		'br' => array(),
111
		'button' => array(
112
			'disabled' => true,
113
			'name' => true,
114
			'type' => true,
115
			'value' => true,
116
		),
117
		'caption' => array(
118
			'align' => true,
119
		),
120
		'cite' => array(
121
			'dir' => true,
122
			'lang' => true,
123
		),
124
		'code' => array(),
125
		'col' => array(
126
			'align' => true,
127
			'char' => true,
128
			'charoff' => true,
129
			'span' => true,
130
			'dir' => true,
131
			'valign' => true,
132
			'width' => true,
133
		),
134
		'colgroup' => array(
135
			'align' => true,
136
			'char' => true,
137
			'charoff' => true,
138
			'span' => true,
139
			'valign' => true,
140
			'width' => true,
141
		),
142
		'del' => array(
143
			'datetime' => true,
144
		),
145
		'dd' => array(),
146
		'dfn' => array(),
147
		'details' => array(
148
			'align' => true,
149
			'dir' => true,
150
			'lang' => true,
151
			'open' => true,
152
			'xml:lang' => true,
153
		),
154
		'div' => array(
155
			'align' => true,
156
			'dir' => true,
157
			'lang' => true,
158
			'xml:lang' => true,
159
		),
160
		'dl' => array(),
161
		'dt' => array(),
162
		'em' => array(),
163
		'fieldset' => array(),
164
		'figure' => array(
165
			'align' => true,
166
			'dir' => true,
167
			'lang' => true,
168
			'xml:lang' => true,
169
		),
170
		'figcaption' => array(
171
			'align' => true,
172
			'dir' => true,
173
			'lang' => true,
174
			'xml:lang' => true,
175
		),
176
		'font' => array(
177
			'color' => true,
178
			'face' => true,
179
			'size' => true,
180
		),
181
		'footer' => array(
182
			'align' => true,
183
			'dir' => true,
184
			'lang' => true,
185
			'xml:lang' => true,
186
		),
187
		'form' => array(
188
			'action' => true,
189
			'accept' => true,
190
			'accept-charset' => true,
191
			'enctype' => true,
192
			'method' => true,
193
			'name' => true,
194
			'target' => true,
195
		),
196
		'h1' => array(
197
			'align' => true,
198
		),
199
		'h2' => array(
200
			'align' => true,
201
		),
202
		'h3' => array(
203
			'align' => true,
204
		),
205
		'h4' => array(
206
			'align' => true,
207
		),
208
		'h5' => array(
209
			'align' => true,
210
		),
211
		'h6' => array(
212
			'align' => true,
213
		),
214
		'header' => array(
215
			'align' => true,
216
			'dir' => true,
217
			'lang' => true,
218
			'xml:lang' => true,
219
		),
220
		'hgroup' => array(
221
			'align' => true,
222
			'dir' => true,
223
			'lang' => true,
224
			'xml:lang' => true,
225
		),
226
		'hr' => array(
227
			'align' => true,
228
			'noshade' => true,
229
			'size' => true,
230
			'width' => true,
231
		),
232
		'i' => array(),
233
		'img' => array(
234
			'alt' => true,
235
			'align' => true,
236
			'border' => true,
237
			'height' => true,
238
			'hspace' => true,
239
			'longdesc' => true,
240
			'vspace' => true,
241
			'src' => true,
242
			'usemap' => true,
243
			'width' => true,
244
		),
245
		'ins' => array(
246
			'datetime' => true,
247
			'cite' => true,
248
		),
249
		'kbd' => array(),
250
		'label' => array(
251
			'for' => true,
252
		),
253
		'legend' => array(
254
			'align' => true,
255
		),
256
		'li' => array(
257
			'align' => true,
258
			'value' => true,
259
		),
260
		'map' => array(
261
			'name' => true,
262
		),
263
		'mark' => array(),
264
		'menu' => array(
265
			'type' => true,
266
		),
267
		'nav' => array(
268
			'align' => true,
269
			'dir' => true,
270
			'lang' => true,
271
			'xml:lang' => true,
272
		),
273
		'p' => array(
274
			'align' => true,
275
			'dir' => true,
276
			'lang' => true,
277
			'xml:lang' => true,
278
		),
279
		'pre' => array(
280
			'width' => true,
281
		),
282
		'q' => array(
283
			'cite' => true,
284
		),
285
		's' => array(),
286
		'samp' => array(),
287
		'span' => array(
288
			'dir' => true,
289
			'align' => true,
290
			'lang' => true,
291
			'xml:lang' => true,
292
		),
293
		'section' => array(
294
			'align' => true,
295
			'dir' => true,
296
			'lang' => true,
297
			'xml:lang' => true,
298
		),
299
		'small' => array(),
300
		'strike' => array(),
301
		'strong' => array(),
302
		'sub' => array(),
303
		'summary' => array(
304
			'align' => true,
305
			'dir' => true,
306
			'lang' => true,
307
			'xml:lang' => true,
308
		),
309
		'sup' => array(),
310
		'table' => array(
311
			'align' => true,
312
			'bgcolor' => true,
313
			'border' => true,
314
			'cellpadding' => true,
315
			'cellspacing' => true,
316
			'dir' => true,
317
			'rules' => true,
318
			'summary' => true,
319
			'width' => true,
320
		),
321
		'tbody' => array(
322
			'align' => true,
323
			'char' => true,
324
			'charoff' => true,
325
			'valign' => true,
326
		),
327
		'td' => array(
328
			'abbr' => true,
329
			'align' => true,
330
			'axis' => true,
331
			'bgcolor' => true,
332
			'char' => true,
333
			'charoff' => true,
334
			'colspan' => true,
335
			'dir' => true,
336
			'headers' => true,
337
			'height' => true,
338
			'nowrap' => true,
339
			'rowspan' => true,
340
			'scope' => true,
341
			'valign' => true,
342
			'width' => true,
343
		),
344
		'textarea' => array(
345
			'cols' => true,
346
			'rows' => true,
347
			'disabled' => true,
348
			'name' => true,
349
			'readonly' => true,
350
		),
351
		'tfoot' => array(
352
			'align' => true,
353
			'char' => true,
354
			'charoff' => true,
355
			'valign' => true,
356
		),
357
		'th' => array(
358
			'abbr' => true,
359
			'align' => true,
360
			'axis' => true,
361
			'bgcolor' => true,
362
			'char' => true,
363
			'charoff' => true,
364
			'colspan' => true,
365
			'headers' => true,
366
			'height' => true,
367
			'nowrap' => true,
368
			'rowspan' => true,
369
			'scope' => true,
370
			'valign' => true,
371
			'width' => true,
372
		),
373
		'thead' => array(
374
			'align' => true,
375
			'char' => true,
376
			'charoff' => true,
377
			'valign' => true,
378
		),
379
		'title' => array(),
380
		'tr' => array(
381
			'align' => true,
382
			'bgcolor' => true,
383
			'char' => true,
384
			'charoff' => true,
385
			'valign' => true,
386
		),
387
		'track' => array(
388
			'default' => true,
389
			'kind' => true,
390
			'label' => true,
391
			'src' => true,
392
			'srclang' => true,
393
		),
394
		'tt' => array(),
395
		'u' => array(),
396
		'ul' => array(
397
			'type' => true,
398
		),
399
		'ol' => array(
400
			'start' => true,
401
			'type' => true,
402
			'reversed' => true,
403
		),
404
		'var' => array(),
405
		'video' => array(
406
			'autoplay' => true,
407
			'controls' => true,
408
			'height' => true,
409
			'loop' => true,
410
			'muted' => true,
411
			'poster' => true,
412
			'preload' => true,
413
			'src' => true,
414
			'width' => true,
415
		),
416
	);
417
418
	/**
419
	 * Kses allowed HTML elements.
420
	 *
421
	 * @global array $allowedtags
422
	 * @since 1.0.0
423
	 */
424
	$allowedtags = array(
425
		'a' => array(
426
			'href' => true,
427
			'title' => true,
428
		),
429
		'abbr' => array(
430
			'title' => true,
431
		),
432
		'acronym' => array(
433
			'title' => true,
434
		),
435
		'b' => array(),
436
		'blockquote' => array(
437
			'cite' => true,
438
		),
439
		'cite' => array(),
440
		'code' => array(),
441
		'del' => array(
442
			'datetime' => true,
443
		),
444
		'em' => array(),
445
		'i' => array(),
446
		'q' => array(
447
			'cite' => true,
448
		),
449
		's' => array(),
450
		'strike' => array(),
451
		'strong' => array(),
452
	);
453
454
	$allowedentitynames = array(
455
		'nbsp',    'iexcl',  'cent',    'pound',  'curren', 'yen',
456
		'brvbar',  'sect',   'uml',     'copy',   'ordf',   'laquo',
457
		'not',     'shy',    'reg',     'macr',   'deg',    'plusmn',
458
		'acute',   'micro',  'para',    'middot', 'cedil',  'ordm',
459
		'raquo',   'iquest', 'Agrave',  'Aacute', 'Acirc',  'Atilde',
460
		'Auml',    'Aring',  'AElig',   'Ccedil', 'Egrave', 'Eacute',
461
		'Ecirc',   'Euml',   'Igrave',  'Iacute', 'Icirc',  'Iuml',
462
		'ETH',     'Ntilde', 'Ograve',  'Oacute', 'Ocirc',  'Otilde',
463
		'Ouml',    'times',  'Oslash',  'Ugrave', 'Uacute', 'Ucirc',
464
		'Uuml',    'Yacute', 'THORN',   'szlig',  'agrave', 'aacute',
465
		'acirc',   'atilde', 'auml',    'aring',  'aelig',  'ccedil',
466
		'egrave',  'eacute', 'ecirc',   'euml',   'igrave', 'iacute',
467
		'icirc',   'iuml',   'eth',     'ntilde', 'ograve', 'oacute',
468
		'ocirc',   'otilde', 'ouml',    'divide', 'oslash', 'ugrave',
469
		'uacute',  'ucirc',  'uuml',    'yacute', 'thorn',  'yuml',
470
		'quot',    'amp',    'lt',      'gt',     'apos',   'OElig',
471
		'oelig',   'Scaron', 'scaron',  'Yuml',   'circ',   'tilde',
472
		'ensp',    'emsp',   'thinsp',  'zwnj',   'zwj',    'lrm',
473
		'rlm',     'ndash',  'mdash',   'lsquo',  'rsquo',  'sbquo',
474
		'ldquo',   'rdquo',  'bdquo',   'dagger', 'Dagger', 'permil',
475
		'lsaquo',  'rsaquo', 'euro',    'fnof',   'Alpha',  'Beta',
476
		'Gamma',   'Delta',  'Epsilon', 'Zeta',   'Eta',    'Theta',
477
		'Iota',    'Kappa',  'Lambda',  'Mu',     'Nu',     'Xi',
478
		'Omicron', 'Pi',     'Rho',     'Sigma',  'Tau',    'Upsilon',
479
		'Phi',     'Chi',    'Psi',     'Omega',  'alpha',  'beta',
480
		'gamma',   'delta',  'epsilon', 'zeta',   'eta',    'theta',
481
		'iota',    'kappa',  'lambda',  'mu',     'nu',     'xi',
482
		'omicron', 'pi',     'rho',     'sigmaf', 'sigma',  'tau',
483
		'upsilon', 'phi',    'chi',     'psi',    'omega',  'thetasym',
484
		'upsih',   'piv',    'bull',    'hellip', 'prime',  'Prime',
485
		'oline',   'frasl',  'weierp',  'image',  'real',   'trade',
486
		'alefsym', 'larr',   'uarr',    'rarr',   'darr',   'harr',
487
		'crarr',   'lArr',   'uArr',    'rArr',   'dArr',   'hArr',
488
		'forall',  'part',   'exist',   'empty',  'nabla',  'isin',
489
		'notin',   'ni',     'prod',    'sum',    'minus',  'lowast',
490
		'radic',   'prop',   'infin',   'ang',    'and',    'or',
491
		'cap',     'cup',    'int',     'sim',    'cong',   'asymp',
492
		'ne',      'equiv',  'le',      'ge',     'sub',    'sup',
493
		'nsub',    'sube',   'supe',    'oplus',  'otimes', 'perp',
494
		'sdot',    'lceil',  'rceil',   'lfloor', 'rfloor', 'lang',
495
		'rang',    'loz',    'spades',  'clubs',  'hearts', 'diams',
496
		'sup1',    'sup2',   'sup3',    'frac14', 'frac12', 'frac34',
497
		'there4',
498
	);
499
500
	$allowedposttags = array_map( '_wp_add_global_attributes', $allowedposttags );
501
} else {
502
	$allowedtags = wp_kses_array_lc( $allowedtags );
503
	$allowedposttags = wp_kses_array_lc( $allowedposttags );
504
}
505
506
/**
507
 * Filters content and keeps only allowable HTML elements.
508
 *
509
 * This function makes sure that only the allowed HTML element names, attribute
510
 * names and attribute values plus only sane HTML entities will occur in
511
 * $string. You have to remove any slashes from PHP's magic quotes before you
512
 * call this function.
513
 *
514
 * The default allowed protocols are 'http', 'https', 'ftp', 'mailto', 'news',
515
 * 'irc', 'gopher', 'nntp', 'feed', 'telnet, 'mms', 'rtsp' and 'svn'. This
516
 * covers all common link protocols, except for 'javascript' which should not
517
 * be allowed for untrusted users.
518
 *
519
 * @since 1.0.0
520
 *
521
 * @param string $string            Content to filter through kses
522
 * @param array  $allowed_html      List of allowed HTML elements
523
 * @param array  $allowed_protocols Optional. Allowed protocol in links.
524
 * @return string Filtered content with only allowed HTML elements
525
 */
526
function wp_kses( $string, $allowed_html, $allowed_protocols = array() ) {
527
	if ( empty( $allowed_protocols ) )
528
		$allowed_protocols = wp_allowed_protocols();
529
	$string = wp_kses_no_null( $string, array( 'slash_zero' => 'keep' ) );
530
	$string = wp_kses_normalize_entities($string);
531
	$string = wp_kses_hook($string, $allowed_html, $allowed_protocols); // WP changed the order of these funcs and added args to wp_kses_hook
532
	return wp_kses_split($string, $allowed_html, $allowed_protocols);
533
}
534
535
/**
536
 * Filters one attribute only and ensures its value is allowed.
537
 *
538
 * This function has the advantage of being more secure than esc_attr() and can
539
 * escape data in some situations where wp_kses() must strip the whole attribute.
540
 *
541
 * @since 4.2.3
542
 *
543
 * @param string $string The 'whole' attribute, including name and value.
544
 * @param string $element The element name to which the attribute belongs.
545
 * @return string Filtered attribute.
546
 */
547
function wp_kses_one_attr( $string, $element ) {
548
	$uris = array('xmlns', 'profile', 'href', 'src', 'cite', 'classid', 'codebase', 'data', 'usemap', 'longdesc', 'action');
549
	$allowed_html = wp_kses_allowed_html( 'post' );
550
	$allowed_protocols = wp_allowed_protocols();
551
	$string = wp_kses_no_null( $string, array( 'slash_zero' => 'keep' ) );
552
	
553
	// Preserve leading and trailing whitespace.
554
	$matches = array();
555
	preg_match('/^\s*/', $string, $matches);
556
	$lead = $matches[0];
557
	preg_match('/\s*$/', $string, $matches);
558
	$trail = $matches[0];
559
	if ( empty( $trail ) ) {
560
		$string = substr( $string, strlen( $lead ) );
561
	} else {
562
		$string = substr( $string, strlen( $lead ), -strlen( $trail ) );
563
	}
564
	
565
	// Parse attribute name and value from input.
566
	$split = preg_split( '/\s*=\s*/', $string, 2 );
567
	$name = $split[0];
568
	if ( count( $split ) == 2 ) {
569
		$value = $split[1];
570
571
		// Remove quotes surrounding $value.
572
		// Also guarantee correct quoting in $string for this one attribute.
573
		if ( '' == $value ) {
574
			$quote = '';
575
		} else {
576
			$quote = $value[0];
577
		}
578
		if ( '"' == $quote || "'" == $quote ) {
579
			if ( substr( $value, -1 ) != $quote ) {
580
				return '';
581
			}
582
			$value = substr( $value, 1, -1 );
583
		} else {
584
			$quote = '"';
585
		}
586
587
		// Sanitize quotes, angle braces, and entities.
588
		$value = esc_attr( $value );
589
590
		// Sanitize URI values.
591
		if ( in_array( strtolower( $name ), $uris ) ) {
592
			$value = wp_kses_bad_protocol( $value, $allowed_protocols );
593
		}
594
595
		$string = "$name=$quote$value$quote";
596
		$vless = 'n';
597
	} else {
598
		$value = '';
599
		$vless = 'y';
600
	}
601
	
602
	// Sanitize attribute by name.
603
	wp_kses_attr_check( $name, $value, $string, $vless, $element, $allowed_html );
604
605
	// Restore whitespace.
606
	return $lead . $string . $trail;
607
}
608
609
/**
610
 * Return a list of allowed tags and attributes for a given context.
611
 *
612
 * @since 3.5.0
613
 *
614
 * @global array $allowedposttags
615
 * @global array $allowedtags
616
 * @global array $allowedentitynames
617
 *
618
 * @param string $context The context for which to retrieve tags.
619
 *                        Allowed values are post, strip, data,entities, or
620
 *                        the name of a field filter such as pre_user_description.
621
 * @return array List of allowed tags and their allowed attributes.
622
 */
623
function wp_kses_allowed_html( $context = '' ) {
624
	global $allowedposttags, $allowedtags, $allowedentitynames;
625
626
	if ( is_array( $context ) ) {
627
		/**
628
		 * Filters HTML elements allowed for a given context.
629
		 *
630
		 * @since 3.5.0
631
		 *
632
		 * @param string $tags    Allowed tags, attributes, and/or entities.
633
		 * @param string $context Context to judge allowed tags by. Allowed values are 'post',
634
		 *                        'data', 'strip', 'entities', 'explicit', or the name of a filter.
635
		 */
636
		return apply_filters( 'wp_kses_allowed_html', $context, 'explicit' );
637
	}
638
639
	switch ( $context ) {
640
		case 'post':
641
			/** This filter is documented in wp-includes/kses.php */
642
			return apply_filters( 'wp_kses_allowed_html', $allowedposttags, $context );
643
644
		case 'user_description':
645
		case 'pre_user_description':
646
			$tags = $allowedtags;
647
			$tags['a']['rel'] = true;
648
			/** This filter is documented in wp-includes/kses.php */
649
			return apply_filters( 'wp_kses_allowed_html', $tags, $context );
650
651
		case 'strip':
652
			/** This filter is documented in wp-includes/kses.php */
653
			return apply_filters( 'wp_kses_allowed_html', array(), $context );
654
655
		case 'entities':
656
			/** This filter is documented in wp-includes/kses.php */
657
			return apply_filters( 'wp_kses_allowed_html', $allowedentitynames, $context);
658
659
		case 'data':
660
		default:
661
			/** This filter is documented in wp-includes/kses.php */
662
			return apply_filters( 'wp_kses_allowed_html', $allowedtags, $context );
663
	}
664
}
665
666
/**
667
 * You add any kses hooks here.
668
 *
669
 * There is currently only one kses WordPress hook, {@see 'pre_kses'}, and it is called here.
670
 * All parameters are passed to the hooks and expected to receive a string.
671
 *
672
 * @since 1.0.0
673
 *
674
 * @param string $string            Content to filter through kses
675
 * @param array  $allowed_html      List of allowed HTML elements
676
 * @param array  $allowed_protocols Allowed protocol in links
677
 * @return string Filtered content through {@see 'pre_kses'} hook.
678
 */
679
function wp_kses_hook( $string, $allowed_html, $allowed_protocols ) {
680
	/**
681
	 * Filters content to be run through kses.
682
	 *
683
	 * @since 2.3.0
684
	 *
685
	 * @param string $string            Content to run through kses.
686
	 * @param array  $allowed_html      Allowed HTML elements.
687
	 * @param array  $allowed_protocols Allowed protocol in links.
688
	 */
689
	return apply_filters( 'pre_kses', $string, $allowed_html, $allowed_protocols );
690
}
691
692
/**
693
 * This function returns kses' version number.
694
 *
695
 * @since 1.0.0
696
 *
697
 * @return string KSES Version Number
698
 */
699
function wp_kses_version() {
700
	return '0.2.2';
701
}
702
703
/**
704
 * Searches for HTML tags, no matter how malformed.
705
 *
706
 * It also matches stray ">" characters.
707
 *
708
 * @since 1.0.0
709
 *
710
 * @global array $pass_allowed_html
711
 * @global array $pass_allowed_protocols
712
 *
713
 * @param string $string            Content to filter
714
 * @param array  $allowed_html      Allowed HTML elements
715
 * @param array  $allowed_protocols Allowed protocols to keep
716
 * @return string Content with fixed HTML tags
717
 */
718
function wp_kses_split( $string, $allowed_html, $allowed_protocols ) {
719
	global $pass_allowed_html, $pass_allowed_protocols;
720
	$pass_allowed_html = $allowed_html;
721
	$pass_allowed_protocols = $allowed_protocols;
722
	return preg_replace_callback( '%(<!--.*?(-->|$))|(<[^>]*(>|$)|>)%', '_wp_kses_split_callback', $string );
723
}
724
725
/**
726
 * Callback for wp_kses_split.
727
 *
728
 * @since 3.1.0
729
 * @access private
730
 *
731
 * @global array $pass_allowed_html
732
 * @global array $pass_allowed_protocols
733
 *
734
 * @return string
735
 */
736
function _wp_kses_split_callback( $match ) {
737
	global $pass_allowed_html, $pass_allowed_protocols;
738
	return wp_kses_split2( $match[0], $pass_allowed_html, $pass_allowed_protocols );
739
}
740
741
/**
742
 * Callback for wp_kses_split for fixing malformed HTML tags.
743
 *
744
 * This function does a lot of work. It rejects some very malformed things like
745
 * <:::>. It returns an empty string, if the element isn't allowed (look ma, no
746
 * strip_tags()!). Otherwise it splits the tag into an element and an attribute
747
 * list.
748
 *
749
 * After the tag is split into an element and an attribute list, it is run
750
 * through another filter which will remove illegal attributes and once that is
751
 * completed, will be returned.
752
 *
753
 * @access private
754
 * @since 1.0.0
755
 *
756
 * @param string $string            Content to filter
757
 * @param array  $allowed_html      Allowed HTML elements
758
 * @param array  $allowed_protocols Allowed protocols to keep
759
 * @return string Fixed HTML element
760
 */
761
function wp_kses_split2($string, $allowed_html, $allowed_protocols) {
762
	$string = wp_kses_stripslashes($string);
763
764
	if (substr($string, 0, 1) != '<')
765
		return '&gt;';
766
	// It matched a ">" character
767
768
	if ( '<!--' == substr( $string, 0, 4 ) ) {
769
		$string = str_replace( array('<!--', '-->'), '', $string );
770
		while ( $string != ($newstring = wp_kses($string, $allowed_html, $allowed_protocols)) )
771
			$string = $newstring;
772
		if ( $string == '' )
773
			return '';
774
		// prevent multiple dashes in comments
775
		$string = preg_replace('/--+/', '-', $string);
776
		// prevent three dashes closing a comment
777
		$string = preg_replace('/-$/', '', $string);
778
		return "<!--{$string}-->";
779
	}
780
	// Allow HTML comments
781
782
	if (!preg_match('%^<\s*(/\s*)?([a-zA-Z0-9-]+)([^>]*)>?$%', $string, $matches))
783
		return '';
784
	// It's seriously malformed
785
786
	$slash = trim($matches[1]);
787
	$elem = $matches[2];
788
	$attrlist = $matches[3];
789
790
	if ( ! is_array( $allowed_html ) )
791
		$allowed_html = wp_kses_allowed_html( $allowed_html );
792
793
	if ( ! isset($allowed_html[strtolower($elem)]) )
794
		return '';
795
	// They are using a not allowed HTML element
796
797
	if ($slash != '')
798
		return "</$elem>";
799
	// No attributes are allowed for closing elements
800
801
	return wp_kses_attr( $elem, $attrlist, $allowed_html, $allowed_protocols );
802
}
803
804
/**
805
 * Removes all attributes, if none are allowed for this element.
806
 *
807
 * If some are allowed it calls wp_kses_hair() to split them further, and then
808
 * it builds up new HTML code from the data that kses_hair() returns. It also
809
 * removes "<" and ">" characters, if there are any left. One more thing it does
810
 * is to check if the tag has a closing XHTML slash, and if it does, it puts one
811
 * in the returned code as well.
812
 *
813
 * @since 1.0.0
814
 *
815
 * @param string $element           HTML element/tag
816
 * @param string $attr              HTML attributes from HTML element to closing HTML element tag
817
 * @param array  $allowed_html      Allowed HTML elements
818
 * @param array  $allowed_protocols Allowed protocols to keep
819
 * @return string Sanitized HTML element
820
 */
821
function wp_kses_attr($element, $attr, $allowed_html, $allowed_protocols) {
822
	if ( ! is_array( $allowed_html ) )
823
		$allowed_html = wp_kses_allowed_html( $allowed_html );
824
825
	// Is there a closing XHTML slash at the end of the attributes?
826
	$xhtml_slash = '';
827
	if (preg_match('%\s*/\s*$%', $attr))
828
		$xhtml_slash = ' /';
829
830
	// Are any attributes allowed at all for this element?
831
	if ( ! isset( $allowed_html[ strtolower( $element ) ] ) || true === $allowed_html[ strtolower( $element ) ] || count( $allowed_html[ strtolower( $element ) ] ) == 0 ) {
832
		return "<$element$xhtml_slash>";
833
	}
834
835
	// Split it
836
	$attrarr = wp_kses_hair($attr, $allowed_protocols);
837
838
	// Go through $attrarr, and save the allowed attributes for this element
839
	// in $attr2
840
	$attr2 = '';
841
	foreach ( $attrarr as $arreach ) {
842
		if ( wp_kses_attr_check( $arreach['name'], $arreach['value'], $arreach['whole'], $arreach['vless'], $element, $allowed_html ) ) {
843
			$attr2 .= ' '.$arreach['whole'];
844
		}
845
	}
846
847
	// Remove any "<" or ">" characters
848
	$attr2 = preg_replace('/[<>]/', '', $attr2);
849
850
	return "<$element$attr2$xhtml_slash>";
851
}
852
853
/**
854
 * Determine whether an attribute is allowed.
855
 *
856
 * @since 4.2.3
857
 *
858
 * @param string $name The attribute name. Returns empty string when not allowed.
859
 * @param string $value The attribute value. Returns a filtered value.
860
 * @param string $whole The name=value input. Returns filtered input.
861
 * @param string $vless 'y' when attribute like "enabled", otherwise 'n'.
862
 * @param string $element The name of the element to which this attribute belongs.
863
 * @param array $allowed_html The full list of allowed elements and attributes.
864
 * @return bool Is the attribute allowed?
865
 */
866
function wp_kses_attr_check( &$name, &$value, &$whole, $vless, $element, $allowed_html ) {
867
	$allowed_attr = $allowed_html[strtolower( $element )];
868
869
	$name_low = strtolower( $name );
870
	if ( ! isset( $allowed_attr[$name_low] ) || '' == $allowed_attr[$name_low] ) {
871
		$name = $value = $whole = '';
872
		return false;
873
	}
874
875
	if ( 'style' == $name_low ) {
876
		$new_value = safecss_filter_attr( $value );
877
878
		if ( empty( $new_value ) ) {
879
			$name = $value = $whole = '';
880
			return false;
881
		}
882
883
		$whole = str_replace( $value, $new_value, $whole );
884
		$value = $new_value;
885
	}
886
887
	if ( is_array( $allowed_attr[$name_low] ) ) {
888
		// there are some checks
889
		foreach ( $allowed_attr[$name_low] as $currkey => $currval ) {
890
			if ( ! wp_kses_check_attr_val( $value, $vless, $currkey, $currval ) ) {
891
				$name = $value = $whole = '';
892
				return false;
893
			}
894
		}
895
	}
896
897
	return true;
898
}
899
900
/**
901
 * Builds an attribute list from string containing attributes.
902
 *
903
 * This function does a lot of work. It parses an attribute list into an array
904
 * with attribute data, and tries to do the right thing even if it gets weird
905
 * input. It will add quotes around attribute values that don't have any quotes
906
 * or apostrophes around them, to make it easier to produce HTML code that will
907
 * conform to W3C's HTML specification. It will also remove bad URL protocols
908
 * from attribute values. It also reduces duplicate attributes by using the
909
 * attribute defined first (foo='bar' foo='baz' will result in foo='bar').
910
 *
911
 * @since 1.0.0
912
 *
913
 * @param string $attr              Attribute list from HTML element to closing HTML element tag
914
 * @param array  $allowed_protocols Allowed protocols to keep
915
 * @return array List of attributes after parsing
916
 */
917
function wp_kses_hair($attr, $allowed_protocols) {
918
	$attrarr = array();
919
	$mode = 0;
920
	$attrname = '';
921
	$uris = array('xmlns', 'profile', 'href', 'src', 'cite', 'classid', 'codebase', 'data', 'usemap', 'longdesc', 'action');
922
923
	// Loop through the whole attribute list
924
925
	while (strlen($attr) != 0) {
926
		$working = 0; // Was the last operation successful?
927
928
		switch ($mode) {
929
			case 0 : // attribute name, href for instance
930
931
				if ( preg_match('/^([-a-zA-Z:]+)/', $attr, $match ) ) {
932
					$attrname = $match[1];
933
					$working = $mode = 1;
934
					$attr = preg_replace( '/^[-a-zA-Z:]+/', '', $attr );
935
				}
936
937
				break;
938
939
			case 1 : // equals sign or valueless ("selected")
940
941
				if (preg_match('/^\s*=\s*/', $attr)) // equals sign
942
					{
943
					$working = 1;
944
					$mode = 2;
945
					$attr = preg_replace('/^\s*=\s*/', '', $attr);
946
					break;
947
				}
948
949
				if (preg_match('/^\s+/', $attr)) // valueless
950
					{
951
					$working = 1;
952
					$mode = 0;
953
					if(false === array_key_exists($attrname, $attrarr)) {
954
						$attrarr[$attrname] = array ('name' => $attrname, 'value' => '', 'whole' => $attrname, 'vless' => 'y');
955
					}
956
					$attr = preg_replace('/^\s+/', '', $attr);
957
				}
958
959
				break;
960
961
			case 2 : // attribute value, a URL after href= for instance
962
963 View Code Duplication
				if (preg_match('%^"([^"]*)"(\s+|/?$)%', $attr, $match))
964
					// "value"
965
					{
966
					$thisval = $match[1];
967
					if ( in_array(strtolower($attrname), $uris) )
968
						$thisval = wp_kses_bad_protocol($thisval, $allowed_protocols);
969
970
					if(false === array_key_exists($attrname, $attrarr)) {
971
						$attrarr[$attrname] = array ('name' => $attrname, 'value' => $thisval, 'whole' => "$attrname=\"$thisval\"", 'vless' => 'n');
972
					}
973
					$working = 1;
974
					$mode = 0;
975
					$attr = preg_replace('/^"[^"]*"(\s+|$)/', '', $attr);
976
					break;
977
				}
978
979 View Code Duplication
				if (preg_match("%^'([^']*)'(\s+|/?$)%", $attr, $match))
980
					// 'value'
981
					{
982
					$thisval = $match[1];
983
					if ( in_array(strtolower($attrname), $uris) )
984
						$thisval = wp_kses_bad_protocol($thisval, $allowed_protocols);
985
986
					if(false === array_key_exists($attrname, $attrarr)) {
987
						$attrarr[$attrname] = array ('name' => $attrname, 'value' => $thisval, 'whole' => "$attrname='$thisval'", 'vless' => 'n');
988
					}
989
					$working = 1;
990
					$mode = 0;
991
					$attr = preg_replace("/^'[^']*'(\s+|$)/", '', $attr);
992
					break;
993
				}
994
995 View Code Duplication
				if (preg_match("%^([^\s\"']+)(\s+|/?$)%", $attr, $match))
996
					// value
997
					{
998
					$thisval = $match[1];
999
					if ( in_array(strtolower($attrname), $uris) )
1000
						$thisval = wp_kses_bad_protocol($thisval, $allowed_protocols);
1001
1002
					if(false === array_key_exists($attrname, $attrarr)) {
1003
						$attrarr[$attrname] = array ('name' => $attrname, 'value' => $thisval, 'whole' => "$attrname=\"$thisval\"", 'vless' => 'n');
1004
					}
1005
					// We add quotes to conform to W3C's HTML spec.
1006
					$working = 1;
1007
					$mode = 0;
1008
					$attr = preg_replace("%^[^\s\"']+(\s+|$)%", '', $attr);
1009
				}
1010
1011
				break;
1012
		} // switch
1013
1014
		if ($working == 0) // not well formed, remove and try again
1015
		{
1016
			$attr = wp_kses_html_error($attr);
1017
			$mode = 0;
1018
		}
1019
	} // while
1020
1021
	if ($mode == 1 && false === array_key_exists($attrname, $attrarr))
1022
		// special case, for when the attribute list ends with a valueless
1023
		// attribute like "selected"
1024
		$attrarr[$attrname] = array ('name' => $attrname, 'value' => '', 'whole' => $attrname, 'vless' => 'y');
1025
1026
	return $attrarr;
1027
}
1028
1029
/**
1030
 * Finds all attributes of an HTML element.
1031
 *
1032
 * Does not modify input.  May return "evil" output.
1033
 *
1034
 * Based on wp_kses_split2() and wp_kses_attr()
1035
 *
1036
 * @since 4.2.3
1037
 *
1038
 * @param string $element HTML element/tag
1039
 * @return array|bool List of attributes found in $element. Returns false on failure.
0 ignored issues
show
Consider making the return type a bit more specific; maybe use false|array.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
1040
 */
1041
function wp_kses_attr_parse( $element ) {
1042
	$valid = preg_match('%^(<\s*)(/\s*)?([a-zA-Z0-9]+\s*)([^>]*)(>?)$%', $element, $matches);
1043
	if ( 1 !== $valid ) {
1044
		return false;
1045
	}
1046
1047
	$begin =  $matches[1];
1048
	$slash =  $matches[2];
1049
	$elname = $matches[3];
1050
	$attr =   $matches[4];
1051
	$end =    $matches[5];
1052
1053
	if ( '' !== $slash ) {
1054
		// Closing elements do not get parsed.
1055
		return false;
1056
	}
1057
1058
	// Is there a closing XHTML slash at the end of the attributes?
1059
	if ( 1 === preg_match( '%\s*/\s*$%', $attr, $matches ) ) {
1060
		$xhtml_slash = $matches[0];
1061
		$attr = substr( $attr, 0, -strlen( $xhtml_slash ) );
1062
	} else {
1063
		$xhtml_slash = '';
1064
	}
1065
	
1066
	// Split it
1067
	$attrarr = wp_kses_hair_parse( $attr );
1068
	if ( false === $attrarr ) {
1069
		return false;
1070
	}
1071
1072
	// Make sure all input is returned by adding front and back matter.
1073
	array_unshift( $attrarr, $begin . $slash . $elname );
1074
	array_push( $attrarr, $xhtml_slash . $end );
1075
	
1076
	return $attrarr;
1077
}
1078
1079
/**
1080
 * Builds an attribute list from string containing attributes.
1081
 *
1082
 * Does not modify input.  May return "evil" output.
1083
 * In case of unexpected input, returns false instead of stripping things.
1084
 *
1085
 * Based on wp_kses_hair() but does not return a multi-dimensional array.
1086
 *
1087
 * @since 4.2.3
1088
 *
1089
 * @param string $attr Attribute list from HTML element to closing HTML element tag
1090
 * @return array|bool List of attributes found in $attr. Returns false on failure.
1091
 */
1092
function wp_kses_hair_parse( $attr ) {
1093
	if ( '' === $attr ) {
1094
		return array();
1095
	}
1096
1097
	$regex =
1098
	  '(?:'
1099
	.     '[-a-zA-Z:]+'   // Attribute name.
1100
	. '|'
1101
	.     '\[\[?[^\[\]]+\]\]?' // Shortcode in the name position implies unfiltered_html.
1102
	. ')'
1103
	. '(?:'               // Attribute value.
1104
	.     '\s*=\s*'       // All values begin with '='
1105
	.     '(?:'
1106
	.         '"[^"]*"'   // Double-quoted
1107
	.     '|'
1108
	.         "'[^']*'"   // Single-quoted
1109
	.     '|'
1110
	.         '[^\s"\']+' // Non-quoted
1111
	.         '(?:\s|$)'  // Must have a space
1112
	.     ')'
1113
	. '|'
1114
	.     '(?:\s|$)'      // If attribute has no value, space is required.
1115
	. ')'
1116
	. '\s*';              // Trailing space is optional except as mentioned above.
1117
1118
	// Although it is possible to reduce this procedure to a single regexp,
1119
	// we must run that regexp twice to get exactly the expected result.
1120
1121
	$validation = "%^($regex)+$%";
1122
	$extraction = "%$regex%";
1123
1124
	if ( 1 === preg_match( $validation, $attr ) ) {
1125
		preg_match_all( $extraction, $attr, $attrarr );
1126
		return $attrarr[0];
1127
	} else {
1128
		return false;
1129
	}
1130
}
1131
1132
/**
1133
 * Performs different checks for attribute values.
1134
 *
1135
 * The currently implemented checks are "maxlen", "minlen", "maxval", "minval"
1136
 * and "valueless".
1137
 *
1138
 * @since 1.0.0
1139
 *
1140
 * @param string $value      Attribute value
1141
 * @param string $vless      Whether the value is valueless. Use 'y' or 'n'
1142
 * @param string $checkname  What $checkvalue is checking for.
1143
 * @param mixed  $checkvalue What constraint the value should pass
1144
 * @return bool Whether check passes
1145
 */
1146
function wp_kses_check_attr_val($value, $vless, $checkname, $checkvalue) {
1147
	$ok = true;
1148
1149
	switch (strtolower($checkname)) {
1150
		case 'maxlen' :
1151
			// The maxlen check makes sure that the attribute value has a length not
1152
			// greater than the given value. This can be used to avoid Buffer Overflows
1153
			// in WWW clients and various Internet servers.
1154
1155
			if (strlen($value) > $checkvalue)
1156
				$ok = false;
1157
			break;
1158
1159
		case 'minlen' :
1160
			// The minlen check makes sure that the attribute value has a length not
1161
			// smaller than the given value.
1162
1163
			if (strlen($value) < $checkvalue)
1164
				$ok = false;
1165
			break;
1166
1167 View Code Duplication
		case 'maxval' :
1168
			// The maxval check does two things: it checks that the attribute value is
1169
			// an integer from 0 and up, without an excessive amount of zeroes or
1170
			// whitespace (to avoid Buffer Overflows). It also checks that the attribute
1171
			// value is not greater than the given value.
1172
			// This check can be used to avoid Denial of Service attacks.
1173
1174
			if (!preg_match('/^\s{0,6}[0-9]{1,6}\s{0,6}$/', $value))
1175
				$ok = false;
1176
			if ($value > $checkvalue)
1177
				$ok = false;
1178
			break;
1179
1180 View Code Duplication
		case 'minval' :
1181
			// The minval check makes sure that the attribute value is a positive integer,
1182
			// and that it is not smaller than the given value.
1183
1184
			if (!preg_match('/^\s{0,6}[0-9]{1,6}\s{0,6}$/', $value))
1185
				$ok = false;
1186
			if ($value < $checkvalue)
1187
				$ok = false;
1188
			break;
1189
1190
		case 'valueless' :
1191
			// The valueless check makes sure if the attribute has a value
1192
			// (like <a href="blah">) or not (<option selected>). If the given value
1193
			// is a "y" or a "Y", the attribute must not have a value.
1194
			// If the given value is an "n" or an "N", the attribute must have one.
1195
1196
			if (strtolower($checkvalue) != $vless)
1197
				$ok = false;
1198
			break;
1199
	} // switch
1200
1201
	return $ok;
1202
}
1203
1204
/**
1205
 * Sanitize string from bad protocols.
1206
 *
1207
 * This function removes all non-allowed protocols from the beginning of
1208
 * $string. It ignores whitespace and the case of the letters, and it does
1209
 * understand HTML entities. It does its work in a while loop, so it won't be
1210
 * fooled by a string like "javascript:javascript:alert(57)".
1211
 *
1212
 * @since 1.0.0
1213
 *
1214
 * @param string $string            Content to filter bad protocols from
1215
 * @param array  $allowed_protocols Allowed protocols to keep
1216
 * @return string Filtered content
1217
 */
1218
function wp_kses_bad_protocol($string, $allowed_protocols) {
1219
	$string = wp_kses_no_null($string);
1220
	$iterations = 0;
1221
1222
	do {
1223
		$original_string = $string;
1224
		$string = wp_kses_bad_protocol_once($string, $allowed_protocols);
1225
	} while ( $original_string != $string && ++$iterations < 6 );
1226
1227
	if ( $original_string != $string )
1228
		return '';
1229
1230
	return $string;
1231
}
1232
1233
/**
1234
 * Removes any invalid control characters in $string.
1235
 *
1236
 * Also removes any instance of the '\0' string.
1237
 *
1238
 * @since 1.0.0
1239
 *
1240
 * @param string $string
1241
 * @param array $options Set 'slash_zero' => 'keep' when '\0' is allowed. Default is 'remove'.
0 ignored issues
show
Should the type for parameter $options not be array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
1242
 * @return string
1243
 */
1244
function wp_kses_no_null( $string, $options = null ) {
1245
	if ( ! isset( $options['slash_zero'] ) ) {
1246
		$options = array( 'slash_zero' => 'remove' );
1247
	}
1248
1249
	$string = preg_replace( '/[\x00-\x08\x0B\x0C\x0E-\x1F]/', '', $string );
1250
	if ( 'remove' == $options['slash_zero'] ) {
1251
		$string = preg_replace( '/\\\\+0+/', '', $string );
1252
	}
1253
1254
	return $string;
1255
}
1256
1257
/**
1258
 * Strips slashes from in front of quotes.
1259
 *
1260
 * This function changes the character sequence \" to just ". It leaves all
1261
 * other slashes alone. It's really weird, but the quoting from
1262
 * preg_replace(//e) seems to require this.
1263
 *
1264
 * @since 1.0.0
1265
 *
1266
 * @param string $string String to strip slashes
1267
 * @return string Fixed string with quoted slashes
1268
 */
1269
function wp_kses_stripslashes($string) {
1270
	return preg_replace('%\\\\"%', '"', $string);
1271
}
1272
1273
/**
1274
 * Goes through an array and changes the keys to all lower case.
1275
 *
1276
 * @since 1.0.0
1277
 *
1278
 * @param array $inarray Unfiltered array
1279
 * @return array Fixed array with all lowercase keys
1280
 */
1281
function wp_kses_array_lc($inarray) {
1282
	$outarray = array ();
1283
1284
	foreach ( (array) $inarray as $inkey => $inval) {
1285
		$outkey = strtolower($inkey);
1286
		$outarray[$outkey] = array ();
1287
1288
		foreach ( (array) $inval as $inkey2 => $inval2) {
1289
			$outkey2 = strtolower($inkey2);
1290
			$outarray[$outkey][$outkey2] = $inval2;
1291
		} // foreach $inval
1292
	} // foreach $inarray
1293
1294
	return $outarray;
1295
}
1296
1297
/**
1298
 * Handles parsing errors in wp_kses_hair().
1299
 *
1300
 * The general plan is to remove everything to and including some whitespace,
1301
 * but it deals with quotes and apostrophes as well.
1302
 *
1303
 * @since 1.0.0
1304
 *
1305
 * @param string $string
1306
 * @return string
1307
 */
1308
function wp_kses_html_error($string) {
1309
	return preg_replace('/^("[^"]*("|$)|\'[^\']*(\'|$)|\S)*\s*/', '', $string);
1310
}
1311
1312
/**
1313
 * Sanitizes content from bad protocols and other characters.
1314
 *
1315
 * This function searches for URL protocols at the beginning of $string, while
1316
 * handling whitespace and HTML entities.
1317
 *
1318
 * @since 1.0.0
1319
 *
1320
 * @param string $string            Content to check for bad protocols
1321
 * @param string $allowed_protocols Allowed protocols
1322
 * @return string Sanitized content
1323
 */
1324
function wp_kses_bad_protocol_once($string, $allowed_protocols, $count = 1 ) {
1325
	$string2 = preg_split( '/:|&#0*58;|&#x0*3a;/i', $string, 2 );
1326
	if ( isset($string2[1]) && ! preg_match('%/\?%', $string2[0]) ) {
1327
		$string = trim( $string2[1] );
1328
		$protocol = wp_kses_bad_protocol_once2( $string2[0], $allowed_protocols );
1329
		if ( 'feed:' == $protocol ) {
1330
			if ( $count > 2 )
1331
				return '';
1332
			$string = wp_kses_bad_protocol_once( $string, $allowed_protocols, ++$count );
1333
			if ( empty( $string ) )
1334
				return $string;
1335
		}
1336
		$string = $protocol . $string;
1337
	}
1338
1339
	return $string;
1340
}
1341
1342
/**
1343
 * Callback for wp_kses_bad_protocol_once() regular expression.
1344
 *
1345
 * This function processes URL protocols, checks to see if they're in the
1346
 * whitelist or not, and returns different data depending on the answer.
1347
 *
1348
 * @access private
1349
 * @since 1.0.0
1350
 *
1351
 * @param string $string            URI scheme to check against the whitelist
1352
 * @param string $allowed_protocols Allowed protocols
1353
 * @return string Sanitized content
1354
 */
1355
function wp_kses_bad_protocol_once2( $string, $allowed_protocols ) {
1356
	$string2 = wp_kses_decode_entities($string);
1357
	$string2 = preg_replace('/\s/', '', $string2);
1358
	$string2 = wp_kses_no_null($string2);
1359
	$string2 = strtolower($string2);
1360
1361
	$allowed = false;
1362
	foreach ( (array) $allowed_protocols as $one_protocol )
1363
		if ( strtolower($one_protocol) == $string2 ) {
1364
			$allowed = true;
1365
			break;
1366
		}
1367
1368
	if ($allowed)
1369
		return "$string2:";
1370
	else
1371
		return '';
1372
}
1373
1374
/**
1375
 * Converts and fixes HTML entities.
1376
 *
1377
 * This function normalizes HTML entities. It will convert `AT&T` to the correct
1378
 * `AT&amp;T`, `&#00058;` to `&#58;`, `&#XYZZY;` to `&amp;#XYZZY;` and so on.
1379
 *
1380
 * @since 1.0.0
1381
 *
1382
 * @param string $string Content to normalize entities
1383
 * @return string Content with normalized entities
1384
 */
1385
function wp_kses_normalize_entities($string) {
1386
	// Disarm all entities by converting & to &amp;
1387
	$string = str_replace('&', '&amp;', $string);
1388
1389
	// Change back the allowed entities in our entity whitelist
1390
	$string = preg_replace_callback('/&amp;([A-Za-z]{2,8}[0-9]{0,2});/', 'wp_kses_named_entities', $string);
1391
	$string = preg_replace_callback('/&amp;#(0*[0-9]{1,7});/', 'wp_kses_normalize_entities2', $string);
1392
	$string = preg_replace_callback('/&amp;#[Xx](0*[0-9A-Fa-f]{1,6});/', 'wp_kses_normalize_entities3', $string);
1393
1394
	return $string;
1395
}
1396
1397
/**
1398
 * Callback for wp_kses_normalize_entities() regular expression.
1399
 *
1400
 * This function only accepts valid named entity references, which are finite,
1401
 * case-sensitive, and highly scrutinized by HTML and XML validators.
1402
 *
1403
 * @since 3.0.0
1404
 *
1405
 * @global array $allowedentitynames
1406
 *
1407
 * @param array $matches preg_replace_callback() matches array
1408
 * @return string Correctly encoded entity
1409
 */
1410
function wp_kses_named_entities($matches) {
1411
	global $allowedentitynames;
1412
1413
	if ( empty($matches[1]) )
1414
		return '';
1415
1416
	$i = $matches[1];
1417
	return ( ! in_array( $i, $allowedentitynames ) ) ? "&amp;$i;" : "&$i;";
1418
}
1419
1420
/**
1421
 * Callback for wp_kses_normalize_entities() regular expression.
1422
 *
1423
 * This function helps wp_kses_normalize_entities() to only accept 16-bit
1424
 * values and nothing more for `&#number;` entities.
1425
 *
1426
 * @access private
1427
 * @since 1.0.0
1428
 *
1429
 * @param array $matches preg_replace_callback() matches array
1430
 * @return string Correctly encoded entity
1431
 */
1432
function wp_kses_normalize_entities2($matches) {
1433
	if ( empty($matches[1]) )
1434
		return '';
1435
1436
	$i = $matches[1];
1437
	if (valid_unicode($i)) {
1438
		$i = str_pad(ltrim($i,'0'), 3, '0', STR_PAD_LEFT);
1439
		$i = "&#$i;";
1440
	} else {
1441
		$i = "&amp;#$i;";
1442
	}
1443
1444
	return $i;
1445
}
1446
1447
/**
1448
 * Callback for wp_kses_normalize_entities() for regular expression.
1449
 *
1450
 * This function helps wp_kses_normalize_entities() to only accept valid Unicode
1451
 * numeric entities in hex form.
1452
 *
1453
 * @since 2.7.0
1454
 * @access private
1455
 *
1456
 * @param array $matches preg_replace_callback() matches array
1457
 * @return string Correctly encoded entity
1458
 */
1459
function wp_kses_normalize_entities3($matches) {
1460
	if ( empty($matches[1]) )
1461
		return '';
1462
1463
	$hexchars = $matches[1];
1464
	return ( ! valid_unicode( hexdec( $hexchars ) ) ) ? "&amp;#x$hexchars;" : '&#x'.ltrim($hexchars,'0').';';
1465
}
1466
1467
/**
1468
 * Helper function to determine if a Unicode value is valid.
1469
 *
1470
 * @since 2.7.0
1471
 *
1472
 * @param int $i Unicode value
1473
 * @return bool True if the value was a valid Unicode number
1474
 */
1475
function valid_unicode($i) {
1476
	return ( $i == 0x9 || $i == 0xa || $i == 0xd ||
1477
			($i >= 0x20 && $i <= 0xd7ff) ||
1478
			($i >= 0xe000 && $i <= 0xfffd) ||
1479
			($i >= 0x10000 && $i <= 0x10ffff) );
1480
}
1481
1482
/**
1483
 * Convert all entities to their character counterparts.
1484
 *
1485
 * This function decodes numeric HTML entities (`&#65;` and `&#x41;`).
1486
 * It doesn't do anything with other entities like &auml;, but we don't
1487
 * need them in the URL protocol whitelisting system anyway.
1488
 *
1489
 * @since 1.0.0
1490
 *
1491
 * @param string $string Content to change entities
1492
 * @return string Content after decoded entities
1493
 */
1494
function wp_kses_decode_entities($string) {
1495
	$string = preg_replace_callback('/&#([0-9]+);/', '_wp_kses_decode_entities_chr', $string);
1496
	$string = preg_replace_callback('/&#[Xx]([0-9A-Fa-f]+);/', '_wp_kses_decode_entities_chr_hexdec', $string);
1497
1498
	return $string;
1499
}
1500
1501
/**
1502
 * Regex callback for wp_kses_decode_entities()
1503
 *
1504
 * @since 2.9.0
1505
 *
1506
 * @param array $match preg match
1507
 * @return string
1508
 */
1509
function _wp_kses_decode_entities_chr( $match ) {
1510
	return chr( $match[1] );
1511
}
1512
1513
/**
1514
 * Regex callback for wp_kses_decode_entities()
1515
 *
1516
 * @since 2.9.0
1517
 *
1518
 * @param array $match preg match
1519
 * @return string
1520
 */
1521
function _wp_kses_decode_entities_chr_hexdec( $match ) {
1522
	return chr( hexdec( $match[1] ) );
1523
}
1524
1525
/**
1526
 * Sanitize content with allowed HTML Kses rules.
1527
 *
1528
 * @since 1.0.0
1529
 *
1530
 * @param string $data Content to filter, expected to be escaped with slashes
1531
 * @return string Filtered content
1532
 */
1533
function wp_filter_kses( $data ) {
1534
	return addslashes( wp_kses( stripslashes( $data ), current_filter() ) );
1535
}
1536
1537
/**
1538
 * Sanitize content with allowed HTML Kses rules.
1539
 *
1540
 * @since 2.9.0
1541
 *
1542
 * @param string $data Content to filter, expected to not be escaped
1543
 * @return string Filtered content
1544
 */
1545
function wp_kses_data( $data ) {
1546
	return wp_kses( $data, current_filter() );
1547
}
1548
1549
/**
1550
 * Sanitize content for allowed HTML tags for post content.
1551
 *
1552
 * Post content refers to the page contents of the 'post' type and not $_POST
1553
 * data from forms.
1554
 *
1555
 * @since 2.0.0
1556
 *
1557
 * @param string $data Post content to filter, expected to be escaped with slashes
1558
 * @return string Filtered post content with allowed HTML tags and attributes intact.
1559
 */
1560
function wp_filter_post_kses( $data ) {
1561
	return addslashes( wp_kses( stripslashes( $data ), 'post' ) );
1562
}
1563
1564
/**
1565
 * Sanitize content for allowed HTML tags for post content.
1566
 *
1567
 * Post content refers to the page contents of the 'post' type and not $_POST
1568
 * data from forms.
1569
 *
1570
 * @since 2.9.0
1571
 *
1572
 * @param string $data Post content to filter
1573
 * @return string Filtered post content with allowed HTML tags and attributes intact.
1574
 */
1575
function wp_kses_post( $data ) {
1576
	return wp_kses( $data, 'post' );
1577
}
1578
1579
/**
1580
 * Navigates through an array, object, or scalar, and sanitizes content for
1581
 * allowed HTML tags for post content.
1582
 *
1583
 * @since 4.4.2
1584
 *
1585
 * @see map_deep()
1586
 *
1587
 * @param mixed $data The array, object, or scalar value to inspect.
1588
 * @return mixed The filtered content.
1589
 */
1590
function wp_kses_post_deep( $data ) {
1591
	return map_deep( $data, 'wp_kses_post' );
1592
}
1593
1594
/**
1595
 * Strips all of the HTML in the content.
1596
 *
1597
 * @since 2.1.0
1598
 *
1599
 * @param string $data Content to strip all HTML from
1600
 * @return string Filtered content without any HTML
1601
 */
1602
function wp_filter_nohtml_kses( $data ) {
1603
	return addslashes( wp_kses( stripslashes( $data ), 'strip' ) );
1604
}
1605
1606
/**
1607
 * Adds all Kses input form content filters.
1608
 *
1609
 * All hooks have default priority. The wp_filter_kses() function is added to
1610
 * the 'pre_comment_content' and 'title_save_pre' hooks.
1611
 *
1612
 * The wp_filter_post_kses() function is added to the 'content_save_pre',
1613
 * 'excerpt_save_pre', and 'content_filtered_save_pre' hooks.
1614
 *
1615
 * @since 2.0.0
1616
 */
1617 View Code Duplication
function kses_init_filters() {
0 ignored issues
show
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1618
	// Normal filtering
1619
	add_filter('title_save_pre', 'wp_filter_kses');
1620
1621
	// Comment filtering
1622
	if ( current_user_can( 'unfiltered_html' ) )
1623
		add_filter( 'pre_comment_content', 'wp_filter_post_kses' );
1624
	else
1625
		add_filter( 'pre_comment_content', 'wp_filter_kses' );
1626
1627
	// Post filtering
1628
	add_filter('content_save_pre', 'wp_filter_post_kses');
1629
	add_filter('excerpt_save_pre', 'wp_filter_post_kses');
1630
	add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
1631
}
1632
1633
/**
1634
 * Removes all Kses input form content filters.
1635
 *
1636
 * A quick procedural method to removing all of the filters that kses uses for
1637
 * content in WordPress Loop.
1638
 *
1639
 * Does not remove the kses_init() function from {@see 'init'} hook (priority is
1640
 * default). Also does not remove kses_init() function from {@see 'set_current_user'}
1641
 * hook (priority is also default).
1642
 *
1643
 * @since 2.0.6
1644
 */
1645 View Code Duplication
function kses_remove_filters() {
0 ignored issues
show
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1646
	// Normal filtering
1647
	remove_filter('title_save_pre', 'wp_filter_kses');
1648
1649
	// Comment filtering
1650
	remove_filter( 'pre_comment_content', 'wp_filter_post_kses' );
1651
	remove_filter( 'pre_comment_content', 'wp_filter_kses' );
1652
1653
	// Post filtering
1654
	remove_filter('content_save_pre', 'wp_filter_post_kses');
1655
	remove_filter('excerpt_save_pre', 'wp_filter_post_kses');
1656
	remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
1657
}
1658
1659
/**
1660
 * Sets up most of the Kses filters for input form content.
1661
 *
1662
 * If you remove the kses_init() function from {@see 'init'} hook and
1663
 * {@see 'set_current_user'} (priority is default), then none of the Kses filter hooks
1664
 * will be added.
1665
 *
1666
 * First removes all of the Kses filters in case the current user does not need
1667
 * to have Kses filter the content. If the user does not have unfiltered_html
1668
 * capability, then Kses filters are added.
1669
 *
1670
 * @since 2.0.0
1671
 */
1672
function kses_init() {
1673
	kses_remove_filters();
1674
1675
	if ( ! current_user_can( 'unfiltered_html' ) ) {
1676
		kses_init_filters();
1677
	}
1678
}
1679
1680
/**
1681
 * Inline CSS filter
1682
 *
1683
 * @since 2.8.1
1684
 *
1685
 * @param string $css        A string of CSS rules.
1686
 * @param string $deprecated Not used.
1687
 * @return string            Filtered string of CSS rules.
1688
 */
1689
function safecss_filter_attr( $css, $deprecated = '' ) {
1690
	if ( !empty( $deprecated ) )
1691
		_deprecated_argument( __FUNCTION__, '2.8.1' ); // Never implemented
1692
1693
	$css = wp_kses_no_null($css);
1694
	$css = str_replace(array("\n","\r","\t"), '', $css);
1695
1696
	if ( preg_match( '%[\\\\(&=}]|/\*%', $css ) ) // remove any inline css containing \ ( & } = or comments
1697
		return '';
1698
1699
	$css_array = explode( ';', trim( $css ) );
1700
1701
	/**
1702
	 * Filters list of allowed CSS attributes.
1703
	 *
1704
	 * @since 2.8.1
1705
	 * @since 4.4.0 Added support for `min-height`, `max-height`, `min-width`, and `max-width`.
1706
	 * @since 4.6.0 Added support for `list-style-type`.
1707
	 *
1708
	 * @param array $attr List of allowed CSS attributes.
1709
	 */
1710
	$allowed_attr = apply_filters( 'safe_style_css', array(
1711
		'background',
1712
		'background-color',
1713
1714
		'border',
1715
		'border-width',
1716
		'border-color',
1717
		'border-style',
1718
		'border-right',
1719
		'border-right-color',
1720
		'border-right-style',
1721
		'border-right-width',
1722
		'border-bottom',
1723
		'border-bottom-color',
1724
		'border-bottom-style',
1725
		'border-bottom-width',
1726
		'border-left',
1727
		'border-left-color',
1728
		'border-left-style',
1729
		'border-left-width',
1730
		'border-top',
1731
		'border-top-color',
1732
		'border-top-style',
1733
		'border-top-width',
1734
1735
		'border-spacing',
1736
		'border-collapse',
1737
		'caption-side',
1738
1739
		'color',
1740
		'font',
1741
		'font-family',
1742
		'font-size',
1743
		'font-style',
1744
		'font-variant',
1745
		'font-weight',
1746
		'letter-spacing',
1747
		'line-height',
1748
		'text-decoration',
1749
		'text-indent',
1750
		'text-align',
1751
1752
		'height',
1753
		'min-height',
1754
		'max-height',
1755
1756
		'width',
1757
		'min-width',
1758
		'max-width',
1759
1760
		'margin',
1761
		'margin-right',
1762
		'margin-bottom',
1763
		'margin-left',
1764
		'margin-top',
1765
1766
		'padding',
1767
		'padding-right',
1768
		'padding-bottom',
1769
		'padding-left',
1770
		'padding-top',
1771
1772
		'clear',
1773
		'cursor',
1774
		'direction',
1775
		'float',
1776
		'overflow',
1777
		'vertical-align',
1778
		'list-style-type',
1779
	) );
1780
1781
	if ( empty($allowed_attr) )
1782
		return $css;
1783
1784
	$css = '';
1785
	foreach ( $css_array as $css_item ) {
1786
		if ( $css_item == '' )
1787
			continue;
1788
		$css_item = trim( $css_item );
1789
		$found = false;
1790
		if ( strpos( $css_item, ':' ) === false ) {
1791
			$found = true;
1792
		} else {
1793
			$parts = explode( ':', $css_item );
1794
			if ( in_array( trim( $parts[0] ), $allowed_attr ) )
1795
				$found = true;
1796
		}
1797
		if ( $found ) {
1798
			if( $css != '' )
1799
				$css .= ';';
1800
			$css .= $css_item;
1801
		}
1802
	}
1803
1804
	return $css;
1805
}
1806
1807
/**
1808
 * Helper function to add global attributes to a tag in the allowed html list.
1809
 *
1810
 * @since 3.5.0
1811
 * @access private
1812
 *
1813
 * @param array $value An array of attributes.
1814
 * @return array The array of attributes with global attributes added.
1815
 */
1816
function _wp_add_global_attributes( $value ) {
1817
	$global_attributes = array(
1818
		'class' => true,
1819
		'id' => true,
1820
		'style' => true,
1821
		'title' => true,
1822
		'role' => true,
1823
	);
1824
1825
	if ( true === $value )
1826
		$value = array();
1827
1828
	if ( is_array( $value ) )
1829
		return array_merge( $value, $global_attributes );
1830
1831
	return $value;
1832
}
1833