<?php
class caijiautoAction extends BackendAction {
	public function _initialize() {
        parent::_initialize();
        
    }

	public function index(){		
		$upload_path = C('ftx_attach_path') . 'txt/';
		$filename = scandir($upload_path);
		for($i=2;$i<count($filename);$i++){
		 $files[] =  mb_convert_encoding($filename[$i],'UTF-8','GBK');		 	 
	    }	
		$totalnum = count($files);		
		$this->assign('totalnum', $totalnum);
		$this->assign('files', $files);
		$catelist = D('article_cate')->select();
		$this->assign('catelist', $catelist);
		$this->display();
	}
	
	
	public function read($file_txt){	
	    if (!file_exists($file_txt)) {
		$this->ajaxReturn(0, '文件不存在！');	
		}
        $data = array();
        $i = 0;
        $content = fopen($file_txt, 'r');
        if (!$content) {
        $this->ajaxReturn(0, $file_txt);
        }
        while (!feof($content)) {
        $str = fgets($content);
        // 读取的数据存在乱码需要设置编码
        $encoding = mb_detect_encoding($str, array('GB2312', 'GBK', 'UTF-16', 'UCS-2', 'UTF-8', 'BIG5', 'ASCII'));
        if ($encoding !== false) {
        if ($encoding !== "EUC-CN") {
            if ($encoding !== "CP936") {
                $data[$i] = iconv($encoding, "UTF-8", $str);
            } else {
                $data[$i] = mb_convert_encoding($str, 'UTF-8', $encoding);
            }
        } else {
            $data[$i] = mb_convert_encoding($str, 'UTF-8', $encoding);
        }
        } else {
        $data[$i] = mb_convert_encoding($str, 'UTF-8', 'Unicode');
        }    
        $i++;
        unset($str);
        }
        fclose($content);
        return $data;
	}

    public function collect() {		
		$p = I('p',1,'intval');
		$status = I('status');
		$cate_id = I('cate_id');		
		$author = I('author');		
		if(!$author){
		$author = C('ftx_ai_author');	
		}
		$upload_path = C('ftx_attach_path') . 'txt/';
		if($p==1){
			$totalcoll = 0;
		}else{
			$totalcoll = F('totalcoll');
		}		
		$files = scandir($upload_path);
		foreach ($files as $val) {
		   $fileInfo = pathinfo($val);
		   if ($fileInfo['extension'] === 'txt') {
              $filelist[] = $val;
		   }
		}
		$totalnum = count($filelist);
		if(!$totalnum){
		F('totalcoll',NULL);
		$obj_dir = new Dir;		
		$obj_dir->del($upload_path);
		$this->ajaxReturn(0, '全部导入完成！');		
		}		
		$xfile = $filelist[0];
		$title =  mb_convert_encoding($xfile,'UTF-8','GBK');
		$title = str_replace(".txt","", $title); 
		$file = $upload_path.$xfile;		
		$arr =  $this->read($file);			
		//$info =  mb_convert_encoding($info,'UTF-8','GBK');
		//$arr = explode(PHP_EOL,$info);
		for($s=0;$s<count($arr);$s++){
			if(trim($arr[$s])){
			$infos .= '<p>'.$arr[$s].'</p>';	
			}
		}
		$infod = $infos;	
		if($cate_id){
		$cinfo = D('article_cate')->where(array('id'=>$cate_id))->find();	
		if($cinfo['peitu'] && C('ftx_img_put')==2){
		if($cinfo['ename']){	
		$cpath = $cinfo['ename'];
		}else{
		$cpath = 'pic';	
		}
		$path = "data/upload/article/".$cpath."/";	
		$newname = md5($title);
		if(is_file($path.$newname.'.'.C('ftx_img_type'))){                                		
        $newimg = C('ftx_site_url').$path.$newname.'.'.C('ftx_img_type');		
        }else{			
		$newimg = $this->doimg($cpath,$title);			
		}
		if(is_file($path.$newname.'.'.C('ftx_img_type'))){		
		$position = strpos($infos, "<p>", strpos($infos, "<p>") + 1); 
        $imageHTML = '<p><center><img src="'.$newimg.'" alt="'.$title.'"></center></p>';
		$infod = substr_replace($infos, $imageHTML, $position, 0);	
		}		
		}		
		}		
		preg_match("<img.*src=[\"](.*?)[\"].*?>", $infod, $match);
		$item['mpic'] = $match[1];
		$item['intro'] = utf_substr($infod,150);			
		$item['mid'] = substr(md5(md5($title)),8,16);
		$item['cate_id'] = $cate_id;	
		$item['title'] = $title;
		$item['author'] = $author;		
		$item['add_time'] = time();
		$item['status'] = $status;		
		$item['fabu'] = 1;	
		$coll=0;
		if($item['cate_id'] && $item['title'] && $infod){			
			$res = D('article')->add($item);
			if($res>0){	
			unlink($file);
			$items['vid'] = $res;
			$items['info'] = $infod;				
			D('article_'.$cate_id)->add($items);
				$coll++;
			}
		}
		$totalcoll++;		
		F('totalcoll',$totalcoll);		
		$this->assign('p',$p);
		$this->assign('coll', $coll); 
		$this->assign('totalnum', $totalnum);
		$this->assign('totalcoll', $totalcoll);
		$resp = $this->fetch('collect');
		$where['title'] = $title;
		if(D('article')->where($where)->find()){
		unlink($file);		
		$this->ajaxReturn(1, '文档标题重复已删除',$resp);
		}		
		$this->ajaxReturn(1, '关键词导入中', $resp);
    }
	public function doimg($cpath,$title){	
		    $con = "data/upload/peitu/".$cpath."/";
		    $imgname = scandir($con);
		    for($y=2;$y<count($imgname);$y++){
                $conname[] = $imgname[$y];
		    }
		    if(count($conname)){
                $num = count($conname)-1;
                $pi = rand(0,$num);
                $img = $conname[$pi];
                $src = C('ftx_site_url').$con.$img;		
                $path = "data/upload/article/".$cpath."/";		
                $this->make_dir($path);
                $newname = md5($title);	                
                Image::image_hp($src,$title,$path,$newname);	
                $newimg = C('ftx_site_url').$path.$newname.'.'.C('ftx_img_type');	
				return $newimg;
			}	
	}
    public function make_dir($path)
    {
        if (!file_exists($path)) {
            $mk = @mkdir($path, 0777, true);
            @chmod($path, 0777, true);
        }
        return true;
    }
}