<?php

set_time_limit(0);
ini_set('max_execution_time', 0);
ini_set('memory_limit', '512M');

$scriptStart = microtime(true);

// Telegram bot token and user ID
$armpToken = "6699635284:AAFklesnB4P6CmMz0mc8nIVq72XjSRf0gCQ";
$armpUserID = "6503581406";

// Check interval in seconds
$checkInterval = 5;

// Function to fetch multiple URLs in parallel
function fetchMultipleUrls($urls) {
$multiHandle = curl_multi_init();
$curlHandles = array();
$results = array();

foreach ($urls as $key => $url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36');
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);

curl_multi_add_handle($multiHandle, $ch);
$curlHandles[$key] = $ch;
}

$running = null;
do {
curl_multi_exec($multiHandle, $running);
curl_multi_select($multiHandle);
} while ($running > 0);

foreach ($curlHandles as $key => $ch) {
$results[$key] = curl_multi_getcontent($ch);
curl_multi_remove_handle($multiHandle, $ch);
curl_close($ch);
}

curl_multi_close($multiHandle);

return $results;
}

// Function to check channels
function checkChannels($Filters, $channels, $armpToken, $armpUserID) {
$urls = array();
foreach ($channels as $channel) {
$channel = trim($channel);
if (!empty($channel)) {
$urls[$channel] = "https://eitaa.com/$channel";
}
}

echo "📡 Fetching " . count($urls) . " channels in parallel...\n";

$fetchStart = microtime(true);
$htmlResults = fetchMultipleUrls($urls);
$fetchTime = (microtime(true) - $fetchStart);

echo "✅ All channels fetched in " . round($fetchTime, 2) . " seconds\n";

foreach ($htmlResults as $Channel => $html) {
$channelStart = microtime(true);

if ($html === false || empty($html)) {
echo "Failed to fetch content for channel: $Channel\n";
continue;
}

$parseStart = microtime(true);
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($html);
libxml_clear_errors();
$xpath = new DOMXPath($dom);
$parseTime = (microtime(true) - $parseStart);

$messages = $xpath->query('//div[contains(@class, "etme_widget_message_wrap")]');

if ($messages->length > 0) {
$lastMessage = $messages->item($messages->length - 1);
$mainMessageTextNodes = $xpath->query('.//div[contains(@class, "etme_widget_message_text") and not(ancestor::div[contains(@class, "etme_widget_message_reply")])]//text()', $lastMessage);
$messageText = "";
foreach ($mainMessageTextNodes as $node) {
$messageText .= $node->textContent . " ";
}
$messageText = trim($messageText);

$filterStart = microtime(true);
$foundFilters = array();
foreach ($Filters as $filter) {
if (str_contains($messageText, $filter)) {
$foundFilters[] = $filter;
}
}
$filterCheckTime = (microtime(true) - $filterStart) * 1000;

if (!empty($foundFilters)) {
$channelFile = "$Channel.txt";
if (!file_exists($channelFile) || $messageText != file_get_contents($channelFile)) {
$filterList = implode(", ", $foundFilters);
$channelTotal = (microtime(true) - $channelStart);

$armpText = "<code>$messageText</code>

━━━━━━━━━━━━━━━━━━━━━━
🔔 NEW MESSAGE FROM EITAA
━━━━━━━━━━━━━━━━━━━━━━

📍 Channel: @$Channel
🔍 Found Filter: $filterList

━━━━━━━━━━━━━━━━━━━━━━
⏱ PERFORMANCE STATS:
━━━━━━━━━━━━━━━━━━━━━━

⚙️ Parse Time: " . round($parseTime, 3) . "s
🔎 Filter Check: " . round($filterCheckTime, 2) . "ms
📊 Total Time: " . round($channelTotal, 2) . "s

━━━━━━━━━━━━━━━━━━━━━━";

$armp = file_get_contents("https://api.telegram.org/bot$armpToken/SendMessage?chat_id=$armpUserID&parse_mode=HTML&text=" . urlencode($armpText));
if ($armp) {
echo "✅ Text Sent To TG for channel: $Channel. [" . round($channelTotal, 2) . "s]\n";
}

file_put_contents($channelFile, $messageText);
} else {
echo "Text Already Sent To TG for channel: $Channel.\n";
}
} else {
echo "Message from $Channel is not important.\n";
}
} else {
echo "No messages found for channel: $Channel.\n";
}
}
}

echo "🚀 Starting continuous monitoring (MULTI-CURL)...\n";
echo "━━━━━━━━━━━━━━━━━━━━━━\n";
echo "Check interval: $checkInterval seconds\n";
echo "━━━━━━━━━━━━━━━━━━━━━━\n\n";

$iteration = 0;
while (true) {
$iteration++;
$loopStart = microtime(true);

echo "🔄 Iteration #$iteration - " . date('Y-m-d H:i:s') . "\n";

$filtersFile = "../Filters.txt";
if (file_exists($filtersFile)) {
$Filters = file($filtersFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$Filters = array_map('trim', $Filters);
} else {
echo "⚠️ Filters.txt not found, using empty filters.\n";
$Filters = array();
}

$channelsFile = "chs.txt";
if (file_exists($channelsFile)) {
$channels = file($channelsFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
} else {
echo "⚠️ chs.txt not found, skipping this iteration.\n";
sleep($checkInterval);
continue;
}

if (!empty($channels) && !empty($Filters)) {
checkChannels($Filters, $channels, $armpToken, $armpUserID);
}

$loopTime = microtime(true) - $loopStart;
echo "⏱️ Loop completed in " . round($loopTime, 2) . " seconds ⚡ [MULTI-CURL]\n";
echo "💤 Sleeping for $checkInterval seconds...\n";
echo "━━━━━━━━━━━━━━━━━━━━━━\n\n";

sleep($checkInterval);
}
?>