<?php
namespace App\Service;
use App\Entity\LoggerService;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Monolog\Logger;
class LoggerHelper
{
/**
* @var EntityManagerInterface
*/
private $em;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @param EntityManagerInterface $em
* @param LoggerInterface $logger
* @param ContainerInterface $container
*/
public function __construct(EntityManagerInterface $em, LoggerInterface $logger, ContainerInterface $container)
{
$this->em = $em;
$this->logger = $logger;
}
public function saveMessage(string $message, string $type, string $description,string $code = '500', string $url= '')
{
$ip = $this->getIpCliente();
$ip2 = $this->getIpCliente2();
$ip = (filter_var($ip, FILTER_VALIDATE_IP)) ? $ip : $ip2;
$loggerService = new LoggerService();
$loggerService->setMessage($message)
->setEventAt(new \DateTime('now'))
->setIpAccess($ip)
->setTypeEvent($type)
->setDescription($description)
->setCode($code)
->setUrl($url)
;
$this->em->persist($loggerService);
$this->em->flush();
}
public function saveLog(string $message, array $context = [], string $code = null)
{
$ip = $this->getIpCliente();
$ip2 = $this->getIpCliente2();
$ip = (filter_var($ip, FILTER_VALIDATE_IP)) ? $ip : $ip2;
$context = array_values($context);
$texto = $context[1];
if(is_array($context[1])){
$texto = implode(", ",$context[1]);
}
$loggerService = new LoggerService();
$loggerService->setMessage($message)
->setEventAt(new \DateTime('now'))
->setIpAccess($ip)
->setTypeEvent($context[0])
->setDescription($texto)
->setCode($code)
;
$repo = $this->em->getRepository(LoggerService::class);
$this->em->persist($loggerService);
$this->em->flush();
}
public function getIpCliente(): string
{
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
if ('' != $_SERVER['HTTP_X_FORWARDED_FOR']) {
$client_ip = (!empty($_SERVER['REMOTE_ADDR'])) ?
$_SERVER['REMOTE_ADDR'] :
((!empty($_ENV['REMOTE_ADDR'])) ?
$_ENV['REMOTE_ADDR'] :
'unknown');
// Los proxy's van añadiendo al final de esta cabecera
// las direcciones ip que van "ocultando". Para localizar la ip real
// del usuario se comienza a mirar por el principio hasta encontrar
// una dirección ip que no sea del rango privado. En caso de no
// encontrarse ninguna se toma como valor el REMOTE_ADDR
$entries = preg_split('/[, ]/', $_SERVER['HTTP_X_FORWARDED_FOR']);
reset($entries);
while (list(, $entry) = each($entries)) {
$entry = trim($entry);
if (preg_match("/^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/", $entry, $ip_list)) {
// http://www.faqs.org/rfcs/rfc1918.html
$private_ip = [
'/^0\./',
'/^127\.0\.0\.1/',
'/^192\.168\..*/',
'/^172\.((1[6-9])|(2[0-9])|(3[0-1]))\..*/',
'/^10\..*/',
];
$found_ip = preg_replace($private_ip, $client_ip, $ip_list[1]);
if ($client_ip != $found_ip) {
$client_ip = $found_ip;
break;
}
}
}
} else {
$client_ip = (!empty($_SERVER['REMOTE_ADDR'])) ?
$_SERVER['REMOTE_ADDR'] :
((!empty($_ENV['REMOTE_ADDR'])) ?
$_ENV['REMOTE_ADDR'] :
'unknown');
}
} else {
$client_ip = $this->requestStack->getCurrentRequest()->getClientIps();
}
return $client_ip[0];
}
public function getIpCliente2(): string
{
if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
$_SERVER['REMOTE_ADDR'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
$_SERVER['HTTP_CLIENT_IP'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
}
$client = @$_SERVER['HTTP_CLIENT_IP'];
$forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote = $_SERVER['REMOTE_ADDR'];
if (filter_var($client, FILTER_VALIDATE_IP)) {
$ip = $client;
} elseif (filter_var($forward, FILTER_VALIDATE_IP)) {
$ip = $forward;
} else {
$ip = $remote;
}
return $ip;
}
/**
* @param $ip <p>ingresar <i> IP </i> a verificar</p>
* @return bool <p>retorna <i> booleano </i> según la validación por FILTER_VALIDATE_IP</p>
*/
public function isIpValid(string $ip): bool
{
return (bool)filter_var($ip, FILTER_VALIDATE_IP);
}
}