<?php  
class downwordAction extends FirstendAction {	
    public function _initialize() {
        parent::_initialize();		
    }	
	
	public function index(){
		header("Access-Control-Allow-Origin: *");
		if(IS_POST){		
		$link = I('link');		
		$ip = get_client_ip();
		$isb = I('isb');
		if($link && $ip){
		$order = md5($link.$ip);			
		$pay = D('ipay_log')->where(array('out_trade_no'=>$order,'pass'=>1))->find();
		if(!$pay && !$isb){
		$this->ajaxReturn (0,'非法下载!');	
		}
		$savepath = C('ftx_attach_path') .'downword/'.$order.'/';
		if(!is_dir($savepath)){        
		$this->make_dir($savepath);	
		}
		$ua = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36";		
		$ch = curl_init();   		
		curl_setopt($ch, CURLOPT_URL, $link); 
		curl_setopt($ch, CURLOPT_REFERER, $link);
		curl_setopt($ch, CURLOPT_TIMEOUT, 5);          
		curl_setopt($ch, CURLOPT_USERAGENT, $ua);      
		curl_setopt($ch ,CURLOPT_FOLLOWLOCATION,1);   		
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 		
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);		
		$source = curl_exec($ch);   
		curl_close($ch);  	
		if(!$source){
		$source = http($link);	
		}		
		$title = get_word($source,$this->fs(C('ftx_t_s')),$this->fs(C('ftx_t_e')));
		$zhen = get_word($source,$this->fs(C('ftx_a_s')),$this->fs(C('ftx_a_e')));				
		if($zhen){
		$zhen = preg_replace("/<p[^>]+>/", "<p>", $zhen);	
		//$txt = mb_convert_case($zhen,MB_CASE_LOWER,'UTF-8');
		//$txt = preg_replace("/<a[^>]+>/", "", $zhen);
		//$txt = preg_replace("/<\/a>/", "", $txt);				
		//$txt = preg_replace("/<font[^>]+>/", "", $txt);
		//$txt = preg_replace("/<\/font>/", "", $txt);	
		//$txt = preg_replace( "@<script(.*?)</script>@is", "", $txt ); 
		//$txt = preg_replace( "@<iframe(.*?)</iframe>@is", "", $txt ); 
		//$txt = preg_replace( "@<style(.*?)</style>@is", "", $txt ); 
		//$txt = preg_replace("/<img[^>]+>/", "", $txt);	
		$txt = strip_tags($zhen,'<p>,<img>,<center>');			
		//$txt = str_replace("<br/>", '', $txt);
		$txt = str_replace('src="/data', 'src="'.C('ftx_site_url').'data', $txt);
		$txt = str_replace('src="data', 'src="'.C('ftx_site_url').'data', $txt);
		$txt = str_replace('　', '', $txt);
		$txt = str_replace('	', '', $txt);
		$txt = str_replace(array("\r\n", "\r", "\n"), "", $txt); 
		$txt = str_replace(PHP_EOL, '', $txt);		
		//$txt = str_replace("<strong>", '', $txt);	
		//$txt = str_replace("</strong>", '', $txt);
		//$txt = str_replace("</div>", '', $txt);
		//$txt = preg_replace("/<p[^>]+>/", "<p>", $txt);
		$txt = $title.'<p></p><p></p>'.$txt;
		}			
		if($title && $txt){		
		$wordname = md5($title.$ip).".doc";		
		//$wordname = iconv('UTF-8','GBK',   $wordname);//防止乱码			
		$filename = $savepath.$wordname;
		if(!file_exists($filename)){
		$word = new set_word();
		$word->start();			
		//$html = mb_convert_encoding($txt, "UTF-8", "GBK");//防止乱码	
		echo $txt;		
		$items = $word->save($filename); //可以自定义保存路径
		ob_flush();//每次执行前刷新缓存
		flush();		
		}		
		$this->ajaxReturn (1,'',C('ftx_site_url').$filename);	
		}else{
		$this->ajaxReturn (0,'抓取失败');		
		}			
		}else{
		$this->ajaxReturn (0,'非法下载!');
		}
		}else{
		$this->ajaxReturn (0,'非法请求!');	
		}
		
	}
	public function fs($da){
		$dda = str_replace('/','\/',$da);
		return $dda;
	}
	public  function make_dir($path){   
        if(!file_exists($path)){
        $mk=@mkdir($path,0777,true);
        @chmod($path,0777,true);   
        }   
        return true;   
   }    
	
    
    
}
?>