File: /var/www/cod67/data/www/cod67.ru/public_html/similar1/redirect.php
<?php
global $logDir, $config;
foreach (glob($logDir.'/*.log') as $f)
if (filemtime($f) < time() - 86400 * $config['log_keep_days']) unlink($f);
}
cleanup_logs();
/* ---------- ЗАГРУЗКА СТАТИСТИКИ ---------- */
$dailyStats = load_json($dailyFile);
$monthlyStats = load_json($monthlyFile);
$today = date('Y-m-d');
$month = date('Y-m');
if (($dailyStats['date'] ?? null) !== $today)
$dailyStats = ['date' => $today, 'links' => []];
if (($monthlyStats['month'] ?? null) !== $month)
$monthlyStats = ['month' => $month, 'links' => []];
/* ---------- СПИСОК ССЫЛОК ---------- */
$links_raw = file_get_contents($config['links_source']);
if ($links_raw === false) die("Не могу загрузить links.txt: " . $config['links_source']);
$links = array_values(array_filter(array_map('trim', explode("
", $links_raw))));
$is_https =
(isset($_SERVER['HTTP_CF_VISITOR']) && json_decode($_SERVER['HTTP_CF_VISITOR'], true)['scheme'] === 'https') ||
(isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) === 'https') ||
(isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) === 'on');
$current_url = strtok(($is_https ? "https" : "http") . "://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}", '?');
$current_index = array_search($current_url, $links);
if ($current_index === false) die("URL не найден в списке");
/* ---------- ПОИСК РАБОЧЕЙ ССЫЛКИ ---------- */
$total = count($links);
$checked = 0;
while ($checked < $total) {
$current_index = ($current_index + 1) % $total;
$url = $links[$current_index];
$daily = $dailyStats['links'][$url] ?? 0;
$monthly = $monthlyStats['links'][$url] ?? 0;
if ($daily >= $config['daily_limit'] || $monthly >= $config['monthly_limit']){
write_log("Лимит превышен: $url (day=$daily, month=$monthly)");
$checked++;
continue;
}
if (!url_alive($url)){
write_log("Недоступен: $url");
telegram_notify("⚠️ Недоступен сайт: $url");
echo "<p>Сайт недоступен: $url</p>";
sleep($config['sleep_unavailable']);
$checked++;
continue;
}
$dailyStats['links'][$url] = $daily + 1;
$monthlyStats['links'][$url] = $monthly + 1;
save_json($dailyFile,$dailyStats);
save_json($monthlyFile,$monthlyStats);
write_log("Редирект → $url");
echo "<meta http-equiv='refresh' content='<?php echo $config['redirect_delay']; ?>;url=" . htmlspecialchars($url) . "'>";
exit;
}
tel egram_notify("❌ Все ссылки недоступны или превысили лимиты!");
die("Нет доступных ссылок.");