 <?php 

	//-------------------------------------------------------------------------
	include("../M0/get_parameters.php") ;

	//-------------------------------------------------------------------------
	$Flag_debug_login = 1;
	$Flag_debug_login = 0;

	$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 ( empty( $_POST['identifiant'] ) ) {
			$errorMessage = 'le champ "identifiant" est vide !';
		}
		elseif ( empty( $_POST['password'] ) ) {
			$errorMessage = 'le champ "password" est vide !';
		}
		else {
			
			//-----------------------------------------------------------------
			$p_identifant		= $_POST['identifiant'] ;
			$p_password		= $_POST['password'] ;
			
			//-----------------------------------------------------------------
			$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 ( 
						( $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++ ;
						
					}
					
				}
				
				//-----------------------------------------------------------------
				// 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" ;
					
				}

			}
			
			//-----------------------------------------------------------------
			// 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é !!!';
					
				}
				else {

					$user_is_found	= true ;
					$user_type		= "normal_user" ;
					$base_users		= "AQCM05_SITE_" . $sites_id_strings[0] ;
					$table_users		= "AQCM05_SITE_USERS" ;

				}
				
			}

			//-----------------------------------------------------------------
			// 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['table_users'] 	= $table_users ;
				
				$_SESSION['list_sites']		= $sites_id_strings ;
				$_SESSION['status_OK'] 		= $status_OK ;
				$_SESSION['level_OK'] 		= $level_OK ;
				$_SESSION['t_max_OK'] 		= $t_max_OK ;
				
				$_SESSION['index_session'] 	= 0 ;
				
				$_SESSION['SITE_ID']		= $sites_id_strings[$_SESSION['index_session']] ;
				$_SESSION['base_users']  	= "AQCM05_SITE_" . $_SESSION['SITE_ID'] ;
				$_SESSION['status']			= $_SESSION['status_OK'][$_SESSION['index_session']] ;
				$_SESSION['level']			= $_SESSION['level_OK'][$_SESSION['index_session']] ;
				$_SESSION['t_max']			= $_SESSION['t_max_OK'][$_SESSION['index_session']] ;
				$_SESSION['login_level'] 	= $_SESSION['login'] . "&nbsp;&nbsp;&nbsp;(" . $_SESSION['level'] . ")" ;
				
				$_SESSION['aqcm_BAT']		= "B99" ;
				$_SESSION['aqcm_NIVEAU']	= "N99" ;

				switch ($_SESSION['level']) {
					case ( ( $_SESSION['level'] >=   1 ) && ( $_SESSION['level']<=9 ) ) :
					case ( ( $_SESSION['level'] ==  30 ) ) :
					case ( ( $_SESSION['level'] >=  90 ) && ( $_SESSION['level']<=99 ) ) :
					case ( ( $_SESSION['level'] >= 100 ) && ( $_SESSION['level']<=127 ) ) :
						header('Location: ' . $root_dir . '/M1/index_2.php');
						exit();
						break;
					default:
						header('Location: ' . $root_dir . '/login.php');
						exit();
						break;
							
				}
				
			}
			
		}
		
	}
		

?>


<?php 
	include("../M0/communs_en_tete_HTML.html") ;
?>

<font size="1" face="Arial, Helvetica, sans-serif">&nbsp;</font>

<table align="center" width="<?php echo $tables_width ; ?>"  border="2" cellspacing="2" cellpadding="2" bordercolor="#ffffff" bgcolor="#000033">	
	
	<?php
		include('../M0/titre_jaune.php');
		$sous_menu_titre = "-&nbsp;&nbsp;sondes AQCM05&nbsp;&nbsp;-";
		affiche_titre_jaune_simple($sous_menu_titre);
	?>
	
</table>

<font size="1" face="Arial, Helvetica, sans-serif">&nbsp;</font>

<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">

<table align="center" width="<?php echo $tables_width ; ?>" border="2" cellspacing="2" cellpadding="2" bordercolor="#ffffff" bgcolor="#000033">	

	<tr align="center" valign="middle" bordercolor="#666666" bgcolor="#000033">
	
		<td>
			
			<?php
			  // Rencontre-t-on une erreur ?
			  if ( ! empty($errorMessage) ) 
			  {
				echo '<font color="Salmon"><p>', htmlspecialchars($errorMessage),'<br>' ,htmlspecialchars($errorMessage_2),'</p></font>';
			  }
			?>
					
			<table width="99%">	
				<tr>
					<td align="right" width="25%">
						<label>
						utilisateur :
						</label>
					</td>
					<td>
						<input type="text" name="identifiant" id="identifiant" value="" size="25" class="txtField_A" >
					</td>
				</tr>
				<tr>
					<td align="right">
						<label>
						mot de passe :
						</label>
					</td>
					<td>
						<input type="password" name="password" id="password" value="" size="25" class="txtField_A" >
						<input type="submit" formaction="./lost_password.php" value="&nbsp;???&nbsp;" class="button_C" >
					</td>
				</tr>

				<tr>
					<td align="center" colspan="2">
					
						<input id="check_box_1" type="checkbox" onclick="myFunction()" class="txtField_B" >
						<label for="check_box_1"  class="txtField_B" >
							&nbsp;&nbsp;afficher le mot de passe
						</label>

					</td>
				</tr>
					

				<tr>
					<td align="center" colspan="2">

						<input type="submit" name="submit" value="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;connexion&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" class="button_A" />

					</td>
				</tr>

			</table>

		</td>

	</tr>

	
	<tr>
		<td>
			<br>
				<div align="center">
					<!--<img src="../photos/AQCM05_HW6_8_1024.JPG" alt="sonde AQCM05" style="width:350;">-->
					<img src="../photos/logo_1024.jpg" alt="sonde AQCM05" style="width:420;"> 
				</div>
			<br>
		</td>
	</tr>

	
</table>
					
</form>

<?php 
	include('../M0/communs_bouton_retour_index.html'); 
?>
	
<script>
	function myFunction() {
	  var x = document.getElementById("password");
	  if (x.type === "password") {
		x.type = "text";
	  } else {
		x.type = "password";
	  }
	}
</script>

</body>

</html>

