<?php
$porta = query_string('1');

if(!is_numeric($porta)) {
die ("Error! Missing data.");
}

$verifica_stm = mysqli_num_rows(mysqli_query($conexao,"SELECT * FROM streamings where porta = '".$porta."'"));

if($verifica_stm == 0) {
die ("Error! Missing data.");
}

$dados_config = @mysqli_fetch_array(@mysqli_query($conexao,"SELECT * FROM configuracoes"));
$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"]."'"));

$total_pontos = mysqli_num_rows(mysqli_query($conexao,"SELECT * FROM multipoint where codigo_stm = '".$dados_stm["codigo"]."'"));

if($total_pontos > 0) {
$ponto = "/1";
} else {
$ponto = "/stream";
}

$url_streaming = "https://".strtolower($dados_servidor["nome"]).".".$dados_config["dominio_padrao"].":".$dados_stm["porta"]."";

if($dados_stm["tipo_servidor"] == "icecast") {
$url_streaming_players = $url_streaming."/".$ponto.".m3u";
} else {
$url_streaming_players = $url_streaming."/listen.m3u?sid=1";
}

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

    function getCapaArtista($str) {
    $termo = urlencode($str);
    $file = file_get_contents('https://itunes.apple.com/search?term=' . $termo . '&media=music&limit=1');
    $jsoncapa = json_decode($file, true);
    if ($jsoncapa['resultCount'] == 1):
        $image60 = (!empty($jsoncapa['results'][0]['artworkUrl60']) ? $jsoncapa['results'][0]['artworkUrl60'] : '');
        $image100 = (!empty($jsoncapa['results'][0]['artworkUrl100']) ? $jsoncapa['results'][0]['artworkUrl100'] : '');
        return str_replace(['60x60', '100x100'], '600x600', ($image100 ? $image100 : $image60));
    else:
        return false;
    endif;
}

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

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

       $dados_icecast = @file_get_contents("http://".$dados_servidor["ip"].":".$porta_admin_icecast."/status-json.xsl?mount=".$ponto);

       $dados_json_icecast = json_decode($dados_icecast, true);

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

    } else {
       $musica = @file_get_contents("http://".$dados_servidor["ip"].":".$dados_stm["porta"]."/currentsong?sid=1");
    }

    if($musica):
        $capa = getCapaArtista($musica);
       echo json_encode(['status' => 'on','musica' => $musica, 'capa' => ($capa ? $capa : '/img/img-player-full-capa.png')]);
    else:
        echo json_encode(['status' => 'off','capa' => '/img/img-player-full-capa.png']);
    endif;    
   
    die();
}
 
?>  
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Player</title>
        <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
        <link href="/playertopo6/dist/font-awesome/css/font-awesome.css" rel="stylesheet" type="text/css"/> 
        <script src="/playertopo6/dist/jQuery/jquery.js" type="text/javascript"></script>
        <script type="text/javascript" src="https://hosted.muses.org/mrp.js"></script>
        <style>body{padding:0;margin:0;font-family:'Open Sans',sans-serif}.player-muses{background:linear-gradient(to left,rgba(71,85,111,.4),rgba(71,85,111,.4),rgba(71,85,111,.4)),url(/playertopo5/images/END5y1XR.jpg);background-position:center;background-size:contain;position:relative;filter:lighter(10px);padding:3px 10px}.campo-player{width:200px;margin-left:10%}.icone-player{position:absolute;float:right;display:flex;list-style:none;padding:0;margin:0;top:0;right:10%}.icone-player a{padding:10px;width:50px;color:#3a8093;text-align:center;height:50px;display:block}.icone-player a:hover{transform:rotatey(180deg);transition:ease-in-out .3s;color:#099fe8}.texto-musica{position:absolute;left:25%;top:10px;border:2px solid #466973;background:rgba(71,85,111,.6);padding:10px;color:#fff;width:350px;height:25px;overflow:hidden}@media (max-width:1020px){.player-muses{background:#46535e;position:relative;padding:5px 10px}.campo-player{width:150px;margin-left:0}.icone-player{position:absolute;float:right;display:flex;list-style:none;padding:0;margin:0;top:22px;right:5%}.icone-player a{font-size:.5em;padding:0;width:30px;color:#3a8093;text-align:center;height:20px;display:block}.texto-musica{right:5px;top:auto;bottom:15px;width:250px;height:25px}}@media (max-width:768px){.texto-musica{font-size:12px;left:150px;top:20px;bottom:5px;width:170px;height:25px;padding:3px}#apple{display:none}#windows{display:none}#android{display:none}}</style>
    </head>
    <body>
        <div class="player-muses">
            <div class="campo-player">
                <script type="text/javascript">
                    MRP.insert({
                        'url': '<?php echo $url_streaming;?><?php echo $ponto;?>',
                        'codec': 'mp3',
                        'volume': 100,
                        'autoplay': false,
                        'forceHTML5': true,
                        'jsevents': false,
                        'buffering': 0,
                        'title': 'Player',
                        'wmode': 'transparent',
                        'skin': 'e76', //e76
                        'width': 191,
                        'height': 62
                    });
                    var getStatusStreaming = function () {

                        $.ajax({url: "<?php echo "https://".$_SERVER['HTTP_HOST']."/player-topo-6/".$dados_stm["porta"]."/info";?>", timeout: 30000, dataType: "json", success: function (response) {

                                if (response.status) {
                                    $('.texto-musica').html("<marquee scrolldelay='200'>" + response.musica + "</marquee>");                                    
                                } else {
                                    $('.texto-musica').html("<marquee scrolldelay='200'>Desligado</marquee>");                                   
                                }
                                
                                 $('.fc_capa').html("<img src='" + data.capa + "'/>");
                            }, error: function (e) {
                                console.log(e);
                            }});
                        setTimeout(getStatusStreaming, 30000);
                    };
                    getStatusStreaming();
                </script>
            </div>
            <div class="texto-musica"></div>
            <ul class="icone-player">
                <li><a href="<?php echo $url_streaming_players;?>" target="_blank" title="android" id="android"><i class="fa fa-android fa-3x"></i></a></li>
                <li><a href="<?php echo $url_streaming_players;?>" target="_blank" title="apple" id="apple"><i class="fa fa-apple fa-3x"></i></a></li>
                <li><a href="<?php echo $url_streaming_players;?>" target="_blank" title="windows" id="windows"><i class="fa fa-windows fa-3x"></i></a></li>
                <?php if(query_string('2')) { ?>
                <li><a href="http://www.facebook.com/<?php echo query_string('2'); ?>" target="_blank" title="facebook"><i class="fa fa-facebook fa-3x"></i></a></li>
                <?php } ?>
                <?php if(query_string('3')) { ?>
                <li><a href="https://instagram.com/<?php echo query_string('3'); ?>" target="_blank" title="instagram"><i class="fa fa-instagram fa-3x"></i></a></li>
                <?php } ?>
                <?php if(query_string('4')) { ?>
                <li><a href="https://api.whatsapp.com/send?phone=<?php echo query_string('4');?>" target="_blank" title="whatsapp"><i class="fa fa-whatsapp fa-3x"></i></a></li>
                <?php } ?>
                <?php if(query_string('5')) { ?>
                <li><a href="https://twitter.com/<?php echo query_string('5'); ?>" target="_blank" title="twitter"><i class="fa fa-twitter fa-3x"></i></a></li>
                <?php } ?>
            </ul>
        </div>                 
    </body>
</html>

