<?php
namespace App\Manager;
use App\Entity\Adherent\MagasinCoordonnees;
use App\Entity\User\AdminUser;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use JetBrains\PhpStorm\NoReturn;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
class WsProgressManager
{
const USERNAME = "espadh@all";
const PASSWORD = "Mozart/AO@59!";
private ?object $tokenStorage;
private DateTime $today;
private string $url = "https://allwslanpp.groupeall.com:40443/Grall/";
public function __construct(
private readonly SessionInterface $session,
private readonly LoggerInterface $progressLogger,
ContainerInterface $container,
private readonly EntityManagerInterface $entityManager,
private readonly ParameterBagInterface $parameterBag
)
{
$env = $this->parameterBag->get('env');
$this->today = (new DateTime('now'));
if ($env === "prod" || $env === "dev") {
$this->url = "https://allws.groupeall.com:40443/Grall/";
} elseif ($env === "preprod") {
$this->url = "https://allwspp.groupeall.com:40443/Grall/";
}
$this->tokenStorage = $container->get('security.token_storage');
}
public function getProgressUsername(): string
{
return self::USERNAME;
}
/**
* @param $url
* @return void
* Set Url
*/
public function setUrl($url)
{
$this->url = $url;
}
public function getUrl(): string
{
return $this->url;
}
public function getCurrentUser()
{
if (is_null($this->tokenStorage->getToken()) || $this->tokenStorage->getToken() instanceof AnonymousToken || $this->tokenStorage->getToken()->getUser() instanceof AdminUser) {
return 'C9920';
}
return $this->tokenStorage->getToken()->getUser()->getOriginalKadherent();
}
public function auth()
{
$this->logout();
$datas = http_build_query([
'j_username' => self::USERNAME,
'j_password' => self::PASSWORD
]);
$ch = curl_init($this->url . 'static/auth/j_spring_security_check');
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/x-www-form-urlencoded']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $datas);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HEADER, 1);
$response = curl_exec($ch);
preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $response, $matches);
$this->progressLogger->info('Connexion', ['Connecté avec l\'adhérent : ' => $this->getCurrentUser()]);
$this->progressLogger->info('Response : ' . $response, ['Connecté avec l\'adhérent : ' => $this->getCurrentUser()]);
if (isset($matches[1]) && !empty($matches[1])) {
$this->session->set('wsProgress_Timer', new \DateTime('now'));
$this->session->set('wsProgress_Token', $matches[1][0]);
$this->progressLogger->info('Reception du token :' . $matches[1][0], ['Connecté avec l\'adhérent' => $this->getCurrentUser()]);
$this->moderationMode(true);
} else {
$this->progressLogger->info("Erreur : Pas de match pour créer la session", ['Connecté avec l\'adhérent' => $this->getCurrentUser()]);
return false;
}
}
public function logout()
{
$ch = curl_init($this->url . 'static/auth/j_spring_security_logout');
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/x-www-form-urlencoded']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIE, $this->session->get('wsProgress_Token'));
$deco = curl_exec($ch);
$returnHeader = curl_getinfo($ch);
$this->progressLogger->info("Deconnexion sur le token : " . $this->session->get('wsProgress_Token'), ['Connecté avec l\'adhérent' => $this->getCurrentUser()]);
$this->session->remove('wsProgress_Token');
$this->session->remove('wsProgress_Timer');
}
public function moderationMode(bool $moderation = true)
{
if ($moderation != $this->session->get('wsProgress_Moderation')) {
ini_set('memory_limit', '-1');
$moderation = ($moderation) ? "1" : "0";
$datas = json_encode(['moderation' => $moderation]);
$ch = curl_init($this->url . 'web/moderation');
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $datas);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_COOKIE, $this->session->get('wsProgress_Token'));
$response = json_decode(curl_exec($ch), true);
$returnHeader = curl_getinfo($ch);
if ($returnHeader['http_code'] == 302 && preg_match('/auth\/login.jsp/', $returnHeader['redirect_url'])) {
$this->progressLogger->info("Modération 302 : " . $this->session->get('wsProgress_Token'), [$this->getCurrentUser()]);
$this->auth();
curl_setopt($ch, CURLOPT_COOKIE, $this->session->get('wsProgress_Token'));
$response = json_decode(curl_exec($ch), true);
}
if (!empty($response)) {
if (isset($response['moderation']['moderation']) && !empty($response['moderation']['moderation'])) {
$this->session->set('wsProgress_Moderation', $response['moderation']['moderation']);
}
}
curl_close($ch);
}
}
/**
* @return string|void
*/
public function codeToCentrale(string $codeAdherent)
{
if (substr(strtolower($codeAdherent), 0, 1) == "c") {
return "ao";
} else if (substr(strtolower($codeAdherent), 0, 1) == "r" || substr(strtolower($codeAdherent), 0, 1) == "l") {
return "rev";
} else if (substr(strtolower($codeAdherent), 0, 1) == "b") {
return "be";
}
}
public function selectTenant(string $codeAdherent)
{
$datas = null;
switch ($this->codeToCentrale($codeAdherent)) {
case "ao":
$datas = json_encode(['tenant' => 'ao_fr']);
$tenant = 'ao_fr';
break;
case "be":
$datas = json_encode(['tenant' => 'ao_be']);
$tenant = 'ao_be';
break;
case "rev":
if (str_starts_with(strtoupper($codeAdherent), 'L')) {
$datas = json_encode(['tenant' => 'rev_lu']);
$tenant = 'rev_lu';
} else {
$datas = json_encode(['tenant' => 'rev_fr']);
$tenant = 'rev_fr';
}
break;
}
if ($this->session->get('wsProgress_Tenant') != $tenant) {
$this->progressLogger->info('Selection du tenant. Adherent : ' . $codeAdherent . ' => ' . $tenant, ['Connecté avec l\'adhérent' => $this->getCurrentUser()]);
$this->progressLogger->info('Selection du tenant.' . $this->url . 'web/tenant', ['Connecté avec l\'adhérent' => $this->getCurrentUser()]);
ini_set('memory_limit', '-1');
$ch = curl_init($this->url . 'web/tenant');
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $datas);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_COOKIE, $this->session->get('wsProgress_Token'));
$this->progressLogger->info('Select Tenant :' . $tenant . ' sur le token : ' . $this->session->get('wsProgress_Token'), ['Connecté avec l\'adhérent' => $this->getCurrentUser()]);
$response = json_decode(curl_exec($ch), true);
$returnHeader = curl_getinfo($ch);
if ($returnHeader['http_code'] == 302 && preg_match('/auth\/login.jsp/', $returnHeader['redirect_url'])) {
$this->progressLogger->info('http_code 302 aucun session => reconnexion', ['Connecté avec l\'adhérent' => $this->getCurrentUser()]);
$this->auth();
curl_setopt($ch, CURLOPT_COOKIE, $this->session->get('wsProgress_Token'));
$response = json_decode(curl_exec($ch), true);
}
$this->progressLogger->info('Response :' . json_encode($response), ['Connecté avec l\'adhérent' => $this->getCurrentUser()]);
if (!empty($response)) {
if (isset($response['tenant']['tenant']) && !empty($response['tenant']['tenant'])) {
$this->session->set('wsProgress_Tenant', $response['tenant']['tenant']);
}
}
curl_close($ch);
}
}
public function findBy(QueryProgress $queryProgress): array|\mixedjson_decode
{
$this->refreshSessionProgress();
ini_set('memory_limit', '-1');
$this->progressLogger->info('Requete : ' . $this->url . 'rest/Grall/' . $queryProgress->getTable() . '?filter=' . $queryProgress->getQuery(), ['token' => $this->session->get('wsProgress_Token'), 'Connecté avec l\'adhérent' => $this->getCurrentUser()]);
$ch = curl_init($this->url . 'rest/Grall/' . $queryProgress->getTable() . '?filter=' . $queryProgress->getQuery());
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_COOKIE, $this->session->get('wsProgress_Token'));
$datas = (new ResultatProgress($queryProgress->getTable(), curl_exec($ch), $queryProgress->getSelect()))->getResult();
$returnHeader = curl_getinfo($ch);
$this->progressLogger->info('Response : ' . json_encode($datas), ['token' => $this->session->get('wsProgress_Token'), 'Connecté avec l\'adhérent' => $this->getCurrentUser()]);
if ($returnHeader['http_code'] == 302 && preg_match('/auth\/login.jsp/', $returnHeader['redirect_url'])) {
$this->progressLogger->info('http_code 302 pas de session au moment de la requete => reconnexion', ['Connecté avec l\'adhérent' => $this->getCurrentUser()]);
$this->auth();
curl_setopt($ch, CURLOPT_COOKIE, $this->session->get('wsProgress_Token'));
$datas = (new ResultatProgress($queryProgress->getTable(), curl_exec($ch), $queryProgress->getSelect()))->getResult();
}
curl_close($ch);
return $datas;
}
/**
* @return mixed
*/
public
function findOneBy(QueryProgress $queryProgress)
{
return current($this->findBy($queryProgress));
}
/**
* @param $table
* @param $object
* @return array
*/
public
function update($table, $object)
{
$this->refreshSessionProgress();
$datas = (new ResultatProgress($table, $object))->arrayToProgress();
ini_set('memory_limit', '-1');
$ch = curl_init($this->url . 'rest/Grall/' . $table);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $datas);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_COOKIE, $this->session->get('wsProgress_Token'));
$datas = (new ResultatProgress($table, curl_exec($ch)))->getUpdateResult();
$returnHeader = curl_getinfo($ch);
if ($returnHeader['http_code'] == 302 && preg_match('/auth\/login.jsp/', $returnHeader['redirect_url'])) {
$this->auth();
curl_setopt($ch, CURLOPT_COOKIE, $this->session->get('wsProgress_Token'));
$datas = (new ResultatProgress($table, curl_exec($ch)))->getUpdateResult();
}
curl_close($ch);
return $datas;
}
public function insert($table, $object)
{
$this->refreshSessionProgress();
$datas = (new ResultatProgress($table, $object))->arrayToProgress();
ini_set('memory_limit', '-1');
$ch = curl_init($this->url . 'rest/Grall/' . $table);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $datas);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_COOKIE, $this->session->get('wsProgress_Token'));
$datas = (new ResultatProgress($table, curl_exec($ch)))->getUpdateResult();
$returnHeader = curl_getinfo($ch);
if ($returnHeader['http_code'] == 302 && preg_match('/auth\/login.jsp/', $returnHeader['redirect_url'])) {
$this->auth();
curl_setopt($ch, CURLOPT_COOKIE, $this->session->get('wsProgress_Token'));
$datas = (new ResultatProgress($table, curl_exec($ch)))->getUpdateResult();
}
curl_close($ch);
return $datas;
}
public function delete($table, $object)
{
$this->refreshSessionProgress();
$datas = (new ResultatProgress($table, $object))->arrayToProgress();
ini_set('memory_limit', '-1');
$ch = curl_init($this->url . 'rest/Grall/' . $table);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_POSTFIELDS, $datas);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_COOKIE, $this->session->get('wsProgress_Token'));
$datas = (new ResultatProgress($table, curl_exec($ch)))->getUpdateResult();
$returnHeader = curl_getinfo($ch);
if ($returnHeader['http_code'] == 302 && preg_match('/auth\/login.jsp/', $returnHeader['redirect_url'])) {
$this->auth();
curl_setopt($ch, CURLOPT_COOKIE, $this->session->get('wsProgress_Token'));
$datas = (new ResultatProgress($table, curl_exec($ch)))->getUpdateResult();
}
curl_close($ch);
return $datas;
}
/**
* @return string
*/
public
function getCookieId()
{
if ($this->session->has('wsProgress_Token')) {
return $this->session->get('wsProgress_Token');
} else {
$this->auth();
return $this->session->get('wsProgress_Token');
}
}
/**
* @return mixed|null
*/
public
function getCurrentTenant()
{
if (!empty($this->session->get('wsProgress_Tenant'))) {
return $this->session->get('wsProgress_Tenant');
} else {
return null;
}
}
public function updateDateMajProgressLines($datas)
{
$datas['D_MAJ'] = $this->today->format('Y-m-d');
$datas['O_MAJ'] = $this->getProgressUsername();
$datas['H_MAJ'] = strtotime($this->today->format('H:i:s'));
return $datas;
}
public function refreshSessionProgress(): void
{
if (!$this->session->has('wsProgress_Timer') || $this->getTotalMinutes($this->session->get('wsProgress_Timer')->diff($this->today)) >= 30) {
$this->auth();
$this->selectTenant($this->getCurrentUser());
}
}
private function getTotalMinutes($dateInterval): float|int
{
return ($dateInterval->d * 24 * 60) + ($dateInterval->h * 60) + $dateInterval->i;
}
}