<?php
require_once('header.php');
require_once("TnCode.class.php");
require_once('check_user.php');

include_once("classes/images.php");  //生成缩略图
include_once("common/watermark.php");//图片添加水印

$msg_title    = flc_request(trim($_POST["msg_title"]));
$msg_downurl  = flc_request(trim($_POST["msg_downurl"]));
$msg_demourl  = flc_request(trim($_POST["msg_demourl"]));
$smallname    = intval(trim($_POST["smallname"]));
$msg_integral = intval(trim($_POST["msg_integral"]));
$msg_content  = flc_request(trim($_POST["msg_content"]));

if ($msg_starttime=="") $msg_starttime=$getnow;
if ($msg_endtime=="") $msg_endtime="2099-11-11 00:00:00";

$msg_downurl = strtolower($msg_downurl); //转成小写
$msg_demourl = strtolower($msg_demourl); //转成小写

$ip=GetUserIP(); 

//滑动验证
if ($_SESSION['tncode_check']!='ok'){
   $response_arr = array("status" => 0,"message"  => '验证未通过,请重新验证!');
   echo json_encode($response_arr);
   exit;
}

unset($_SESSION['tncode_check']);

$sql="select * from {$tablepre}message where msg_title='".$msg_title."'";
$row   = $db->get_row($sql);

if (!empty($row)){
   $response_arr = array("status" => 0,"message"  => $msg_title.'您发布的资料已存在!');
   echo json_encode($response_arr);
   exit;
}else{

    $Folder=date("Ym",time());                      //按月生成文件夹
	if(!file_exists ("upload/".$Folder)){
	    mkdir("upload/".$Folder,0777);   
	}
	
	$savedb_url = '/upload/'.$Folder.'/';            //文件入库URL
	$save_path  = 'upload/'.$Folder.'/';             //文件保存目录路径
	//$ext_arr    = array('gif', 'jpg', 'png');      //定义允许上传的文件扩展名	
	$max_size   = $site_filesize;                    //最大文件大小

    $file_name  = $_FILES['file']['name'];
	$tmp_name   = $_FILES['file']['tmp_name'];
	$file_size  = $_FILES['file']['size']/1024;      //字节

	$temp_arr   = explode(".", $file_name);          //获得文件扩展名
	$file_ext   = array_pop($temp_arr);              //返回 array 数组的最后一个单元,并将数组 array 的长度减一
	$file_ext   = trim($file_ext);
	$file_ext   = strtolower($file_ext);

	//检查文件大小
	if ($file_size > $site_filesize) {
		$response_arr = array("status" => 0,"message"  => '图片大小不能超过'.$file_size.'MB');
        echo json_encode($response_arr);
        exit;
	}
	
	if (in_array($file_ext, $ext_arr) === false) { //检查扩展名
		$response_arr = array("status" => 0,"message"  => '限传'.$ext_str.'类型');
        echo json_encode($response_arr);
        exit;
	}
	$file_path = $save_path . iconv("UTF-8","gb2312",$file_name);

	$pathArray = array("imgsmall");
	if(!file_exists($save_path."/imgsmall")){
		$createResult = createFilePath($save_path,$pathArray);
		if($createResult == false){	
			$response_arr = array("status" => 0,"message"  => '无法创建文件夹！');
            echo json_encode($response_arr);
            exit;
		}
	}
	
	try {
           
        $db->startTrans();

		$sql="insert into {$tablepre}message (msg_title,msg_content,msg_downurl,msg_demourl,msg_integral,msg_status,msg_poster,msg_starttime,msg_endtime) 
			 values('".$msg_title."','".$msg_content."','".$msg_downurl."','".$msg_demourl."','".$msg_integral."',3,'".$_SESSION["uid"]."','".$msg_starttime."','".$msg_endtime."')";
		$db->exec($sql);
		$msgid=$db->lastInsertId2();

		$db->exec("insert into {$tablepre}categorymng (category_id,msg_id) values('".$smallname."','".$msgid."')");
		$db->exec("insert into {$tablepre}attachmng (category_id,msg_id,attach_path,attach_size,attach_ext,thumbnail_path,index_box,addtime) 
						 values('".$smallname."','".$msgid."','".$savedb_url.$file_name."','".$file_size."','".$file_ext."',
						 '".$savedb_url."imgsmall/".$file_name."',1,'".$getnow."')");
		
		if (move_uploaded_file($tmp_name, $file_path)){

			$thumbnail = new ImageResize();	
			$thumbnail->resizeimage($save_path.$file_name,265,350,0,$save_path."imgsmall/".$file_name);    //缩略图片
			$thumbnail->resizeimage($save_path.$file_name,$width,$height,0,$save_path.$file_name);         //原始图片按原尺寸重新生成,防止图片木马

            $ip_addr = GetUserIP();
			$sql = "insert into {$tablepre}upload_log (uplod_type,scuser_id,ip_addr,action_file,upload_file,add_time) 
                        values(0,'".$_SESSION["a_uid"]."','".$ip_addr."','submit_pub.php','".$savedb_url.$file_name."','".$getnow."')";	   
	        $db->exec($sql,true);

		}else{
			throw new Exception('图片上传失败!');
		}
	   
	    $db->commit();
		
		$response_arr = array("status" => 1,"message"  => '您已发布成功了,信息审核中!');
		echo json_encode($response_arr);
		exit;
	
	} catch (Exception $e) {
		$db->rollback();
		$response_arr = array("status" => 0,"message"  => $e->getMessage());
		echo json_encode($response_arr);
        exit;
    }
}
?>