<?php
		
	//--------------------------------------------------------------------------------------------------------
	// initialisation des variables
	//--------------------------------------------------------------------------------------------------------
	$code_erreur = 0;
	$MySQL_connected = false;
	
	$Flag_debug_1 = 1 ;
	$Flag_debug_1 = 0 ;
	
	//--------------------------------------------------------------------------------------------------------
	// récupération des variables
	//--------------------------------------------------------------------------------------------------------
	$p_TARGET_ADMIN_ID		= $_SESSION['CONNECTED_USER_ID'];
	
	$p_TARGET_ADMIN_STATUS	= isset($_POST["TARGET_ADMIN_STATUS"])	? $_POST["TARGET_ADMIN_STATUS"] : $_SESSION['CONNECTED_USER_STATUS'];
	$p_TARGET_ADMIN_LEVEL	= isset($_POST["TARGET_ADMIN_LEVEL"])		? $_POST["TARGET_ADMIN_LEVEL"] : $_SESSION['CONNECTED_USER_LEVEL'];
	$p_TARGET_ADMIN_T_MAX	= isset($_POST["TARGET_ADMIN_T_MAX"])		? $_POST["TARGET_ADMIN_T_MAX"] : $_SESSION['CONNECTED_USER_T_MAX'];
	
	$p_TARGET_ADMIN_2FA_PHONE		= isset($_POST["TARGET_ADMIN_2FA_PHONE"])	? $_POST["TARGET_ADMIN_2FA_PHONE"] : $_SESSION['CONNECTED_USER_2FA_PHONE'];
	$p_TARGET_ADMIN_2FA_STATUS	= isset($_POST["TARGET_ADMIN_2FA_STATUS"])	? $_POST["TARGET_ADMIN_2FA_STATUS"] : $_SESSION['CONNECTED_USER_2FA_STATUS'];
	$p_TARGET_ADMIN_2FAS_SECRET	= isset($_POST["TARGET_ADMIN_2FAS_SECRET"])	? $_POST["TARGET_ADMIN_2FAS_SECRET"] : $_SESSION['CONNECTED_USER_2FAS_SECRET'];
	$p_TARGET_ADMIN_2FAS_STATUS	= isset($_POST["TARGET_ADMIN_2FAS_STATUS"])	? $_POST["TARGET_ADMIN_2FAS_STATUS"] : $_SESSION['CONNECTED_USER_2FAS_STATUS'];
	$p_T_MAX_value		= strtotime($p_TARGET_ADMIN_T_MAX);

	echo("identifiant : $p_TARGET_ADMIN_ID");echo("</br>");
	//echo("mot de passe : $p_USER_PW");echo("</br>");
	echo("n° téléphone : $p_TARGET_ADMIN_2FA_PHONE");echo("&nbsp;(pour 2FA SMS)</br>");
	echo("secret : $p_TARGET_ADMIN_2FAS_SECRET");echo("&nbsp;(pour 2FAS TOTP)</br></br>");
	echo("niveau admin : $p_TARGET_ADMIN_LEVEL");echo("</br>");
	echo("2FA status : $p_TARGET_ADMIN_2FA_STATUS");echo("</br>");
	echo("2FAS status : $p_TARGET_ADMIN_2FAS_STATUS");echo("</br>");

	echo("T_MAX : $p_TARGET_ADMIN_T_MAX");echo("</br>");
	echo("T_MAX : $p_T_MAX_value");echo("</br></br>");


	if ( $p_TARGET_ADMIN_ID == "none" ) {
		echo( "<font color='Salmon'>" . "choisir un utilisateur à modifier !" . "</font>" . "<br>") ;
		$code_erreur = 1 ;
	}
	
	if ( ! preg_match("/^\+33[1-9][0-9]{8}$/", $p_TARGET_ADMIN_2FA_PHONE ) ) {
			echo('<font color="red">');
			echo("le n° de téléphone est incorrect ! <br>") ;
			echo('</font>');
			$code_erreur = 4 ;
	}


	//--------------------------------------------------------------------------------------------------------
	// connexion à la base de données
	//--------------------------------------------------------------------------------------------------------
	if ( $code_erreur == 0 ) {
		
		include ("../_identifiants/sql_identification_main.php");

		$link_AQCM05 = mysqli_connect($mysql_host,$mysql_user,$mysql_password, $mysql_base);
		
		if ($link_AQCM05) {

			$MySQL_connected = true ;
			echo( "- connexion à la base de donnée MySQL : " . "<font color='Lime'>" . "succès..." . "</br></br></font>" ) ;
			
		}
		else {
			
			$code_erreur = 2 ;
			echo( "- connexion à la base de donnée MySQL : " . "<font color='Salmon'>" . "échec !!!" . "</br></br></font>" ) ;
			
		}

	}

	//--------------------------------------------------------------------------------------------------------
	// modification de l'administrateur $p_TARGET_ADMIN_ID
	//--------------------------------------------------------------------------------------------------------
	//$code_erreur = 100;
	if ( $code_erreur == 0 ) {
		
		echo( "- modification : " ) ;

		$query = "
			UPDATE AQCM05_SUPER_USERS 
			SET 
				USER_STATUS='"			. $p_TARGET_ADMIN_STATUS 				. "', 
				USER_LEVEL='" 			. $p_TARGET_ADMIN_LEVEL 				. "', 
				USER_T_MAX='" 			. strtotime($p_TARGET_ADMIN_T_MAX) 	. "', 
				USER_2FA_STATUS='" 		. $p_TARGET_ADMIN_2FA_STATUS 			. "',
				USER_2FA_PHONE='" 		. $p_TARGET_ADMIN_2FA_PHONE			. "',
				USER_2FAS_STATUS='"		. $p_TARGET_ADMIN_2FAS_STATUS			. "',
				USER_2FAS_SECRET='"		. $p_TARGET_ADMIN_2FAS_SECRET 		. "' 
			WHERE ( USER_ID = '" 		. $p_TARGET_ADMIN_ID 					. "' )"  ;
			
		//echo( $query ); echo("<br>");
		
		
		$result = mysqli_query($link_AQCM05, $query); 
		
		if ( ! $result ) {
			
			echo( "<font color='Salmon'>" . "NOK !!!" . "</font>" . "<br>") ;
			$code_erreur = 3;
			
		} 
		else {
			
			/*
			$_SESSION['CONNECTED_USER_STATUS'] = $p_TARGET_ADMIN_STATUS;
			$_SESSION['CONNECTED_USER_LEVEL'] = $p_TARGET_ADMIN_LEVEL;
			$_SESSION['CONNECTED_USER_T_MAX'] = strtotime($p_TARGET_ADMIN_T_MAX);
			$_SESSION['CONNECTED_USER_2FA_STATUS'] = $p_TARGET_ADMIN_2FA_STATUS;
			$_SESSION['CONNECTED_USER_2FA_PHONE'] = $p_TARGET_ADMIN_2FA_PHONE;
			$_SESSION['CONNECTED_USER_2FAS_STATUS'] = $p_TARGET_ADMIN_2FAS_STATUS;
			$_SESSION['CONNECTED_USER_2FAS_SECRET'] = $p_TARGET_ADMIN_2FAS_SECRET;
			*/
			echo( "<font color='Lime'>" . "OK..." . "</font>" . "<br>") ;
			
		}
		
	}
		
	//--------------------------------------------------------------------------------------------------------
	// 
	//--------------------------------------------------------------------------------------------------------
	if ( $MySQL_connected ) {
		mysqli_close($link_AQCM05);
	}

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

?>
