<?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($edit))
{
$request=mysql_query("SELECT * FROM $name_table_device WHERE id=\"$_GET[id]\"") or die ("ERROR!");
while($tmp_request=mysql_fetch_array($request))
{
?>
<form method="post" action="index.php?include=mainview&page=device&action=edit&edit=ok&id=<?php echo $id;?>" name="edit_device">
<fieldset class="form">
<legend><img src="images/edit.gif" alt="Edit" align="top"> <?php echo TXT_EDIT; ?> <?php echo TXT_DEVICE; ?></legend>
<label><?php echo TXT_MANUFACTURER; ?></label>
<select name="manufacturer" id="manufacturer" size="1">
<option value="null"><?php echo TXT_SELECT_MANUFACTURER; ?></option>
<?php
$requestcountmodel=mysql_query("SELECT count(*) FROM $name_table_model WHERE id=$tmp_request[model_id]") or die ("ERROR!");
$all_model = mysql_fetch_array($requestcountmodel);
if ($all_model[0]!=0)
{
$request2=mysql_query("SELECT id,name FROM $name_table_manufacturer") or die ("ERROR!");
while($tmp_request2=mysql_fetch_array($request2))
{
$request3=mysql_query("SELECT manufacturer_id FROM $name_table_model WHERE id=$tmp_request[model_id]") or die ("ERROR!");
while($tmp_request3=mysql_fetch_array($request3))
{
if ($tmp_request3['manufacturer_id'] == $tmp_request2['id'])
{
echo "<option value=\"$tmp_request2[id]\" selected>$tmp_request2[name]</option>";
}
else
{
echo "<option value=\"$tmp_request2[id]\">$tmp_request2[name]</option>";
}
}
}
}
else
{
$request2=mysql_query("SELECT id,name FROM $name_table_manufacturer") or die ("ERROR!");
while($tmp_request2=mysql_fetch_array($request2))
{
echo "<option value=\"$tmp_request2[id]\">$tmp_request2[name]</option>";
}
}
?>
</select>
<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
if ($all_model[0]!=0)
{
$request4=mysql_query("select id,name FROM $name_table_category") or die ("ERROR!");
while($tmp_request4=mysql_fetch_array($request4))
{
$request5=mysql_query("SELECT category_id FROM $name_table_model WHERE id=$tmp_request[model_id]") or die ("ERROR!");
while($tmp_request5=mysql_fetch_array($request5))
{
$category_id=$tmp_request5['category_id'];
if ($tmp_request5['category_id'] == $tmp_request4['id'])
{
echo "<option value=\"$tmp_request4[id]\" selected>$tmp_request4[name]</option>";
}
else
{
echo "<option value=\"$tmp_request4[id]\">$tmp_request4[name]</option>";
}
}
}
}
else
{
$request4=mysql_query("select id,name FROM $name_table_category") or die ("ERROR!");
while($tmp_request4=mysql_fetch_array($request4))
{
echo "<option value=\"$tmp_request4[id]\">$tmp_request4[name]</option>";
}
}
?>
</select>
<div id="ajaxmodelarea">
<?php
if ($all_model[0]!=0)
{
?>
<label><?php echo TXT_MODEL; ?></label>
<select name="model" id="model" size="1">
<option value="null"><?php echo TXT_SELECT_MODEL; ?></option>
<?php
$request6=mysql_query("select id,name FROM $name_table_model WHERE category_id=$category_id") or die ("ERROR!");
while($tmp_request6=mysql_fetch_array($request6))
{
if ($tmp_request6['id'] == $tmp_request['model_id'])
{
echo "<option value=\"$tmp_request6[id]\" selected>$tmp_request6[name]</option>";
}
else
{
echo "<option value=\"$tmp_request6[id]\">$tmp_request6[name]</option>";
}
}
?>
</select>
<?php
}
?>
</div>
<fieldset class="form">
<legend><img src="images/add.gif" alt="Add" align="top"> <?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');document.getElementById('ajaxipaddressarea').innerHTML='';">
<option value="null"><?php echo TXT_SELECT_NETWORK_SEGMENT; ?></option>
<?php
$requestcountipaddress=mysql_query("SELECT count(*) FROM $name_table_ip_address WHERE id=$tmp_request[ip_address_id]") or die ("ERROR!");
$all_ipaddress = mysql_fetch_array($requestcountipaddress);
if ($all_ipaddress[0]!=0)
{
$request_ip_address = mysql_query("select id,subnet_id FROM $name_table_ip_address WHERE id=$tmp_request[ip_address_id]") or die ("ERROR!");
while($tmp_request_ip_address=mysql_fetch_array($request_ip_address))
{
$request_subnet=mysql_query("select id,network_id FROM $name_table_subnet WHERE id=$tmp_request_ip_address[subnet_id]") or die ("ERROR!");
while($tmp_request_subnet=mysql_fetch_array($request_subnet))
{
$request_network=mysql_query("select id,name FROM $name_table_network") or die ("ERROR!");
while($tmp_request_network=mysql_fetch_array($request_network))
{
if ($tmp_request_subnet['network_id']==$tmp_request_network['id'])
{
$subnet_network_id=$tmp_request_network['id'];
echo "<option value=\"$tmp_request_network[id]\" selected>$tmp_request_network[name]</option>";
}
else
{
echo "<option value=\"$tmp_request_network[id]\">$tmp_request_network[name]</option>";
}
}
}
}
}
else
{
$request_network=mysql_query("select id,name FROM $name_table_network") or die ("ERROR!");
while($tmp_request_network=mysql_fetch_array($request_network))
{
echo "<option value=\"$tmp_request_network[id]\">$tmp_request_network[name]</option>";
}
}
?>
</select>
<div id="ajaxsubnetarea">
<?php
if ($all_ipaddress[0]!=0)
{
?>
<label><?php echo TXT_SUBNET; ?></label>
<select name="subnet" id="subnet" size="1" onchange="javascript:urlredirectdiv('ajax_view.php?what=ajaxipaddress&subnet_id='+this.value,'ajaxipaddressarea');">
<option value="null"><?php echo TXT_SELECT_SUBNET; ?></option>
<?php
$request_ip_address = mysql_query("select id,subnet_id,ip FROM $name_table_ip_address WHERE id=$tmp_request[ip_address_id]") or die ("ERROR!");
while($tmp_request_ip_address=mysql_fetch_array($request_ip_address))
{
$addressarray=explode(".",$tmp_request_ip_address['ip']);
$ip_address=$tmp_request_ip_address['ip'];
$request_subnet=mysql_query("select id,name,network_id FROM $name_table_subnet WHERE network_id=$subnet_network_id") or die ("ERROR!");
while($tmp_request_subnet=mysql_fetch_array($request_subnet))
{
if ($tmp_request_subnet['id']==$tmp_request_ip_address['subnet_id'])
{
$subnet_id = $tmp_request_subnet['id'];
echo "<option value=\"$tmp_request_subnet[id]\" selected>$tmp_request_subnet[name]</option>";
}
else
{
echo "<option value=\"$tmp_request_subnet[id]\">$tmp_request_subnet[name]</option>";
}
}
}
?>
</select>
<?php
}
?>
</div>
<div id="ajaxipaddressarea">
<?php
if ($all_ipaddress[0]!=0)
{
?>
<label><?php echo TXT_IP_ADDRESS; ?></label>
<input type="text" name="oct1" id="oct1" size="3" maxlength="3" value="<?php echo $addressarray[0]; ?>" onkeydown="keynext(event.keyCode,'oct2');if (event.keyCode==190) return false;" onKeyPress="return numbersonly(this, event);" disabled><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" value="<?php echo $addressarray[1]; ?>"onKeyPress="return numbersonly(this, event);" disabled><span style="top: -6px;position:relative;">.</span><input type="text" name="oct3" id="oct3" size="3" maxlength="3" value="<?php echo $addressarray[2]; ?>"onkeydown="keynext(event.keyCode,'oct4');if (event.keyCode==190) return false;" onKeyPress="return numbersonly(this, event);" disabled><span style="top: -6px;position:relative;">.</span><input type="text" name="oct4" id="oct4" size="3" maxlength="3" value="<?php echo $addressarray[3]; ?>" onKeyPress="return numbersonly(this, event);" disabled> <input type="button" value="Change" onclick="urlredirectdiv('ajax_view.php?what=ajaxipaddress2&subnet_id=<?php echo $subnet_id; ?>','ajaxipaddressarea');"><br>
<?php
}
?>
</div>
</div>
<label><b><?php echo TXT_OTHER_IP_ADDRESS; ?></b></label>
<table id="otheripaddress" width="100%" style="margin-left:20px;" cellpadding="2" cellspacing="0">
<tbody>
<?php
$i=1;
$amountotherip = 0;
$request_link_device_ip_address = mysql_query("SELECT * FROM $name_table_link_device_ip_address WHERE device_id=$id") or die ("ERROR!");
while($tmp_request_link_device_ip_address=mysql_fetch_array($request_link_device_ip_address))
{
$amountotherip++;
$request_ip_address = mysql_query("select id,ip FROM $name_table_ip_address WHERE id=$tmp_request_link_device_ip_address[ip_address_id]") or die ("ERROR!");
while($tmp_request_ip_address=mysql_fetch_array($request_ip_address))
{
$addressarray=explode(".",$tmp_request_ip_address['ip']);
$ip_address=$tmp_request_ip_address['ip'];
echo "<tr><td valign=\"top\">$i</td><td valign=\"top\">";
?>
<div id="otheripaddress_<?php echo "$i"; ?>">
<label><?php echo TXT_IP_ADDRESS; ?></label>
<input type="text" name="oct1_<?php echo "$i"; ?>" id="oct1_<?php echo "$i"; ?>" size="3" maxlength="3" value="<?php echo $addressarray[0]; ?>" onkeydown="keynext(event.keyCode,'oct2_<?php echo "$i"; ?>');if (event.keyCode==190) return false;" onKeyPress="return numbersonly(this, event);" disabled><span style="top: -6px;position:relative;">.</span><input type="text" name="oct2_<?php echo "$i"; ?>" id="oct2_<?php echo "$i"; ?>" onkeydown="keynext(event.keyCode,'oct3_<?php echo "$i"; ?>');if (event.keyCode==190) return false;" size="3" maxlength="3" value="<?php echo $addressarray[1]; ?>"onKeyPress="return numbersonly(this, event);" disabled><span style="top: -6px;position:relative;">.</span><input type="text" name="oct3_<?php echo "$i"; ?>" id="oct3_<?php echo "$i"; ?>" size="3" maxlength="3" value="<?php echo $addressarray[2]; ?>"onkeydown="keynext(event.keyCode,'oct4_<?php echo "$i"; ?>');if (event.keyCode==190) return false;" onKeyPress="return numbersonly(this, event);" disabled><span style="top: -6px;position:relative;">.</span><input type="text" name="oct4_<?php echo "$i"; ?>" id="oct4_<?php echo "$i"; ?>" size="3" maxlength="3" value="<?php echo $addressarray[3]; ?>" onKeyPress="return numbersonly(this, event);" disabled><br>
</div><input type="hidden" name="id_other_ip_address_<?php echo "$i"; ?>" value="<?php echo "$tmp_request_link_device_ip_address[ip_address_id]"; ?>">
<?php
echo "</td><td valign=\"top\" align=\"right\"><br><input type=\"button\" value=\"Change\" onclick=\"urlredirectdiv('ajax_view.php?what=ajaxnetworkdevice&irowcount=$i','otheripaddress_$i');\"> <input type=\"button\" name=\"delete\" value=\"Delete\" onclick=\"window.location='index.php?include=mainview&page=ip_address&action=delete&id=$tmp_request_link_device_ip_address[ip_address_id]'\"><td></tr>";
$i++;
}
}
echo "<input type=\"hidden\" name=\"amountotherip\" id=\"amountotherip\" value=\"$amountotherip\">";
?>
</tbody>
</table><br>
<input type="button" value="<?php echo TXT_ADD; ?>" name="button_addone" id="button_addone" onclick="addRow('otheripaddress','amountotherip');">
</fieldset><br>
<label><?php echo TXT_SNMP_READ_COMMUNITY_PORT; ?></label>
<input type="text" name="snmpreadcommunity" size="15" maxlength="15" value="<?php echo $tmp_request['snmp_read_community']; ?>"> <input type="text" name="snmpport" size="15" maxlength="15" value="<?php echo $tmp_request['snmp_port']; ?>">
<label><?php echo TXT_NAME; ?></label>
<input type="text" name="name" size="50" value="<?php echo $tmp_request['name']; ?>"><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
$request7=mysql_query("SELECT id,location_id FROM $name_table_rack WHERE id=$tmp_request[rack_id]") or die ("ERROR!");
while($tmp_request7=mysql_fetch_array($request7))
{
$location_id=$tmp_request7['location_id'];
}
$request8=mysql_query("SELECT name,id FROM $name_table_location ORDER BY name ASC") or die ("ERROR!");
while($tmp_request8=mysql_fetch_array($request8))
{
if ($location_id==$tmp_request8['id'])
{
echo "<option value=\"$tmp_request8[id]\" selected>$tmp_request8[name]</option>\n";
}
else
{
echo "<option value=\"$tmp_request8[id]\">$tmp_request8[name]</option>\n";
}
}
?>
</select>
<div id="ajaxrackarea">
<label><?php echo TXT_RACK; ?></label>
<select name="rack" size="1">
<?php
$requestcountrack=mysql_query("SELECT count(*) FROM $name_table_rack WHERE id=$tmp_request[rack_id]") or die ("ERROR!");
$all_rack = mysql_fetch_array($requestcountrack);
if ($all_rack[0]!=0)
{
$request9=mysql_query("SELECT id,name FROM $name_table_rack WHERE location_id=$location_id") or die ("ERROR!");
while($tmp_request9=mysql_fetch_array($request9))
{
if ($tmp_request['rack_id']==$tmp_request9['id'])
{
echo "<option value=\"$tmp_request9[id]\" selected>$tmp_request9[name]</option>\n";
}
else
{
echo "<option value=\"$tmp_request9[id]\">$tmp_request9[name]</option>\n";
}
}
}
?>
</select>
</div>
<label><?php echo TXT_DESCRIPTION; ?></label>
<textarea name="description" rows="10" cols="50"><?php echo $tmp_request['description']; ?></textarea><br>
<input type="submit" name="submit" value="<?php echo TXT_EDIT; ?>"> <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("edit_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 ($edit=="ok")
{
$request=mysql_query("SELECT * FROM $name_table_device WHERE id=\"$id\"") or die ("ERROR!");
while($tmp_request=mysql_fetch_array($request))
{
$preceding_name = $tmp_request['name'];
$preceding_description = $tmp_request['description'];
}
if ((isset($new_address)) && ($new_address == "yes"))
{
$address = $oct1.".".$oct2.".".$oct3.".".$oct4;
$request_count_address=mysql_query("SELECT count(*) FROM $name_table_ip_address WHERE ip=\"$address\"") or die ("ERROR!");
$result_count_address= mysql_fetch_array($request_count_address);
if ($result_count_address[0]=="0")
{
$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();
$query=mysql_query("UPDATE $name_table_device SET name=\"$name\", ip_address_id=\"$new_ip_address_id\", model_id=\"$model\", rack_id=\"$rack\", snmp_read_community=\"$snmpreadcommunity\", snmp_port=\"$snmpport\", description=\"$description\" WHERE id=\"$id\"") 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
{
$currentdate = date('Y-m-d');
$currenttime = date("H:i:s");
user_log ($_SESSION['login'],$name_table_users,$name_table_users_log,"edit","location","$name ($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'].' - edit location '.$name.'('.$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_EDITED_DEVICE;
echo "</div><br><a href=\"javascript:history.go(-2);\">";
echo TXT_BACK;
echo "</a></center>";
}
}
else
{
echo "<center><div id=\"status\" style=\"color:red\"><img src=\"images/warning.png\" alt=\"WARNING\"><br><br>";
echo TXT_IP_ADDRESS_ALREADY_EXIST;
echo "</div><br><a href=\"javascript:history.go(-2);\">";
echo TXT_BACK;
echo "</a></center>";
}
}
else
{
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")
{
$address2 = $_POST['oct1_'.$i].".".$_POST['oct2_'.$i].".".$_POST['oct3_'.$i].".".$_POST['oct4_'.$i];
$mac2 = $_POST['mac1_'.$i].".".$_POST['mac2_'.$i].".".$_POST['mac3_'.$i];
$subnet2 = $_POST['subnet_'.$i];
$new_address_name = $_POST['new_address_name_'.$i];
$new_address_description = $_POST['new_address_description_'.$i];
if (isset($_POST['id_other_ip_address_'.$i]))
{
$id_other_ip_address = $_POST['id_other_ip_address_'.$i];
}
$query_address=mysql_query("INSERT INTO $name_table_ip_address VALUES ('',\"$subnet2\",\"$address2\",\"$mac2\",\"$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 (\"$id\",\"$new_ip_address_id\")") or die ("ERROR!");
$query_delete_link_device_address = mysql_query("DELETE FROM $name_table_link_device_ip_address WHERE device_id=\"$id\" AND ip_address_id=\"$id_other_ip_address\"") or die ("ERROR!");
}
else
{
$address2 = $_POST['address_'.$i];
if (isset($_POST['id_other_ip_address_'.$i]))
{
$id_other_ip_address = $_POST['id_other_ip_address_'.$i];
}
$query_link_device_address = mysql_query("INSERT INTO $name_table_link_device_ip_address VALUES (\"$id\",\"$address2\")") or die ("ERROR!");
$query_delete_link_device_address = mysql_query("DELETE FROM $name_table_link_device_ip_address WHERE device_id=\"$id\" AND ip_address_id=\"$id_other_ip_address\"") or die ("ERROR!");
}
}
}
}
$query=mysql_query("UPDATE $name_table_device SET name=\"$name\", model_id=\"$model\", rack_id=\"$rack\", snmp_read_community=\"$snmpreadcommunity\", snmp_port=\"$snmpport\", description=\"$description\" WHERE id=\"$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
{
$currentdate = date('Y-m-d');
$currenttime = date("H:i:s");
user_log ($_SESSION['login'],$name_table_users,$name_table_users_log,"edit","location","$name ($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'].' - edit location '.$name.'('.$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_EDITED_DEVICE;
echo "</div><br><a href=\"javascript:history.go(-2);\">";
echo TXT_BACK;
echo "</a></center>";
}
}
}
?>