src/Controller/MainController.php line 46

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. // Start session before rendering the CSRF field so the first POST can
  93. // validate the token (avoids "invalid CSRF" on the first try).
  94. $session = $request->getSession();
  95. if (!$session->isStarted()) {
  96. $session->start();
  97. }
  98. $form = $this->createForm(ContactType::class, ['subject' => $subject]);
  99. $form->handleRequest($request);
  100. if ($form->isSubmitted() && $form->isValid()) {
  101. $name = (string) $form->get('name')->getData();
  102. $from = (string) $form->get('email')->getData();
  103. $subjectValue = (string) $form->get('subject')->getData();
  104. $content = (string) $form->get('content')->getData();
  105. $robotHoneypot = $form->get('spam_content')->getData();
  106. $messageData = "Name: {$name}\n"
  107. . "E-Mail: {$from}\n"
  108. . "Subject: {$subjectValue}\n"
  109. . "IP: {$request->getClientIp()}\n"
  110. . "Message: {$content}\n";
  111. $spam = false;
  112. foreach (['course-fitness.com'] as $spamEmailTerm) {
  113. if (str_contains(strtolower($from), strtolower($spamEmailTerm))) {
  114. $spam = true;
  115. break;
  116. }
  117. }
  118. $looksLikeSpam = $robotHoneypot !== null && $robotHoneypot !== ''
  119. || $spam
  120. || preg_match('(\\ssex|\\sporn|essay|girls|casino|money|iphone|\\scrypto|\\.ru|bitcoin)', strtolower($messageData)) === 1
  121. || preg_match('(\\.ru)', strtolower($from)) === 1;
  122. if ($looksLikeSpam) {
  123. // Silent success for bots; no mail.
  124. $this->addFlash('success', 'Ihre Nachricht wurde versandt. Ich werde mich in Kürze bei Dir melden.');
  125. return $this->redirectToRoute('contact');
  126. }
  127. try {
  128. $email = (new TemplatedEmail())
  129. ->from(new Address($_ENV['MAILER_FROM'], 'martinbrake.de'))
  130. ->to(new Address($_ENV['ADMIN_MAILER_TO'], 'Admin'))
  131. ->replyTo(new Address($from, $name))
  132. ->subject('Kontakt: ' . $subjectValue)
  133. ->htmlTemplate('email/message.html.twig')
  134. ->context([
  135. 'name' => $name,
  136. 'ip' => $request->getClientIp(),
  137. 'mail' => $from,
  138. 'message' => $content,
  139. ]);
  140. $mailer->send($email);
  141. $this->addFlash('success', 'Ihre Nachricht wurde versandt. Ich werde mich in Kürze bei Dir melden.');
  142. return $this->redirectToRoute('contact');
  143. } catch (\Exception $e) {
  144. $this->addFlash('error', 'Irgendetwas ist gründlich schief gelaufen. Ich kümmere mich darum! Deine Eingaben bleiben erhalten — bitte später erneut senden.');
  145. // Fall through: re-render with submitted field values.
  146. }
  147. }
  148. $response = $this->render('main/contact.html.twig', [
  149. 'form' => $form->createView(),
  150. ]);
  151. // Never cache the form page — cached HTML would ship a stale CSRF token.
  152. $response->headers->set('Cache-Control', 'private, no-store, no-cache, must-revalidate');
  153. $response->headers->set('Pragma', 'no-cache');
  154. return $response;
  155. }
  156. /**
  157. * @Route("/impressum", name="impressum")
  158. */
  159. public function impressumAction()
  160. {
  161. return $this->render('main/impressum.html.twig');
  162. }
  163. /**
  164. * @Route("/datenschutz", name="datenschutz")
  165. */
  166. public function datenschutzAction()
  167. {
  168. return $this->render('main/datenschutz.html.twig');
  169. }
  170. }