-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipmanager.class.php
1379 lines (1320 loc) · 35.5 KB
/
ipmanager.class.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
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
/*
* SET ERROR REPORTING LEVEL
*/ /***/
$level = '0' ;
if(empty($level) || ($level == '')) {
define('ERROR_LEVEL',E_ALL);
} else {
define('ERROR_LEVEL',$level);
}
error_reporting(ERROR_LEVEL);
/*
* Other configurable options
*/
define('CURRENT_DATE_FORMAT' , "Y-m-d H:i:s" );
define('ERROR_LOG_LOCATION' , 'error_log.log' );
define('DEBUG_DEFAULT' , 0 ); // Toggles global log debugging. If true(1), all provided information will be logged. False(0) will disable logging.
class Error {
public function __construct()
{
$this->error_level = ERROR_LEVEL;
$this->log_location = ERROR_LOG_LOCATION;
$this->date_format = CURRENT_DATE_FORMAT;
}
public function log($message,$function=null,$line=null,$die=0)
{
$message = date(CURRENT_DATE_FORMAT);
foreach(func_get_args() as $k=>$v) {
if(!empty($v) || ($v != '')) {
if($k != 3) {
$message .= ' - ' . $v;
}
}
}
if(!error_log($message . "\r\n",3,ERROR_LOG_LOCATION)) {
die("Log entry unable to be created. Please check all settings.");
}
if($die!=1) {
trigger_error($message);
} else if($die = 1) {
die($message);
}
}
public function fullHalt($message,$function=null,$line=null)
{
if(!error::log($message,$function,$line,1)) {
die($message);
}
}
}
class DB {
public function __construct()
{
$this->error = new Error;
$this->user = 'root';
$this->pass = '';
$this->host = 'localhost';
$this->target_db = 'ip';
try {
if(!$this->_connect(true)) {
throw new Exception("Connection to '$this->host' failed. Error: " . self::getError());
}
if(!$this->_select($this->target_db)) {
throw new Exception("Connection to database '$this->target_db' failed. Error: " . self::getError());
}
} catch (Exception $e) {
error::fullHalt($e->getMessage(),__FUNCTION__,__LINE__);
}
}
final private function _connect()
{
if(mysql_connect($this->host,$this->user,$this->pass)) {
return true;
}
return false;
}
final private function _select($db)
{
if(mysql_select_db($db)) {
return true;
}
return false;
}
final public function prepare($var)
{
if(is_array($var)) {
foreach($var as $k=>$v) {
$ret[$k] = mysql_real_escape_string($v);
}
} else {
$ret = mysql_real_escape_string($var);
}
return $ret;
}
public function query($sql,$debug=0,$unsafe=0)
{
if($unsafe == 0) {
$sql = self::prepare($sql);
}
if($debug == 1) {
print "$sql <br />";
}
$data = array();
if($q = mysql_query($sql)) {
$ins_id = @self::getInsertId();
if(!empty($ins_id) || ($ins_id != '')) {
return $ins_id;
} else {
$rows = @self::numRows($q);
if($rows == 1) {
return @mysql_fetch_assoc($q);
} else {
while($row = @mysql_fetch_assoc($q)) {
$data[] = $row;
}
}
return $data;
}
}
return false;
}
public function getInsertId()
{
return mysql_insert_id();
}
public function getError()
{
return mysql_error();
}
public function numRows($id)
{
return mysql_num_rows($id);
}
public function countFieldsInTable($name)
{
$q = self::query("SELECT * FROM `$name`");
return count($q);
}
}
class IPManager {
public function __construct()
{
//$db = new DB;
//$this->error = new Error;
}
/**
* Logs provided data provided by user or function, according to defined constant for "DEBUG_DEFAULT"
*
* @param type: string
* @param message: string
* @param debug: bool
* @return bool
*/
function log($type,$message,$debug = DEBUG_DEFAULT)
{
if($debug == 1) {
log_message($type,$message);
}
return;
}
/**
* Checks validity of provided IPv4 or IPv6 address.
*
* @param string -> IP address to be validated
* @return bool -> Tells calling script whether or not IP is valid.
*/
public function isValidIP($ip)
{
$ip = self::expandIP($ip);
if(filter_var($ip,FILTER_VALIDATE_IP)) {
return true;
} else {
return false;
}
}
/**
* @param string $ip
* @return boolean
*
* Detects whether or not the IP provided is a valid IP to be the first in an allocation.
* i.e. 192.168.1.11/28 is not a valid IP block, as it starts on an odd number not divisible by 8
*/
public function isValidStartIP($ip)
{
$list = self::ipToArray($ip);
$type = self::getIPType($ip);
switch($type) {
case 4:
if($list[3] == 0) {
return true;
}
if($list[3] == 255) {
return false;
}
if((($list[3]) % 8) == 0) {
return true;
} else {
return false;
}
break;
case 6:
if(hexdec($list[7]) == 0) {
return true;
}
if(hexdec($list[7]) == 65535) {
return false;
}
if((hexdec($list[7]) % 8) == 0) {
return true;
} else {
return false;
}
break;
default:
return false;
break;
}
}
/**
* @param string -> IPv4 or IPv6 address whose type will be returned (IPv4/IPv6)
* @return int -> Protocol of IP address provided
*
* Function getIPType returns the protocol of the given IP address, after checking validity of IP.
*/
final public function getIPType($ip)
{
//print "<h1>getIPType() -- IP = $ip</h1>";
if(strpos($ip,':') && !strpos($ip,'.')) {
return 6;
} else if(strpos($ip,'.') && !strpos($ip,':')) {
return 4;
} else {
return false;// Invalid ip, break process.
}
}
final public function getIPTypeFromLong($ip)
{
if(strlen($ip) == 40) {
return 6;
} else if(strlen($ip) != 40) {
return 4;
}
}
/***
* ************* BORROWED FUNCTIONS FROM FORMER CLASS.IPMANAGER.PHP ***************
*/
/***
* Replaces broken ip2long() function of PHP.
*
* NOTE: Fixed error in original logic of function.
* ERROR:
*
* @param string $a
* @return int
*/
function inet_aton($a) {
$inet = 0.0;
if (count($t = explode(".", $a)) != 4) {
return false;
}
for ($i = 0; $i < 4; $i++) {
$inet *= 256.0;
$inet += $t[$i];
};
return $inet;
}
/***
* Replaces broken long2ip() function of PHP.
*
* @param int $n
* @return string
*/
function inet_ntoa($n) {
$t=array(0,0,0,0);
$msk = 16777216.0;
$n += 0.0;
if ($n < 1)
return('0.0.0.0');
for ($i = 0; $i < 4; $i++) {
$k = (int) ($n / $msk);
$n -= $msk * $k;
$t[$i]= $k;
$msk /=256.0;
};
$a=join('.', $t);
return($a);
}
/***
* @param float $ip
* @return int
*
* Function ipToLong is a more secure, accurate form of ip2long(), a core PHP function.
*/
public function ipToLong($ip)
{
if(!self::isValidIP($ip)) {
return false;
}
$ip = self::expandIP($ip);
$type = self::getIPType($ip);
switch($type) {
case 4:
return self::inet_aton($ip);
break;
case 6:
$list = self::ipToArray($ip);
foreach($list as $k=>$v) {
$list[$k] = hexdec($v);
$list[$k] = str_pad($list[$k],5,0,STR_PAD_LEFT);
}
return implode('',$list);
break;
default:
return false; // Invalid IP, would be caught long before this.
break;
}
return false;
}
public function longToIP($ip)
{
$type = self::getIPTypeFromLong($ip);
switch($type) {
case 4:
return self::inet_ntoa($ip);
break;
case 6:
$list = str_split($ip,5);
foreach($list as $k=>$v) {
$list[$k] = dechex($v);
}
return self::shrinkIP(implode(':',$list));
break;
default:
return false; // Invalid IP, would be caught long before this.
}
return false;
}
/***
* @param string $ip
* @return string
*
* Function expandIP ensures that all IP addresses fed to it are properly identified and adhere to a
* standardized format.
*/
public function expandIP($ip,$show=null)
{
$ip = strtolower($ip);
$type = self::getIPType($ip);
if($type==6) {
// Check for abbreviated IPv6 address, such as 4a2b::1234:5678
if(strpos($ip,"::")) {
if(substr_count($ip,'::') > 1) {
// throw error, invalid IP
error::log('Invalid IP: More than 1 abbreviated block.',__FUNCTION__,__LINE__);
return false;
} else {
$ip = self::unfoldIPv6($ip);
}
}
}
$list = self::ipToArray($ip,true);
switch(self::getIPType($ip)) {
case 4:
$part_length = 3;
break;
case 6:
$part_length = 4;
break;
}
if($list == '') {
return false;
}
foreach($list as $k=>$v) {
if(strlen($v) < $part_length) {
// Adds zeroes to the left of number (192.1.x.x becomes 192.001.x.x)
$list[$k] = str_pad($v,$part_length,"0",STR_PAD_LEFT);
}
}
$final = self::arrayToIP($list,$type);
if($show == 1) {
print $final;
}
return $final;
}
/***
* Take an expanded IP (1234:0000:0000:0000:0000:0000:0000:0001) and shrink to normal size (1234::1)
*/
public function shrinkIP($ip)
{
$type = self::getIPType($ip);
$list = self::ipToArray($ip);
if($list == '') {
return false;
}
if($type == 4) {
foreach($list as $k=>$v) {
$list[$k] = ltrim($v,0);
if($list[$k] == '') {
$list[$k] = 0;
}
}
$ip = self::arrayToIP($list,$type);
}
if($type == 6) {
foreach($list as $k=>$v) {
$list[$k] = ltrim($v,0);
if(strlen($list[$k]) == 0) {
$list[$k] = '0';
}
}
foreach($list as $k=>$v) {
if(($list[$k] == '0') && (!isset($start))) {
$start = $k;
}
if($k < (count($list) - 1)) {
if(($list[$k] === '0') && ($list[$k + 1] === '0')) {
$end = $k + 1;
} else if(($list[$k] == '0') && ($list[$k + 1] != '0')) {
$end = $k;
}
if(($k == (count($list) - 2)) && ($list[$k] == '0') && ($list[$k + 1] == '0')) {
$end = $k + 1;
//break;
}
}
}
$list[$start] = '';
for($i = $end;$i>$start;$i--) {
unset($list[$i]);
}
if(array_key_exists((count($list) - 1),$list)) {
if($list[count($list) - 1] == '') {
$list[count($list)] = '';
}
}
if(($type == 4) && ($list[count($list) - 1] == '')) {
$list[count($list) - 1] == 0;
}
$ip = self::arrayToIP($list,$type);
if(($ip[strlen($ip) - 1] == ':') && ($ip[strlen($ip) - 2] != ':')) {
$ip = substr_replace($ip,'',-1);
}
}
return $ip;
}
/***
* Take an abbreviated IPv6 address (i.e. 1234::5678) and fill all necessary zeroes.
*/
function unfoldIPv6($ip) {
$count = substr_count($ip,":");
if($ip[strlen($ip) - 1] == ':') {
$count = $count - 1;
}
if(($count == 2) && (strlen($ip) == 6)) {
$count = 1;
}
$pads = '';
for($i=$count;$i<8;$i++) {
$pads .= ':0000';
}
if($pads && ($ip[strlen($ip) - 1] != ':')) {
$pads .= ':';
}
return str_replace("::",$pads,$ip);
}
/***
* Create an enumerated array from a valid IP
*/
public function ipToArray($ip,$disable_checks=false)
{
if($disable_checks == false) {
$ip = self::expandIP($ip);
if(!self::isValidIP($ip)) {
error::log('Invalid IP.',__FUNCTION__,__LINE__);
return false;
}
}
$type = self::getIPType($ip);
switch($type) {
case 4:
$separator = '.';
$part_length = 3;
break;
case 6:
$separator = ':';
$part_length = 4;
break;
// No default, as anything else is invalid.
}
return @explode($separator,$ip);
}
/***
* Create an valid IP from an enumerated array
*/
public function arrayToIP($ip,$type)
{
switch($type) {
case 4:
$separator = '.';
break;
case 6:
$separator = ':';
break;
// No default, as anything else is invalid.
}
return @implode($separator,$ip);
}
/***
* Build array of all IPv4 CIDR notations (/32,/16), with corresponding numbers of IP addresses
*/
public function buildIPv4CIDRArray()
{
$cidr = array();
$cidr['/32'] = 1;
$inc = 31;
for($i = 1;$inc>=0;$i++) {
$cidr['/' . $inc] = pow(2,$i);
$inc--;
}
return $cidr;
}
/***
* Build array of all IPv6 CIDR notations (/128,/64), with corresponding numbers of IP addresses
*/
public function buildIPv6CIDRArray()
{
$cidr = array();
$cidr['/128'] = 1;
$inc = 127;
for($i = 1;$inc>=0;$i++) {
$cidr['/' . $inc] = pow(2,$i);
$inc--;
}
return $cidr;
}
/***
* Returns an integer amount of the IPs provided to function (/29 returns 8, etc.)
* NOTE: This function only accepts a CIDR notation (ex. /29 IPv4 or /112 IPv6)
*/
public function countIPsInBlock($size,$type)
{
if($type == 4) {
$cidr = self::buildIPv4CIDRArray();
} else if($type == 6) {
$cidr = self::buildIPv6CIDRArray();
}
return $cidr[$size];
}
/***
* Returns the last IP in a given block.
*
* @param string $ip
* @param string $size
* @return string
*/
public function findEndOfIPBlock($start_ip,$size)
{
$type = self::getIPType($start_ip);
$cidr = $size;
$size = self::countIPsInBlock($size,$type);
if(!self::isValidIP($start_ip)) {
error::log("Invalid IP $start_ip.",__FUNCTION__,__LINE__);
return false;
}
if(!self::isValidStartIP($start_ip)) {
return false;
}
//print self::expandIP($start_ip) . '<br />';
try {
$message = "Invalid allocation size for starting IP '$start_ip$cidr'. No cross-block allocation is allowed.";
switch($type) {
case 4:
$ip = @self::ipToArray($start_ip);
$cidr = self::buildIPv4CIDRArray();
if((($size >= $cidr['/24']) &&($size <= $cidr['/16']) && (($ip[3] > 0)))) {
throw new exception($message);
}
if((($size > $cidr['/16']) &&($size <= $cidr['/8']) && (($ip[3] > 0) || ($ip[2] > 0)))) {
throw new exception($message);
}
if($size > $cidr['/8']) {
throw new exception("IP Block exceeds maximum allowed IP allocation.");
}
if($size == $cidr['/24']) {
$ip[3] = 255;
}
if($size == $cidr['/16']) {
$ip[2] = $ip[3] = 255;
}
if($size == $cidr['/8']) {
$ip[1] = $ip[2] = $ip[3] = 255;
}
$start = self::inet_aton($start_ip);
return self::inet_ntoa((self::inet_aton($start_ip) + $size) - 1);
break;
case 6:
$ip = @self::ipToArray($start_ip);
$cidr = self::buildIPv6CIDRArray();
foreach($ip as $k=>$v) {
$ip[$k] = hexdec($v);
}
if((($size >= $cidr['/112']) && ($size <= $cidr['/96'])) && (($ip[7] > 0))) {
throw new exception($message);
}
if((($size > $cidr['/96']) && ($size <= $cidr['/80'])) && (($ip[6] > 0) || ($ip[7]) > 0)) {
throw new exception($message);
}
if((($size > $cidr['/80']) && ($size <= $cidr['/64'])) && (($ip[5] > 0) || ($ip[6] > 0) || ($ip[7]) > 0)) {
throw new exception($message);
}
if((($size > $cidr['/64']) && ($size <= $cidr['/48'])) && (($ip[4] > 0) || ($ip[5] > 0) || ($ip[6] > 0) || ($ip[7]) > 0)) {
throw new exception($message);
}
if((($size > $cidr['/48']) && ($size <= $cidr['/32'])) && (($ip[3] > 0) || ($ip[4] > 0) || ($ip[5] > 0) || ($ip[6] > 0) || ($ip[7]) > 0)) {
throw new exception($message);
}
if((($size > $cidr['/32']) && ($size <= $cidr['/16'])) && (($ip[2] > 0) || ($ip[3] > 0) || ($ip[4] > 0) || ($ip[5] > 0) || ($ip[6] > 0) || ($ip[7]) > 0)) {
throw new exception($message);
}
if((($size > $cidr['/16']) && $size <= $cidr['/0'])) {
throw new exception("IP block exceeds maximum available IP block allocation.");
}
if($size == $cidr['/96']) {
$ip[6] = $ip[7] = 65535;
}
if($size == $cidr['/80']) {
$ip[5] = $ip[6] = $ip[7] = 65535;
}
if($size == $cidr['/64']) {
$ip[4] = $ip[5] = $ip[6] = $ip[7] = 65535;
}
if($size == $cidr['/48']) {
$ip[3] = $ip[4] = $ip[5] = $ip[6] = $ip[7] = 65535;
}
if($size == $cidr['/32']) {
$ip[2] = $ip[3] = $ip[4] = $ip[5] = $ip[6] = $ip[7] = 65535;
}
if($size == $cidr['/16']) {
$ip[1] = $ip[2] = $ip[3] = $ip[4] = $ip[5] = $ip[6] = $ip[7] = 65535;
}
if($size <= $cidr['/112']) {
$ip[7] = ($ip[7] + $size) - 1;
} else if(($size > $cidr['/112']) && ($size <= $cidr['/96'])) {
$rem =($size / $cidr['/112']) - 1;
$ip[7] = 65535;
$ip[6] = $rem;
} else if(($size > $cidr['/96']) && ($size <= $cidr['/80'])) {
$rem =($size / $cidr['/96']) - 1;
$ip[6] = $ip[7] = 65535;
$ip[5] = $rem;
} else if(($size > $cidr['/80']) && ($size <= $cidr['/64'])) {
$rem =($size / $cidr['/80']) - 1;
$ip[5] = $ip[6] = $ip[7] = 65535;
$ip[4] = $rem;
} else if(($size > $cidr['/64']) && ($size <= $cidr['/48'])) {
$rem =($size / $cidr['/64']) - 1;
$ip[4] = $ip[5] = $ip[6] = $ip[7] = 65535;
$ip[3] = $rem;
} else if(($size > $cidr['/48']) && ($size <= $cidr['/32'])) {
$rem =($size / $cidr['/48']) - 1;
$ip[3] = $ip[4] = $ip[5] = $ip[6] = $ip[7] = 65535;
$ip[2] = $rem;
} else if(($size > $cidr['/32']) && ($size <= $cidr['/16'])) {
$rem =($size / $cidr['/32']) - 1;
$ip[2] = $ip[3] = $ip[4] = $ip[5] = $ip[6] = $ip[7] = 65535;
$ip[1] = $rem;
}
break;
default:
throw new exception("Invalid IP or allocation size provided.");
break;
}
} catch (exception $e) {
error::log($e->getMessage(),__FUNCTION__,__LINE__);
return false;
}
foreach($ip as $k=>$v) {
$ip[$k] = dechex($v);
}
return self::expandIP(self::arrayToIP($ip,$type));
}
/***
* Function to increment an IP by one. Used primarily in conjunction with findBestIPBlock()
*
* @param string $ip
* @return string
*/
public function findNextIP($ip)
{
$type = self::getIPType($ip);
$parts = self::ipToArray($ip);
$ip = array();
try {
switch($type) {
case 4:
$ip = array();
foreach($parts as $k=>$v) {
$ip[$k] = $v;
}
if($ip[3] == 255) {
if($ip[2] == 255) {
if($ip[1] == 255) {
if($ip[0] == 255) {
throw new exception("Invalid IP. Unable to increment by 1.");
} else {
++$ip[0];
$ip[3] = $ip[2] =$ip[1] = 0;
}
} else {
++$ip[1];
$ip[3] = $ip[2] = 0;
}
} else {
++$ip[2];
$ip[3] = 0;
}
} else {
++$ip[3];
}
break;
case 6:
foreach($parts as $k=>$v) {
$ip[$k] = hexdec($v);
}
$k = 7;
if($ip[$k] == 65535) {
if($ip[$k - 1] == 65535) {
if($ip[$k - 2] == 65535) {
if($ip[$k - 3] == 65535) {
if($ip[$k - 4] == 65535) {
if($ip[$k - 5] == 65535) {
if($ip[$k - 6] == 65535) {
if($ip[$k - 7] == 65535) {
throw new exception("Invalid IP " . self::arrayToIP($ip,$type) . " unable to be incremented.");
} else {
++$ip[$k - 7];
$ip[$k] = $ip[$k - 1] = $ip[$k - 2] = $ip[$k - 3] = $ip[$k - 4] = $ip[$k - 5] = $ip[$k - 6] = 0;
}
} else {
++$ip[$k - 6];
$ip[$k] = $ip[$k - 1] = $ip[$k - 2] = $ip[$k - 3] = $ip[$k - 4] = $ip[$k - 5] = 0;
}
} else {
++$ip[$k - 5];
$ip[$k] = $ip[$k - 1] = $ip[$k - 2] = $ip[$k - 3] = $ip[$k - 4] = 0;
}
} else {
++$ip[$k - 4];
$ip[$k] = $ip[$k - 1] = $ip[$k - 2] = $ip[$k - 3] = 0;
}
} else {
++$ip[$k - 3];
$ip[$k] = $ip[$k - 1] = $ip[$k - 2] = 0;
}
} else {
++$ip[$k - 2];
$ip[$k] = $ip[$k - 1] = 0;
}
} else {
++$ip[$k - 1];
$ip[$k] = 0;
}
} else {
++$ip[$k];
}
foreach($ip as $k=>$v) {
$ip[$k] = dechex($v);
}
break;
}
$ip = self::arrayToIP($ip,$type);
return $ip;
} catch(exception $e) {
error::log($e->getMessage(),__FUNCTION__,__LINE__);
return false;
}
}
/***
* Detects whether a given IP is within an IP block owned by the network.
*
* @param string $ip
* @return boolean
*/
public function isWithinOwnedIPBlock($ip)
{
$type = self::getIPType($ip);
$ip = self::ipToLong($ip);
try {
$owned_blocks = self::getAllIPBlocks();
if(isset($owned_blocks[0])) {
foreach($owned_blocks as $b) {
if(($ip >= $b['starting_ip'])) {
if(($ip <= $b['ending_ip'])) {
return true;
}
}
}
} else {
if(($ip >= $owned_blocks['starting_ip'])) {
if(($ip <= $owned_blocks['ending_ip'])) {
return true;
}
}
}
throw new exception("IP '$ip' is not within any owned ranges.");
} catch(exception $e) {
error::log($e->getMessage(),__FUNCTION__,__LINE__);
return false;
}
return false;
}
/***
* Checks all system IP blocks to see if a new incoming IP block is taken already or will cause overlap.
*
* @param string $start_ip,$end_ip
* @param int $type
* @return boolean
*/
public function ipBlockFree($start_ip,$end_ip,$type)
{
list($start_ip,$end_ip) = db::prepare(func_get_args());
try {
$blocks = self::getAllIPBlocks();
if(isset($blocks[0])) {
foreach($blocks as $b) {
if($b['ip_type'] == $type) {
if((($start_ip >= $b['starting_ip']) && ($start_ip <= $b['ending_ip'])) || (($end_ip <= $b['ending_ip']) && ($end_ip >= $b['starting_ip']))) {
throw new exception("IP Block starting with IP '" . self::longToIP($start_ip) . "' already exists.");
}
}
}
} else {
if($blocks['ip_type'] == $type) {
if((($start_ip >= $blocks['starting_ip']) && ($start_ip <= $blocks['ending_ip'])) || (($end_ip <= $blocks['ending_ip']) && ($end_ip >= $blocks['starting_ip']))) {
throw new exception("IP Block starting with IP '" . self::longToIP($start_ip) . "' already exists.");
}
}
}
return true;
} catch(exception $e) {
error::log($e->getMessage(),__FUNCTION__,__LINE__);
return false;
}
return false;
}
/***
* Checks all allocated IP blocks to see if a new incoming IP block is taken already or will cause overlap.
*
* @param string $start_ip,$end_ip
* @param int $type
* @return boolean
*/
public function ipAllocationFree($start_ip,$end_ip,$type)
{
list($start_ip,$end_ip,$type) = db::prepare(func_get_args());
try {
$start_ip = self::ipToLong($start_ip);
$end_ip = self::ipToLong($end_ip);
//print "<h1>ipAllocationFree --- Type: $type, Start: $start_ip, End: $end_ip</h1>";
$allocations = self::getAllAssignedIPRanges($type);
if(isset($allocations[0])) {
foreach($allocations as $a) {
if($a['ip_type'] == $type) {
if(bccomp($start_ip,$a['starting_ip']) >= 0) {
if(bccomp($start_ip,$a['ending_ip']) == -1) {
return false;
}
}
if(bccomp($end_ip,$a['starting_ip']) >= 0) {
if(bccomp($end_ip,$a['ending_ip']) <= 0) {
return false;
}
}
}
}
} else {
if($allocations['ip_type'] == $type) {
if(bccomp($start_ip,$allocations['starting_ip']) >= 0) {
if(bccomp($start_ip,$allocations['ending_ip']) == -1) {
return false;
}
}
if(bccomp($end_ip,$allocations['starting_ip']) == 1) {
if(bccomp($end_ip,$allocations['ending_ip']) <= 0) {
return false;
}
}
}
}
return true;
} catch(exception $e) {
error::log($e->getMessage(),__FUNCTION__,__LINE__);
return false;
}
return false;
}
function isTrue()
{
return true;
}
/***
* Calculate whether the desired IP is already taken, or in another allocated block
*
* @param string $start_ip,$size
* @return boolean
*/
public function isAllocatable($start_ip,$size)
{
list($start_ip,$size) = db::prepare(func_get_args());
$type = self::getIPType($start_ip);
$start_ip = self::expandIP($start_ip);
try {
if(!self::isValidIP($start_ip)) {
throw new exception('Invalid IP.');
}
if(!self::isValidStartIP($start_ip)) {
throw new exception("Invalid starting IP '$start_ip' provided.");
}
if(!self::isWithinOwnedIPBlock($start_ip)) {
throw new exception("IP block " . $start_ip . " is not within an IP block under system control.");
}
if(!$end = self::findEndOfIPBlock($start_ip,$size)) {
throw new exception("IP block is invalid, and no ending IP address could be found.");
}
if(!self::ipAllocationFree($start_ip,self::findEndOfIPBlock($start_ip,$size),$type)) {
throw new exception("IPv$type block '$start_ip$size' is not free to be allocated.");
}
return true;
} catch(exception $e) {
//error::log($e->getMessage(),__FUNCTION__,__LINE__);
return false;
}
return false;
}
/***
* When finding an IP block, locate an IP block that sits squarely on top of another IP block,
* instead of having them spread out widely across the IP range.
*
* @param string $size
* @param int $type
* @return string
*/
public function findBestIPBlock($size,$type)
{
list($size,$type) = db::prepare(func_get_args());
try {
if(!isset($size) || !isset($type)) {
throw new exception("Parameters for size and type of IP block required.");
}
$allocations = self::getAllAssignedIPRanges($type);
$blocks = self::getAllIPBlocks($type);
if(isset($allocations[0])) {
foreach($allocations as $a) {