working a bit
[PHPIPManager.git] / lib / ip.php
1 <?php
2
3 class ip {
4         
5         static function isValid($ip, $mask = 0)
6         {
7                 $ipv4 = ereg('^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$', $ip);
8                 $ipv6 = ereg("^[0-9a-fA-F:]+$", $ip);
9                 
10                 if(!$ipv6&&!$ipv4) return false;
11
12                 if($ipv6!=false) {
13                         if($mask != 0) {
14                                 if($mask%16 != 0) {
15                                         error_log("no");
16                                         return false;
17                                 } else {
18                                         $sns = $mask / 16;
19                                         
20                                         // now check that we have $sns number of subnets specified
21                                         $pss = explode(":", $ip);
22                                         $ns = count($pss);
23                                         
24                                         // we need to specify an error we can throw back at the user
25                                         if($ns < $sns) {
26                                                 error_log("no 2, $ns, $sns, $ip");
27                                                 print_r($pss);
28                                                 return false;
29                                         }
30                                         for($i = 0; $i < $sns; $i++) {
31                                                 if(strlen($pss[$i]) < 1) {
32                                                         error_log("no 3");
33                                                         return false;
34                                                 }
35                                         }
36                                         
37                                         // we are still a valid ipv6 ip address/mask
38                                         error_log("Valid");
39                                         return true;
40                                 }
41                         }
42                 }
43                 
44                 if($ipv4!=false) {
45                         if($mask != 0) {
46                                 
47                         }
48                 }
49         }
50         
51         static function truncateNetwork($ip, $mask)
52         {
53                 $ipv4 = ereg('^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$', $ip);
54                 $ipv6 = ereg("^[0-9a-fA-F:]+$", $ip);
55                 
56                 if(!$ipv6&&!$ipv4) return false;
57
58                 if($ipv6!=false) {
59                         if($mask != 0) {
60                                 if($mask%16 != 0) {
61                                         error_log("no");
62                                         return false;
63                                 } else {
64                                         $sns = $mask / 16;
65                                         
66                                         // now check that we have $sns number of subnets specified
67                                         $pss = explode(":", $ip);
68                                         $ns = count($pss);
69                                         
70                                         // we need to specify an error we can throw back at the user
71                                         if($ns < $sns) {
72                                                 error_log("no 2, $ns, $sns, $ip");
73                                                 print_r($pss);
74                                                 return false;
75                                         }
76                                         
77                                         $slt = "";
78                                         for($i = 0; $i < $sns; $i++) {
79                                                 if(strlen($pss[$i]) < 1) {
80                                                         error_log("no 3");
81                                                         return false;
82                                                 }
83                                                 $slt .= $pss[$i].":";
84                                         }
85                                         $slt .= ":0";
86                                         
87                                         // we are still a valid ipv6 ip address/mask
88                                         error_log("Valid");
89                                         return $slt;
90                                 }
91                         }
92                 }
93                 
94                 if($ipv4!=false) {
95                         if($mask != 0) {
96                                 
97                         }
98                 }
99         }
100         
101         function addSupernet($supernet)
102         {
103                 
104         }
105         
106         function addSubnet($subnet)
107         {
108                 
109         }
110         
111         function isConflicting($subnet)
112         {
113                 
114         }
115         
116         public $supers = "";
117         public $subs = "";
118 }
119 ?>