<?php
ERROR_REPORTING(0);
header("Content-type: text/html; charset=utf-8");
$fname = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'];
$query = $_SERVER[QUERY_STRING];
parse_str($query);
if ($yahoo) {
	yahoo(urlencode($yahoo));
	exit;
}
if ($sougou) {
	sougou(urlencode($sougou));
	exit;
}
if ($baidu) {
	baidu(urlencode($baidu));
	exit;
}
if ($sina) {
	sina(urlencode($sina));
	exit;
}
if ($qq) {
	qq($qq);
	exit;
}
if ($kugou) {
	kugou($kugou);
	exit;
}
if ($hash) {
	hashcode($hash);
	exit;
}
if ($kuwo) {
	kuwo($kuwo);
	exit;
}if ($kglist) {
	kglist($kglist);
	exit;
}
 else {
	echo "支持六种格式，?kugou=歌曲名称[强力推荐],  ?sina=歌曲名称[推荐], ?kuwo=歌曲名称[防盗链ing] ,?yahoo=歌曲名称[不推荐] ?baidu=歌曲名称[不推荐] ?sougou=歌曲名称[推荐],     ?qq=歌曲名称[BUG_ing]";
}
function kglist($str) {
	$thisurl='http://' . $_SERVER['SERVER_NAME'] . $_SERVER["SCRIPT_NAME"];
	$str=iconv('utf-8','gbk',$str);
	$str = urlencode($str);
	$url = "http://61.142.208.217:8003/service/search.aspx?page=1&pagesize=200&code=gbk&xml=3&HighQuality=0&keywords=$str";
	$json = iconv('gbk', 'utf-8', file_get_contents($url)) ;
	$json = json_decode($json);
	$data=$json->data;
	$list="<list>\n";
	foreach($data as $obj){
		$label=$obj->FileName;
		$lable=htmlspecialchars($label);
		$label=str_replace(' ','',$label);
		$hash=$obj->Hash;
		$list.="<m  src=\"$thisurl?hash=$hash\"  label=\"$label\" />\n";
	}
	echo  $list."</list>";
	
}
function kugou($str) {
	if(preg_match('/^(\w+)$/',$str)&&strlen($str)==32){
		hashcode($str);
	}
	$str = urlencode($str);
	$url = "http://www1.kugou.com/ting/Search.aspx?keywords=$str";
	$kustr = file_get_contents($url);
	preg_match('/SongManager\.Listen4\(\'(.*)\'\)/imsU', $kustr, $hash);
	$hash = explode('|', $hash[1]);
	hashcode($hash[2]);
}
function  hashcode($hash){
	$key = kgkey($hash);
	$trace = "http://tracker1.kugou.com/i/?hash=$hash&cmd=3&key=$key";
	$tracestr = iconv('gbk', 'utf-8', file_get_contents($trace));
	$tobj = json_decode($tracestr);
	$m4a = $tobj->url;
	header("location:$m4a");
}
function kgkey($hash) {
	$hash .= "kgcloud";
	return md5($hash);
}
function kuwo($name) {
	$name = urlencode($name);
	$url = "http://sou.kuwo.cn/ws/NSearch?key=$name&type=all";
	preg_match('/<li class="songName"><a href="http:\/\/www\.kuwo\.cn\/yinyue\/([0-9]+)\/"/imsU', file_get_contents($url), $sid);
	$surl = "http://player.kuwo.cn/webmusic/st/getMuiseByRid?rid=MUSIC_$sid[1]";
	$strl = file_get_contents($surl);
	preg_match('/<mp3path>([^<]+)<\/mp3path>/imsU', file_get_contents($surl), $mp3);
	$mp3url = "http://mp3dl.cdn.kuwo.cn/$mp3[1]";
	//echo $mp3url;
	header("location:$mp3url");
}

function qq($name) {
	$name = urldecode($name);
	$url = "http://portalcgi.music.qq.com/fcgi-bin/music_mini_portal/cgi_mini_portal_search_json.fcg?search_input=$name&start=1&return_num=20&utf8=0&outputtype=1";
	$str = curl($url);
	$str = iconv('gbk', 'utf-8', $str);
	preg_match('/"songID":([0-9]+),"auditionID/imsU', $str, $id);
	//echo $id[1];
	if ($id[1]) {
		qqid($id[1]);
	} else {
		return;
	}
}
function qqid($id) {
	$sid = $id % 100;
	$url = "http://music.qq.com/portal_v2/static/spread/$sid/spread_$id.html";
	preg_match('/mstream=([0-9]+)/ims', file_get_contents($url), $fp);
	$mp3 = "http://stream1$fp[1].qqmusic.qq.com/30$id.mp3";
	header("location:$mp3");
}
function sina($name) {
	$url = "http://music.sina.com.cn/yueku/search/s.php?e=utf-8&t=all&k=$name";
	preg_match('/mpwPlay\(([0-9]+)\)/imsU', curl($url), $id);
	sid($id[1]);
	exit;
}
function sid($id) {
	$url = "http://m.v.iask.com/ask_n.php?oid=$id&pid=707";
	preg_match('|"([^"]+)"|U', curl($url), $mp3);
	if ($mp3[1]) {
		header("location:$mp3[1]");
	} else {
		$urls = "http://music.sina.com.cn/yueku/m.php?id=$id&FLAG_ADDLIST=0&coFlag=100013";
		preg_match('/"url":"([^"]+)","/imsU', curl($urls), $murl);
		$mp3url = urldecode(base64_decode($murl[1]));
		preg_match('|iask_music_song_url="([^"]+)";|', curl($mp3url), $mp3);
		header("location:$mp3[1]");
	}

}
function sougou($name) {
	$url = "http://mp3.sogou.com/api/links2?query=$name&id=" . rand();
	$str = iconv('GBK', 'UTF-8', curl($url));
	$obj = json_decode($str);
	$list = $obj->list;
	foreach ($list as $value) {
		$mp3 = $value->postfix;
		$mp3url = $value->url;
		if ($mp3 == 'mp3') {
			header("location:$mp3url");
		}
		exit;
	}

}
function yahoo_gl($url) {
	$domain = parse_url($url, PHP_URL_HOST);
	$domainend = end(explode('.', $domain));
	$domainarr = array (
		'biz',
		'info'
	);
	if (in_array($domainend, $domainarr)) {
		return false;
	} else {
		return true;
	}
}
function yahoo($str) {
	$url = "http://music.yahoo.cn/s?q=$str&m=5";
	preg_match_all('/&url=([^"]+)"/imsU', curl($url), $mp3);
	array_unique($mp3[1]);
	foreach ($mp3[1] as $value) {
		$mp3 = urldecode($value);
		if (yahoo_gl($mp3) == true) {
			header("location:$mp3");
			exit;
		}
	}

}

function baidu($str) {
	$url = 'http://box.zhangmen.baidu.com/x?op=12&count=1&mtype=2&title=' . $str . '$$';
	$xml = simplexml_load_string(curl($url));
	$arr = $xml->url;
	foreach ($arr as $value) {
		$encode = $value->encode;
		$decode = $value->decode;
		$encode = str_replace(end(explode('/', $encode)), '', $encode);
		$domain = parse_url($encode, PHP_URL_HOST);
		$type = explode('?', $decode);
		$type = end(explode('.', $type[0]));
		if ($domain != "zhangmenshiting.baidu.com" && $type == "mp3") {
			$src = $encode . $decode;
			if ($src) {
				header("location:$src");
			} else {
				return;
			}
			//echo $src;
			break;
		}
	}
}

function curl($url) {
	if (function_exists('file_get_contents')) {
		$data = file_get_contents($url);
	} else {
		$user_agent = $_SERVER['HTTP_USER_AGENT'];
		$ch = curl_init();
		$timeout = 30;
		curl_setopt($ch, CURLOPT_URL, $url);
		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;
}
?>