<?php
include_once("inc/config.php");
include_once("inc/ftbbsdbclass.php");
include_once("inc/publicfun.php");
include_once("conn.php");
include_once("checkuser.php");

$query = "SELECT * FROM {$tablepre}ftbbsmb";
$ftbbsmb=makecache('ftbbsmb.html',$query,$expire);

$url=$ftbbsmb[10];
$upfiletype=$ftbbsmb[12];
$upfilesize=$ftbbsmb[13];
$uploadoff=$ftbbsmb[15];
$waterimg=$ftbbsmb[36];
$uploadvalue=$ftbbsmb[60];
$username=get_cookie('username');
$clubuser_id=get_cookie('clubuser_id');
if ($uploadvalue==0){
$topqs=1;
}
else{
$topqs=jingyuantoqs($clubuser_id,$uploadvalue);
}
$now=date("Y-m-d H:i:s");
$filename=ftbbs_request($_GET["filename"]);
//用户判断
$userlogin=checkuserlogin();
if ($userlogin=="N"){
   echo "你还没有登录或非法操作,<a href=FtbbsUpFile.php?filename=".$filename.">重新上传</a>";
   exit;
}
if ($topqs==0){
   echo "<font color=red>你的积分太低无法上传附件</font>";
   exit;
}
?>
<html><title>保存上传(飞天论坛)</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script language=javascript src="inc/cookies.js"></script>
<script>
var stylecook = getCookie("STYLESHOW");
var style;
var strstyle;
if(stylecook=="")
{
 stylecook=1;
}
else
{
 stylecook++;
}
style="main0"+stylecook+".css";
strstyle="<link href=\"css/"+style+"\"  rel=\"stylesheet\" type=\"text/css\"> ";
document.write(strstyle);
</script>
<script type="text/javascript" src="fteditor/iframe.js"></script>
<script language="javascript">
function insertnl(filetype,imgsrc,id,url,fn) {
	filetype=filetype.toLowerCase( );
    if (filetype=='gif'||filetype=='jpg'||filetype=='png'||filetype=='bmp'||filetype=='jpeg'){
	 parent.document.getElementById("bbs_img_display").value=1;
     nl='[IMG]'+imgsrc+'[/IMG]';
	}else if(filetype=='swf'){
	 parent.document.getElementById("bbs_img_display").value=2;
	 nl='[flash=400,300]'+imgsrc+'[/flash]';
	}else if(filetype=='flv'){
	 parent.document.getElementById("bbs_img_display").value=4;
	 nl='[flv=500,400,false]'+imgsrc+'[/flv]';
	}else{
	 parent.document.getElementById("bbs_img_display").value=3;
	 nl='[img]images/file.gif[/img][b]附件:[/b][url=down.php?id='+id+']'+fn+'[/url]';
	}
	parent.document.getElementById("bbs_img").value=filetype;
	var CurrentMode=parent.document.getElementById("CurrentMode").value;
	if (CurrentMode=='code'){
	alert('代码模式下不允许插入附件');
	return false;
	}else{
	var element = parent.document.createElement("span");
	element.appendChild(parent.document.createTextNode(nl));
	FtSelection();
	FtInsertItem(element);
	}
}
</script>
<body topmargin="0" class="maintbtr3">
<?
include_once("inc/upload_fun.php");
//按时间创建文件夹
if ($ftbbsmb[7]=="M"){
 $inFolder=date("Ym",time());
 }
else{
 $inFolder=date("Ymd",time());
}
if(!file_exists ($inFolder)){
  mkdir($inFolder,0777);   
}

if(isset($_FILES) && !empty($_FILES['userfile']) && $_FILES['userfile']['size']>0) 
{ 
	if (checkfile($_FILES['userfile']['tmp_name'])==1){
	echo "您上传的文件有问题!&nbsp;&nbsp;<a href=FtbbsUpFile.php?filename=".$filename.">重新上传</a>";
	exit;
	}
	$filetype=$_FILES['userfile']['type'];
	$size=$_FILES['userfile']['size'];
	$strFileName=$_FILES['userfile']['name'];
    $arrName  = explode(".",$strFileName);
    $intName = sizeof($arrName);
    $extName = strtolower($arrName[$intName-1]);
	if ($filetype=="text/plain"){
	echo "您上传的文件有问题!&nbsp;&nbsp;<a href=FtbbsUpFile.php?filename=".$filename.">重新上传</a>";
	exit;
	}
    if (!in_array($extName,explode("|", $upfiletype))){
	echo "不允许上传".$extName."类型的文件&nbsp;&nbsp;<a href=FtbbsUpFile.php?filename=".$filename.">重新上传</a>";
	exit;
    }
	if ($size>$upfilesize){
	echo "上传的文件最大限制为:".$upfilesize."字节&nbsp;&nbsp;<a href=FtbbsUpFile.php?filename=".$filename.">重新上传</a>";
	exit;
	}
    //$uploadfile = $inFolder."/".time()."_".$strFileName;
	$fn=time().".".$extName;
	$uploadfile = $inFolder."/".$fn;
    //if (copy($_FILES['userfile']['tmp_name'], $uploadfile)) //2008-7-9改成以下方法
	if(@copy($_FILES['userfile']['tmp_name'], $uploadfile) || (function_exists('move_uploaded_file') && @move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile))) 
    { 
		if ($uploadoff==1){
		   if ($waterimg==0 && $extName=="jpg"){ //文字水印 
              imageWaterMark($uploadfile,5,"",$url,5,"#FFFFFF"); 
		   }
		   elseif ($extName=="jpg"){//JPG格式才中图片水印 
              $waterImage="images/tmlogo.gif";//水印图片路径
              imageWaterMark($uploadfile,9,$waterImage);
		   }
		 }
		//图片相关信息写入数据库
		//$inFolder/$filename 080412改成$filename
		@unlink($_FILES['userfile']['tmp_name']);
		$insql = "insert into {$tablepre}ftbbs_upload (upload_pic,indate,upload_size,upload_ext,userid,upload_htm,clubuser_id) values('$uploadfile','$now','$size','$extName','$username','$filename','$clubuser_id')";
        mysql_query($insql);
		$id = mysql_insert_id();
		echo $uploadfile." &nbsp;<a href=\"javascript:;\" title=\"插入附件到编辑器中\" onclick=\"insertnl('".$extName."','".$uploadfile."','".$id."','".$url."','".$fn."');\">插入</a>";
        echo "&nbsp;&nbsp;<a href=FtbbsUpFile.php?filename=".$filename.">继续上传</a>";
		//}
    } 
    else 
    { 
        echo "<font color=red>上传文件不能为空</font>&nbsp;&nbsp;<a href=FtbbsUpFile.php?filename=".$filename.">重新上传</a>"; 
    } 
}
?>
</body> 
</html>