Saatavuuskyselyt: IP/hostname, duplikaattien esto

- Reverse DNS -haku tallentaa hostnamen IP:n rinnalle (paljastaa
  operaattorin ja alueen, esim. dsl-hel-123.elisa.fi)
- Duplikaattikyselyn (sama osoite+postinumero+kaupunki) ei tallenneta
  uudelleen samalle yritykselle
- IP/hostname -sarake lisätty taulukkoon

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-13 02:26:16 +02:00
parent 64dc02f432
commit 74380a3176
4 changed files with 38 additions and 19 deletions

45
api.php
View File

@@ -1215,23 +1215,36 @@ switch ($action) {
}
}
// Tallenna kysely tietokantaan
// Tallenna kysely tietokantaan (ohita duplikaatit: sama osoite+postinumero+kaupunki+yritys)
try {
_dbExecute(
"INSERT INTO availability_queries (company_id, osoite, postinumero, kaupunki, saatavilla, ip_address, user_agent, referer, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
[
$matchedCompany['id'],
$_GET['osoite'] ?? '',
$_GET['postinumero'] ?? '',
$_GET['kaupunki'] ?? '',
$found ? 1 : 0,
getClientIp(),
substr($_SERVER['HTTP_USER_AGENT'] ?? '', 0, 500),
substr($_SERVER['HTTP_REFERER'] ?? '', 0, 500),
date('Y-m-d H:i:s'),
]
$rawOsoite = $_GET['osoite'] ?? '';
$rawPostinumero = $_GET['postinumero'] ?? '';
$rawKaupunki = $_GET['kaupunki'] ?? '';
$exists = _dbFetchScalar(
"SELECT COUNT(*) FROM availability_queries WHERE company_id = ? AND LOWER(osoite) = LOWER(?) AND postinumero = ? AND LOWER(kaupunki) = LOWER(?)",
[$matchedCompany['id'], $rawOsoite, $rawPostinumero, $rawKaupunki]
);
if (!$exists) {
$ip = getClientIp();
$hostname = @gethostbyaddr($ip) ?: '';
if ($hostname === $ip) $hostname = ''; // gethostbyaddr palauttaa IP:n jos ei löydy
_dbExecute(
"INSERT INTO availability_queries (company_id, osoite, postinumero, kaupunki, saatavilla, ip_address, hostname, user_agent, referer, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
[
$matchedCompany['id'],
$rawOsoite,
$rawPostinumero,
$rawKaupunki,
$found ? 1 : 0,
$ip,
$hostname,
substr($_SERVER['HTTP_USER_AGENT'] ?? '', 0, 500),
substr($_SERVER['HTTP_REFERER'] ?? '', 0, 500),
date('Y-m-d H:i:s'),
]
);
}
} catch (\Throwable $e) { /* logitus ei saa kaataa API-vastausta */ }
echo json_encode(['saatavilla' => $found]);
@@ -1254,7 +1267,7 @@ switch ($action) {
$total = (int)_dbFetchScalar("SELECT COUNT(*) FROM availability_queries WHERE company_id IN ($placeholders)", $userCompanyIds);
$params = array_merge($userCompanyIds, [$limit, $offset]);
$rows = _dbFetchAll(
"SELECT aq.id, aq.company_id, c.nimi as company_nimi, aq.osoite, aq.postinumero, aq.kaupunki, aq.saatavilla, aq.ip_address, aq.referer, aq.created_at
"SELECT aq.id, aq.company_id, c.nimi as company_nimi, aq.osoite, aq.postinumero, aq.kaupunki, aq.saatavilla, aq.ip_address, aq.hostname, aq.referer, aq.created_at
FROM availability_queries aq LEFT JOIN companies c ON c.id = aq.company_id
WHERE aq.company_id IN ($placeholders) ORDER BY aq.created_at DESC LIMIT ? OFFSET ?",
$params