Completed
Push — master ( 35e698...79f205 )
by Lorenzo
02:08
created

ip.php ➔ anonimizeIp()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 9.2
1
<?php
2
3
/**
4
 * @param array $server
5
 * @return string
6
 */
7
function getIPVisitor(array $server = []) : string
8
{
9
    if (empty($server)) {
10
        return '';
11
    }
12
13
    if (array_key_exists('HTTP_X_FORWARDED_FOR', $server) && trim($server['HTTP_X_FORWARDED_FOR']) != '') {
14
        $IP2Check = $server['HTTP_X_FORWARDED_FOR'];
15
    } elseif (array_key_exists('REMOTE_ADDR', $server) && trim($server['REMOTE_ADDR'])) {
16
        $IP2Check = $server['REMOTE_ADDR'];
17
    }
18
19
    if ($IP2Check == '') {
20
        return '';
21
    } elseif (strpos($IP2Check, ',') === false) {
0 ignored issues
show
Bug introduced by
The variable $IP2Check does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
22
        return $IP2Check;
23
    } else {
24
        // Header can contain multiple IP-s of proxies that are passed through.
25
        // Only the IP added by the last proxy (last IP in the list) can be trusted.
26
        $client_ip = trim(end(explode(',', $IP2Check)));
0 ignored issues
show
Bug introduced by
explode(',', $IP2Check) cannot be passed to end() as the parameter $array expects a reference.
Loading history...
27
        return $client_ip;
28
    }
29
}
30
31
/**
32
 * anonimizeIp masquerade last 3 digit of IP address
33
 * @param  string $ip
34
 * @return string  masked IP
35
 */
36
function anonimizeIp(string $ip) : string
37
{
38
    if ($ip === null || strlen($ip) < 2 || strrpos($ip, ".") === false) {
39
        return $ip;
40
    }
41
    return substr($ip, 0, strrpos($ip, ".") + 1) . '0';
42
}
43
44
/**
45
 * getHost Get the Internet host name corresponding to a given IP address
46
 * @param  string $ip the IP to resolve.
47
 * @return string    Returns the host name of the Internet host specified by ip_address on success,
48
 *                   the unmodified ip_address on failure,
49
 *                   or FALSE on malformed input.
50
 */
51
function getHost($ip)
52
{
53
    return gethostbyaddr($ip);
54
}
55