kses.php 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625
  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 Optional. Array of allowed URL protocols.
  702. * Defaults to the result of wp_allowed_protocols().
  703. * @return string Filtered content containing only the allowed HTML.
  704. */
  705. function wp_kses( $string, $allowed_html, $allowed_protocols = array() ) {
  706. if ( empty( $allowed_protocols ) ) {
  707. $allowed_protocols = wp_allowed_protocols();
  708. }
  709. $string = wp_kses_no_null( $string, array( 'slash_zero' => 'keep' ) );
  710. $string = wp_kses_normalize_entities( $string );
  711. $string = wp_kses_hook( $string, $allowed_html, $allowed_protocols );
  712. return wp_kses_split( $string, $allowed_html, $allowed_protocols );
  713. }
  714. /**
  715. * Filters one HTML attribute and ensures its value is allowed.
  716. *
  717. * This function can escape data in some situations where `wp_kses()` must strip the whole attribute.
  718. *
  719. * @since 4.2.3
  720. *
  721. * @param string $string The 'whole' attribute, including name and value.
  722. * @param string $element The HTML element name to which the attribute belongs.
  723. * @return string Filtered attribute.
  724. */
  725. function wp_kses_one_attr( $string, $element ) {
  726. $uris = wp_kses_uri_attributes();
  727. $allowed_html = wp_kses_allowed_html( 'post' );
  728. $allowed_protocols = wp_allowed_protocols();
  729. $string = wp_kses_no_null( $string, array( 'slash_zero' => 'keep' ) );
  730. // Preserve leading and trailing whitespace.
  731. $matches = array();
  732. preg_match( '/^\s*/', $string, $matches );
  733. $lead = $matches[0];
  734. preg_match( '/\s*$/', $string, $matches );
  735. $trail = $matches[0];
  736. if ( empty( $trail ) ) {
  737. $string = substr( $string, strlen( $lead ) );
  738. } else {
  739. $string = substr( $string, strlen( $lead ), -strlen( $trail ) );
  740. }
  741. // Parse attribute name and value from input.
  742. $split = preg_split( '/\s*=\s*/', $string, 2 );
  743. $name = $split[0];
  744. if ( count( $split ) == 2 ) {
  745. $value = $split[1];
  746. // Remove quotes surrounding $value.
  747. // Also guarantee correct quoting in $string for this one attribute.
  748. if ( '' === $value ) {
  749. $quote = '';
  750. } else {
  751. $quote = $value[0];
  752. }
  753. if ( '"' === $quote || "'" === $quote ) {
  754. if ( substr( $value, -1 ) != $quote ) {
  755. return '';
  756. }
  757. $value = substr( $value, 1, -1 );
  758. } else {
  759. $quote = '"';
  760. }
  761. // Sanitize quotes, angle braces, and entities.
  762. $value = esc_attr( $value );
  763. // Sanitize URI values.
  764. if ( in_array( strtolower( $name ), $uris, true ) ) {
  765. $value = wp_kses_bad_protocol( $value, $allowed_protocols );
  766. }
  767. $string = "$name=$quote$value$quote";
  768. $vless = 'n';
  769. } else {
  770. $value = '';
  771. $vless = 'y';
  772. }
  773. // Sanitize attribute by name.
  774. wp_kses_attr_check( $name, $value, $string, $vless, $element, $allowed_html );
  775. // Restore whitespace.
  776. return $lead . $string . $trail;
  777. }
  778. /**
  779. * Returns an array of allowed HTML tags and attributes for a given context.
  780. *
  781. * @since 3.5.0
  782. * @since 5.0.1 `form` removed as allowable HTML tag.
  783. *
  784. * @global array $allowedposttags
  785. * @global array $allowedtags
  786. * @global array $allowedentitynames
  787. *
  788. * @param string|array $context The context for which to retrieve tags. Allowed values are 'post',
  789. * 'strip', 'data', 'entities', or the name of a field filter such as
  790. * 'pre_user_description', or an array of allowed HTML elements and attributes.
  791. * @return array Array of allowed HTML tags and their allowed attributes.
  792. */
  793. function wp_kses_allowed_html( $context = '' ) {
  794. global $allowedposttags, $allowedtags, $allowedentitynames;
  795. if ( is_array( $context ) ) {
  796. // When `$context` is an array it's actually an array of allowed HTML elements and attributes.
  797. $html = $context;
  798. $context = 'explicit';
  799. /**
  800. * Filters the HTML tags that are allowed for a given context.
  801. *
  802. * HTML tags and attribute names are case-insensitive in HTML but must be
  803. * added to the KSES allow list in lowercase. An item added to the allow list
  804. * in upper or mixed case will not recognized as permitted by KSES.
  805. *
  806. * @since 3.5.0
  807. *
  808. * @param array[] $html Allowed HTML tags.
  809. * @param string $context Context name.
  810. */
  811. return apply_filters( 'wp_kses_allowed_html', $html, $context );
  812. }
  813. switch ( $context ) {
  814. case 'post':
  815. /** This filter is documented in wp-includes/kses.php */
  816. $tags = apply_filters( 'wp_kses_allowed_html', $allowedposttags, $context );
  817. // 5.0.1 removed the `<form>` tag, allow it if a filter is allowing it's sub-elements `<input>` or `<select>`.
  818. if ( ! CUSTOM_TAGS && ! isset( $tags['form'] ) && ( isset( $tags['input'] ) || isset( $tags['select'] ) ) ) {
  819. $tags = $allowedposttags;
  820. $tags['form'] = array(
  821. 'action' => true,
  822. 'accept' => true,
  823. 'accept-charset' => true,
  824. 'enctype' => true,
  825. 'method' => true,
  826. 'name' => true,
  827. 'target' => true,
  828. );
  829. /** This filter is documented in wp-includes/kses.php */
  830. $tags = apply_filters( 'wp_kses_allowed_html', $tags, $context );
  831. }
  832. return $tags;
  833. case 'user_description':
  834. case 'pre_user_description':
  835. $tags = $allowedtags;
  836. $tags['a']['rel'] = true;
  837. /** This filter is documented in wp-includes/kses.php */
  838. return apply_filters( 'wp_kses_allowed_html', $tags, $context );
  839. case 'strip':
  840. /** This filter is documented in wp-includes/kses.php */
  841. return apply_filters( 'wp_kses_allowed_html', array(), $context );
  842. case 'entities':
  843. /** This filter is documented in wp-includes/kses.php */
  844. return apply_filters( 'wp_kses_allowed_html', $allowedentitynames, $context );
  845. case 'data':
  846. default:
  847. /** This filter is documented in wp-includes/kses.php */
  848. return apply_filters( 'wp_kses_allowed_html', $allowedtags, $context );
  849. }
  850. }
  851. /**
  852. * You add any KSES hooks here.
  853. *
  854. * There is currently only one KSES WordPress hook, {@see 'pre_kses'}, and it is called here.
  855. * All parameters are passed to the hooks and expected to receive a string.
  856. *
  857. * @since 1.0.0
  858. *
  859. * @param string $string Content to filter through KSES.
  860. * @param array[]|string $allowed_html An array of allowed HTML elements and attributes,
  861. * or a context name such as 'post'. See wp_kses_allowed_html()
  862. * for the list of accepted context names.
  863. * @param string[] $allowed_protocols Array of allowed URL protocols.
  864. * @return string Filtered content through {@see 'pre_kses'} hook.
  865. */
  866. function wp_kses_hook( $string, $allowed_html, $allowed_protocols ) {
  867. /**
  868. * Filters content to be run through KSES.
  869. *
  870. * @since 2.3.0
  871. *
  872. * @param string $string Content to filter through KSES.
  873. * @param array[]|string $allowed_html An array of allowed HTML elements and attributes,
  874. * or a context name such as 'post'. See wp_kses_allowed_html()
  875. * for the list of accepted context names.
  876. * @param string[] $allowed_protocols Array of allowed URL protocols.
  877. */
  878. return apply_filters( 'pre_kses', $string, $allowed_html, $allowed_protocols );
  879. }
  880. /**
  881. * Returns the version number of KSES.
  882. *
  883. * @since 1.0.0
  884. *
  885. * @return string KSES version number.
  886. */
  887. function wp_kses_version() {
  888. return '0.2.2';
  889. }
  890. /**
  891. * Searches for HTML tags, no matter how malformed.
  892. *
  893. * It also matches stray `>` characters.
  894. *
  895. * @since 1.0.0
  896. *
  897. * @global array[]|string $pass_allowed_html An array of allowed HTML elements and attributes,
  898. * or a context name such as 'post'.
  899. * @global string[] $pass_allowed_protocols Array of allowed URL protocols.
  900. *
  901. * @param string $string Content to filter.
  902. * @param array[]|string $allowed_html An array of allowed HTML elements and attributes,
  903. * or a context name such as 'post'. See wp_kses_allowed_html()
  904. * for the list of accepted context names.
  905. * @param string[] $allowed_protocols Array of allowed URL protocols.
  906. * @return string Content with fixed HTML tags
  907. */
  908. function wp_kses_split( $string, $allowed_html, $allowed_protocols ) {
  909. global $pass_allowed_html, $pass_allowed_protocols;
  910. $pass_allowed_html = $allowed_html;
  911. $pass_allowed_protocols = $allowed_protocols;
  912. return preg_replace_callback( '%(<!--.*?(-->|$))|(<[^>]*(>|$)|>)%', '_wp_kses_split_callback', $string );
  913. }
  914. /**
  915. * Returns an array of HTML attribute names whose value contains a URL.
  916. *
  917. * This function returns a list of all HTML attributes that must contain
  918. * a URL according to the HTML specification.
  919. *
  920. * This list includes URI attributes both allowed and disallowed by KSES.
  921. *
  922. * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes
  923. *
  924. * @since 5.0.1
  925. *
  926. * @return string[] HTML attribute names whose value contains a URL.
  927. */
  928. function wp_kses_uri_attributes() {
  929. $uri_attributes = array(
  930. 'action',
  931. 'archive',
  932. 'background',
  933. 'cite',
  934. 'classid',
  935. 'codebase',
  936. 'data',
  937. 'formaction',
  938. 'href',
  939. 'icon',
  940. 'longdesc',
  941. 'manifest',
  942. 'poster',
  943. 'profile',
  944. 'src',
  945. 'usemap',
  946. 'xmlns',
  947. );
  948. /**
  949. * Filters the list of attributes that are required to contain a URL.
  950. *
  951. * Use this filter to add any `data-` attributes that are required to be
  952. * validated as a URL.
  953. *
  954. * @since 5.0.1
  955. *
  956. * @param string[] $uri_attributes HTML attribute names whose value contains a URL.
  957. */
  958. $uri_attributes = apply_filters( 'wp_kses_uri_attributes', $uri_attributes );
  959. return $uri_attributes;
  960. }
  961. /**
  962. * Callback for `wp_kses_split()`.
  963. *
  964. * @since 3.1.0
  965. * @access private
  966. * @ignore
  967. *
  968. * @global array[]|string $pass_allowed_html An array of allowed HTML elements and attributes,
  969. * or a context name such as 'post'.
  970. * @global string[] $pass_allowed_protocols Array of allowed URL protocols.
  971. *
  972. * @param array $match preg_replace regexp matches
  973. * @return string
  974. */
  975. function _wp_kses_split_callback( $match ) {
  976. global $pass_allowed_html, $pass_allowed_protocols;
  977. return wp_kses_split2( $match[0], $pass_allowed_html, $pass_allowed_protocols );
  978. }
  979. /**
  980. * Callback for `wp_kses_split()` for fixing malformed HTML tags.
  981. *
  982. * This function does a lot of work. It rejects some very malformed things like
  983. * `<:::>`. It returns an empty string, if the element isn't allowed (look ma, no
  984. * `strip_tags()`!). Otherwise it splits the tag into an element and an attribute
  985. * list.
  986. *
  987. * After the tag is split into an element and an attribute list, it is run
  988. * through another filter which will remove illegal attributes and once that is
  989. * completed, will be returned.
  990. *
  991. * @access private
  992. * @ignore
  993. * @since 1.0.0
  994. *
  995. * @param string $string Content to filter.
  996. * @param array[]|string $allowed_html An array of allowed HTML elements and attributes,
  997. * or a context name such as 'post'. See wp_kses_allowed_html()
  998. * for the list of accepted context names.
  999. * @param string[] $allowed_protocols Array of allowed URL protocols.
  1000. * @return string Fixed HTML element
  1001. */
  1002. function wp_kses_split2( $string, $allowed_html, $allowed_protocols ) {
  1003. $string = wp_kses_stripslashes( $string );
  1004. // It matched a ">" character.
  1005. if ( '<' !== substr( $string, 0, 1 ) ) {
  1006. return '&gt;';
  1007. }
  1008. // Allow HTML comments.
  1009. if ( '<!--' === substr( $string, 0, 4 ) ) {
  1010. $string = str_replace( array( '<!--', '-->' ), '', $string );
  1011. while ( ( $newstring = wp_kses( $string, $allowed_html, $allowed_protocols ) ) != $string ) {
  1012. $string = $newstring;
  1013. }
  1014. if ( '' === $string ) {
  1015. return '';
  1016. }
  1017. // Prevent multiple dashes in comments.
  1018. $string = preg_replace( '/--+/', '-', $string );
  1019. // Prevent three dashes closing a comment.
  1020. $string = preg_replace( '/-$/', '', $string );
  1021. return "<!--{$string}-->";
  1022. }
  1023. // It's seriously malformed.
  1024. if ( ! preg_match( '%^<\s*(/\s*)?([a-zA-Z0-9-]+)([^>]*)>?$%', $string, $matches ) ) {
  1025. return '';
  1026. }
  1027. $slash = trim( $matches[1] );
  1028. $elem = $matches[2];
  1029. $attrlist = $matches[3];
  1030. if ( ! is_array( $allowed_html ) ) {
  1031. $allowed_html = wp_kses_allowed_html( $allowed_html );
  1032. }
  1033. // They are using a not allowed HTML element.
  1034. if ( ! isset( $allowed_html[ strtolower( $elem ) ] ) ) {
  1035. return '';
  1036. }
  1037. // No attributes are allowed for closing elements.
  1038. if ( '' !== $slash ) {
  1039. return "</$elem>";
  1040. }
  1041. return wp_kses_attr( $elem, $attrlist, $allowed_html, $allowed_protocols );
  1042. }
  1043. /**
  1044. * Removes all attributes, if none are allowed for this element.
  1045. *
  1046. * If some are allowed it calls `wp_kses_hair()` to split them further, and then
  1047. * it builds up new HTML code from the data that `wp_kses_hair()` returns. It also
  1048. * removes `<` and `>` characters, if there are any left. One more thing it does
  1049. * is to check if the tag has a closing XHTML slash, and if it does, it puts one
  1050. * in the returned code as well.
  1051. *
  1052. * An array of allowed values can be defined for attributes. If the attribute value
  1053. * doesn't fall into the list, the attribute will be removed from the tag.
  1054. *
  1055. * Attributes can be marked as required. If a required attribute is not present,
  1056. * KSES will remove all attributes from the tag. As KSES doesn't match opening and
  1057. * closing tags, it's not possible to safely remove the tag itself, the safest
  1058. * fallback is to strip all attributes from the tag, instead.
  1059. *
  1060. * @since 1.0.0
  1061. * @since 5.9.0 Added support for an array of allowed values for attributes.
  1062. * Added support for required attributes.
  1063. *
  1064. * @param string $element HTML element/tag.
  1065. * @param string $attr HTML attributes from HTML element to closing HTML element tag.
  1066. * @param array[]|string $allowed_html An array of allowed HTML elements and attributes,
  1067. * or a context name such as 'post'. See wp_kses_allowed_html()
  1068. * for the list of accepted context names.
  1069. * @param string[] $allowed_protocols Array of allowed URL protocols.
  1070. * @return string Sanitized HTML element.
  1071. */
  1072. function wp_kses_attr( $element, $attr, $allowed_html, $allowed_protocols ) {
  1073. if ( ! is_array( $allowed_html ) ) {
  1074. $allowed_html = wp_kses_allowed_html( $allowed_html );
  1075. }
  1076. // Is there a closing XHTML slash at the end of the attributes?
  1077. $xhtml_slash = '';
  1078. if ( preg_match( '%\s*/\s*$%', $attr ) ) {
  1079. $xhtml_slash = ' /';
  1080. }
  1081. // Are any attributes allowed at all for this element?
  1082. $element_low = strtolower( $element );
  1083. if ( empty( $allowed_html[ $element_low ] ) || true === $allowed_html[ $element_low ] ) {
  1084. return "<$element$xhtml_slash>";
  1085. }
  1086. // Split it.
  1087. $attrarr = wp_kses_hair( $attr, $allowed_protocols );
  1088. // Check if there are attributes that are required.
  1089. $required_attrs = array_filter(
  1090. $allowed_html[ $element_low ],
  1091. function( $required_attr_limits ) {
  1092. return isset( $required_attr_limits['required'] ) && true === $required_attr_limits['required'];
  1093. }
  1094. );
  1095. /*
  1096. * If a required attribute check fails, we can return nothing for a self-closing tag,
  1097. * but for a non-self-closing tag the best option is to return the element with attributes,
  1098. * as KSES doesn't handle matching the relevant closing tag.
  1099. */
  1100. $stripped_tag = '';
  1101. if ( empty( $xhtml_slash ) ) {
  1102. $stripped_tag = "<$element>";
  1103. }
  1104. // Go through $attrarr, and save the allowed attributes for this element in $attr2.
  1105. $attr2 = '';
  1106. foreach ( $attrarr as $arreach ) {
  1107. // Check if this attribute is required.
  1108. $required = isset( $required_attrs[ strtolower( $arreach['name'] ) ] );
  1109. if ( wp_kses_attr_check( $arreach['name'], $arreach['value'], $arreach['whole'], $arreach['vless'], $element, $allowed_html ) ) {
  1110. $attr2 .= ' ' . $arreach['whole'];
  1111. // If this was a required attribute, we can mark it as found.
  1112. if ( $required ) {
  1113. unset( $required_attrs[ strtolower( $arreach['name'] ) ] );
  1114. }
  1115. } elseif ( $required ) {
  1116. // This attribute was required, but didn't pass the check. The entire tag is not allowed.
  1117. return $stripped_tag;
  1118. }
  1119. }
  1120. // If some required attributes weren't set, the entire tag is not allowed.
  1121. if ( ! empty( $required_attrs ) ) {
  1122. return $stripped_tag;
  1123. }
  1124. // Remove any "<" or ">" characters.
  1125. $attr2 = preg_replace( '/[<>]/', '', $attr2 );
  1126. return "<$element$attr2$xhtml_slash>";
  1127. }
  1128. /**
  1129. * Determines whether an attribute is allowed.
  1130. *
  1131. * @since 4.2.3
  1132. * @since 5.0.0 Added support for `data-*` wildcard attributes.
  1133. *
  1134. * @param string $name The attribute name. Passed by reference. Returns empty string when not allowed.
  1135. * @param string $value The attribute value. Passed by reference. Returns a filtered value.
  1136. * @param string $whole The `name=value` input. Passed by reference. Returns filtered input.
  1137. * @param string $vless Whether the attribute is valueless. Use 'y' or 'n'.
  1138. * @param string $element The name of the element to which this attribute belongs.
  1139. * @param array $allowed_html The full list of allowed elements and attributes.
  1140. * @return bool Whether or not the attribute is allowed.
  1141. */
  1142. function wp_kses_attr_check( &$name, &$value, &$whole, $vless, $element, $allowed_html ) {
  1143. $name_low = strtolower( $name );
  1144. $element_low = strtolower( $element );
  1145. if ( ! isset( $allowed_html[ $element_low ] ) ) {
  1146. $name = '';
  1147. $value = '';
  1148. $whole = '';
  1149. return false;
  1150. }
  1151. $allowed_attr = $allowed_html[ $element_low ];
  1152. if ( ! isset( $allowed_attr[ $name_low ] ) || '' === $allowed_attr[ $name_low ] ) {
  1153. /*
  1154. * Allow `data-*` attributes.
  1155. *
  1156. * When specifying `$allowed_html`, the attribute name should be set as
  1157. * `data-*` (not to be mixed with the HTML 4.0 `data` attribute, see
  1158. * https://www.w3.org/TR/html40/struct/objects.html#adef-data).
  1159. *
  1160. * Note: the attribute name should only contain `A-Za-z0-9_-` chars,
  1161. * double hyphens `--` are not accepted by WordPress.
  1162. */
  1163. if ( strpos( $name_low, 'data-' ) === 0 && ! empty( $allowed_attr['data-*'] )
  1164. && preg_match( '/^data(?:-[a-z0-9_]+)+$/', $name_low, $match )
  1165. ) {
  1166. /*
  1167. * Add the whole attribute name to the allowed attributes and set any restrictions
  1168. * for the `data-*` attribute values for the current element.
  1169. */
  1170. $allowed_attr[ $match[0] ] = $allowed_attr['data-*'];
  1171. } else {
  1172. $name = '';
  1173. $value = '';
  1174. $whole = '';
  1175. return false;
  1176. }
  1177. }
  1178. if ( 'style' === $name_low ) {
  1179. $new_value = safecss_filter_attr( $value );
  1180. if ( empty( $new_value ) ) {
  1181. $name = '';
  1182. $value = '';
  1183. $whole = '';
  1184. return false;
  1185. }
  1186. $whole = str_replace( $value, $new_value, $whole );
  1187. $value = $new_value;
  1188. }
  1189. if ( is_array( $allowed_attr[ $name_low ] ) ) {
  1190. // There are some checks.
  1191. foreach ( $allowed_attr[ $name_low ] as $currkey => $currval ) {
  1192. if ( ! wp_kses_check_attr_val( $value, $vless, $currkey, $currval ) ) {
  1193. $name = '';
  1194. $value = '';
  1195. $whole = '';
  1196. return false;
  1197. }
  1198. }
  1199. }
  1200. return true;
  1201. }
  1202. /**
  1203. * Builds an attribute list from string containing attributes.
  1204. *
  1205. * This function does a lot of work. It parses an attribute list into an array
  1206. * with attribute data, and tries to do the right thing even if it gets weird
  1207. * input. It will add quotes around attribute values that don't have any quotes
  1208. * or apostrophes around them, to make it easier to produce HTML code that will
  1209. * conform to W3C's HTML specification. It will also remove bad URL protocols
  1210. * from attribute values. It also reduces duplicate attributes by using the
  1211. * attribute defined first (`foo='bar' foo='baz'` will result in `foo='bar'`).
  1212. *
  1213. * @since 1.0.0
  1214. *
  1215. * @param string $attr Attribute list from HTML element to closing HTML element tag.
  1216. * @param string[] $allowed_protocols Array of allowed URL protocols.
  1217. * @return array[] Array of attribute information after parsing.
  1218. */
  1219. function wp_kses_hair( $attr, $allowed_protocols ) {
  1220. $attrarr = array();
  1221. $mode = 0;
  1222. $attrname = '';
  1223. $uris = wp_kses_uri_attributes();
  1224. // Loop through the whole attribute list.
  1225. while ( strlen( $attr ) != 0 ) {
  1226. $working = 0; // Was the last operation successful?
  1227. switch ( $mode ) {
  1228. case 0:
  1229. if ( preg_match( '/^([_a-zA-Z][-_a-zA-Z0-9:.]*)/', $attr, $match ) ) {
  1230. $attrname = $match[1];
  1231. $working = 1;
  1232. $mode = 1;
  1233. $attr = preg_replace( '/^[_a-zA-Z][-_a-zA-Z0-9:.]*/', '', $attr );
  1234. }
  1235. break;
  1236. case 1:
  1237. if ( preg_match( '/^\s*=\s*/', $attr ) ) { // Equals sign.
  1238. $working = 1;
  1239. $mode = 2;
  1240. $attr = preg_replace( '/^\s*=\s*/', '', $attr );
  1241. break;
  1242. }
  1243. if ( preg_match( '/^\s+/', $attr ) ) { // Valueless.
  1244. $working = 1;
  1245. $mode = 0;
  1246. if ( false === array_key_exists( $attrname, $attrarr ) ) {
  1247. $attrarr[ $attrname ] = array(
  1248. 'name' => $attrname,
  1249. 'value' => '',
  1250. 'whole' => $attrname,
  1251. 'vless' => 'y',
  1252. );
  1253. }
  1254. $attr = preg_replace( '/^\s+/', '', $attr );
  1255. }
  1256. break;
  1257. case 2:
  1258. if ( preg_match( '%^"([^"]*)"(\s+|/?$)%', $attr, $match ) ) {
  1259. // "value"
  1260. $thisval = $match[1];
  1261. if ( in_array( strtolower( $attrname ), $uris, true ) ) {
  1262. $thisval = wp_kses_bad_protocol( $thisval, $allowed_protocols );
  1263. }
  1264. if ( false === array_key_exists( $attrname, $attrarr ) ) {
  1265. $attrarr[ $attrname ] = array(
  1266. 'name' => $attrname,
  1267. 'value' => $thisval,
  1268. 'whole' => "$attrname=\"$thisval\"",
  1269. 'vless' => 'n',
  1270. );
  1271. }
  1272. $working = 1;
  1273. $mode = 0;
  1274. $attr = preg_replace( '/^"[^"]*"(\s+|$)/', '', $attr );
  1275. break;
  1276. }
  1277. if ( preg_match( "%^'([^']*)'(\s+|/?$)%", $attr, $match ) ) {
  1278. // 'value'
  1279. $thisval = $match[1];
  1280. if ( in_array( strtolower( $attrname ), $uris, true ) ) {
  1281. $thisval = wp_kses_bad_protocol( $thisval, $allowed_protocols );
  1282. }
  1283. if ( false === array_key_exists( $attrname, $attrarr ) ) {
  1284. $attrarr[ $attrname ] = array(
  1285. 'name' => $attrname,
  1286. 'value' => $thisval,
  1287. 'whole' => "$attrname='$thisval'",
  1288. 'vless' => 'n',
  1289. );
  1290. }
  1291. $working = 1;
  1292. $mode = 0;
  1293. $attr = preg_replace( "/^'[^']*'(\s+|$)/", '', $attr );
  1294. break;
  1295. }
  1296. if ( preg_match( "%^([^\s\"']+)(\s+|/?$)%", $attr, $match ) ) {
  1297. // value
  1298. $thisval = $match[1];
  1299. if ( in_array( strtolower( $attrname ), $uris, true ) ) {
  1300. $thisval = wp_kses_bad_protocol( $thisval, $allowed_protocols );
  1301. }
  1302. if ( false === array_key_exists( $attrname, $attrarr ) ) {
  1303. $attrarr[ $attrname ] = array(
  1304. 'name' => $attrname,
  1305. 'value' => $thisval,
  1306. 'whole' => "$attrname=\"$thisval\"",
  1307. 'vless' => 'n',
  1308. );
  1309. }
  1310. // We add quotes to conform to W3C's HTML spec.
  1311. $working = 1;
  1312. $mode = 0;
  1313. $attr = preg_replace( "%^[^\s\"']+(\s+|$)%", '', $attr );
  1314. }
  1315. break;
  1316. } // End switch.
  1317. if ( 0 == $working ) { // Not well-formed, remove and try again.
  1318. $attr = wp_kses_html_error( $attr );
  1319. $mode = 0;
  1320. }
  1321. } // End while.
  1322. if ( 1 == $mode && false === array_key_exists( $attrname, $attrarr ) ) {
  1323. // Special case, for when the attribute list ends with a valueless
  1324. // attribute like "selected".
  1325. $attrarr[ $attrname ] = array(
  1326. 'name' => $attrname,
  1327. 'value' => '',
  1328. 'whole' => $attrname,
  1329. 'vless' => 'y',
  1330. );
  1331. }
  1332. return $attrarr;
  1333. }
  1334. /**
  1335. * Finds all attributes of an HTML element.
  1336. *
  1337. * Does not modify input. May return "evil" output.
  1338. *
  1339. * Based on `wp_kses_split2()` and `wp_kses_attr()`.
  1340. *
  1341. * @since 4.2.3
  1342. *
  1343. * @param string $element HTML element.
  1344. * @return array|false List of attributes found in the element. Returns false on failure.
  1345. */
  1346. function wp_kses_attr_parse( $element ) {
  1347. $valid = preg_match( '%^(<\s*)(/\s*)?([a-zA-Z0-9]+\s*)([^>]*)(>?)$%', $element, $matches );
  1348. if ( 1 !== $valid ) {
  1349. return false;
  1350. }
  1351. $begin = $matches[1];
  1352. $slash = $matches[2];
  1353. $elname = $matches[3];
  1354. $attr = $matches[4];
  1355. $end = $matches[5];
  1356. if ( '' !== $slash ) {
  1357. // Closing elements do not get parsed.
  1358. return false;
  1359. }
  1360. // Is there a closing XHTML slash at the end of the attributes?
  1361. if ( 1 === preg_match( '%\s*/\s*$%', $attr, $matches ) ) {
  1362. $xhtml_slash = $matches[0];
  1363. $attr = substr( $attr, 0, -strlen( $xhtml_slash ) );
  1364. } else {
  1365. $xhtml_slash = '';
  1366. }
  1367. // Split it.
  1368. $attrarr = wp_kses_hair_parse( $attr );
  1369. if ( false === $attrarr ) {
  1370. return false;
  1371. }
  1372. // Make sure all input is returned by adding front and back matter.
  1373. array_unshift( $attrarr, $begin . $slash . $elname );
  1374. array_push( $attrarr, $xhtml_slash . $end );
  1375. return $attrarr;
  1376. }
  1377. /**
  1378. * Builds an attribute list from string containing attributes.
  1379. *
  1380. * Does not modify input. May return "evil" output.
  1381. * In case of unexpected input, returns false instead of stripping things.
  1382. *
  1383. * Based on `wp_kses_hair()` but does not return a multi-dimensional array.
  1384. *
  1385. * @since 4.2.3
  1386. *
  1387. * @param string $attr Attribute list from HTML element to closing HTML element tag.
  1388. * @return array|false List of attributes found in $attr. Returns false on failure.
  1389. */
  1390. function wp_kses_hair_parse( $attr ) {
  1391. if ( '' === $attr ) {
  1392. return array();
  1393. }
  1394. // phpcs:disable Squiz.Strings.ConcatenationSpacing.PaddingFound -- don't remove regex indentation
  1395. $regex =
  1396. '(?:'
  1397. . '[_a-zA-Z][-_a-zA-Z0-9:.]*' // Attribute name.
  1398. . '|'
  1399. . '\[\[?[^\[\]]+\]\]?' // Shortcode in the name position implies unfiltered_html.
  1400. . ')'
  1401. . '(?:' // Attribute value.
  1402. . '\s*=\s*' // All values begin with '='.
  1403. . '(?:'
  1404. . '"[^"]*"' // Double-quoted.
  1405. . '|'
  1406. . "'[^']*'" // Single-quoted.
  1407. . '|'
  1408. . '[^\s"\']+' // Non-quoted.
  1409. . '(?:\s|$)' // Must have a space.
  1410. . ')'
  1411. . '|'
  1412. . '(?:\s|$)' // If attribute has no value, space is required.
  1413. . ')'
  1414. . '\s*'; // Trailing space is optional except as mentioned above.
  1415. // phpcs:enable
  1416. // Although it is possible to reduce this procedure to a single regexp,
  1417. // we must run that regexp twice to get exactly the expected result.
  1418. $validation = "%^($regex)+$%";
  1419. $extraction = "%$regex%";
  1420. if ( 1 === preg_match( $validation, $attr ) ) {
  1421. preg_match_all( $extraction, $attr, $attrarr );
  1422. return $attrarr[0];
  1423. } else {
  1424. return false;
  1425. }
  1426. }
  1427. /**
  1428. * Performs different checks for attribute values.
  1429. *
  1430. * The currently implemented checks are "maxlen", "minlen", "maxval", "minval",
  1431. * and "valueless".
  1432. *
  1433. * @since 1.0.0
  1434. *
  1435. * @param string $value Attribute value.
  1436. * @param string $vless Whether the attribute is valueless. Use 'y' or 'n'.
  1437. * @param string $checkname What $checkvalue is checking for.
  1438. * @param mixed $checkvalue What constraint the value should pass.
  1439. * @return bool Whether check passes.
  1440. */
  1441. function wp_kses_check_attr_val( $value, $vless, $checkname, $checkvalue ) {
  1442. $ok = true;
  1443. switch ( strtolower( $checkname ) ) {
  1444. case 'maxlen':
  1445. /*
  1446. * The maxlen check makes sure that the attribute value has a length not
  1447. * greater than the given value. This can be used to avoid Buffer Overflows
  1448. * in WWW clients and various Internet servers.
  1449. */
  1450. if ( strlen( $value ) > $checkvalue ) {
  1451. $ok = false;
  1452. }
  1453. break;
  1454. case 'minlen':
  1455. /*
  1456. * The minlen check makes sure that the attribute value has a length not
  1457. * smaller than the given value.
  1458. */
  1459. if ( strlen( $value ) < $checkvalue ) {
  1460. $ok = false;
  1461. }
  1462. break;
  1463. case 'maxval':
  1464. /*
  1465. * The maxval check does two things: it checks that the attribute value is
  1466. * an integer from 0 and up, without an excessive amount of zeroes or
  1467. * whitespace (to avoid Buffer Overflows). It also checks that the attribute
  1468. * value is not greater than the given value.
  1469. * This check can be used to avoid Denial of Service attacks.
  1470. */
  1471. if ( ! preg_match( '/^\s{0,6}[0-9]{1,6}\s{0,6}$/', $value ) ) {
  1472. $ok = false;
  1473. }
  1474. if ( $value > $checkvalue ) {
  1475. $ok = false;
  1476. }
  1477. break;
  1478. case 'minval':
  1479. /*
  1480. * The minval check makes sure that the attribute value is a positive integer,
  1481. * and that it is not smaller than the given value.
  1482. */
  1483. if ( ! preg_match( '/^\s{0,6}[0-9]{1,6}\s{0,6}$/', $value ) ) {
  1484. $ok = false;
  1485. }
  1486. if ( $value < $checkvalue ) {
  1487. $ok = false;
  1488. }
  1489. break;
  1490. case 'valueless':
  1491. /*
  1492. * The valueless check makes sure if the attribute has a value
  1493. * (like `<a href="blah">`) or not (`<option selected>`). If the given value
  1494. * is a "y" or a "Y", the attribute must not have a value.
  1495. * If the given value is an "n" or an "N", the attribute must have a value.
  1496. */
  1497. if ( strtolower( $checkvalue ) != $vless ) {
  1498. $ok = false;
  1499. }
  1500. break;
  1501. case 'values':
  1502. /*
  1503. * The values check is used when you want to make sure that the attribute
  1504. * has one of the given values.
  1505. */
  1506. if ( false === array_search( strtolower( $value ), $checkvalue, true ) ) {
  1507. $ok = false;
  1508. }
  1509. break;
  1510. case 'value_callback':
  1511. /*
  1512. * The value_callback check is used when you want to make sure that the attribute
  1513. * value is accepted by the callback function.
  1514. */
  1515. if ( ! call_user_func( $checkvalue, $value ) ) {
  1516. $ok = false;
  1517. }
  1518. break;
  1519. } // End switch.
  1520. return $ok;
  1521. }
  1522. /**
  1523. * Sanitizes a string and removed disallowed URL protocols.
  1524. *
  1525. * This function removes all non-allowed protocols from the beginning of the
  1526. * string. It ignores whitespace and the case of the letters, and it does
  1527. * understand HTML entities. It does its work recursively, so it won't be
  1528. * fooled by a string like `javascript:javascript:alert(57)`.
  1529. *
  1530. * @since 1.0.0
  1531. *
  1532. * @param string $string Content to filter bad protocols from.
  1533. * @param string[] $allowed_protocols Array of allowed URL protocols.
  1534. * @return string Filtered content.
  1535. */
  1536. function wp_kses_bad_protocol( $string, $allowed_protocols ) {
  1537. $string = wp_kses_no_null( $string );
  1538. $iterations = 0;
  1539. do {
  1540. $original_string = $string;
  1541. $string = wp_kses_bad_protocol_once( $string, $allowed_protocols );
  1542. } while ( $original_string != $string && ++$iterations < 6 );
  1543. if ( $original_string != $string ) {
  1544. return '';
  1545. }
  1546. return $string;
  1547. }
  1548. /**
  1549. * Removes any invalid control characters in a text string.
  1550. *
  1551. * Also removes any instance of the `\0` string.
  1552. *
  1553. * @since 1.0.0
  1554. *
  1555. * @param string $string Content to filter null characters from.
  1556. * @param array $options Set 'slash_zero' => 'keep' when '\0' is allowed. Default is 'remove'.
  1557. * @return string Filtered content.
  1558. */
  1559. function wp_kses_no_null( $string, $options = null ) {
  1560. if ( ! isset( $options['slash_zero'] ) ) {
  1561. $options = array( 'slash_zero' => 'remove' );
  1562. }
  1563. $string = preg_replace( '/[\x00-\x08\x0B\x0C\x0E-\x1F]/', '', $string );
  1564. if ( 'remove' === $options['slash_zero'] ) {
  1565. $string = preg_replace( '/\\\\+0+/', '', $string );
  1566. }
  1567. return $string;
  1568. }
  1569. /**
  1570. * Strips slashes from in front of quotes.
  1571. *
  1572. * This function changes the character sequence `\"` to just `"`. It leaves all other
  1573. * slashes alone. The quoting from `preg_replace(//e)` requires this.
  1574. *
  1575. * @since 1.0.0
  1576. *
  1577. * @param string $string String to strip slashes from.
  1578. * @return string Fixed string with quoted slashes.
  1579. */
  1580. function wp_kses_stripslashes( $string ) {
  1581. return preg_replace( '%\\\\"%', '"', $string );
  1582. }
  1583. /**
  1584. * Converts the keys of an array to lowercase.
  1585. *
  1586. * @since 1.0.0
  1587. *
  1588. * @param array $inarray Unfiltered array.
  1589. * @return array Fixed array with all lowercase keys.
  1590. */
  1591. function wp_kses_array_lc( $inarray ) {
  1592. $outarray = array();
  1593. foreach ( (array) $inarray as $inkey => $inval ) {
  1594. $outkey = strtolower( $inkey );
  1595. $outarray[ $outkey ] = array();
  1596. foreach ( (array) $inval as $inkey2 => $inval2 ) {
  1597. $outkey2 = strtolower( $inkey2 );
  1598. $outarray[ $outkey ][ $outkey2 ] = $inval2;
  1599. }
  1600. }
  1601. return $outarray;
  1602. }
  1603. /**
  1604. * Handles parsing errors in `wp_kses_hair()`.
  1605. *
  1606. * The general plan is to remove everything to and including some whitespace,
  1607. * but it deals with quotes and apostrophes as well.
  1608. *
  1609. * @since 1.0.0
  1610. *
  1611. * @param string $string
  1612. * @return string
  1613. */
  1614. function wp_kses_html_error( $string ) {
  1615. return preg_replace( '/^("[^"]*("|$)|\'[^\']*(\'|$)|\S)*\s*/', '', $string );
  1616. }
  1617. /**
  1618. * Sanitizes content from bad protocols and other characters.
  1619. *
  1620. * This function searches for URL protocols at the beginning of the string, while
  1621. * handling whitespace and HTML entities.
  1622. *
  1623. * @since 1.0.0
  1624. *
  1625. * @param string $string Content to check for bad protocols.
  1626. * @param string[] $allowed_protocols Array of allowed URL protocols.
  1627. * @param int $count Depth of call recursion to this function.
  1628. * @return string Sanitized content.
  1629. */
  1630. function wp_kses_bad_protocol_once( $string, $allowed_protocols, $count = 1 ) {
  1631. $string = preg_replace( '/(&#0*58(?![;0-9])|&#x0*3a(?![;a-f0-9]))/i', '$1;', $string );
  1632. $string2 = preg_split( '/:|&#0*58;|&#x0*3a;|&colon;/i', $string, 2 );
  1633. if ( isset( $string2[1] ) && ! preg_match( '%/\?%', $string2[0] ) ) {
  1634. $string = trim( $string2[1] );
  1635. $protocol = wp_kses_bad_protocol_once2( $string2[0], $allowed_protocols );
  1636. if ( 'feed:' === $protocol ) {
  1637. if ( $count > 2 ) {
  1638. return '';
  1639. }
  1640. $string = wp_kses_bad_protocol_once( $string, $allowed_protocols, ++$count );
  1641. if ( empty( $string ) ) {
  1642. return $string;
  1643. }
  1644. }
  1645. $string = $protocol . $string;
  1646. }
  1647. return $string;
  1648. }
  1649. /**
  1650. * Callback for `wp_kses_bad_protocol_once()` regular expression.
  1651. *
  1652. * This function processes URL protocols, checks to see if they're in the
  1653. * list of allowed protocols or not, and returns different data depending
  1654. * on the answer.
  1655. *
  1656. * @access private
  1657. * @ignore
  1658. * @since 1.0.0
  1659. *
  1660. * @param string $string URI scheme to check against the list of allowed protocols.
  1661. * @param string[] $allowed_protocols Array of allowed URL protocols.
  1662. * @return string Sanitized content.
  1663. */
  1664. function wp_kses_bad_protocol_once2( $string, $allowed_protocols ) {
  1665. $string2 = wp_kses_decode_entities( $string );
  1666. $string2 = preg_replace( '/\s/', '', $string2 );
  1667. $string2 = wp_kses_no_null( $string2 );
  1668. $string2 = strtolower( $string2 );
  1669. $allowed = false;
  1670. foreach ( (array) $allowed_protocols as $one_protocol ) {
  1671. if ( strtolower( $one_protocol ) == $string2 ) {
  1672. $allowed = true;
  1673. break;
  1674. }
  1675. }
  1676. if ( $allowed ) {
  1677. return "$string2:";
  1678. } else {
  1679. return '';
  1680. }
  1681. }
  1682. /**
  1683. * Converts and fixes HTML entities.
  1684. *
  1685. * This function normalizes HTML entities. It will convert `AT&T` to the correct
  1686. * `AT&amp;T`, `&#00058;` to `&#058;`, `&#XYZZY;` to `&amp;#XYZZY;` and so on.
  1687. *
  1688. * When `$context` is set to 'xml', HTML entities are converted to their code points. For
  1689. * example, `AT&T&hellip;&#XYZZY;` is converted to `AT&amp;T…&amp;#XYZZY;`.
  1690. *
  1691. * @since 1.0.0
  1692. * @since 5.5.0 Added `$context` parameter.
  1693. *
  1694. * @param string $string Content to normalize entities.
  1695. * @param string $context Context for normalization. Can be either 'html' or 'xml'.
  1696. * Default 'html'.
  1697. * @return string Content with normalized entities.
  1698. */
  1699. function wp_kses_normalize_entities( $string, $context = 'html' ) {
  1700. // Disarm all entities by converting & to &amp;
  1701. $string = str_replace( '&', '&amp;', $string );
  1702. // Change back the allowed entities in our list of allowed entities.
  1703. if ( 'xml' === $context ) {
  1704. $string = preg_replace_callback( '/&amp;([A-Za-z]{2,8}[0-9]{0,2});/', 'wp_kses_xml_named_entities', $string );
  1705. } else {
  1706. $string = preg_replace_callback( '/&amp;([A-Za-z]{2,8}[0-9]{0,2});/', 'wp_kses_named_entities', $string );
  1707. }
  1708. $string = preg_replace_callback( '/&amp;#(0*[0-9]{1,7});/', 'wp_kses_normalize_entities2', $string );
  1709. $string = preg_replace_callback( '/&amp;#[Xx](0*[0-9A-Fa-f]{1,6});/', 'wp_kses_normalize_entities3', $string );
  1710. return $string;
  1711. }
  1712. /**
  1713. * Callback for `wp_kses_normalize_entities()` regular expression.
  1714. *
  1715. * This function only accepts valid named entity references, which are finite,
  1716. * case-sensitive, and highly scrutinized by HTML and XML validators.
  1717. *
  1718. * @since 3.0.0
  1719. *
  1720. * @global array $allowedentitynames
  1721. *
  1722. * @param array $matches preg_replace_callback() matches array.
  1723. * @return string Correctly encoded entity.
  1724. */
  1725. function wp_kses_named_entities( $matches ) {
  1726. global $allowedentitynames;
  1727. if ( empty( $matches[1] ) ) {
  1728. return '';
  1729. }
  1730. $i = $matches[1];
  1731. return ( ! in_array( $i, $allowedentitynames, true ) ) ? "&amp;$i;" : "&$i;";
  1732. }
  1733. /**
  1734. * Callback for `wp_kses_normalize_entities()` regular expression.
  1735. *
  1736. * This function only accepts valid named entity references, which are finite,
  1737. * case-sensitive, and highly scrutinized by XML validators. HTML named entity
  1738. * references are converted to their code points.
  1739. *
  1740. * @since 5.5.0
  1741. *
  1742. * @global array $allowedentitynames
  1743. * @global array $allowedxmlentitynames
  1744. *
  1745. * @param array $matches preg_replace_callback() matches array.
  1746. * @return string Correctly encoded entity.
  1747. */
  1748. function wp_kses_xml_named_entities( $matches ) {
  1749. global $allowedentitynames, $allowedxmlentitynames;
  1750. if ( empty( $matches[1] ) ) {
  1751. return '';
  1752. }
  1753. $i = $matches[1];
  1754. if ( in_array( $i, $allowedxmlentitynames, true ) ) {
  1755. return "&$i;";
  1756. } elseif ( in_array( $i, $allowedentitynames, true ) ) {
  1757. return html_entity_decode( "&$i;", ENT_HTML5 );
  1758. }
  1759. return "&amp;$i;";
  1760. }
  1761. /**
  1762. * Callback for `wp_kses_normalize_entities()` regular expression.
  1763. *
  1764. * This function helps `wp_kses_normalize_entities()` to only accept 16-bit
  1765. * values and nothing more for `&#number;` entities.
  1766. *
  1767. * @access private
  1768. * @ignore
  1769. * @since 1.0.0
  1770. *
  1771. * @param array $matches `preg_replace_callback()` matches array.
  1772. * @return string Correctly encoded entity.
  1773. */
  1774. function wp_kses_normalize_entities2( $matches ) {
  1775. if ( empty( $matches[1] ) ) {
  1776. return '';
  1777. }
  1778. $i = $matches[1];
  1779. if ( valid_unicode( $i ) ) {
  1780. $i = str_pad( ltrim( $i, '0' ), 3, '0', STR_PAD_LEFT );
  1781. $i = "&#$i;";
  1782. } else {
  1783. $i = "&amp;#$i;";
  1784. }
  1785. return $i;
  1786. }
  1787. /**
  1788. * Callback for `wp_kses_normalize_entities()` for regular expression.
  1789. *
  1790. * This function helps `wp_kses_normalize_entities()` to only accept valid Unicode
  1791. * numeric entities in hex form.
  1792. *
  1793. * @since 2.7.0
  1794. * @access private
  1795. * @ignore
  1796. *
  1797. * @param array $matches `preg_replace_callback()` matches array.
  1798. * @return string Correctly encoded entity.
  1799. */
  1800. function wp_kses_normalize_entities3( $matches ) {
  1801. if ( empty( $matches[1] ) ) {
  1802. return '';
  1803. }
  1804. $hexchars = $matches[1];
  1805. return ( ! valid_unicode( hexdec( $hexchars ) ) ) ? "&amp;#x$hexchars;" : '&#x' . ltrim( $hexchars, '0' ) . ';';
  1806. }
  1807. /**
  1808. * Determines if a Unicode codepoint is valid.
  1809. *
  1810. * @since 2.7.0
  1811. *
  1812. * @param int $i Unicode codepoint.
  1813. * @return bool Whether or not the codepoint is a valid Unicode codepoint.
  1814. */
  1815. function valid_unicode( $i ) {
  1816. return ( 0x9 == $i || 0xa == $i || 0xd == $i ||
  1817. ( 0x20 <= $i && $i <= 0xd7ff ) ||
  1818. ( 0xe000 <= $i && $i <= 0xfffd ) ||
  1819. ( 0x10000 <= $i && $i <= 0x10ffff ) );
  1820. }
  1821. /**
  1822. * Converts all numeric HTML entities to their named counterparts.
  1823. *
  1824. * This function decodes numeric HTML entities (`&#65;` and `&#x41;`).
  1825. * It doesn't do anything with named entities like `&auml;`, but we don't
  1826. * need them in the allowed URL protocols system anyway.
  1827. *
  1828. * @since 1.0.0
  1829. *
  1830. * @param string $string Content to change entities.
  1831. * @return string Content after decoded entities.
  1832. */
  1833. function wp_kses_decode_entities( $string ) {
  1834. $string = preg_replace_callback( '/&#([0-9]+);/', '_wp_kses_decode_entities_chr', $string );
  1835. $string = preg_replace_callback( '/&#[Xx]([0-9A-Fa-f]+);/', '_wp_kses_decode_entities_chr_hexdec', $string );
  1836. return $string;
  1837. }
  1838. /**
  1839. * Regex callback for `wp_kses_decode_entities()`.
  1840. *
  1841. * @since 2.9.0
  1842. * @access private
  1843. * @ignore
  1844. *
  1845. * @param array $match preg match
  1846. * @return string
  1847. */
  1848. function _wp_kses_decode_entities_chr( $match ) {
  1849. return chr( $match[1] );
  1850. }
  1851. /**
  1852. * Regex callback for `wp_kses_decode_entities()`.
  1853. *
  1854. * @since 2.9.0
  1855. * @access private
  1856. * @ignore
  1857. *
  1858. * @param array $match preg match
  1859. * @return string
  1860. */
  1861. function _wp_kses_decode_entities_chr_hexdec( $match ) {
  1862. return chr( hexdec( $match[1] ) );
  1863. }
  1864. /**
  1865. * Sanitize content with allowed HTML KSES rules.
  1866. *
  1867. * This function expects slashed data.
  1868. *
  1869. * @since 1.0.0
  1870. *
  1871. * @param string $data Content to filter, expected to be escaped with slashes.
  1872. * @return string Filtered content.
  1873. */
  1874. function wp_filter_kses( $data ) {
  1875. return addslashes( wp_kses( stripslashes( $data ), current_filter() ) );
  1876. }
  1877. /**
  1878. * Sanitize content with allowed HTML KSES rules.
  1879. *
  1880. * This function expects unslashed data.
  1881. *
  1882. * @since 2.9.0
  1883. *
  1884. * @param string $data Content to filter, expected to not be escaped.
  1885. * @return string Filtered content.
  1886. */
  1887. function wp_kses_data( $data ) {
  1888. return wp_kses( $data, current_filter() );
  1889. }
  1890. /**
  1891. * Sanitizes content for allowed HTML tags for post content.
  1892. *
  1893. * Post content refers to the page contents of the 'post' type and not `$_POST`
  1894. * data from forms.
  1895. *
  1896. * This function expects slashed data.
  1897. *
  1898. * @since 2.0.0
  1899. *
  1900. * @param string $data Post content to filter, expected to be escaped with slashes.
  1901. * @return string Filtered post content with allowed HTML tags and attributes intact.
  1902. */
  1903. function wp_filter_post_kses( $data ) {
  1904. return addslashes( wp_kses( stripslashes( $data ), 'post' ) );
  1905. }
  1906. /**
  1907. * Sanitizes global styles user content removing unsafe rules.
  1908. *
  1909. * @since 5.9.0
  1910. *
  1911. * @param string $data Post content to filter.
  1912. * @return string Filtered post content with unsafe rules removed.
  1913. */
  1914. function wp_filter_global_styles_post( $data ) {
  1915. $decoded_data = json_decode( wp_unslash( $data ), true );
  1916. $json_decoding_error = json_last_error();
  1917. if (
  1918. JSON_ERROR_NONE === $json_decoding_error &&
  1919. is_array( $decoded_data ) &&
  1920. isset( $decoded_data['isGlobalStylesUserThemeJSON'] ) &&
  1921. $decoded_data['isGlobalStylesUserThemeJSON']
  1922. ) {
  1923. unset( $decoded_data['isGlobalStylesUserThemeJSON'] );
  1924. $data_to_encode = WP_Theme_JSON::remove_insecure_properties( $decoded_data );
  1925. $data_to_encode['isGlobalStylesUserThemeJSON'] = true;
  1926. return wp_slash( wp_json_encode( $data_to_encode ) );
  1927. }
  1928. return $data;
  1929. }
  1930. /**
  1931. * Sanitizes content for allowed HTML tags for post content.
  1932. *
  1933. * Post content refers to the page contents of the 'post' type and not `$_POST`
  1934. * data from forms.
  1935. *
  1936. * This function expects unslashed data.
  1937. *
  1938. * @since 2.9.0
  1939. *
  1940. * @param string $data Post content to filter.
  1941. * @return string Filtered post content with allowed HTML tags and attributes intact.
  1942. */
  1943. function wp_kses_post( $data ) {
  1944. return wp_kses( $data, 'post' );
  1945. }
  1946. /**
  1947. * Navigates through an array, object, or scalar, and sanitizes content for
  1948. * allowed HTML tags for post content.
  1949. *
  1950. * @since 4.4.2
  1951. *
  1952. * @see map_deep()
  1953. *
  1954. * @param mixed $data The array, object, or scalar value to inspect.
  1955. * @return mixed The filtered content.
  1956. */
  1957. function wp_kses_post_deep( $data ) {
  1958. return map_deep( $data, 'wp_kses_post' );
  1959. }
  1960. /**
  1961. * Strips all HTML from a text string.
  1962. *
  1963. * This function expects slashed data.
  1964. *
  1965. * @since 2.1.0
  1966. *
  1967. * @param string $data Content to strip all HTML from.
  1968. * @return string Filtered content without any HTML.
  1969. */
  1970. function wp_filter_nohtml_kses( $data ) {
  1971. return addslashes( wp_kses( stripslashes( $data ), 'strip' ) );
  1972. }
  1973. /**
  1974. * Adds all KSES input form content filters.
  1975. *
  1976. * All hooks have default priority. The `wp_filter_kses()` function is added to
  1977. * the 'pre_comment_content' and 'title_save_pre' hooks.
  1978. *
  1979. * The `wp_filter_post_kses()` function is added to the 'content_save_pre',
  1980. * 'excerpt_save_pre', and 'content_filtered_save_pre' hooks.
  1981. *
  1982. * @since 2.0.0
  1983. */
  1984. function kses_init_filters() {
  1985. // Normal filtering.
  1986. add_filter( 'title_save_pre', 'wp_filter_kses' );
  1987. // Comment filtering.
  1988. if ( current_user_can( 'unfiltered_html' ) ) {
  1989. add_filter( 'pre_comment_content', 'wp_filter_post_kses' );
  1990. } else {
  1991. add_filter( 'pre_comment_content', 'wp_filter_kses' );
  1992. }
  1993. // Global Styles filtering: Global Styles filters should be executed before normal post_kses HTML filters.
  1994. add_filter( 'content_save_pre', 'wp_filter_global_styles_post', 9 );
  1995. add_filter( 'content_filtered_save_pre', 'wp_filter_global_styles_post', 9 );
  1996. // Post filtering.
  1997. add_filter( 'content_save_pre', 'wp_filter_post_kses' );
  1998. add_filter( 'excerpt_save_pre', 'wp_filter_post_kses' );
  1999. add_filter( 'content_filtered_save_pre', 'wp_filter_post_kses' );
  2000. }
  2001. /**
  2002. * Removes all KSES input form content filters.
  2003. *
  2004. * A quick procedural method to removing all of the filters that KSES uses for
  2005. * content in WordPress Loop.
  2006. *
  2007. * Does not remove the `kses_init()` function from {@see 'init'} hook (priority is
  2008. * default). Also does not remove `kses_init()` function from {@see 'set_current_user'}
  2009. * hook (priority is also default).
  2010. *
  2011. * @since 2.0.6
  2012. */
  2013. function kses_remove_filters() {
  2014. // Normal filtering.
  2015. remove_filter( 'title_save_pre', 'wp_filter_kses' );
  2016. // Comment filtering.
  2017. remove_filter( 'pre_comment_content', 'wp_filter_post_kses' );
  2018. remove_filter( 'pre_comment_content', 'wp_filter_kses' );
  2019. // Global Styles filtering.
  2020. remove_filter( 'content_save_pre', 'wp_filter_global_styles_post', 9 );
  2021. remove_filter( 'content_filtered_save_pre', 'wp_filter_global_styles_post', 9 );
  2022. // Post filtering.
  2023. remove_filter( 'content_save_pre', 'wp_filter_post_kses' );
  2024. remove_filter( 'excerpt_save_pre', 'wp_filter_post_kses' );
  2025. remove_filter( 'content_filtered_save_pre', 'wp_filter_post_kses' );
  2026. }
  2027. /**
  2028. * Sets up most of the KSES filters for input form content.
  2029. *
  2030. * First removes all of the KSES filters in case the current user does not need
  2031. * to have KSES filter the content. If the user does not have `unfiltered_html`
  2032. * capability, then KSES filters are added.
  2033. *
  2034. * @since 2.0.0
  2035. */
  2036. function kses_init() {
  2037. kses_remove_filters();
  2038. if ( ! current_user_can( 'unfiltered_html' ) ) {
  2039. kses_init_filters();
  2040. }
  2041. }
  2042. /**
  2043. * Filters an inline style attribute and removes disallowed rules.
  2044. *
  2045. * @since 2.8.1
  2046. * @since 4.4.0 Added support for `min-height`, `max-height`, `min-width`, and `max-width`.
  2047. * @since 4.6.0 Added support for `list-style-type`.
  2048. * @since 5.0.0 Added support for `background-image`.
  2049. * @since 5.1.0 Added support for `text-transform`.
  2050. * @since 5.2.0 Added support for `background-position` and `grid-template-columns`.
  2051. * @since 5.3.0 Added support for `grid`, `flex` and `column` layout properties.
  2052. * Extended `background-*` support for individual properties.
  2053. * @since 5.3.1 Added support for gradient backgrounds.
  2054. * @since 5.7.1 Added support for `object-position`.
  2055. * @since 5.8.0 Added support for `calc()` and `var()` values.
  2056. * @since 6.1.0 Added support for `min()`, `max()`, `minmax()`, `clamp()`,
  2057. * nested `var()` values, and assigning values to CSS variables.
  2058. * Added support for `object-fit`, `gap`, `column-gap`, `row-gap`, and `flex-wrap`.
  2059. * Extended `margin-*` and `padding-*` support for logical properties.
  2060. *
  2061. * @param string $css A string of CSS rules.
  2062. * @param string $deprecated Not used.
  2063. * @return string Filtered string of CSS rules.
  2064. */
  2065. function safecss_filter_attr( $css, $deprecated = '' ) {
  2066. if ( ! empty( $deprecated ) ) {
  2067. _deprecated_argument( __FUNCTION__, '2.8.1' ); // Never implemented.
  2068. }
  2069. $css = wp_kses_no_null( $css );
  2070. $css = str_replace( array( "\n", "\r", "\t" ), '', $css );
  2071. $allowed_protocols = wp_allowed_protocols();
  2072. $css_array = explode( ';', trim( $css ) );
  2073. /**
  2074. * Filters the list of allowed CSS attributes.
  2075. *
  2076. * @since 2.8.1
  2077. *
  2078. * @param string[] $attr Array of allowed CSS attributes.
  2079. */
  2080. $allowed_attr = apply_filters(
  2081. 'safe_style_css',
  2082. array(
  2083. 'background',
  2084. 'background-color',
  2085. 'background-image',
  2086. 'background-position',
  2087. 'background-size',
  2088. 'background-attachment',
  2089. 'background-blend-mode',
  2090. 'border',
  2091. 'border-radius',
  2092. 'border-width',
  2093. 'border-color',
  2094. 'border-style',
  2095. 'border-right',
  2096. 'border-right-color',
  2097. 'border-right-style',
  2098. 'border-right-width',
  2099. 'border-bottom',
  2100. 'border-bottom-color',
  2101. 'border-bottom-left-radius',
  2102. 'border-bottom-right-radius',
  2103. 'border-bottom-style',
  2104. 'border-bottom-width',
  2105. 'border-bottom-right-radius',
  2106. 'border-bottom-left-radius',
  2107. 'border-left',
  2108. 'border-left-color',
  2109. 'border-left-style',
  2110. 'border-left-width',
  2111. 'border-top',
  2112. 'border-top-color',
  2113. 'border-top-left-radius',
  2114. 'border-top-right-radius',
  2115. 'border-top-style',
  2116. 'border-top-width',
  2117. 'border-top-left-radius',
  2118. 'border-top-right-radius',
  2119. 'border-spacing',
  2120. 'border-collapse',
  2121. 'caption-side',
  2122. 'columns',
  2123. 'column-count',
  2124. 'column-fill',
  2125. 'column-gap',
  2126. 'column-rule',
  2127. 'column-span',
  2128. 'column-width',
  2129. 'color',
  2130. 'filter',
  2131. 'font',
  2132. 'font-family',
  2133. 'font-size',
  2134. 'font-style',
  2135. 'font-variant',
  2136. 'font-weight',
  2137. 'letter-spacing',
  2138. 'line-height',
  2139. 'text-align',
  2140. 'text-decoration',
  2141. 'text-indent',
  2142. 'text-transform',
  2143. 'height',
  2144. 'min-height',
  2145. 'max-height',
  2146. 'width',
  2147. 'min-width',
  2148. 'max-width',
  2149. 'margin',
  2150. 'margin-right',
  2151. 'margin-bottom',
  2152. 'margin-left',
  2153. 'margin-top',
  2154. 'margin-block-start',
  2155. 'margin-block-end',
  2156. 'margin-inline-start',
  2157. 'margin-inline-end',
  2158. 'padding',
  2159. 'padding-right',
  2160. 'padding-bottom',
  2161. 'padding-left',
  2162. 'padding-top',
  2163. 'padding-block-start',
  2164. 'padding-block-end',
  2165. 'padding-inline-start',
  2166. 'padding-inline-end',
  2167. 'flex',
  2168. 'flex-basis',
  2169. 'flex-direction',
  2170. 'flex-flow',
  2171. 'flex-grow',
  2172. 'flex-shrink',
  2173. 'flex-wrap',
  2174. 'gap',
  2175. 'column-gap',
  2176. 'row-gap',
  2177. 'grid-template-columns',
  2178. 'grid-auto-columns',
  2179. 'grid-column-start',
  2180. 'grid-column-end',
  2181. 'grid-column-gap',
  2182. 'grid-template-rows',
  2183. 'grid-auto-rows',
  2184. 'grid-row-start',
  2185. 'grid-row-end',
  2186. 'grid-row-gap',
  2187. 'grid-gap',
  2188. 'justify-content',
  2189. 'justify-items',
  2190. 'justify-self',
  2191. 'align-content',
  2192. 'align-items',
  2193. 'align-self',
  2194. 'clear',
  2195. 'cursor',
  2196. 'direction',
  2197. 'float',
  2198. 'list-style-type',
  2199. 'object-fit',
  2200. 'object-position',
  2201. 'overflow',
  2202. 'vertical-align',
  2203. // Custom CSS properties.
  2204. '--*',
  2205. )
  2206. );
  2207. /*
  2208. * CSS attributes that accept URL data types.
  2209. *
  2210. * This is in accordance to the CSS spec and unrelated to
  2211. * the sub-set of supported attributes above.
  2212. *
  2213. * See: https://developer.mozilla.org/en-US/docs/Web/CSS/url
  2214. */
  2215. $css_url_data_types = array(
  2216. 'background',
  2217. 'background-image',
  2218. 'cursor',
  2219. 'list-style',
  2220. 'list-style-image',
  2221. );
  2222. /*
  2223. * CSS attributes that accept gradient data types.
  2224. *
  2225. */
  2226. $css_gradient_data_types = array(
  2227. 'background',
  2228. 'background-image',
  2229. );
  2230. if ( empty( $allowed_attr ) ) {
  2231. return $css;
  2232. }
  2233. $css = '';
  2234. foreach ( $css_array as $css_item ) {
  2235. if ( '' === $css_item ) {
  2236. continue;
  2237. }
  2238. $css_item = trim( $css_item );
  2239. $css_test_string = $css_item;
  2240. $found = false;
  2241. $url_attr = false;
  2242. $gradient_attr = false;
  2243. $is_custom_var = false;
  2244. if ( strpos( $css_item, ':' ) === false ) {
  2245. $found = true;
  2246. } else {
  2247. $parts = explode( ':', $css_item, 2 );
  2248. $css_selector = trim( $parts[0] );
  2249. // Allow assigning values to CSS variables.
  2250. if ( in_array( '--*', $allowed_attr, true ) && preg_match( '/^--[a-zA-Z0-9-_]+$/', $css_selector ) ) {
  2251. $allowed_attr[] = $css_selector;
  2252. $is_custom_var = true;
  2253. }
  2254. if ( in_array( $css_selector, $allowed_attr, true ) ) {
  2255. $found = true;
  2256. $url_attr = in_array( $css_selector, $css_url_data_types, true );
  2257. $gradient_attr = in_array( $css_selector, $css_gradient_data_types, true );
  2258. }
  2259. if ( $is_custom_var ) {
  2260. $css_value = trim( $parts[1] );
  2261. $url_attr = str_starts_with( $css_value, 'url(' );
  2262. $gradient_attr = str_contains( $css_value, '-gradient(' );
  2263. }
  2264. }
  2265. if ( $found && $url_attr ) {
  2266. // Simplified: matches the sequence `url(*)`.
  2267. preg_match_all( '/url\([^)]+\)/', $parts[1], $url_matches );
  2268. foreach ( $url_matches[0] as $url_match ) {
  2269. // Clean up the URL from each of the matches above.
  2270. preg_match( '/^url\(\s*([\'\"]?)(.*)(\g1)\s*\)$/', $url_match, $url_pieces );
  2271. if ( empty( $url_pieces[2] ) ) {
  2272. $found = false;
  2273. break;
  2274. }
  2275. $url = trim( $url_pieces[2] );
  2276. if ( empty( $url ) || wp_kses_bad_protocol( $url, $allowed_protocols ) !== $url ) {
  2277. $found = false;
  2278. break;
  2279. } else {
  2280. // Remove the whole `url(*)` bit that was matched above from the CSS.
  2281. $css_test_string = str_replace( $url_match, '', $css_test_string );
  2282. }
  2283. }
  2284. }
  2285. if ( $found && $gradient_attr ) {
  2286. $css_value = trim( $parts[1] );
  2287. if ( preg_match( '/^(repeating-)?(linear|radial|conic)-gradient\(([^()]|rgb[a]?\([^()]*\))*\)$/', $css_value ) ) {
  2288. // Remove the whole `gradient` bit that was matched above from the CSS.
  2289. $css_test_string = str_replace( $css_value, '', $css_test_string );
  2290. }
  2291. }
  2292. if ( $found ) {
  2293. /*
  2294. * Allow CSS functions like var(), calc(), etc. by removing them from the test string.
  2295. * Nested functions and parentheses are also removed, so long as the parentheses are balanced.
  2296. */
  2297. $css_test_string = preg_replace(
  2298. '/\b(?:var|calc|min|max|minmax|clamp)(\((?:[^()]|(?1))*\))/',
  2299. '',
  2300. $css_test_string
  2301. );
  2302. /*
  2303. * Disallow CSS containing \ ( & } = or comments, except for within url(), var(), calc(), etc.
  2304. * which were removed from the test string above.
  2305. */
  2306. $allow_css = ! preg_match( '%[\\\(&=}]|/\*%', $css_test_string );
  2307. /**
  2308. * Filters the check for unsafe CSS in `safecss_filter_attr`.
  2309. *
  2310. * Enables developers to determine whether a section of CSS should be allowed or discarded.
  2311. * By default, the value will be false if the part contains \ ( & } = or comments.
  2312. * Return true to allow the CSS part to be included in the output.
  2313. *
  2314. * @since 5.5.0
  2315. *
  2316. * @param bool $allow_css Whether the CSS in the test string is considered safe.
  2317. * @param string $css_test_string The CSS string to test.
  2318. */
  2319. $allow_css = apply_filters( 'safecss_filter_attr_allow_css', $allow_css, $css_test_string );
  2320. // Only add the CSS part if it passes the regex check.
  2321. if ( $allow_css ) {
  2322. if ( '' !== $css ) {
  2323. $css .= ';';
  2324. }
  2325. $css .= $css_item;
  2326. }
  2327. }
  2328. }
  2329. return $css;
  2330. }
  2331. /**
  2332. * Helper function to add global attributes to a tag in the allowed HTML list.
  2333. *
  2334. * @since 3.5.0
  2335. * @since 5.0.0 Added support for `data-*` wildcard attributes.
  2336. * @since 6.0.0 Added `dir`, `lang`, and `xml:lang` to global attributes.
  2337. *
  2338. * @access private
  2339. * @ignore
  2340. *
  2341. * @param array $value An array of attributes.
  2342. * @return array The array of attributes with global attributes added.
  2343. */
  2344. function _wp_add_global_attributes( $value ) {
  2345. $global_attributes = array(
  2346. 'aria-describedby' => true,
  2347. 'aria-details' => true,
  2348. 'aria-label' => true,
  2349. 'aria-labelledby' => true,
  2350. 'aria-hidden' => true,
  2351. 'class' => true,
  2352. 'data-*' => true,
  2353. 'dir' => true,
  2354. 'id' => true,
  2355. 'lang' => true,
  2356. 'style' => true,
  2357. 'title' => true,
  2358. 'role' => true,
  2359. 'xml:lang' => true,
  2360. );
  2361. if ( true === $value ) {
  2362. $value = array();
  2363. }
  2364. if ( is_array( $value ) ) {
  2365. return array_merge( $value, $global_attributes );
  2366. }
  2367. return $value;
  2368. }
  2369. /**
  2370. * Helper function to check if this is a safe PDF URL.
  2371. *
  2372. * @since 5.9.0
  2373. * @access private
  2374. * @ignore
  2375. *
  2376. * @param string $url The URL to check.
  2377. * @return bool True if the URL is safe, false otherwise.
  2378. */
  2379. function _wp_kses_allow_pdf_objects( $url ) {
  2380. // We're not interested in URLs that contain query strings or fragments.
  2381. if ( str_contains( $url, '?' ) || str_contains( $url, '#' ) ) {
  2382. return false;
  2383. }
  2384. // If it doesn't have a PDF extension, it's not safe.
  2385. if ( ! str_ends_with( $url, '.pdf' ) ) {
  2386. return false;
  2387. }
  2388. // If the URL host matches the current site's media URL, it's safe.
  2389. $upload_info = wp_upload_dir( null, false );
  2390. $parsed_url = wp_parse_url( $upload_info['url'] );
  2391. $upload_host = isset( $parsed_url['host'] ) ? $parsed_url['host'] : '';
  2392. $upload_port = isset( $parsed_url['port'] ) ? ':' . $parsed_url['port'] : '';
  2393. if ( str_starts_with( $url, "http://$upload_host$upload_port/" )
  2394. || str_starts_with( $url, "https://$upload_host$upload_port/" )
  2395. ) {
  2396. return true;
  2397. }
  2398. return false;
  2399. }