Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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