Просмотр исходного кода

adding country lookup for ip to click-tracking

Josef Straßl 1 месяц назад
Родитель
Сommit
6d2add814f
2 измененных файлов с 41 добавлено и 3 удалено
  1. 17 3
      click/index.php
  2. 24 0
      click/track.php

+ 17 - 3
click/index.php

@@ -12,6 +12,19 @@ $baseUrl = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : '
 
 $trackingLink = $baseUrl . 'track.php';
 
+function countryCodeToFlag($countryCode)
+{
+    if (!$countryCode || !preg_match('/^[A-Za-z]{2}$/', $countryCode)) {
+        return null;
+    }
+    $countryCode = strtoupper($countryCode);
+    $flag = '';
+    foreach (str_split($countryCode) as $letter) {
+        $flag .= mb_chr(0x1F1E6 + (ord($letter) - ord('A')), 'UTF-8');
+    }
+    return $flag;
+}
+
 $trackedKeys = [];
 if (is_file($keysFile) && is_readable($keysFile)) {
     $raw = file_get_contents($keysFile);
@@ -60,7 +73,7 @@ if (is_file($logFile) && is_readable($logFile)) {
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <title>Click Tracker – Admin</title>
     <style>
-        body { font-family: system-ui, sans-serif; margin: 1rem 2rem; max-width: 1200px; }
+        body { font-family: system-ui, sans-serif; margin: 1rem 2rem; max-width: 1800px; }
         h1 { margin-top: 0; }
         .form-group { margin-bottom: 1rem; }
         .form-group label { display: block; margin-bottom: 0.25rem; font-weight: 500; }
@@ -73,7 +86,8 @@ if (is_file($logFile) && is_readable($logFile)) {
         th { background: #f5f5f5; font-weight: 600; }
         tr:nth-child(even) { background: #fafafa; }
         .col-time { white-space: nowrap; }
-        .col-ua, .col-referrer, .col-target { max-width: 280px; word-break: break-all; }
+        .col-ua { max-width: 500px; word-break: break-all; }
+        .col-referrer, .col-target { max-width: 400px; word-break: break-all; }
         .empty { color: #666; font-style: italic; margin-top: 1rem; }
     </style>
 </head>
@@ -131,7 +145,7 @@ if (is_file($logFile) && is_readable($logFile)) {
                     <tr>
                         <td class="col-time"><?php echo htmlspecialchars($t['timestamp'] ?? '-'); ?></td>
                         <td><?php echo htmlspecialchars($t['key'] ?? '-'); ?></td>
-                        <td><?php echo htmlspecialchars($t['ip'] ?? '-'); ?><?php if (!empty($t['ip_forwarded'])) echo ' <small>(X-Forwarded: ' . htmlspecialchars($t['ip_forwarded']) . ')</small>'; ?></td>
+                        <td><?php $flag = countryCodeToFlag($t['country_code'] ?? null); ?><?php if ($flag): ?><span title="<?php echo htmlspecialchars($t['country_code']); ?>"><?php echo $flag; ?></span> <?php endif; ?><?php echo htmlspecialchars($t['ip'] ?? '-'); ?><?php if (!empty($t['ip_forwarded'])) echo ' <small>(X-Forwarded: ' . htmlspecialchars($t['ip_forwarded']) . ')</small>'; ?></td>
                         <td class="col-ua"><?php echo htmlspecialchars($t['user_agent'] ?? '-'); ?></td>
                         <td class="col-referrer"><?php echo htmlspecialchars($t['referrer'] ?? '-'); ?></td>
                         <td class="col-target"><?php echo htmlspecialchars($t['target_url'] ?? '-'); ?></td>

+ 24 - 0
click/track.php

@@ -16,6 +16,29 @@ $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
 $forwarded = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : '';
 $realIp = isset($_SERVER['HTTP_X_REAL_IP']) ? $_SERVER['HTTP_X_REAL_IP'] : '';
 
+function lookupCountryCode($ip)
+{
+    if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
+        return null;
+    }
+    $context = stream_context_create(['http' => ['timeout' => 2]]);
+    $response = @file_get_contents(
+        'http://ip-api.com/json/' . urlencode($ip) . '?fields=status,countryCode',
+        false,
+        $context
+    );
+    if ($response === false) {
+        return null;
+    }
+    $data = json_decode($response, true);
+    if (!is_array($data) || ($data['status'] ?? '') !== 'success') {
+        return null;
+    }
+    return $data['countryCode'] ?? null;
+}
+
+$countryCode = lookupCountryCode($ip);
+
 $key = isset($_GET['key']) && $_GET['key'] !== '' ? trim($_GET['key']) : null;
 
 if ($key === 'KEY') {
@@ -30,6 +53,7 @@ $payload = [
     'ip' => $ip,
     'ip_forwarded' => $forwarded !== '' ? $forwarded : null,
     'ip_real' => $realIp !== '' ? $realIp : null,
+    'country_code' => $countryCode,
     'user_agent' => isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null,
     'referrer' => isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] !== '' ? $_SERVER['HTTP_REFERER'] : null,
     'request_uri' => isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : null,