| Conditions | 9 |
| Paths | 18 |
| Total Lines | 106 |
| Code Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 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 defined('SYSPATH') or die('No direct access allowed.'); |
||
| 260 | public function xss_clean($data, $tool = null) |
||
| 261 | { |
||
| 262 | if ($tool === null) { |
||
| 263 | // Use the default tool |
||
| 264 | $tool = Kohana::config('core.global_xss_filtering'); |
||
| 265 | } |
||
| 266 | |||
| 267 | if (is_array($data)) { |
||
| 268 | foreach ($data as $key => $val) { |
||
| 269 | $data[$key] = $this->xss_clean($val, $tool); |
||
| 270 | } |
||
| 271 | |||
| 272 | return $data; |
||
| 273 | } |
||
| 274 | |||
| 275 | // Do not clean empty strings |
||
| 276 | if (trim($data) === '') { |
||
| 277 | return $data; |
||
| 278 | } |
||
| 279 | |||
| 280 | if ($tool === true) { |
||
| 281 | // NOTE: This is necessary because switch is NOT type-sensative! |
||
| 282 | $tool = 'default'; |
||
| 283 | } |
||
| 284 | |||
| 285 | switch ($tool) { |
||
| 286 | case 'htmlpurifier': |
||
| 287 | /** |
||
| 288 | * @todo License should go here, http://htmlpurifier.org/ |
||
| 289 | */ |
||
| 290 | if (! class_exists('HTMLPurifier_Config', false)) { |
||
| 291 | // Load HTMLPurifier |
||
| 292 | require Kohana::find_file('vendor', 'htmlpurifier/HTMLPurifier.auto', true); |
||
| 293 | require 'HTMLPurifier.func.php'; |
||
| 294 | } |
||
| 295 | |||
| 296 | // Set configuration |
||
| 297 | $config = HTMLPurifier_Config::createDefault(); |
||
| 298 | $config->set('HTML', 'TidyLevel', 'none'); // Only XSS cleaning now |
||
| 299 | |||
| 300 | // Run HTMLPurifier |
||
| 301 | $data = HTMLPurifier($data, $config); |
||
| 302 | break; |
||
| 303 | default: |
||
| 304 | // http://svn.bitflux.ch/repos/public/popoon/trunk/classes/externalinput.php |
||
| 305 | // +----------------------------------------------------------------------+ |
||
| 306 | // | Copyright (c) 2001-2006 Bitflux GmbH | |
||
| 307 | // +----------------------------------------------------------------------+ |
||
| 308 | // | Licensed under the Apache License, Version 2.0 (the "License"); | |
||
| 309 | // | you may not use this file except in compliance with the License. | |
||
| 310 | // | You may obtain a copy of the License at | |
||
| 311 | // | http://www.apache.org/licenses/LICENSE-2.0 | |
||
| 312 | // | Unless required by applicable law or agreed to in writing, software | |
||
| 313 | // | distributed under the License is distributed on an "AS IS" BASIS, | |
||
| 314 | // | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | |
||
| 315 | // | implied. See the License for the specific language governing | |
||
| 316 | // | permissions and limitations under the License. | |
||
| 317 | // +----------------------------------------------------------------------+ |
||
| 318 | // | Author: Christian Stocker <[email protected]> | |
||
| 319 | // +----------------------------------------------------------------------+ |
||
| 320 | // |
||
| 321 | // Kohana Modifications: |
||
| 322 | // * Changed double quotes to single quotes, changed indenting and spacing |
||
| 323 | // * Removed magic_quotes stuff |
||
| 324 | // * Increased regex readability: |
||
| 325 | // * Used delimeters that aren't found in the pattern |
||
| 326 | // * Removed all unneeded escapes |
||
| 327 | // * Deleted U modifiers and swapped greediness where needed |
||
| 328 | // * Increased regex speed: |
||
| 329 | // * Made capturing parentheses non-capturing where possible |
||
| 330 | // * Removed parentheses where possible |
||
| 331 | // * Split up alternation alternatives |
||
| 332 | // * Made some quantifiers possessive |
||
| 333 | |||
| 334 | // Fix &entity\n; |
||
| 335 | $data = str_replace(array('&', '<', '>'), array('&amp;', '&lt;', '&gt;'), $data); |
||
| 336 | $data = preg_replace('/(&#*\w+)[\x00-\x20]+;/u', '$1;', $data); |
||
| 337 | $data = preg_replace('/(&#x*[0-9A-F]+);*/iu', '$1;', $data); |
||
| 338 | $data = html_entity_decode($data, ENT_COMPAT, 'UTF-8'); |
||
| 339 | |||
| 340 | // Remove any attribute starting with "on" or xmlns |
||
| 341 | $data = preg_replace('#(<[^>]+?[\x00-\x20"\'])(?:on|xmlns)[^>]*+>#iu', '$1>', $data); |
||
| 342 | |||
| 343 | // Remove javascript: and vbscript: protocols |
||
| 344 | $data = preg_replace('#([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); |
||
| 345 | $data = preg_replace('#([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); |
||
| 346 | $data = preg_replace('#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*-moz-binding[\x00-\x20]*:#u', '$1=$2nomozbinding...', $data); |
||
| 347 | |||
| 348 | // Only works in IE: <span style="width: expression(alert('Ping!'));"></span> |
||
| 349 | $data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?expression[\x00-\x20]*\([^>]*+>#i', '$1>', $data); |
||
| 350 | $data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?behaviour[\x00-\x20]*\([^>]*+>#i', '$1>', $data); |
||
| 351 | $data = preg_replace('#(<[^>]+?)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); |
||
| 352 | |||
| 353 | // Remove namespaced elements (we do not need them) |
||
| 354 | $data = preg_replace('#</*\w+:\w[^>]*+>#i', '', $data); |
||
| 355 | |||
| 356 | do { |
||
| 357 | // Remove really unwanted tags |
||
| 358 | $old_data = $data; |
||
| 359 | $data = preg_replace('#</*(?:applet|b(?:ase|gsound|link)|embed|frame(?:set)?|i(?:frame|layer)|l(?:ayer|ink)|meta|object|s(?:cript|tyle)|title|xml)[^>]*+>#i', '', $data); |
||
| 360 | } while ($old_data !== $data); |
||
| 361 | break; |
||
| 362 | } |
||
| 363 | |||
| 364 | return $data; |
||
| 365 | } |
||
| 366 | |||
| 420 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.