getting towards base functionality
[PHPIPManager.git] / lib / ip.php
1 <?php
2
3 function ipversion($ip)
4 {
5         $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);
6         $ipv6 = ereg("^[0-9a-fA-F:]+$", $ip);
7         
8         if($ipv4 == 1) return 4;
9         if($ipv6 == 1) return 6;
10         return 0;
11 }
12
13 // this function tries to take a short representation of a ip6 address and converts it to a long one.
14 // this is not entirely going to work.. but we'll get back to this
15 function ip6ToLongStr($ip)
16 {
17         if(ipversion($ip)!=6) return false;
18         
19         $pss = explode(":", $ip);
20         $ns = count($pss);
21         
22         $nstr = "";
23         foreach($pss as $ele) {
24                 if($nstr != "") {
25                         $nstr .= ":";
26                 }
27                 if(strlen($ele < 1)) {
28                         // this is where we pad 8-count of 0000:
29                         $pds = 8-$ns+1;
30                         for($i=0; $i < $pds; $i++) {
31                                 $nstr .= "0000";
32                         }
33                 } else if(strlen($ele) < 4) {
34                         $nstr .= str_pad($ele, 4, "0", STR_PAD_LEFT);
35                 } else {
36                         $nstr .= $ele;
37                 }
38         }
39         return $nstr;
40 }
41
42 // this function converts an ip address to a comparable integer
43 // so we can bounds-check.
44 // it returns an array of [firstnum][lastnum]
45 function ip6ToComp($ip, $mask)
46 {
47         if($ipversion($ip)!=6) return false;
48         
49         $rip = ip6ToLongStr($ip);
50         
51 }
52
53 function ip4ToComp($ip, $mask)
54 {
55         
56 }
57
58 class ip {
59         
60         static function isValid($ip, $mask = 0)
61         {
62                 
63                 $ver = ipversion($ip);
64                 
65                 if($ver == 0) return 0;
66
67                 if($ver == 6) {
68                         if($mask != 0) {
69                                 if($mask%16 != 0) {
70                                         error_log("no");
71                                         return false;
72                                 } else {
73                                         $sns = $mask / 16;
74                                         
75                                         // now check that we have $sns number of subnets specified
76                                         $pss = explode(":", $ip);
77                                         $ns = count($pss);
78                                         
79                                         // we need to specify an error we can throw back at the user
80                                         if($ns < $sns) {
81                                                 error_log("no 2, $ns, $sns, $ip");
82                                                 print_r($pss);
83                                                 return false;
84                                         }
85                                         for($i = 0; $i < $sns; $i++) {
86                                                 if(strlen($pss[$i]) < 1) {
87                                                         error_log("no 3");
88                                                         return false;
89                                                 }
90                                         }
91                                         
92                                         // we are still a valid ipv6 ip address/mask
93                                         error_log("Valid");
94                                         return true;
95                                 }
96                         }
97                 }
98                 
99                 if($ver == 4) {
100                         if($mask != 0) {
101                                 
102                         }
103                 }
104         }
105         
106         static function truncateNetwork($ip, $mask)
107         {
108                 $ver = ipversion($ip);
109                 
110                 if($ver == 0) return false;
111                 
112                 if($ver == 6) {
113                         if($mask != 0) {
114                                 if($mask%16 != 0) {
115                                         error_log("no");
116                                         return false;
117                                 } else {
118                                         $sns = $mask / 16;
119                                         
120                                         // now check that we have $sns number of subnets specified
121                                         $pss = explode(":", $ip);
122                                         $ns = count($pss);
123                                         
124                                         // we need to specify an error we can throw back at the user
125                                         if($ns < $sns) {
126                                                 error_log("no 2, $ns, $sns, $ip");
127                                                 print_r($pss);
128                                                 return false;
129                                         }
130                                         
131                                         $slt = "";
132                                         for($i = 0; $i < $sns; $i++) {
133                                                 if($i!=0) $slt .= ":";
134                                                 if(strlen($pss[$i]) < 1) {
135                                                         error_log("no 3");
136                                                         return false;
137                                                 }
138                                                 $slt .= "".$pss[$i];
139                                         }
140                                         
141                                         // we are still a valid ipv6 ip address/mask
142                                         error_log("Valid");
143                                         return $slt;
144                                 }
145                         }
146                 }
147                 
148                 if($ver == 4) {
149                         if($mask != 0) {
150                                 
151                         }
152                 }
153         }
154         
155         function addSupernet($name, $sn, $mask, $desc)
156         {
157                 global $db;
158                 
159                 if(ip::isValid($sn, $mask)) {
160                         $sn = ip::truncateNetwork($sn, $mask);
161                         $sql = "insert into supernet values (NULL, '$name', '$sn', '$mask', '$desc')";
162                         $db->dbobject->query($sql);
163                         return true;
164                 }
165                 return false;
166         }
167         
168         function addSubnet($name, $subnet, $mask, $desc, $super)
169         {
170                 global $db;
171                 if(ip::isValid($subnet, $mask)) {
172                         $sn = ip::truncateNetwork($sn, $mask);
173                         // ("sn_id" INTEGER PRIMARY KEY AUTOINCREMENT,"snid_id" INTEGER,"sn_ip" TEXT,"sn_mask" TEXT,"sn_name" TEXT, "sn_desc" TEXT);';
174                         //if(!isSubnet($subnet, $mask, $super)) return "Is not a valid subnet"; 
175                         
176                         $sql = "insert into subnet values (NULL, '$super', '$subnet', '$mask', '$name', '$desc')";
177                         error_log("sql: $sql");
178                         $db->dbobject->query($sql);
179                         return true;
180                 }
181         }
182         
183         function isSubnet($subnet, $mask, $superid)
184         {
185                 return true;
186         }
187         
188         // this function returns true if it is NOT conflicting
189         function isConflicting($subnet, $mask, $superid)
190         {
191                 return true;
192         }
193         
194         public $supers = "";
195         public $subs = "";
196 }
197 ?>