PageRenderTime 60ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/kses.php

https://gitlab.com/math4youbyusgroupillinois/WordPress
PHP | 1545 lines | 948 code | 119 blank | 478 comment | 89 complexity | b5facd52d2567f257b8f5cd99c1c6657 MD5 | raw file
  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. * You can override this in a plugin.
  35. *
  36. * The wp_kses_allowed_html filter is more powerful and supplies context.
  37. * CUSTOM_TAGS is not recommended and should be considered deprecated.
  38. *
  39. * @see wp_kses_allowed_html()
  40. *
  41. * @since 1.2.0
  42. */
  43. if ( ! defined( 'CUSTOM_TAGS' ) )
  44. define( 'CUSTOM_TAGS', false );
  45. // Ensure that these variables are added to the global namespace
  46. // (e.g. if using namespaces / autoload in the current PHP environment).
  47. global $allowedposttags, $allowedtags, $allowedentitynames;
  48. if ( ! CUSTOM_TAGS ) {
  49. /**
  50. * Kses global for default allowable HTML tags.
  51. *
  52. * Can be override by using CUSTOM_TAGS constant.
  53. *
  54. * @global array $allowedposttags
  55. * @since 2.0.0
  56. */
  57. $allowedposttags = array(
  58. 'address' => array(),
  59. 'a' => array(
  60. 'href' => true,
  61. 'rel' => true,
  62. 'rev' => true,
  63. 'name' => true,
  64. 'target' => true,
  65. ),
  66. 'abbr' => array(),
  67. 'acronym' => array(),
  68. 'area' => array(
  69. 'alt' => true,
  70. 'coords' => true,
  71. 'href' => true,
  72. 'nohref' => true,
  73. 'shape' => true,
  74. 'target' => true,
  75. ),
  76. 'article' => array(
  77. 'align' => true,
  78. 'dir' => true,
  79. 'lang' => true,
  80. 'xml:lang' => true,
  81. ),
  82. 'aside' => array(
  83. 'align' => true,
  84. 'dir' => true,
  85. 'lang' => true,
  86. 'xml:lang' => true,
  87. ),
  88. 'audio' => array(
  89. 'autoplay' => true,
  90. 'controls' => true,
  91. 'loop' => true,
  92. 'muted' => true,
  93. 'preload' => true,
  94. 'src' => true,
  95. ),
  96. 'b' => array(),
  97. 'big' => array(),
  98. 'blockquote' => array(
  99. 'cite' => true,
  100. 'lang' => true,
  101. 'xml:lang' => true,
  102. ),
  103. 'br' => array(),
  104. 'button' => array(
  105. 'disabled' => true,
  106. 'name' => true,
  107. 'type' => true,
  108. 'value' => true,
  109. ),
  110. 'caption' => array(
  111. 'align' => true,
  112. ),
  113. 'cite' => array(
  114. 'dir' => true,
  115. 'lang' => true,
  116. ),
  117. 'code' => array(),
  118. 'col' => array(
  119. 'align' => true,
  120. 'char' => true,
  121. 'charoff' => true,
  122. 'span' => true,
  123. 'dir' => true,
  124. 'valign' => true,
  125. 'width' => true,
  126. ),
  127. 'colgroup' => array(
  128. 'align' => true,
  129. 'char' => true,
  130. 'charoff' => true,
  131. 'span' => true,
  132. 'valign' => true,
  133. 'width' => true,
  134. ),
  135. 'del' => array(
  136. 'datetime' => true,
  137. ),
  138. 'dd' => array(),
  139. 'dfn' => array(),
  140. 'details' => array(
  141. 'align' => true,
  142. 'dir' => true,
  143. 'lang' => true,
  144. 'open' => true,
  145. 'xml:lang' => true,
  146. ),
  147. 'div' => array(
  148. 'align' => true,
  149. 'dir' => true,
  150. 'lang' => true,
  151. 'xml:lang' => true,
  152. ),
  153. 'dl' => array(),
  154. 'dt' => array(),
  155. 'em' => array(),
  156. 'fieldset' => array(),
  157. 'figure' => array(
  158. 'align' => true,
  159. 'dir' => true,
  160. 'lang' => true,
  161. 'xml:lang' => true,
  162. ),
  163. 'figcaption' => array(
  164. 'align' => true,
  165. 'dir' => true,
  166. 'lang' => true,
  167. 'xml:lang' => true,
  168. ),
  169. 'font' => array(
  170. 'color' => true,
  171. 'face' => true,
  172. 'size' => true,
  173. ),
  174. 'footer' => array(
  175. 'align' => true,
  176. 'dir' => true,
  177. 'lang' => true,
  178. 'xml:lang' => true,
  179. ),
  180. 'form' => array(
  181. 'action' => true,
  182. 'accept' => true,
  183. 'accept-charset' => true,
  184. 'enctype' => true,
  185. 'method' => true,
  186. 'name' => true,
  187. 'target' => true,
  188. ),
  189. 'h1' => array(
  190. 'align' => true,
  191. ),
  192. 'h2' => array(
  193. 'align' => true,
  194. ),
  195. 'h3' => array(
  196. 'align' => true,
  197. ),
  198. 'h4' => array(
  199. 'align' => true,
  200. ),
  201. 'h5' => array(
  202. 'align' => true,
  203. ),
  204. 'h6' => array(
  205. 'align' => true,
  206. ),
  207. 'header' => array(
  208. 'align' => true,
  209. 'dir' => true,
  210. 'lang' => true,
  211. 'xml:lang' => true,
  212. ),
  213. 'hgroup' => array(
  214. 'align' => true,
  215. 'dir' => true,
  216. 'lang' => true,
  217. 'xml:lang' => true,
  218. ),
  219. 'hr' => array(
  220. 'align' => true,
  221. 'noshade' => true,
  222. 'size' => true,
  223. 'width' => true,
  224. ),
  225. 'i' => array(),
  226. 'img' => array(
  227. 'alt' => true,
  228. 'align' => true,
  229. 'border' => true,
  230. 'height' => true,
  231. 'hspace' => true,
  232. 'longdesc' => true,
  233. 'vspace' => true,
  234. 'src' => true,
  235. 'usemap' => true,
  236. 'width' => true,
  237. ),
  238. 'ins' => array(
  239. 'datetime' => true,
  240. 'cite' => true,
  241. ),
  242. 'kbd' => array(),
  243. 'label' => array(
  244. 'for' => true,
  245. ),
  246. 'legend' => array(
  247. 'align' => true,
  248. ),
  249. 'li' => array(
  250. 'align' => true,
  251. 'value' => true,
  252. ),
  253. 'map' => array(
  254. 'name' => true,
  255. ),
  256. 'mark' => array(),
  257. 'menu' => array(
  258. 'type' => true,
  259. ),
  260. 'nav' => array(
  261. 'align' => true,
  262. 'dir' => true,
  263. 'lang' => true,
  264. 'xml:lang' => true,
  265. ),
  266. 'p' => array(
  267. 'align' => true,
  268. 'dir' => true,
  269. 'lang' => true,
  270. 'xml:lang' => true,
  271. ),
  272. 'pre' => array(
  273. 'width' => true,
  274. ),
  275. 'q' => array(
  276. 'cite' => true,
  277. ),
  278. 's' => array(),
  279. 'samp' => array(),
  280. 'span' => array(
  281. 'dir' => true,
  282. 'align' => true,
  283. 'lang' => true,
  284. 'xml:lang' => true,
  285. ),
  286. 'section' => array(
  287. 'align' => true,
  288. 'dir' => true,
  289. 'lang' => true,
  290. 'xml:lang' => true,
  291. ),
  292. 'small' => array(),
  293. 'strike' => array(),
  294. 'strong' => array(),
  295. 'sub' => array(),
  296. 'summary' => array(
  297. 'align' => true,
  298. 'dir' => true,
  299. 'lang' => true,
  300. 'xml:lang' => true,
  301. ),
  302. 'sup' => array(),
  303. 'table' => array(
  304. 'align' => true,
  305. 'bgcolor' => true,
  306. 'border' => true,
  307. 'cellpadding' => true,
  308. 'cellspacing' => true,
  309. 'dir' => true,
  310. 'rules' => true,
  311. 'summary' => true,
  312. 'width' => true,
  313. ),
  314. 'tbody' => array(
  315. 'align' => true,
  316. 'char' => true,
  317. 'charoff' => true,
  318. 'valign' => true,
  319. ),
  320. 'td' => array(
  321. 'abbr' => true,
  322. 'align' => true,
  323. 'axis' => true,
  324. 'bgcolor' => true,
  325. 'char' => true,
  326. 'charoff' => true,
  327. 'colspan' => true,
  328. 'dir' => true,
  329. 'headers' => true,
  330. 'height' => true,
  331. 'nowrap' => true,
  332. 'rowspan' => true,
  333. 'scope' => true,
  334. 'valign' => true,
  335. 'width' => true,
  336. ),
  337. 'textarea' => array(
  338. 'cols' => true,
  339. 'rows' => true,
  340. 'disabled' => true,
  341. 'name' => true,
  342. 'readonly' => true,
  343. ),
  344. 'tfoot' => array(
  345. 'align' => true,
  346. 'char' => true,
  347. 'charoff' => true,
  348. 'valign' => true,
  349. ),
  350. 'th' => array(
  351. 'abbr' => true,
  352. 'align' => true,
  353. 'axis' => true,
  354. 'bgcolor' => true,
  355. 'char' => true,
  356. 'charoff' => true,
  357. 'colspan' => true,
  358. 'headers' => true,
  359. 'height' => true,
  360. 'nowrap' => true,
  361. 'rowspan' => true,
  362. 'scope' => true,
  363. 'valign' => true,
  364. 'width' => true,
  365. ),
  366. 'thead' => array(
  367. 'align' => true,
  368. 'char' => true,
  369. 'charoff' => true,
  370. 'valign' => true,
  371. ),
  372. 'title' => array(),
  373. 'tr' => array(
  374. 'align' => true,
  375. 'bgcolor' => true,
  376. 'char' => true,
  377. 'charoff' => true,
  378. 'valign' => true,
  379. ),
  380. 'track' => array(
  381. 'default' => true,
  382. 'kind' => true,
  383. 'label' => true,
  384. 'src' => true,
  385. 'srclang' => true,
  386. ),
  387. 'tt' => array(),
  388. 'u' => array(),
  389. 'ul' => array(
  390. 'type' => true,
  391. ),
  392. 'ol' => array(
  393. 'start' => true,
  394. 'type' => true,
  395. ),
  396. 'var' => array(),
  397. 'video' => array(
  398. 'autoplay' => true,
  399. 'controls' => true,
  400. 'height' => true,
  401. 'loop' => true,
  402. 'muted' => true,
  403. 'poster' => true,
  404. 'preload' => true,
  405. 'src' => true,
  406. 'width' => true,
  407. ),
  408. );
  409. /**
  410. * Kses allowed HTML elements.
  411. *
  412. * @global array $allowedtags
  413. * @since 1.0.0
  414. */
  415. $allowedtags = array(
  416. 'a' => array(
  417. 'href' => true,
  418. 'title' => true,
  419. ),
  420. 'abbr' => array(
  421. 'title' => true,
  422. ),
  423. 'acronym' => array(
  424. 'title' => true,
  425. ),
  426. 'b' => array(),
  427. 'blockquote' => array(
  428. 'cite' => true,
  429. ),
  430. 'cite' => array(),
  431. 'code' => array(),
  432. 'del' => array(
  433. 'datetime' => true,
  434. ),
  435. 'em' => array(),
  436. 'i' => array(),
  437. 'q' => array(
  438. 'cite' => true,
  439. ),
  440. 'strike' => array(),
  441. 'strong' => array(),
  442. );
  443. $allowedentitynames = array(
  444. 'nbsp', 'iexcl', 'cent', 'pound', 'curren', 'yen',
  445. 'brvbar', 'sect', 'uml', 'copy', 'ordf', 'laquo',
  446. 'not', 'shy', 'reg', 'macr', 'deg', 'plusmn',
  447. 'acute', 'micro', 'para', 'middot', 'cedil', 'ordm',
  448. 'raquo', 'iquest', 'Agrave', 'Aacute', 'Acirc', 'Atilde',
  449. 'Auml', 'Aring', 'AElig', 'Ccedil', 'Egrave', 'Eacute',
  450. 'Ecirc', 'Euml', 'Igrave', 'Iacute', 'Icirc', 'Iuml',
  451. 'ETH', 'Ntilde', 'Ograve', 'Oacute', 'Ocirc', 'Otilde',
  452. 'Ouml', 'times', 'Oslash', 'Ugrave', 'Uacute', 'Ucirc',
  453. 'Uuml', 'Yacute', 'THORN', 'szlig', 'agrave', 'aacute',
  454. 'acirc', 'atilde', 'auml', 'aring', 'aelig', 'ccedil',
  455. 'egrave', 'eacute', 'ecirc', 'euml', 'igrave', 'iacute',
  456. 'icirc', 'iuml', 'eth', 'ntilde', 'ograve', 'oacute',
  457. 'ocirc', 'otilde', 'ouml', 'divide', 'oslash', 'ugrave',
  458. 'uacute', 'ucirc', 'uuml', 'yacute', 'thorn', 'yuml',
  459. 'quot', 'amp', 'lt', 'gt', 'apos', 'OElig',
  460. 'oelig', 'Scaron', 'scaron', 'Yuml', 'circ', 'tilde',
  461. 'ensp', 'emsp', 'thinsp', 'zwnj', 'zwj', 'lrm',
  462. 'rlm', 'ndash', 'mdash', 'lsquo', 'rsquo', 'sbquo',
  463. 'ldquo', 'rdquo', 'bdquo', 'dagger', 'Dagger', 'permil',
  464. 'lsaquo', 'rsaquo', 'euro', 'fnof', 'Alpha', 'Beta',
  465. 'Gamma', 'Delta', 'Epsilon', 'Zeta', 'Eta', 'Theta',
  466. 'Iota', 'Kappa', 'Lambda', 'Mu', 'Nu', 'Xi',
  467. 'Omicron', 'Pi', 'Rho', 'Sigma', 'Tau', 'Upsilon',
  468. 'Phi', 'Chi', 'Psi', 'Omega', 'alpha', 'beta',
  469. 'gamma', 'delta', 'epsilon', 'zeta', 'eta', 'theta',
  470. 'iota', 'kappa', 'lambda', 'mu', 'nu', 'xi',
  471. 'omicron', 'pi', 'rho', 'sigmaf', 'sigma', 'tau',
  472. 'upsilon', 'phi', 'chi', 'psi', 'omega', 'thetasym',
  473. 'upsih', 'piv', 'bull', 'hellip', 'prime', 'Prime',
  474. 'oline', 'frasl', 'weierp', 'image', 'real', 'trade',
  475. 'alefsym', 'larr', 'uarr', 'rarr', 'darr', 'harr',
  476. 'crarr', 'lArr', 'uArr', 'rArr', 'dArr', 'hArr',
  477. 'forall', 'part', 'exist', 'empty', 'nabla', 'isin',
  478. 'notin', 'ni', 'prod', 'sum', 'minus', 'lowast',
  479. 'radic', 'prop', 'infin', 'ang', 'and', 'or',
  480. 'cap', 'cup', 'int', 'sim', 'cong', 'asymp',
  481. 'ne', 'equiv', 'le', 'ge', 'sub', 'sup',
  482. 'nsub', 'sube', 'supe', 'oplus', 'otimes', 'perp',
  483. 'sdot', 'lceil', 'rceil', 'lfloor', 'rfloor', 'lang',
  484. 'rang', 'loz', 'spades', 'clubs', 'hearts', 'diams',
  485. 'sup1', 'sup2', 'sup3', 'frac14', 'frac12', 'frac34',
  486. 'there4',
  487. );
  488. $allowedposttags = array_map( '_wp_add_global_attributes', $allowedposttags );
  489. } else {
  490. $allowedtags = wp_kses_array_lc( $allowedtags );
  491. $allowedposttags = wp_kses_array_lc( $allowedposttags );
  492. }
  493. /**
  494. * Filters content and keeps only allowable HTML elements.
  495. *
  496. * This function makes sure that only the allowed HTML element names, attribute
  497. * names and attribute values plus only sane HTML entities will occur in
  498. * $string. You have to remove any slashes from PHP's magic quotes before you
  499. * call this function.
  500. *
  501. * The default allowed protocols are 'http', 'https', 'ftp', 'mailto', 'news',
  502. * 'irc', 'gopher', 'nntp', 'feed', 'telnet, 'mms', 'rtsp' and 'svn'. This
  503. * covers all common link protocols, except for 'javascript' which should not
  504. * be allowed for untrusted users.
  505. *
  506. * @since 1.0.0
  507. *
  508. * @param string $string Content to filter through kses
  509. * @param array $allowed_html List of allowed HTML elements
  510. * @param array $allowed_protocols Optional. Allowed protocol in links.
  511. * @return string Filtered content with only allowed HTML elements
  512. */
  513. function wp_kses( $string, $allowed_html, $allowed_protocols = array() ) {
  514. if ( empty( $allowed_protocols ) )
  515. $allowed_protocols = wp_allowed_protocols();
  516. $string = wp_kses_no_null($string);
  517. $string = wp_kses_js_entities($string);
  518. $string = wp_kses_normalize_entities($string);
  519. $string = wp_kses_hook($string, $allowed_html, $allowed_protocols); // WP changed the order of these funcs and added args to wp_kses_hook
  520. return wp_kses_split($string, $allowed_html, $allowed_protocols);
  521. }
  522. /**
  523. * Return a list of allowed tags and attributes for a given context.
  524. *
  525. * @since 3.5.0
  526. *
  527. * @param string $context The context for which to retrieve tags. Allowed values are
  528. * post | strip | data | entities or the name of a field filter such as pre_user_description.
  529. * @return array List of allowed tags and their allowed attributes.
  530. */
  531. function wp_kses_allowed_html( $context = '' ) {
  532. global $allowedposttags, $allowedtags, $allowedentitynames;
  533. if ( is_array( $context ) ) {
  534. /**
  535. * Filter HTML elements allowed for a given context.
  536. *
  537. * @since 3.5.0
  538. *
  539. * @param string $tags Allowed tags, attributes, and/or entities.
  540. * @param string $context Context to judge allowed tags by. Allowed values are 'post',
  541. * 'data', 'strip', 'entities', 'explicit', or the name of a filter.
  542. */
  543. return apply_filters( 'wp_kses_allowed_html', $context, 'explicit' );
  544. }
  545. switch ( $context ) {
  546. case 'post':
  547. /** This filter is documented in wp-includes/kses.php */
  548. return apply_filters( 'wp_kses_allowed_html', $allowedposttags, $context );
  549. case 'user_description':
  550. case 'pre_user_description':
  551. $tags = $allowedtags;
  552. $tags['a']['rel'] = true;
  553. /** This filter is documented in wp-includes/kses.php */
  554. return apply_filters( 'wp_kses_allowed_html', $tags, $context );
  555. case 'strip':
  556. /** This filter is documented in wp-includes/kses.php */
  557. return apply_filters( 'wp_kses_allowed_html', array(), $context );
  558. case 'entities':
  559. /** This filter is documented in wp-includes/kses.php */
  560. return apply_filters( 'wp_kses_allowed_html', $allowedentitynames, $context);
  561. case 'data':
  562. default:
  563. /** This filter is documented in wp-includes/kses.php */
  564. return apply_filters( 'wp_kses_allowed_html', $allowedtags, $context );
  565. }
  566. }
  567. /**
  568. * You add any kses hooks here.
  569. *
  570. * There is currently only one kses WordPress hook and it is called here. All
  571. * parameters are passed to the hooks and expected to receive a string.
  572. *
  573. * @since 1.0.0
  574. *
  575. * @param string $string Content to filter through kses
  576. * @param array $allowed_html List of allowed HTML elements
  577. * @param array $allowed_protocols Allowed protocol in links
  578. * @return string Filtered content through 'pre_kses' hook
  579. */
  580. function wp_kses_hook( $string, $allowed_html, $allowed_protocols ) {
  581. /**
  582. * Filter content to be run through kses.
  583. *
  584. * @since 2.3.0
  585. *
  586. * @param string $string Content to run through kses.
  587. * @param array $allowed_html Allowed HTML elements.
  588. * @param array $allowed_protocols Allowed protocol in links.
  589. */
  590. $string = apply_filters( 'pre_kses', $string, $allowed_html, $allowed_protocols );
  591. return $string;
  592. }
  593. /**
  594. * This function returns kses' version number.
  595. *
  596. * @since 1.0.0
  597. *
  598. * @return string KSES Version Number
  599. */
  600. function wp_kses_version() {
  601. return '0.2.2';
  602. }
  603. /**
  604. * Searches for HTML tags, no matter how malformed.
  605. *
  606. * It also matches stray ">" characters.
  607. *
  608. * @since 1.0.0
  609. *
  610. * @param string $string Content to filter
  611. * @param array $allowed_html Allowed HTML elements
  612. * @param array $allowed_protocols Allowed protocols to keep
  613. * @return string Content with fixed HTML tags
  614. */
  615. function wp_kses_split( $string, $allowed_html, $allowed_protocols ) {
  616. global $pass_allowed_html, $pass_allowed_protocols;
  617. $pass_allowed_html = $allowed_html;
  618. $pass_allowed_protocols = $allowed_protocols;
  619. return preg_replace_callback( '%(<!--.*?(-->|$))|(<[^>]*(>|$)|>)%', '_wp_kses_split_callback', $string );
  620. }
  621. /**
  622. * Callback for wp_kses_split.
  623. *
  624. * @since 3.1.0
  625. * @access private
  626. */
  627. function _wp_kses_split_callback( $match ) {
  628. global $pass_allowed_html, $pass_allowed_protocols;
  629. return wp_kses_split2( $match[0], $pass_allowed_html, $pass_allowed_protocols );
  630. }
  631. /**
  632. * Callback for wp_kses_split for fixing malformed HTML tags.
  633. *
  634. * This function does a lot of work. It rejects some very malformed things like
  635. * <:::>. It returns an empty string, if the element isn't allowed (look ma, no
  636. * strip_tags()!). Otherwise it splits the tag into an element and an attribute
  637. * list.
  638. *
  639. * After the tag is split into an element and an attribute list, it is run
  640. * through another filter which will remove illegal attributes and once that is
  641. * completed, will be returned.
  642. *
  643. * @access private
  644. * @since 1.0.0
  645. *
  646. * @param string $string Content to filter
  647. * @param array $allowed_html Allowed HTML elements
  648. * @param array $allowed_protocols Allowed protocols to keep
  649. * @return string Fixed HTML element
  650. */
  651. function wp_kses_split2($string, $allowed_html, $allowed_protocols) {
  652. $string = wp_kses_stripslashes($string);
  653. if (substr($string, 0, 1) != '<')
  654. return '&gt;';
  655. // It matched a ">" character
  656. if ( '<!--' == substr( $string, 0, 4 ) ) {
  657. $string = str_replace( array('<!--', '-->'), '', $string );
  658. while ( $string != ($newstring = wp_kses($string, $allowed_html, $allowed_protocols)) )
  659. $string = $newstring;
  660. if ( $string == '' )
  661. return '';
  662. // prevent multiple dashes in comments
  663. $string = preg_replace('/--+/', '-', $string);
  664. // prevent three dashes closing a comment
  665. $string = preg_replace('/-$/', '', $string);
  666. return "<!--{$string}-->";
  667. }
  668. // Allow HTML comments
  669. if (!preg_match('%^<\s*(/\s*)?([a-zA-Z0-9]+)([^>]*)>?$%', $string, $matches))
  670. return '';
  671. // It's seriously malformed
  672. $slash = trim($matches[1]);
  673. $elem = $matches[2];
  674. $attrlist = $matches[3];
  675. if ( ! is_array( $allowed_html ) )
  676. $allowed_html = wp_kses_allowed_html( $allowed_html );
  677. if ( ! isset($allowed_html[strtolower($elem)]) )
  678. return '';
  679. // They are using a not allowed HTML element
  680. if ($slash != '')
  681. return "</$elem>";
  682. // No attributes are allowed for closing elements
  683. return wp_kses_attr( $elem, $attrlist, $allowed_html, $allowed_protocols );
  684. }
  685. /**
  686. * Removes all attributes, if none are allowed for this element.
  687. *
  688. * If some are allowed it calls wp_kses_hair() to split them further, and then
  689. * it builds up new HTML code from the data that kses_hair() returns. It also
  690. * removes "<" and ">" characters, if there are any left. One more thing it does
  691. * is to check if the tag has a closing XHTML slash, and if it does, it puts one
  692. * in the returned code as well.
  693. *
  694. * @since 1.0.0
  695. *
  696. * @param string $element HTML element/tag
  697. * @param string $attr HTML attributes from HTML element to closing HTML element tag
  698. * @param array $allowed_html Allowed HTML elements
  699. * @param array $allowed_protocols Allowed protocols to keep
  700. * @return string Sanitized HTML element
  701. */
  702. function wp_kses_attr($element, $attr, $allowed_html, $allowed_protocols) {
  703. // Is there a closing XHTML slash at the end of the attributes?
  704. if ( ! is_array( $allowed_html ) )
  705. $allowed_html = wp_kses_allowed_html( $allowed_html );
  706. $xhtml_slash = '';
  707. if (preg_match('%\s*/\s*$%', $attr))
  708. $xhtml_slash = ' /';
  709. // Are any attributes allowed at all for this element?
  710. if ( ! isset($allowed_html[strtolower($element)]) || count($allowed_html[strtolower($element)]) == 0 )
  711. return "<$element$xhtml_slash>";
  712. // Split it
  713. $attrarr = wp_kses_hair($attr, $allowed_protocols);
  714. // Go through $attrarr, and save the allowed attributes for this element
  715. // in $attr2
  716. $attr2 = '';
  717. $allowed_attr = $allowed_html[strtolower($element)];
  718. foreach ($attrarr as $arreach) {
  719. if ( ! isset( $allowed_attr[strtolower($arreach['name'])] ) )
  720. continue; // the attribute is not allowed
  721. $current = $allowed_attr[strtolower($arreach['name'])];
  722. if ( $current == '' )
  723. continue; // the attribute is not allowed
  724. if ( strtolower( $arreach['name'] ) == 'style' ) {
  725. $orig_value = $arreach['value'];
  726. $value = safecss_filter_attr( $orig_value );
  727. if ( empty( $value ) )
  728. continue;
  729. $arreach['value'] = $value;
  730. $arreach['whole'] = str_replace( $orig_value, $value, $arreach['whole'] );
  731. }
  732. if ( ! is_array($current) ) {
  733. $attr2 .= ' '.$arreach['whole'];
  734. // there are no checks
  735. } else {
  736. // there are some checks
  737. $ok = true;
  738. foreach ($current as $currkey => $currval) {
  739. if ( ! wp_kses_check_attr_val($arreach['value'], $arreach['vless'], $currkey, $currval) ) {
  740. $ok = false;
  741. break;
  742. }
  743. }
  744. if ( $ok )
  745. $attr2 .= ' '.$arreach['whole']; // it passed them
  746. } // if !is_array($current)
  747. } // foreach
  748. // Remove any "<" or ">" characters
  749. $attr2 = preg_replace('/[<>]/', '', $attr2);
  750. return "<$element$attr2$xhtml_slash>";
  751. }
  752. /**
  753. * Builds an attribute list from string containing attributes.
  754. *
  755. * This function does a lot of work. It parses an attribute list into an array
  756. * with attribute data, and tries to do the right thing even if it gets weird
  757. * input. It will add quotes around attribute values that don't have any quotes
  758. * or apostrophes around them, to make it easier to produce HTML code that will
  759. * conform to W3C's HTML specification. It will also remove bad URL protocols
  760. * from attribute values. It also reduces duplicate attributes by using the
  761. * attribute defined first (foo='bar' foo='baz' will result in foo='bar').
  762. *
  763. * @since 1.0.0
  764. *
  765. * @param string $attr Attribute list from HTML element to closing HTML element tag
  766. * @param array $allowed_protocols Allowed protocols to keep
  767. * @return array List of attributes after parsing
  768. */
  769. function wp_kses_hair($attr, $allowed_protocols) {
  770. $attrarr = array();
  771. $mode = 0;
  772. $attrname = '';
  773. $uris = array('xmlns', 'profile', 'href', 'src', 'cite', 'classid', 'codebase', 'data', 'usemap', 'longdesc', 'action');
  774. // Loop through the whole attribute list
  775. while (strlen($attr) != 0) {
  776. $working = 0; // Was the last operation successful?
  777. switch ($mode) {
  778. case 0 : // attribute name, href for instance
  779. if ( preg_match('/^([-a-zA-Z:]+)/', $attr, $match ) ) {
  780. $attrname = $match[1];
  781. $working = $mode = 1;
  782. $attr = preg_replace( '/^[-a-zA-Z:]+/', '', $attr );
  783. }
  784. break;
  785. case 1 : // equals sign or valueless ("selected")
  786. if (preg_match('/^\s*=\s*/', $attr)) // equals sign
  787. {
  788. $working = 1;
  789. $mode = 2;
  790. $attr = preg_replace('/^\s*=\s*/', '', $attr);
  791. break;
  792. }
  793. if (preg_match('/^\s+/', $attr)) // valueless
  794. {
  795. $working = 1;
  796. $mode = 0;
  797. if(false === array_key_exists($attrname, $attrarr)) {
  798. $attrarr[$attrname] = array ('name' => $attrname, 'value' => '', 'whole' => $attrname, 'vless' => 'y');
  799. }
  800. $attr = preg_replace('/^\s+/', '', $attr);
  801. }
  802. break;
  803. case 2 : // attribute value, a URL after href= for instance
  804. if (preg_match('%^"([^"]*)"(\s+|/?$)%', $attr, $match))
  805. // "value"
  806. {
  807. $thisval = $match[1];
  808. if ( in_array(strtolower($attrname), $uris) )
  809. $thisval = wp_kses_bad_protocol($thisval, $allowed_protocols);
  810. if(false === array_key_exists($attrname, $attrarr)) {
  811. $attrarr[$attrname] = array ('name' => $attrname, 'value' => $thisval, 'whole' => "$attrname=\"$thisval\"", 'vless' => 'n');
  812. }
  813. $working = 1;
  814. $mode = 0;
  815. $attr = preg_replace('/^"[^"]*"(\s+|$)/', '', $attr);
  816. break;
  817. }
  818. if (preg_match("%^'([^']*)'(\s+|/?$)%", $attr, $match))
  819. // 'value'
  820. {
  821. $thisval = $match[1];
  822. if ( in_array(strtolower($attrname), $uris) )
  823. $thisval = wp_kses_bad_protocol($thisval, $allowed_protocols);
  824. if(false === array_key_exists($attrname, $attrarr)) {
  825. $attrarr[$attrname] = array ('name' => $attrname, 'value' => $thisval, 'whole' => "$attrname='$thisval'", 'vless' => 'n');
  826. }
  827. $working = 1;
  828. $mode = 0;
  829. $attr = preg_replace("/^'[^']*'(\s+|$)/", '', $attr);
  830. break;
  831. }
  832. if (preg_match("%^([^\s\"']+)(\s+|/?$)%", $attr, $match))
  833. // value
  834. {
  835. $thisval = $match[1];
  836. if ( in_array(strtolower($attrname), $uris) )
  837. $thisval = wp_kses_bad_protocol($thisval, $allowed_protocols);
  838. if(false === array_key_exists($attrname, $attrarr)) {
  839. $attrarr[$attrname] = array ('name' => $attrname, 'value' => $thisval, 'whole' => "$attrname=\"$thisval\"", 'vless' => 'n');
  840. }
  841. // We add quotes to conform to W3C's HTML spec.
  842. $working = 1;
  843. $mode = 0;
  844. $attr = preg_replace("%^[^\s\"']+(\s+|$)%", '', $attr);
  845. }
  846. break;
  847. } // switch
  848. if ($working == 0) // not well formed, remove and try again
  849. {
  850. $attr = wp_kses_html_error($attr);
  851. $mode = 0;
  852. }
  853. } // while
  854. if ($mode == 1 && false === array_key_exists($attrname, $attrarr))
  855. // special case, for when the attribute list ends with a valueless
  856. // attribute like "selected"
  857. $attrarr[$attrname] = array ('name' => $attrname, 'value' => '', 'whole' => $attrname, 'vless' => 'y');
  858. return $attrarr;
  859. }
  860. /**
  861. * Performs different checks for attribute values.
  862. *
  863. * The currently implemented checks are "maxlen", "minlen", "maxval", "minval"
  864. * and "valueless".
  865. *
  866. * @since 1.0.0
  867. *
  868. * @param string $value Attribute value
  869. * @param string $vless Whether the value is valueless. Use 'y' or 'n'
  870. * @param string $checkname What $checkvalue is checking for.
  871. * @param mixed $checkvalue What constraint the value should pass
  872. * @return bool Whether check passes
  873. */
  874. function wp_kses_check_attr_val($value, $vless, $checkname, $checkvalue) {
  875. $ok = true;
  876. switch (strtolower($checkname)) {
  877. case 'maxlen' :
  878. // The maxlen check makes sure that the attribute value has a length not
  879. // greater than the given value. This can be used to avoid Buffer Overflows
  880. // in WWW clients and various Internet servers.
  881. if (strlen($value) > $checkvalue)
  882. $ok = false;
  883. break;
  884. case 'minlen' :
  885. // The minlen check makes sure that the attribute value has a length not
  886. // smaller than the given value.
  887. if (strlen($value) < $checkvalue)
  888. $ok = false;
  889. break;
  890. case 'maxval' :
  891. // The maxval check does two things: it checks that the attribute value is
  892. // an integer from 0 and up, without an excessive amount of zeroes or
  893. // whitespace (to avoid Buffer Overflows). It also checks that the attribute
  894. // value is not greater than the given value.
  895. // This check can be used to avoid Denial of Service attacks.
  896. if (!preg_match('/^\s{0,6}[0-9]{1,6}\s{0,6}$/', $value))
  897. $ok = false;
  898. if ($value > $checkvalue)
  899. $ok = false;
  900. break;
  901. case 'minval' :
  902. // The minval check makes sure that the attribute value is a positive integer,
  903. // and that it is not smaller than the given value.
  904. if (!preg_match('/^\s{0,6}[0-9]{1,6}\s{0,6}$/', $value))
  905. $ok = false;
  906. if ($value < $checkvalue)
  907. $ok = false;
  908. break;
  909. case 'valueless' :
  910. // The valueless check makes sure if the attribute has a value
  911. // (like <a href="blah">) or not (<option selected>). If the given value
  912. // is a "y" or a "Y", the attribute must not have a value.
  913. // If the given value is an "n" or an "N", the attribute must have one.
  914. if (strtolower($checkvalue) != $vless)
  915. $ok = false;
  916. break;
  917. } // switch
  918. return $ok;
  919. }
  920. /**
  921. * Sanitize string from bad protocols.
  922. *
  923. * This function removes all non-allowed protocols from the beginning of
  924. * $string. It ignores whitespace and the case of the letters, and it does
  925. * understand HTML entities. It does its work in a while loop, so it won't be
  926. * fooled by a string like "javascript:javascript:alert(57)".
  927. *
  928. * @since 1.0.0
  929. *
  930. * @param string $string Content to filter bad protocols from
  931. * @param array $allowed_protocols Allowed protocols to keep
  932. * @return string Filtered content
  933. */
  934. function wp_kses_bad_protocol($string, $allowed_protocols) {
  935. $string = wp_kses_no_null($string);
  936. $iterations = 0;
  937. do {
  938. $original_string = $string;
  939. $string = wp_kses_bad_protocol_once($string, $allowed_protocols);
  940. } while ( $original_string != $string && ++$iterations < 6 );
  941. if ( $original_string != $string )
  942. return '';
  943. return $string;
  944. }
  945. /**
  946. * Removes any invalid control characters in $string.
  947. *
  948. * Also removes any instance of the '\0' string.
  949. *
  950. * @since 1.0.0
  951. *
  952. * @param string $string
  953. * @return string
  954. */
  955. function wp_kses_no_null($string) {
  956. $string = preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F]/', '', $string);
  957. $string = preg_replace('/(\\\\0)+/', '', $string);
  958. return $string;
  959. }
  960. /**
  961. * Strips slashes from in front of quotes.
  962. *
  963. * This function changes the character sequence \" to just ". It leaves all
  964. * other slashes alone. It's really weird, but the quoting from
  965. * preg_replace(//e) seems to require this.
  966. *
  967. * @since 1.0.0
  968. *
  969. * @param string $string String to strip slashes
  970. * @return string Fixed string with quoted slashes
  971. */
  972. function wp_kses_stripslashes($string) {
  973. return preg_replace('%\\\\"%', '"', $string);
  974. }
  975. /**
  976. * Goes through an array and changes the keys to all lower case.
  977. *
  978. * @since 1.0.0
  979. *
  980. * @param array $inarray Unfiltered array
  981. * @return array Fixed array with all lowercase keys
  982. */
  983. function wp_kses_array_lc($inarray) {
  984. $outarray = array ();
  985. foreach ( (array) $inarray as $inkey => $inval) {
  986. $outkey = strtolower($inkey);
  987. $outarray[$outkey] = array ();
  988. foreach ( (array) $inval as $inkey2 => $inval2) {
  989. $outkey2 = strtolower($inkey2);
  990. $outarray[$outkey][$outkey2] = $inval2;
  991. } // foreach $inval
  992. } // foreach $inarray
  993. return $outarray;
  994. }
  995. /**
  996. * Removes the HTML JavaScript entities found in early versions of Netscape 4.
  997. *
  998. * @since 1.0.0
  999. *
  1000. * @param string $string
  1001. * @return string
  1002. */
  1003. function wp_kses_js_entities($string) {
  1004. return preg_replace('%&\s*\{[^}]*(\}\s*;?|$)%', '', $string);
  1005. }
  1006. /**
  1007. * Handles parsing errors in wp_kses_hair().
  1008. *
  1009. * The general plan is to remove everything to and including some whitespace,
  1010. * but it deals with quotes and apostrophes as well.
  1011. *
  1012. * @since 1.0.0
  1013. *
  1014. * @param string $string
  1015. * @return string
  1016. */
  1017. function wp_kses_html_error($string) {
  1018. return preg_replace('/^("[^"]*("|$)|\'[^\']*(\'|$)|\S)*\s*/', '', $string);
  1019. }
  1020. /**
  1021. * Sanitizes content from bad protocols and other characters.
  1022. *
  1023. * This function searches for URL protocols at the beginning of $string, while
  1024. * handling whitespace and HTML entities.
  1025. *
  1026. * @since 1.0.0
  1027. *
  1028. * @param string $string Content to check for bad protocols
  1029. * @param string $allowed_protocols Allowed protocols
  1030. * @return string Sanitized content
  1031. */
  1032. function wp_kses_bad_protocol_once($string, $allowed_protocols, $count = 1 ) {
  1033. $string2 = preg_split( '/:|&#0*58;|&#x0*3a;/i', $string, 2 );
  1034. if ( isset($string2[1]) && ! preg_match('%/\?%', $string2[0]) ) {
  1035. $string = trim( $string2[1] );
  1036. $protocol = wp_kses_bad_protocol_once2( $string2[0], $allowed_protocols );
  1037. if ( 'feed:' == $protocol ) {
  1038. if ( $count > 2 )
  1039. return '';
  1040. $string = wp_kses_bad_protocol_once( $string, $allowed_protocols, ++$count );
  1041. if ( empty( $string ) )
  1042. return $string;
  1043. }
  1044. $string = $protocol . $string;
  1045. }
  1046. return $string;
  1047. }
  1048. /**
  1049. * Callback for wp_kses_bad_protocol_once() regular expression.
  1050. *
  1051. * This function processes URL protocols, checks to see if they're in the
  1052. * whitelist or not, and returns different data depending on the answer.
  1053. *
  1054. * @access private
  1055. * @since 1.0.0
  1056. *
  1057. * @param string $string URI scheme to check against the whitelist
  1058. * @param string $allowed_protocols Allowed protocols
  1059. * @return string Sanitized content
  1060. */
  1061. function wp_kses_bad_protocol_once2( $string, $allowed_protocols ) {
  1062. $string2 = wp_kses_decode_entities($string);
  1063. $string2 = preg_replace('/\s/', '', $string2);
  1064. $string2 = wp_kses_no_null($string2);
  1065. $string2 = strtolower($string2);
  1066. $allowed = false;
  1067. foreach ( (array) $allowed_protocols as $one_protocol )
  1068. if ( strtolower($one_protocol) == $string2 ) {
  1069. $allowed = true;
  1070. break;
  1071. }
  1072. if ($allowed)
  1073. return "$string2:";
  1074. else
  1075. return '';
  1076. }
  1077. /**
  1078. * Converts and fixes HTML entities.
  1079. *
  1080. * This function normalizes HTML entities. It will convert `AT&T` to the correct
  1081. * `AT&amp;T`, `&#00058;` to `&#58;`, `&#XYZZY;` to `&amp;#XYZZY;` and so on.
  1082. *
  1083. * @since 1.0.0
  1084. *
  1085. * @param string $string Content to normalize entities
  1086. * @return string Content with normalized entities
  1087. */
  1088. function wp_kses_normalize_entities($string) {
  1089. // Disarm all entities by converting & to &amp;
  1090. $string = str_replace('&', '&amp;', $string);
  1091. // Change back the allowed entities in our entity whitelist
  1092. $string = preg_replace_callback('/&amp;([A-Za-z]{2,8}[0-9]{0,2});/', 'wp_kses_named_entities', $string);
  1093. $string = preg_replace_callback('/&amp;#(0*[0-9]{1,7});/', 'wp_kses_normalize_entities2', $string);
  1094. $string = preg_replace_callback('/&amp;#[Xx](0*[0-9A-Fa-f]{1,6});/', 'wp_kses_normalize_entities3', $string);
  1095. return $string;
  1096. }
  1097. /**
  1098. * Callback for wp_kses_normalize_entities() regular expression.
  1099. *
  1100. * This function only accepts valid named entity references, which are finite,
  1101. * case-sensitive, and highly scrutinized by HTML and XML validators.
  1102. *
  1103. * @since 3.0.0
  1104. *
  1105. * @param array $matches preg_replace_callback() matches array
  1106. * @return string Correctly encoded entity
  1107. */
  1108. function wp_kses_named_entities($matches) {
  1109. global $allowedentitynames;
  1110. if ( empty($matches[1]) )
  1111. return '';
  1112. $i = $matches[1];
  1113. return ( ( ! in_array($i, $allowedentitynames) ) ? "&amp;$i;" : "&$i;" );
  1114. }
  1115. /**
  1116. * Callback for wp_kses_normalize_entities() regular expression.
  1117. *
  1118. * This function helps {@see wp_kses_normalize_entities()} to only accept 16-bit
  1119. * values and nothing more for `&#number;` entities.
  1120. *
  1121. * @access private
  1122. * @since 1.0.0
  1123. *
  1124. * @param array $matches preg_replace_callback() matches array
  1125. * @return string Correctly encoded entity
  1126. */
  1127. function wp_kses_normalize_entities2($matches) {
  1128. if ( empty($matches[1]) )
  1129. return '';
  1130. $i = $matches[1];
  1131. if (valid_unicode($i)) {
  1132. $i = str_pad(ltrim($i,'0'), 3, '0', STR_PAD_LEFT);
  1133. $i = "&#$i;";
  1134. } else {
  1135. $i = "&amp;#$i;";
  1136. }
  1137. return $i;
  1138. }
  1139. /**
  1140. * Callback for wp_kses_normalize_entities() for regular expression.
  1141. *
  1142. * This function helps wp_kses_normalize_entities() to only accept valid Unicode
  1143. * numeric entities in hex form.
  1144. *
  1145. * @access private
  1146. *
  1147. * @param array $matches preg_replace_callback() matches array
  1148. * @return string Correctly encoded entity
  1149. */
  1150. function wp_kses_normalize_entities3($matches) {
  1151. if ( empty($matches[1]) )
  1152. return '';
  1153. $hexchars = $matches[1];
  1154. return ( ( ! valid_unicode(hexdec($hexchars)) ) ? "&amp;#x$hexchars;" : '&#x'.ltrim($hexchars,'0').';' );
  1155. }
  1156. /**
  1157. * Helper function to determine if a Unicode value is valid.
  1158. *
  1159. * @param int $i Unicode value
  1160. * @return bool True if the value was a valid Unicode number
  1161. */
  1162. function valid_unicode($i) {
  1163. return ( $i == 0x9 || $i == 0xa || $i == 0xd ||
  1164. ($i >= 0x20 && $i <= 0xd7ff) ||
  1165. ($i >= 0xe000 && $i <= 0xfffd) ||
  1166. ($i >= 0x10000 && $i <= 0x10ffff) );
  1167. }
  1168. /**
  1169. * Convert all entities to their character counterparts.
  1170. *
  1171. * This function decodes numeric HTML entities (`&#65;` and `&#x41;`).
  1172. * It doesn't do anything with other entities like &auml;, but we don't
  1173. * need them in the URL protocol whitelisting system anyway.
  1174. *
  1175. * @since 1.0.0
  1176. *
  1177. * @param string $string Content to change entities
  1178. * @return string Content after decoded entities
  1179. */
  1180. function wp_kses_decode_entities($string) {
  1181. $string = preg_replace_callback('/&#([0-9]+);/', '_wp_kses_decode_entities_chr', $string);
  1182. $string = preg_replace_callback('/&#[Xx]([0-9A-Fa-f]+);/', '_wp_kses_decode_entities_chr_hexdec', $string);
  1183. return $string;
  1184. }
  1185. /**
  1186. * Regex callback for wp_kses_decode_entities()
  1187. *
  1188. * @param array $match preg match
  1189. * @return string
  1190. */
  1191. function _wp_kses_decode_entities_chr( $match ) {
  1192. return chr( $match[1] );
  1193. }
  1194. /**
  1195. * Regex callback for wp_kses_decode_entities()
  1196. *
  1197. * @param array $match preg match
  1198. * @return string
  1199. */
  1200. function _wp_kses_decode_entities_chr_hexdec( $match ) {
  1201. return chr( hexdec( $match[1] ) );
  1202. }
  1203. /**
  1204. * Sanitize content with allowed HTML Kses rules.
  1205. *
  1206. * @since 1.0.0
  1207. *
  1208. * @param string $data Content to filter, expected to be escaped with slashes
  1209. * @return string Filtered content
  1210. */
  1211. function wp_filter_kses( $data ) {
  1212. return addslashes( wp_kses( stripslashes( $data ), current_filter() ) );
  1213. }
  1214. /**
  1215. * Sanitize content with allowed HTML Kses rules.
  1216. *
  1217. * @since 2.9.0
  1218. *
  1219. * @param string $data Content to filter, expected to not be escaped
  1220. * @return string Filtered content
  1221. */
  1222. function wp_kses_data( $data ) {
  1223. return wp_kses( $data , current_filter() );
  1224. }
  1225. /**
  1226. * Sanitize content for allowed HTML tags for post content.
  1227. *
  1228. * Post content refers to the page contents of the 'post' type and not $_POST
  1229. * data from forms.
  1230. *
  1231. * @since 2.0.0
  1232. *
  1233. * @param string $data Post content to filter, expected to be escaped with slashes
  1234. * @return string Filtered post content with allowed HTML tags and attributes intact.
  1235. */
  1236. function wp_filter_post_kses($data) {
  1237. return addslashes ( wp_kses( stripslashes( $data ), 'post' ) );
  1238. }
  1239. /**
  1240. * Sanitize content for allowed HTML tags for post content.
  1241. *
  1242. * Post content refers to the page contents of the 'post' type and not $_POST
  1243. * data from forms.
  1244. *
  1245. * @since 2.9.0
  1246. *
  1247. * @param string $data Post content to filter
  1248. * @return string Filtered post content with allowed HTML tags and attributes intact.
  1249. */
  1250. function wp_kses_post($data) {
  1251. return wp_kses( $data , 'post' );
  1252. }
  1253. /**
  1254. * Strips all of the HTML in the content.
  1255. *
  1256. * @since 2.1.0
  1257. *
  1258. * @param string $data Content to strip all HTML from
  1259. * @return string Filtered content without any HTML
  1260. */
  1261. function wp_filter_nohtml_kses( $data ) {
  1262. return addslashes ( wp_kses( stripslashes( $data ), 'strip' ) );
  1263. }
  1264. /**
  1265. * Adds all Kses input form content filters.
  1266. *
  1267. * All hooks have default priority. The wp_filter_kses() function is added to
  1268. * the 'pre_comment_content' and 'title_save_pre' hooks.
  1269. *
  1270. * The wp_filter_post_kses() function is added to the 'content_save_pre',
  1271. * 'excerpt_save_pre', and 'content_filtered_save_pre' hooks.
  1272. *
  1273. * @since 2.0.0
  1274. */
  1275. function kses_init_filters() {
  1276. // Normal filtering
  1277. add_filter('title_save_pre', 'wp_filter_kses');
  1278. // Comment filtering
  1279. if ( current_user_can( 'unfiltered_html' ) )
  1280. add_filter( 'pre_comment_content', 'wp_filter_post_kses' );
  1281. else
  1282. add_filter( 'pre_comment_content', 'wp_filter_kses' );
  1283. // Post filtering
  1284. add_filter('content_save_pre', 'wp_filter_post_kses');
  1285. add_filter('excerpt_save_pre', 'wp_filter_post_kses');
  1286. add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
  1287. }
  1288. /**
  1289. * Removes all Kses input form content filters.
  1290. *
  1291. * A quick procedural method to removing all of the filters that kses uses for
  1292. * content in WordPress Loop.
  1293. *
  1294. * Does not remove the kses_init() function from 'init' hook (priority is
  1295. * default). Also does not remove kses_init() function from 'set_current_user'
  1296. * hook (priority is also default).
  1297. *
  1298. * @since 2.0.6
  1299. */
  1300. function kses_remove_filters() {
  1301. // Normal filtering
  1302. remove_filter('title_save_pre', 'wp_filter_kses');
  1303. // Comment filtering
  1304. remove_filter( 'pre_comment_content', 'wp_filter_post_kses' );
  1305. remove_filter( 'pre_comment_content', 'wp_filter_kses' );
  1306. // Post filtering
  1307. remove_filter('content_save_pre', 'wp_filter_post_kses');
  1308. remove_filter('excerpt_save_pre', 'wp_filter_post_kses');
  1309. remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
  1310. }
  1311. /**
  1312. * Sets up most of the Kses filters for input form content.
  1313. *
  1314. * If you remove the kses_init() function from 'init' hook and
  1315. * 'set_current_user' (priority is default), then none of the Kses filter hooks
  1316. * will be added.
  1317. *
  1318. * First removes all of the Kses filters in case the current user does not need
  1319. * to have Kses filter the content. If the user does not have unfiltered_html
  1320. * capability, then Kses filters are added.
  1321. *
  1322. * @since 2.0.0
  1323. */
  1324. function kses_init() {
  1325. kses_remove_filters();
  1326. if (current_user_can('unfiltered_html') == false)
  1327. kses_init_filters();
  1328. }
  1329. add_action('init', 'kses_init');
  1330. add_action('set_current_user', 'kses_init');
  1331. /**
  1332. * Inline CSS filter
  1333. *
  1334. * @since 2.8.1
  1335. */
  1336. function safecss_filter_attr( $css, $deprecated = '' ) {
  1337. if ( !empty( $deprecated ) )
  1338. _deprecated_argument( __FUNCTION__, '2.8.1' ); // Never implemented
  1339. $css = wp_kses_no_null($css);
  1340. $css = str_replace(array("\n","\r","\t"), '', $css);
  1341. if ( preg_match( '%[\\\\(&=}]|/\*%', $css ) ) // remove any inline css containing \ ( & } = or comments
  1342. return '';
  1343. $css_array = explode( ';', trim( $css ) );
  1344. /**
  1345. * Filter list of allowed CSS attributes.
  1346. *
  1347. * @since 2.8.1
  1348. *
  1349. * @param array $attr List of allowed CSS attributes.
  1350. */
  1351. $allowed_attr = apply_filters( 'safe_style_css', array( 'text-align', 'margin', 'color', 'float',
  1352. 'border', 'background', 'background-color', 'border-bottom', 'border-bottom-color',
  1353. 'border-bottom-style', 'border-bottom-width', 'border-collapse', 'border-color', 'border-left',
  1354. 'border-left-color', 'border-left-style', 'border-left-width', 'border-right', 'border-right-color',
  1355. 'border-right-style', 'border-right-width', 'border-spacing', 'border-style', 'border-top',
  1356. 'border-top-color', 'border-top-style', 'border-top-width', 'border-width', 'caption-side',
  1357. 'clear', 'cursor', 'direction', 'font', 'font-family', 'font-size', 'font-style',
  1358. 'font-variant', 'font-weight', 'height', 'letter-spacing', 'line-height', 'margin-bottom',
  1359. 'margin-left', 'margin-right', 'margin-top', 'overflow', 'padding', 'padding-bottom',
  1360. 'padding-left', 'padding-right', 'padding-top', 'text-decoration', 'text-indent', 'vertical-align',
  1361. 'width' ) );
  1362. if ( empty($allowed_attr) )
  1363. return $css;
  1364. $css = '';
  1365. foreach ( $css_array as $css_item ) {
  1366. if ( $css_item == '' )
  1367. continue;
  1368. $css_item = trim( $css_item );
  1369. $found = false;
  1370. if ( strpos( $css_item, ':' ) === false ) {
  1371. $found = true;
  1372. } else {
  1373. $parts = explode( ':', $css_item );
  1374. if ( in_array( trim( $parts[0] ), $allowed_attr ) )
  1375. $found = true;
  1376. }
  1377. if ( $found ) {
  1378. if( $css != '' )
  1379. $css .= ';';
  1380. $css .= $css_item;
  1381. }
  1382. }
  1383. return $css;
  1384. }
  1385. /**
  1386. * Helper function to add global attributes to a tag in the allowed html list.
  1387. *
  1388. * @since 3.5.0
  1389. * @access private
  1390. *
  1391. * @param array $value An array of attributes.
  1392. * @return array The array of attributes with global attributes added.
  1393. */
  1394. function _wp_add_global_attributes( $value ) {
  1395. $global_attributes = array(
  1396. 'class' => true,
  1397. 'id' => true,
  1398. 'style' => true,
  1399. 'title' => true,
  1400. 'role' => true,
  1401. );
  1402. if ( true === $value )
  1403. $value = array();
  1404. if ( is_array( $value ) )
  1405. return array_merge( $value, $global_attributes );
  1406. return $value;
  1407. }