<?php
if (file_exists("embedded/functions/site_functions.php"))
	{
	include_once("embedded/functions/site_functions.php");
	}
else
	{
include_once("../../embedded/functions/site_functions.php");
	}
if ((isset($_SESSION['language']) && !empty($_SESSION['language'])) || (isset($_SESSION['lang']) && !empty($_SESSION['lang'])))
	{
	if (!empty($_SESSION['language']))
	{
	$language=$_SESSION['language'];
	}
	else
	{
	$language=$_SESSION['lang'];
	}
if ($language==1)
	{
	$head1="Κωδικός Δεκτός!";
	$head2="Κωδικός Λάθος!";
	}
else
	{
	$head1="Valid Password!";
	$head2="Invalid Password!";
	}
}
else
	{
	$head1="Valid Password!";
	$head2="Invalid Password!";
	}
db_open();
if (isset($_GET["q"]) && !empty($_GET["q"]))
{
$q= $_GET["q"];
$a=password($option=3,$_SESSION['userlevel'],$_SESSION['email'],$q,$_SESSION['device_id'],0);
if ($a==1)
	{
		echo'<span class="valid">'.$head1.'</span>';
	}
else
	{
		echo'<span class="invalid">'.$head2.'</span>';
	}

}
else if ((isset($_POST["email"]) && !empty($_POST["email"])) && (isset($_POST["password"]) && !empty($_POST["password"])))
	{
	$email= strip_tags(htmlspecialchars(mysql_real_escape_string(trim($_POST["email"]))));
	$password= strip_tags(htmlspecialchars(mysql_real_escape_string(trim($_POST["password"]))));
	if (isset($_POST["remember_me"]) && !empty($_POST["remember_me"]))
		{
		$remember= strip_tags(htmlspecialchars(mysql_real_escape_string(trim($_POST['remember_me']))));
		}
	else
		{
		$remember=0;
		}
	if(preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $email))
		{
		
		$b=user_find($email);
		if( $b[0]!="")
		{
		$time=time();
		$a=password(2,0,$email,$password,1,$time);
		if ($a==1 || $a==11)
			{
			$_SESSION['email']=$b[2];
			$_SESSION['password']=$b[12];
			$_SESSION['device_id']=$b[1];
			$_SESSION['userlevel']=$b[3];
			$_SESSION['language']=$b[16];
			if($remember==1)
				{
				setcookie('email',"$b[2]",time()+3600);
				setcookie('password',"$b[12]",time()+3600);
				setcookie('device_id',"$b[1]",time()+3600);
				setcookie('userlevel',"$b[3]",time()+3600);
				setcookie('language','$b[16]',time()+3600);
				}
			if ($a==1)
				{
				header("Location:../../index.php");
				}
			else
				{
				header("Location:../../admin.php");
				}
			}
		else
			{
			$msg="1";
			header("Location:../../index.php?q=".$msg);
			}
		
		}
		else
		{
		$msg=2;
		header("Location:../../index.php?q=".$msg);
		}
	}
else
	{
	session_unset();
	header("Location:../../index.php");
	}
}
else
	{
	session_unset();
	header("Location:../../index.php");
	}
db_close();	
?>