<?php
ERROR_REPORTING(0);
$fname = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'];
header("Content-type: text/xml; charset=utf-8");
$xml = "<list>\n";
if ($_SERVER[QUERY_STRING]) {
        $query = explode('=', $_SERVER[QUERY_STRING]);
        switch ($query[0]) {
                case 'cid' :
                        $xml .= cid($query[1]);
                        break;
                case 'name' :
                        $xml .= name($query[1]);
                        break;
                case 'user' :
                        $xml .= user($query[1]);
                        break;
        }
} else {
        $xml .= songlist();
}
$xml .= "</list>\n";
//echo iconv('gbk','UTF-8',$xml);
echo $xml;
function name($str) {
        $str=urldecode($str);
        $name = explode('-', $str);
        $size = sizeof($name);
        $songname = urlencode($name[0]);
        if ($size > 1) {
                $singername = urlencode($name[1]);
                $surl = 'http://music.yahoo.cn/s?q=' . $songname . '+' . $singername . '&m=5';
        } else {
                $surl = "http://music.yahoo.cn/s?q=$songname&m=5";
        }
        $src = '';
        if (preg_match_all('|&url=([^"]+)" onclick|', curl($surl), $mp3)) {
                $mp3 = $mp3[1];
                for ($i = 0; $i <= 10; $i++) {
                        $src .= urldecode($mp3[$i]) . ',';
                }
                $src .= urldecode($mp3[11]);
        } else {
                $str = 'No  song';
        }
        $list = "<m type=\"1\" src=\"$src\" label=\"$str\"  />\n";
        return iconv('gbk', 'utf-8', $list);

}
function user($name) {
        $list = '';
        $names = str_replace('-', '+', $name);
        $surl = "http://music.yahoo.cn/s?q=" . urlencode($names) . "&m=5";
        preg_match_all('|<a href="down\.php\?title=([^"]+)"|', curl($surl), $mp3);
        foreach ($mp3[1] as $value) {
                $value = urldecode($value);
                $v = explode('&url=', $value);
                $list .= "<m src=\"$v[1]\" label=\"$v[0]\"  />\n";
        }
        return $list;
}

function cid($cid) {
        global $fname;
        $url = "http://music.yahoo.cn/toplist.php?cid=$cid";
        $str = curl($url);
        $list = '';
        preg_match('|<td width="1%">1.</td>(.*)<div id="foot">|ims', $str, $song);
        preg_match_all('|s\?q=([^"]+)" target="_blank"><span class="p20">|ims', $song[1], $songname);
        foreach ($songname[1] as $value) {
                $name = urldecode($value);
                if ($cid <= 37) {
                        $list .= "<m list_src=\"$fname?name=$name\" label=\"$name\" />\n ";
                } else {
                        $list .= "<m list_src=\"$fname?user=$name\" label=\"$name\" />\n ";
                }
        }
        return $list;
}
function songlist() {
        global $fname;
        preg_match_all('|<div class="nav_ml">([^<]+)</div>|ims', curl('http://music.yahoo.cn/toplist.php'), $toplist);
        $list = '';
        foreach ($toplist[1] as $k => $v) {
                $cid = $k +1;
                $list .= "<m list_src=\"$fname?cid=$cid\" label=\"$v\" />\n";
        }
        return $list;
}

function curl($url) {
        $user_agent = $_SERVER['HTTP_USER_AGENT'];
        $ref = 'http://music.yahoo.cn/';
        $ch = curl_init();
        $timeout = 30;
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_REFERER, $ref);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
}
?>