<?php
	
	// http://www.aqcm.fr/v2/M0/get_users_of_1_site.php
	/*
	$p_aqcm_SITE_ID = "05230_002";
	*/
	
	
	//--------------------------------------------------------------------------------------------------------
	$Flag_debug_A = true ;
	$Flag_debug_A = false ;

	//--------------------------------------------------------------------------------------------------------
	if ( isset( $_SESSION['SITE_ID'] ) ) {
		$p_aqcm_SITE_ID = $_SESSION['SITE_ID'] ;
	}
	
	//--------------------------------------------------------------------------------------------------------
	// initialisations des variables
	//--------------------------------------------------------------------------------------------------------
	$NB_users    					= 0 ;
	
	$aqcm_USER_ID					= [];
	$aqcm_USER_PW					= [];
	$aqcm_USER_STATUS			= [];
	$aqcm_USER_LEVEL				= [];
	$aqcm_USER_T_MAX				= [];
	$aqcm_USER_WEEK_MAIL		= [];
	$aqcm_USER_2FA_STATUS		= [];
	$aqcm_USER_2FA_PHONE		= [];
	$aqcm_USER_RX_WARNING_EN	= [];

	$aqcm_USER_ID[0]					=	"" ;
	$aqcm_USER_PW	[0]					=	"" ;
	$aqcm_USER_STATUS[0]			=	"" ;
	$aqcm_USER_LEVEL[0]				=	"" ;
	$aqcm_USER_T_MAX[0]				=	"" ;
	$aqcm_USER_WEEK_MAIL[0]			=	"" ;
	$aqcm_USER_2FA_STATUS[0]		=	"" ;
	$aqcm_USER_2FA_PHONE[0]			=	"" ;
	$aqcm_USER_RX_WARNING_EN[0]	=  "" ;

	$link_AQCM05_OK = false ;
	$get_users_of_one_site_erreur  = 0 ;
	$get_users_of_one_site_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) );

	//--------------------------------------------------------------------------------------------------------
	// connexion à la base de données du site
	//--------------------------------------------------------------------------------------------------------
	include ("../_identifiants/sql_identification_sans_base.php");
	$mysql_base = 'AQCM05_SITE_' . $p_aqcm_SITE_ID ;
	
	if ( $Flag_debug_A ) {
		echo( $mysql_base ); echo '<br>';
	}


	$conn_AQCM05 = new mysqli( $mysql_host,$mysql_user,$mysql_password );
	
	if ( empty ( mysqli_fetch_array( mysqli_query( $conn_AQCM05,"SHOW DATABASES LIKE '$mysql_base'" ) ) ) ) {
		$get_users_of_one_site_erreur  = 1 ;
		$get_users_of_one_site_message = "la base du site " . $p_aqcm_SITE_ID . " n'existe pas !!!" ;
	}

	if ( $get_users_of_one_site_erreur == 0 ) {
		
		$link_AQCM05 = mysqli_connect($mysql_host,$mysql_user,$mysql_password, $mysql_base);
		
		if ( ! $link_AQCM05 ) {
			
			$get_users_of_one_site_erreur  = 2 ;
			$get_users_of_one_site_message = "connexion à la base du site " . $p_aqcm_SITE_ID . " : échec !!!" ;
			
			if ( $Flag_debug_A ) {
				echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL; echo '<br>';
				echo "Debugging error: " . mysqli_connect_error() . PHP_EOL; echo '<br>';
				echo "Connexion impossible à la base de donnée MySQL !" . PHP_EOL; echo '<br>';
			}

		}
		else {
			$link_AQCM05_OK = true ;
		}
	}

	//--------------------------------------------------------------------------------------------------------
	// recherche des utilisateurs du site demandé
	//--------------------------------------------------------------------------------------------------------
	if ( $link_AQCM05_OK ) {

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

		if ($Flag_debug_A) {
			echo $query . "<br>" ;
		}

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

					$aqcm_USER_ID[$i]				= $row["USER_ID"];
					$aqcm_USER_PW[$i]				= $row["USER_PW"];
					$aqcm_USER_STATUS[$i]			= $row["USER_STATUS"];
					$aqcm_USER_LEVEL[$i]			= $row["USER_LEVEL"];
					$aqcm_USER_T_MAX[$i]			= $row["USER_T_MAX"];
					$aqcm_USER_T_MAX_dmy[$i]		= date( "d/m/y", $aqcm_USER_T_MAX[$i] );
					$aqcm_USER_WEEK_MAIL[$i]		= $row["USER_WEEK_MAIL"];
					$aqcm_USER_2FA_STATUS[$i]		= $row["USER_2FA_STATUS"];
					$aqcm_USER_2FA_PHONE[$i]		= $row["USER_2FA_PHONE"];
					$aqcm_USER_RX_WARNING_EN[$i]	= $row["USER_RX_WARNING_EN"];
					
					if ($Flag_debug_A) {
						echo("i=" . $i . " ; " . $aqcm_USER_ID[$i] . " ; " . $aqcm_USER_PW[$i] . " ; " . $aqcm_USER_STATUS[$i] . " ; " . $aqcm_USER_LEVEL[$i] . " ; " . $aqcm_USER_T_MAX[$i] . " ; " . $aqcm_USER_WEEK_MAIL[$i] . "<br>" );
					}
					
					$i++ ;
				}
				
				$NB_users = $i ;

			}
			else {
				
				$get_users_of_one_site_erreur  = 3 ;
				$get_users_of_one_site_message = "résultat de la requête sur le site " . $p_aqcm_SITE_ID . " : vide !!!" ;
			}


		}
		else {
			
			$get_users_of_one_site_erreur  = 4 ;
			$get_users_of_one_site_message = "requête sur le site " . $p_aqcm_SITE_ID . " : échec !!!" ;

		}

	}
	
	//--------------------------------------------------------------------------------------------------------
	// clôture
	//--------------------------------------------------------------------------------------------------------
	if ($Flag_debug_A) {
		echo "NB_users = " . $NB_users . "<br>";
		echo( $get_users_of_one_site_erreur . " ; " . $get_users_of_one_site_message . "<br>") ;
	}
	
	if ( $link_AQCM05_OK ) {
		mysqli_close($link_AQCM05);
	}

	//--------------------------------------------------------------------------------------------------------
	// retour $NB_users, $get_users_of_one_site_erreur, $get_users_of_one_site_message
	//$aqcm_USER_ID[$i]
	//$aqcm_USER_PW[$i]	
	//$aqcm_USER_STATUS[$i]
	//$aqcm_USER_LEVEL[$i]
	//$aqcm_USER_T_MAX[$i]
	//$aqcm_USER_T_MAX_dmy
	//$aqcm_USER_WEEK_MAIL[$i]
	//$aqcm_USER_2FA_STATUS[$i]
	//$aqcm_USER_2FA_PHONE[$i]
	//$aqcm_USER_RX_WARNING_EN[$i]
	//--------------------------------------------------------------------------------------------------------

?>
