src/Controller/WebController.php line 84

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\Routing\Annotation\Route;
  5. //use Symfony\Contracts\Translation\TranslatorInterface;
  6. //use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
  7. //use Symfony\Component\HttpFoundation\Session\Session;
  8. //use Symfony\Component\HttpFoundation\Session\SessionInterface;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use App\Service\EmailService;
  13. class WebController extends AbstractController
  14. {
  15.     private $session;
  16.     private $requestStack;
  17.     public function __construct(RequestStack $requestStack)
  18.     {
  19.         //$this->session = $session;
  20.         $this->session $requestStack->getSession();
  21.         $this->requestStack $requestStack;
  22.         $this->session->start();
  23.     }
  24.     /**
  25.      * @Route({
  26.      *     "sk": "/",
  27.      *     "en": "/{_locale}/",
  28.      *     "de": "/{_locale}/",
  29.      *     "pl": "/{_locale}/"
  30.      * }, name="web_Home")
  31.      */              
  32.     public function web_Home(Request $request)
  33.     {    
  34.         /*
  35.         // Ak je to prvy vstup - presmerovanie na jazyk podkla browsera
  36.         // problem je ze toto sa deje aj ked je vstup s _locale v url - neviem ako identifikovat ze jazyk v url nie je (je tam vzdy doplneny SK ako default)
  37.         if (!$this->session->has('lang')) {
  38.             $user_lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
  39.             $languages = ['sk', 'en', 'de'];
  40.             
  41.             if (in_array($user_lang, $languages)) {
  42.                 $lang = $user_lang;
  43.             }
  44.             else {
  45.                 $lang = $this->getParameter('kernel.default_locale');
  46.             }
  47.             
  48.             $this->session->set('lang', $lang);        
  49.             return $this->redirectToRoute('web_Home', array('_locale' => $lang));    
  50.         }
  51.         */
  52.     
  53.         // Captcha
  54.         /* // Session $session
  55.         $session->start();
  56.         require_once("../public/captcha/captcha.php");
  57.         $session->set('captcha', getCaptcha());        
  58.         $captcha = $session->get('captcha');
  59.         */
  60.         require_once("../public/captcha/captcha.php");
  61.         $this->session->set('captcha'getCaptcha());        
  62.         $captcha $this->session->get('captcha');        
  63.         
  64.         return $this->render('web/page_index.html.twig', [
  65.             'captcha_image_src' => $captcha['image_src']
  66.         ]);
  67.     }
  68.      
  69.     /**
  70.      * @Route({
  71.      *     "sk": "/{_locale}/ubytovanie",
  72.      *     "en": "/{_locale}/accommodation",
  73.      *     "de": "/{_locale}/unterkunft",
  74.      *     "pl": "/{_locale}/zakwaterowanie"
  75.      * }, name="web_Accommodation")
  76.      */     
  77.     public function web_Accommodation()
  78.     {    
  79.         return $this->render('web/page_accommodation.html.twig');
  80.     }
  81.      
  82.     /**
  83.      * @Route({
  84.      *     "sk": "/{_locale}/galeria",
  85.      *     "en": "/{_locale}/gallery",
  86.      *     "de": "/{_locale}/galerie",
  87.      *     "pl": "/{_locale}/galeria"
  88.      * }, name="web_Gallery")
  89.      */     
  90.     public function web_Gallery()
  91.     {    
  92.         return $this->render('web/page_gallery.html.twig');
  93.     }    
  94.     
  95.     /**
  96.      * @Route({
  97.      *     "sk": "/{_locale}/okolie",
  98.      *     "en": "/{_locale}/surroundings",
  99.      *     "de": "/{_locale}/umfeld",
  100.      *     "pl": "/{_locale}/okolica"
  101.      * }, name="web_Surroundings")
  102.      */     
  103.     public function web_Surroundings()
  104.     {    
  105.         return $this->render('web/page_surroundings.html.twig');
  106.     }        
  107.     /**
  108.      * @Route({
  109.      *     "sk": "/{_locale}/kontakt",
  110.      *     "en": "/{_locale}/contact",
  111.      *     "de": "/{_locale}/kontakt",
  112.      *     "pl": "/{_locale}/kontakt"
  113.      * }, name="web_Contact")
  114.      */     
  115.     public function web_Contact()
  116.     {    
  117.         // Captcha
  118.         require_once("../public/captcha/captcha.php");
  119.         $this->session->set('captcha'getCaptcha());        
  120.         $captcha $this->session->get('captcha');
  121.         return $this->render('web/page_contact.html.twig', [
  122.             'captcha_image_src' => $captcha['image_src']        
  123.         ]);
  124.     }    
  125.      
  126.     /**
  127.      * @Route("/email/send", name="web_EmailSend")
  128.      * @Route("/{_locale}/email/send", name="web_EmailSend2")
  129.      */
  130.     public function web_EmailSend(Request $requestEmailService $emailService)
  131.     {    
  132.         //file_put_contents('log.txt', $request->get('user_name'));
  133.     
  134.         $subject "Kontaktný formulár"
  135.         $success_mssg "Vaša sprva bola odoslaná. Ďakujeme.";               
  136.         $error_mssg "Nastala chyba. Správa nebola odoslaná."
  137.         $short_mssg "Správa je príliš krátka.";                      
  138.         $empty_fields "Nezadali ste všetky potrebné údaje.";              
  139.         $name_mssg "Meno je príliš krátke (alebo prázdne).";             
  140.         $email_mssg "Emailová adresa nie je platná.";                                  
  141.         $captcha_mssg "Overovací kód nie je platný."
  142.         if ($request->isMethod('POST')) {
  143.             // Sanitize input data using PHP filter_var(). *PHP 5.2.0+
  144.             $user_name filter_var($request->get('user_name'), FILTER_SANITIZE_STRING);
  145.             $user_email filter_var($request->get('user_email'), FILTER_SANITIZE_EMAIL);
  146.             $user_message filter_var($request->get('user_message'), FILTER_SANITIZE_STRING);
  147.             $user_phone filter_var($request->get('user_phone'), FILTER_SANITIZE_STRING);
  148.             $user_subject filter_var($request->get('user_subject'), FILTER_SANITIZE_STRING);
  149.             $user_captcha filter_var($request->get('user_captcha'), FILTER_SANITIZE_STRING);            
  150.             if (!empty($user_subject)) {
  151.                 $subject $user_subject;
  152.             }
  153.                         
  154.             // Check $_POST vars are set, exit if any missing
  155.             if (empty($user_name) or empty($user_email) or empty($user_message) or empty($user_captcha)) {                
  156.                 $output = array('type'=>'error''text' => $empty_fields);
  157.             }
  158.             else {
  159.                 
  160.                 // Additional php validation
  161.                 if (strlen($user_name) < 4) { // If length is less than  it will throw an HTTP error.
  162.                     $output = array('type'=>'error''text' => $name_mssg);
  163.                 }
  164.                 
  165.                 // Check Email
  166.                 else if (!filter_var($user_emailFILTER_VALIDATE_EMAIL)) { //email validation
  167.                     $output = array('type'=>'error''text' => $email_mssg);
  168.                 }
  169.                 // Check Message
  170.                 else if (strlen($user_message) < 10) {
  171.                     $output = array('type'=>'error''text' => $short_mssg);
  172.                 }
  173.                 
  174.                 // Check captcha
  175.                 /*
  176.                 else if ($user_captcha != $_SESSION['_sf2_attributes']['captcha']['code']) {
  177.                     $output = array('type'=>'error', 'text' => $captcha_mssg);
  178.                 }
  179.                 */                
  180.                 
  181.                 // Odoslanie
  182.                 else {        
  183.                     $template 'web/email.html.twig';
  184.                     $data = [
  185.                         'user_name' => $user_name,
  186.                         'user_email' => $user_email,
  187.                         'user_phone' => $user_phone,
  188.                         'subject' => $subject,
  189.                         'user_message' => $user_message
  190.                     ];                    
  191.                     $content $this->render($template, ['data' => $data])->getContent();
  192.                     
  193.                     $action_result $emailService->sendTemplatedEmail($template$data$user_email$subject$content);
  194.                     
  195.                     if ($action_result['result'] == 'OK') {
  196.                         $output = array('type'=>'message''text' => $success_mssg);
  197.                     }
  198.                     else {
  199.                         $output = array('type'=>'error''text' => $error_mssg);
  200.                     }
  201.                 }
  202.             }
  203.             
  204.             return $this->json($output);
  205.         }
  206.         else {
  207.         
  208.             return new Response('<html><body>ONLY POST AlLOWED</body></html>');
  209.         
  210.         }    
  211.     }        
  212.     
  213. }