68 lines
1.8 KiB
PHP
Executable File
68 lines
1.8 KiB
PHP
Executable File
<?php
|
|
// data.php - Servicio de datos corregido
|
|
require_once 'config.php';
|
|
|
|
// Configurar tiempo de ejecución
|
|
set_time_limit(10);
|
|
|
|
// Cabecera JSON
|
|
header('Content-Type: application/json');
|
|
header('Cache-Control: no-store, no-cache, must-revalidate');
|
|
header('Pragma: no-cache');
|
|
header('Access-Control-Allow-Origin: *');
|
|
|
|
// Usar caché de resultados completos (5 segundos)
|
|
$cache_file = IP_CACHE_DIR . '/last_result.json';
|
|
if (file_exists($cache_file) && (time() - filemtime($cache_file)) < 5) {
|
|
readfile($cache_file);
|
|
exit;
|
|
}
|
|
|
|
// Obtener conexiones y procesar
|
|
$connections = get_connected_ips();
|
|
$locations = [];
|
|
$ip_counts = [];
|
|
|
|
// Procesar cada conexión
|
|
foreach ($connections as $conn) {
|
|
if (!empty($conn['ip'])) {
|
|
$ip = filter_var($conn['ip'], FILTER_VALIDATE_IP);
|
|
|
|
// Solo procesar IPs válidas
|
|
if ($ip) {
|
|
// Contar IPs duplicadas
|
|
if (!isset($ip_counts[$ip])) {
|
|
$ip_counts[$ip] = 0;
|
|
}
|
|
$ip_counts[$ip]++;
|
|
|
|
// Solo procesar cada IP una vez
|
|
if ($ip_counts[$ip] === 1) {
|
|
$location = ip_to_location($ip);
|
|
if ($location) {
|
|
// Validar coordenadas
|
|
if ($location['lat'] !== null && $location['lon'] !== null) {
|
|
$locations[] = $location;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Agregar conteo de conexiones a cada ubicación
|
|
foreach ($locations as &$loc) {
|
|
$loc['connections'] = $ip_counts[$loc['ip']] ?? 1;
|
|
}
|
|
|
|
// Si no hay ubicaciones, devolver un array vacío
|
|
if (empty($locations)) {
|
|
$locations = [];
|
|
}
|
|
|
|
// Guardar y enviar resultado
|
|
$result = json_encode($locations);
|
|
file_put_contents($cache_file, $result);
|
|
echo $result;
|
|
?>
|