Saatavuuskyselyt: IP-organisaatio, siirrä API-tabiin + sähköpostien formatointi + tikettiviestivärit

- Lisää IP-organisaatio/ISP-kenttä saatavuuskyselyihin (ip-api.com haku)
- Siirrä saatavuuskyselyt-taulukko Asiakkaat-tabista API-asetussivulle
- Korjaa rivinvaihdot ja välilyönnit Zammad-sähköpostivastauksissa (white-space:pre-wrap)
- Korjaa quoted thread: plain-text viestit muunnetaan HTML:ksi oikein
- Tikettiviestiketjun värit selkeämmiksi (sininen=saapuva, vihreä=lähtevä)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-13 02:40:32 +02:00
parent 74380a3176
commit 8d5ef864f9
5 changed files with 57 additions and 46 deletions

28
api.php
View File

@@ -1227,10 +1227,21 @@ switch ($action) {
if (!$exists) {
$ip = getClientIp();
$hostname = @gethostbyaddr($ip) ?: '';
if ($hostname === $ip) $hostname = ''; // gethostbyaddr palauttaa IP:n jos ei löydy
if ($hostname === $ip) $hostname = '';
// Hae IP-osoitteen organisaatio/ISP ip-api.com:sta
$org = '';
try {
$ipApiUrl = "http://ip-api.com/json/{$ip}?fields=org,isp,as";
$ctx = stream_context_create(['http' => ['timeout' => 3]]);
$ipJson = @file_get_contents($ipApiUrl, false, $ctx);
if ($ipJson) {
$ipData = json_decode($ipJson, true);
$org = $ipData['org'] ?? $ipData['isp'] ?? '';
}
} catch (\Throwable $e) { /* IP-haku ei saa kaataa API:a */ }
_dbExecute(
"INSERT INTO availability_queries (company_id, osoite, postinumero, kaupunki, saatavilla, ip_address, hostname, user_agent, referer, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
"INSERT INTO availability_queries (company_id, osoite, postinumero, kaupunki, saatavilla, ip_address, hostname, org, user_agent, referer, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
[
$matchedCompany['id'],
$rawOsoite,
@@ -1239,6 +1250,7 @@ switch ($action) {
$found ? 1 : 0,
$ip,
$hostname,
$org,
substr($_SERVER['HTTP_USER_AGENT'] ?? '', 0, 500),
substr($_SERVER['HTTP_REFERER'] ?? '', 0, 500),
date('Y-m-d H:i:s'),
@@ -1267,7 +1279,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.hostname, 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.org, 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
@@ -5398,8 +5410,8 @@ switch ($action) {
$to = !empty($input['to']) ? trim($input['to']) : ($ticket['from_email'] ?? '');
$cc = !empty($input['cc']) ? trim($input['cc']) : '';
// Muunna uusi viesti HTML:ksi
$newMsgHtml = nl2br(htmlspecialchars($body, ENT_QUOTES, 'UTF-8'));
// Muunna uusi viesti HTML:ksi (säilytä rivinvaihdot ja välilyönnit)
$newMsgHtml = '<div style="white-space:pre-wrap;">' . htmlspecialchars($body, ENT_QUOTES, 'UTF-8') . '</div>';
// Rakenna viestiketju (quoted thread) vastaukseen
$messages = _dbFetchAll(
@@ -5411,6 +5423,10 @@ switch ($action) {
$sender = $msg['from_name'] ?: $msg['from_email'];
$date = date('d.m.Y H:i', strtotime($msg['timestamp']));
$msgBody = $msg['body'] ?: '';
// Jos viesti on plain text (ei HTML-tageja), muunna HTML:ksi
if ($msgBody !== '' && strip_tags($msgBody) === $msgBody) {
$msgBody = '<div style="white-space:pre-wrap;">' . htmlspecialchars($msgBody, ENT_QUOTES, 'UTF-8') . '</div>';
}
$quotedThread .= '<br><div style="padding-left:0.5em;border-left:2px solid #ccc;color:#555;">'
. '<small><strong>' . htmlspecialchars($sender) . '</strong> — ' . $date . '</small><br>'
. '<div>' . $msgBody . '</div>'