<?php
ini_set("memory_limit", "1024M");
ini_set("max_execution_time", 0);
set_time_limit(0);

session_start();
session_write_close();

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, Accept, Authorization, X-Request-With');
header('Access-Control-Allow-Credentials: true');

$porta = query_string('1');

$dados_stm = mysqli_fetch_array(mysqli_query($conexao,"SELECT * FROM streamings where porta = '".$porta."'"));
$dados_servidor = mysqli_fetch_array(mysqli_query($conexao,"SELECT * FROM servidores where codigo = '".$dados_stm["codigo_servidor"]."'"));

if(query_string('2') == "currentsong") {

if($dados_stm["tipo_servidor"] == "icecast") {

$porta_admin_icecast = $dados_stm["porta"]+1;

$json_icecast = utf8_encode(file_get_contents("http://".$dados_servidor["ip"].":".$porta_admin_icecast."/status-json.xsl?mount=/stream"));
$stats_icecast = json_decode($json_icecast,TRUE);

$musica = ($stats_icecast["icestats"]["source"]["artist"]) ? $stats_icecast["icestats"]["source"]["artist"]." - ".$stats_icecast["icestats"]["source"]["title"] : $stats_icecast["icestats"]["source"]["title"];

$musica = str_replace("_"," ",$musica);
$musica = str_replace("+"," ",$musica);
$musica = str_replace("_-_"," - ",$musica);

} else {

$musica = @file_get_contents("http://".$dados_servidor["ip"].":".$dados_stm["porta"]."/currentsong");
$musica = str_replace("_"," ",$musica);
$musica = str_replace("+"," ",$musica);
$musica = str_replace("_-_"," - ",$musica);

}

echo $musica;
exit();
}

if(query_string('2') == "title") {

$resultado = @file_get_contents("http://".$dados_servidor["ip"].":".$dados_stm["porta"]."/stats");

$xml = @simplexml_load_string(utf8_encode($resultado));

echo $xml->SERVERTITLE;

exit();
}

if(query_string('2') == "7.html") {

echo @file_get_contents("http://".$dados_servidor["ip"].":".$dados_stm["porta"]."/7.html");

exit();
}

if(query_string('2') == "stats?sid=1") {

header('Content-type: text/xml');

echo @file_get_contents("http://".$dados_servidor["ip"].":".$dados_stm["porta"]."/stats?sid=1");

exit();
}

if(query_string('2') == "played.html") {

echo @file_get_contents("http://".$dados_servidor["ip"].":".$dados_stm["porta"]."/played.html");

exit();
}

$fp = @fsockopen ($dados_servidor["ip"], $dados_stm["porta"], $errno, $errstr, 10);
if ($fp) {
        header("Content-type: audio/mpeg");
        $request = "GET / HTTP/1.0\r\n";
        $request .= "Host: ".$_SERVER['REMOTE_ADDR']."\r\n";
        $request .= "User-Agent: Proxy/".$_SERVER['REMOTE_ADDR']."\r\n";
        $request .= "Accept: */*\r\n";
//      $request .= "Icy-MetaData:1\r\n";
        $request .= "Connection: close\r\n\r\n";
        fputs ($fp, $request);
        while (!feof($fp)) {
                $data = fgets($fp,128);
                if (@strpos($data,"icy-") > -1 || @strpos($data,"ICY ") > -1 || @strpos($data,"content-type") > -1) {
                     header($data);
                } else {
                        echo $data;
                }
        }
        fclose ($fp);
}

?>
