<?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_id       = intval($_POST["msg_id"]);
$msg_title    = flc_request(trim($_POST["msg_title"]));
$msg_downurl  = flc_request(trim($_POST["msg_downurl"]));
$msg_demourl  = flc_request(trim($_POST["msg_demourl"]));
$category_id  = intval(trim($_POST["smallname"]));
$msg_integral = intval(trim($_POST["msg_integral"]));
$msg_content  = flc_request(trim($_POST["msg_content"]));

$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']);


	try {
           
        $db->startTrans();

		$sql="update {$tablepre}message set msg_title='".$msg_title."', msg_content='".$msg_content."',msg_demourl='".$msg_demourl."', msg_downurl='".$msg_downurl."', msg_integral='".$msg_integral."', msg_status=3 where msg_id=".$msg_id."";
	    $result = $db->exec($sql,true);
		if (!$result){
            throw new Exception('数据更新失败!');
		}

		if (empty($_FILES)){
		
		   //1.如果没有重新上传图片就取原有图片,然后重新插入图片
		   $strsql  ="select * from {$tablepre}attachmng where msg_id='".$msg_id."'";
		   $attach  =$db->get_row($strsql);
		   $attach_path = $attach['attach_path'];
		   $attach_size = $attach['attach_size'];
		   $attach_ext  = $attach['attach_ext'];
		   $thumbnail_path = $attach['thumbnail_path'];

		   $db->exec("delete from {$tablepre}categorymng where msg_id='".$msg_id."'");
		   $db->exec("delete from {$tablepre}attachmng where msg_id='".$msg_id."'"); 

		   $db->exec("insert into {$tablepre}categorymng (category_id,msg_id) values('".$category_id."','".$msg_id."')");
		   $db->exec("insert into {$tablepre}attachmng (category_id,msg_id,attach_path,attach_size,attach_ext,thumbnail_path,index_box,addtime) 
					 values('".$category_id."','".$msg_id."','".$attach_path."','".$attach_size."','".$attach_ext."','".$thumbnail_path."',1,'".$getnow."')");

		   $response_arr = array("status" => 1,"message"  => '修改成功,本次没有修收原有的图片!');

	    }else{
           
		   //2.如果有上传新的图片就取新的图片信息,然后重新插入图片
			$width      = intval($_POST["width"]);         //图片宽度
			$height     = intval($_POST["height"]);        //图片高度

			$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;
				}
			}

			/*删除原来已上传的图片*/
			include_once("classes/fileclass.php");
			$file = new fileoperate();  
			$sql1="select attach_path,thumbnail_path from {$tablepre}attachmng where msg_id=".$msg_id;
			$rm = $db->get_all($sql1);
			if(is_array($rm) && !empty($rm)){
			   foreach ($rm as $r){
				  $file->del_attach("./",$r['attach_path']);
				  $file->del_attach("./",$r['thumbnail_path']);
			   }
			}
			/**********************/

			$db->exec("delete from {$tablepre}categorymng where msg_id='".$msg_id."'");
			$db->exec("delete from {$tablepre}attachmng where msg_id='".$msg_id."'"); 

			$db->exec("insert into {$tablepre}categorymng (category_id,msg_id) values('".$category_id."','".$msg_id."')");
			$db->exec("insert into {$tablepre}attachmng (category_id,msg_id,attach_path,attach_size,attach_ext,thumbnail_path,index_box,addtime) 
							 values('".$category_id."','".$msg_id."','".$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."','edit_submit_pub.php','".$savedb_url.$file_name."','".$getnow."')";	   
				$db->exec($sql,true);

			}else{
				throw new Exception('图片上传失败!');
			}

			$response_arr = array("status" => 1,"message"  => '修改成功,原有图片也进行了修改!');
		}

	    $db->commit();
		
		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;
    }

?>