34968-vm/api/support.php
Flatlogic Bot 8795a633f6 V22
2025-10-16 20:00:52 +00:00

75 lines
3.4 KiB
PHP

<?php
session_start();
require_once '../db/config.php';
require_once '../mail/MailService.php';
header('Content-Type: application/json');
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(['success' => false, 'message' => 'Method Not Allowed']);
exit;
}
$full_name = trim($_POST['full_name'] ?? '');
$email = trim($_POST['email'] ?? '');
$phone = trim($_POST['phone'] ?? null);
$subject = trim($_POST['subject'] ?? '');
$message = trim($_POST['message'] ?? '');
$latitude = !empty($_POST['latitude']) ? trim($_POST['latitude']) : null;
$longitude = !empty($_POST['longitude']) ? trim($_POST['longitude']) : null;
if (empty($full_name) || empty($email) || empty($subject) || empty($message) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
http_response_code(400);
echo json_encode(['success' => false, 'message' => 'Please fill in all required fields and provide a valid email.']);
exit;
}
try {
$db = db();
$stmt = $db->prepare(
"INSERT INTO support_tickets (full_name, email, phone, subject, message, latitude, longitude) VALUES (?, ?, ?, ?, ?, ?, ?)"
);
$stmt->execute([$full_name, $email, $phone, $subject, $message, $latitude, $longitude]);
// Notify support team
$support_email_to = 'support@majuroeats.com';
$support_email_subject = "New Support Ticket: " . htmlspecialchars($subject);
$support_email_html = "
<h1>New Support Inquiry</h1>
<p><strong>Name:</strong> " . htmlspecialchars($full_name) . "</p>
<p><strong>Email:</strong> " . htmlspecialchars($email) . "</p>
" . ($phone ? "<p><strong>Phone:</strong> " . htmlspecialchars($phone) . "</p>" : "") . "
<p><strong>Subject:</strong> " . htmlspecialchars($subject) . "</p>
<p><strong>Message:</strong></p>
<p>" . nl2br(htmlspecialchars($message)) . "</p>
" . ($latitude && $longitude ? "<p><strong>Location:</strong> <a href=\"https://www.google.com/maps?q={$latitude},{$longitude}\" target=\"_blank\">View on Map</a></p>" : "") . "
";
$support_email_text = strip_tags($support_email_html);
MailService::sendMail($support_email_to, $support_email_subject, $support_email_html, $support_email_text, ['reply_to' => $email]);
// Send confirmation to user
$user_email_subject = "We've received your message | MajuroEats Support";
$user_email_html = "
<h1>Thank You For Reaching Out!</h1>
<p>Hi " . htmlspecialchars($full_name) . ",</p>
<p>We've received your support request and a member of our team will get back to you shortly. Here is a copy of your message:</p>
<hr>
<p><strong>Subject:</strong> " . htmlspecialchars($subject) . "</p>
<p><strong>Message:</strong></p>
<p>" . nl2br(htmlspecialchars($message)) . "</p>
<hr>
<p>With thanks,</p>
<p>The MajuroEats Team</p>
";
$user_email_text = strip_tags($user_email_html);
MailService::sendMail($email, $user_email_subject, $user_email_html, $user_email_text);
echo json_encode(['success' => true, 'message' => 'Thank you! Our support team will contact you soon.']);
} catch (Exception $e) {
http_response_code(500);
// In a real app, you would log this error, not expose it to the user.
echo json_encode(['success' => false, 'message' => 'An unexpected error occurred. Please try again later.', 'error' => $e->getMessage()]);
}