<?php
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);
        return $c;
}
function re_place($str) {
        $s = str_replace('\/', '/', $str);
        return $s;
}
function get_u($u) {
        global $fname;
        $list = '';
        if ($u == 'm') {
                for ($i = 1; $i <= 49; $i++) {
                        $q = 'http://list.letv.com/movie/iq_aq_dq_octime_lg_p' . $i . '.html';
                        $list .= '<m label="第' . $i . '页" list_src="' . $fname . '?m=' . $q . '" />' . "\n";
                }
        }
        elseif ($u == 'tv') {
                for ($i = 1; $i <= 49; $i++) {
                        $q = 'http://listapp.letv.com/listpage/list/c/5/i/q/a/q/d/q/o/rate/l/g/p/' . $i . '/';
                        $list .= '<m label="第' . $i . '页" list_src="' . $fname . '?tv=' . $q . '" />' . "\n";
                }
        } else {
                for ($i = 1; $i <= 49; $i++) {
                        $q = 'http://www.letv.com/vchannel/new_ch' . $u . '_d1_p' . $i . '.html';
                        $list .= '<m label="第' . $i . '页" list_src="' . $fname . '?n=' . $q . '" />' . "\n";
                }
        }
        return $list;
}
function get_n($n) {
        global $fname;
        $str = file_data($n);
        $list = '';
        preg_match_all('|<dt><a target="_blank" title="([^>]+)\.html">|', $str, $a1);
        foreach ($a1[1] as $k => $v) {
                $a2 = explode('" href="', $v);
                $a3 = str_replace('http://www.letv.com/ptv/vplay/', '', $a2[1]);
                $a4 = $a2[0];
                $list .= '<m label="' . $a4 . '"  src="' . $fname . '?id=' . $a3 . '" />' . "\n";
        }
        return $list;
}
function get_m($m) {
        global $fname;
        $str = file_data($m);
        $list = '';
        preg_match_all('|<dt><a href="([^>]+)" target="_blank">|', $str, $a1);
        foreach ($a1[1] as $k => $v) {
                $a2 = explode('" title="', $v);
                $a3 = $a2[1];
                $list .= '<m label="' . $a3 . '"  src="' . $fname . '?mid=' . $a2[0] . '" />' . "\n";
        }
        return $list;
}
function get_tv($tv) {
        global $fname;
        $str = file_data($tv);
        $list = '';
        preg_match_all('|<dt><a href="([^>]+)" target=|', $str, $a1);
        foreach ($a1[1] as $k => $v) {
                $a2 = explode('" title="', $v);
                $a3 = $a2[1];
                $list .= '<m label="' . $a3 . '"  list_src="' . $fname . '?tvid=' . $a2[0] . '" />' . "\n";
        }
        return $list;
}
function get_tvid($tvid) {
        global $fname;
        $strl = file_data($tvid);
        preg_match('|<h1 class="fl">([^<]+)<small>|', $strl, $name);
        preg_match_all('|<li>\s+<a href="([^>]+)"|ims', $strl, $as);
        $name = htmlspecialchars($name[1], ENT_QUOTES);
        $list = '';
        foreach ($as[1] as $k => $v) {
                $z = $k +1;
                $list .= '<m  src="' . $fname . '?mid=' . $v . '" label="第' . $z . '集" />';
        }
        return $list;
}
function get_mid($mid) {
        $strl = file_data($mid);
        preg_match('|vid:([0-9]+),|', $strl, $as);
        $vd = 'http://app.letv.com/v.php?id=' . $as[1];
        $str = file_data($vd);
        preg_match('|"url":"([^"]+)"|', $str, $a);
        $c = re_place($a[1]);
        $fp = file_data($c);
        preg_match('|"location": "([^"]+)"|', $fp, $ar);
        $d = re_place($ar[1]);
        header("location:$d");
}
function get_id($id) {
        $vd = 'http://app.letv.com/v.php?id=' . $id;
        $str = file_data($vd);
        preg_match('|"url":"([^"]+)"|', $str, $a);
        $c = re_place($a[1]);
        $fp = file_data($c);
        preg_match('|"location": "([^"]+)"|', $fp, $ar);
        $d = re_place($ar[1]);
        header("location:$d");
}
function default_list() {
        global $fname;
        $lb = array (
                '电影' => 'm',
                '电视' => 'tv',
                '动漫' => '5',
                '娱乐' => '3',
                '体育' => '4',
                '汽车' => '14',
                '原创' => '7',
                '音乐' => '9',
                '搞笑' => '10',
                '科教' => '12',
                '生活' => '13',
                '其他' => '8'
        );
        $list = '';
        foreach ($lb as $k => $v) {
                $list .= '<m label="' . $k . '" list_src="' . $fname . '?u=' . $v . '" />' . "\n";
        }
        return $list;
}
$fname = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER["SCRIPT_NAME"];
$xml = "<?xml version=\"1.0\" encoding=\"GBK\" ?>\n<list>\n";
if (isset ($_GET['u'])) {
        $xml .= get_u($_GET['u']);
}
elseif (isset ($_GET['n'])) {
        $xml .= get_n($_GET['n']);
}
elseif (isset ($_GET['m'])) {
        $xml .= get_m($_GET['m']);
}
elseif (isset ($_GET['mid'])) {
        $xml .= get_mid($_GET['mid']);
}
elseif (isset ($_GET['tvid'])) {
        $xml .= get_tvid($_GET['tvid']);
}
elseif (isset ($_GET['tv'])) {
        $xml .= get_tv($_GET['tv']);
}
elseif (isset ($_GET['id'])) {
        get_id($_GET['id']);
} else {
        $xml .= default_list();
}
$xml .= "</list>\n";
echo $xml;
?>