<?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%原创、未引入任何网上代码,对一切抄袭行为、坚决严肃追究法律责任
'*******************************************************************************************/
checkPower();
$action = getForm("action", "get"); $url=getForm("url");
switch ($action) {
	case "gather":
		gather();
	break;
}

function gather(){
	global $url;
	if (empty($url)) die("");
	if (isExistStr($url,"youku.com")) gatherYouku($url);
	if (isExistStr($url,"www.tudou.com")) gatherTudou($url);
	if (isExistStr($url,"you.video.sina.com.cn") || isExistStr($url,"video.sina.com.cn")) gatherSina($url);
	if (isExistStr($url,"movie.video.sina.com.cn")) gatherSinaHd($url);
	if (isExistStr($url,"hd.openv.com")) gatherOpenvHd($url);
	if (isExistStr($url,"tv.sohu.com")) gatherSohuHd($url);
	if (isExistStr($url,"qq.com")) gatherQQ($url);
	if (isExistStr($url,"v.ku6.com")) gatherKu6($url);
	if (isExistStr($url,"joy.cn")) gatherJoy($url);
	if (isExistStr($url,"qiyi.com")) gatherQiyi($url);
}

function gatherQQ($url){
	if (isExistStr($url,"detail/z")){
		$url = utf82gb(getRemoteContent($url));
		$ayl = StrSlice($url,'<div class="tab_cont" id="mod_videolist">','</div>');
		$ay = StrSplits($ayl,'id="','"');$ayr = StrSplits($ayl,'">','</a>');
		$arlen = count($ay);
		for ($i = 0;$i < $arlen;$i++) {
			$result .= $ayr[$i]."$".$ay[$i]."$"."qq\r\n";
		}
		$result=rtrim($result,"\r\n");
		echo $result;
    }else{
		$pageStr = utf82gb(getRemoteContent($url));
		$title=StrSlice($pageStr,'coverTitle:"','"');
		$id=StrSlice($pageStr,'vp_vid="','"');
		$result = $title."$".$id."$"."qq";
		echo $result;
    }
}

function gatherYoukuTV($url){
	$id=StrSlice($url,"id_",".html");
	if ($id<>""){
		$allstr = utf82gb(getRemoteContent($url));
		if (strpos($allstr,"剧集列表")<1){
		///show_page/id_
			$id=StrSlice(StrSlice($allstr,'<span class="action">',"</a>"),"/v_show/id_",".html");
			$result=StrSlice($allstr,'<span class="name">',"</span>")."$".$id."$"."youku";
		}else{
			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;
	}
}

function gatherYouku($url){
	if (isExistStr($url,"playlist_show")){
		$youkuRss = "http://www.youku.com/playlist/rss/id/".StrSlice($url,"id_",".html");
		$xmlstr = utf82gb(getRemoteContent($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;
	}elseif (isExistStr($url,"show_page")){
		gatherYoukuTV($url);
	}elseif (isExistStr($url,"v_show") || isExistStr($url,"v_playlist")){
		$allstr = utf82gb(getRemoteContent($url));
		preg_match_all("/\<meta name=\"title\" content=\"(.*?)\"\>/",$allstr,$title);
		preg_match_all("/var videoId = '(\d{3,}?)'/",$allstr,$guid);
		$result = $title[1][0]."$".$guid[1][0]."$"."youku";
		echo $result;
	}else{
		//alertMsg "不支持你输入的优酷网址，请看清楚本工具下面列出来的范例",""
	}
}

function gatherTudou($url){
	if (isExistStr($url,"/playlist/album/")){
		$allstr=getRemoteContent($url);$getser=StrSlice($allstr,"<div class=\"pack pack_video_card\">","</div><div class=\"page_nav\"");
		$r=StrSplits($getser,'<a class="vinf" target="new" href="','"');
		$p=StrSplits($getser,'class="inner" target="new" title="','"');
		if (isExistStr($r[0],"/playlist/p/")){
			gatherTudou($r[0]);
		}else{
			$arlen = count($r);
		    for ($m = 0;$m < $arlen;$m++) {
				$r[$i]=$p[$i]."$".StrSliceB($r[$i],"?iid=","")."$"."tudou";
		    }
		}
	}elseif (isExistStr($url,"playlist/playindex") || isExistStr($url,"playlist/id/")){
		if (isExistStr($url,"playindex.do?lid=")) $url=replaceStr($url,"playindex.do?lid=","id/");
		$url=replaceStr(replaceStr($url,"/playlist/id","/playlist/id/"),".html","");
		$tudouRss = "http://www.tudou.com/playlist/rss.do?lid=".StrSlice($url,"/id/","/");
		$pageStr = getRemoteContent($tudouRss);
		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");
	}elseif (isExistStr($url,"playlist/p/")){
		$allstr = getRemoteContent($url);
		$getser = StrSlice($allstr,"listData = [{","}]");
		$title = StrSlice($getser,',title:"','"');
		$r=StrSplits($getser,"iid:",",");
		$p=StrSplits($getser,',title:"','"');
		$arlen = count($r);
		for ($m = 0;$m < $arlen;$m++) {
			$kk.=$title.$p[$m]."$".$r[$m]."$"."tudou\n";
		}
		$result=rtrim($result,"\n");
		$result=$kk;
	}else{
		$allstr = getRemoteContent($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 gatherSohuHd($url){
	$pageStr = file_get_contents($url);
	$result=gatherSohuHdPlay($pageStr);
	if (isExistStr($url,"<div id=\"roll\" class=\"area\">")){
		$playlist = split("$",$result);
		$playlistid = $playlist[1];
		$result = "";
		$pageStr = getRemoteContent("http://hot.vrs.sohu.com/vrs_videolist.action?vid=".$playlistid);
		$labelRule = 'videoName":"(.*?)","videoId":"(.*?)",';
		preg_match_all(buildregx($labelRule,"is"), $pageStr, $ar);
		$arlen = count($ar[1]);
		for ($m = 0;$m < $arlen;$m++) {
			$result = $result.decodeSohu($ar[1])."$".$ar[0]."$"."sohu\n";
	    }
		$result=rtrim($result,"\n");
    }else{
		$result=$result;
    }
	echo $result;
}

function gatherSina($url){
	$url=replaceStr($url,"/playlist/","/a/");
	if (isExistStr($url,"/movie/detail/")){
		gatherSinaHd($url);
	}elseif (isExistStr($url,"/m/")){
		$result=gatherSinaHdPlay($url);
	}elseif (isExistStr($url,"/a/")){
		$paraArray = split("-",StrSlice($url,"/a/",".html"));
		$url1="http://you.video.sina.com.cn/api/catevideoList.php?uid=".$paraArray[1]."&tid=".$paraArray[0];
		$allstr = utf82gb(getRemoteContent($url));
		$allVideos = StrSlice($url,'count":"','"');
		$url2=$url1."&pagesize=".$allVideos;
		$allstr = utf82gb(getRemoteContent($url));
		$labelRule = 'vid":"(.*?)","uid":"(.*?)","nick":"(.*?)","name":"(.*?)",';
		preg_match_all(buildregx($labelRule,"is"), $allstr, $ar);
		$arlen = count($ar[1]);
		for ($m = 0;$m < $arlen;$m++) {
			$result = $result.decodeSohu($ar[2])."$".$ar[0]."&uid=".$ar[1]."$"."iask\n";
		}
	}elseif (isExistStr($url,"/v/b/")){
		$allstr = StrSlice(utf82gb(getRemoteContent($url)),"<div id=\"Interfix\">","</ul>");
		$matches = StrSplits($allstr,'<a class="plicon" href="/v/b/','.html"');
		$match = StrSplits($allstr,' alt="','"');
		$arlen = count($matches);
		for ($i = 0;$i < $arlen;$i++) {
			$result = $result.$match[$i]."$".replaceStr($matches[$i],"-","&uid=")."$"."iask\n";
		}
	}elseif (isExistStr($url,"/pg/")){
		$allstr = getRemoteContent($url);
		$pages = StrSlice($allstr,"var scope","}");
		$pages = StrSlice($pages,'"$tid":"','"')."&uid=".StrSlice($pages,'"$uid":"',"'");
		if ($pages<>"&uid="){
			$url="http://you.video.sina.com.cn/api/catevideoList.php?tid=".$pages."&page=1&pagesize=1000";
			$allstr=utf82gb(getRemoteContent($url));
			$labelRule = 'vid":"(.*?)","uid":"(.*?)","nick":"(.*?)","name":"(.*?)",';
		    preg_match_all(buildregx($labelRule,"is"), $allstr, $ar);
		    $arlen = count($ar[1]);
		    for ($m = 0;$m < $arlen;$m++) {
			    $result = $result.decodeSohu($ar[2])."$".$ar[0]."&uid=".$ar[1]."$"."iask\n";
		    }
		}
    }else{
		$allstr = utf82gb(getRemoteContent($url));
		$result = StrSlice($allstr,"<title>","</title>")."$".StrSlice($url,"/b/","-")."&uid=".StrSlice($url,"-",".html")."$"."iask";
	}
	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 (isExistStr($url,"/tv_show")){
		$pageStr = utf82gb(getRemoteContent($url));
		if(strpos($pageStr,"<li><a href=")==0){
		    $labelRule='<a class="alink"[\s]href="tv_play-(.*?)html">(.+?)</a>';
		    preg_match_all(buildregx($labelRule,"is"), $pageStr, $ar);
	        $arlen = count($ar[1]);
	        for ($i = 0;$i < $arlen;$i++) {
			    if (isExistStr($ar[1][$i],"cartoon")){
				    $id = "hdcartoon_".StrSlice($ar[1][$i],"n_",".");
				}elseif (isExistStr($ar[1][$i],"hddoc_")){
				    $id = "hddoc_".StrSlice($ar[1][$i],"c_",".");
				}else{ 
				    $id = "hdteleplay_".StrSlice($ar[1][$i],"y_",".");
				} 
			    $title = $ar[2][$i];
			    $result = $result.$title."$".$id."$"."hd_openv\r\n";
			}
		    echo rtrim($result,"\r\n");
		}
	}else{
		echo gatherOpenvHdPlay($url);
	}
}

function gatherOpenvHdPlay($url){
	$pageStr = utf82gb(getRemoteContent($url));
	if (isExistStr($url,"clips")){
		$id="hdmovieclips_".StrSlice($url,"s_",".");
		$title=StrSlice($pageStr,"<title>","</title>") ; $title=msubstr($title,strpos($title,"-高清电影")-1);
	}elseif (isExistStr($url,"prog_")){
		$id="CTIYuLeprog_".StrSlice($url,"g_",".");
		$title=StrSlice($pageStr,"<title>","</title>") ; $title=msubstr($title,strpos($title,"-高清电视节目")-1) ;
    }else{
		$id="hdmovie_".StrSlice($url,"e_",".");
		$title=StrSlice($pageStr,"<title>","</title>") ; $title=msubstr($title,strpos($title,"-高清电影")-1);
	} 
	return $title."$".$id."$"."hd_openv";
}

function gatherSinaHdPlay($url){
	$allstr = utf82gb(getRemoteContent($url));
	$id=StrSlice($allstr,"vid:'","'");
	$title=StrSlice($allstr,"episode:'","'");
	return "第".$title."集"."$".$id."$"."iask\r\n";
}

function gatherSinaHd($url){
	if (isExistStr($url,"/movie/detail/")){
		$allstr = utf82gb(getRemoteContent($url));
		$getstr=StrSlice($allstr,"分集点播 begin","分集点播 end");
		$x=StrSplits($getstr,'<div class="pic"><a href="','"');
		$arlen = count($x);
	    for ($i = 0;$i < $arlen;$i++) {
			$l.=gatherSinaHdPlay(paseAbsURI($x[$i],$url));
		}
		$l = rtrim($l, "\r\n");
		echo $l;
	}
}

function gatherKu6($url){
	if (isExistStr($url,'playlist/')){
		$topicId=StrSlice($url,"index_",".html");
		gatKu6List($topicId,1);
	}else{
		$pageStr = getRemoteContent($url);
		if ($pageStr<>""){
			$id=StrSlice($pageStr,"vid : '","'");
			$title=StrSlice($pageStr,"<title>"," - ") ;
			$result=decodeSohu($title)."$".$id."$"."ku6";
		}
		echo $result;
	}
}

function gatherJoy($url){
	if (isExistStr(strtolower($url),"/album/")){
		$lid=StrSlice($url,"/album/","/");
		if (!is_numeric($lid)) $lid=StrSlice($url,"/album/",".htm");
		if (!is_numeric($lid)) die("无法识别的lid");
		$allstr = utf82gb(getRemoteContent("http://you.joy.cn/play.do?method=albumvideolist&id=".$lid."&sort="));
		$x=StrSplits($allstr,ifthen(isExistStr($allstr,'playUrl: "'),'playUrl: "','playUrl:"'),'"');
		$arlen = count($x);
	    for ($i = 0;$i < $arlen;$i++) {
			gatherJoy($x[$i]);
		}
	}elseif (isExistStr(strtolower($url),"/tvplay/") || isExistStr(strtolower($url),"/teleplay/detail/")){
		$lid=StrSlice($url,"/tvplay/","/");
		if (!is_numeric($lid)) $lid=StrSlice($url,"/tvplay/",".htm");
		if (!is_numeric($lid)) $lid=StrSlice($url,"/teleplay/detail/",".htm");
		if (!is_numeric($lid)) $lid=StrSlice($url,"/teleplay/detail/","/");
		if (!is_numeric($lid)) die("无法识别的tvid");
		$allstr = utf82gb(getRemoteContent($url));
		$title=StrSlice($allstr,'<h1 title="','"');
		$x=StrSplits($allstr,'videoId:"','"');
		$l = count($x);
		if ($l==0){
			$result= $title." 全集$2_2_1_57_".$lid."_".$x[0]."$joy";
		}else{
			for ($i = 0;$i < $l;$i++) {
				$result= $result.ifthen($result<>"",'\n',"")."第".ifthen($i<9,"0","").($i+1)."集 ".$title."$2_2_1_57_".$lid."_".$x[$i]."$"."joy";
			}
		}
	}elseif (isExistStr(strtolower($url),"/movie/") || isExistStr(strtolower($url),"/movie/detail/")){
		$lid=StrSlice($url,"/movie/","/");
		if (!is_numeric($lid)) $lid=StrSlice($url,"/movie/",".htm");
		if (!is_numeric($lid)) $lid=StrSlice($url,"/movie/detail/",".htm");
		if (!is_numeric($lid)) die("无法识别的tvid");
		$allstr = utf82gb(getRemoteContent($url));
		$title=StrSlice($allstr,'<h1 title="','"');
		$x=StrSplits($allstr,'videoId:"','"');
		$l = count($x);
		if ($l==0){
			$result= $title." 全集$2_2_1_57_".$lid."_".$x[0]."$joy";
		}else{
			for ($i = 0;$i < $l;$i++) {
				$result= $result.ifthen($result<>"",'\n',"")."第".ifthen($i<9,"0","").($i+1)."集 ".$title."$2_2_1_57_".$lid."_".$x[$i]."$"."joy";
			}
		}
	}elseif (isExistStr(strtolower($url),"/video/")){
		$allstr = utf82gb(getRemoteContent($url));
		$title=StrSlice($allstr,'og:description" content="','"');
		$lid=StrSlice($allstr,'programId:"','"');
		if (!is_numeric($lid)) $lid=0;
		$id=StrSlice($allstr,'videoType:"','"')."_".StrSlice($allstr,'playerMode:"','"')."_1_".StrSlice($allstr,'channelId:"','"')."_".$lid."_".StrSlice($allstr,'videoId:"','"');
		$result= $result.ifthen($result<>"",'\n',"").$title."$".$id."$"."joy";
	}else{
		$allstr = utf82gb(getRemoteContent($url));
		$title=StrSlice($allstr,'<h1 title="','"');
		$x=StrSlice($allstr,'programId:"','"');
		if (empty($x)) $x=0;
		$id=StrSlice($allstr,'videoType:"','"')."_".StrSlice($allstr,'playerMode:"','"')."_1_".StrSlice($allstr,'channelId:"','"')."_".$x."_".StrSlice($allstr,'videoId:"','"');
		$result= $result.ifthen($result<>"",'\n',"").$title."$".$id."$"."joy";
	}
	echo $result;
}

function gatherQiyi($url){
	$allstr = utf82gb(getRemoteContent($url));
	$result="";
	if (isExistStr($allstr,'-firstEpisodeUrl"')){
		$url=mtrim(replaceStr(StrSlice($allstr,'-firstEpisodeUrl"','" title="'),'href="',""));
		$allstr = utf82gb(getRemoteContent($url));
		$pid=StrSlice($allstr,'pid : "','"');
		$ptype=StrSlice($allstr,'ptype : "','"');
		$albid=StrSlice($allstr,'albumId : "','"');
		$tmp=utf82gb(getRemoteContent("http://cache.video.qiyi.com/l/".$pid."/".$ptype."/".$albid));
		$videos=StrSplits($tmp,'"videoUrl":"','"');
		$videoNames=StrSplits($tmp,'"videoName":"','"');
		$tmp="";
		foreach ($videos as $row) {
			$result.=qiyiPlay($row);
		}
		echo $result;
	}else{
		$pid=StrSlice($allstr,'pid : "','"');
		$ptype=StrSlice($allstr,'ptype : "','"');
		$albid=StrSlice($allstr,'albumId : "','"');
		$result=StrSlice($allstr,'title : "','"')."$vid=".trim(StrSlice($allstr,'videoId : "','"'))."-pid=".$pid."-ptype=".$ptype."-albumId=".$albid."-tvId=".StrSlice($allstr,'tvId : "','"')."$"."qiyi";
		echo $result;
	}
}

function qiyiPlay($url){
	$allstr = utf82gb(getRemoteContent($url));
	$pid=StrSlice($allstr,'pid : "','"');
	$ptype=StrSlice($allstr,'ptype : "','"');
	$albid=StrSlice($allstr,'albumId : "','"');
	$result=StrSlice($allstr,'title : "','"')."$vid=".trim(StrSlice($allstr,'videoId : "','"'))."-pid=".$pid."-ptype=".$ptype."-albumId=".$albid."-tvId=".StrSlice($allstr,'tvId : "','"')."$"."qiyi\r\n";
	return $result;
}

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

function RegReplace($Str,$rule,$value)
{
	if (!empty($Str)){
		$result_text = preg_replace($rule,$value,$Str);
	}
	return $result_text;
}
function mtrim($sStr){
	return RegReplace($sStr,"/(^\s*)|(\s*$)/is","");
}

function paseAbsURI($aim, $baseUrl) { 
    $strSource = $baseUrl; 
    if (preg_match_all('/([^:\\/]+)\//', $strSource, $aryResult1, PREG_PATTERN_ORDER)) { 
        $intUrlLevel= count($aryResult1[0]); 
    } else { 
        $intUrlLevel = 0; 
        preg_match_all('/http:\/\/(.+?)$/', $strSource, $aryResult1, PREG_PATTERN_ORDER); 
        $aryResult1[0][0] = $aryResult1[1][0] . "/"; 
    }
    $strSource = $aim; 
    if (substr($strSource,0,7) == 'http://') { 
        return $aim; 
    } else {
	    if (preg_match_all("/^\//",$strSource, $aryResult2, PREG_PATTERN_ORDER)){ 
		    $strAimUrl = "http://" . $aryResult1[1][0] . str_replace("../","",$aim); 
		} else { 
			preg_match_all('/(\\.\\.\/)/', $strSource, $aryResult2, PREG_PATTERN_ORDER); 
			$intAimUrlLevel= count($aryResult2[0]); 
			if ($intUrlLevel<=$intAimUrlLevel){ 
		        $strAimUrl = "http://" . $aryResult1[0][0] . str_replace("../","",$aim); 
			} else { 
				$strAimUrl = "http://" . implode("",array_slice ($aryResult1[0], 0, $intUrlLevel - $intAimUrlLevel)) . str_replace("../","",$aim); 
			} 
		} 
	    return $strAimUrl; 
	}
}
?>
