Advertisement
Guest User

Untitled

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