class AimonTNX{
var $from = '+393346243294';
var $login = "tnx@tnx.sms";
var $password = "nNczquZO";
var $idApi = "3498";//3498 pro rivendita tnx - 106 smart - 84 pro (personalizzazione mittente)
var $prezzoSms = 0.06;//per calcolo sms residui, si imposta su https://sms.aimon.it/rivenditore/
function controllaCredito(){
$buffer = array(
"authlogin" => $this->login,
"authpasswd" => $this->password,
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://secure.apisms.it/http/get_credit");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $buffer);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$ret = curl_exec($ch);
curl_close($ch);
if($ret != (float)$ret) trigger_error("Errore controllo credito: ".$ret);
// return floor($ret / ($this->idApi == 84 ? 1.6 : 0.7501));//questi sono i prezzi standard senza rivendita
return floor($ret / $this->prezzoSms);
}
function invia_sms($numero, $testo){//passare sempre il testo nello stesso encoding dell'applicazione
//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 = substr($numero, 1);
if(substr($numero, 0, 4) != '393' && strlen($numero) < 11){
$message = 'E\' stato aggiunto il prefisso internazionale italiano al numero '.$numero;
$this->infoMessage($message);
// trigger_error($message);//da togliere dopo un paio di verifiche - una fatta - due fatte
$numero = '39'.$numero;
}
$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;
}
if(!$this->demo){
$buffer = array(
"authlogin" => $this->login,
"authpasswd" => $this->password,
"sender" => base64_encode(mb_convert_encoding($this->from, 'ISO-8859-1', $this->charset)),
"body" => base64_encode(mb_convert_encoding($testo, 'ISO-8859-1', $this->charset)),
"destination" => $numero,
"id_api" => $this->idApi
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://secure.apisms.it/http/send_sms");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $buffer);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$ret = curl_exec($ch);
curl_close($ch);
if(strpos($ret, "+01 SMS Queued") === 0){
$this->sms_ok($numero, $testo);
return true;
}
else{
if(stristr($ret, "access denied")) $ret .= " (controlla data scadenza credito su http://sms.aimon.it/rivenditore/index.php)";
$this->sms_ko($numero, $testo, $ret);
return false;
}
}
else{
$this->sms_ok($numero, $testo, 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;
}
}
?>