working a bit
authorpaulr <me@pjr.cc>
Fri, 10 Sep 2010 08:21:14 +0000 (18:21 +1000)
committerpaulr <me@pjr.cc>
Fri, 10 Sep 2010 08:21:14 +0000 (18:21 +1000)
lib/ip.php
lib/www.php
unittest/ip_regex.php [new file with mode: 0644]

index e07eb30..95d71b0 100644 (file)
@@ -1,6 +1,103 @@
 <?php
 
 class ip {
+       
+       static function isValid($ip, $mask = 0)
+       {
+               $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);
+               $ipv6 = ereg("^[0-9a-fA-F:]+$", $ip);
+               
+               if(!$ipv6&&!$ipv4) return false;
+
+               if($ipv6!=false) {
+                       if($mask != 0) {
+                               if($mask%16 != 0) {
+                                       error_log("no");
+                                       return false;
+                               } else {
+                                       $sns = $mask / 16;
+                                       
+                                       // now check that we have $sns number of subnets specified
+                                       $pss = explode(":", $ip);
+                                       $ns = count($pss);
+                                       
+                                       // we need to specify an error we can throw back at the user
+                                       if($ns < $sns) {
+                                               error_log("no 2, $ns, $sns, $ip");
+                                               print_r($pss);
+                                               return false;
+                                       }
+                                       for($i = 0; $i < $sns; $i++) {
+                                               if(strlen($pss[$i]) < 1) {
+                                                       error_log("no 3");
+                                                       return false;
+                                               }
+                                       }
+                                       
+                                       // we are still a valid ipv6 ip address/mask
+                                       error_log("Valid");
+                                       return true;
+                               }
+                       }
+               }
+               
+               if($ipv4!=false) {
+                       if($mask != 0) {
+                               
+                       }
+               }
+       }
+       
+       static function truncateNetwork($ip, $mask)
+       {
+               $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);
+               $ipv6 = ereg("^[0-9a-fA-F:]+$", $ip);
+               
+               if(!$ipv6&&!$ipv4) return false;
+
+               if($ipv6!=false) {
+                       if($mask != 0) {
+                               if($mask%16 != 0) {
+                                       error_log("no");
+                                       return false;
+                               } else {
+                                       $sns = $mask / 16;
+                                       
+                                       // now check that we have $sns number of subnets specified
+                                       $pss = explode(":", $ip);
+                                       $ns = count($pss);
+                                       
+                                       // we need to specify an error we can throw back at the user
+                                       if($ns < $sns) {
+                                               error_log("no 2, $ns, $sns, $ip");
+                                               print_r($pss);
+                                               return false;
+                                       }
+                                       
+                                       $slt = "";
+                                       for($i = 0; $i < $sns; $i++) {
+                                               if(strlen($pss[$i]) < 1) {
+                                                       error_log("no 3");
+                                                       return false;
+                                               }
+                                               $slt .= $pss[$i].":";
+                                       }
+                                       $slt .= ":0";
+                                       
+                                       // we are still a valid ipv6 ip address/mask
+                                       error_log("Valid");
+                                       return $slt;
+                               }
+                       }
+               }
+               
+               if($ipv4!=false) {
+                       if($mask != 0) {
+                               
+                       }
+               }
+       }
+       
        function addSupernet($supernet)
        {
                
index c45de8a..1b8085d 100644 (file)
@@ -43,6 +43,7 @@ class www {
        {
                // we build a frame of framey's
                $this->header();
+               $this->printError();
                $this->mainPage();              
                $this->footer();
        }
@@ -51,7 +52,6 @@ class www {
        {
                global $db;
                
-               echo "<h1>Welcome to PHPIPManager</h1>";
                ?>
 <form method="post" action="?action=addsuper">
 Create Supernet: name <input type="text" name="name"></input>
@@ -60,16 +60,42 @@ Mask <input type="text" name="mask"></input>
 Description <input type="text" name="desc"></input>
 <input type="submit" name="go" Value="Create"></input>
 </form>
-               <?php 
+               <?php
+
+               // now print the super nets
+               $res = $db->dbobject->query("select * from supernet");
+               foreach($res as $row) {
+                       //echo "<pre>";
+               //      print_r($row);
+               //      echo "</pre><hr>";
+                       echo "<table border=\"1\"><tr><th>".$row["sn_name"]."</th><td>".$row["sn_ip"]."/".$row["sn_mask"]."</td><td>".$row["sn_desc"]."</td>";
+                       echo "<td><a href=\"?action=allocate\">Allocate Subnet</a></td>";
+                       echo "<td><a href=\"?action=delete\">Delete Supernet</a></td>";
+                       // now we search for sub's
+                       echo "</table><br>";
+               }
+       }
+       
+       function printError()
+       {
+               if(isset($_REQUEST["error"]))
+               {
+                       echo "<font color=\"red\">".$_REQUEST["error"]."</font>";
+               }
+               if(isset($_REQUEST["notice"]))
+               {
+                       echo "<font color=\"green\">".$_REQUEST["notice"]."</font>";
+               }
+               
        }
        
-       function header()
+       function header($title = "Welcome to PHP IP Manager")
        {
                ?>
 <html>
-<head>
+<head><title><?php echo $title ?></title>
 </head>
-<body>
+<body><h1><?php echo $title ?></h1>
                <?php
        }
        
@@ -96,10 +122,15 @@ function www_ip_addSuperRange()
        $mask = $_REQUEST["mask"];
        $desc = $_REQUEST["desc"];
        
-       $sql = "insert into supernet values (NULL, '$name', '$sn', '$mask', '$desc')";
-       $db->dbobject->query($sql);
+       if(ip::isValid($sn, $mask)) {
+               $sn = ip::truncateNetwork($sn, $mask);
+               $sql = "insert into supernet values (NULL, '$name', '$sn', '$mask', '$desc')";
+               $db->dbobject->query($sql);
+               header("Location: index.php?notice=range added");
+       } else {
+               header("Location: index.php?error=invalid ipaddress");
+       }
 
-       header("Location: index.php");
 }
 
 ?>
\ No newline at end of file
diff --git a/unittest/ip_regex.php b/unittest/ip_regex.php
new file mode 100644 (file)
index 0000000..724a19a
--- /dev/null
@@ -0,0 +1,9 @@
+<?php
+
+require_once "../lib/lib.php";
+
+ip::isValid("2003:1:2:3::1",64);
+ip::isValid("2003:1:2:3::1",48);
+ip::isValid("2003:1:2::1",64);
+
+?>
\ No newline at end of file