<?

	include("./config.inc.php");	//ตั้งค่าต่างๆของเวปบอร์ด
	include("./function.php");		//ฟังก์ชั่นที่ใช้ในเวปบอร์ด
    $key=$_GET['key'];
    $user=$_GET['user'];							
    $num=$_POST['num'];
    $title=$_POST['title'];
    $ndate=$_POST['ndate'];
    $detail=$_POST['detail'];
    $selpic=$_POST['selpic'];
    $data=$_POST['data'];

   $Upload_Img_name = $_FILES["Upload_Img"]["name"];
	$Upload_Img_type = $_FILES["Upload_Img"]["type"];	
    $Upload_Img_size = $_FILES["Upload_Img"]["size"];
 	$Upload_Img = $_FILES["Upload_Img"]["tmp_name"]; 

		$DirOK = 0;
		$DIR = opendir("./");
		while ($text = readdir($DIR)){
			if($text == $dataDir) $DirOK = 1;
		}
	
		if(!$DirOK) mkdir("./$dataDir/",0777);

    if($Upload_Img!="")
	{
	// ถ้าอนุญาตให้อัปโหลดรูปได้	(0 ตัวแรกนั่นคือการอัปโหลดรูปจากการตั้งกระทู้)
		$ImgDisplay="none";
		if($SendImage) 	$ImgDisplay = Send_Img(0, $num, $Upload_Img, $Upload_Img_type, $Upload_Img_size);
	}
	$num=sprintf("%d",$num);

 	$filename="new.txt";
	if(file_exists($filename))
	{
	 $dat01 = file($filename);
		for ($i=0 ; $i<sizeof($dat01) ; $i++)
		{		
                $split = explode("|X|",$dat01[$i]);
				$number=sprintf("%d",$split[0]);
	         	if($number==$num)
				   {
				        $ind=$i;
						$txtpic=$split[3];
				   }
	  }
	}		

    $title = CheckTag($title);
    //$detail = CheckTag($detail);
    $txtpic = CheckTag($txtpic);
    $data = CheckTag($data);
		
//echo $txtpic;

if($ImgDisplay=="")
{
    if($selpic!="1")
	   {   
      	 $strbuf ="$num|X|$title|X|$num.txt|X|$txtpic|X|$ndate|X|$data|X|\n";
		} 
	   else
	   {
       	 $strbuf ="$num|X|$title|X|$num.txt|X|0|X|$ndate|X|$data|X|$data|X|\n";
		} 
}else{
     	 $strbuf ="$num|X|$title|X|$num.txt|X|$ImgDisplay|X|$ndate|X|$data|X|\n";
     }
//echo ">> $strbuf  <br> txtpic=$txtpic <br>ImgDisplay=$ImgDisplay<br>selpic=$selpic"; return 0;
	
	$FILE=fopen($filename,"w+");
	flock($FILE,2);
	
		for ($i=0 ; $i<sizeof($dat01) ; $i++)
		{		
	         	if($i!=$ind)
				   {
				       fputs($FILE,$dat01[$i]);
					}
 			       else		
				   {
				      fputs($FILE,$strbuf);
					}
		}	

	fclose($FILE);
	
	$FILE=fopen("data/$num.txt","w+");
	flock($FILE,2);
	$detail = CheckTag($detail);
    fputs($FILE,$detail);
	fclose($FILE);
	
  header("Location: edit.php?key=$key&user=$user");		
		
?>
