Location: PHPKode > projects > OpenDataCenter Network Management System > include/mainview_page/device/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("vlan");
opt.saveRemovedLeftOptions("removed_vlan");
</script>
<form method="post" action="index.php?include=mainview&page=device&action=add&add=ok" name="add_device" onSubmit="return checkform();">
	<fieldset class="form">
		<legend><img src="images/add.gif" alt="Add" align="top"> <?php echo TXT_ADD; ?> <?php echo TXT_DEVICE; ?></legend>
		<?php
			if (isset($model_ident))
			{
			
			$request_model=mysql_query("select id,name,category_id,manufacturer_id FROM $name_table_model") or die ("ERROR!");

			while($tmp_request_model=mysql_fetch_array($request_model))
			{
				$manufacturer_ident=$tmp_request_model['manufacturer_id'];
				$category_ident=$tmp_request_model['category_id'];
			}		
?>
		<label><?php echo TXT_MANUFACTURER; ?></label>
		<select name="manufacturer" id="manufacturer" size="1" onchange="document.getElementById('categorydiv').style.display='block';">
			<option value="null"><?php echo TXT_SELECT_MANUFACTURER; ?></option>
		<?php
		$request=mysql_query("select id,name FROM $name_table_manufacturer") or die ("ERROR!");

		while($tmp_request=mysql_fetch_array($request))
		{
			if ($manufacturer_ident == $tmp_request['id'])
			{
				echo "<option value=\"$tmp_request[id]\" SELECTED>$tmp_request[name]</option>";
			}
			else
			{
				echo "<option value=\"$tmp_request[id]\">$tmp_request[name]</option>";
			}
		}
		?>
		</select>
		<div id="categorydiv" style="display:block;">
		<label><?php echo TXT_CATEGORY; ?></label>
		<select name="category" id="category" size="1" onchange="javascript:urlredirectdiv('ajax_view.php?what=model&category_id='+this.value+'&manufacturer_id='+manufacturer.value,'ajaxmodelarea');">
			<option value="null"><?php echo TXT_SELECT_CATEGORY; ?></option>
		<?php
		$request=mysql_query("select id,name FROM $name_table_category") or die ("ERROR!");

		while($tmp_request=mysql_fetch_array($request))
		{
			if ($category_ident == $tmp_request['id'])
			{
				echo "<option value=\"$tmp_request[id]\" SELECTED>$tmp_request[name]</option>";
			}
			else
			{
				echo "<option value=\"$tmp_request[id]\">$tmp_request[name]</option>";
			}
		}
		?>
		</select>
		<div id="ajaxmodelarea">
		<label><?php echo TXT_MODEL; ?></label>
		<select name="model" id="model" size="1">
			<option value="null"><?php echo TXT_SELECT_MODEL; ?></option>
		<?php
		$request=mysql_query("select id,name FROM $name_table_model WHERE category_id=$category_ident AND manufacturer_id=$manufacturer_ident") or die ("ERROR!");

		while($tmp_request=mysql_fetch_array($request))
		{
			if ($model_ident == $tmp_request['id'])
			{
				echo "<option value=\"$tmp_request[id]\" SELECTED>$tmp_request[name]</option>";
			}
			else
			{
				echo "<option value=\"$tmp_request[id]\">$tmp_request[name]</option>";
			}
		}
		?>
		</select>
		</div></div>
		<?php
			}
			else
			{
		?>
		<label><?php echo TXT_MANUFACTURER; ?></label>
		<select name="manufacturer" id="manufacturer" size="1" onchange="document.getElementById('categorydiv').style.display='block';">
			<option value="null"><?php echo TXT_SELECT_MANUFACTURER; ?></option>
		<?php
		$request=mysql_query("select id,name FROM $name_table_manufacturer") or die ("ERROR!");

		while($tmp_request=mysql_fetch_array($request))
		{
			if ($manufacturer_ident == $tmp_request['id'])
			{
				echo "<option value=\"$tmp_request[id]\" SELECTED>$tmp_request[name]</option>";
			}
			else
			{
				echo "<option value=\"$tmp_request[id]\">$tmp_request[name]</option>";
			}
		}
		?>
		</select>
		<div id="categorydiv" style="display:none;">
		<label><?php echo TXT_CATEGORY; ?></label>
		<select name="category" id="category" size="1" onchange="javascript:urlredirectdiv('ajax_view.php?what=model3&category_id='+this.value+'&manufacturer_id='+manufacturer.value,'ajaxmodelarea');">
			<option value="null"><?php echo TXT_SELECT_CATEGORY; ?></option>
		<?php
		$request=mysql_query("select id,name FROM $name_table_category") or die ("ERROR!");

		while($tmp_request=mysql_fetch_array($request))
		{
			echo "<option value=\"$tmp_request[id]\">$tmp_request[name]</option>";
		}
		?>
		</select>
		<div id="ajaxmodelarea"></div></div>
		<?php
		}
		?>
		<div id="snmpajaxarea">
		<label><?php echo TXT_NAME; ?></label>
		<input type="text" name="name" size="50"><br>
		<label><?php echo TXT_LOCATION; ?></label>
		<select name="location" onchange="javascript:urlredirectdiv('ajax_view.php?what=ajaxrack&location_id='+this.value,'ajaxrackarea');">
		<option value="null"><?php echo TXT_SELECT_LOCATION; ?></option>
			<?php
			$request=mysql_query("SELECT name,id FROM $name_table_location ORDER BY name ASC") or die ("ERROR!");

			while($tmp_request=mysql_fetch_array($request))
			{
				echo "<option value=\"$tmp_request[id]\">$tmp_request[name]</option>\n";
			}
			?>
		</select>
		<div id="ajaxrackarea"></div>
		<fieldset class="form">
			<legend><?php echo TXT_IP_ADDRESS; ?></legend>
				<label><b><?php echo TXT_DEFAULT; ?> <?php echo TXT_IP_ADDRESS; ?></b></label>
				<div style="margin-left:20px;"><label><?php echo TXT_NETWORK; ?></label>
				<select name="network" id="network" size="1" onchange="javascript:urlredirectdiv('ajax_view.php?what=ajaxsubnet2&network_id='+this.value,'ajaxsubnetarea');">
					<option value="null"><?php echo TXT_SELECT_NETWORK_SEGMENT; ?></option>
				<?php
				$request=mysql_query("select id,name FROM $name_table_network") or die ("ERROR!");

				while($tmp_request=mysql_fetch_array($request))
				{
					echo "<option value=\"$tmp_request[id]\">$tmp_request[name]</option>";
				}
				?>
				</select>
				<div id="ajaxsubnetarea"></div>
				<div id="ajaxipaddressarea"></div></div>
				<div id="otherip" style="display:none;">
				<label><b><?php echo TXT_OTHER_IP_ADDRESS; ?></b></label>
				<table id="otheripaddress" width="100%" style="margin-left:20px;" cellpadding="2" cellspacing="0"><tbody></tbody></table><br>
				<input type="hidden" name="amountotherip" id="amountotherip" value="0">
				<input type="button" value="Add" name="button_addone" id="button_addone" onclick="addRow('otheripaddress','amountotherip');">
				</div>
		</fieldset>
		<fieldset class="form" id="addvlan" style="display:none;">
			<legend><?php echo TXT_VLAN; ?></legend>
			<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				
				$request3=mysql_query("SELECT id,name FROM $name_table_vlan") or die ("ERROR!");

				while($tmp_request3=mysql_fetch_array($request3))
				{
					echo "<option value=\"$tmp_request3[id]\">$tmp_request3[name]</option>";
				}
				?>
				</select>
				</td>
			</tr>
			</table>
		</fieldset>			
		<fieldset class="form">
			<legend><?php echo TXT_OPTIONAL; ?></legend>		
		<label><?php echo TXT_SNMP_READ_COMMUNITY_PORT; ?></label>
		<input type="text" name="snmpreadcommunity" id="snmpreadcommunity" size="15" maxlength="15" value="public"> <input type="text" name="snmpport" id="snmpport" size="15" maxlength="15" value="161">
<br>
		<label><?php echo TXT_DESCRIPTION; ?></label>
		<textarea name="description" rows="10" cols="50"></textarea>
		</fieldset><br>
		<input type="hidden" name="checkaddvlan" id="checkaddvlan" value="0">
		<input type="hidden" name="vlan" value="">
		<input type="hidden" name="removed_vlan" value="">
		<input type="submit" name="submit" value="<?php echo TXT_ADD; ?>"> <input type="button" value="<?php echo TXT_BACK; ?>" onClick="javascript:history.go(-1);">
	</fieldset>
</form>
<script language="JavaScript" type="text/javascript">
	 var frmvalidator = new Validator("add_device");
		 frmvalidator.addValidation("name","req","Please enter the name of the device");
		 frmvalidator.addValidation("name","maxlen=255",
"Max length for Name is 255");
		 frmvalidator.addValidation("manufacturer","dontselect=0","Please select the manufacturer of the device");
		 frmvalidator.addValidation("category","dontselect=0","Please select the category of the device");
		 frmvalidator.addValidation("location","dontselect=0","Please select the location of the device");	 
</script>
<?php
}
else if ($add=="ok")
{	
	if ((isset($new_address)) && ($new_address == "yes"))
	{
		$address = $oct1.".".$oct2.".".$oct3.".".$oct4;
		$mac = $mac1.".".$mac2.".".$mac3;

		$query_address=mysql_query("INSERT INTO $name_table_ip_address VALUES ('',\"$subnet\",\"$address\",\"$mac\",\"$new_address_name\",\"$new_address_description\",\"0\")") or die ("ERROR!");
		
		$new_ip_address_id = mysql_insert_id();
		
		$model_value = explode (",", $_POST['model']);
		
		$query=mysql_query("INSERT INTO $name_table_device VALUES ('',\"$name\",\"$new_ip_address_id\", \"$model_value[0]\", \"$rack\", \"$description\", \"$snmpreadcommunity\", \"$snmpport\")") or die ("ERROR!");
		
		$device_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","ip address","$address ($new_ip_address_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 ip address '.$address.'('.$new_ip_address_id.')');
			$syslog->SetServer($arrayvalue);
			$syslog->Send();
		}
		
		
	}
	else
	{
		$model_value = explode (",", $_POST['model']);
		
		if (isset($address))
		{
			$query=mysql_query("INSERT INTO $name_table_device VALUES ('',\"$name\",\"$address\", \"$model_value[0]\", \"$rack\", \"$description\", \"$snmpreadcommunity\", \"$snmpport\")") or die ("ERROR!");
			
			$device_id = mysql_insert_id();
		}
		else
		{
			$query=mysql_query("INSERT INTO $name_table_device VALUES ('',\"$name\",\"\", \"$model_value[0]\", \"$rack\", \"$description\", \"$snmpreadcommunity\", \"$snmpport\")") or die ("ERROR!");
			
			$device_id = mysql_insert_id();
		}
	}

	
	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
	{
		
		if ($checkaddvlan=="1")
		{
			$vlan_value = explode (";", $_POST['vlan']);
	
			foreach ($vlan_value as $arrayvalue)
			{
				$query_vlan=mysql_query("INSERT INTO $name_table_link_vlan_device VALUES (\"$arrayvalue\",\"$device_id\")") or die ("ERROR!");
			}	
		}
		
		$index = 0;
		
		if ($amountotherip > 0)
		{
			for($i=1; $i < $amountotherip+1; $i++)
			{
				if ((isset($_POST['new_address_'.$i])) || (isset($_POST['address_'.$i])))
				{
					if ($_POST['new_address_'.$i] == "yes")
					{
						$address = $_POST['oct1_'.$i].".".$_POST['oct2_'.$i].".".$_POST['oct3_'.$i].".".$_POST['oct4_'.$i];
						$mac = $_POST['mac1_'.$i].".".$_POST['mac2_'.$i].".".$_POST['mac3_'.$i];
						$subnet = $_POST['subnet_'.$i];
						$new_address_name = $_POST['new_address_name_'.$i];
						$new_address_description = $_POST['new_address_description_'.$i];

						$query_address=mysql_query("INSERT INTO $name_table_ip_address VALUES ('',\"$subnet\",\"$address\",\"$mac\",\"$new_address_name\",\"$new_address_description\",\"0\")") or die ("ERROR!");
						
						$new_ip_address_id = mysql_insert_id();
						
						$query_link_device_address = mysql_query("INSERT INTO $name_table_link_device_ip_address VALUES (\"$device_id\",\"$new_ip_address_id\")") or die ("ERROR!");
					}
					else
					{
						$address = $_POST['address_'.$i];
						
						$query_link_device_address = mysql_query("INSERT INTO $name_table_link_device_ip_address VALUES (\"$device_id\",\"$address\")") or die ("ERROR!");
					}
				}
			}
		}
		
		$model_value = explode (",", $_POST['model']);
		
		$request2=mysql_query("SELECT * FROM $name_table_model_slot WHERE model_id=$model_value[0]") or die ("ERROR!");

		while($tmp_request2=mysql_fetch_array($request2))
		{
			$query2=mysql_query("INSERT INTO $name_table_slot VALUES ('',\"$device_id\", \"$tmp_request2[number]\", \"$description\")") or die ("ERROR!");
			$slot_id = mysql_insert_id();
			
			for ($x=1; $x < ($tmp_request2['interface_density']+1); $x++)
			{
				$index = $index + 1;				
				$name_interface = "$tmp_request2[number]/$index";
				
				$query3=mysql_query("INSERT INTO $name_table_interface VALUES ('',\"$slot_id\",\"$tmp_request2[interface_type]\", \"$tmp_request2[interface_speed]\", \"$index\",\"$name_interface\", \"$description\")") 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","device","$name ($device_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 device '.$name.'('.$device_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_DEVICE;
		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=device&action=add\">";
		echo TXT_ADD_ANOTHER_DEVICE;
		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