Location: PHPKode > projects > OpenDataCenter Network Management System > include/mainview_page/network/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=network&action=add&add=ok" name="add_network">
	<fieldset class="form" style="min-width:1000px;">
		<fieldset class="form" style="width:420px; float: right;">
			<legend style="font-weight:normal;"><img src="images/stats.gif" alt="Stats" align="top"> <?php echo TXT_NETWORK; ?> <?php echo TXT_INFORMATION; ?> <?php echo TXT_STATISTIC; ?></legend>
			<label><?php echo TXT_NETWORK; ?> <?php echo TXT_TYPE; ?></label>
			<input type="text" name="nwclass" size="7" maxlength="7" disabled> <input type="text" name="subsuper" size=14 maxlength=14 disabled> <input type="text" name="nwclass1" size=7 maxlength=7 disabled>
			<label><?php echo TXT_AMOUNT_OF; ?> <?php echo TXT_SUBNET; ?></label>
			<input type="text" name="numsubnet" disabled>
			<label><?php echo TXT_AMOUNT_OF; ?> <?php echo TXT_NODESHOSTS; ?> <?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>			
		</fieldset>
		<legend><img src="images/add.gif" alt="Add" align="top"> <?php echo TXT_ADD; ?> <?php echo TXT_NETWORK; ?></legend>
		<label><?php echo TXT_NAME; ?></label>
		<input type="text" name="name" size="50"><br>
		<label><?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="networktype(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="networktype(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="networktype(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="networktype(this.form);"><br>
		<label><?php echo TXT_MASK; ?></label>
		<span style="float:right;margin:0px;margin-right:100px;"><script language="JavaScript">
	var A_TPL5h = {
		'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_INIT5h = {
		's_form' : 0,
		's_name': 'snmbits',
		'n_minValue' : 1,
		'n_maxValue' : 32,
		'n_value' : 1,
		'n_step' : 1
	}

	new slider(A_INIT5h, A_TPL5h);
</script></span>
		<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);calcsubnet(this.form);networktype(this.form);calcbroadcastaddress(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);calcsubnet(this.form);networktype(this.form);calcbroadcastaddress(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);calcsubnet(this.form);networktype(this.form);calcbroadcastaddress(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);calcsubnet(this.form);calcbroadcastaddress(this.form);"><span style="position:relative;top:-10px;"> or /</span><input type="text" name="snmbits" size="2" maxlength="2" value="" onKeyPress="return numbersonly(this, event);" onkeyup="calcmask(this.form);calcnode(this.form);calcsubnet(this.form);networktype(this.form);calcbroadcastaddress(this.form);A_SLIDERS[0].f_setValue(this.value);"> <br>
		<label style="clear:left;"><?php echo TXT_SUBNETTED; ?> ?</label>
		<select name="subnetted" onchange="urlredirectdiv('ajax_view.php?what=subnetted&value='+this.value,'ajaxsubnetted');">
			<option value="0"><?php echo TXT_NO; ?></option>
			<option value="1"><?php echo TXT_YES; ?></option>
		</select>
		<div id="ajaxsubnetted">
		<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=''}">		
		</div>
		<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_network");
		 frmvalidator.addValidation("name","req","Please enter the name of the network");
		 frmvalidator.addValidation("name","maxlen=255",
"Max length for name is 255");
		 
		 frmvalidator.addValidation("oct1","req","Please enter the first part of the Address of the network");
		 frmvalidator.addValidation("oct1","num","Please enter numeric data for the first part of the Address of the network");
		 frmvalidator.addValidation("oct1","maxlen=3","Max length for the first part of the Address of the network is 3");
		 frmvalidator.addValidation("oct2","req","Please enter the second part of the Address of the network");
		 frmvalidator.addValidation("oct2","num","Please enter numeric data for the second part of the Address of the network");
		 frmvalidator.addValidation("oct2","maxlen=3","Max length for the second part of the Address of the network is 3");
		 frmvalidator.addValidation("oct3","req","Please enter the third part of the Address of the network");
		 frmvalidator.addValidation("oct3","num","Please enter numeric data for the third part of the Address of the network");
		 frmvalidator.addValidation("oct3","maxlen=3","Max length for the third part of the Address of the network is 3");
		 frmvalidator.addValidation("oct4","req","Please enter the fourth part of the Address of the network");
		 frmvalidator.addValidation("oct4","num","Please enter numeric data for the fourth part of the Address of the network");
		 frmvalidator.addValidation("oct4","maxlen=3","Max length for the fourth part of the Address of the network is 3");
		 
		 frmvalidator.addValidation("snm1","req","Please enter the first part of the Mask of the network");
		 frmvalidator.addValidation("snm1","num","Please enter numeric data for the first part of the Mask of the network");
		 frmvalidator.addValidation("snm1","maxlen=3","Max length for the first part of the Mask of the network is 3");
		 frmvalidator.addValidation("snm2","req","Please enter the second part of the Mask of the network");
		 frmvalidator.addValidation("snm2","num","Please enter numeric data for the second part of the Mask of the network");
		 frmvalidator.addValidation("snm2","maxlen=3","Max length for the second part of the Mask of the network is 3");
		 frmvalidator.addValidation("snm3","req","Please enter the third part of the Mask of the network");
		 frmvalidator.addValidation("snm3","num","Please enter numeric data for the third part of the Mask of the network");
		 frmvalidator.addValidation("snm3","maxlen=3","Max length for the third part of the Mask of the network is 3");
		 frmvalidator.addValidation("snm4","req","Please enter the fourth part of the Mask of the network");
		 frmvalidator.addValidation("snm4","num","Please enter numeric data for the fourth part of the Mask of the network");
		 frmvalidator.addValidation("snm4","maxlen=3","Max length for the fourth part of the Mask of the network is 3");		 
		 
</script>
<?php
}
else if ($add=="ok")
{

	$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,address,mask FROM $name_table_network") or die ("ERROR!");
	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))))
		{
			$overlap = "overlap";
			$overlap_address = $tmp_request['address'];
			$overlap_cidr = $cidr_network['cidr'];			
		}
	}
	if (isset($overlap) && ($overlap == "overlap"))
	{
		echo "<div id=\"status\" style=\"color:red\"><img src=\"images/warning.png\" alt=\"WARNING\" align=\"center\"> ";
		echo "Network ".$address2.$cidr['cidr'];
		echo " can't be created : overlap with existing network (<a href=\"index.php?include=mainview&page=network&action=view&id=$tmp_request[id]\">$overlap_address$overlap_cidr</a>)</div><br><center><a href=\"javascript:history.go(-2);\">Back</a></center>";
	}
	else
	{
		$address = $oct1.".".$oct2.".".$oct3.".".$oct4;
		$mask = $snm1.".".$snm2.".".$snm3.".".$snm4;
		$query=mysql_query("INSERT INTO $name_table_network VALUES ('',\"$name\",\"$address\",\"$mask\",\"$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
		{
			$network_id = mysql_insert_id();
			
			if ($subnetted=="0")
			{
				$query=mysql_query("INSERT INTO $name_table_subnet VALUES ('',\"$network_id\",\"$name\",\"$address\",\"$mask\",\"0\",\"$description\")") or die ("ERROR!");
			
				$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","network","$name ($network_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 network '.$name.'('.$network_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_NETWORK;
				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><option value=\"index.php?include=mainview&page=network&action=add\">";
				echo TXT_ADD_ANOTHER_NETWORK;
				echo "</option></select></form></div><br><br><a href=\"javascript:history.go(-2);\">";
				echo TXT_BACK;
				echo "</a></center>";
			}
			else
			{
						
				$currentdate = date('Y-m-d');
				$currenttime = date("H:i:s"); 
				
				user_log ($_SESSION['login'],$name_table_users,$name_table_users_log,"add","network","$name ($network_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 network '.$name.'('.$network_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_NETWORK;
				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><option value=\"index.php?include=mainview&page=network&action=add\">";
				echo TXT_ADD_ANOTHER_NETWORK;
				echo "</option><option value=\"index.php?include=mainview&page=subnet&action=automatically_add&network_ident=$network_id\">";
				echo TXT_ADD_CHILD_SUBNETS_AUTOMATICALLY;
				echo "</option><option value=\"index.php?include=mainview&page=subnet&action=manually_add&network_ident=$network_id\">";
				echo TXT_ADD_CHILD_SUBNETS_MANUALLY;
				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