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') |
||
347 | { |
||
348 | if (stristr($str, '&') === false) { |
||
349 | return $str; |
||
350 | } |
||
351 | |||
352 | $str = html_entity_decode($str, ENT_COMPAT, $charset); |
||
353 | $str = preg_replace_callback('~&#x(0*[0-9a-f]{2,5})~i', function($matches) { |
||
354 | return chr(hexdec($matches[1])); |
||
355 | }, $str); |
||
356 | return preg_replace_callback('~&#([0-9]{2,4})~', function($matches) { |
||
357 | return chr($matches[1]); |
||
358 | }, $str); |
||
359 | } |
||
360 | |||
361 | // -------------------------------------------------------------------- |
||
362 | |||
363 | /** |
||
364 | * Filename Security |
||
365 | * |
||
366 | * @param string |
||
367 | * @param bool |
||
368 | * @return string |
||
369 | */ |
||
370 | public function sanitize_filename($str, $relative_path = false) |
||
415 | |||
416 | // ---------------------------------------------------------------- |
||
417 | |||
418 | /** |
||
419 | * Compact Exploded Words |
||
420 | * |
||
421 | * Callback function for xss_clean() to remove whitespace from |
||
422 | * things like j a v a s c r i p t |
||
423 | * |
||
424 | * @param type |
||
425 | * @return type |
||
426 | */ |
||
427 | protected function _compact_exploded_words($matches) |
||
431 | |||
432 | // -------------------------------------------------------------------- |
||
433 | |||
434 | /** |
||
435 | * Sanitize Naughty HTML |
||
436 | * |
||
437 | * Callback function for xss_clean() to remove naughty HTML elements |
||
438 | * |
||
439 | * @param array |
||
440 | * @return string |
||
441 | */ |
||
442 | protected function _sanitize_naughty_html($matches) |
||
453 | |||
454 | // -------------------------------------------------------------------- |
||
455 | |||
456 | /** |
||
457 | * JS Link Removal |
||
458 | * |
||
459 | * Callback function for xss_clean() to sanitize links |
||
460 | * This limits the PCRE backtracks, making it more performance friendly |
||
461 | * and prevents PREG_BACKTRACK_LIMIT_ERROR from being triggered in |
||
462 | * PHP 5.2+ on link-heavy strings |
||
463 | * |
||
464 | * @param array |
||
465 | * @return string |
||
466 | */ |
||
467 | View Code Duplication | protected function _js_link_removal($match) |
|
479 | |||
480 | // -------------------------------------------------------------------- |
||
481 | |||
482 | /** |
||
483 | * JS Image Removal |
||
484 | * |
||
485 | * Callback function for xss_clean() to sanitize image tags |
||
486 | * This limits the PCRE backtracks, making it more performance friendly |
||
487 | * and prevents PREG_BACKTRACK_LIMIT_ERROR from being triggered in |
||
488 | * PHP 5.2+ on image tag heavy strings |
||
489 | * |
||
490 | * @param array |
||
491 | * @return string |
||
492 | */ |
||
493 | View Code Duplication | protected function _js_img_removal($match) |
|
505 | |||
506 | // -------------------------------------------------------------------- |
||
507 | |||
508 | /** |
||
509 | * Attribute Conversion |
||
510 | * |
||
511 | * Used as a callback for XSS Clean |
||
512 | * |
||
513 | * @param array |
||
514 | * @return string |
||
515 | */ |
||
516 | protected function _convert_attribute($match) |
||
520 | |||
521 | // -------------------------------------------------------------------- |
||
522 | |||
523 | /** |
||
524 | * Filter Attributes |
||
525 | * |
||
526 | * Filters tag attributes for consistency and safety |
||
527 | * |
||
528 | * @param string |
||
529 | * @return string |
||
530 | */ |
||
531 | protected function _filter_attributes($str) |
||
543 | |||
544 | // -------------------------------------------------------------------- |
||
545 | |||
546 | /** |
||
547 | * HTML Entity Decode Callback |
||
548 | * |
||
549 | * Used as a callback for XSS Clean |
||
550 | * |
||
551 | * @param array |
||
552 | * @return string |
||
553 | */ |
||
554 | protected function _decode_entity($match) |
||
558 | |||
559 | // -------------------------------------------------------------------- |
||
560 | |||
561 | /** |
||
562 | * Validate URL entities |
||
563 | * |
||
564 | * Called by xss_clean() |
||
565 | * |
||
566 | * @param string |
||
567 | * @return string |
||
568 | */ |
||
569 | protected function _validate_entities($str) |
||
599 | |||
600 | // ---------------------------------------------------------------------- |
||
601 | |||
602 | /** |
||
603 | * Do Never Allowed |
||
604 | * |
||
605 | * A utility function for xss_clean() |
||
606 | * |
||
607 | * @param string |
||
608 | * @return string |
||
609 | */ |
||
610 | protected function _do_never_allowed($str) |
||
620 | |||
621 | |||
622 | protected function remove_invisible_characters($str, $url_encoded = true) |
||
642 | |||
643 | /** |
||
644 | * Random Hash for protecting URLs |
||
645 | * |
||
646 | * @return string |
||
647 | */ |
||
648 | public function xss_hash() |
||
657 | |||
658 | |||
659 | 6 | public static function escapeLike($str, $escape = '\'\'') |
|
666 | } |
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.