Location: PHPKode > projects > OpenDataCenter Network Management System > include/mainview_page/subnet/manually_add.php
<?php
// ----------------------------------------------------------------------
// Copyright (C) 2007 by GREGORY LE BRAS
// ----------------------------------------------------------------------
// LICENSE
//
// This file is part of ODCNMS - Open DataCenter Network Management System
// 
// ODCNMS is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// Foobar is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with Foobar; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
// ----------------------------------------------------------------------
// Original Author of file: GREGORY LE BRAS - http://www.odcnms.org/
// ----------------------------------------------------------------------
?>
<?php
if (!isset($add))
{
?>
<script language="JavaScript">
var opt = new OptionTransfer("list1","list2");
opt.setAutoSort(true);
opt.setDelimiter(";");
opt.saveNewLeftOptions("customer");
</script>
<form method="post" action="index.php?include=mainview&page=subnet&action=manually_add&add=ok" name="add_subnet" id="add_subnet">
	<fieldset class="form" style="min-width:1000px;">
		<fieldset class="form" style="width:500px; float: right;">
			<legend style="font-weight:normal;"><img src="images/stats.gif" alt="Stats" align="top"> <?php echo TXT_SUBNET; ?> <?php echo TXT_INFORMATION; ?> <?php echo TXT_STATISTIC; ?></legend>
			<label><?php echo TXT_AMOUNT_OF; ?> <?php echo TXT_SUBNET; ?></label>
			<input type="text" name="numnode" disabled>
			<label><?php echo TXT_BROADCAST; ?> <?php echo TXT_ADDRESS; ?></label>
			<input type="text" name="broadcastaddress" disabled>
			<label><?php echo TXT_IP; ?> <?php echo TXT_RANGE; ?></label>
			<input type="text" name="range_1" disabled><span style="position:relative; top:-10px;"> <?php echo TXT_TO; ?> </span><input type="text" name="range_2" disabled>
		</fieldset>
		<legend><img src="images/add.gif" alt="Add" align="top"> <?php echo TXT_MANUALLY; ?> <?php echo TXT_ADD; ?> <?php echo TXT_SUBNET; ?></legend>
		<label><?php echo TXT_NAME; ?></label>
		<input type="text" name="name" size="50"><br>
		<label><?php echo TXT_NETWORK; ?></label>
		<select name="network" id="network" size="1" onchange="calcaddress(this.options[this.selectedIndex].value);" onclick="calcnode(this.form);calcbroadcastaddress(this.form);calciprange(this.form);A_SLIDERS[0].f_setValue(document.getElementById('snmbits').value);">
			<option value="null"><?php echo TXT_SELECT_NETWORK_SEGMENT; ?></option>
		<?php
		$request=mysql_query("select id,name,address,mask FROM $name_table_network") or die ("ERROR!");

		while($tmp_request=mysql_fetch_array($request))
		{
			if ($network_ident2 == $tmp_request['id'])
			{
				echo "<option value=\"$tmp_request[id];$tmp_request[address];$tmp_request[mask]\" SELECTED>$tmp_request[name] - $tmp_request[address]";
				$cidr = find_net($tmp_request['address'],$tmp_request['mask']);
				echo "$cidr[cidr]</option>";
			}
			else
			{
				echo "<option value=\"$tmp_request[id];$tmp_request[address];$tmp_request[mask]\">$tmp_request[name] - $tmp_request[address]";
				$cidr = find_net($tmp_request['address'],$tmp_request['mask']);
				echo "$cidr[cidr]</option>";
			}
		}
		?>
		</select>
		<label><?php echo TXT_CUSTOMERS; ?></label>
		<table border="0">
		<tr>
			<td>
			<select name="list1" multiple="multiple" size="10" onDblClick="opt.transferRight()">
			</select>
			</td>
			<td align="center" valign="middle">
				<input name="left" value="<?php echo TXT_ADD; ?> &lt;&lt;" ONCLICK="opt.transferLeft()" type="button"><br><br>
				<input name="left" value="<?php echo TXT_ADD; ?> <?php echo TXT_ALL; ?> &lt;&lt;" ONCLICK="opt.transferAllLeft()" type="button"><br><br>
				<input name="right" value="<?php echo TXT_REMOVE; ?> &gt;&gt;" ONCLICK="opt.transferRight()" type="button"><br><br>
				<input name="right" value="<?php echo TXT_REMOVE; ?> <?php echo TXT_ALL; ?> &gt;&gt;" ONCLICK="opt.transferAllRight()" type="button">
			</td>
			<td>
			<select name="list2" multiple="multiple" size="10" onDblClick="opt.transferLeft()">
			<?php
			$request=mysql_query("select name,id FROM $name_table_customer") or die ("ERROR!");

			while($tmp_request=mysql_fetch_array($request))
			{
				echo "<option value=\"$tmp_request[id]\">$tmp_request[name]</option>";
			}
			?>
			</select>
			</td>
		</tr>
		</table>
		<label><?php echo TXT_VLANS; ?></label>
		<div id="add_vlan" style="float:left;display:none;margin-right:5px;"> <input type="text" name="new_vlan_name" id="new_vlan_name" size="15" style="background-image:url(images/background_name.png);background-repeat:no-repeat;"> <input type="text" name="new_vlan_number" id="new_vlan_number" size="15" style="background-image:url(images/background_number.png);background-repeat:no-repeat;"> <input type="text" name="new_vlan_description" id="new_vlan_description" size="20"  style="background-image:url(images/background_description.png);background-repeat:no-repeat;"></div>
		<select name="vlan" id="vlan" size="1" style="float:left; margin-right:5px;">
			<?php
			$request=mysql_query("select id,name,number FROM $name_table_vlan") or die ("ERROR!");

			while($tmp_request=mysql_fetch_array($request))
			{
				echo "<option value=\"$tmp_request[id]\">$tmp_request[name] ($tmp_request[number])</option>";
			}
			?>
		</select>
		<input type="button" value="<?php echo TXT_ADD_NEW_VLAN; ?>" onclick="changedisabled('vlan');displayelement('add_vlan');displayelement('vlan'); if(document.getElementById('add_vlan').style.display=='none') this.value='<?php echo TXT_ADD_NEW_VLAN; ?>'; else {this.value='<?php echo TXT_SELECT_VLAN; ?>'; document.getElementById('new_vlan_name').value='';document.getElementById('new_vlan_number').value='';document.getElementById('new_vlan_description').value=''}">
		<label style="clear:both;"><?php echo TXT_ADDRESS; ?></label>
		<input type="text" name="oct1" id="oct1" size="3" maxlength="3" onkeydown="keynext(event.keyCode,'oct2');if (event.keyCode==190) return false;" onKeyPress="return numbersonly(this, event)" onkeyup="calciprange(this.form);"><span style="top: -6px;position:relative;">.</span><input type="text" name="oct2" id="oct2" onkeydown="keynext(event.keyCode,'oct3');if (event.keyCode==190) return false;" size="3" maxlength="3" onKeyPress="return numbersonly(this, event)" onkeyup="calciprange(this.form);"><span style="top: -6px;position:relative;">.</span><input type="text" name="oct3" id="oct3" size="3" maxlength="3" onkeydown="keynext(event.keyCode,'oct4');if (event.keyCode==190) return false;" onKeyPress="return numbersonly(this, event)" onkeyup="calciprange(this.form);"><span style="top: -6px;position:relative;">.</span><input type="text" name="oct4" id="oct4" size="3" maxlength="3" onKeyPress="return numbersonly(this, event)" onkeyup="calciprange(this.form);"><br>
		<label><?php echo TXT_MASK; ?></label>
		<span style="float:left;">
		<input type="text" name="snm1" id="snm1" size="3" maxlength="3" onkeydown="keynext(event.keyCode,'snm2');if (event.keyCode==190) return false;" onKeyPress="return numbersonly(this, event);" onkeyup="calccidr(this.form);calcnode(this.form);calcbroadcastaddress(this.form);calciprange(this.form);"><span style="top: -6px;position:relative;">.</span><input type="text" name="snm2" id="snm2" size="3" maxlength="3" onkeydown="keynext(event.keyCode,'snm3');if (event.keyCode==190) return false;" onKeyPress="return numbersonly(this, event);" onkeyup="calccidr(this.form);calcnode(this.form);calcbroadcastaddress(this.form);calciprange(this.form);"><span style="top: -6px;position:relative;">.</span><input type="text" name="snm3" id="snm3" size="3" maxlength="3" onkeydown="keynext(event.keyCode,'snm4');if (event.keyCode==190) return false;" onKeyPress="return numbersonly(this, event);" onkeyup="calccidr(this.form);calcnode(this.form);calcbroadcastaddress(this.form);calciprange(this.form);"><span style="top: -6px;position:relative;">.</span><input type="text" name="snm4" id="snm4" size="3" maxlength="3" onKeyPress="return numbersonly(this, event);" onkeyup="calccidr(this.form);calcnode(this.form);calcbroadcastaddress(this.form);calciprange(this.form);"><span style="position:relative;top:-10px;"> or /</span><input style="display:inline;" type="text" name="snmbits" id="snmbits" value="" size="3" maxlength="2" maxlength="2" onKeyPress="return numbersonly(this, event);" onkeyup="calcmask(this.form);calcnode(this.form);calcbroadcastaddress(this.form);calciprange(this.form);A_SLIDERS[0].f_setValue(document.getElementById('snmbits').value);"></span>
		<span style="float:left;position:relative;margin:0px; margin-left:30px;"><script language="JavaScript">
	var A_TPL4h = {
		'b_vertical' : false,
		'b_watch': true,
		'n_controlWidth': 157,
		'n_controlHeight': 26,
		'n_sliderWidth': 11,
		'n_sliderHeight': 21,
		'n_pathLeft' : 0,
		'n_pathTop' : 0,
		'n_pathLength' : 146,
		's_imgControl': 'images/background_slide_bar.png',
		's_imgSlider': 'images/cursor_slide_bar.png',
		'n_zIndex': 1
	}
	var A_INIT4h = {
		's_form' : 0,
		's_name': 'snmbits',
		'n_minValue' : 1,
		'n_maxValue' : 32,
		'n_value' : 1,
		'n_step' : 1
	}

	new slider(A_INIT4h, A_TPL4h);
</script></span>
		<label style="clear:left;"><?php echo TXT_STATUS; ?></label>
		<select name="status" size="1">
			<option value="0"><?php echo TXT_ASSIGNED; ?></option>
			<option value="1"><?php echo TXT_UNUSED; ?></option>
			<option value="2"><?php echo TXT_RESERVED; ?></option>
		</select>
		<label><?php echo TXT_DESCRIPTION; ?></label>
		<textarea name="description" rows="5" cols="50"></textarea><br>
		<input type="hidden" name="customer" value="">
		<input type="submit" name="submit" value="<?php echo TXT_ADD; ?>">
		<input type="button" name="Cancel" value="<?php echo TXT_CANCEL; ?>" onclick="javascript:history.go(-1);">
	</fieldset>
</form>
<br><center><a href="javascript:history.go(-1);"><?php echo TXT_BACK; ?></a></center>
 <script language="JavaScript" type="text/javascript">
	 var frmvalidator = new Validator("add_subnet");
		 frmvalidator.addValidation("name","req","Please enter the name of the subnet");
		 frmvalidator.addValidation("name","maxlen=255",
"Max length for name is 255");
		 
		 frmvalidator.addValidation("network","req","Please select the network of the subnet");
		 frmvalidator.addValidation("network","dontselect=0","Please select the network of the subnet");
		 frmvalidator.addValidation("status","req","Please select the status of the subnet");
		 
		 frmvalidator.addValidation("oct1","req","Please enter the first part of the Address of the subnet");
		 frmvalidator.addValidation("oct1","num","Please enter numeric data for the first part of the Address of the subnet");
		 frmvalidator.addValidation("oct1","maxlen=3","Max length for the first part of the Address of the subnet is 3");
		 frmvalidator.addValidation("oct2","req","Please enter the second part of the Address of the subnet");
		 frmvalidator.addValidation("oct2","num","Please enter numeric data for the second part of the Address of the subnet");
		 frmvalidator.addValidation("oct2","maxlen=3","Max length for the second part of the Address of the subnet is 3");
		 frmvalidator.addValidation("oct3","req","Please enter the third part of the Address of the subnet");
		 frmvalidator.addValidation("oct3","num","Please enter numeric data for the third part of the Address of the subnet");
		 frmvalidator.addValidation("oct3","maxlen=3","Max length for the third part of the Address of the subnet is 3");
		 frmvalidator.addValidation("oct4","req","Please enter the fourth part of the Address of the subnet");
		 frmvalidator.addValidation("oct4","num","Please enter numeric data for the fourth part of the Address of the subnet");
		 frmvalidator.addValidation("oct4","maxlen=3","Max length for the fourth part of the Address of the subnet is 3");
		 
		 frmvalidator.addValidation("snm1","req","Please enter the first part of the Mask of the subnet");
		 frmvalidator.addValidation("snm1","num","Please enter numeric data for the first part of the Mask of the subnet");
		 frmvalidator.addValidation("snm1","maxlen=3","Max length for the first part of the Mask of the subnet is 3");
		 frmvalidator.addValidation("snm2","req","Please enter the second part of the Mask of the subnet");
		 frmvalidator.addValidation("snm2","num","Please enter numeric data for the second part of the Mask of the subnet");
		 frmvalidator.addValidation("snm2","maxlen=3","Max length for the second part of the Mask of the subnet is 3");
		 frmvalidator.addValidation("snm3","req","Please enter the third part of the Mask of the subnet");
		 frmvalidator.addValidation("snm3","num","Please enter numeric data for the third part of the Mask of the subnet");
		 frmvalidator.addValidation("snm3","maxlen=3","Max length for the third part of the Mask of the subnet is 3");
		 frmvalidator.addValidation("snm4","req","Please enter the fourth part of the Mask of the subnet");
		 frmvalidator.addValidation("snm4","num","Please enter numeric data for the fourth part of the Mask of the subnet");
		 frmvalidator.addValidation("snm4","maxlen=3","Max length for the fourth part of the Mask of the subnet is 3");		 
		 
</script>
<?php
}
else if ($add=="ok")
{
	$networkarray = explode(";",$network);
	$address2 = $oct1.".".$oct2.".".$oct3.".".$oct4;
	$address = (intval(ip2long($address2)));
	$mask = $snm1.".".$snm2.".".$snm3.".".$snm4;
	$subnet_size_array = calculate_ip_address($address2,$mask);
	$subnet_size = $subnet_size_array[4];
	$cidr = find_net($address2,$mask);
		
	$request = mysql_query ("SELECT id,network_id,address,mask FROM $name_table_subnet WHERE network_id=$networkarray[0]") or die ("ERROR!");
	$requestcount=mysql_query("SELECT count(id) FROM $name_table_subnet WHERE network_id=$networkarray[0]") or die ("ERROR!");
	$all_subnet = mysql_fetch_array($requestcount);
	while($tmp_request=mysql_fetch_array($request))
	{
		$network_address = (intval(ip2long($tmp_request['address'])));
		$network_size_array = calculate_ip_address($tmp_request['address'],$tmp_request['mask']);
		$network_size = $network_size_array[4];
		$cidr_network = find_net($tmp_request['address'],$tmp_request['mask']);
	
		if ((($address==$network_address) && (($address+$subnet_size)==($network_address+$network_size))) || (($address==$network_address) && ($address+($subnet_size-1) < ($network_address+$network_size))) || (($address==$network_address) && ($address+$subnet_size) > ($network_address+($network_size-1))) || (($network_address < $address) && ($address+($subnet_size-1) < ($network_address+$network_size)) || (($network_address > $address) && ($address+$subnet_size) > $network_address+($network_size-1))))
		{
			echo "<div id=\"status\" style=\"color:red\"><img src=\"images/warning.png\" alt=\"WARNING\" align=\"center\"> ";
			echo TXT_SUBNET; echo $address2.$cidr['cidr'];
			echo TXT_OVERLAP_EXISTING_SUBNET; echo "(<a href=\"index.php?include=mainview&page=subnet&action=view&id=$tmp_request[id]\">$tmp_request[address]$cidr_network[cidr]</a>)</div>";
      
      $launch_query = "NO_OK";
		}
		else
		{
      $launch_query = "OK";
		}
	}
  
  if ($launch_query=="OK")
  {
    $query=mysql_query("INSERT INTO $name_table_subnet VALUES ('',\"$networkarray[0]\",\"$name\",\"$address2\",\"$mask\",\"$status\",\"$description\")") or die ("ERROR!");
		
    if(!$query)
    {
      echo "<center><div id=\"status\" style=\"color:red\"><img src=\"images/warning.png\" alt=\"WARNING\"><br><br>";
      echo TXT_DATABASE_ERROR;
      echo "</div><br><a href=\"javascript:history.go(-2);\">";
      echo TXT_BACK;
      echo "</a></center>";
    }
    else
    {
      $subnet_id = mysql_insert_id();
      
      $customerarray = explode (";",$customer);
    
      foreach ($customerarray as $arrayvalue)
      {
        $query2=mysql_query("INSERT INTO $name_table_link_customer_subnet VALUES (\"$arrayvalue\",\"$subnet_id\")") or die ("ERROR!");
      }

      if ($new_vlan_name != "")
      {
        $requestcountvlan=mysql_query("SELECT count(number) FROM $name_table_vlan WHERE number=$new_vlan_number") or die ("ERROR!");
        $all_vlan = mysql_fetch_array($requestcountvlan);
        
        if ($all_vlan[0]=="0")
        {
          $query3=mysql_query("INSERT INTO $name_table_vlan VALUES ('',\"$new_vlan_number\",\"$new_vlan_name\",\"$new_vlan_description\")") or die ("ERROR!");
          $vlan_id = mysql_insert_id();
          $query4=mysql_query("INSERT INTO $name_table_link_subnet_vlan VALUE (\"$subnet_id\",\"$vlan_id\")") or die ("ERROR!");
        }
        else
        {
          echo "<center><div id=\"status\" style=\"color:red\"><img src=\"images/warning.png\" alt=\"WARNING\"><br><br>";
          echo TXT_VLAN_NUMBER_EXIST;
          echo "</div><br><a href=\"javascript:history.go(-2);\">";
          echo TXT_BACK;
          echo "</a></center>";
        }
      }
      else
      {
        $query5=mysql_query("INSERT INTO $name_table_link_subnet_vlan VALUE (\"$subnet_id\",\"$vlan\")") or die ("ERROR!");
      }
      
      $currentdate = date('Y-m-d');
      $currenttime = date("H:i:s"); 
      
      user_log ($_SESSION['login'],$name_table_users,$name_table_users_log,"add","subnet","$name ($subnet_id)",$mysql_hostname,$mysql_username,$mysql_password,$mysql_database);
      
      foreach ($syslog_server as $arrayvalue)
      {
        $syslog = new Syslog();
        $syslog->SetFacility(13);
        $syslog->SetSeverity(5);
        $syslog->SetProcess('odcnms');
        $syslog->SetHostname('');
        $syslog->SetFqdn('');
        $syslog->SetIpFrom('');		
        $syslog->SetContent($currentdate.' '.$currenttime.' user: '.$_SESSION['login'].' - add subnet '.$name.'('.$subnet_id.')');
        $syslog->SetServer($arrayvalue);
        $syslog->Send();
      }
      
      echo "<center><div id=\"status\" style=\"color:green\"><img src=\"images/info.png\" alt=\"INFO\"><br><br>";
      echo TXT_SUCCESSFULLY_ADD_SUBNET;
      echo "</div><br><div id=\"status\" style=\"color:blue\"><img src=\"images/question.gif\" alt=\"INFO\"><br><br>";
      echo TXT_WHAT_DO_YOU_WANT_TO_DO;
      echo "<form name=\"option_after\"><select name=\"select\" onchange=\"goto(this.form);\" class=\"form\"><option value=\"\">-------";
      echo TXT_CHOOSE_SELECTION;
      echo "-------</option></select></form></div><br><br><a href=\"javascript:history.go(-2);\">";
      echo TXT_BACK;
      echo "</a></center>";
    }
  }
	
	if ($all_subnet[0]=="0")
	{
		$query=mysql_query("INSERT INTO $name_table_subnet VALUES ('',\"$networkarray[0]\",\"$name\",\"$address2\",\"$mask\",\"$status\",\"$description\")") or die ("ERROR!");
	
		if(!$query)
		{
			echo "<center><div id=\"status\" style=\"color:red\"><img src=\"images/warning.png\" alt=\"WARNING\"><br><br>";
			echo TXT_DATABASE_ERROR;
			echo "</div><br><a href=\"javascript:history.go(-2);\">";
			echo TXT_BACK;
			echo "</a></center>";
		}
		else
		{
			$subnet_id = mysql_insert_id();
			
			$customerarray = explode (";",$customer);
		
			foreach ($customerarray as $arrayvalue)
			{
				$query2=mysql_query("INSERT INTO $name_table_link_customer_subnet VALUES (\"$arrayvalue\",\"$subnet_id\")") or die ("ERROR!");
			}

			if ($new_vlan_name != "")
			{
				$requestcountvlan=mysql_query("SELECT count(number) FROM $name_table_vlan WHERE number=$new_vlan_number") or die ("ERROR!");
				$all_vlan = mysql_fetch_array($requestcountvlan);
				
				if ($all_vlan[0]=="0")
				{
					$query3=mysql_query("INSERT INTO $name_table_vlan VALUES ('',\"$new_vlan_number\",\"$new_vlan_name\",\"$new_vlan_description\")") or die ("ERROR!");
					$vlan_id = mysql_insert_id();
					$query4=mysql_query("INSERT INTO $name_table_link_subnet_vlan VALUE (\"$subnet_id\",\"$vlan_id\")") or die ("ERROR!");
				}
				else
				{
					echo "<center><div id=\"status\" style=\"color:red\"><img src=\"images/warning.png\" alt=\"WARNING\"><br><br>";
					echo TXT_VLAN_NUMBER_EXIST;
					echo "</div><br><a href=\"javascript:history.go(-2);\">";
					echo TXT_BACK;
					echo "</a></center>";
				}
			}
			else
			{
				$query5=mysql_query("INSERT INTO $name_table_link_subnet_vlan VALUE (\"$subnet_id\",\"$vlan\")") or die ("ERROR!");
			}
			
			$currentdate = date('Y-m-d');
			$currenttime = date("H:i:s"); 
			
			user_log ($_SESSION['login'],$name_table_users,$name_table_users_log,"add","subnet","$name ($subnet_id)",$mysql_hostname,$mysql_username,$mysql_password,$mysql_database);
			
			foreach ($syslog_server as $arrayvalue)
			{
				$syslog = new Syslog();
				$syslog->SetFacility(13);
				$syslog->SetSeverity(5);
				$syslog->SetProcess('odcnms');
				$syslog->SetHostname('');
				$syslog->SetFqdn('');
				$syslog->SetIpFrom('');		
				$syslog->SetContent($currentdate.' '.$currenttime.' user: '.$_SESSION['login'].' - add subnet '.$name.'('.$subnet_id.')');
				$syslog->SetServer($arrayvalue);
				$syslog->Send();
			}
			
			echo "<center><div id=\"status\" style=\"color:green\"><img src=\"images/info.png\" alt=\"INFO\"><br><br>";
			echo TXT_SUCCESSFULLY_ADD_SUBNET;
			echo "</div><br><div id=\"status\" style=\"color:blue\"><img src=\"images/question.gif\" alt=\"INFO\"><br><br>";
			echo TXT_WHAT_DO_YOU_WANT_TO_DO;
			echo "<form name=\"option_after\"><select name=\"select\" onchange=\"goto(this.form);\" class=\"form\"><option value=\"\">-------";
			echo TXT_CHOOSE_SELECTION;
			echo "-------</option></select></form></div><br><br><a href=\"javascript:history.go(-2);\">";
			echo TXT_BACK;
			echo "</a></center>";
		}
	}
}
?>
Return current item: OpenDataCenter Network Management System