| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- <?php
- /**
- * Admin page: generate tracking links and view all captured clicks.
- */
- $dataDir = __DIR__ . '/data';
- $logFile = $dataDir . '/tracks.jsonl';
- $keysFile = $dataDir . '/keys.json';
- $baseUrl = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http')
- . '://' . ($_SERVER['HTTP_HOST'] ?? 'localhost')
- . rtrim(dirname($_SERVER['SCRIPT_NAME'] ?? '/'), '/') . '/';
- $trackingLink = $baseUrl . 'track.php';
- $trackedKeys = [];
- if (is_file($keysFile) && is_readable($keysFile)) {
- $raw = file_get_contents($keysFile);
- $decoded = json_decode($raw, true);
- if (is_array($decoded)) {
- $trackedKeys = $decoded;
- }
- }
- if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'create_key') {
- $existing = array_flip($trackedKeys);
- do {
- $newKey = bin2hex(random_bytes(8));
- } while (isset($existing[$newKey]));
- $trackedKeys[] = $newKey;
- if (!is_dir($dataDir)) {
- mkdir($dataDir, 0755, true);
- }
- file_put_contents($keysFile, json_encode($trackedKeys, JSON_PRETTY_PRINT), LOCK_EX);
- }
- $maxTracks = 100;
- $tracks = [];
- if (is_file($logFile) && is_readable($logFile)) {
- $lines = file($logFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
- if ($lines !== false) {
- if (count($lines) > $maxTracks) {
- $lines = array_slice($lines, -$maxTracks);
- file_put_contents($logFile, implode("\n", $lines) . "\n", LOCK_EX);
- }
- $reversed = array_reverse($lines);
- foreach ($reversed as $line) {
- $decoded = json_decode($line, true);
- if (is_array($decoded)) {
- $tracks[] = $decoded;
- }
- }
- }
- }
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <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; }
- h1 { margin-top: 0; }
- .form-group { margin-bottom: 1rem; }
- .form-group label { display: block; margin-bottom: 0.25rem; font-weight: 500; }
- .form-group input[type="url"] { width: 100%; max-width: 480px; padding: 0.5rem; box-sizing: border-box; }
- .generated { margin: 1rem 0; padding: 0.75rem; background: #e8f5e9; border-radius: 4px; word-break: break-all; }
- .generated a { color: #2e7d32; }
- .error { color: #c62828; margin: 1rem 0; }
- table { border-collapse: collapse; width: 100%; margin-top: 1.5rem; font-size: 0.9rem; }
- th, td { border: 1px solid #ccc; padding: 0.5rem 0.75rem; text-align: left; vertical-align: top; }
- 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; }
- .empty { color: #666; font-style: italic; margin-top: 1rem; }
- </style>
- </head>
- <body>
- <h1>Click Tracker – Admin</h1>
- <section>
- <h2>Tracking link</h2>
- <p class="generated">Use this link to capture clicks (redirects here after): <a href="<?php echo htmlspecialchars($trackingLink); ?>"><?php echo htmlspecialchars($trackingLink); ?></a></p>
- </section>
- <section>
- <h2>Tracked links (by key)</h2>
- <p>Create a unique key to get a link that identifies which link was clicked. Clicks are logged with the key.</p>
- <form method="post" action="">
- <input type="hidden" name="action" value="create_key">
- <button type="submit">Create unique key</button>
- </form>
- <?php if (!empty($trackedKeys)): ?>
- <?php $linkTemplate = $baseUrl . 'track.php?key=KEY'; ?>
- <ul style="margin-top: 1rem; list-style: none; padding: 0;">
- <?php foreach (array_reverse($trackedKeys) as $k): ?>
- <li style="margin-bottom: 0.5rem; padding: 0.5rem; background: #f5f5f5; border-radius: 4px;">
- <code style="font-size: 0.85em;"><?php echo htmlspecialchars($k); ?></code>
- <span style="margin-left: 0.5rem; word-break: break-all; color: #555;"><?php echo htmlspecialchars($linkTemplate); ?></span>
- </li>
- <?php endforeach; ?>
- </ul>
- <p class="empty" style="margin-top: 0.5rem;">Replace <code>KEY</code> in the URL with the key above to construct your link.</p>
- <?php else: ?>
- <p class="empty">No tracked keys yet. Click the button above to create one.</p>
- <?php endif; ?>
- </section>
- <section>
- <h2>Past tracks</h2>
- <?php if (empty($tracks)): ?>
- <p class="empty">No tracks yet.</p>
- <?php else: ?>
- <table>
- <thead>
- <tr>
- <th class="col-time">Time</th>
- <th>Key</th>
- <th>IP</th>
- <th class="col-ua">User-Agent</th>
- <th class="col-referrer">Referrer</th>
- <th class="col-target">Target URL</th>
- <th>Method</th>
- <th>Request URI</th>
- </tr>
- </thead>
- <tbody>
- <?php foreach ($tracks as $t): ?>
- <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 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>
- <td><?php echo htmlspecialchars($t['request_method'] ?? '-'); ?></td>
- <td><?php echo htmlspecialchars($t['request_uri'] ?? '-'); ?></td>
- </tr>
- <?php endforeach; ?>
- </tbody>
- </table>
- <?php endif; ?>
- </section>
- </body>
- </html>
|