<?php
echo "<div style='direction: rtl; font-family: Tahoma;'>";
echo "<h3>تست اتصال لایه شبکه به TGJU</h3>";

$url = "https://call3.tgju.org/ajax.json?rev=" . time();

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 15); // زمان انتظار را کمی بیشتر کردیم
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36");

// فعال‌سازی لاگ‌گیری دقیق از کانکشن
curl_setopt($ch, CURLOPT_VERBOSE, true);
$verbose = fopen('php://temp', 'w+');
curl_setopt($ch, CURLOPT_STDERR, $verbose);

$response = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error_msg = curl_error($ch);
$error_code = curl_errno($ch);
curl_close($ch);

rewind($verbose);
$verboseLog = stream_get_contents($verbose);

echo "<b>کد وضعیت HTTP:</b> " . $httpcode . "<br>";
echo "<b>کد ارور cURL:</b> " . $error_code . "<br>";
echo "<b>متن ارور cURL:</b> <span style='color:red;'>" . (!empty($error_msg) ? $error_msg : "بدون خطا") . "</span><br>";

echo "<hr><b>لاگ دقیق اتصال (مهم‌ترین بخش برای تشخیص مشکل):</b><br>";
echo "<textarea style='width:100%; height:250px; direction: ltr; text-align: left; background: #222; color: #0f0; padding: 10px; font-family: monospace;'>" . htmlspecialchars($verboseLog) . "</textarea>";

echo "<hr><b>پاسخ (Response):</b><br>";
echo "<textarea style='width:100%; height:150px; direction: ltr; text-align: left;'>" . htmlspecialchars($response) . "</textarea>";
echo "</div>";
?>