<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Gbook extends CI_Controller {

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

	public function index()
	{
            $data='';
                if($this->session->userdata('cs_name')){
		      $mname= $this->CsdjSkins->getuser('nichen','',$this->session->userdata('cs_name'));
	        }else{
		      $mname= '游客';
                }
                $Gbook="<script language=\"javascript\" src=\"".Web_Path."csdj/js/gbookajax.js\"></script><script>var cs_path='".Web_Path."';getbooklist(1);</script>
                <div id=\"cscms_book\" style=\"width:100%;\"><img src=\"".Web_Path."csdj/images/load.gif\"><font style=\"font-size:12px;\">&nbsp;&nbsp;加载留言内容,请稍等......</font></div>
                <div id=\"cscms_book_add\" style=\"border:1px solid #ddd; height:200px; width:99.8%; margin-top:20px; font-size:12px;\">
	                <div style=\"width:98%; height:30px; line-height:30px; margin-top:5px;\">&nbsp;你的网名：<input type=\"text\" name=\"names\" id=\"names\" size=\"15\" value=\"".$mname."\">&nbsp;<font color=\"red\">＊</font></div>
	                <div style=\"margin-top:5px; height:110px; width:100%; \">
		                <div style=\"float:left; height:110px; width:70%; margin-left:2px;  \"><textarea name=\"content\" id=\"content\" style=\"width:95%;border:1px solid #ddd; float:left;\" rows=\"7\"  ></textarea>&nbsp;<font color=\"red\">＊</font></div>
                                <div style=\"float:left; height:80px; color:#36C;  font-size:12px;line-height:20px;\">声明：您如果有什么建议或意见<br/>请在这里给管理员留言<br/>您所发的信息不能触犯国家法律<br/>否者您需对自己言行负全部责任！</div>
                        </div>
	                <div style=\"margin-top:5px;\">&nbsp;&nbsp;<input onclick=\"bookadd();\" type=\"button\" value=\"我要留言\" style=\" background:url(".Web_Path."csdj/images/btn.gif); border:0px solid #CCC; color:#39F; height:31px; width:96px; border-left:0px solid red; margin-top:5px; font-weight:bold; \" ></div>
                </div> 
                ";
                $this->load->get_templates();
		$template=$this->load->view('cs-gbook.html',$data,true);
	        $Mark_Text=str_replace("{cscms:gbook}",$Gbook,$template);
                $this->CsdjSkins->skins($Mark_Text,$data);
	}


        //新增
	public function add()
	{
            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');}
            @header("Content-type: text/html;charset=gb2312");

            $name     = $this->filter($this->input->post('name', TRUE));   //会员名
            $content  = $this->filter($this->input->post('content', TRUE));   //内容
            $content  = $this->CsdjSkins->str_checkhtml($content); //屏蔽所有HTML代码
		//php获取ip的算法
		$CS_IP = $_SERVER["REMOTE_ADDR"]; 

                if(User_BookFun==2){
			exit("Err_000");//留言关闭
                }
                if(empty($name)){
			exit("Err_100");//用户名为空
                }
		if(strlen($name)>20){
			exit("Err_101");//用户名过长
		}
                if(empty($content)){
			exit("Err_102");//内容为空
                }	
		if(strlen($content)>200){
			exit("Err_103");//内容过长
		}
		if($this->CsdjSkins->DateDiff($this->session->userdata('gbooktime'),date("Y-m-d H:i:s"))<30){
			exit("Err_104"); //时间太短
		}	


		   $book['CS_Usera']   = "cscms";
		   $book['CS_Userb']   = $name;	
		   $book['CS_Neir']    = $content;
		   $book['CS_Ip']      = $CS_IP;
		   $book['CS_CID']     = 1;
		   $book['CS_AddTime'] = date('Y-m-d H:i:s');

		  $bookadd=$this->CsdjDB->get_insert ('gbook',$book);

                  if($bookadd){
		        $bookdata = array(
                                        'gbooktime'  => date("Y-m-d H:i:s"),
                                   );
                        $this->session->set_userdata($bookdata);
			exit("Err_105");
		  }else{
			exit("Err_106");
                  }
        }


        //列表
	public function lists()
	{
            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');}
            @header("Content-type: text/html;charset=gb2312");

            $page = intval($this->uri->segment(3));   //分页
            if($page==0) $page=1;

	    if(User_BookFun==2){
		exit("站长已经关闭留言！");//留言关闭
	    }

	        $sql="select * from ".CS_SqlPrefix."gbook  where cs_usera='cscms' and cs_cid=1 order by CS_ID desc";
		$Arr=$this->getpagerow2($sql,User_BookNum,$page);//sql,每页显示条数
		$result=$this->CsdjDB->db->query($Arr[2]);
		$recount=$result->num_rows();

		if($recount=="0"){
			echo "<div style='width:100%; border:1px solid #ddd; margin-top:10px; height:30px; font-size:12px; line-height:30px;'>&nbsp;Sorry!暂无留言,请留言吧！！</div>";
		}else{
		    if($result){
			 foreach ($result->result() as $row){	
                              echo '
 			      <div style="width:99.8%;height:auto; border:1px solid #ddd; margin:0 auto;margin-top:10px;color:#333;" >
                                    <div style="width:100%; height:30px; line-height:30px; font-size:12px; background:#f6f6f6">
 			                  <div style="width:50%; float:left;  color:#185691;">&nbsp;<strong>'.$row->CS_Userb.'</strong>&nbsp;&nbsp;IP：'.$row->CS_Ip.'</div>
 			                  <div style="width:48%; float:left;text-align:right; font-size:12px;">发表于:<font color=""red"">'.$row->CS_AddTime.'</font></div>
 			            </div>
 			            <div style="width:95%; margin:0 auto; height:50px;font-size:12px; line-height:25px;">&nbsp;&nbsp;'.$row->CS_Neir.'</div>';
			     if(!empty($row->CS_Hfneir)){  //回复
                              echo '
 			            <div style="width:95%; margin:0 auto; height:20px;font-size:12px; line-height:20px; color:red;">&nbsp;&nbsp;站长回复：</div>
 			            <div style="width:95%; margin:0 auto; color:red;height:auto;font-size:12px; line-height:20px;">&nbsp;&nbsp;'.$row->CS_Hfneir.'</div>
                              ';
			     }
                              echo '</div>';
			 }
		    }
                    echo '    <div style=" font-size:12px;">'.$Arr[0].'</div>';
		}
        }


        function getpagerow2($mysql,$pagesize,$pages){

	        $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);//总页数
	        }	
 	        $str="总<font color=red>".$nums."</font>条记录&nbsp;";
    
                if($pages>$pagejs){
                     $pages=$pagejs;
                }
  
                $sql=$mysql." LIMIT ".$pagesok*($pages-1).",".$pagesok;
	        $result=$this->CsdjDB->db->query($sql);

	        $str.="当前<font color=red>".$pages."/</font>".$pagejs."页&nbsp;";                                                              ;
	        $str.= "<a href='javascript:;' onclick=\"getbooklist('1');\">第一页</a>";
	        if($pagejs<=10){
  		        for($i=1;$i<=$pagejs;$i++){
   			        $str.="<a href='javascript:;' onclick=\"getbooklist('".$i."');\">".$i."</a>&nbsp;";
 	 	        }
	        }else{
 		        if($pages>=12){
 			        for($i=$pages-5;$i<=$pages+6;$i++){
   				        if($i<=$pagejs){
   					        $str.="<a href='javascript:;' onclick=\"getbooklist('".$i."');\">".$i."</a>&nbsp;";
    			                }
  			        }
  			        if($i<=$pagejs){ 
    			                $str.="....";
	    		                $str.="<a href='javascript:;' onclick=\"getbooklist('".$pagejs."');\">".$pagejs."</a>&nbsp;";
   			        }
   		        }else{
  			        for($i=1;$i<=12;$i++){
    			                $str.="<a href='javascript:;' onclick=\"getbooklist('".$i."');\">".$i."</a>&nbsp;";
 		                } 
 			        if($i<=$pagejs){ 
      				        $str.="....";
	  			        $str.="<a href='javascript:;' onclick=\"getbooklist('".$pagejs."');\">".$pagejs."</a>&nbsp;";
    			        }
 		        }
	        }

	        $str.="<a href='javascript:;' onclick=\"getbooklist('".($pages+1)."');\">下一页</a>&nbsp;";
	        $str.="<a  href='javascript:;' onclick=\"getbooklist('".$pagejs."');\">最后页</a>&nbsp;";
	        $str.="转<input type=text size=4 name='gopage' style='margin-top:4px;' value='".$pages."'/>&nbsp;<a style='cursor:pointer;' onclick='getbooklist(gopage.value)'>GO>></a>&nbsp;";
                foreach ($result as $result) { }
	        $arr=array($str,$result,$sql);
	        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;
        }
}


