<?php
/*
'**************************************************************************************************
' 软件名称: 搜一次 Content Management System
' 版本编号: Version 1.X
' 官方网站: http://www.syccms.com
' 官方论坛：http://bbs.syccms.com
' 版权所有: 搜一次运营团队
' 法律顾问: 杭州市元茂律师事务所 严飞律师
' 郑重声明:
'    1、任何个人或组织不得在未经授权的情况下删除、修改、拷贝本软件及其他副本上一切关于版权的信息；
'    2、搜一次网络事业部保留此软件的法律追究权利
'**************************************************************************************************
*/
header("Content-type: text/html;charset=gb2312");
include "../include/conn.php";
include "../include/function.php";
include "../include/userset.php";
$action=SafeRequest("action","post");
switch($action){
	case 'list':
		mlist();
		break;
	case 'add':
		add();
		break;
	default:
		mlist();
	}	

function add(){
		if(S_MStar=="0"){
			die("close");
		}
		global $db;
		$S_Content=SafeRequest("content","post","filter");
		$S_Name=SafeRequest("names","post","filter");
		$S_Code=SafeRequest("codes","post","filter");
		//$S_IP=$_SERVER['SERVER_ADDR'];
		//php获取ip的算法
		$S_IP = isset($_SERVER["HTTP_X_FORWARDED_FOR"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : $_SERVER["REMOTE_ADDR"];
		$S_VID=SafeRequest("id","post","filter");
		$S_IP=str_encode($S_IP);
		if(!IsNul($S_VID)){$S_VID="0";}
		if(function_exists("mb_convert_encoding")){
			$S_Name=filter(mb_convert_encoding($S_Name,"GBK","UTF-8"));
			$S_Content=filter(mb_convert_encoding($S_Content,"GBK","UTF-8"));
		}else{
			$S_Name = filter(iconv( "UTF-8", "GBK//IGNORE" , $S_Name));
			$S_Content = filter(iconv( "UTF-8", "GBK//IGNORE" , $S_Content));			
		}
		$S_AddTime=date('Y-m-d H:i:s');
		
		$S_VID = intval($S_VID);
		$S_Name = strip_tags($S_Name);
		$S_Content = strip_tags($S_Content);
	
		if(defined("S_Mcode") && S_Mcode=='1'){
			if(!isset($_COOKIE["captcha"]) || $_COOKIE["captcha"]!=$S_Code)  die("codeerr");//验证码错误
		}
		if(strlen($S_Name)>S_MNameLen){
			die("namelenerr");//用户名过长
		}	
		if(strlen($S_Content)>S_Message){
			die("contentlenerr");//内容过长
		}
		if(isset($_COOKIE['gbooktime']) && DateDiff($_COOKIE['gbooktime'],date("Y-m-d H:i:s"))<16){
			die("timeerror");
		}	
		$sql="Insert s_gbook (S_Name,S_Content,S_IP,S_VID,S_AddTime) values ('".$S_Name."','".$S_Content."','".$S_IP."','".$S_VID."','".$S_AddTime."')";
		echo $sql;
		if($db->query($sql)){
			$domain=$_SERVER['HTTP_HOST']=='localhost' ? false : $_SERVER['HTTP_HOST'];
			//设置供验证码用的cookie
			setcookie("gbooktime",date("Y-m-d H:i:s"),time()+3600,'/',$domain,false,true); 
			echo "ok";
		}
	}
	
function mlist(){
	global $db;
	if(S_MStar=="0"){
		die("close");//留言关闭
	}
	$sql="select * from s_gbook order by S_ID desc";
		$Arr=getpagerow2($sql,S_MCount);//sql,每页显示条数
		$result=$db->query($Arr[2]);
		$recount=$db->num_rows($result);
		//$result=$db->query($sql);
		if($recount=="0"){
			echo "<div style='width:100%; border:1px solid #B5D6E6; margin-top:10px; height:30px; font-size:12px; line-height:30px;'>&nbsp;Sorry!暂无留言,请留言吧！！</div>";
			}
		else{
		if($result){
			while ($row = $db ->fetch_array($result)){
				if($row['S_VID']!=0){
					$sqlclass="select S_ClassID from s_video where S_ID=".$row['S_VID']."";
					$vclassid=$db->getrow($sqlclass);
					
					if($vclassid){
						$videoclassid=$vclassid['S_ClassID'];
						}
					else{
						$videoclassid="";
						}	

					$videolink="<br/>&nbsp;&nbsp;地址:<a href=".LinkUrl("s_video",$videoclassid,1,$row['S_VID'])." target=_blank>".S_SiteDomain.LinkUrl("s_video",$videoclassid,1,$row['S_VID'])."</a>";
					}
				else{
					$videolink="";
					}	
				if($row['S_Reply']=="1" && trim($row['S_ReContent'])!=""){
?>
<div style="width:99.8%; border:1px solid #B5D6E6; margin:0 auto;margin-top:10px;" ><div style="width:100%; height:30px; line-height:30px; font-size:12px; background:#EEF7FE">
	<div style="width:50%; float:left;  color:#185691;">&nbsp;<strong><?php echo $row['S_Name']?></strong>&nbsp;&nbsp;IP：<?php echo $row['S_IP']?></div>
    <div style="width:48%; float:left;text-align:right; font-size:12px;">发表于:<font color=""red""><?php echo $row['S_AddTime']?></font></div>
    </div>
    <div style="width:95%; margin:0 auto; height:50px;font-size:12px; line-height:25px;">&nbsp;&nbsp;<?php echo str_decode($row['S_Content']).$videolink?></div>
    <div style="width:95%; margin:0 auto; height:20px;font-size:12px; line-height:20px; color:red;">&nbsp;&nbsp;站长回复：时间(<?php echo $row['S_ReTime']?>)</div>
    <div style="width:95%; margin:0 auto; color:red; height:30px;font-size:12px; line-height:20px">&nbsp;&nbsp;<?php echo $row['S_ReContent']?></div></div>
<?php					
					}
				else{					
?>				
		<div style="width:99.8%; border:1px solid #B5D6E6;margin-top:10px;">
        	<div style="width:100%; height:30px; line-height:30px; font-size:12px; background:#EEF7FE">
            	<div style="width:50%; float:left;  color:#185691;">&nbsp;<strong><?php echo $row['S_Name']?></strong>&nbsp;IP:<?php echo $row['S_IP']?></div>
                <div style="width:48%; float:left;text-align:right; font-size:12px;">发表于：<font color=red><?php echo $row['S_AddTime']?></font></div>
             </div>
            <div style="width:95%; margin:0 auto; height:80px;font-size:12px; line-height:25px;">&nbsp;&nbsp;<?php echo str_decode($row['S_Content']).$videolink?></div>
      </div>		

<?php
				}
			}
?>
            <div style=" font-size:12px;"><?php echo $Arr[0];?>&nbsp;</div>
<?php			
		}
	}		
	
}
	
function getpagerow2($mysql,$pagesize){
	global $db;
	$url=$_SERVER["QUERY_STRING"];
	if(stristr($url,'&pages')){
		$url=preg_replace('/&pages=([\S]+?)$/','',$url);
	}
	if(stristr($url,'pages')){
		$url=preg_replace('/pages=([\S]+?)$/','',$url);
	}
	if(IsNul($url)){$url.="&";}

	$pages=SafeRequest("pages","post");
	$pagesok=$pagesize;//每页显示记录数
    if (!isset($pages)||$pages==""||!is_numeric($pages)||$pages<=0){
    $pages=1;
	}
  	$sqlstr=$mysql;
  	$res=$db -> query($sqlstr);
 	$nums= $db -> num_rows($res);
	if($nums==0){
		$pagejs=1;
		}
	else{
		$pagejs=ceil($nums/$pagesok);//总页数
		}	
 	$str="总<font color=red>".$nums."</font>条记录&nbsp;";
    
  
    if($pages>$pagejs){
     $pages=$pagejs;
    }
  
    $sql=$sqlstr." LIMIT ".$pagesok*($pages-1).",".$pagesok;
	$str.="当前<font color=red>".$pages."/</font>".$pagejs."页&nbsp;";                                                              ;
	$result = $db -> query($sql);
 
 
 	//$str.= "页数".$pagejs."&nbsp;";
 


	$str.= "<a href='#' onclick='showlist(1)'>第一页</a>";
	if($pagejs<=10){
  		for($i=1;$i<=$pagejs;$i++){
   			$str.="<a href='#' onclick='showlist(".$i.")'>".$i."</a>&nbsp;";
 	 	}
	}
	else{
 		if($pages>=12){
 			for($i=$pages-5;$i<=$pages+6;$i++){
   				if($i<=$pagejs){
   					$str.="<a href='#' onclick='showlist(".$i.")'>".$i."</a>&nbsp;";
    			}
  			}
  			if($i<=$pagejs){ 
    			$str.="....";
	    		$str.="<a href='#' onclick='showlist(".$pagejs.")'>".$pagejs."</a>&nbsp;";
   			}
   		}
  		else{
  			for($i=1;$i<=12;$i++){
    			$str.="<a href='#' onclick='showlist(".$i.")'>".$i."</a>&nbsp;";
 		} 
 				if($i<=$pagejs){ 
      				$str.="....";
	  				$str.="<a href='#' onclick='showlist(".$pagejs.")'>".$pagejs."</a>&nbsp;";
    			}
 		 }
	}

	$str.="<a href='#' onclick='showlist(".($pages+1).")'>下一页</a>&nbsp;";
	$str.="<a  href='#' onclick='showlist(".$pagejs.")'>最后页</a>&nbsp;";
	$str.="转<input type=text size=4 name='gopage' style='margin-top:4px;' value='".$pages."'/>&nbsp;<a style='cursor:pointer;' onclick='showlist(gopage.value)'>GO>></a>&nbsp;";
	 while ($row = $db -> fetch_array($result) ){
	 }
	 $arr=array($str,$result,$sql);
	 return $arr;
}

function filter($str){
	$KeyArr=explode(',',Syc_GSQL);
	for($i=0;$i<count($KeyArr);$i++){
		$str=ReplaceStr($str,$KeyArr[$i],"***");
		}
		return $str;
	}
	
?>
