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

	function get(){
		global $_SET;
		$data = array();
		if(strpos($this->url, '/show/') !== false){
			$this->html = qbase::iconvs($this->curl->load($this->url), $_SET['def_charset'], 'gbk');
			if(strpos($this->url, 'hd.ku6.com') !== false){
				$name = $this->curl->cut($this->html, "<title>", " - ¸كاه");
				$name = str_replace("،¶", "", $name);
				$name = str_replace("،·", "", $name);
				$name = $this->replace_script($name);
				$html = $this->curl->cut($this->html, "<ul class=\"clearfix\" id=\"chapterPart_1\"", "</ul>");
				if($html){
					preg_match_all("/\<a href=\"http:\/\/hd\.ku6\.com\/show\/(.*?)\.html\"(.*?)\>(.*?)\<\/a\>/", $html, $temp);
					$data = array();
					foreach($temp[3] as $key=>$val){
						$data[] = array(
								$name .' '. $val, $this->linkToUrl($temp[1][$key])
						);
					}
					return $data;
				}else{
					$player = $this->curl->cut($this->url, "/show/", ".html");
					if(!$player || !$name) return false;
					$name = str_replace("،¶", "", $name);
					$name = str_replace("،·", "", $name);
					$name = $this->replace_script($name);
				}
			}else{
				$name = $this->curl->cut($this->html, "<meta name=\"title\" content=\"", "/>");
				$player = $this->curl->cut($this->url, "/show/", ".html");
				if(!$player || !$name) return false;
				$name = str_replace("،¶", "", $name);
				$name = str_replace("،·", "", $name);
				$name = $this->replace_script($name);
			}
			$data[] = array($name, $this->linkToUrl($player));
		}elseif(strpos($this->url, '/filmshow-') !== false){
			$html = $this->curl->load($this->url);
			$result = preg_match("/\<a href=\"(.*?)\" class=\"playSpeed fl\" target=\"show_v\"\>\<\/a\>/", $html, $temp);
			if($result){
				$this->url = $temp[1];
				return $this->get_player();
			}else{
				return $data;
			}
		}else{
			$data = $this->get_player();
		}
		return $data;
	}

	function get_player()
	{
		$this->specialid = $this->get_special_id();
		return $this->get_play_ids($this->specialid);
	}

	function get_special_id()
	{
		$t = explode("special/index_", $this->url);
		$sp =  intval($t[1]);
		if($sp == 0){
			$t = explode("playlist/index_", $this->url);
			$sp =  intval($t[1]);
		}
		if($sp == 0){
			$t = explode("special/show_", $this->url);
			$sp =  intval($t[1]);
		}
		if($sp == 0){
			$t = explode("/film/index_", $this->url);
			$sp =  intval($t[1]);
		}
		return $sp;
	}


	function get_play_ids($specialid)
	{
		$data = array();
		if(!$specialid) return $data;
		for($i=1;$i<1000;$i++){
			$s = $this->curl->load("http://v.ku6.com/playlistVideo.htm?t=list&playlistId={$specialid}&p={$i}&s=8");
			$temp = qbase::jsondecode($s);
			if(!is_array($temp['data']['list'])) break;
			foreach($temp['data']['list'] as $val){
				if(!$val['video']) continue;
				$data[] = array(
					$this->replace_script(qbase::iconvs($val['video']['title'], $_SET['def_charset'], 'utf-8')), $this->linkToUrl($val['video']['vid'])
				);
			}
			if($i * 8 >= $temp['data']['count'] ) break;
		}
		$data = $this->order($data);
		return $data;
	}

	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;
	}

	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 replace_script($str)
	{
		$sc = array('\'', '"', "\n", "\t"," ","،،",'\n');
		$to = array('','','','','','','');
		return str_replace($sc, $to, $str);
	}

	function linkToUrl($code){
		return $code;
	}
}
?>