<?php
$fname = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'];
function file_get($url) {
        $user_agent = $_SERVER['HTTP_USER_AGENT'];
        $referer = 'http://qiyi.com/';
        $ch = curl_init();
        $timeout = 30;
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_REFERER, $referer);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
        @ $c = curl_exec($ch);
        curl_close($ch);
        @ $c = iconv('UTF-8', 'GBK', $c);
        return $c;
}
function v_key() {
        $url = 'http://data.video.qiyi.com/t.hml?tn=' . rand();
        $str = file_get($url);
        $obj = json_decode($str);
        $t = $obj -> t;
        return bindec(decbin($t ^ 6814186432));
}
function make_list($id) {
        global $fname;
        $strid = file_get($id);
        preg_match('|pid : "([^"]+)"|', $strid, $pid);
        preg_match('|ptype : "([^"]+)"|', $strid, $ptype);
        preg_match('|videoId : "([^"]+)"|', $strid, $videoid);
        $url = 'http://cache.video.qiyi.com/v/' . $videoid[1] . '/' . $pid[1] . '/' . $ptype[1] . '/';
        $str = file_get($url);
        preg_match('|<title>([^<]+)</title>|', $str, $title);
        preg_match_all('|<file>([^<]+)</file>|', $str, $arr);
        $list = '';
        foreach ($arr[1] as $k => $v) {
               $d = $k +1;
               $list .= '<m type="2" src="' . $fname . '?k=' . $v . '"  label="' . $title[1] . '-' . $d . '" />' . "\n";
        }
        return $list;
       }
function fd_location($url) {
        $key = '.hml?v=' . v_key();
        $ak = array (
                '.f4v' => $key
        );
        $url = strtr($url, $ak);
        $str = file_get($url);
        $obj = json_decode($str);
        $flv=$obj->l;
        header("location:$flv");
       }
function str_xml($str) {
        global $fname;
        $xml = '';
        preg_match_all('|<h2 class="h2Title1">\s+<a href="http://www.qiyi.com/dianying/([^>]+)" alt="|ims', $str, $arr);
        foreach ($arr[1] as $v) {
                $v = explode('" title="', $v);
                $xml .= '<m list_src="' . $fname . '?id=http://www.qiyi.com/dianying/' . $v[0] . '" label="' . $v[1] . '" />' . "\n";
        }
        return $xml;
        }
function film_list($s) {
        global $fname;
        $s = explode('-', $s);
        @ $page = $s[1] + 0;
        $xml='';
        $str = '';
        if ($page > 5) {
               $t = ceil($page / 5);
                for ($i = 1; $i <= $t; $i++) {
                        $xml .= '<m list_src="' . $fname . '?fpage=' . $s[0] . '-' . $i . '" label="No.' . $i . '" />' . "\n";
       }
        } else {
               for ($i = 1; $i <= $page; $i++) {
                        $url = 'http://list.qiyi.com/www/1/-' . $s[0] . '-----------2-2-' . $i . '-1---.html';
                        $str .= file_get($url);

                }
                $xml = str_xml($str);
        }
        return $xml;
       }
function film_fpage($s) {
        global $fname;
        $s = explode('-', $s);
        $p = $s[1] + 0;
        $str = '';
        for ($i = $p*5-4; $i <= $p*5+1; $i++) {
               $url = 'http://list.qiyi.com/www/1/-' . $s[0] . '-----------2-2-' . $i . '-1---.html';
               $str .= file_get($url);
        }
        $xml = str_xml($str);
        return $xml;
       }
function moive_list() {
        global $fname;
        $list = array (
                '6-41' => '爱情',
                '7-5' => '战争',
                '8-35' => '喜剧',
                '11-34' => '动作',
                '13-1' => '悲剧',
                '14-1' => '灾难',
                '127-75' => '剧情',
                '128-17' => '惊悚',
                '130-5' => '青春',
                '132-3' => '传记',
                '10-6' => '恐怖',
                '129-6' => '魔幻',
                '9-5' => '科幻',
                '131-3' => '枪战',
                '133-2' => '艺术片'
        );
        $xml = '';
        foreach ($list as $k => $v) {
               $xml .= '<m list_src="' . $fname . '?list=' . $k . '" label="' . $v . '" />' . "\n";
        }
        return $xml;
        }
$xml = '<list>' . "\n";
if (isset ($_GET['id'])) {
        $xml .= make_list($_GET['id']);
        }
elseif (isset ($_GET['k'])) {

        fd_location($_GET['k']);

        }
elseif (isset ($_GET['list'])) {
        $xml .= film_list($_GET['list']);
        }
elseif (isset ($_GET['fpage'])) {
        $xml .= film_fpage($_GET['fpage']);
        }
else {
        $xml .= moive_list();
       }
$xml .= '</list>';
echo $xml;
?>