<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Contracts\Translation\TranslatorInterface;
use App\Security\User;
use App\Util\AccountTools;
use App\Util\Api;
use App\Util\Reporter;
use App\Util\TwoFactor;
use App\Util\AppLogger;
class SecurityController extends AbstractController
{
private $translator;
private $logger;
private $params;
private $session;
public function __construct(TranslatorInterface $translator, ParameterBagInterface $params, RequestStack $requestStack, AppLogger $logger)
{
$this->translator = $translator;
$this->params = $params;
$this->session = $requestStack->getSession();
$this->logger = $logger;
}
/**
* @Route("/login/{forceLoginTarget}", name="app_login", defaults={"forceLoginTarget"=""}, requirements={"forceLoginTarget"="|panel|webmail"})
*/
public function login(AuthenticationUtils $authenticationUtils, Request $request, $forceLoginTarget): Response
{
// if set by URL - get it from there
if($request->get('_locale')) {
$locale = $request->get('_locale');
}
else {
// if we have locale stored in cookie get it from there
$locale = $request->cookies->get(User::LOCALE_COOKIE_NAME);
}
if($locale == '') {
// no URL or cookie set locale, use default
// $locale = $this->translator->getLocale();
if(isset($_SERVER['APP_DEFAULT_LANGUAGE'])) {
$locale = $_SERVER['APP_DEFAULT_LANGUAGE'];
}
else {
$locale = 'pl';
}
}
$this->translator->setLocale($locale);
// maintenance mode?
$maintenanceMode = false;
if(isset($_SERVER['MAINTENANCE_MODE'])) {
if($_SERVER['MAINTENANCE_MODE'] == 'full') {
return $this->render('security/maintenance.html.twig');
}
if(($_SERVER['MAINTENANCE_MODE'] == 'panel') || ($_SERVER['MAINTENANCE_MODE'] == 'webmail')) {
$forceLoginTarget = $_SERVER['MAINTENANCE_MODE'];
$maintenanceMode = true;
}
}
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
$apiHealth = Api::checkSystemHealth();
$reporter = new Reporter($this->params);
$licenseStatus = -1;
if($apiHealth == 1) {
$licenseStatus = Api::checkSystemHealth('license');
if(Reporter::shouldSendReport()) {
if($reporter->sendPanelInfo()) {
$reporter->setAsSent();
}
else {
$reporter->setAsTriedNotSent();
}
}
}
$showForgotPasswordUrl = '';
// should we show the link to "forgot password" path?
if(isset($_SERVER['APP_SHOW_FORGOT_PASS'])) {
$showForgotPassword = true;
$showForgotPasswordUrl = $_SERVER['APP_SHOW_FORGOT_PASS']; // URL or keyword 'internal'
}
else {
$showForgotPassword = false;
}
$showLoginLanguage = true;
if(isset($_SERVER['APP_LOGIN_LANGUAGE_SHOW']) && ($_SERVER['APP_LOGIN_LANGUAGE_SHOW'] == 'false')) {
$showLoginLanguage = false;
}
$recaptchaAllowed = false;
$recaptchaSiteKey = '';
if(isset($_SERVER['APP_RECAPTCHA_ALLOWED'])) {
if($_SERVER['APP_RECAPTCHA_ALLOWED'] == 'true') {
$recaptchaAllowed = true;
if(isset($_SERVER['APP_RECAPTCHA_SITE_KEY'])) {
$recaptchaSiteKey = $_SERVER['APP_RECAPTCHA_SITE_KEY'];
}
}
}
// determine template name based on APP_CLIENT_NAME
$loginTemplate = 'security/login.html.twig';
if(isset($_SERVER['APP_CLIENT_NAME']) && $_SERVER['APP_CLIENT_NAME'] != '') {
$loginTemplate = 'security/login.' . $_SERVER['APP_CLIENT_NAME'] . '.html.twig';
}
return $this->render($loginTemplate, [
'last_username' => $lastUsername,
'error' => $error,
'language' => $locale,
'apiHealth' => $apiHealth,
'licenseStatus' => $licenseStatus,
'show_forgot_password' => $showForgotPassword,
'show_forgot_password_url' => $showForgotPasswordUrl,
'show_login_language' => $showLoginLanguage,
'force_login_target' => $forceLoginTarget,
'show_login_target' => isset($_SERVER['APP_LOGIN_TARGET']) ? false : true,
'default_signin_program' => isset($_SERVER['APP_DEFAULT_SIGNIN_PROGRAM']) ? $_SERVER['APP_DEFAULT_SIGNIN_PROGRAM'] : 'panel',
'maintenance_mode' => $maintenanceMode,
'recaptcha_allowed' => $recaptchaAllowed,
'recaptcha_site_key' => $recaptchaSiteKey,
]);
}
/**
* @Route("/logout", name="app_logout")
*/
public function logout()
{
// it will never be executed. Needed only for route
}
}