<?php
ob_start();session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Informacny system</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="login.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php

$alreadySent = 0;
function send_iri($type, $username, $ip)
{
	global $alreadySent;
	if($alreadySent == 1)
		return;	
	
	if($type == "BEGIN") $poznamka = "Prihlasenie uzivatela";
	if($type == "CONTINUE") $poznamka = "Aktivita uzivatela";
	if($type == "END") $poznamka = "Odhlasenie uzivatela";


	
	$message = "('WIS', ".time().", '".$type."', '".$poznamka."', [('IPv4', '".$ip."'), ('WIS login', '".$username."')])\n";
	echo $message . "<br/>";
	
	
	$fp = fsockopen("unix:///tmp/iricol", -1, $errno, $errstr, 30);
	if (!$fp) {
		echo "$errstr ($errno)<br />\n";
	} else {
		fwrite($fp, $message);
		fclose($fp);
	}
	
	$alreadySent = 1;
}

if($_GET['odhlasenie'] == 1)
{
	if($_SESSION['uzivatel'] != "")
		send_iri("END", $_SESSION['uzivatel'], $_SERVER['REMOTE_ADDR']);
	$_SESSION['uzivatel'] = "";
}

$users = array(
array("alice", "management"),
array("bob", "development"),
array("charlie", "development"),
array("guest", "guest"),
);

if(isset($_GET['meno']) and (!isset($_SESSION['uzivatel']) or $_SESSION['uzivatel']=="") )
{
	foreach($users as $user)
	{
		if($_GET['meno'] == $user[0])
		{
			$_SESSION['uzivatel'] = $user[0];
			$_SESSION['skupina'] = $user[1];
			send_iri("BEGIN", $_SESSION['uzivatel'], $_SERVER['REMOTE_ADDR']);
			break;
		}
	}
}

if(isset($_SESSION['uzivatel']) and $_SESSION['uzivatel'] != "")
{
	send_iri("CONTINUE", $_SESSION['uzivatel'], $_SERVER['REMOTE_ADDR']);
	echo '
<center>
<a href="index.php"><h2>Informacny system</h2></a>
Prihlaseny ako: <b>'.$_SESSION['uzivatel'].' ('.$_SESSION['skupina'].')</b> - <a href="index.php?odhlasenie=1">odhlasit sa</a><br/><br/>
<a href="index.php?menu=1">Menu 1</a> - 
<a href="index.php?menu=2">Menu 2</a> - 
<a href="index.php?menu=3">Menu 3</a><br/>
<b>'.$_GET["menu"].'</b>
</center>
';
}
else
{
echo '
	<div class="login" style="border:none !important">
		<table style="border:none !important">';

	if(isset($_GET['meno']))
		echo '<font color="red" style="margin-left:40px;">Neplatne uzivatelske meno</font>';
		
echo '
			<form method="get" style="border:none !important">
				<tr>
					<td style="border:none !important"><input id="name" type="text" name="meno" value="zadajte meno pouzivatela" onclick="getElementById(\'name\').value=\'\'" /></td>
					<td style="border:none !important"><input type="submit" name="submit" value="Prihlasenie"/></td>
				</tr>
			</form>
		</table>
	</div>
	<center>
		<table>
			<tr>
				<td><b>uzivatel</b></td>
				<td><b>skupina</b></td>
			</tr>';
foreach($users as $user)
{
	echo '
			<tr>
				<td>'.$user[0].'</td>
				<td>'.$user[1].'</td>
			</tr>
	';
}
echo '
		</table>
	</center>';
}

?>

</body>
</html>
