-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
215 lines (164 loc) · 5.59 KB
/
index.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
##Classe de Token de sua Preferencia
include_once('../token/Token.php');
function debug(){
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
}
// debug();
function is_base64($s) {
return (bool) preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $s);
}
require 'config.php';
require 'PHPMailer-master/src/Exception.php';
require 'PHPMailer-master/src/PHPMailer.php';
require 'PHPMailer-master/src/SMTP.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
header("Content-Type: application/json");
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Headers: *');
$body = file_get_contents('php://input');
try {
switch($_SERVER['REQUEST_METHOD']){
case 'POST':
$body = json_decode($body);
if(!isset($body->token)){
$httpCode = 401;
$return = array("status" => 401, "message" => "Faltando Token!");
// var_dump($body);
}else{
$TokenVip = new Token($body->token, 1);
$auth = $TokenVip->authToken();
if($auth['status'] != 200){
$httpCode = 401;
$return = array("status" => 401, "message" => "Token Invalido!");
}else{
######################### V A L I D A R Q U A T I D A D E #################
$centroCusto = $TokenVip->verificaCentroCusto();
if($centroCusto['status'] == 200){
$free = true;
}else{
if($centroCusto['free'] > 0){
$free = true;
}else{
$free = false;
}
}
if($free){
########################
if(!isset($body->destino)){
$httpCode = 200;
$return = array("status" => 204, "message" => "Faltando destino");
}else if(!isset($body->assunto)){
$httpCode = 200;
$return = array("status" => 204, "message" => "Faltando assunto");
}else if(!isset($body->mensagem)){
$httpCode = 200;
$return = array("status" => 204, "message" => "Faltando mensagem");
}
$mail = new PHPMailer();
if(!filter_var($body->destino, FILTER_VALIDATE_EMAIL)){
### EMAIL INVALIDO
$httpCode = 200;
$return = array("status" => 204, "message" => "Destino invalido");
} else {
$anexos = true;
$anexos_array = true;
if(isset($body->anexo)){
if(is_array($body->anexo)){
$tt = count($body->anexo);
for($i = 0; $i < $tt ; $i++){
if( (!is_base64($body->anexo[$i]->content)) || (empty($body->anexo[$i]->content)) )
$anexos = false;
}
}else{
$anexos_array = false;
}
}
if(!$anexos){
$httpCode = 200;
$return = array("status" => 204, "message" => "contem anexo que não está em base64");
}else if(!$anexos_array){
$httpCode = 200;
$return = array("status" => 204, "message" => "Formato do anexo invalido");
}else{
$mail->AddAddress($body->destino);
$mail->IsSMTP();
$mail->Host = CONF_MAIL_HOST;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Port = CONF_MAIL_PORT;
$mail->Username = CONF_MAIL_USERNAME;
$mail->Password = CONF_MAIL_PASSWD;
$mail->From = CONF_MAIL_USERNAME;
$mail->FromName = CONF_MAIL_NAME;
$mail->IsHTML(true);
$mail->CharSet = "UTF-8";
$erro_array = false;
##### anexo
if(isset($body->anexo)){
if(!is_array($body->anexo)){
$erro_array = true;
}else{
$tt = count($body->anexo);
for($i = 0; $i < $tt ; $i++){
#### CRIA ARQUIVOS TMP PARA ANEXO
$temp[$i] = tmpfile();
fwrite($temp[$i], base64_decode($body->anexo[$i]->content));
fseek($temp[$i], 0);
$path = stream_get_meta_data($temp[$i])['uri'];
################################
$mail->AddAttachment($path, $body->anexo[$i]->nome);
}
}
}
#####
if($erro_array){
$httpCode = 200;
$return = array("status" => 204, "message" => "Anexo não está em array");
}else{
$mail->Subject = $body->assunto;
$mail->Body = $body->mensagem;
$mail->AltBody = trim(strip_tags($body->mensagem));
if ($mail->Send()) {
$httpCode = 200;
$return = array("status" => 200, "message" => "Email enviado com sucesso!");
$TokenVip->storeRecord("Email enviado para: ".$body->destino);
} else {
$httpCode = 200;
$TokenVip->storeRecord("Erro ao enviar email: ".$mail->ErrorInfo);
$return = array("status" => 204, "message" => "Ocorreu um erro ao enviar email!");
}
##### DELETA ARQUIVOS TEMPORARIOS
if(isset($body->anexo)){
if(is_array($body->anexo)){
$tt = count($body->anexo);
for($i = 0; $i < $tt ; $i++){
fclose($temp[$i]);
}
}
}
#########################################
}
}
}
}else{
$httpCode = 200;
$return = array("status" => 208, "message" => "Limite do mês atingido!");
}
####################
}
}
break;
default:
$httpCode = 200;
$return = array("status" => 204, "message" => "Método ".$_SERVER['REQUEST_METHOD']." invalido.");
break;
}
print_r(json_encode($return));
http_response_code($httpCode);
} catch (Exception $e) {
http_response_code(500);
}