|
| 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