Address   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 92
rs 10
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A toBinary() 0 11 3
A check() 0 11 4
A fromBinary() 0 11 2
A cidrToDec() 0 3 1
A parse4() 0 7 2
1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      https://syspass.org
7
 * @copyright 2012-2019, Rubén Domínguez nuxsmin@$syspass.org
8
 *
9
 * This file is part of sysPass.
10
 *
11
 * sysPass is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * sysPass is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 *  along with sysPass.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace SP\Http;
26
27
use SP\Core\Exceptions\InvalidArgumentException;
28
29
/**
30
 * Class Address
31
 *
32
 * @package SP\Http
33
 */
34
final class Address
35
{
36
    const PATTERN_IP_ADDRESS = '#^(?<address>[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3})(?:/(?:(?<mask>[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3})|(?<cidr>[\d]{1,2})))?$#';
37
38
    /**
39
     * @param $address
40
     *
41
     * @return mixed
42
     * @throws InvalidArgumentException
43
     */
44
    public static function toBinary(string $address)
45
    {
46
        if (!filter_var($address, FILTER_VALIDATE_IP)
47
            || ($binAddress = @inet_pton($address)) === false
48
        ) {
49
            logger(sprintf('%s : %s', __('Invalid IP'), $address));
50
51
            throw new InvalidArgumentException(__u('Invalid IP'), InvalidArgumentException::ERROR, $address);
52
        }
53
54
        return $binAddress;
55
    }
56
57
    /**
58
     * @param string $address
59
     *
60
     * @return string
61
     * @throws InvalidArgumentException
62
     */
63
    public static function fromBinary(string $address)
64
    {
65
        $stringAddress = @inet_ntop($address);
66
67
        if ($stringAddress === false) {
68
            logger(sprintf('%s : %s', __('Invalid IP'), $address));
69
70
            throw new InvalidArgumentException(__u('Invalid IP'), InvalidArgumentException::ERROR, $address);
71
        }
72
73
        return $stringAddress;
74
    }
75
76
    /**
77
     * Parses an IPv4 address from either "192.168.0.1", "192.168.0.0/255.255.255.0" or "192.168.0.0/24" formats
78
     *
79
     * @param string $address
80
     *
81
     * @return array
82
     * @throws InvalidArgumentException
83
     */
84
    public static function parse4(string $address)
85
    {
86
        if (preg_match(self::PATTERN_IP_ADDRESS, $address, $matches)) {
87
            return $matches;
88
        }
89
90
        throw new InvalidArgumentException(__u('Invalid IP'), InvalidArgumentException::ERROR, $address);
91
    }
92
93
    /**
94
     * Checks whether an IP address is included within $inAddress and $inMask
95
     *
96
     * @param string $address
97
     * @param string $inAddress
98
     * @param string $inMask
99
     *
100
     * @return int
101
     * @throws InvalidArgumentException
102
     */
103
    public static function check(string $address, string $inAddress, string $inMask)
104
    {
105
        if (!filter_var($address, FILTER_VALIDATE_IP)
106
            || !filter_var($inAddress, FILTER_VALIDATE_IP)
107
            || !filter_var($inMask, FILTER_VALIDATE_IP)
108
        ) {
109
            throw new InvalidArgumentException(__u('Invalid IP'), InvalidArgumentException::ERROR, $address);
110
        }
111
112
        // Obtains subnets based on mask ie.: subnet === subnet
113
        return (ip2long($address) & ip2long($inMask)) === (ip2long($inAddress) & ip2long($inMask));
0 ignored issues
show
Bug Best Practice introduced by
The expression return ip2long($address)...ess) & ip2long($inMask) returns the type boolean which is incompatible with the documented return type integer.
Loading history...
114
    }
115
116
    /**
117
     * Converts a CIDR mask into decimal
118
     *
119
     * @param int $bits
120
     *
121
     * @return int
122
     */
123
    public static function cidrToDec(int $bits)
124
    {
125
        return long2ip(-1 << (32 - $bits));
0 ignored issues
show
Bug Best Practice introduced by
The expression return long2ip(-1 << 32 - $bits) returns the type string which is incompatible with the documented return type integer.
Loading history...
126
    }
127
}