<?php
header("Content-Type: application/json; charset=UTF-8");

$url = $_GET['url'] ?? '';
if (empty($url)) {
    die(json_encode(['error' => 'No URL provided'], JSON_UNESCAPED_UNICODE));
}

// فك التشفير
$url = urldecode($url);

$ch = curl_init();
curl_setopt_array($ch, [
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0 Safari/537.36',
    CURLOPT_REFERER => 'https://www.faselhd.pro/',
    CURLOPT_HTTPHEADER => [
        'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Accept-Language: ar-SA,ar;q=0.9,en-US;q=0.8,en;q=0.7',
        'Accept-Encoding: gzip, deflate, br',
        'Connection: keep-alive',
        'Upgrade-Insecure-Requests: 1',
        'Sec-Fetch-Dest: document',
        'Sec-Fetch-Mode: navigate',
        'Sec-Fetch-Site: same-origin',
    ],
    CURLOPT_ENCODING => "gzip",
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_TIMEOUT => 20,
    CURLOPT_AUTOREFERER => true,
]);

$html = curl_exec($ch);

if (curl_errno($ch)) {
    die(json_encode(['error' => 'cURL Error: ' . curl_error($ch)], JSON_UNESCAPED_UNICODE));
}

$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($httpCode !== 200) {
    die(json_encode(['error' => "Failed to load page, HTTP: $httpCode"], JSON_UNESCAPED_UNICODE));
}

if (empty($html)) {
    die(json_encode(['error' => 'Empty response from server'], JSON_UNESCAPED_UNICODE));
}

// استخراج player_token من iframe
$pattern = '/src=[\'"]([^\'"]+video_player\?player_token=[^\'"]+)[\'"]/i';
if (!preg_match($pattern, $html, $matches)) {
    die(json_encode(['error' => 'player_token not found in iframe'], JSON_UNESCAPED_UNICODE));
}

$iframe_src = urldecode($matches[1]);

// استخرج التوكن فقط
parse_str(parse_url($iframe_src, PHP_URL_QUERY), $query);
$token = $query['player_token'] ?? '';

if (empty($token)) {
    die(json_encode(['error' => 'Token extraction failed'], JSON_UNESCAPED_UNICODE));
}

$player_url = "https://web188.faselhd.cafe/video_player?player_token=" . $token;

// الآن ادخل على صفحة المشغل
$ch2 = curl_init();
curl_setopt_array($ch2, [
    CURLOPT_URL => $player_url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0 Safari/537.36',
    CURLOPT_REFERER => $url,
    CURLOPT_HTTPHEADER => [
        'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
        'Accept-Language: ar-SA,ar;q=0.9',
        'Connection: keep-alive',
    ],
    CURLOPT_ENCODING => "gzip",
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_TIMEOUT => 15,
]);

$player_html = curl_exec($ch2);
curl_close($ch2);

if (empty($player_html)) {
    die(json_encode(['error' => 'Player page is empty'], JSON_UNESCAPED_UNICODE));
}

// استخرج m3u8
if (preg_match('/https?:\/\/[^\s\'"]+\.m3u8[^\s\'"]*/', $player_html, $m3u8_matches)) {
    $m3u8_url = urldecode($m3u8_matches[0]);
    echo json_encode([
        'status' => 'success',
        'm3u8' => $m3u8_url,
        'expires_in' => 300
    ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
} else {
    die(json_encode(['error' => 'm3u8 not found in player page'], JSON_UNESCAPED_UNICODE));
}