class MobytTNX{
var $from = 'TNX';
var $user = 'C09942_001';
var $pass = 'pu2263kh';//per accedere al pannello: 28hayx22
var $quality = "MEDIUM";
//se non sono settate le riempie con $controller->application->admin_email:
// var $cc_email = '';
// var $from_email = '';
/*
http://app.mobyt.it/
Login : C09942_001
Password per areariservata : 28hayx
Password dispositiva : pu2263kh
*/
var $email_tnx = "tnxstaff@gmail.com";//per copia bcc
var $cc_email = "";//per copia
var $charset = "UTF-8";
var $demo; //impostare a true per fare le prove (non vengono inviati sms)
var $BASEURL = "https://app.esendex.it/API/v1.0/REST/";
var $MESSAGE_HIGH_QUALITY = "N";
var $MESSAGE_MEDIUM_QUALITY = "L";
var $MESSAGE_LOW_QUALITY = "LL";
function login(){
if(!$this->auth){
$ch = curl_init();
$url = $this->BASEURL.'login?username='.$this->user.'&password='.$this->pass;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if ($info['http_code'] != 200) {
trigger_error("Mobyt login error: $url - ".print_r($info, true));
return false;
}
$this->auth = explode(";", $response);
}
return true;
}
function controllaCredito(){
if(!$this->login()) return false;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://app.mobyt.it/API/v1.0/REST/status?getMoney=true&typeAliases=true');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-type: application/json',
'user_key: ' . $this->auth[0],
'Session_key: ' . $this->auth[1]
// When using Access Token authentication, use this instead:
// 'Access_token: UserParam{access_token}'
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if ($info['http_code'] == 200) {
$obj = json_decode($response);
$q = $this->getQuality();
foreach($obj->sms as $values) if($values->type == $q) return $values->quantity;
}
else return false;
}
function getQuality(){
return $this->{"MESSAGE_".$this->quality."_QUALITY"};
}
function invia_sms($numero, $testo){//passare sempre il testo nello stesso encoding dell'applicazione
if(!$this->login()) return false;
$testo = mb_convert_encoding($testo, 'UTF-8', $this->charset);
$testo = str_replace(array("\r\n", "\n\r"), "\n", $testo);//il browser inserisce doppio carattere di a capo!
$testo_no_entity = html_entity_decode(strip_tags($testo), null, 'UTF-8');
if($testo != $testo_no_entity){
$this->errorMessage(
"Invio SMS con tag html",
"I tag sono stati automaticamente tolti per questo invio.".
"
testo: ".htmlentities($testo, null, $this->charset).
"
testo_no_entity: ".htmlentities($testo_no_entity, null, $this->charset)
);
$testo = $testo_no_entity;
}
//controllo mittente
if(!preg_match("/^[\+\d]*$/", $this->from) && strlen($this->from) > 11) $this->infoMessage("Il mittente degli SMS è impostato è troppo lungo. Verranno usati i primi 11 caratteri. (Usare un nome più corto o un numero di telefono)");
//controllo numero
$numero = preg_replace("/[^\+\d]/", "", $numero);
$numero_orig = $numero;
if(substr($numero, 0, 2) == "00") $numero = "+".substr($numero, 2);
if(substr($numero, 0, 1) != "+") $numero = "+39".$numero;
// if($numero_orig !== $numero) $this->infoMessage("NOTA: numero \"$numero_orig\" corretto in \"$numero\"");
if(substr($numero, 0, 4) != '+393'){
$this->infoMessage(sprintf('NOTA: invio a "%s" ANNULLATO (non è un numero di cellulare valido)', $numero_orig));
return false;
}
// $testo = str_replace(array("\n", "\r"), " ", $testo);
if(strlen($testo) > 160){
$testo = substr($testo, 0, 160);
$this->infoMessage('Per evitare non bloccare l\'invio il testo dell\'SMS è stato tagliato a 160 caratteri: "'.$testo.'"');
}
$testoOriginalEncoding = mb_convert_encoding($testo, $this->charset, 'UTF-8');
if(!$this->demo){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->BASEURL . 'sms');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-type: application/json',
'user_key: ' . $this->auth[0],
'Session_key: ' . $this->auth[1]
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array(
"message" => $testo,
"message_type" => $this->getQuality(),
// "returnCredits" => true,//Returns the number of credits used instead of the number of messages
"recipient" => array($numero),
"sender" => $this->from, // Place here a custom sender if desired
// "scheduled_delivery_time" => date('YmdHi', strtotime("+5 minutes")), // postpone by 5 minutes
)));
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
$result = json_decode($response);
if($info['http_code'] == 201 && $result->result == "OK"){
$this->sms_ok($numero, $testoOriginalEncoding);
return true;
}
else{
$this->sms_ko($numero, $testoOriginalEncoding, $result->result." ".$result->error_message);
return false;
}
}
else{
$this->sms_ok($numero, $testoOriginalEncoding, true);
return true;
}
}
function sms_ok($numero, $testo, $is_demo = false){
$esito = $this->get_ok_subject($numero, $testo);
if($is_demo) $esito = "[SMS DEMO] ".$esito;
$this->infoMessage($esito);
$this->mailMessage($esito, $this->get_ok_body($numero, $testo));
}
function sms_ko($numero, $testo, $result){
$subject = $this->get_ko_subject($numero, $testo, $result);
$this->errorMessage($subject);
$this->infoMessage($subject);
$this->mailMessage($subject, $this->get_ko_body($numero, $testo, $errore));
}
//DA PERSONALIZZARE
function mailMessage($subject, $body){//dopo qualsiasi invio
$from = "From: $this->from_email\r\nReply-To: $this->from_email";
if(!$this->demo) mail($this->email_tnx, $subject, $body, $from);
mail($this->cc_email, $subject, $body);
}
function errorMessage($msg){//dopo errore (invio a from_email invece che a cc_email)
trigger_error($msg);
$from = "From: $this->from_email\r\nReply-To: $this->from_email";
if(!$this->demo) mail($this->email_tnx, $msg, $msg, $from);
mail($this->from_email, $msg, $msg, $from);
}
function infoMessage($msg){//non mail
}
function get_ok_subject($numero, $testo){
return sprintf("SMS inviato a %s", $numero);
}
function get_ok_body($numero, $testo){
return $testo;
}
function get_ko_subject($numero, $testo, $errore){
return sprintf("Errore invio SMS a %s", $numero).": ".$errore;
}
function get_ko_body($numero, $testo, $errore){
return $testo;
}
}
?>