<!DOCTYPE html>
<html lang="fr-FR" prefix="og: http://ogp.me/ns#">
    <head>
        <meta HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
        <meta HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
        <meta charset="UTF-8" />
    </head>
<body style="background: #5A72B7; color: white; font-family: arial;">
<?php

//script icecast
$server = "http://flux.radiodesballons.com:8000";
$mount = $_GET['stream'];

function getCoverOf($track_name)
    {
        $api_endpoint = 'https://api.deezer.com/2.0/search';
        $search_parameters = array(
            'q' => $track_name,
            'output' => 'json',
            'nb_items' => 1
        );
        $search_uri = $api_endpoint
                      .'?'
                      .http_build_query($search_parameters);

        $track = json_decode(file_get_contents($search_uri));

        return empty($track->data)
            ?: $track->data[0]->album->cover;
    }


function getStreamStatus($server, $mount){
        $header = get_headers($server.$mount);
        if($header[0] == 'HTTP/1.0 200 OK'){
                return 'On Air';
        }
        else{
                return  'Off Air';
        }
}


function getStreamInfo($server, $mount){
        $output = file_get_contents($server.'/status.xsl?mount='.$mount);
        $temp_array = array(); 
        $search_for = "<td\s[^>]*class=\"streamstats\">(.*)<\/td>"; 
        $search_td = array('<td class="streamstats">','</td>'); 

        if(preg_match_all("/$search_for/siU",$output,$matches)) { 
           foreach($matches[0] as $match) { 
                  $to_push = str_replace($search_td,'',$match); 
                  $to_push = trim($to_push); 
                  array_push($temp_array,$to_push); 
           } 
        }
                 
        $stream_info['currently_playing'] = $temp_array[5];
                $x = explode(" - ", $temp_array[5]); 
        $stream_info['artiste'] = $x[0]; 
        $stream_info['titre'] = $x[1];
//      print_r($stream_info);
        return $stream_info;
}

$etat = getStreamStatus($server, $mount);
$stream_info = getStreamInfo($server, $mount);
$interpTitre = $stream_info['currently_playing'];
$titre = $stream_info['titre'];
$interp = $stream_info['artiste'];
$lienInfo="https://www.deezer.com/search/".$interpTitre;
$cover = getCoverOf($interp." ".$titre);

// Si zcover non référencée
if ($cover=="1") {
    $cover = "img/rdb.png";
}

?>
<table>
    <tr>
        <td style="width: 25%;">
        <a title="Plus d'infos sur ce titre" target="_blank" href="<?php echo $lienInfo; ?>">
        <img width="60" src="<?php echo $cover; ?>"></a>
        </td>
        <td style="width: 75%;">
        <b><?php echo $interp; ?></b><br />
        <?php echo $titre; ?>
        </td>
    </tr>
</table>
</body>
</html>