radio-tags / shoutcastcli.php /
4e17b94 7 years ago
1 contributor
652 lines | 23.929kb
<?php
$ERROR_STRING="An Error Occured";
function getSSLPage($url) {
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $result = curl_exec($ch);
      curl_close($ch);
      return $result;
}
function clean_string($string)
{
	//~ $search = array("&", "<", ">", '\\','u00e9','u00e0', 'u00e8', 'u20ac', 'U00e9', 'U00e0', 'U00c9', 'U00b0', 'u2019', 'U2019', 'u00ea', 'u00ea', 'u00e2', '    U00e2', 'u00f4', 'U00f4');
	//~ $replace = array("&amp;", "&lt;", "&gt;", '', 'e', 'a', 'e', 'e', 'e', 'a', 'e', '°', "'", "'", 'e', 'e', 'a', 'a', 'o', 'o' );
	//~ $substring = str_replace($search, $replace, $string);
	//~ echo "$string";
	$substring = iconv("UTF-8","ASCII//TRANSLIT", $string);
	$substring = strip_tags($substring);
	$search = array("&", "<", ">", '\\', 'N?', 'n?');
	$replace = array("&amp;", "&lt;", "&gt;", '', 'N°', 'n°');
	$substring = str_replace($search, $replace, $substring);
	return $substring;
}

function get_shoutcast_tags($string) {
	$headers = array();
	$metadata = array();
	$useragent = 'Winamp 2.81';
	$t = parse_url($string);
	$context = stream_context_create([
	  'ssl' => [
		'verify_peer' => false,
		'allow_self_signed' => true,
	  ],
	]);
    $sock = stream_socket_client('tls://'.$t['host'].':'.$t['port'], $errno, $errstr, 10, STREAM_CLIENT_CONNECT, $context); 
    //$sock = stream_socket_client('127.0.0.1:'.$t['port'], $errno, $errstr, 10, STREAM_CLIENT_CONNECT, $context);
    $path = isset($t['path'])?$t['path']:'/';
    if ($sock){
		$request = 'GET '.$path.' HTTP/1.0'."\r\n".
            'Host: '.$t['host']."\r\n".
            'Connection: Close'."\r\n". 
            'User-Agent: '.$useragent."\r\n".
            'Accept: */*'."\r\n".
            'icy-metadata: 1'."\r\n".
            'icy-prebuffer: 65536'."\r\n\r\n";
		if (fwrite($sock, $request)){
            $theaders = $line = '';
            $count=0;

            while (!feof($sock)){ 
                //~ $line = fgets($sock, 4096); 
                $line = fgets($sock, 16384); 
                $count += strlen($line);
                //~ echo ". ";
                if('' == trim($line)){
                    break;
                }                
                $theaders .= $line;
            }
			$theaders = explode("\r\n", $theaders);
            foreach ($theaders as $header){
                $t = explode(':', $header); 
                if (isset($t[0]) && trim($t[0]) != ''){
                    $name = preg_replace('/[^a-z][^a-z0-9]*/i','', strtolower(trim($t[0])));
                    array_shift($t);
                    $value = trim(implode(':', $t));
                    if ($value != ''){
                        if (is_numeric($value)){
                            $headers[$name] = (int)$value;
                        }else{
                            $headers[$name] = $value;
                        }
                    }
                }
            }
            if (!isset($headers['icymetaint'])){
                $data = ''; $metainterval = 512;
                while(!feof($sock)){
                    $data .= fgetc($sock);
                    if (strlen($data) >= $metainterval) break;
                }
                $matches = array();
                preg_match_all('/([\x00-\xff]{2})\x0\x0([a-z]+)=/i', $data, $matches, PREG_OFFSET_CAPTURE);
				preg_match_all('/([a-z]+)=([a-z0-9\(\)\[\]., ]+)/i', $data, $matches, PREG_SPLIT_NO_EMPTY);
                $title = $artist = '';
                foreach ($matches[0] as $nr => $values){
                  $offset = $values[1];
                  $length = ord($values[0]{0}) + 
                            (ord($values[0]{1}) * 256)+ 
                            (ord($values[0]{2}) * 256*256)+ 
                            (ord($values[0]{3}) * 256*256*256);
                  $info = substr($data, $offset + 4, $length);
                  $seperator = strpos($info, '=');
                  $metadata[substr($info, 0, $seperator)] = substr($info, $seperator + 1);
                    if (substr($info, 0, $seperator) == 'title') $title = substr($info, $seperator + 1);
                    if (substr($info, 0, $seperator) == 'artist') $artist = substr($info, $seperator + 1);
                }
                $metadata['streamtitle'] = $artist . ' - ' . $title;
            }else{
                $metainterval = $headers['icymetaint'];
                $intervals = 0;
                $metadata = '';
                while(1){
                    $data = '';
                    while(!feof($sock)){
                        $data .= fgetc($sock);
                        if (strlen($data) >= $metainterval) break;
                    }
                    //print_data($data);
                    //$len = join(unpack('c', fgetc($sock))) * 16;
                    $len = @join(@unpack('c', fgetc($sock))) * 16;
                    if ($len > 0){
                        $metadata = str_replace("\0", '', fread($sock, $len));
                        break;
                    }else{
                        $intervals++;
                        if ($intervals > 100) break;
                    }
                }
                //~ echo "metadata : [".$metadata."]\n";
                $metarr = explode(';', $metadata);
                foreach ($metarr as $meta){
					$pos_StreamTitle = strpos($meta, "StreamTitle=", 0 );
					if( 0 === $pos_StreamTitle )
					{
						$t = explode('=', $meta);
						if (isset($t[0]) && trim($t[0]) != ''){
							$name = preg_replace('/[^a-z][^a-z0-9]*/i','', strtolower(trim($t[0])));
							array_shift($t);
							$value = trim(implode('=', $t));
							if (substr($value, 0, 1) == '"' || substr($value, 0, 1) == "'"){
								$value = substr($value, 1);
							}
							if (substr($value, -1) == '"' || substr($value, -1) == "'"){
								$value = substr($value, 0, -1);
							}
							if ($value != ''){		
								$valuearr = explode(' - ', $value);
								return $valuearr;
							}
						}
					}
                }
            }
            fclose($sock);
		}

	} else {
		$metadata[0]=$ERROR_STRING."2";
		$metadata[1]=$errstr;
		return $metadata;
	}
}

function get_fipradio_tags() {
	$metadata = array();
	$jsondata=file_get_contents("http://www.fipradio.fr/sites/default/files/import_si/si_titre_antenne/FIP_player_current.json");
	$array_json = json_decode($jsondata,TRUE);
	$fipradio_artist = "An Error Occured";
	$fipradio_title = "An Error Occured";
	$fipradio_url = "";
	if( isset($array_json["current"]["song"]) ) {
		$fipradio_title=$array_json["current"]["song"]["titre"];
		if( isset($array_json["current"]["song"]["interpreteMorceau"]) ) {
			$fipradio_artist=$array_json["current"]["song"]["interpreteMorceau"];
		}
		$fipradio_url=$array_json["current"]["song"]["visuel"]["small"];
	}
	$metadata[0]=$fipradio_artist;
	$metadata[1]=$fipradio_title;
	$metadata[2]=$fipradio_url;
	return $metadata; 
}

function get_lemouv_tags() {
	$metadata = array();
	//~ $jsondata=file_get_contents("http://www.lemouv.fr/sites/default/files/direct.json");
	$jsondata=file_get_contents("http://www.mouv.fr/sites/default/files/import_si/si_titre_antenne/leMouv_player_current.json");
	$array_json = json_decode($jsondata,TRUE);
	//~ $lemouv_title=$array_json["rf_titre_antenne"]["titre"];
	//~ $lemouv_artist=$array_json["rf_titre_antenne"]["interprete"];
	//~ $lemouv_url=$array_json["rf_titre_antenne"]["image"];
	if( isset($array_json["current"]["song"]["titre"]) ) {
		$lemouv_title=$array_json["current"]["song"]["titre"];
	} else {
		$lemouv_title=$array_json["current"]["emission"]["titre"];
	}
	if( isset($array_json["current"]["song"]["interpreteMorceau"]) ) {
		$lemouv_artist=$array_json["current"]["song"]["interpreteMorceau"];
	} else {
		$lemouv_artist=$lemouv_title;
	}
	if( isset($array_json["current"]["song"]["visuel"]["small"]) ) {
		$lemouv_url=$array_json["current"]["song"]["visuel"]["small"];
		$SEARCHSUB='image=';
		$pos_start = strpos($lemouv_url, $SEARCHSUB, 0 );
		if( false !== $pos_start ) {
			$SEARCHSUB2='&';
			$pos_stop = strpos($lemouv_url, $SEARCHSUB2, $pos_start );
			$lemouv_url = substr($lemouv_url, $pos_start+strlen($SEARCHSUB), $pos_stop - $pos_start - strlen($SEARCHSUB) );
		}
	} else {
		$lemouv_url=$array_json["current"]["emission"]["visuel"]["small"];
	}
	//~ $lemouv_title=$array_json["current"]["emission"]["titre"];
	//~ $lemouv_artist=$array_json["current"]["emission"]["lien"];
	//~ $lemouv_url=$array_json["current"]["emission"]["visuel"]["small"];
	if( 0 == strcmp("null",$lemouv_title) )
	{
		$lemouv_title = "An Error Occured";
	}
	if( 0 == strcmp("null",$lemouv_artist) )
	{
		$lemouv_artist = "An Error Occured";
	}
	if( 0 == strcmp("null",$lemouv_url) )
	{
		$lemouv_url = "";
	}
	$metadata[0]=$lemouv_artist;
	$metadata[1]=$lemouv_title;
	$metadata[2]=$lemouv_url;
	return $metadata; 
}

function get_classic21_tags() {
	$currDate = time();
	$fake_json_data=file_get_contents("http://np.maradio.be/qp/v3/events?rpId=2&serviceNameSize=200&nameSize=200&artistNameSize=200&descriptionSize=200&callback=radioplayer.playing.receive&_=".$currDate );
	//~ echo "http://np.maradio.be/qp/v3/events?rpId=2&serviceNameSize=200&nameSize=200&artistNameSize=200&descriptionSize=200&callback=radioplayer.playing.receive&_=".$currDate;
	$pos_start = strlen("radioplayer.playing.receive(");
	$pos_end = strlen( $fake_json_data ) - 1;
	$jsondata = substr($fake_json_data, $pos_start, $pos_end - $pos_start );
	$array_json = json_decode($jsondata,TRUE);
	$classic21_artist = "An Error Occured";
	$classic21_title = "An Error Occured";
	$classic21_url = "";
	if( isset($array_json["results"]["now"]["name"]) ) {
		$classic21_title=$array_json["results"]["now"]["name"];
	}
	if( isset($array_json["results"]["now"]["artistName"]) ) {
		$classic21_artist=$array_json["results"]["now"]["artistName"];
	}
	$classic21_url=$array_json["results"]["now"]["imageUrl"];
	$metadata[0]=$classic21_artist;
	$metadata[1]=$classic21_title;
	$metadata[2]=$classic21_url;
	return $metadata; 
}

function get_radio_nova_tags() {
	$metadata = array();
	$radio_nova_artist = "An Error Occured";
	$radio_nova_title = "An Error Occured";
	$radio_nova_url = "";
	$jsondata=file_get_contents("http://www.novaplanet.com/radionova/ontheair");
	$array_json = json_decode($jsondata,TRUE);
	$htmldata=$array_json['track']['markup'];

	//title
	$SEARCHSUB='<div class="title">';
	//19 = strlen($SEARCHSUB);
	$pos_start = strpos($htmldata, $SEARCHSUB, 0 );
	if( false !== $pos_start ) {
		$SEARCHSUB='</div>';
		$pos_stop = strpos($htmldata, $SEARCHSUB, $pos_start );
		
		$radio_nova_title = substr($htmldata, $pos_start+19, $pos_stop - $pos_start - 19 );
	}

	//artist
	$SEARCHSUB='<div class="artist">';
	//20 = strlen($SEARCHSUB);
	$pos_start = strpos($htmldata, $SEARCHSUB, 0 );
	if( false !== $pos_start ) {
		$SEARCHSUB='</div>';
		$pos_stop = strpos($htmldata, $SEARCHSUB, $pos_start );
		
		$radio_nova_artist = substr($htmldata, $pos_start+20, $pos_stop - $pos_start - 20 );
		$pattern = '<a href';
		if (preg_match("/<a href/", $radio_nova_artist)) {
			$SEARCHSUB='>      ';
			$pos_start = strpos($radio_nova_artist, $SEARCHSUB, 0 );
			if( false !== $pos_start ) {
				$SEARCHSUB='    </a>';
				$pos_stop = strpos($radio_nova_artist, $SEARCHSUB, $pos_start );
				$radio_nova_artist = substr($radio_nova_artist, $pos_start+7, $pos_stop - $pos_start - 7 );
			}
		}
	}
	
	//url
	$SEARCHSUB='<img src="';
	//10 = strlen($SEARCHSUB);
	$pos_start = strpos($htmldata, $SEARCHSUB, 0 );
	if( false !== $pos_start ) {
		$SEARCHSUB = '" alt="" title="" width="84"';
		$pos_stop = strpos($htmldata, $SEARCHSUB, $pos_start );
		$radio_nova_url = substr($htmldata, $pos_start+10, $pos_stop - $pos_start - 10 );
	}
	$metadata[0]=$radio_nova_artist;
	$metadata[1]=$radio_nova_title;
	$metadata[2]=$radio_nova_url;
	return $metadata; 
}

function get_fculture_tags() {
	$metadata = array();
	$epochLocal = time();
	$jsondata=file_get_contents("http://www.franceculture.fr/programmes?xmlHttpRequest=1");
	$array_json = json_decode($jsondata,TRUE);
	$i = 0;
	$fculture_artist="An Error Occured";
	$fculture_title="An Error Occured";
	$metadata[0]=$fculture_artist;
	$metadata[1]=$fculture_title;
	while( isset($array_json[$i]) ) {
		$time_start=$array_json[$i]["start"];
		$time_stop=$array_json[$i]["end"];	
		if( $time_stop >= $epochLocal ) {
			if( isset($array_json[$i]["conceptTitle"])) {
			  $fculture_artist=$array_json[$i]["conceptTitle"];
			}
			if( isset($array_json[$i]["expressionTitle"])) {
			  $fculture_title=$array_json[$i]["expressionTitle"];
			}
			$metadata[0]=$fculture_artist;
			$metadata[1]=$fculture_title;
			return $metadata; 
		}
		$i++;
	}
	return $metadata; 
}

function get_fmusique_tags() {
	$metadata = array();
	$epochts=time();
	$fmusique_artist="An Error Occured";
	$fmusique_title="An Error Occured";
	$jsondata=file_get_contents("https://www.francemusique.fr/programmes?xmlHttpRequest=1");
	$array_json = json_decode($jsondata,TRUE);
	$epochLocal = $epochts-0*3600;
	$i=0;
	while(isset($array_json[$i]["start"])) {
		//~ if( ( $epochLocal < $array_json[$i]["end"] ) &&
		    //~ ( $epochLocal >= $array_json[$i]["start"]) ) {
			//~ if( isset($array_json[$i]["conceptTitle"]) ) {
				//~ $fmusique_artist=$array_json[$i]["conceptTitle"];
			//~ }
			//~ if( isset($array_json[$i]["expressionTitle"]) ) {
				//~ $fmusique_title=$array_json[$i]["expressionTitle"];
			//~ }
			//~ break;
		//~ }
		if( $epochLocal < $array_json[$i]["start"] ) {
			if( isset($array_json[$i-1]["conceptTitle"]) ) {
				$fmusique_artist=$array_json[$i-1]["conceptTitle"];
			}
			if( isset($array_json[$i-1]["expressionTitle"]) ) {
				$fmusique_title=$array_json[$i-1]["expressionTitle"];
			}
			break;
		}
		$i++;
	}
	$metadata[0]=$fmusique_artist;
	$metadata[1]=$fmusique_title;
	return $metadata; 
}

function get_finfo_tags() {
	$metadata = array();
	$epochts=time();
	$finfo_artist="An Error Occured";
	$finfo_title="An Error Occured";
	$html=file_get_contents("http://www.francetvinfo.fr/replay-radio/grille-des-emissions");
	$SEARCHSUB ='class="program__grid__subline " ';
	$pos_start = strpos($html, $SEARCHSUB, 0 );
	if( false !== $pos_start ) {
		$SEARCHSUB2='<span class="program__grid__line__right__duration">';
		$pos_stop = strpos($html, $SEARCHSUB2, $pos_start );
		$item_content = substr($html, $pos_start+strlen($SEARCHSUB), $pos_stop - $pos_start - strlen($SEARCHSUB) );
		$SEARCHSUB='class="program__grid__line__right__title"> ';
		$pos_start = strpos($item_content, $SEARCHSUB, 0 );
		if( false !== $pos_start ) {
			$SEARCHSUB2='</a>';
			$pos_stop = strpos($item_content, $SEARCHSUB2, $pos_start );
			$finfo_title = substr($item_content, $pos_start+strlen($SEARCHSUB), $pos_stop - $pos_start - strlen($SEARCHSUB) );
			//~ $finfo_title = substr($item_content, $pos_start+strlen($SEARCHSUB));
			//~ echo "=====\n";
		}
		$finfo_title=trim($finfo_title);
		//~ echo $finfo_title;
	}
	$metadata[0]=$finfo_artist;
	$metadata[1]=$finfo_title;
	return $metadata; 
}

function get_finter_tags() {
	$metadata = array();
	$epochts=time();
	$epochLocal = $epochts;
	$modSeconds=$epochts % 3600;
	$modHours = $epochts / 3600 % 24;
	$epochts=$epochts - $modHours*3600 - $modSeconds - 7200;
	$finter_artist="An Error Occured";
	$finter_title="An Error Occured";
	$metadata[0]=$finter_artist;
	$metadata[1]=$finter_title;
	$jsondata=file_get_contents("https://www.franceinter.fr/programmes");
	$SEARCH_STRING='class="onair-element-content-title">';
	$pos_start = strpos($jsondata, $SEARCH_STRING );
	$title="";
	$artist="";
	if( false !== $pos_start ) {
		$SEARCH_STRING2="</a>";
		$pos_stop = strpos($jsondata, $SEARCH_STRING2, $pos_start );
		if( false !== $pos_stop ) {
			$artist = substr($jsondata,$pos_start+strlen($SEARCH_STRING), $pos_stop - ($pos_start+strlen($SEARCH_STRING)));
			$SEARCH_STRING=' class="onair-element-content-text">';
			$pos_start = strpos($jsondata, $SEARCH_STRING, $pos_stop );
			if( false !== $pos_start ) {
				$SEARCH_STRING2="</a>";
				$pos_stop = strpos($jsondata, $SEARCH_STRING2, $pos_start );
				$title = substr($jsondata,$pos_start+strlen($SEARCH_STRING), $pos_stop - ($pos_start+strlen($SEARCH_STRING)));
			}
		}
		$metadata[0]=$artist;
		$metadata[1]=$title;
	}
	return $metadata;
}

function get_lgrrock_url() {
	$SEARCHSUB='<img class="border_gris" src="http://www.lagrosseradio.com/artiste/images/';
	$htmldata = file_get_contents("http://www.lagrosseradio.com/_include/ajax_global.php?type_page=0&top_good=-5");
	$pos_start = strpos($htmldata, $SEARCHSUB, 0 );
	if( false !== $pos_start ) {
		$SEARCHSUB='" width="80" height="80"';
		$pos_stop = strpos($htmldata, $SEARCHSUB, $pos_start );
		//30 = strlen('<img class="border_gris" src="');
		$url = substr($htmldata, $pos_start+30, $pos_stop - $pos_start - 30 );
		return $url;
	}
	return "";
}

function get_lgrmetal_url() {
	$SEARCHSUB='<img class="border_gris" src="http://www.lagrosseradio.com/artiste/images/';
	$htmldata = file_get_contents("http://www.lagrosseradio.com/_include/ajax_global.php?type_page=0&top_good=-5");
	$pos_start = strpos($htmldata, $SEARCHSUB, 0 );
	if( false !== $pos_start ) {
		$SEARCHSUB='<img class="border_gris" src="http://www.lagrosseradio.com/artiste/images/art_pochettes/';
		$pos_start2 = strpos($htmldata, $SEARCHSUB, $pos_start+strlen($SEARCHSUB) );
		if( false !== $pos_start2 ) {
			$SEARCHSUB='" width="80" height="80"';
			$pos_stop = strpos($htmldata, $SEARCHSUB, $pos_start2 );
			//30 = strlen('<img class="border_gris" src="');
			$url = substr($htmldata, $pos_start2+30, $pos_stop - $pos_start2 - 30 );
			return $url;
		}
	}
	return "";
}

function get_lgrreggae_url() {
	$SEARCHSUB='<img class="border_gris" src="http://www.lagrosseradio.com/artiste/images/';
	$htmldata = file_get_contents("http://www.lagrosseradio.com/_include/ajax_global.php?type_page=0&top_good=-5");
	$pos_start = strpos($htmldata, $SEARCHSUB, 0 );
	if( false !== $pos_start ) {
		$pos_start2 = strpos($htmldata, $SEARCHSUB, $pos_start+strlen($SEARCHSUB) );
		if( false !== $pos_start2 ) {
			$pos_start3 = strpos($htmldata, $SEARCHSUB, $pos_start2+strlen($SEARCHSUB) );
			if( false !== $pos_start3 ) {
				$SEARCHSUB='" width="80" height="80"';
				$pos_stop = strpos($htmldata, $SEARCHSUB, $pos_start3 );
				//30 = strlen('<img class="border_gris" src="');
				$url = substr($htmldata, $pos_start3+30, $pos_stop - $pos_start3 - 30 );
				return $url;
			}
		}
	}
	return "";
}

function print_data($data){
    $data = str_split($data);
    $c = 0;
    $string = '';
    echo "<pre>\n000000 ";
    foreach ($data as $char){
        $string .= addcslashes($char, "\n\r\0\t");
        $hex = dechex(join(unpack('C', $char)));
        if ($c % 4 == 0) echo ' ';
        if ($c % (4*4) == 0 && $c != 0){
          foreach (str_split($string) as $s){
            //echo " $string\n";
            if (ord($s) < 32 || ord($s) > 126){
              echo '\\'.ord($s);
            }else{
              echo $s;
            }
          }
          echo "\n";
          $string = '';
          echo str_pad($c, 6, '0', STR_PAD_LEFT).'  ';
        }
        if (strlen($hex) < 1) $hex = '00';
        if (strlen($hex) < 2) $hex = '0'.$hex;
          echo $hex.' ';
        $c++;
    }
    echo "  $string\n</pre>";
}

function get_rfi_tags() {
	$jsondata=file_get_contents("http://www.rfi.fr/current-program-grid-info/monde/?xmlHttpRequest=1");
	$array_json = json_decode($jsondata,TRUE);
	$rfimonde_artist = "An Error Occured";
	$rfimonde_title = "An Error Occured";
	$rfimonde_url = "";
	if( isset($array_json["currentProgramTitle"]) ) {
		$rfimonde_title=$array_json["currentProgramTitle"];
	}
	//~ if( isset($array_json["results"]["now"]["artistName"]) ) {
		//~ $rfimonde_artist=$array_json["results"]["now"]["artistName"];
	//~ }
	//~ $rfimonde_url=$array_json["results"]["now"]["imageUrl"];
	$metadata[0]=$rfimonde_artist;
	$metadata[1]=$rfimonde_title;
	$metadata[2]=$rfimonde_url;
	return $metadata; 
}

$DEBUG=0;
if( $DEBUG != 0 )
{
	$STREAMURL="http://67.212.163.150:80/;stream.nsv";
	echo "[DEBUG] stream : $STREAMURL<br>\n";
	clean_string('http://str45.streamakaci.com:8014/;895614762790501stream.nsv');
	$tags = array();
	$tags = get_shoutcast_tags($STREAMURL);
}


//~ echo "Load done\n";
//~ exit(0);
//$xsl_contents = file_get_contents( "https://files.kawi.fr:8000/status.xsl" );
$xsl_contents = getSSLPage("https://files.kawi.fr:8000/status.xsl" );
$xsl_contents_utf8 = mb_convert_encoding($xsl_contents, 'HTML-ENTITIES', "UTF-8");
$doc = new DOMDocument();
$doc->loadHTML($xsl_contents_utf8);
//~ $doc->loadHTML($xsl_contents);
		
$file_contents_playlist=file_get_contents( "playlist.xml" );
$xml = simplexml_load_string($file_contents_playlist);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
$CPTMAX=0;
foreach ( $array as $channel_list )
{
	echo "<?xml version='1.0' encoding='UTF-8'?>\n";
	echo "<channel_list>\n";
	foreach ( $channel_list as $channel )
	{
		$track_artist=$ERROR_STRING;
		$track_title=$ERROR_STRING;
		$track_url="";
		$track=array();

		//JS:var mountpoints = div.getElementsByTagName("h3");
		if( 1 == $channel['icy'] ) {
			$i = 0;
			foreach ($doc->getElementsByTagName('h3') as $node) {
				$mountpoint_raw=$node->textContent;
				$mountpoint_name = substr($mountpoint_raw, 13, strlen($mountpoint_raw) - 13);
				if( 0 == strcmp( $channel['shortname'], $mountpoint_name ) ) {
					$j = 0;
					foreach ( $doc->getElementsByTagName('table') as $table ) {
						if( $j == 2*$i+1 ) {
							$streamdata_raw=$table->textContent;
							$SEARCHSUB="Current Song:";
							$pos_start = strpos($streamdata_raw, $SEARCHSUB, 0 );
							if( false !== $pos_start ) {
								$streamdata_title_raw = substr($streamdata_raw, $pos_start+strlen($SEARCHSUB));
								$SEARCHSUB=" - ";
								$pos_start = strpos($streamdata_title_raw, $SEARCHSUB, 0 );
							}
							if(  false !== $pos_start ) {
								$track_artist=substr($streamdata_title_raw,0,$pos_start);
								$track_title=substr($streamdata_title_raw,$pos_start+strlen($SEARCHSUB));
							} else {
								$track_title=$streamdata_title_raw;
							}
						}
						$j++;
					}
				}	
				$i++;			
			}
		} else {
			//JSON or OTHERS tags
			$track_artist=$ERROR_STRING;
			$track_title=$ERROR_STRING;
			$track_url="";
			if( 0 == strcmp($channel['shortname'],"fipradio") ) {
				$track=get_fipradio_tags();
			} else if( 0 == strcmp($channel['shortname'],"lemouv") ) {
				$track=get_lemouv_tags();
			} else if( 0 == strcmp($channel['shortname'],"classic21") ) {
				$track=get_classic21_tags();
			} else if( 0 == strcmp($channel['shortname'],"radio_nova") ) {
				$track=get_radio_nova_tags();
			} else if( 0 == strcmp($channel['shortname'],"finfo") ) {
				$track=get_finfo_tags();
			} else if( 0 == strcmp($channel['shortname'],"finter") ) {
				$track=get_finter_tags();
			} else if( 0 == strcmp($channel['shortname'],"fculture") ) {
				$track=get_fculture_tags();
			} else if( 0 == strcmp($channel['shortname'],"fmusique") ) {
				$track=get_fmusique_tags();
			} else if( 0 == strcmp($channel['shortname'],"rfi") ) {
				$track=get_rfi_tags();
			}
			
			if( isset( $track[0] ) ) {
				$track_artist=trim($track[0]);
			}
			if( isset( $track[1] ) ) {
				$track_title=trim($track[1]);
			}
			if( isset( $track[2] ) ) {
				$track_url=trim($track[2]);
			}
		}
		$track_artist=clean_string($track_artist);
		$track_title=clean_string($track_title);
		echo "<channel>\n";
		echo "\t<title>".$channel['title']."</title>\n";
		echo "\t<shortname>".$channel['shortname']."</shortname>\n";
		echo "\t<track_artist>".$track_artist."</track_artist>\n";
		echo "\t<track_title>".$track_title."</track_title>\n";
		echo "\t<track_url>".$track_url."</track_url>\n";
		echo "</channel>\n";
	}
	echo "</channel_list>\n";
}
?>