<?php
session_start();
require($_SERVER['DOCUMENT_ROOT'].'/site/config/php/lib.inc.php');
require($_SERVER['DOCUMENT_ROOT'].'/site/config/php/var.inc.php');
require($_SERVER['DOCUMENT_ROOT'].'/espace_personnel/php/lib.php');

//echo "<pre>";print_r($_POST);echo "</pre>";

$login	= addslashes($_POST['login']);
$pass	= addslashes($_POST['pass']);
$_SESSION['user']['id'] = 0;


// Connexion par numero d'auditeur
	if(is_numeric($login) && $pass=="Arcnam974"){

		$sql = dbquery("SELECT * FROM auditeurs_diplomes WHERE id_auditeur = '".$login."' ");

		if(dbnumrows($sql)>0){
			$_SESSION['user']['id'] = dbresult($sql,0,"id");
			$_SESSION['user']['session'] = 1;
		}
	}

// Connexion par login/mdp
	else{
		$sql = dbquery("SELECT * FROM auditeurs WHERE login = '".$login."' AND password = '".$pass."' ");

		if(dbnumrows($sql)>0)	{
			$_SESSION['user']['id'] = dbresult($sql,0,"id");
			$_SESSION['user']['session'] = 1;
		}
	}
	
// Pas d'id trouvé
	if($_SESSION['user']['id'] == 0){
		Header("Location: index.php?connexion=fail");
		exit();
	}

// Récupération données relatives à l'utilisateur
	$id = $_SESSION['user']['id'];
	$sql = dbquery("SELECT * FROM auditeurs,auditeurs_diplomes 
					WHERE auditeurs.id = '".$id."' 
					AND auditeurs.id = auditeurs_diplomes.id 
					ORDER BY auditeurs_diplomes.annee_universitaire DESC
					LIMIT 1
			");
	
	$_SESSION['user']['id_auditeur']		= dbresult($sql,0,"id_auditeur");
	$_SESSION['user']['nom_patronyme']		= dbresult($sql,0,"nom_patronyme");
	$_SESSION['user']['nom_marital']		= dbresult($sql,0,"nom_marital");
	$_SESSION['user']['prenom']				= dbresult($sql,0,"prenom");
	$_SESSION['user']['adresse']			= dbresult($sql,0,"adresse");
	$_SESSION['user']['code_postal']		= dbresult($sql,0,"code_postal");
	$_SESSION['user']['commune']			= dbresult($sql,0,"commune");
	$_SESSION['user']['tel_etudiant']		= dbresult($sql,0,"tel_etudiant");
	$_SESSION['user']['fax_etudiant']		= dbresult($sql,0,"fax_etudiant");
	$_SESSION['user']['gsm_etudiant']		= dbresult($sql,0,"gsm_etudiant");
	$_SESSION['user']['email_auditeur']		= dbresult($sql,0,"email_auditeur");
	$_SESSION["user"]["agent"]				= $_SERVER['HTTP_USER_AGENT'];

	// Recherche si élève inscrit ou pas
	
	$derniere_annee_inscription	= dbresult($sql,0,"annee_universitaire");

	if($derniere_annee_inscription == $annee_universitaire){
		$_SESSION['user']['inscrit'] = 1;
		Header("Location: eleve/accueil/index.php");
		exit();
	}
	else{
		$_SESSION['user']['inscrit'] = 0;
		Header("Location: eleve/accueil/index.php");
		exit();
	}

?>