<?php
class pushAction extends BackendAction {
	public function _initialize() {
		parent::_initialize();	
		$config_file = CONF_PATH . 'url.php';
        $this->_config = require $config_file;	
	}
	public function index() {
		$this->display();
	}
	public function getename($cid){
		$ename = D('article_cate')->where(array('id'=>$cid))->getField('ename');
		return $ename;
	}
	public function collect() {
		if(!C('ftx_site_token')){
			$this->ajaxReturn(0, '请在站点设置中填写百度推送工具token！');
		}
		$mod = I('mod');				
		$p = I('p', 1, 'intval');			
		$siteurl = C('ftx_site_url');					
		$page_size = 100;
		$start = $page_size * ($p - 1) ;		
		$order = 'id asc';
		$where = array();			
		if($mod){
		$where['push'] = 0;	
		$start = 0;
		}
		$where['status'] = 1;		
		$where['fabu'] = 1;
		$list = D('article')->field('id,mid,cate_id')->where($where)->order($order)->limit($start . ',' . $page_size)->select();
		$count = D('article')->where($where)->count();				
		foreach($list as $key=>$val){
			if(C('ftx_a_link')=='md5'){
			$vid = $val['mid'];	
			}else{
			$vid = $val['id'];	
			}
			if(C('ftx_url_set')==1){				
			$upt = $this->getename($val['cate_id']);	
			}else{
			if(C('ftx_c_name')){
			$upt = C('ftx_c_name');	
			}else{
			$upt = 'wen';		
			}			
			}		
		    $urls[]= $siteurl.$upt.'/'.$vid.$this->_config['URL_HTML_SUFFIX'];				
		    $idlist[] = $val['id'];
		}	
		
			
		if($mod){			
		if(!$count) {
			$this->ajaxReturn(0, '推送完成！');
		}		
		}else{
		$allpage = ceil($count/100);		
		if($p>$allpage) {
			$this->ajaxReturn(0, '推送完成！');
		}	
		}
		$msg = $this->push($urls,$siteurl);	
		if($msg['status'] && $mod){		
			foreach($idlist as $keys=>$vals) {
		      $res = $this->change($vals);
		    }		
		}
		$this->assign('msg', $msg['msg']);		
		$this->assign('p', $p);
		$this->assign('totalnum', $count);			
		$resp = $this->fetch('collect');
		$this->ajaxReturn(1, '百度推送中', $resp);
	}
	private function change($id){			    
		$res = M('article')->where(array('id'=>$id))->save(array('push'=>1));			
		return $res;
	}
	private function push($urls,$siteurl) {		  
		  $api = 'http://data.zz.baidu.com/urls?site='.$siteurl.'&token='.C('ftx_site_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);
		  $results = curl_exec($ch);
		  $msg = json_decode($results,true);		  
		  $error = $msg['error'];
		  $message = $msg['message'];
		  $remain = $msg['remain'];
		  $success = $msg['success'];
		  $not_same_site = $msg['not_same_site'];
		  $not_valid = $msg['not_valid'];
		  if($error){
		  $ermsg = $message;	  
		  if($message=="site error"){
		  $ermsg = '本站点未在百度站长平台验证';
		  }
		  if($message=="empty content"){
		  $ermsg = '本次推送链接数据为空';
		  }
		  if($message=="only 2000 urls are allowed once"){
		  $ermsg = '每次最多只能提交2000条链接';
		  }
		  if($message=="over quota"){
		  $ermsg = '超过每日配额了，超配额后再提交都是无效的';
		  }
		  if($message=="token is not valid"){
		  $ermsg = 'token错误';
		  }
		  if($message=="not found"){
		  $ermsg = '接口地址填写错误';
		  }
		  if($message=="internal error, please try later"){
		  $ermsg = '服务器偶然异常，通常重试就会成功';
		  }
		  $item['msg'] = '推送失败！返回的错误码：'.$error.'，错误信息提示：'.$ermsg;		
		  $item['status'] = 0;
		  }else{
		  $item['msg'] = '本次成功推送'.$success.'条,今日还可推送'.$remain.'条';
		  $item['status'] = 1;
		  }
       return $item;
    }
	
	
}
?>