| Conditions | 4 |
| Paths | 3 |
| Total Lines | 51 |
| Code Lines | 33 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 41 | public static function sanitize(&$data) |
||
| 42 | { |
||
| 43 | if (empty($data)) { |
||
| 44 | return $data; |
||
| 45 | } |
||
| 46 | |||
| 47 | if (is_array($data)) { |
||
|
|
|||
| 48 | array_walk_recursive($data, '\SP\Html\Html::sanitize'); |
||
| 49 | } else { |
||
| 50 | $data = strip_tags($data); |
||
| 51 | |||
| 52 | // Fix &entity\n; |
||
| 53 | $data = str_replace(['&', '<', '>'], ['&amp;', '&lt;', '&gt;'], $data); |
||
| 54 | $data = preg_replace('/(&#*\w+)[\x00-\x20]+;/u', '$1;', $data); |
||
| 55 | $data = preg_replace(/** @lang RegExp */ |
||
| 56 | '/(&#x*[0-9A-F]+);*/iu', '$1;', $data); |
||
| 57 | $data = html_entity_decode($data, ENT_COMPAT, 'UTF-8'); |
||
| 58 | |||
| 59 | // Remove any attribute starting with "on" or xmlns |
||
| 60 | $data = preg_replace(/** @lang RegExp */ |
||
| 61 | '#(<[^>]+?[\x00-\x20\x2f"\'])(?:on|xmlns)[^>]*+>#iu', '$1>', $data); |
||
| 62 | |||
| 63 | // Remove javascript: and vbscript: protocols |
||
| 64 | $data = preg_replace(/** @lang RegExp */ |
||
| 65 | '#([a-z]*)[\x00-\x20]*=[\x00-\x20]*([`\'"]*)[\x00-\x20]*j[\x00-\x20]*a[\x00-\x20]*v[\x00-\x20]*a[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iu', '$1=$2nojavascript...', $data); |
||
| 66 | $data = preg_replace(/** @lang RegExp */ |
||
| 67 | '#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*v[\x00-\x20]*b[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iu', '$1=$2novbscript...', $data); |
||
| 68 | $data = preg_replace(/** @lang RegExp */ |
||
| 69 | '#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*-moz-binding[\x00-\x20]*:#u', '$1=$2nomozbinding...', $data); |
||
| 70 | |||
| 71 | // Only works in IE: <span style="width: expression(alert('Ping!'));"></span> |
||
| 72 | $data = preg_replace(/** @lang RegExp */ |
||
| 73 | '#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?expression[\x00-\x20]*\([^>]*+>#i', '$1>', $data); |
||
| 74 | $data = preg_replace(/** @lang RegExp */ |
||
| 75 | '#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?behaviour[\x00-\x20]*\([^>]*+>#i', '$1>', $data); |
||
| 76 | $data = preg_replace(/** @lang RegExp */ |
||
| 77 | '#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:*[^>]*+>#iu', '$1>', $data); |
||
| 78 | |||
| 79 | // Remove namespaced elements (we do not need them) |
||
| 80 | $data = preg_replace(/** @lang RegExp */ |
||
| 81 | '#</*\w+:\w[^>]*+>#i', '', $data); |
||
| 82 | |||
| 83 | do { |
||
| 84 | // Remove really unwanted tags |
||
| 85 | $old_data = $data; |
||
| 86 | $data = preg_replace(/** @lang RegExp */ |
||
| 87 | '#</*(?:applet|b(?:ase|gsound|link)|embed|frame(?:set)?|i(?:frame|layer)|l(?:ayer|ink)|meta|object|s(?:cript|tyle)|title|xml)[^>]*+>#i', '', $data); |
||
| 88 | } while ($old_data !== $data); |
||
| 89 | } |
||
| 90 | |||
| 91 | return $data; |
||
| 92 | } |
||
| 163 |