kses.php 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312
  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. * Specifies the default allowable HTML tags.
  34. *
  35. * Using `CUSTOM_TAGS` is not recommended and should be considered deprecated. The
  36. * {@see 'wp_kses_allowed_html'} filter is more powerful and supplies context.
  37. *
  38. * @see wp_kses_allowed_html()
  39. * @since 1.2.0
  40. *
  41. * @var array[]|bool Array of default allowable HTML tags, or false to use the defaults.
  42. */
  43. if ( ! defined( 'CUSTOM_TAGS' ) ) {
  44. define( 'CUSTOM_TAGS', false );
  45. }
  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 overridden with the `CUSTOM_TAGS` constant.
  54. *
  55. * @var array[] $allowedposttags Array of default allowable HTML tags.
  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. 'download' => array(
  67. 'valueless' => 'y',
  68. ),
  69. ),
  70. 'abbr' => array(),
  71. 'acronym' => array(),
  72. 'area' => array(
  73. 'alt' => true,
  74. 'coords' => true,
  75. 'href' => true,
  76. 'nohref' => true,
  77. 'shape' => true,
  78. 'target' => true,
  79. ),
  80. 'article' => array(
  81. 'align' => true,
  82. 'dir' => true,
  83. 'lang' => true,
  84. 'xml:lang' => true,
  85. ),
  86. 'aside' => array(
  87. 'align' => true,
  88. 'dir' => true,
  89. 'lang' => true,
  90. 'xml:lang' => true,
  91. ),
  92. 'audio' => array(
  93. 'autoplay' => true,
  94. 'controls' => true,
  95. 'loop' => true,
  96. 'muted' => true,
  97. 'preload' => true,
  98. 'src' => true,
  99. ),
  100. 'b' => array(),
  101. 'bdo' => array(
  102. 'dir' => true,
  103. ),
  104. 'big' => array(),
  105. 'blockquote' => array(
  106. 'cite' => true,
  107. 'lang' => true,
  108. 'xml:lang' => true,
  109. ),
  110. 'br' => array(),
  111. 'button' => array(
  112. 'disabled' => true,
  113. 'name' => true,
  114. 'type' => true,
  115. 'value' => true,
  116. ),
  117. 'caption' => array(
  118. 'align' => true,
  119. ),
  120. 'cite' => array(
  121. 'dir' => true,
  122. 'lang' => true,
  123. ),
  124. 'code' => array(),
  125. 'col' => array(
  126. 'align' => true,
  127. 'char' => true,
  128. 'charoff' => true,
  129. 'span' => true,
  130. 'dir' => true,
  131. 'valign' => true,
  132. 'width' => true,
  133. ),
  134. 'colgroup' => array(
  135. 'align' => true,
  136. 'char' => true,
  137. 'charoff' => true,
  138. 'span' => true,
  139. 'valign' => true,
  140. 'width' => true,
  141. ),
  142. 'del' => array(
  143. 'datetime' => true,
  144. ),
  145. 'dd' => array(),
  146. 'dfn' => array(),
  147. 'details' => array(
  148. 'align' => true,
  149. 'dir' => true,
  150. 'lang' => true,
  151. 'open' => true,
  152. 'xml:lang' => true,
  153. ),
  154. 'div' => array(
  155. 'align' => true,
  156. 'dir' => true,
  157. 'lang' => true,
  158. 'xml:lang' => true,
  159. ),
  160. 'dl' => array(),
  161. 'dt' => array(),
  162. 'em' => array(),
  163. 'fieldset' => array(),
  164. 'figure' => array(
  165. 'align' => true,
  166. 'dir' => true,
  167. 'lang' => true,
  168. 'xml:lang' => true,
  169. ),
  170. 'figcaption' => array(
  171. 'align' => true,
  172. 'dir' => true,
  173. 'lang' => true,
  174. 'xml:lang' => true,
  175. ),
  176. 'font' => array(
  177. 'color' => true,
  178. 'face' => true,
  179. 'size' => true,
  180. ),
  181. 'footer' => array(
  182. 'align' => true,
  183. 'dir' => true,
  184. 'lang' => true,
  185. 'xml:lang' => true,
  186. ),
  187. 'h1' => array(
  188. 'align' => true,
  189. ),
  190. 'h2' => array(
  191. 'align' => true,
  192. ),
  193. 'h3' => array(
  194. 'align' => true,
  195. ),
  196. 'h4' => array(
  197. 'align' => true,
  198. ),
  199. 'h5' => array(
  200. 'align' => true,
  201. ),
  202. 'h6' => array(
  203. 'align' => true,
  204. ),
  205. 'header' => array(
  206. 'align' => true,
  207. 'dir' => true,
  208. 'lang' => true,
  209. 'xml:lang' => true,
  210. ),
  211. 'hgroup' => array(
  212. 'align' => true,
  213. 'dir' => true,
  214. 'lang' => true,
  215. 'xml:lang' => true,
  216. ),
  217. 'hr' => array(
  218. 'align' => true,
  219. 'noshade' => true,
  220. 'size' => true,
  221. 'width' => true,
  222. ),
  223. 'i' => array(),
  224. 'img' => array(
  225. 'alt' => true,
  226. 'align' => true,
  227. 'border' => true,
  228. 'height' => true,
  229. 'hspace' => true,
  230. 'longdesc' => true,
  231. 'vspace' => true,
  232. 'src' => true,
  233. 'usemap' => true,
  234. 'width' => true,
  235. ),
  236. 'ins' => array(
  237. 'datetime' => true,
  238. 'cite' => true,
  239. ),
  240. 'kbd' => array(),
  241. 'label' => array(
  242. 'for' => true,
  243. ),
  244. 'legend' => array(
  245. 'align' => true,
  246. ),
  247. 'li' => array(
  248. 'align' => true,
  249. 'value' => true,
  250. ),
  251. 'map' => array(
  252. 'name' => true,
  253. ),
  254. 'mark' => array(),
  255. 'menu' => array(
  256. 'type' => true,
  257. ),
  258. 'nav' => array(
  259. 'align' => true,
  260. 'dir' => true,
  261. 'lang' => true,
  262. 'xml:lang' => true,
  263. ),
  264. 'p' => array(
  265. 'align' => true,
  266. 'dir' => true,
  267. 'lang' => true,
  268. 'xml:lang' => true,
  269. ),
  270. 'pre' => array(
  271. 'width' => true,
  272. ),
  273. 'q' => array(
  274. 'cite' => true,
  275. ),
  276. 's' => array(),
  277. 'samp' => array(),
  278. 'span' => array(
  279. 'dir' => true,
  280. 'align' => true,
  281. 'lang' => true,
  282. 'xml:lang' => true,
  283. ),
  284. 'section' => array(
  285. 'align' => true,
  286. 'dir' => true,
  287. 'lang' => true,
  288. 'xml:lang' => true,
  289. ),
  290. 'small' => array(),
  291. 'strike' => array(),
  292. 'strong' => array(),
  293. 'sub' => array(),
  294. 'summary' => array(
  295. 'align' => true,
  296. 'dir' => true,
  297. 'lang' => true,
  298. 'xml:lang' => true,
  299. ),
  300. 'sup' => array(),
  301. 'table' => array(
  302. 'align' => true,
  303. 'bgcolor' => true,
  304. 'border' => true,
  305. 'cellpadding' => true,
  306. 'cellspacing' => true,
  307. 'dir' => true,
  308. 'rules' => true,
  309. 'summary' => true,
  310. 'width' => true,
  311. ),
  312. 'tbody' => array(
  313. 'align' => true,
  314. 'char' => true,
  315. 'charoff' => true,
  316. 'valign' => true,
  317. ),
  318. 'td' => array(
  319. 'abbr' => true,
  320. 'align' => true,
  321. 'axis' => true,
  322. 'bgcolor' => true,
  323. 'char' => true,
  324. 'charoff' => true,
  325. 'colspan' => true,
  326. 'dir' => true,
  327. 'headers' => true,
  328. 'height' => true,
  329. 'nowrap' => true,
  330. 'rowspan' => true,
  331. 'scope' => true,
  332. 'valign' => true,
  333. 'width' => true,
  334. ),
  335. 'textarea' => array(
  336. 'cols' => true,
  337. 'rows' => true,
  338. 'disabled' => true,
  339. 'name' => true,
  340. 'readonly' => true,
  341. ),
  342. 'tfoot' => array(
  343. 'align' => true,
  344. 'char' => true,
  345. 'charoff' => true,
  346. 'valign' => true,
  347. ),
  348. 'th' => array(
  349. 'abbr' => true,
  350. 'align' => true,
  351. 'axis' => true,
  352. 'bgcolor' => true,
  353. 'char' => true,
  354. 'charoff' => true,
  355. 'colspan' => true,
  356. 'headers' => true,
  357. 'height' => true,
  358. 'nowrap' => true,
  359. 'rowspan' => true,
  360. 'scope' => true,
  361. 'valign' => true,
  362. 'width' => true,
  363. ),
  364. 'thead' => array(
  365. 'align' => true,
  366. 'char' => true,
  367. 'charoff' => true,
  368. 'valign' => true,
  369. ),
  370. 'title' => array(),
  371. 'tr' => array(
  372. 'align' => true,
  373. 'bgcolor' => true,
  374. 'char' => true,
  375. 'charoff' => true,
  376. 'valign' => true,
  377. ),
  378. 'track' => array(
  379. 'default' => true,
  380. 'kind' => true,
  381. 'label' => true,
  382. 'src' => true,
  383. 'srclang' => true,
  384. ),
  385. 'tt' => array(),
  386. 'u' => array(),
  387. 'ul' => array(
  388. 'type' => true,
  389. ),
  390. 'ol' => array(
  391. 'start' => true,
  392. 'type' => true,
  393. 'reversed' => true,
  394. ),
  395. 'var' => array(),
  396. 'video' => array(
  397. 'autoplay' => true,
  398. 'controls' => true,
  399. 'height' => true,
  400. 'loop' => true,
  401. 'muted' => true,
  402. 'poster' => true,
  403. 'preload' => true,
  404. 'src' => true,
  405. 'width' => true,
  406. ),
  407. );
  408. /**
  409. * @var array[] $allowedtags Array of KSES allowed HTML elements.
  410. * @since 1.0.0
  411. */
  412. $allowedtags = array(
  413. 'a' => array(
  414. 'href' => true,
  415. 'title' => true,
  416. ),
  417. 'abbr' => array(
  418. 'title' => true,
  419. ),
  420. 'acronym' => array(
  421. 'title' => true,
  422. ),
  423. 'b' => array(),
  424. 'blockquote' => array(
  425. 'cite' => true,
  426. ),
  427. 'cite' => array(),
  428. 'code' => array(),
  429. 'del' => array(
  430. 'datetime' => true,
  431. ),
  432. 'em' => array(),
  433. 'i' => array(),
  434. 'q' => array(
  435. 'cite' => true,
  436. ),
  437. 's' => array(),
  438. 'strike' => array(),
  439. 'strong' => array(),
  440. );
  441. /**
  442. * @var string[] $allowedentitynames Array of KSES allowed HTML entitity names.
  443. * @since 1.0.0
  444. */
  445. $allowedentitynames = array(
  446. 'nbsp',
  447. 'iexcl',
  448. 'cent',
  449. 'pound',
  450. 'curren',
  451. 'yen',
  452. 'brvbar',
  453. 'sect',
  454. 'uml',
  455. 'copy',
  456. 'ordf',
  457. 'laquo',
  458. 'not',
  459. 'shy',
  460. 'reg',
  461. 'macr',
  462. 'deg',
  463. 'plusmn',
  464. 'acute',
  465. 'micro',
  466. 'para',
  467. 'middot',
  468. 'cedil',
  469. 'ordm',
  470. 'raquo',
  471. 'iquest',
  472. 'Agrave',
  473. 'Aacute',
  474. 'Acirc',
  475. 'Atilde',
  476. 'Auml',
  477. 'Aring',
  478. 'AElig',
  479. 'Ccedil',
  480. 'Egrave',
  481. 'Eacute',
  482. 'Ecirc',
  483. 'Euml',
  484. 'Igrave',
  485. 'Iacute',
  486. 'Icirc',
  487. 'Iuml',
  488. 'ETH',
  489. 'Ntilde',
  490. 'Ograve',
  491. 'Oacute',
  492. 'Ocirc',
  493. 'Otilde',
  494. 'Ouml',
  495. 'times',
  496. 'Oslash',
  497. 'Ugrave',
  498. 'Uacute',
  499. 'Ucirc',
  500. 'Uuml',
  501. 'Yacute',
  502. 'THORN',
  503. 'szlig',
  504. 'agrave',
  505. 'aacute',
  506. 'acirc',
  507. 'atilde',
  508. 'auml',
  509. 'aring',
  510. 'aelig',
  511. 'ccedil',
  512. 'egrave',
  513. 'eacute',
  514. 'ecirc',
  515. 'euml',
  516. 'igrave',
  517. 'iacute',
  518. 'icirc',
  519. 'iuml',
  520. 'eth',
  521. 'ntilde',
  522. 'ograve',
  523. 'oacute',
  524. 'ocirc',
  525. 'otilde',
  526. 'ouml',
  527. 'divide',
  528. 'oslash',
  529. 'ugrave',
  530. 'uacute',
  531. 'ucirc',
  532. 'uuml',
  533. 'yacute',
  534. 'thorn',
  535. 'yuml',
  536. 'quot',
  537. 'amp',
  538. 'lt',
  539. 'gt',
  540. 'apos',
  541. 'OElig',
  542. 'oelig',
  543. 'Scaron',
  544. 'scaron',
  545. 'Yuml',
  546. 'circ',
  547. 'tilde',
  548. 'ensp',
  549. 'emsp',
  550. 'thinsp',
  551. 'zwnj',
  552. 'zwj',
  553. 'lrm',
  554. 'rlm',
  555. 'ndash',
  556. 'mdash',
  557. 'lsquo',
  558. 'rsquo',
  559. 'sbquo',
  560. 'ldquo',
  561. 'rdquo',
  562. 'bdquo',
  563. 'dagger',
  564. 'Dagger',
  565. 'permil',
  566. 'lsaquo',
  567. 'rsaquo',
  568. 'euro',
  569. 'fnof',
  570. 'Alpha',
  571. 'Beta',
  572. 'Gamma',
  573. 'Delta',
  574. 'Epsilon',
  575. 'Zeta',
  576. 'Eta',
  577. 'Theta',
  578. 'Iota',
  579. 'Kappa',
  580. 'Lambda',
  581. 'Mu',
  582. 'Nu',
  583. 'Xi',
  584. 'Omicron',
  585. 'Pi',
  586. 'Rho',
  587. 'Sigma',
  588. 'Tau',
  589. 'Upsilon',
  590. 'Phi',
  591. 'Chi',
  592. 'Psi',
  593. 'Omega',
  594. 'alpha',
  595. 'beta',
  596. 'gamma',
  597. 'delta',
  598. 'epsilon',
  599. 'zeta',
  600. 'eta',
  601. 'theta',
  602. 'iota',
  603. 'kappa',
  604. 'lambda',
  605. 'mu',
  606. 'nu',
  607. 'xi',
  608. 'omicron',
  609. 'pi',
  610. 'rho',
  611. 'sigmaf',
  612. 'sigma',
  613. 'tau',
  614. 'upsilon',
  615. 'phi',
  616. 'chi',
  617. 'psi',
  618. 'omega',
  619. 'thetasym',
  620. 'upsih',
  621. 'piv',
  622. 'bull',
  623. 'hellip',
  624. 'prime',
  625. 'Prime',
  626. 'oline',
  627. 'frasl',
  628. 'weierp',
  629. 'image',
  630. 'real',
  631. 'trade',
  632. 'alefsym',
  633. 'larr',
  634. 'uarr',
  635. 'rarr',
  636. 'darr',
  637. 'harr',
  638. 'crarr',
  639. 'lArr',
  640. 'uArr',
  641. 'rArr',
  642. 'dArr',
  643. 'hArr',
  644. 'forall',
  645. 'part',
  646. 'exist',
  647. 'empty',
  648. 'nabla',
  649. 'isin',
  650. 'notin',
  651. 'ni',
  652. 'prod',
  653. 'sum',
  654. 'minus',
  655. 'lowast',
  656. 'radic',
  657. 'prop',
  658. 'infin',
  659. 'ang',
  660. 'and',
  661. 'or',
  662. 'cap',
  663. 'cup',
  664. 'int',
  665. 'sim',
  666. 'cong',
  667. 'asymp',
  668. 'ne',
  669. 'equiv',
  670. 'le',
  671. 'ge',
  672. 'sub',
  673. 'sup',
  674. 'nsub',
  675. 'sube',
  676. 'supe',
  677. 'oplus',
  678. 'otimes',
  679. 'perp',
  680. 'sdot',
  681. 'lceil',
  682. 'rceil',
  683. 'lfloor',
  684. 'rfloor',
  685. 'lang',
  686. 'rang',
  687. 'loz',
  688. 'spades',
  689. 'clubs',
  690. 'hearts',
  691. 'diams',
  692. 'sup1',
  693. 'sup2',
  694. 'sup3',
  695. 'frac14',
  696. 'frac12',
  697. 'frac34',
  698. 'there4',
  699. );
  700. $allowedposttags = array_map( '_wp_add_global_attributes', $allowedposttags );
  701. } else {
  702. $allowedtags = wp_kses_array_lc( $allowedtags );
  703. $allowedposttags = wp_kses_array_lc( $allowedposttags );
  704. }
  705. /**
  706. * Filters text content and strips out disallowed HTML.
  707. *
  708. * This function makes sure that only the allowed HTML element names, attribute
  709. * names, attribute values, and HTML entities will occur in the given text string.
  710. *
  711. * This function expects unslashed data.
  712. *
  713. * @see wp_kses_post() for specifically filtering post content and fields.
  714. * @see wp_allowed_protocols() for the default allowed protocols in link URLs.
  715. *
  716. * @since 1.0.0
  717. *
  718. * @param string $string Text content to filter.
  719. * @param array[]|string $allowed_html An array of allowed HTML elements and attributes, or a
  720. * context name such as 'post'.
  721. * @param string[] $allowed_protocols Array of allowed URL protocols.
  722. * @return string Filtered content containing only the allowed HTML.
  723. */
  724. function wp_kses( $string, $allowed_html, $allowed_protocols = array() ) {
  725. if ( empty( $allowed_protocols ) ) {
  726. $allowed_protocols = wp_allowed_protocols();
  727. }
  728. $string = wp_kses_no_null( $string, array( 'slash_zero' => 'keep' ) );
  729. $string = wp_kses_normalize_entities( $string );
  730. $string = wp_kses_hook( $string, $allowed_html, $allowed_protocols );
  731. return wp_kses_split( $string, $allowed_html, $allowed_protocols );
  732. }
  733. /**
  734. * Filters one HTML attribute and ensures its value is allowed.
  735. *
  736. * This function can escape data in some situations where `wp_kses()` must strip the whole attribute.
  737. *
  738. * @since 4.2.3
  739. *
  740. * @param string $string The 'whole' attribute, including name and value.
  741. * @param string $element The HTML element name to which the attribute belongs.
  742. * @return string Filtered attribute.
  743. */
  744. function wp_kses_one_attr( $string, $element ) {
  745. $uris = wp_kses_uri_attributes();
  746. $allowed_html = wp_kses_allowed_html( 'post' );
  747. $allowed_protocols = wp_allowed_protocols();
  748. $string = wp_kses_no_null( $string, array( 'slash_zero' => 'keep' ) );
  749. // Preserve leading and trailing whitespace.
  750. $matches = array();
  751. preg_match( '/^\s*/', $string, $matches );
  752. $lead = $matches[0];
  753. preg_match( '/\s*$/', $string, $matches );
  754. $trail = $matches[0];
  755. if ( empty( $trail ) ) {
  756. $string = substr( $string, strlen( $lead ) );
  757. } else {
  758. $string = substr( $string, strlen( $lead ), -strlen( $trail ) );
  759. }
  760. // Parse attribute name and value from input.
  761. $split = preg_split( '/\s*=\s*/', $string, 2 );
  762. $name = $split[0];
  763. if ( count( $split ) == 2 ) {
  764. $value = $split[1];
  765. // Remove quotes surrounding $value.
  766. // Also guarantee correct quoting in $string for this one attribute.
  767. if ( '' == $value ) {
  768. $quote = '';
  769. } else {
  770. $quote = $value[0];
  771. }
  772. if ( '"' == $quote || "'" == $quote ) {
  773. if ( substr( $value, -1 ) != $quote ) {
  774. return '';
  775. }
  776. $value = substr( $value, 1, -1 );
  777. } else {
  778. $quote = '"';
  779. }
  780. // Sanitize quotes, angle braces, and entities.
  781. $value = esc_attr( $value );
  782. // Sanitize URI values.
  783. if ( in_array( strtolower( $name ), $uris ) ) {
  784. $value = wp_kses_bad_protocol( $value, $allowed_protocols );
  785. }
  786. $string = "$name=$quote$value$quote";
  787. $vless = 'n';
  788. } else {
  789. $value = '';
  790. $vless = 'y';
  791. }
  792. // Sanitize attribute by name.
  793. wp_kses_attr_check( $name, $value, $string, $vless, $element, $allowed_html );
  794. // Restore whitespace.
  795. return $lead . $string . $trail;
  796. }
  797. /**
  798. * Returns an array of allowed HTML tags and attributes for a given context.
  799. *
  800. * @since 3.5.0
  801. * @since 5.0.1 `form` removed as allowable HTML tag.
  802. *
  803. * @global array $allowedposttags
  804. * @global array $allowedtags
  805. * @global array $allowedentitynames
  806. *
  807. * @param string|array $context The context for which to retrieve tags. Allowed values are 'post',
  808. * 'strip', 'data', 'entities', or the name of a field filter such as
  809. * 'pre_user_description'.
  810. * @return array Array of allowed HTML tags and their allowed attributes.
  811. */
  812. function wp_kses_allowed_html( $context = '' ) {
  813. global $allowedposttags, $allowedtags, $allowedentitynames;
  814. if ( is_array( $context ) ) {
  815. /**
  816. * Filters the HTML that is allowed for a given context.
  817. *
  818. * @since 3.5.0
  819. *
  820. * @param array[]|string $context Context to judge allowed tags by.
  821. * @param string $context_type Context name.
  822. */
  823. return apply_filters( 'wp_kses_allowed_html', $context, 'explicit' );
  824. }
  825. switch ( $context ) {
  826. case 'post':
  827. /** This filter is documented in wp-includes/kses.php */
  828. $tags = apply_filters( 'wp_kses_allowed_html', $allowedposttags, $context );
  829. // 5.0.1 removed the `<form>` tag, allow it if a filter is allowing it's sub-elements `<input>` or `<select>`.
  830. if ( ! CUSTOM_TAGS && ! isset( $tags['form'] ) && ( isset( $tags['input'] ) || isset( $tags['select'] ) ) ) {
  831. $tags = $allowedposttags;
  832. $tags['form'] = array(
  833. 'action' => true,
  834. 'accept' => true,
  835. 'accept-charset' => true,
  836. 'enctype' => true,
  837. 'method' => true,
  838. 'name' => true,
  839. 'target' => true,
  840. );
  841. /** This filter is documented in wp-includes/kses.php */
  842. $tags = apply_filters( 'wp_kses_allowed_html', $tags, $context );
  843. }
  844. return $tags;
  845. case 'user_description':
  846. case 'pre_user_description':
  847. $tags = $allowedtags;
  848. $tags['a']['rel'] = true;
  849. /** This filter is documented in wp-includes/kses.php */
  850. return apply_filters( 'wp_kses_allowed_html', $tags, $context );
  851. case 'strip':
  852. /** This filter is documented in wp-includes/kses.php */
  853. return apply_filters( 'wp_kses_allowed_html', array(), $context );
  854. case 'entities':
  855. /** This filter is documented in wp-includes/kses.php */
  856. return apply_filters( 'wp_kses_allowed_html', $allowedentitynames, $context );
  857. case 'data':
  858. default:
  859. /** This filter is documented in wp-includes/kses.php */
  860. return apply_filters( 'wp_kses_allowed_html', $allowedtags, $context );
  861. }
  862. }
  863. /**
  864. * You add any KSES hooks here.
  865. *
  866. * There is currently only one KSES WordPress hook, {@see 'pre_kses'}, and it is called here.
  867. * All parameters are passed to the hooks and expected to receive a string.
  868. *
  869. * @since 1.0.0
  870. *
  871. * @param string $string Content to filter through KSES.
  872. * @param array[]|string $allowed_html List of allowed HTML elements.
  873. * @param string[] $allowed_protocols Array of allowed URL protocols.
  874. * @return string Filtered content through {@see 'pre_kses'} hook.
  875. */
  876. function wp_kses_hook( $string, $allowed_html, $allowed_protocols ) {
  877. /**
  878. * Filters content to be run through kses.
  879. *
  880. * @since 2.3.0
  881. *
  882. * @param string $string Content to run through KSES.
  883. * @param array[]|string $allowed_html Allowed HTML elements.
  884. * @param string[] $allowed_protocols Array of allowed URL protocols.
  885. */
  886. return apply_filters( 'pre_kses', $string, $allowed_html, $allowed_protocols );
  887. }
  888. /**
  889. * Returns the version number of KSES.
  890. *
  891. * @since 1.0.0
  892. *
  893. * @return string KSES version number.
  894. */
  895. function wp_kses_version() {
  896. return '0.2.2';
  897. }
  898. /**
  899. * Searches for HTML tags, no matter how malformed.
  900. *
  901. * It also matches stray `>` characters.
  902. *
  903. * @since 1.0.0
  904. *
  905. * @global array $pass_allowed_html
  906. * @global array $pass_allowed_protocols
  907. *
  908. * @param string $string Content to filter.
  909. * @param array $allowed_html Allowed HTML elements.
  910. * @param string[] $allowed_protocols Array of allowed URL protocols.
  911. * @return string Content with fixed HTML tags
  912. */
  913. function wp_kses_split( $string, $allowed_html, $allowed_protocols ) {
  914. global $pass_allowed_html, $pass_allowed_protocols;
  915. $pass_allowed_html = $allowed_html;
  916. $pass_allowed_protocols = $allowed_protocols;
  917. return preg_replace_callback( '%(<!--.*?(-->|$))|(<[^>]*(>|$)|>)%', '_wp_kses_split_callback', $string );
  918. }
  919. /**
  920. * Helper function listing HTML attributes containing a URL.
  921. *
  922. * This function returns a list of all HTML attributes that must contain
  923. * a URL according to the HTML specification.
  924. *
  925. * This list includes URI attributes both allowed and disallowed by KSES.
  926. *
  927. * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes
  928. *
  929. * @since 5.0.1
  930. *
  931. * @return array HTML attributes that must include a URL.
  932. */
  933. function wp_kses_uri_attributes() {
  934. $uri_attributes = array(
  935. 'action',
  936. 'archive',
  937. 'background',
  938. 'cite',
  939. 'classid',
  940. 'codebase',
  941. 'data',
  942. 'formaction',
  943. 'href',
  944. 'icon',
  945. 'longdesc',
  946. 'manifest',
  947. 'poster',
  948. 'profile',
  949. 'src',
  950. 'usemap',
  951. 'xmlns',
  952. );
  953. /**
  954. * Filters the list of attributes that are required to contain a URL.
  955. *
  956. * Use this filter to add any `data-` attributes that are required to be
  957. * validated as a URL.
  958. *
  959. * @since 5.0.1
  960. *
  961. * @param array $uri_attributes HTML attributes requiring validation as a URL.
  962. */
  963. $uri_attributes = apply_filters( 'wp_kses_uri_attributes', $uri_attributes );
  964. return $uri_attributes;
  965. }
  966. /**
  967. * Callback for `wp_kses_split()`.
  968. *
  969. * @since 3.1.0
  970. * @access private
  971. * @ignore
  972. *
  973. * @global array $pass_allowed_html
  974. * @global array $pass_allowed_protocols
  975. *
  976. * @return string
  977. */
  978. function _wp_kses_split_callback( $match ) {
  979. global $pass_allowed_html, $pass_allowed_protocols;
  980. return wp_kses_split2( $match[0], $pass_allowed_html, $pass_allowed_protocols );
  981. }
  982. /**
  983. * Callback for `wp_kses_split()` for fixing malformed HTML tags.
  984. *
  985. * This function does a lot of work. It rejects some very malformed things like
  986. * `<:::>`. It returns an empty string, if the element isn't allowed (look ma, no
  987. * `strip_tags()`!). Otherwise it splits the tag into an element and an attribute
  988. * list.
  989. *
  990. * After the tag is split into an element and an attribute list, it is run
  991. * through another filter which will remove illegal attributes and once that is
  992. * completed, will be returned.
  993. *
  994. * @access private
  995. * @ignore
  996. * @since 1.0.0
  997. *
  998. * @param string $string Content to filter.
  999. * @param array $allowed_html Allowed HTML elements.
  1000. * @param string[] $allowed_protocols Array of allowed URL protocols.
  1001. * @return string Fixed HTML element
  1002. */
  1003. function wp_kses_split2( $string, $allowed_html, $allowed_protocols ) {
  1004. $string = wp_kses_stripslashes( $string );
  1005. // It matched a ">" character.
  1006. if ( substr( $string, 0, 1 ) != '<' ) {
  1007. return '&gt;';
  1008. }
  1009. // Allow HTML comments.
  1010. if ( '<!--' == substr( $string, 0, 4 ) ) {
  1011. $string = str_replace( array( '<!--', '-->' ), '', $string );
  1012. while ( $string != ( $newstring = wp_kses( $string, $allowed_html, $allowed_protocols ) ) ) {
  1013. $string = $newstring;
  1014. }
  1015. if ( $string == '' ) {
  1016. return '';
  1017. }
  1018. // prevent multiple dashes in comments
  1019. $string = preg_replace( '/--+/', '-', $string );
  1020. // prevent three dashes closing a comment
  1021. $string = preg_replace( '/-$/', '', $string );
  1022. return "<!--{$string}-->";
  1023. }
  1024. // It's seriously malformed.
  1025. if ( ! preg_match( '%^<\s*(/\s*)?([a-zA-Z0-9-]+)([^>]*)>?$%', $string, $matches ) ) {
  1026. return '';
  1027. }
  1028. $slash = trim( $matches[1] );
  1029. $elem = $matches[2];
  1030. $attrlist = $matches[3];
  1031. if ( ! is_array( $allowed_html ) ) {
  1032. $allowed_html = wp_kses_allowed_html( $allowed_html );
  1033. }
  1034. // They are using a not allowed HTML element.
  1035. if ( ! isset( $allowed_html[ strtolower( $elem ) ] ) ) {
  1036. return '';
  1037. }
  1038. // No attributes are allowed for closing elements.
  1039. if ( $slash != '' ) {
  1040. return "</$elem>";
  1041. }
  1042. return wp_kses_attr( $elem, $attrlist, $allowed_html, $allowed_protocols );
  1043. }
  1044. /**
  1045. * Removes all attributes, if none are allowed for this element.
  1046. *
  1047. * If some are allowed it calls `wp_kses_hair()` to split them further, and then
  1048. * it builds up new HTML code from the data that `kses_hair()` returns. It also
  1049. * removes `<` and `>` characters, if there are any left. One more thing it does
  1050. * is to check if the tag has a closing XHTML slash, and if it does, it puts one
  1051. * in the returned code as well.
  1052. *
  1053. * @since 1.0.0
  1054. *
  1055. * @param string $element HTML element/tag.
  1056. * @param string $attr HTML attributes from HTML element to closing HTML element tag.
  1057. * @param array $allowed_html Allowed HTML elements.
  1058. * @param string[] $allowed_protocols Array of allowed URL protocols.
  1059. * @return string Sanitized HTML element.
  1060. */
  1061. function wp_kses_attr( $element, $attr, $allowed_html, $allowed_protocols ) {
  1062. if ( ! is_array( $allowed_html ) ) {
  1063. $allowed_html = wp_kses_allowed_html( $allowed_html );
  1064. }
  1065. // Is there a closing XHTML slash at the end of the attributes?
  1066. $xhtml_slash = '';
  1067. if ( preg_match( '%\s*/\s*$%', $attr ) ) {
  1068. $xhtml_slash = ' /';
  1069. }
  1070. // Are any attributes allowed at all for this element?
  1071. $element_low = strtolower( $element );
  1072. if ( empty( $allowed_html[ $element_low ] ) || true === $allowed_html[ $element_low ] ) {
  1073. return "<$element$xhtml_slash>";
  1074. }
  1075. // Split it
  1076. $attrarr = wp_kses_hair( $attr, $allowed_protocols );
  1077. // Go through $attrarr, and save the allowed attributes for this element
  1078. // in $attr2
  1079. $attr2 = '';
  1080. foreach ( $attrarr as $arreach ) {
  1081. if ( wp_kses_attr_check( $arreach['name'], $arreach['value'], $arreach['whole'], $arreach['vless'], $element, $allowed_html ) ) {
  1082. $attr2 .= ' ' . $arreach['whole'];
  1083. }
  1084. }
  1085. // Remove any "<" or ">" characters
  1086. $attr2 = preg_replace( '/[<>]/', '', $attr2 );
  1087. return "<$element$attr2$xhtml_slash>";
  1088. }
  1089. /**
  1090. * Determines whether an attribute is allowed.
  1091. *
  1092. * @since 4.2.3
  1093. * @since 5.0.0 Add support for `data-*` wildcard attributes.
  1094. *
  1095. * @param string $name The attribute name. Passed by reference. Returns empty string when not allowed.
  1096. * @param string $value The attribute value. Passed by reference. Returns a filtered value.
  1097. * @param string $whole The `name=value` input. Passed by reference. Returns filtered input.
  1098. * @param string $vless Whether the attribute is valueless. Use 'y' or 'n'.
  1099. * @param string $element The name of the element to which this attribute belongs.
  1100. * @param array $allowed_html The full list of allowed elements and attributes.
  1101. * @return bool Whether or not the attribute is allowed.
  1102. */
  1103. function wp_kses_attr_check( &$name, &$value, &$whole, $vless, $element, $allowed_html ) {
  1104. $allowed_attr = $allowed_html[ strtolower( $element ) ];
  1105. $name_low = strtolower( $name );
  1106. if ( ! isset( $allowed_attr[ $name_low ] ) || '' == $allowed_attr[ $name_low ] ) {
  1107. /*
  1108. * Allow `data-*` attributes.
  1109. *
  1110. * When specifying `$allowed_html`, the attribute name should be set as
  1111. * `data-*` (not to be mixed with the HTML 4.0 `data` attribute, see
  1112. * https://www.w3.org/TR/html40/struct/objects.html#adef-data).
  1113. *
  1114. * Note: the attribute name should only contain `A-Za-z0-9_-` chars,
  1115. * double hyphens `--` are not accepted by WordPress.
  1116. */
  1117. if ( strpos( $name_low, 'data-' ) === 0 && ! empty( $allowed_attr['data-*'] ) && preg_match( '/^data(?:-[a-z0-9_]+)+$/', $name_low, $match ) ) {
  1118. /*
  1119. * Add the whole attribute name to the allowed attributes and set any restrictions
  1120. * for the `data-*` attribute values for the current element.
  1121. */
  1122. $allowed_attr[ $match[0] ] = $allowed_attr['data-*'];
  1123. } else {
  1124. $name = '';
  1125. $value = '';
  1126. $whole = '';
  1127. return false;
  1128. }
  1129. }
  1130. if ( 'style' == $name_low ) {
  1131. $new_value = safecss_filter_attr( $value );
  1132. if ( empty( $new_value ) ) {
  1133. $name = '';
  1134. $value = '';
  1135. $whole = '';
  1136. return false;
  1137. }
  1138. $whole = str_replace( $value, $new_value, $whole );
  1139. $value = $new_value;
  1140. }
  1141. if ( is_array( $allowed_attr[ $name_low ] ) ) {
  1142. // there are some checks
  1143. foreach ( $allowed_attr[ $name_low ] as $currkey => $currval ) {
  1144. if ( ! wp_kses_check_attr_val( $value, $vless, $currkey, $currval ) ) {
  1145. $name = '';
  1146. $value = '';
  1147. $whole = '';
  1148. return false;
  1149. }
  1150. }
  1151. }
  1152. return true;
  1153. }
  1154. /**
  1155. * Builds an attribute list from string containing attributes.
  1156. *
  1157. * This function does a lot of work. It parses an attribute list into an array
  1158. * with attribute data, and tries to do the right thing even if it gets weird
  1159. * input. It will add quotes around attribute values that don't have any quotes
  1160. * or apostrophes around them, to make it easier to produce HTML code that will
  1161. * conform to W3C's HTML specification. It will also remove bad URL protocols
  1162. * from attribute values. It also reduces duplicate attributes by using the
  1163. * attribute defined first (`foo='bar' foo='baz'` will result in `foo='bar'`).
  1164. *
  1165. * @since 1.0.0
  1166. *
  1167. * @param string $attr Attribute list from HTML element to closing HTML element tag.
  1168. * @param string[] $allowed_protocols Array of allowed URL protocols.
  1169. * @return array[] Array of attribute information after parsing.
  1170. */
  1171. function wp_kses_hair( $attr, $allowed_protocols ) {
  1172. $attrarr = array();
  1173. $mode = 0;
  1174. $attrname = '';
  1175. $uris = wp_kses_uri_attributes();
  1176. // Loop through the whole attribute list
  1177. while ( strlen( $attr ) != 0 ) {
  1178. $working = 0; // Was the last operation successful?
  1179. switch ( $mode ) {
  1180. case 0:
  1181. if ( preg_match( '/^([-a-zA-Z:]+)/', $attr, $match ) ) {
  1182. $attrname = $match[1];
  1183. $working = 1;
  1184. $mode = 1;
  1185. $attr = preg_replace( '/^[-a-zA-Z:]+/', '', $attr );
  1186. }
  1187. break;
  1188. case 1:
  1189. if ( preg_match( '/^\s*=\s*/', $attr ) ) { // equals sign
  1190. $working = 1;
  1191. $mode = 2;
  1192. $attr = preg_replace( '/^\s*=\s*/', '', $attr );
  1193. break;
  1194. }
  1195. if ( preg_match( '/^\s+/', $attr ) ) { // valueless
  1196. $working = 1;
  1197. $mode = 0;
  1198. if ( false === array_key_exists( $attrname, $attrarr ) ) {
  1199. $attrarr[ $attrname ] = array(
  1200. 'name' => $attrname,
  1201. 'value' => '',
  1202. 'whole' => $attrname,
  1203. 'vless' => 'y',
  1204. );
  1205. }
  1206. $attr = preg_replace( '/^\s+/', '', $attr );
  1207. }
  1208. break;
  1209. case 2:
  1210. if ( preg_match( '%^"([^"]*)"(\s+|/?$)%', $attr, $match ) ) {
  1211. // "value"
  1212. $thisval = $match[1];
  1213. if ( in_array( strtolower( $attrname ), $uris ) ) {
  1214. $thisval = wp_kses_bad_protocol( $thisval, $allowed_protocols );
  1215. }
  1216. if ( false === array_key_exists( $attrname, $attrarr ) ) {
  1217. $attrarr[ $attrname ] = array(
  1218. 'name' => $attrname,
  1219. 'value' => $thisval,
  1220. 'whole' => "$attrname=\"$thisval\"",
  1221. 'vless' => 'n',
  1222. );
  1223. }
  1224. $working = 1;
  1225. $mode = 0;
  1226. $attr = preg_replace( '/^"[^"]*"(\s+|$)/', '', $attr );
  1227. break;
  1228. }
  1229. if ( preg_match( "%^'([^']*)'(\s+|/?$)%", $attr, $match ) ) {
  1230. // 'value'
  1231. $thisval = $match[1];
  1232. if ( in_array( strtolower( $attrname ), $uris ) ) {
  1233. $thisval = wp_kses_bad_protocol( $thisval, $allowed_protocols );
  1234. }
  1235. if ( false === array_key_exists( $attrname, $attrarr ) ) {
  1236. $attrarr[ $attrname ] = array(
  1237. 'name' => $attrname,
  1238. 'value' => $thisval,
  1239. 'whole' => "$attrname='$thisval'",
  1240. 'vless' => 'n',
  1241. );
  1242. }
  1243. $working = 1;
  1244. $mode = 0;
  1245. $attr = preg_replace( "/^'[^']*'(\s+|$)/", '', $attr );
  1246. break;
  1247. }
  1248. if ( preg_match( "%^([^\s\"']+)(\s+|/?$)%", $attr, $match ) ) {
  1249. // value
  1250. $thisval = $match[1];
  1251. if ( in_array( strtolower( $attrname ), $uris ) ) {
  1252. $thisval = wp_kses_bad_protocol( $thisval, $allowed_protocols );
  1253. }
  1254. if ( false === array_key_exists( $attrname, $attrarr ) ) {
  1255. $attrarr[ $attrname ] = array(
  1256. 'name' => $attrname,
  1257. 'value' => $thisval,
  1258. 'whole' => "$attrname=\"$thisval\"",
  1259. 'vless' => 'n',
  1260. );
  1261. }
  1262. // We add quotes to conform to W3C's HTML spec.
  1263. $working = 1;
  1264. $mode = 0;
  1265. $attr = preg_replace( "%^[^\s\"']+(\s+|$)%", '', $attr );
  1266. }
  1267. break;
  1268. } // switch
  1269. if ( $working == 0 ) { // not well formed, remove and try again
  1270. $attr = wp_kses_html_error( $attr );
  1271. $mode = 0;
  1272. }
  1273. } // while
  1274. if ( $mode == 1 && false === array_key_exists( $attrname, $attrarr ) ) {
  1275. // special case, for when the attribute list ends with a valueless
  1276. // attribute like "selected"
  1277. $attrarr[ $attrname ] = array(
  1278. 'name' => $attrname,
  1279. 'value' => '',
  1280. 'whole' => $attrname,
  1281. 'vless' => 'y',
  1282. );
  1283. }
  1284. return $attrarr;
  1285. }
  1286. /**
  1287. * Finds all attributes of an HTML element.
  1288. *
  1289. * Does not modify input. May return "evil" output.
  1290. *
  1291. * Based on `wp_kses_split2()` and `wp_kses_attr()`.
  1292. *
  1293. * @since 4.2.3
  1294. *
  1295. * @param string $element HTML element.
  1296. * @return array|bool List of attributes found in the element. Returns false on failure.
  1297. */
  1298. function wp_kses_attr_parse( $element ) {
  1299. $valid = preg_match( '%^(<\s*)(/\s*)?([a-zA-Z0-9]+\s*)([^>]*)(>?)$%', $element, $matches );
  1300. if ( 1 !== $valid ) {
  1301. return false;
  1302. }
  1303. $begin = $matches[1];
  1304. $slash = $matches[2];
  1305. $elname = $matches[3];
  1306. $attr = $matches[4];
  1307. $end = $matches[5];
  1308. if ( '' !== $slash ) {
  1309. // Closing elements do not get parsed.
  1310. return false;
  1311. }
  1312. // Is there a closing XHTML slash at the end of the attributes?
  1313. if ( 1 === preg_match( '%\s*/\s*$%', $attr, $matches ) ) {
  1314. $xhtml_slash = $matches[0];
  1315. $attr = substr( $attr, 0, -strlen( $xhtml_slash ) );
  1316. } else {
  1317. $xhtml_slash = '';
  1318. }
  1319. // Split it
  1320. $attrarr = wp_kses_hair_parse( $attr );
  1321. if ( false === $attrarr ) {
  1322. return false;
  1323. }
  1324. // Make sure all input is returned by adding front and back matter.
  1325. array_unshift( $attrarr, $begin . $slash . $elname );
  1326. array_push( $attrarr, $xhtml_slash . $end );
  1327. return $attrarr;
  1328. }
  1329. /**
  1330. * Builds an attribute list from string containing attributes.
  1331. *
  1332. * Does not modify input. May return "evil" output.
  1333. * In case of unexpected input, returns false instead of stripping things.
  1334. *
  1335. * Based on `wp_kses_hair()` but does not return a multi-dimensional array.
  1336. *
  1337. * @since 4.2.3
  1338. *
  1339. * @param string $attr Attribute list from HTML element to closing HTML element tag.
  1340. * @return array|bool List of attributes found in $attr. Returns false on failure.
  1341. */
  1342. function wp_kses_hair_parse( $attr ) {
  1343. if ( '' === $attr ) {
  1344. return array();
  1345. }
  1346. // phpcs:disable Squiz.Strings.ConcatenationSpacing.PaddingFound -- don't remove regex indentation
  1347. $regex =
  1348. '(?:'
  1349. . '[-a-zA-Z:]+' // Attribute name.
  1350. . '|'
  1351. . '\[\[?[^\[\]]+\]\]?' // Shortcode in the name position implies unfiltered_html.
  1352. . ')'
  1353. . '(?:' // Attribute value.
  1354. . '\s*=\s*' // All values begin with '='
  1355. . '(?:'
  1356. . '"[^"]*"' // Double-quoted
  1357. . '|'
  1358. . "'[^']*'" // Single-quoted
  1359. . '|'
  1360. . '[^\s"\']+' // Non-quoted
  1361. . '(?:\s|$)' // Must have a space
  1362. . ')'
  1363. . '|'
  1364. . '(?:\s|$)' // If attribute has no value, space is required.
  1365. . ')'
  1366. . '\s*'; // Trailing space is optional except as mentioned above.
  1367. // phpcs:enable
  1368. // Although it is possible to reduce this procedure to a single regexp,
  1369. // we must run that regexp twice to get exactly the expected result.
  1370. $validation = "%^($regex)+$%";
  1371. $extraction = "%$regex%";
  1372. if ( 1 === preg_match( $validation, $attr ) ) {
  1373. preg_match_all( $extraction, $attr, $attrarr );
  1374. return $attrarr[0];
  1375. } else {
  1376. return false;
  1377. }
  1378. }
  1379. /**
  1380. * Performs different checks for attribute values.
  1381. *
  1382. * The currently implemented checks are "maxlen", "minlen", "maxval", "minval",
  1383. * and "valueless".
  1384. *
  1385. * @since 1.0.0
  1386. *
  1387. * @param string $value Attribute value.
  1388. * @param string $vless Whether the attribute is valueless. Use 'y' or 'n'.
  1389. * @param string $checkname What $checkvalue is checking for.
  1390. * @param mixed $checkvalue What constraint the value should pass.
  1391. * @return bool Whether check passes.
  1392. */
  1393. function wp_kses_check_attr_val( $value, $vless, $checkname, $checkvalue ) {
  1394. $ok = true;
  1395. switch ( strtolower( $checkname ) ) {
  1396. case 'maxlen':
  1397. // The maxlen check makes sure that the attribute value has a length not
  1398. // greater than the given value. This can be used to avoid Buffer Overflows
  1399. // in WWW clients and various Internet servers.
  1400. if ( strlen( $value ) > $checkvalue ) {
  1401. $ok = false;
  1402. }
  1403. break;
  1404. case 'minlen':
  1405. // The minlen check makes sure that the attribute value has a length not
  1406. // smaller than the given value.
  1407. if ( strlen( $value ) < $checkvalue ) {
  1408. $ok = false;
  1409. }
  1410. break;
  1411. case 'maxval':
  1412. // The maxval check does two things: it checks that the attribute value is
  1413. // an integer from 0 and up, without an excessive amount of zeroes or
  1414. // whitespace (to avoid Buffer Overflows). It also checks that the attribute
  1415. // value is not greater than the given value.
  1416. // This check can be used to avoid Denial of Service attacks.
  1417. if ( ! preg_match( '/^\s{0,6}[0-9]{1,6}\s{0,6}$/', $value ) ) {
  1418. $ok = false;
  1419. }
  1420. if ( $value > $checkvalue ) {
  1421. $ok = false;
  1422. }
  1423. break;
  1424. case 'minval':
  1425. // The minval check makes sure that the attribute value is a positive integer,
  1426. // and that it is not smaller than the given value.
  1427. if ( ! preg_match( '/^\s{0,6}[0-9]{1,6}\s{0,6}$/', $value ) ) {
  1428. $ok = false;
  1429. }
  1430. if ( $value < $checkvalue ) {
  1431. $ok = false;
  1432. }
  1433. break;
  1434. case 'valueless':
  1435. // The valueless check makes sure if the attribute has a value
  1436. // (like `<a href="blah">`) or not (`<option selected>`). If the given value
  1437. // is a "y" or a "Y", the attribute must not have a value.
  1438. // If the given value is an "n" or an "N", the attribute must have a value.
  1439. if ( strtolower( $checkvalue ) != $vless ) {
  1440. $ok = false;
  1441. }
  1442. break;
  1443. } // switch
  1444. return $ok;
  1445. }
  1446. /**
  1447. * Sanitizes a string and removed disallowed URL protocols.
  1448. *
  1449. * This function removes all non-allowed protocols from the beginning of the
  1450. * string. It ignores whitespace and the case of the letters, and it does
  1451. * understand HTML entities. It does its work recursively, so it won't be
  1452. * fooled by a string like `javascript:javascript:alert(57)`.
  1453. *
  1454. * @since 1.0.0
  1455. *
  1456. * @param string $string Content to filter bad protocols from.
  1457. * @param string[] $allowed_protocols Array of allowed URL protocols.
  1458. * @return string Filtered content.
  1459. */
  1460. function wp_kses_bad_protocol( $string, $allowed_protocols ) {
  1461. $string = wp_kses_no_null( $string );
  1462. $iterations = 0;
  1463. do {
  1464. $original_string = $string;
  1465. $string = wp_kses_bad_protocol_once( $string, $allowed_protocols );
  1466. } while ( $original_string != $string && ++$iterations < 6 );
  1467. if ( $original_string != $string ) {
  1468. return '';
  1469. }
  1470. return $string;
  1471. }
  1472. /**
  1473. * Removes any invalid control characters in a text string.
  1474. *
  1475. * Also removes any instance of the `\0` string.
  1476. *
  1477. * @since 1.0.0
  1478. *
  1479. * @param string $string Content to filter null characters from.
  1480. * @param array $options Set 'slash_zero' => 'keep' when '\0' is allowed. Default is 'remove'.
  1481. * @return string Filtered content.
  1482. */
  1483. function wp_kses_no_null( $string, $options = null ) {
  1484. if ( ! isset( $options['slash_zero'] ) ) {
  1485. $options = array( 'slash_zero' => 'remove' );
  1486. }
  1487. $string = preg_replace( '/[\x00-\x08\x0B\x0C\x0E-\x1F]/', '', $string );
  1488. if ( 'remove' == $options['slash_zero'] ) {
  1489. $string = preg_replace( '/\\\\+0+/', '', $string );
  1490. }
  1491. return $string;
  1492. }
  1493. /**
  1494. * Strips slashes from in front of quotes.
  1495. *
  1496. * This function changes the character sequence `\"` to just `"`. It leaves all other
  1497. * slashes alone. The quoting from `preg_replace(//e)` requires this.
  1498. *
  1499. * @since 1.0.0
  1500. *
  1501. * @param string $string String to strip slashes from.
  1502. * @return string Fixed string with quoted slashes.
  1503. */
  1504. function wp_kses_stripslashes( $string ) {
  1505. return preg_replace( '%\\\\"%', '"', $string );
  1506. }
  1507. /**
  1508. * Converts the keys of an array to lowercase.
  1509. *
  1510. * @since 1.0.0
  1511. *
  1512. * @param array $inarray Unfiltered array.
  1513. * @return array Fixed array with all lowercase keys.
  1514. */
  1515. function wp_kses_array_lc( $inarray ) {
  1516. $outarray = array();
  1517. foreach ( (array) $inarray as $inkey => $inval ) {
  1518. $outkey = strtolower( $inkey );
  1519. $outarray[ $outkey ] = array();
  1520. foreach ( (array) $inval as $inkey2 => $inval2 ) {
  1521. $outkey2 = strtolower( $inkey2 );
  1522. $outarray[ $outkey ][ $outkey2 ] = $inval2;
  1523. }
  1524. }
  1525. return $outarray;
  1526. }
  1527. /**
  1528. * Handles parsing errors in `wp_kses_hair()`.
  1529. *
  1530. * The general plan is to remove everything to and including some whitespace,
  1531. * but it deals with quotes and apostrophes as well.
  1532. *
  1533. * @since 1.0.0
  1534. *
  1535. * @param string $string
  1536. * @return string
  1537. */
  1538. function wp_kses_html_error( $string ) {
  1539. return preg_replace( '/^("[^"]*("|$)|\'[^\']*(\'|$)|\S)*\s*/', '', $string );
  1540. }
  1541. /**
  1542. * Sanitizes content from bad protocols and other characters.
  1543. *
  1544. * This function searches for URL protocols at the beginning of the string, while
  1545. * handling whitespace and HTML entities.
  1546. *
  1547. * @since 1.0.0
  1548. *
  1549. * @param string $string Content to check for bad protocols.
  1550. * @param string[] $allowed_protocols Array of allowed URL protocols.
  1551. * @return string Sanitized content.
  1552. */
  1553. function wp_kses_bad_protocol_once( $string, $allowed_protocols, $count = 1 ) {
  1554. $string = preg_replace( '/(&#0*58(?![;0-9])|&#x0*3a(?![;a-f0-9]))/i', '$1;', $string );
  1555. $string2 = preg_split( '/:|&#0*58;|&#x0*3a;/i', $string, 2 );
  1556. if ( isset( $string2[1] ) && ! preg_match( '%/\?%', $string2[0] ) ) {
  1557. $string = trim( $string2[1] );
  1558. $protocol = wp_kses_bad_protocol_once2( $string2[0], $allowed_protocols );
  1559. if ( 'feed:' == $protocol ) {
  1560. if ( $count > 2 ) {
  1561. return '';
  1562. }
  1563. $string = wp_kses_bad_protocol_once( $string, $allowed_protocols, ++$count );
  1564. if ( empty( $string ) ) {
  1565. return $string;
  1566. }
  1567. }
  1568. $string = $protocol . $string;
  1569. }
  1570. return $string;
  1571. }
  1572. /**
  1573. * Callback for `wp_kses_bad_protocol_once()` regular expression.
  1574. *
  1575. * This function processes URL protocols, checks to see if they're in the
  1576. * whitelist or not, and returns different data depending on the answer.
  1577. *
  1578. * @access private
  1579. * @ignore
  1580. * @since 1.0.0
  1581. *
  1582. * @param string $string URI scheme to check against the whitelist.
  1583. * @param string[] $allowed_protocols Array of allowed URL protocols.
  1584. * @return string Sanitized content.
  1585. */
  1586. function wp_kses_bad_protocol_once2( $string, $allowed_protocols ) {
  1587. $string2 = wp_kses_decode_entities( $string );
  1588. $string2 = preg_replace( '/\s/', '', $string2 );
  1589. $string2 = wp_kses_no_null( $string2 );
  1590. $string2 = strtolower( $string2 );
  1591. $allowed = false;
  1592. foreach ( (array) $allowed_protocols as $one_protocol ) {
  1593. if ( strtolower( $one_protocol ) == $string2 ) {
  1594. $allowed = true;
  1595. break;
  1596. }
  1597. }
  1598. if ( $allowed ) {
  1599. return "$string2:";
  1600. } else {
  1601. return '';
  1602. }
  1603. }
  1604. /**
  1605. * Converts and fixes HTML entities.
  1606. *
  1607. * This function normalizes HTML entities. It will convert `AT&T` to the correct
  1608. * `AT&amp;T`, `&#00058;` to `&#58;`, `&#XYZZY;` to `&amp;#XYZZY;` and so on.
  1609. *
  1610. * @since 1.0.0
  1611. *
  1612. * @param string $string Content to normalize entities.
  1613. * @return string Content with normalized entities.
  1614. */
  1615. function wp_kses_normalize_entities( $string ) {
  1616. // Disarm all entities by converting & to &amp;
  1617. $string = str_replace( '&', '&amp;', $string );
  1618. // Change back the allowed entities in our entity whitelist
  1619. $string = preg_replace_callback( '/&amp;([A-Za-z]{2,8}[0-9]{0,2});/', 'wp_kses_named_entities', $string );
  1620. $string = preg_replace_callback( '/&amp;#(0*[0-9]{1,7});/', 'wp_kses_normalize_entities2', $string );
  1621. $string = preg_replace_callback( '/&amp;#[Xx](0*[0-9A-Fa-f]{1,6});/', 'wp_kses_normalize_entities3', $string );
  1622. return $string;
  1623. }
  1624. /**
  1625. * Callback for `wp_kses_normalize_entities()` regular expression.
  1626. *
  1627. * This function only accepts valid named entity references, which are finite,
  1628. * case-sensitive, and highly scrutinized by HTML and XML validators.
  1629. *
  1630. * @since 3.0.0
  1631. *
  1632. * @global array $allowedentitynames
  1633. *
  1634. * @param array $matches preg_replace_callback() matches array.
  1635. * @return string Correctly encoded entity.
  1636. */
  1637. function wp_kses_named_entities( $matches ) {
  1638. global $allowedentitynames;
  1639. if ( empty( $matches[1] ) ) {
  1640. return '';
  1641. }
  1642. $i = $matches[1];
  1643. return ( ! in_array( $i, $allowedentitynames ) ) ? "&amp;$i;" : "&$i;";
  1644. }
  1645. /**
  1646. * Callback for `wp_kses_normalize_entities()` regular expression.
  1647. *
  1648. * This function helps `wp_kses_normalize_entities()` to only accept 16-bit
  1649. * values and nothing more for `&#number;` entities.
  1650. *
  1651. * @access private
  1652. * @ignore
  1653. * @since 1.0.0
  1654. *
  1655. * @param array $matches `preg_replace_callback()` matches array.
  1656. * @return string Correctly encoded entity.
  1657. */
  1658. function wp_kses_normalize_entities2( $matches ) {
  1659. if ( empty( $matches[1] ) ) {
  1660. return '';
  1661. }
  1662. $i = $matches[1];
  1663. if ( valid_unicode( $i ) ) {
  1664. $i = str_pad( ltrim( $i, '0' ), 3, '0', STR_PAD_LEFT );
  1665. $i = "&#$i;";
  1666. } else {
  1667. $i = "&amp;#$i;";
  1668. }
  1669. return $i;
  1670. }
  1671. /**
  1672. * Callback for `wp_kses_normalize_entities()` for regular expression.
  1673. *
  1674. * This function helps `wp_kses_normalize_entities()` to only accept valid Unicode
  1675. * numeric entities in hex form.
  1676. *
  1677. * @since 2.7.0
  1678. * @access private
  1679. * @ignore
  1680. *
  1681. * @param array $matches `preg_replace_callback()` matches array.
  1682. * @return string Correctly encoded entity.
  1683. */
  1684. function wp_kses_normalize_entities3( $matches ) {
  1685. if ( empty( $matches[1] ) ) {
  1686. return '';
  1687. }
  1688. $hexchars = $matches[1];
  1689. return ( ! valid_unicode( hexdec( $hexchars ) ) ) ? "&amp;#x$hexchars;" : '&#x' . ltrim( $hexchars, '0' ) . ';';
  1690. }
  1691. /**
  1692. * Determines if a Unicode codepoint is valid.
  1693. *
  1694. * @since 2.7.0
  1695. *
  1696. * @param int $i Unicode codepoint.
  1697. * @return bool Whether or not the codepoint is a valid Unicode codepoint.
  1698. */
  1699. function valid_unicode( $i ) {
  1700. return ( $i == 0x9 || $i == 0xa || $i == 0xd ||
  1701. ( $i >= 0x20 && $i <= 0xd7ff ) ||
  1702. ( $i >= 0xe000 && $i <= 0xfffd ) ||
  1703. ( $i >= 0x10000 && $i <= 0x10ffff ) );
  1704. }
  1705. /**
  1706. * Converts all numeric HTML entities to their named counterparts.
  1707. *
  1708. * This function decodes numeric HTML entities (`&#65;` and `&#x41;`).
  1709. * It doesn't do anything with named entities like `&auml;`, but we don't
  1710. * need them in the URL protocol whitelisting system anyway.
  1711. *
  1712. * @since 1.0.0
  1713. *
  1714. * @param string $string Content to change entities.
  1715. * @return string Content after decoded entities.
  1716. */
  1717. function wp_kses_decode_entities( $string ) {
  1718. $string = preg_replace_callback( '/&#([0-9]+);/', '_wp_kses_decode_entities_chr', $string );
  1719. $string = preg_replace_callback( '/&#[Xx]([0-9A-Fa-f]+);/', '_wp_kses_decode_entities_chr_hexdec', $string );
  1720. return $string;
  1721. }
  1722. /**
  1723. * Regex callback for `wp_kses_decode_entities()`.
  1724. *
  1725. * @since 2.9.0
  1726. * @access private
  1727. * @ignore
  1728. *
  1729. * @param array $match preg match
  1730. * @return string
  1731. */
  1732. function _wp_kses_decode_entities_chr( $match ) {
  1733. return chr( $match[1] );
  1734. }
  1735. /**
  1736. * Regex callback for `wp_kses_decode_entities()`.
  1737. *
  1738. * @since 2.9.0
  1739. * @access private
  1740. * @ignore
  1741. *
  1742. * @param array $match preg match
  1743. * @return string
  1744. */
  1745. function _wp_kses_decode_entities_chr_hexdec( $match ) {
  1746. return chr( hexdec( $match[1] ) );
  1747. }
  1748. /**
  1749. * Sanitize content with allowed HTML KSES rules.
  1750. *
  1751. * This function expects slashed data.
  1752. *
  1753. * @since 1.0.0
  1754. *
  1755. * @param string $data Content to filter, expected to be escaped with slashes.
  1756. * @return string Filtered content.
  1757. */
  1758. function wp_filter_kses( $data ) {
  1759. return addslashes( wp_kses( stripslashes( $data ), current_filter() ) );
  1760. }
  1761. /**
  1762. * Sanitize content with allowed HTML KSES rules.
  1763. *
  1764. * This function expects unslashed data.
  1765. *
  1766. * @since 2.9.0
  1767. *
  1768. * @param string $data Content to filter, expected to not be escaped.
  1769. * @return string Filtered content.
  1770. */
  1771. function wp_kses_data( $data ) {
  1772. return wp_kses( $data, current_filter() );
  1773. }
  1774. /**
  1775. * Sanitizes content for allowed HTML tags for post content.
  1776. *
  1777. * Post content refers to the page contents of the 'post' type and not `$_POST`
  1778. * data from forms.
  1779. *
  1780. * This function expects slashed data.
  1781. *
  1782. * @since 2.0.0
  1783. *
  1784. * @param string $data Post content to filter, expected to be escaped with slashes.
  1785. * @return string Filtered post content with allowed HTML tags and attributes intact.
  1786. */
  1787. function wp_filter_post_kses( $data ) {
  1788. return addslashes( wp_kses( stripslashes( $data ), 'post' ) );
  1789. }
  1790. /**
  1791. * Sanitizes content for allowed HTML tags for post content.
  1792. *
  1793. * Post content refers to the page contents of the 'post' type and not `$_POST`
  1794. * data from forms.
  1795. *
  1796. * This function expects unslashed data.
  1797. *
  1798. * @since 2.9.0
  1799. *
  1800. * @param string $data Post content to filter.
  1801. * @return string Filtered post content with allowed HTML tags and attributes intact.
  1802. */
  1803. function wp_kses_post( $data ) {
  1804. return wp_kses( $data, 'post' );
  1805. }
  1806. /**
  1807. * Navigates through an array, object, or scalar, and sanitizes content for
  1808. * allowed HTML tags for post content.
  1809. *
  1810. * @since 4.4.2
  1811. *
  1812. * @see map_deep()
  1813. *
  1814. * @param mixed $data The array, object, or scalar value to inspect.
  1815. * @return mixed The filtered content.
  1816. */
  1817. function wp_kses_post_deep( $data ) {
  1818. return map_deep( $data, 'wp_kses_post' );
  1819. }
  1820. /**
  1821. * Strips all HTML from a text string.
  1822. *
  1823. * This function expects slashed data.
  1824. *
  1825. * @since 2.1.0
  1826. *
  1827. * @param string $data Content to strip all HTML from.
  1828. * @return string Filtered content without any HTML.
  1829. */
  1830. function wp_filter_nohtml_kses( $data ) {
  1831. return addslashes( wp_kses( stripslashes( $data ), 'strip' ) );
  1832. }
  1833. /**
  1834. * Adds all KSES input form content filters.
  1835. *
  1836. * All hooks have default priority. The `wp_filter_kses()` function is added to
  1837. * the 'pre_comment_content' and 'title_save_pre' hooks.
  1838. *
  1839. * The `wp_filter_post_kses()` function is added to the 'content_save_pre',
  1840. * 'excerpt_save_pre', and 'content_filtered_save_pre' hooks.
  1841. *
  1842. * @since 2.0.0
  1843. */
  1844. function kses_init_filters() {
  1845. // Normal filtering
  1846. add_filter( 'title_save_pre', 'wp_filter_kses' );
  1847. // Comment filtering
  1848. if ( current_user_can( 'unfiltered_html' ) ) {
  1849. add_filter( 'pre_comment_content', 'wp_filter_post_kses' );
  1850. } else {
  1851. add_filter( 'pre_comment_content', 'wp_filter_kses' );
  1852. }
  1853. // Post filtering
  1854. add_filter( 'content_save_pre', 'wp_filter_post_kses' );
  1855. add_filter( 'excerpt_save_pre', 'wp_filter_post_kses' );
  1856. add_filter( 'content_filtered_save_pre', 'wp_filter_post_kses' );
  1857. }
  1858. /**
  1859. * Removes all KSES input form content filters.
  1860. *
  1861. * A quick procedural method to removing all of the filters that KSES uses for
  1862. * content in WordPress Loop.
  1863. *
  1864. * Does not remove the `kses_init()` function from {@see 'init'} hook (priority is
  1865. * default). Also does not remove `kses_init()` function from {@see 'set_current_user'}
  1866. * hook (priority is also default).
  1867. *
  1868. * @since 2.0.6
  1869. */
  1870. function kses_remove_filters() {
  1871. // Normal filtering
  1872. remove_filter( 'title_save_pre', 'wp_filter_kses' );
  1873. // Comment filtering
  1874. remove_filter( 'pre_comment_content', 'wp_filter_post_kses' );
  1875. remove_filter( 'pre_comment_content', 'wp_filter_kses' );
  1876. // Post filtering
  1877. remove_filter( 'content_save_pre', 'wp_filter_post_kses' );
  1878. remove_filter( 'excerpt_save_pre', 'wp_filter_post_kses' );
  1879. remove_filter( 'content_filtered_save_pre', 'wp_filter_post_kses' );
  1880. }
  1881. /**
  1882. * Sets up most of the KSES filters for input form content.
  1883. *
  1884. * First removes all of the KSES filters in case the current user does not need
  1885. * to have KSES filter the content. If the user does not have `unfiltered_html`
  1886. * capability, then KSES filters are added.
  1887. *
  1888. * @since 2.0.0
  1889. */
  1890. function kses_init() {
  1891. kses_remove_filters();
  1892. if ( ! current_user_can( 'unfiltered_html' ) ) {
  1893. kses_init_filters();
  1894. }
  1895. }
  1896. /**
  1897. * Filters an inline style attribute and removes disallowed rules.
  1898. *
  1899. * @since 2.8.1
  1900. *
  1901. * @param string $css A string of CSS rules.
  1902. * @param string $deprecated Not used.
  1903. * @return string Filtered string of CSS rules.
  1904. */
  1905. function safecss_filter_attr( $css, $deprecated = '' ) {
  1906. if ( ! empty( $deprecated ) ) {
  1907. _deprecated_argument( __FUNCTION__, '2.8.1' ); // Never implemented
  1908. }
  1909. $css = wp_kses_no_null( $css );
  1910. $css = str_replace( array( "\n", "\r", "\t" ), '', $css );
  1911. $allowed_protocols = wp_allowed_protocols();
  1912. $css_array = explode( ';', trim( $css ) );
  1913. /**
  1914. * Filters list of allowed CSS attributes.
  1915. *
  1916. * @since 2.8.1
  1917. * @since 4.4.0 Added support for `min-height`, `max-height`, `min-width`, and `max-width`.
  1918. * @since 4.6.0 Added support for `list-style-type`.
  1919. * @since 5.0.0 Added support for `background-image`.
  1920. * @since 5.1.0 Added support for `text-transform`.
  1921. * @since 5.2.0 Added support for `background-position` and `grid-template-columns`
  1922. * @since 5.3.0 Added support for `grid`, `flex` and `column` layout properties.
  1923. * Extend `background-*` support of individual properties.
  1924. *
  1925. * @param string[] $attr Array of allowed CSS attributes.
  1926. */
  1927. $allowed_attr = apply_filters(
  1928. 'safe_style_css',
  1929. array(
  1930. 'background',
  1931. 'background-color',
  1932. 'background-image',
  1933. 'background-position',
  1934. 'background-size',
  1935. 'background-attachment',
  1936. 'background-blend-mode',
  1937. 'border',
  1938. 'border-radius',
  1939. 'border-width',
  1940. 'border-color',
  1941. 'border-style',
  1942. 'border-right',
  1943. 'border-right-color',
  1944. 'border-right-style',
  1945. 'border-right-width',
  1946. 'border-bottom',
  1947. 'border-bottom-color',
  1948. 'border-bottom-style',
  1949. 'border-bottom-width',
  1950. 'border-left',
  1951. 'border-left-color',
  1952. 'border-left-style',
  1953. 'border-left-width',
  1954. 'border-top',
  1955. 'border-top-color',
  1956. 'border-top-style',
  1957. 'border-top-width',
  1958. 'border-spacing',
  1959. 'border-collapse',
  1960. 'caption-side',
  1961. 'columns',
  1962. 'column-count',
  1963. 'column-fill',
  1964. 'column-gap',
  1965. 'column-rule',
  1966. 'column-span',
  1967. 'column-width',
  1968. 'color',
  1969. 'font',
  1970. 'font-family',
  1971. 'font-size',
  1972. 'font-style',
  1973. 'font-variant',
  1974. 'font-weight',
  1975. 'letter-spacing',
  1976. 'line-height',
  1977. 'text-align',
  1978. 'text-decoration',
  1979. 'text-indent',
  1980. 'text-transform',
  1981. 'height',
  1982. 'min-height',
  1983. 'max-height',
  1984. 'width',
  1985. 'min-width',
  1986. 'max-width',
  1987. 'margin',
  1988. 'margin-right',
  1989. 'margin-bottom',
  1990. 'margin-left',
  1991. 'margin-top',
  1992. 'padding',
  1993. 'padding-right',
  1994. 'padding-bottom',
  1995. 'padding-left',
  1996. 'padding-top',
  1997. 'flex',
  1998. 'flex-basis',
  1999. 'flex-direction',
  2000. 'flex-flow',
  2001. 'flex-grow',
  2002. 'flex-shrink',
  2003. 'grid-template-columns',
  2004. 'grid-auto-columns',
  2005. 'grid-column-start',
  2006. 'grid-column-end',
  2007. 'grid-column-gap',
  2008. 'grid-template-rows',
  2009. 'grid-auto-rows',
  2010. 'grid-row-start',
  2011. 'grid-row-end',
  2012. 'grid-row-gap',
  2013. 'grid-gap',
  2014. 'justify-content',
  2015. 'justify-items',
  2016. 'justify-self',
  2017. 'align-content',
  2018. 'align-items',
  2019. 'align-self',
  2020. 'clear',
  2021. 'cursor',
  2022. 'direction',
  2023. 'float',
  2024. 'overflow',
  2025. 'vertical-align',
  2026. 'list-style-type',
  2027. )
  2028. );
  2029. /*
  2030. * CSS attributes that accept URL data types.
  2031. *
  2032. * This is in accordance to the CSS spec and unrelated to
  2033. * the sub-set of supported attributes above.
  2034. *
  2035. * See: https://developer.mozilla.org/en-US/docs/Web/CSS/url
  2036. */
  2037. $css_url_data_types = array(
  2038. 'background',
  2039. 'background-image',
  2040. 'cursor',
  2041. 'list-style',
  2042. 'list-style-image',
  2043. );
  2044. if ( empty( $allowed_attr ) ) {
  2045. return $css;
  2046. }
  2047. $css = '';
  2048. foreach ( $css_array as $css_item ) {
  2049. if ( $css_item == '' ) {
  2050. continue;
  2051. }
  2052. $css_item = trim( $css_item );
  2053. $css_test_string = $css_item;
  2054. $found = false;
  2055. $url_attr = false;
  2056. if ( strpos( $css_item, ':' ) === false ) {
  2057. $found = true;
  2058. } else {
  2059. $parts = explode( ':', $css_item, 2 );
  2060. $css_selector = trim( $parts[0] );
  2061. if ( in_array( $css_selector, $allowed_attr, true ) ) {
  2062. $found = true;
  2063. $url_attr = in_array( $css_selector, $css_url_data_types, true );
  2064. }
  2065. }
  2066. if ( $found && $url_attr ) {
  2067. // Simplified: matches the sequence `url(*)`.
  2068. preg_match_all( '/url\([^)]+\)/', $parts[1], $url_matches );
  2069. foreach ( $url_matches[0] as $url_match ) {
  2070. // Clean up the URL from each of the matches above.
  2071. preg_match( '/^url\(\s*([\'\"]?)(.*)(\g1)\s*\)$/', $url_match, $url_pieces );
  2072. if ( empty( $url_pieces[2] ) ) {
  2073. $found = false;
  2074. break;
  2075. }
  2076. $url = trim( $url_pieces[2] );
  2077. if ( empty( $url ) || $url !== wp_kses_bad_protocol( $url, $allowed_protocols ) ) {
  2078. $found = false;
  2079. break;
  2080. } else {
  2081. // Remove the whole `url(*)` bit that was matched above from the CSS.
  2082. $css_test_string = str_replace( $url_match, '', $css_test_string );
  2083. }
  2084. }
  2085. }
  2086. // Remove any CSS containing containing \ ( & } = or comments, except for url() useage checked above.
  2087. if ( $found && ! preg_match( '%[\\\(&=}]|/\*%', $css_test_string ) ) {
  2088. if ( $css != '' ) {
  2089. $css .= ';';
  2090. }
  2091. $css .= $css_item;
  2092. }
  2093. }
  2094. return $css;
  2095. }
  2096. /**
  2097. * Helper function to add global attributes to a tag in the allowed html list.
  2098. *
  2099. * @since 3.5.0
  2100. * @since 5.0.0 Add support for `data-*` wildcard attributes.
  2101. * @access private
  2102. * @ignore
  2103. *
  2104. * @param array $value An array of attributes.
  2105. * @return array The array of attributes with global attributes added.
  2106. */
  2107. function _wp_add_global_attributes( $value ) {
  2108. $global_attributes = array(
  2109. 'aria-describedby' => true,
  2110. 'aria-details' => true,
  2111. 'aria-label' => true,
  2112. 'aria-labelledby' => true,
  2113. 'aria-hidden' => true,
  2114. 'class' => true,
  2115. 'id' => true,
  2116. 'style' => true,
  2117. 'title' => true,
  2118. 'role' => true,
  2119. 'data-*' => true,
  2120. );
  2121. if ( true === $value ) {
  2122. $value = array();
  2123. }
  2124. if ( is_array( $value ) ) {
  2125. return array_merge( $value, $global_attributes );
  2126. }
  2127. return $value;
  2128. }