Skip to content

Commit 125c151

Browse files
committed
Add HTML purifier filtering and filter traversal
1 parent e07b6e2 commit 125c151

File tree

5 files changed

+89
-14
lines changed

5 files changed

+89
-14
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"require" : {
1616
"php" : ">=7.0.0",
1717
"ext-mbstring" : "*",
18-
"technicalguru/i18n" : "~1"
18+
"technicalguru/i18n" : "~1",
19+
"ezyang/htmlpurifier":"^4.13"
1920
},
2021
"autoload" : {
2122
"psr-4" : {

src/TgUtils/AbstractStringFilter.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace TgUtils;
4+
5+
/**
6+
* Abstract string filter that traverses objects and arrays.
7+
*/
8+
abstract class AbstractStringFilter implements StringFilter {
9+
10+
public function __construct() {
11+
}
12+
13+
/**
14+
* Filters the given string and returns sanitized value.
15+
* @param string $s - string to sanitize (can be null)
16+
* @return the sanitized string.
17+
*/
18+
public function filter($s) {
19+
if ($s == NULL) return $s;
20+
if (is_string($s)) {
21+
return $this->filterString($s);
22+
} else if (is_array($s)) {
23+
foreach ($s AS $key => $value) {
24+
$s[$key] = $this->filter($value);
25+
}
26+
} else if (is_object($s)) {
27+
foreach (get_object_vars($s) AS $name => $value) {
28+
$s->$name = $this->filter($value);
29+
}
30+
}
31+
return $s;
32+
}
33+
34+
protected function filterString($s) {
35+
return $s;
36+
}
37+
}
38+

src/TgUtils/NoHtmlStringFilter.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,17 @@
55
/**
66
* An interface for filter strings from any HTML tags.
77
*/
8-
class NoHtmlStringFilter implements StringFilter {
8+
class NoHtmlStringFilter extends AbstractStringFilter {
99

1010
public static $INSTANCE;
1111

1212
public function __construct() {
13+
parent::__construct();
1314
}
1415

15-
/**
16-
* Filters the given string and returns sanitized value.
17-
* @param string $s - string to sanitize (can be null)
18-
* @return the sanitized string.
19-
*/
20-
public function filter($s) {
21-
if ($s == NULL) return $s;
16+
protected function filterString($s) {
2217
return strip_tags($s);
2318
}
24-
2519
}
2620
NoHtmlStringFilter::$INSTANCE = new NoHtmlStringFilter();
2721

src/TgUtils/PurifierStringFilter.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+

src/TgUtils/StringFilters.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
<?php
22

3-
package TgUtils;
3+
namespace TgUtils;
44

55
/**
66
* Provides default string filters.
77
*/
8-
public class StringFilters {
8+
class StringFilters {
99

10-
public static $DUMMY = DummyStringFilter::$INSTANCE;
11-
public static $NO_HTML = NoHtmlStringFilter::$INSTANCE;
10+
public static $DUMMY;
11+
public static $NO_HTML;
12+
public static $TEXTBOX;
1213

1314
}
15+
StringFilters::$DUMMY = DummyStringFilter::$INSTANCE;
16+
StringFilters::$NO_HTML = NoHtmlStringFilter::$INSTANCE;
17+
StringFilters::$TEXTBOX = PurifierStringFilter::$INSTANCE;
1418

0 commit comments

Comments
 (0)