src/Controller/MainController.php line 21

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