Completed
Push — master ( 9767fa...c574ef )
by Dmitry
26:32
created

BaseIpHelper   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Test Coverage

Coverage 96.55%

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 106
ccs 28
cts 29
cp 0.9655
rs 10
c 0
b 0
f 0
wmc 13

4 Methods

Rating   Name   Duplication   Size   Complexity  
A inRange() 0 18 6
A getIpVersion() 0 3 2
A ip2bin() 0 16 4
A expandIPv6() 0 4 1
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\helpers;
9
10
use yii\base\NotSupportedException;
11
12
/**
13
 * Class BaseIpHelper provides concrete implementation for [[IpHelper]]
14
 *
15
 * Do not use BaseIpHelper, use [[IpHelper]] instead.
16
 *
17
 * @author Dmytro Naumenko <[email protected]>
18
 * @since 2.0.14
19
 */
20
class BaseIpHelper
21
{
22
    const IPV4 = 4;
23
    const IPV6 = 6;
24
    /**
25
     * The length of IPv6 address in bits
26
     */
27
    const IPV6_ADDRESS_LENGTH = 128;
28
    /**
29
     * The length of IPv4 address in bits
30
     */
31
    const IPV4_ADDRESS_LENGTH = 32;
32
33
34
    /**
35
     * Gets the IP version. Does not perform IP address validation.
36
     *
37
     * @param string $ip the valid IPv4 or IPv6 address.
38
     * @return int [[IPV4]] or [[IPV6]]
39
     */
40 44
    public static function getIpVersion($ip)
41
    {
42 44
        return strpos($ip, ':') === false ? self::IPV4 : self::IPV6;
43
    }
44
45
    /**
46
     * Checks whether IP address or subnet $subnet is contained by $subnet.
47
     *
48
     * For example, the following code checks whether subnet `192.168.1.0/24` is in subnet `192.168.0.0/22`:
49
     *
50
     * ```php
51
     * IpHelper::inRange('192.168.1.0/24', '192.168.0.0/22'); // true
52
     * ```
53
     *
54
     * In case you need to check whether a single IP address `192.168.1.21` is in the subnet `192.168.1.0/24`,
55
     * you can use any of theses examples:
56
     *
57
     * ```php
58
     * IpHelper::inRange('192.168.1.21', '192.168.1.0/24'); // true
59
     * IpHelper::inRange('192.168.1.21/32', '192.168.1.0/24'); // true
60
     * ```
61
     *
62
     * @param string $subnet the valid IPv4 or IPv6 address or CIDR range, e.g.: `10.0.0.0/8` or `2001:af::/64`
63
     * @param string $range the valid IPv4 or IPv6 CIDR range, e.g. `10.0.0.0/8` or `2001:af::/64`
64
     * @return bool whether $subnet is contained by $range
65
     *
66
     * @throws NotSupportedException
67
     * @see https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing
68
     */
69 25
    public static function inRange($subnet, $range)
70
    {
71 25
        list($ip, $mask) = array_pad(explode('/', $subnet), 2, null);
72 25
        list($net, $netMask) = array_pad(explode('/', $range), 2, null);
73
74 25
        $ipVersion = static::getIpVersion($ip);
75 25
        $netVersion = static::getIpVersion($net);
76 25
        if ($ipVersion !== $netVersion) {
77 2
            return false;
78
        }
79
80 25
        $maxMask = $ipVersion === self::IPV4 ? self::IPV4_ADDRESS_LENGTH : self::IPV6_ADDRESS_LENGTH;
81 25
        $mask = isset($mask) ? $mask : $maxMask;
82 25
        $netMask = isset($netMask) ? $netMask : $maxMask;
83
84 25
        $binIp = static::ip2bin($ip);
85 25
        $binNet = static::ip2bin($net);
86 25
        return substr($binIp, 0, $netMask) === substr($binNet, 0, $netMask) && $mask >= $netMask;
87
    }
88
89
    /**
90
     * Expands an IPv6 address to it's full notation.
91
     *
92
     * For example `2001:db8::1` will be expanded to `2001:0db8:0000:0000:0000:0000:0000:0001`
93
     *
94
     * @param string $ip the original valid IPv6 address
95
     * @return string the expanded IPv6 address
96
     */
97 4
    public static function expandIPv6($ip)
98
    {
99 4
        $hex = unpack('H*hex', inet_pton($ip));
100 3
        return substr(preg_replace('/([a-f0-9]{4})/i', '$1:', $hex['hex']), 0, -1);
101
    }
102
103
    /**
104
     * Converts IP address to bits representation.
105
     *
106
     * @param string $ip the valid IPv4 or IPv6 address
107
     * @return string bits as a string
108
     * @throws NotSupportedException
109
     */
110 30
    public static function ip2bin($ip)
111
    {
112 30
        $ipBinary = null;
113 30
        if (static::getIpVersion($ip) === self::IPV4) {
114 22
            $ipBinary = pack('N', ip2long($ip));
115 11
        } elseif (@inet_pton('::1') === false) {
116
            throw new NotSupportedException('IPv6 is not supported by inet_pton()!');
117
        } else {
118 11
            $ipBinary = inet_pton($ip);
119
        }
120
121 30
        $result = '';
122 30
        for ($i = 0; $i < strlen($ipBinary); $i += 4) {
123 30
            $result .= str_pad(decbin(unpack('N', substr($ipBinary, $i, 4))[1]), 32, '0', STR_PAD_LEFT);
124
        }
125 30
        return $result;
126
    }
127
}
128