<?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 = "1289522313";

// Check interval in seconds
$checkInterval = 5;

// Function to fetch content using cURL
function fetchContent($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 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36');
curl_setopt($ch, CURLOPT_TIMEOUT, 30);

$html = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if (curl_errno($ch)) {
echo "cURL Error: " . curl_error($ch) . "\n";
curl_close($ch);
return false;
}

curl_close($ch);

if ($httpCode != 200) {
echo "HTTP Error: $httpCode\n";
return false;
}

return $html;
}

// Function to check channels
function checkChannels($Filters, $channels, $armpToken, $armpUserID) {
foreach ($channels as $Channel) {
$channelStart = microtime(true);

$Channel = trim($Channel);
if (empty($Channel)) {
continue;
}

$url = "https://eitaa.com/$Channel";

$fetchStart = microtime(true);
$html = fetchContent($url);
$fetchTime = (microtime(true) - $fetchStart);

if ($html === false) {
echo "Failed to fetch content for channel: $Channel\n\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:
━━━━━━━━━━━━━━━━━━━━━━

⏳ Fetch Time: " . round($fetchTime, 2) . "s
⚙️ 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...\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\n";
echo "💤 Sleeping for $checkInterval seconds...\n";
echo "━━━━━━━━━━━━━━━━━━━━━━\n\n";

sleep($checkInterval);
}
?>