<?php

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

	$SMS_code_erreur = 0 ;

	//---------------------------------------------------------------------------------------
	if ( ! ( preg_match("/^\+{1}?[1-9][0-9]{10}$/", $admin_phone_number ) ) ) {

	}
	else {
		
		//---------------------------------------------------------------------------------------
		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 ) {
					$SMS_code_msg = "<font color='Lime'>envoi du SMS WARNING : succès...</font>";	
				}
				else {
					$SMS_code_erreur = 12 ;
					$SMS_code_msg = "envoi du SMS WARNING : é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" ) ;
					$SMS_code_erreur = 21 ;
				} 
				else {
					if ( $response[0] == "O" and $response[1] == "K") {
						$SMS_code_msg = "<font color='Lime'>envoi du SMS WARNING : succès...</font>";	
					} 
					else {
						$SMS_code_erreur = 22 ;
						$SMS_code_msg = "envoi du SMS WARNING : é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" ) ;
					$SMS_code_erreur = 31 ;
				} 
				else {
					if ( $response[0] == "O" and $response[1] == "K") {
						$SMS_code_msg = "<font color='Lime'>envoi du SMS WARNING : succès...</font>";	
					} 
					else {
						$SMS_code_erreur = 32 ;
						$SMS_code_msg = "envoi du SMS WARNING : échec !!!";	
					}
				}
			}
			else {
				$SMS_code_erreur = 2 ;
				$SMS_code_msg = "le service SMS déclaré est inconnu !!!";	
			}
		}
		else {
			$SMS_code_erreur = 1 ;
			$SMS_code_msg = "lecture des paramètres SMS : échec !!!";	
		}
	}

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

?>

