/** * ====== ARCHIVE SYSTEM ====== */ /** * Get config (helper for archive system) */ function getConfig(): array { if (!defined('CONFIG_FILE')) return []; if (!file_exists(CONFIG_FILE)) return ['aff_id'=>'','site_name'=>'Promo Shopee']; return json_decode(file_get_contents(CONFIG_FILE), true) ?: ['aff_id'=>'','site_name'=>'Promo Shopee']; } /** * Save config (helper for archive system) */ function saveConfig(array $c): void { if (!defined('CONFIG_FILE')) return; file_put_contents(CONFIG_FILE, json_encode($c, JSON_PRETTY_PRINT)); } /** * Load visits (helper) */ function loadVisits(): array { if (!defined('VISITS_FILE')) return []; if (!file_exists(VISITS_FILE)) return []; $d = json_decode(file_get_contents(VISITS_FILE), true); return is_array($d) ? $d : []; } /** * Load stats.json */ function loadStats(): array { $statsFile = DATA_DIR . '/stats.json'; if (!file_exists($statsFile)) { return [ 'daily' => [], 'all_time' => [ 'total_visits' => 0, 'bot_visits' => 0, 'user_visits' => 0, 'headless' => 0, 'meta_asn' => 0, 'campaigns' => [], 'links' => [], 'top_ips' => [] ] ]; } return json_decode(file_get_contents($statsFile), true) ?: []; } /** * Save stats.json */ function saveStats(array $stats): void { $statsFile = DATA_DIR . '/stats.json'; file_put_contents($statsFile, json_encode($stats, JSON_PRETTY_PRINT)); } /** * Update daily stats with visit data */ function updateDailyStats(array $visits, string $date): void { if (empty($visits)) return; $stats = loadStats(); // Calculate stats from visits $totalVisits = count($visits); $botVisits = count(array_filter($visits, fn($v) => $v['is_bot'] ?? false)); $userVisits = $totalVisits - $botVisits; $headless = count(array_filter($visits, fn($v) => $v['is_headless'] ?? false)); $metaASN = count(array_filter($visits, fn($v) => $v['is_meta_asn'] ?? false)); // Top IPs $ipCounts = []; foreach ($visits as $v) { $ip = $v['ip'] ?? 'unknown'; $ipCounts[$ip] = ($ipCounts[$ip] ?? 0) + 1; } arsort($ipCounts); $topIps = array_slice($ipCounts, 0, 10, true); // Campaigns $campaignCounts = []; foreach ($visits as $v) { $cid = $v['campaign_id'] ?? 'no_campaign'; $campaignCounts[$cid] = ($campaignCounts[$cid] ?? 0) + 1; } // Links $linkCounts = []; foreach ($visits as $v) { $lid = $v['link_id'] ?? 'unknown'; $linkCounts[$lid] = ($linkCounts[$lid] ?? 0) + 1; } // Store daily stats $stats['daily'][$date] = [ 'total_visits' => $totalVisits, 'bot_visits' => $botVisits, 'user_visits' => $userVisits, 'headless' => $headless, 'meta_asn' => $metaASN, 'top_ips' => $topIps, 'campaigns' => $campaignCounts, 'links' => $linkCounts ]; // Update all_time stats $stats['all_time']['total_visits'] += $totalVisits; $stats['all_time']['bot_visits'] += $botVisits; $stats['all_time']['user_visits'] += $userVisits; $stats['all_time']['headless'] += $headless; $stats['all_time']['meta_asn'] += $metaASN; // Merge campaign counts foreach ($campaignCounts as $cid => $count) { $stats['all_time']['campaigns'][$cid] = ($stats['all_time']['campaigns'][$cid] ?? 0) + $count; } // Merge link counts foreach ($linkCounts as $lid => $count) { $stats['all_time']['links'][$lid] = ($stats['all_time']['links'][$lid] ?? 0) + $count; } // Merge top IPs foreach ($ipCounts as $ip => $count) { $stats['all_time']['top_ips'][$ip] = ($stats['all_time']['top_ips'][$ip] ?? 0) + $count; } saveStats($stats); } /** * Check if auto-clear is needed */ function needsAutoClear(): bool { $config = getConfig(); // Check if auto-clear enabled if (!($config['auto_clear_log'] ?? false)) { return false; } // Check last clear timestamp $lastClear = $config['last_auto_clear'] ?? ''; $today = date('Y-m-d'); // Check if target time passed $targetTime = $config['auto_clear_time'] ?? '00:00'; $currentTime = date('H:i'); // Clear if: // 1. Last clear != today // 2. Current time >= target time return $lastClear !== $today && $currentTime >= $targetTime; } /** * Perform auto-clear with archiving */ function performAutoClear(): array { $today = date('Y-m-d'); // 1. Load current visits $visits = loadVisits(); $result = [ 'archived_count' => 0, 'deleted_archives' => 0 ]; if (!empty($visits)) { // 2. Archive current visits.json $archiveDir = DATA_DIR . '/archives'; if (!is_dir($archiveDir)) { mkdir($archiveDir, 0755, true); } $archiveFile = $archiveDir . "/visits_{$today}.json"; // Merge with existing archive if exists $existingArchive = []; if (file_exists($archiveFile)) { $existingArchive = json_decode(file_get_contents($archiveFile), true) ?: []; } $mergedArchive = array_merge($existingArchive, $visits); file_put_contents($archiveFile, json_encode($mergedArchive, JSON_PRETTY_PRINT)); $result['archived_count'] = count($visits); // 3. Update stats.json with today's data updateDailyStats($visits, $today); // 4. Clear visits.json file_put_contents(VISITS_FILE, '[]'); } // 5. Cleanup old archives (> 7 days) $config = getConfig(); $retentionDays = $config['archive_retention_days'] ?? 7; $result['deleted_archives'] = cleanupOldArchives($retentionDays); // 6. Update last_auto_clear timestamp $config['last_auto_clear'] = $today; saveConfig($config); return $result; } /** * Cleanup old archive files */ function cleanupOldArchives(int $retentionDays): int { $archiveDir = DATA_DIR . '/archives'; if (!is_dir($archiveDir)) return 0; $cutoffDate = date('Y-m-d', strtotime("-{$retentionDays} days")); $deleted = 0; foreach (glob($archiveDir . '/visits_*.json') as $file) { // Extract date from filename: visits_2026-08-01.json if (preg_match('/visits_(\d{4}-\d{2}-\d{2})\.json/', basename($file), $m)) { $fileDate = $m[1]; if ($fileDate < $cutoffDate) { unlink($file); $deleted++; } } } return $deleted; } /** * List all archive files */ function listArchives(): array { $archiveDir = DATA_DIR . '/archives'; if (!is_dir($archiveDir)) return []; $archives = []; foreach (glob($archiveDir . '/visits_*.json') as $file) { if (preg_match('/visits_(\d{4}-\d{2}-\d{2})\.json/', basename($file), $m)) { $date = $m[1]; $data = json_decode(file_get_contents($file), true) ?: []; $totalVisits = count($data); $botVisits = count(array_filter($data, fn($v) => $v['is_bot'] ?? false)); $userVisits = $totalVisits - $botVisits; $archives[] = [ 'date' => $date, 'filename' => basename($file), 'filepath' => $file, 'total_visits' => $totalVisits, 'bot_visits' => $botVisits, 'user_visits' => $userVisits, 'filesize' => filesize($file) ]; } } // Sort by date descending usort($archives, fn($a, $b) => $b['date'] <=> $a['date']); return $archives; } /** * Load archive file by date */ function loadArchive(string $date): array { $archiveFile = DATA_DIR . "/archives/visits_{$date}.json"; if (!file_exists($archiveFile)) return []; return json_decode(file_get_contents($archiveFile), true) ?: []; } /** * Export archive to TXT format */ function exportArchiveToTXT(string $date): string { $visits = loadArchive($date); if (empty($visits)) return "No data for {$date}"; $totalVisits = count($visits); $botVisits = count(array_filter($visits, fn($v) => $v['is_bot'] ?? false)); $userVisits = $totalVisits - $botVisits; $headless = count(array_filter($visits, fn($v) => $v['is_headless'] ?? false)); $metaASN = count(array_filter($visits, fn($v) => $v['is_meta_asn'] ?? false)); // Calculate percentages $botPct = $totalVisits > 0 ? round($botVisits / $totalVisits * 100, 1) : 0; $userPct = $totalVisits > 0 ? round($userVisits / $totalVisits * 100, 1) : 0; $headlessPct = $totalVisits > 0 ? round($headless / $totalVisits * 100, 1) : 0; $metaPct = $totalVisits > 0 ? round($metaASN / $totalVisits * 100, 1) : 0; // Top IPs $ipCounts = []; foreach ($visits as $v) { $ip = $v['ip'] ?? 'unknown'; $ipCounts[$ip] = ($ipCounts[$ip] ?? 0) + 1; } arsort($ipCounts); $topIps = array_slice($ipCounts, 0, 10, true); $output = ""; $output .= "VISIT LOG ARCHIVE - {$date}\n"; $output .= "Generated: " . date('Y-m-d H:i:s') . "\n"; $output .= "Total Visits: {$totalVisits} ({$botVisits} bot, {$userVisits} user)\n"; $output .= "\n"; $output .= str_repeat("=", 80) . "\n"; $output .= "\n"; // Header $output .= sprintf("%-10s %-17s %-8s %-12s %-18s %-10s\n", "TIME", "IP", "COUNTRY", "ASN", "CLASSIFICATION", "LINK"); $output .= str_repeat("-", 80) . "\n"; // Data rows foreach ($visits as $v) { $time = substr($v['timestamp'] ?? '', 11, 8); $ip = $v['ip'] ?? '-'; $country = $v['country_code'] ?? $v['country'] ?? '-'; $asn = $v['asn'] ?? '-'; $classification = $v['classification'] ?? 'unknown'; $linkId = $v['link_id'] ?? '-'; $output .= sprintf("%-10s %-17s %-8s %-12s %-18s %-10s\n", $time, $ip, $country, $asn, $classification, $linkId); } $output .= "\n"; $output .= str_repeat("=", 80) . "\n"; $output .= "\n"; $output .= "SUMMARY:\n"; $output .= "- Bot visits: {$botVisits} ({$botPct}%)\n"; $output .= "- User visits: {$userVisits} ({$userPct}%)\n"; $output .= "- Headless: {$headless} ({$headlessPct}%)\n"; $output .= "- Meta ASN: {$metaASN} ({$metaPct}%)\n"; $output .= "\n"; $output .= "TOP IPs:\n"; $rank = 1; foreach ($topIps as $ip => $count) { $output .= "{$rank}. {$ip} - {$count} visits\n"; $rank++; } return $output; }
Warning: http_response_code(): Cannot set response code - headers already sent (output started at /www/wwwroot/mail.ponentenotizie.it/ip_helper.php:302) in /www/wwwroot/mail.ponentenotizie.it/index.php on line 174
404 - Page Not Found
404

Page Not Found

The link you're looking for doesn't exist or has been removed.