From 3b155059fba60367ee57c0dd8d3b143e7752889f Mon Sep 17 00:00:00 2001 From: Maxime Gratens Date: Sun, 8 Jan 2023 15:16:24 +0000 Subject: [PATCH 1/4] fix: removed cache folder --- EditorJS/BlockHandler.php | 7 ------- 1 file changed, 7 deletions(-) diff --git a/EditorJS/BlockHandler.php b/EditorJS/BlockHandler.php index 5a55151..12ca39d 100644 --- a/EditorJS/BlockHandler.php +++ b/EditorJS/BlockHandler.php @@ -267,13 +267,6 @@ private function getDefaultPurifier() $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; } From 3198b524a638f50295aa38c371c97e7567b3a2f7 Mon Sep 17 00:00:00 2001 From: Maxime Gratens Date: Tue, 10 Jan 2023 13:56:56 +0000 Subject: [PATCH 2/4] wip --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 0ac1795..4120639 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "codex-team/editor.js", + "name": "desti/editor.js", "type": "library", "description": "PHP backend implementation for the Editor.js", "license": "MIT", From ed5897d274fad89b2f21449be69490dfef62e3e1 Mon Sep 17 00:00:00 2001 From: Maxime Gratens Date: Wed, 11 Jan 2023 11:48:58 +0000 Subject: [PATCH 3/4] wip --- EditorJS/BlockHandler.php | 90 --------------------------------------- README.md | 44 +++++++++---------- 2 files changed, 22 insertions(+), 112 deletions(-) diff --git a/EditorJS/BlockHandler.php b/EditorJS/BlockHandler.php index 12ca39d..3fb439f 100644 --- a/EditorJS/BlockHandler.php +++ b/EditorJS/BlockHandler.php @@ -88,96 +88,6 @@ public function sanitizeBlock($blockType, $blockData, $blockTunes) */ 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; } diff --git a/README.md b/README.md index f3a5e01..8c4f17e 100644 --- a/README.md +++ b/README.md @@ -243,32 +243,32 @@ Another configuration example: [/tests/samples/test-config.json](/tests/samples/ # Exceptions ### EditorJS class -| Exception text | Cause -| ----------------------------- | ------------------------------------------------ -| JSON is empty | EditorJS initiated with empty `$json` argument -| Wrong JSON format: `error` | `json_decode` failed during `$json` processing -| Input is null | `json_decode` returned null `$data` object -| Input array is empty | `$data` is an empty array -| Field \`blocks\` is missing | `$data` doesn't contain 'blocks' key -| Blocks is not an array | `$data['blocks']` is not an array -| Block must be an Array | one element in `$data['blocks']` is not an array +| Exception text | Cause | +|-----------------------------|--------------------------------------------------| +| JSON is empty | EditorJS initiated with empty `$json` argument | +| Wrong JSON format: `error` | `json_decode` failed during `$json` processing | +| Input is null | `json_decode` returned null `$data` object | +| Input array is empty | `$data` is an empty array | +| Field \`blocks\` is missing | `$data` doesn't contain 'blocks' key | +| Blocks is not an array | `$data['blocks']` is not an array | +| Block must be an Array | one element in `$data['blocks']` is not an array | ### BlockHandler class -| Exception text | Cause -| --------------------- | ----------------------------------------------- -| Tool \`**TOOL_NAME**\` not found in the configuration | Configuration file doesn't contain **TOOL_NAME** in `tools{}` dictionary -| Not found required param \`**key**\` | **key** tool param exists in configuration but doesn't exist in input data. *(Params are always required by default unless `required: false` is set)* -| Found extra param \`**key**\` | Param **key** exists in input data but doesn't defined in configuration -| Option \`**key**\` with value \`**value**\` has invalid value. Check canBeOnly param. | Parameter must have one of the values from **canBeOnly** array in tool configuration -| Option \`**key**\` with value \`**value**\` must be **TYPE** | Param must have type which is defined in tool configuration *(string, integer, boolean)* -| Unhandled type \`**elementType**\` | Param type in configuration is invalid +| Exception text | Cause | +|---------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------| +| Tool \`**TOOL_NAME**\` not found in the configuration | Configuration file doesn't contain **TOOL_NAME** in `tools{}` dictionary | +| Not found required param \`**key**\` | **key** tool param exists in configuration but doesn't exist in input data. *(Params are always required by default unless `required: false` is set)* | +| Found extra param \`**key**\` | Param **key** exists in input data but doesn't defined in configuration | +| Option \`**key**\` with value \`**value**\` has invalid value. Check canBeOnly param. | Parameter must have one of the values from **canBeOnly** array in tool configuration | +| Option \`**key**\` with value \`**value**\` must be **TYPE** | Param must have type which is defined in tool configuration *(string, integer, boolean)* | +| Unhandled type \`**elementType**\` | Param type in configuration is invalid | ### ConfigLoader class -| Exception text | Cause -| ----------------------------- | ------------------------------------------------ -| Configuration data is empty | EditorJS initiated with empty `$configuration` argument -| Tools not found in configuration | Configuration file doesn't contain `tools` key -| Duplicate tool \`**toolName**\` in configuration | Configuration file has different tools with the same name +| Exception text | Cause | +|--------------------------------------------------|-----------------------------------------------------------| +| Configuration data is empty | EditorJS initiated with empty `$configuration` argument | +| Tools not found in configuration | Configuration file doesn't contain `tools` key | +| Duplicate tool \`**toolName**\` in configuration | Configuration file has different tools with the same name | # Make Tools From 63a4c76ba0c693d63d3f849a714d1493b2962cdb Mon Sep 17 00:00:00 2001 From: Maxime Gratens Date: Wed, 11 Jan 2023 12:00:29 +0000 Subject: [PATCH 4/4] wip --- EditorJS/BlockHandler.php | 90 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/EditorJS/BlockHandler.php b/EditorJS/BlockHandler.php index 3fb439f..12ca39d 100644 --- a/EditorJS/BlockHandler.php +++ b/EditorJS/BlockHandler.php @@ -88,6 +88,96 @@ public function sanitizeBlock($blockType, $blockData, $blockTunes) */ 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; }