<?php

	//---------------------------------------------------------------------------------------
	require '../OVH_API/vendor/autoload.php';
	use \Ovh\Api;

	//---------------------------------------------------------------------------------------
	$FLAG_DEBUG = true;
	$FLAG_DEBUG = false;
	
	//---------------------------------------------------------------------------------------
	// routine de création des mots de passe
	//---------------------------------------------------------------------------------------
	function CarAleatoire($taille) {
		  // Liste des caractères possibles
		  $cars="1234567890";
		  $mdp='';
		  $long=strlen($cars);

		  srand((double)microtime()*1000000); //Initialise le générateur de nombres aléatoires

		  for($i=0;$i<$taille;$i++) $mdp=$mdp.substr($cars,rand(0,$long-1),1);

		  return $mdp;
	}

	//---------------------------------------------------------------------------------------
	$code_2fa_6_digits = CarAleatoire(6) ;

	$_SESSION['code_2FA']			= $code_2fa_6_digits ;
	$_SESSION['code_2FA_hashed']	= password_hash( $code_2fa_6_digits, PASSWORD_DEFAULT ) ;

	if ( $FLAG_DEBUG ) {
		$_SESSION['code_2FA_t_limit']	= time() + 20;
	} else {
		$_SESSION['code_2FA_t_limit']	= time() + 5*60;
	}

	$SMS_text		= "AQCM05 : " . $code_2fa_6_digits . "\n code valable pendant 5mn !" ;
	$admin_phone_number = $USER_2FA_PHONE ;
	
	//---------------------------------------------------------------------------------------
	if ( ! ( preg_match("/^\+{1}?[1-9][0-9]{10}$/", $admin_phone_number ) ) ) {
		$_SESSION['2FA_code_erreur'] = 11 ;
		$_SESSION['code_2FA_msg'] = "le numéro de téléphone n'est pas correct !!!<br> (" . $admin_phone_number . ") !!!";	
	}
	
	$masked_admin_phone_number = substr($admin_phone_number, 0, 4) . "..." . substr($admin_phone_number, -2)  ;

	//---------------------------------------------------------------------------------------
	if ( $_SESSION['2FA_code_erreur'] == 20 ) {
		
		if ( ! $FLAG_DEBUG ) {
			
			include("../M6_6/AQCM_get_SMS_parameters.php") ;
			
			if ( $get_SMS_code_erreur == 0 ) {
				
				$end_point 			= $aqcm_SMS_end_point ;
				$application_key 	= $aqcm_SMS_application_key ;
				$application_secret	= $aqcm_SMS_application_secret ;
				$consumer_key			= $aqcm_SMS_consumer_key ;

				$conn = new Api(	
									$application_key,
									$application_secret,
									$end_point,
									$consumer_key
								  );

				$smsServices = $conn -> get('/sms');
				
				$content = (object) array(
					"charset"				=> "UTF-8",
					"class"				=> "phoneDisplay",
					"coding"				=> "7bit",
					"message"				=> $SMS_text,
					"noStopClause"		=> false,
					"priority"			=> "high",
					"receivers"			=> [ $admin_phone_number ],
					"senderForResponse"	=> true,
					"validityPeriod"		=> 2880
				);
				
				$resultPostJob = $conn -> post('/sms/'. $smsServices[0] . '/jobs', $content);

				$resultPostJob_string = print_r($resultPostJob, true);
				$resultPostJob_string = preg_replace('/\s+/', '', $resultPostJob_string);

				if ( strpos($resultPostJob_string, "[totalCreditsRemoved]=>1") !== false ) {
					
					$_SESSION['2FA_code_erreur'] = 21 ;
					$_SESSION['code_2FA_msg'] = "<font color='Lime'>envoi du SMS 2FA (" . $masked_admin_phone_number . ") : succès...</font>";	
					
				}
				else {
					
					$_SESSION['2FA_code_erreur'] = 12 ;
					$_SESSION['code_2FA_msg'] = "envoi du SMS 2FA : échec !!!";	
					
				}
				
			}
			else {
				
				$_SESSION['2FA_code_erreur'] = 13 ;
				$_SESSION['code_2FA_msg'] = "lecture des paramètres SMS : échec !!!";	
				
			}


		}
		else {

			$_SESSION['2FA_code_erreur'] = 14 ;
			$_SESSION['code_2FA_msg'] = "l'envoi des SMS est en mode DEBUG...";
			
		}

	}
		
	//---------------------------------------------------------------------------------------

?>

