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