1 | <?php |
||
23 | |||
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 | public function __construct(Filesystem $files, Repository $config) |
||
50 | { |
||
51 | $this->files = $files; |
||
52 | $this->config = $config; |
||
53 | |||
54 | $this->setUp(); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Setup |
||
59 | * |
||
60 | * @throws Exception |
||
61 | */ |
||
62 | private function setUp() |
||
63 | { |
||
64 | if (!$this->config->has('purifier')) { |
||
65 | throw new Exception('Configuration parameters not loaded!'); |
||
66 | } |
||
67 | |||
68 | $this->checkCacheDirectory(); |
||
69 | |||
70 | // Create a new configuration object |
||
71 | $config = $this->getConfig(); |
||
72 | |||
73 | // Create HTMLPurifier object |
||
74 | $this->purifier = new HTMLPurifier($config); |
||
75 | } |
||
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 | private function addCustomDefinition(array $definitionConfig, HTMLPurifier_Config $configObject = null) |
||
87 | { |
||
88 | if (!$configObject) { |
||
89 | $configObject = HTMLPurifier_Config::createDefault(); |
||
90 | $configObject->loadArray($this->getConfig()); |
||
|
|||
91 | } |
||
92 | |||
93 | // Setup the custom definition |
||
94 | $configObject->set('HTML.DefinitionID', $definitionConfig['id']); |
||
95 | $configObject->set('HTML.DefinitionRev', $definitionConfig['rev']); |
||
96 | |||
97 | // Enable debug mode |
||
98 | if (!isset($definitionConfig['debug']) || $definitionConfig['debug']) { |
||
99 | $configObject->set('Cache.DefinitionImpl', null); |
||
100 | } |
||
101 | |||
102 | // Start configuring the definition |
||
103 | if ($def = $configObject->maybeGetRawHTMLDefinition()) { |
||
104 | // Create the definition attributes |
||
105 | if (!empty($definitionConfig['attributes'])) { |
||
106 | $this->addCustomAttributes($definitionConfig['attributes'], $def); |
||
107 | } |
||
108 | |||
109 | // Create the definition elements |
||
110 | if (!empty($definitionConfig['elements'])) { |
||
111 | $this->addCustomElements($definitionConfig['elements'], $def); |
||
112 | } |
||
113 | } |
||
114 | |||
115 | 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 | private function addCustomAttributes(array $attributes, HTMLPurifier_HTMLDefinition $definition) |
||
127 | { |
||
128 | foreach ($attributes as $attribute) { |
||
129 | // Get configuration of attribute |
||
130 | $required = !empty($attribute[3]) ? true : false; |
||
131 | $onElement = $attribute[0]; |
||
132 | $attrName = $required ? $attribute[1] . '*' : $attribute[1]; |
||
133 | $validValues = $attribute[2]; |
||
134 | |||
135 | if ($onElement === '*') { |
||
136 | $def = $validValues; |
||
137 | if (is_string($validValues)) { |
||
138 | $def = new $validValues(); |
||
139 | } |
||
140 | |||
141 | if ($def instanceof \HTMLPurifier_AttrDef) { |
||
142 | $definition->info_global_attr[$attrName] = $def; |
||
143 | } |
||
144 | |||
145 | continue; |
||
146 | } |
||
147 | |||
148 | if (class_exists($validValues)) { |
||
149 | $validValues = new $validValues(); |
||
150 | } |
||
151 | |||
152 | $definition->addAttribute($onElement, $attrName, $validValues); |
||
153 | } |
||
154 | |||
155 | return $definition; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Add provided elements to the provided definition |
||
160 | * |
||
161 | * @param array $elements |
||
162 | * @param HTMLPurifier_HTMLDefinition $definition |
||
163 | * |
||
164 | * @return HTMLPurifier_HTMLDefinition $definition |
||
165 | */ |
||
166 | private function addCustomElements(array $elements, HTMLPurifier_HTMLDefinition $definition) |
||
180 | } |
||
181 | } |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Check/Create cache directory |
||
186 | */ |
||
187 | private function checkCacheDirectory() |
||
194 | } |
||
195 | } |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * @param null $config |
||
200 | * |
||
201 | * @return mixed|null |
||
202 | */ |
||
203 | protected function getConfig($config = null) |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * @param $dirty |
||
259 | * @param null $config |
||
260 | * @param \Closure|null $postCreateConfigHook |
||
261 | * @return mixed |
||
262 | */ |
||
263 | public function clean($dirty, $config = null, \Closure $postCreateConfigHook = null) |
||
264 | { |
||
265 | if (is_array($dirty)) { |
||
266 | return array_map(function ($item) use ($config) { |
||
267 | return $this->clean($item, $config); |
||
268 | }, $dirty); |
||
269 | } |
||
270 | |||
300 |