Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Security 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Security, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Security |
||
13 | { |
||
14 | |||
15 | |||
16 | /** |
||
17 | * Random Hash for protecting URLs |
||
18 | * |
||
19 | * @var string |
||
20 | * @access protected |
||
21 | */ |
||
22 | protected $_xss_hash = ''; |
||
23 | |||
24 | /** |
||
25 | * List of never allowed strings |
||
26 | * |
||
27 | * @var array |
||
28 | * @access protected |
||
29 | */ |
||
30 | protected $_never_allowed_str = [ |
||
31 | 'document.cookie' => '[removed]', |
||
32 | 'document.write' => '[removed]', |
||
33 | '.parentNode' => '[removed]', |
||
34 | '.innerHTML' => '[removed]', |
||
35 | 'window.location' => '[removed]', |
||
36 | '-moz-binding' => '[removed]', |
||
37 | '<!--' => '<!--', |
||
38 | '-->' => '-->', |
||
39 | '<![CDATA[' => '<![CDATA[', |
||
40 | '<comment>' => '<comment>' |
||
41 | ]; |
||
42 | |||
43 | /* never allowed, regex replacement */ |
||
44 | /** |
||
45 | * List of never allowed regex replacement |
||
46 | * |
||
47 | * @var array |
||
48 | * @access protected |
||
49 | */ |
||
50 | protected $_never_allowed_regex = [ |
||
51 | 'javascript\s*:', |
||
52 | 'expression\s*(\(|&\#40;)', // CSS and IE |
||
53 | 'vbscript\s*:', // IE, surprise! |
||
54 | 'Redirect\s+302', |
||
55 | "([\"'])?data\s*:[^\\1]*?base64[^\\1]*?,[^\\1]*?\\1?" |
||
56 | ]; |
||
57 | |||
58 | |||
59 | /** |
||
60 | * XSS Clean |
||
61 | * |
||
62 | * Sanitizes data so that Cross Site Scripting Hacks can be |
||
63 | * prevented. This function does a fair amount of work but |
||
64 | * it is extremely thorough, designed to prevent even the |
||
65 | * most obscure XSS attempts. Nothing is ever 100% foolproof, |
||
66 | * of course, but I haven't been able to get anything passed |
||
67 | * the filter. |
||
68 | * |
||
69 | * Note: This function should only be used to deal with data |
||
70 | * upon submission. It's not something that should |
||
71 | * be used for general runtime processing. |
||
72 | * |
||
73 | * This function was based in part on some code and ideas I |
||
74 | * got from Bitflux: http://channel.bitflux.ch/wiki/XSS_Prevention |
||
75 | * |
||
76 | * To help develop this script I used this great list of |
||
77 | * vulnerabilities along with a few other hacks I've |
||
78 | * harvested from examining vulnerabilities in other programs: |
||
79 | * http://ha.ckers.org/xss.html |
||
80 | * |
||
81 | * @param mixed string or array |
||
82 | * @return string |
||
83 | */ |
||
84 | public function xss_clean($str, $is_image = false, $evilAttribute = true) |
||
281 | |||
282 | // -------------------------------------------------------------------- |
||
283 | |||
284 | /* |
||
285 | * Remove Evil HTML Attributes (like evenhandlers and style) |
||
286 | * |
||
287 | * It removes the evil attribute and either: |
||
288 | * - Everything up until a space |
||
289 | * For example, everything between the pipes: |
||
290 | * <a |style=document.write('hello');alert('world');| class=link> |
||
291 | * - Everything inside the quotes |
||
292 | * For example, everything between the pipes: |
||
293 | * <a |style="document.write('hello'); alert('world');"| class="link"> |
||
294 | * |
||
295 | * @param string $str The string to check |
||
296 | * @param boolean $is_image TRUE if this is an image |
||
297 | * @return string The string with the evil attributes removed |
||
298 | */ |
||
299 | protected function _remove_evil_attributes($str, $is_image, $evilAttribute) |
||
326 | |||
327 | |||
328 | |||
329 | // -------------------------------------------------------------------- |
||
330 | |||
331 | /** |
||
332 | * HTML Entities Decode |
||
333 | * |
||
334 | * This function is a replacement for html_entity_decode() |
||
335 | * |
||
336 | * The reason we are not using html_entity_decode() by itself is because |
||
337 | * while it is not technically correct to leave out the semicolon |
||
338 | * at the end of an entity most browsers will still interpret the entity |
||
339 | * correctly. html_entity_decode() does not convert entities without |
||
340 | * semicolons, so we are left with our own little solution here. Bummer. |
||
341 | * |
||
342 | * @param string |
||
343 | * @param string |
||
344 | * @return string |
||
345 | */ |
||
346 | public function entity_decode($str, $charset = 'UTF-8') |
||
357 | |||
358 | // -------------------------------------------------------------------- |
||
359 | |||
360 | /** |
||
361 | * Filename Security |
||
362 | * |
||
363 | * @param string |
||
364 | * @param bool |
||
365 | * @return string |
||
366 | */ |
||
367 | public function sanitize_filename($str, $relative_path = false) |
||
412 | |||
413 | // ---------------------------------------------------------------- |
||
414 | |||
415 | /** |
||
416 | * Compact Exploded Words |
||
417 | * |
||
418 | * Callback function for xss_clean() to remove whitespace from |
||
419 | * things like j a v a s c r i p t |
||
420 | * |
||
421 | * @param type |
||
422 | * @return type |
||
423 | */ |
||
424 | protected function _compact_exploded_words($matches) |
||
428 | |||
429 | // -------------------------------------------------------------------- |
||
430 | |||
431 | /** |
||
432 | * Sanitize Naughty HTML |
||
433 | * |
||
434 | * Callback function for xss_clean() to remove naughty HTML elements |
||
435 | * |
||
436 | * @param array |
||
437 | * @return string |
||
438 | */ |
||
439 | protected function _sanitize_naughty_html($matches) |
||
450 | |||
451 | // -------------------------------------------------------------------- |
||
452 | |||
453 | /** |
||
454 | * JS Link Removal |
||
455 | * |
||
456 | * Callback function for xss_clean() to sanitize links |
||
457 | * This limits the PCRE backtracks, making it more performance friendly |
||
458 | * and prevents PREG_BACKTRACK_LIMIT_ERROR from being triggered in |
||
459 | * PHP 5.2+ on link-heavy strings |
||
460 | * |
||
461 | * @param array |
||
462 | * @return string |
||
463 | */ |
||
464 | View Code Duplication | protected function _js_link_removal($match) |
|
476 | |||
477 | // -------------------------------------------------------------------- |
||
478 | |||
479 | /** |
||
480 | * JS Image Removal |
||
481 | * |
||
482 | * Callback function for xss_clean() to sanitize image tags |
||
483 | * This limits the PCRE backtracks, making it more performance friendly |
||
484 | * and prevents PREG_BACKTRACK_LIMIT_ERROR from being triggered in |
||
485 | * PHP 5.2+ on image tag heavy strings |
||
486 | * |
||
487 | * @param array |
||
488 | * @return string |
||
489 | */ |
||
490 | View Code Duplication | protected function _js_img_removal($match) |
|
502 | |||
503 | // -------------------------------------------------------------------- |
||
504 | |||
505 | /** |
||
506 | * Attribute Conversion |
||
507 | * |
||
508 | * Used as a callback for XSS Clean |
||
509 | * |
||
510 | * @param array |
||
511 | * @return string |
||
512 | */ |
||
513 | protected function _convert_attribute($match) |
||
517 | |||
518 | // -------------------------------------------------------------------- |
||
519 | |||
520 | /** |
||
521 | * Filter Attributes |
||
522 | * |
||
523 | * Filters tag attributes for consistency and safety |
||
524 | * |
||
525 | * @param string |
||
526 | * @return string |
||
527 | */ |
||
528 | protected function _filter_attributes($str) |
||
540 | |||
541 | // -------------------------------------------------------------------- |
||
542 | |||
543 | /** |
||
544 | * HTML Entity Decode Callback |
||
545 | * |
||
546 | * Used as a callback for XSS Clean |
||
547 | * |
||
548 | * @param array |
||
549 | * @return string |
||
550 | */ |
||
551 | protected function _decode_entity($match) |
||
555 | |||
556 | // -------------------------------------------------------------------- |
||
557 | |||
558 | /** |
||
559 | * Validate URL entities |
||
560 | * |
||
561 | * Called by xss_clean() |
||
562 | * |
||
563 | * @param string |
||
564 | * @return string |
||
565 | */ |
||
566 | protected function _validate_entities($str) |
||
596 | |||
597 | // ---------------------------------------------------------------------- |
||
598 | |||
599 | /** |
||
600 | * Do Never Allowed |
||
601 | * |
||
602 | * A utility function for xss_clean() |
||
603 | * |
||
604 | * @param string |
||
605 | * @return string |
||
606 | */ |
||
607 | protected function _do_never_allowed($str) |
||
617 | |||
618 | |||
619 | protected function remove_invisible_characters($str, $url_encoded = true) |
||
639 | |||
640 | /** |
||
641 | * Random Hash for protecting URLs |
||
642 | * |
||
643 | * @return string |
||
644 | */ |
||
645 | public function xss_hash() |
||
654 | |||
655 | |||
656 | public static function escapeLike($str, $escape = '\'\'') |
||
663 | } |
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.