-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIp.php
326 lines (253 loc) · 8.28 KB
/
Ip.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<?php
declare(strict_types=1);
/*
* This file is part of the QuidPHP package <https://quidphp.com>
* Author: Pierre-Philippe Emond <[email protected]>
* License: https://github.com/quidphp/base/blob/master/LICENSE
*/
namespace Quid\Base;
// ip
// class with static methods to work with IP strings
final class Ip extends Root
{
// config
protected static array $config = [
'allowed'=>[ // option par défaut pour la méthode allowed
'whiteList'=>null,'blackList'=>null,'range'=>true,'level'=>null],
'range'=>'*', // caractère pour range
'reformat'=>[ // défini le nombre de groupe de 256 ips par niveaux
20=>16,
21=>8,
22=>4,
23=>2,
24=>1]
];
// is
// retourne vrai si la valeur est un ip
final public static function is($value):bool
{
return Validate::regex('ip',$value);
}
// isLocal
// retourne vrai si l'ip est local
final public static function isLocal($value):bool
{
return self::is($value) && ($value === '127.0.0.1' || strpos($value,'192.168.') === 0);
}
// normalize
// normalize le ip, par exemple si c'est ::1
// si invalide retourne 0.0.0.0
final public static function normalize($value):string
{
$return = '0.0.0.0';
if(is_string($value))
{
if($value === '::1')
$return = '127.0.0.1';
elseif(self::is($value))
$return = $value;
}
return $return;
}
// allowed
// retourne vrai si le ip passe le test du whitelist et blacklist
// si option est un tableau indexé, c'est un whitelist
final public static function allowed(string $value,?array $option=null):bool
{
$return = false;
if(Arr::isIndexed($option))
$option = ['whiteList'=>$option];
$option = Arr::plus(self::$config['allowed'],$option);
if(!empty($option['whiteList']) && !is_array($option['whiteList']))
$option['whiteList'] = (array) $option['whiteList'];
if(!empty($option['blackList']) && !is_array($option['blackList']))
$option['blackList'] = (array) $option['blackList'];
if(self::is($value) && is_bool($option['range']))
{
$return = true;
if(is_array($option['whiteList']) && !self::in($value,$option['whiteList'],$option['range'],$option['level']))
$return = false;
elseif(is_array($option['blackList']) && self::in($value,$option['blackList'],$option['range'],$option['level']))
$return = false;
}
return $return;
}
// compareRange
// retourne vrai si le ip est dans le range
// le range identifié par le caractère *
final public static function compareRange(string $value,string $range):bool
{
$return = false;
$value = self::explode($value);
$range = self::explode($range);
if(!empty($value) && !empty($range) && !empty(self::$config['range']))
{
$return = true;
foreach ($value as $k => $v)
{
if(!is_numeric($v) || ($range[$k] !== self::$config['range'] && $range[$k] !== $v))
{
$return = false;
break;
}
}
}
return $return;
}
// compareLevel
// compare deux valeurs ip
// level permet de spécifier jusqu'à quel hauteur ils doivent être identique (1 à 4)
final public static function compareLevel(string $value,string $compare,int $level=2):bool
{
$return = false;
if($level >= 1 && $level <= 4)
{
$value = self::explode($value);
$compare = self::explode($compare);
if(!empty($value) && !empty($compare))
{
$return = true;
$i = 0;
while ($i < $level)
{
if(!is_numeric($value[$i]) || $value[$i] !== $compare[$i])
{
$return = false;
break;
}
$i++;
}
}
}
return $return;
}
// in
// retourne vrai si le ip est dans la list
// un test via compareRange et compareLevel peut être fait
final public static function in(string $value,array $array,bool $range=true,?int $level=null):bool
{
$return = false;
if(self::is($value))
{
if(in_array($value,$array,true))
$return = true;
else
{
foreach ($array as $ip)
{
if($range === true && self::compareRange($value,$ip))
{
$return = true;
break;
}
elseif(is_int($level) && self::compareLevel($value,$ip,$level))
{
$return = true;
break;
}
}
}
}
return $return;
}
// reformat
// permet de reformater un ip dont la dernière partie est de type 0/22 ou 0/24
// représente des groupes de 256 ips
final public static function reformat(string $value):?array
{
$return = null;
$explode = self::explode($value);
if(!empty($explode) && count($explode) === 4)
{
$x = explode('/',$explode[3]);
$start = $explode[2];
unset($explode[2]);
unset($explode[3]);
$explode = Arr::cast($explode);
$x = Arr::cast($x);
if(count($x) === 2 && $x[0] === 0 && is_int($x[1]) && array_key_exists($x[1],self::$config['reformat']))
{
$return = [];
$i = 0;
$group = self::$config['reformat'][$x[1]];
while ($i < $group)
{
$ip = $explode;
$ip[] = ($start + $i);
$ip[] = '*';
$implode = self::implode($ip);
if(!empty($implode))
$return[] = $implode;
$i++;
}
}
}
return $return;
}
// reformats
// permet de reformater plusieurs ips dont la dernière partie est de type 0/22 ou 0/24
// un tableau multidimensionnel est retourné
final public static function reformats(string ...$values):array
{
$return = [];
foreach ($values as $value)
{
$return[$value] = self::reformat($value);
}
return $return;
}
// reformatsUnique
// permet de reformater plusieurs ips dont la dernière partie est de type 0/22 ou 0/24
// un tableau unidimensionnel avec les ips unique sont retournés
final public static function reformatsUnique(string ...$values):array
{
$return = [];
foreach ($values as $value)
{
$reformat = self::reformat($value);
if(!empty($reformat))
$return = Arr::mergeUnique($return,$reformat);
}
return $return;
}
// toLong
// convertit une ip en int
final public static function toLong(string $value):?int
{
$return = null;
$long = ip2long($value);
if(is_int($long) && !empty($long))
$return = $long;
return $return;
}
// fromLong
// retourne une string ip à partir d'un int long
final public static function fromLong(int $long):?string
{
$return = null;
$ip = long2ip($long);
if(is_string($ip) && !empty($ip))
$return = $ip;
return $return;
}
// explode
// explode un ip et retourne un tableau à 4 clés
final public static function explode(string $value):?array
{
$return = null;
$explode = explode('.',$value);
if(count($explode) === 4)
$return = $explode;
return $return;
}
// implode
// implode un tableau à 4 clés en un chaîne
final public static function implode(array $value):?string
{
$return = null;
if(count($value) === 4)
$return = implode('.',$value);
return $return;
}
}
?>