<?php
class indexAction extends FirstendAction {	
	public function _initialize() {
        parent::_initialize();	
		$this->assign('cur', 'index');		
		$map['status'] = 1;
		$catelist = D('article_cate')->where($map)->order('ordid asc')->select();
		foreach ($catelist as $key => $val) {
			$catelist[$key] = $val;
		}		
		$this->assign('catelist', $catelist);	
		$res = D('article_cate')->field('id,name,ename')->select();
		$cate_list = array();
            foreach ($res as $val) {
                $cate_list[$val['id']] = array($val['name'],$val['ename']);
            }
		$this->_catelist = 	$cate_list;		
		$hot = $this->gethot();
		$new = $this->getnew();
		$this->assign('hot', $hot);	
		$this->assign('new', $new);	
		
    }	
	public function keyWordCheck($content){
        $str = trim($content);
        $nokw = C('ftx_nokw');
        $nokw = trim($nokw);
        $arr = explode(",", $nokw);
        for($i=0,$k=count($arr);$i<$k;$i++){    
            if($arr[$i]==''){
              continue;    
            }
            if(@strpos($str,$arr[$i])!==false){        
              return $arr[$i];
            }    
        }   
        return false;
    }
	public function re_pl($content){
        $str = trim($content);
        $nokw = C('ftx_nokw');
        $nokw = trim($nokw);
        $arr = explode(",", $nokw);
        for($i=0,$k=count($arr);$i<$k;$i++){    
            if($arr[$i]){
             $str = str_replace($arr[$i],"***", $str);   
            }			
        }   
        return $str;
    }
    public function so(){
		$k = I('k','','trim');
		if($k){
		$nowds = $this->keyWordCheck($k);
		if($nowds){				
			header("Content-type: text/html; charset=utf-8");             
			die('<script language="javascript">alert("您搜索的关键词不允许，请更换！");history.go(-1);</script>');
		}else{
		$jpurl = '/rs/'.$k;	
		header('Content-Type:text/plain; charset=utf-8');
		header("Location:" .$jpurl);		
		exit();	
		}				
		}			
	}	
	
	public function index(){	
		$p = I('p',1, 'intval');
		$px = C('ftx_px_or')?C('ftx_px_or'):'id desc';
		$order = 'tui desc, '.$px;
		$page_size = 20;				
		$start = $page_size * ($p - 1) ;
		$map['status'] = 1;		
		$map['fabu'] = 1;	
		$map['tui'] = 1;	
		if(C('ftx_img_put')==2){
		$map['mpic'] = array('exp','is not null');
		}
		$hot_list = D('article')->where($map)->order($order)->limit('0,120')->select();
		if(count($hot_list)>5){
		$hot = array_rand($hot_list,6);
		foreach($hot as $ve){
        $nhotlist[]=$hot_list[$ve];
		}
		}else{
		$nhotlist = $hot_list;	
		}
		foreach ($nhotlist as $key => $val) {			
			$hot_lists[$key] = $val;
			if(C('ftx_a_link')=='md5'){
			$vid = $val['mid'];	
			}else{
			$vid = $val['id'];	
			}
			if(C('ftx_url_set')==1){				
			$upt = $this->_catelist[$val['cate_id']][1];	
			}else{
			if(C('ftx_c_name')){
			$upt = C('ftx_c_name');	
			}else{
			$upt = 'wen';		
			}			
			}
			$url = '/'.$upt.'/'.$vid.C('URL_HTML_SUFFIX');
			$hot_lists[$key]['url'] = $url;	
			$hot_lists[$key]['add_time'] = date('Y-m-d',$val['add_time']);
			$hot_lists[$key]['intro'] = $val['intro'];	
			$hot_lists[$key]['cname'] = $this->_catelist[$val['cate_id']][0];
			$hot_lists[$key]['cename'] = $this->_catelist[$val['cate_id']][1];
			if(C('ftx_img_put')==1){
			if(!$val['mpic']){	
			$hot_lists[$key]['pic'] = '/?m=images&id='.$vid.'.'.C('ftx_img_type');	
			}else{
			$hot_lists[$key]['pic'] = $val['mpic'];		
			}			
			}
			if(C('ftx_img_put')==2){
			$hot_lists[$key]['pic'] = $val['mpic'];	
			}
		}
		$this->assign('tuijian_list',$hot_lists);		
		$where['status'] = 1;	
		$where['fabu'] = 1;	
		$where['tui'] = 0;	
		$item_list = D('article')->where($where)->order($px)->limit($start . ',' . $page_size)->select();
		foreach($item_list as $key=>$val){    
            $item_list[$key] = $val;	
			if(C('ftx_a_link')=='md5'){
			$vid = $val['mid'];	
			}else{
			$vid = $val['id'];	
			}
			if(C('ftx_url_set')==1){
			$upt = $this->_catelist[$val['cate_id']][1];	
			}else{
			if(C('ftx_c_name')){
			$upt = C('ftx_c_name');	
			}else{
			$upt = 'wen';		
			}			
			}			
			$url = '/'.$upt.'/'.$vid.C('URL_HTML_SUFFIX');			
			$item_list[$key]['url'] = $url;
			$item_list[$key]['add_time'] = date('Y-m-d',$val['add_time']);				
			if(C('ftx_img_put')==1){
			if(!$val['mpic']){	
			$item_list[$key]['pic'] = '/?m=images&id='.$vid.'.'.C('ftx_img_type');	
			}else{
			$item_list[$key]['pic'] = $val['mpic'];		
			}			
			}
			if(C('ftx_img_put')==2){
			$item_list[$key]['pic'] = $val['mpic'];	
			}
        }
		$count = D('article')->where($where)->count();
		$this->assign('item_list',$item_list);
		$pager = $this->_pager($count, $page_size);
        $page = $pager->share_show();
		$page = str_replace('/index/index/','/',$page);
		$page = str_replace('/p/','/',$page);
		$this->assign('page',$page);
		$this->assign('p',$p);
		$maps['status'] = 1;
		$link_list = D('link')->where($maps)->order('ordid asc,id desc')->select();
		$this->assign('link_list',$link_list);
		$tag_list = D('tag')->order('id desc')->limit(0,25)->select();
		$this->assign('tag_list',$tag_list);
		$this->_config_seo(C('ftx_seo_config.index'));	
		$this->display();	
	} 	
	public function cate(){	
	    $ename = I('ename');
		$cat = I('cid');
		$px = C('ftx_px_or')?C('ftx_px_or'):'id desc';		
		if($ename){
		$cinfo = D('article_cate')->where(array('ename'=>$ename))->find();	
		}
		if($cat){
		$cinfo = D('article_cate')->where(array('id'=>$cat))->find();	
		}
		!$cinfo && $this->_404();
		
		$cid = $cinfo['id'];
		$this->assign('cinfo', $cinfo);
		$this->assign('cid', $cid);
		$p = I('p',1, 'intval');
		if(C('ftx_cate_page') && $p>1){
		$this->_404();	
		}
		$page_size = 20;				
		$start = $page_size * ($p - 1) ;
		$where['cate_id'] = $cid;
		$where['status'] = 1;		
		$where['fabu'] = 1;		
		$item_list = D('article')->where($where)->order($px)->limit($start . ',' . $page_size)->select();		
		foreach($item_list as $key=>$val){    
            $item_list[$key] = $val;					
			if(C('ftx_a_link')=='md5'){
			$vid = $val['mid'];	
			}else{
			$vid = $val['id'];	
			}
			if(C('ftx_url_set')==1){
			$upt = $this->_catelist[$val['cate_id']][1];	
			}else{
			if(C('ftx_c_name')){
			$upt = C('ftx_c_name');	
			}else{
			$upt = 'wen';		
			}			
			}
			$url = '/'.$upt.'/'.$vid.C('URL_HTML_SUFFIX');
			$item_list[$key]['url'] = $url;
			$item_list[$key]['add_time'] = date('Y-m-d',$val['add_time']);				
			if(C('ftx_img_put')==1){
			if(!$val['mpic']){	
			$item_list[$key]['pic'] = '/?m=images&id='.$vid.'.'.C('ftx_img_type');	
			}else{
			$item_list[$key]['pic'] = $val['mpic'];		
			}			
			}
			if(C('ftx_img_put')==2){
			$item_list[$key]['pic'] = $val['mpic'];	
			}
        }
		$count = D('article')->where($where)->count();
		$this->assign('item_list',$item_list);
		$pager = $this->_pager($count, $page_size);
        $page = $pager->share_show();
		$page = str_replace('/index/cate/ename/','/',$page);
		$page = str_replace('/p/','_',$page);
		$this->assign('page',$page);
	    $this->_config_seo(C('ftx_seo_config.cate'), array(           
            'name' => $cinfo['name'],
			'seo_title' => $cinfo['seo_title'],
			'seo_keys' => $cinfo['seo_keys'],
			'seo_desc' => $cinfo['seo_desc'],
        ));
		$this->display();	
	} 
	public function wen(){	
	    $cat = I('cat');		
	    $mid = I('id');
		if(C('ftx_a_link')=='id'){
		$item = D('article')->where(array('id'=>$mid,'status'=>1,'fabu'=>1))->find();	
		}else{
		$item = D('article')->where(array('mid'=>$mid,'status'=>1,'fabu'=>1))->find();	
		}		
		!$item && $this->_404();		
		$id = $item['id'];
		$kitem = D('klist')->where(array('article_id'=>$id))->find();
		$bdtitle = json_decode($kitem['bdtitle'],true);
		$item['bdtitle'] = $bdtitle;
		$cid = $item['cate_id'];
		$item['info'] = D('article_'.$cid)->where(array('vid'=>$id))->getField('info');
		$cinfo = D('article_cate')->where(array('id'=>$item['cate_id']))->find();
		!$cinfo && $this->_404();	
		if($cat){
		if($cinfo['ename'] != $cat){
		$this->_404();	
		}
		}
		D('article')->hits($id);		
		$this->assign('cinfo', $cinfo);
		$this->assign('cid', $cid);
		$item['time'] = date('Y-m-d H:i:s',$item['add_time']);				
		$item['tags'] = D('article')->get_tag_by_title($item['title']);
		$tags = implode(',',$item['tags']);		
		//$pattern = '/<img[^>]+src=[\'"]([^\'"]+)[\'"][^>]*>/i';
		$pattern = '/<img[^>]+>/i';
		if(!preg_match($pattern, $item['info'])){
		if($cinfo['peitu']){
		if(C('ftx_img_put')==1){
		$newimg = '/?m=images&id='.$mid.'.'.C('ftx_img_type');
		$position = strpos($item['info'], "<p>", strpos($item['info'], "<p>") + 1); 
        $imageHTML = '<center><img src="'.$newimg.'" alt="'.$item['title'].'"></center>';
		$item['info'] = substr_replace($item['info'], $imageHTML, $position, 0);			
		}
		if(C('ftx_img_put')==2){
		if($cinfo['ename']){	
		$cpath = $cinfo['ename'];
		}else{
		$cpath = 'pic';	
		}
		$path = "data/upload/article/".$cpath."/";	
		$newname = md5($item['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,$item['title']);								
		}							
		if(is_file($path.$newname.'.'.C('ftx_img_type'))){		 
		$position = strpos($item['info'], "<p>", strpos($item['info'], "<p>") + 1); 
        $imageHTML = '<center><img src="'.$newimg.'" alt="2'.$item['title'].'"></center>';
		$item['info'] = substr_replace($item['info'], $imageHTML, $position, 0);		
		D('article_'.$cid)->where(array('vid'=>$id))->save(array('info'=>$item['info']));
		if(!$item['mpic']){		
		D('article')->where(array('id'=>$id))->save(array('mpic'=>$newimg));			
		}
		}			
		}
		}		
		}		
		
		$item['title'] = $this->re_pl($item['title']);
		$item['info'] = $this->re_pl($item['info']);
		$item['author'] = $this->re_pl($item['author']);
		$this->assign('item', $item);		
		$pev['id'] = array('lt',$id);
        $pev['status'] = 1;			
		$pev['fabu'] = 1;		
		$pevs = D('article')->where($pev)->order('id desc')->find();
		
		$nex['id'] = array('gt',$id);	
		$nex['status'] = 1;		
		$nex['fabu'] = 1;
		$nexs = D('article')->where($nex)->order('id asc')->find();
		
		if(C('ftx_a_link')=='md5'){
		$vid = $item['mid'];
		$pevsid = $pevs['mid'];
		$nexsid = $nexs['mid'];
		}else{
		$vid = $item['id'];	
		$pevsid = $pevs['id'];
		$nexsid = $nexs['id'];
		}
		if(C('ftx_url_set')==1){
		$upt = $cinfo['ename'];	
		$pevsupt = $this->getename($pevs['cate_id']);
		$nexsupt = $this->getename($nexs['cate_id']);
		}else{
		if(C('ftx_c_name')){
		$upt = $pevsupt = $nexsupt = C('ftx_c_name');	
		}else{
		$upt = $pevsupt = $nexsupt = 'wen';		
		}			
		}
		$pevs['url'] = C('ftx_site_url').$pevsupt.'/'.$pevsid.C('URL_HTML_SUFFIX');
		$nexs['url'] = C('ftx_site_url').$nexsupt.'/'.$nexsid.C('URL_HTML_SUFFIX');
		$this->assign('pevs', $pevs);
		$this->assign('nexs', $nexs);
		$same = $this->getsame($cid);
		$this->assign('same', $same);
		$desc = gettt(htmlspecialchars_decode($item['info']),130);		
		$url = C('ftx_site_url').$upt.'/'.$vid.C('URL_HTML_SUFFIX');	
		$this->assign('url', $url);
        $this->_config_seo(C('ftx_seo_config.wen'), array(           
            'title' => $item['title'],
			'tags' => $tags,
            'description' => $desc 
        ));		
		$this->display();	
	} 
	
	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 getename($cid){
		$ename = $this->_catelist[$cid][1];
		return $ename;
	}
	public function gethot(){
		$where['status'] = 1;		
		$where['fabu'] = 1;	
		if(C('ftx_img_put')==2){
		$where['mpic'] = array('exp','is not null');
		}
		$item_list = D('article')->where($where)->order('hits desc')->limit(0,8)->select();		
		foreach($item_list as $key=>$val){    
            $item_list[$key] = $val;					
			$item_list[$key]['add_time'] = date('Y-m-d',$val['add_time']);	
			if(C('ftx_a_link')=='md5'){
			$vid = $val['mid'];	
			}else{
			$vid = $val['id'];	
			}
			if(C('ftx_img_put')==1){
			if(!$val['mpic']){	
			$item_list[$key]['pic'] = '/?m=images&id='.$vid.'.'.C('ftx_img_type');	
			}else{
			$item_list[$key]['pic'] = $val['mpic'];		
			}			
			}
			if(C('ftx_img_put')==2){
			$item_list[$key]['pic'] = $val['mpic'];	
			}			
			if(C('ftx_url_set')==1){
			$upt = $this->_catelist[$val['cate_id']][1];	
			}else{
			if(C('ftx_c_name')){
			$upt = C('ftx_c_name');	
			}else{
			$upt = 'wen';		
			}			
			}
			$url = '/'.$upt.'/'.$vid.C('URL_HTML_SUFFIX');
			$item_list[$key]['url'] = $url;
        }
		return $item_list;
	}
	public function getnew(){
		$where['status'] = 1;
		$where['fabu'] = 1;	
		if(C('ftx_img_put')==2){
		$where['mpic'] = array('exp','is not null');
		}
		$item_list = D('article')->where($where)->order('id desc')->limit(0,8)->select();		
		foreach($item_list as $key=>$val){    
            $item_list[$key] = $val;				
			$item_list[$key]['add_time'] = date('Y-m-d',$val['add_time']);						
			if(C('ftx_a_link')=='md5'){
			$vid = $val['mid'];	
			}else{
			$vid = $val['id'];	
			}
			if(C('ftx_img_put')==1){
			if(!$val['mpic']){	
			$item_list[$key]['pic'] = '/?m=images&id='.$vid.'.'.C('ftx_img_type');	
			}else{
			$item_list[$key]['pic'] = $val['mpic'];		
			}			
			}
			if(C('ftx_img_put')==2){
			$item_list[$key]['pic'] = $val['mpic'];	
			}
			if(C('ftx_url_set')==1){
			$upt = $this->_catelist[$val['cate_id']][1];	
			}else{
			if(C('ftx_c_name')){
			$upt = C('ftx_c_name');	
			}else{
			$upt = 'wen';		
			}			
			}
			$url = '/'.$upt.'/'.$vid.C('URL_HTML_SUFFIX');
			$item_list[$key]['url'] = $url;
        }
		return $item_list;
	}
	public function getsame($cid){
		$where['cate_id'] = $cid;
		$where['status'] = 1;
		$where['fabu'] = 1;		
		if(C('ftx_img_put')==2){
		$where['mpic'] = array('exp','is not null');
		}
		$item_list = D('article')->where($where)->order('id desc')->limit(0,8)->select();		
		foreach($item_list as $key=>$val){    
            $item_list[$key] = $val;					
			$item_list[$key]['add_time'] = date('Y-m-d',$val['add_time']);				
			if(C('ftx_a_link')=='md5'){
			$vid = $val['mid'];	
			}else{
			$vid = $val['id'];	
			}
			if(C('ftx_img_put')==1){
			if(!$val['mpic']){	
			$item_list[$key]['pic'] = '/?m=images&id='.$vid.'.'.C('ftx_img_type');	
			}else{
			$item_list[$key]['pic'] = $val['mpic'];		
			}			
			}
			if(C('ftx_img_put')==2){
			$item_list[$key]['pic'] = $val['mpic'];	
			}
			if(C('ftx_url_set')==1){
			$upt = $this->_catelist[$val['cate_id']][1];	
			}else{
			if(C('ftx_c_name')){
			$upt = C('ftx_c_name');	
			}else{
			$upt = 'wen';		
			}			
			}
			$url = '/'.$upt.'/'.$vid.C('URL_HTML_SUFFIX');
			$item_list[$key]['url'] = $url;
        }
		return $item_list;
	}
	public function make_dir($path)
    {
        if (!file_exists($path)) {
            $mk = @mkdir($path, 0777, true);
            @chmod($path, 0777, true);
        }
        return true;
    }
	
}
?>
