src/Service/LoggerHelper.php line 100

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Entity\LoggerService;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Psr\Log\LoggerInterface;
  6. use Symfony\Component\DependencyInjection\ContainerInterface;
  7. use Monolog\Logger;
  8. class LoggerHelper
  9. {
  10.     /**
  11.      * @var EntityManagerInterface
  12.      */
  13.     private $em;
  14.     /**
  15.      * @var LoggerInterface
  16.      */
  17.     private $logger;
  18.     /**
  19.      * @param EntityManagerInterface $em
  20.      * @param LoggerInterface $logger
  21.      * @param ContainerInterface $container
  22.      */
  23.     public function __construct(EntityManagerInterface $emLoggerInterface $loggerContainerInterface $container)
  24.     {
  25.         $this->em $em;
  26.         $this->logger $logger;
  27.     }
  28.     public function saveMessage(string $messagestring $typestring $description,string $code '500'string $url'')
  29.     {
  30.         $ip $this->getIpCliente();
  31.         $ip2 $this->getIpCliente2();
  32.         $ip = (filter_var($ipFILTER_VALIDATE_IP)) ? $ip $ip2;
  33.         $loggerService = new LoggerService();
  34.         $loggerService->setMessage($message)
  35.             ->setEventAt(new \DateTime('now'))
  36.             ->setIpAccess($ip)
  37.             ->setTypeEvent($type)
  38.             ->setDescription($description)
  39.             ->setCode($code)
  40.             ->setUrl($url)
  41.         ;
  42.         $this->em->persist($loggerService);
  43.         $this->em->flush();
  44.     }
  45.     public function saveLog(string $message, array $context = [], string $code null)
  46.     {
  47.         $ip $this->getIpCliente();
  48.         $ip2 $this->getIpCliente2();
  49.         $ip = (filter_var($ipFILTER_VALIDATE_IP)) ? $ip $ip2;
  50.         $context array_values($context);
  51.         $texto $context[1];
  52.         if(is_array($context[1])){
  53.             $texto implode(", ",$context[1]);
  54.         }
  55.         $loggerService = new LoggerService();
  56.         $loggerService->setMessage($message)
  57.             ->setEventAt(new \DateTime('now'))
  58.             ->setIpAccess($ip)
  59.             ->setTypeEvent($context[0])
  60.             ->setDescription($texto)
  61.             ->setCode($code)
  62.         ;
  63.         $repo $this->em->getRepository(LoggerService::class);
  64.         $this->em->persist($loggerService);
  65.         $this->em->flush();
  66.     }
  67.     public function getIpCliente(): string
  68.     {
  69.         if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  70.             if ('' != $_SERVER['HTTP_X_FORWARDED_FOR']) {
  71.                 $client_ip = (!empty($_SERVER['REMOTE_ADDR'])) ?
  72.                     $_SERVER['REMOTE_ADDR'] :
  73.                     ((!empty($_ENV['REMOTE_ADDR'])) ?
  74.                         $_ENV['REMOTE_ADDR'] :
  75.                         'unknown');
  76.                 // Los proxy's van añadiendo al final de esta cabecera
  77.                 // las direcciones ip que van "ocultando". Para localizar la ip real
  78.                 // del usuario se comienza a mirar por el principio hasta encontrar
  79.                 // una dirección ip que no sea del rango privado. En caso de no
  80.                 // encontrarse ninguna se toma como valor el REMOTE_ADDR
  81.                 $entries preg_split('/[, ]/'$_SERVER['HTTP_X_FORWARDED_FOR']);
  82.                 reset($entries);
  83.                 while (list(, $entry) = each($entries)) {
  84.                     $entry trim($entry);
  85.                     if (preg_match("/^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/"$entry$ip_list)) {
  86.                         // http://www.faqs.org/rfcs/rfc1918.html
  87.                         $private_ip = [
  88.                             '/^0\./',
  89.                             '/^127\.0\.0\.1/',
  90.                             '/^192\.168\..*/',
  91.                             '/^172\.((1[6-9])|(2[0-9])|(3[0-1]))\..*/',
  92.                             '/^10\..*/',
  93.                         ];
  94.                         $found_ip preg_replace($private_ip$client_ip$ip_list[1]);
  95.                         if ($client_ip != $found_ip) {
  96.                             $client_ip $found_ip;
  97.                             break;
  98.                         }
  99.                     }
  100.                 }
  101.             } else {
  102.                 $client_ip = (!empty($_SERVER['REMOTE_ADDR'])) ?
  103.                     $_SERVER['REMOTE_ADDR'] :
  104.                     ((!empty($_ENV['REMOTE_ADDR'])) ?
  105.                         $_ENV['REMOTE_ADDR'] :
  106.                         'unknown');
  107.             }
  108.         } else {
  109.             $client_ip $this->requestStack->getCurrentRequest()->getClientIps();
  110.         }
  111.         return $client_ip[0];
  112.     }
  113.     public function getIpCliente2(): string
  114.     {
  115.         if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
  116.             $_SERVER['REMOTE_ADDR'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
  117.             $_SERVER['HTTP_CLIENT_IP'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
  118.         }
  119.         $client = @$_SERVER['HTTP_CLIENT_IP'];
  120.         $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
  121.         $remote $_SERVER['REMOTE_ADDR'];
  122.         if (filter_var($clientFILTER_VALIDATE_IP)) {
  123.             $ip $client;
  124.         } elseif (filter_var($forwardFILTER_VALIDATE_IP)) {
  125.             $ip $forward;
  126.         } else {
  127.             $ip $remote;
  128.         }
  129.         return $ip;
  130.     }
  131.     /**
  132.      * @param $ip <p>ingresar <i> IP </i> a verificar</p>
  133.      * @return bool <p>retorna <i> booleano </i> según la validación por FILTER_VALIDATE_IP</p>
  134.      */
  135.     public function isIpValid(string $ip): bool
  136.     {
  137.         return (bool)filter_var($ipFILTER_VALIDATE_IP);
  138.     }
  139. }