<?php
class sina
{
	private $curl = null;
	private $mid = 0;
	private $url = '';
	private $html = '';
	private $total = 0;
	private $specialid = 0;
	function sina($url, $mid)
	{
		$this->curl = qbase::get_object('curl');
		$this->url = $url;
		$this->mid = $mid;
	}

	public function get()
	{
		if(strpos($this->url, '/b/') !== false){
			return $this->get_single_player();
		}else if(strpos($this->url, '/a/') !== false || strpos($this->url, "/topicdetail/topicPlay") !== false){
			return $this->get_player();
		}else if(preg_match("/movie\.video\.sina\.com\.cn\/(movie|teleplay|documentary|category)\//", $this->url, $match)){
			if($match[1] == 'movie'){
				$link = $this->get_dapian_player($this->url);
				return array($link);
			}else{
				if("detail.html" == substr($this->url, -11)){
					$this->url = substr($this->url, 0, -11) . "001.html";
				}
				$html = $this->curl->load($this->url);
				$html = $this->curl->cut($html, "<div id=\"content_1\"", "<div id=\"sliders_rule\">");
				preg_match_all("/\<a href=\"(.*?)#play\"/ies", $html, $temp);
				$url = array_unique($temp[1]);
				$data = array();
				foreach($url as $val){
					$tmp= $this->get_dapian_player("http://movie.video.sina.com.cn" . $val);
					if(!empty($tmp)) $data[] = $tmp;
				}
				return $data;
			}
		}else if(preg_match("/video\.sina\.com/", $this->url)){
			return $this->get_single_player();
		}
		return array();
	}

	private function get_dapian_player($url){
		$html = $this->curl->load($url);
		$title = explode("-", $this->curl->charset_to_utf8($this->curl->cut($html, "<title>", "</title>")));
		$name = preg_replace("/《(.*)》/", "\\1" , $title[0]);
		$vid = intval($this->curl->cut($html, "var vid='", "';"));
		return $vid > 0 ? array($this->charsetLocal($name), $vid) : array();
	}

	public function get_single_player()
	{
		$t = explode("/", $this->url);
		$tt = array_pop($t);
		$player = intval($tt);
		if($player < 1) return array();
		$html = $this->curl->load($this->url);
		$name_all = explode("_", $this->curl->cut($html, "<title>", "</title>"));
		$name = $this->charsetLocal(trim($name_all[0]));
		$data[] = array($name, $player);
		return $data;
	}

	public function get_player()
	{
		//topicPlay.php?tid=2707725&uid=1592318883&t=1#636351
		$tid = intval($this->curl->cut($this->url, "?tid=", "&uid"));
		if($tid == 0){
			$tid = intval(substr($this->url, strpos($this->url, '/a/')+3));
		}
		$url = "http://you.video.sina.com.cn/pg/topicdetail/getTopicVideo.php?tid={$tid}&page=1&pagesize=1000&extaData=2";
		$this->html = $this->curl->load($url);
		$temp = qbase::jsondecode($this->html);
		$data = array();
		if(is_array($temp['list'])) foreach($temp['list'] as $val){
			$val['name'] = preg_replace("/【(.*)】/", "", $val['name']);
			$data[] = array($this->charsetLocal(addslashes($val['name'])), $val['vid']);
		}
		$data = $this->order($data);
		return $data;
	}
	private function order($data)
	{
		if(count($data) < 3) return $data;
		$temp = $id = array();
		foreach($data as $key=>$val){
			$temp[$key] = $this->get_int($val[0]);
			$id[$key] = $key;
		}
		//$n = count($temp);
		//$m = count(array_unique($temp));
		array_multisort($temp, SORT_ASC, $id, SORT_ASC, $data);
		return $data;
	}

	public function get_int($str)
	{
		$arr = array('０','１','２','３','４','５','６','７','８','９');
		$to = array('0','1','2','3','4','5','6','7','8','9');
		$str = str_replace($arr, $to, $str);
		$str = preg_replace("/200[0-9]/", "", $str);
		$t = '';
		$status = 0;
		for ($i=0;$i<strlen($str);$i++){
			$s = substr($str,$i,1);
			if (is_numeric($s)) {
				if($status == 2) break;
				$status = 1;
				$t .= $s;
			}else{
				if($status == 1) $status = 2;
			}
		}
		while($t[0] === '0'){
			$t = substr($t, 1);	
		}
		return $t;
	}

	function charsetLocal($s){
		global $_SET;
		return qbase::iconvs($s, $_SET['def_charset'], 'utf-8');
	}

	function linkToUrl($code){
		return "http://v.youku.com/v_show/id_". $code .".html";
	}
}
?>