<?php
	
	//--------------------------------------------------------------------------------------------------------
	// initialisations des variables
	//--------------------------------------------------------------------------------------------------------
	$Flag_debug_GSO1U = true ;
	$Flag_debug_GSO1U = false ;
	
	if ( ! isset($Flag_logged) ) {
		$Flag_logged = true ;
	}
	if ( ! isset($p_password) ) {
		$p_password = "1234" ;
	}

	$k = 0 ; 
	$kk = 0 ;
	
	$active_sites_id_strings				= [] ;
	$active_sites_names_strings				= [] ;
	$active_sites_status_strings			= [] ;
	$active_sites_t_max_strings				= [] ;
	$active_sites_nb_max_strings			= [] ;
	$active_sites_rx_warning_en_strings	= [] ;
	$active_sites_lon_strings				= [] ;
	$active_sites_lat_strings				= [] ;
	$active_sites_ibm_strings				= [] ;
	
	$inactive_sites_id_strings				= [] ;
	$inactive_sites_names_strings			= [] ;
	$inactive_sites_status_strings			= [] ;
	$inactive_sites_t_max_strings			= [] ;
	$inactive_sites_nb_max_strings			= [] ;
	$inactive_sites_rx_warning_en_strings	= [] ;
	$inactive_sites_lon_strings				= [] ;
	$inactive_sites_lat_strings				= [] ;
	$inactive_sites_ibm_strings				= [] ;

	
	$NB_users								= [] ;
	$Flag_found_normal_user				= false ;
	$link_AQCM05_OK						= [] ;
	$get_sites_for_one_user_erreur  	= [] ;
	$get_sites_for_one_user_message 	= [] ;
	
	$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) );

	//--------------------------------------------------------------------------------------------------------
	// tests successifs des différents sites
	//--------------------------------------------------------------------------------------------------------
	for ($i = 0; $i < sizeof($all_sites_id_strings); $i++) {
		
		//--------------------------------------------------------------------------------------------------------
		// vérification de l'existence du site
		//--------------------------------------------------------------------------------------------------------
		if ( isset($Flag_login) ) {
			if ( $Flag_login ) {
				include ("./_identifiants/sql_identification_main.php");
			}
			else {
				include ("../_identifiants/sql_identification_main.php");
			}
		}
		else {
			include ("../_identifiants/sql_identification_main.php");
		}

		$mysql_base = 'AQCM05_SITE_' . $all_sites_id_strings[$i] ;

		$conn_AQCM05 = new mysqli( $mysql_host,$mysql_user,$mysql_password );
		
		if ( empty ( mysqli_fetch_array( mysqli_query( $conn_AQCM05,"SHOW DATABASES LIKE '" . $mysql_base . "'" ) ) ) ) {
			
			if ( $Flag_debug_GSO1U ) {
				echo( "i = " . $i . " -> le site " . $all_sites_id_strings[$i] . " n'existe pas !!! <br>" ) ;
			}
			
			$get_sites_for_one_user_erreur[$i]  = 1 ;
			$get_sites_for_one_user_message[$i] = "la base du site " . $all_sites_id_strings[$i] . " n'existe pas !!!" ;
		}
		else {
			
			$get_sites_for_one_user_erreur[$i]  = 0 ;
			$get_sites_for_one_user_message[$i] = "la base du site " . $all_sites_id_strings[$i] . " existe..." ;
		}
		
		if ( $Flag_debug_GSO1U ) {
			echo( "i = " . $i . " -> " . $get_sites_for_one_user_message[$i] . "<br>" ) ;
		}


		//--------------------------------------------------------------------------------------------------------
		// connexion à la base de données du site
		//--------------------------------------------------------------------------------------------------------
		if ( $get_sites_for_one_user_erreur[$i] == 0 ) {
			
			$link_AQCM05 = mysqli_connect($mysql_host,$mysql_user,$mysql_password, $mysql_base);
			
			if ( ! $link_AQCM05 ) {
				
				$get_sites_for_one_user_erreur[$i]  = 2 ;
				$link_AQCM05_OK[$i] = false ;
				$get_sites_for_one_user_message[$i] = "connexion à la base du site " . $all_sites_id_strings[$i] . " : échec !!!" ;

			}
			else {
				
				$link_AQCM05_OK[$i] = true ;
				$get_sites_for_one_user_message[$i] = "connexion à la base du site " . $all_sites_id_strings[$i] . " : succès..." ;
				
			}
		}

		if ( $Flag_debug_GSO1U ) {
			echo( "i = " . $i . " -> " . $get_sites_for_one_user_message[$i] . "<br>" ) ;
		}

		//--------------------------------------------------------------------------------------------------------
		// recherche des utilisateurs du site testé
		//--------------------------------------------------------------------------------------------------------
		if ( $link_AQCM05_OK[$i] ) {

			$query = "
				SELECT 
					USER_ID,
					USER_PW,
					USER_STATUS,
					USER_LEVEL,
					USER_T_MAX,
					USER_WEEK_MAIL,
					USER_2FA_STATUS,
					USER_2FA_PHONE,
					USER_2FAS_STATUS,
					USER_2FAS_SECRET,
					USER_RX_WARNING_EN 
				FROM AQCM05_SITE_USERS 
				ORDER BY USER_ID
			";

			$result = mysqli_query($link_AQCM05, $query) ; 
			
			if ( $result ) {
				
				if (mysqli_num_rows($result) > 0) {	
					
					$j = 0 ;
					
					while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {

						$aqcm_USER_ID[$j]			= $row["USER_ID"];
						$aqcm_USER_PW[$j]			= $row["USER_PW"];
						$aqcm_USER_STATUS[$j]		= $row["USER_STATUS"];
						$aqcm_USER_LEVEL[$j]		= $row["USER_LEVEL"];
						$aqcm_USER_T_MAX[$j]		= $row["USER_T_MAX"];
						$aqcm_USER_2FA_STATUS[$j]	= $row["USER_2FA_STATUS"];
						$aqcm_USER_2FA_PHONE[$j]	= $row["USER_2FA_PHONE"];
						$aqcm_USER_2FAS_STATUS[$j]	= $row["USER_2FAS_STATUS"];
						$aqcm_USER_2FAS_SECRET[$j]	= $row["USER_2FAS_SECRET"];
				
						//---------------------------------------------------------------
						// les users de site sont limités en droits : 1 à 9
						//---------------------------------------------------------------
						$USER_LEVEL				= $row["USER_LEVEL"];
						if ( $USER_LEVEL > 9 ) {
							$aqcm_USER_LEVEL[$j]	= 1;
						}
						else {
							$aqcm_USER_LEVEL[$j]	= $USER_LEVEL ;
						}
						
						$aqcm_USER_T_MAX[$j]	= intval($row["USER_T_MAX"]);
						
						if ($Flag_debug_GSO1U) {
							echo("j=" . $j . " ; " . $aqcm_USER_ID[$j] . " ; " . $aqcm_USER_PW[$j] . " ; " . $aqcm_USER_STATUS[$j] . " ; " . $aqcm_USER_LEVEL[$j] . " ; " . $aqcm_USER_T_MAX[$j] . "<br>" );
						}
						
						if ( $p_identifiant == $aqcm_USER_ID[$j] ) {
							
							$Flag_found_normal_user = true ;

							if	(
								( $aqcm_USER_STATUS[$j] 	== "ON"   						) &&  
								( $aqcm_USER_LEVEL[$j] 		>= 1  								) &&  
								( $aqcm_USER_T_MAX[$j]		>= $marqueur_temps_maintenant	) &&
								( ( ! $Flag_logged ) || password_verify($p_password, $aqcm_USER_PW[$j] ) )
								) {
								
								$active_sites_id_strings[$k]				= $all_sites_id_strings[$i] ;
								$active_sites_names_strings[$k]			= $all_sites_names_strings[$i] ;
								$active_sites_status_strings[$k]			= $all_sites_status_strings[$i] ;
								$active_sites_t_max_strings[$k]			= $all_sites_t_max_strings[$i] ;
								$active_sites_nb_max_strings[$k]			= $all_sites_nb_max_strings[$i] ;
								$active_sites_rx_warning_en_strings[$k]	= $all_sites_rx_warning_en_strings[$i] ;
								$active_sites_lon_strings[$k]				= $all_sites_lon_strings[$i] ;
								$active_sites_lat_strings[$k]				= $all_sites_lat_strings[$i] ;
								$active_sites_ibm_strings[$k]				= $all_sites_ibm_strings[$i] ;
								
								$user_OK[$k]						= $aqcm_USER_ID[$j] ;
								$password_OK[$k]					= $aqcm_USER_PW[$j] ;
								$status_OK[$k]					= $aqcm_USER_STATUS[$j] ;
								$level_OK[$k]						= $aqcm_USER_LEVEL[$j] ;
								$t_max_OK[$k]						= $aqcm_USER_T_MAX[$j] ;
								
								$NORMAL_USER_ID_ok[$k]				= $aqcm_USER_ID[$j] ;
								$NORMAL_USER_PW_ok[$k]				= $aqcm_USER_PW[$j] ;
								$NORMAL_USER_STATUS_ok[$k]			= $aqcm_USER_STATUS[$j] ;
								$NORMAL_USER_LEVEL_ok[$k]			= $aqcm_USER_LEVEL[$j] ;
								$NORMAL_USER_T_MAX_ok[$k]			= $aqcm_USER_T_MAX[$j] ;
								$NORMAL_USER_2FA_STATUS_ok[$k]	= $aqcm_USER_2FA_STATUS[$j] ;
								$NORMAL_USER_2FA_PHONE_ok[$k]		= $aqcm_USER_2FA_PHONE[$j] ;
								$NORMAL_USER_2FAS_STATUS_ok[$k]	= $aqcm_USER_2FAS_STATUS[$j] ;
								$NORMAL_USER_2FAS_SECRET_ok[$k]	= $aqcm_USER_2FAS_SECRET[$j] ;

								if ($Flag_debug_GSO1U) {
									echo("k = " . $k . " ; site : " . $active_sites_id_strings[$k] . "<br>" );
								}
								
								$k++ ;
								
							}
							else {
								
								$inactive_sites_id_strings[$kk]				= $all_sites_id_strings[$i] ;
								$inactive_sites_names_strings[$kk]				= $all_sites_names_strings[$i] ;
								$inactive_sites_status_strings[$kk]			= $all_sites_status_strings[$i] ;
								$inactive_sites_t_max_strings[$kk]				= $all_sites_t_max_strings[$i] ;
								$inactive_sites_nb_max_strings[$kk]			= $all_sites_nb_max_strings[$i] ;
								$inactive_sites_rx_warning_en_strings[$kk]	= $all_sites_rx_warning_en_strings[$i] ;
								$inactive_sites_lon_strings[$kk]				= $all_sites_lon_strings[$i] ;
								$inactive_sites_lat_strings[$kk]				= $all_sites_lat_strings[$i] ;
								$inactive_sites_ibm_strings[$kk]				= $all_sites_ibm_strings[$i] ;
								$kk++ ;

							}
							
						}
						else {
						}

						$j++ ;
						
					}
					
		
					/*
					*/
					if ($Flag_debug_GSO1U) {
						print_r( $aqcm_USER_ID ) ; echo("<br>");
						print_r( $aqcm_USER_STATUS ) ; echo("<br>");
						print_r( $aqcm_USER_LEVEL ) ; echo("<br>");
						print_r( $aqcm_USER_T_MAX ) ; echo("<br>");
						print_r( $aqcm_USER_2FA_STATUS ) ; echo("<br>");
						print_r( $aqcm_USER_2FA_PHONE ) ; echo("<br>");
						print_r( $aqcm_USER_2FAS_STATUS ) ; echo("<br>");
						print_r( $aqcm_USER_2FAS_SECRET ) ; echo("<br><br>");
					}

					$NB_users[$i] = $j ;
					$get_sites_for_one_user_message[$i] = "résultat de la requête sur le site " . $all_sites_id_strings[$i] . " ; " . $NB_users[$i] . " utilisateurs identifiés et valides..." ;

				}
				else {
					
					$NB_users[$i] = 0 ;
					$get_sites_for_one_user_erreur[$i]  = 3 ;
					$get_sites_for_one_user_message[$i] = "résultat de la requête sur le site " . $all_sites_id_strings[$i] . " : vide !!!" ;
				}


			}
			else {
				
				$NB_users[$i] = 0 ;
				$get_sites_for_one_user_erreur[$i]  = 4 ;
				$get_sites_for_one_user_message[$i] = "requête sur le site " . $all_sites_id_strings[$i] . " : échec !!!" ;

			}

		}

		if ( $Flag_debug_GSO1U ) {
			echo( "i = " . $i . " -> " . $get_sites_for_one_user_message[$i] . "<br>" ) ;
		}


			
		//--------------------------------------------------------------------------------------------------------
		// clôture
		//--------------------------------------------------------------------------------------------------------
		if ( $link_AQCM05_OK[$i] ) {
			mysqli_close($link_AQCM05);
		}

	}



	//--------------------------------------------------------------------------------------------------------
	// retour $Nb_sites, $active_sites_id_strings
	//--------------------------------------------------------------------------------------------------------
	$Nb_sites = $k ;

	if ($Flag_debug_GSO1U) {
		echo("<br><br><br><br>");
		echo "NB_sites = " . $Nb_sites . "<br>";
		print_r( $active_sites_id_strings ) ; echo("<br>");
		print_r( $level_OK ) ; echo("<br><br>");
	}
		
		

	//--------------------------------------------------------------------------------------------------------

?>
