|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* MikoPBX - free phone system for small business |
|
5
|
|
|
* Copyright © 2017-2023 Alexey Portnov and Nikolay Beketov |
|
6
|
|
|
* |
|
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
8
|
|
|
* it under the terms of the GNU General Public License as published by |
|
9
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
|
10
|
|
|
* (at your option) any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
* GNU General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
|
18
|
|
|
* If not, see <https://www.gnu.org/licenses/>. |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace MikoPBX\AdminCabinet\Library; |
|
22
|
|
|
|
|
23
|
|
|
use Phalcon\Di\Injectable; |
|
24
|
|
|
|
|
25
|
|
|
class Cidr extends Injectable |
|
26
|
|
|
{ |
|
27
|
|
|
// convert cidr to netmask |
|
28
|
|
|
// e.g. 21 = 255.255.248.0 |
|
29
|
|
|
public function cidr2netmask(int $cidr): false|string |
|
30
|
|
|
{ |
|
31
|
|
|
$bin = ''; |
|
32
|
|
|
for ($i = 1; $i <= 32; $i++) { |
|
33
|
|
|
$bin .= $cidr >= $i ? '1' : '0'; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$netmask = long2ip(intval(bindec($bin))); |
|
37
|
|
|
|
|
38
|
|
|
if ($netmask == "0.0.0.0") { |
|
39
|
|
|
return false; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return $netmask; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
// get network address from cidr subnet |
|
46
|
|
|
// e.g. 10.0.2.56/21 = 10.0.0.0 |
|
47
|
|
|
public function cidr2network(string $ip, int $cidr): false|string |
|
48
|
|
|
{ |
|
49
|
|
|
return long2ip((ip2long($ip)) & ((-1 << (32 - $cidr)))); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
// convert netmask to cidr |
|
53
|
|
|
// e.g. 255.255.255.128 = 25 |
|
54
|
|
|
public function netmask2cidr(string $netmask): int |
|
55
|
|
|
{ |
|
56
|
|
|
$bits = 0; |
|
57
|
|
|
$netmask = explode(".", $netmask); |
|
58
|
|
|
|
|
59
|
|
|
foreach ($netmask as $octect) { |
|
60
|
|
|
$bits += strlen(str_replace("0", "", decbin((int)$octect))); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $bits; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
// is ip in subnet |
|
67
|
|
|
// e.g. is 10.5.21.30 in 10.5.16.0/20 == true |
|
68
|
|
|
// is 192.168.50.2 in 192.168.30.0/23 == false |
|
69
|
|
|
public function cidr_match(string $ip, string $network, int $cidr): bool |
|
70
|
|
|
{ |
|
71
|
|
|
if ((ip2long($ip) & ~((1 << (32 - $cidr)) - 1)) == ip2long($network)) { |
|
72
|
|
|
return true; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return false; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Retrieves an array of network masks with CIDR notation as the key and the corresponding netmask. |
|
80
|
|
|
* |
|
81
|
|
|
* @return array Associative array where keys are CIDR notation (0-32) and values |
|
82
|
|
|
* are the corresponding netmask strings. |
|
83
|
|
|
*/ |
|
84
|
|
|
public static function getNetMasks(): array |
|
85
|
|
|
{ |
|
86
|
|
|
$arrMasks = [ |
|
87
|
|
|
"0" => "0 - 0.0.0.0", |
|
88
|
|
|
"1" => "1 - 128.0.0.0", |
|
89
|
|
|
"2" => "2 - 192.0.0.0", |
|
90
|
|
|
"3" => "3 - 224.0.0.0", |
|
91
|
|
|
"4" => "4 - 240.0.0.0", |
|
92
|
|
|
"5" => "5 - 248.0.0.0", |
|
93
|
|
|
"6" => "6 - 252.0.0.0", |
|
94
|
|
|
"7" => "7 - 254.0.0.0", |
|
95
|
|
|
"8" => "8 - 255.0.0.0", |
|
96
|
|
|
"9" => "9 - 255.128.0.0", |
|
97
|
|
|
"10" => "10 - 255.192.0.0", |
|
98
|
|
|
"11" => "11 - 255.224.0.0", |
|
99
|
|
|
"12" => "12 - 255.240.0.0", |
|
100
|
|
|
"13" => "13 - 255.248.0.0", |
|
101
|
|
|
"14" => "14 - 255.252.0.0", |
|
102
|
|
|
"15" => "15 - 255.254.0.0", |
|
103
|
|
|
"16" => "16 - 255.255.0.0", |
|
104
|
|
|
"17" => "17 - 255.255.128.0", |
|
105
|
|
|
"18" => "18 - 255.255.192.0", |
|
106
|
|
|
"19" => "19 - 255.255.224.0", |
|
107
|
|
|
"20" => "20 - 255.255.240.0", |
|
108
|
|
|
"21" => "21 - 255.255.248.0", |
|
109
|
|
|
"22" => "22 - 255.255.252.0", |
|
110
|
|
|
"23" => "23 - 255.255.254.0", |
|
111
|
|
|
"24" => "24 - 255.255.255.0", |
|
112
|
|
|
"25" => "25 - 255.255.255.128", |
|
113
|
|
|
"26" => "26 - 255.255.255.192", |
|
114
|
|
|
"27" => "27 - 255.255.255.224", |
|
115
|
|
|
"28" => "28 - 255.255.255.240", |
|
116
|
|
|
"29" => "29 - 255.255.255.248", |
|
117
|
|
|
"30" => "30 - 255.255.255.252", |
|
118
|
|
|
"31" => "31 - 255.255.255.254", |
|
119
|
|
|
"32" => "32 - 255.255.255.255", |
|
120
|
|
|
]; |
|
121
|
|
|
krsort($arrMasks, SORT_NUMERIC); |
|
122
|
|
|
return $arrMasks; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|