Completed
Push — master ( 5f905c...e3dbe0 )
by Daniel
07:31
created

getClientRealIpAddress()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
rs 9.4286
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
/**
4
 *
5
 * The MIT License (MIT)
6
 *
7
 * Copyright (c) 2015 Daniel Popiniuc
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
10
 * of this software and associated documentation files (the "Software"), to deal
11
 * in the Software without restriction, including without limitation the rights
12
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 * copies of the Software, and to permit persons to whom the Software is
14
 * furnished to do so, subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be included in all
17
 * copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
 * SOFTWARE.
26
 *
27
 */
28
29
namespace danielgp\network_components;
30
31
trait NetworkComponentsByDanielGP
32
{
33
34
    /**
35
     * Determines if a given IP is with a defined range
36
     *
37
     * @param ipv4 $ipGiven
38
     * @param ipv4 $ipStart
39
     * @param ipv4 $ipEnd
40
     * @return string
41
     */
42
    protected function checkIpIsInRange($ipGiven, $ipStart, $ipEnd)
43
    {
44
        $sReturn     = 'out';
45
        $startNo     = $this->convertIpToNumber($ipStart);
46
        $endNo       = $this->convertIpToNumber($ipEnd);
47
        $evaluatedNo = $this->convertIpToNumber($ipGiven);
48
        if ($sReturn == 'out') {
49
            if (($evaluatedNo >= $startNo) && ($evaluatedNo <= $endNo)) {
50
                $sReturn = 'in';
51
            }
52
        }
53
        return $sReturn;
54
    }
55
56
    /**
57
     * Checks if given IP is a private or public one
58
     *
59
     * @param ipv4 $ipGiven
60
     * @return string
61
     */
62
    protected function checkIpIsPrivate($ipGiven)
63
    {
64
        $ipType = 'unkown';
0 ignored issues
show
Unused Code introduced by
$ipType is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
65
        if (filter_var($ipGiven, FILTER_VALIDATE_IP)) {
66
            if (!filter_var($ipGiven, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE | FILTER_FLAG_NO_PRIV_RANGE)) {
67
                $ipType = 'private';
68
            } else {
69
                $ipType = 'public';
70
            }
71
        } else {
72
            $ipType = 'invalid';
73
        }
74
        return $ipType;
75
    }
76
77
    /**
78
     * Checks if given IP is a V4 or V6
79
     *
80
     * @param ipv4 $ipGiven
81
     * @return string
82
     */
83
    protected function checkIpIsV4OrV6($ipGiven)
84
    {
85
        $ipType = 'unkown';
86
        if (filter_var($ipGiven, FILTER_VALIDATE_IP)) {
87
            if (filter_var($ipGiven, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
88
                $ipType = 'V4';
89
            } elseif (filter_var($ipGiven, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
90
                $ipType = 'V6';
91
            }
92
        } else {
93
            $ipType = 'invalid';
94
        }
95
        return $ipType;
96
    }
97
98
    /**
99
     * Converts IP to a number
100
     *
101
     * @param type $ipGiven
102
     * @return string|int
103
     */
104
    protected function convertIpToNumber($ipGiven)
105
    {
106
        $sReturn = '';
107
        if (filter_var($ipGiven, FILTER_VALIDATE_IP)) {
108
            if (filter_var($ipGiven, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
109
                $ips     = explode('.', $ipGiven);
110
                $sReturn = $ips[3] + $ips[2] * 256 + $ips[1] * 65536 + $ips[0] * 16777216;
111
            } elseif (filter_var($ipGiven, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
112
                $sReturn = $this->convertIpV6ToNumber($ipGiven);
113
            }
114
        } else {
115
            $sReturn = 'invalid IP';
116
        }
117
        return $sReturn;
118
    }
119
120
    private function convertIpV6ToNumber($ipGiven)
121
    {
122
        $binNum = '';
123
        foreach (unpack('C*', inet_pton($ipGiven)) as $byte) {
124
            $binNum .= str_pad(decbin($byte), 8, "0", STR_PAD_LEFT);
125
        }
126
        return base_convert(ltrim($binNum, '0'), 2, 10);
127
    }
128
129
    /**
130
     * Returns the IP of the client
131
     *
132
     * @return string
133
     */
134
    protected function getClientRealIpAddress()
135
    {
136
        $rqst         = new \Symfony\Component\HttpFoundation\Request;
137
        $superGlobals = $rqst->createFromGlobals();
138
        return $superGlobals->getClientIp();
139
    }
140
}
141