src/Services/GeneralService.php line 330

Open in your IDE?
  1. <?php
  2. namespace EADPlataforma\Services;
  3. use Psr\Container\ContainerInterface;
  4. use Symfony\Component\HttpFoundation\RequestStack;
  5. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  6. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  7. use EADPlataforma\Entity\Session;
  8. use EADPlataforma\Enum\ServicesEnum;
  9. /**
  10.  * GeneralService
  11.  */
  12. class GeneralService
  13. {   
  14.     /**
  15.      * @var ContainerInterface
  16.      */
  17.     protected $container;
  18.     /**
  19.      * @var RequestStack
  20.      */
  21.     protected $requestStack;
  22.     /**
  23.      * @var UrlGeneratorInterface
  24.      */
  25.     protected $router;
  26.     /**
  27.      * @var Array
  28.      */
  29.     protected $eadToken;
  30.     /**
  31.      * @var Object
  32.      */
  33.     protected $dataTokens;
  34.     /**
  35.      * @var Array
  36.      */
  37.     protected $services = [];
  38.     /**
  39.      * @var SessionInterface
  40.      */
  41.     protected $sessionSym;
  42.     /**
  43.      * Constructor
  44.      *
  45.      * @param ContainerInterface $container
  46.      */
  47.     public function __construct(
  48.         ContainerInterface $container
  49.         RequestStack $requestStack
  50.         UrlGeneratorInterface $router,
  51.         SessionInterface $sessionSym
  52.     )
  53.     {
  54.         $this->container $container;
  55.         $this->requestStack $requestStack;
  56.         $this->router $router;
  57.         $this->sessionSym $sessionSym;
  58.         $this->eadToken $this->container->getParameter('ead-token');
  59.         $this->dataTokens json_decode($this->container->getParameter('ead-services'));
  60.     }
  61.     public function isSandbox()
  62.     {
  63.         return ($this->eadToken['sandbox'] == ServicesEnum::YES);
  64.     }
  65.     public function isDev()
  66.     {
  67.         return $this->container->getParameter('kernel.environment') == "dev";
  68.     }
  69.     public function getSessionSym()
  70.     {
  71.         return $this->sessionSym;
  72.     }
  73.     public function getTokenCron()
  74.     {
  75.         return $this->container->getParameter('ead-cron');
  76.     }
  77.     public function getServiceAccess(int $serviceType, ?int $sandbox ServicesEnum::NO)
  78.     {
  79.         $keyService "{$serviceType}{$sandbox}";
  80.         if(!empty($this->services[$keyService])){
  81.             return $this->services[$keyService];
  82.         }
  83.         if($this->isSandbox()){
  84.             $sandbox ServicesEnum::YES;
  85.         }
  86.         $mainServices = [
  87.             ServicesEnum::IP_API,
  88.             ServicesEnum::RDSTATION,
  89.             ServicesEnum::LOGIN,
  90.             ServicesEnum::BLING,
  91.         ];
  92.         if($serviceType == ServicesEnum::AWS_SECRET){
  93.             $this->services[$keyService] = json_decode(
  94.                 $this->container->getParameter('ead-sget')
  95.             );
  96.             return $this->services[$keyService];
  97.         }
  98.         if($sandbox != ServicesEnum::YES || $serviceType == ServicesEnum::MEET){
  99.             if($serviceType == ServicesEnum::AWS_DYNAMODB_GET){
  100.                 $this->services[$keyService] = json_decode(
  101.                     $this->container->getParameter('ead-dget')
  102.                 );
  103.                 return $this->services[$keyService];
  104.             }
  105.             if(!in_array($serviceType$mainServices)){
  106.                 $serviceSecretId ServicesEnum::SERVICES_NAME[$serviceType];
  107.                 $serviceSecretId "{$serviceSecretId}/production";
  108.                 $awsSecretsManager $this->getService("Aws\\AwsSecretsManager");
  109.                 $info $awsSecretsManager->getServiceInfo($serviceSecretId);
  110.                 if(!empty($info)){
  111.                     $this->services[$keyService] = $info;
  112.                     return $this->services[$keyService];
  113.                 }
  114.             }
  115.         }
  116.         if(!empty($this->dataTokens)){
  117.             if(isset($this->dataTokens->{$serviceType})){
  118.                 return $this->dataTokens->{$serviceType};
  119.             }
  120.         }
  121.         throw new \Exception("Token not found for service: {$serviceType}");
  122.         return;
  123.     }
  124.     public function getUserFromEADAdmin(
  125.         string $email,
  126.         string $password,
  127.         string $clientId,
  128.         string $ip,
  129.         string $userAgent,
  130.         string $host,
  131.         ?bool $json false
  132.     )
  133.     {
  134.         $info $this->getServiceAccess(ServicesEnum::LOGIN);
  135.         $data = [
  136.             "action" => "user",
  137.             "email" => $email,
  138.             "password" => $password,
  139.             "cliente_id" => $clientId,
  140.             "userAgent" => $userAgent,
  141.             "host" => $host,
  142.             "ip" => $ip,
  143.         ];
  144.         $data http_build_query($data);
  145.         $url "https://eadmin.eadplataforma.com/modulos/api/?{$data}";
  146.         $ch curl_init();
  147.         curl_setopt($chCURLOPT_URL$url);
  148.         curl_setopt($chCURLOPT_POST0);
  149.         curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  150.         curl_setopt($chCURLOPT_HTTPHEADER, [
  151.             'Authorization: Token token="' $info->token '"'
  152.         ]);
  153.         $response curl_exec($ch);
  154.         curl_close($ch);
  155.         
  156.         if (!$json){
  157.             $response json_decode($responsetrue);
  158.             if($this->isDev()){
  159.                 $discordService $this->getService('DiscordService');
  160.                 $discordService->setChannel('debug-login');
  161.                 
  162.                 $discordService->setMessage(json_encode($data));
  163.                 $discordService->sendDiscord();
  164.                 if(isset($response['usuario_id'])){
  165.                     $discordService->setMessage($response['usuario_id']);
  166.                     $discordService->sendDiscord();
  167.                 }else if(isset($response['message'])){
  168.                     $discordService->setMessage($response['message']);
  169.                     $discordService->sendDiscord();
  170.                 }
  171.             }
  172.         }
  173.         
  174.         return $response;
  175.     }
  176.     public function getUserFromEADAdminByID(
  177.         string $userId,
  178.         string $clientId,
  179.         string $ip,
  180.         string $userAgent,
  181.         string $host,
  182.         ?bool $json false
  183.     )
  184.     {
  185.         $info $this->getServiceAccess(ServicesEnum::LOGIN);
  186.         $data = [
  187.             "action" => "userById",
  188.             "userId" => $userId,
  189.             "cliente_id" => $clientId,
  190.             "userAgent" => $userAgent,
  191.             "host" => $host,
  192.             "ip" => $ip,
  193.         ];
  194.         $data http_build_query($data);
  195.         $url "https://eadmin.eadplataforma.com/modulos/api/?{$data}";
  196.         $ch curl_init();
  197.         curl_setopt($chCURLOPT_URL$url);
  198.         curl_setopt($chCURLOPT_POST0);
  199.         curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  200.         curl_setopt($chCURLOPT_HTTPHEADER, [
  201.             'Authorization: Token token="' $info->token '"'
  202.         ]);
  203.         $response curl_exec($ch);
  204.         curl_close($ch);
  205.         
  206.         if (!$json){
  207.             $response json_decode($responsetrue);
  208.             if($this->isDev()){
  209.                 $discordService $this->getService('DiscordService');
  210.                 $discordService->setChannel('debug-login');
  211.                 
  212.                 $discordService->setMessage(json_encode($data));
  213.                 $discordService->sendDiscord();
  214.                 if(isset($response['usuario_id'])){
  215.                     $discordService->setMessage($response['usuario_id']);
  216.                     $discordService->sendDiscord();
  217.                 }else if(isset($response['message'])){
  218.                     $discordService->setMessage($response['message']);
  219.                     $discordService->sendDiscord();
  220.                 }
  221.             }
  222.         }
  223.         
  224.         return $response;
  225.     }
  226.     public function getContainer()
  227.     {
  228.         return $this->container;
  229.     }
  230.     public function getRequest()
  231.     {
  232.         return $this->requestStack->getCurrentRequest();
  233.     }
  234.     public function getClientIp()
  235.     {
  236.         $request $this->getRequest();
  237.         if($request){
  238.             return $request->getClientIp();
  239.         }
  240.         return;
  241.     }
  242.     public function getHost()
  243.     {
  244.         $request $this->getRequest();
  245.         if($request){
  246.             return $request->getHost();
  247.         }
  248.         return;
  249.     }
  250.     public function getRouter()
  251.     {
  252.         return $this->router;
  253.     }
  254.     public function getPath()
  255.     {
  256.         return $this->container->getParameter('kernel.project_dir');
  257.     }
  258.     public function getPublicPath()
  259.     {
  260.         return "{$this->getPath()}/public/";
  261.     }
  262.     public function getAssetsPath()
  263.     {
  264.         return "{$this->getPath()}/assets/";
  265.     }
  266.     public function getService(string $serviceName)
  267.     {
  268.         $service $this->container->get("EADPlataforma\Services\\{$serviceName}");
  269.         return $service;
  270.     }
  271.     public function getUtil(string $utilName)
  272.     {
  273.         $util $this->container->get("EADPlataforma\Util\\{$utilName}");
  274.         return $util;
  275.     }
  276.     public function getSignatureHash(string $value)
  277.     {
  278.         return hash_hmac('sha256'$value$this->getTokenCron());
  279.     }
  280.     public function signData(string $value)
  281.     {
  282.         $signature $this->getSignatureHash($value);
  283.         return "{$value}.ead.{$signature}";
  284.     }
  285.     public function signDataWithExpiration(string $valueint $expirationTime 30)
  286.     {
  287.         $expires time() + $expirationTime;
  288.         $signature $this->getSignatureHash("{$value}.ead.{$expires}");
  289.         return "{$value}.ead.{$expires}.ead.{$signature}";
  290.     }
  291.     public function verifySignedDataExpire(string $hash)
  292.     {
  293.         try{
  294.             list($value$expires$signature) = explode('.ead.'$hash3);
  295.             if(time() >= $expires){
  296.                 return false
  297.             }
  298.             $valid hash_equals($this->getSignatureHash("{$value}.ead.{$expires}"), $signature);
  299.             return $valid $value false;
  300.         }catch(\Exception $e){
  301.         }
  302.         return false;
  303.     }
  304.     public function verifySignedData(string $hash)
  305.     {
  306.         try{
  307.             list($value$signature) = explode('.ead.'$hash2);
  308.             return hash_equals($this->getSignatureHash($value), $signature) ? $value false;
  309.         }catch(\Exception $e){
  310.         }
  311.         return false;
  312.     }
  313.     public function setCookieSign(string $cookieNamestring $value, ?int $time null)
  314.     {
  315.         $cookieValue $this->signData($value);
  316.         $cookieName "edp_{$cookieName}";
  317.         $request $this->requestStack->getCurrentRequest();
  318.         $host $request->headers->get('host');
  319.         $options = [
  320.             "expires" => $time,
  321.             "path" => "/",
  322.             "domain" => $host,
  323.             "secure" => true,
  324.             "httponly" => true,
  325.             "samesite" => "Lax",
  326.         ];
  327.         if(in_array($hostServicesEnum::DOMAIN_DEV)){
  328.             setcookie($cookieName$cookieValue$time'/'$host);
  329.             
  330.             return;
  331.         }
  332.         setcookie(
  333.             $cookieName,
  334.             $cookieValue,
  335.             $options
  336.         );
  337.         return;
  338.     }
  339.     function verifySignedCookie(string $cookieName): bool
  340.     {
  341.         $cookieName "edp_{$cookieName}";
  342.         $request $this->requestStack->getCurrentRequest();
  343.         $cookies $request->cookies;
  344.         if(!$cookies->has($cookieName)){
  345.             return false;
  346.         }
  347.         return $this->verifySignedData($cookies->get($cookieName));
  348.     }
  349.     public function setCookie($cookieName$value$time null, ?bool $useMd5 true)
  350.     {
  351.         if($useMd5){
  352.             $cookieName md5($cookieName);
  353.         }
  354.         $cookieName "edp_{$cookieName}";
  355.         $request $this->requestStack->getCurrentRequest();
  356.         $host $request->headers->get('host');
  357.         if(empty($time)){
  358.             $time time() + (10 365 24 60 60);
  359.         }
  360.         $options = [
  361.             "expires" => $time,
  362.             "path" => "/",
  363.             "domain" => $host,
  364.             "secure" => true,
  365.             "httponly" => true,
  366.             "samesite" => "Lax",
  367.         ];
  368.         if(in_array($hostServicesEnum::DOMAIN_DEV)){
  369.             setcookie($cookieName$value$time'/'$host);
  370.             return;
  371.         }
  372.         setcookie($cookieName$value$options);
  373.         return;
  374.     }
  375.     public function getCookie($cookieName, ?bool $useMd5 true)
  376.     {
  377.         if($useMd5){
  378.             $cookieName md5($cookieName);
  379.         }
  380.         $cookieName "edp_{$cookieName}";
  381.         $request $this->requestStack->getCurrentRequest();
  382.         $cookies $request->cookies;
  383.         if($cookies->has($cookieName)){
  384.             return $cookies->get($cookieName);
  385.         }
  386.         return;
  387.     }
  388.     public function deleteCookie($cookieName, ?bool $useMd5 true)
  389.     {
  390.         if($useMd5){
  391.             $cookieName md5($cookieName);
  392.         }
  393.         $cookieName "edp_{$cookieName}";
  394.         
  395.         $request $this->requestStack->getCurrentRequest();
  396.         $host $request->headers->get('host');
  397.         
  398.         setcookie($cookieNamenull, -1'/'$host); 
  399.     }
  400.     public function getCookieHashIdentify()
  401.     {
  402.         $cookieName 'hashcartoff';
  403.         $hashIdentify $this->getCookie($cookieName);
  404.         if(empty($hashIdentify)){
  405.             $hashIdentify md5(rand() . strtotime(date('Y-m-d H:i:s')));
  406.             $hashIdentify .= md5(rand() . password_hash($hashIdentifyPASSWORD_DEFAULT));
  407.             $this->setCookie($cookieName$hashIdentify);
  408.         }
  409.         return $hashIdentify;
  410.     }
  411.     public function generateUrl(string $routeName, ?array $params = [])
  412.     {
  413.         return $this->router->generate($routeName, (!empty($params) ? $params : []));
  414.     }
  415.     public function logoffWS(Session $sessionstring $clientId)
  416.     {
  417.         $url "https://metrics.eadplataforma.app/api/users/token/invalidate";
  418.         $curl curl_init();
  419.         curl_setopt($curlCURLOPT_URL$url);
  420.         curl_setopt($curlCURLOPT_TIMEOUT50);
  421.             
  422.         $headers = [
  423.             "Content-Type: application/json",
  424.             "Authorization: {$this->getTokenCron()}",
  425.         ];
  426.         $userToken md5($clientId) . md5($session->getId() . $session->getToken());
  427.         $data = [
  428.             "token" => $userToken,
  429.         ];
  430.         curl_setopt($curlCURLOPT_HTTPHEADER$headers);
  431.         curl_setopt($curlCURLOPT_CUSTOMREQUEST"POST");
  432.         curl_setopt($curlCURLOPT_POSTFIELDSjson_encode($data));
  433.         curl_setopt($curlCURLOPT_FOLLOWLOCATIONtrue);
  434.         curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
  435.         curl_setopt($curlCURLOPT_SSL_VERIFYPEERfalse);
  436.         
  437.         $response curl_exec($curl);
  438.         $error curl_error($curl);
  439.         curl_close($curl);
  440.         return;
  441.     }
  442. }