<?php 
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
@header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
@header("Cache-Control: no-cache, must-revalidate"); 
@header("Pragma: no-cache");
if(@!eregi($_SERVER['SERVER_NAME'],$_SERVER['HTTP_REFERER'])){exit('QQ:848769359');}
class Discuss extends CI_Controller {

	function __construct(){
		parent::__construct();
        	$this->load->helper('url');
                $this->load->library('session'); //加载session
		$this->load->model('CsdjDB');
		$this->load->model('CsdjSkins');
                $this->load->model('CsdjUser');
                $this->db->cache_off();
	}

        //新增
	public function add()
	{
            $sid   = $this->input->post('sid', TRUE);   //SID
            $did   = intval($this->input->post('did', TRUE));   //DID
            $neir  = $this->filter($this->input->post('neir', TRUE));   //内容
            $neir  = $this->CsdjSkins->str_checkhtml($neir); //屏蔽所有HTML代码

		//php获取ip的算法
		$CS_IP = $this->CsdjSkins->GetIP(); 

                if($did==0 || empty($sid)){
                        exit('Err_000');//参数错误!
                }
                if($this->CsdjUser->User_Login(1)){
                      $CS_User=$this->session->userdata('cs_name');
                }else{
			exit("Err_100");//没有登入
                }
                if(empty($neir)){
			exit("Err_101");//内容为空
                }	
		if(strlen($neir)>500){
			exit("Err_102");//内容过长
		}
		if($this->CsdjSkins->DateDiff($this->session->userdata('addpltime'),date("Y-m-d H:i:s"))<30){
			exit("Err_103"); //时间太短
		}	

            	   if($sid=='dance'){
                 	   $fid=1;
            	   }elseif($sid=='news'){
                 	   $fid=2;
            	   }elseif($sid=='vod'){
                 	   $fid=3;
            	   }else{
                 	   $fid=4;
            	   }

		   $pl['CS_User']    = $CS_User;
		   $pl['CS_Content'] = $neir;	
		   $pl['CS_DID']     = $did;
		   $pl['CS_Ip']      = $CS_IP;
		   $pl['CS_SID']     = $fid;
		   $pl['CS_AddTime'] = date('Y-m-d H:i:s');

		  $pladd=$this->CsdjDB->get_insert ('pl',$pl);

                  if($pladd){
		        $pldata = array(
                                        'addpltime'  => date("Y-m-d H:i:s"),
                                   );
                        $this->session->set_userdata($pldata);
			exit("Err_104");
		  }else{
			exit("Err_105");
                  }
        }


        //列表
	public function lists()
	{
            $sid   = $this->uri->segment(3);   //ID
            $did   = intval($this->uri->segment(4));   //ID
            $page = intval($this->uri->segment(5));   //分页
            if($page==0) $page=1;

            if($did==0){
                  show_404();
            }
            if($sid=='dance'){
                 $fid=1;
            }elseif($sid=='news'){
                 $fid=2;
            }elseif($sid=='vod'){
                 $fid=3;
            }else{
                 $fid=4;
            }

            $this->load->get_templates();
            $Mark_Text=$this->load->view('cs-pl.html','',true);

            if($this->CsdjUser->User_Login(1)){
		$Mark_Text=str_replace("[pl:ulogin]","ok",$Mark_Text);
            }else{
		$Mark_Text=str_replace("[pl:ulogin]","no",$Mark_Text);
            }
            $plfaces="";
    	    for($i=1;$i<=56;$i++){
                $plfaces.="<img src=\"".Web_Path."csdj/images/faces/e".$i.".gif\" onclick=\"insertsmilie('[em:".$i."]')\" />";
            }
	    $Mark_Text=str_replace("[pl:faces]",$plfaces,$Mark_Text);
	    //预先除了分页
	    $pagenum=$this->CsdjSkins->GetPageNum($Mark_Text);
	    preg_match_all('/{cscms:pl(.*?pagesize=([\S]+).*?)}([\s\S]+?){\/cscms:pl}/',$Mark_Text,$page_arr);//判断是否有分页标识
	    if(!empty($page_arr) && !empty($page_arr[2])){
                        $data_content="";
			$sqlstr=$this->CsdjSkins->Mark_Sql('pl',$page_arr[1][0],$did,$fid);
			$Arr=$this->SpanPage($sqlstr,$page_arr[2][0],$pagenum,$page);//sql,每页显示条数
			$result=$this->db->query($Arr[2]);
			$recount=$result->num_rows();
			if($recount==0){
				$data_content="<div align=center><strong><font style=font-size:12px;>暂无评论，快来抢沙发吧！</font></strong></div>";
			}else{
				if($result){
					$sorti=1;
					foreach ($result->result() as $row2) {
						$datatmp=$this->CsdjSkins->DataPl($page_arr[0][0],$page_arr[3][0],$row2,$sorti);
						$sorti=$sorti+1;
						$data_content.=$datatmp;
					}
				}
			}// end if recount
			$Mark_Text=$this->CsdjSkins->Page_Mark($Mark_Text,$Arr);	
			$Mark_Text=str_replace($page_arr[0][0],$data_content,$Mark_Text);
			unset($Arr);
	    }
	    unset($page_arr);
            $this->CsdjSkins->skins($Mark_Text,'');

        }

        function SpanPage($mysql,$pagesize,$pagenum=10,$pages=1){

	        $pagesok=$pagesize;//每页显示记录数
                if (!isset($pages)||$pages==""||!is_numeric($pages)||$pages<=0){
                     $pages=1;
	        }
		$result2=$this->CsdjDB->db->query($mysql);
		$nums=$result2->num_rows();
	        if($nums==0){
		        $pagejs=1;
	        }else{
		        $pagejs=ceil($nums/$pagesok);//总页数
	        }	
    
                if($pages>$pagejs){
                     $pages=$pagejs;
                }
  
                $sql=$mysql." LIMIT ".$pagesok*($pages-1).",".$pagesok;
	        $result=$this->db->query($sql);
	        $str="";

		$first="javascript:getpllist('1');";
       	 	if($pages==1){
	     		$pageup="javascript:getpllist('1');";
        	}else{
	     		$pageup="javascript:getpllist('".($pages-1)."');";
        	}
       	 	if($pagejs>$pages){
	     		$pagenext="javascript:getpllist('".($pages+1)."');";
        	}else{
	     		$pagenext="javascript:getpllist('$pagejs');";
        	}
		$last="javascript:getpllist('$pagejs');";
		$pagelist="<select  onchange=javascript:getpllist(this.options[this.selectedIndex].value);>\r\n<option value='0'>跳转</option>\r\n";
		for($k=1;$k<=$pagejs;$k++){
			$pagelist.="<option value='".$k."'>第".$k."页</option>\r\n";
		}
		$pagelist.="</select>";	
		if($pagejs<=$pagenum){
  			for($i=1;$i<=$pagejs;$i++){
                              if($i==$pages){
   				   $str.="<a href='javascript:;' onclick=\"getpllist('".$i."');\" class='on'>".$i."</a>";
                              }else{
   				   $str.="<a href='javascript:;' onclick=\"getpllist('".$i."');\">".$i."</a>";
                              }
 	 		}
		}else{
 			if($pages>=$pagenum){
 				for($i=$pages-intval($pagenum/2);$i<=$pages+(intval($pagenum/2));$i++){
   					if($i<=$pagejs){
                                    	      	if($i==$pages){
   				          	      	$str.="<a href='javascript:;' onclick=\"getpllist('".$i."');\" class='on'>".$i."</a>";
                                    	      	}else{
   				          	      	$str.="<a href='javascript:;' onclick=\"getpllist('".$i."');\">".$i."</a>";
                                    	      	}
    			        	}
  				}
  				if($i<=$pagejs){ 
	    		        	$str.="<a href='javascript:;' onclick=\"getpllist('".$pagejs."');\">".$pagejs."</a>";
   				}
   			}else{
  				for($i=1;$i<=$pagenum;$i++){
                                    	      	if($i==$pages){
   				          	      	$str.="<a href='javascript:;' onclick=\"getpllist('".$i."');\" class='on'>".$i."</a>";
                                    	      	}else{
   				          	      	$str.="<a href='javascript:;' onclick=\"getpllist('".$i."','".$did."','$sid');\">".$i."</a>";
                                    	      	}
 		        	} 

 		        	if($i<=$pagejs){ 
	  		     		$str.="<a href='javascript:;' onclick=\"getpllist('".$pagejs."');\">".$pagejs."</a>";
    				}
 		 	}
		}
	 	$arr=array($str,$result,$sql,$nums,$pagelist,$pages,$pagejs,$first,$pageup,$pagenext,$last,$pagesok);
	 	return $arr;
        }

        function filter($str){
                $str=iconv('UTF-8', 'GB2312', $str);
	        $KeyArr=explode(',',Pl_Str);
	        for($i=0;$i<count($KeyArr);$i++){
		        $str=str_replace($KeyArr[$i],"**",$str);
	        }
	        return $str;
        }
}

