kses.php 50 KB

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