<?php

error_reporting(0);

$APP_CONFIG['DATA_FOLDER'] = "_AppData_"; //Cache Directory
$APP_CONFIG['STREAM_PROXY'] = "ON"; //"ON" or "OFF" -- Put This Off To Redirect Main Playing Link Directly To User
$APP_CONFIG['OPTION_LISTINGPAGE_STATUS'] = "ON"; //"ON" or "OFF"
$APP_CONFIG['APP_NAME'] = "Sony24X7 LiveTV";
$APP_CONFIG['APP_LOGO'] = "assets/applogo.png";
$APP_CONFIG['APP_FAVICON'] = "assets/applogo.png";
$APP_CONFIG['EXT_TOKEN_API'] = "https://slivbts.bangladeshiptv.workers.dev/token/";
//========================================================//

if(!isset($APP_CONFIG['DATA_FOLDER']) || empty($APP_CONFIG['DATA_FOLDER'])){ $APP_CONFIG['DATA_FOLDER'] = "__SecureData__"; }
if(!is_dir($APP_CONFIG['DATA_FOLDER'])){ mkdir($APP_CONFIG['DATA_FOLDER']); }
if(!file_exists($APP_CONFIG['DATA_FOLDER']."/index.php")){ @file_put_contents($APP_CONFIG['DATA_FOLDER']."/index.php", ""); }
if(!file_exists($APP_CONFIG['DATA_FOLDER']."/.htaccess")){ @file_put_contents($APP_CONFIG['DATA_FOLDER']."/.htaccess", "deny from all"); }
$_SERVER['HTTP_HOST'] = strtok($_SERVER['HTTP_HOST'], ':'); $_SERVER['HTTP_HOST'] = str_ireplace('localhost', '127.0.0.1', $_SERVER['HTTP_HOST']);if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") { $streamenvproto = "https"; } elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == "https") { $streamenvproto = "https"; } else { $streamenvproto = "http"; } $plhoth = ($_SERVER['SERVER_ADDR'] !== "127.0.0.1") ? $_SERVER['HTTP_HOST'] : getHostByName(php_uname('n'));

//==========================================================//

function response($status, $code, $message, $data)
{
    header("Content-Type: application/json");
    header("Access-Control-Allow-Origin: *");
    $resj = array("status" => $status, "code" => $code, "message" => $message, "data" => $data);
    print(json_encode($resj));
    exit();
}

function encdec($action, $data)
{
  $output = ""; $ky = $iv = base64_decode('Tm9vdngtQF9fQC14dm9vTg');
  if($action == "encrypt") {
    $encrypted = openssl_encrypt($data, "AES-128-CBC", $ky, OPENSSL_RAW_DATA, $iv);
    if(!empty($encrypted)) { $output = bin2hex($encrypted); }
  }
  if($action == "decrypt")
  {
    if(strlen($data) % 2 == 0) {
      $dexBinary = hex2bin($data);
      $decrypted = openssl_decrypt($dexBinary, "AES-128-CBC", $ky, OPENSSL_RAW_DATA, $iv);
      if(!empty($decrypted)) { $output = $decrypted; }
    }
  }
  return $output;
}

function get_stream_base($url)
{
  if(stripos($url, "?") !== false){
  $xrl = explode("?", $url); if(isset($xrl[0]) && !empty($xrl[0])) { $url = trim($xrl[0]); } }
  return str_replace(basename($url), "", $url);
}

function extract_uri_part($string)
{
  $output = "";
  $pattern = '/URI="([^"]+)"/';
  preg_match($pattern, $string, $matches);
  if(isset($matches[1])) {
    $output = ($matches[1]);
  }
  return $output;
}

function extract_token_from_url($url)
{
  $output = "";
  if(stripos($url, "hdntl=") !== false) {
    $siis = explode("hdntl=", $url);
    if(isset($siis[1]) && !empty($siis[1])) {
      $output = "hdntl=".trim($siis[1]);
    }
  }
  return $output;
}

//============================================================================//

function getRequest($url, $header)
{
  $process = curl_init($url);
  curl_setopt($process, CURLOPT_HTTPHEADER, $header);
  curl_setopt($process, CURLOPT_HEADER, 0);
  curl_setopt($process, CURLOPT_TIMEOUT, 5);
  curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($process, CURLOPT_SSL_VERIFYHOST, 0);
  curl_setopt($process, CURLOPT_SSL_VERIFYPEER, 0);
  $return = curl_exec($process);
  $effURL = curl_getinfo($process, CURLINFO_EFFECTIVE_URL);
  $httpcode = curl_getinfo($process, CURLINFO_HTTP_CODE);
  curl_close($process);
  return array("url" => $effURL, "code" => $httpcode, "data" => $return);
}

function get_channel_detail($id)
{
  $output = array(); $tvlist = rawChannels();
  if(isset($tvlist[0])) {
    foreach($tvlist as $itv) {
      if(md5($id) == md5($itv['id'])) { $output = $itv; }
    }
  }
  if(!empty($output))
  {
    $output['stream_url'] = "";
    if(isset($output['cf_link']) && !empty($output['cf_link'])) { $output['cf_link'] = $output['cf_link']."?".a_video_token(); }
    if(isset($output['s247_link']) && !empty($output['s247_link'])) { $output['s247_link'] = $output['s247_link']."?".s_video_token(); }
    if(isset($output['ds_link']) && !empty($output['ds_link'])) { $output['ds_link'] = $output['ds_link']."?".d_video_token(); }
    if(!empty($output['cf_link']) && !empty(a_video_token()) ) {
      $output['stream_url'] = $output['cf_link'];
    }
    elseif(!empty($output['s247_link']) && !empty(s_video_token())) { 
      $output['stream_url'] = $output['s247_link'];
    }
    elseif(!empty($output['ds_link']) && !empty(d_video_token())) { 
      $output['stream_url'] = $output['ds_link'];
    }
    else{}
  }
  return $output;
}

//============================================================================//

function a_video_token()
{
  global $APP_CONFIG;
  $output = "";
  $tokenCachePath = $APP_CONFIG['DATA_FOLDER']."/a_tokenData.enc";
  if(file_exists($tokenCachePath)) {
    $readStmToken = @json_decode(@file_get_contents($tokenCachePath), true);
    if(isset($readStmToken['exp']) && time() < $readStmToken['exp'] && isset($readStmToken['token']) && !empty($readStmToken['token'])) {
      $output = $readStmToken['token'];
    }
  }
  $reqHead = array("User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.9999.99 Safari/537.36");
  if(empty($output))
  {
    $api1 = 'https://dai.google.com/linear/hls/event/wG75n5U8RrOKiFzaWObXbA/master.m3u8';
    $fetch1 = getRequest($api1, $reqHead);
    $response = $fetch1['data'];
    if($response !== false && strpos($response, '#EXTM3U') === 0)
    {
      preg_match('/hdntl=([^&]+)/', $response, $matches);
      if(isset($matches[1]) && !empty($matches[1])) {
        $output = "hdntl=".$matches[1];
      }
    }
    if(!empty($output)){ @file_put_contents($tokenCachePath, json_encode(array("token" => $output, "exp" => time() + 7200))); }
  }
  if(empty($output))
  {
    $api2 = $APP_CONFIG['EXT_TOKEN_API'].'a';
    $fetch2 = getRequest($api2, $reqHead);
    $response = $fetch2['data'];
    if(!empty($response)) {
      $rexsn = @json_decode($response, true);
      if(isset($rexsn['status']) && $rexsn['message'] && $rexsn['status'] == "success")
      {
        $output = $rexsn['message'];
      }
    }
    if(!empty($output)){ @file_put_contents($tokenCachePath, json_encode(array("token" => $output, "exp" => time() + 7200))); }
  }
  return $output;
}

function s_video_token()
{
  global $APP_CONFIG;
  $output = "";
  $tokenCachePath = $APP_CONFIG['DATA_FOLDER']."/s_tokenData.enc";
  if(file_exists($tokenCachePath))
  {
    $readStmToken = @json_decode(@file_get_contents($tokenCachePath), true);
    if(isset($readStmToken['exp']) && time() < $readStmToken['exp'] && isset($readStmToken['token']) && !empty($readStmToken['token'])) {
      $output = $readStmToken['token'];
    }
  }
  $reqHead = array("User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.9999.99 Safari/537.36");
  if(empty($output))
  {
    $api1 = 'https://pubads.g.doubleclick.net/ssai/event/4_pnLi2QTe6bRGvvahRbfg/master.m3u8';
    $fetch1 = getRequest($api1, $reqHead);
    $response = $fetch1['url'];
    if(!empty($response))
    {
      preg_match('/hdnea=([^&]+)/', $response, $matches);
      if(isset($matches[1]) && !empty($matches[1])) {
        $output = "hdnea=".$matches[1];
      }
    }
    if(!empty($output)){ @file_put_contents($tokenCachePath, json_encode(array("token" => $output, "exp" => time() + 220))); }
  }
  if(empty($output))
  {
    $api2 = $APP_CONFIG['EXT_TOKEN_API'].'s';
    $fetch2 = getRequest($api2, $reqHead);
    $response = $fetch2['data'];
    if(!empty($response)) {
      $rexsn = @json_decode($response, true);
      if(isset($rexsn['status']) && $rexsn['message'] && $rexsn['status'] == "success")
      {
        $output = $rexsn['message'];
      }
    }
    if(!empty($output)){ @file_put_contents($tokenCachePath, json_encode(array("token" => $output, "exp" => time() + 200))); }
  }
  return $output;
}

function d_video_token()
{
  global $APP_CONFIG;
  $tokenCachePath = $APP_CONFIG['DATA_FOLDER']."/d_tokenData.enc";
  if(file_exists($tokenCachePath)) {
    $readStmToken = @json_decode(@file_get_contents($tokenCachePath), true);
    if(isset($readStmToken['exp']) && time() < $readStmToken['exp'] && isset($readStmToken['token']) && !empty($readStmToken['token'])) {
      $output = $readStmToken['token'];
    }
  }
  $reqHead = array("User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.9999.99 Safari/537.36");
  if(empty($output))
  {
    $api2 = $APP_CONFIG['EXT_TOKEN_API'].'d';
    $fetch2 = getRequest($api2, $reqHead);
    $response = $fetch2['data'];
    if(!empty($response)) {
      $rexsn = @json_decode($response, true);
      if(isset($rexsn['status']) && $rexsn['message'] && $rexsn['status'] == "success")
      {
        $output = $rexsn['message'];
      }
    }
    if(!empty($output)){ @file_put_contents($tokenCachePath, json_encode(array("token" => $output, "exp" => time() + 7200))); }
  }
  return $output;
}

function rawChannels()
{
  $json = '[
    {
      "id": "sony-hd",
      "title": "Sony HD (SET HD)",
      "logo": "https://i.postimg.cc/xCV0N1yy/SONY-SET-HD.png",
      "genre": "Entertainment",
      "language": "Hindi",
      "cf_link": "https://lin-gd-001-cf.slivcdn.com/out/v1/9f4f650a3ef84ec187f35444aeff456e/master.m3u8",
      "s247_link": "https://sony247channels.akamaized.net/hls/live/2011671/SETHD/master.m3u8",
      "ds_link": "https://dishmt.slivcdn.com/hls/live/2011671/SETHD/master.m3u8"
    },
    {
      "id": "sony-sab-hd",
      "title": "Sony SAB HD",
      "logo": "https://i.postimg.cc/HLrGvfHN/SONY-SAB-HD.png",
      "genre": "Entertainment",
      "language": "Hindi",
      "cf_link": "https://lin-gd-001-cf.slivcdn.com/out/v1/d3a414ee43fc440fbe6190e2a1ed5fe1/master.m3u8",
      "s247_link": "https://sony247channels.akamaized.net/hls/live/2011749/SABHD/master.m3u8",
      "ds_link": "https://dishmt.slivcdn.com/hls/live/2011749/SABHD/master.m3u8"
    },
    {
      "id": "sony-marathi",
      "title": "Sony Marathi",
      "logo": "https://i.postimg.cc/ZqvcSG5j/SONY-MARATHI.png",
      "genre": "Entertainment",
      "language": "Marathi",
      "cf_link": "https://lin-gd-001-cf.slivcdn.com/out/v1/459f7d1ce6104bb3a3fe18766936402e/master.m3u8",
      "s247_link": "https://sony247channels.akamaized.net/hls/live/2011740/SonyMarathi/master.m3u8",
      "ds_link": "https://dishmt.slivcdn.com/hls/live/2011740/SonyMarathi/master.m3u8"
    },
    {
      "id": "sony-pal",
      "title": "Sony Pal",
      "logo": "https://i.postimg.cc/nrQ6V7MG/SONY-PAL.png",
      "genre": "Entertainment",
      "language": "Hindi",
      "cf_link": "https://lin-gd-001-cf.slivcdn.com/out/v1/3950521d96f7427fa0f0485e82e53487/master.m3u8",
      "s247_link": "https://sony247channels.akamaized.net/hls/live/2011741/SonyPalSD/master.m3u8",
      "ds_link": "https://dishmt.slivcdn.com/hls/live/2011741/SonyPalSD/master.m3u8"
    },
    {
      "id": "sony-aath",
      "title": "Sony Aath",
      "logo": "https://i.postimg.cc/bY12WVTm/SONY-AATH.png",
      "genre": "Entertainment",
      "language": "Bengali",
      "cf_link": "",
      "s247_link": "https://sony247channels.akamaized.net/hls/live/2011641/SonyAathSD/master.m3u8",
      "ds_link": "https://dishmt.slivcdn.com/hls/live/2011641/SonyAathSD/master.m3u8"
    },
    {
      "id": "sony-yay",
      "title": "Sony Yay",
      "logo": "https://i.postimg.cc/qRxtS9Gy/SONY-YAY.png",
      "genre": "Kids",
      "language": "Hindi",
      "cf_link": "https://lin-gd-001-cf.slivcdn.com/out/v1/556cf446f0b1457b98640c553b652a4a/master.m3u8",
      "s247_link": "https://sony247channels.akamaized.net/hls/live/2011746/SonyYaySD/master.m3u8",
      "ds_link": "https://dishmt.slivcdn.com/hls/live/2011746/SonyYaySD/master.m3u8"
    },
    {
      "id": "sony-max-hd",
      "title": "Sony MAX HD",
      "logo": "https://i.postimg.cc/XYF71G8F/SONY-MAX-HD.png",
      "genre": "Movies",
      "language": "Hindi",
      "cf_link": "https://lin-gd-001-cf.slivcdn.com/out/v1/8fe2e6b11f184c34ac3f44cbd6a7caf0/master.m3u8",
      "s247_link": "https://sony247channels.akamaized.net/hls/live/2011670/SonyMaxhd/master.m3u8",
      "ds_link": "https://dishmt.slivcdn.com/hls/live/2011670/SonyMaxhd/master.m3u8"
    },
    {
      "id": "sony-max",
      "title": "Sony MAX",
      "logo": "https://i.postimg.cc/X7MmrgCs/SONY-MAX.png",
      "genre": "Movies",
      "language": "Hindi",
      "cf_link": "",
      "s247_link": "https://sony247channels.akamaized.net/hls/live/2011745-b/SonyMaxSD/master.m3u8",
      "ds_link": "https://dishmt.slivcdn.com/hls/live/2011745/SonyMaxSD/master.m3u8"
    },
    {
      "id": "sony-wah",
      "title": "Sony WAH",
      "logo": "https://i.postimg.cc/jq6Qh61g/SONY-WAH.png",
      "genre": "Movies",
      "language": "Hindi",
      "cf_link": "https://lin-gd-001-cf.slivcdn.com/out/v1/3ad8c562a750403f9357923085554110/master.m3u8",
      "s247_link": "https://sony247channels.akamaized.net/hls/live/2011906/SonyWah/master.m3u8",
      "ds_link": "https://dishmt.slivcdn.com/hls/live/2011906/SonyWah/master.m3u8"
    },
    {
      "id": "sony-max-2",
      "title": "Sony MAX 2",
      "logo": "https://i.postimg.cc/ZnBh92s3/SONY-MAX-2.png",
      "genre": "Movies",
      "language": "Hindi",
      "cf_link": "https://lin-gd-001-cf.slivcdn.com/out/v1/3387ff1dbcb84b06b22dab95a4ee30e5/master.m3u8",
      "s247_link": "https://sony247channels.akamaized.net/hls/live/2011908/MAX2/master.m3u8",
      "ds_link": "https://dishmt.slivcdn.com/hls/live/2011908/MAX2/master.m3u8"
    },
    {
      "id": "sony-pix-hd",
      "title": "Sony PIX HD",
      "logo": "https://i.postimg.cc/hGYnSKpg/SONY-PIX-HD.png",
      "genre": "Movies",
      "language": "English",
      "cf_link": "https://lin-gd-001-cf.slivcdn.com/out/v1/f88af37f57d94cbd8c584cd5e83d92aa/master.m3u8",
      "s247_link": "https://sony247channels.akamaized.net/hls/live/2011748/PIXHD/master.m3u8",
      "ds_link": "https://dishmt.slivcdn.com/hls/live/2011748/PIXHD/master.m3u8"
    },
    {
      "id": "sony-bbc-earth-hd",
      "title": "Sony BBC Earth HD",
      "logo": "https://i.postimg.cc/t4g4cy4Z/SONY-BBC-EARTH.png",
      "genre": "Infotainment",
      "language": "English",
      "cf_link": "",
      "s247_link": "https://sony247channels.akamaized.net/hls/live/2011907/SonyBBCEarthHD/master.m3u8",
      "ds_link": "https://dishmt.slivcdn.com/hls/live/2011907/SonyBBCEarthHD/master.m3u8"
    },
    {
      "id": "sony-ten-1",
      "title": "Sony Sports Ten 1",
      "logo": "https://i.postimg.cc/6QYLXBdk/SONY-SPORTS-TEN-1.png",
      "genre": "Sports",
      "language": "English",
      "cf_link": "",
      "s247_link": "https://sony247channels.akamaized.net/hls/live/2011739-b/TEN1SD/master.m3u8",
      "ds_link": "https://dishmt.slivcdn.com/hls/live/2011739/TEN1SD/master.m3u8"
    },
    {
      "id": "sony-ten-2",
      "title": "Sony Sports Ten 2",
      "logo": "https://i.postimg.cc/FsbQV6bf/SONY-SPORTS-TEN-2.png",
      "genre": "Sports",
      "language": "English",
      "cf_link": "",
      "s247_link": "https://sony247channels.akamaized.net/hls/live/2020590/TEN2SD/master.m3u8",
      "ds_link": "https://dishmt.slivcdn.com/hls/live/2020590/TEN2SD/master.m3u8"
    },
    {
      "id": "sony-ten-3",
      "title": "Sony Sports Ten 3",
      "logo": "https://i.postimg.cc/nzLv0NQn/SONY-SPORTS-TEN-3.png",
      "genre": "Sports",
      "language": "Hindi",
      "cf_link": "",
      "s247_link": "https://sony247channels.akamaized.net/hls/live/2020592/TEN3SD/master.m3u8",
      "ds_link": "https://dishmt.slivcdn.com/hls/live/2020592/TEN3SD/master.m3u8"
    },
   {  "id": "sony-ten-4",
      "title": "Sony Sports Ten 4",
      "logo": "https://images.slivcdn.com/portrait_thumb/Sony_TEN4_23oct_portrait_thumb.jpg",
      "genre": "Sports",
      "language": "Hindi",
      "cf_link": "",
      "s247_link": "https://sony247channels.akamaized.net/hls/live/2020437/ten4sd/master.m3u8",
      "ds_link": "https://dishmt.slivcdn.com/hls/live/2020437/ten4sd/master.m3u8"
    },    
    {
      "id": "sony-ten-5",
      "title": "Sony Sports Ten 5",
      "logo": "https://i.postimg.cc/Vs72zb9N/SONY-SPORTS-TEN-5.png",
      "genre": "Sports",
      "language": "English",
      "cf_link": "",
      "s247_link": "https://sony247channels.akamaized.net/hls/live/2020594/SONYSIXSD/master.m3u8",
      "ds_link": "https://dishmt.slivcdn.com/hls/live/2020594/SONYSIXSD/master.m3u8"
    },
    {
      "id": "sony-ten-1-hd",
      "title": "Sony Sports Ten 1 HD",
      "logo": "https://i.postimg.cc/d00sTpK0/SONY-SPORTS-TEN-1-HD.png",
      "genre": "Sports",
      "language": "English",
      "cf_link": "",
      "s247_link": "https://sony247channels.akamaized.net/hls/live/2011747/TEN1HD/master.m3u8",
      "ds_link": "https://dishmt.slivcdn.com//hls/live/2011747/TEN1HD/master.m3u8"
    },
    {
      "id": "sony-ten-2-hd",
      "title": "Sony Sports Ten 2 HD",
      "logo": "https://i.postimg.cc/wvfsmLrC/SONY-SPORTS-TEN-2-HD.png",
      "genre": "Sports",
      "language": "English",
      "cf_link": "",
      "s247_link": "https://sony247channels.akamaized.net/hls/live/2020434/TEN2HD/master.m3u8",
      "ds_link": "https://dishmt.slivcdn.com/hls/live/2020434/TEN2HD/master.m3u8"
    },
    {
      "id": "sony-ten-3-hd",
      "title": "Sony Sports Ten 3 HD",
      "logo": "https://i.postimg.cc/2SGvDswj/SONY-SPORTS-TEN-3-HD.png",
      "genre": "Sports",
      "language": "Hindi",
      "cf_link": "",
      "s247_link": "https://sony247channels.akamaized.net/hls/live/2020591/TEN3HD/master.m3u8",
      "ds_link": "https://dishmt.slivcdn.com/hls/live/2020591/TEN3HD/master.m3u8"
    },
    { "id": "sony-ten-4-hd",
      "title": "Sony Sports Ten 4 HD",
      "logo": "https://images.slivcdn.com/portrait_thumb/Sony_TEN4HD_23oct_portrait_thumb.jpg",
      "genre": "Sports",
      "language": "Hindi",
      "cf_link": "",
      "s247_link": "https://sony247channels.akamaized.net/hls/live/2020589/ten4hd/master.m3u8",
      "ds_link": "https://dishmt.slivcdn.com/hls/live/2020589/ten4hd/master.m3u8"
    },
    {
      "id": "sony-ten-5-hd",
      "title": "Sony Sports Ten 5 HD",
      "logo": "https://i.postimg.cc/xCzcchKz/SONY-SPORTS-TEN-5-HD.png",
      "genre": "Sports",
      "language": "English",
      "cf_link": "",
      "s247_link": "https://sony247channels.akamaized.net/hls/live/2020593-b/SONYSIXHD/master.m3u8",
      "ds_link": "https://dishmt.slivcdn.com/hls/live/2020593-b/SONYSIXHD/master.m3u8"
    },
    {
      "id": "sony-kal",
      "title": "Sony KAL",
      "logo": "https://i.postimg.cc/ZqnmcXdx/Sony-KAL.png",
      "genre": "Entertainment",
      "language": "Hindi",
      "cf_link": "https://spt-sonykal-1-us.lg.wurl.tv/playlist.m3u8",
      "s247_link": "",
      "ds_link": ""
    }
  ]';
  return json_decode($json, true);
}

?>