<?php
include "connect.php"; 

// aman start
$pass = $_POST["pass"];

if ($pass == 12111998) {
    // aman end   

    // الحصول على الـ IP الخاص بالزائر
    $ip_address = $_SERVER['REMOTE_ADDR'];

    // الحصول على الدولة بناءً على الـ IP باستخدام ip-api.com
    $json = file_get_contents("http://ip-api.com/json/{$ip_address}");
    $details = json_decode($json);
    $country = $details->countryCode ?? 'Unknown';

    // تاريخ اليوم
    $today = date('Y-m-d');

    // تحديث زيارات القنوات
    $updateVisitSql = "INSERT INTO channel_daily_visits (ip_address, country, visit_date, visit_count) 
                       VALUES ('$ip_address', '$country', '$today', 1) 
                       ON DUPLICATE KEY UPDATE visit_count = visit_count + 1";
    $mysql->query($updateVisitSql);

    // الحصول على الفئة من النموذج
    $category = $_POST["category"];
    $result_array = array();

    $sql = "SELECT * FROM channel WHERE category ='$category'";
    
    $result = $mysql->query($sql);
    
    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            array_push($result_array, $row);
        }
    }
    
    // تشفير البيانات وعكس السلسلة النصية
    $data = base64_encode(json_encode($result_array));
    echo strrev($data);
    
    // إغلاق الاتصال بقاعدة البيانات
    $mysql->close();
} else {
    echo "Error"; 
}
// aman end
?>
