forked from rlanvin/php-ip
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathip.lib.php
More file actions
executable file
·1085 lines (957 loc) · 23.9 KB
/
ip.lib.php
File metadata and controls
executable file
·1085 lines (957 loc) · 23.9 KB
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Licensed under the MIT license.
*
* For the full copyright and license information, please view the LICENSE file.
*
* @author Rémi Lanvin <remi@cloudconnected.fr>
* @link https://github.com/rlanvin/php-ip
*/
if ( ! function_exists('gmp_shiftl') ) {
/**
* Shift left (<<)
* @link http://www.php.net/manual/en/ref.gmp.php#99788
*/
function gmp_shiftl($x, $n)
{
return gmp_mul($x, gmp_pow('2', $n));
}
}
if ( ! function_exists('gmp_shiftr') ) {
/**
* Shift right (>>)
* @link http://www.php.net/manual/en/ref.gmp.php#99788
*/
function gmp_shiftr($x, $n)
{
return gmp_div($x, gmp_pow('2', $n));
}
}
/**
* Base class to manipulate an IP address.
*/
abstract class IP
{
/**
* Internal representation of the IP as a numeric format.
* For IPv4, this will be an SIGNED int (32 bits).
* For IPv6, this will be a GMP ressource (128 bits big int).
* @var mixed
*/
protected $ip;
/**
* @var bool
*/
protected $is_private;
/**
* Take an IP string/int and return an object of the correct type.
*
* Either IPv4 or IPv6 may be supplied, but integers less than 2^32 will
* be considered to be IPv4 by default.
*
* @param $ip mixed Anything that can be converted into an IP (string, int, bin, etc.)
* @return IPv4 or IPv6
*/
public static function create($ip)
{
try {
return new IPv4($ip);
} catch ( InvalidArgumentException $e ) {
// do nothing
}
try {
return new IPv6($ip);
} catch ( InvalidArgumentException $e ) {
// do nothing
}
throw new InvalidArgumentException("$ip does not appear to be an IPv4 or IPv6 address");
}
/**
* Return human readable representation of the IP (e.g. 127.0.0.1 or ::1)
*
* @return string
*/
abstract public function humanReadable();
/**
* Return numeric representation of the IP in base $base.
*
* The return value is a PHP string. It can base used for comparaison.
*
* @param $base int from 2 to 36
* @return string
*/
public function numeric($base = 10)
{
if ( $base < 2 || $base > 36 ) {
throw new InvalidArgumentException("Base must be between 2 and 36 (included)");
}
$value = gmp_strval($this->ip, $base);
// fix for newer versions of GMP (> 5.0) in PHP 5.4+ that removes
// the leading 0 in base 2
if ( $base == 2 ) {
$n = constant("$this->class::NB_BITS"); // ugly, but necessary because of PHP 5.2
$value = str_pad($value, $n, '0', STR_PAD_LEFT);
}
return $value;
}
/**
* Return binary string representation
*
* @todo could be optimized with pack() instead?
*
* @return string Binary string
*/
public function binary()
{
return inet_pton($this->humanReadable());
}
/**
* Bitwise AND
*
* @param $value mixed anything that can be converted into an IP object
* @return IP
*/
public function bit_and($value)
{
if ( ! $value instanceof self ) {
$value = new $this->class($value);
}
return new $this->class(gmp_and($this->ip, $value->ip));
}
/**
* Bitwise OR
*
* @param $value mixed anything that can be converted into an IP object
* @return IP
*/
public function bit_or($value)
{
if ( ! $value instanceof self ) {
$value = new $this->class($value);
}
return new $this->class(gmp_or($this->ip, $value->ip));
}
/**
* Plus (+)
*
* @throws OutOfBoundsException
* @param $value mixed anything that can be converted into an IP object
* @return IP
*/
public function plus($value)
{
if ( $value < 0 ) {
return $this->minus(-1*$value);
}
if ( $value == 0 ) {
return clone $this;
}
if ( ! $value instanceof self ) {
$value = new $this->class($value);
}
$result = gmp_add($this->ip, $value->ip);
if ( gmp_cmp($result,0) < 0 || gmp_cmp($result, constant("$this->class::MAX_INT")) > 0 ) {
throw new OutOfBoundsException();
}
return new $this->class($result);
}
/**
* Minus(-)
*
* @throws OutOfBoundsException
* @param $value mixed anything that can be converted into an IP object
* @return IP
*/
public function minus($value)
{
if ( $value < 0 ) {
return $this->plus(-1*$value);
}
if ( $value == 0 ) {
return clone $this;
}
if ( ! $value instanceof self ) {
$value = new $this->class($value);
}
$result = gmp_sub($this->ip, $value->ip);
if ( gmp_cmp($result,0) < 0 || gmp_cmp($result, constant("$this->class::MAX_INT")) > 0 ) {
throw new OutOfBoundsException();
}
return new $this->class($result);
}
/**
* @see humanReadable()
*/
public function __toString()
{
return $this->humanReadable();
}
/**
* Return the version number (4 or 6).
*
* Note: this is left abstract because there is not late static binding
* in PHP 5.2 (which I need to support).
*
* @return int
*/
abstract public function getVersion();
/**
* Check if the IP is contained in given block.
*
* @param $block mixed Anything that can be converted into an IPBlock
* @return bool
*/
public function isIn($block)
{
if ( ! $block instanceof IPBlock ) {
$block = IPBlock::create($block);
}
return $block->contains($this);
}
abstract public function isPrivate();
/**
* Return true if the address is allocated for public networks
*
* @return bool
*/
public function isPublic()
{
return ! $this->isPrivate();
}
}
/**
* Licensed under the MIT license.
*
* For the full copyright and license information, please view the LICENSE file.
*
* @author Rémi Lanvin <remi@cloudconnected.fr>
* @link https://github.com/rlanvin/php-ip
*/
/**
* Class to manipulate IPv4
*/
class IPv4 extends IP
{
const IP_VERSION = 4;
const MAX_INT = '4294967295';
const NB_BITS = 32;
/**
* Workaround for lack of late static binding in PHP 5.2
* so I can use "new $this->class()"" instead of "new static()"
*/
protected $class = __CLASS__;
public function getVersion()
{
return self::IP_VERSION;
}
/**
* Constructor tries to guess what is the $ip
*
* @param $ip mixed String, binary string, int or float
*/
public function __construct($ip)
{
if ( is_int($ip) ) {
// if an integer is provided, we have to be careful of the architecture
// on 32 bits plateform, it's always a valid IP
// on 64 bits plateform, we have to test the value
$ip = gmp_init(sprintf('%u',$ip),10);
if ( gmp_cmp($ip, self::MAX_INT) > 0 ) {
throw new InvalidArgumentException(sprintf('The integer %s is not a valid IPv4 address', gmp_strval($ip)));
}
$this->ip = $ip;
}
elseif ( is_float($ip) && floor($ip) == $ip ) {
// float (or double) with an integer value
$ip = gmp_init(sprintf('%s',$ip), 10);
if ( gmp_cmp($ip, 0) < 0 || gmp_cmp($ip, self::MAX_INT) > 0 ) {
throw new InvalidArgumentException(sprintf('The double %s is not a valid IPv4 address', gmp_strval($ip)));
}
$this->ip = $ip;
}
elseif ( is_string($ip) ) {
// binary string
if ( ! ctype_print($ip) ) {
if ( strlen($ip) != 4 ) {
throw new InvalidArgumentException("The binary string is not a valid IPv4 address");
}
$hex = unpack('H*',$ip);
$this->ip = gmp_init($hex[1],16);
}
// human readable IPv4
elseif ( filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) ) {
$this->ip = gmp_init(sprintf('%u',ip2long($ip)));
}
// numeric string (decimal)
elseif ( ctype_digit($ip) ) {
$ip = gmp_init($ip);
if ( gmp_cmp($ip, self::MAX_INT) > 0 ) {
throw new InvalidArgumentException(sprintf("%s is not a valid decimal IPv4 address", gmp_strval($ip)));
}
$this->ip = $ip;
}
else {
throw new InvalidArgumentException("$ip is not a valid IPv4 address");
}
}
elseif ( (is_resource($ip) && get_resource_type($ip) == 'GMP integer') || $ip instanceof GMP ) {
if ( gmp_cmp($ip, 0) < 0 || gmp_cmp($ip, self::MAX_INT) > 0 ) {
throw new InvalidArgumentException(sprintf("%s is not a valid decimal IPv4 address", gmp_strval($ip)));
}
$this->ip = $ip;
}
else {
throw new InvalidArgumentException("Unsupported argument type: ".gettype($ip));
}
}
/**
* Returns human readable representation of the IP
*
* @param $compress bool Wether to compress IPv4 or not
* @return string
*/
public function humanReadable($compress = true)
{
if ( $compress ) {
$ip = long2ip(intval(doubleval($this->numeric())));
}
else {
$hex = $this->numeric(16);
$hex = str_pad($hex, 8, '0', STR_PAD_LEFT);
$segments = str_split($hex, 2);
foreach ( $segments as & $s ) {
$s = str_pad(base_convert($s,16,10), 3, '0', STR_PAD_LEFT);
}
$ip = implode('.',$segments);
}
return $ip;
}
/**
* Return true if the address is reserved per iana-ipv4-special-registry
*/
public function isPrivate()
{
if ( $this->is_private === null ) {
$this->is_private =
$this->isIn('0.0.0.0/8') ||
$this->isIn('10.0.0.0/8') ||
$this->isIn('127.0.0.0/8') ||
$this->isIn('169.254.0.0/16') ||
$this->isIn('172.16.0.0/12') ||
$this->isIn('192.0.0.0/29') ||
$this->isIn('192.0.0.170/31') ||
$this->isIn('192.0.2.0/24') ||
$this->isIn('192.168.0.0/16') ||
$this->isIn('198.18.0.0/15') ||
$this->isIn('198.51.100.0/24') ||
$this->isIn('203.0.113.0/24') ||
$this->isIn('240.0.0.0/4') ||
$this->isIn('255.255.255.255/32');
}
return $this->is_private;
}
}
/**
* Licensed under the MIT license.
*
* For the full copyright and license information, please view the LICENSE file.
*
* @author Rémi Lanvin <remi@cloudconnected.fr>
* @link https://github.com/rlanvin/php-ip
*/
/**
* Class to manipulate IPv6
*
* Addresses are stored internally as GMP ressource (big int).
*/
class IPv6 extends IP
{
const IP_VERSION = 6;
const MAX_INT = '340282366920938463463374607431768211455';
const NB_BITS = 128;
/**
* Workaround for lack of late static binding in PHP 5.2
* so I can use "new $this->class()"" instead of "new static()"
*/
protected $class = __CLASS__;
public function getVersion()
{
return self::IP_VERSION;
}
/**
* Constuctor tries to guess what is $ip.
*
* @param mixed $ip
*/
public function __construct($ip)
{
if ( is_int($ip) ) {
// always a valid IP, since even in 64bits plateform, it's less than max value
$this->ip = gmp_init(sprintf('%u',$ip),10);
}
elseif ( is_float($ip) && floor($ip) == $ip ) {
// float (or double) with an integer value
$ip = gmp_init(sprintf('%s',$ip), 10);
if ( gmp_cmp($ip, 0) < 0 || gmp_cmp($ip, self::MAX_INT) > 0 ) {
throw new InvalidArgumentException(sprintf('The double %s is not a valid IPv6 address', gmp_strval($ip)));
}
$this->ip = $ip;
}
elseif ( is_string($ip) ) {
// binary string
if ( ! ctype_print($ip) ) {
// probably the result of inet_pton
// must be 16 bytes exactly to be valid
if ( strlen($ip) != 16 ) {
throw new InvalidArgumentException("The binary string is not a valid IPv6 address");
}
$hex = unpack('H*',$ip);
$this->ip = gmp_init($hex[1],16);
}
// valid human readable representation
elseif ( filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) ) {
$ip = inet_pton($ip);
$hex = unpack('H*',$ip);
$this->ip = gmp_init($hex[1],16);
}
// numeric string (decimal)
elseif ( ctype_digit($ip) ) {
$ip = gmp_init($ip, 10);
if ( gmp_cmp($ip, '340282366920938463463374607431768211455') > 0 ) {
throw new InvalidArgumentException(sprintf("%s is not a valid decimal IPv6 address", gmp_strval($ip)));
}
$this->ip = $ip;
}
else {
throw new InvalidArgumentException("$ip is not a valid IPv6 address");
}
}
elseif ( (is_resource($ip) && get_resource_type($ip) == 'GMP integer') || $ip instanceof GMP ) {
if ( gmp_cmp($ip, 0) < 0 || gmp_cmp($ip, self::MAX_INT) > 0 ) {
throw new InvalidArgumentException(sprintf("%s is not a valid decimal IPv6 address", gmp_strval($ip)));
}
$this->ip = $ip;
}
else {
throw new InvalidArgumentException("Unsupported argument type: ".gettype($ip));
}
}
/**
* Returns human readable representation of the IP
*
* @param $compress bool Wether to compress IPv6 or not
* @return string
*/
public function humanReadable($compress = true)
{
$hex = $this->numeric(16);
$hex = str_pad($hex, 32, '0', STR_PAD_LEFT);
$bytes = str_split($hex,4);
$ip = implode(':',$bytes);
if ( $compress ) {
$ip = @ inet_ntop(@ inet_pton($ip));
}
return $ip;
}
/**
* Return true if the address is reserved per iana-ipv6-special-registry
*/
public function isPrivate()
{
if ( $this->is_private === null ) {
$this->is_private =
$this->isIn('::1/128') ||
$this->isIn('::/128') ||
$this->isIn('::ffff:0:0/96') ||
$this->isIn('100::/64') ||
$this->isIn('2001::/23') ||
$this->isIn('2001:2::/48') ||
$this->isIn('2001:db8::/32') ||
$this->isIn('2001:10::/28') ||
$this->isIn('fc00::/7') ||
$this->isIn('fe80::/10');
}
return $this->is_private;
}
}
/**
* Licensed under the MIT license.
*
* For the full copyright and license information, please view the LICENSE file.
*
* @author Rémi Lanvin <remi@cloudconnected.fr>
* @link https://github.com/rlanvin/php-ip
*/
/**
* Base class to manipulate CIDR block (aka "networks").
*/
abstract class IPBlock implements Iterator, ArrayAccess, Countable
{
/**
* @var IP
*/
protected $first_ip;
/**
* @var IP
*/
protected $last_ip;
/**
* @var int
*/
protected $prefix;
/**
* @var IP
*/
protected $mask;
/**
* @var IP
*/
protected $delta;
/**
* @var Numeric string
*/
protected $nb_addresses;
/**
* Return netmask
*
* @return IPv6
*/
public function getMask()
{
if ( $this->mask === null ) {
if ( $this->prefix == 0 ) {
$this->mask = new $this->ip_class(0);
}
else {
$max_int = gmp_init(constant("$this->ip_class::MAX_INT"));
$mask = gmp_shiftl($max_int, constant("$this->ip_class::NB_BITS") - $this->prefix);
$mask = gmp_and($mask, $max_int); // truncate to 128 bits only
$this->mask = new $this->ip_class($mask);
}
}
return $this->mask;
}
/**
* Return delta to last IP address
*
* @return IPv6
*/
public function getDelta()
{
if ( $this->delta === null ) {
if ( $this->prefix == 0 ) {
$this->delta = new $this->ip_class(constant("$this->ip_class::MAX_INT"));
}
else {
$this->delta = new $this->ip_class(gmp_sub(gmp_shiftl(1, constant("$this->ip_class::NB_BITS") - $this->prefix),1));
}
}
return $this->delta;
}
/**
* Factory method.
*/
public static function create($ip, $prefix = '')
{
try {
return new IPv4Block($ip, $prefix);
} catch ( InvalidArgumentException $e ) {
// do nothing
}
try {
return new IPv6Block($ip, $prefix);
} catch ( InvalidArgumentException $e ) {
// do nothing
}
throw new InvalidArgumentException("$ip does not appear to be an IPv4 or IPv6 block");
}
/**
* Accepts a CIDR string (e.g. 192.168.0.0/24) or an IP and a prefix as
* two separate parameters
*
* @param $ip mixed IP or CIDR string
* @param $prefix int (optional) The "slash" part
*/
public function __construct($ip_or_cidr, $prefix = '')
{
$ip = $ip_or_cidr;
if ( strpos($ip_or_cidr, '/') !== false ) {
list($ip, $prefix) = explode('/', $ip_or_cidr, 2);
}
if ( ! $ip instanceof IP ) {
$ip = new $this->ip_class($ip);
}
$this->checkPrefix($prefix);
$this->prefix = (int) $prefix;
$this->first_ip = $ip->bit_and($this->getMask());
$this->last_ip = $this->first_ip->bit_or($this->getDelta());
}
public function __toString()
{
return (string) $this->first_ip.'/'.$this->prefix;
}
/**
* Returns the prefix (the slash part)
*
* @return int
*/
public function getPrefix()
{
return $this->prefix;
}
public function getMaxPrefix()
{
return constant("$this->ip_class::NB_BITS");
}
public function getVersion()
{
return constant("$this->ip_class::IP_VERSION");
}
public function plus($value)
{
if ( $value < 0 ) {
return $this->minus(-1*$value);
}
if ( ! is_int($value) ) {
throw new InvalidArgumentException('plus() takes an integer');
}
if ( $value == 0 ) {
return clone $this;
}
// check boundaries
try {
$first_ip = $this->first_ip->plus(gmp_mul($value, $this->getNbAddresses()));
return new $this->class(
$first_ip,
$this->prefix
);
} catch ( InvalidArgumentException $e ) {
throw new OutOfBoundsException($e->getMessage());
}
}
public function minus($value)
{
if ( $value < 0 ) {
return $this->plus(-1*$value);
}
if ( ! is_int($value) ) {
throw new InvalidArgumentException('plus() takes an integer');
}
if ( $value == 0 ) {
return clone $this;
}
// check boundaries
try {
$first_ip = $this->first_ip->minus(gmp_mul($value, $this->getNbAddresses()));
return new $this->class(
$first_ip,
$this->prefix
);
} catch ( InvalidArgumentException $e ) {
throw new OutOfBoundsException($e->getMessage());
}
}
/**
* Returns the first IP address of the block.
*
* @return IP
*/
public function getFirstIp()
{
return $this->first_ip;
}
/**
* Returns the last IP address of the block.
*
* @return IP
*/
public function getLastIp()
{
return $this->last_ip;
}
/**
* Returns the Network IP address of the block (the first address).
*
* @see getFirstIp
* @return IP
*/
public function getNetworkAddress()
{
return $this->first_ip;
}
/**
* Returns the Broadcast IP address of the block (the last address).
*
* @see getLastIp
* @return IP
*/
public function getBroadcastAddress()
{
return $this->last_ip;
}
/**
* @internal
* Check if the prefix is valid
*
* @throws InvalidArgumentException
*/
protected function checkPrefix($prefix)
{
if ( $prefix === '' || $prefix === null || $prefix === false || $prefix < 0 || $prefix > $this->getMaxPrefix() ) {
throw new InvalidArgumentException(sprintf(
"Invalid IPv%s block prefix '%s'",
$this->getVersion(),
$prefix
));
}
}
/**
* Split the block into smaller blocks.
*
* Returns an iterator, use foreach to loop it and count to get number of subnets.
*
* @return IPBlockIterator
*/
public function getSubblocks($prefix)
{
$prefix = ltrim($prefix,'/');
$this->checkPrefix($prefix);
if ( $prefix <= $this->prefix ) {
throw new InvalidArgumentException("Prefix must be smaller than {$this->prefix} ($prefix given)");
}
$first_block = new $this->class($this->first_ip, $prefix);
$number_of_blocks = gmp_pow(2, $prefix - $this->prefix);
return new IPBlockIterator($first_block, $number_of_blocks);
}
/**
* Return the superblock containing the current block.
*
* @return IPBlock
*/
public function getSuper($prefix)
{
$prefix = ltrim($prefix,'/');
$this->checkPrefix($prefix);
if ( $prefix >= $this->prefix ) {
throw new InvalidArgumentException("Prefix must be bigger than {$this->prefix} ($prefix given)");
}
return new $this->class($this->first_ip, $prefix);
}
/**
* Determine if the current block contains an IP address or block.
*
* @param $ip_or_block mixed
* @return bool
*/
public function contains($ip_or_block)
{
if ( (is_string($ip_or_block) && strpos($ip_or_block,'/') !== false) || $ip_or_block instanceof IPBlock ) {
return $this->containsBlock($ip_or_block);
}
else {
return $this->containsIP($ip_or_block);
}
}
/**
* Determine if the current block contains an IP address
*
* @param $ip mixed
* @return bool
*/
public function containsIP($ip)
{
if ( ! $ip instanceof IP ) {
$ip = IP::create($ip);
}
return ($ip->numeric() >= $this->getFirstIp()->numeric()) && ($ip->numeric() <= $this->getLastIp()->numeric());
}
/**
* Determine if the current block contains another block.
*
* True in this situation:
* $this: first_ip[ ]last_ip
* $block: first_ip[ ]last_ip
*
* @param $ip mixed
* @return bool
*/
public function containsBlock($block)
{
if ( ! $block instanceof IPBlock ) {
$block = new $this->class($block);
}
return $block->getFirstIp()->numeric() >= $this->first_ip->numeric() && $block->getLastIp()->numeric() <= $this->last_ip->numeric();
}
/**
* Determine if the current block is contained in another block.
*
* @param $block mixed
* @return bool
*/
public function isIn($block)
{
if ( ! $block instanceof IPBlock ) {
$block = new $this->class($block);
}
return $block->containsBlock($this);
}
/**
* Test is the two blocks overlap, i.e. if block1 contains block2, or block2 contains block1
*
* @param $block mixed
* @return bool
*/
public function overlaps($block)
{
if ( ! $block instanceof IPBlock ) {
$block = new $this->class($block);
}
return ! ($block->getFirstIp()->numeric() > $this->last_ip->numeric() || $block->getLastIp()->numeric() < $this->first_ip->numeric());
}
/**
* Return the number of IP addresses in the block.
*
* @return string numeric string (can be huge)
*/
public function getNbAddresses()
{
if ( $this->nb_addresses === null ) {
$this->nb_addresses = gmp_strval(gmp_pow(2, $this->getMaxPrefix() - $this->prefix));
}
return $this->nb_addresses;
}
// Countable
public function count()
{
$n = $this->getNbAddresses();
if ( $n > PHP_INT_MAX ) {
throw new RuntimeException('The number of addresses is bigger than PHP_INT_MAX, use getNbAddresses() instead');
}
return $n;
}
// Iterator
protected $position = 0;
public function rewind()
{
$this->position = gmp_init(0);
}
public function current()
{
return $this->first_ip->plus(gmp_strval($this->position));
}
public function key()
{
return $this->position;
}
public function next()
{
$this->position = gmp_add($this->position,1);
}
public function valid()
{
return gmp_cmp($this->position,0) >= 0 && gmp_cmp($this->position, $this->getNbAddresses()) < 0;
}
// ArrayAccess
public function offsetExists($offset)
{
return gmp_cmp($offset,0) >= 0 && gmp_cmp($offset, $this->getNbAddresses()) < 0;
}
public function offsetGet($offset)
{
if ( ! $this->offsetExists($offset) ) {
throw new OutOfBoundsException("Offset $offset does not exists");
}
return $this->first_ip->plus($offset);
}
public function offsetSet($offset, $value)
{
throw new LogicException('Setting IP in block is not supported');
}
public function offsetUnset($offset)
{
throw new LogicException('Unsetting IP in block is not supported');
}
}
/**
* Licensed under the MIT license.
*
* For the full copyright and license information, please view the LICENSE file.
*
* @author Rémi Lanvin <remi@cloudconnected.fr>
* @link https://github.com/rlanvin/php-ip
*/
/**
* An IPv4 CIDR block
*/