<?php
require_once(dirname(__FILE__)."/admin_inc.php");
/*******************************************************************************************
' Software name: DJCMS Content Management System
' Version:3.0
' Web: http://www.djcms.net
' Author: 心情(qq:1130731)
' Copyright (C) 2009-2010 DJCMS官方 版权所有
' 法律申明：DJCMS程序所有代码100%原创、未引入任何网上代码,对一切抄袭行为、坚决严肃追究法律责任
'*******************************************************************************************/
AjaxHead();
$action = getForm("action", "get");$url=getForm("url","get");
switch ($action) {
	case "gather":
		gather();break;
	default : echo "err";break;
} 

function gather() {
	global $url;
	if (empty($url)) echo '';
	if (strpos($url,"youku.com")>0) gatherYouku($url);
	if (strpos($url,"www.tudou.com")>0) gatherTudou($url);
	if (strpos($url,"hd.tudou.com")>0) gatherTudouHd($url);
	if (strpos($url,"you.video.sina.com.cn")>0) gatherSina($url);
	if (strpos($url,"movie.video.sina.com.cn")>0) gatherSinaHd($url);
	if (strpos($url,"hd.openv.com")>0) gatherOpenvHd($url);
	if (strpos($url,"v.blog.sohu.com")>0) gatherSohu($url);
	if (strpos($url,"tv.sohu.com")>0) gatherSohuHd($url);
	if (strpos($url,"www.56.com")>0) gather56($url);
	if (strpos($url,"tv.56.com")>0) gather56Hd($url);
	if (strpos($url,"video.qq.com")>0) gatherQQ($url);
	if (strpos($url,"6.cn")>0) gather6Room($url);
	if (strpos($url,"youtube.com")>0) gatherYouTuBe($url);
	if (strpos($url,"hd.ku6.com")>0) gatherKu6Hd($url);
	if (strpos($url,"v.ku6.com")>0) gatherKu6($url);
}

function gatherYouTuBe($url) {
    $id=gsubStr($url."&","v=","&");
	$pageStr = get($url);
	preg_match_all("/<title>YouTube - (.+?)<\/title>/",$pageStr,$title);
	$result = $title[1][0].'$'.$id."$youtube";
	echo $result;
}

function gatherQQ($url) {
    if (strpos($url,"group?g") > 0) {
		$vid=gsubStr($url."#","g=","#");
		$pageStr = utf82gb(get($url));
		preg_match_all("/专辑所有视频<font>\((\d+)\)<\/font>/",$pageStr,$itemCountarr);
		$itemCount = $itemCountarr[1][0];
		if ($itemCount <= 10) {
			$pcount = 1;
		} else {
			if ($itemCount % 10 == 0) {$pcount = ceil($itemCount / 10);} else {$pcount = ceil($itemCount / 10)+1;}
		}
		for ($i = 1;$i <= $pcount;$i++) {
			$purl = "http://video.qq.com/v1/group/video?p=".$i."&g=".$vid;
			preg_match_all("/<a onClick=\"playVideo\('(.+?)'\)\">(.+?)<\/a>/",$pageStr,$matches);
			$matcheslen=count($matches[1]);
			for ($j = 0;$j < $matcheslen;$j++) {
				$result = $result.$matches[2][$j].'$'.$matches[1][$j].'$qq'."\r\n";
			}
		}
		$result = rtrim($result,"\r\n");
		echo $result;
	} else {
		$pageStr = utf82gb(get($url));
		preg_match_all("/vp_title=\"视频：(.+?)\";/",$pageStr,$title);
		preg_match_all("/vp_vid=\"(.+?)\";/",$pageStr,$guid);
		$result = $title[1][0].'$'.$guid[1][0].'$qq';
		echo $result;
	}
}

function gather56($url) {
	$result='';
	if (strpos($url,"/album") > 0) {
		$substring1 = gsubStr($url,"http://","album");
		$vid=gsubStr($url,"-aid-",".");
		$playurl = "http://".$substring1."album_v2/album_videolist.phtml?callback=albumV2.callback&aid=".$vid."&o=0";
		$pageStr = get($playurl);
		preg_match_all("/\"video_id\":\"(.+?)\"/",$pageStr,$matches);
		$matcheslen=count($matches[1]);
		for ($j = 0;$j < $matcheslen;$j++) {
			$pageurl = "http://".$substring1."play_album-aid-".$vid."_vid-".$matches[1][$j].".html";
			$pageStr = get($pageurl);
			$paras = gsubStr($pageStr,"img_host=","'");
			$id = gsubStr($paras,"host=",".")."_/".gsubStr($paras,"pURL=","&")."_/".gsubStr($paras,"sURL=","&")."_/".gsubStr($paras,"user=","&")."_/".gsubStr($paras,"URLid=","&");
			$title = decodeSohu(gsubStr($pageStr,"Subject".chr(34).":".chr(34)."", ","));
			//$title = gsubStr($pageStr,"Subject' : '","',");
			$result = $result.$title.'$'.$id.'$hd_56'."\r\n";
		}
		$result=rtrim($result,"\r\n");
		echo $result;
	}elseif (strpos($url,"/play_album-")>0){
		$allstr = get($url);
		$paras =  gsubStr($allstr,"img_host=","'");
		$title = decodeSohu(gsubStr($allstr,"Subject".chr(34).":".chr(34)."",","));
		$result = $result.$title."$".gsubStr($allstr,"&vid=","&").""."$"."hd_56\n";
		echo $result;
	}else{
		$allstr = get($url);
		$title = decodeSohu(gsubStr($allstr,"Subject".chr(34).":".chr(34)."", ","));
		$id=gsubStr($url,"/v_",".html");
		if ($id<>""){
			$result = $title."$".$id.""."$"."hd_56";
	    }else{
			$id = gsubStr($allstr,"var _oFlv_o = '","'");
			$result = $title."$".de56($id).""."$"."hd_56";
	    }
		echo $result;
    }
}

function gather56Hd($url) {
	$pageStr = get($url);
	preg_match_all("/<li><a href=\"(.+?)\">(.+?)<\/a><\/li>/",$pageStr,$matches);
	$matcheslen=count($matches[1]);
	for ($j = 0;$j < $matcheslen;$j++) {
		$title=$matches[2][$j];
		$pageurl = $matches[1][$j];
		$pageStr = get($pageurl);
		preg_match_all("/<title>(.+?)-/",$pageStr,$title2);
		$title=str_replace("【播放】",trim($title2[1][0]),$title);
		$paras = gsubStr($pageStr,"img_host=","'");
		$id = gsubStr($paras,"host=",".")."_/".gsubStr($paras,"pURL=","&")."_/".gsubStr($paras,"sURL=","&")."_/".gsubStr($paras,"user=","&")."_/".gsubStr($paras,"URLid=","&");
		$result = $result.$title.'$'.$id.'$hd_56'."\r\n";
	}
	$result=rtrim($result,"\r\n");
	echo $result;
}

function getTudouHdPage($vid) {
	$pageurl = "http://hd.tudou.com/ajax/albumVideos.html?videoId=".$vid."&pageNumer=";
	for ($i = 1;$i <= 20;$i++) {
		$pagestr = subStr(get($pageurl.$i),0,100);
		$pagestr2 = subStr(get($pageurl.($i+1)),0,100);
		if($pagestr==$pagestr2) return $i;
	}
}

function gatherTudouHd($url) {
	$playlistid = gsubStr($url."/","program/","/");
	$pagenum = getTudouHdPage($playlistid);
	for ($i = 1;$i <= $pagenum;$i++) {
		$pageStr = get("http://hd.tudou.com/ajax/albumVideos.html?videoId=".$playlistid."&pageNumer=".$i);
		preg_match_all("/<a href=\"(.+?)\"/",$pageStr,$matches);
		$matcheslen=count($matches[1]);
		for ($j = 0;$j < $matcheslen;$j++) {
			$pageurl = "http://hd.tudou.com".$matches[1][$j];
			$pageStr = utf82gb(get($pageurl));
			preg_match_all("/iid: \"(\d+)\",/",$pageStr,$guid);
			preg_match_all("/title: \"(.+?)\",/",$pageStr,$title);
			$result = $result.$title[1][0].'$'.$guid[1][0].'$hd_tudou'."\r\n";
		}
	}
	$result = rtrim($result,"\r\n");
	echo $result;	
}

function gatherSohuHd($url) {
	$pageStr = get($url);
	$result = gatherSohuHdPlay($pageStr);
	if (strpos($pageStr,"<div id=\"roll\" class=\"area\">")>0){
		preg_match_all("/var pid =\"(\d+)\"/",$pageStr,$pid);
		$playlistidarr = explode("$",$result);
		$playlistid = $playlistidarr[1];
		$result = "";
		$pageStr = get("http://hot.vrs.sohu.com/vrs_videolist.action?vid=".$playlistid."&pid=".$pid[1][0]);
		preg_match_all("/\"videoName\":\"(.+?)\".+?\"videoId\":(\d+),\"/",$pageStr,$matches);
		$matcheslen=count($matches[1]);
		for ($j = 0;$j < $matcheslen;$j++) {
			$result = $result.$matches[1][$j].'$'.$matches[2][$j].'$hd_sohu'."\r\n";
		}
		$result = rtrim($result,"\r\n");
		echo $result;
	} else {
		echo $result;
	}
}

function gatherSohuHdPlay($pageStr) {
	preg_match_all("/var vid=\"(\d+)\"/",$pageStr,$id);
	preg_match_all("/<title>(.+?)<\/title>/",$pageStr,$title);
	return str_replace("》","",str_replace("《","",str_replace("-搜狐视频","",$title[1][0]))).'$'.$id[1][0].'$hd_sohu';
}

function gatherOpenvHd($url) {
	if (strpos($url,"/tv_show") > 0) {
		$pageStr = utf82gb(get($url));
		if (strpos($pageStr,"<li><a href=") == 0) {
			preg_match_all("/<a class=\"alink\"[\s]href=\"tv_play-(\S+?)html\">(.+?)<\/a>/",$pageStr,$matches);
			$matcheslen=count($matches[1]);
			for ($j = 0;$j < $matcheslen;$j++) {
				if (strpos($matches[1][$j],"cartoon")>0) {
					$id = "hdcartoon_".gsubStr($matches[1][$j],"n_",".");
				} else if (strpos($matches[1][$j],"hddoc_")>0) {
					$id = "hddoc_".gsubStr($matches[1][$j],"c_",".");
				} else {
				    $id = "hdteleplay_".gsubStr($matches[1][$j],"y_",".");
				}
				$result = $result.$matches[2][$j].'$'.$id.'$hd_openv'."\r\n";
			}
		}
		$result = rtrim($result,"\r\n");
		echo $result;
	}else{
		echo gatherOpenvHdPlay($url);
	}
}

function gatherOpenvHdPlay($url) {
	$pageStr = utf82gb(get($url));
	if (strpos($url,"clips") > 0){
		$id = "hdmovieclips_".gsubStr($url,"s_",".");
		preg_match_all("/<title>(.+?)<\/title>/",$pageStr,$titlearr);
		$title = str_replace("-高清电影","",$titlearr[1][0]);
	} else if (strpos($url,"prog_") > 0){
		$id = "CTIYuLeprog_".gsubStr($url,"g_",".");
		preg_match_all("/<title>(.+?)<\/title>/",$pageStr,$titlearr);
		$title = str_replace("-高清电视节目","",$titlearr[1][0]);
	} else {
		$id = "hdmovie_".gsubStr($url,"e_",".");
		preg_match_all("/<title>(.+?)<\/title>/",$pageStr,$titlearr);
		$title = str_replace("-高清电影","",$titlearr[1][0]);
	}
	return $title.'$'.$id.'$hd_openv';
}

function gatherSinaHdPlay($url){
	$pageStr = get($url);
	preg_match_all("/var vid='(.+?)'/",$pageStr,$id);
	preg_match_all("/<title>(.+?)- 新浪/",$pageStr,$title);
	return trim($title[1][0]).'$'.$id[1][0].'$hd_iask';
}

function gatherSinaHd($url) {
	if (strpos($url,"teleplay") > 0) {
		$pageStr = get($url);
		preg_match_all("/<div class=\"btn_special_100\"><a href=\"([\s\S]+?)\"/",$pageStr,$matches);
		$matcheslen = count($matches[1]);
		for ($j = 0;$j < $matcheslen;$j++) {
			$playurl = "http://movie.video.sina.com.cn".$matches[1][$j];
			$result = $result.gatherSinaHdPlay($playurl)."\r\n";
		}
		$result = rtrim($result,"\r\n");
		echo $result;
	} else {
		echo gatherSinaHdPlay($url);
	}
}

function gatherYouku($url){
	$result='';
	if (strpos($url,"playlist_show") > 0) {
		$youkuRss = "http://www.youku.com/playlist/rss/id/".gsubStr($url,"id_",".html");
		$pageStr = utf82gb(get($youkuRss));
		preg_match_all("/\<item\>(.*?)\<\/item>/s",$pageStr,$itemblocks);
			foreach ($itemblocks[1] as $block) {
				preg_match_all( "/\<title\>(.*?)\<\/title\>/",$block,$title);
				preg_match_all( "/\<guid\>(\d{3,}?)\<\/guid\>/",$block,$guid);
				$result = $result.$title[1][0].'$'.$guid[1][0].'$youku'."\r\n";
			}
		$result = rtrim($result,"\r\n");
		echo $result;
	} else if(strpos($url,"show_page")>0){
		for ($i = 1;$i <= 50;$i++) {
			$youkuurl = "http://www.youku.com/show_eplist/showid_".gsubStr($url,"id_",".html")."_page_".$i.".html";
			$pageStr = utf82gb(get($youkuurl));
			if (empty($pageStr) || strpos($pageStr,"暂无剧集")) {
				echo $result;
				exit;
			}
			preg_match_all("/href=\"http:\/\/v.youku.com\/v_show\/id_(.*?).html\" target=\"video\"\>(.*?)\<\/a\>/",$pageStr,$temparr);
			$j = count($temparr[1]);
			for ($k = 0;$k < $j;$k++) {
				$result .= $temparr[2][$k].'$'.$temparr[1][$k].'$youku'."\r\n";
			}
		}
		$result=rtrim($result,"\r\n");
		echo $result;
	} else if (strpos($url,"v_show") > 0) {
		$temparr = gsubStr($url,"id_",".html");
		$pageStr = utf82gb(get($url));
		$title = gsubStr($pageStr,"<title>"," - ");
		$result = $title.'$'.$temparr.'$youku'."\r\n";
		$result = rtrim($result,"\r\n");
		echo $result;
	}
}

function gatherTudou($url) {
	$result = '';
	if (strpos($url,"playlist") > 0) {
		if (strpos($url,"playindex.do?lid=") > 0) $url = replaceStr($url,"playindex.do?lid=","id/");
		$tudouRss = "http://www.tudou.com/playlist/rss.do?lid=".subStr($url,"/id/","/");
		$pageStr = utf82gb(get($url));
		preg_match_all("/\<item\>(.*?)\<\/item>/s",$pageStr,$itemblocks);
			foreach ($itemblocks[1] as $block) {
				preg_match_all( "/\<title\>(.*?)\<\/title\>/",$block,$title);
				preg_match_all( "/\<tudou:info id=\"(\d{3,}?)\"/",$block,$guid);
				$result = $result.$title[1][0].'$'.$guid[1][0].'$tudou'."\r\n";
			}
		$result = rtrim($result,"\r\n");
		echo $result;
	} else {
		$pageStr = get($url);
		preg_match_all("/,kw = \"(.*?)\"/",$pageStr,$title);
		preg_match_all("/var iid = (\d+)/",$pageStr,$guid);
		$result = $result.$title[1][0].'$'.$guid[1][0].'$tudou';
		echo $result;
	}
}

function gatherSina($url) {
	if (strpos($url,"/a/") > 0) {
		$paraArray = explode("-",gsubStr($url,"/a/",".html"));
		$url1 = "http://you.video.sina.com.cn/api/catevideoList.php?uid=".$paraArray[1]."&tid=".$paraArray[0];
		$pageStr = substr(get($url1),0,50);
		preg_match_all("/count\":\"(\d+)\"/",$pageStr,$allVideos);
		$url2 = $url1."&pagesize=".$allVideos[1][0];
		$pageStr = get($url2);
		preg_match_all("/\"vid\":\"(\d+)\",\"uid\":\"(.*?)\",\"nick\":\"(.*?)\",\"name\":\"(.*?)\"/",$pageStr,$matches);
		$matcheslen = count($matches[1]);
		for ($j = 0;$j < $matcheslen;$j++) {
			$result = $result.decodeSohu($matches[4][$j]).'$'.$matches[2][$j].'&uid='.$matches[1][$j].'$iask'."\r\n";
		}
		$result = rtrim($result,"\r\n");
		echo $result;
	} else {
		$pageStr = utf82gb(get($url));
		preg_match_all("/<title>(.*?)_/",$pageStr,$title);
		$guid = gsubStr($url,"/b/","-").gsubStr($url,"-",".html");
		$result = $result.$title[1][0].'$'.$guid.'$iask';
		echo $result;
	}
}

function gatherSohu($url) {
	if (strpos($url,"/pw/") > 0) {
		$id = gsubStr($url."/","/pw/","/");
		$sohuDataUrl = "http://v.blog.sohu.com/playlistVideo.jhtml?outType=3&from=2&m=viewlist&playlistId=".$id."&size=300";
		$pageStr = get($sohuDataUrl);
		preg_match_all("/\"id\":(\d+),\"title\":\"(.*?)\"/",$pageStr,$matches);
		$matcheslen = count($matches[1]);
		for ($j = 0;$j < $matcheslen;$j++) {
			$result = $result.decodeSohu($matches[2][$j]).'$'.$matches[1][$j].'$sohu'."\r\n";
		}
		$result = rtrim($result,"\r\n");
		echo $result;
	} else {
		$pageStr = get($url);
		preg_match_all("/<title>(.*?) -/",$pageStr,$title);
		$guid = gsubStr($url."/","/vw/","/");
		$result = $result.$title[1][0].'$'.$guid.'$sohu';
		echo $result;
	}
}

function gather6Room($url) {
	$pageStr = get($url);
	if (strpos($url,"playlist/") > 0) {
		$topicId = gsubStr($url."/","playlist/","/");
		preg_match_all("/<em class=\"hit\">(\d+)<\/em>/",$pageStr,$itemCountarr);
		$itemCount  =$itemCountarr[1][0];
		for ($j = 0;$j < $itemCount;$j++) {
			$vPageStr = utf82gb(get("http://6.cn/plist/".$topicId."/".$j.".html"));
			preg_match_all( "/<h2 class=\"pvt\">(.*?)<\/h2>/",$vPageStr,$title);
			preg_match_all( "/pageMessage.evid = '(.*?)'/",$vPageStr,$guid);
			$result = $result.$title[1][0].'$'.$guid[1][0].'$6rooms'."\r\n";
		}
		$result = rtrim($result,"\r\n");
		echo $result;
	} else {
		$pageStr = utf82gb($pageStr);
		preg_match_all( "/<title>(.*?) -/",$pageStr,$title);
		preg_match_all( "/pageMessage.evid = '(.*?)'/",$pageStr,$guid);
		$result = $title[1][0].'$'.$guid[1][0].'$6rooms';
		echo $result;
	}
}

function gsubStr($str,$startStr,$endStr){
    $strtemp1 = explode($startStr, $str);
    $strtemp2 = explode($endStr, $strtemp1[1]);
    return $strtemp2[0];
}

function decodeSohu($str){
    $str = str_replace('\u','%u',$str);
    $str = UnicodeUrl2Gbk($str);
    return $str;
}
?>
