src/Controller/HomeController.php line 142

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Dto\ContactFormData;
  4. use App\Form\ContactForm;
  5. use App\Security\ApiUser;
  6. use App\Service\Client\ActivityFeedService;
  7. use App\Service\Client\Content\VideoListingService;
  8. use App\Service\Client\MailService;
  9. use App\Service\Client\User\AccountService;
  10. use App\Service\Client\User\AmateurListingService;
  11. use App\Service\Client\User\MemberService;
  12. use App\Service\ReCaptchaService;
  13. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  14. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  15. use Symfony\Component\HttpFoundation\RedirectResponse;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  21. class HomeController extends AbstractController
  22. {
  23.     protected VideoListingService $videoService;
  24.     protected AmateurListingService $amateurService;
  25.     protected ActivityFeedService $activityService;
  26.     protected AccountService $accountService;
  27.     public function __construct(VideoListingService $serviceAmateurListingService $amateurServiceActivityFeedService $activityServiceAccountService $accountService)
  28.     {
  29.         $this->videoService $service;
  30.         $this->amateurService $amateurService;
  31.         $this->activityService $activityService;
  32.         $this->accountService $accountService;
  33.     }
  34.     /**
  35.      * @Route("/")
  36.      * @Route("/infostream")
  37.      * @Template()
  38.      * @return array
  39.      */
  40.     public function index(\Redis $redis): array
  41.     {
  42.         /** @var ?ApiUser $user */
  43.         $user $this->getUser();
  44.         $feedHtmls = ['initial' => [], 'delayed' => []];
  45.         if (null === $user) {
  46.             # gitlab #593
  47.             #$feedHtmls = $this->activityService->getPublicInfostream($redis);
  48.         }
  49.         return [
  50.             'videos' => $this->videoService->getBestOfTheMonthVideos(16),
  51.             'onlineAmateurs' => $this->amateurService->getTopRatedAmateurs(130truetrue),
  52.             'otherAmateurs' => $this->amateurService->getRandomAmateurs(6true),
  53.             'feedHtmls' => $feedHtmls
  54.         ];
  55.     }
  56.     /**
  57.      * @Route("/contact/privacy")
  58.      * @Template()
  59.      * @return array
  60.      */
  61.     public function privacy(): array
  62.     {
  63.         return [];
  64.     }
  65.     /**
  66.      * @Route("/contact/terms")
  67.      * @Template()
  68.      * @return array
  69.      */
  70.     public function terms(): array
  71.     {
  72.         return [];
  73.     }
  74.     /**
  75.      * @Route("/contact/about")
  76.      * @Template()
  77.      * @return array
  78.      */
  79.     public function about(): array
  80.     {
  81.         return [];
  82.     }
  83.     /**
  84.      * @Route("/kontakt")
  85.      * @Template()
  86.      * @return array|Response
  87.      */
  88.     public function contact(Request $requestMemberService $memberServiceMailService $mailServiceReCaptchaService $recaptchaServiceFlashBagInterface $flashBag)
  89.     {
  90.         $user $this->getUser();
  91.         $userDetails = [];
  92.         if ($user instanceof ApiUser && $user->getUsername() !== '') {
  93.             $userDetails $memberService->getMemberDetailByUsername($user->getUsername());
  94.         }
  95.         $form $this->createForm(ContactForm::class, (new ContactFormData($userDetails)));
  96.         $form->handleRequest($request);
  97.         if ($form->isSubmitted() && $form->isValid()) {
  98.             if (!$recaptchaService->isValidSubmission($request)) {
  99.                 $flashBag->add('info''Bitte lösen Sie das Captcha.');
  100.                 return new RedirectResponse($this->generateUrl('app_home_contact'));
  101.             }
  102.             $form->getData()->setIsUserLoggedIn(false);
  103.             try {
  104.                 $this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED');
  105.                 $form->getData()->setIsUserLoggedIn(true);
  106.             } catch (AccessDeniedException $a) {
  107.             }
  108.             // Some sanity checks to battle spam
  109.             $doSend $this->doesContainSpam($form->getData()) === false;
  110.             if($doSend === true) {
  111.                 if ($mailService->sendContactInquiry($form->getData())) {
  112.                     $request->getSession()->set('emailSent'true);
  113.                     $request->getSession()->set('failure'false);
  114.                 } else {
  115.                     $request->getSession()->set('emailSent'false);
  116.                     $request->getSession()->set('failure'true);
  117.                 }
  118.             } else {
  119.                 // if spam is detacted we don't send an email but don't want to alarm anybody, so stay silent!
  120.                 $request->getSession()->set('emailSent'true);
  121.                 $request->getSession()->set('failure'false);
  122.             }
  123.             return new RedirectResponse($this->generateUrl('app_home_contact'));
  124.         }
  125.         $emailSent $request->getSession()->remove('emailSent') ?? false;
  126.         $failure $request->getSession()->remove('failure') ?? false;
  127.         return [
  128.             'form' => $form->createView(),
  129.             'emailSent' => $emailSent,
  130.             'failure' => $failure,
  131.         ];
  132.     }
  133.     /**
  134.      * @Route("/password-change/confirm/{confirmationToken}")
  135.      * @param string $confirmationToken
  136.      * @return RedirectResponse
  137.      * @throws \Psr\Cache\InvalidArgumentException
  138.      * @throws \ReflectionException
  139.      */
  140.     public function passwordChangeConfirm(string $confirmationToken): RedirectResponse
  141.     {
  142.         try {
  143.             if ($this->accountService->changePasswordConfirmation($confirmationToken)) {
  144.                 return $this->redirectToRoute('app_security_logout');
  145.             }
  146.             $this->addFlash('warning''Der verwendete Link ist evtl. nicht mehr gültig. Bitte versuche es erneut oder kontaktiere den Support.');
  147.         } catch (\DomainException $e) {
  148.             $this->addFlash('error'$e->getMessage());
  149.         }
  150.         if ($this->getUser() instanceof ApiUser) {
  151.             return $this->redirectToRoute('app_user_dashboard_index');
  152.         }
  153.         return $this->redirectToRoute('login');
  154.     }
  155.     /**
  156.      * @Route("/email-change/confirm/{confirmationToken}")
  157.      * @param string $confirmationToken
  158.      * @return RedirectResponse
  159.      */
  160.     public function emailChangeConfirm(string $confirmationToken): RedirectResponse
  161.     {
  162.         try {
  163.             if ($this->accountService->changeEmailConfirmation($confirmationToken)) {
  164.                 return $this->redirectToRoute('app_security_logout');
  165.             }
  166.             $this->addFlash('warning''Der verwendete Link ist evtl. nicht mehr gültig. Bitte versuche es erneut oder kontaktiere den Support.');
  167.         } catch (\DomainException $e) {
  168.             $this->addFlash('error'$e->getMessage());
  169.         }
  170.         if ($this->getUser() instanceof ApiUser) {
  171.             return $this->redirectToRoute('app_user_dashboard_index');
  172.         }
  173.         return $this->redirectToRoute('login');
  174.     }
  175.     /**
  176.      * Checks if the given $text contains any link
  177.      * 
  178.      * @param string $text text to search in
  179.      * @return bool
  180.      */
  181.     private function doesStringContainLink(string $text): bool {
  182.         return (bool)preg_match("/http[s]?:\/\//"$text);
  183.     }
  184.     /**
  185.      * Checks some values to determine if contact request is valid (length checks, link checks)
  186.      * 
  187.      * @param ContactFormData $data data to check
  188.      * @return bool is the request fishy and shall be processed as spam?
  189.      */
  190.     private function doesContainSpam(ContactFormData $data) {
  191.         $dataAsArr $data->toArray();
  192.         foreach(array_keys($dataAsArr) as $key) {
  193.             if(in_array($key, ["message"]) === false && strlen($dataAsArr[$key]) > && $this->doesStringContainLink($dataAsArr[$key]) === true) {
  194.                 // links are only allowed in the message field
  195.                 return true;
  196.             }
  197.         }
  198.         if(strlen($data->getFirstName()) > 50) {
  199.             // firstName exceeding 50 chars? Seams fishy -> bye bye
  200.             return true;
  201.         }
  202.         if(strlen($data->getLastName()) > 50) {
  203.             // lastName exceeding 50 chars? Seams fishy -> bye bye
  204.             return true;
  205.         }
  206.         if(strlen($data->getEmail()) > 100 || filter_var($data->getEmail(), FILTER_VALIDATE_EMAIL) === false) {
  207.             // mail exceeding 100 chars? Seams fishy -> bye bye
  208.             return true;
  209.         }
  210.         return false;
  211.     }
  212. }