<?php
  session_start();
error_reporting(E_ALL ^ E_NOTICE);   
$url= $_SERVER["HTTP_REFERER"];
$gotera= explode("tv.gotera.top",$url);
$cabc=count($gotera);
if(empty($_SERVER["HTTP_REFERER"]) && $_GET['myapp']!='ok' && $cabc==1){
    //echo 'Download';
    //header('Location:https://gotera.top/tv.apk');
}


  //session_destroy();
$abc=$_REQUEST;  

if (empty($_COOKIE["uuid"]) && !empty($_POST['phone'])) {
    $uuid = uniqid();  // or use a real UUID
    setcookie("uuid", $uuid, time()+30*324*360*360, "/");
    

 
}
else {
    
    $uuid = $_COOKIE["uuid"];
}

if(!empty($abc)){
      foreach($abc as $keyp => $valuep)
  {
     $adsc.= $keyp.'='.$valuep.' ';
  }
  $myfilec = fopen("c.txt", "a") or die("Unable to open file!");
  fwrite($myfilec, $uuid.'$'.$adsc.'
');
  fclose($myfilec); 
}

  
   
  $ctime= time();
  $ab=$_GET['cnn'];
  if(empty($ab)){
  $rcnn= array('ebstvWorldwide','fanabroadcastingcorporate','ArtsTvWorld','EBCworld','BalageruTV','WaltaTV','AshamTube');
  $rnn=rand(0,6);
  $ab=$rcnn[$rnn];
  }		

  $file = './b.dat';
  if (file_exists($file) && isset($ab)) {
  $myfile = fopen($file, "r") or die("Unable to open file!");
  $readfile =  fread($myfile,filesize($file));
  fclose($myfile);
  }
  $replaceok = explode('#'.$ab.'#',$readfile);
  $read = explode('|',$replaceok[1]);	// header
  		
  // header 
  //echo $ctime.'-'.$read[1];
  //exit;
  if($ctime>=$read[1]){
  //echo $ab;    
  //   echo 'Time';
 	if($ab=='MashaBearEN' || $ab=='GrizzylesLemmings' || $ab=='ellarecords' || $ab=='hoplessable' || $ab=='AwtarTVMusic' || $ab=='GrizzylesLemmings'){ $channel='c/';}
//elseif($ab=='hoplessablex' || $ab=='AwtarTVMusicx'){ $channel='channel/';}
    else{ $channel='@';}
      
  $yurl= "https://www.youtube.com/".$channel.$ab."/videos"; 
      
  //echo $yurl;
  //exit;
  $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$yurl);
   // curl_setopt($ch,CURLOPT_URL,"https://api.myip.com");
  	
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_HEADER, false);
  $buffer = curl_exec($ch);
  	curl_close($ch);
  //print_r($buffer);
  //exit;
  //$product = explode('"watchEndpoint":{"videoId":"',$buffer);
  //$productok = explode('"',$product[1]);
  
  //echo $productok[0].'='.$yurl;
  //exit;
  
  for ($x = 1; $x <= 30; $x++) {
  $product = explode('"watchEndpoint":{"videoId":"',$buffer);
  $productok = explode('"',$product[$x]);
  $titlea = explode('"title":{"runs":[{"text":"',$buffer);
  $titleb = explode('"}],"a',$titlea[$x]);
  $titleyesok= str_replace( array( '\\', '"', "'", ',' , ';', '<', '>','/','|' ), ' ',$titleb[0]);
  
  if(!empty($productok[0]))
  $pcount= strlen($productok[0]);
  	if($pcount==11){
    $ads.= "'".$productok[0]."':".$titleyesok.",";
  	$pp.=$productok[0];
  	}
  } 
  //$ads=str_replace( array( '"', "'", ',' , ';', '<', '>','/','|' ), '',$adsyes);	
  
  //echo $pp;
  //exit;
  if(!empty($pp) && !empty($replaceok[1]) && !empty($ab)){
  if(!empty($read[0])){
  $ctimeok = $ctime +($read[0]*3600);
  }
  //echo $ctimeok;
  //exit;
  $adsok= $read[0].'|'.$ctimeok.'|'.$ads;	
  	
  //echo $adsok;
  	//exit;
  $allsave= str_replace($replaceok[1], $adsok, $readfile);	
  
  $myfile = fopen("b.dat", "w") or die("Unable to open file!");
  fwrite($myfile, $allsave);
  fclose($myfile);
  $nday = round((time()/300),0);	
  copy('b.dat', './bk/'.$nday.'b.dat');
  //$msg= 'Update ok '.$ab.'<br>';	
  }	
  elseif(!empty($pp) && !empty($ab)){	
  
  if(empty($period)){$period=3;}		
  $ctimeok = 	$ctime +($period*3600);
  $periodtime= $period.'|'.$ctimeok.'|';	
  	
  $myfile = fopen("b.dat", "a") or die("Unable to open file!");
  fwrite($myfile, '#'.$ab.'#'.$periodtime.$ads.'#'.$ab.'#
');
  fclose($myfile);
  $nday = round((time()/300),0);	
  copy('b.dat', './bk/'.$nday.'b.dat');
  
  //$msg= 'New ok '.$ab.'<br>';
  	
  }	
  	
  }
  
  //manage next and here
  $nextok= explode(',',$read[2]);
  //echo $replaceok[1];
  $countv= count($nextok)-1;	
  $next=$_GET['next'];
  		
  if(empty($next)){$next=0;}
  if($countv<$next){$countv=1;}	
  	
  	
 include('./view/body.php'); 
  
    if(!isset($uuid)){
 include('./view/login.php'); 

	} ?>
