<?php
namespace App\Controller;
use App\Entity\IpOk;
use App\Helper\Tools;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
class SecurityController extends AbstractController
{
/**
* @Route("/login", name="app_login")
* @Route("/login/{slug}", name="app_login_slug")
*/
public function login(AuthenticationUtils $authenticationUtils, SessionInterface $session, $slug = false): Response
{
if ($_SERVER['HTTP_HOST'] == 'share4.kelkii.com' || $_SERVER['HTTP_HOST'] == 'share.kelkii.com') {
header("Location: https://www.kelkii.com/");
exit;
}
if ($slug) {
$em = $this->getDoctrine()->getManager();
$ipok = $em->getRepository(IpOk::class)->findOneBy(['slug' => $slug, 'ip' => $_SERVER['REMOTE_ADDR']]);
if(!$ipok) {
$this->get('session')->getFlashBag()->add('error', '404 dude !');
return $this->redirectToRoute('app_login');
}
$ipok->setUpdateDate(new \DateTime());
$ipok->setValid(1);
$em->persist($ipok);
$em->flush();
return $this->redirectToRoute('app_login');
}
$timeout = 0;
if ($this->getUser()) {
return $this->redirectToRoute('homepage');
}
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
if($error) {
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
Tools::sendErrorToKelkii($actual_link, urlencode('Erreur de connexion pour l\'utilisateur '.$lastUsername), Tools::getUserIpAddr());
$timeout = $session->get('login-error-timeout') + 10;
$session->set('login-error-timeout', $timeout);
sleep($timeout);
} else {
$session->set('login-error-timeout', 0);
}
return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error, 'timeout' => $timeout]);
}
/**
* @Route("/logout", name="app_logout")
*/
public function logout(): void
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
}