<?php
namespace App\EventSubscriber;
use App\Controller\SecurePageInterface;
use Doctrine\ORM\EntityManagerInterface;
use Empire\Core\Core;
use Exception;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Routing\RequestContext;
use Twig\Environment;
class CoreEventsSubscriber implements EventSubscriberInterface
{
private $request;
private $session;
private $requestStack;
private $resolver;
private $logger;
public function __construct (
EntityManagerInterface $entityManager,
RequestStack $request,
RequestContext $rc,
Environment $twig,
RequestStack $requestStack,
LoggerInterface $logger
//ControllerResolver $resolver
) {
Core::setDB($entityManager->getConnection());
//$this->connection = $entityManager->getConnection();
Core::set('version', '8.4.3' . trim(file_exists(EMPIRE_WEBROOT . '/VERSION') ? '-'. file_get_contents(EMPIRE_WEBROOT . '/VERSION') : ''));
$base_url = $rc->getScheme() . "://" . $rc->getHost() . "/" . $rc->getBaseUrl();
$this->request = $request->getCurrentRequest();
Core::set('base_url', $base_url);
$this->requestStack = $requestStack;
$this->session = $this->requestStack->getSession();
Core::setSession($this->session);
Core::setTwig($twig);
//$this->resolver = $resolver;
$this->logger = $logger;
Core::set('discord.guild', "503020279136649250");
Core::set('discord.token', "NTAzMTc1OTE5MDA4MjE5MTQ2.DqyrLQ.d4W-wNoNGUORjnudyx0bdmJkle8");
Core::set('discord.clientid', "503175919008219146");
Core::set('discord.clientse', "0coE9mZSGpOXkPZkiL5rOOwDL4WcfTZN");
Core::set('discord.botname', "Bot"); // Name of the ROLE in discord server
Core::set('discord.postINB', true); // Keep disabled for now
Core::set('discord.dev.inb.webhookUrl', "https://discord.com/api/webhooks/1086305402221772951/shyH8ajqj7PnDrV3cWiLK3zorihe-rmVyPc14UZL2RZCRpuhUnR15S5qPHZ-UIdtsCne");
Core::set('discord.dev.inb.avatarUrl', 'https://swc-empire.com/assets/uploads/image_26_MmM3ZGM0M2_empire-logo-large.png');
Core::set('discord.dev.inb.username', 'DEV Imperial News Bureau');
Core::set('discord.membersLinkEnabled', false); // If set to false: Individual Overwrite via Access-Key 'Discord-Release'
Core::set('discord.imperialRole', "887246214549090365");
Core::set('discord.tier1', "888688878431662110");
Core::set('discord.tier2', "888688794621083669");
Core::set('discord.tier3', "888688698424696883");
Core::set('discord.tier4', "888688420463980605");
Core::set('combine.dev.clientid', "65b86ccb18fe01e20e39f10236574a5ae5c56127");
Core::set('combine.dev.clientse', "77a8cda2e4e0218a4c116a6c0c11fda7b7646e35");
Core::set('combine.prod.clientid', "4d2974503f6f59966386378879b18467734ff054");
Core::set('combine.prod.clientse', "94895db6fb9ee1846a3242118d1217e0ae84b5be");
}
/**
* @inheritDoc
* @return Array
*/
public static function getSubscribedEvents()
{
return [
KernelEvents::CONTROLLER => [['pathInfo', 50],['loginSetup', 99], ['loginCheck', 0]]
];
}
public function pathInfo(ControllerEvent $event) {
Core::set('fullPage', explode("~",$this->request->getPathInfo())[0]);
Core::set('secure', false);
}
public function loginSetup(ControllerEvent $event) {
$request = $this->request;
$this->logger->info('irms_token: ' . $request->cookies->get('irms_token') );
$this->logger->info('member_id: ' . $this->session->get('irms_token') );
if($request->cookies->get('irms_token') && !$this->session->get('member_id')) {
try {
\Empire\Core\Login::cookieLogin($request->cookies->get('irms_token'));
} catch (Exception $ex) {
$this->session->getFlashBag()->add('error', 'While attempting to remember your login, the following error occurred: ' . $ex->getMessage());
}
}
if($request->request->get('login')) {
try {
$getLogin = \Empire\Core\Login::processLogin($request);
} catch (Exception $ex) {
$getLogin = false;
$this->session->getFlashBag()->add('error', 'While attempting to login, the following error occured: ' . $ex->getMessage());
}
if ($getLogin) {
if($request->request->get('rememberMe', false)) {
try {
\Empire\Core\Login::setLoginCookie();
} catch (Exception $e) {
$this->session->getFlashBag()->add('error', $e->getMessage());
}
}
} else {
$this->session->getFlashBag()->add('error', 'Unable to log you in.');
}
}
Core::setUser(\Empire\Core\Login::getLoggedIn());
$redirectUrl = $request->request->get('redirectUrl');
if ($redirectUrl != null ) {
$event->setController(function() use ($redirectUrl) {
return new RedirectResponse(urldecode($redirectUrl));
});
}
$referer = $request->headers->get('referer');
if (!is_null($referer)) {
return new RedirectResponse($referer);
}
}
public function loginCheck(ControllerEvent $event) {
$controller = $event->getController();
if(is_array($controller)) $controller = $controller[0];
if($controller instanceof SecurePageInterface) {
$resp = Core::loginCheck();
if($resp instanceof Response) {
$event->setController(function () use ($resp) {
return $resp;
});
}
Core::set('secure', true);
}
}
}