src/Controller/UserController.php line 163

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. use App\Entity\User;
  9. use App\Entity\Userlog;
  10. use App\Entity\Cart;
  11. class UserController extends AbstractController
  12. {
  13.     private $requestStack;
  14.     public function __construct(RequestStack $requestStack)
  15.     {
  16.         $this->requestStack $requestStack;
  17.         // Accessing the session in the constructor is *NOT* recommended, since
  18.         // it might not be accessible yet or lead to unwanted side-effects
  19.         // $this->session = $requestStack->getSession();
  20.     }
  21.     /**
  22.      * @Route("/loggedin/" , name="loggedin")
  23.      */
  24.     public function loggedin()
  25.     {
  26.         include("./include/function.php");
  27.         include("./include/global.php");
  28.         $user $this->get('security.token_storage')->getToken()->getUser();
  29.         if($user->getId() != 0)
  30.         {
  31.             $em $this->getDoctrine()->getManager();
  32.             $d = new \DateTime(strtotime('Y-m-d H:i:s'));
  33.             $user->setULastlogin($d);
  34.         
  35.             $em->persist($user);
  36.             $em->flush();
  37.         
  38.             
  39.             $session $this->requestStack->getSession(); //$session = $this->get('session');
  40.             $placeid $session->get('place');
  41.             $log = new Userlog();
  42.             if($this->get('security.token_storage')->getToken()->getUser() == 'anon.'){
  43.                 $log->setUlUid(0);
  44.             }
  45.             else{
  46.                 $log->setUlUid$user->getId());
  47.             }
  48.             $log->setUlSessionid($session->getId());
  49.             $log->setUlBrowser($_SERVER['HTTP_USER_AGENT']);
  50.             $log->setUlIp($_SERVER['REMOTE_ADDR']);
  51.             
  52.             $now = new \DateTime(date("Y-m-d H:i:s"));
  53.             $log->setUlNow($now);
  54.             if($placeid == NULL)
  55.             {
  56.                 $log->setUlPlace(0);
  57.             }
  58.             else
  59.             {
  60.                 $log->setUlPlace($placeid);
  61.             }
  62.             $log->setUlAction(1);
  63.             $log->setUlReferer($_SERVER['HTTP_REFERER']);
  64.             $log->setUlCurrentPage($_SERVER['REQUEST_URI']);
  65.             $em $this->getDoctrine()->getManager();
  66.             $em->persist($log);
  67.             $em->flush();
  68.             $conn $this->getDoctrine()->getManager()->getConnection();
  69.             $query "DELETE FROM cart WHERE  ca_now <= ?";
  70.             
  71.             $conn->executeQuery(
  72.                 $query
  73.                 ,array(
  74.                     date("Y-m-d 00:00:00")
  75.                 )
  76.              );
  77.         }
  78.         $session $this->requestStack->getSession(); //$session = $this->get('session');
  79.         if($company == 'KVO')
  80.         {
  81.             $placeid 1;
  82.         }
  83.         else
  84.         {
  85.             $placeid $session->get('place');
  86.         }
  87.         
  88.         //$response = $this->forward('App\Controller\CourseController::homepage', []);
  89.         $response $this->forward('App\Controller\CourseController::homepage2', ['placeid' => $placeid]);
  90.         return $response;
  91.     }
  92.     /**
  93.      * @Route("/usersettings" , name="userSettings")
  94.      */
  95.     public function settings(): Response
  96.     {
  97.        
  98.         if($this->get('security.token_storage')->getToken()->getUser() == 'anon.')
  99.         {
  100.             $response $this->forward('App\Controller\CourseController::homepage', []);
  101.             return $response;
  102.         }
  103.         $user $this->get('security.token_storage')->getToken()->getUser();
  104.         $user->getId();
  105.         
  106.         
  107.         $t1 $user->getUBirthday(); ;
  108.         $t1 $t1->format('d.m.Y'); 
  109.         $session $this->requestStack->getSession(); //$session = $this->get('session');
  110.         $place $session->get('place');
  111.         return $this->render('user/settings.html.twig', [
  112.             'first' => $user->getUFirstname(),
  113.             'last' => $user->getULastname(),
  114.             'email' => $user->getUEmail(),
  115.             'username' => $user->getUUsername(),
  116.             'password' => $user->getUPassword(),
  117.             'street' => $user->getUStreet(),
  118.             'zip' => $user->getUZip(),
  119.             'city' => $user->getUCity(),
  120.             'birthday' => $t1,
  121.             'place' => $place,
  122.             'telephone' => $user->getUTelephone()
  123.         ]);
  124.     }
  125.     /**
  126.      * @Route("/register" , name="register")
  127.      */
  128.     public function register(): Response
  129.     {
  130.         $session $this->requestStack->getSession(); //$session = $this->get('session');
  131.         $place $session->get('place');
  132.         return $this->render('user/register.html.twig',['place' => $place]);
  133.     }
  134.     
  135.     /**
  136.      * @Route("/pwforgot" , name="pwforgot")
  137.      */
  138.     public function pwforgot(): Response
  139.     {
  140.         $session $this->requestStack->getSession(); //$session = $this->get('session');
  141.         $place $session->get('place');
  142.         return $this->render('user/pwforgot.html.twig',['place' => $place]);
  143.     }
  144.     
  145.     /**
  146.      * @Route("/sendNewPw/{mail}/{username}/" , name="sendNewPw", options={"expose"=true})
  147.      */
  148.     public function sendNewPw($mail$username)
  149.     {
  150.         include("./include/function.php");
  151.         include("./include/global.php");
  152.         if(trim(checkUserInput($username)) == ""  || trim(checkUserInput($mail)) == ""){ return new Response('error2'); die(); }
  153.         
  154.         $repository $this->getDoctrine()->getRepository(User::class);
  155.         $user $repository->findOneBy([
  156.         'u_username' => trim(checkUserInput($username)),
  157.         'u_email' => trim(checkUserInput($mail))
  158.         ]);
  159.         if(empty($user)) 
  160.         {
  161.             return new Response('error1');
  162.             die();
  163.         }
  164.         
  165.         
  166.         $session $this->requestStack->getSession(); //$session = $this->get('session');
  167.         $placeid $session->get('place');
  168.         
  169.         if($placeid == "" || $placeid == NULL || $placeid == 0)
  170.         {
  171.             $placeid 2;
  172.         }
  173.         
  174.         $newpw $this->generateRandomString(10);
  175.         
  176.         ini_set('max_execution_time'30);
  177.         
  178.         require("./include/phpmailer/PHPMailerAutoload.php");
  179.         $mail2             = new \PHPMailer();
  180.         $mail2->IsSMTP();
  181.         $mail2->SMTPDebug  0;
  182.         $mail2->Host       $smtp_mail_host;
  183.         $mail2->Port       $smtp_mail_port;
  184.         if(!empty($smtp_mail_user[$placeid])) {
  185.             $mail2->SMTPAuth   true;
  186.             $mail2->Username   $smtp_mail_user[$placeid];
  187.             $mail2->Password   $smtp_mail_pass[$placeid];
  188.         } else {
  189.             $mail2->SMTPAuth   false;
  190.         }
  191.         if($smtp_mail_secured
  192.         {
  193.             $mail2->SMTPSecure "ssl";
  194.         }
  195.                     
  196.                     
  197.         $mail2->SetFrom($mailFromAdress[$placeid], $mailFromAdressName);
  198.         
  199.         $mail2->Subject "Ihr neues Passwort";
  200.         
  201.         $txt "Sehr geehrte Damen und Herren, <br><br>";
  202.         $txt .= "anbei senden wir Ihnen Ihr neues Passwort f&uuml;r das Kurstool.<br><br>";
  203.         $txt .= "Ihr neues Passwort lautet: ".$newpw."<br><br>";
  204.         $txt .= "Mit freundlichen Gr&uuml;&szlig;en,<br>";
  205.         $txt .= "".$companyFull;
  206.         
  207.         $mail2->MsgHTML($txt);
  208.         
  209.         $mail2->AddAddress(checkUserInput($mail));
  210.         if(!$mail2->Send()) 
  211.         {
  212.             debugText("Die E-Mail konnte nicht versendet werden! Bitte wenden Sie sich an den Administrator!<br><br>" $mail2->ErrorInfo);
  213.             return new Response("nok");
  214.         }
  215.         else
  216.         {
  217.             $em $this->getDoctrine()->getManager();
  218.             $user->setUPassword(password_hash($newpw,  PASSWORD_BCRYPT ));
  219.             $em->persist($user);
  220.             $em->flush();
  221.            return new Response('ok');
  222.             die();
  223.         }
  224.     }
  225.     
  226.     function generateRandomString($length 10
  227.     {
  228.         $characters '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  229.         $charactersLength strlen($characters);
  230.         $randomString '';
  231.         for ($i 0$i $length$i++) {
  232.             $randomString .= $characters[rand(0$charactersLength 1)];
  233.         }
  234.         return $randomString;
  235.     }
  236.     /**
  237.      * @Route("/setAccountInfos/{email}/{username}/{first}/{last}/{pw}/{street}/{zip}/{city}/{birthday}/{telephone}/" , name="setAccountInfos", options={"expose"=true})
  238.      */
  239.     public function setAccountInfos($email$username$first$last$pw$street$zip$city$birthday$telephone)
  240.     {
  241.         if($this->get('security.token_storage')->getToken()->getUser() == 'anon.')
  242.         {
  243.             $response $this->forward('App\Controller\CourseController::homepage', []);
  244.             return $response;
  245.         }
  246.         include("./include/function.php");
  247.         
  248.         if($pw == "empty"){$pw "";}
  249.         if($first == "empty"){$first "";}
  250.         if($last == "empty"){$last "";}
  251.         if($street == "empty"){$street "";}
  252.         if($zip == "empty"){$zip 0;}
  253.         if($city == "empty"){$city "";}
  254.         
  255.         if(!is_numeric($zip)){$zip 0;}
  256.         if(trim(checkUserInput($username)) == "" || trim(checkUserInput($email)) == "" || trim(checkUserInput($birthday)) == ""){ return new Response('error2'); die(); }
  257.         
  258.         $pw checkUserInput(trim($pw));
  259.         
  260.         
  261.         if($pw != "")
  262.         {
  263.             if (strlen($pw) < 8) {
  264.                 return new Response('error3');die();
  265.             }
  266.             if (!preg_match("#[0-9]+#"$pw)) {
  267.                 return new Response('error4');die();
  268.             }
  269.             if (!preg_match("#[a-zA-Z]+#"$pw)) {
  270.                 return new Response('error5');die();
  271.             }  
  272.         }
  273.         $em $this->getDoctrine()->getManager();
  274.         $user $this->get('security.token_storage')->getToken()->getUser();
  275.         $repository $this->getDoctrine()->getRepository(User::class);
  276.         $user2 $repository->findOneBy(['u_username' => trim(checkUserInput($username))]);
  277.         if($user2
  278.         {
  279.             if($user2->getUUsername() != $user->getUUsername())
  280.             {
  281.                 return new Response('error1');
  282.                 die();
  283.             }
  284.         }
  285.         if(trim(checkUserInput($first)) != "")
  286.         {
  287.             $user->setUFirstName(checkUserInput($first));
  288.         }
  289.         if(trim(checkUserInput($last)) != "")
  290.         {
  291.             $user->setULastName(checkUserInput($last));
  292.         }
  293.         $user->setUUsername(checkUserInput($username));
  294.         $user->setUEmail(checkUserInput($email));
  295.         if($pw != "")
  296.         {
  297.             $user->setUPassword(password_hash($pw,  PASSWORD_BCRYPT ));
  298.         }
  299.         $user->setUStreet(checkUserInput($street));
  300.         $user->setUZip(checkUserInput($zip));
  301.         $user->setUCity(checkUserInput($city));
  302.         $user->setUTelephone(checkUserInput($telephone));
  303.         $d = new \DateTime(date('Y-m-d',strtotime(checkUserInput($birthday))));
  304.         $user->setUBirthday($d);
  305.       
  306.         $em->persist($user);
  307.         $em->flush();
  308.         return new Response('ok');die();
  309.     }
  310.     /**
  311.      * @Route("/checkNewUser/{mail}/{username}/{pw}/{birthday}/{first}/{last}/{street}/{zip}/{city}/{telephone}" , name="checkNewUser", options={"expose"=true})
  312.      */
  313.     public function checkNewUser($mail$username$pw$birthday$first$last$street,$zip$city$telephone )
  314.     {
  315.         include("./include/function.php");
  316.         
  317.         if(trim(checkUserInput($username)) == "" || trim(checkUserInput($pw)) == "" || trim(checkUserInput($mail)) == "" || trim(checkUserInput($birthday)) == "" ){ return new Response('error2'); die(); }
  318.         
  319.         $repository $this->getDoctrine()->getRepository(User::class);
  320.         $user $repository->findOneBy(['u_username' => trim(checkUserInput($username))]);
  321.         if($user
  322.         {
  323.             return new Response('error1');
  324.             die();
  325.         }
  326.         
  327.         $pw checkUserInput($pw);
  328.         
  329.         if (strlen($pw) < 8) {
  330.             return new Response('error3');die();
  331.         }
  332.         if (!preg_match("#[0-9]+#"$pw)) {
  333.             return new Response('error4');die();
  334.         }
  335.         if (!preg_match("#[a-zA-Z]+#"$pw)) {
  336.             return new Response('error5');die();
  337.         }  
  338.         
  339.         
  340.         
  341.         $em $this->getDoctrine()->getManager();
  342.         
  343.         if($first == '###'){$first "";}
  344.         if($last == '###'){$last "";}
  345.         if($street == '###'){$street "";}
  346.         if($zip == '###'){$zip 0;}
  347.         if($city == '###'){$city "";}
  348.         if($telephone == '###'){$telephone "";}
  349.         if(!is_numeric($zip)){$zip 0;}
  350.         
  351.         $d = new \DateTime(strtotime('Y-m-d H:i:s'));
  352.         
  353.         $user = new User();
  354.         $user->setUUsername($username);
  355.         $user->setUPassword(password_hash($pw,  PASSWORD_BCRYPT ));
  356.         $user->setUEmail(checkUserInput($mail));
  357.         $user->setURole(1);
  358.         $user->setULocked(0);
  359.         $user->setUFirstname(checkUserInput($first));
  360.         $user->setULastname(checkUserInput($last));
  361.         $user->setULastlogin($d);
  362.         $user->setUStreet(checkUserInput($street));
  363.         $user->setUCity(checkUserInput($city));
  364.         $user->setUZip(checkUserInput($zip));
  365.         $user->setUHouseadmin(0);
  366.         $user->setUTelephone(checkUserInput($telephone));
  367.         $user->setUCompany(0);
  368.         $d = new \DateTime(date('Y-m-d',strtotime(checkUserInput($birthday))));
  369.         $user->setUBirthday($d);
  370.         // tell Doctrine you want to (eventually) save the Product (no queries yet)
  371.         $em->persist($user);
  372.         // actually executes the queries (i.e. the INSERT query)
  373.         $em->flush();
  374.         $session $this->requestStack->getSession(); //$session = $this->get('session');
  375.         $placeid $session->get('place');
  376.         $log = new Userlog();
  377.         $log->setUlUid$user->getId());
  378.         $log->setUlSessionid($session->getId());
  379.         $log->setUlBrowser($_SERVER['HTTP_USER_AGENT']);
  380.         $log->setUlIp($_SERVER['REMOTE_ADDR']);
  381.         
  382.         $now = new \DateTime(date("Y-m-d H:i:s"));
  383.         $log->setUlNow($now);
  384.         if($placeid == NULL)
  385.         {
  386.             $log->setUlPlace(0);
  387.         }
  388.         else
  389.         {
  390.             $log->setUlPlace($placeid);
  391.         }
  392.         $log->setUlAction(4);
  393.         $log->setUlReferer($_SERVER['HTTP_REFERER']);
  394.         $log->setUlCurrentPage($_SERVER['REQUEST_URI']);
  395.         $em $this->getDoctrine()->getManager();
  396.         $em->persist($log);
  397.         $em->flush();
  398.         return new Response('ok');
  399.         
  400.     }
  401. }