| Total Complexity | 66 |
| Total Lines | 315 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like HTMLEditorSanitiser often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use HTMLEditorSanitiser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class HTMLEditorSanitiser |
||
| 20 | { |
||
| 21 | use Injectable; |
||
| 22 | |||
| 23 | /** @var [stdClass] - $element => $rule hash for whitelist element rules where the element name isn't a pattern */ |
||
|
|
|||
| 24 | protected $elements = array(); |
||
| 25 | /** @var [stdClass] - Sequential list of whitelist element rules where the element name is a pattern */ |
||
| 26 | protected $elementPatterns = array(); |
||
| 27 | |||
| 28 | /** @var [stdClass] - The list of attributes that apply to all further whitelisted elements added */ |
||
| 29 | protected $globalAttributes = array(); |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Construct a sanitiser from a given HTMLEditorConfig |
||
| 33 | * |
||
| 34 | * Note that we build data structures from the current state of HTMLEditorConfig - later changes to |
||
| 35 | * the passed instance won't cause this instance to update it's whitelist |
||
| 36 | * |
||
| 37 | * @param HTMLEditorConfig $config |
||
| 38 | */ |
||
| 39 | public function __construct(HTMLEditorConfig $config) |
||
| 40 | { |
||
| 41 | $valid = $config->getOption('valid_elements'); |
||
| 42 | if ($valid) { |
||
| 43 | $this->addValidElements($valid); |
||
| 44 | } |
||
| 45 | |||
| 46 | $valid = $config->getOption('extended_valid_elements'); |
||
| 47 | if ($valid) { |
||
| 48 | $this->addValidElements($valid); |
||
| 49 | } |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Given a TinyMCE pattern (close to unix glob style), create a regex that does the match |
||
| 54 | * |
||
| 55 | * @param $str - The TinyMCE pattern |
||
| 56 | * @return string - The equivalent regex |
||
| 57 | */ |
||
| 58 | protected function patternToRegex($str) |
||
| 59 | { |
||
| 60 | return '/^' . preg_replace('/([?+*])/', '.$1', $str) . '$/'; |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Given a valid_elements string, parse out the actual element and attribute rules and add to the |
||
| 65 | * internal whitelist |
||
| 66 | * |
||
| 67 | * Logic based heavily on javascript version from tiny_mce_src.js |
||
| 68 | * |
||
| 69 | * @param string $validElements - The valid_elements or extended_valid_elements string to add to the whitelist |
||
| 70 | */ |
||
| 71 | protected function addValidElements($validElements) |
||
| 72 | { |
||
| 73 | $elementRuleRegExp = '/^([#+\-])?([^\[\/]+)(?:\/([^\[]+))?(?:\[([^\]]+)\])?$/'; |
||
| 74 | $attrRuleRegExp = '/^([!\-])?(\w+::\w+|[^=:<]+)?(?:([=:<])(.*))?$/'; |
||
| 75 | $hasPatternsRegExp = '/[*?+]/'; |
||
| 76 | |||
| 77 | foreach (explode(',', $validElements) as $validElement) { |
||
| 78 | if (preg_match($elementRuleRegExp, $validElement, $matches)) { |
||
| 79 | $prefix = isset($matches[1]) ? $matches[1] : null; |
||
| 80 | $elementName = isset($matches[2]) ? $matches[2] : null; |
||
| 81 | $outputName = isset($matches[3]) ? $matches[3] : null; |
||
| 82 | $attrData = isset($matches[4]) ? $matches[4] : null; |
||
| 83 | |||
| 84 | // Create the new element |
||
| 85 | $element = new stdClass(); |
||
| 86 | $element->attributes = array(); |
||
| 87 | $element->attributePatterns = array(); |
||
| 88 | |||
| 89 | $element->attributesRequired = array(); |
||
| 90 | $element->attributesDefault = array(); |
||
| 91 | $element->attributesForced = array(); |
||
| 92 | |||
| 93 | foreach (array('#' => 'paddEmpty', '-' => 'removeEmpty') as $match => $means) { |
||
| 94 | $element->$means = ($prefix === $match); |
||
| 95 | } |
||
| 96 | |||
| 97 | // Copy attributes from global rule into current rule |
||
| 98 | if ($this->globalAttributes) { |
||
| 99 | $element->attributes = array_merge($element->attributes, $this->globalAttributes); |
||
| 100 | } |
||
| 101 | |||
| 102 | // Attributes defined |
||
| 103 | if ($attrData) { |
||
| 104 | foreach (explode('|', $attrData) as $attr) { |
||
| 105 | if (preg_match($attrRuleRegExp, $attr, $matches)) { |
||
| 106 | $attr = new stdClass(); |
||
| 107 | |||
| 108 | $attrType = isset($matches[1]) ? $matches[1] : null; |
||
| 109 | $attrName = isset($matches[2]) ? str_replace('::', ':', $matches[2]) : null; |
||
| 110 | $prefix = isset($matches[3]) ? $matches[3] : null; |
||
| 111 | $value = isset($matches[4]) ? $matches[4] : null; |
||
| 112 | |||
| 113 | // Required |
||
| 114 | if ($attrType === '!') { |
||
| 115 | $element->attributesRequired[] = $attrName; |
||
| 116 | $attr->required = true; |
||
| 117 | } // Denied from global |
||
| 118 | elseif ($attrType === '-') { |
||
| 119 | unset($element->attributes[$attrName]); |
||
| 120 | continue; |
||
| 121 | } |
||
| 122 | |||
| 123 | // Default value |
||
| 124 | if ($prefix) { |
||
| 125 | // Default value |
||
| 126 | if ($prefix === '=') { |
||
| 127 | $element->attributesDefault[$attrName] = $value; |
||
| 128 | $attr->defaultValue = $value; |
||
| 129 | } // Forced value |
||
| 130 | elseif ($prefix === ':') { |
||
| 131 | $element->attributesForced[$attrName] = $value; |
||
| 132 | $attr->forcedValue = $value; |
||
| 133 | } // Required values |
||
| 134 | elseif ($prefix === '<') { |
||
| 135 | $attr->validValues = explode('?', $value); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | // Check for attribute patterns |
||
| 140 | if (preg_match($hasPatternsRegExp, $attrName)) { |
||
| 141 | $attr->pattern = $this->patternToRegex($attrName); |
||
| 142 | $element->attributePatterns[] = $attr; |
||
| 143 | } else { |
||
| 144 | $element->attributes[$attrName] = $attr; |
||
| 145 | } |
||
| 146 | } |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | // Global rule, store away these for later usage |
||
| 151 | if (!$this->globalAttributes && $elementName == '@') { |
||
| 152 | $this->globalAttributes = $element->attributes; |
||
| 153 | } |
||
| 154 | |||
| 155 | // Handle substitute elements such as b/strong |
||
| 156 | if ($outputName) { |
||
| 157 | $element->outputName = $elementName; |
||
| 158 | $this->elements[$outputName] = $element; |
||
| 159 | } |
||
| 160 | |||
| 161 | // Add pattern or exact element |
||
| 162 | if (preg_match($hasPatternsRegExp, $elementName)) { |
||
| 163 | $element->pattern = $this->patternToRegex($elementName); |
||
| 164 | $this->elementPatterns[] = $element; |
||
| 165 | } else { |
||
| 166 | $this->elements[$elementName] = $element; |
||
| 167 | } |
||
| 168 | } |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Given an element tag, return the rule structure for that element |
||
| 174 | * @param string $tag The element tag |
||
| 175 | * @return stdClass The element rule |
||
| 176 | */ |
||
| 177 | protected function getRuleForElement($tag) |
||
| 178 | { |
||
| 179 | if (isset($this->elements[$tag])) { |
||
| 180 | return $this->elements[$tag]; |
||
| 181 | } |
||
| 182 | foreach ($this->elementPatterns as $element) { |
||
| 183 | if (preg_match($element->pattern, $tag)) { |
||
| 184 | return $element; |
||
| 185 | } |
||
| 186 | } |
||
| 187 | return null; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Given an attribute name, return the rule structure for that attribute |
||
| 192 | * |
||
| 193 | * @param stdClass $elementRule |
||
| 194 | * @param string $name The attribute name |
||
| 195 | * @return stdClass The attribute rule |
||
| 196 | */ |
||
| 197 | protected function getRuleForAttribute($elementRule, $name) |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Given a DOMElement and an element rule, check if that element passes the rule |
||
| 212 | * @param DOMElement $element The element to check |
||
| 213 | * @param stdClass $rule The rule to check against |
||
| 214 | * @return bool true if the element passes (and so can be kept), false if it fails (and so needs stripping) |
||
| 215 | */ |
||
| 216 | protected function elementMatchesRule($element, $rule = null) |
||
| 217 | { |
||
| 218 | // If the rule doesn't exist at all, the element isn't allowed |
||
| 219 | if (!$rule) { |
||
| 220 | return false; |
||
| 221 | } |
||
| 222 | |||
| 223 | // If the rule has attributes required, check them to see if this element has at least one |
||
| 224 | if ($rule->attributesRequired) { |
||
| 225 | $hasMatch = false; |
||
| 226 | |||
| 227 | foreach ($rule->attributesRequired as $attr) { |
||
| 228 | if ($element->getAttribute($attr)) { |
||
| 229 | $hasMatch = true; |
||
| 230 | break; |
||
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 234 | if (!$hasMatch) { |
||
| 235 | return false; |
||
| 236 | } |
||
| 237 | } |
||
| 238 | |||
| 239 | // If the rule says to remove empty elements, and this element is empty, remove it |
||
| 240 | if ($rule->removeEmpty && !$element->firstChild) { |
||
| 241 | return false; |
||
| 242 | } |
||
| 243 | |||
| 244 | // No further tests required, element passes |
||
| 245 | return true; |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Given a DOMAttr and an attribute rule, check if that attribute passes the rule |
||
| 250 | * @param DOMAttr $attr - the attribute to check |
||
| 251 | * @param stdClass $rule - the rule to check against |
||
| 252 | * @return bool - true if the attribute passes (and so can be kept), false if it fails (and so needs stripping) |
||
| 253 | */ |
||
| 254 | protected function attributeMatchesRule($attr, $rule = null) |
||
| 255 | { |
||
| 256 | // If the rule doesn't exist at all, the attribute isn't allowed |
||
| 257 | if (!$rule) { |
||
| 258 | return false; |
||
| 259 | } |
||
| 260 | |||
| 261 | // If the rule has a set of valid values, check them to see if this attribute is one |
||
| 262 | if (isset($rule->validValues) && !in_array($attr->value, $rule->validValues)) { |
||
| 263 | return false; |
||
| 264 | } |
||
| 265 | |||
| 266 | // No further tests required, attribute passes |
||
| 267 | return true; |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Given an SS_HTMLValue instance, will remove and elements and attributes that are |
||
| 272 | * not explicitly included in the whitelist passed to __construct on instance creation |
||
| 273 | * |
||
| 274 | * @param HTMLValue $html - The HTMLValue to remove any non-whitelisted elements & attributes from |
||
| 275 | */ |
||
| 276 | public function sanitise(HTMLValue $html) |
||
| 334 | } |
||
| 335 | } |
||
| 336 | } |
||
| 337 | } |
||
| 338 | } |
||
| 339 |