Нет описания

kses.php 62KB

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