Location: PHPKode > projects > WaveWatcher > wavewatcher3/html/addnode.php
<?
	require_once("../classes/user.php");
	require_once("../classes/network.php");
    
	session_start();

	//User have entered a name that already exists in the database.
	$exists		= false;
	$err_code = "unknown";

	//Look if the page have been posted
	if ($HTTP_POST_VARS)
	{		
		$new_node = $HTTP_POST_VARS["newNode"];	
		$sel_network = $HTTP_POST_VARS["selNetwork"];	
		$sel_parentnode = $HTTP_POST_VARS["selParentNode"];		
		$sel_node_type = $HTTP_POST_VARS["selNodeTypes"];	
		$ip_address = $HTTP_POST_VARS["ip_address"];			
		$location = $HTTP_POST_VARS["location"];
		$sel_monitor = $HTTP_POST_VARS["selMonitor"];
		$sel_log = $HTTP_POST_VARS["selLog"];
		$sel_signal = $HTTP_POST_VARS["selSignal"];
		$sel_alarm = $HTTP_POST_VARS["selAlarm"];
		$password = $HTTP_POST_VARS["password"];

		//Try to create a new network
		if( !empty($new_node) )
		{
			$exists		= false;
			$user			= new User();
			$networks	= new Network();
			$dbcn			= new DBConnection();
								
			$node_result = $networks->GetNodeNamesAndIDs();
			while( List($node_id, $node_name) = $dbcn->FetchRow($node_result) )
			{			
				if($node_name == $new_node)
				{				
					$exists = true;
				}
			}		
			
			if(!$exists)			
			{			
				if(!FindSqlSyntax($new_node) && $new_node != "")
				{
					if(!FindSqlSyntax($ip_address) && $ip_address != "")
					{
						if(!FindSqlSyntax($location) && $location != "")
						{
							if(!FindSqlSyntax($password) && $password != "")
							{
								$networks->SetNodeDetails(0, $sel_node_type, $sel_network, $sel_parentnode, $new_node,  $password, $ip_address, $location, $sel_monitor, $sel_log, $sel_signal, $sel_alarm, 1);
							}
							else
							{
								$exists = true;
								$err_code = "password is incorrect";
							}
						}
						else
						{
							$exists = true;
							$err_code = "location is incorrect";
						}
					}
					else
					{
						$exists = true;
						$err_code = "IP-address is incorrect";
					}
				}
				else
				{
					$exists = true;
					$err_code = "node name is incorrect";
				}
			}
			else
			{
				$err_code = "node name exists";
				$exists = true;
			}		
		}
		else
		{
			$exists = true;
			$err_code = "node name is incorrect";
		}
	}	

	
	
	//Creates a user object
	$user = new User();

  //If we haven't logged in as a admin we should redirect to another page.
  if (!$user->IsLoggedIn() || !$user->IsAdmin())
  {	
		header("Location: index.php");		
  }

	// check so that no unwanted SQL querys are inside the strings
  function FindSqlSyntax($str)
  {				
		$str = strtoupper($str);
		     
    if (strpos($str,"SELECT") === FALSE && strpos($str,"UPDATE") === FALSE && strpos($str,"INSERT") === FALSE && strpos($str,"DELETE") === FALSE)
			return FALSE;
		else
			return TRUE;	
	}	

	function createOptionNetworks()
	{
		$user			= new User();
		$networks	= new Network();
		$dbcn			= new DBConnection();
				
		//
		//  THIS CODE GENERATES THE EXISTING NETWORK DROPDOWN LIST.
		//
		$network_result = $networks->GetNetworks(0, "yes");
		while( List($network_id, $network_name) = $dbcn->FetchRow($network_result) )
		{	
			if(!empty($GLOBALS[sel_network]) && $GLOBALS[exists] && $GLOBALS[sel_network] == $network_id)			
			{
				print "<option selected value=\"$network_id\">$network_name</option>\n";
			}
			else
			{
				print "<option value=\"$network_id\">$network_name</option>\n";
			}
		}			
		//
		// END OF EXISTING NETWORK
		//
	}


	function createOptionParentNode()
	{
		$user			= new User();
		$networks	= new Network();
		$dbcn			= new DBConnection();
				
		//
		//  THIS CODE GENERATES THE EXISTING NODES DROPDOWN LIST.
		//
		if(!empty($GLOBALS[selParentNode]) && $GLOBALS[exists] && $GLOBALS[sel_parentnode] == "000")
		{
				print "<option selected value=\"000\">No parent node</option>\n";
		}
		else
		//if($GLOBALS[sel_parentnode] != "000")
		{
			print "<option value=\"000\">No parent node</option>\n";
		}

		$node_result = $networks->GetNodeNamesAndIDs();
		while( List($node_id, $node_name) = $dbcn->FetchRow($node_result) )
		{			
			if(!empty($GLOBALS[selParentNode]) && $GLOBALS[exists] && $GLOBALS[sel_parentnode] == $node_id)			
			{
				print "<option selected value=\"$node_id\">$node_name</option>\n";
			}			
			else
			{
				print "<option value=\"$node_id\">$node_name</option>\n";
			}
		}	
		
		
		//
		// END OF EXISTING NODE
		//
	}

	function createOptionTypes()
	{
		$user			= new User();
		$networks	= new Network();
		$dbcn			= new DBConnection();
				
		//
		//  THIS CODE GENERATES THE EXISTING NODE TYPES DROPDOWN LIST.
		//
		$node_types_result = $networks->GetNodeTypes();
		while( List($node_type_id, $node_type_name, $interfaces) = $dbcn->FetchRow($node_types_result) )
		{		
			if(!empty($GLOBALS[sel_node_type]) && $GLOBALS[exists] && $GLOBALS[sel_node_type] == $node_type_id)
			{
				print "<option selected value=\"$node_type_id\">$node_type_name</option>\n";
			}
			else
			{
				print "<option value=\"$node_type_id\">$node_type_name</option>\n";
			}
		}			
		//
		// END OF EXISTING NODE TYPES
		//
	}

	function setIP()
	{
		if(!empty($GLOBALS[ip_address]) && $GLOBALS[exists])
		{
			return $GLOBALS[ip_address];
		}
		else
		{
			return "";
		}
	}

	function setLocation()
	{
		if(!empty($GLOBALS[location]) && $GLOBALS[exists])
		{
			return $GLOBALS[location];
		}
		else
		{
			return "";
		}
	}

	function setNodeName()
	{
		if(!empty($GLOBALS[new_node]) && $GLOBALS[exists])
		{
			return $GLOBALS[new_node];
		}
		else
		{
			return "";
		}
	}	
	
	function setPassword()
	{
		if(!empty($GLOBALS[password]) && $GLOBALS[exists])
		{
			return $GLOBALS[password];
		}
		else
		{
			return "";
		}
	}	

	function setMonitor($yes_no)
	{
		if(!empty($GLOBALS[sel_monitor]) && $GLOBALS[exists] && $yes_no == 'Y' && $GLOBALS[sel_monitor] == 'Y')
		{
			return "selected";
		}
		else if(!empty($GLOBALS[sel_monitor]) && $GLOBALS[exists] && $yes_no == 'N' && $GLOBALS[sel_monitor] == 'N')
		{
			return "selected";
		}
		else
		{
			return "";
		}
	}	

	function setLog($yes_no)
	{
		if(!empty($GLOBALS[sel_log]) && $GLOBALS[exists] && $yes_no == 'Y' && $GLOBALS[sel_log] == 'Y')
		{
			return "selected";
		}
		else if(!empty($GLOBALS[sel_log]) && $GLOBALS[exists] && $yes_no == 'N' && $GLOBALS[sel_log] == 'N')
		{
			return "selected";
		}
		else
		{
			return "";
		}
	}	

	function setSignal($yes_no)
	{
		if(!empty($GLOBALS[sel_signal]) && $GLOBALS[exists] && $yes_no == 'Y' && $GLOBALS[sel_signal] == 'Y')
		{
			return "selected";
		}
		else if(!empty($GLOBALS[sel_signal]) && $GLOBALS[exists] && $yes_no == 'N' && $GLOBALS[sel_signal] == 'N')
		{
			return "selected";
		}
		else
		{
			return "";
		}
	}

	function setAlarm($yes_no)
	{
		if(!empty($GLOBALS[sel_alarm]) && $GLOBALS[exists] && $yes_no == 'Y' && $GLOBALS[sel_alarm] == 'Y')
		{
			return "selected";
		}
		else if(!empty($GLOBALS[sel_alarm]) && $GLOBALS[exists] && $yes_no == 'N' && $GLOBALS[sel_alarm] == 'N')
		{
			return "selected";
		}
		else
		{
			return "";
		}
	}



	function showErr()
	{
		return $GLOBALS[err_code];
	}



?>

<html>
	<head>
		<title>Wavewatcher New Node</title>
		<link href="css/style1.css" rel="styleSheet" type="text/css">
		<script language="Javascript">			
		function AskConfirm()
		{			
			var bQuestion = confirm("Are you sure you want to create this node?");			
			if(bQuestion)
			{
				document.create_node.submit();
			}
		}	
	</script>
	</head>
	
	<body bgcolor="#ffffff">
	<table width = "100%" height=20 align="center" border=0 cellpadding="0" cellspacing="0">
		<tr>
			<td><font face="Verdana, Arial, Helvetica, sans-serif" size=1 color="#000000"><b>Wavewatcher</b></font></td>
			<td align=right>
				<a href="home.php">home</a> | 
				<a href="monitor.php">monitor network</a> | 
				<a href="profile.php">profile</a> |
				 <? if ($user->IsAdmin())
                                     print "<a href=\"procsetup.php\">process setup</a> | ";
                          ?> 
				<? if ($user->IsAdmin()) 
				     print "<a href=\"sconfig.php\">configure server</a> | ";
				?>
				<a href="help.html" target="_blank">help</a> | 
				<a href="logout.php">logout</a>
			</td>
		</tr>
	</table>
	
	


	<form name="create_node" method="post">
	
	<table width = "50%" height="80%" align="center" border="0">
		<tr>
			<td>
				<!-- background table -->
				<table width="350" align="center" border="0" cellspacing="1" cellpadding="0" bgcolor="#000000">
					<tr>
						<td>
							<!-- gui table -->
							<table width="350" border="0" cellspacing="1" cellpadding="2">
								
								<!-- title row -->
								<tr>
									<td bgcolor="#515173">
										<font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#FFFFFF">
										<b>New Node</b>
										</font>
									</td>
								</tr>
								
								<!-- message row -->
								<? 
								if (!$exists)
								{ 
								?>

								<!-- message row -->
								<tr>
									<td bgcolor="#c0c0c0">
										<font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#000000">
											Fill this form and press the create button to make a new node.
										</font>
									</td>
								</tr>

								<?	
								}
								else
								{
								?>

								<tr>
									<td bgcolor="#FC4848">
										<font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#000000">
											There is an error.<br>
											Error code: <? echo showErr() ?>
										</font>
									</td>
								</tr>
								<?
								}
								?>

								<!-- add node -->
								<tr bgcolor="#fcfcfc">
									<td>
										<table width="100%" border="0" cellspacing="0" cellpadding="2">
											<tr>
												<td align="right">
													<font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#000000">
														Node Name: 
													</font>
												</td>
												<td align="left">
													<input type="text" size="40" name="newNode" value="<? echo setNodeName() ?>">																										
												</td>
											</tr>
											<tr>
												<td align="right">
													<font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#000000">
														Network: 
													</font>
												</td>
												<td align="left">
													<select style="WIDTH: 100%" name="selNetwork">
														<? createOptionNetworks() ?>												  
												  </select>
												</td>
											</tr>
											<tr>
												<td align="right">
													<font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#000000">
														Parent Node: 
													</font>
												</td>
												<td align="left">
													<select style="WIDTH: 100%" name="selParentNode">
														<? createOptionParentNode() ?>												  
												  </select>
												</td>
											</tr>
											<tr>
												<td align="right">
													<font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#000000">
														Node Type: 
													</font>
												</td>
												<td>
													<select style="WIDTH: 100%" name="selNodeTypes">
														<? createOptionTypes() ?>												  
												  </select>
												</td>
											</tr>
											<tr>
												<td align="right">
													<font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#000000">
														IP Address: 
													</font>
												</td>
												<td>													
													<input type="text" name="ip_address" size="40" value="<? echo setIP() ?>">											  
												</td>
											</tr>											
											<tr>
												<td align="right">
													<font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#000000">
														Location: 
													</font>
												</td>
												<td >													
													<input type="text" name="location" size="40" value="<? echo setLocation() ?>">											  
												</td>
											</tr>
											<tr>
												<td align="right">
													<font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#000000">
														Password: 
													</font>
												</td>
												<td>													
													<input type="password" name="password" size="40" value="<? echo setPassword() ?>">	  
												</td>
											</tr>
											<tr>
												<td align="right">
													<font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#000000">
														Monitor: 
													</font>
												</td>
												<td>
													<select name="selMonitor">
														<option <? echo setMonitor('Y') ?> name="Y">Y</option>
														<option <? echo setMonitor('N') ?> name="N">N</option>																
													</select>													
												</td>												
											</tr>
											<tr>
												<td align="right">
													<font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#000000">
														Log: 
													</font>
												</td>
												<td>
													<select name="selLog">
														<option <? echo setLog('Y') ?> name="Y">Y</option>
														<option <? echo setLog('N') ?> name="N">N</option>															
													</select>													
												</td>												
											</tr>
											<tr>
												<td align="right">
													<font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#000000">
														Signal: 
													</font>
												</td>
												<td>
													<select name="selSignal">
														<option <? echo setSignal('Y') ?> name="Y">Y</option>
														<option <? echo setSignal('N') ?> name="N">N</option>						
													</select>													
												</td>												
											</tr>
											<tr>
												<td align="right">
													<font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#000000">
														Alarm: 
													</font>
												</td>
												<td>
													<select name="selAlarm">
														<option <? echo setAlarm('Y') ?> name="Y">Y</option>
														<option <? echo setAlarm('N') ?> name="N">N</option>				
													</select>													
												</td>												
											</tr>
											<tr>
												<td colspan="2" align="right">
													<input onClick="javascript:AskConfirm()" name="createBtn" type="button" value="Create Node">
												</td>
											</tr>
										</table>
									</td>
								</tr>

							</table>
						</td>
					</tr>
				</table>
				<br>				
			</td>
		</tr>
	</table>
	</form>
	</body>
<html>






Return current item: WaveWatcher