Naprawa lokalizacji - odczytu

This commit is contained in:
2026-06-10 20:31:48 +02:00
parent 04aaa6e321
commit 79a83d4d73
16 changed files with 1826 additions and 477 deletions
+22
View File
@@ -0,0 +1,22 @@
<?php
header('Content-Type: application/json; charset=utf-8');
require_once __DIR__ . '/request_ip.php';
if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
http_response_code(405);
echo json_encode([
'status' => 'error',
'message' => 'Method not allowed',
], JSON_UNESCAPED_UNICODE);
exit;
}
$clientIp = getRequestClientIp();
$bypass = isGeoBypassTrustedIp($clientIp);
echo json_encode([
'status' => 'success',
'bypassGeo' => $bypass,
'clientIp' => $clientIp,
], JSON_UNESCAPED_UNICODE);
+94
View File
@@ -0,0 +1,94 @@
<?php
/**
* Adres IP klienta (pierwszy z X-Forwarded-For lub REMOTE_ADDR).
*/
function getRequestClientIp(): string
{
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$parts = explode(',', (string) $_SERVER['HTTP_X_FORWARDED_FOR']);
return trim($parts[0]);
}
return trim((string) ($_SERVER['REMOTE_ADDR'] ?? ''));
}
/**
* Pojedyncze IP z pominięciem geo (zewnętrzne, dev, przykładowe hosty LAN).
*/
function getGeoBypassTrustedIps(): array
{
return [
'82.160.190.247',
'127.0.0.1',
'::1',
'192.168.20.84',
'10.0.0.3',
'10.0.0.7',
];
}
/**
* Pule wewnętrznych sieci — goście widziani przez lokalny serwer (REMOTE_ADDR z LAN).
*/
function getGeoBypassTrustedCidrs(): array
{
return [
'10.0.0.0/24',
];
}
function normalizeClientIp(string $ip): string
{
if (strpos($ip, '::ffff:') === 0) {
return substr($ip, 7);
}
return $ip;
}
function ipv4InCidr(string $ip, string $cidr): bool
{
if (!str_contains($cidr, '/')) {
return false;
}
[$subnet, $bits] = explode('/', $cidr, 2);
$bits = (int) $bits;
if ($bits < 0 || $bits > 32) {
return false;
}
$ipLong = ip2long($ip);
$subnetLong = ip2long($subnet);
if ($ipLong === false || $subnetLong === false) {
return false;
}
$mask = $bits === 0 ? 0 : (-1 << (32 - $bits)) & 0xFFFFFFFF;
return ($ipLong & $mask) === ($subnetLong & $mask);
}
function isGeoBypassTrustedIp(string $ip): bool
{
$ip = normalizeClientIp($ip);
if ($ip === '') {
return false;
}
if (in_array($ip, getGeoBypassTrustedIps(), true)) {
return true;
}
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
foreach (getGeoBypassTrustedCidrs() as $cidr) {
if (ipv4InCidr($ip, $cidr)) {
return true;
}
}
}
return false;
}
+77
View File
@@ -0,0 +1,77 @@
<?php
header('Content-Type: application/json; charset=utf-8');
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/message_text_helper.php';
if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
http_response_code(405);
echo json_encode([
'status' => 'error',
'message' => 'Method not allowed',
], JSON_UNESCAPED_UNICODE);
exit;
}
try {
$pdo = getAnalyticsPdo();
$stmt = $pdo->query("
SELECT
id,
table_id,
message_type,
message_text,
otwierajacy_imie,
otwierajacy_nazwisko,
status_kds,
api_sent,
created_at,
updated_at
FROM guest_action_queue
WHERE DATE(created_at) = CURDATE()
ORDER BY created_at DESC
LIMIT 500
");
$rows = $stmt->fetchAll();
$pending = 0;
$waiterCalls = 0;
$billRequests = 0;
foreach ($rows as &$row) {
$row['id'] = (int) $row['id'];
$row['status_kds'] = (int) $row['status_kds'];
$row['api_sent'] = (int) $row['api_sent'];
$row['message_text'] = normalizeQueueMessageText((string) ($row['message_text'] ?? ''));
if ($row['status_kds'] === 0) {
$pending++;
}
if ($row['message_type'] === 'waiter_call') {
$waiterCalls++;
} elseif ($row['message_type'] === 'bill_request') {
$billRequests++;
}
}
unset($row);
echo json_encode([
'status' => 'success',
'date' => date('Y-m-d'),
'count' => count($rows),
'summary' => [
'pending' => $pending,
'waiter_calls' => $waiterCalls,
'bill_requests' => $billRequests,
],
'polled_at' => date('Y-m-d H:i:s'),
'poll_interval_seconds' => 15,
'data' => $rows,
], JSON_UNESCAPED_UNICODE);
} catch (Throwable $e) {
http_response_code(500);
echo json_encode([
'status' => 'error',
'message' => 'Waiter feed fetch failed',
], JSON_UNESCAPED_UNICODE);
}