/
recebe-sms.php
151 lines (137 loc) · 5.09 KB
/
recebe-sms.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
include_once "classes/conexao.php";
include_once "classes/funcionalidades.php";
$conn = new conn();
function token() {
$tamanho = 6;
$maiusculas = true;
$numeros = true;
$simbolos = false;
// Caracteres de cada tipo
$lmai = 'ABCDEFGHIJKLMNPQRSTUVWXYZ';
$num = '1234567890';
$simb = '!@#$%*-';
// Variaveis internas
$retorno = '';
$caracteres = '';
// Agrupamos todos os caracteres que poderão ser utilizados
if ($maiusculas) {
$caracteres .= $lmai;
}
if ($numeros) {
$caracteres .= $num;
}
if ($simbolos) {
$caracteres .= $simb;
}
// Calculamos o total de caracteres possíveis
$len = strlen($caracteres);
for ($n = 1; $n <= $tamanho; $n++) {
// Criamos um número aleatório de 1 até $len para pegar um dos caracteres
$rand = mt_rand(1, $len);
// Concatenamos um dos caracteres na variável $retorno
$retorno .= $caracteres[$rand - 1];
}
return $retorno;
}
function converteComandosTxt($txt, $campanha, $dtvalidade) {
//altera variaveis
$conn = new conn();
$codigo = token();
$conn->insert(array('dtCad' => date("Y-m-d"),
'campanha' => $campanha,
'codigo_cupom' => $codigo,
'dtvalidade' => $dtvalidade,
'usuario_acao' => $_SESSION["login"]["usuario"],
'status' => 0,
), "", "cupom");
$txt = str_replace("&CODE&", $codigo, $txt);
return $txt;
}
if ($_GET['receiver'] && $_GET['sender'] && $_GET['when'] && $_GET['text']) {
$chave = $conn->read(array("idSms", "palavra_chave", "campanha"), "status = 1 AND qtdCupons > 0", "", "campanha_sms", "query");
while ($palavra_chave = $chave->fetch_assoc()) {
$campanha = $palavra_chave['idSms'];
$txtMaiusculo = strtoupper($_GET['text']);
$txtPMaiusculo = ucfirst($_GET['text']);
$txtMinusculo = strtolower($_GET['text']);
if ((preg_match('/^' . $palavra_chave['palavra_chave'] . '/', $txtMaiusculo, $matches, PREG_OFFSET_CAPTURE)) || (preg_match('/^' . $palavra_chave['palavra_chave'] . '/', $txtMinusculo, $matches, PREG_OFFSET_CAPTURE)) || (preg_match('/^' . $palavra_chave['palavra_chave'] . '/', $txtPMaiusculo, $matches, PREG_OFFSET_CAPTURE))) {
$dados = $conn->read(array("*"), "idSms = '" . $palavra_chave['idSms'] . "' AND status = 1", "", "campanha_sms", "fetch");
$celular = $conn->read(array("*"), "CelularCliente = '" . $_GET['sender'] . "' AND idCampanha = ".$palavra_chave['idSms']." AND status != 4", "msgFim_enviado ASC ", "sms", "fetch");
if (count($celular) != 0) {
$mensagem = $dados['mensagem_encerrado'];
$msgFim_enviado = 1;
} else {
$msgFim_enviado = 0;
$time_inicial = (date("Y-m-d"));
$time_final = ($dados['dt_limiteCupom']);
$diferenca = strtotime($time_final) - strtotime($time_inicial);
$dias = floor($diferenca / (60 * 60 * 24) * (-1));
$dtvalidade = date('Y-m-d', strtotime("+" . $dias . " days", strtotime(date("Y-m-d"))));
$conn->insert(array('dtCad' => date("Y-m-d"),
'idCampanha' => $palavra_chave['idSms'],
'CelularCliente' => $_GET['sender'],
'dtEnvio' => $_GET['when'],
'mensagem' => $_GET['text'],
'status' => 1,
), "", "sms");
$mensagem = converteComandosTxt($dados["mensagem"], $campanha, $dtvalidade);
}
echo $mensagem ;
//exit("dadafrg ".$msgFim_enviado);
if($msgFim_enviado < 1 ){
$usuario = 'montnet';
$senha = 'Qym56fMu';
$celulares = explode(";", $_POST['celular']);
$url = "http://api.infobip.com/api/v3/sendsms/xml";
$xmlString = "
<SMS>
<authentification>
<username>" . $usuario . "</username>
<password>" . $senha . "</password>
</authentification>
<message>
<sender>" . $dados["campanha"] . "</sender>
<text>" . $mensagem . "</text>
<flash></flash>
<type></type>
<wapurl></wapurl>
<binary></binary>
<datacoding></datacoding>
<esmclass></esmclass>
<srcton></srcton>
<srcnpi></srcnpi>
<destton></destton>
<destnpi></destnpi>
<ValidityPeriod></ValidityPeriod>
</message>
<recipients>
<gsm>" . $_GET['sender'] . "</gsm>
</recipients>
</SMS> ";
$fields = "XML=" . urlencode($xmlString);
// in this example, POST request was made using PHP's CURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
// response of the POST request
$response = curl_exec($ch);
curl_close($ch);
$cupons_qtd = $palavra_chave['qtdCupons'] - 1;
$dados = $conn->update(array("qtdCupons" => $cupons_qtd), "idSms = '" . $palavra_chave['idSms'] . "'", "", "campanha_sms");
$conn->insert(array('dtCad' => date("Y-m-d"),
'idCampanha' => $palavra_chave['idSms'],
'CelularCliente' => $_GET['sender'],
'dtEnvio' => $_GET['when'],
'mensagem' => $mensagem,
'status' => 3,
'msgFim_enviado' => $msgFim_enviado,
), "", "sms");
}
}
if (preg_match('/^SAIR/', $txt, $matches, PREG_OFFSET_CAPTURE) || preg_match('/^PARAR/', $txt, $matches, PREG_OFFSET_CAPTURE)) {
$dados = $conn->delete("CelularCliente = '" . $_GET['sender'] . "'", "", "sms");
}
}
}