Location: PHPKode > projects > OpenDataCenter Network Management System > include/mainview_page/subnet/edit.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($edit))
{
	$request=mysql_query("SELECT * FROM $name_table_subnet WHERE id=\"$_GET[id]\"") or die ("ERROR!");

	while($tmp_request=mysql_fetch_array($request))
	{
		$address_array = explode(".",$tmp_request['address']);
		$mask_array = explode(".",$tmp_request['mask']);
		$cidr = find_net ($tmp_request['address'],$tmp_request['mask']);
		$cidr = str_replace("/","",$cidr['cidr']);
?>
<script language="JavaScript">
var opt = new OptionTransfer("list1","list2");
opt.setAutoSort(true);
opt.setDelimiter(";");
opt.saveNewLeftOptions("customer");
opt.saveRemovedLeftOptions("removed_customer");
</script>
<form method="post" action="index.php?include=mainview&page=subnet&action=edit&edit=ok&id=<?php echo $id;?>" name="edit_subnet">
	<fieldset class="form">
		<legend><img src="images/edit.gif" alt="Edit" align="top"> <?php echo TXT_EDIT; ?> <?php echo TXT_SUBNET; ?></legend>
		<label><?php echo TXT_NAME; ?></label>
		<input type="text" name="name" size="50" value="<?php echo $tmp_request['name']; ?>"><br>
		<label><?php echo TXT_ADDRESS; ?></label>
		<input type="text" name="oct1" id="oct1" size="3" maxlength="3" value="<?php echo $address_array[0]; ?>" disabled><span style="top: -6px;position:relative;">.</span><input type="text" name="oct2" id="oct2" size="3" maxlength="3" value="<?php echo $address_array[1]; ?>" disabled><span style="top: -6px;position:relative;">.</span><input type="text" name="oct3" id="oct3" size="3" maxlength="3" value="<?php echo $address_array[2]; ?>" disabled><span style="top: -6px;position:relative;">.</span><input type="text" name="oct4" id="oct4" size="3" maxlength="3" value="<?php echo $address_array[3]; ?>" disabled><br>		
		<label><?php echo TXT_MASK; ?></label>
		<input type="text" name="snm" id="snm1" size="3" maxlength="3" value="<?php echo $mask_array[0]; ?>" disabled><span style="top: -6px;position:relative;">.</span><input type="text" name="snm2" id="snm2" size="3" maxlength="3" value="<?php echo $mask_array[1]; ?>" disabled><span style="top: -6px;position:relative;">.</span><input type="text" name="snm3" id="snm3" size="3" maxlength="3" value="<?php echo $mask_array[2]; ?>" disabled><span style="top: -6px;position:relative;">.</span><input type="text" name="snm4" id="snm4" size="3" maxlength="3" value="<?php echo $mask_array[3]; ?>" disabled><span style="position:relative;top:-10px;"> or /</span><input type="text" name="snmbits" size="2" maxlength="2" value="<?php echo $cidr; ?>" disabled><br>
		<label><?php echo TXT_CUSTOMERS; ?></label>
		<table border="0">
		<tr>
			<td>
			<select name="list1" multiple="multiple" size="10" onDblClick="opt.transferRight()">
			<?php
			$customer_array = array();
			$request2=mysql_query("SELECT * FROM $name_table_link_customer_subnet WHERE subnet_id=\"$id\"") or die ("ERROR!");

			while($tmp_request2=mysql_fetch_array($request2))
			{
				$request3=mysql_query("SELECT id,name FROM $name_table_customer WHERE id=\"$tmp_request2[customer_id]\"") or die ("ERROR!");

				while($tmp_request3=mysql_fetch_array($request3))
				{
					echo "<option value=\"$tmp_request3[id]\">$tmp_request3[name]</option>";
					array_push($customer_array,$tmp_request3['id']);
				}
			}
			?>						
			</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
			$sqlrequest="WHERE ";
			$count = 1;
			$customer_array_size = sizeof($customer_array);
      
      if ($customer_array_size == 0)
      {
        $request4=mysql_query("SELECT id,name FROM $name_table_customer") or die ("ERROR!");

  			while($tmp_request4=mysql_fetch_array($request4))
  			{
  				echo "<option value=\"$tmp_request4[id]\">$tmp_request4[name]</option>";
  			}
      }
      else
      {
  			foreach ($customer_array as $arrayvalue)
  			{
  				if ($count == $customer_array_size)
  				{
  					$sqlrequest.="id!=\"$arrayvalue\" ";
  				}
  				else
  				{
  					$sqlrequest.="id!=\"$arrayvalue\" AND ";
  				}
  				$count++;
  			}
  			
  			$sqlrequest2="SELECT id,name FROM ".$name_table_customer." ".$sqlrequest;
  			
  			$request4=mysql_query($sqlrequest2) or die ("ERROR!");

  			while($tmp_request4=mysql_fetch_array($request4))
  			{
  				echo "<option value=\"$tmp_request4[id]\">$tmp_request4[name]</option>";
  			}
      }
			?>
			</select>
			</td>
		</tr>
		</table>
		<label>Vlan</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
			$request5=mysql_query("SELECT * FROM $name_table_link_subnet_vlan WHERE subnet_id=$id") or die ("ERROR!");

			while($tmp_request5=mysql_fetch_array($request5))
			{
				$previous_vlan = $tmp_request5['vlan_id'];
				$request6=mysql_query("SELECT id,name FROM $name_table_vlan WHERE id=$tmp_request5[vlan_id]") or die ("ERROR!");
				while($tmp_request6=mysql_fetch_array($request6))
				{
					echo "<option value=\"$tmp_request6[id]\" SELECTED>$tmp_request6[name]</option>";
				}
				
				$request7=mysql_query("SELECT id,name FROM $name_table_vlan WHERE id!=$tmp_request5[vlan_id]") or die ("ERROR!");
				while($tmp_request7=mysql_fetch_array($request7))
				{
					echo "<option value=\"$tmp_request7[id]\">$tmp_request7[name]</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_description').value=''}">
		<label style="clear:left;"><?php echo TXT_STATUS; ?></label>
		<select name="status" size="1">
		<?php
			switch ($tmp_request['status'])
			{
				case '0':
				echo "<option value=\"0\" selected>";
				echo TXT_ASSIGNED;
				echo "</option>
				<option value=\"1\">";
				echo TXT_UNUSED;
				echo "</option>
				<option value=\"2\">";
				echo TXT_RESERVED;
				echo "</option>";
				break;
				
				case '1':
				echo "<option value=\"0\">";
				echo TXT_ASSIGNED;
				echo "</option>
				<option value=\"1\" selected>";
				echo TXT_UNUSED;
				echo "</option>
				<option value=\"2\">";
				echo TXT_RESERVED;
				echo "</option>";				
				break;
				
				case '2':
				echo "<option value=\"0\">";
				echo TXT_ASSIGNED;
				echo "</option>
				<option value=\"1\">";
				echo TXT_UNUSED;
				echo "</option>
				<option value=\"2\" selected>";
				echo TXT_RESERVED;
				echo "</option>";							
				break;
			}
		?>
		</select>
		<label><?php echo TXT_DESCRIPTION; ?></label>
		<textarea name="description" rows="10" cols="50"><?php echo $tmp_request['description']; ?></textarea><br>
		<input type="hidden" name="previousvlan" value="<?php echo $previous_vlan; ?>">
		<input type="hidden" name="customer" value="">
		<input type="hidden" name="removed_customer" value="">
		<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_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 ($edit=="ok")
{
	$query=mysql_query("UPDATE $name_table_subnet SET name=\"$name\", description=\"$description\", status=\"$status\" 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
	{
		$customer_value = explode (";", $_POST['customer']);
	
		foreach ($customer_value as $arrayvalue)
		{
			$request=mysql_query("SELECT count(*) FROM $name_table_link_customer_subnet WHERE customer_id=\"$arrayvalue\" and subnet_id=\"$id\"") or die ("ERROR!");
			$data = mysql_fetch_array($request);
			if ($data[0] == 0)
			{
				$query2=mysql_query("INSERT INTO $name_table_link_customer_subnet VALUES (\"$arrayvalue\",\"$id\")") or die ("ERROR!");
			}
		}
		
		$removedcustomer_value = explode (";", $_POST['removed_customer']);
		
		foreach ($removedcustomer_value as $arrayvalue)
		{
			$query3=mysql_query("DELETE FROM $name_table_link_customer_subnet WHERE customer_id=\"$arrayvalue\" and subnet_id=\"$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 (\"$id\",\"$vlan_id\")") or die ("ERROR!");
				
				if ($query4)
				{
					$query5=mysql_query("DELETE FROM $name_table_link_subnet_vlan WHERE vlan_id=\"$previousvlan\" and subnet_id=\"$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
		{
			$request2=mysql_query("SELECT count(*) FROM $name_table_link_subnet_vlan WHERE vlan_id=\"$vlan\" and subnet_id=\"$id\"") or die ("ERROR!");
			$data2 = mysql_fetch_array($request2);
			if ($data2[0] == 0)
			{
				$query4=mysql_query("INSERT INTO $name_table_link_subnet_vlan VALUES (\"$id\",\"$vlan\")") or die ("ERROR!");
				
				if ($query4)
				{
					$query5=mysql_query("DELETE FROM $name_table_link_subnet_vlan WHERE vlan_id=\"$previousvlan\" and subnet_id=\"$id\"") or die ("ERROR!");
				}
			}	
		}	
	
		$currentdate = date('Y-m-d');
		$currenttime = date("H:i:s"); 
		
		user_log ($_SESSION['login'],$name_table_users,$name_table_users_log,"edit","subnet","$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 subnet '.$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_SUBNET;
		echo "</div><br><a href=\"javascript:history.go(-2);\">";
		echo TXT_BACK;
		echo "</a></center>";
	}

}
?>
Return current item: OpenDataCenter Network Management System