<?php 
///////////////////////////////////
/////BBS  管理系统          ///////
///////////////////////////////////
require "../Run.php";
require "../include/getmkurl.fun.php";
$action = $_GET['action'];
$type   = $_POST['type'];
if($login != 1){
    header("location: ../login.php?cid=".$_GET['cid']);
	}
	
	$sql="SELECT bbsid,siteid,userid,title,content,pic,name,v,time,gro,zip,top,waptop,good FROM bbs WHERE bbsid='{$_GET['id']}' AND siteid='{$wapid}'";
    $view = mysql_query($sql);
    if($view && mysql_num_rows($view)>0){
    list($bbsid,$siteid,$userid,$title,$content,$pic,$name,$v,$time,$gro,$zip,$top,$waptop,$good) = mysql_fetch_row($view);
    }else{
    $tip="抱歉 内容获取失败"; 
    }
	mysql_free_result($view);
	if($action == 'mod'){
	//加载帖子修改页面
	if($admin==1||$admin==2||$admin==3||$userid == $id||is_bbsadmin(MYID)){
	}else{
	die('权限不足');
	}
	   if($type=='mod'){
	if($_POST['fn'] == 0||$_POST['fn'] == NULL||$_POST['fn'] == ''){
	 $upload_num = 1;
	 }else{
	 $upload_num = $_POST['fn'];
	 }
	for($i = 1;$i <= $upload_num;$i++){
	    $D1 = date('Y');
	    $D2 = date('m');
	    $D3 = date('d');
	    if (!is_file($_SERVER['DOCUMENT_ROOT'].'/bbs/uploads/'.WAPID)){
			mkdir ($_SERVER['DOCUMENT_ROOT'].'/bbs/uploads/'.WAPID); 
		}
		if (!is_file($_SERVER['DOCUMENT_ROOT'].'/bbs/uploads/'.WAPID.'/'.$D1)){
			mkdir ($_SERVER['DOCUMENT_ROOT'].'/bbs/uploads/'.WAPID.'/'.$D1); 
		}
		if (!is_file($_SERVER['DOCUMENT_ROOT'].'/bbs/uploads/'.WAPID.'/'.$D1.'/'.$D2)){
			mkdir ($_SERVER['DOCUMENT_ROOT'].'/bbs/uploads/'.WAPID.'/'.$D1.'/'.$D2); 
		}
		if (!is_file($_SERVER['DOCUMENT_ROOT'].'/bbs/uploads/'.WAPID.'/'.$D1.'/'.$D2.'/'.$D3)){
			mkdir ($_SERVER['DOCUMENT_ROOT'].'/bbs/uploads/'.WAPID.'/'.$D1.'/'.$D2.'/'.$D3); 
		}
	    $D = date('YmdHis');
			$filetype    =  substr(strrchr($_FILES['userfile'.$i]['name'],"."),1);
			$newname     =  $wapid."_".MYNAME."_".$D."_".$i.".".$filetype;
			$uploaddir   =  $_SERVER['DOCUMENT_ROOT'].'/bbs/uploads/'.WAPID.'/'.$D1.'/'.$D2.'/'.$D3.'/';
			$UploadFile  =  $uploaddir.$newname;
			$UploadPath  =  Addslashes(dirname($PATH_TRANSLATED))."/uploads/".WAPID."/".$D1."/".$D2."/".$D3;
			$FileName = $uploadPath.$UploadFile_name;	
	    /*上传文件检测*/
	    $error = $_FILES['userfile'.$i]['error'];
	if($error == 1){ 
	   echo '上传失败：你上传的文件超过了php的系统约定值！';
	}elseif($error == 2){
	   echo '上传失败：上传的文件超过了表单设置的最大值！';
	}elseif($error == 3){
	   echo '上传失败：文件只有部分被上传';
    }elseif($error == 5){
	   echo '上传失败：发生未知错误！';
	}elseif($error == 4){
	     $file_url = NULL;
	}else{
            if(move_uploaded_file($_FILES['userfile'.$i]['tmp_name'],$UploadFile)){
	        $fpath = "uploads/".WAPID."/".$D1."/".$D2."/".$D3."/".$newname;
	        }else{
	        echo "文件上传失败<br>";
	        $fpath = $zip;
	              }		  
		       }
			if($i==1){
			   $fh='';
			  }else{
			   $fh='|';
			  }
            $file_url .= $fh.$fpath;					   
       }		  
   
        $time = time();		  
		$content = preg_replace('/(\r\n)|(\n)/','[br]',$_POST['content']);
	    $sql="UPDATE bbs SET title='{$_POST['title']}',content='{$content}',endtime='{$time}' WHERE bbsid='{$bbsid}'";
        $result = mysql_query($sql);
	    if($result && mysql_affected_rows()>0){
	    $tip = "<div class=\"tip\">修改成功</div>";
        }else{
        $tip =  "<div class=\"tip\">修改失败</div>";
         }
	}
	 
	require 'include/mod.inc.php';
	$html = $tip.$html;
	}elseif($action == 'del'){
	//加载帖子删除页面
	if($admin==1||$admin==2||$admin==3||$admin==4||$userid == $id||is_bbsadmin(MYID)){
	}else{
	die('权限不足');
	}
	$html='';
	if($_GET['type']=='del'){
	 $result = mysql_query("DELETE FROM bbs WHERE bbsid='{$_GET["id"]}'");
	 if($result && mysql_affected_rows()>0){
	 $html= '<div class="tip">成功删除[<a href="list.php?cid='.$gro.'&">返回帖子列表</a>]</div>';
	 }else{
     $html= '<div class="tip">删除失败</div>';
	     }
	}else{
	$html = '您好，您确定删除《'.$title.'》吗？<br><a href="bbs_admin.php?action=del&type=del&id='.$_GET['id'].'&">[确定删除]</a>';
	     }
	}elseif($action == 'top'){
	//加载帖子置顶页面
	if($admin==1||$admin==2||$admin==3||$admin==4||is_bbsadmin(MYID)){
	}else{
	die('权限不足');
	}
	if($top == 1){
		   $ml=0;
		   $t= '取消置顶';
		   }else{
		   $ml=1;
		   $t ='置顶帖子';
		   
		   }
	if($_GET['type']=='top'){
	     if($top==1){
		    $sql="UPDATE bbs SET top='0' WHERE bbsid='{$_GET['id']}'";
            $result = mysql_query($sql);
	        if($result && mysql_affected_rows()>0){
	        $tip = "<div class=\"tip\">取消置顶成功</div>";
            }else{
            $tip =  "<div class=\"tip\">取消置顶失败</div>";
            }
		  }else{
		    $sql="UPDATE bbs SET top='1' WHERE bbsid='{$_GET['id']}'";
            $result = mysql_query($sql);
	        if($result && mysql_affected_rows()>0){
	        $tip = "<div class=\"tip\">置顶成功</div>";
            }else{
            $tip =  "<div class=\"tip\">置顶失败</div>";
            }
		  }
		  $html=$tip;
	}else{
	     
	     $html = '<div class="tip">您确定要'.$t.'《'.$title.'》吗?<br>
		 [<a href="bbs_admin.php?action=top&type=top&id='.$_GET['id'].'&">确定执行</a>]</div>';
		 
		 }
	}elseif($action == 'waptop'){
	//加载帖子全区置顶页面
	if($admin==1||$admin==2||$admin==3||$admin==4||is_bbsadmin(MYID)){
	}else{
	die('权限不足');
	}
	if($waptop == 1){
		   $ml=0;
		   $t= '取消全区置顶';
		   }else{
		   $ml=1;
		   $t ='全区置顶帖子';
		   
		   }
	if($_GET['type']=='top'){
	     if($waptop ==1){
		    $sql="UPDATE bbs SET waptop='0' WHERE bbsid='{$_GET['id']}'";
            $result = mysql_query($sql);
	        if($result && mysql_affected_rows()>0){
	        $tip = "<div class=\"tip\">取消置顶成功</div>";
            }else{
            $tip =  "<div class=\"tip\">取消置顶失败</div>";
            }
		  }else{
		    $sql="UPDATE bbs SET waptop='1' WHERE bbsid='{$_GET['id']}'";
            $result = mysql_query($sql);
	        if($result && mysql_affected_rows()>0){
	        $tip = "<div class=\"tip\">置顶成功</div>";
            }else{
            $tip =  "<div class=\"tip\">置顶失败</div>";
            }
		  }
		  $html=$tip;
	}else{
	     
	     $html = '<div class="tip">您确定要'.$t.'《'.$title.'》吗?<br>
		 [<a href="bbs_admin.php?action=waptop&type=top&id='.$_GET['id'].'&">确定执行</a>]</div>';
		 
		 }
	}elseif($action == 'good'){
	//加载帖子加精页面
	if($admin==1||$admin==2||$admin==3||$admin==4){
	}else{
	die('权限不足');
	}
	if($good == 1){
		   $ml=0;
		   $t= '取消加精';
		   }else{
		   $ml=1;
		   $t ='加精帖子';
		   
		   }
	if($_GET['type']=='good'){
	     if($good==1){
		    $sql="UPDATE bbs SET good='0' WHERE bbsid='{$_GET['id']}'";
            $result = mysql_query($sql);
	        if($result && mysql_affected_rows()>0){
	        $tip = "<div class=\"tip\">取消加精成功</div>";
            }else{
            $tip =  "<div class=\"tip\">取消加精失败</div>";
            }
		  }else{
		    $sql="UPDATE bbs SET good='1' WHERE bbsid='{$_GET['id']}'";
            $result = mysql_query($sql);
	        if($result && mysql_affected_rows()>0){
	        $tip = "<div class=\"tip\">加精成功</div>";
            }else{
            $tip =  "<div class=\"tip\">加精失败</div>";
            }
		  }
		  $html=$tip;
	}else{
	     
	     $html = '<div class="tip">您确定要'.$t.'《'.$title.'》吗?<br>
		 [<a href="bbs_admin.php?action=good&type=good&id='.$_GET['id'].'&">确定执行</a>]</div>';
		 
		 }
	}elseif($action == 'file'){
	$html='5';
	//加载附件管理页面
	}elseif($action == 'm'){
	 if($admin !=1&&$admin !=2&&$admin !=3&& $admin!=4&&!is_bbsadmin(MYID)){
	  die('非法权限！您没有进入此页面的权限，假如你是管理员并且已经登录却看到警告，请联系程序提供商！');
	  }
	
	$sql = "SELECT cid,cn,url,xuhao,q,siteid,gro,top,bottom FROM class WHERE siteid='{$wapid}' AND url=".get_mkid('bbs/list.php')." ORDER BY xuhao ASC";
    $classcon=mysql_query($sql);
	if($classcon && mysql_num_rows($classcon)>0){
	   $i          =       0; 
	while(list($cid,$cn,$url,$xuhao,$q,$siteid,$gro)=mysql_fetch_row($classcon)){
	   if($cid==$_GET['cid']){
	     $mr='selected="selected"';
		 }else{
		 $mr='';
		 }
	   $html.='<option value="'.$cid.'"'.$mr.'>'.$cn.'</option>';
	    }
	}else{
	   $html.='<option>暂无记录</option>';
	}		 
mysql_free_result($classcon);
    if($_POST['to']!=NULL && $_POST['to']!=''){
	     $sql="UPDATE bbs SET gro='{$_POST['to']}' WHERE bbsid='{$_GET["id"]}' AND siteid='{$wapid}'";
         $result=mysql_query($sql);
	     if($result && mysql_affected_rows()>0){
	     $tip= "修改成功";
         }else{
         $tip= "修改失败";
         }
	   
	   }
	$html=$tip.' <form method="post" action="bbs_admin.php?action=m&id='.$_GET['id'].'&cid='.$_GET['cid'].'&"><div class="subtitle">请选择要转移的栏目</div>
	<select name="to">'.$html.'
	</select>
	<input type="submit" value="确认转移">
	</form>';
	
	
	}else{
	//管理界面
	if($waptop==1){$WPtext='取消总顶';}else{$WPtext='全区总顶';}
	if($top   ==1){$TPtext='取消置顶';}else{$TPtext='置顶帖子';}
	if($good  ==1){$GDtext='取消加精';}else{$GDtext='加精帖子';}
	$html = '
	<div class="title">
	帖子：《<a href="content.php?cid='.$gro.'&id='.$_GET['id'].'&">'.$title.'</a>》
	</div><div class="content">
	作者：'.user('nc','id',$userid).'
	</div>
	<div class="subtitle">作者管理</div>
	<div class="content">
	<a href="bbs_admin.php?action=mod&id='.$_GET['id'].'&">帖子修改</a>|
    <a href="bbs_admin.php?action=del&id='.$_GET['id'].'&">删除帖子</a></div>
	<div class="subtitle">版主管理</div>
	<div class="content">
    <a href="bbs_admin.php?action=top&id='.$_GET['id'].'&">'.$TPtext.'</a>|
    <a href="bbs_admin.php?action=waptop&id='.$_GET['id'].'&">'.$WPtext.'</a><br>
    <a href="bbs_admin.php?action=good&id='.$_GET['id'].'&">'.$GDtext.'</a>|
    <a href="bbs_admin.php?action=file&id='.$_GET['id'].'&">附件管理</a>
	</div><div class="subtitle">总版管理</div><div class="content">
	<a href="bbs_admin.php?action=m&cid='.$_GET['cid'].'&id='.$_GET['id'].'&">转移帖子</a>
	</div>
	';
	}
	mysql_close($link);
	//页面输出
	
	$html = head("帖子管理").'
	<div class="bbstitle">帖子管理</div>'.$html.'
	<div class="mylink">
	<a href="content.php?cid='.$gro.'&id='.$_GET['id'].'&">返回帖子</a>
	<a href="bbs_admin.php?cid='.$gro.'&id='.$_GET['id'].'&">帖子管理</a><br>
	<a href="list.php?cid='.$gro.'&">帖子列表</a>
	<a href="../index.php?">网站首页</a>
   '.footer(1);
	echo UBB($html,$wapid);
?>