91d7013d523b7c666221d80680ff7bdae8fbeac2
[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($i!=0) $slt .= ":";
80                                                 if(strlen($pss[$i]) < 1) {
81                                                         error_log("no 3");
82                                                         return false;
83                                                 }
84                                                 $slt .= "".$pss[$i];
85                                         }
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($name, $sn, $mask, $desc)
102         {
103                 global $db;
104                 
105                 if(ip::isValid($sn, $mask)) {
106                         $sn = ip::truncateNetwork($sn, $mask);
107                         $sql = "insert into supernet values (NULL, '$name', '$sn', '$mask', '$desc')";
108                         $db->dbobject->query($sql);
109                         return true;
110                 }
111                 return false;
112         }
113         
114         function addSubnet($subnet)
115         {
116                 
117         }
118         
119         function isConflicting($subnet)
120         {
121                 
122         }
123         
124         public $supers = "";
125         public $subs = "";
126 }
127 ?>