Location: PHPKode > projects > Network Traffic Monitor > config.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
// require needed 2 make DB connection
require_once 'DB.php';
require_once "HTML/Template/IT.php";

define("TEMPLATEDIR", "templates/");

	//start session
	session_start();

//login function
function dbLogin (){

	//DB login info:
	$dsn = array(
    		'phptype'  => 'pgsql',
    		'username' => 'postgres',
    		'hostspec' => '192.168.0.2',
	    	'database' => 'monitoring',
	);

	// Login options
	$options = array(
	    'debug'       => 2,
	);

	//SQL Database connection
	$db =& DB::connect($dsn, $options);
	if (DB::isError($db)) {
	    die($db->getMessage());
	}// if error
	
	$logtype = getUserType($db);
	$_SESSION['logtype']=$logtype;
	//return connection
	return $db;
}//function login

function dblogout (&$db){
	$db->disconnect();
}//function logout

//fillframe will take care of filling up the complete frame
function fillFrame ($db, $content){
	$tpl = new HTML_Template_IT(TEMPLATEDIR);
	$tpl->loadTemplatefile("layout.inc", true, true);

	$tpl->setVariable("DATA", $content);


        $menutpl = new HTML_Template_IT(TEMPLATEDIR);
        $menutpl->loadTemplatefile("usermenu.tpl", true, false);
        $tpl->setVariable("menu",$menutpl->get());
	$logtype = getUserType($db);

	//Load different menu parts for different users

	$_SESSION['logtype']=$logtype;
        if($logtype == "Administrator" || $logtype == "Finance" ){
		$fmenutpl = new HTML_Template_IT(TEMPLATEDIR);
		$fmenutpl->loadTemplatefile("financemenu.tpl",true,false);
		$fmenutpl->setVariable("GROUPOPTIONS",createGroupCombo($db));
		$fmenutpl->setVariable("SCRIPT",$_SERVER[SCRIPT_NAME]);
		$tpl->setVariable("financemenu",$fmenutpl->get());
	}// if admin or finance
        if($logtype == "Administrator"){
               $menutpl = new HTML_Template_IT(TEMPLATEDIR);
               $menutpl->loadTemplatefile("adminmenu.tpl",true,false);
               $tpl->setVariable("adminmenu",$menutpl->get());
        }//if administrator
	$uid= $_SSION['uid']; 
        $tpl->setVariable("PORTOPTIONS",createIntCombo($db,$uid));
	$tpl->setVariable("SCRIPT", $_SERVER[SCRIPT_NAME]);
	$tpl->setVariable("HISTORY", createHistoryCombo());

	$tpl->show();
}//function fillFrame


function createIntCombo($db,$uid){
	global $ifa;
	global $tpl;
	//select ports from portgroup linked with the user
	//have to refine select statement, should be linked to a portgroup!!!
	//select other ports, depending on login type: User, Administrator, Finance

	$logtype = getUserType($db);
	
	if($logtype == "User"){
		$sql = "SELECT ports.pid, ports.portname FROM ports INNER JOIN portgroups ON portgroups.pid=ports.pid INNER JOIN groups ON portgroups.gid = groups.gid INNER JOIN groupmember ON groupmember.gid = groups.gid INNER JOIN users ON users.uid = groupmember.uid WHERE users.uid = $_SESSION[uid] ORDER BY ports.portname";
	}//if user
	if($logtype == "Administrator" || $logtype == "Finance"){
		$sql= "SELECT DISTINCT ports.pid, ports.portname FROM ports ORDER BY ports.portname"; 
	}
	$result = $db->query($sql);

	// Get each row of data on each iteration until
	// there are no more rows

	while ($row = $result->fetchRow()){

		if (!$_SESSION[iface]) $_SESSION[iface] = $row[0];
		$selected = ($row[0] == $_SESSION[iface]) ? " selected": "";
    		$combobox .= "<option value=\"$row[0]\" $selected>$row[1]</option>";

	}
	return $combobox;
}//function createIntCombo

function createGroupCombo($db){
	$sql = "SELECT distinct groups.gid, groups.groupname FROM groups ORDER BY groups.groupname";

	$result = $db->query($sql);

	// Get each row of data on each iteration until
	// there are no more rows
	while ($row = $result->fetchRow()){
		if (!$_SESSION[group]) $_SESSION[group] = $row[0];
		$selected = ($row[0] == $_SESSION[group]) ? " selected": "";
    		$combobox .= "<option value=\"$row[0]\" $selected>$row[1]</option>";
	}
	return $combobox;

}//function createGroupCombo

function createHistoryCombo(){
	
	$hist = array(0,1,2,3,4,5,6,7,8,9,10,11);

	// Get each row of data on each iteration until
	// there are no more rows
	for ($i=0;$i<12;$i++){
		if (!$_SESSION[history]) $_SESSION[history] = 0;
		if ($i == $_SESSION[history]) {
			$selected="selected";
			$_SESSION[history]=$i;	
		}
		else $selected="";
    		$hist .= "<option value=\"$i\" $selected>$i months ago </option>";
	}//for
	return $hist;

}//function createGroupCombo


//get the usertype and group of the user that's logged in, store these values in a session variable
function getUserType($db) {
	$sql = "SELECT usertype.type FROM usertype INNER JOIN users ON users.tid=usertype.tid WHERE uid='$_SESSION[uid]'";
	$sql2 = "SELECT groupmember.gid FROM groups INNER JOIN groupmember ON groupmember.gid = groups.gid INNER JOIN users ON users.uid = groupmember.uid WHERE users.uid LIKE $_SESSION[uid]";
	$_SESSION['grp'] = $db->getOne($sql2);
	$var = $_SESSION['grp'];
	return $db->getOne($sql);
}//function getUserType

function setReadable($iref){
	if($iref > 0){
		//$ref = $iref%1073741824;
		if($iref > 1073741824) {
			$ref = $iref/1073741824;
			$iref = round ($ref, 2);
			$iref = "$iref Gbyte";
		}
		else if ($iref > 1048576 ){
			$ref = $iref/1048576;
			$iref = round ($ref, 2);
			$iref = "$iref Mbyte";
		}
		else if ($iref > 1024){
			$ref =$iref/1024;
			$iref = round ($ref, 2);
			$iref = "$iref Kbyte";
		}
		else $iref = "$iref Kbyte";
	}

	return $iref;

}
	

?>
Return current item: Network Traffic Monitor