kses.php 49 KB

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