<?php
error_reporting(0);
header("Content-type: text/xml; charset=utf-8");
$thisurl = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'];
$query = $_SERVER['QUERY_STRING'];
parse_str($query);
if ($key) {
        if (isset ($date)) {
                cctv_date($key, $date);
                exit;
        } else {
                cctv($key);
                exit;
        }
} else {
        index();
        exit;
}
function index() {
        global $thisurl;
        $listarr = array (
                "cctv1" => "CCTV-1综合频道","cctv2" => "CCTV-2财政频道","cctv3" => "CCTV-3综艺频道","cctv4" => "CCTV-4中文国际","cctv5" => "CCTV-5体育频道",
                "cctv6" => "CCTV-6电影频道","cctv7" => "CCTV-7军事农业","cctv8" => "CCTV-8影剧频道","cctv9" => "CCTV-9新闻国际","cctv10" => "CCTV-10科教频道",
                "cctv11" => "CCTV-11戏曲频道","cctv12" => "CCTV-12社会与法","cctvgaoqing" => "CCTV-HD高清频道","cctvchildren" => "CCTV-14少儿频道","cctvmusic" => "CCTV-15音乐频道",
                "cctv9" => "CCTV-NEWS","cctvf" => "CCTV-法语频道","cctvarabic" => "CCTV-阿拉伯语","russian" => "CCTV-俄语国际","cctveurope" => "CCTV-4欧洲国际",
                "cctvamericas" => "CCTV-4美洲国际","btv1" => "北京卫视","shanghai" => "东方卫视","tianjin" => "天津卫视","chongqing" => "重庆卫视","sichuan" => "四川卫视",
                "hunan" => "湖南卫视","guangdong" => "广东卫视","hebei" => "河北卫视","sichuan" => "山西卫视","liaoning" => "辽宁卫视","jilin" => "吉林卫视","zhejiang" => "浙江卫视",
                "anhui" => "安徽卫视","dongnan" => "东南卫视","jiangxi" => "江西卫视","shandong" => "山东卫视","henan" => "河南卫视","hubei" => "湖北卫视","luyou" => "旅游卫视",
                "yunnan" => "云南卫视","shanxi2" => "陕西卫视","gansu" => "甘肃卫视","qinghai" => "青海卫视","guangxi" => "广西卫视","ningxia" => "宁夏卫视",
                "guizhou" => "贵州卫视","heilongjiang" => "黑龙江卫视","neimenggu" => "内蒙古卫视","xinjiang" => "新疆卫视");
        $list = "<list>\n";
        foreach ($listarr as $key => $value) {
                $list .= "<m list_src=\"$thisurl?key=$key\" label=\"$value\" />\n";
        }
        echo $list . "</list>\n";
}
function cctv($key) {
        date_default_timezone_set("PRC");
        global $thisurl;
        $Y = date("Y");
        $m = date("m");
        $d = date("d");
        $list = "<list>\n";
        $mon=30; //设置每个节目显示天数 最大为30天，最小为2
        if ($d >= $mon) {
                for ($i = $d +0; $i > $d -$mon; $i--) {
                        if ($i < 10) {$k = "0$i";} 
                        else {$k = $i;}
                        $riqi = "$Y/$m/$k";
                        $list .= "<m label=\"$riqi\" list_src=\"$thisurl?key=$key&date=$riqi\" />\n";
                }
        } else {
                for ($i = $d +0; $i > 0; $i--) {
                        if ($i < 10) {$k = "0$i";}
                         else {$k = $i;}
                        $riqi = "$Y/$m/$k";
                        $list .= "<m label=\"$riqi\" list_src=\"$thisurl?key=$key&date=$riqi\" />\n";
                }
                $j = $m -1;
                if($j<10){$j="0$j";}
                $lm = "$Y-$j-$d";
                $days = date('t', strtotime("$lm")); //获取上个月天数
                for ($i = $days; $i > $days + $d -$mon; $i--) {
                        if ($i < 10) {$k = "0$i";}
                        else {$k = $i;}
                        $riqi = "$Y/$j/$k";
                        $list .= "<m label=\"$riqi\" list_src=\"$thisurl?key=$key&date=$riqi\" />\n";
                }
        }
        header("Content-type: text/xml; charset=utf-8");
        echo $list . "</list>";
}

function cctv_date($key, $day) {
        date_default_timezone_set("PRC");
        $key=strtolower($key);
        $thisday = date("Y/m/d");
        $thishour=date("H");
        $list = "<list>\n";
        //http://bugu.cntv.cn/nettv/jiemudan/xmldata/2011/10/02/$key.xml
        $ccurl = "http://bugu.cntv.cn/nettv/jiemudan/xmldata/$day/$key.xml";
        $xday=str_replace('/','-',$day);
        $cstr = file_get_contents($ccurl);
        $res = simplexml_load_string($cstr,NULL,LIBXML_NOCDATA);
        $res = json_decode(json_encode($res),true);
        $programs=$res["programs"]["program"];
        foreach($programs as $program){
                $program=$program["@attributes"];
                $showTimes=$program['showTime'];
                $showTime=explode(':',$showTimes);
                $hour=$showTime[0];
                if(strlen($hour)<=1){
                        $hour="0$hour";
                }
                $lt=$showTime[1];
                $rt=60-$lt;
                $title=$program['title'];
                $td=$program['duration'];
                $jt=$td/60;
                $hhour=$hour+1;
                if(strlen($hhour)<2){
                        $hhour="0$hhour";
                }
                $ks=ceil($lt/5);
                if($ks==0){
                                $ks=1;
                }
                //如果超过现实时间则结束
                if($day==$thisday&&$hour>=$thishour){
                         continue ;
                }
                if($day>$thisday){
                        die("超过日期");
                }
                        
                if($jt<=$rt){
                        $js=ceil($rt/5);
                        for($i=$ks;$i<$js;$i++){
                                if($i<10){
                                        $i="0$i";
                                }
                                $list.="<m label=\"$title\" src=\"http://v.cctv.com/live_back/nettv_$key/$key-$xday-$hour-0$i.mp4\" />\n";
                        }
                }else{
                        for($i=$ks;$i<=12;$i++){
                                if($i<10){
                                        $i="0$i";
                                }
                                $list.="<m label=\"$title\" src=\"http://v.cctv.com/live_back/nettv_$key/$key-$xday-$hour-0$i.mp4\" />\n";
                        }
                        for($o=1;$o<=($jt-$rt)/5+1;$o++){
                                if($o<10){
                                        $o="0$o";
                                }
                                if($o>12||$hhour>23){
                                        break;
                                }

                                $list.="<m label=\"$title\" src=\"http://v.cctv.com/live_back/nettv_$key/$key-$xday-$hhour-0$o.mp4\" />\n";
                        }

                }


        }
        echo $list."</list>";
}
/*
*淘汰的第二套方案 每个小时只获取一个节目
function cctv_date($key, $day) {
        $key=strtolower($key);
        $thisday = date("Y/m/d");
        $list = "<list>\n";
        //http://bugu.cntv.cn/nettv/jiemudan/xmldata/2011/10/02/$key.xml
        $ccurl = "http://bugu.cntv.cn/nettv/jiemudan/xmldata/$day/$key.xml";
        $cstr = file_get_contents($ccurl);
        $sxml = simplexml_load_string($cstr);
        $programs = $sxml->programsback->program;
        foreach ($programs as $program) {
                $program = (array) $program;
                $details = $program['details'];
                $num = count($details)-1;
                $farr = (array) $details[0];
                $endarr = (array) $details[$num];
                $t1 = $endarr['@attributes']['time'];
                $t2 = $farr['@attributes']['time'];
                $lurl = $farr['@attributes']['url'];
                $lurl = str_replace('01.mp4', '', $lurl);
                $lurl = str_replace('lback', 'http', $lurl);
                $title = $farr['@attributes']['title'];
                $ttt = time_de($t1, $t2);
                for ($i = 1; $i <= $ttt; $i++) {
                        if ($i < 10) {
                                $i = "0$i";
                        }
                        $murl = "$lurl$i.mp4";
                        if($i>12){
                        break;
                        }
                        if ($day != $thisday) {
                                $list .= "<m src=\"$murl\" label=\"$title\" />\n";
                                } else {
                                if (time_fa($t2)) {
                                        $list .= "<m src=\"$murl\" label=\"$title\" />\n";
                                        } else {
                                        break;
                                        }
                                }
                        }
                
        }
        $list .= "</list>";
        echo $list;

}

function time_fa($tt) {
        date_default_timezone_set("PRC");
        $t = date("H");
        $tt = explode(':', $tt);
        if ($t > $tt[0]) {
                return true;
        } else {
                return false;
        }
}
function time_de($t1, $t2) {
        $tt1 = explode(':', $t1);
        $tt2 = explode(':', $t2);
        if ($tt1[0] < $tt2[0]) {
                $ttt = ($tt1[0] - $tt2[0] + 24) * 60 + $tt1[1] - $tt2[1];
        } else {
                $ttt = ($tt1[0] - $tt2[0]) * 60 + $tt1[1] - $tt2[1];
        }
        return ceil($ttt / 5);
}
*/
?>