PageRenderTime 67ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/WordPress/wp-includes/kses.php

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