false, 'message' => 'Método não permitido.']); } $name = trim((string)($_POST['name'] ?? '')); $email = trim((string)($_POST['email'] ?? '')); $phone = trim((string)($_POST['phone'] ?? '')); $projectType = trim((string)($_POST['project_type'] ?? '')); $message = trim((string)($_POST['message'] ?? '')); $honeypot = trim((string)($_POST['website'] ?? '')); if ($honeypot !== '') { respond(200, ['success' => true, 'message' => 'Pedido recebido. Obrigado.']); } $errors = []; if (mb_strlen($name) < 2 || mb_strlen($name) > 120) $errors[] = 'Indica um nome válido.'; if (!filter_var($email, FILTER_VALIDATE_EMAIL) || mb_strlen($email) > 160) $errors[] = 'Indica um email válido.'; if ($phone !== '' && mb_strlen($phone) > 40) $errors[] = 'O telemóvel é demasiado longo.'; if ($projectType === '' || mb_strlen($projectType) > 80) $errors[] = 'Selecciona o tipo de projecto.'; if (mb_strlen($message) < 20 || mb_strlen($message) > 1800) $errors[] = 'A mensagem deve ter entre 20 e 1800 caracteres.'; if ($errors) { respond(422, ['success' => false, 'message' => implode(' ', $errors)]); } $recipient = 'ola@paulogomes.pt'; $siteName = 'pg Web e APP Designer'; $emailSent = false; $submissionId = null; try { require_once __DIR__ . '/../db/config.php'; $pdo = db(); $pdo->exec("CREATE TABLE IF NOT EXISTS contact_requests ( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, name VARCHAR(120) NOT NULL, email VARCHAR(160) NOT NULL, phone VARCHAR(40) NULL, project_type VARCHAR(80) NOT NULL, message TEXT NOT NULL, email_sent TINYINT(1) NOT NULL DEFAULT 0, status VARCHAR(30) NOT NULL DEFAULT 'new', ip_address VARCHAR(45) NULL, user_agent VARCHAR(255) NULL, created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci"); $stmt = $pdo->prepare('INSERT INTO contact_requests (name, email, phone, project_type, message, ip_address, user_agent) VALUES (:name, :email, :phone, :project_type, :message, :ip, :user_agent)'); $stmt->execute([ ':name' => $name, ':email' => $email, ':phone' => $phone !== '' ? $phone : null, ':project_type' => $projectType, ':message' => $message, ':ip' => substr((string)($_SERVER['REMOTE_ADDR'] ?? ''), 0, 45) ?: null, ':user_agent' => substr((string)($_SERVER['HTTP_USER_AGENT'] ?? ''), 0, 255) ?: null, ]); $submissionId = (int)$pdo->lastInsertId(); } catch (Throwable $e) { error_log('Contact DB error: ' . $e->getMessage()); } try { require_once __DIR__ . '/../mail/MailService.php'; $safeName = htmlspecialchars($name, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); $safeEmail = htmlspecialchars($email, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); $safePhone = htmlspecialchars($phone !== '' ? $phone : 'Não indicado', ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); $safeProject = htmlspecialchars($projectType, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); $safeMessage = nl2br(htmlspecialchars($message, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8')); $html = "
Nome: {$safeName}
" . "Email: {$safeEmail}
" . "Telemóvel: {$safePhone}
" . "Tipo de projecto: {$safeProject}
" . "Website: {$siteName}
" . "Detalhe da mensagem:
{$safeMessage}
"; $text = "Novo pedido — {$siteName}\nNome: {$name}\nEmail: {$email}\nTelemóvel: " . ($phone !== '' ? $phone : 'Não indicado') . "\nTipo de projecto: {$projectType}\n\nDetalhe da mensagem:\n{$message}"; $result = MailService::sendMail($recipient, $siteName . ' — novo pedido de contacto', $html, $text, ['reply_to' => $email]); $emailSent = !empty($result['success']); if (!$emailSent) error_log('Contact mail error: ' . ($result['error'] ?? 'unknown')); } catch (Throwable $e) { error_log('Contact mail exception: ' . $e->getMessage()); } if ($submissionId) { try { $stmt = db()->prepare('UPDATE contact_requests SET email_sent = :email_sent WHERE id = :id'); $stmt->execute([':email_sent' => $emailSent ? 1 : 0, ':id' => $submissionId]); } catch (Throwable $e) { error_log('Contact DB update error: ' . $e->getMessage()); } } $messageText = $emailSent ? 'Mensagem enviada com sucesso. Obrigado — responderei assim que possível.' : 'Pedido guardado. Se o envio por email não estiver configurado no servidor, contacta directamente ola@paulogomes.pt.'; respond(200, ['success' => true, 'message' => $messageText]);