<?php
$porta = query_string('1');
$cor_fundo = (query_string('2') != "") ? str_replace("#","",query_string('2')) : "000000";

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.");
}

$facebook = (query_string('3')) ? '<li class="facebook opcoes " data-opcao=".facebook-shared"><i class="fa fa-facebook"></i> <span> Facebook</span></li>' : '';
$instagram = (query_string('4')) ? '<li class="instagram "><a href="https://instagram.com/'.query_string('4').'"><i class="fa fa-instagram"></i> <span>Instagram</span></a></li>' : '';
$whatsapp = (query_string('5')) ? '<li class="whatsapp opcoes " data-opcao=".whatsapp-shared"><i class="fa fa-whatsapp"></i> <span> Whatsapp</span></li>' : '';
$twitter = (query_string('6')) ? '<li class="twitter "><a href="https://twitter.com/'.query_string('6').'" ><i class="fa fa-twitter"></i> <span>Twitter</span></a></li>' : '';

$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 ($_POST) {

    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="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
        <link href="/playertopo5/dist/font-awesome/css/font-awesome.css" rel="stylesheet" type="text/css"/>  
        <link href="/playertopo5/dist/bootstrap-slider/slider.css" rel="stylesheet" type="text/css"/>
        <link href="/playertopo5/paodeacucar.css" rel="stylesheet" type="text/css"/>
        <script src="/playertopo5/dist/jQuery/jquery.js" type="text/javascript"></script>           
        <script src="/playertopo5/dist/bootstrap-slider/bootstrap-slider.js" type="text/javascript"></script>
        <script src="/playertopo5/paodeacucar.js"></script>
    </head> 
    <body class=" " >
        <div class="bodyback"></div>
        <div class='player' data-pedido="/pedido/<?php echo $dados_stm["porta"];?>" data-porta="<?php echo $dados_stm["porta"];?>" data-autoplay='0'  data-api="https://braudio.com.br/api/json/" data-url="<?php echo $url_streaming;?><?php echo $ponto; ?>" ></div>

        <div class="amazonia-content" style='background:#<?php echo $cor_fundo;?>;'>
            <span class='balanco-left balanco'></span>
            <span class='balanco-right balanco'></span>
            <div class="spacks-left"></div>
            <div class="amazonia-player">
                <div class="radio-play ligar-desligar on"></div>
                <div class="radio-volume">
                    <div class="volume">
                        <input type="slider" class="span2" value="" data-slider-min="0" data-slider-max="10" data-slider-step="1" data-slider-value="5" data-slider-id="GC" id="volume" data-slider-tooltip="true" data-slider-handle="custom" />
                    </div>
                </div>
                <div class="musica_atuals"></div>
            </div>
            <div class="amazonia-center">        
                <div class="back-left"></div>
                <canvas id="mono-L"   height="120" ></canvas>
                <div class="amazonia-capa fc_capa_back"></div>
                <canvas id="mono-R"   height="120" ></canvas>
            </div> 
            <div class="amazonia-shared">
                <ul class="nav-shared">
                    <?php if($facebook || $instagram || $whatsapp || $twitter) { ?>
                    <li class="nav-item"><a href="#share" class='acao-nav' data-menu='.social'><i class="fa fa-share-alt-square"></i></a>
                        <ul class="submenu social">
                            <?php echo $facebook;?>
                            <?php echo $instagram;?>
                            <?php echo $whatsapp;?>
                            <?php echo $twitter;?>
                        </ul>
                    </li>
                    <?php } ?>
                    <li class="nav-item hidden-sm"><a href="#share" class='acao-nav' data-menu='.links'><i class="fa fa-external-link-square"></i></a>
                        <ul class="submenu links" >
                            <li><a target="_blank" class="ico-hide" href="<?php echo $url_source_shoutcast;?>/listen.pls?sid=1"><img src="/playertopo5/images/icones/img-icone-player-winamp.png" width="32" height="32" title="Ouvir no Winamp" data-toggle="tooltip" /><span> Winamp</span></a></li>
                            <li><a target="_blank" class="ico-hide" href="<?php echo $url_source_shoutcast;?>/listen.asx?sid=1"><img src="/playertopo5/images/icones/img-icone-player-mediaplayer.png" width="32" height="32" title="Ouvir no MediaPlayer" data-toggle="tooltip"  /><span> Mediaplayer</span></a></li>
                            <li><a target="_blank" class="ico-hide" href="<?php echo $url_source_shoutcast;?>/listen.ram?sid=1"><img src="/playertopo5/images/icones/img-icone-player-realplayer.png" width="32" height="32" title="Ouvir no RealPlayer" data-toggle="tooltip" /><span> Realplayer</span></a></li>
                            <li><a target="_blank" class="ico-hide" href="<?php echo $url_source_shoutcast;?>/listen.m3u?sid=1"><img src="/playertopo5/images/icones/img-icone-player-vlc.png" width="32" height="32" title="Ouvir no RealPlayer" data-toggle="tooltip" /><span> Vlc</span></a></li>
                            <li><a target="_blank" class="ico-hide" href="<?php echo $url_source_shoutcast;?>/listen.qtl?sid=1"><img src="/playertopo5/images/icones/img-icone-player-qt.png" width="32" height="32" title="Ouvir no RealPlayer" data-toggle="tooltip" /><span> Quicktime</span></a></li>
                            <li><a target="_blank" href="<?php echo $url_source_shoutcast;?>/listen.m3u?sid=1" class="init"><img src="/playertopo5/images/icones/img-icone-player-iphone.png" width="32" height="32" title="Ouvir no iphone" data-toggle="tooltip" /><span> Iphone</span></a></li>
                        </ul>
                    </li>

                    <li class="nav-item hidden-sm"><a href="#share" class='acao-nav' data-menu='.pedido' ><i class="fa fa-comment"></i></a>
                        <ul class="submenu pedido" >
                            <span>Pedir M&uacute;sica</span> 
                            <li><input class="pedido_nome" placeholder="Seu nome" required="" ></li> 
                            <li><input class="pedido_email" placeholder="Seu email" required="" ></li> 
                            <li><input class="pedido_artista" placeholder="Artista" required="" ></li> 
                            <li><input class="pedido_musica" placeholder="M&uacute;sica" required="" ></li> 
                            <li><button class="btn-enviar btn-pedido">Enviar</button></li> 
                        </ul>
                    </li>
                </ul>
                <div class='opcional'>
                    <ul class="shared-opcional facebook-shared">
                        <li class=""><a href='https://fb.com/<?php echo query_string('3');?>' target="_blank"><i class="fa fa-external-link"></i> <span> Acesse nosso facebook</span></a></li>
                        <li class=""><a href='http://www.facebook.com/sharer/sharer.php?app_id=&display=popup&redirect_uri=<?php echo "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];?>&u=<?php echo "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];?>' target="_blank"><i class="fa fa-external-link"></i> <span>Compartilhe o player no facebook</span></a></li>
                    </ul>
                    <ul class="shared-opcional whatsapp-shared">
                        <li class=""><a href='https://api.whatsapp.com/send?phone=<?php echo query_string('5');?>&text=Pedido' target="_blank"><i class="fa fa-external-link"></i> <span> Pedir M&uacute;sica</span></a></li>
                        <li class=""><a href='https://api.whatsapp.com/send/?phone&text=Radio:%20<?php echo "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];?>' target="_blank" ><i class="fa fa-external-link"></i> <span>Compartilhe o player no whatsapp</span></a></li>
                    </ul>
                </div>
                <div class="fechar"><i class="fa fa-times fa-2x"></i></div>
            </div>
            <div class="spacks-right"></div>
        </div>

    </body>
</html>