<?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)
{
{
// we build a frame of framey's
$this->header();
+ $this->printError();
$this->mainPage();
$this->footer();
}
{
global $db;
- echo "<h1>Welcome to PHPIPManager</h1>";
?>
<form method="post" action="?action=addsuper">
Create Supernet: name <input type="text" name="name"></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
}
$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