<?

/*
#============================================================================
# Program: User-Profile.php
# Programmer: Remo Rickli
#
# DATE		COMMENT
# -----------------------------------------------------------
# 08/03/05	initial version.
# 10/03/06	new SQL query support
# 17/07/07	improved announcements
# 30/08/07	implemented CSS scheme
# 20/09/07	implemented language support
*/

$msgfile= 'log/msg.txt';

include_once ("inc/header.php");

$name = $_SESSION['user'];
$_POST = sanitize($_POST);
$msg = isset( $_POST['msg']) ? $_POST['msg'] : "";

$link	= @DbConnect($dbhost,$dbuser,$dbpass,$dbname);
if(isset($_GET['up']) ){
	if($_GET['pass']){
		if($_GET['pass'] == $_GET['vpas']){
			$pass = md5( $_GET['pass'] );
			$query	= GenQuery('user','u','name',$name,'',array('password'),array('='),array($pass) );
			if( !@DbQuery($query,$link) ){echo "<h4>".DbError($link)."</h4>";}else{echo "<h5>Password $updlbl OK</h5>";}
		}else{
			echo "<h4>Password: $mullbl $vallbl!</h4>";
		}
	}
	$query	= GenQuery('user','u','name',$name,'',array('email','phone','comment','language','theme'),array('='),array($_GET['email'],$_GET['phone'],$_GET['comment'],$_GET['lang'],$_GET['teme']) );
	if( !@DbQuery($query,$link) ){echo "<h4>".DbError($link)."</h4>";}else{echo "<h5>$name $updlbl OK</h5>";}
	$_SESSION['lang'] = $_GET['lang'];
	$_SESSION['theme'] = $_GET['teme'];
}
$query	= GenQuery('user','s','*','','',array('name'),array('='),array($name) );
$res	= @DbQuery($query,$link);
$uok	= @DbNumRows($res);
if ($uok == 1) {
	$u = @DbFetchRow($res);
}else{
	echo "<h4>user $name doesn't exist! ($uok)</h4>";
	die;
}
?>
<h1><?=$usrlbl?> Profile</h1>
<form method="get" action="<?=$self?>.php" name="pro">
<table class="content"><tr class="<?=$modgroup[$self]?>1">
<th width=80><a href="<?=$self?>.php"><img src="<?=Smilie($name)?>"></a>
<br><?=$name?></th>
<th valign="top" align="right">
Password <input type="password" name="pass" size="12"><p>
<?=$acklbl?> <input type="password" name="vpas" size="12">
</th>

<th valign="top" align="right">
Email <input type="text" name="email" size="32" value="<?=$u[8]?>" >
Phone <input type="text" name="phone" size="12" value="<?=$u[9]?>" >
<p>
<?=$cmtlbl?> <input type="text" name="comment" size="50" value="<?=$u[12]?>" >
</th>
<th valign="top">
<select name="lang">
<?
if ($dh = opendir("languages")) {
	while (($f = readdir($dh)) !== false) {
		if($f != "." && $f != ".."){
			echo "<OPTION VALUE=\"$f\" ".(($u[13] == $f)?"selected":"").">$f\n";
		}
	}
	closedir($dh);
}
?>
</select>
<p>
<select name="teme">
<?
if ($dh = opendir("themes")) {
	while (($f = readdir($dh)) !== false) {
		if( $p = strpos($f, ".css") ){
			$t = substr($f,0,$p);
			echo "<OPTION VALUE=\"$t\" ".(($u[14] == $t)?"selected":"").">$t\n";
		}
	}
	closedir($dh);
}
?>
</select>
</th>
</th>
<th width=80><input type="submit" name="up" value="<?=$updlbl?>"></th>
</tr></table></form>

<h2><?=$grplbl?></h2>
<table class="content">
<tr class="<?=$modgroup[$self]?>2">
<th>Admin</th><th><?=$netlbl?></th><th>Helpdesk</th><th>Monitoring</th><th>Manager</th><th>Other</th>
<th><?=$timlbl?></th>
<tr class="txta">
<th><?=($u[2])?"<img src=\"img/32/cfg.png\">":"<img src=\"img/16/bcls.png\">"?></th>
<th><?=($u[3])?"<img src=\"img/32/net.png\">":"<img src=\"img/16/bcls.png\">"?></th>
<th><?=($u[4])?"<img src=\"img/32/ring.png\">":"<img src=\"img/16/bcls.png\">"?></th>
<th><?=($u[5])?"<img src=\"img/32/sys.png\">":"<img src=\"img/16/bcls.png\">"?></th>
<th><?=($u[6])?"<img src=\"img/32/umgr.png\">":"<img src=\"img/16/bcls.png\">"?></th>
<th><?=($u[7])?"<img src=\"img/32/glob.png\">":"<img src=\"img/16/bcls.png\">"?></th>
<th><?=date("j. M Y",$u[10])?></th>
</tr></table><p>

<?
if($u[2]){
	if(isset($_POST['cam']) ){
		unlink($msgfile);
	}elseif(isset($_POST['sam']) ){
		$fh = fopen($msgfile, 'w') or die("Cannot write $msgfile!");
		fwrite($fh, "$msg");
		fclose($fh);
	}
	if(isset($_GET['eam']) ){
?>
<form method="post" action="<?=$self?>.php" name="ano">
<table class="content">
<tr class="warn">
<th width="80">
<input type="button" value="Bold" OnClick='document.ano.msg.value = document.ano.msg.value + "<b></b>"';>
<p>
<input type="button" value="Italic" OnClick='document.ano.msg.value = document.ano.msg.value + "<i></i>"';>
<p>
<input type="button" value="Pre" OnClick='document.ano.msg.value = document.ano.msg.value + "<pre></pre>"';>
<p>
<input type="button" value="Break" OnClick='document.ano.msg.value = document.ano.msg.value + "<br>\n"';>
<p>
<input type="button" value="Title" OnClick='document.ano.msg.value = document.ano.msg.value + "<h2></h2>\n"';>
<p>
<input type="button" value="List" OnClick='document.ano.msg.value = document.ano.msg.value + "<ul>\n<li>\n<li>\n</ul>\n"';>
</th><th>
<textarea rows="16" name="msg" cols="100">
<?
	if (file_exists($msgfile)) {
		readfile($msgfile);
	};
?>
</textarea>
</th>
<th width="80">
<input type="submit" name="cam" value="<?=$delbtn?>">
<p>
<input type="submit" name="sam" value="<?=$addbtn?>">
</th></table>
<?
	}else{
	?>
	<div align="center"><a href="?eam=1"><img src="img/16/tab.png"></a></div>
	<?
	}
}
if (file_exists('log/msg.txt')) {
	echo "<h2>Admin $msglbl</h2><div class=\"textpad warn\">\n";
	include_once ($msgfile);
	echo "</div>";
}
include_once ("inc/footer.php");
?>
