Skip to content

Commit 65b3065

Browse files
authored
Revert "Revert "Fix editor according to the Beta requirements""
1 parent 46ccc4a commit 65b3065

32 files changed

+3185
-877
lines changed

.php_cs

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
return PhpCsFixer\Config::create()
3+
->setUsingCache(false)
4+
->setRules([
5+
'@PSR2' => true,
6+
'array_syntax' => ['syntax' => 'short'],
7+
'ordered_imports' => true,
8+
'single_import_per_statement' => false,
9+
'no_whitespace_in_blank_line' => true,
10+
'no_unused_imports' => true,
11+
'no_blank_lines_before_namespace' => false,
12+
'blank_line_before_return' => true,
13+
'binary_operator_spaces' => true,
14+
'cast_spaces' => true,
15+
'short_scalar_cast' => true,
16+
'declare_equal_normalize' => true,
17+
'method_argument_space' => true,
18+
'method_separation' => true,
19+
'no_leading_namespace_whitespace' => true,
20+
'no_multiline_whitespace_around_double_arrow' => true,
21+
'no_whitespace_before_comma_in_array' => true,
22+
'trim_array_spaces' => true,
23+
//'single_quote' => true,
24+
'phpdoc_add_missing_param_annotation' => true,
25+
'phpdoc_align' => true,
26+
'phpdoc_scalar' => true,
27+
'phpdoc_indent' => true,
28+
'phpdoc_order' => true,
29+
'phpdoc_separation' => true,
30+
'no_empty_statement' => true,
31+
'concat_space' => ['spacing' => 'one'],
32+
'no_multiline_whitespace_before_semicolons' => true,
33+
'no_leading_import_slash' => true,
34+
'no_trailing_comma_in_list_call' => true,
35+
'no_trailing_comma_in_singleline_array' => true,
36+
'phpdoc_add_missing_param_annotation' => true,
37+
'phpdoc_align' => true,
38+
'phpdoc_no_empty_return' => true,
39+
'return_type_declaration' => true,
40+
'ternary_operator_spaces' => true,
41+
])
42+
->setFinder(
43+
PhpCsFixer\Finder::create()
44+
->in(__DIR__)
45+
);

CodexEditor/BlockHandler.php

+169
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
<?php
2+
3+
namespace CodexEditor;
4+
5+
/**
6+
* Class BlockHandler
7+
*
8+
* @package CodexEditor
9+
*/
10+
class BlockHandler
11+
{
12+
const DEFAULT_ARRAY_KEY = "-";
13+
14+
/**
15+
* @var ConfigLoader|null
16+
*/
17+
private $rules = null;
18+
19+
/**
20+
* @var \HTMLPurifier_Config
21+
*/
22+
private $sanitizer;
23+
24+
/**
25+
* BlockHandler constructor
26+
*
27+
* @param \HTMLPurifier_Config $sanitizer
28+
* @param mixed $configuration
29+
*
30+
* @throws \Exception
31+
*/
32+
public function __construct($configuration, $sanitizer)
33+
{
34+
$this->rules = new ConfigLoader($configuration);
35+
$this->sanitizer = $sanitizer;
36+
}
37+
38+
/**
39+
* Validate block for correctness and apply sanitizing rules according to the block type
40+
*
41+
* @param $blockType
42+
* @param $blockData
43+
*
44+
* @throws \Exception
45+
*
46+
* @return array|bool
47+
*/
48+
public function validate_block($blockType, $blockData)
49+
{
50+
/**
51+
* Default action for blocks that are not mentioned in a configuration
52+
*/
53+
if (!array_key_exists($blockType, $this->rules->tools)) {
54+
return true;
55+
}
56+
57+
$rule = $this->rules->tools[$blockType];
58+
59+
return [
60+
'type' => $blockType,
61+
'data' => $this->validate($rule, $blockData)
62+
];
63+
}
64+
65+
/**
66+
* Apply validation rule to the data block
67+
*
68+
* @param $rules
69+
* @param $blockData
70+
*
71+
* @throws \Exception
72+
*
73+
* @return mixed
74+
*/
75+
public function validate($rules, $blockData)
76+
{
77+
/**
78+
* Make sure that every required param exists in data block
79+
*/
80+
foreach ($rules as $key => $value) {
81+
if (($key != BlockHandler::DEFAULT_ARRAY_KEY) && (isset($value['required']) ? $value['required'] : true)) {
82+
if (!isset($blockData[$key])) {
83+
throw new \Exception("Not found required param `$key`");
84+
}
85+
}
86+
}
87+
88+
/**
89+
* Check if there is not extra params (not mentioned in configuration rule)
90+
*/
91+
foreach ($blockData as $key => $value) {
92+
if (!is_integer($key) && !isset($rules[$key])) {
93+
throw new \Exception("Found extra param `$key`");
94+
}
95+
}
96+
97+
/**
98+
* Validate every key in data block
99+
*/
100+
foreach ($blockData as $key => $value) {
101+
/**
102+
* PHP Array has integer keys
103+
*/
104+
if (is_integer($key)) {
105+
$key = BlockHandler::DEFAULT_ARRAY_KEY;
106+
}
107+
108+
$rule = $rules[$key];
109+
$elementType = $rule['type'];
110+
111+
/**
112+
* Process canBeOnly rule
113+
*/
114+
if (isset($rule['canBeOnly'])) {
115+
if (!in_array($value, $rule['canBeOnly'])) {
116+
throw new \Exception("Option '$key' with value `$value` has invalid value. Check canBeOnly param.");
117+
}
118+
}
119+
120+
/**
121+
* Validate element types
122+
*/
123+
switch ($elementType) {
124+
case 'string':
125+
$allowedTags = isset($rule['allowedTags']) ? $rule['allowedTags'] : '';
126+
$blockData[$key] = $this->getPurifier($allowedTags)->purify($value);
127+
break;
128+
129+
case 'integer':
130+
case 'int':
131+
if (!is_integer($value)) {
132+
throw new \Exception("`$value` is not an integer");
133+
}
134+
break;
135+
136+
case 'array':
137+
$blockData[$key] = $this->validate($rule['data'], $value);
138+
break;
139+
140+
case 'boolean':
141+
case 'bool':
142+
$blockData[$key] = boolval($value);
143+
break;
144+
145+
default:
146+
throw new \Exception("Unhandled type `$elementType`");
147+
}
148+
}
149+
150+
return $blockData;
151+
}
152+
153+
/**
154+
* Create and return new default purifier
155+
*
156+
* @param $allowedTags
157+
*
158+
* @return \HTMLPurifier
159+
*/
160+
private function getPurifier($allowedTags)
161+
{
162+
$sanitizer = clone $this->sanitizer;
163+
$sanitizer->set('HTML.Allowed', $allowedTags);
164+
165+
$purifier = new \HTMLPurifier($sanitizer);
166+
167+
return $purifier;
168+
}
169+
}

0 commit comments

Comments
 (0)