<?
class Session {

	function Session() {}
	
	/** Création d'une session **/
	function creerSession($user, $password) {
		$password=md5($password);
		$sql="SELECT login, nom, prenom, etat FROM utilisateurs WHERE login='$user' AND password='$password' LIMIT 0,1";
		//echo $sql;
		//echo $password;
		$db=new DB();
		$db->query($sql);
		$ret=$db->num_rows();
		$db->next_record();
		$_SESSION['connected']=true;
		$_SESSION['mon_login']=$user;
		$_SESSION['mon_nom']=$db->f('nom');
		$_SESSION['mon_prenom']=$db->f('prenom');
		$_SESSION['mon_email']=$db->f('login');
		Session::majSession($user);
		return $ret;
	}
	
	function majSession($user, $page='') {
		if ($_SESSION['connected']==true) {
			$db=new DB();
			$sql="SELECT lastAction FROM sessions WHERE login='$user' LIMIT 0,1";
			$db->query($sql);
			if ($db->num_rows()>0)
				$db->query("UPDATE sessions SET lastAction='".DATETIME_LOG."', page='$page' WHERE login='".$user."'");
			else
				$db->query("INSERT INTO sessions (login, lastAction, page) VALUES ('$user', '".DATETIME_LOG."', '$page');");
			return $ret;
		}
		else return false;
	}
	
	function existeSession($user) {
		if ($_SESSION['connected']) {
			Session::existeSession($user);
			return true;
		}
		else
			return false;
	}

	function supprSession($user) {
		if ($_SESSION['connected']) {
			$_SESSION['connected']=false;
			$_SESSION['mon_login']='';
			$_SESSION['mon_nom']='';
			$_SESSION['mon_prenom']='';
			$_SESSION['mon_email']='';
			Session::majSession($user);
			$db=new DB();
			$sql="DELETE FROM sessions WHERE login='$user' LIMIT 1";
			$db->query($sql);
			session_destroy();
			return true;
		}
		else
			return false;
	}

}
 ?>