 <?php 

	// https://www.aqcm.fr/v2/M1/login_test.php
	
	include("../M0/get_parameters.php") ;

	$Flag_debug_login = false ;
	$Flag_debug_login = true ;

	$marqueur_temps_maintenant = time();
	$marqueur_temps_maintenant = mktime(0, 0, 0, date("m", $marqueur_temps_maintenant)  , date("d", $marqueur_temps_maintenant) + 1, date("Y", $marqueur_temps_maintenant) );

	$errorMessage = '';
	$errorMessage_2 = '';

	// recensement des sites
	include ("../M0/get_AQCM05_sites.php");
	$all_sites_id_strings = $sites_from_AQCM05_SITES_LIST ;

	//if ( ! empty( $_POST ) ) {
	if ( true ) {
		
		//if ( empty( $_POST['identifiant'] ) ) {
		if ( false ) {
			$errorMessage = 'le champ "identifiant" est vide !';
		}
		//elseif ( empty( $_POST['password'] ) ) {
		if ( false ) {
			$errorMessage = 'le champ "password" est vide !';
		}
		else {
			
			//-----------------------------------------------------------------
			//$p_identifant	= $_POST['identifiant'] ;
			//$p_password		= $_POST['password'] ;
			
			$p_identifant	= "jerome.candau@aqcm05.fr" ;
			$p_password		= "124816aBCDeF" ;
			
			$p_identifant	= "jerome.candau@hermes05.fr" ;
			$p_password		= "1205864K" ;
			
			$p_identifant	= "jeromecandau@aqcm05.fr" ;
			$p_password		= "1205864K" ;
			
			//-----------------------------------------------------------------
			$user_is_found  = false ;
			$user_type		= "none" ;
			$base_users  = "none" ;
			$table_users = "none" ;

			
			//-----------------------------------------------------------------
			// recensement des identifiants et passwords des "super_users"
			//-----------------------------------------------------------------
			include ("../M0/get_super_users.php");
			$k = 0 ;
			
			if ( $NB_super_users > 0 ) {
				
				for ($j = 0; $j < sizeof($aqcm_SUPER_USER_ID); $j++) {
					
					if ( $Flag_debug_login ) echo( "super-user : " . $aqcm_SUPER_USER_ID[$j] . "<br>" ) ; 
					
					if ( 
						( $aqcm_SUPER_USER_ID[$j] 			== $p_identifant 				) && 
						( $aqcm_SUPER_USER_PW[$j] 			== $p_password 					) && 
						( $aqcm_SUPER_USER_STATUS[$j] 		== "ON" 						) &&  
						( $aqcm_SUPER_USER_LEVEL[$j]  		>= 90 							) &&  
						( $int_aqcm_SUPER_USER_T_MAX[$j] 	>= $marqueur_temps_maintenant 	) 
					) {
						
						$sites_id_strings	= $all_sites_id_strings ;

						$user_OK[$k]		= $aqcm_SUPER_USER_ID[$j] ;
						$password_OK[$k]	= $aqcm_SUPER_USER_PW[$j] ;
						$status_OK[$k]		= $aqcm_SUPER_USER_STATUS[$j] ;
						$level_OK[$k]		= $aqcm_SUPER_USER_LEVEL[$j] ;
						$t_max_OK[$k]		= $aqcm_SUPER_USER_T_MAX[$j] ;
						
						$k++ ;
						
					}
					
				}
				
				if ( $Flag_debug_login ) echo( "k = " . $k . "<br>" ) ; 	
				
				//-----------------------------------------------------------------
				// si k == 1, l'utilisateur est un "super_user" unique !
				//-----------------------------------------------------------------
				if ( $k == 1 ) {
					
					$user_is_found  = true ;
					$user_type		= "super_user" ;
					$base_users     = "AQCM05_main" ;
					$table_users    = "AQCM05_SUPER_USERS" ;
					
				}
				else {
					if ( $Flag_debug_login ) echo( "pas dans super-users..." . "<br>" ) ; 					
				}

			}
			
			//-----------------------------------------------------------------
			// si non super-user, on le cherche dans les utilisateurs de sites
			//-----------------------------------------------------------------
			if ( ! $user_is_found ) {
				
				// recensement des sites valides pour cet utilisateurs
				include ("../M0/get_sites_of_1_user.php");
				
				if ( $Nb_sites <= 0 ) {
					
					$errorMessage = 'utilisateur non identifié !!!';
					if ( $Flag_debug_login ) echo( $errorMessage . "<br>" ) ; 					
					
				}
				else {

					$user_is_found	= true ;
					$user_type		= "normal_user" ;
					$base_users		= "AQCM05_SITE_" . $sites_id_strings[0] ;
					$table_users	= "AQCM05_SITE_USERS" ;
					if ( $Flag_debug_login ) echo( join(',', $sites_id_strings) . "<br>" ) ; 					

				}
				
			}

			//-----------------------------------------------------------------
			// si utilisateur identifié, on ouvre la session
			//-----------------------------------------------------------------
			if ( $user_is_found ) {

				session_start();

				// On crée les variables de session
				$_SESSION['login']			= $p_identifant ;
				$_SESSION['password']		= $p_password ;
				$_SESSION['user_type']		= $user_type ;
				$_SESSION['list_sites']		= $sites_id_strings ;
				$_SESSION['table_users'] 	= $table_users ;
				
				$_SESSION['index_session'] 	= 0 ;
				
				$_SESSION['SITE_ID']		= $sites_id_strings[$_SESSION['index_session']] ;
				$_SESSION['base_users']  	= "AQCM05_SITE_" . $_SESSION['SITE_ID'] ;
				$_SESSION['status']			= $status_OK[$_SESSION['index_session']] ;
				$_SESSION['level']			= $level_OK[$_SESSION['index_session']] ;
				$_SESSION['t_max']			= $t_max_OK[$_SESSION['index_session']] ;
				$_SESSION['login_level'] 	= $_SESSION['login'] . "&nbsp;&nbsp;&nbsp;(" . $_SESSION['level'] . ")" ;

				/*
				switch ($_SESSION['level']) {
					case 1:
					case 2:
					case 3:
					case 4:
					case 5:
					case 6:
					case 7:
					case 8:
					case 9:
					case 99:
						header('Location: ' . $root_dir . '/M1/index_2.php');
						exit();
						break;
					default:
						header('Location: ' . $root_dir . '/M1/login.php');
						exit();
						break;
							
				}
				*/
				
			}
			
		}
		
	}
	
	
		

?>

