Location: PHPKode > projects > Network Traffic Monitor > online/add.php
<?php

#Network Traffic Monitor is an application to monitor the network traffic on all SNMP managable devices.
#
#Copyright (c) 2004, Daimler Trust nv <hide@address.com> - Jan Van Hees <hide@address.com>
# 
#This file is part of Network Traffic Monitor.
#
#    Network Traffic Monitor 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.
#
#    Network Traffic Monitor 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 Network Traffic Monitor; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
/*
* This script takes care of the actual adding of the various objects
*/
require_once("../auth.php");
require_once ("../config.php");
require_once ("HTML/Template/IT.php");
require_once "HTML/Table.php";
if($_POST['port']){
	header("Location: error.php");
	exit;
}
# get what object you have to add
$what = $_POST['what'];

$db = dbLogin();

$tpl = new HTML_Template_IT(TEMPLATEDIR);
$tpl->loadTemplatefile("basic.tpl");

switch ($what){
	case 'firm':{
			$tpl->setVariable("title","Firms");
			$cname = $_POST['cname'];
			$addr = $_POST['addr'];
			$btw = $_POST['btw'];
			$sql = "INSERT INTO company (address, compname, btw) VALUES ('$addr','$cname','$btw')";
			$script="firms.php";
		}//case firm
	break;
	case 'group':{
			$tpl->setVariable("title","Groups");
			$gname = $_POST['gname'];
			$sql = "INSERT INTO groups (groupname) VALUES('$gname')";
			$script="groups.php";
		}//case group
	break;
	case 'device':{
			$tpl->setVariable("title","Devices");
			$dname = $_POST['dname'];	
			$host = $_POST['host'];
			$pass = $_POST['pass'];
			$sql = "INSERT INTO devices (devicename, host, community) VALUES ('$dname','$host','$pass')";
			$script="switch.php";
		}//case device
	break;
	case 'user':{
			$tpl->setVariable("title", "Users");
			$uname = $_POST['uname'];
			$group = $_POST['group'];
			$comp = $_POST['comp'];
			$pass = $_POST['pass'];
			$fullname = $_POST['fullname'];
			$mail = $_POST['mail'];
			$tid = $_POST['tid']; 
			$sql = "INSERT INTO users (username, cid, passwd, fullname,email,tid) VALUES ('$uname','$comp','$pass','$fullname','$mail','$tid')";
			$script="userlist.php";

		}//case user
	break;
	case 'port':{
			$tpl->setVariable("title", "Ports");
			$gid = $_POST['group'];
			$did = $_POST['device'];
			$pname = $_POST['pname'];
			$pnum = $_POST['pnum'];
			$speed = $_POST['speed'];
			$tp=$_POST['burst'];
			if($tp=='on')$burst="true";
			else $burst="false";
			$sql = "INSERT INTO ports (portnr, portname, speed, did, burstable) VALUES ($pnum, '$pname', $speed, $did, '$burst')";
			$script="devices.php";
		}//case port
	break;
	case 'usertype':{
			$tpl->setVariable("title", "User Type");
			$tid=$_POST['tid'];
			$type=$_POST['type'];
			$sql = "INSERT INTO usertype (type) VALUES('$type')";
			$script="usertype.php";
		}//case usertype
	break;
	case 'speed':{
			$tpl->setVariable("title", "Speed");
			$mbit = $_POST['mbit'];
			$speed = $mbit * 1048576;
			$sql = "INSERT INTO speed (mbit, speed) VALUES ($mbit, $speed)";
			$script="bandwith.php";

		}//case speed
	break;
	default:{
			$tpl->setVariable("data","Default...");	
		}

}//

//execute query, give error message back if error occurs
$error = $db->query($sql) or die;
if($error != 1){
	$tpl->setVariable("data", $error->getMessage());
}//if error
else{
	$tpl->setVariable("data", "Insert completed");
}//else

/*
* It's necessary to know the UID before the user can be added to the group
* first add the user to the user table
* then get that uid and add the user to a group
*/

if($what == "user" && $error == 1){
	$ui = $db->getone("SELECT uid from users where fullname='$fullname' AND cid='$comp' AND passwd='$pass'");
	$test = "INSERT INTO groupmember (uid, gid) VALUES ('$ui','$group')";
	$err2 = $db->query("INSERT INTO groupmember (uid, gid) VALUES ('$ui','$group')") or die;
	if($err2 != 1){
		$tpl->setVariable("data", $err2->getMessage());
	}//if error
	else{
		$tpl->setVariable("data", "Insert completed");
	}//else

}// if user

/*
* to add a port to a portgroup and thus enable it, its pid must be known
* first add the port in the port table, then get the pid
* once the portid is known, the port can be enabeled
* this by adding it to portgroups and preparing an entry in LASTVALUES!
*/
if($what == "port" && $error == 1){
	$pid = $db->getone("SELECT pid from ports where portname='$pname' AND did='$did' AND portnr='$pnum'");
	$test = "INSERT INTO portgroups (pid, gid) VALUES ('$pid','$gid')";
	$err2 = $db->query("INSERT INTO portgroups (pid, gid) VALUES ('$pid','$gid')") or die;
	$err = $db->query("INSERT INTO lastvalues (pid, incoming, outgoing) VALUES ('$pid', '0', '0')");
	if($err2 != 1 || $err != 1){
		$tpl->setVariable("data", $err2->getMessage());
	}//if error
	else{
		$tpl->setVariable("data", "Insert completed");
	}//else

}// if user

$tpl->setVariable("add", $sql);

	header("Location: $script");
	exit();


$content = $tpl->get();
fillFrame($db, $content);
dblogout($db);

?> 
Return current item: Network Traffic Monitor