<?php 
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Sitemap extends CI_Controller {

	function __construct(){
		parent::__construct();
        $this->load->helper('url');
		$this->load->model('CsdjDB');
		$this->load->model('CsdjSkins');
        $this->db->cache_off();
		$this->token = 'w1kWl5fUWHVGwPWE'; //这里填写您的token
	}


	public function index()
	{
        //获取上次推送时间
		$sts=@file_get_contents(FCPATH."sitemap.txt");
		$arr=explode('-',$sts);
		$urls = array();
		if((int)$arr[0]<time()){
		    $did=(int)$arr[1];
		    $vid=(int)$arr[2];
		    $nid=(int)$arr[3];
		    $sid=(int)$arr[4];
            //歌曲
            $query = $this->db->query("SELECT cs_id FROM ".CS_SqlPrefix."dance where cs_yid=0 and cs_hid=0 and cs_id>".$did." order by cs_id asc limit 500"); 
            foreach ($query->result() as $row) {
			    $urls[]=$this->CsdjSkins->LinkUrl('play','dance','id',$row->cs_id);
				$did=$row->cs_id;
		    }
            //视频
            $query = $this->db->query("SELECT cs_id FROM ".CS_SqlPrefix."vod where cs_yid=0 and cs_hid=0 and cs_id>".$vid." order by cs_id asc limit 500"); 
            foreach ($query->result() as $row) {
			    $urls[]=$this->CsdjSkins->LinkUrl('show','vod','id',$row->cs_id);
				$vid=$row->cs_id;
		    }
            //文章
            $query = $this->db->query("SELECT cs_id FROM ".CS_SqlPrefix."news where cs_yid=0 and cs_hid=0 and cs_id>".$nid." order by cs_id asc limit 500"); 
            foreach ($query->result() as $row) {
			    $urls[]=$this->CsdjSkins->LinkUrl('show','news','id',$row->cs_id);
				$nid=$row->cs_id;
		    }
            //歌手
            $query = $this->db->query("SELECT cs_id FROM ".CS_SqlPrefix."singer where cs_yid=0 and cs_id>".$sid." order by cs_id asc limit 500"); 
            foreach ($query->result() as $row) {
			    $urls[]=$this->CsdjSkins->LinkUrl('show','singer','id',$row->cs_id);
				$sid=$row->cs_id;
		    }
			//写入时间记录
			$str=(time()+7200).'-'.$did.'-'.$vid.'-'.$nid.'-'.$sid;
			@file_put_contents(FCPATH."sitemap.txt",$str);
			if(!empty($urls)){
			    //推送
                $api = 'http://data.zz.baidu.com/urls?site='.Web_Url.'&token='.$this->token;
                $ch = curl_init();
                $options =  array(
                    CURLOPT_URL => $api,
                    CURLOPT_POST => true,
                    CURLOPT_RETURNTRANSFER => true,
                    CURLOPT_POSTFIELDS => implode("\n", $urls),
                    CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
                );
                curl_setopt_array($ch, $options);
                $result = curl_exec($ch);
		        $str = json_decode($result,true);
		        print_r($str);
			}
	    }
	}
}
