File size: 5,909 Bytes
220b380
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php
// Autoload Composer dependencies
require_once __DIR__ . '/vendor/autoload.php';

// Load environment variables if .env exists
if (file_exists(__DIR__ . '/.env')) {
    $dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
    $dotenv->load();
}

// Generate CSRF token for the form (must be before any output)
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}
$_SESSION['csrf_token'] = bin2hex(random_bytes(32));

// PROCESSAMENTO DO ENVIO DE EMAIL
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    header('Content-Type: application/json');
    header('X-Content-Type-Options: nosniff');
    header('X-Frame-Options: DENY');
    header('X-XSS-Protection: 1; mode=block');

    try {
        // Rate limiting - prevent spam
        $rateLimitFile = __DIR__ . '/logs/rate_limit_' . md5($_SERVER['REMOTE_ADDR'] ?? 'unknown') . '.txt';
        $currentTime = time();

        if (file_exists($rateLimitFile)) {
            $lastRequest = (int)file_get_contents($rateLimitFile);
            if ($currentTime - $lastRequest < 60) { // 1 minute cooldown
                echo json_encode(['success' => false, 'error' => 'Aguarde um momento antes de enviar outra mensagem.']);
                exit;
            }
        }

        // Update rate limit
        file_put_contents($rateLimitFile, $currentTime);

        // Sanitize and validate input
        $nome = trim($_POST['nome'] ?? '');
        $email = trim($_POST['email'] ?? '');
        $empresa = trim($_POST['empresa'] ?? '(não informado)');
        $mensagem = trim($_POST['mensagem'] ?? '');

        // CSRF protection (basic)
        $csrfToken = $_POST['csrf_token'] ?? '';
        $sessionToken = $_SESSION['csrf_token'] ?? '';

        if (empty($csrfToken) || $csrfToken !== $sessionToken) {
            echo json_encode(['success' => false, 'error' => 'Erro de segurança. Recarregue a página e tente novamente.']);
            exit;
        }

        // Prepare data for email service
        $contactData = [
            'nome' => $nome,
            'email' => $email,
            'empresa' => $empresa,
            'mensagem' => $mensagem
        ];

        // Use professional email service
        $emailService = new \SoftEdge\EmailService();

        if ($emailService->sendContactEmail($contactData)) {
            echo json_encode(['success' => true]);
        } else {
            echo json_encode(['success' => false, 'error' => 'Erro ao enviar mensagem. Por favor, tente novamente ou entre em contato via WhatsApp.']);
        }

    } catch (\InvalidArgumentException $e) {
        echo json_encode(['success' => false, 'error' => $e->getMessage()]);
    } catch (\Exception $e) {
        error_log('Contact form error: ' . $e->getMessage());
        echo json_encode(['success' => false, 'error' => 'Erro interno do servidor. Nossa equipe foi notificada.']);
    }

    exit;
}
?>
=======
<?php
// Autoload Composer dependencies
require_once __DIR__ . '/vendor/autoload.php';

// Load environment variables if .env exists
if (file_exists(__DIR__ . '/.env')) {
    $dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
    $dotenv->load();
}

// PROCESSAMENTO DO ENVIO DE EMAIL
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    header('Content-Type: application/json');
    header('X-Content-Type-Options: nosniff');
    header('X-Frame-Options: DENY');
    header('X-XSS-Protection: 1; mode=block');

    try {
        // Rate limiting - prevent spam
        $rateLimitFile = __DIR__ . '/logs/rate_limit_' . md5($_SERVER['REMOTE_ADDR'] ?? 'unknown') . '.txt';
        $currentTime = time();

        if (file_exists($rateLimitFile)) {
            $lastRequest = (int)file_get_contents($rateLimitFile);
            if ($currentTime - $lastRequest < 60) { // 1 minute cooldown
                echo json_encode(['success' => false, 'error' => 'Aguarde um momento antes de enviar outra mensagem.']);
                exit;
            }
        }

        // Update rate limit
        file_put_contents($rateLimitFile, $currentTime);

        // Sanitize and validate input
        $nome = trim($_POST['nome'] ?? '');
        $email = trim($_POST['email'] ?? '');
        $empresa = trim($_POST['empresa'] ?? '(não informado)');
        $mensagem = trim($_POST['mensagem'] ?? '');

        // CSRF protection (basic)
        $csrfToken = $_POST['csrf_token'] ?? '';
        $sessionToken = $_SESSION['csrf_token'] ?? '';

        if (empty($csrfToken) || $csrfToken !== $sessionToken) {
            echo json_encode(['success' => false, 'error' => 'Erro de segurança. Recarregue a página e tente novamente.']);
            exit;
        }

        // Prepare data for email service
        $contactData = [
            'nome' => $nome,
            'email' => $email,
            'empresa' => $empresa,
            'mensagem' => $mensagem
        ];

        // Use professional email service
        $emailService = new \SoftEdge\EmailService();

        if ($emailService->sendContactEmail($contactData)) {
            echo json_encode(['success' => true]);
        } else {
            echo json_encode(['success' => false, 'error' => 'Erro ao enviar mensagem. Por favor, tente novamente ou entre em contato via WhatsApp.']);
        }

    } catch (\InvalidArgumentException $e) {
        echo json_encode(['success' => false, 'error' => $e->getMessage()]);
    } catch (\Exception $e) {
        error_log('Contact form error: ' . $e->getMessage());
        echo json_encode(['success' => false, 'error' => 'Erro interno do servidor. Nossa equipe foi notificada.']);
    }

    exit;
}

// Generate CSRF token for the form
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}
$_SESSION['csrf_token'] = bin2hex(random_bytes(32));
?>