src/Services/GeneralService.php line 307

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