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

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($dados_stm["tipo_servidor"] == "icecast") {

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

$json_icecast = @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 = utf8_decode($musica);
$musica = str_replace("_"," ",$musica);
$musica = str_replace("+"," ",$musica);
$musica = str_replace("_-_"," - ",$musica);

}

if(query_string('2')) {
echo '<html><body>0,1,1,UNLIMITED,0,128,'.utf8_decode($musica).'</body></html>';
} else {
echo utf8_decode($musica);
}

exit();