1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mews\Purifier; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Laravel 5 HTMLPurifier package |
7
|
|
|
* |
8
|
|
|
* @copyright Copyright (c) 2015 MeWebStudio |
9
|
|
|
* @version 2.0.0 |
10
|
|
|
* @author Muharrem ERİN |
11
|
|
|
* @contact [email protected] |
12
|
|
|
* @web http://www.mewebstudio.com |
13
|
|
|
* @date 2014-04-02 |
14
|
|
|
* @license MIT |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
use Exception; |
18
|
|
|
use HTMLPurifier; |
19
|
|
|
use HTMLPurifier_Config; |
20
|
|
|
use Illuminate\Config\Repository; |
21
|
|
|
use Illuminate\Filesystem\Filesystem; |
22
|
|
|
|
23
|
|
|
class Purifier |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Filesystem |
28
|
|
|
*/ |
29
|
|
|
protected $files; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var Repository |
33
|
|
|
*/ |
34
|
|
|
protected $config; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var HTMLPurifier |
38
|
|
|
*/ |
39
|
|
|
protected $purifier; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Constructor |
43
|
|
|
* |
44
|
|
|
* @param Filesystem $files |
45
|
|
|
* @param Repository $config |
46
|
|
|
* @throws Exception |
47
|
|
|
*/ |
48
|
24 |
|
public function __construct(Filesystem $files, Repository $config) |
49
|
|
|
{ |
50
|
24 |
|
$this->files = $files; |
51
|
24 |
|
$this->config = $config; |
52
|
|
|
|
53
|
24 |
|
$this->setUp(); |
54
|
18 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Setup |
58
|
|
|
* |
59
|
|
|
* @throws Exception |
60
|
|
|
*/ |
61
|
24 |
|
private function setUp() |
62
|
|
|
{ |
63
|
24 |
|
if (!$this->config->has('purifier')) { |
64
|
6 |
|
throw new Exception('Configuration parameters not loaded!'); |
65
|
|
|
} |
66
|
|
|
|
67
|
18 |
|
$this->checkCacheDirectory(); |
68
|
|
|
|
69
|
|
|
// Create a new configuration object |
70
|
18 |
|
$config = HTMLPurifier_Config::createDefault(); |
71
|
|
|
|
72
|
|
|
// Allow configuration to be modified |
73
|
18 |
|
if (!$this->config->get('purifier.finalize')) { |
74
|
|
|
$config->autoFinalize = false; |
75
|
|
|
} |
76
|
|
|
|
77
|
18 |
|
$config->loadArray($this->getConfig()); |
78
|
|
|
|
79
|
|
|
// Load custom definition if set |
80
|
18 |
|
if ($definitionConfig = $this->config->get('purifier.settings.custom_definition')) { |
81
|
18 |
|
$this->addCustomDefinition($definitionConfig, $config); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// Load custom elements if set |
85
|
|
|
if ($elements = $this->config->get('purifier.settings.custom_elements')) { |
86
|
18 |
|
if ($def = $config->maybeGetRawHTMLDefinition()) { |
87
|
|
|
$this->addCustomElements($elements, $def); |
|
|
|
|
88
|
18 |
|
} |
89
|
|
|
} |
90
|
18 |
|
|
91
|
18 |
|
// Load custom attributes if set |
92
|
3 |
|
if ($attributes = $this->config->get('purifier.settings.custom_attributes')) { |
93
|
3 |
|
if ($def = $config->maybeGetRawHTMLDefinition()) { |
94
|
18 |
|
$this->addCustomAttributes($attributes, $def); |
|
|
|
|
95
|
18 |
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
// Create HTMLPurifier object |
99
|
|
|
$this->purifier = new HTMLPurifier($this->configure($config)); |
100
|
|
|
} |
101
|
|
|
|
102
|
18 |
|
/** |
103
|
|
|
* Add a custom definition |
104
|
18 |
|
* |
105
|
|
|
* @see http://htmlpurifier.org/docs/enduser-customize.html |
106
|
|
|
* @param array $definitionConfig |
107
|
|
|
* @param HTML_Purifier_Config $configObject Defaults to using default config |
108
|
|
|
* |
109
|
|
|
* @return HTML_Purifier_Config $configObject |
110
|
|
|
*/ |
111
|
|
|
private function addCustomDefinition(array $definitionConfig, $configObject = null) |
112
|
18 |
|
{ |
113
|
|
|
if (!$configObject) { |
114
|
18 |
|
$configObject = HTMLPurifier_Config::createDefault(); |
115
|
18 |
|
$configObject->loadArray($this->getConfig()); |
116
|
18 |
|
} |
117
|
18 |
|
|
118
|
|
|
// Setup the custom definition |
119
|
18 |
|
$configObject->set('HTML.DefinitionID', $definitionConfig['id']); |
120
|
18 |
|
$configObject->set('HTML.DefinitionRev', $definitionConfig['rev']); |
121
|
18 |
|
|
122
|
3 |
|
// Enable debug mode |
123
|
3 |
|
if (!isset($definitionConfig['debug']) || $definitionConfig['debug']) { |
124
|
|
|
$configObject->set('Cache.DefinitionImpl', null); |
125
|
18 |
|
} |
126
|
3 |
|
|
127
|
3 |
|
// Start configuring the definition |
128
|
|
|
if ($def = $configObject->maybeGetRawHTMLDefinition()) { |
129
|
18 |
|
// Create the definition attributes |
130
|
|
|
if (!empty($definitionConfig['attributes'])) { |
131
|
18 |
|
$this->addCustomAttributes($definitionConfig['attributes'], $def); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
// Create the definition elements |
135
|
|
|
if (!empty($definitionConfig['elements'])) { |
136
|
|
|
$this->addCustomElements($definitionConfig['elements'], $def); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
3 |
|
return $configObject; |
141
|
|
|
} |
142
|
3 |
|
|
143
|
3 |
|
/** |
144
|
3 |
|
* Add provided attributes to the provided definition |
145
|
3 |
|
* |
146
|
|
|
* @param array $attributes |
147
|
|
|
* @param HTMLPurifier_HTMLDefinition $definition |
148
|
3 |
|
* |
149
|
|
|
* @return HTMLPurifier_HTMLDefinition $definition |
150
|
|
|
*/ |
151
|
|
|
private function addCustomAttributes(array $attributes, $definition) |
152
|
|
|
{ |
153
|
|
|
foreach ($attributes as $attribute) { |
154
|
|
|
// Get configuration of attribute |
155
|
|
|
$required = !empty($attribute[3]) ? true : false; |
156
|
6 |
|
$onElement = $attribute[0]; |
157
|
|
|
$attrName = $required ? $attribute[1] . '*' : $attribute[1]; |
158
|
6 |
|
$validValues = $attribute[2]; |
159
|
|
|
|
160
|
|
|
$definition->addAttribute($onElement, $attrName, $validValues); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return $definition; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Add provided elements to the provided definition |
168
|
|
|
* |
169
|
|
|
* @param array $elements |
170
|
|
|
* @param HTMLPurifier_HTMLDefinition $definition |
171
|
|
|
* |
172
|
|
|
* @return HTMLPurifier_HTMLDefinition $definition |
173
|
|
|
*/ |
174
|
|
|
private function addCustomElements(array $elements, $definition) |
175
|
|
|
{ |
176
|
|
|
foreach ($elements as $element) { |
177
|
|
|
// Get configuration of element |
178
|
|
|
$name = $element[0]; |
179
|
|
|
$contentSet = $element[1]; |
180
|
|
|
$allowedChildren = $element[2]; |
181
|
|
|
$attributeCollection = $element[3]; |
182
|
|
|
$attributes = isset($element[4]) ? $element[4] : null; |
183
|
|
|
|
184
|
|
|
if (!empty($attributes)) { |
185
|
|
|
$definition->addElement($name, $contentSet, $allowedChildren, $attributeCollection, $attributes); |
186
|
|
|
} else { |
187
|
|
|
$definition->addElement($name, $contentSet, $allowedChildren, $attributeCollection); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Check/Create cache directory |
194
|
|
|
*/ |
195
|
|
|
private function checkCacheDirectory() |
196
|
|
|
{ |
197
|
|
|
$cachePath = $this->config->get('purifier.cachePath'); |
198
|
|
|
|
199
|
|
|
if ($cachePath) { |
200
|
|
|
if (!$this->files->isDirectory($cachePath)) { |
201
|
|
|
$this->files->makeDirectory($cachePath, $this->config->get('purifier.cacheFileMode', 0755)); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param HTMLPurifier_Config $config |
208
|
|
|
* |
209
|
|
|
* @return HTMLPurifier_Config |
210
|
|
|
*/ |
211
|
|
|
protected function configure(HTMLPurifier_Config $config) |
212
|
|
|
{ |
213
|
|
|
return HTMLPurifier_Config::inherit($config); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @param null $config |
218
|
|
|
* |
219
|
|
|
* @return mixed|null |
220
|
|
|
*/ |
221
|
|
|
protected function getConfig($config = null) |
222
|
|
|
{ |
223
|
|
|
$default_config = []; |
224
|
|
|
$default_config['Core.Encoding'] = $this->config->get('purifier.encoding'); |
225
|
|
|
$default_config['Cache.SerializerPath'] = $this->config->get('purifier.cachePath'); |
226
|
|
|
$default_config['Cache.SerializerPermissions'] = $this->config->get('purifier.cacheFileMode', 0755); |
227
|
|
|
|
228
|
|
|
if (!$config) { |
229
|
|
|
$config = $this->config->get('purifier.settings.default'); |
230
|
|
|
} elseif (is_string($config)) { |
231
|
|
|
$config = $this->config->get('purifier.settings.'.$config); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
if (!is_array($config)) { |
235
|
|
|
$config = []; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
$config = $default_config + $config; |
239
|
|
|
|
240
|
|
|
return $config; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @param $dirty |
245
|
|
|
* @param null $config |
246
|
|
|
* |
247
|
|
|
* @return mixed |
248
|
|
|
*/ |
249
|
|
|
public function clean($dirty, $config = null) |
250
|
|
|
{ |
251
|
|
|
if (is_array($dirty)) { |
252
|
|
|
return array_map(function ($item) use ($config) { |
253
|
|
|
return $this->clean($item, $config); |
254
|
|
|
}, $dirty); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
return $this->purifier->purify($dirty, $this->getConfig($config)); |
|
|
|
|
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Get HTMLPurifier instance. |
262
|
|
|
* |
263
|
|
|
* @return \HTMLPurifier |
264
|
|
|
*/ |
265
|
|
|
public function getInstance() |
266
|
|
|
{ |
267
|
|
|
return $this->purifier; |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: