<?php

// start autentificare
if($_POST['autentificare']) {
	$_SESSION['nr_incercari'] = (++$_SESSION['nr_incercari']);
$user = md5($_POST['user']);
$parola = md5(trim($_POST['parola']));
//$cod_securitate = trim($_POST['cod_securitate']);
	
$useri = "SELECT * FROM `utilizatori` where user = '".$user."'";
	$result = $conectare->query($useri);
		if ($result->num_rows > 0) {
			while($row = $result->fetch_assoc()) {
				$_SESSION['id_user'] = $row["id"];
				$_SESSION['user'] = $row["user"];
				$_SESSION['parola'] = $row["parola"];
			if($parola == $row["parola"] and $row["status"] == 'Activ'){
				$_SESSION['nume_utilizator'] =  $row["nume"].' '.$row["prenume"];
				$_SESSION['rol'] = $row["rol"];
				$_SESSION['login'] = 'da';	
				$_SESSION['status'] = $row["status"];
					$data_autentif = ("UPDATE `utilizatori` SET `data_autentificare` = '".$ymdhis."', `ip` = '".$_SERVER['REMOTE_ADDR']."' where id = '".$row['id']."'") or die(mysql_error());
					if (mysqli_query($conectare, $data_autentif)) {
						//echo "<script>alert('Document MODIFICAT cu SUCCES.')</script>";
					}else{
						//echo "<script>alert('EROARE la update user')</script>";
					}
			}else{
				echo "<script>alert('Autentificare ESUATA. Parola incorecta sau Utilizator INACTIV.')</script>";
				echo "<script>location.replace('index.php')</script>";
			}	
			if($_SESSION['rol'] == 'Cititor' and $_SESSION['status'] == 'Activ'){
				echo "<script>location.replace('index.php?pag=2')</script>";
					}
			if($_SESSION['rol'] == 'Editor' and $_SESSION['status'] == 'Activ'){
				echo "<script>location.replace('index.php?pag=1a')</script>";
					}
			if($_SESSION['status'] == 'INACTIV'){
				echo "<script>alert('Utilizator INACTIV, va rugam contactati Administratorul de sistem.')</script>";
				echo "<script>location.replace('index.php')</script>";
				}
		}						

}else{
	echo "<script>alert('Autentificare ESUATA. User INCORECT sau INACTIV. Rog contactati Administratorul de sistem')</script>";
}
	
}
if($_SESSION['nr_incercari'] >= $_SESSION['nr_incercari_permise']){		
	$nr_autentif = ("UPDATE `utilizatori` SET  `data_autentificare` = '".$ymdhis."', `ip` = '".$_SERVER['REMOTE_ADDR']."', `status` = 'INACTIV' where id = '".$_SESSION['id_user']."'") or die(mysql_error());	
		if (mysqli_query($conectare, $nr_autentif)) {
			echo "<script>alert('Numarul de incercari de autentificare acceptat a fost atins. Posibilitatea de a va autentifica este restrictionata, va rugam contactati Administratorul de sistem.')</script>";
		}else{
			//echo "<script>alert('EROARE la update user')</script>";
					}
			}
//echo $_SESSION['nr_incercari'];
//if($os_win == '1'){
echo'
<br /><br /><br /><br />
<TABLE width="60%" align="center">
	<form action="" method="post">
		<TR>
			<TD height="100px" align="center" colspan="2">
				Pentru acces in aplicatie este nevoie de autentificare.
					<br />
				Va rugam introduceti datele necesare in campurile de mai jos
					<br />';
			if($_SESSION['nr_incercari'] == '0'){
				echo'Sunt permise maxim <b>'.$_SESSION['nr_incercari_permise'].'</b> incercari de autentificare';
			}else{
				echo'Mai aveti <b>'.($_SESSION['nr_incercari_permise']-$_SESSION['nr_incercari']).'</b> incercari de autentificare';
			}
		echo'</TD>
		</TR>
		<TR>
			<TD align="right">Nume utilizator :</TD>
			<TD align="left"><input id="campdreapta" type="text" name="user" required></TD>
		</TR>
		<TR>
			<TD align="right">Parola :</TD>
			<TD align="left"><input id="campdreapta" type="password" name="parola" value="" required></TD>
		</TR>
		<TR>
			<TD align="center" colspan="2">Toate campurile sunt obligatorii</TD>
		</TR>
		<TR>
			<TD align="center" colspan="2">
				<input type="hidden" name="crt" value="1">
				<input id="butonsubmit" type="submit" name="autentificare" value="Autentificare">
			</TD>
		</TR>
	</form>
</TABLE>';
// }else{
	// echo'
// <br /><br /><br /><br />
// <form action="" method="post">
	// <div id="">Pentru acces in aplicatie este nevoie de autentificare.<br />Va rugam introduceti datele necesare in campurile de mai jos</div>
	// <div id=""><input id="inputautentificare" type="text" name="user" required placeholder="Nume de utilizator"></div>
	// <div id=""><input id="inputautentificare" type="password" name="parola" value="" required  placeholder="Parola"></div>
	// <div id="">Toate campurile sunt obligatorii</div>
		// <input type="hidden" name="crt" value="1">
	// <div id=""><input id="butonsubmit" type="submit" name="autentificare" value="Autentificare"></div>
// </form>';
//}
?>