<?
/*
#============================================================================
# Program: User-Accounts.php
# Programmer: Remo Rickli
#
# DATE     COMMENT
# -------- ------------------------------------------------------------------
# 10/03/05	initial version
# 10/02/06	pw reset added
# 19/01/07	new Sorting approach
# 30/08/07	implemented CSS scheme
# 20/09/07	implemented language support
*/

include_once ("inc/header.php");

$_GET = sanitize($_GET);
$ord = isset( $_GET['ord']) ? $_GET['ord'] : "";
$grp = isset( $_GET['grp']) ? $_GET['grp'] : "";
$del = isset( $_GET['del']) ? $_GET['del'] : "";

$cols = array(	"name"=>"$namlbl",
		"email"=>"Email",
		"phone"=>"Phone",
		"time"=>"$timlbl",
		"lastseen"=>$laslbl,
		"comment"=>"$cmtlbl"
		);

?>
<h1><?=$usrlbl?> Accounts</h1>
<form method="get" action="<?=$self?>.php">
<table class="content" ><tr class="<?=$modgroup[$self]?>1">
<th width=80><a href="<?=$self?>.php"><img src="img/32/user.png"></a></th>
<th><?=$grplbl?> 
<SELECT size=1 name="grp" onchange="this.form.submit();">
<OPTION VALUE=""><?=$sellbl?> ->
<OPTION VALUE="adm" <?=($grp == "adm")?"selected":""?> >Admin
<OPTION VALUE="net" <?=($grp == "net")?"selected":""?> ><?=$netlbl?>
<OPTION VALUE="dsk" <?=($grp == "dsk")?"selected":""?> >Helpdesk
<OPTION VALUE="mon" <?=($grp == "mon")?"selected":""?> >Monitor
<OPTION VALUE="mgr" <?=($grp == "mgr")?"selected":""?> >Manager
<OPTION VALUE="oth" <?=($grp == "oth")?"selected":""?> >Other
</SELECT>
</th>
<th><?=$usrlbl?> 
<input type="text" name="usr" size="12">
<input type="submit" name="create" value="<?=$addbtn?>">
</th>
</table></form>
<p>
<?
$link	= @DbConnect($dbhost,$dbuser,$dbpass,$dbname);
if (isset($_GET['usr']) and isset($_GET['create']) ){
	$now = time();
	$pass = md5( $_GET['usr'] );
	$query	= GenQuery('user','i','','','',array('name','password','time','language','theme'),'',array($_GET['usr'],$pass,$now,'english','default') );
	if( !@DbQuery($query,$link) ){echo "<h4>".DbError($link)."</h4>";}else{echo "<h5>$usrlbl $_GET[usr]: $addbtn OK</h5>";}
}elseif(isset($_GET['psw']) ){
	$pass = md5( $_GET['psw'] );
	$query	= GenQuery('user','u','name',$_GET['psw'],'',array('password'),'',array($pass) );
	if( !@DbQuery($query,$link) ){echo "<h4>".DbError($link)."</h4>";}else{echo "<h5>$usrlbl $_GET[psw]: password reset OK</h5>";}
}elseif( isset($_GET['grm']) ){
	$query	= GenQuery('user','u','name',$_GET['grm'],'',array($_GET['mgp']),'',array('0') );
	if( !@DbQuery($query,$link) ){echo "<h4>".DbError($link)."</h4>";}else{echo "<h5>$usrlbl $_GET[grm]: $grplbl $_GET[mgp] $delbtn OK</h5>";}
}elseif(isset($_GET['gad']) ){
	$query	= GenQuery('user','u','name',$_GET['gad'],'',array($_GET['mgp']),'',array('1') );
	if( !@DbQuery($query,$link) ){echo "<h4>".DbError($link)."</h4>";}else{echo "<h5>$usrlbl $_GET[gad]: $grplbl $_GET[mgp] $addbtn OK</h5>";}
}elseif($del){
	$query	= GenQuery('user','d','','','',array('name'),array('='),array($_GET['del']) );
	if( !@DbQuery($query,$link) ){echo "<h4>".DbError($link)."</h4>";}else{echo "<h5>$usrlbl $_GET[del]: $delbtn OK</h5>";}
}
?>
<table class="content"><tr class="<?=$modgroup[$self]?>2">
<?
ColHead('name');
ColHead('email');
ColHead('phone');
ColHead('comment');
ColHead('time');
ColHead('lastseen');
echo "<th>$grplbl</th><th>GUI</th><th>Action</th></tr>\n";

if ($grp){
	$query	= GenQuery('user','s','*',$ord,'',array($grp),array('='),array('1') );
}else{
	$query	= GenQuery('user','s','*',$ord );
}
$res	= @DbQuery($query,$link);
if($res){
	$row = 0;
	while( ($u = @DbFetchRow($res)) ){
		if ($row % 2){$bg = "txta"; $bi = "imga";}else{$bg = "txtb"; $bi = "imgb";}
		$row++;
		list($cc,$lc) = Agecol($u[10],$u[11],$row % 2);
?>
<tr class="<?=$bg?>"><th class="<?=$bi?>">
<img src="<?=(Smilie($u[0]))?>" title="Hello I'm <?=$u[0]?>"><br><?=$u[0]?></th>
<td><?=$u[8]?></td><td align="center"><?=$u[9]?></td>
<td><?=$u[12]?></td>
<td bgcolor=#<?=$cc?>><?=(date($datfmt,$u[10]))?></td>
<td bgcolor=#<?=$lc?>><?=(date($datfmt,$u[11]))?></td>
<th>
<?
GroupButton($u[0],$u[2],'adm','cfg');
GroupButton($u[0],$u[3],'net','net');
GroupButton($u[0],$u[4],'dsk','ring');
GroupButton($u[0],$u[5],'mon','sys');
GroupButton($u[0],$u[6],'mgr','umgr');
GroupButton($u[0],$u[7],'oth','3d');
?>
</th>
<td align="center"><?=$u[13]?>/<?=$u[14]?></td>
<th><a href="?grp=<?=$grp?>&ord=<?=$ord?>&del=<?=$u[0]?>"><img src="img/16/bcnl.png" title="<?=$delbtn?>" onclick="return confirm('<?=$u[0]?> <?=$delbtn?>?')"></a>
<a href="?grp=<?=$grp?>&ord=<?=$ord?>&psw=<?=$u[0]?>"><img src="img/16/key.png" title="reset password" onclick="return confirm('<?=$u[0]?> reset password?')"></a></th></tr>
<?
	}
	@DbFreeResult($res);
}else{
	print @DbError($link);
}
?>
</table>
<table class="content">
<tr class="<?=$modgroup[$self]?>2"><td><?=$row?> Users (<?=$query?>)</td></tr>
</table>
<?

include_once ("inc/footer.php");

//===================================================================
// Draw group button
function GroupButton($us,$st,$gp,$ic){
	
	global $grp,$ord;
	
	if($st){
		echo "<a href=\"?grp=$grp&ord=$ord&grm=$us&mgp=$gp\">\n";
		echo "<img src=\"img/16/$ic.png\"  title=\"+$gp\"></a>\n";
	}else{
		echo "<a href=\"?grp=$grp&ord=$ord&gad=$us&mgp=$gp\">\n";
		echo "<img src=\"img/16/bcls.png\"  title=\"-$gp\">\n";
	}
}
?>