src/Controller/ContactController.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Main\Contact;
  4. use ContainerC2aw7aC\getMessenger_Transport_AsyncService;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Contracts\Translation\TranslatorInterface;
  11. use Symfony\Component\Mailer\MailerInterface;
  12. use Symfony\Component\Mime\Email;
  13. class ContactController extends AbstractController
  14. {
  15.     /**
  16.      * @Route({
  17.      *     "en": "/contact-us",
  18.      *     "tr": "/iletisim"
  19.      * }, name="contact", methods={"GET","POST"})
  20.      */
  21.     public function index(Request $requestTranslatorInterface $translatorEntityManagerInterface $manager, \Swift_Mailer $mailer): Response
  22.     {
  23.         $errorMessage null;
  24.         $successMessage null;
  25.         $fullName null;
  26.         $email null;
  27.         $subject null;
  28.         $message null;
  29.         $recaptchaError null;
  30.         $content null;
  31.         if ($request->getMethod() == 'POST') {
  32.             parse_str($request->getContent(), $content);
  33.             // reCAPTCHA doğrulaması
  34.             $recaptchaError $this->validateRecaptcha($request$content$translator);
  35.             if (!isset($content['full_name']) || !$content['full_name']) {
  36.                 $fullName $translator->trans('THIS_VALUE_NOT_BE_BLANK');
  37.             }
  38.             if (!isset($content['email']) || !$content['email']) {
  39.                 $email $translator->trans('THIS_VALUE_NOT_BE_BLANK');
  40.             } else if (!is_numeric(strpos($content['email'], '@'))) {
  41.                 $email $translator->trans('THIS_VALUE_IS_NOT_CORRECT_EMAIL_ADDRESS');
  42.             }
  43.             if (!isset($content['subject']) || !$content['subject']) {
  44.                 $subject $translator->trans('THIS_VALUE_NOT_BE_BLANK');
  45.             }
  46.             if (!isset($content['message']) || !$content['message']) {
  47.                 $message $translator->trans('THIS_VALUE_NOT_BE_BLANK');
  48.             }
  49.             if (!$fullName && !$email && !$subject && !$message && !$recaptchaError) {
  50.                 try {
  51.                     // SwiftMailer ile mesaj oluşturma
  52.                     $emailMessage = (new \Swift_Message('Komili İletişim Formu: ' $content['subject']))
  53.                         ->setFrom(['eitrwcp@evyap.com' => 'Komili'])
  54.                         ->setTo(['eitr@evyap.com','serhanbuy@gmail.com','fetirli@evyap.com'])
  55.                         ->setBody("
  56.                             <h2>İletişim Formu</h2>
  57.                             <p><strong>İsim:</strong> {$content['full_name']}</p>
  58.                             <p><strong>Email:</strong> {$content['email']}</p>
  59.                             <p><strong>Konu:</strong> {$content['subject']}</p>
  60.                             <p><strong>Mesaj:</strong></p>
  61.                             <p>{$content['message']}</p>
  62.                         "'text/html');
  63.                     // Email'i gönder
  64.                     $logger = new \Swift_Plugins_Loggers_ArrayLogger();
  65.                     $mailer->registerPlugin(new \Swift_Plugins_LoggerPlugin($logger));
  66.                     try {
  67.                         $result $mailer->send($emailMessage);
  68.                     } catch (\Throwable $e) {
  69.                         error_log('SwiftMailer exception: '.$e->getMessage());
  70.                     } finally {
  71.                         // DİKKAT: log bazen base64 auth satırları içerebilir; prod’da uzun tutma
  72.                         error_log($logger->dump());
  73.                     }
  74.                     // Email başarıyla gönderildiyse veritabanına kaydet
  75.                     if ($result) {
  76.                         $contact = new Contact();
  77.                         $contact->setMessage($content['message'])
  78.                             ->setEmail($content['email'])
  79.                             ->setFullName($content['full_name'])
  80.                             ->setSubject($content['subject']);
  81.                         $manager->persist($contact);
  82.                         $manager->flush();
  83.                         // Başarılı mesajı
  84.                         $successMessage $translator->trans('YOUR_REQUEST_HAS_BEEN_SUCCESSFULLY_SUBMITTED');
  85.                         $content null;
  86.                     } else {
  87.                         $errorMessage $translator->trans('MESSAGE_COULD_NOT_BE_SENT');
  88.                     }
  89.                 } catch (\Exception $e) {
  90.                     // Hata mesajı
  91.                     $errorMessage $translator->trans('MESSAGE_COULD_NOT_BE_SENT');
  92.                     // Hata logla
  93.                     error_log('Mail gönderim hatası: ' $e->getMessage());
  94.                 }
  95.             } else {
  96.                 $errorMessage $translator->trans('CHECK_INFORMATION_AND_TRY_AGAIN');
  97.             }
  98.         }
  99.         return $this->render('contact/index.html.twig', [
  100.             'fullName' => $fullName,
  101.             'email' => $email,
  102.             'subject' => $subject,
  103.             'message' => $message,
  104.             'errorMessage' => $errorMessage,
  105.             'successMessage' => $successMessage,
  106.             'values' => $content,
  107.             'recaptchaError' => $recaptchaError ?? null,
  108.         ]);
  109.     }
  110.     /**
  111.      * reCAPTCHA v3 doğrulaması yapar
  112.      */
  113.     private function validateRecaptcha(Request $request, array $contentTranslatorInterface $translator): ?string
  114.     {
  115.         // reCAPTCHA response'ı kontrol et
  116.         if (!isset($content['g-recaptcha-response']) || empty($content['g-recaptcha-response'])) {
  117.             return $translator->trans('RECAPTCHA_REQUIRED');
  118.         }
  119.         $recaptchaResponse $content['g-recaptcha-response'];
  120.         $secretKey $_ENV['GOOGLE_RECAPTCHA_SECRET_KEY'] ?? '6LcmpY4rAAAAAD-HIDPv9v7ehCUrRMAtOcJfSEhO';
  121.         // Google'a doğrulama isteği gönder
  122.         $verifyURL 'https://www.google.com/recaptcha/api/siteverify';
  123.         $postData = [
  124.             'secret' => $secretKey,
  125.             'response' => $recaptchaResponse,
  126.             'remoteip' => $request->getClientIp()
  127.         ];
  128.         $ch curl_init();
  129.         curl_setopt($chCURLOPT_URL$verifyURL);
  130.         curl_setopt($chCURLOPT_POSTtrue);
  131.         curl_setopt($chCURLOPT_POSTFIELDShttp_build_query($postData));
  132.         curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  133.         curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);
  134.         $response curl_exec($ch);
  135.         curl_close($ch);
  136.         $responseData json_decode($responsetrue);
  137.         // v3 için success ve score kontrolü
  138.         if (!$responseData['success']) {
  139.             return $translator->trans('RECAPTCHA_FAILED');
  140.         }
  141.         // reCAPTCHA v3 score kontrolü (0.0-1.0, yüksek = insan, düşük = bot)
  142.         $score $responseData['score'] ?? 0;
  143.         $threshold 0.5// Eşik değeri, ayarlanabilir
  144.         if ($score $threshold) {
  145.             error_log("reCAPTCHA v3 Score düşük: $score (eşik: $threshold)");
  146.             return $translator->trans('RECAPTCHA_FAILED');
  147.         }
  148.         // Action kontrolü (opsiyonel)
  149.         $action $responseData['action'] ?? '';
  150.         if ($action !== 'contact_form') {
  151.             error_log("reCAPTCHA v3 Action uyumsuzluğu: $action");
  152.             return $translator->trans('RECAPTCHA_FAILED');
  153.         }
  154.         return null// Doğrulama başarılı
  155.     }
  156. }