1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EZAMA; |
4
|
|
|
|
5
|
|
|
class HtmlStrip extends HtmlStripHelper |
6
|
|
|
{ |
7
|
|
|
protected $type; |
8
|
|
|
|
9
|
|
|
public function __construct($html, $type = 'replace', $notAllowedTags = ['', false], $notAllowedAttributes = ['', false]) |
10
|
|
|
{ |
11
|
|
|
$callback = function ($v) { |
12
|
|
|
return trim(strtolower($v)); |
13
|
|
|
}; |
14
|
|
|
if (!$this->handleTags($notAllowedTags, $callback, function ($v) { |
15
|
|
|
return isset(self::$tags[$v]); |
16
|
|
|
})) { |
17
|
|
|
$this->allowedTags = self::$tags; |
18
|
|
|
} |
19
|
|
|
if (!$this->handleAttributes($notAllowedAttributes, $callback, function ($v) { |
20
|
|
|
return isset(self::$attributes[$v]); |
21
|
|
|
})) { |
22
|
|
|
$this->allowedAttributes = self::$attributes; |
23
|
|
|
} |
24
|
|
|
$this->type = !is_string($type) || !in_array($tmp = strtolower(trim($type)), ['remove', 'replace']) ? 'remove' : $tmp; |
25
|
|
|
|
26
|
|
|
$bs = new BeforeStrip($html); |
27
|
|
|
$this->is_html = $bs->isHtml(); |
28
|
|
|
$this->is_php = $bs->isPHP(); |
29
|
|
|
$bs = new PrepareStrip($bs); |
30
|
|
|
$this->loadHTML($bs->getPrepared()); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function go($type = self::TAGS) |
34
|
|
|
{ |
35
|
|
|
switch ($type) { |
36
|
|
|
case self::TAGS_AND_ATTRIBUTES: |
37
|
|
|
return self::strip($this->html, $this->type, $this->allowedTags, $this->allowedAttributes, $this->is_php); |
38
|
|
|
break; |
|
|
|
|
39
|
|
|
case self::TAGS_WITH_ATTRIBUTES: |
40
|
|
|
return self::stripTagsWithAttributes($this->html, $this->allowedTags, $this->allowedAttributes, $this->is_php); |
41
|
|
|
break; |
42
|
|
|
case self::TAGS: |
43
|
|
|
return self::strip($this->html, $this->type, $this->allowedTags, self::$attributes, $this->is_php); |
44
|
|
|
break; |
45
|
|
|
case self::ATTRIBUTES: |
46
|
|
|
return self::strip($this->html, $this->type, self::$tags, $this->allowedAttributes, $this->is_php); |
47
|
|
|
break; |
48
|
|
|
default: |
49
|
|
|
throw new invalidArgumentException('unexpected strip type'); |
|
|
|
|
50
|
|
|
break; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected static function strip($domDoc, $type, $allowed_tags, $allowed_attrs, $is_php) |
55
|
|
|
{ |
56
|
|
|
$firstDiv = 0; |
57
|
|
|
if (count(self::$tags) > count($allowed_tags) || count(self::$attributes) > count($allowed_attrs)) { |
58
|
|
|
foreach (new DOMNodeRecursiveIterator($domDoc->getElementsByTagName('*'))as $tag) { |
59
|
|
|
if (!isset($allowed_tags['<' . $tag->tagName . '>'])) { |
60
|
|
|
if (self::skipUsefull($tag, $firstDiv)) { |
61
|
|
|
continue; |
62
|
|
|
} |
63
|
|
|
self::replaceOrRemove($tag, $type); |
64
|
|
|
} else { |
65
|
|
|
self::stripAttributes($tag, $allowed_attrs); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
self::handleComments($domDoc, $allowed_tags); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return self::handlePhp($is_php, $domDoc, $allowed_tags); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected static function skipUsefull($tag, &$firstDiv) |
76
|
|
|
{ |
77
|
|
|
if ($tag->tagName === 'div' && !$firstDiv) { |
78
|
|
|
$firstDiv++; |
79
|
|
|
|
80
|
|
|
return true; |
81
|
|
|
} |
82
|
|
|
if ($tag->tagName === 'doctypetag') { |
83
|
|
|
return true; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private static function replaceOrRemove($tag, $type) |
88
|
|
|
{ |
89
|
|
|
if ($type === 'remove') { |
90
|
|
|
$tag->parentNode->removeChild($tag); |
91
|
|
|
} else { |
92
|
|
|
if (!in_array(strtolower($tag->tagName), ['php', 'style', 'script'])) { |
93
|
|
|
$elem = $tag->parentNode; |
94
|
|
|
$childNodes = $tag->childNodes; |
95
|
|
|
while ($childNodes->length > 0) { |
96
|
|
|
$elem->insertBefore($childNodes->item(0), $tag); |
97
|
|
|
} |
98
|
|
|
$elem->removeChild($tag); |
99
|
|
|
} else { |
100
|
|
|
$tag->parentNode->removeChild($tag); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
protected static function stripTagsWithAttributes($domDoc, &$allowed_tags, &$allowed_attrs, $is_php) |
106
|
|
|
{ |
107
|
|
|
if (count(self::$attributes) > count($allowed_attrs)) { |
108
|
|
|
foreach (new DOMNodeRecursiveIterator($domDoc->getElementsByTagName('*'))as $tag) { |
109
|
|
|
self::stripAttributes($tag, $allowed_attrs, 2); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return self::handlePhp($is_php, $domDoc, $allowed_tags); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public static function getEventsAttributes() |
117
|
|
|
{ |
118
|
|
|
return array_keys(self::$events_attributes); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public static function getAttributes() |
122
|
|
|
{ |
123
|
|
|
return array_keys(self::$attributes); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public static function getTags() |
127
|
|
|
{ |
128
|
|
|
return array_keys(self::$tags); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
The
break
statement is not necessary if it is preceded for example by areturn
statement:If you would like to keep this construct to be consistent with other
case
statements, you can safely mark this issue as a false-positive.