<?php
session_start();

if (isset($_POST['logowanie'])) {
	if ((($_POST['user_login'])=='') || ($_POST['user_haslo']=='')) {
		$_SESSION['blad'] = '<p style="color: red; text-align: center; font-size: 1.2em;">Wpisz login oraz hasło!</p>';
		header('Location: login.php');
	} else {
		$_SESSION['blad'] = '';
		$login=$_POST['user_login'];
		$login = htmlentities($login, ENT_QUOTES, "UTF-8");
		require_once "wiras_php/wa_baza/wa_polacz.php";
		$zap = "SELECT * FROM users WHERE login='$login'";	
			if ($pobierz = mysqli_query($db, $zap)) {
				$lr = $pobierz->num_rows;
				if ($lr>0) {
					$wiersz = $pobierz->fetch_assoc();
					$haslo=$_POST['user_haslo'];
					if (password_verify($haslo, $wiersz['haslo'])) {
						$_SESSION['zalogowany'] = true;
						$_SESSION['id'] = $wiersz['id_user'];
						$_SESSION['status'] = (int)$wiersz['status'];
						$_SESSION['login'] = $wiersz['login'];
						$_SESSION['email'] = $wiersz['email'];
						$_SESSION['nazwa'] = $wiersz['nazwa'];
						$_SESSION['imie'] = $wiersz['imie'];
						$_SESSION['node_pass'] = $wiersz['node_pass'];
						$_SESSION['chat'] = 0;
						$uid = $_SESSION['id'];
						$nazwa = $_SESSION['nazwa'];
						
						//Sprawdzenie czy uzytkownik jest juz zapisany w tabeli users_online
						//jezeli TAK przepisz go do tabeli users_session a nastepnie wykonaj UPDATE, 
						//jezeli NIE - wykonaj INSERT
						$zap = "SELECT id_user, log_time, last_time FROM users_online WHERE id_user=$uid";
						$pobierz = mysqli_query($db, $zap);
						$jest = $pobierz->num_rows;
						if ($jest>0) {
							$wiersz = $pobierz->fetch_assoc();
							$on_id = $wiersz['id_user'];
							$dlog = $wiersz['log_time'];
							$dlast = $wiersz['last_time'];
							$zap = "INSERT INTO users_session (id_user, log_time, last_time) 
										Values ($on_id, '$dlog', '$dlast')";
							$zapisz = mysqli_query($db, $zap);
							$zap = "UPDATE users_online SET 
								log_time=NOW(), last_time=NOW() WHERE id_user=$uid";
						} else {
							$zap = "INSERT INTO users_online (id_user, name, log_time, last_time) 
										Values ($uid, '$nazwa', NOW(), NOW())";
							$zap2 = "INSERT INTO chat_online (id_user, name, log_time, last_time) 
										Values ($uid, '$nazwa', NOW(), NOW())";
							$wykonaj2 = mysqli_query($db, $zap2);
						}
						$wykonaj = mysqli_query($db, $zap);
						
						//Odczyt tresci stopki
						$zap = "SELECT id_user, nazwa, tekst, wyswietl, data_stopki FROM stopka";
						$pobierz = mysqli_query($db, $zap);
						$lws = $pobierz->num_rows;					
						$wynik = array();
							while ($wiersz = mysqli_fetch_row($pobierz)) 
							{
								$wynik[] = $wiersz;
							}
						for ($i=0; $i<$lws; $i++) {
							$_SESSION['stopka'][$i][0]=$wynik[$i][0];
							$_SESSION['stopka'][$i][1]=$wynik[$i][1];
							$_SESSION['stopka'][$i][2]=$wynik[$i][2];
							$_SESSION['stopka'][$i][3]=$wynik[$i][3];
							$_SESSION['stopka'][$i][4]=$wynik[$i][4];
						}
						for ($i=0; $i<$lws; $i++) {
							if ($_SESSION['stopka'][$i][0]==$_SESSION['id']) {
								$_SESSION['stopka_u']=$i;
								break;
							}
						}
						
						//Pobranie dat dla info formularz oraz quiz
						$id = $_SESSION['id'];
						$zap = "SELECT * FROM users_formularze WHERE id_user=$id";
						$pobierz = mysqli_query($db, $zap);
						$wiersz = $pobierz->fetch_assoc();
						$_SESSION['info_data'] = $wiersz['info_data'];
						$_SESSION['ankieta_data'] = $wiersz['ankieta_data'];
						$_SESSION['test_data'] = $wiersz['test_data'];						
						//--------------------------------
						
						
						//Czy jest nowa informacja
						$_SESSION['info_data_status']=false;
						$zap = "SELECT * FROM admin_info ORDER BY data DESC LIMIT 1";
						$pobierz = mysqli_query($db, $zap);
						$lr = $pobierz->num_rows;
						if ($lr>0) {
							$wiersz = $pobierz->fetch_assoc();
							$info_tekst = $wiersz['tekst'];
							$info_data = $wiersz['data'];
							if ($_SESSION['info_data']!=$info_data) {
								$_SESSION['info_data_status']=true;
								$_SESSION['info_data']=$info_data;
								$_SESSION['info_tekst']=$info_tekst;
								$dt = strtotime($info_data);
								$data = date('Y-m-d', $dt);
								$zap = "UPDATE users_formularze SET info_data='$data' WHERE id_user=$id";
								$wykonaj = mysqli_query($db, $zap);
							} 
						}
						
						//Czy jest nowy test
						$_SESSION['test_data_status']=false;
						$zap = "SELECT * FROM admin_test ORDER BY data DESC LIMIT 7";
						$pobierz = mysqli_query($db, $zap);
						$wynik_ft = array();
						while ($wiersz = mysqli_fetch_row($pobierz)) 
						{
							$wynik_ft[] = $wiersz;
						}
						$lrft = count($wynik_ft);
						$dzisiaj = date('Y-m-d');
						if ($lrft>0) {
							for ($i=0; $i<$lrft; $i++) {
								if ($wynik_ft[$i][2]==$dzisiaj) {
									if ($_SESSION['test_data']!=$dzisiaj) {
										$_SESSION['test_data_status']=true;
										$_SESSION['test_id'] = $wynik_ft[$i][1];
										$_SESSION['test_info'] = $wynik_ft[$i][3];
										$_SESSION['test_data_lp']=0;
										//$zap = "UPDATE users_formularze SET test_data='$dzisiaj' WHERE id_user=$id";
										//$wykonaj = mysqli_query($db, $zap);
									}
									break;
								}
							}
						}
						
						//---------------------------------------------
						//Pobranie nazw uzytkownikow online
						$zap = "SELECT id_user, name FROM users_online WHERE timediff(NOW(), last_time)<6";
						$wynik_pobierz = mysqli_query($db, $zap);
						$lo = $wynik_pobierz->num_rows;
						$wynik = array();
							while ($wiersz = mysqli_fetch_row($wynik_pobierz)) 
							{
								$wynik[] = $wiersz;
							}
							
						for ($i=0; $i<$lo; $i++) {
							$_SESSION['user_on'][$i][0]=$wynik[$i][0];
							$_SESSION['user_on'][$i][1]=$wynik[$i][1];
						}
						//Pobranie nazw oraz statusu wszystkich uzytkownikow zarejestrowanych na platformie WA
						$zap = "SELECT id_user, status, nazwa FROM users ORDER BY status, id_user ASC";
						$wynik_pobierz = mysqli_query($db, $zap);
						$lu = $wynik_pobierz->num_rows;
						$wynik = array();
							while ($wiersz = mysqli_fetch_row($wynik_pobierz)) 
							{
								$wynik[] = $wiersz;
							}
						for ($i=0; $i<$lu; $i++) {
							$_SESSION['usersid'][$i] = $wynik[$i][0];
							$_SESSION['usersn'][$i] = $wynik[$i][2];
							$_SESSION['userss'][$i] = $wynik[$i][1];
						}
						$_SESSION['audio_start']=0;
						unset($_SESSION['blad']);
						$wynik_pobierz->free_result();	
						$pobierz->free_result();
						header('Location: index.php');
					} else $_SESSION['blad'] = '<p style="color: red; text-align: center; font-size: 1.2em;">1Błędny login lub hasło...</p>';
				} else {
					$_SESSION['blad'] = '<p style="color: red; text-align: center; font-size: 1.2em;">2Błędny login lub hasło...</p>';
				}
			} else {
				$_SESSION['blad'] = '<p style="color: red; text-align: center; font-size: 1.2em;">Brak połączenia...</p>';
			}
		
		
	}
}
?>

<!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" xml:lang="pl" lang="pl">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>Platforma Wirtualny asystent</title>
	<meta name="description" content="Wirtualny asystent" />
    <meta name="keywords" content="asystent, niepełnosprawny intelektualnie, pomoc" />

<style type="text/css">
body {
  background: #004;
  background: url("img/tla/tlo_02.jpg"); 
  background-repeat: repeat;
}

#login_tlo {
	width: 500px;
	height: 60%;
	margin: 100px auto;
	padding: 10px 10px;
	box-shadow: 5px 3px 8px rgba(0, 0, 0, 0.7);
	border-bottom: 4px outset #007;
	border-radius: 8px;
	background: #009;
	overflow-y: auto;
}

#login_form {
	width: 430px;
	height: 220px;
	margin: 10px auto;
	padding: 20px 25px;
	box-shadow: 5px 3px 8px rgba(0, 0, 0, 0.7);
	border-bottom: 4px outset #007;
	border-radius: 8px;
	background: #006;
	overflow-y: auto;
}

#form_login {
	width: 300px;
	height: 120px;
	margin: 10px auto;
	padding: 20px 25px;
	box-shadow: 5px 3px 8px rgba(0, 0, 0, 0.7);
	border-bottom: 4px outset #007;
	border-radius: 8px;
	background: #ccc;
}

#log_t {
	font-family: "Comic Sans MS", cursive, sans-serif;
	font-size: 1.3em;
	text-align: center;
	color: #ee3;
	margin: 5px 0 0 0;
}

#log_t2 {
	font-family: "Comic Sans MS", cursive, sans-serif;
	font-size: 1em;
	text-align: center;
	color: #e63;
	margin: 5px 0 0 0;
}

input[type=text] {
	width: 250px;
	height: 20px;
	margin: 2px 10px 7px 12px;
	border: 2px ridge #bbb;
}

input[type=text]:focus {
	background-color: #d8ffe0;
    border: 2px outset #0bb;
}

input[type=password] {
	width: 250px;
	height: 20px;
	margin: 2px 10px 7px 12px;
	border: 2px ridge #bbb;
}

input[type=password]:focus {
	background-color: #d8ffe0;
    border: 2px outset #0bb;
}

#logowanie {
	border: 2px outset #666;
	background: #d0f8f8;
	font-family: "Trebuchet MS", Helvetica, sans-serif;
	padding: 3px 10px;
	font-size: 1em;
	font-weight: bold;
}

#logowanie:hover {
	border: 2px inset #666;
	background: #c0e0e8;
	font-size: 1.1em;
	padding: 3px 8px 3px 10px;
}
</style>
 </head>
 <body>
	
	
	<div id="login_tlo">
	<p id="log_t">Logowanie do platformy<br /> - WIRTUALNY ASYSTENT -</p>
		<div id="login_form">
			<p id="log_t2">Wpisz swój login oraz hasło</p>
			<form id="form_login" action="login.php" method="post" name="form_login">
				Login<input type="text" name="user_login" id="user_login" value="bossmat"><br />
				Hasło<input type="password" name="user_haslo" id="user_haslo" value="123anerik">
				<input type="submit" name="logowanie" id="logowanie" value="Zaloguj">
			</form>
		</div>	
<?php
	if (isset($_SESSION['blad'])) {
		echo $_SESSION['blad'];
	}
?>		
	</div>
</body>
</html>