Нет описания

kses.php 67KB

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