|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace TgUtils; |
| 4 | + |
| 5 | +class PurifierStringFilter extends AbstractStringFilter { |
| 6 | + |
| 7 | + public static $INSTANCE; |
| 8 | + |
| 9 | + protected $purifier; |
| 10 | + |
| 11 | + public function __construct() { |
| 12 | + parent::__construct(); |
| 13 | + $config = $this->getConfig(); |
| 14 | + $this->purifier = new \HTMLPurifier($config); |
| 15 | + } |
| 16 | + |
| 17 | + public function filterString($s) { |
| 18 | + return $this->purifier->purify($s); |
| 19 | + } |
| 20 | + |
| 21 | + protected function getConfig() { |
| 22 | + $config = \HTMLPurifier_Config::createDefault(); |
| 23 | + $config->set('HTML.DefinitionID', 'simple'); |
| 24 | + $config->set('HTML.DefinitionRev', 1); |
| 25 | + $config->set('HTML.AllowedElements', array('br', 'p', 'div', 'li', 'ol', 'ul', 'i', 'b', 'strong', 'a', 'h4', 'h5','table','tr','td','th')); |
| 26 | + $config->set('HTML.AllowedAttributes', array( |
| 27 | + 'a.href', 'a.class', 'a.style', |
| 28 | + 'p.style', 'div.style', |
| 29 | + 'li.style', 'ol.style', 'ul.style', |
| 30 | + 'i.style', 'b.style', 'strong.style', |
| 31 | + 'h4.style', 'h5.style', |
| 32 | + 'table.style','table.class','tr.style','td.colspan','td.rowspan','td.style','th.colspan','th.rowspan','th.style','tr.class','td.class', |
| 33 | + )); |
| 34 | + return $config; |
| 35 | + } |
| 36 | +} |
| 37 | +PurifierStringFilter::$INSTANCE = new PurifierStringFilter(); |
| 38 | + |
0 commit comments