<?php
//php PPTV官方电影
function file_data($url) {
        $user_agent = $_SERVER['HTTP_USER_AGENT'];
        $ch = curl_init();
        $timeout = 30;
        curl_setopt($ch, CURLOPT_URL, $url);
        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 = mb_convert_encoding($c, 'GB2312', 'UTF-8');
        return $c;
}
function get_u($u) {
        global $fname;
        $t = explode('-', $u);
        $list = '';
        for ($i = 1; $i <= $t[1]; $i++) {
                $q = 'http://list.pptv.com/sort_list/1-' . $t[0] . '----0--' . $i . '.html';
                $list .= '<m list_src="' . $fname . '?n=' . $q . '" label="第' . $i . '页" />' . "\n";
        }
        return $list;
}
function get_n($n) {
        global $fname;
        $str = file_data($n);  
        preg_match_all('|<dt><a href="([^<]+)" target="_play" title=|', $str, $ar1);
        preg_match_all('|target="_play" title="([^"]+)">|', $str, $ar2);
        $ar = array_combine($ar2[1], $ar1[1]);
        foreach ($ar as $i => $d) {
                $list .= '<m list_src="http://ytlsl.sinaapp.com/pptv.php?url=' . $d . '" label="' . $i . '" />';
                $list.= "\n";
        }
        return $list;
}
function default_list() {
        global $fname;
        $lb = array (
                '全部' => '-358',
                '动作' => '100-61',
                '喜剧' => '124-48',
                '爱情' => '101-49',
                '科幻' => '104-7',
                '灾难' => '102-1',
                '恐怖' => '103-24',
                '悬疑' => '125-14',
                '魔幻' => '126-7',
                '战争' => '127-14',
                '犯罪' => '128-24',
                '惊悚' => '129-19',
                '动画' => '130-8',       
                '冒险' => '131-11',
                '青春' => '132-9',
                '剧情' => '133-113',
                '文艺' => '134-10',
                '纪实' => '135-5'
        );
        $list = '';
        foreach ($lb as $k => $v) {
                $list .= '<m list_src="' . $fname . '?u=' . $v . '" label="' . $k . '" />' . "\n";
        }
        return $list;
}
$fname = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER["SCRIPT_NAME"];
$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<list>\n";
if (isset ($_GET['u'])) {
        $xml .= get_u($_GET['u']);
}
elseif (isset ($_GET['n'])) {
        $xml .= get_n($_GET['n']);
}
elseif (isset ($_GET['id'])) {
        $xml .= get_id($_GET['id']);
} else {
        $xml .= default_list();
}
$xml .= "</list>\n";
echo $xml;
?>