1 | <?php |
||
24 | class Purifier |
||
25 | { |
||
26 | |||
27 | /** |
||
28 | * @var Filesystem |
||
29 | */ |
||
30 | protected $files; |
||
31 | |||
32 | /** |
||
33 | * @var Repository |
||
34 | */ |
||
35 | protected $config; |
||
36 | |||
37 | /** |
||
38 | * @var HTMLPurifier |
||
39 | */ |
||
40 | protected $purifier; |
||
41 | |||
42 | /** |
||
43 | * Constructor |
||
44 | * |
||
45 | * @param Filesystem $files |
||
46 | * @param Repository $config |
||
47 | * @throws Exception |
||
48 | */ |
||
49 | 27 | public function __construct(Filesystem $files, Repository $config) |
|
56 | |||
57 | /** |
||
58 | * Setup |
||
59 | * |
||
60 | * @throws Exception |
||
61 | */ |
||
62 | 27 | private function setUp() |
|
76 | |||
77 | /** |
||
78 | * Add a custom definition |
||
79 | * |
||
80 | * @see http://htmlpurifier.org/docs/enduser-customize.html |
||
81 | * @param array $definitionConfig |
||
82 | * @param HTMLPurifier_Config $configObject Defaults to using default config |
||
83 | * |
||
84 | * @return HTMLPurifier_Config $configObject |
||
85 | */ |
||
86 | 24 | private function addCustomDefinition(array $definitionConfig, HTMLPurifier_Config $configObject = null) |
|
87 | { |
||
88 | 24 | if (!$configObject) { |
|
89 | $configObject = HTMLPurifier_Config::createDefault(); |
||
90 | $configObject->loadArray($this->getConfig()); |
||
|
|||
91 | } |
||
92 | |||
93 | // Setup the custom definition |
||
94 | 24 | $configObject->set('HTML.DefinitionID', $definitionConfig['id']); |
|
95 | 24 | $configObject->set('HTML.DefinitionRev', $definitionConfig['rev']); |
|
96 | |||
97 | // Enable debug mode |
||
98 | 24 | if (!isset($definitionConfig['debug']) || $definitionConfig['debug']) { |
|
99 | $configObject->set('Cache.DefinitionImpl', null); |
||
100 | } |
||
101 | |||
102 | // Start configuring the definition |
||
103 | 24 | if ($def = $configObject->maybeGetRawHTMLDefinition()) { |
|
104 | // Create the definition attributes |
||
105 | 21 | if (!empty($definitionConfig['attributes'])) { |
|
106 | 21 | $this->addCustomAttributes($definitionConfig['attributes'], $def); |
|
107 | } |
||
108 | |||
109 | // Create the definition elements |
||
110 | 21 | if (!empty($definitionConfig['elements'])) { |
|
111 | 21 | $this->addCustomElements($definitionConfig['elements'], $def); |
|
112 | } |
||
113 | } |
||
114 | |||
115 | 24 | return $configObject; |
|
116 | } |
||
117 | |||
118 | /** |
||
119 | * Add provided attributes to the provided definition |
||
120 | * |
||
121 | * @param array $attributes |
||
122 | * @param HTMLPurifier_HTMLDefinition $definition |
||
123 | * |
||
124 | * @return HTMLPurifier_HTMLDefinition $definition |
||
125 | */ |
||
126 | 21 | private function addCustomAttributes(array $attributes, HTMLPurifier_HTMLDefinition $definition) |
|
127 | { |
||
128 | 21 | foreach ($attributes as $attribute) { |
|
129 | // Get configuration of attribute |
||
130 | 21 | $required = !empty($attribute[3]) ? true : false; |
|
131 | 21 | $onElement = $attribute[0]; |
|
132 | 21 | $attrName = $required ? $attribute[1] . '*' : $attribute[1]; |
|
133 | 21 | $validValues = $attribute[2]; |
|
134 | |||
135 | 21 | $definition->addAttribute($onElement, $attrName, $validValues); |
|
136 | } |
||
137 | |||
138 | 21 | return $definition; |
|
139 | } |
||
140 | |||
141 | /** |
||
142 | * Add provided elements to the provided definition |
||
143 | * |
||
144 | * @param array $elements |
||
145 | * @param HTMLPurifier_HTMLDefinition $definition |
||
146 | * |
||
147 | * @return HTMLPurifier_HTMLDefinition $definition |
||
148 | */ |
||
149 | 21 | private function addCustomElements(array $elements, HTMLPurifier_HTMLDefinition $definition) |
|
150 | { |
||
151 | 21 | foreach ($elements as $element) { |
|
152 | // Get configuration of element |
||
153 | 21 | $name = $element[0]; |
|
154 | 21 | $contentSet = $element[1]; |
|
155 | 21 | $allowedChildren = $element[2]; |
|
156 | 21 | $attributeCollection = $element[3]; |
|
157 | 21 | $attributes = isset($element[4]) ? $element[4] : null; |
|
158 | |||
159 | 21 | if (!empty($attributes)) { |
|
160 | 21 | $definition->addElement($name, $contentSet, $allowedChildren, $attributeCollection, $attributes); |
|
161 | } else { |
||
162 | 21 | $definition->addElement($name, $contentSet, $allowedChildren, $attributeCollection); |
|
163 | } |
||
164 | } |
||
165 | 21 | } |
|
166 | |||
167 | /** |
||
168 | * Check/Create cache directory |
||
169 | */ |
||
170 | 24 | private function checkCacheDirectory() |
|
171 | { |
||
172 | 24 | $cachePath = $this->config->get('purifier.cachePath'); |
|
173 | |||
174 | 24 | if ($cachePath) { |
|
175 | 24 | if (!$this->files->isDirectory($cachePath)) { |
|
176 | 3 | $this->files->makeDirectory($cachePath, $this->config->get('purifier.cacheFileMode', 0755),true); |
|
177 | } |
||
178 | } |
||
179 | 24 | } |
|
180 | |||
181 | /** |
||
182 | * @param null $config |
||
183 | * |
||
184 | * @return mixed|null |
||
185 | */ |
||
186 | 24 | protected function getConfig($config = null) |
|
187 | { |
||
188 | // Create a new configuration object |
||
189 | 24 | $configObject = HTMLPurifier_Config::createDefault(); |
|
190 | |||
191 | // Allow configuration to be modified |
||
192 | 24 | if (! $this->config->get('purifier.finalize')) { |
|
193 | $configObject->autoFinalize = false; |
||
194 | } |
||
195 | |||
196 | // Set default config |
||
197 | 24 | $defaultConfig = []; |
|
198 | 24 | $defaultConfig['Core.Encoding'] = $this->config->get('purifier.encoding'); |
|
199 | 24 | $defaultConfig['Cache.SerializerPath'] = $this->config->get('purifier.cachePath'); |
|
200 | 24 | $defaultConfig['Cache.SerializerPermissions'] = $this->config->get('purifier.cacheFileMode', 0755); |
|
201 | |||
202 | 24 | if (! $config) { |
|
203 | 24 | $config = $this->config->get('purifier.settings.default'); |
|
204 | 9 | } elseif (is_string($config)) { |
|
205 | 6 | $config = $this->config->get('purifier.settings.' . $config); |
|
206 | } |
||
207 | |||
208 | 24 | if (! is_array($config)) { |
|
209 | 3 | $config = []; |
|
210 | } |
||
211 | |||
212 | // Merge configurations |
||
213 | 24 | $config = $defaultConfig + $config; |
|
214 | |||
215 | // Load to Purifier config |
||
216 | 24 | $configObject->loadArray($config); |
|
217 | |||
218 | // Load custom definition if set |
||
219 | 24 | if ($definitionConfig = $this->config->get('purifier.settings.custom_definition')) { |
|
220 | 24 | $this->addCustomDefinition($definitionConfig, $configObject); |
|
221 | } |
||
222 | |||
223 | // Load custom elements if set |
||
224 | 24 | if ($elements = $this->config->get('purifier.settings.custom_elements')) { |
|
225 | 24 | if ($def = $configObject->maybeGetRawHTMLDefinition()) { |
|
226 | 21 | $this->addCustomElements($elements, $def); |
|
227 | } |
||
228 | } |
||
229 | |||
230 | // Load custom attributes if set |
||
231 | 24 | if ($attributes = $this->config->get('purifier.settings.custom_attributes')) { |
|
232 | 24 | if ($def = $configObject->maybeGetRawHTMLDefinition()) { |
|
233 | 21 | $this->addCustomAttributes($attributes, $def); |
|
234 | } |
||
235 | } |
||
236 | |||
237 | 24 | return $configObject; |
|
238 | } |
||
239 | |||
240 | /** |
||
241 | * @param $dirty |
||
242 | * @param null $config |
||
243 | * |
||
244 | * @return mixed |
||
245 | */ |
||
246 | 9 | public function clean($dirty, $config = null) |
|
256 | |||
257 | /** |
||
258 | * Get HTMLPurifier instance. |
||
259 | * |
||
260 | * @return \HTMLPurifier |
||
261 | */ |
||
262 | 3 | public function getInstance() |
|
266 | } |
||
267 |
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: