@ -14,8 +14,10 @@ class SendSMSclass
//--------------------------------------
var $host;
var $XMLgsmnumbers;
var $xmldata;
var $token;
var $optional_headers;
var $fomatedGSM = array();
var $formatedMessage = array();
var $request_data;
var $response;
@ -26,39 +28,61 @@ class SendSMSclass
$this->username = $username;
$this->password = $password;
$this->sender = $options['sender'];
$this->token = $options['token'] ? TRUE : FALSE;
if ($options['type_sms'] == 'simulate'){
$dir = '/send/simulate'; // l'API accueillera la demande mais le SMS ne sera pas envoyé
} else {
$dir = '/send';
}
// definir un header pour accueillir le json et, si besoin, donner le token
$headers = array('Content-Type: application/json', 'Accept: application/json');
if ($options['token']){
$headers = array_merge($headers,["Authorization: Bearer " . $options['token']]);
}
$this->optional_headers = $headers;
$this->message = $message;
$this->inputgsmnumbers = $inputgsmnumbers;
$this->host = "https://api.smsfactor.com" . $dir;
$this->formatGSM();
$this->formatMessage();
$this->convertGSMnumberstoXML();
$this->prepareXMLdata( );
$this->response = $this->do_post_request($this->host,$this->request_data);
// un petit log avec les informations essentielles pour le debug
spip_log('host = ' . $this->host . ' request_data = ' . $this->request_data . ' headers = ' . print_r($this->optional_headers,true), 'sms_error.' . _LOG_DEBUG );
// on envoie le SMS
$this->response = $this->do_post_request($this->host,$this->request_data,$this->optional_headers );
return $this->response;
}
function convertGSMnumberstoXML ()
function formatGSM ()
{
$gsmcount = count($this->inputgsmnumbers); #counts gsm numbers
for ( $i = 0; $i < $gsmcount; $i++ )
{
$this->XMLgsmnumbers .= "< gsm > " . $this->inputgsmnumbers[$i] . "< / gsm > ";
foreach ($this->inputgsmnumbers as $cle => $n) {
$fomatedGSM[] = array('value' => $n);
}
$this->fomatedGSM = $fomatedGSM;
}
function prepareXMLdata ()
function formatMessage ()
{
$this->xmldata = "< sms > < authentification > < username > " . $this->username . "< / username > < password > " . $this->password . "< / password > < / authentification > < message > < sender > " . $this->sender . "< / sender > " . $this->texte_avec_liens_courts() . "< / message > < recipients > " . $this->XMLgsmnumbers . "< / recipients > < / sms > ";
$this->request_data = 'XML=' . $this->xmldata;
$formated = [
'sms' => [
'message' =>
array_merge( ['sender' => $this->sender], $this->texte_avec_liens_courts() ),
'recipients' => [
'gsm' => $this->fomatedGSM
]
]
];
// s'il n'y a pas de token, on transmet le login et mot de passe
if ($this->token == FALSE){
$formated['sms']['authentification'] = [
'username' => $this->username,
'password' => $this->password
];
}
$this->formatedMessage = $formated;
$this->request_data = json_encode($this->formatedMessage);
}
function do_post_request($url, $postdata, $optional_headers = null)
{
$ch = curl_init();
@ -66,37 +90,40 @@ class SendSMSclass
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_HTTPHEADER, $optional_headers);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
/**
* Méthode privée pour permettre l'usage de liens courts
*
* Il faut identifier les liens dans le texte et les substituer par le tag prévu ```< -short- > ```,
* puis indiquer à l'API les liens devant être raccourcis ```< links > lien< / links > ```,
* que l'API calculera et placera à la place du tag ```< -short- > ```.
* puis indiquer à l'API les liens devant être raccourcis par un tableau links.
* L'API les calculera et les placera à la place du tag ```< -short- > ```.
*
* @link https://dev.smsfactor.com/fr/api/sms/envoi/liens-courts
*
* @param string $ message
* Le texte du SMS
* @return string $message
* Un nouveau formatage du texte et l'ajout de balises si nécessaire
* @uses string $this-> message
* Le texte du SMS
* @return array $message
* Un nouveau formatage du texte en tableau 'text' s'il y a des liens 'links'
*/
private function texte_avec_liens_courts() {
// Masque
$regex = '@([http|ftp|https]+://[a-z0-9?=:&\./+,%#_-]+)@i';
preg_match_all($regex,$this->message,$trouvaille,PREG_SET_ORDER);
$links = '';
$links = array();
$text = $this->message;
preg_match_all($regex,$text,$trouvaille,PREG_SET_ORDER);
if ($trouvaille){
foreach ($trouvaille as $key => $value) {
$links . = "< links > $value[0]< / links > " ;
$reformatage = str_replace($value[0], '< -short- > ', $this->message );
$links[] = $value[0];
$text = str_replace($value[0], '< -short- > ', $text );
}
return "< text > <![CDATA[${reformatage}]]> < / text > " . $links;
return ['text' => $text, 'links' => $links];
}
return "< text > " . $this->message . "< / text > " ;
return ['text' => $text] ;
}
}