PageRenderTime 64ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/kses.php

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