<?php
namespace EADPlataforma\Services;
use Psr\Container\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use EADPlataforma\Entity\Session;
use EADPlataforma\Enum\ServicesEnum;
/**
* GeneralService
*/
class GeneralService
{
/**
* @var ContainerInterface
*/
protected $container;
/**
* @var RequestStack
*/
protected $requestStack;
/**
* @var UrlGeneratorInterface
*/
protected $router;
/**
* @var Array
*/
protected $eadToken;
/**
* @var Object
*/
protected $dataTokens;
/**
* @var Array
*/
protected $services = [];
/**
* Constructor
*
* @param ContainerInterface $container
*/
public function __construct(
ContainerInterface $container,
RequestStack $requestStack,
UrlGeneratorInterface $router
)
{
$this->container = $container;
$this->requestStack = $requestStack;
$this->router = $router;
$this->eadToken = $this->container->getParameter('ead-token');
$this->dataTokens = json_decode($this->container->getParameter('ead-services'));
}
public function isSandbox()
{
return ($this->eadToken['sandbox'] == ServicesEnum::YES);
}
public function getTokenCron()
{
return $this->container->getParameter('ead-cron');
}
public function getServiceAccess(int $serviceType, ?int $sandbox = ServicesEnum::NO)
{
$keyService = "{$serviceType}{$sandbox}";
if(!empty($this->services[$keyService])){
return $this->services[$keyService];
}
if($this->isSandbox()){
$sandbox = ServicesEnum::YES;
}
$mainServices = [
ServicesEnum::IP_API,
ServicesEnum::RDSTATION,
ServicesEnum::LOGIN,
];
if($serviceType == ServicesEnum::AWS_SECRET){
$this->services[$keyService] = json_decode(
$this->container->getParameter('ead-sget')
);
return $this->services[$keyService];
}
if($sandbox != ServicesEnum::YES){
if($serviceType == ServicesEnum::AWS_DYNAMODB_GET){
$this->services[$keyService] = json_decode(
$this->container->getParameter('ead-dget')
);
return $this->services[$keyService];
}
if(!in_array($serviceType, $mainServices)){
$serviceSecretId = ServicesEnum::SERVICES_NAME[$serviceType];
$serviceSecretId = "{$serviceSecretId}/production";
$awsSecretsManager = $this->getService("Aws\\AwsSecretsManager");
$info = $awsSecretsManager->getServiceInfo($serviceSecretId);
if(!empty($info)){
$this->services[$keyService] = $info;
return $this->services[$keyService];
}
}
}
if(!empty($this->dataTokens)){
if(isset($this->dataTokens->{$serviceType})){
return $this->dataTokens->{$serviceType};
}
}
throw new \Exception("Token not found for service: {$serviceType}");
return;
}
public function getUserFromEADAdmin($email, $clientId, $json = false){
$info = $this->getServiceAccess(ServicesEnum::LOGIN);
$data = [
"action" => "user",
"email" => $email,
"cliente_id" => $clientId,
];
$data = http_build_query($data);
$url = "https://eadmin.eadplataforma.com/modulos/api/?{$data}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Token token="' . $info->token . '"'
]);
$response = curl_exec($ch);
curl_close($ch);
if (!$json){
$response = json_decode($response, true);
}
return $response;
}
public function getContainer()
{
return $this->container;
}
public function getRequest()
{
return $this->requestStack->getCurrentRequest();
}
public function getClientIp()
{
$request = $this->getRequest();
if($request){
return $request->getClientIp();
}
return;
}
public function getRouter()
{
return $this->router;
}
public function getPath()
{
return $this->container->getParameter('kernel.project_dir');
}
public function getPublicPath()
{
return "{$this->getPath()}/public/";
}
public function getAssetsPath()
{
return "{$this->getPath()}/assets/";
}
public function getService(string $serviceName)
{
$service = $this->container->get("EADPlataforma\Services\\{$serviceName}");
return $service;
}
public function getUtil(string $utilName)
{
$util = $this->container->get("EADPlataforma\Util\\{$utilName}");
return $util;
}
public function setCookie($cookieName, $value, $time = null, ?bool $useMd5 = true)
{
if($useMd5){
$cookieName = md5($cookieName);
}
$cookieName = "edp_{$cookieName}";
$request = $this->requestStack->getCurrentRequest();
$host = $request->headers->get('host');
if(empty($time)){
$time = time() + (10 * 365 * 24 * 60 * 60);
}
$options = [
"expires" => $time,
"path" => "/",
"domain" => $host,
"secure" => true,
"httponly" => true,
"samesite" => "None",
];
// setcookie($cookieName, $value, $options);
setcookie($cookieName, $value, $time, '/', $host);
}
public function getCookie($cookieName, ?bool $useMd5 = true)
{
if($useMd5){
$cookieName = md5($cookieName);
}
$cookieName = "edp_{$cookieName}";
$request = $this->requestStack->getCurrentRequest();
$cookies = $request->cookies;
if($cookies->has($cookieName)){
return $cookies->get($cookieName);
}
return;
}
public function deleteCookie($cookieName, ?bool $useMd5 = true)
{
if($useMd5){
$cookieName = md5($cookieName);
}
$cookieName = "edp_{$cookieName}";
$request = $this->requestStack->getCurrentRequest();
$host = $request->headers->get('host');
setcookie($cookieName, null, -1, '/', $host);
}
public function getCookieHashIdentify()
{
$cookieName = 'hashcartoff';
$hashIdentify = $this->getCookie($cookieName);
if(empty($hashIdentify)){
$hashIdentify = md5(rand() . strtotime(date('Y-m-d H:i:s')));
$hashIdentify .= md5(rand() . password_hash($hashIdentify, PASSWORD_DEFAULT));
$this->setCookie($cookieName, $hashIdentify);
}
return $hashIdentify;
}
public function generateUrl(string $routeName, ?array $params = [])
{
return $this->router->generate($routeName, (!empty($params) ? $params : []));
}
public function logoffWS(Session $session, string $clientId)
{
$url = "https://metrics.eadplataforma.app/ws/user/token/invalidate";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_TIMEOUT, 50);
$headers = [
"Accept: application/json",
"Authorization: {$this->getTokenCron()}",
];
$userToken = md5($clientId) . md5($session->getId() . $session->getToken());
$data = [
"token" => $userToken,
];
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
$error = curl_error($curl);
curl_close($curl);
return;
}
}