src/Controller/MainController.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\Mailer\MailerInterface;
  7. use Symfony\Component\Mime\Address;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use App\Form\Type\ContactType;
  10. class MainController extends BaseController
  11. {
  12. /**
  13. * @Route("/", name="main")
  14. * @Route("/main/{sub}", name="main_sub")
  15. */
  16. public function index($sub = null): Response
  17. {
  18. return $this->render('main/index.html.twig', [
  19. 'controller_name' => 'MainController',
  20. 'subPage' => $sub
  21. ]);
  22. }
  23. /**
  24. * @Route("/timelines/edit/{id}", name="edit_timelines")
  25. */
  26. public function editTimelineAction(Request $request, $id = null)
  27. {
  28. if ($id !== null) {
  29. $id = preg_replace('/[^a-z0-9_-]/i', '', $id);
  30. }
  31. return $this->render('main/editor.html.twig', [
  32. 'id' => $id,
  33. 'title' => $id,
  34. 'editorUser' => $this->getUser(),
  35. ]);
  36. }
  37. /**
  38. * @Route("/timelines/{id}", name="timelines")
  39. */
  40. public function timelinesAction(Request $request, $id=null)
  41. {
  42. if($id == 'info')
  43. {
  44. return $this->render('main/timelines.html.twig',
  45. array('id' => 'info', 'title'=> "Timelines")
  46. );
  47. }
  48. else
  49. {
  50. if($id !== null)
  51. {
  52. $id = preg_replace('/[^a-z0-9_-]/i', '', $id);
  53. }
  54. return $this->render('main/timelines.html.twig',
  55. array('id' => $id, 'title' => $id)
  56. );
  57. }
  58. }
  59. /**
  60. * @Route("/tour/{id}", name="tour")
  61. */
  62. public function tourAction(Request $request, $id)
  63. {
  64. if($id !== null)
  65. {
  66. $id = preg_replace ( '/[^a-z0-9 ]/i', '', $id );
  67. }
  68. $title = ucwords($id);
  69. return $this->render('main/album.html.twig',
  70. array('album' => $id, 'title' => $title, 'backmenutitle'=>'Touren', 'backnav' => 'navmenuTouren')
  71. );
  72. }
  73. /**
  74. * @Route("/album/{id}", name="album")
  75. */
  76. public function albumAction(Request $request, $id)
  77. {
  78. if($id !== null)
  79. {
  80. $id = preg_replace ( '/[^a-z0-9 ]/i', '', $id );
  81. }
  82. $title = ucwords($id);
  83. return $this->render('main/album.html.twig',
  84. array('album' => $id, 'title' => $title, 'backmenutitle'=>'Projekte', 'backnav' => 'navmenuProjekte')
  85. );
  86. }
  87. /**
  88. * @Route("/kontakt/{subject}", name="contact")
  89. */
  90. public function contactAction(MailerInterface $mailer, Request $request, $subject = null)
  91. {
  92. $form = $this->createForm(ContactType::class, ['subject' => $subject]);
  93. $form->handleRequest($request);
  94. if ($form->isSubmitted() && $form->isValid()) {
  95. $name = (string) $form->get('name')->getData();
  96. $from = (string) $form->get('email')->getData();
  97. $subjectValue = (string) $form->get('subject')->getData();
  98. $content = (string) $form->get('content')->getData();
  99. $robotHoneypot = $form->get('spam_content')->getData();
  100. $messageData = "Name: {$name}\n"
  101. . "E-Mail: {$from}\n"
  102. . "Subject: {$subjectValue}\n"
  103. . "IP: {$request->getClientIp()}\n"
  104. . "Message: {$content}\n";
  105. $spam = false;
  106. foreach (['course-fitness.com'] as $spamEmailTerm) {
  107. if (str_contains(strtolower($from), strtolower($spamEmailTerm))) {
  108. $spam = true;
  109. break;
  110. }
  111. }
  112. $looksLikeSpam = $robotHoneypot !== null && $robotHoneypot !== ''
  113. || $spam
  114. || preg_match('(\\ssex|\\sporn|essay|girls|casino|money|iphone|\\scrypto|\\.ru|bitcoin)', strtolower($messageData)) === 1
  115. || preg_match('(\\.ru)', strtolower($from)) === 1;
  116. if ($looksLikeSpam) {
  117. // Silent success for bots; no mail.
  118. $this->addFlash('success', 'Ihre Nachricht wurde versandt. Ich werde mich in Kürze bei Dir melden.');
  119. return $this->redirectToRoute('contact');
  120. }
  121. try {
  122. $email = (new TemplatedEmail())
  123. ->from(new Address($_ENV['MAILER_FROM'], 'martinbrake.de'))
  124. ->to(new Address($_ENV['ADMIN_MAILER_TO'], 'Admin'))
  125. ->replyTo(new Address($from, $name))
  126. ->subject('Kontakt: ' . $subjectValue)
  127. ->htmlTemplate('email/message.html.twig')
  128. ->context([
  129. 'name' => $name,
  130. 'ip' => $request->getClientIp(),
  131. 'mail' => $from,
  132. 'message' => $content,
  133. ]);
  134. $mailer->send($email);
  135. $this->addFlash('success', 'Ihre Nachricht wurde versandt. Ich werde mich in Kürze bei Dir melden.');
  136. } catch (\Exception $e) {
  137. $this->addFlash('error', 'Irgendetwas ist gründlich schief gelaufen. Ich kümmere mich darum!');
  138. }
  139. return $this->redirectToRoute('contact');
  140. }
  141. return $this->render('main/contact.html.twig', [
  142. 'form' => $form->createView(),
  143. ]);
  144. }
  145. /**
  146. * @Route("/impressum", name="impressum")
  147. */
  148. public function impressumAction()
  149. {
  150. return $this->render('main/impressum.html.twig');
  151. }
  152. /**
  153. * @Route("/datenschutz", name="datenschutz")
  154. */
  155. public function datenschutzAction()
  156. {
  157. return $this->render('main/datenschutz.html.twig');
  158. }
  159. }