-
Notifications
You must be signed in to change notification settings - Fork 48
/
BlockHandler.php
323 lines (280 loc) · 8.72 KB
/
BlockHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<?php
namespace EditorJS;
/**
* Class BlockHandler
*
* @package EditorJS
*/
class BlockHandler
{
/**
* Default pseudo-key for numerical arrays
*/
const DEFAULT_ARRAY_KEY = "-";
/**
* @var ConfigLoader|null
*/
private $rules = null;
/**
* BlockHandler constructor
*
* @param string $configuration
*
* @throws EditorJSException
*/
public function __construct($configuration)
{
$this->rules = new ConfigLoader($configuration);
}
/**
* Validate block for correctness
*
* @param string $blockType
* @param array $blockData
*
* @throws EditorJSException
*
* @return bool
*/
public function validateBlock($blockType, $blockData)
{
/**
* Default action for blocks that are not mentioned in a configuration
*/
if (!array_key_exists($blockType, $this->rules->tools)) {
throw new EditorJSException("Tool `$blockType` not found in the configuration");
}
$rule = $this->rules->tools[$blockType];
return $this->validate($rule, $blockData);
}
/**
* Apply sanitizing rules according to the block type
*
* @param string $blockType
* @param array $blockData
*
* @throws EditorJSException
*
* @return array|bool
*/
public function sanitizeBlock($blockType, $blockData, $blockTunes)
{
$rule = $this->rules->tools[$blockType];
return [
'type' => $blockType,
'data' => $this->sanitize($rule, $blockData),
'tunes' => $blockTunes
];
}
/**
* Apply validation rule to the data block
*
* @param array $rules
* @param array $blockData
*
* @throws EditorJSException
*
* @return bool
*/
private function validate($rules, $blockData)
{
/**
* Make sure that every required param exists in data block
*/
foreach ($rules as $key => $value) {
if (($key != BlockHandler::DEFAULT_ARRAY_KEY) && (isset($value['required']) ? $value['required'] : true)) {
if (!isset($blockData[$key])) {
throw new EditorJSException("Not found required param `$key`");
}
}
}
/**
* Check if there is not extra params (not mentioned in configuration rule)
*/
foreach ($blockData as $key => $value) {
if (!is_integer($key) && !isset($rules[$key])) {
throw new EditorJSException("Found extra param `$key`");
}
}
/**
* Validate every key in data block
*/
foreach ($blockData as $key => $value) {
/**
* PHP Array has integer keys
*/
if (is_integer($key)) {
$key = BlockHandler::DEFAULT_ARRAY_KEY;
}
$rule = $rules[$key];
$rule = $this->expandToolSettings($rule);
$elementType = $rule['type'];
/**
* Process canBeOnly rule
*/
if (isset($rule['canBeOnly'])) {
if (!in_array($value, $rule['canBeOnly'])) {
throw new EditorJSException("Option '$key' with value `$value` has invalid value. Check canBeOnly param.");
}
// Do not perform additional elements validation in any case
continue;
}
/**
* Do not check element type if it is not required and null
*/
if (isset($rule['required']) && $rule['required'] === false &&
isset($rule['allow_null']) && $rule['allow_null'] === true && $value === null) {
continue;
}
/**
* Validate element types
*/
switch ($elementType) {
case 'string':
if (!is_string($value)) {
throw new EditorJSException("Option '$key' with value `$value` must be string");
}
break;
case 'integer':
case 'int':
if (!is_integer($value)) {
throw new EditorJSException("Option '$key' with value `$value` must be integer");
}
break;
case 'array':
$this->validate($rule['data'], $value);
break;
case 'boolean':
case 'bool':
if (!is_bool($value)) {
throw new EditorJSException("Option '$key' with value `$value` must be boolean");
}
break;
default:
throw new EditorJSException("Unhandled type `$elementType`");
}
}
return true;
}
/**
* Sanitize strings in the data block
*
* @param array $rules
* @param array $blockData
*
* @throws EditorJSException
*
* @return array
*/
private function sanitize($rules, $blockData)
{
/**
* Sanitize every key in data block
*/
foreach ($blockData as $key => $value) {
/**
* PHP Array has integer keys
*/
if (is_integer($key)) {
$rule = $rules[BlockHandler::DEFAULT_ARRAY_KEY];
} else {
$rule = $rules[$key];
}
$rule = $this->expandToolSettings($rule);
$elementType = $rule['type'];
/**
* Sanitize string with Purifier
*/
if ($elementType == 'string') {
$allowedTags = isset($rule['allowedTags']) ? $rule['allowedTags'] : '';
if ($allowedTags !== '*') {
$blockData[$key] = $this->getPurifier($allowedTags)->purify($value);
}
}
/**
* Sanitize nested elements
*/
if ($elementType == 'array') {
$blockData[$key] = $this->sanitize($rule['data'], $value);
}
}
return $blockData;
}
/**
* Create and return new default purifier
*
* @param $allowedTags
*
* @return \HTMLPurifier
*/
private function getPurifier($allowedTags)
{
$sanitizer = $this->getDefaultPurifier();
$sanitizer->set('HTML.Allowed', $allowedTags);
/**
* Define custom HTML Definition for mark tool
*/
if ($def = $sanitizer->maybeGetRawHTMLDefinition()) {
$def->addElement('mark', 'Inline', 'Inline', 'Common');
}
$purifier = new \HTMLPurifier($sanitizer);
return $purifier;
}
/**
* Initialize HTML Purifier with default settings
*/
private function getDefaultPurifier()
{
$sanitizer = \HTMLPurifier_Config::createDefault();
$sanitizer->set('HTML.TargetBlank', true);
$sanitizer->set('URI.AllowedSchemes', ['http' => true, 'https' => true, 'mailto' => true, 'tel' => true]);
$sanitizer->set('AutoFormat.RemoveEmpty', true);
$sanitizer->set('HTML.DefinitionID', 'html5-definitions');
$cacheDirectory = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'purifier';
if (!is_dir($cacheDirectory)) {
mkdir($cacheDirectory, 0777, true);
}
$sanitizer->set('Cache.SerializerPath', $cacheDirectory);
return $sanitizer;
}
/**
* Check whether the array is associative or sequential
*
* @param array $arr – array to check
*
* @return bool – true if the array is associative
*/
private function isAssoc(array $arr)
{
if ([] === $arr) {
return false;
}
return array_keys($arr) !== range(0, count($arr) - 1);
}
/**
* Expand shortified tool settings
*
* @param $rule – tool settings
*
* @throws EditorJSException
*
* @return array – expanded tool settings
*/
private function expandToolSettings($rule)
{
if (is_string($rule)) {
// 'blockName': 'string' – tool with string type and default settings
$expandedRule = ["type" => $rule];
} elseif (is_array($rule)) {
if ($this->isAssoc($rule)) {
$expandedRule = $rule;
} else {
// 'blockName': [] – tool with canBeOnly and default settings
$expandedRule = ["type" => "string", "canBeOnly" => $rule];
}
} else {
throw new EditorJSException("Cannot determine element type of the rule `$rule`.");
}
return $expandedRule;
}
}