 <html>

	<head>
	
		<title>AQCM</title>
		<meta name="Description" content="Sondes AQCM05">
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

		<meta name="viewport" content="width=device-width, initial-scale=0.80">
		
		<script language="javascript" type="text/javascript" src="./M0/jquery.min.js"></script>
		
		<link rel="stylesheet" type="text/css" href="./styles/AQCM05.css" />

		<link rel="manifest" href="./manifest.json">
		<link rel="icon" href="./photos/logo_1024_b.jpg">

		<style>
			#blink_red {
				font-size: 18px;
				font-style:italic;  
				font-family: Arial, Helvetica, sans-serif;
				color: Red;
			}
		</style>

	</head>
	
<body bgcolor="#000033" text="#CCCCCC" link="#00CCFF" vlink="#9966FF" alink="#CC0000">

<table align="center" width="480px" border="2" cellspacing="2" cellpadding="2" bordercolor="#ffffff" bgcolor="#000033">	
	
	<tr valign="middle" bordercolor="#ffffff"> 
		<td>
			<table align="center">
			
				<tr>
					<td align="center" height="38" >
						<font size="5" color="lightblue" face="Arial, Helvetica, sans-serif">
							- sondes AQCM05 -
						</font>						
					</td>
				</tr>
				
			</table>				
		</td>
	</tr>
	
</table>

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

 <?php 

	//-------------------------------------------------------------------------
	$Flag_login = true ;

	session_start();
	
	if ( isset( $_SESSION['login'] ) ) {
		header('Location: ./M1/index_2.php');
		exit();
	}

	//-------------------------------------------------------------------------
	$FLAG_session_is_destroyed = false ;
	if ( isset( $_SESSION['lost_password_login'] ) ) {
		$_SESSION = array();
		session_destroy();
		unset($_SESSION);
		$FLAG_session_is_destroyed = true ;
	}
	
	//-------------------------------------------------------------------------
	include("./M0/get_parameters.php") ;
	include("./M0/logging.php");
	include("./M0/logging_function.php");

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

	$p_identifiant	= "none" ;
	$log_user_type	= "inconnu" ;
	
	$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) );

	$error = '0' ;
	$errorMessage = '';
	$errorMessage_2 = '';

	//-------------------------------------------------------------------------
	// recensement de tous les sites
	//-------------------------------------------------------------------------
	include ("./M0/get_AQCM05_sites.php");
	$all_sites_id_strings 				= $sites_from_AQCM05_SITES_LIST ;
	$all_sites_names_strings 			= $sites_from_AQCM05_SITES_NAMES ;
	$all_sites_status_strings			= $sites_from_AQCM05_SITES_STATUS ;
	$all_sites_t_max_strings			= $sites_from_AQCM05_SITES_T_MAX ;
	$all_sites_nb_max_strings			= $sites_from_AQCM05_SITES_NB_MAX ;
	$all_sites_rx_warning_en_strings	= $sites_from_AQCM05_SITES_RX_WARNING_EN ;
	$all_sites_lon_strings 				= $sites_from_AQCM05_SITES_LON ;
	$all_sites_lat_strings 				= $sites_from_AQCM05_SITES_LAT ;
	$all_sites_ibm_strings 				= $sites_from_AQCM05_SITES_IBM ;
	
	//-------------------------------------------------------------------------
	if ( ! empty( $_POST ) ) {
		
		if ( empty( $_POST['identifiant'] ) ) {
			$error = '1' ;
			$errorMessage = 'le champ "identifiant" est vide !';
		}
		elseif ( empty( $_POST['password'] ) ) {
			$error = '2' ;
			$errorMessage = 'le champ "password" est vide !';
		}
		else {
			
			//-----------------------------------------------------------------
			$p_identifiant	= $_POST['identifiant'] ;
			$p_password		= $_POST['password'] ;
			
			//-----------------------------------------------------------------
			$Flag_found_super_user	= false ;
			$found_super_user		= false ;
			
			$found_normal_user		= false ;
			
			$user_type				= "none" ;
			$base_users				= "none" ;
			$table_users				= "none" ;
			
			//-----------------------------------------------------------------
			// recensement des identifiants et passwords des "super_users"
			//-----------------------------------------------------------------
			include ("./M0/get_super_users.php");
			
			if ( $NB_super_users > 0 ) {
				
				$k = 0 ;
				
				$sites_id_strings		= $all_sites_id_strings ;
				$sites_status_strings	= $all_sites_status_strings ;
				$sites_t_max_strings	= $all_sites_t_max_strings ;
				$sites_nb_max_strings	= $all_sites_nb_max_strings ;

				$off_sites_id_strings	= [] ;

				for ($j = 0; $j < sizeof($aqcm_SUPER_USER_ID); $j++) {
					
					if ( $aqcm_SUPER_USER_ID[$j] == $p_identifiant ) {
						
							$Flag_found_super_user	= true ;
							
							if ( 
									( $aqcm_SUPER_USER_STATUS[$j] 	== "ON" 							) &&  
									( $aqcm_SUPER_USER_LEVEL[$j]  	>= 90 								) &&  
									( $int_aqcm_SUPER_USER_T_MAX[$j] 	>= $marqueur_temps_maintenant	) &&  
									( password_verify( $p_password, $aqcm_SUPER_USER_PW[$j] )   	)
								//	( $p_password == $aqcm_SUPER_USER_PW[$j] 							)
								) {
								
								$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] ;
								
								$SUPER_USER_ID_ok[$k]			= $aqcm_SUPER_USER_ID[$j] ;
								$SUPER_USER_PW_ok[$k]			= $aqcm_SUPER_USER_PW[$j] ;
								$SUPER_USER_STATUS_ok[$k]		= $aqcm_SUPER_USER_STATUS[$j] ;
								$SUPER_USER_LEVEL_ok[$k]		= $aqcm_SUPER_USER_LEVEL[$j] ;
								$SUPER_USER_T_MAX_ok[$k]		= $aqcm_SUPER_USER_T_MAX[$j] ;
								$SUPER_USER_2FA_STATUS_ok[$k]	= $aqcm_SUPER_USER_2FA_STATUS[$j] ;
								$SUPER_USER_2FA_PHONE_ok[$k]	= $aqcm_SUPER_USER_2FA_PHONE[$j] ;
								$SUPER_USER_2FAS_STATUS_ok[$k]	= $aqcm_SUPER_USER_2FAS_STATUS[$j] ;
								$SUPER_USER_2FAS_SECRET_ok[$k]	= $aqcm_SUPER_USER_2FAS_SECRET[$j] ;
								
								$k++ ;
								
							}
					
					}
					
				}

				if ( $Flag_found_super_user ) {
					
					//-----------------------------------------------------------------
					// si k == 1, l'utilisateur est un "super_user" unique !
					//-----------------------------------------------------------------
					if ( $k == 1 ) {
						
						$log_user_type		= "administrateur" ;
						$user_type			= "super_user" ;
						$found_super_user	= true ;

						$base_users			= "AQCM05_main" ;
						$table_users			= "AQCM05_SUPER_USERS" ;
						
					}
					else {
						
						$log_user_type		= "administrateur rejeté" ;
						$error = '3' ;
						$errorMessage = "administrateur identifié mais l'accès est refusé !!!";
						
					}
					
				}
				else {
					
					//-----------------------------------------------------------------
					// si non super-user, on le cherche dans les utilisateurs de sites
					//-----------------------------------------------------------------

					// recensement des sites valides pour cet utilisateurs
					include ("./M0/get_sites_of_1_user.php");
					
					if ( $Nb_sites <= 0 ) {
						
						if ( $Flag_found_normal_user ) {
							$log_user_type		= "utilisateur rejeté" ;
							$error = '4' ;
							$errorMessage = "utilisateur standard identifié mais l'accès est refusé !!!";
						}
						else {
							$log_user_type		= "utilisateur inconnu" ;
							$error = '5' ;
							$errorMessage = "utilisateur non identifié : accès refusé !!!";
						}
						
					}
					else {

						$log_user_type		= "utilisateur" ;
						$user_type			= "normal_user" ;
						$found_normal_user	= true ;						

						$base_users			= "AQCM05_SITE_" . $sites_id_strings[0] ;
						$table_users			= "AQCM05_SITE_USERS" ;
						
						$sites_id_strings		= $active_sites_id_strings ;
						$sites_status_strings	= $active_sites_status_strings ;
						$sites_t_max_strings	= $active_sites_t_max_strings ;
						$sites_nb_max_strings	= $active_sites_nb_max_strings ;

						$off_sites_id_strings	= $inactive_sites_id_strings ;

					}

				}

			}
			
			//-----------------------------------------------------------------
			// si utilisateur identifié, on ouvre la session
			//-----------------------------------------------------------------
			if ( $found_super_user || $found_normal_user ) {

				if ( $FLAG_session_is_destroyed ) {
					session_start();
				}

				// On crée les premières variables de session

				if ( $found_super_user ) {
					$_SESSION['CONNECTED_USER_TYPE']				= "super_user" ;
					$_SESSION['CONNECTED_USER_ID']				= $SUPER_USER_ID_ok[0] ;
					$_SESSION['CONNECTED_USER_PW']				= $SUPER_USER_PW_ok[0] ;
					$_SESSION['CONNECTED_USER_STATUS']			= $SUPER_USER_STATUS_ok[0] ;
					$_SESSION['CONNECTED_USER_LEVEL']				= $SUPER_USER_LEVEL_ok[0] ;
					$_SESSION['CONNECTED_USER_T_MAX']				= $SUPER_USER_T_MAX_ok[0] ;
					$_SESSION['CONNECTED_USER_2FA_STATUS']		= $SUPER_USER_2FA_STATUS_ok[0] ;
					$_SESSION['CONNECTED_USER_2FA_PHONE']		= $SUPER_USER_2FA_PHONE_ok[0] ;
					$_SESSION['CONNECTED_USER_2FAS_STATUS']		= $SUPER_USER_2FAS_STATUS_ok[0] ;
					$_SESSION['CONNECTED_USER_2FAS_SECRET']		= $SUPER_USER_2FAS_SECRET_ok[0] ;
				}
				elseif ( $found_normal_user ) {
					$_SESSION['CONNECTED_USER_TYPE']				= "normal_user" ;
					$_SESSION['CONNECTED_USER_ID']				= $NORMAL_USER_ID_ok[0] ;
					$_SESSION['CONNECTED_USER_PW']				= $NORMAL_USER_PW_ok[0] ;
					$_SESSION['CONNECTED_USER_STATUS']			= $NORMAL_USER_STATUS_ok[0] ;
					$_SESSION['CONNECTED_USER_LEVEL']				= $NORMAL_USER_LEVEL_ok[0] ;
					$_SESSION['CONNECTED_USER_T_MAX']				= $NORMAL_USER_T_MAX_ok[0] ;
					$_SESSION['CONNECTED_USER_2FA_STATUS']		= $NORMAL_USER_2FA_STATUS_ok[0] ;
					$_SESSION['CONNECTED_USER_2FA_PHONE']		= $NORMAL_USER_2FA_PHONE_ok[0] ;
					$_SESSION['CONNECTED_USER_2FAS_STATUS']		= $NORMAL_USER_2FAS_STATUS_ok[0] ;
					$_SESSION['CONNECTED_USER_2FAS_SECRET']		= $NORMAL_USER_2FAS_SECRET_ok[0] ;
				}

				$_SESSION['login']			= $p_identifiant ;
				$_SESSION['password']		= $p_password ;
				$_SESSION['user_type']		= $user_type ;
				
				$_SESSION['admin_menus']	= false ;

				$_SESSION['admin_2FA']			= false ;
				$_SESSION['code_2FA']			= "" ;
				$_SESSION['code_2FA_hashed']	= "" ;
				$_SESSION['code_2FA_t_limit']	= time() ;
				$_SESSION['code_2FA_msg']		= "" ;
				$_SESSION['2FA_code_erreur']	= "0" ;
				
				$_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['list_inactive_sites'] = $off_sites_id_strings ;
				
				$_SESSION['index_session'] 	= 0 ;
				
				$_SESSION['SITE_ID']		= $sites_id_strings[$_SESSION['index_session']] ;
				$_SESSION['SITE_STATUS']	= $sites_status_strings[$_SESSION['index_session']] ;
				$_SESSION['SITE_T_MAX']		= $sites_t_max_strings[$_SESSION['index_session']] ;
				$_SESSION['SITE_NB_MAX']	= $sites_nb_max_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" ;
				$_SESSION['aqcm_PREFIXE']	= "00" ;

				$_SESSION['list_users_of_1_site_id'] 	= ["none"] ;
				
				$_SESSION['user_of_1_site_index']			= -1 ;
				$_SESSION['user_of_1_site_id']			= "none" ;
				$_SESSION['user_of_1_site_status']		= "none" ;
				$_SESSION['user_of_1_site_level']			= "none" ;
				$_SESSION['user_of_1_site_t_max']			= "none" ;
				$_SESSION['user_of_1_site_week_mail']	= "none" ;
				$_SESSION['user_of_1_site_2fa_status']	= "none" ;
				$_SESSION['user_of_1_site_2fa_phone']	= "none" ;

				$_SESSION['target_user_index']		= -1 ;
				$_SESSION['target_user_id']			= "none" ;
				$_SESSION['target_user_status']		= "none" ;
				$_SESSION['target_user_level']		= "none" ;
				$_SESSION['target_user_t_max']		= "none" ;
				$_SESSION['target_user_week_mail']	= "none" ;
				$_SESSION['target_user_2fa_status']	= "none" ;
				$_SESSION['target_user_2fa_phone']	= "none" ;

				switch ($_SESSION['level']) {
					
					case ( ( $_SESSION['level']>=1 ) && ( $_SESSION['level']<=9 ) ) :
					
						log_string( "AQCM05", "INFO -----> " . "connexion de l'" . $log_user_type . " : " . $p_identifiant . " : " . $_SESSION['level'] . " : succès..." ) ;
						header('Location: ' . $root_dir . '/M1/index_2.php');
						exit();
						break;
						
					case ( ( $_SESSION['level']>=90 ) && ( $_SESSION['level']<=99 ) ) :
					
						log_string( "AQCM05", "INFO -----> " . "connexion de l'" . $log_user_type . " : " . $p_identifiant . " : " . $_SESSION['level'] . " : succès..." ) ;
						header('Location: ' . $root_dir . '/M1/index_2.php');
						exit();
						break;
						
					case ( ( $_SESSION['level']>=100 ) && ( $_SESSION['level']<=127 ) ) :
					
						log_string( "AQCM05", "INFO -----> " . "connexion de l'" . $log_user_type . " : " . $p_identifiant . " : " . $_SESSION['level'] . " : succès..." ) ;
						header('Location: ' . $root_dir . '/M1/index_2.php');
						exit();
						break;
						
					default:
					
						log_string( "AQCM05", "WARNING --> " . "connexion de l'" . $log_user_type . " : " . $p_identifiant . " : échec !!! (" . $error . ")" ) ;
						header('Location: ' . $root_dir . '/login.php');
						exit();
						break;
							
				}
				
			}
			else {
				
				log_string( "AQCM05", "WARNING --> " . "connexion de l'" . $log_user_type . " : " . $p_identifiant . " : échec !!! (" . $error . ")" ) ;
			
			}
			
		}
		
	}

?>

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

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

	<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>';
			  }
			?>
			
			</br>
			<table width="99%">	
				<tr>
					<td align="right" >
						<label>
						identifiant :
						</label>
					</td>
					<td align="left" colspan="2" >
						<input type="text" name="identifiant" id="identifiant" value="" size="30" class="txtField_A" style="width:300px;" >
					</td>
				</tr>
				<tr>
					<td align="right" width="120px" >
						<label>
						mot de passe :
						</label>
					</td>
					<td align="left" width="260px">
						<input type="password" name="password" id="password_1" value="" size="30" class="txtField_A" style="width:250px;" >
					</td>
					<td align="left">
						<label class="container"  for="check_box_1" >
							<input type="checkbox" id="check_box_1" name="check_box_1" onclick="myFunction()" >
							<img class="img-unchecked"  src="styles/open_eye_small.png" width="30px">
							<img class="img-checked"    src="styles/close_eye_small.png"   width="30px">
						</label>						
					</td>
				</tr>

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

			<table align="center" width="100%">	
						
				<tr>
				
					<td>
					&nbsp;
					</td>
					
					<td align="center" width="30%" >
						<a href="https://www.aqcm.fr/v2/documentation/index.html" target="_blank" class="a_sans" ><img align="center" src="./photos/documentation.png" alt="" style="height:65" /></a>
					</td>
						
					<td align="center" width="37%">
						<input type="image" src="./photos/login.png" height="65px" alt="submit" />
					</td>

					<td align="center" width="31%" >
						<a href="./M1_1/lost_password.php" class="a_sans" ><img src="./photos/reset_pwd.png" alt="" style="height:65" /></a>
					</td>
				</tr>

			</table>

			</br>
			<div align="center">
				<img src="./photos/logo_1024_b.jpg" alt="sonde AQCM05" style="width:300;"> 
			</div>
			</br>

		</td>
	</tr>

	</form>
	
</table>
					
	
<script>
	function myFunction() {
	  var x = document.getElementById("password_1");
	  if (x.type === "password") {
		x.type = "text";
	  } else {
		x.type = "password";
	  }
	}
</script>

</body>

</html>

