<?php

header('Content-Type: audio/mp4');
//$json_channels = json_decode(file_get_contents('_data_star.json'), true);
$env = getenv("PATH_INFO");
$id = search($env, "/", "/");
$direcctorio = "/var/www/html/star/channels/".$id;
#$f = $json_channels[$id]['playbackUrl'];
$f = file_get_contents($direcctorio.'/'.$id.'.url');
$en2 = str_replace($id, '', $f);
$env = str_replace($id, "", $env);
$ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36";


if (file_exists($direcctorio."/")) {
    mkdir($direcctorio, 0777, true);
    mkdir($direcctorio."/audio/u", 0777, true);
    mkdir($direcctorio."/audio/f", 0777, true);
    mkdir($direcctorio."/video/u", 0777, true);
    mkdir($direcctorio."/video/f", 0777, true);
}
$llab = file_get_contents($direcctorio.'/'.$id.'.key');
if (strpos($env, "7000_slide.m3u8") == !false) {
    $net = get_data(str_replace('///','/',$en2.$env));
    preg_match_all('/#EXT-X-KEY:METHOD=SAMPLE-AES-CTR,KEYFORMAT="(.*)"\n/i', $net,$matches);
    $net = str_replace($matches[0][0], "", $net);
    $net = str_replace($matches[0][1], "", $net);
    $net = str_replace($matches[0][2], "", $net);
    #preg_match_all('/#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="aac-128k(.*)"\n/i', $net,$matchesA);
    #$net = str_replace($matchesA[0][0], "", $net);
  # preg_match_all('/#EXT-X-INDEPENDENT-SEGMENTS(.*)\n/i', $net,$matchesB);
  #$net = str_replace($matchesB[0][0], "", $net);
  #preg_match_all('/#EXTM3U


#(.*)\n/i', $net,$matchesC);
  
 # $net = str_replace($matchesC[0][0], "", $net);
  # $net = str_replace('#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="aac-64k",NAME="English",LANGUAGE="en",DEFAULT=YES,AUTOSELECT=YES,CHANNELS="2",URI="audio-1-64K/64_slide.m3u8"','#EXTM3U',$net);
  
    #echo $net;
  
    }elseif (preg_match("/128_slide.m3u8/i", $env)) {
    $net = get_data(str_replace('///','/',$en2.$env));
    preg_match_all('/#EXT-X-KEY:METHOD=SAMPLE-AES-CTR,KEYFORMAT="(.*)"\n/i', $net,$matches);
    $net = str_replace($matches[0][0], "", $net);
    $net = str_replace($matches[0][1], "", $net);
    $net = str_replace($matches[0][2], "", $net);
    echo $net;
  
    }elseif (preg_match("/cmaf-cenc-ctr/i", $env)) {
    $net = str_replace('///','/',$en2.$env);
  $final = explode('/',$net);
  #print_r($final);
   if(end($final)=='map.mp4'){
     #echo "Aqui va el map video";
    if (!file_exists($direcctorio."/map.mp4")) {
        $j = exec("/usr/bin/aria2c -U \"".$ua."\" --check-certificate=false \"".$net."\" --dir \"".$direcctorio."/\" -o map.mp4");
      #echo "/usr/bin/aria2c -U \"".$ua."\" --check-certificate=false \"".$net."\" --dir \"".$direcctorio."/\" -o map.mp4";
    }
   }else {
     
 #echo "AQUI BAJO SEGMENTOS";
 $init = $direcctorio."/map.mp4";
 $net = str_replace('///','/',$en2.$env);
 $env2 = end($final);
 $j = exec("/usr/bin/aria2c -U \"".$ua."\" --check-certificate=false \"".$net."\" --dir \"".$direcctorio."/video/\" -o ".$env2);
#echo "/usr/bin/aria2c -U \"".$ua."\" --check-certificate=false \"".$net."\" --dir \"".$direcctorio."/video/\" -o ".$env2; //bien
$d = exec("cat \"".$init."\" \"".$direcctorio."/video/".$env2."\" > \"".$direcctorio."/video/u/".$env2."\"");
#echo "cat \"".$init."\" \"".$direcctorio."/video/".$env2."\" > \"".$direcctorio."/video/u/".$env2."\"";
    #unlink($direcctorio."/video/".$env2);
    $r = exec("/var/www/html/star/bin/mp4 --key 1:".$llab." \"".$direcctorio."/video/u/".$env2."\" \"".$direcctorio."/video/f/".$env2."\"");
     #echo "/var/www/html/star/bin/mp4 --key 1:".$llab." \"".$direcctorio."/video/u/".$env2."\" \"".$direcctorio."/video/f/".$env2."\"";
   # unlink($direcctorio."/video/u".$env2);
    $f = file_get_contents("http://localhost/star/channels/".$id."/video/f/".$env2);
     #echo "https://localhost:8080/star/channels/".$id."/video/f/".$env2;
    header("Content-Type: video/mp4");
   # unlink($direcctorio."/video/f".$env2);
    echo $f;
  unlink($direcctorio."/video/".$env2);
   unlink($direcctorio."/video/u".$env2);
     unlink($direcctorio."/video/f".$env2);
     
}
    }elseif (preg_match("/128K\/map.mp4a/i", $env,$matches)) {
    $net = str_replace('///','/',$en2.$env);
  #echo "ESTE ES MAP DE AUDIO: ".$net;
    if (!file_exists($direcctorio."/map.mp4a")) {
        $j = exec("/usr/bin/aria2c -U \"".$ua."\" --check-certificate=false \"".$net."\" --dir \"".$direcctorio."/\" -o map.mp4a");
      #echo "/usr/bin/aria2c -U \"".$ua."\" --check-certificate=false \"".$net."\" --dir \"".$direcctorio."/\" -o map.mp4a";
    }

  } elseif (preg_match("/audio-1-128K/i", $env)) {
    $init = $direcctorio."/map.mp4a";
     $net = str_replace('///','/',$en2.$env);
    $env2 = str_replace($id, '', $env);
    $env2 = str_replace("//", "/", $env2);
    $data = explode('/', $env2);;
    $env2 = "/".$data[4];
    $j = exec("/usr/bin/aria2c -U \"".$ua."\" --check-certificate=false \"".$net."\" --dir \"".$direcctorio."/audio/\" -o ".$env2);
    $d = exec("cat \"".$init."\" \"".$direcctorio."/audio".$env2."\" > \"".$direcctorio."/audio/u".$env2."\"");
    $r = exec("/var/www/html/star/bin/mp4 --key 1:".$llab." \"".$direcctorio."/audio/u".$env2."\" \"".$direcctorio."/audio/f".$env2."\"");
    $f = file_get_contents("http://localhost/star/channels/".$id."/audio/f".$env2);
    header("Content-Type: audio/mp4");
    echo $f;
  unlink($direcctorio."/audio/".$env2);
   unlink($direcctorio."/audio/u".$env2);
     unlink($direcctorio."/audio/f".$env2);

}   
function search($target, $start, $end)
{
    $start_at = strpos($target, $start) + strlen($start);
    $target = substr($target, $start_at);
    $end_at = strpos($target, $end);
    return substr($target, 0, $end_at);
}


function get_data($f) {

    $ch = curl_init();
    $timeout = 15;
    $headers = array( 
"User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36"
                ); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_URL, $f);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_TIMEOUT, ($timeout));
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

