<?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;
	}

	//---------------------------------------------------------------------------------------
	$_SESSION['lost_code_2FA']			= CarAleatoire(6) ;
	$_SESSION['lost_code_2FA_hashed']	= password_hash( $_SESSION['lost_code_2FA'], PASSWORD_DEFAULT ) ;

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

	//---------------------------------------------------------------------------------------
	$sms_text				= "AQCM05 : " . $_SESSION['lost_code_2FA'] . "\n code valable pendant 5mn !" ;
	$admin_phone_number	= $_SESSION['lost_password_2FA_phone'] ;
	
	//---------------------------------------------------------------------------------------
	if ( ! ( preg_match("/^\+{1}?[1-9][0-9]{10}$/", $admin_phone_number ) ) ) {
		$_SESSION['lost_2FA_code_erreur'] = 11 ;
		$_SESSION['lost_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['lost_2FA_code_erreur'] == 20 ) {
		
		if ( ! $FLAG_DEBUG ) {
			
			include("../_identifiants/sql_identification_main.php") ;
			include("../M6_6/AQCM_get_SMS_parameters.php") ;
			
			if ( $get_SMS_code_erreur == 0 ) {

				//------------------------------------------------------------------------------
				if ( $aqcm_SMS_SERVICE == "OVH" ) {
					
					//------------------------------------------------------------------------------
					$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['lost_2FA_code_erreur'] = 21 ;
						$_SESSION['lost_code_2FA_msg'] = "<font color='Lime'>envoi du SMS 2FA (" . $masked_admin_phone_number . ") : succès...</font>";	
					}
					else {
						$_SESSION['lost_2FA_code_erreur'] = 12 ;
						$_SESSION['lost_code_2FA_msg'] = "envoi du SMS 2FA : échec !!!";	
					}

					//------------------------------------------------------------------------------
					
				}
				elseif ( $aqcm_SMS_SERVICE == "E3531" ) {
					
					//------------------------------------------------------------------------------
					$POST_DATA = array(
						'SMS_PASSWORD'	=> base64_encode(password_hash($aqcm_SMS_E3531_MAC, PASSWORD_DEFAULT)),
						'SMS_PHONE'  		=> base64_encode($admin_phone_number),
						'SMS_TEXT'		=> base64_encode($sms_text)
					);

					$curl = curl_init();
					curl_setopt($curl, CURLOPT_URL, $aqcm_SMS_E3531_URL );
					curl_setopt($curl, CURLOPT_TIMEOUT, 30);
					curl_setopt($curl, CURLOPT_POST, 1);
					curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
					curl_setopt($curl, CURLOPT_POSTFIELDS, $POST_DATA);

					$response = curl_exec($curl);

					if ( curl_errno($curl) ) {
						echo ("NOK : CURL -> requête -> échec !!!" . "</br>\r\n") ;
						echo ( curl_error($curl) . "</br>\r\n" ) ;
					} 
					else {
						if ( $response[0] == "O" and $response[1] == "K") {
							$_SESSION['lost_2FA_code_erreur'] = 21 ;
							$_SESSION['lost_code_2FA_msg'] = "<font color='Lime'>envoi du SMS 2FA (" . $masked_admin_phone_number . ") : succès...</font>";	
						} 
						else {
							$_SESSION['lost_2FA_code_erreur'] = 12 ;
							$_SESSION['lost_code_2FA_msg'] = "envoi du SMS 2FA : échec !!!";	
						}
					}

					//------------------------------------------------------------------------------
					
				}
				elseif ( $aqcm_SMS_SERVICE == "B525" ) {
					
					//------------------------------------------------------------------------------
					$POST_DATA = array(
						'SMS_PASSWORD'	=> base64_encode(password_hash($aqcm_SMS_B525_MAC, PASSWORD_DEFAULT)),
						'SMS_PHONE'  		=> base64_encode($admin_phone_number),
						'SMS_TEXT'		=> base64_encode($sms_text)
					);

					$curl = curl_init();
					curl_setopt($curl, CURLOPT_URL, $aqcm_SMS_B525_URL );
					curl_setopt($curl, CURLOPT_TIMEOUT, 30);
					curl_setopt($curl, CURLOPT_POST, 1);
					curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
					curl_setopt($curl, CURLOPT_POSTFIELDS, $POST_DATA);

					$response = curl_exec($curl);

					if ( curl_errno($curl) ) {
						echo ("NOK : CURL -> requête -> échec !!!" . "</br>\r\n") ;
						echo ( curl_error($curl) . "</br>\r\n" ) ;
					} 
					else {
						if ( $response[0] == "O" and $response[1] == "K") {
							$_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['lost_2FA_code_erreur'] = 15 ;
					$_SESSION['lost_code_2FA_msg'] = "le service SMS déclaré est inconnu !!!";	
				}
			}
			else {
				$_SESSION['lost_2FA_code_erreur'] = 13 ;
				$_SESSION['lost_code_2FA_msg'] = "lecture des paramètres SMS : échec !!!";	
			}
		}
		else {
			$_SESSION['lost_2FA_code_erreur'] = 14 ;
			$_SESSION['lost_code_2FA_msg'] = "l'envoi des SMS est en mode DEBUG...";
		}
	}
		
	//---------------------------------------------------------------------------------------

?>

