<?php
include('common.php');

//1st check for for user in ED table
$SQL="SELECT * FROM execdir WHERE email like '".secure_user_input($_POST['email'])."' 
		AND password = '".secure_user_input($_POST['password'])."' ";
$results = llnb_query($SQL);
$row = llnb_fetch_assoc($results);
if ($row)
	$UT=$_EXECDIR;

if (!$row)
{
	//2nd: check for for user in Coord table
	$SQL="SELECT * FROM coordinator WHERE active=1 AND alive=1 AND email like '".secure_user_input($_POST['email'])."' 
			AND password = '".secure_user_input($_POST['password'])."' ";
	$results = llnb_query($SQL);
	$row = llnb_fetch_assoc($results);
	
	if ($row)
	{	
		$UT=$_COORD;
		$council_id= $row['council_id'];
	}
		

}

if (!$row)
{
	//3rd: check for for user in Tutor table
	$SQL="SELECT * FROM tutor WHERE active=1 AND email like '".secure_user_input($_POST['email'])."' 
			AND password = '".secure_user_input($_POST['password'])."' ";
	$results = llnb_query($SQL);
	$row = llnb_fetch_assoc($results);
	
	if ($row)
	{	
		$UT=$_TUTOR;
		$council_id= $row['council_id'];
	}

	

}



/********************************************************
SITE STATES
- user login failed: 1 - no match of pass/user
- user not logged in: 2 - cookies not enabled properly in browser, or security breach
- user logged out: 3
- user is registered but not ACTIVATED: 4
- user logged in: n/a 
**************************************************************/


if ($row)
{
	if(count($_SESSION)>0) //reset session in case
		kill_session();	

	//check that cookies are enabled in browser of client otherwise start session
	if ( session_start() )
	{
		$_SESSION['user_id']=$row['id'];
		$_SESSION['firstname']=$row['first_name'];
		$_SESSION['lastname']=$row['last_name'];
		$_SESSION['ulevel']=$UT; 
		$_SESSION['module_loaded']="overview";

		if ($UT == $_COORD || $UT == $_TUTOR)
			$_SESSION['council_id']= $council_id;

		//one day
		ini_set('session.gc_maxlifetime', 1200);
			
		header("Location: ./home.php");
	}
	else
		header("Location: index.php?s=2");

		

}
else
{
	if(count($_SESSION)>0)	//force logout automatically
		kill_session();
		
	unset($row);
	
	header("Location: index.php?s=1&email=".$_POST['email']."");
}

?>

