1
|
|
|
<?php |
2
|
|
|
/* Copyright (C) NAVER <http://www.navercorp.com> */ |
3
|
|
|
|
4
|
|
|
class Purifier |
5
|
|
|
{ |
6
|
|
|
|
7
|
|
|
private $_cacheDir; |
8
|
|
|
private $_htmlPurifier; |
9
|
|
|
private $_config; |
10
|
|
|
private $_def; |
11
|
|
|
|
12
|
|
|
public function Purifier() |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
$this->_checkCacheDir(); |
15
|
|
|
|
16
|
|
|
// purifier setting |
17
|
|
|
require_once _XE_PATH_ . 'classes/security/htmlpurifier/library/HTMLPurifier.auto.php'; |
18
|
|
|
require_once 'HTMLPurifier.func.php'; |
19
|
|
|
|
20
|
|
|
$this->_setConfig(); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function getInstance() |
24
|
|
|
{ |
25
|
|
|
if(!isset($GLOBALS['__PURIFIER_INSTANCE__'])) |
26
|
|
|
{ |
27
|
|
|
$GLOBALS['__PURIFIER_INSTANCE__'] = new Purifier(); |
28
|
|
|
} |
29
|
|
|
return $GLOBALS['__PURIFIER_INSTANCE__']; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
private function _setConfig() |
33
|
|
|
{ |
34
|
|
|
$whiteDomainRegex = $this->_getWhiteDomainRegx(); |
35
|
|
|
//$allowdClasses = array('emoticon'); |
36
|
|
|
|
37
|
|
|
$this->_config = HTMLPurifier_Config::createDefault(); |
38
|
|
|
$this->_config->set('HTML.TidyLevel', 'light'); |
39
|
|
|
$this->_config->set('Output.FlashCompat', TRUE); |
40
|
|
|
$this->_config->set('HTML.SafeObject', TRUE); |
41
|
|
|
$this->_config->set('HTML.SafeEmbed', TRUE); |
42
|
|
|
$this->_config->set('HTML.SafeIframe', TRUE); |
43
|
|
|
$this->_config->set('URI.SafeIframeRegexp', $whiteDomainRegex); |
44
|
|
|
$this->_config->set('Cache.SerializerPath', $this->_cacheDir); |
45
|
|
|
$this->_config->set('Attr.AllowedFrameTargets', array('_blank')); |
46
|
|
|
//$this->_config->set('Attr.AllowedClasses', $allowdClasses); |
47
|
|
|
|
48
|
|
|
$this->_def = $this->_config->getHTMLDefinition(TRUE); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
private function _setDefinition(&$content) |
52
|
|
|
{ |
53
|
|
|
// add attribute for edit component |
54
|
|
|
$editComponentAttrs = $this->_searchEditComponent($content); |
55
|
|
|
if(is_array($editComponentAttrs)) |
56
|
|
|
{ |
57
|
|
|
foreach($editComponentAttrs AS $k => $v) |
58
|
|
|
{ |
59
|
|
|
$this->_def->addAttribute('img', $v, 'CDATA'); |
60
|
|
|
$this->_def->addAttribute('div', $v, 'CDATA'); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// add attribute for widget component |
65
|
|
|
$widgetAttrs = $this->_searchWidget($content); |
66
|
|
|
if(is_array($widgetAttrs)) |
67
|
|
|
{ |
68
|
|
|
foreach($widgetAttrs AS $k => $v) |
69
|
|
|
{ |
70
|
|
|
$this->_def->addAttribute('img', $v, 'CDATA'); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Search attribute of edit component tag |
77
|
|
|
* @param string $content |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
private function _searchEditComponent($content) |
81
|
|
|
{ |
82
|
|
|
preg_match_all('!<(?:(div)|img)([^>]*)editor_component=([^>]*)>(?(1)(.*?)</div>)!is', $content, $m); |
83
|
|
|
|
84
|
|
|
$attributeList = array(); |
85
|
|
|
if(is_array($m[2])) |
86
|
|
|
{ |
87
|
|
|
foreach($m[2] as $key => $value) |
88
|
|
|
{ |
89
|
|
|
unset($script, $m2); |
90
|
|
|
$script = " {$m[2][$key]} editor_component={$m[3][$key]}"; |
91
|
|
|
|
92
|
|
View Code Duplication |
if(preg_match_all('/([a-z0-9_-]+)="([^"]+)"/is', $script, $m2)) |
93
|
|
|
{ |
94
|
|
|
foreach($m2[1] as $value2) |
95
|
|
|
{ |
96
|
|
|
//SECISSUE check style attr |
97
|
|
|
if($value2 == 'style') |
98
|
|
|
{ |
99
|
|
|
continue; |
100
|
|
|
} |
101
|
|
|
$attributeList[] = $value2; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return array_unique($attributeList); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Search edit component tag |
112
|
|
|
* @param string $content |
113
|
|
|
* @return array |
114
|
|
|
*/ |
115
|
|
|
private function _searchWidget(&$content) |
116
|
|
|
{ |
117
|
|
|
preg_match_all('!<(?:(div)|img)([^>]*)class="zbxe_widget_output"([^>]*)>(?(1)(.*?)</div>)!is', $content, $m); |
118
|
|
|
|
119
|
|
|
$attributeList = array(); |
120
|
|
|
if(is_array($m[3])) |
121
|
|
|
{ |
122
|
|
|
$content = str_replace('<img class="zbxe_widget_output"', '<img src="" class="zbxe_widget_output"', $content); |
123
|
|
|
|
124
|
|
|
foreach($m[3] as $key => $value) |
125
|
|
|
{ |
126
|
|
View Code Duplication |
if (preg_match_all('/([a-z0-9_-]+)="([^"]+)"/is', $m[3][$key], $m2)) |
127
|
|
|
{ |
128
|
|
|
foreach($m2[1] as $value2) |
129
|
|
|
{ |
130
|
|
|
//SECISSUE check style attr |
131
|
|
|
if($value2 == 'style') |
132
|
|
|
{ |
133
|
|
|
continue; |
134
|
|
|
} |
135
|
|
|
$attributeList[] = $value2; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
return array_unique($attributeList); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
private function _getWhiteDomainRegx() |
144
|
|
|
{ |
145
|
|
|
$oEmbedFilter = EmbedFilter::getInstance(); |
146
|
|
|
$whiteIframeUrlList = $oEmbedFilter->getWhiteIframeUrlList(); |
147
|
|
|
|
148
|
|
|
$whiteDomain = array(); |
149
|
|
|
foreach($whiteIframeUrlList as $value) |
150
|
|
|
{ |
151
|
|
|
$whiteDomain[] = preg_quote($value, '%'); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$whiteDomainRegex = '%^(' . implode('|', $whiteDomain) . ')%'; |
155
|
|
|
|
156
|
|
|
return $whiteDomainRegex; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
private function _checkCacheDir() |
160
|
|
|
{ |
161
|
|
|
// check htmlpurifier cache directory |
162
|
|
|
$this->_cacheDir = _XE_PATH_ . 'files/cache/htmlpurifier'; |
163
|
|
|
FileHandler::makeDir($this->_cacheDir); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function purify(&$content) |
167
|
|
|
{ |
168
|
|
|
$this->_setDefinition($content); |
169
|
|
|
$this->_htmlPurifier = new HTMLPurifier($this->_config); |
170
|
|
|
|
171
|
|
|
$content = $this->_htmlPurifier->purify($content); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
} |
175
|
|
|
/* End of file : Purifier.class.php */ |
176
|
|
|
/* Location: ./classes/security/Purifier.class.php */ |
177
|
|
|
|