<?php
error_reporting(0);
//header("Content-type: text/xml; charset=utf-8");
$f = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'];
$query = $_SERVER[QUERY_STRING];
if (!empty ($query)) {
	$get = explode('=', $query);
	switch ($get[0]) {
		case "list" :
			get_list($get[1], true);
			break;
		case "id" :
			ids($get[1]);
			break;
		case "ablum" :
			ablum($get[1]);
			break;
		case "zj" :
			get_zj($get[1]);
			break;
		case "film" :
			defaultList("film");
			break;
		case "tvlist" :
			get_list($get[1], false);
			break;
		case "tv_ablum" :
			tv_ablum($get[1]);
			break;
		case "tv" :
			defaultList("tv");
			break;
		default :
			echo "参数有误";
			break;
	}
	exit;
} else {
	$xml = "<list>\n<m list_src=\"$f?film\" label=\"电影\"/>\n";
	$xml .= "<m list_src=\"$f?tv\" label=\"电视\"/>\n</list>";
	echo iconv('utf-8', 'gbk', $xml);
}

function ablum($url) {
	$ablum = preg($url, '|<div class="h"><h2><a href="([^"]+)"|ms', false);
	ids($ablum);
}
function tv_ablum($url) {
	$zj = preg($url, '|<div class="h"><h2><a href="([^"]+)" class="album-title">|ims', false);
	get_zj($zj);

}

function get_zj($url) {
	$str = preg($url, '|listData = \[([^\]]+)\]|imsU', false);
	$str = preg_replace("/\n/", "", $str);
	$str = str_replace(",cartoonType:0", "", $str);
	$str = str_replace(",shortDesc:\"\"", "", $str);
	$arr = preg($str, '|\{iid:([^\{]+)",icode:"|ms', true);
	echo makexml($arr, ',title:"', 'id', false);
}

function get_list($url, $bool) {
	$n = get_list_num($url);
	$str = '';
	if (empty ($n)) {
		$str = file_get_contents($url);
	} else {
		$hurl = str_replace('1.html', '', $url);
		for ($i = 1; $i <= $n; $i++) {
			$eurl = "$hurl$i.html";
			$str .= file_get_contents($eurl);
		}
	}

	$arr = preg($str, '|<div class="txt"><h6 class="caption"><a href="([^<]+)" target="_blank">|ims', true);
	if ($bool) {
		echo makexml($arr, '" title="', 'ablum', false);
	} else {
		echo makexml($arr, '" title="', 'tv_ablum', true);
	}

}

function get_list_num($url) {
	$num = preg($url, '|">([0-9]+)<\/a><\/li><\/ul><\/div><\/div><!--|', false);
	return $num;
}

function defaultList($change) {
	if ($change == "film") {
		$url = "http://movie.tudou.com/albumtop/c22t-1a-1y-1h-1s1p1.html";
		$filmstr = preg($url, '|<div class="sort">(.*)</li></ul><h3>|imsU', false);
		$filmlist = preg($filmstr, '|<li><a href="([^<]+)<|', true);
		return makexml($filmlist, '">', 'list', true);
	} else {
		$url = "http://tv.tudou.com/albumtop/c30t-1a-1y-1h-1s1p1.html";
		$filmstr = preg($url, '|<div class="sort">(.*)</li></ul><h3>|imsU', false);
		$filmlist = preg($filmstr, '|<li><a href="([^<]+)<|', true);
		return makexml($filmlist, '">', 'tvlist', true);
	}

}

function makexml($arr, $cut, $get, $bool) {
	global $f;
	$list = "<list>\n";
	foreach ($arr as $value) {
		if ($bool) {
			$temp = explode("$cut", $value);
			$name = htmlspecialChars($temp[1]);
			$list .= "<m list_src=\"$f?$get=$temp[0]\" label=\"$name\" />\n";
		} else {
			$temp = explode("$cut", $value);
			$name = htmlspecialChars($temp[1]);
			$list .= "<m  type=\"2\" src=\"$f?$get=$temp[0]\" label=\"$name\" />\n";
		}
	}
	$list .= "</list>";
	//$list=iconv('gbk','utf-8',$list);
	echo $list;
}

function ids($id) {
	if (preg_match('|^\d+$|', $id)) {
		$iid = $id;
	}
	elseif (preg_match('|^([0-9a-zA-Z_-]+)$|', $id)) {
		$iid = longurl($id, false);
	} else {
		$iid = longurl($id, true);
	}
	nums($iid);
	exit;
}

function nums($iid) {
	$url = "http://v2.tudou.com/v2/cdn?id=$iid";
	$src = preg($url, '|">([^<]+)<|', false);
	$src = explode('?', $src);
	$src = "$src[0]?92001";
	header("location:$src");
}
function longurl($id, $bool) {
	if ($bool) {
		$url = $id;
		;
	} else {
		$url = "http://www.tudou.com/programs/view/$id/";
	}
	$iid = preg($url, '|iid\s?:?=?\s?([0-9]+)$|imsU', false);
	return $iid;
}

function preg($url, $preg, $bool) {
	if ($bool) {
		preg_match_all($preg, $url, $ar);
	} else {
		preg_match($preg, file_get_contents($url), $ar);
	}
	return $ar[1];
}
?>