diff --git a/api.php b/api.php index 0a07f68..24459e7 100644 --- a/api.php +++ b/api.php @@ -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 diff --git a/db.php b/db.php index 55bd96e..2764390 100644 --- a/db.php +++ b/db.php @@ -675,6 +675,7 @@ function initDatabase(): void { "ALTER TABLE ticket_rules ADD COLUMN set_tags VARCHAR(255) DEFAULT '' AFTER set_priority", "ALTER TABLE tickets ADD COLUMN zammad_ticket_id INT DEFAULT NULL AFTER mailbox_id", "ALTER TABLE ticket_messages ADD COLUMN zammad_article_id INT DEFAULT NULL AFTER message_id", + "ALTER TABLE availability_queries ADD COLUMN hostname VARCHAR(255) DEFAULT '' AFTER ip_address", ]; foreach ($alters as $sql) { try { $db->query($sql); } catch (\Throwable $e) { /* sarake on jo olemassa / jo ajettu */ } diff --git a/index.html b/index.html index 8da7295..fd7d19d 100644 --- a/index.html +++ b/index.html @@ -4,7 +4,7 @@