Location: PHPKode > projects > OpenDataCenter Network Management System > include/mainview_page/subnet/automatically_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 type="text/javascript"> 

function openconfirm(f)
{
	if (parseInt(f.amountsubnet.value) > parseInt(f.numsubnet.value))
	{
		alert("The amount of divisible subnets can't be more than "+f.numsubnet.value+"!");
		f.amountsubnet.style.backgroundColor="#ffb3b3";
		f.amountsubnet.focus();
	}
	else
	{
		f.amountsubnet.style.backgroundColor="#f6f6f6";		
		Dialog.info({url: "ajax_view.php?what=generatesubnet&amountsubnet="+f.amountsubnet.value+"&cidr="+f.snmbits.value+"&oct1="+f.oct1.value+"&oct2="+f.oct2.value+"&oct3="+f.oct3.value+"&oct4="+f.oct4.value+"&numnode="+f.numnode.value+"&network_id="+f.network.value+"&mask="+f.snm1.value+"."+f.snm2.value+"."+f.snm3.value+"."+f.snm4.value, options: {method: 'post'}}, {top: 10, width:1024, height:550, className: "alphacube"});
	}
  
}
var info_customer = new Array();

<?php
$request=mysql_query("select name,id FROM $name_table_customer") or die ("ERROR!");

while($tmp_request=mysql_fetch_array($request))
{
	echo "info_customer[$tmp_request[id]] = \"$tmp_request[name]\";\n";
}
?>

var info_vlan = new Array();

<?php
$request=mysql_query("select name,id FROM $name_table_vlan") or die ("ERROR!");

while($tmp_request=mysql_fetch_array($request))
{
	echo "info_vlan[$tmp_request[id]] = \"$tmp_request[name]\";\n";
}
?>
</script>
<form method="post" name="add_subnet" id="add_subnet" action="ajax_view.php?what=generatesubnet">
	<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_POSSIBLE; ?> <?php echo TXT_SUBNET; ?></label>
			<input type="text" name="numsubnet" id="numsubnet" disabled>
			<label><?php echo TXT_AMOUNT_OF; ?> <?php echo TXT_NODESHOSTS; ?> <?php echo TXT_SUBNET; ?></label>
			<input type="text" name="numnode" style="color:#978e7b;" readonly>
			<input type="hidden" name="broadcastaddress" disabled>
			<input type="hidden" name="range_1" disabled>
			<input type="hidden" name="range_2" disabled>
		</fieldset>
		<legend><img src="images/add.gif" alt="Add" align="top"> <?php echo TXT_AUTOMATICALLY; ?> <?php echo TXT_ADD; ?> <?php echo TXT_SUBNET; ?></legend>
		<label><?php echo TXT_NETWORK; ?></label>
		<select name="network" id="network" size="1" onchange="addnetworkinfo(this.form,this.value);calcamountsubnet(this.form);">
			<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_ident == $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_AMOUNT_OF; ?> <?php echo TXT_SUBNET; ?></label>
			<input type="text" name="amountsubnet" id="amountsubnet" size="5" onKeyPress="return numbersonly(this, event);"><br>
			<table border="0" cellspacing="0" cellpadding="0">
				<tr>
					<td width="60%"><label><?php echo TXT_SUBNET; ?> <?php echo TXT_MASK; ?></label>
			<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);"><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);"><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);"><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);"><span style="position:relative;top:-10px;"> or /</span><input type="text" name="snmbits" id="snmbits" size="3" maxlength="3" maxlength="3" onKeyPress="return numbersonly(this, event);" onkeyup="calcmask(this.form);" onchange="calcsubnet(this.form);"></td>
					<td style="padding-top:11px;padding-left:10px;"><script language="JavaScript">
	var A_TPL3h = {
		'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_INIT3h = {
		's_form' : 0,
		's_name': 'snmbits',
		'n_minValue' : 1,
		'n_maxValue' : 32,
		'n_value' : 1,
		'n_step' : 1
	}

	new slider(A_INIT3h, A_TPL3h);
</script></td>
				</tr>
			</table><br>
			<input type="hidden" name="oct1" id="oct1" size="3" maxlength="3"><input type="hidden" name="oct2" id="oct2" size="3" maxlength="3"><input type="hidden" name="oct3" id="oct3" size="3" maxlength="3"><input type="hidden" name="oct4" id="oct4" size="3" maxlength="3">
		<input type="button" name="generatesubnetbutton" id="generatesubnetbutton" value="<?php echo TXT_GENERATE_SUBNET; ?>" onclick="openconfirm(this.form);" disabled>
	</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");
		 
	 
		 
</script>
<?php
}
else if ($add=="confirm")
{
?>
<fieldset class="form" style="min-width:1000px;">
<legend><img src="images/add.gif" alt="Add" align="top"> <?php echo TXT_AUTOMATICALLY; ?> <?php echo TXT_ADD; ?> <?php echo TXT_SUBNET; ?></legend>
<?php
		$network_id = explode (";",$network_id);
		$overlap = array ();
		$overlap2 = array ();
		for ($i = 1;$i < $amountsubnet+1;$i++)
		{
			$address = (intval(ip2long($_POST['address_'.$i])));
			$address2 = ($_POST['address_'.$i]);
			$subnet_size_array = calculate_ip_address($address2,$mask);
			$subnet_size = $subnet_size_array[4];
				
			$request = mysql_query ("SELECT id,network_id,address,mask FROM $name_table_subnet WHERE network_id=$network_id[0]") or die ("ERROR!");
			$requestcount=mysql_query("SELECT count(id) FROM $name_table_subnet WHERE network_id=$network_id[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 = 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 $_POST['address_'.$i]."/".$_POST['cidr_'.$i];
					echo TXT_OVERLAP_EXISTING_SUBNET; echo "(<a href=\"index.php?include=mainview&page=subnet&action=view&id=$tmp_request[id]\">$tmp_request[address]$cidr[cidr]</a>)</div>";
					array_push($overlap,$address2);
				}
			}
			array_push($overlap2,$address2);
		}

		$result = array_diff($overlap2, $overlap);
		$result_count = sizeof($result);
		$i=($amountsubnet - ($result_count-1));
		foreach ($result as $resultsubnet)
		{
			$insert_name = $_POST['name_'.$i];
			$insert_status = $_POST['status_'.$i];
			$insert_description = $_POST['description_'.$i];
			$insert_customer = $_POST['customer_array_'.$i];
			$insert_vlan = $_POST['vlan_array_'.$i];
			
			$query=mysql_query("INSERT INTO $name_table_subnet VALUES ('',\"$network_id[0]\",\"$insert_name\",\"$resultsubnet\",\"$mask\",\"$insert_status\",\"$insert_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();
				
				$currentdate = date('Y-m-d');
				$currenttime = date("H:i:s"); 
				
				user_log ($_SESSION['login'],$name_table_users,$name_table_users_log,"add","subnet","$insert_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 '.$insert_name.'('.$subnet_id.')');
					$syslog->SetServer($arrayvalue);
					$syslog->Send();
				}
				
				$customerarray = explode (";",$insert_customer);
			
				foreach ($customerarray as $arrayvalue)
				{
					$query2=mysql_query("INSERT INTO $name_table_link_customer_subnet VALUES (\"$arrayvalue\",\"$subnet_id\")") or die ("ERROR!");
				}

				$vlanarray = explode (";",$insert_vlan);
				
				if ($vlanarray[0] == "new_vlan")
				{					
					$requestcountnewvlan=mysql_query("SELECT count(id) FROM $name_table_vlan WHERE number=\"$vlanarray[2]\" AND name=\"$vlanarray[1]\"") or die ("ERROR!1");
					$all_new_vlan = mysql_fetch_array($requestcountnewvlan);
					
					if ($all_new_vlan[0]=="0")
					{
						$query3=mysql_query("INSERT INTO $name_table_vlan VALUES ('',\"$vlanarray[2]\",\"$vlanarray[1]\",'')") or die ("ERROR!2");
						$vlan_id = mysql_insert_id();
						$query4=mysql_query("INSERT INTO $name_table_link_subnet_vlan VALUE (\"$subnet_id\",\"$vlan_id\")") or die ("ERROR!3");
					}
					else
					{
						$request_new_vlan = mysql_query("SELECT id FROM $name_table_vlan WHERE number=\"$vlanarray[2]\" AND name=\"$vlanarray[1]\"") or die ("ERROR!4");
						while($tmp_request_new_vlan=mysql_fetch_array($request_new_vlan))
						{
							$query5=mysql_query("INSERT INTO $name_table_link_subnet_vlan VALUE (\"$subnet_id\",\"$tmp_request_new_vlan[id]\")") or die ("ERROR!5");
						}
					}
					
				}
				else
				{
					$query5=mysql_query("INSERT INTO $name_table_link_subnet_vlan VALUE (\"$subnet_id\",\"$vlanarray[0]\")") or die ("ERROR!");
				}
				
				echo "<div id=\"status\" style=\"color:green\"><img src=\"images/info.png\" alt=\"INFO\"><br><br>";
				echo TXT_SUCCESSFULLY_ADD_SUBNET; echo "$resultsubnet $mask</div>";
			}
			
			$i++;
		}
}
?>
</fieldset>
Return current item: OpenDataCenter Network Management System