2022-07-21 16:15:47 +02:00
|
|
|
<?php
|
|
|
|
|
2022-09-23 16:14:15 +02:00
|
|
|
namespace App\Service;
|
|
|
|
|
2022-07-21 16:15:47 +02:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2022-09-23 16:14:15 +02:00
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
2022-07-21 16:15:47 +02:00
|
|
|
use Symfony\Component\HttpFoundation\RequestStack;
|
2022-09-23 16:14:15 +02:00
|
|
|
use Symfony\Component\HttpKernel\Event\RequestEvent;
|
2022-07-21 16:15:47 +02:00
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
|
|
|
|
2022-09-23 16:14:15 +02:00
|
|
|
class AppSession
|
|
|
|
{
|
2022-07-21 16:15:47 +02:00
|
|
|
private $container;
|
|
|
|
protected $em;
|
|
|
|
protected $requeststack;
|
|
|
|
protected $token;
|
|
|
|
|
|
|
|
public function __construct(ContainerInterface $container, EntityManagerInterface $em, RequestStack $requeststack, TokenStorageInterface $token)
|
|
|
|
{
|
|
|
|
$this->container = $container;
|
|
|
|
$this->requeststack = $requeststack;
|
|
|
|
$this->em = $em;
|
|
|
|
$this->token = $token;
|
|
|
|
}
|
|
|
|
|
2022-09-23 16:14:15 +02:00
|
|
|
public function onDomainParse(RequestEvent $event)
|
|
|
|
{
|
2022-07-21 16:15:47 +02:00
|
|
|
$session = $this->requeststack->getSession();
|
|
|
|
|
|
|
|
$configs = $this->em->getRepository("App\Entity\Config")->findAll();
|
2022-09-23 16:14:15 +02:00
|
|
|
foreach ($configs as $config) {
|
2022-07-21 16:15:47 +02:00
|
|
|
$session->set($config->getId(), strval($config->getValue()));
|
|
|
|
}
|
2022-09-23 16:14:15 +02:00
|
|
|
$session->set('headerimage', 'header/'.$session->get('headerimage'));
|
2022-07-21 16:15:47 +02:00
|
|
|
|
|
|
|
// Calcul couleur
|
2022-09-23 16:14:15 +02:00
|
|
|
$session->set('colorbgbodylight-darker', $this->adjustBrightness($session->get('colorbgbodylight'), -10));
|
|
|
|
$session->set('colorfttitlelight-darker', $this->adjustBrightness($session->get('colorfttitlelight'), -50));
|
2022-07-21 16:15:47 +02:00
|
|
|
|
2022-09-23 16:14:15 +02:00
|
|
|
$session->set('colorbgbodydark-darker', $this->adjustBrightness($session->get('colorbgbodydark'), -50));
|
|
|
|
$session->set('colorbgbodydark-lighter', $this->adjustBrightness($session->get('colorbgbodydark'), +50));
|
|
|
|
|
|
|
|
$session->set('colorbgbodydark-rgb', $this->hexToRgb($session->get('colorbgbodydark')));
|
|
|
|
$session->set('colorbgbodydark-darkrgb', $this->hexToRgb($session->get('colorbgbodydark-darker')));
|
|
|
|
$session->set('colorbgbodydark-lightrgb', $this->hexToRgb($session->get('colorbgbodydark-lighter')));
|
2022-07-21 16:15:47 +02:00
|
|
|
|
|
|
|
// Current user
|
|
|
|
$token = $this->token->getToken();
|
2022-09-23 16:14:15 +02:00
|
|
|
if (!$token) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$curentuser = $token->getUser();
|
|
|
|
|
|
|
|
// Préférence par défaut
|
|
|
|
$session->set('fgheader', true);
|
|
|
|
$session->set('fgaudit', false);
|
2022-07-21 16:15:47 +02:00
|
|
|
|
|
|
|
// Préférence
|
2022-09-23 16:14:15 +02:00
|
|
|
if ('anon.' != $curentuser) {
|
|
|
|
$preference = $curentuser->getPreference();
|
|
|
|
if (is_array($preference)) {
|
2022-07-21 16:15:47 +02:00
|
|
|
// Préférence header
|
2022-09-23 16:14:15 +02:00
|
|
|
if (array_key_exists('fgheader', $preference)) {
|
|
|
|
$fgheader = ('true' == $preference['fgheader'][0]);
|
|
|
|
$session->set('fgheader', $fgheader);
|
2022-07-21 16:15:47 +02:00
|
|
|
}
|
2022-07-26 16:03:53 +02:00
|
|
|
|
|
|
|
// Préférence audit
|
2022-09-23 16:14:15 +02:00
|
|
|
if (array_key_exists('fgaudit', $preference)) {
|
|
|
|
$fgaudit = ('true' == $preference['fgaudit'][0]);
|
|
|
|
$session->set('fgaudit', $fgaudit);
|
|
|
|
}
|
2022-07-21 16:15:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Permissions
|
2022-09-23 16:14:15 +02:00
|
|
|
$showannuaire = false;
|
|
|
|
$submitgroup = false;
|
|
|
|
if ('anon.' != $curentuser) {
|
|
|
|
switch ($session->get('permannu')) {
|
|
|
|
case 'ROLE_USER':
|
|
|
|
$showannuaire = ($curentuser->hasRole('ROLE_ADMIN') || $curentuser->hasRole('ROLE_MODO') || $curentuser->hasRole('ROLE_MASTER') || $curentuser->hasRole('ROLE_USER'));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'ROLE_MASTER':
|
|
|
|
$showannuaire = ($curentuser->hasRole('ROLE_ADMIN') || $curentuser->hasRole('ROLE_MODO') || $curentuser->hasRole('ROLE_MASTER'));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'ROLE_MODO':
|
|
|
|
$showannuaire = ($curentuser->hasRole('ROLE_ADMIN') || $curentuser->hasRole('ROLE_MODO'));
|
|
|
|
break;
|
2022-07-21 16:15:47 +02:00
|
|
|
}
|
|
|
|
|
2022-09-23 16:14:15 +02:00
|
|
|
switch ($session->get('permgroup')) {
|
|
|
|
case 'ROLE_USER':
|
|
|
|
$submitgroup = ($curentuser->hasRole('ROLE_ADMIN') || $curentuser->hasRole('ROLE_MODO') || $curentuser->hasRole('ROLE_MASTER') || $curentuser->hasRole('ROLE_USER'));
|
|
|
|
break;
|
2022-07-21 16:15:47 +02:00
|
|
|
|
2022-09-23 16:14:15 +02:00
|
|
|
case 'ROLE_MASTER':
|
|
|
|
$submitgroup = ($curentuser->hasRole('ROLE_ADMIN') || $curentuser->hasRole('ROLE_MODO') || $curentuser->hasRole('ROLE_MASTER'));
|
|
|
|
break;
|
2022-07-21 16:15:47 +02:00
|
|
|
|
2022-09-23 16:14:15 +02:00
|
|
|
case 'ROLE_MODO':
|
|
|
|
$submitgroup = ($curentuser->hasRole('ROLE_ADMIN') || $curentuser->hasRole('ROLE_MODO'));
|
|
|
|
break;
|
2022-07-21 16:15:47 +02:00
|
|
|
}
|
|
|
|
}
|
2022-09-23 16:14:15 +02:00
|
|
|
$session->set('showannuaire', $showannuaire);
|
|
|
|
$session->set('submitgroup', $submitgroup);
|
2022-07-21 16:15:47 +02:00
|
|
|
|
2022-09-23 16:14:15 +02:00
|
|
|
// Visite
|
|
|
|
if ('anon.' != $curentuser) {
|
|
|
|
$now = new \DateTime();
|
|
|
|
if (!$curentuser->getVisitedate()) {
|
2022-07-21 16:15:47 +02:00
|
|
|
$curentuser->setVisitedate($now);
|
2022-09-23 16:14:15 +02:00
|
|
|
$curentuser->setVisitecpt($curentuser->getVisitecpt() + 1);
|
2022-07-21 16:15:47 +02:00
|
|
|
$this->em->persist($curentuser);
|
2022-09-23 16:14:15 +02:00
|
|
|
$this->em->flush();
|
|
|
|
} else {
|
|
|
|
$visitedate = clone $curentuser->getVisitedate();
|
|
|
|
$visitedate->add(new \DateInterval('PT1H'));
|
|
|
|
if ($visitedate < $now) {
|
2022-07-21 16:15:47 +02:00
|
|
|
$curentuser->setVisitedate($now);
|
2022-09-23 16:14:15 +02:00
|
|
|
$curentuser->setVisitecpt($curentuser->getVisitecpt() + 1);
|
2022-07-21 16:15:47 +02:00
|
|
|
$this->em->persist($curentuser);
|
2022-09-23 16:14:15 +02:00
|
|
|
$this->em->flush();
|
2022-07-21 16:15:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-23 16:14:15 +02:00
|
|
|
private function adjustBrightness($hex, $steps)
|
|
|
|
{
|
2022-07-21 16:15:47 +02:00
|
|
|
// Steps should be between -255 and 255. Negative = darker, positive = lighter
|
|
|
|
$steps = max(-255, min(255, $steps));
|
2022-09-23 16:14:15 +02:00
|
|
|
|
2022-07-21 16:15:47 +02:00
|
|
|
// Normalize into a six character long hex string
|
|
|
|
$hex = str_replace('#', '', $hex);
|
2022-09-23 16:14:15 +02:00
|
|
|
if (3 == strlen($hex)) {
|
|
|
|
$hex = str_repeat(substr($hex, 0, 1), 2).str_repeat(substr($hex, 1, 1), 2).str_repeat(substr($hex, 2, 1), 2);
|
2022-07-21 16:15:47 +02:00
|
|
|
}
|
2022-09-23 16:14:15 +02:00
|
|
|
|
2022-07-21 16:15:47 +02:00
|
|
|
// Split into three parts: R, G and B
|
|
|
|
$color_parts = str_split($hex, 2);
|
|
|
|
$return = '';
|
2022-09-23 16:14:15 +02:00
|
|
|
|
2022-07-21 16:15:47 +02:00
|
|
|
foreach ($color_parts as $color) {
|
2022-09-23 16:14:15 +02:00
|
|
|
$color = hexdec($color); // Convert to decimal
|
|
|
|
$color = max(0, min(255, $color + $steps)); // Adjust color
|
2022-07-21 16:15:47 +02:00
|
|
|
$return .= str_pad(dechex($color), 2, '0', STR_PAD_LEFT); // Make two char hex code
|
|
|
|
}
|
2022-09-23 16:14:15 +02:00
|
|
|
|
2022-07-21 16:15:47 +02:00
|
|
|
return '#'.$return;
|
2022-09-23 16:14:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function hexToRgb($hex)
|
|
|
|
{
|
|
|
|
$hex = str_replace('#', '', $hex);
|
|
|
|
$length = strlen($hex);
|
|
|
|
$rgb['r'] = hexdec(6 == $length ? substr($hex, 0, 2) : (3 == $length ? str_repeat(substr($hex, 0, 1), 2) : 0));
|
|
|
|
$rgb['g'] = hexdec(6 == $length ? substr($hex, 2, 2) : (3 == $length ? str_repeat(substr($hex, 1, 1), 2) : 0));
|
|
|
|
$rgb['b'] = hexdec(6 == $length ? substr($hex, 4, 2) : (3 == $length ? str_repeat(substr($hex, 2, 1), 2) : 0));
|
|
|
|
|
|
|
|
return $rgb['r'].','.$rgb['g'].','.$rgb['b'];
|
|
|
|
}
|
2022-07-21 16:15:47 +02:00
|
|
|
}
|