src/Controller/HomeController.php line 135

Open in your IDE?
  1. <?php
  2. // src/Controller/HomeController.php
  3. namespace App\Controller;
  4. use App\Entity\Users;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. use App\Repository\MediaRepository;
  13. class HomeController extends AbstractController
  14. {
  15.     private $session;
  16.     private $mediaRepository;
  17.     public function __construct(SessionInterface $sessionRequestStack $requestStackMediaRepository $mediaRepository)
  18.     {
  19.         $this->session $requestStack->getSession();
  20.         $this->mediaRepository $mediaRepository;
  21.     }
  22.     /**
  23.      * @Route("/home/login", name="app_home_login")
  24.      */
  25.     public function login(Request $request): Response
  26.     {
  27.         $error '';
  28.         $email '';
  29.         if ($request->isMethod('POST')) {
  30.             $email $request->request->get('email');
  31.             $password $request->request->get('password');
  32.             $user $this->getDoctrine()->getRepository(Users::class)->findOneBy(['email' => $email]);
  33.             if (!$user) {
  34.                 $error 'Invalid email or password';
  35.             } elseif ($user->getPassword() !== sha1($password)) {
  36.                 $error 'Invalid email or password';
  37.             } else {
  38.                 $this->session->set('uid'$user->getId());
  39.                 return $this->redirectToRoute('app_dashboard');
  40.             }
  41.         }
  42.         return $this->render('home/login.html.twig', [
  43.             'error' => $error,
  44.             'email' => $email,
  45.         ]);
  46.     }
  47.     /**
  48.         * @Route("/home/jsonlogin", name="app_home_login")
  49.      */
  50.     public function jsonlogin(Request $request): Response
  51.     {
  52.         $error '';
  53.         $email '';
  54.         
  55.         if ($request->isMethod('POST')) {
  56.             $email $request->request->get('email');
  57.             $password $request->request->get('password');
  58.             $user $this->getDoctrine()->getRepository(Users::class)->findOneBy(['email' => $email]);
  59.             if (!$user) {
  60.                 $error 'Invalid email or password';
  61.             } elseif ($user->getPassword() !== sha1($password)) {
  62.                 $error 'Invalid email or password';
  63.             } else {
  64.                 $this->session->set('uid'$user->getId());
  65.                 // Return a JSON response upon successful login
  66.                 $response = [
  67.                     'success' => true,
  68.                     'message' => 'Login successful',
  69.                     'redirect' => $this->generateUrl('app_dashboard'), // Provide the route name
  70.                 ];
  71.                 return new JsonResponse($response);
  72.             }
  73.         }
  74.         // Return an error message as a JSON response if applicable
  75.         if (!empty($error)) {
  76.             $response = [
  77.                 'success' => false,
  78.                 'message' => $error,
  79.             ];
  80.             return new JsonResponse($responseResponse::HTTP_BAD_REQUEST);
  81.         }
  82.         // Return a JSON response for initial rendering
  83.         $response = [
  84.             'success' => true,
  85.             'message' => 'Render login form',
  86.             'email' => $email,
  87.         ];
  88.         return new JsonResponse($response);
  89.     }
  90.     /**
  91.         * @Route("/home/fetchobjects", name="app_home_login")
  92.      */
  93.     public function fetchobjects(): JsonResponse
  94.     {
  95.         $mediaObjects $this->mediaRepository->findByObjects('unity_model');
  96.         return $this->json($mediaObjects);
  97.     }
  98.     /**
  99.  * @Route("/home/storelogs", name="app_store_logs")
  100.  */
  101. public function storelogs(Request $request): JsonResponse
  102. {
  103.     $msg $request->query->get('msg');
  104.     
  105.     $newuser = new Logger();
  106.     $newuser->errmessage $msg;
  107.     $newuser->created date('Y-m-d');            
  108.                 $entityManager $this->getDoctrine()->getManager();
  109.                 $entityManager->persist($newuser);
  110.                 $entityManager->flush();
  111.     return $this->json($mediaObjects);
  112. }
  113.     /**
  114.      * @Route("/home/signup", name="app_home_signup")
  115.      */
  116.     public function signup(Request $request): Response
  117.     {
  118.         $exists $request->query->get('exists');
  119.         if ($exists !== null) {
  120.             $vars['exists'] = 1;
  121.         }
  122.         $emailFromFront $request->query->get('email');
  123.         $passFromFront $request->query->get('passowrd');
  124.         $vars['email'] = $emailFromFront;
  125.         $vars['pass'] = $passFromFront;
  126.         if ($request->isMethod('POST')) {
  127.             $checkuser $this->getDoctrine()->getRepository(Users::class)->findOneByEmail($request->request->get('email'));
  128.             if (!$checkuser) {
  129.                 $ip $_SERVER['REMOTE_ADDR'];
  130.                 $lookup "https://extreme-ip-lookup.com/json/".$ip."?key=k9efMu0Pi0rkCdJKFNWE";
  131.                 $json file_get_contents($lookup);
  132.                 $lookupArray json_decode($jsontrue);
  133.                 $ipName $lookupArray['ipName'];
  134.                 $checkCarrier $this->getDoctrine()->getRepository(Carriers::class)->findOneByCarrierUrl($ipName);
  135.                 if (!$checkCarrier) {
  136.                     $carrier = new Carriers();
  137.                     $carrier->setCarrierName($lookupArray['asnOrg']);
  138.                     $carrier->setCarrierUrl($ipName);
  139.                     $carrier->setCreated(new \DateTime());
  140.                     $entityManager $this->getDoctrine()->getManager();
  141.                     $entityManager->persist($carrier);
  142.                     $entityManager->flush();
  143.                 }
  144.                 $newuser = new Users();
  145.                 $newuser->setFirstName($request->request->get('firstname'));
  146.                 $newuser->setLastName($request->request->get('lastname'));
  147.                 $newuser->setEmail($request->request->get('email'));
  148.                 $newuser->setUsername($request->request->get('username'));
  149.                 $newuser->setPassword(sha1($request->request->get('password')));
  150.                 $newuser->setCarrier($ipName);
  151.                 $newuser->setDob(new \DateTime($request->request->get('dob')));
  152.                 $newuser->setGender($request->request->get('gender'));
  153.                 $newuser->setCity($request->request->get('city'));
  154.                 $newuser->setRegionStateProvince($request->request->get('region'));
  155.                 $newuser->setCountry($request->request->get('country'));
  156.                 $newuser->setCellphone($request->request->get('cellphone'));
  157.                 $newuser->setRegistered(new \DateTime());
  158.                 $newuser->setLastLogin(new \DateTime());
  159.                 $entityManager $this->getDoctrine()->getManager();
  160.                 $entityManager->persist($newuser);
  161.                 $entityManager->flush();
  162.                 $transport = (new Swift_SmtpTransport());
  163.                 //start the session for the new user
  164.                 $this->get('session')->set('uid'$newuser->getId());
  165.                 return $this->redirectToRoute('dashboard');
  166.             } else {
  167.                 return $this->redirectToRoute('signup', ['exists' => 1]);
  168.             }
  169.         }
  170.         return $this->render('home/signup.html.twig'$vars);
  171.     }
  172.     /**
  173.      * @Route("/home/session", name="app_home_session")
  174.      */
  175.     public function session(): Response
  176.     {
  177.         return $this->render('session/session_expired.html.twig');
  178.     }
  179.     
  180.     /**
  181.         * @Route("/home/api", name="app_api")
  182.      */
  183.     public function api(Request $request): Response
  184.     {
  185.         $error '';
  186.         $email 'ldev22@gmail.com';
  187.         print "API actions";
  188.         $user $this->getDoctrine()->getRepository(Users::class)->findOneBy(['email' => $email]);
  189.          
  190.         // Return an error message as a JSON response if applicable
  191.        
  192.         // Return a JSON response for initial rendering
  193.         $response = [
  194.             'success' => true,
  195.             'message' => 'Render login form',
  196.             'email' => $email,
  197.         ];
  198.         return new JsonResponse($response);
  199.     }
  200. }