<?php 
require "../Run.php";
require "../include/class/simage.class.php";
  
	if(LOGIN != 1){
		ehtml("您好，请登录后在进行发帖！");
		exit;
	}
	if(is_bbshei()){
		ehtml("您好，您已经被列入了黑名单无法进行发帖操作！");
		exit;
	}       
	echo head('发表帖子');
    if($_POST['add'] != NULL ){
	if($_POST['title'] == NULL || $_POST['content'] == NULL){
	   die("标题内容均不能为空！");
	   }
	if($_POST['file'] == 'yes'){
	if($_POST['fn'] == 0||$_POST['fn'] == NULL){
	 $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); 
		}
        date_default_timezone_set('PRC');
	    $D = date('YmdHis');
			$filetype    =  substr(strrchr($_FILES['userfile'.$i]['name'],"."),1);
			$newname     =  $wapid."_".$name."_".$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){
	     $fpath = NULL;
	}else{
            if(move_uploaded_file($_FILES['userfile'.$i]['tmp_name'],$UploadFile)){
	        echo "文件".$i."上传成功<br>";
	        $fpath = "uploads/".WAPID."/".$D1."/".$D2."/".$D3."/".$newname;
	        if($i==1){
			 $fh='';
			  }else{
			   $fh='|';
			  }
			}else{
	        echo "文件上传失败<br>";
	        $fpath=NULL;
	              }		  
		       }
            $file_url .= $fh.$fpath;
			$file_text .= $fh.$_POST['text'.$i];
       }		  
    }else{
	        $file_url = NULL;
	        $file_text = NULL;
	}		  
	/*文件处理结束,帖子信息处理开始*/
	$time = time();
	$v_t = time();
	$content = preg_replace('/(\r\n)|(\n)/','[br]',$_POST['content']);
	$sql="INSERT INTO bbs(title,userid,name,gro,content,time,pic,top,zip,siteid,good,waptop,zan,xi,nu,zipstr,endtime,view_time,viewType,viewValue)VALUES
	('{$_POST["title"]}','{$id}','{$nc}','{$_POST['gro']}','{$content}','{$time}','NULL','NULL','{$file_url}','{$wapid}','0','0','0','0','0','{$file_text}','{$time}','{$v_t}','{$_POST['viewType']}','{$_POST['value']}')";
	$article = mysql_query($sql);
    if($article && mysql_affected_rows()>0){
	$value = cre('+',40,$id);
	echo '<div class="title">帖子上传</div>';
	echo "<div class=\"tip\">";
    echo "帖子成功发布！系统已经奖励您40积分喽，当前您的积分余额是".$value."<br>";
	echo "请阅读检查";
	echo "</div>";
	}else{
      echo '<div class="tip">上传失败!请重试或者联系网站管理员！</div>';
	}
    echo '<div class="mylink">';
	echo "<a href=\"list.php?cid=".$_POST['gro']."\">返回帖子列表</a></div>";
	}else{
	if($_POST['file'] != NULL){
	    echo "
	    <div class=\"title\">发布帖子</div>
		<div class=\"content\"><div class=\"bbs_view\">
        <form method=\"post\" enctype=\"multipart/form-data\" action=\"add.php?\">      
        <br><input type=\"text\" name=\"title\"  value=\"".$_POST['title']."\">
        <input type=\"hidden\" name=\"userid\" value=\"".$id."\">
        <br>
        <input type=\"hidden\" name=\"gro\" value=\"".$_POST['gro']."\"/>
        <textarea name=\"content\">".$_POST['content']."</textarea><br>
		
		附件上传<br>";
		
	    if($_POST['fn_sel'] == NULL || $_POST['fn_sel'] == 1||$_POST['fn_sel'] == 0){
		    $fn=1;
	    }else{
			if($_POST['fn_add'] == NULL){
				$fn=1;
				}else{
			$fn=$_POST['fn_sel'];
			}
			}
	    for($i = 1;$i <= $fn;$i++){
			echo "<br><input type=\"file\" name=\"userfile".$i."\" id=\"fileField\" value=\"图片上传\"/>";
			echo "<br>文件说明<br><input value=\"\" type=\"text\" name=\"text".$i."\"><br>";
		}
		
    echo "<input type=\"hidden\" name=\"fn\" value=\"".$fn."\"/><input type=\"hidden\" name=\"file\" value=\"yes\"/><div class=\"subtitle\">上传更多附件</div>
	<input type=\"text\" name=\"fn_sel\" value=\"4\"/>
	<input type=\"submit\" name=\"fn_add\" value=\"确定\" ><br>";
	echo "<select name=\"viewType\">";
	echo "<option value=\"\">[1]普通帖子权限</option>";
	echo "<option value=\"noLoginStop\">[2]浏览者必须登录查看</option>";
	echo "<option value=\"creMin\">[3]金币必须大于指定值</option>";
	echo "<option value=\"huiFu\">[4]浏览者必须回复帖子</option>";
	echo "<option value=\"buy\">[5]付费可见</option>";
	echo "<option value=\"vip\">[6]VIP可见</option>";
	echo"</select>";
	echo '<br>[帖子权限选择3时请输入此固定值]<br><input type="text" value="" name="value"><br>';
	echo "<input type=\"submit\" value=\"写好了，马上发布\" name=\"add\" ><br>
	</form></div>";
	echo "<div class=\"mylink\"><a href=\"list.php?cid=".$_GET['cid']."\">返回上级</a><a href=\"../index.php?\">网站首页</a>
	</div>";
	
	}else{
    echo "
	<div class=\"title\">发布帖子</div>
	<div class=\"content\"><div class=\"bbs_view\">
    <form method=\"post\"  action=\"add.php?add=y\">      
    <br><input type=\"text\" name=\"title\"  value=\"标题\">
    <input type=\"hidden\" name=\"gro\" value=\"".$_GET['cid']."\"/><br>
    <textarea name=\"content\">帖子内容</textarea><br><input type=\"submit\" value=\"添加附件\" name=\"file\"><br>";
	echo "<select name=\"viewType\">";
	echo "<option value=\"\">[1]普通帖子权限</option>";
	echo "<option value=\"noLoginStop\">[2]浏览者必须登录查看</option>";
	echo "<option value=\"creMin\">[3]金币必须大于指定值</option>";
	echo "<option value=\"huiFu\">[4]浏览者必须回复帖子</option>";
	echo "<option value=\"buy\">[5]付费可见</option>";
	echo "<option value=\"vip\">[5]VIP可见</option>";
	echo"</select>";
	echo '<br>[帖子权限选择3时请输入此固定值]<br><input type="text" value="" name="value"><br>';
	echo "<input type=\"submit\" value=\"写好了，马上发布\" name=\"add\" ><br>
	</form></div></div>";
	echo "<div class=\"mylink\">
	<a href=\"list.php?cid=".$_GET['cid']."\">返回上级</a><a href=\"../index.php?\">网站首页</a></div>";
			}
		}
	  echo footer(1);
?>	         